2 This file is part of GNUnet.
3 (C) 2001-2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "gnunet_dht_service.h"
54 #include "gnunet_statistics_service.h"
55 #include "gnunet_regex_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);
1250 /******************************************************************************/
1251 /************************ REGEX INTEGRATION ****************************/
1252 /******************************************************************************/
1255 * Cancel a mesh regex search and free resources.
1258 regex_cancel_search (struct MeshRegexSearchInfo *regex_search)
1260 DEBUG_REGEX ("Search for %s canelled.\n", regex_search->description);
1261 GNUNET_REGEX_search_cancel (regex_search->search_handle);
1262 if (0 < regex_search->n_peers)
1263 GNUNET_free (regex_search->peers);
1264 if (GNUNET_SCHEDULER_NO_TASK != regex_search->timeout)
1266 GNUNET_SCHEDULER_cancel(regex_search->timeout);
1268 GNUNET_free (regex_search);
1273 * Function called if the connect attempt to a peer found via
1274 * connect_by_string times out. Try to connect to another peer, if any.
1275 * Otherwise try to reconnect to the same peer.
1277 * @param cls Closure (info about regex search).
1278 * @param tc TaskContext.
1281 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1283 struct MeshRegexSearchInfo *info = cls;
1284 struct MeshPeerInfo *peer_info;
1288 DEBUG_REGEX ("Regex connect timeout\n");
1289 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1290 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1292 DEBUG_REGEX (" due to shutdown\n");
1297 DEBUG_REGEX (" timed out: %u\n", old);
1299 if (0 < info->n_peers)
1301 // Select next peer, put current in that spot.
1302 id = info->peers[info->i_peer];
1303 info->peers[info->i_peer] = info->peer;
1304 info->i_peer = (info->i_peer + 1) % info->n_peers;
1308 // Try to connect to same peer again.
1311 DEBUG_REGEX (" trying: %u\n", id);
1313 peer_info = peer_info_get_short(id);
1314 tunnel_add_peer (info->t, peer_info);
1316 tunnel_delete_peer (info->t, old);
1317 peer_info_connect (peer_info, info->t);
1318 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1319 ®ex_connect_timeout,
1321 DEBUG_REGEX ("Regex connect timeout END\n");
1326 * Function to process DHT string to regex matching.
1327 * Called on each result obtained for the DHT search.
1329 * @param cls Closure provided in GNUNET_REGEX_search.
1330 * @param id Peer providing a regex that matches the string.
1331 * @param get_path Path of the get request.
1332 * @param get_path_length Lenght of get_path.
1333 * @param put_path Path of the put request.
1334 * @param put_path_length Length of the put_path.
1337 regex_found_handler (void *cls,
1338 const struct GNUNET_PeerIdentity *id,
1339 const struct GNUNET_PeerIdentity *get_path,
1340 unsigned int get_path_length,
1341 const struct GNUNET_PeerIdentity *put_path,
1342 unsigned int put_path_length)
1344 struct MeshRegexSearchInfo *info = cls;
1345 struct MeshPeerPath *p;
1346 struct MeshPeerInfo *peer_info;
1348 DEBUG_REGEX ("Got regex results from DHT!\n");
1349 DEBUG_REGEX (" for %s\n", info->description);
1351 peer_info = peer_info_get (id);
1352 p = path_build_from_dht (get_path, get_path_length,
1353 put_path, put_path_length);
1354 path_add_to_peers (p, GNUNET_NO);
1357 tunnel_add_peer (info->t, peer_info);
1358 peer_info_connect (peer_info, info->t);
1359 if (0 == info->peer)
1361 info->peer = peer_info->id;
1365 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
1368 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1371 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1372 ®ex_connect_timeout,
1380 * Store the regular expression describing a local service into the DHT.
1382 * @param regex The regular expresion.
1385 regex_put (struct MeshRegexDescriptor *regex)
1387 DEBUG_REGEX (" regex_put (%s) start\n", regex->regex);
1388 if (NULL == regex->h)
1390 DEBUG_REGEX (" first put, creating DFA\n");
1391 regex->h = GNUNET_REGEX_announce (dht_handle,
1399 DEBUG_REGEX (" not first put, using cached data\n");
1400 GNUNET_REGEX_reannounce (regex->h);
1402 DEBUG_REGEX (" regex_put (%s) end\n", regex->regex);
1407 * Periodically announce what applications are provided by local clients
1410 * @param cls closure
1411 * @param tc task context
1414 regex_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1416 struct MeshClient *c = cls;
1419 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1420 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1422 DEBUG_REGEX ("Starting announce for regex\n");
1423 for (i = 0; i < c->n_regex; i++)
1424 regex_put (&c->regexes[i]);
1425 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1428 DEBUG_REGEX ("Finished announce for regex\n");
1432 /******************************************************************************/
1433 /************************ PERIODIC FUNCTIONS ****************************/
1434 /******************************************************************************/
1437 * Announce iterator over for each application provided by the peer
1439 * @param cls closure
1440 * @param key current key code
1441 * @param value value in the hash map
1442 * @return GNUNET_YES if we should continue to
1447 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1449 struct PBlock block;
1450 struct MeshClient *c;
1452 block.id = my_full_id;
1453 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1454 GNUNET_assert(NULL != c);
1455 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1456 if (0 == block.type)
1461 block.type = htonl (block.type);
1463 GNUNET_break (NULL !=
1464 GNUNET_DHT_put (dht_handle, key,
1465 dht_replication_level,
1466 GNUNET_DHT_RO_RECORD_ROUTE |
1467 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1468 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1470 (const char *) &block,
1471 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1472 app_announce_time, NULL, NULL));
1478 * Periodically announce what applications are provided by local clients
1481 * @param cls closure
1482 * @param tc task context
1485 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1487 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1489 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1493 DEBUG_DHT ("Starting PUT for apps\n");
1495 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1497 announce_applications_task =
1498 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1500 DEBUG_DHT ("Finished PUT for apps\n");
1505 * Periodically announce self id in the DHT
1507 * @param cls closure
1508 * @param tc task context
1511 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1513 struct PBlock block;
1515 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1517 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1521 * - Set data expiration in function of X
1522 * - Adapt X to churn
1524 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1526 block.id = my_full_id;
1527 block.type = htonl (0);
1528 GNUNET_DHT_put (dht_handle, /* DHT handle */
1529 &my_full_id.hashPubKey, /* Key to use */
1530 dht_replication_level, /* Replication level */
1531 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1532 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1533 sizeof (block), /* Size of the data */
1534 (const char *) &block, /* Data itself */
1535 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1536 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1537 NULL, /* Continuation */
1538 NULL); /* Continuation closure */
1540 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1544 /******************************************************************************/
1545 /****************** GENERAL HELPER FUNCTIONS ************************/
1546 /******************************************************************************/
1549 * Decrements the reference counter and frees all resources if needed
1551 * @param mesh_data Data Descriptor used in a multicast message.
1552 * Freed no longer needed (last message).
1555 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1557 if (0 == --(mesh_data->reference_counter))
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1560 GNUNET_free (mesh_data->data);
1561 GNUNET_free (mesh_data);
1567 * Check if client has registered with the service and has not disconnected
1569 * @param client the client to check
1571 * @return non-NULL if client exists in the global DLL
1573 static struct MeshClient *
1574 client_get (struct GNUNET_SERVER_Client *client)
1576 struct MeshClient *c;
1581 if (c->handle == client)
1590 * Checks if a given client has subscribed to certain message type
1592 * @param message_type Type of message to check
1593 * @param c Client to check
1595 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1597 * FIXME: use of crypto_hash slows it down
1598 * The hash function alone takes 8-10us out of the ~55us for the whole
1599 * process of retransmitting the message from one local client to another.
1600 * Find faster implementation!
1603 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1605 struct GNUNET_HashCode hc;
1607 if (NULL == c->types)
1610 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1611 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1616 * Check whether client wants traffic from a tunnel.
1618 * @param c Client to check.
1619 * @param t Tunnel to be found.
1621 * @return GNUNET_YES if client knows tunnel.
1623 * TODO look in client hashmap
1626 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1630 for (i = 0; i < t->nclients; i++)
1631 if (t->clients[i] == c)
1638 * Check whether client has been informed about a tunnel.
1640 * @param c Client to check.
1641 * @param t Tunnel to be found.
1643 * @return GNUNET_YES if client knows tunnel.
1645 * TODO look in client hashmap
1648 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1652 for (i = 0; i < t->nignore; i++)
1653 if (t->ignore[i] == c)
1655 return client_wants_tunnel(c, t);
1660 * Marks a client as uninterested in traffic from the tunnel, updating both
1661 * client and tunnel to reflect this.
1663 * @param c Client that doesn't want traffic anymore.
1664 * @param t Tunnel which should be ignored.
1666 * FIXME when to delete an incoming tunnel?
1669 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1671 struct GNUNET_HashCode hash;
1673 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1674 GNUNET_break (GNUNET_YES ==
1675 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1677 GNUNET_break (GNUNET_YES ==
1678 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1679 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1680 tunnel_delete_active_client (t, c);
1681 GNUNET_array_append (t->ignore, t->nignore, c);
1686 * Deletes a tunnel from a client (either owner or destination). To be used on
1687 * tunnel destroy, otherwise, use client_ignore_tunnel.
1689 * @param c Client whose tunnel to delete.
1690 * @param t Tunnel which should be deleted.
1693 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1695 struct GNUNET_HashCode hash;
1699 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1700 GNUNET_assert (GNUNET_YES ==
1701 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1707 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1709 GNUNET_assert (GNUNET_YES ==
1710 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1714 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1722 * Notify the owner of a tunnel that a peer has disconnected.
1724 * @param c Client (owner of tunnel).
1725 * @param t Tunnel this message is about.
1726 * @param peer_id Short ID of the disconnected peer.
1729 client_notify_peer_disconnected (struct MeshClient *c,
1730 struct MeshTunnel *t,
1731 GNUNET_PEER_Id peer_id)
1733 struct GNUNET_MESH_PeerControl msg;
1735 if (NULL == t->owner || NULL == nc)
1738 msg.header.size = htons (sizeof (msg));
1739 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1740 msg.tunnel_id = htonl (t->local_tid);
1741 GNUNET_PEER_resolve (peer_id, &msg.peer);
1742 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1743 &msg.header, GNUNET_NO);
1748 * Send the message to all clients that have subscribed to its type
1750 * @param msg Pointer to the message itself
1751 * @param payload Pointer to the payload of the message.
1752 * @param t The tunnel to whose clients this message goes.
1754 * @return number of clients this message was sent to
1757 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1758 const struct GNUNET_MessageHeader *payload,
1759 struct MeshTunnel *t)
1761 struct MeshClient *c;
1762 MESH_TunnelNumber *tid;
1765 char cbuf[htons (msg->size)];
1767 type = ntohs (payload->type);
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1770 GNUNET_MESH_DEBUG_M2S (type));
1772 memcpy (cbuf, msg, sizeof (cbuf));
1773 switch (htons (msg->type))
1775 struct GNUNET_MESH_Unicast *uc;
1776 struct GNUNET_MESH_Multicast *mc;
1777 struct GNUNET_MESH_ToOrigin *to;
1779 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1780 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1783 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1784 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1787 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1788 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1796 for (count = 0, c = clients; c != NULL; c = c->next)
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1799 if (client_is_subscribed (type, c))
1801 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1805 *tid = htonl (t->local_tid);
1809 if (GNUNET_NO == client_knows_tunnel (c, t))
1811 /* This client doesn't know the tunnel */
1812 struct GNUNET_MESH_TunnelNotification tmsg;
1813 struct GNUNET_HashCode hash;
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1816 tmsg.header.size = htons (sizeof (tmsg));
1817 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1818 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1819 tmsg.tunnel_id = htonl (t->local_tid_dest);
1821 if (GNUNET_YES == t->speed_min)
1822 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
1823 if (GNUNET_YES == t->nobuffer)
1824 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1825 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1826 &tmsg.header, GNUNET_NO);
1827 tunnel_add_client (t, c);
1828 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1830 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1831 c->incoming_tunnels, &hash, t,
1832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1834 *tid = htonl (t->local_tid_dest);
1837 /* Check if the client wants to get traffic from the tunnel */
1838 if (GNUNET_NO == client_wants_tunnel(c, t))
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1842 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1843 (struct GNUNET_MessageHeader
1844 *) cbuf, GNUNET_NO);
1853 * Notify the client that owns the tunnel that a peer has connected to it
1854 * (the requested path to it has been confirmed).
1856 * @param t Tunnel whose owner to notify
1857 * @param id Short id of the peer that has connected
1860 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1862 struct GNUNET_MESH_PeerControl pc;
1864 if (NULL == t->owner || GNUNET_YES == t->destroy)
1867 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1868 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1869 pc.tunnel_id = htonl (t->local_tid);
1870 GNUNET_PEER_resolve (id, &pc.peer);
1871 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1877 * Notify all clients (not depending on registration status) that the incoming
1878 * tunnel is no longer valid.
1880 * @param t Tunnel that was destroyed.
1883 send_clients_tunnel_destroy (struct MeshTunnel *t)
1885 struct GNUNET_MESH_TunnelMessage msg;
1887 msg.header.size = htons (sizeof (msg));
1888 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1889 msg.tunnel_id = htonl (t->local_tid_dest);
1890 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1895 * Notify clients of tunnel disconnections, if needed.
1896 * In case the origin disconnects, the destination clients get a tunnel destroy
1897 * notification. If the last destination disconnects (only one remaining client
1898 * in tunnel), the origin gets a (local ID) peer disconnected.
1899 * Note that the function must be called BEFORE removing the client from
1902 * @param t Tunnel that was destroyed.
1903 * @param c Client that disconnected.
1906 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1912 struct GNUNET_MESH_TunnelMessage msg;
1914 msg.header.size = htons (sizeof (msg));
1915 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1916 msg.tunnel_id = htonl (t->local_tid_dest);
1917 for (i = 0; i < t->nclients; i++)
1918 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1919 &msg.header, GNUNET_NO);
1921 // FIXME when to disconnect an incoming tunnel?
1922 else if (1 == t->nclients && NULL != t->owner)
1924 struct GNUNET_MESH_PeerControl msg;
1926 msg.header.size = htons (sizeof (msg));
1927 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1928 msg.tunnel_id = htonl (t->local_tid);
1929 msg.peer = my_full_id;
1930 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1931 &msg.header, GNUNET_NO);
1937 * Iterator over all the peers to remove the oldest not-used entry.
1939 * @param cls Closure (unsued).
1940 * @param key ID of the peer.
1941 * @param value Peer_Info of the peer.
1946 peer_info_timeout (void *cls,
1947 const struct GNUNET_HashCode *key,
1954 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1955 * and insert it in the appropiate structures if the peer is not known yet.
1957 * @param peer Full identity of the peer.
1959 * @return Existing or newly created peer info.
1961 static struct MeshPeerInfo *
1962 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1964 struct MeshPeerInfo *peer_info;
1966 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1967 if (NULL == peer_info)
1970 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1971 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1973 GNUNET_CONTAINER_multihashmap_iterate (peers,
1977 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1978 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1979 peer_info->id = GNUNET_PEER_intern (peer);
1981 peer_info->last_contact = GNUNET_TIME_absolute_get();
1988 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1989 * and insert it in the appropiate structures if the peer is not known yet.
1991 * @param peer Short identity of the peer.
1993 * @return Existing or newly created peer info.
1995 static struct MeshPeerInfo *
1996 peer_info_get_short (const GNUNET_PEER_Id peer)
1998 struct GNUNET_PeerIdentity id;
2000 GNUNET_PEER_resolve (peer, &id);
2001 return peer_info_get (&id);
2006 * Iterator to remove the tunnel from the list of tunnels a peer participates
2009 * @param cls Closure (tunnel info)
2010 * @param key GNUNET_PeerIdentity of the peer (unused)
2011 * @param value PeerInfo of the peer
2013 * @return always GNUNET_YES, to keep iterating
2016 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2018 struct MeshTunnel *t = cls;
2019 struct MeshPeerInfo *peer = value;
2022 for (i = 0; i < peer->ntunnels; i++)
2025 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2028 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2029 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2038 * Core callback to write a pre-constructed data packet to core buffer
2040 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2041 * @param size Number of bytes available in buf.
2042 * @param buf Where the to write the message.
2044 * @return number of bytes written to buf
2047 send_core_data_raw (void *cls, size_t size, void *buf)
2049 struct MeshTransmissionDescriptor *info = cls;
2050 struct GNUNET_MessageHeader *msg;
2053 GNUNET_assert (NULL != info);
2054 GNUNET_assert (NULL != info->mesh_data);
2055 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2056 total_size = ntohs (msg->size);
2058 if (total_size > size)
2063 memcpy (buf, msg, total_size);
2064 data_descriptor_decrement_rc (info->mesh_data);
2071 * Sends an already built non-multicast message to a peer,
2072 * properly registrating all used resources.
2074 * @param message Message to send. Function makes a copy of it.
2075 * @param peer Short ID of the neighbor whom to send the message.
2076 * @param t Tunnel on which this message is transmitted.
2079 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2080 const struct GNUNET_PeerIdentity *peer,
2081 struct MeshTunnel *t)
2083 struct MeshTransmissionDescriptor *info;
2084 struct MeshPeerInfo *neighbor;
2085 struct MeshPeerPath *p;
2089 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2091 size = ntohs (message->size);
2092 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2093 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2094 info->mesh_data->data = GNUNET_malloc (size);
2095 memcpy (info->mesh_data->data, message, size);
2096 type = ntohs(message->type);
2099 struct GNUNET_MESH_Unicast *m;
2100 struct GNUNET_MESH_ToOrigin *to;
2102 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2103 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2104 m->ttl = htonl (ntohl (m->ttl) - 1);
2106 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2107 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2109 to->pid = htonl(t->bck_pid);
2111 info->mesh_data->data_len = size;
2112 info->mesh_data->reference_counter = 1;
2113 info->mesh_data->total_out = 1;
2114 neighbor = peer_info_get (peer);
2115 for (p = neighbor->path_head; NULL != p; p = p->next)
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2126 " %s IS NOT DIRECTLY CONNECTED\n",
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 for (p = neighbor->path_head; NULL != p; p = p->next)
2133 struct GNUNET_PeerIdentity debug_id;
2136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2137 " path with %u hops through:\n",
2139 for (i = 0; i < p->length; i++)
2141 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2144 i, GNUNET_i2s(&debug_id));
2148 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2150 " no direct connection to %s\n",
2152 GNUNET_free (info->mesh_data->data);
2153 GNUNET_free (info->mesh_data);
2157 info->peer = neighbor;
2158 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2169 * Sends a CREATE PATH message for a path to a peer, properly registrating
2170 * all used resources.
2172 * @param peer PeerInfo of the final peer for whom this path is being created.
2173 * @param p Path itself.
2174 * @param t Tunnel for which the path is created.
2177 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2178 struct MeshTunnel *t)
2180 struct GNUNET_PeerIdentity id;
2181 struct MeshPathInfo *path_info;
2182 struct MeshPeerInfo *neighbor;
2188 p = tree_get_path_to_peer (t->tree, peer->id);
2195 for (i = 0; i < p->length; i++)
2197 if (p->peers[i] == myid)
2200 if (i >= p->length - 1)
2206 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2208 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2209 path_info->path = p;
2211 neighbor = peer_info_get (&id);
2212 path_info->peer = neighbor;
2213 queue_add (path_info,
2214 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2215 sizeof (struct GNUNET_MESH_ManipulatePath) +
2216 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2223 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2225 * @param t Tunnel whose path to destroy.
2226 * @param destination Short ID of the peer to whom the path to destroy.
2229 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2231 struct MeshPeerPath *p;
2234 p = tree_get_path_to_peer (t->tree, destination);
2240 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2241 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2243 struct GNUNET_MESH_ManipulatePath *msg;
2244 struct GNUNET_PeerIdentity *pi;
2248 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2249 msg->header.size = htons (size);
2250 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2251 msg->tid = htonl (t->id.tid);
2252 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2253 for (i = 0; i < p->length; i++)
2255 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2257 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2264 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2266 * @param t Tunnel which to confirm.
2269 send_path_ack (struct MeshTunnel *t)
2271 struct MeshTransmissionDescriptor *info;
2272 struct GNUNET_PeerIdentity id;
2273 GNUNET_PEER_Id peer;
2275 peer = tree_get_predecessor (t->tree);
2276 GNUNET_PEER_resolve (peer, &id);
2277 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2278 info->origin = &t->id;
2279 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2280 GNUNET_assert (NULL != info->peer);
2283 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2284 sizeof (struct GNUNET_MESH_PathACK),
2291 * Try to establish a new connection to this peer.
2292 * Use the best path for the given tunnel.
2293 * If the peer doesn't have any path to it yet, try to get one.
2294 * If the peer already has some path, send a CREATE PATH towards it.
2296 * @param peer PeerInfo of the peer.
2297 * @param t Tunnel for which to create the path, if possible.
2300 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2302 struct MeshPeerPath *p;
2303 struct MeshPathInfo *path_info;
2305 if (NULL != peer->path_head)
2307 p = tree_get_path_to_peer (t->tree, peer->id);
2314 // FIXME always send create path to self
2317 send_create_path (peer, p, t);
2321 struct GNUNET_HashCode hash;
2324 send_client_peer_connected (t, myid);
2325 t->local_tid_dest = next_local_tid++;
2326 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2329 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2330 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2337 else if (NULL == peer->dhtget)
2339 struct GNUNET_PeerIdentity id;
2341 GNUNET_PEER_resolve (peer->id, &id);
2342 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2343 path_info->peer = peer;
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2347 peer->dhtgetcls = path_info;
2348 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2349 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2350 &id.hashPubKey, /* key to search */
2351 dht_replication_level, /* replication level */
2352 GNUNET_DHT_RO_RECORD_ROUTE |
2353 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2354 NULL, /* xquery */ // FIXME BLOOMFILTER
2355 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2356 &dht_get_id_handler, path_info);
2358 /* Otherwise, there is no path but the DHT get is already started. */
2363 * Task to delay the connection of a peer
2365 * @param cls Closure (path info with tunnel and peer to connect).
2366 * Will be free'd on exection.
2367 * @param tc TaskContext
2370 peer_info_connect_task (void *cls,
2371 const struct GNUNET_SCHEDULER_TaskContext *tc)
2373 struct MeshPathInfo *path_info = cls;
2375 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2377 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2382 peer_info_connect (path_info->peer, path_info->t);
2388 * Destroy the peer_info and free any allocated resources linked to it
2390 * @param pi The peer_info to destroy.
2392 * @return GNUNET_OK on success
2395 peer_info_destroy (struct MeshPeerInfo *pi)
2397 struct GNUNET_PeerIdentity id;
2398 struct MeshPeerPath *p;
2399 struct MeshPeerPath *nextp;
2401 GNUNET_PEER_resolve (pi->id, &id);
2402 GNUNET_PEER_change_rc (pi->id, -1);
2405 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2408 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2409 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2411 if (NULL != pi->dhtget)
2413 GNUNET_DHT_get_stop (pi->dhtget);
2414 GNUNET_free (pi->dhtgetcls);
2420 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2424 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2426 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2434 * Remove all paths that rely on a direct connection between p1 and p2
2435 * from the peer itself and notify all tunnels about it.
2437 * @param peer PeerInfo of affected peer.
2438 * @param p1 GNUNET_PEER_Id of one peer.
2439 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2442 * TODO: optimize (see below)
2445 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2448 struct MeshPeerPath *p;
2449 struct MeshPeerPath *aux;
2450 struct MeshPeerInfo *peer_d;
2452 unsigned int destroyed;
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2459 p = peer->path_head;
2463 for (i = 0; i < (p->length - 1); i++)
2465 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2466 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2468 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2479 for (i = 0; i < peer->ntunnels; i++)
2481 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2485 * Problem: one or more peers have been deleted from the tunnel tree.
2486 * We don't know who they are to try to add them again.
2487 * We need to try to find a new path for each of the disconnected peers.
2488 * Some of them might already have a path to reach them that does not
2489 * involve p1 and p2. Adding all anew might render in a better tree than
2490 * the trivial immediate fix.
2492 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2493 * its children will be reachable trough him.
2495 peer_d = peer_info_get_short (d);
2498 for (p = peer_d->path_head; NULL != p; p = p->next)
2500 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2508 /* No callback, as peer will be already disconnected and a connection
2509 * scheduled by tunnel_notify_connection_broken.
2511 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2515 peer_info_connect (peer_d, peer->tunnels[i]);
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2523 * Add the path to the peer and update the path used to reach it in case this
2526 * @param peer_info Destination peer to add the path to.
2527 * @param path New path to add. Last peer must be the peer in arg 1.
2528 * Path will be either used of freed if already known.
2529 * @param trusted Do we trust that this path is real?
2532 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2535 struct MeshPeerPath *aux;
2539 if ((NULL == peer_info) || (NULL == path))
2542 path_destroy (path);
2545 if (path->peers[path->length - 1] != peer_info->id)
2548 path_destroy (path);
2551 if (path->length <= 2 && GNUNET_NO == trusted)
2553 /* Only allow CORE to tell us about direct paths */
2554 path_destroy (path);
2557 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2558 for (l = 1; l < path->length; l++)
2560 if (path->peers[l] == myid)
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2563 for (l2 = 0; l2 < path->length - l; l2++)
2565 path->peers[l2] = path->peers[l + l2];
2570 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2575 struct GNUNET_PeerIdentity id;
2577 GNUNET_PEER_resolve (peer_info->id, &id);
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2579 path->length, GNUNET_i2s (&id));
2582 l = path_get_length (path);
2589 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2590 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2592 l2 = path_get_length (aux);
2595 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2596 peer_info->path_tail, aux, path);
2601 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2603 path_destroy (path);
2608 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2615 * Add the path to the origin peer and update the path used to reach it in case
2616 * this is the shortest.
2617 * The path is given in peer_info -> destination, therefore we turn the path
2618 * upside down first.
2620 * @param peer_info Peer to add the path to, being the origin of the path.
2621 * @param path New path to add after being inversed.
2622 * @param trusted Do we trust that this path is real?
2625 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2626 struct MeshPeerPath *path, int trusted)
2629 peer_info_add_path (peer_info, path, trusted);
2634 * Function called if the connection to the peer has been stalled for a while,
2635 * possibly due to a missed ACK. Poll the peer about its ACK status.
2637 * @param cls Closure (cinfo).
2638 * @param tc TaskContext.
2641 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2643 struct MeshTunnelChildInfo *cinfo = cls;
2644 struct GNUNET_MESH_Poll msg;
2645 struct GNUNET_PeerIdentity id;
2646 struct MeshTunnel *t;
2648 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2649 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2655 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2656 msg.header.size = htons (sizeof (msg));
2657 msg.tid = htonl (t->id.tid);
2658 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2659 msg.last_ack = htonl (cinfo->fwd_ack);
2661 GNUNET_PEER_resolve (cinfo->id, &id);
2662 send_prebuilt_message (&msg.header, &id, cinfo->t);
2663 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2665 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2666 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2667 &tunnel_poll, cinfo);
2672 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2673 * to obtain a local peer -> destination path and interning the peer ids.
2675 * @return Newly allocated and created path
2677 static struct MeshPeerPath *
2678 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2679 unsigned int get_path_length,
2680 const struct GNUNET_PeerIdentity *put_path,
2681 unsigned int put_path_length)
2683 struct MeshPeerPath *p;
2689 GNUNET_PEER_change_rc (myid, 1);
2690 i = get_path_length;
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2692 for (i--; i >= 0; i--)
2694 id = GNUNET_PEER_intern (&get_path[i]);
2695 if (p->length > 0 && id == p->peers[p->length - 1])
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2698 GNUNET_PEER_change_rc (id, -1);
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2703 GNUNET_i2s (&get_path[i]));
2705 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2706 p->peers[p->length - 1] = id;
2709 i = put_path_length;
2710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2711 for (i--; i >= 0; i--)
2713 id = GNUNET_PEER_intern (&put_path[i]);
2716 /* PUT path went through us, so discard the path up until now and start
2717 * from here to get a much shorter (and loop-free) path.
2722 if (p->length > 0 && id == p->peers[p->length - 1])
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2725 GNUNET_PEER_change_rc (id, -1);
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2730 GNUNET_i2s (&put_path[i]));
2732 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2733 p->peers[p->length - 1] = id;
2737 if (get_path_length > 0)
2738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2739 GNUNET_i2s (&get_path[0]));
2740 if (put_path_length > 0)
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2742 GNUNET_i2s (&put_path[0]));
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2745 for (i = 0; i < p->length; i++)
2747 struct GNUNET_PeerIdentity peer_id;
2749 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2751 GNUNET_i2s (&peer_id));
2759 * Adds a path to the peer_infos of all the peers in the path
2761 * @param p Path to process.
2762 * @param confirmed Whether we know if the path works or not.
2765 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2769 /* TODO: invert and add */
2770 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2771 for (i++; i < p->length; i++)
2773 struct MeshPeerInfo *aux;
2774 struct MeshPeerPath *copy;
2776 aux = peer_info_get_short (p->peers[i]);
2777 copy = path_duplicate (p);
2778 copy->length = i + 1;
2779 peer_info_add_path (aux, copy, GNUNET_NO);
2785 * Send keepalive packets for a peer
2787 * @param cls Closure (tunnel for which to send the keepalive).
2788 * @param tc Notification context.
2790 * TODO: implement explicit multicast keepalive?
2793 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2797 * Search for a tunnel among the incoming tunnels
2799 * @param tid the local id of the tunnel
2801 * @return tunnel handler, NULL if doesn't exist
2803 static struct MeshTunnel *
2804 tunnel_get_incoming (MESH_TunnelNumber tid)
2806 struct GNUNET_HashCode hash;
2808 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2809 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2810 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2815 * Search for a tunnel among the tunnels for a client
2817 * @param c the client whose tunnels to search in
2818 * @param tid the local id of the tunnel
2820 * @return tunnel handler, NULL if doesn't exist
2822 static struct MeshTunnel *
2823 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2825 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2827 return tunnel_get_incoming (tid);
2831 struct GNUNET_HashCode hash;
2833 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2834 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2840 * Search for a tunnel by global ID using PEER_ID
2842 * @param pi owner of the tunnel
2843 * @param tid global tunnel number
2845 * @return tunnel handler, NULL if doesn't exist
2847 static struct MeshTunnel *
2848 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2850 struct MESH_TunnelID id;
2851 struct GNUNET_HashCode hash;
2856 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2857 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2862 * Search for a tunnel by global ID using full PeerIdentities
2864 * @param oid owner of the tunnel
2865 * @param tid global tunnel number
2867 * @return tunnel handler, NULL if doesn't exist
2869 static struct MeshTunnel *
2870 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2872 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2877 * Delete an active client from the tunnel.
2883 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2887 for (i = 0; i < t->nclients; i++)
2889 if (t->clients[i] == c)
2891 t->clients[i] = t->clients[t->nclients - 1];
2892 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2893 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2895 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2903 * Delete an ignored client from the tunnel.
2909 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2913 for (i = 0; i < t->nignore; i++)
2915 if (t->ignore[i] == c)
2917 t->ignore[i] = t->ignore[t->nignore - 1];
2918 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2926 * Delete a client from the tunnel. It should be only done on
2927 * client disconnection, otherwise use client_ignore_tunnel.
2933 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2935 tunnel_delete_ignored_client (t, c);
2936 tunnel_delete_active_client (t, c);
2941 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
2943 * Destroys queue elements of all waiting transmissions and frees all memory
2944 * used by the struct and its elements.
2946 * @param cls Closure (tunnel info).
2947 * @param key Hash of GNUNET_PEER_Id (unused).
2948 * @param value MeshTunnelChildInfo of the child.
2950 * @return always GNUNET_YES, to keep iterating
2953 tunnel_destroy_child (void *cls,
2954 const struct GNUNET_HashCode * key,
2957 struct MeshTunnelChildInfo *cinfo = value;
2958 struct MeshTunnel *t = cls;
2959 struct MeshPeerQueue *q;
2963 for (c = 0; c < cinfo->send_buffer_n; c++)
2965 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
2966 q = cinfo->send_buffer[i];
2967 cinfo->send_buffer[i] = NULL;
2969 queue_destroy (q, GNUNET_YES);
2972 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
2974 GNUNET_free_non_null (cinfo->send_buffer);
2975 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
2977 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
2978 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2980 GNUNET_free (cinfo);
2986 * Callback used to notify a client owner of a tunnel that a peer has
2987 * disconnected, most likely because of a path change.
2989 * @param cls Closure (tunnel this notification is about).
2990 * @param peer_id Short ID of disconnected peer.
2993 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2995 struct MeshTunnel *t = cls;
2996 struct MeshPeerInfo *peer;
2997 struct MeshPathInfo *path_info;
2999 client_notify_peer_disconnected (t->owner, t, peer_id);
3001 peer = peer_info_get_short (peer_id);
3002 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3003 path_info->peer = peer;
3005 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3011 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3012 * needed rescources.
3013 * If peer already exists, reevaluate shortest path and change if different.
3015 * @param t Tunnel we want to add a new peer to
3016 * @param peer PeerInfo of the peer being added
3020 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3022 struct GNUNET_PeerIdentity id;
3023 struct MeshPeerPath *best_p;
3024 struct MeshPeerPath *p;
3025 unsigned int best_cost;
3028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3029 GNUNET_PEER_resolve (peer->id, &id);
3031 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3033 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3034 GNUNET_assert (GNUNET_OK ==
3035 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3040 if (NULL != (p = peer->path_head))
3043 best_cost = tree_get_path_cost (t->tree, p);
3046 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3053 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3054 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3055 t->path_refresh_task =
3056 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3060 /* Start a DHT get */
3061 peer_info_connect (peer, t);
3063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3067 * Add a path to a tunnel which we don't own, just to remember the next hop.
3068 * If destination node was already in the tunnel, the first hop information
3069 * will be replaced with the new path.
3071 * @param t Tunnel we want to add a new peer to
3072 * @param p Path to add
3073 * @param own_pos Position of local node in path.
3077 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3078 unsigned int own_pos)
3080 struct GNUNET_PeerIdentity id;
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3083 GNUNET_assert (0 != own_pos);
3084 tree_add_path (t->tree, p, NULL, NULL);
3085 if (own_pos < p->length - 1)
3087 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3088 tree_update_first_hops (t->tree, myid, &id);
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3094 * Add a client to a tunnel, initializing all needed data structures.
3096 * @param t Tunnel to which add the client.
3097 * @param c Client which to add to the tunnel.
3100 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3102 struct MeshTunnelClientInfo clinfo;
3104 GNUNET_array_append (t->clients, t->nclients, c);
3105 clinfo.fwd_ack = t->fwd_pid + 1;
3106 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3107 clinfo.fwd_pid = t->fwd_pid;
3108 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3110 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3115 * Notifies a tunnel that a connection has broken that affects at least
3116 * some of its peers. Sends a notification towards the root of the tree.
3117 * In case the peer is the owner of the tree, notifies the client that owns
3118 * the tunnel and tries to reconnect.
3120 * @param t Tunnel affected.
3121 * @param p1 Peer that got disconnected from p2.
3122 * @param p2 Peer that got disconnected from p1.
3124 * @return Short ID of the peer disconnected (either p1 or p2).
3125 * 0 if the tunnel remained unaffected.
3127 static GNUNET_PEER_Id
3128 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3134 tree_notify_connection_broken (t->tree, p1, p2,
3135 &tunnel_notify_client_peer_disconnected,
3137 if (myid != p1 && myid != p2)
3143 if (tree_get_predecessor (t->tree) != 0)
3145 /* We are the peer still connected, notify owner of the disconnection. */
3146 struct GNUNET_MESH_PathBroken msg;
3147 struct GNUNET_PeerIdentity neighbor;
3149 msg.header.size = htons (sizeof (msg));
3150 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3151 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3152 msg.tid = htonl (t->id.tid);
3153 msg.peer1 = my_full_id;
3154 GNUNET_PEER_resolve (pid, &msg.peer2);
3155 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3156 send_prebuilt_message (&msg.header, &neighbor, t);
3164 * Send a multicast packet to a neighbor.
3166 * @param cls Closure (Info about the multicast packet)
3167 * @param neighbor_id Short ID of the neighbor to send the packet to.
3170 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3172 struct MeshData *mdata = cls;
3173 struct MeshTransmissionDescriptor *info;
3174 struct GNUNET_PeerIdentity neighbor;
3175 struct GNUNET_MessageHeader *msg;
3177 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3179 info->mesh_data = mdata;
3180 (mdata->reference_counter) ++;
3181 info->destination = neighbor_id;
3182 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3184 GNUNET_i2s (&neighbor));
3185 info->peer = peer_info_get (&neighbor);
3186 GNUNET_assert (NULL != info->peer);
3187 msg = (struct GNUNET_MessageHeader *) mdata->data;
3190 info->mesh_data->data_len,
3197 * Queue a message in a tunnel in multicast, sending a copy to each child node
3198 * down the local one in the tunnel tree.
3200 * @param t Tunnel in which to send the data.
3201 * @param msg Message to be sent.
3204 tunnel_send_multicast (struct MeshTunnel *t,
3205 const struct GNUNET_MessageHeader *msg)
3207 struct MeshData *mdata;
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3210 " sending a multicast packet...\n");
3212 mdata = GNUNET_malloc (sizeof (struct MeshData));
3213 mdata->data_len = ntohs (msg->size);
3215 mdata->data = GNUNET_malloc (mdata->data_len);
3216 memcpy (mdata->data, msg, mdata->data_len);
3217 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3219 struct GNUNET_MESH_Multicast *mcast;
3221 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3222 if (t->fwd_queue_n >= t->fwd_queue_max)
3225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3227 " message from %s!\n",
3228 GNUNET_i2s(&mcast->oid));
3229 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3230 " message at %s!\n",
3231 GNUNET_i2s(&my_full_id));
3232 GNUNET_free (mdata->data);
3233 GNUNET_free (mdata);
3237 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3239 ntohl (mcast->ttl));
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3246 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3247 if (mdata->reference_counter == 0)
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3250 " no one to send data to\n");
3251 GNUNET_free (mdata->data);
3252 GNUNET_free (mdata);
3253 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3258 mdata->total_out = mdata->reference_counter;
3260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3261 " sending a multicast packet done\n");
3267 * Increase the SKIP value of all peers that
3268 * have not received a unicast message.
3270 * @param cls Closure (ID of the peer that HAS received the message).
3271 * @param key ID of the neighbor.
3272 * @param value Information about the neighbor.
3274 * @return GNUNET_YES to keep iterating.
3277 tunnel_add_skip (void *cls,
3278 const struct GNUNET_HashCode * key,
3281 struct GNUNET_PeerIdentity *neighbor = cls;
3282 struct MeshTunnelChildInfo *cinfo = value;
3284 /* TODO compare only pointers? key == neighbor? */
3285 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3295 * @brief Get neighbor's Flow Control information.
3297 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3298 * descendant of the local node in a certain tunnel.
3299 * If the info is not yet there (recently created path), creates the data struct
3300 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3303 * @param t Tunnel related.
3304 * @param peer Neighbor whose Flow Control info is needed.
3306 * @return Neighbor's Flow Control info.
3308 static struct MeshTunnelChildInfo *
3309 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3310 const struct GNUNET_PeerIdentity *peer)
3312 struct MeshTunnelChildInfo *cinfo;
3314 if (NULL == t->children_fc)
3317 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3323 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3324 cinfo->id = GNUNET_PEER_intern (peer);
3325 cinfo->skip = t->fwd_pid;
3328 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3329 cinfo->fwd_ack = t->fwd_pid + delta;
3330 cinfo->bck_ack = delta;
3331 cinfo->bck_pid = -1;
3333 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3334 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3336 cinfo->send_buffer =
3337 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3339 GNUNET_assert (GNUNET_OK ==
3340 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3343 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3350 * Get the Flow Control info of a client.
3352 * @param t Tunnel on which to look.
3353 * @param c Client whose ACK to get.
3355 * @return ACK value.
3357 static struct MeshTunnelClientInfo *
3358 tunnel_get_client_fc (struct MeshTunnel *t,
3359 struct MeshClient *c)
3363 for (i = 0; i < t->nclients; i++)
3365 if (t->clients[i] != c)
3367 return &t->clients_fc[i];
3370 return NULL; // avoid compiler / coverity complaints
3375 * Iterator to get the appropiate ACK value from all children nodes.
3377 * @param cls Closue (tunnel).
3378 * @param id Id of the child node.
3381 tunnel_get_child_fwd_ack (void *cls,
3384 struct GNUNET_PeerIdentity peer_id;
3385 struct MeshTunnelChildInfo *cinfo;
3386 struct MeshTunnelChildIteratorContext *ctx = cls;
3387 struct MeshTunnel *t = ctx->t;
3390 GNUNET_PEER_resolve (id, &peer_id);
3391 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3392 ack = cinfo->fwd_ack;
3395 if (GNUNET_NO == ctx->init)
3397 ctx->max_child_ack = ack;
3398 ctx->init = GNUNET_YES;
3401 if (GNUNET_YES == t->speed_min)
3403 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3407 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3414 * Get the maximum PID allowed to transmit to any
3415 * tunnel child of the local peer, depending on the tunnel
3416 * buffering/speed settings.
3420 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3423 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3425 struct MeshTunnelChildIteratorContext ctx;
3427 ctx.max_child_ack = 0;
3429 ctx.init = GNUNET_NO;
3430 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3432 if (0 == ctx.nchildren)
3434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3435 " tunnel has no children, no FWD ACK\n");
3439 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3440 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3442 return (int64_t) ctx.max_child_ack;
3447 * Set the FWD ACK value of a client in a particular tunnel.
3449 * @param t Tunnel affected.
3450 * @param c Client whose ACK to set.
3451 * @param ack ACK value.
3454 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3455 struct MeshClient *c,
3460 for (i = 0; i < t->nclients; i++)
3462 if (t->clients[i] != c)
3464 t->clients_fc[i].fwd_ack = ack;
3472 * Get the highest ACK value of all clients in a particular tunnel,
3473 * according to the buffering/speed settings.
3475 * @param t Tunnel on which to look.
3477 * @return Corresponding ACK value (max uint32_t).
3478 * If no clients are suscribed, -1LL.
3481 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3486 if (0 == t->nclients)
3488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3489 " tunnel has no clients, no FWD ACK\n");
3493 for (ack = -1LL, i = 0; i < t->nclients; i++)
3496 (GNUNET_YES == t->speed_min &&
3497 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3498 (GNUNET_NO == t->speed_min &&
3499 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3501 ack = t->clients_fc[i].fwd_ack;
3505 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3506 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3508 return (uint32_t) ack;
3513 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3514 * mode and the status of all children nodes.
3518 * @return Maximum PID allowed.
3521 tunnel_get_fwd_ack (struct MeshTunnel *t)
3525 uint32_t buffer_free;
3529 count = t->fwd_pid - t->skip;
3530 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3531 child_ack = tunnel_get_children_fwd_ack (t);
3532 client_ack = tunnel_get_clients_fwd_ack (t);
3533 if (GNUNET_YES == t->nobuffer)
3536 if (-1LL == child_ack)
3537 child_ack = client_ack;
3538 if (-1LL == child_ack)
3541 client_ack = child_ack = ack;
3546 ack = count + buffer_free; // Overflow? OK!
3548 if (-1LL == child_ack)
3550 // Node has no children, child_ack AND core buffer are irrelevant.
3551 if (-1LL == client_ack) // No children AND no clients? Not good!
3553 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3557 return (uint32_t) client_ack;
3559 if (-1LL == client_ack)
3563 if (GNUNET_YES == t->speed_min)
3565 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3566 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3570 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3571 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3574 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3575 count, buffer_free, child_ack, client_ack, ack);
3581 * Build a local ACK message and send it to a local client.
3583 * @param t Tunnel on which to send the ACK.
3584 * @param c Client to whom send the ACK.
3585 * @param ack Value of the ACK.
3588 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3590 struct GNUNET_MESH_LocalAck msg;
3592 msg.header.size = htons (sizeof (msg));
3593 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3594 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3595 msg.max_pid = htonl (ack);
3596 GNUNET_SERVER_notification_context_unicast(nc,
3603 * Build an ACK message and queue it to send to the given peer.
3605 * @param t Tunnel on which to send the ACK.
3606 * @param peer Peer to whom send the ACK.
3607 * @param ack Value of the ACK.
3610 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3612 struct GNUNET_MESH_ACK msg;
3614 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3615 msg.header.size = htons (sizeof (msg));
3616 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3617 msg.pid = htonl (ack);
3618 msg.tid = htonl (t->id.tid);
3620 send_prebuilt_message (&msg.header, peer, t);
3625 * Notify a the owner of a tunnel about how many more
3626 * payload packages will we accept on a given tunnel.
3628 * @param t Tunnel on which to send the ACK.
3631 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3636 "Sending client FWD ACK on tunnel %X\n",
3639 ack = tunnel_get_fwd_ack (t);
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3642 if (t->last_fwd_ack == ack)
3644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3649 t->last_fwd_ack = ack;
3650 send_local_ack (t, t->owner, ack);
3655 * Send an ACK informing the predecessor about the available buffer space.
3656 * In case there is no predecessor, inform the owning client.
3657 * If buffering is off, send only on behalf of children or self if endpoint.
3658 * If buffering is on, send when sent to children and buffer space is free.
3659 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3660 * the ACK itself goes "back" (towards root).
3662 * @param t Tunnel on which to send the ACK.
3663 * @param type Type of message that triggered the ACK transmission.
3666 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3668 struct GNUNET_PeerIdentity id;
3671 if (NULL != t->owner)
3673 tunnel_send_client_fwd_ack (t);
3676 /* Is it after unicast / multicast retransmission? */
3679 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3680 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3682 "ACK due to FWD DATA retransmission\n");
3683 if (GNUNET_YES == t->nobuffer)
3685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3689 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3690 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3692 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3693 t->force_ack = GNUNET_YES;
3699 /* Check if we need to transmit the ACK */
3700 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3701 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
3702 GNUNET_NO == t->force_ack)
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3706 " t->qmax: %u, t->qn: %u\n",
3707 t->fwd_queue_max, t->fwd_queue_n);
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3709 " t->pid: %u, t->ack: %u\n",
3710 t->fwd_pid, t->last_fwd_ack);
3714 /* Ok, ACK might be necessary, what PID to ACK? */
3715 ack = tunnel_get_fwd_ack (t);
3717 /* If speed_min and not all children have ack'd, dont send yet */
3718 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3724 t->last_fwd_ack = ack;
3725 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3726 send_ack (t, &id, ack);
3728 t->force_ack = GNUNET_NO;
3733 * Iterator to send a child node a BCK ACK to allow him to send more
3736 * @param cls Closure (tunnel).
3737 * @param id Id of the child node.
3740 tunnel_send_child_bck_ack (void *cls,
3743 struct MeshTunnel *t = cls;
3744 struct MeshTunnelChildInfo *cinfo;
3745 struct GNUNET_PeerIdentity peer;
3748 GNUNET_PEER_resolve (id, &peer);
3749 cinfo = tunnel_get_neighbor_fc (t, &peer);
3750 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
3752 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
3754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3755 " Not sending ACK, not needed\n");
3758 cinfo->bck_ack = ack;
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3761 " Sending BCK ACK %u (last sent: %u)\n",
3762 ack, cinfo->bck_ack);
3763 send_ack (t, &peer, ack);
3768 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3770 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3772 * FIXME fc: what happens if we have 2 clients but q_size is 1?
3773 * - implement a size 1 buffer in each client_fc AND children_fc
3774 * to hold at least 1 message per "child".
3775 * problem: violates no buffer policy
3776 * - ack 0 and make "children" poll for transmission slots
3777 * problem: big overhead, extra latency even in low traffic
3780 * @param t Tunnel on which to send the BCK ACKs.
3783 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3786 unsigned int tunnel_delta;
3788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3790 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3793 /* Find client whom to allow to send to origin (with lowest buffer space) */
3794 for (i = 0; i < t->nclients; i++)
3796 struct MeshTunnelClientInfo *clinfo;
3799 clinfo = &t->clients_fc[i];
3800 delta = clinfo->bck_ack - clinfo->bck_pid;
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3802 t->clients[i]->id, delta);
3804 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3805 (GNUNET_YES == t->nobuffer && 0 == delta))
3809 ack = clinfo->bck_pid;
3810 ack += t->nobuffer ? 1 : tunnel_delta;
3811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3812 " sending ack to client %u: %u\n",
3813 t->clients[i]->id, ack);
3814 send_local_ack (t, t->clients[i], ack);
3815 clinfo->bck_ack = ack;
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3820 " not sending ack to client %u (td %u, d %u)\n",
3821 t->clients[i]->id, tunnel_delta, delta);
3828 * Send an ACK informing the children nodes and destination clients about
3829 * the available buffer space.
3830 * If buffering is off, send only on behalf of root (can be self).
3831 * If buffering is on, send when sent to predecessor and buffer space is free.
3832 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3833 * the ACK itself goes "forward" (towards children/clients).
3835 * @param t Tunnel on which to send the ACK.
3836 * @param type Type of message that triggered the ACK transmission.
3839 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3842 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3843 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3844 /* Is it after data to_origin retransmission? */
3847 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3848 if (GNUNET_YES == t->nobuffer)
3850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3851 " Not sending ACK, nobuffer\n");
3855 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3856 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3858 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3859 t->force_ack = GNUNET_YES;
3865 tunnel_send_clients_bck_ack (t);
3866 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3867 t->force_ack = GNUNET_NO;
3872 * @brief Re-initiate traffic to this peer if necessary.
3874 * Check if there is traffic queued towards this peer
3875 * and the core transmit handle is NULL (traffic was stalled).
3876 * If so, call core tmt rdy.
3878 * @param cls Closure (unused)
3879 * @param peer_id Short ID of peer to which initiate traffic.
3882 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3884 struct MeshPeerInfo *peer;
3885 struct GNUNET_PeerIdentity id;
3886 struct MeshPeerQueue *q;
3889 peer = peer_info_get_short(peer_id);
3890 if (NULL != peer->core_transmit)
3893 q = queue_get_next(peer);
3896 /* Might br multicast traffic already sent to this particular peer but
3897 * not to other children in this tunnel.
3898 * This way t->queue_n would be > 0 but the queue of this particular peer
3904 GNUNET_PEER_resolve (peer->id, &id);
3905 peer->core_transmit =
3906 GNUNET_CORE_notify_transmit_ready(core_handle,
3909 GNUNET_TIME_UNIT_FOREVER_REL,
3919 * @brief Allow transmission of FWD traffic on this tunnel
3921 * Check if there is traffic queued towards any children
3922 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3924 * @param t Tunnel on which to unlock FWD traffic.
3927 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3929 if (0 == t->fwd_queue_n)
3932 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3937 * @brief Allow transmission of BCK traffic on this tunnel
3939 * Check if there is traffic queued towards the root of the tree
3940 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3942 * @param t Tunnel on which to unlock BCK traffic.
3945 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3947 if (0 == t->bck_queue_n)
3950 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3955 * Send a message to all peers in this tunnel that the tunnel is no longer
3958 * @param t The tunnel whose peers to notify.
3959 * @param parent ID of the parent, in case the tree is already destroyed.
3962 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3964 struct GNUNET_MESH_TunnelDestroy msg;
3965 struct GNUNET_PeerIdentity id;
3967 msg.header.size = htons (sizeof (msg));
3968 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3969 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3970 msg.tid = htonl (t->id.tid);
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3972 " sending tunnel destroy for tunnel: %s [%X]\n",
3973 GNUNET_i2s (&msg.oid), t->id.tid);
3974 if (tree_count_children(t->tree) > 0)
3976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to children\n");
3977 tunnel_send_multicast (t, &msg.header);
3980 parent = tree_get_predecessor (t->tree);
3981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3985 GNUNET_PEER_resolve (parent, &id);
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3987 " sending back to %s\n",
3989 send_prebuilt_message (&msg.header, &id, t);
3994 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3996 * @param cls Closure (Tunnel which to cancel).
3997 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4000 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4002 struct MeshTunnel *t = cls;
4003 struct MeshPeerInfo *peer_info;
4004 struct MeshPeerQueue *pq;
4005 struct MeshPeerQueue *next;
4007 peer_info = peer_info_get_short (neighbor_id);
4008 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4011 if (pq->tunnel == t)
4013 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4014 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4015 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4017 // Should have been removed on destroy children
4020 queue_destroy (pq, GNUNET_YES);
4023 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4025 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4026 peer_info->core_transmit = NULL;
4031 * Destroy the tunnel and free any allocated resources linked to it.
4033 * @param t the tunnel to destroy
4035 * @return GNUNET_OK on success
4038 tunnel_destroy (struct MeshTunnel *t)
4040 struct MeshClient *c;
4041 struct GNUNET_HashCode hash;
4052 struct GNUNET_PeerIdentity id;
4054 GNUNET_PEER_resolve (t->id.oid, &id);
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4056 GNUNET_i2s (&id), t->id.tid);
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4062 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4063 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4071 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4073 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4080 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4081 for (i = 0; i < t->nclients; i++)
4085 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4091 for (i = 0; i < t->nignore; i++)
4095 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4102 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4103 GNUNET_free_non_null (t->clients);
4104 GNUNET_free_non_null (t->ignore);
4105 GNUNET_free_non_null (t->clients_fc);
4107 if (NULL != t->peers)
4109 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4111 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4114 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4115 &tunnel_destroy_child,
4117 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4118 t->children_fc = NULL;
4120 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4121 tree_destroy (t->tree);
4123 if (NULL != t->regex_search)
4124 GNUNET_REGEX_search_cancel (t->regex_search->search_handle);
4125 if (NULL != t->dht_get_type)
4126 GNUNET_DHT_get_stop (t->dht_get_type);
4127 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4128 GNUNET_SCHEDULER_cancel (t->timeout_task);
4129 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4130 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4133 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4138 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4141 * Tunnel is empty: destroy it.
4143 * @param cls Closure (Tunnel).
4144 * @param tc TaskContext.
4147 tunnel_destroy_empty_delayed (void *cls,
4148 const struct GNUNET_SCHEDULER_TaskContext *tc)
4150 struct MeshTunnel *t = cls;
4152 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4153 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4156 if (0 != t->nclients ||
4157 0 != tree_count_children (t->tree))
4162 struct GNUNET_PeerIdentity id;
4164 GNUNET_PEER_resolve (t->id.oid, &id);
4165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4166 "executing destruction of empty tunnel %s [%X]\n",
4167 GNUNET_i2s (&id), t->id.tid);
4171 tunnel_send_destroy (t, 0);
4172 if (0 == t->pending_messages)
4175 t->destroy = GNUNET_YES;
4180 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4182 * @param t Tunnel to destroy if empty.
4185 tunnel_destroy_empty (struct MeshTunnel *t)
4187 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4189 0 != tree_count_children (t->tree))
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4193 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4199 struct GNUNET_PeerIdentity id;
4201 GNUNET_PEER_resolve (t->id.oid, &id);
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4203 "scheduling destruction of empty tunnel %s [%X]\n",
4204 GNUNET_i2s (&id), t->id.tid);
4208 t->delayed_destroy =
4209 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4210 &tunnel_destroy_empty_delayed,
4216 * Create a new tunnel
4218 * @param owner Who is the owner of the tunnel (short ID).
4219 * @param tid Tunnel Number of the tunnel.
4220 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4221 * @param local Tunnel Number for the tunnel, for the client point of view.
4223 * @return A new initialized tunnel. NULL on error.
4225 static struct MeshTunnel *
4226 tunnel_new (GNUNET_PEER_Id owner,
4227 MESH_TunnelNumber tid,
4228 struct MeshClient *client,
4229 MESH_TunnelNumber local)
4231 struct MeshTunnel *t;
4232 struct GNUNET_HashCode hash;
4234 if (n_tunnels >= max_tunnels && NULL == client)
4237 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4240 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4241 t->bck_queue_max = t->fwd_queue_max;
4242 t->tree = tree_new (owner);
4244 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4245 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4246 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4247 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4248 t->local_tid = local;
4249 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4251 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4253 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4255 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4263 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4270 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4272 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4273 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4277 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4286 * Callback when removing children from a tunnel tree. Notify owner.
4288 * @param cls Closure (tunnel).
4289 * @param peer_id Short ID of the peer deleted.
4292 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
4294 struct MeshTunnel *t = cls;
4296 client_notify_peer_disconnected (t->owner, t, peer_id);
4300 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4301 * that are no longer needed, as well as nodes of no longer reachable peers.
4302 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4304 * @param t Tunnel from which to remove the path.
4305 * @param peer Short id of the peer which should be removed.
4308 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4312 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
4315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4316 "Tunnel %u [%u] has no more nodes\n",
4317 t->id.oid, t->id.tid);
4323 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4324 * client when the client disconnects. If the client is not the owner, the
4325 * owner will get notified if no more clients are in the tunnel and the client
4326 * get removed from the tunnel's list.
4328 * @param cls closure (client that is disconnecting)
4329 * @param key the hash of the local tunnel id (used to access the hashmap)
4330 * @param value the value stored at the key (tunnel to destroy)
4332 * @return GNUNET_OK, keep iterating.
4335 tunnel_destroy_iterator (void *cls,
4336 const struct GNUNET_HashCode * key,
4339 struct MeshTunnel *t = value;
4340 struct MeshClient *c = cls;
4342 send_client_tunnel_disconnect (t, c);
4345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4346 tunnel_delete_client (t, c);
4347 client_delete_tunnel (c, t);
4348 tunnel_destroy_empty (t);
4351 tunnel_send_destroy (t, 0);
4353 t->destroy = GNUNET_YES;
4360 * Timeout function, destroys tunnel if called
4362 * @param cls Closure (tunnel to destroy).
4363 * @param tc TaskContext
4366 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4368 struct MeshTunnel *t = cls;
4369 struct GNUNET_PeerIdentity id;
4371 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4372 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4374 GNUNET_PEER_resolve(t->id.oid, &id);
4375 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4376 "Tunnel %s [%X] timed out. Destroying.\n",
4377 GNUNET_i2s(&id), t->id.tid);
4378 send_clients_tunnel_destroy (t);
4383 * Resets the tunnel timeout. Starts it if no timeout was running.
4385 * @param t Tunnel whose timeout to reset.
4387 * TODO use heap to improve efficiency of scheduler.
4390 tunnel_reset_timeout (struct MeshTunnel *t)
4392 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4393 GNUNET_SCHEDULER_cancel (t->timeout_task);
4395 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4396 (refresh_path_time, 4), &tunnel_timeout, t);
4400 /******************************************************************************/
4401 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4402 /******************************************************************************/
4405 * Function to send a create path packet to a peer.
4407 * @param cls closure
4408 * @param size number of bytes available in buf
4409 * @param buf where the callee should write the message
4410 * @return number of bytes written to buf
4413 send_core_path_create (void *cls, size_t size, void *buf)
4415 struct MeshPathInfo *info = cls;
4416 struct GNUNET_MESH_ManipulatePath *msg;
4417 struct GNUNET_PeerIdentity *peer_ptr;
4418 struct MeshTunnel *t = info->t;
4419 struct MeshPeerPath *p = info->path;
4424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4426 sizeof (struct GNUNET_MESH_ManipulatePath) +
4427 p->length * sizeof (struct GNUNET_PeerIdentity);
4429 if (size < size_needed || NULL == buf)
4434 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4435 msg->header.size = htons (size_needed);
4436 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4437 msg->tid = ntohl (t->id.tid);
4440 if (GNUNET_YES == t->speed_min)
4441 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4442 if (GNUNET_YES == t->nobuffer)
4443 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4444 msg->opt = htonl(opt);
4447 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4448 for (i = 0; i < p->length; i++)
4450 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4457 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4463 * Fill the core buffer
4465 * @param cls closure (data itself)
4466 * @param size number of bytes available in buf
4467 * @param buf where the callee should write the message
4469 * @return number of bytes written to buf
4472 send_core_data_multicast (void *cls, size_t size, void *buf)
4474 struct MeshTransmissionDescriptor *info = cls;
4477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4478 GNUNET_assert (NULL != info);
4479 GNUNET_assert (NULL != info->peer);
4480 total_size = info->mesh_data->data_len;
4481 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4483 if (total_size > size)
4488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4489 memcpy (buf, info->mesh_data->data, total_size);
4492 struct GNUNET_MESH_Multicast *mc;
4493 struct GNUNET_MessageHeader *mh;
4496 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4498 mc = (struct GNUNET_MESH_Multicast *) mh;
4499 mh = (struct GNUNET_MessageHeader *) &mc[1];
4500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4501 " multicast, payload type %s\n",
4502 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4504 " multicast, payload size %u\n", ntohs (mh->size));
4508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4509 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4513 data_descriptor_decrement_rc (info->mesh_data);
4514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4522 * Creates a path ack message in buf and frees all unused resources.
4524 * @param cls closure (MeshTransmissionDescriptor)
4525 * @param size number of bytes available in buf
4526 * @param buf where the callee should write the message
4527 * @return number of bytes written to buf
4530 send_core_path_ack (void *cls, size_t size, void *buf)
4532 struct MeshTransmissionDescriptor *info = cls;
4533 struct GNUNET_MESH_PathACK *msg = buf;
4535 GNUNET_assert (NULL != info);
4536 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4541 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4542 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4543 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4544 msg->tid = htonl (info->origin->tid);
4545 msg->peer_id = my_full_id;
4548 /* TODO add signature */
4550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4551 return sizeof (struct GNUNET_MESH_PathACK);
4556 * Free a transmission that was already queued with all resources
4557 * associated to the request.
4559 * @param queue Queue handler to cancel.
4560 * @param clear_cls Is it necessary to free associated cls?
4563 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4565 struct MeshTransmissionDescriptor *dd;
4566 struct MeshPathInfo *path_info;
4567 struct MeshTunnelChildInfo *cinfo;
4568 struct GNUNET_PeerIdentity id;
4572 if (GNUNET_YES == clear_cls)
4574 switch (queue->type)
4576 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4577 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4578 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4580 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4581 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4582 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4583 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4584 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4585 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4587 " prebuilt message\n");
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4590 GNUNET_MESH_DEBUG_M2S(queue->type));
4592 data_descriptor_decrement_rc (dd->mesh_data);
4594 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4596 path_info = queue->cls;
4597 path_destroy (path_info->path);
4601 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4602 " type %s unknown!\n",
4603 GNUNET_MESH_DEBUG_M2S(queue->type));
4605 GNUNET_free_non_null (queue->cls);
4607 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4608 queue->peer->queue_tail,
4611 /* Delete from child_fc in the appropiate tunnel */
4612 max = queue->tunnel->fwd_queue_max;
4613 GNUNET_PEER_resolve (queue->peer->id, &id);
4614 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4617 for (i = 0; i < cinfo->send_buffer_n; i++)
4620 i2 = (cinfo->send_buffer_start + i) % max;
4621 if (cinfo->send_buffer[i2] == queue)
4623 /* Found corresponding entry in the send_buffer. Move all others back. */
4628 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4630 j2 = (cinfo->send_buffer_start + j) % max;
4631 j3 = (cinfo->send_buffer_start + j + 1) % max;
4632 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4635 cinfo->send_buffer[j3] = NULL;
4636 cinfo->send_buffer_n--;
4641 GNUNET_free (queue);
4646 * @brief Get the next transmittable message from the queue.
4648 * This will be the head, except in the case of being a data packet
4649 * not allowed by the destination peer.
4651 * @param peer Destination peer.
4653 * @return The next viable MeshPeerQueue element to send to that peer.
4654 * NULL when there are no transmittable messages.
4656 struct MeshPeerQueue *
4657 queue_get_next (const struct MeshPeerInfo *peer)
4659 struct MeshPeerQueue *q;
4660 struct MeshTunnel *t;
4661 struct MeshTransmissionDescriptor *info;
4662 struct MeshTunnelChildInfo *cinfo;
4663 struct GNUNET_MESH_Unicast *ucast;
4664 struct GNUNET_MESH_ToOrigin *to_orig;
4665 struct GNUNET_MESH_Multicast *mcast;
4666 struct GNUNET_PeerIdentity id;
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4671 for (q = peer->queue_head; NULL != q; q = q->next)
4675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4677 GNUNET_MESH_DEBUG_M2S(q->type));
4680 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4681 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4682 pid = ntohl (ucast->pid);
4683 GNUNET_PEER_resolve (info->peer->id, &id);
4684 cinfo = tunnel_get_neighbor_fc(t, &id);
4685 ack = cinfo->fwd_ack;
4687 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4688 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4689 pid = ntohl (to_orig->pid);
4692 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4693 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4694 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4696 // Not a multicast payload: multicast control traffic (destroy, etc)
4699 pid = ntohl (mcast->pid);
4700 GNUNET_PEER_resolve (info->peer->id, &id);
4701 cinfo = tunnel_get_neighbor_fc(t, &id);
4702 ack = cinfo->fwd_ack;
4705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4710 "********* ACK: %u, PID: %u\n",
4712 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4721 "********* NEXT!\n");
4724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4725 "********* nothing found\n");
4731 * Core callback to write a queued packet to core buffer
4733 * @param cls Closure (peer info).
4734 * @param size Number of bytes available in buf.
4735 * @param buf Where the to write the message.
4737 * @return number of bytes written to buf
4740 queue_send (void *cls, size_t size, void *buf)
4742 struct MeshPeerInfo *peer = cls;
4743 struct GNUNET_MessageHeader *msg;
4744 struct MeshPeerQueue *queue;
4745 struct MeshTunnel *t;
4746 struct MeshTunnelChildInfo *cinfo;
4747 struct GNUNET_PeerIdentity dst_id;
4750 peer->core_transmit = NULL;
4753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4754 queue = queue_get_next (peer);
4756 /* Queue has no internal mesh traffic nor sendable payload */
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4760 if (NULL == peer->queue_head)
4761 GNUNET_break (0); // Should've been canceled
4764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4766 GNUNET_PEER_resolve (peer->id, &dst_id);
4767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4768 "********* towards %s\n",
4769 GNUNET_i2s(&dst_id));
4770 /* Check if buffer size is enough for the message */
4771 if (queue->size > size)
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4774 "********* not enough room, reissue\n");
4775 peer->core_transmit =
4776 GNUNET_CORE_notify_transmit_ready (core_handle,
4779 GNUNET_TIME_UNIT_FOREVER_REL,
4786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4789 GNUNET_assert (0 < t->pending_messages);
4790 t->pending_messages--;
4791 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4795 "********* unicast: t->q (%u/%u)\n",
4796 t->fwd_queue_n, t->fwd_queue_max);
4798 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4805 switch (queue->type)
4808 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4809 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4810 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4811 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4812 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4814 "********* raw: %s\n",
4815 GNUNET_MESH_DEBUG_M2S (queue->type));
4817 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4818 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4819 data_size = send_core_data_raw (queue->cls, size, buf);
4820 msg = (struct GNUNET_MessageHeader *) buf;
4821 switch (ntohs (msg->type)) // Type of preconstructed message
4823 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4824 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4826 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4827 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4833 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4836 struct MeshTransmissionDescriptor *info = queue->cls;
4838 if ((1 == info->mesh_data->reference_counter
4839 && GNUNET_YES == t->speed_min)
4841 (info->mesh_data->total_out == info->mesh_data->reference_counter
4842 && GNUNET_NO == t->speed_min))
4844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4845 "********* considered sent\n");
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4851 "********* NOT considered sent yet\n");
4852 t->pending_messages++;
4855 data_size = send_core_data_multicast(queue->cls, size, buf);
4856 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4858 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4860 data_size = send_core_path_create (queue->cls, size, buf);
4862 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4864 data_size = send_core_path_ack (queue->cls, size, buf);
4866 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4868 data_size = send_core_data_multicast (queue->cls, size, buf);
4872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4873 "********* type unknown: %u\n",
4877 switch (queue->type)
4879 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4880 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4881 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4882 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4883 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4886 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4887 "at pos %u (%p) != %p\n",
4888 cinfo->send_buffer_start,
4889 cinfo->send_buffer[cinfo->send_buffer_start],
4892 if (cinfo->send_buffer_n > 0)
4894 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4895 cinfo->send_buffer_n--;
4896 cinfo->send_buffer_start++;
4897 cinfo->send_buffer_start %= t->fwd_queue_max;
4908 /* Free queue, but cls was freed by send_core_* */
4909 queue_destroy (queue, GNUNET_NO);
4911 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4917 /* If more data in queue, send next */
4918 queue = queue_get_next(peer);
4921 struct GNUNET_PeerIdentity id;
4923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4924 GNUNET_PEER_resolve (peer->id, &id);
4925 peer->core_transmit =
4926 GNUNET_CORE_notify_transmit_ready(core_handle,
4929 GNUNET_TIME_UNIT_FOREVER_REL,
4937 if (NULL != peer->queue_head)
4939 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4940 "********* %s stalled\n",
4941 GNUNET_i2s(&my_full_id));
4943 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4944 // FIXME unify bck/fwd structures, bck does not have cinfo right now
4945 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
4947 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
4948 &tunnel_poll, cinfo);
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4958 * @brief Queue and pass message to core when possible.
4960 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4961 * and accounts for it. In case the queue is full, the message is dropped and
4964 * Otherwise, message is treated as internal and allowed to go regardless of
4967 * @param cls Closure (@c type dependant). It will be used by queue_send to
4968 * build the message to be sent if not already prebuilt.
4969 * @param type Type of the message, 0 for a raw message.
4970 * @param size Size of the message.
4971 * @param dst Neighbor to send message to.
4972 * @param t Tunnel this message belongs to.
4975 queue_add (void *cls, uint16_t type, size_t size,
4976 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4978 struct MeshPeerQueue *queue;
4979 struct MeshTunnelChildInfo *cinfo;
4980 struct GNUNET_PeerIdentity id;
4986 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4987 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4989 n = &t->fwd_queue_n;
4990 max = &t->fwd_queue_max;
4992 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4994 n = &t->bck_queue_n;
4995 max = &t->bck_queue_max;
5002 GNUNET_STATISTICS_update(stats,
5003 "# messages dropped (buffer full)",
5005 return; // Drop message
5009 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5015 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5016 GNUNET_PEER_resolve (dst->id, &id);
5017 if (NULL == dst->core_transmit)
5019 dst->core_transmit =
5020 GNUNET_CORE_notify_transmit_ready (core_handle,
5023 GNUNET_TIME_UNIT_FOREVER_REL,
5029 t->pending_messages++;
5030 if (NULL == n) // Is this internal mesh traffic?
5033 // It's payload, keep track of buffer per peer.
5034 cinfo = tunnel_get_neighbor_fc(t, &id);
5035 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5036 if (NULL != cinfo->send_buffer[i])
5038 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5039 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5040 cinfo->send_buffer_start++;
5041 cinfo->send_buffer_start %= t->fwd_queue_max;
5045 cinfo->send_buffer_n++;
5047 cinfo->send_buffer[i] = queue;
5048 if (cinfo->send_buffer_n > t->fwd_queue_max)
5051 cinfo->send_buffer_n = t->fwd_queue_max;
5056 /******************************************************************************/
5057 /******************** MESH NETWORK HANDLERS **************************/
5058 /******************************************************************************/
5062 * Core handler for path creation
5064 * @param cls closure
5065 * @param message message
5066 * @param peer peer identity this notification is about
5067 * @param atsi performance data
5068 * @param atsi_count number of records in 'atsi'
5070 * @return GNUNET_OK to keep the connection open,
5071 * GNUNET_SYSERR to close it (signal serious error)
5074 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5075 const struct GNUNET_MessageHeader *message,
5076 const struct GNUNET_ATS_Information *atsi,
5077 unsigned int atsi_count)
5079 unsigned int own_pos;
5082 MESH_TunnelNumber tid;
5083 struct GNUNET_MESH_ManipulatePath *msg;
5084 struct GNUNET_PeerIdentity *pi;
5085 struct GNUNET_HashCode hash;
5086 struct MeshPeerPath *path;
5087 struct MeshPeerInfo *dest_peer_info;
5088 struct MeshPeerInfo *orig_peer_info;
5089 struct MeshTunnel *t;
5091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5092 "Received a path create msg [%s]\n",
5093 GNUNET_i2s (&my_full_id));
5094 size = ntohs (message->size);
5095 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5097 GNUNET_break_op (0);
5101 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5102 if (size % sizeof (struct GNUNET_PeerIdentity))
5104 GNUNET_break_op (0);
5107 size /= sizeof (struct GNUNET_PeerIdentity);
5110 GNUNET_break_op (0);
5113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5114 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5116 tid = ntohl (msg->tid);
5117 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5119 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5120 t = tunnel_get (pi, tid);
5121 if (NULL == t) // FIXME only for INCOMING tunnels?
5125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5126 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5129 // FIXME notify failure
5132 opt = ntohl (msg->opt);
5133 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5134 GNUNET_YES : GNUNET_NO;
5135 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5137 t->nobuffer = GNUNET_YES;
5138 t->last_fwd_ack = t->fwd_pid + 1;
5140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5141 " speed_min: %d, nobuffer:%d\n",
5142 t->speed_min, t->nobuffer);
5144 if (GNUNET_YES == t->nobuffer)
5146 t->bck_queue_max = 1;
5147 t->fwd_queue_max = 1;
5150 // FIXME only assign a local tid if a local client is interested (on demand)
5151 while (NULL != tunnel_get_incoming (next_local_tid))
5152 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5153 t->local_tid_dest = next_local_tid++;
5154 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5157 tunnel_reset_timeout (t);
5158 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5160 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5161 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5169 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5170 if (NULL == dest_peer_info)
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5173 " Creating PeerInfo for destination.\n");
5174 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5175 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5176 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5178 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5180 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5181 if (NULL == orig_peer_info)
5183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5184 " Creating PeerInfo for origin.\n");
5185 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5186 orig_peer_info->id = GNUNET_PEER_intern (pi);
5187 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5188 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5191 path = path_new (size);
5193 for (i = 0; i < size; i++)
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5196 GNUNET_i2s (&pi[i]));
5197 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5198 if (path->peers[i] == myid)
5201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5204 /* cannot be self, must be 'not found' */
5205 /* create path: self not found in path through self */
5206 GNUNET_break_op (0);
5207 path_destroy (path);
5211 path_add_to_peers (path, GNUNET_NO);
5212 tunnel_add_path (t, path, own_pos);
5213 if (own_pos == size - 1)
5215 /* It is for us! Send ack. */
5216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5217 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5218 if (NULL == t->peers)
5220 /* New tunnel! Notify clients on first payload message. */
5221 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5223 GNUNET_break (GNUNET_SYSERR !=
5224 GNUNET_CONTAINER_multihashmap_put (t->peers,
5225 &my_full_id.hashPubKey,
5228 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5233 struct MeshPeerPath *path2;
5235 /* It's for somebody else! Retransmit. */
5236 path2 = path_duplicate (path);
5237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5238 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5239 path2 = path_duplicate (path);
5240 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5241 send_create_path (dest_peer_info, path, t);
5248 * Core handler for path destruction
5250 * @param cls closure
5251 * @param message message
5252 * @param peer peer identity this notification is about
5253 * @param atsi performance data
5254 * @param atsi_count number of records in 'atsi'
5256 * @return GNUNET_OK to keep the connection open,
5257 * GNUNET_SYSERR to close it (signal serious error)
5260 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5261 const struct GNUNET_MessageHeader *message,
5262 const struct GNUNET_ATS_Information *atsi,
5263 unsigned int atsi_count)
5265 struct GNUNET_MESH_ManipulatePath *msg;
5266 struct GNUNET_PeerIdentity *pi;
5267 struct MeshPeerPath *path;
5268 struct MeshTunnel *t;
5269 unsigned int own_pos;
5273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5274 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5275 size = ntohs (message->size);
5276 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5278 GNUNET_break_op (0);
5282 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5283 if (size % sizeof (struct GNUNET_PeerIdentity))
5285 GNUNET_break_op (0);
5288 size /= sizeof (struct GNUNET_PeerIdentity);
5291 GNUNET_break_op (0);
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5296 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5297 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5299 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5301 t = tunnel_get (pi, ntohl (msg->tid));
5304 /* TODO notify back: we don't know this tunnel */
5305 GNUNET_break_op (0);
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5309 path = path_new (size);
5311 for (i = 0; i < size; i++)
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5314 GNUNET_i2s (&pi[i]));
5315 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5316 if (path->peers[i] == myid)
5319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5320 if (own_pos < path->length - 1)
5321 send_prebuilt_message (message, &pi[own_pos + 1], t);
5323 send_client_tunnel_disconnect(t, NULL);
5325 tunnel_delete_peer (t, path->peers[path->length - 1]);
5326 path_destroy (path);
5332 * Core handler for notifications of broken paths
5334 * @param cls closure
5335 * @param message message
5336 * @param peer peer identity this notification is about
5337 * @param atsi performance data
5338 * @param atsi_count number of records in 'atsi'
5340 * @return GNUNET_OK to keep the connection open,
5341 * GNUNET_SYSERR to close it (signal serious error)
5344 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5345 const struct GNUNET_MessageHeader *message,
5346 const struct GNUNET_ATS_Information *atsi,
5347 unsigned int atsi_count)
5349 struct GNUNET_MESH_PathBroken *msg;
5350 struct MeshTunnel *t;
5352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5353 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5354 msg = (struct GNUNET_MESH_PathBroken *) message;
5355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5356 GNUNET_i2s (&msg->peer1));
5357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5358 GNUNET_i2s (&msg->peer2));
5359 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5362 GNUNET_break_op (0);
5365 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5366 GNUNET_PEER_search (&msg->peer2));
5373 * Core handler for tunnel destruction
5375 * @param cls closure
5376 * @param message message
5377 * @param peer peer identity this notification is about
5378 * @param atsi performance data
5379 * @param atsi_count number of records in 'atsi'
5381 * @return GNUNET_OK to keep the connection open,
5382 * GNUNET_SYSERR to close it (signal serious error)
5385 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5386 const struct GNUNET_MessageHeader *message,
5387 const struct GNUNET_ATS_Information *atsi,
5388 unsigned int atsi_count)
5390 struct GNUNET_MESH_TunnelDestroy *msg;
5391 struct MeshTunnel *t;
5392 GNUNET_PEER_Id parent;
5395 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5397 "Got a TUNNEL DESTROY packet from %s\n",
5399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5400 " for tunnel %s [%u]\n",
5401 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5402 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5403 /* Check signature */
5406 /* Probably already got the message from another path,
5407 * destroyed the tunnel and retransmitted to children.
5410 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5414 parent = tree_get_predecessor (t->tree);
5415 pid = GNUNET_PEER_search (peer);
5420 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5421 nc = tree_count_children (t->tree);
5422 if (nc > 0 || NULL != t->owner || t->nclients > 0)
5424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5425 "still in use: %u cl, %u ch\n",
5430 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5432 /* Tunnel was incoming, notify clients */
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5434 t->local_tid, t->local_tid_dest);
5435 send_clients_tunnel_destroy (t);
5437 tunnel_send_destroy (t, parent);
5438 t->destroy = GNUNET_YES;
5439 // TODO: add timeout to destroy the tunnel anyway
5445 * Core handler for mesh network traffic going from the origin to a peer
5447 * @param cls closure
5448 * @param peer peer identity this notification is about
5449 * @param message message
5450 * @param atsi performance data
5451 * @param atsi_count number of records in 'atsi'
5452 * @return GNUNET_OK to keep the connection open,
5453 * GNUNET_SYSERR to close it (signal serious error)
5456 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5457 const struct GNUNET_MessageHeader *message,
5458 const struct GNUNET_ATS_Information *atsi,
5459 unsigned int atsi_count)
5461 struct GNUNET_MESH_Unicast *msg;
5462 struct GNUNET_PeerIdentity *neighbor;
5463 struct MeshTunnelChildInfo *cinfo;
5464 struct MeshTunnel *t;
5465 GNUNET_PEER_Id dest_id;
5470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5473 size = ntohs (message->size);
5475 sizeof (struct GNUNET_MESH_Unicast) +
5476 sizeof (struct GNUNET_MessageHeader))
5481 msg = (struct GNUNET_MESH_Unicast *) message;
5482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5483 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5485 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5488 /* TODO notify back: we don't know this tunnel */
5489 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5490 GNUNET_break_op (0);
5493 pid = ntohl (msg->pid);
5494 if (t->fwd_pid == pid)
5496 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5497 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5498 " Already seen pid %u, DROPPING!\n", pid);
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5504 " pid %u not seen yet, forwarding\n", pid);
5507 t->skip += (pid - t->fwd_pid) - 1;
5510 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5512 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5513 GNUNET_break_op (0);
5514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5515 "Received PID %u, ACK %u\n",
5516 pid, t->last_fwd_ack);
5520 tunnel_reset_timeout (t);
5521 dest_id = GNUNET_PEER_search (&msg->destination);
5522 if (dest_id == myid)
5524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5525 " it's for us! sending to clients...\n");
5526 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5527 send_subscribed_clients (message, &msg[1].header, t);
5528 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5531 ttl = ntohl (msg->ttl);
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5535 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5536 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5537 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5541 " not for us, retransmitting...\n");
5543 neighbor = tree_get_first_hop (t->tree, dest_id);
5544 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5545 cinfo->fwd_pid = pid;
5546 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5549 if (GNUNET_YES == t->nobuffer &&
5550 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5552 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5553 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5554 GNUNET_break_op (0);
5557 send_prebuilt_message (message, neighbor, t);
5558 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5564 * Core handler for mesh network traffic going from the origin to all peers
5566 * @param cls closure
5567 * @param message message
5568 * @param peer peer identity this notification is about
5569 * @param atsi performance data
5570 * @param atsi_count number of records in 'atsi'
5571 * @return GNUNET_OK to keep the connection open,
5572 * GNUNET_SYSERR to close it (signal serious error)
5574 * TODO: Check who we got this from, to validate route.
5577 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5578 const struct GNUNET_MessageHeader *message,
5579 const struct GNUNET_ATS_Information *atsi,
5580 unsigned int atsi_count)
5582 struct GNUNET_MESH_Multicast *msg;
5583 struct MeshTunnel *t;
5587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5589 size = ntohs (message->size);
5590 if (sizeof (struct GNUNET_MESH_Multicast) +
5591 sizeof (struct GNUNET_MessageHeader) > size)
5593 GNUNET_break_op (0);
5596 msg = (struct GNUNET_MESH_Multicast *) message;
5597 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5601 /* TODO notify that we dont know that tunnel */
5602 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5603 GNUNET_break_op (0);
5606 pid = ntohl (msg->pid);
5607 if (t->fwd_pid == pid)
5609 /* already seen this packet, drop */
5610 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5612 " Already seen pid %u, DROPPING!\n", pid);
5613 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5619 " pid %u not seen yet, forwarding\n", pid);
5621 t->skip += (pid - t->fwd_pid) - 1;
5623 tunnel_reset_timeout (t);
5625 /* Transmit to locally interested clients */
5626 if (NULL != t->peers &&
5627 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5629 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5630 send_subscribed_clients (message, &msg[1].header, t);
5631 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5634 if (ntohl (msg->ttl) == 0)
5636 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5638 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5641 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5642 tunnel_send_multicast (t, message);
5648 * Core handler for mesh network traffic toward the owner of a tunnel
5650 * @param cls closure
5651 * @param message message
5652 * @param peer peer identity this notification is about
5653 * @param atsi performance data
5654 * @param atsi_count number of records in 'atsi'
5656 * @return GNUNET_OK to keep the connection open,
5657 * GNUNET_SYSERR to close it (signal serious error)
5660 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5661 const struct GNUNET_MessageHeader *message,
5662 const struct GNUNET_ATS_Information *atsi,
5663 unsigned int atsi_count)
5665 struct GNUNET_MESH_ToOrigin *msg;
5666 struct GNUNET_PeerIdentity id;
5667 struct MeshPeerInfo *peer_info;
5668 struct MeshTunnel *t;
5669 struct MeshTunnelChildInfo *cinfo;
5670 GNUNET_PEER_Id predecessor;
5674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5676 size = ntohs (message->size);
5677 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5678 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5680 GNUNET_break_op (0);
5683 msg = (struct GNUNET_MESH_ToOrigin *) message;
5684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5685 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5686 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5687 pid = ntohl (msg->pid);
5691 /* TODO notify that we dont know this tunnel (whom)? */
5692 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5694 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
5695 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5699 cinfo = tunnel_get_neighbor_fc(t, peer);
5706 if (cinfo->bck_pid == pid)
5708 /* already seen this packet, drop */
5709 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5711 " Already seen pid %u, DROPPING!\n", pid);
5712 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5717 " pid %u not seen yet, forwarding\n", pid);
5718 cinfo->bck_pid = pid;
5720 if (NULL != t->owner)
5723 struct GNUNET_MESH_ToOrigin *copy;
5725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5726 " it's for us! sending to clients...\n");
5727 /* TODO signature verification */
5728 memcpy (cbuf, message, size);
5729 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5730 copy->tid = htonl (t->local_tid);
5732 copy->pid = htonl (t->bck_pid);
5733 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5734 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5735 ©->header, GNUNET_NO);
5736 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5740 " not for us, retransmitting...\n");
5742 peer_info = peer_info_get (&msg->oid);
5743 if (NULL == peer_info)
5745 /* unknown origin of tunnel */
5749 predecessor = tree_get_predecessor (t->tree);
5750 if (0 == predecessor)
5752 if (GNUNET_YES == t->destroy)
5754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5755 "to orig received on a dying tunnel %s [%X]\n",
5756 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5760 "unknown to origin at %s\n",
5761 GNUNET_i2s (&my_full_id));
5762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5766 "for tunnel %s [%X]\n",
5767 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5768 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5770 tree_debug (t->tree);
5773 GNUNET_PEER_resolve (predecessor, &id);
5774 send_prebuilt_message (message, &id, t);
5775 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5782 * Core handler for mesh network traffic point-to-point acks.
5784 * @param cls closure
5785 * @param message message
5786 * @param peer peer identity this notification is about
5787 * @param atsi performance data
5788 * @param atsi_count number of records in 'atsi'
5790 * @return GNUNET_OK to keep the connection open,
5791 * GNUNET_SYSERR to close it (signal serious error)
5794 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5795 const struct GNUNET_MessageHeader *message,
5796 const struct GNUNET_ATS_Information *atsi,
5797 unsigned int atsi_count)
5799 struct GNUNET_MESH_ACK *msg;
5800 struct MeshTunnel *t;
5803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5805 msg = (struct GNUNET_MESH_ACK *) message;
5807 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5811 /* TODO notify that we dont know this tunnel (whom)? */
5812 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5815 ack = ntohl (msg->pid);
5816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5818 /* Is this a forward or backward ACK? */
5819 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5821 struct MeshTunnelChildInfo *cinfo;
5824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5825 cinfo = tunnel_get_neighbor_fc (t, peer);
5826 cinfo->fwd_ack = ack;
5827 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5828 tunnel_unlock_fwd_queues (t);
5829 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
5831 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
5832 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
5833 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
5838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5840 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5841 tunnel_unlock_bck_queue (t);
5848 * Core handler for mesh network traffic point-to-point ack polls.
5850 * @param cls closure
5851 * @param message message
5852 * @param peer peer identity this notification is about
5853 * @param atsi performance data
5854 * @param atsi_count number of records in 'atsi'
5856 * @return GNUNET_OK to keep the connection open,
5857 * GNUNET_SYSERR to close it (signal serious error)
5860 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5861 const struct GNUNET_MessageHeader *message,
5862 const struct GNUNET_ATS_Information *atsi,
5863 unsigned int atsi_count)
5865 struct GNUNET_MESH_Poll *msg;
5866 struct MeshTunnel *t;
5868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5871 msg = (struct GNUNET_MESH_Poll *) message;
5873 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5877 /* TODO notify that we dont know this tunnel (whom)? */
5878 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5879 GNUNET_break_op (0);
5883 /* Is this a forward or backward ACK? */
5884 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5886 struct MeshTunnelChildInfo *cinfo;
5888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5889 cinfo = tunnel_get_neighbor_fc (t, peer);
5890 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5891 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5896 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5903 * Core handler for path ACKs
5905 * @param cls closure
5906 * @param message message
5907 * @param peer peer identity this notification is about
5908 * @param atsi performance data
5909 * @param atsi_count number of records in 'atsi'
5911 * @return GNUNET_OK to keep the connection open,
5912 * GNUNET_SYSERR to close it (signal serious error)
5915 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5916 const struct GNUNET_MessageHeader *message,
5917 const struct GNUNET_ATS_Information *atsi,
5918 unsigned int atsi_count)
5920 struct GNUNET_MESH_PathACK *msg;
5921 struct GNUNET_PeerIdentity id;
5922 struct MeshPeerInfo *peer_info;
5923 struct MeshPeerPath *p;
5924 struct MeshTunnel *t;
5926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5927 GNUNET_i2s (&my_full_id));
5928 msg = (struct GNUNET_MESH_PathACK *) message;
5929 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5932 /* TODO notify that we don't know the tunnel */
5933 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5935 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5939 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5941 peer_info = peer_info_get (&msg->peer_id);
5942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5943 GNUNET_i2s (&msg->peer_id));
5944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5947 if (NULL != t->regex_search && t->regex_search->peer == peer_info->id)
5949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5950 "connect_by_string completed, stopping search\n");
5951 regex_cancel_search (t->regex_search);
5952 t->regex_search = NULL;
5955 /* Add paths to peers? */
5956 p = tree_get_path_to_peer (t->tree, peer_info->id);
5959 path_add_to_peers (p, GNUNET_YES);
5967 /* Message for us? */
5968 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5971 if (NULL == t->owner)
5973 GNUNET_break_op (0);
5976 if (NULL != t->dht_get_type)
5978 GNUNET_DHT_get_stop (t->dht_get_type);
5979 t->dht_get_type = NULL;
5981 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5983 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5984 send_client_peer_connected (t, peer_info->id);
5986 if (NULL != peer_info->dhtget)
5988 GNUNET_DHT_get_stop (peer_info->dhtget);
5989 peer_info->dhtget = NULL;
5994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5995 " not for us, retransmitting...\n");
5996 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5997 peer_info = peer_info_get (&msg->oid);
5998 send_prebuilt_message (message, &id, t);
6004 * Core handler for mesh keepalives.
6006 * @param cls closure
6007 * @param message message
6008 * @param peer peer identity this notification is about
6009 * @param atsi performance data
6010 * @param atsi_count number of records in 'atsi'
6011 * @return GNUNET_OK to keep the connection open,
6012 * GNUNET_SYSERR to close it (signal serious error)
6014 * TODO: Check who we got this from, to validate route.
6017 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6018 const struct GNUNET_MessageHeader *message,
6019 const struct GNUNET_ATS_Information *atsi,
6020 unsigned int atsi_count)
6022 struct GNUNET_MESH_TunnelKeepAlive *msg;
6023 struct MeshTunnel *t;
6025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6028 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6029 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6033 /* TODO notify that we dont know that tunnel */
6034 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
6039 tunnel_reset_timeout (t);
6041 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6042 tunnel_send_multicast (t, message);
6049 * Functions to handle messages from core
6051 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6052 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6053 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6054 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6055 sizeof (struct GNUNET_MESH_PathBroken)},
6056 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6057 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6058 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6059 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6060 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6061 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6062 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6063 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6064 sizeof (struct GNUNET_MESH_ACK)},
6065 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6066 sizeof (struct GNUNET_MESH_Poll)},
6067 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6068 sizeof (struct GNUNET_MESH_PathACK)},
6074 /******************************************************************************/
6075 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6076 /******************************************************************************/
6079 * deregister_app: iterator for removing each application registered by a client
6081 * @param cls closure
6082 * @param key the hash of the application id (used to access the hashmap)
6083 * @param value the value stored at the key (client)
6085 * @return GNUNET_OK on success
6088 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6090 struct MeshClient *c = cls;
6092 GNUNET_break (GNUNET_YES ==
6093 GNUNET_CONTAINER_multihashmap_remove (applications, key, c));
6099 * notify_client_connection_failure: notify a client that the connection to the
6100 * requested remote peer is not possible (for instance, no route found)
6101 * Function called when the socket is ready to queue more data. "buf" will be
6102 * NULL and "size" zero if the socket was closed for writing in the meantime.
6104 * @param cls closure
6105 * @param size number of bytes available in buf
6106 * @param buf where the callee should write the message
6107 * @return number of bytes written to buf
6110 notify_client_connection_failure (void *cls, size_t size, void *buf)
6113 struct MeshPeerInfo *peer_info;
6114 struct GNUNET_MESH_PeerControl *msg;
6115 struct GNUNET_PeerIdentity id;
6117 if (0 == size && NULL == buf)
6119 // TODO retry? cancel?
6123 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6124 peer_info = (struct MeshPeerInfo *) cls;
6125 msg = (struct GNUNET_MESH_PeerControl *) buf;
6126 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6127 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6128 // msg->tunnel_id = htonl(peer_info->t->tid);
6129 GNUNET_PEER_resolve (peer_info->id, &id);
6130 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6138 * Send keepalive packets for a peer
6140 * @param cls Closure (tunnel for which to send the keepalive).
6141 * @param tc Notification context.
6144 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6146 struct MeshTunnel *t = cls;
6147 struct GNUNET_MESH_TunnelKeepAlive *msg;
6148 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6151 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6152 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6158 "sending keepalive for tunnel %d\n", t->id.tid);
6160 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6161 msg->header.size = htons (size);
6162 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6163 msg->oid = my_full_id;
6164 msg->tid = htonl (t->id.tid);
6165 tunnel_send_multicast (t, &msg->header);
6167 t->path_refresh_task =
6168 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6169 tunnel_reset_timeout(t);
6174 * Function to process paths received for a new peer addition. The recorded
6175 * paths form the initial tunnel, which can be optimized later.
6176 * Called on each result obtained for the DHT search.
6178 * @param cls closure
6179 * @param exp when will this value expire
6180 * @param key key of the result
6181 * @param get_path path of the get request
6182 * @param get_path_length lenght of get_path
6183 * @param put_path path of the put request
6184 * @param put_path_length length of the put_path
6185 * @param type type of the result
6186 * @param size number of bytes in data
6187 * @param data pointer to the result data
6189 * TODO: re-issue the request after certain time? cancel after X results?
6192 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6193 const struct GNUNET_HashCode * key,
6194 const struct GNUNET_PeerIdentity *get_path,
6195 unsigned int get_path_length,
6196 const struct GNUNET_PeerIdentity *put_path,
6197 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6198 size_t size, const void *data)
6200 struct MeshPathInfo *path_info = cls;
6201 struct MeshPeerPath *p;
6202 struct GNUNET_PeerIdentity pi;
6205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6206 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6209 p = path_build_from_dht (get_path, get_path_length,
6210 put_path, put_path_length);
6211 path_add_to_peers (p, GNUNET_NO);
6213 for (i = 0; i < path_info->peer->ntunnels; i++)
6215 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6216 peer_info_connect (path_info->peer, path_info->t);
6224 * Function to process paths received for a new peer addition. The recorded
6225 * paths form the initial tunnel, which can be optimized later.
6226 * Called on each result obtained for the DHT search.
6228 * @param cls closure
6229 * @param exp when will this value expire
6230 * @param key key of the result
6231 * @param get_path path of the get request
6232 * @param get_path_length lenght of get_path
6233 * @param put_path path of the put request
6234 * @param put_path_length length of the put_path
6235 * @param type type of the result
6236 * @param size number of bytes in data
6237 * @param data pointer to the result data
6240 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6241 const struct GNUNET_HashCode * key,
6242 const struct GNUNET_PeerIdentity *get_path,
6243 unsigned int get_path_length,
6244 const struct GNUNET_PeerIdentity *put_path,
6245 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6246 size_t size, const void *data)
6248 const struct PBlock *pb = data;
6249 const struct GNUNET_PeerIdentity *pi = &pb->id;
6250 struct MeshTunnel *t = cls;
6251 struct MeshPeerInfo *peer_info;
6252 struct MeshPeerPath *p;
6254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6255 if (size != sizeof (struct PBlock))
6257 GNUNET_break_op (0);
6260 if (ntohl(pb->type) != t->type)
6262 GNUNET_break_op (0);
6265 GNUNET_assert (NULL != t->owner);
6266 peer_info = peer_info_get (pi);
6267 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6269 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6271 p = path_build_from_dht (get_path, get_path_length, put_path,
6273 path_add_to_peers (p, GNUNET_NO);
6275 tunnel_add_peer (t, peer_info);
6276 peer_info_connect (peer_info, t);
6280 /******************************************************************************/
6281 /********************* MESH LOCAL HANDLES **************************/
6282 /******************************************************************************/
6286 * Handler for client disconnection
6288 * @param cls closure
6289 * @param client identification of the client; NULL
6290 * for the last call when the server is destroyed
6293 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6295 struct MeshClient *c;
6296 struct MeshClient *next;
6299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6309 if (c->handle != client)
6311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6317 GNUNET_SERVER_client_drop (c->handle);
6318 c->shutting_down = GNUNET_YES;
6319 GNUNET_assert (NULL != c->own_tunnels);
6320 GNUNET_assert (NULL != c->incoming_tunnels);
6321 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6322 &tunnel_destroy_iterator, c);
6323 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6324 &tunnel_destroy_iterator, c);
6325 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6326 &tunnel_destroy_iterator, c);
6327 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6328 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6329 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6331 /* deregister clients applications */
6332 if (NULL != c->apps)
6334 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c);
6335 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6337 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6338 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6340 GNUNET_SCHEDULER_cancel (announce_applications_task);
6341 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6343 if (NULL != c->types)
6344 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6345 for (i = 0; i < c->n_regex; i++)
6347 GNUNET_free (c->regexes[i].regex);
6348 if (NULL != c->regexes[i].h)
6349 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
6351 GNUNET_free_non_null (c->regexes);
6352 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6353 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6355 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6358 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6367 * Handler for new clients
6369 * @param cls closure
6370 * @param client identification of the client
6371 * @param message the actual message, which includes messages the client wants
6374 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6375 const struct GNUNET_MessageHeader *message)
6377 struct GNUNET_MESH_ClientConnect *cc_msg;
6378 struct MeshClient *c;
6379 GNUNET_MESH_ApplicationType *a;
6386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6387 /* Check data sanity */
6388 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6389 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6390 ntypes = ntohs (cc_msg->types);
6391 napps = ntohs (cc_msg->applications);
6393 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6400 /* Create new client structure */
6401 c = GNUNET_malloc (sizeof (struct MeshClient));
6402 c->id = next_client_id++;
6403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6405 GNUNET_SERVER_client_keep (client);
6406 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6409 GNUNET_MESH_ApplicationType at;
6410 struct GNUNET_HashCode hc;
6412 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6413 for (i = 0; i < napps; i++)
6416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6417 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6418 /* store in clients hashmap */
6419 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6420 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6421 /* store in global hashmap, for announcements */
6422 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6423 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6425 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6426 announce_applications_task =
6427 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6433 struct GNUNET_HashCode hc;
6435 t = (uint16_t *) & a[napps];
6436 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6437 for (i = 0; i < ntypes; i++)
6440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6441 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6443 /* store in clients hashmap */
6444 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6445 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6446 /* store in global hashmap */
6447 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6448 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6452 " client has %u+%u subscriptions\n", napps, ntypes);
6454 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6455 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6456 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6457 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6458 GNUNET_SERVER_notification_context_add (nc, client);
6459 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6461 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6467 * Handler for clients announcing available services by a regular expression.
6469 * @param cls closure
6470 * @param client identification of the client
6471 * @param message the actual message, which includes messages the client wants
6474 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6475 const struct GNUNET_MessageHeader *message)
6477 const struct GNUNET_MESH_RegexAnnounce *msg;
6478 struct MeshRegexDescriptor rd;
6479 struct MeshClient *c;
6484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6486 /* Sanity check for client registration */
6487 if (NULL == (c = client_get (client)))
6490 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6495 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6497 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6498 if (NULL != c->partial_regex)
6500 regex = c->partial_regex;
6501 offset = strlen (c->partial_regex);
6502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6503 " continuation, already have %u bytes\n",
6512 regex = GNUNET_realloc (regex, offset + len + 1);
6513 memcpy (®ex[offset], &msg[1], len);
6514 regex[offset + len] = '\0';
6515 if (0 == ntohs (msg->last))
6517 c->partial_regex = regex;
6518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6519 " not ended, stored %u bytes for later\n",
6521 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6525 rd.compression = ntohs (msg->compression_characters);
6527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " compr %u\n", ntohs (rd.compression));
6530 GNUNET_array_append (c->regexes, c->n_regex, rd);
6531 c->partial_regex = NULL;
6532 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6534 c->regex_announce_task = GNUNET_SCHEDULER_add_now (®ex_announce, c);
6540 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6546 * Handler for requests of new tunnels
6548 * @param cls closure
6549 * @param client identification of the client
6550 * @param message the actual message
6553 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6554 const struct GNUNET_MessageHeader *message)
6556 struct GNUNET_MESH_TunnelMessage *t_msg;
6557 struct MeshTunnel *t;
6558 struct MeshClient *c;
6559 MESH_TunnelNumber tid;
6561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6563 /* Sanity check for client registration */
6564 if (NULL == (c = client_get (client)))
6567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6572 /* Message sanity check */
6573 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6580 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6581 /* Sanity check for tunnel numbering */
6582 tid = ntohl (t_msg->tunnel_id);
6583 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6586 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6589 /* Sanity check for duplicate tunnel IDs */
6590 if (NULL != tunnel_get_by_local_id (c, tid))
6593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6597 while (NULL != tunnel_get_by_pi (myid, next_tid))
6598 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6599 t = tunnel_new (myid, next_tid++, c, tid);
6602 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6607 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6609 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6610 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6613 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6619 * Handler for requests of deleting tunnels
6621 * @param cls closure
6622 * @param client identification of the client
6623 * @param message the actual message
6626 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6627 const struct GNUNET_MessageHeader *message)
6629 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6630 struct MeshClient *c;
6631 struct MeshTunnel *t;
6632 MESH_TunnelNumber tid;
6634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6635 "Got a DESTROY TUNNEL from client!\n");
6637 /* Sanity check for client registration */
6638 if (NULL == (c = client_get (client)))
6641 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6646 /* Message sanity check */
6647 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6650 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6654 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6656 /* Retrieve tunnel */
6657 tid = ntohl (tunnel_msg->tunnel_id);
6658 t = tunnel_get_by_local_id(c, tid);
6661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6666 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6668 client_ignore_tunnel (c, t);
6669 tunnel_destroy_empty (t);
6670 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6673 send_client_tunnel_disconnect (t, c);
6674 client_delete_tunnel (c, t);
6676 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6678 tunnel_send_destroy (t, 0);
6679 t->destroy = GNUNET_YES;
6680 /* The tunnel will be destroyed when the last message is transmitted. */
6681 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6687 * Handler for requests of seeting tunnel's speed.
6689 * @param cls Closure (unused).
6690 * @param client Identification of the client.
6691 * @param message The actual message.
6694 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6695 const struct GNUNET_MessageHeader *message)
6697 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6698 struct MeshClient *c;
6699 struct MeshTunnel *t;
6700 MESH_TunnelNumber tid;
6702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6703 "Got a SPEED request from client!\n");
6705 /* Sanity check for client registration */
6706 if (NULL == (c = client_get (client)))
6709 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6715 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6717 /* Retrieve tunnel */
6718 tid = ntohl (tunnel_msg->tunnel_id);
6719 t = tunnel_get_by_local_id(c, tid);
6722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6728 switch (ntohs(message->type))
6730 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6731 t->speed_min = GNUNET_YES;
6733 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6734 t->speed_min = GNUNET_NO;
6739 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6744 * Handler for requests of seeting tunnel's buffering policy.
6746 * @param cls Closure (unused).
6747 * @param client Identification of the client.
6748 * @param message The actual message.
6751 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6752 const struct GNUNET_MessageHeader *message)
6754 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6755 struct MeshClient *c;
6756 struct MeshTunnel *t;
6757 MESH_TunnelNumber tid;
6759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6760 "Got a BUFFER request from client!\n");
6762 /* Sanity check for client registration */
6763 if (NULL == (c = client_get (client)))
6766 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6771 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6773 /* Retrieve tunnel */
6774 tid = ntohl (tunnel_msg->tunnel_id);
6775 t = tunnel_get_by_local_id(c, tid);
6778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6780 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6784 switch (ntohs(message->type))
6786 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6787 t->nobuffer = GNUNET_NO;
6789 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6790 t->nobuffer = GNUNET_YES;
6796 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6801 * Handler for connection requests to new peers
6803 * @param cls closure
6804 * @param client identification of the client
6805 * @param message the actual message (PeerControl)
6808 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6809 const struct GNUNET_MessageHeader *message)
6811 struct GNUNET_MESH_PeerControl *peer_msg;
6812 struct MeshPeerInfo *peer_info;
6813 struct MeshClient *c;
6814 struct MeshTunnel *t;
6815 MESH_TunnelNumber tid;
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6818 /* Sanity check for client registration */
6819 if (NULL == (c = client_get (client)))
6822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6827 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6829 /* Sanity check for message size */
6830 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6833 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6837 /* Tunnel exists? */
6838 tid = ntohl (peer_msg->tunnel_id);
6839 t = tunnel_get_by_local_id (c, tid);
6843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6847 /* Does client own tunnel? */
6848 if (t->owner->handle != client)
6851 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6855 GNUNET_i2s (&peer_msg->peer));
6856 peer_info = peer_info_get (&peer_msg->peer);
6858 tunnel_add_peer (t, peer_info);
6859 peer_info_connect (peer_info, t);
6861 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6867 * Handler for disconnection requests of peers in a tunnel
6869 * @param cls closure
6870 * @param client identification of the client
6871 * @param message the actual message (PeerControl)
6874 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6875 const struct GNUNET_MessageHeader *message)
6877 struct GNUNET_MESH_PeerControl *peer_msg;
6878 struct MeshPeerInfo *peer_info;
6879 struct MeshClient *c;
6880 struct MeshTunnel *t;
6881 MESH_TunnelNumber tid;
6883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6884 /* Sanity check for client registration */
6885 if (NULL == (c = client_get (client)))
6888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6893 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6895 /* Sanity check for message size */
6896 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6903 /* Tunnel exists? */
6904 tid = ntohl (peer_msg->tunnel_id);
6905 t = tunnel_get_by_local_id (c, tid);
6909 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6914 /* Does client own tunnel? */
6915 if (t->owner->handle != client)
6918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6923 GNUNET_i2s (&peer_msg->peer));
6924 /* Is the peer in the tunnel? */
6926 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6927 if (NULL == peer_info)
6930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6934 /* Ok, delete peer from tunnel */
6935 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6936 &peer_msg->peer.hashPubKey);
6938 send_destroy_path (t, peer_info->id);
6939 tunnel_delete_peer (t, peer_info->id);
6940 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6945 * Handler for blacklist requests of peers in a tunnel
6947 * @param cls closure
6948 * @param client identification of the client
6949 * @param message the actual message (PeerControl)
6951 * FIXME implement DHT block bloomfilter
6954 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6955 const struct GNUNET_MessageHeader *message)
6957 struct GNUNET_MESH_PeerControl *peer_msg;
6958 struct MeshClient *c;
6959 struct MeshTunnel *t;
6960 MESH_TunnelNumber tid;
6962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6963 /* Sanity check for client registration */
6964 if (NULL == (c = client_get (client)))
6967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6972 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6974 /* Sanity check for message size */
6975 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6982 /* Tunnel exists? */
6983 tid = ntohl (peer_msg->tunnel_id);
6984 t = tunnel_get_by_local_id (c, tid);
6988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6993 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6994 GNUNET_PEER_intern(&peer_msg->peer));
6999 * Handler for unblacklist requests of peers in a tunnel
7001 * @param cls closure
7002 * @param client identification of the client
7003 * @param message the actual message (PeerControl)
7006 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7007 const struct GNUNET_MessageHeader *message)
7009 struct GNUNET_MESH_PeerControl *peer_msg;
7010 struct MeshClient *c;
7011 struct MeshTunnel *t;
7012 MESH_TunnelNumber tid;
7016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7017 /* Sanity check for client registration */
7018 if (NULL == (c = client_get (client)))
7021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7026 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7028 /* Sanity check for message size */
7029 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7036 /* Tunnel exists? */
7037 tid = ntohl (peer_msg->tunnel_id);
7038 t = tunnel_get_by_local_id (c, tid);
7042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7047 /* if peer is not known, complain */
7048 pid = GNUNET_PEER_search (&peer_msg->peer);
7055 /* search and remove from list */
7056 for (i = 0; i < t->nblacklisted; i++)
7058 if (t->blacklisted[i] == pid)
7060 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7061 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7066 /* if peer hasn't been blacklisted, complain */
7072 * Handler for connection requests to new peers by type
7074 * @param cls closure
7075 * @param client identification of the client
7076 * @param message the actual message (ConnectPeerByType)
7079 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7080 const struct GNUNET_MessageHeader *message)
7082 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7083 struct MeshClient *c;
7084 struct MeshTunnel *t;
7085 struct GNUNET_HashCode hash;
7086 MESH_TunnelNumber tid;
7088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7089 /* Sanity check for client registration */
7090 if (NULL == (c = client_get (client)))
7093 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7098 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7100 /* Sanity check for message size */
7101 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7102 ntohs (connect_msg->header.size))
7105 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7109 /* Tunnel exists? */
7110 tid = ntohl (connect_msg->tunnel_id);
7111 t = tunnel_get_by_local_id (c, tid);
7115 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7119 /* Does client own tunnel? */
7120 if (t->owner->handle != client)
7123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7127 /* Do WE have the service? */
7128 t->type = ntohl (connect_msg->type);
7129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7130 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7131 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7134 /* Yes! Fast forward, add ourselves to the tunnel and send the
7135 * good news to the client, and alert the destination client of
7136 * an incoming tunnel.
7138 * FIXME send a path create to self, avoid code duplication
7140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7141 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7142 peer_info_get (&my_full_id),
7143 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7146 send_client_peer_connected (t, myid);
7147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7148 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7150 t->local_tid_dest = next_local_tid++;
7151 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7152 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7153 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7157 /* Ok, lets find a peer offering the service */
7158 if (NULL != t->dht_get_type)
7160 GNUNET_DHT_get_stop (t->dht_get_type);
7162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7163 GNUNET_h2s (&hash));
7165 GNUNET_DHT_get_start (dht_handle,
7166 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7168 dht_replication_level,
7169 GNUNET_DHT_RO_RECORD_ROUTE |
7170 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7172 &dht_get_type_handler, t);
7174 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7180 * Handler for connection requests to new peers by a string service description.
7182 * @param cls closure
7183 * @param client identification of the client
7184 * @param message the actual message, which includes messages the client wants
7187 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7188 const struct GNUNET_MessageHeader *message)
7190 struct GNUNET_MESH_ConnectPeerByString *msg;
7191 struct MeshRegexSearchInfo *info;
7192 struct MeshTunnel *t;
7193 struct MeshClient *c;
7194 MESH_TunnelNumber tid;
7199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7200 "Connect by string started\n");
7201 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7202 size = htons (message->size);
7204 /* Sanity check for client registration */
7205 if (NULL == (c = client_get (client)))
7208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7213 /* Message size sanity check */
7214 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7221 /* Tunnel exists? */
7222 tid = ntohl (msg->tunnel_id);
7223 t = tunnel_get_by_local_id (c, tid);
7227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7231 /* Does client own tunnel? */
7232 if (t->owner->handle != client)
7235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7240 " on tunnel %s [%u]\n",
7241 GNUNET_i2s(&my_full_id),
7244 /* Only one connect_by_string allowed at the same time! */
7245 /* FIXME: allow more, return handle at api level to cancel, document */
7246 if (NULL != t->regex_search)
7249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7253 /* Find string itself */
7254 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7255 string = (const char *) &msg[1];
7257 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7259 info->description = GNUNET_strndup (string, len);
7260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7262 t->regex_search = info;
7264 info->search_handle = GNUNET_REGEX_search (dht_handle,
7266 ®ex_found_handler, info,
7269 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7275 * Handler for client traffic directed to one peer
7277 * @param cls closure
7278 * @param client identification of the client
7279 * @param message the actual message
7282 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7283 const struct GNUNET_MessageHeader *message)
7285 struct MeshClient *c;
7286 struct MeshTunnel *t;
7287 struct MeshPeerInfo *pi;
7288 struct GNUNET_MESH_Unicast *data_msg;
7289 MESH_TunnelNumber tid;
7292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7293 "Got a unicast request from a client!\n");
7295 /* Sanity check for client registration */
7296 if (NULL == (c = client_get (client)))
7299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7304 data_msg = (struct GNUNET_MESH_Unicast *) message;
7306 /* Sanity check for message size */
7307 size = ntohs (message->size);
7308 if (sizeof (struct GNUNET_MESH_Unicast) +
7309 sizeof (struct GNUNET_MessageHeader) > size)
7312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7316 /* Tunnel exists? */
7317 tid = ntohl (data_msg->tid);
7318 t = tunnel_get_by_local_id (c, tid);
7322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7326 /* Is it a local tunnel? Then, does client own the tunnel? */
7327 if (t->owner->handle != client)
7330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7334 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7335 &data_msg->destination.hashPubKey);
7336 /* Is the selected peer in the tunnel? */
7340 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7344 /* PID should be as expected */
7345 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7349 "Unicast PID, expected %u, got %u\n",
7350 t->fwd_pid + 1, ntohl (data_msg->pid));
7351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7355 /* Ok, everything is correct, send the message
7356 * (pretend we got it from a mesh peer)
7359 /* Work around const limitation */
7360 char buf[ntohs (message->size)] GNUNET_ALIGN;
7361 struct GNUNET_MESH_Unicast *copy;
7363 copy = (struct GNUNET_MESH_Unicast *) buf;
7364 memcpy (buf, data_msg, size);
7365 copy->oid = my_full_id;
7366 copy->tid = htonl (t->id.tid);
7367 copy->ttl = htonl (default_ttl);
7368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7369 " calling generic handler...\n");
7370 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7373 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7380 * Handler for client traffic directed to the origin
7382 * @param cls closure
7383 * @param client identification of the client
7384 * @param message the actual message
7387 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7388 const struct GNUNET_MessageHeader *message)
7390 struct GNUNET_MESH_ToOrigin *data_msg;
7391 struct MeshTunnelClientInfo *clinfo;
7392 struct MeshClient *c;
7393 struct MeshTunnel *t;
7394 MESH_TunnelNumber tid;
7397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7398 "Got a ToOrigin request from a client!\n");
7399 /* Sanity check for client registration */
7400 if (NULL == (c = client_get (client)))
7403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7408 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7410 /* Sanity check for message size */
7411 size = ntohs (message->size);
7412 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7413 sizeof (struct GNUNET_MessageHeader) > size)
7416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7420 /* Tunnel exists? */
7421 tid = ntohl (data_msg->tid);
7422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7423 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7429 t = tunnel_get_by_local_id (c, tid);
7432 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7439 /* It should be sent by someone who has this as incoming tunnel. */
7440 if (GNUNET_NO == client_knows_tunnel (c, t))
7443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7447 /* PID should be as expected */
7448 clinfo = tunnel_get_client_fc (t, c);
7449 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7452 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7453 "To Origin PID, expected %u, got %u\n",
7454 clinfo->bck_pid + 1,
7455 ntohl (data_msg->pid));
7456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7461 /* Ok, everything is correct, send the message
7462 * (pretend we got it from a mesh peer)
7465 char buf[ntohs (message->size)] GNUNET_ALIGN;
7466 struct GNUNET_MESH_ToOrigin *copy;
7468 /* Work around const limitation */
7469 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7470 memcpy (buf, data_msg, size);
7471 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7472 copy->tid = htonl (t->id.tid);
7473 copy->ttl = htonl (default_ttl);
7474 copy->pid = htonl (t->bck_pid + 1);
7476 copy->sender = my_full_id;
7477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7478 " calling generic handler...\n");
7479 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7481 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7488 * Handler for client traffic directed to all peers in a tunnel
7490 * @param cls closure
7491 * @param client identification of the client
7492 * @param message the actual message
7495 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7496 const struct GNUNET_MessageHeader *message)
7498 struct MeshClient *c;
7499 struct MeshTunnel *t;
7500 struct GNUNET_MESH_Multicast *data_msg;
7501 MESH_TunnelNumber tid;
7503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7504 "Got a multicast request from a client!\n");
7506 /* Sanity check for client registration */
7507 if (NULL == (c = client_get (client)))
7510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7515 data_msg = (struct GNUNET_MESH_Multicast *) message;
7517 /* Sanity check for message size */
7518 if (sizeof (struct GNUNET_MESH_Multicast) +
7519 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7526 /* Tunnel exists? */
7527 tid = ntohl (data_msg->tid);
7528 t = tunnel_get_by_local_id (c, tid);
7532 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7533 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7535 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7539 /* Does client own tunnel? */
7540 if (t->owner->handle != client)
7543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7547 /* PID should be as expected */
7548 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7551 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7552 "Multicast PID, expected %u, got %u\n",
7553 t->fwd_pid + 1, ntohl (data_msg->pid));
7554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7559 char buf[ntohs (message->size)] GNUNET_ALIGN;
7560 struct GNUNET_MESH_Multicast *copy;
7562 copy = (struct GNUNET_MESH_Multicast *) buf;
7563 memcpy (buf, message, ntohs (message->size));
7564 copy->oid = my_full_id;
7565 copy->tid = htonl (t->id.tid);
7566 copy->ttl = htonl (default_ttl);
7567 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7569 " calling generic handler...\n");
7570 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7573 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7579 * Handler for client's ACKs for payload traffic.
7581 * @param cls Closure (unused).
7582 * @param client Identification of the client.
7583 * @param message The actual message.
7586 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7587 const struct GNUNET_MessageHeader *message)
7589 struct GNUNET_MESH_LocalAck *msg;
7590 struct MeshTunnel *t;
7591 struct MeshClient *c;
7592 MESH_TunnelNumber tid;
7595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7596 /* Sanity check for client registration */
7597 if (NULL == (c = client_get (client)))
7600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7605 msg = (struct GNUNET_MESH_LocalAck *) message;
7607 /* Tunnel exists? */
7608 tid = ntohl (msg->tunnel_id);
7609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7610 t = tunnel_get_by_local_id (c, tid);
7614 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7616 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7620 ack = ntohl (msg->max_pid);
7621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7623 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
7624 if (NULL != t->owner && t->owner->handle == client)
7626 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7628 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7632 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7633 tunnel_set_client_fwd_ack (t, c, ack);
7634 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7637 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7644 * Iterator over all peers to send a monitoring client info about a tunnel.
7646 * @param cls Closure (message being built).
7647 * @param key Key (hashed tunnel ID, unused).
7648 * @param value Peer info.
7650 * @return GNUNET_YES, to keep iterating.
7653 monitor_peers_iterator (void *cls,
7654 const struct GNUNET_HashCode * key,
7657 struct GNUNET_MESH_LocalMonitor *msg = cls;
7658 struct GNUNET_PeerIdentity *id;
7659 struct MeshPeerInfo *info = value;
7661 id = (struct GNUNET_PeerIdentity *) &msg[1];
7662 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
7665 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7666 "* sending info about peer %s [%u]\n",
7667 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
7675 * Iterator over all tunnels to send a monitoring client info about each tunnel.
7677 * @param cls Closure (client handle).
7678 * @param key Key (hashed tunnel ID, unused).
7679 * @param value Tunnel info.
7681 * @return GNUNET_YES, to keep iterating.
7684 monitor_all_tunnels_iterator (void *cls,
7685 const struct GNUNET_HashCode * key,
7688 struct GNUNET_SERVER_Client *client = cls;
7689 struct MeshTunnel *t = value;
7690 struct GNUNET_MESH_LocalMonitor *msg;
7693 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
7694 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
7695 npeers * sizeof (struct GNUNET_PeerIdentity));
7696 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
7697 msg->tunnel_id = htonl (t->id.tid);
7698 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
7699 npeers * sizeof (struct GNUNET_PeerIdentity));
7700 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
7702 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
7703 monitor_peers_iterator,
7706 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7707 "* sending info about tunnel %s [%u] (%u peers)\n",
7708 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
7710 if (msg->npeers != npeers)
7713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7714 "Get tunnels fail: size %u - iter %u\n",
7715 npeers, msg->npeers);
7718 msg->npeers = htonl (npeers);
7719 GNUNET_SERVER_notification_context_unicast (nc, client,
7720 &msg->header, GNUNET_NO);
7726 * Handler for client's MONITOR request.
7728 * @param cls Closure (unused).
7729 * @param client Identification of the client.
7730 * @param message The actual message.
7733 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
7734 const struct GNUNET_MessageHeader *message)
7736 struct MeshClient *c;
7738 /* Sanity check for client registration */
7739 if (NULL == (c = client_get (client)))
7742 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7746 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7747 "Received get tunnels request from client %u\n",
7749 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
7750 monitor_all_tunnels_iterator,
7752 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7753 "Get tunnels request from client %u completed\n",
7755 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7760 * Data needed to build a Monitor_Tunnel message.
7762 struct MeshMonitorTunnelContext
7765 * Partial message, including peer count.
7767 struct GNUNET_MESH_LocalMonitor *msg;
7770 * Hashmap with positions: peer->position.
7772 struct GNUNET_CONTAINER_MultiHashMap *lookup;
7775 * Index of the parent of each peer in the message, realtive to the absolute
7776 * order in the array (can be in a previous message).
7778 uint32_t parents[1024];
7781 * Peers visited so far in the tree, aka position of the current peer.
7783 unsigned int npeers;
7786 * Client requesting the info.
7788 struct MeshClient *c;
7793 * Send a client a message about the structure of a tunnel.
7795 * @param ctx Context of the tunnel iteration, with info regarding the state
7796 * of the execution and the number of peers visited for this message.
7799 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
7801 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
7802 struct GNUNET_PeerIdentity *pid;
7803 unsigned int *parent;
7806 size = sizeof (struct GNUNET_MESH_LocalMonitor);
7807 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
7808 resp->header.size = htons (size);
7809 pid = (struct GNUNET_PeerIdentity *) &resp[1];
7810 parent = (unsigned int *) &pid[resp->npeers];
7811 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
7812 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
7813 &resp->header, GNUNET_NO);
7817 * Iterator over a tunnel tree to build a message containing all peers
7818 * the in the tunnel, including relay nodes.
7820 * @param cls Closure (pointer to pointer of message being built).
7821 * @param peer Short ID of a peer.
7822 * @param parent Short ID of the @c peer 's parent.
7825 tunnel_tree_iterator (void *cls,
7826 GNUNET_PEER_Id peer,
7827 GNUNET_PEER_Id parent)
7829 struct MeshMonitorTunnelContext *ctx = cls;
7830 struct GNUNET_MESH_LocalMonitor *msg;
7831 struct GNUNET_PeerIdentity *pid;
7832 struct GNUNET_PeerIdentity ppid;
7835 pid = (struct GNUNET_PeerIdentity *) &msg[1];
7836 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
7837 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
7838 &pid[msg->npeers].hashPubKey,
7839 (void *) (long) ctx->npeers,
7840 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7841 GNUNET_PEER_resolve (parent, &ppid);
7842 ctx->parents[msg->npeers] =
7843 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
7849 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
7851 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
7854 send_client_tunnel_info (ctx);
7861 * Handler for client's MONITOR_TUNNEL request.
7863 * @param cls Closure (unused).
7864 * @param client Identification of the client.
7865 * @param message The actual message.
7868 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
7869 const struct GNUNET_MessageHeader *message)
7871 const struct GNUNET_MESH_LocalMonitor *msg;
7872 struct GNUNET_MESH_LocalMonitor *resp;
7873 struct MeshMonitorTunnelContext ctx;
7874 struct MeshClient *c;
7875 struct MeshTunnel *t;
7877 /* Sanity check for client registration */
7878 if (NULL == (c = client_get (client)))
7881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7885 msg = (struct GNUNET_MESH_LocalMonitor *) message;
7886 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7887 "Received tunnel info request from client %u for tunnel %s[%X]\n",
7890 ntohl (msg->tunnel_id));
7891 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
7894 /* We don't know the tunnel */
7895 struct GNUNET_MESH_LocalMonitor warn;
7898 warn.npeers = htonl (UINT_MAX);
7899 GNUNET_SERVER_notification_context_unicast (nc, client,
7902 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7906 /* Initialize context */
7907 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
7911 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_YES);
7914 /* Collect and send information */
7915 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
7916 send_client_tunnel_info (&ctx);
7919 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
7922 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7923 "Monitor tunnel request from client %u completed\n",
7925 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7930 * Functions to handle messages from clients
7932 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7933 {&handle_local_new_client, NULL,
7934 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7935 {&handle_local_announce_regex, NULL,
7936 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7937 {&handle_local_tunnel_create, NULL,
7938 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7939 sizeof (struct GNUNET_MESH_TunnelMessage)},
7940 {&handle_local_tunnel_destroy, NULL,
7941 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7942 sizeof (struct GNUNET_MESH_TunnelMessage)},
7943 {&handle_local_tunnel_speed, NULL,
7944 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7945 sizeof (struct GNUNET_MESH_TunnelMessage)},
7946 {&handle_local_tunnel_speed, NULL,
7947 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7948 sizeof (struct GNUNET_MESH_TunnelMessage)},
7949 {&handle_local_tunnel_buffer, NULL,
7950 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7951 sizeof (struct GNUNET_MESH_TunnelMessage)},
7952 {&handle_local_tunnel_buffer, NULL,
7953 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7954 sizeof (struct GNUNET_MESH_TunnelMessage)},
7955 {&handle_local_connect_add, NULL,
7956 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7957 sizeof (struct GNUNET_MESH_PeerControl)},
7958 {&handle_local_connect_del, NULL,
7959 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7960 sizeof (struct GNUNET_MESH_PeerControl)},
7961 {&handle_local_blacklist, NULL,
7962 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7963 sizeof (struct GNUNET_MESH_PeerControl)},
7964 {&handle_local_unblacklist, NULL,
7965 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7966 sizeof (struct GNUNET_MESH_PeerControl)},
7967 {&handle_local_connect_by_type, NULL,
7968 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7969 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7970 {&handle_local_connect_by_string, NULL,
7971 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7972 {&handle_local_unicast, NULL,
7973 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7974 {&handle_local_to_origin, NULL,
7975 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7976 {&handle_local_multicast, NULL,
7977 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7978 {&handle_local_ack, NULL,
7979 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7980 sizeof (struct GNUNET_MESH_LocalAck)},
7981 {&handle_local_get_tunnels, NULL,
7982 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
7983 sizeof (struct GNUNET_MessageHeader)},
7984 {&handle_local_show_tunnel, NULL,
7985 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
7986 sizeof (struct GNUNET_MESH_LocalMonitor)},
7992 * To be called on core init/fail.
7994 * @param cls service closure
7995 * @param server handle to the server for this service
7996 * @param identity the public identity of this peer
7999 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8000 const struct GNUNET_PeerIdentity *identity)
8003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8004 core_handle = server;
8005 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8008 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8011 GNUNET_i2s (identity));
8012 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8014 GNUNET_i2s (&my_full_id));
8015 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8017 GNUNET_abort(); // Try harder
8024 * Method called whenever a given peer connects.
8026 * @param cls closure
8027 * @param peer peer identity this notification is about
8028 * @param atsi performance data for the connection
8029 * @param atsi_count number of records in 'atsi'
8032 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8033 const struct GNUNET_ATS_Information *atsi,
8034 unsigned int atsi_count)
8036 struct MeshPeerInfo *peer_info;
8037 struct MeshPeerPath *path;
8039 DEBUG_CONN ("Peer connected\n");
8040 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8041 peer_info = peer_info_get (peer);
8042 if (myid == peer_info->id)
8044 DEBUG_CONN (" (self)\n");
8049 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8051 path = path_new (2);
8052 path->peers[0] = myid;
8053 path->peers[1] = peer_info->id;
8054 GNUNET_PEER_change_rc (myid, 1);
8055 GNUNET_PEER_change_rc (peer_info->id, 1);
8056 peer_info_add_path (peer_info, path, GNUNET_YES);
8057 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8063 * Method called whenever a peer disconnects.
8065 * @param cls closure
8066 * @param peer peer identity this notification is about
8069 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8071 struct MeshPeerInfo *pi;
8072 struct MeshPeerQueue *q;
8073 struct MeshPeerQueue *n;
8075 DEBUG_CONN ("Peer disconnected\n");
8076 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8086 /* TODO try to reroute this traffic instead */
8087 queue_destroy(q, GNUNET_YES);
8090 if (NULL != pi->core_transmit)
8092 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8093 pi->core_transmit = NULL;
8095 peer_info_remove_path (pi, pi->id, myid);
8098 DEBUG_CONN (" (self)\n");
8100 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8105 /******************************************************************************/
8106 /************************ MAIN FUNCTIONS ****************************/
8107 /******************************************************************************/
8110 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8112 * @param cls closure
8113 * @param key current key code
8114 * @param value value in the hash map
8115 * @return GNUNET_YES if we should continue to iterate,
8119 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8121 struct MeshTunnel *t = value;
8128 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8130 * @param cls closure
8131 * @param key current key code
8132 * @param value value in the hash map
8133 * @return GNUNET_YES if we should continue to iterate,
8137 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8139 struct MeshPeerInfo *p = value;
8140 struct MeshPeerQueue *q;
8141 struct MeshPeerQueue *n;
8149 queue_destroy(q, GNUNET_YES);
8153 peer_info_destroy (p);
8159 * Task run during shutdown.
8165 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8169 if (core_handle != NULL)
8171 GNUNET_CORE_disconnect (core_handle);
8176 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
8179 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8180 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8181 if (dht_handle != NULL)
8183 GNUNET_DHT_disconnect (dht_handle);
8188 GNUNET_SERVER_notification_context_destroy (nc);
8191 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8193 GNUNET_SCHEDULER_cancel (announce_id_task);
8194 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8196 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8198 GNUNET_SCHEDULER_cancel (announce_applications_task);
8199 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8206 * Callback for hostkey read/generation.
8208 * @param cls Closure (Configuration handle).
8209 * @param pk The ECC private key.
8210 * @param emsg Error message, if any.
8213 key_generation_cb (void *cls,
8214 struct GNUNET_CRYPTO_EccPrivateKey *pk,
8217 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8218 struct MeshPeerInfo *peer;
8219 struct MeshPeerPath *p;
8224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8225 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8227 GNUNET_SCHEDULER_shutdown ();
8230 my_private_key = pk;
8231 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
8232 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8233 &my_full_id.hashPubKey);
8234 myid = GNUNET_PEER_intern (&my_full_id);
8235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8236 "Mesh for peer [%s] starting\n",
8237 GNUNET_i2s(&my_full_id));
8239 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8240 NULL, /* Closure passed to MESH functions */
8241 &core_init, /* Call core_init once connected */
8242 &core_connect, /* Handle connects */
8243 &core_disconnect, /* remove peers on disconnects */
8244 NULL, /* Don't notify about all incoming messages */
8245 GNUNET_NO, /* For header only in notification */
8246 NULL, /* Don't notify about all outbound messages */
8247 GNUNET_NO, /* For header-only out notification */
8248 core_handlers); /* Register these handlers */
8250 if (core_handle == NULL)
8253 GNUNET_SCHEDULER_shutdown ();
8258 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8261 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8262 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8263 GNUNET_SERVER_disconnect_notify (server_handle,
8264 &handle_local_client_disconnect, NULL);
8268 clients_tail = NULL;
8271 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8272 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8274 /* Create a peer_info for the local peer */
8275 peer = peer_info_get (&my_full_id);
8278 GNUNET_PEER_change_rc (myid, 1);
8279 peer_info_add_path (peer, p, GNUNET_YES);
8280 GNUNET_SERVER_resume (server_handle);
8281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8286 * Process mesh requests.
8288 * @param cls closure
8289 * @param server the initialized server
8290 * @param c configuration to use
8293 run (void *cls, struct GNUNET_SERVER_Handle *server,
8294 const struct GNUNET_CONFIGURATION_Handle *c)
8298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8299 server_handle = server;
8302 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
8305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8307 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8308 "mesh", "peer/privatekey");
8309 GNUNET_SCHEDULER_shutdown ();
8314 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8315 &refresh_path_time))
8317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8319 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8320 "mesh", "refresh path time");
8321 GNUNET_SCHEDULER_shutdown ();
8326 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8327 &app_announce_time))
8329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8331 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8332 "mesh", "app announce time");
8333 GNUNET_SCHEDULER_shutdown ();
8336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8337 "APP_ANNOUNCE_TIME %llu ms\n",
8338 app_announce_time.rel_value);
8340 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8345 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8346 "mesh", "id announce time");
8347 GNUNET_SCHEDULER_shutdown ();
8352 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8357 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8358 "mesh", "connect timeout");
8359 GNUNET_SCHEDULER_shutdown ();
8364 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8369 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8370 "mesh", "max msgs queue");
8371 GNUNET_SCHEDULER_shutdown ();
8376 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8381 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8382 "mesh", "max tunnels");
8383 GNUNET_SCHEDULER_shutdown ();
8388 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8393 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8394 "mesh", "default ttl", 64);
8399 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8403 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8404 "mesh", "max peers", 1000);
8409 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8410 &dht_replication_level))
8412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8414 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8415 "mesh", "dht replication level", 3);
8416 dht_replication_level = 3;
8419 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8420 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8421 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8422 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8423 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8425 dht_handle = GNUNET_DHT_connect (c, 64);
8426 if (NULL == dht_handle)
8430 stats = GNUNET_STATISTICS_create ("mesh", c);
8432 GNUNET_SERVER_suspend (server_handle);
8433 /* Scheduled the task to clean up when shutdown is called */
8434 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8436 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
8439 GNUNET_free (keyfile);
8444 * The main function for the mesh service.
8446 * @param argc number of arguments from the command line
8447 * @param argv command line arguments
8448 * @return 0 ok, 1 on error
8451 main (int argc, char *const *argv)
8456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8457 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8459 ret = (GNUNET_OK == r) ? 0 : 1;
8460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8464 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8465 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8466 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);