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 * Number of peers that have been added to the tunnel
493 unsigned int peers_total;
496 * Client owner of the tunnel, if any
498 struct MeshClient *owner;
501 * Clients that have been informed about and want to stay in the tunnel.
503 struct MeshClient **clients;
506 * Flow control info for each client.
508 struct MeshTunnelClientInfo *clients_fc;
511 * Number of elements in clients/clients_fc
513 unsigned int nclients;
516 * Clients that have been informed but requested to leave the tunnel.
518 struct MeshClient **ignore;
521 * Number of elements in clients
523 unsigned int nignore;
528 GNUNET_PEER_Id *blacklisted;
531 * Number of elements in blacklisted
533 unsigned int nblacklisted;
536 * Bloomfilter (for peer identities) to stop circular routes
538 char bloomfilter[MESH_BLOOM_SIZE];
543 struct MeshTunnelTree *tree;
546 * Application type we are looking for in this tunnel
548 GNUNET_MESH_ApplicationType type;
551 * Used to search peers offering a service
553 struct GNUNET_DHT_GetHandle *dht_get_type;
556 * Handle for the regex search for a connect_by_string
558 struct MeshRegexSearchInfo *regex_search;
561 * Task to keep the used paths alive
563 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
566 * Task to destroy the tunnel after timeout
568 * FIXME: merge the two? a tunnel will have either
569 * a path refresh OR a timeout, never both!
571 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
574 * Flag to signal the destruction of the tunnel.
575 * If this is set GNUNET_YES the tunnel will be destroyed
576 * when the queue is empty.
581 * Total messages pending for this tunnels, payload or not.
583 unsigned int pending_messages;
586 * If the tunnel is empty, destoy it.
588 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
593 * Info about a child node in a tunnel, needed to perform flow control.
595 struct MeshTunnelChildInfo
598 * ID of the child node.
618 * Maximum PID allowed (FWD ACK received).
623 * Last ACK sent to that child (BCK ACK).
628 * Circular buffer pointing to MeshPeerQueue elements for all
629 * payload traffic going to this child.
630 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
632 struct MeshPeerQueue **send_buffer;
635 * Index of the oldest element in the send_buffer.
637 unsigned int send_buffer_start;
640 * How many elements are already in the buffer.
642 unsigned int send_buffer_n;
645 * Tunnel this info is about
647 struct MeshTunnel *t;
650 * Task to poll peer in case of a stall.
652 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
655 * Time to use for next polling call.
657 struct GNUNET_TIME_Relative fc_poll_time;
662 * Info about a leaf client of a tunnel, needed to perform flow control.
664 struct MeshTunnelClientInfo
667 * PID of the last packet sent to the client (FWD).
672 * PID of the last packet received from the client (BCK).
677 * Maximum PID allowed (FWD ACK received).
682 * Last ACK sent to that child (BCK ACK).
690 * Info collected during iteration of child nodes in order to get the ACK value
693 struct MeshTunnelChildIteratorContext
696 * Tunnel whose info is being collected.
698 struct MeshTunnel *t;
701 * Is this context initialized? Is the value in max_child_ack valid?
706 * Maximum child ACK so far.
708 uint32_t max_child_ack;
711 * Number of children nodes
713 unsigned int nchildren;
718 * Info needed to work with tunnel paths and peers
725 struct MeshTunnel *t;
728 * Neighbouring peer to whom we send the packet to
730 struct MeshPeerInfo *peer;
735 struct MeshPeerPath *path;
740 * Struct containing information about a client of the service
747 struct MeshClient *next;
752 struct MeshClient *prev;
755 * Tunnels that belong to this client, indexed by local id
757 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
760 * Tunnels this client has accepted, indexed by incoming local id
762 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
765 * Tunnels this client has rejected, indexed by incoming local id
767 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
769 * Handle to communicate with the client
771 struct GNUNET_SERVER_Client *handle;
774 * Applications that this client has claimed to provide: H(app) = app.
776 struct GNUNET_CONTAINER_MultiHashMap *apps;
779 * Messages that this client has declared interest in
781 struct GNUNET_CONTAINER_MultiHashMap *types;
784 * Whether the client is active or shutting down (don't send confirmations
785 * to a client that is shutting down.
790 * ID of the client, mainly for debug messages
795 * Regular expressions describing the services offered by this client.
797 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
800 * Number of regular expressions in regexes.
802 unsigned int n_regex;
805 * Task to refresh all regular expresions in the DHT.
807 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
810 * Tmp store for partially retrieved regex.
817 /******************************************************************************/
818 /************************ DEBUG FUNCTIONS ****************************/
819 /******************************************************************************/
823 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
824 * @param cls string to print
825 * @param success GNUNET_OK if the PUT was transmitted,
826 * GNUNET_NO on timeout,
827 * GNUNET_SYSERR on disconnect from service
828 * after the PUT message was transmitted
829 * (so we don't know if it was received or not)
834 mesh_debug (void *cls, int success)
838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
842 unsigned int debug_fwd_ack;
843 unsigned int debug_bck_ack;
847 /******************************************************************************/
848 /*********************** GLOBAL VARIABLES ****************************/
849 /******************************************************************************/
851 /************************** Configuration parameters **************************/
854 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
856 static struct GNUNET_TIME_Relative refresh_path_time;
859 * How often to PUT local application numbers in the DHT.
861 static struct GNUNET_TIME_Relative app_announce_time;
864 * How often to PUT own ID in the DHT.
866 static struct GNUNET_TIME_Relative id_announce_time;
869 * Maximum time allowed to connect to a peer found by string.
871 static struct GNUNET_TIME_Relative connect_timeout;
874 * Default TTL for payload packets.
876 static unsigned long long default_ttl;
879 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
881 static unsigned long long dht_replication_level;
884 * How many tunnels are we willing to maintain.
885 * Local tunnels are always allowed, even if there are more tunnels than max.
887 static unsigned long long max_tunnels;
890 * How many messages *in total* are we willing to queue, divided by number of
891 * tunnels to get tunnel queue size.
893 static unsigned long long max_msgs_queue;
896 * How many peers do we want to remember?
898 static unsigned long long max_peers;
901 /*************************** Static global variables **************************/
904 * Hostkey generation context
906 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
909 * DLL with all the clients, head.
911 static struct MeshClient *clients;
914 * DLL with all the clients, tail.
916 static struct MeshClient *clients_tail;
919 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
921 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
924 * Number of tunnels known.
926 static unsigned long long n_tunnels;
929 * Tunnels incoming, indexed by MESH_TunnelNumber
930 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
932 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
935 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
937 static struct GNUNET_CONTAINER_MultiHashMap *peers;
940 * Handle to communicate with transport
942 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
945 * Handle to communicate with core.
947 static struct GNUNET_CORE_Handle *core_handle;
952 static struct GNUNET_DHT_Handle *dht_handle;
957 static struct GNUNET_SERVER_Handle *server_handle;
960 * Handle to the statistics service.
962 static struct GNUNET_STATISTICS_Handle *stats;
965 * Notification context, to send messages to local clients.
967 static struct GNUNET_SERVER_NotificationContext *nc;
970 * Local peer own ID (memory efficient handle).
972 static GNUNET_PEER_Id myid;
975 * Local peer own ID (full value).
977 static struct GNUNET_PeerIdentity my_full_id;
982 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
987 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
990 * Tunnel ID for the next created tunnel (global tunnel number).
992 static MESH_TunnelNumber next_tid;
995 * Tunnel ID for the next incoming tunnel (local tunnel number).
997 static MESH_TunnelNumber next_local_tid;
1000 * All application types provided by this peer: H(app) = *Client.
1002 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1005 * All message types clients of this peer are interested in.
1007 static struct GNUNET_CONTAINER_MultiHashMap *types;
1010 * Task to periodically announce provided applications.
1012 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1015 * Task to periodically announce itself in the network.
1017 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1020 * Next ID to assign to a client.
1022 unsigned int next_client_id;
1025 /******************************************************************************/
1026 /*********************** DECLARATIONS **************************/
1027 /******************************************************************************/
1030 * Function to process paths received for a new peer addition. The recorded
1031 * paths form the initial tunnel, which can be optimized later.
1032 * Called on each result obtained for the DHT search.
1034 * @param cls closure
1035 * @param exp when will this value expire
1036 * @param key key of the result
1037 * @param type type of the result
1038 * @param size number of bytes in data
1039 * @param data pointer to the result data
1042 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1043 const struct GNUNET_HashCode * key,
1044 const struct GNUNET_PeerIdentity *get_path,
1045 unsigned int get_path_length,
1046 const struct GNUNET_PeerIdentity *put_path,
1047 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1048 size_t size, const void *data);
1052 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1053 * and insert it in the appropiate structures if the peer is not known yet.
1055 * @param peer Full identity of the peer.
1057 * @return Existing or newly created peer info.
1059 static struct MeshPeerInfo *
1060 peer_info_get (const struct GNUNET_PeerIdentity *peer);
1064 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1065 * and insert it in the appropiate structures if the peer is not known yet.
1067 * @param peer Short identity of the peer.
1069 * @return Existing or newly created peer info.
1071 static struct MeshPeerInfo *
1072 peer_info_get_short (const GNUNET_PEER_Id peer);
1076 * Try to establish a new connection to this peer.
1077 * Use the best path for the given tunnel.
1078 * If the peer doesn't have any path to it yet, try to get one.
1079 * If the peer already has some path, send a CREATE PATH towards it.
1081 * @param peer PeerInfo of the peer.
1082 * @param t Tunnel for which to create the path, if possible.
1085 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1089 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1090 * to obtain a local peer -> destination path and interning the peer ids.
1092 * @return Newly allocated and created path
1094 static struct MeshPeerPath *
1095 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1096 unsigned int get_path_length,
1097 const struct GNUNET_PeerIdentity *put_path,
1098 unsigned int put_path_length);
1102 * Adds a path to the peer_infos of all the peers in the path
1104 * @param p Path to process.
1105 * @param confirmed Whether we know if the path works or not.
1108 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1112 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1113 * needed rescources.
1114 * If peer already exists, reevaluate shortest path and change if different.
1116 * @param t Tunnel we want to add a new peer to
1117 * @param peer PeerInfo of the peer being added
1121 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1125 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1126 * that are no longer needed, as well as nodes of no longer reachable peers.
1127 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1129 * @param t Tunnel from which to remove the path.
1130 * @param peer Short id of the peer which should be removed.
1133 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1137 * Search for a tunnel by global ID using full PeerIdentities.
1139 * @param oid owner of the tunnel.
1140 * @param tid global tunnel number.
1142 * @return tunnel handler, NULL if doesn't exist.
1144 static struct MeshTunnel *
1145 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1149 * Delete an active client from the tunnel.
1155 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1158 * Notify a tunnel that a connection has broken that affects at least
1159 * some of its peers.
1161 * @param t Tunnel affected.
1162 * @param p1 Peer that got disconnected from p2.
1163 * @param p2 Peer that got disconnected from p1.
1165 * @return Short ID of the peer disconnected (either p1 or p2).
1166 * 0 if the tunnel remained unaffected.
1168 static GNUNET_PEER_Id
1169 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1174 * Get the current ack value for a tunnel, for data going from root to leaves,
1175 * taking in account the tunnel mode and the status of all children and clients.
1179 * @return Maximum PID allowed.
1182 tunnel_get_fwd_ack (struct MeshTunnel *t);
1186 * Add a client to a tunnel, initializing all needed data structures.
1188 * @param t Tunnel to which add the client.
1189 * @param c Client which to add to the tunnel.
1192 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1196 * @brief Queue and pass message to core when possible.
1198 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1199 * and accounts for it. In case the queue is full, the message is dropped and
1202 * Otherwise, message is treated as internal and allowed to go regardless of
1205 * @param cls Closure (@c type dependant). It will be used by queue_send to
1206 * build the message to be sent if not already prebuilt.
1207 * @param type Type of the message, 0 for a raw message.
1208 * @param size Size of the message.
1209 * @param dst Neighbor to send message to.
1210 * @param t Tunnel this message belongs to.
1213 queue_add (void *cls, uint16_t type, size_t size,
1214 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1218 * Free a transmission that was already queued with all resources
1219 * associated to the request.
1221 * @param queue Queue handler to cancel.
1222 * @param clear_cls Is it necessary to free associated cls?
1225 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1229 * @brief Get the next transmittable message from the queue.
1231 * This will be the head, except in the case of being a data packet
1232 * not allowed by the destination peer.
1234 * @param peer Destination peer.
1236 * @return The next viable MeshPeerQueue element to send to that peer.
1237 * NULL when there are no transmittable messages.
1239 struct MeshPeerQueue *
1240 queue_get_next (const struct MeshPeerInfo *peer);
1244 * Core callback to write a queued packet to core buffer
1246 * @param cls Closure (peer info).
1247 * @param size Number of bytes available in buf.
1248 * @param buf Where the to write the message.
1250 * @return number of bytes written to buf
1253 queue_send (void *cls, size_t size, void *buf);
1255 /******************************************************************************/
1256 /************************ REGEX INTEGRATION ****************************/
1257 /******************************************************************************/
1260 * Cancel a mesh regex search and free resources.
1263 regex_cancel_search (struct MeshRegexSearchInfo *regex_search)
1265 DEBUG_REGEX ("Search for %s canelled.\n", regex_search->description);
1266 GNUNET_REGEX_search_cancel (regex_search->search_handle);
1267 if (0 < regex_search->n_peers)
1268 GNUNET_free (regex_search->peers);
1269 if (GNUNET_SCHEDULER_NO_TASK != regex_search->timeout)
1271 GNUNET_SCHEDULER_cancel(regex_search->timeout);
1273 GNUNET_free (regex_search);
1278 * Function called if the connect attempt to a peer found via
1279 * connect_by_string times out. Try to connect to another peer, if any.
1280 * Otherwise try to reconnect to the same peer.
1282 * @param cls Closure (info about regex search).
1283 * @param tc TaskContext.
1286 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1288 struct MeshRegexSearchInfo *info = cls;
1289 struct MeshPeerInfo *peer_info;
1293 DEBUG_REGEX ("Regex connect timeout\n");
1294 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1295 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1297 DEBUG_REGEX (" due to shutdown\n");
1302 DEBUG_REGEX (" timed out: %u\n", old);
1304 if (0 < info->n_peers)
1306 // Select next peer, put current in that spot.
1307 id = info->peers[info->i_peer];
1308 info->peers[info->i_peer] = info->peer;
1309 info->i_peer = (info->i_peer + 1) % info->n_peers;
1313 // Try to connect to same peer again.
1316 DEBUG_REGEX (" trying: %u\n", id);
1318 peer_info = peer_info_get_short(id);
1319 tunnel_add_peer (info->t, peer_info);
1321 tunnel_delete_peer (info->t, old);
1322 peer_info_connect (peer_info, info->t);
1323 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1324 ®ex_connect_timeout,
1326 DEBUG_REGEX ("Regex connect timeout END\n");
1331 * Function to process DHT string to regex matching.
1332 * Called on each result obtained for the DHT search.
1334 * @param cls Closure provided in GNUNET_REGEX_search.
1335 * @param id Peer providing a regex that matches the string.
1336 * @param get_path Path of the get request.
1337 * @param get_path_length Lenght of get_path.
1338 * @param put_path Path of the put request.
1339 * @param put_path_length Length of the put_path.
1342 regex_found_handler (void *cls,
1343 const struct GNUNET_PeerIdentity *id,
1344 const struct GNUNET_PeerIdentity *get_path,
1345 unsigned int get_path_length,
1346 const struct GNUNET_PeerIdentity *put_path,
1347 unsigned int put_path_length)
1349 struct MeshRegexSearchInfo *info = cls;
1350 struct MeshPeerPath *p;
1351 struct MeshPeerInfo *peer_info;
1353 DEBUG_REGEX ("Got regex results from DHT!\n");
1354 DEBUG_REGEX (" for %s\n", info->description);
1356 peer_info = peer_info_get (id);
1357 p = path_build_from_dht (get_path, get_path_length,
1358 put_path, put_path_length);
1359 path_add_to_peers (p, GNUNET_NO);
1362 tunnel_add_peer (info->t, peer_info);
1363 peer_info_connect (peer_info, info->t);
1364 if (0 == info->peer)
1366 info->peer = peer_info->id;
1370 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
1373 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1376 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1377 ®ex_connect_timeout,
1385 * Store the regular expression describing a local service into the DHT.
1387 * @param regex The regular expresion.
1390 regex_put (struct MeshRegexDescriptor *regex)
1392 DEBUG_REGEX (" regex_put (%s) start\n", regex->regex);
1393 if (NULL == regex->h)
1395 DEBUG_REGEX (" first put, creating DFA\n");
1396 regex->h = GNUNET_REGEX_announce (dht_handle,
1404 DEBUG_REGEX (" not first put, using cached data\n");
1405 GNUNET_REGEX_reannounce (regex->h);
1407 DEBUG_REGEX (" regex_put (%s) end\n", regex->regex);
1412 * Periodically announce what applications are provided by local clients
1415 * @param cls closure
1416 * @param tc task context
1419 regex_announce (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1421 struct MeshClient *c = cls;
1424 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1425 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1427 DEBUG_REGEX ("Starting announce for regex\n");
1428 for (i = 0; i < c->n_regex; i++)
1429 regex_put (&c->regexes[i]);
1430 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1433 DEBUG_REGEX ("Finished announce for regex\n");
1437 /******************************************************************************/
1438 /************************ PERIODIC FUNCTIONS ****************************/
1439 /******************************************************************************/
1442 * Announce iterator over for each application provided by the peer
1444 * @param cls closure
1445 * @param key current key code
1446 * @param value value in the hash map
1447 * @return GNUNET_YES if we should continue to
1452 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1454 struct PBlock block;
1455 struct MeshClient *c;
1457 block.id = my_full_id;
1458 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1459 GNUNET_assert(NULL != c);
1460 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1461 if (0 == block.type)
1466 block.type = htonl (block.type);
1468 GNUNET_break (NULL !=
1469 GNUNET_DHT_put (dht_handle, key,
1470 dht_replication_level,
1471 GNUNET_DHT_RO_RECORD_ROUTE |
1472 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1473 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1475 (const char *) &block,
1476 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1477 app_announce_time, NULL, NULL));
1483 * Periodically announce what applications are provided by local clients
1486 * @param cls closure
1487 * @param tc task context
1490 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1492 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1494 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1498 DEBUG_DHT ("Starting PUT for apps\n");
1500 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1502 announce_applications_task =
1503 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1505 DEBUG_DHT ("Finished PUT for apps\n");
1510 * Periodically announce self id in the DHT
1512 * @param cls closure
1513 * @param tc task context
1516 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1518 struct PBlock block;
1520 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1522 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1526 * - Set data expiration in function of X
1527 * - Adapt X to churn
1529 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1531 block.id = my_full_id;
1532 block.type = htonl (0);
1533 GNUNET_DHT_put (dht_handle, /* DHT handle */
1534 &my_full_id.hashPubKey, /* Key to use */
1535 dht_replication_level, /* Replication level */
1536 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1537 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1538 sizeof (block), /* Size of the data */
1539 (const char *) &block, /* Data itself */
1540 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1541 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1542 NULL, /* Continuation */
1543 NULL); /* Continuation closure */
1545 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1549 /******************************************************************************/
1550 /****************** GENERAL HELPER FUNCTIONS ************************/
1551 /******************************************************************************/
1554 * Decrements the reference counter and frees all resources if needed
1556 * @param mesh_data Data Descriptor used in a multicast message.
1557 * Freed no longer needed (last message).
1560 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1562 if (0 == --(mesh_data->reference_counter))
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1565 GNUNET_free (mesh_data->data);
1566 GNUNET_free (mesh_data);
1572 * Check if client has registered with the service and has not disconnected
1574 * @param client the client to check
1576 * @return non-NULL if client exists in the global DLL
1578 static struct MeshClient *
1579 client_get (struct GNUNET_SERVER_Client *client)
1581 struct MeshClient *c;
1586 if (c->handle == client)
1595 * Checks if a given client has subscribed to certain message type
1597 * @param message_type Type of message to check
1598 * @param c Client to check
1600 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1602 * FIXME: use of crypto_hash slows it down
1603 * The hash function alone takes 8-10us out of the ~55us for the whole
1604 * process of retransmitting the message from one local client to another.
1605 * Find faster implementation!
1608 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1610 struct GNUNET_HashCode hc;
1612 if (NULL == c->types)
1615 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1616 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1621 * Check whether client wants traffic from a tunnel.
1623 * @param c Client to check.
1624 * @param t Tunnel to be found.
1626 * @return GNUNET_YES if client knows tunnel.
1628 * TODO look in client hashmap
1631 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1635 for (i = 0; i < t->nclients; i++)
1636 if (t->clients[i] == c)
1643 * Check whether client has been informed about a tunnel.
1645 * @param c Client to check.
1646 * @param t Tunnel to be found.
1648 * @return GNUNET_YES if client knows tunnel.
1650 * TODO look in client hashmap
1653 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1657 for (i = 0; i < t->nignore; i++)
1658 if (t->ignore[i] == c)
1660 return client_wants_tunnel(c, t);
1665 * Marks a client as uninterested in traffic from the tunnel, updating both
1666 * client and tunnel to reflect this.
1668 * @param c Client that doesn't want traffic anymore.
1669 * @param t Tunnel which should be ignored.
1671 * FIXME when to delete an incoming tunnel?
1674 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1676 struct GNUNET_HashCode hash;
1678 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1679 GNUNET_break (GNUNET_YES ==
1680 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1682 GNUNET_break (GNUNET_YES ==
1683 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1685 tunnel_delete_active_client (t, c);
1686 GNUNET_array_append (t->ignore, t->nignore, c);
1691 * Deletes a tunnel from a client (either owner or destination). To be used on
1692 * tunnel destroy, otherwise, use client_ignore_tunnel.
1694 * @param c Client whose tunnel to delete.
1695 * @param t Tunnel which should be deleted.
1698 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1700 struct GNUNET_HashCode hash;
1704 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1705 GNUNET_assert (GNUNET_YES ==
1706 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1712 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1714 GNUNET_assert (GNUNET_YES ==
1715 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1719 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1727 * Notify the owner of a tunnel that a peer has disconnected.
1729 * @param c Client (owner of tunnel).
1730 * @param t Tunnel this message is about.
1731 * @param peer_id Short ID of the disconnected peer.
1734 client_notify_peer_disconnected (struct MeshClient *c,
1735 struct MeshTunnel *t,
1736 GNUNET_PEER_Id peer_id)
1738 struct GNUNET_MESH_PeerControl msg;
1740 if (NULL == t->owner || NULL == nc)
1743 msg.header.size = htons (sizeof (msg));
1744 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1745 msg.tunnel_id = htonl (t->local_tid);
1746 GNUNET_PEER_resolve (peer_id, &msg.peer);
1747 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1748 &msg.header, GNUNET_NO);
1753 * Send the message to all clients that have subscribed to its type
1755 * @param msg Pointer to the message itself
1756 * @param payload Pointer to the payload of the message.
1757 * @param t The tunnel to whose clients this message goes.
1759 * @return number of clients this message was sent to
1762 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1763 const struct GNUNET_MessageHeader *payload,
1764 struct MeshTunnel *t)
1766 struct MeshClient *c;
1767 MESH_TunnelNumber *tid;
1770 char cbuf[htons (msg->size)];
1772 type = ntohs (payload->type);
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1775 GNUNET_MESH_DEBUG_M2S (type));
1777 memcpy (cbuf, msg, sizeof (cbuf));
1778 switch (htons (msg->type))
1780 struct GNUNET_MESH_Unicast *uc;
1781 struct GNUNET_MESH_Multicast *mc;
1782 struct GNUNET_MESH_ToOrigin *to;
1784 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1785 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1788 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1789 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1792 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1793 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1801 for (count = 0, c = clients; c != NULL; c = c->next)
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1804 if (client_is_subscribed (type, c))
1806 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1810 *tid = htonl (t->local_tid);
1814 if (GNUNET_NO == client_knows_tunnel (c, t))
1816 /* This client doesn't know the tunnel */
1817 struct GNUNET_MESH_TunnelNotification tmsg;
1818 struct GNUNET_HashCode hash;
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1821 tmsg.header.size = htons (sizeof (tmsg));
1822 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1823 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1824 tmsg.tunnel_id = htonl (t->local_tid_dest);
1826 if (GNUNET_YES == t->speed_min)
1827 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
1828 if (GNUNET_YES == t->nobuffer)
1829 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1830 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1831 &tmsg.header, GNUNET_NO);
1832 tunnel_add_client (t, c);
1833 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1835 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1836 c->incoming_tunnels, &hash, t,
1837 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1839 *tid = htonl (t->local_tid_dest);
1842 /* Check if the client wants to get traffic from the tunnel */
1843 if (GNUNET_NO == client_wants_tunnel(c, t))
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1847 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1848 (struct GNUNET_MessageHeader
1849 *) cbuf, GNUNET_NO);
1858 * Notify the client that owns the tunnel that a peer has connected to it
1859 * (the requested path to it has been confirmed).
1861 * @param t Tunnel whose owner to notify
1862 * @param id Short id of the peer that has connected
1865 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1867 struct GNUNET_MESH_PeerControl pc;
1869 if (NULL == t->owner || GNUNET_YES == t->destroy)
1872 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1873 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1874 pc.tunnel_id = htonl (t->local_tid);
1875 GNUNET_PEER_resolve (id, &pc.peer);
1876 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1882 * Notify all clients (not depending on registration status) that the incoming
1883 * tunnel is no longer valid.
1885 * @param t Tunnel that was destroyed.
1888 send_clients_tunnel_destroy (struct MeshTunnel *t)
1890 struct GNUNET_MESH_TunnelMessage msg;
1892 msg.header.size = htons (sizeof (msg));
1893 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1894 msg.tunnel_id = htonl (t->local_tid_dest);
1895 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1900 * Notify clients of tunnel disconnections, if needed.
1901 * In case the origin disconnects, the destination clients get a tunnel destroy
1902 * notification. If the last destination disconnects (only one remaining client
1903 * in tunnel), the origin gets a (local ID) peer disconnected.
1904 * Note that the function must be called BEFORE removing the client from
1907 * @param t Tunnel that was destroyed.
1908 * @param c Client that disconnected.
1911 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1917 struct GNUNET_MESH_TunnelMessage msg;
1919 msg.header.size = htons (sizeof (msg));
1920 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1921 msg.tunnel_id = htonl (t->local_tid_dest);
1922 for (i = 0; i < t->nclients; i++)
1923 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1924 &msg.header, GNUNET_NO);
1926 // FIXME when to disconnect an incoming tunnel?
1927 else if (1 == t->nclients && NULL != t->owner)
1929 struct GNUNET_MESH_PeerControl msg;
1931 msg.header.size = htons (sizeof (msg));
1932 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1933 msg.tunnel_id = htonl (t->local_tid);
1934 msg.peer = my_full_id;
1935 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1936 &msg.header, GNUNET_NO);
1942 * Iterator over all the peers to remove the oldest not-used entry.
1944 * @param cls Closure (unsued).
1945 * @param key ID of the peer.
1946 * @param value Peer_Info of the peer.
1951 peer_info_timeout (void *cls,
1952 const struct GNUNET_HashCode *key,
1959 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1960 * and insert it in the appropiate structures if the peer is not known yet.
1962 * @param peer Full identity of the peer.
1964 * @return Existing or newly created peer info.
1966 static struct MeshPeerInfo *
1967 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1969 struct MeshPeerInfo *peer_info;
1971 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1972 if (NULL == peer_info)
1975 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1976 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1978 GNUNET_CONTAINER_multihashmap_iterate (peers,
1982 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1983 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1984 peer_info->id = GNUNET_PEER_intern (peer);
1986 peer_info->last_contact = GNUNET_TIME_absolute_get();
1993 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1994 * and insert it in the appropiate structures if the peer is not known yet.
1996 * @param peer Short identity of the peer.
1998 * @return Existing or newly created peer info.
2000 static struct MeshPeerInfo *
2001 peer_info_get_short (const GNUNET_PEER_Id peer)
2003 struct GNUNET_PeerIdentity id;
2005 GNUNET_PEER_resolve (peer, &id);
2006 return peer_info_get (&id);
2011 * Iterator to remove the tunnel from the list of tunnels a peer participates
2014 * @param cls Closure (tunnel info)
2015 * @param key GNUNET_PeerIdentity of the peer (unused)
2016 * @param value PeerInfo of the peer
2018 * @return always GNUNET_YES, to keep iterating
2021 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2023 struct MeshTunnel *t = cls;
2024 struct MeshPeerInfo *peer = value;
2027 for (i = 0; i < peer->ntunnels; i++)
2030 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2033 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2034 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2043 * Core callback to write a pre-constructed data packet to core buffer
2045 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2046 * @param size Number of bytes available in buf.
2047 * @param buf Where the to write the message.
2049 * @return number of bytes written to buf
2052 send_core_data_raw (void *cls, size_t size, void *buf)
2054 struct MeshTransmissionDescriptor *info = cls;
2055 struct GNUNET_MessageHeader *msg;
2058 GNUNET_assert (NULL != info);
2059 GNUNET_assert (NULL != info->mesh_data);
2060 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2061 total_size = ntohs (msg->size);
2063 if (total_size > size)
2068 memcpy (buf, msg, total_size);
2069 data_descriptor_decrement_rc (info->mesh_data);
2076 * Sends an already built non-multicast message to a peer,
2077 * properly registrating all used resources.
2079 * @param message Message to send. Function makes a copy of it.
2080 * @param peer Short ID of the neighbor whom to send the message.
2081 * @param t Tunnel on which this message is transmitted.
2084 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2085 const struct GNUNET_PeerIdentity *peer,
2086 struct MeshTunnel *t)
2088 struct MeshTransmissionDescriptor *info;
2089 struct MeshPeerInfo *neighbor;
2090 struct MeshPeerPath *p;
2094 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2096 size = ntohs (message->size);
2097 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2098 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2099 info->mesh_data->data = GNUNET_malloc (size);
2100 memcpy (info->mesh_data->data, message, size);
2101 type = ntohs(message->type);
2104 struct GNUNET_MESH_Unicast *m;
2105 struct GNUNET_MESH_ToOrigin *to;
2107 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2108 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2109 m->ttl = htonl (ntohl (m->ttl) - 1);
2111 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2112 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2114 to->pid = htonl(t->bck_pid);
2116 info->mesh_data->data_len = size;
2117 info->mesh_data->reference_counter = 1;
2118 info->mesh_data->total_out = 1;
2119 neighbor = peer_info_get (peer);
2120 for (p = neighbor->path_head; NULL != p; p = p->next)
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 " %s IS NOT DIRECTLY CONNECTED\n",
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2136 for (p = neighbor->path_head; NULL != p; p = p->next)
2138 struct GNUNET_PeerIdentity debug_id;
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2142 " path with %u hops through:\n",
2144 for (i = 0; i < p->length; i++)
2146 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 i, GNUNET_i2s(&debug_id));
2153 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2155 " no direct connection to %s\n",
2157 GNUNET_free (info->mesh_data->data);
2158 GNUNET_free (info->mesh_data);
2162 info->peer = neighbor;
2163 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2174 * Sends a CREATE PATH message for a path to a peer, properly registrating
2175 * all used resources.
2177 * @param peer PeerInfo of the final peer for whom this path is being created.
2178 * @param p Path itself.
2179 * @param t Tunnel for which the path is created.
2182 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2183 struct MeshTunnel *t)
2185 struct GNUNET_PeerIdentity id;
2186 struct MeshPathInfo *path_info;
2187 struct MeshPeerInfo *neighbor;
2193 p = tree_get_path_to_peer (t->tree, peer->id);
2200 for (i = 0; i < p->length; i++)
2202 if (p->peers[i] == myid)
2205 if (i >= p->length - 1)
2211 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2213 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2214 path_info->path = p;
2216 neighbor = peer_info_get (&id);
2217 path_info->peer = neighbor;
2218 queue_add (path_info,
2219 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2220 sizeof (struct GNUNET_MESH_ManipulatePath) +
2221 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2228 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2230 * @param t Tunnel whose path to destroy.
2231 * @param destination Short ID of the peer to whom the path to destroy.
2234 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2236 struct MeshPeerPath *p;
2239 p = tree_get_path_to_peer (t->tree, destination);
2245 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2246 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2248 struct GNUNET_MESH_ManipulatePath *msg;
2249 struct GNUNET_PeerIdentity *pi;
2253 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2254 msg->header.size = htons (size);
2255 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2256 msg->tid = htonl (t->id.tid);
2257 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2258 for (i = 0; i < p->length; i++)
2260 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2262 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2269 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2271 * @param t Tunnel which to confirm.
2274 send_path_ack (struct MeshTunnel *t)
2276 struct MeshTransmissionDescriptor *info;
2277 struct GNUNET_PeerIdentity id;
2278 GNUNET_PEER_Id peer;
2280 peer = tree_get_predecessor (t->tree);
2281 GNUNET_PEER_resolve (peer, &id);
2282 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2283 info->origin = &t->id;
2284 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2285 GNUNET_assert (NULL != info->peer);
2288 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2289 sizeof (struct GNUNET_MESH_PathACK),
2296 * Try to establish a new connection to this peer.
2297 * Use the best path for the given tunnel.
2298 * If the peer doesn't have any path to it yet, try to get one.
2299 * If the peer already has some path, send a CREATE PATH towards it.
2301 * @param peer PeerInfo of the peer.
2302 * @param t Tunnel for which to create the path, if possible.
2305 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2307 struct MeshPeerPath *p;
2308 struct MeshPathInfo *path_info;
2310 if (NULL != peer->path_head)
2312 p = tree_get_path_to_peer (t->tree, peer->id);
2319 // FIXME always send create path to self
2322 send_create_path (peer, p, t);
2326 struct GNUNET_HashCode hash;
2329 send_client_peer_connected (t, myid);
2330 t->local_tid_dest = next_local_tid++;
2331 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2334 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2335 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2342 else if (NULL == peer->dhtget)
2344 struct GNUNET_PeerIdentity id;
2346 GNUNET_PEER_resolve (peer->id, &id);
2347 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2348 path_info->peer = peer;
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2352 peer->dhtgetcls = path_info;
2353 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2354 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2355 &id.hashPubKey, /* key to search */
2356 dht_replication_level, /* replication level */
2357 GNUNET_DHT_RO_RECORD_ROUTE |
2358 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2359 NULL, /* xquery */ // FIXME BLOOMFILTER
2360 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2361 &dht_get_id_handler, path_info);
2363 /* Otherwise, there is no path but the DHT get is already started. */
2368 * Task to delay the connection of a peer
2370 * @param cls Closure (path info with tunnel and peer to connect).
2371 * Will be free'd on exection.
2372 * @param tc TaskContext
2375 peer_info_connect_task (void *cls,
2376 const struct GNUNET_SCHEDULER_TaskContext *tc)
2378 struct MeshPathInfo *path_info = cls;
2380 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2382 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2387 peer_info_connect (path_info->peer, path_info->t);
2393 * Destroy the peer_info and free any allocated resources linked to it
2395 * @param pi The peer_info to destroy.
2397 * @return GNUNET_OK on success
2400 peer_info_destroy (struct MeshPeerInfo *pi)
2402 struct GNUNET_PeerIdentity id;
2403 struct MeshPeerPath *p;
2404 struct MeshPeerPath *nextp;
2406 GNUNET_PEER_resolve (pi->id, &id);
2407 GNUNET_PEER_change_rc (pi->id, -1);
2410 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2414 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2416 if (NULL != pi->dhtget)
2418 GNUNET_DHT_get_stop (pi->dhtget);
2419 GNUNET_free (pi->dhtgetcls);
2425 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2429 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2431 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2439 * Remove all paths that rely on a direct connection between p1 and p2
2440 * from the peer itself and notify all tunnels about it.
2442 * @param peer PeerInfo of affected peer.
2443 * @param p1 GNUNET_PEER_Id of one peer.
2444 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2447 * TODO: optimize (see below)
2450 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2453 struct MeshPeerPath *p;
2454 struct MeshPeerPath *aux;
2455 struct MeshPeerInfo *peer_d;
2457 unsigned int destroyed;
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2464 p = peer->path_head;
2468 for (i = 0; i < (p->length - 1); i++)
2470 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2471 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2473 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2484 for (i = 0; i < peer->ntunnels; i++)
2486 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2490 * Problem: one or more peers have been deleted from the tunnel tree.
2491 * We don't know who they are to try to add them again.
2492 * We need to try to find a new path for each of the disconnected peers.
2493 * Some of them might already have a path to reach them that does not
2494 * involve p1 and p2. Adding all anew might render in a better tree than
2495 * the trivial immediate fix.
2497 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2498 * its children will be reachable trough him.
2500 peer_d = peer_info_get_short (d);
2503 for (p = peer_d->path_head; NULL != p; p = p->next)
2505 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2513 /* No callback, as peer will be already disconnected and a connection
2514 * scheduled by tunnel_notify_connection_broken.
2516 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2520 peer_info_connect (peer_d, peer->tunnels[i]);
2523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2528 * Add the path to the peer and update the path used to reach it in case this
2531 * @param peer_info Destination peer to add the path to.
2532 * @param path New path to add. Last peer must be the peer in arg 1.
2533 * Path will be either used of freed if already known.
2534 * @param trusted Do we trust that this path is real?
2537 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2540 struct MeshPeerPath *aux;
2544 if ((NULL == peer_info) || (NULL == path))
2547 path_destroy (path);
2550 if (path->peers[path->length - 1] != peer_info->id)
2553 path_destroy (path);
2556 if (path->length <= 2 && GNUNET_NO == trusted)
2558 /* Only allow CORE to tell us about direct paths */
2559 path_destroy (path);
2562 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2563 for (l = 1; l < path->length; l++)
2565 if (path->peers[l] == myid)
2567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2568 for (l2 = 0; l2 < path->length - l; l2++)
2570 path->peers[l2] = path->peers[l + l2];
2575 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2580 struct GNUNET_PeerIdentity id;
2582 GNUNET_PEER_resolve (peer_info->id, &id);
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2584 path->length, GNUNET_i2s (&id));
2587 l = path_get_length (path);
2594 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2595 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2597 l2 = path_get_length (aux);
2600 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2601 peer_info->path_tail, aux, path);
2606 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2608 path_destroy (path);
2613 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2620 * Add the path to the origin peer and update the path used to reach it in case
2621 * this is the shortest.
2622 * The path is given in peer_info -> destination, therefore we turn the path
2623 * upside down first.
2625 * @param peer_info Peer to add the path to, being the origin of the path.
2626 * @param path New path to add after being inversed.
2627 * @param trusted Do we trust that this path is real?
2630 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2631 struct MeshPeerPath *path, int trusted)
2634 peer_info_add_path (peer_info, path, trusted);
2639 * Function called if the connection to the peer has been stalled for a while,
2640 * possibly due to a missed ACK. Poll the peer about its ACK status.
2642 * @param cls Closure (cinfo).
2643 * @param tc TaskContext.
2646 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2648 struct MeshTunnelChildInfo *cinfo = cls;
2649 struct GNUNET_MESH_Poll msg;
2650 struct GNUNET_PeerIdentity id;
2651 struct MeshTunnel *t;
2653 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2654 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2660 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2661 msg.header.size = htons (sizeof (msg));
2662 msg.tid = htonl (t->id.tid);
2663 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2664 msg.last_ack = htonl (cinfo->fwd_ack);
2666 GNUNET_PEER_resolve (cinfo->id, &id);
2667 send_prebuilt_message (&msg.header, &id, cinfo->t);
2668 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2670 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2671 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2672 &tunnel_poll, cinfo);
2677 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2678 * to obtain a local peer -> destination path and interning the peer ids.
2680 * @return Newly allocated and created path
2682 static struct MeshPeerPath *
2683 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2684 unsigned int get_path_length,
2685 const struct GNUNET_PeerIdentity *put_path,
2686 unsigned int put_path_length)
2688 struct MeshPeerPath *p;
2694 GNUNET_PEER_change_rc (myid, 1);
2695 i = get_path_length;
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2697 for (i--; i >= 0; i--)
2699 id = GNUNET_PEER_intern (&get_path[i]);
2700 if (p->length > 0 && id == p->peers[p->length - 1])
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2703 GNUNET_PEER_change_rc (id, -1);
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2708 GNUNET_i2s (&get_path[i]));
2710 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2711 p->peers[p->length - 1] = id;
2714 i = put_path_length;
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2716 for (i--; i >= 0; i--)
2718 id = GNUNET_PEER_intern (&put_path[i]);
2721 /* PUT path went through us, so discard the path up until now and start
2722 * from here to get a much shorter (and loop-free) path.
2727 if (p->length > 0 && id == p->peers[p->length - 1])
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2730 GNUNET_PEER_change_rc (id, -1);
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2735 GNUNET_i2s (&put_path[i]));
2737 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2738 p->peers[p->length - 1] = id;
2742 if (get_path_length > 0)
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2744 GNUNET_i2s (&get_path[0]));
2745 if (put_path_length > 0)
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2747 GNUNET_i2s (&put_path[0]));
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2750 for (i = 0; i < p->length; i++)
2752 struct GNUNET_PeerIdentity peer_id;
2754 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2756 GNUNET_i2s (&peer_id));
2764 * Adds a path to the peer_infos of all the peers in the path
2766 * @param p Path to process.
2767 * @param confirmed Whether we know if the path works or not.
2770 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2774 /* TODO: invert and add */
2775 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2776 for (i++; i < p->length; i++)
2778 struct MeshPeerInfo *aux;
2779 struct MeshPeerPath *copy;
2781 aux = peer_info_get_short (p->peers[i]);
2782 copy = path_duplicate (p);
2783 copy->length = i + 1;
2784 peer_info_add_path (aux, copy, GNUNET_NO);
2790 * Send keepalive packets for a peer
2792 * @param cls Closure (tunnel for which to send the keepalive).
2793 * @param tc Notification context.
2795 * TODO: implement explicit multicast keepalive?
2798 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2802 * Search for a tunnel among the incoming tunnels
2804 * @param tid the local id of the tunnel
2806 * @return tunnel handler, NULL if doesn't exist
2808 static struct MeshTunnel *
2809 tunnel_get_incoming (MESH_TunnelNumber tid)
2811 struct GNUNET_HashCode hash;
2813 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2814 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2815 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2820 * Search for a tunnel among the tunnels for a client
2822 * @param c the client whose tunnels to search in
2823 * @param tid the local id of the tunnel
2825 * @return tunnel handler, NULL if doesn't exist
2827 static struct MeshTunnel *
2828 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2830 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2832 return tunnel_get_incoming (tid);
2836 struct GNUNET_HashCode hash;
2838 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2839 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2845 * Search for a tunnel by global ID using PEER_ID
2847 * @param pi owner of the tunnel
2848 * @param tid global tunnel number
2850 * @return tunnel handler, NULL if doesn't exist
2852 static struct MeshTunnel *
2853 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2855 struct MESH_TunnelID id;
2856 struct GNUNET_HashCode hash;
2861 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2862 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2867 * Search for a tunnel by global ID using full PeerIdentities
2869 * @param oid owner of the tunnel
2870 * @param tid global tunnel number
2872 * @return tunnel handler, NULL if doesn't exist
2874 static struct MeshTunnel *
2875 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2877 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2882 * Delete an active client from the tunnel.
2888 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2892 for (i = 0; i < t->nclients; i++)
2894 if (t->clients[i] == c)
2896 t->clients[i] = t->clients[t->nclients - 1];
2897 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2898 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2900 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2908 * Delete an ignored client from the tunnel.
2914 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2918 for (i = 0; i < t->nignore; i++)
2920 if (t->ignore[i] == c)
2922 t->ignore[i] = t->ignore[t->nignore - 1];
2923 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2931 * Delete a client from the tunnel. It should be only done on
2932 * client disconnection, otherwise use client_ignore_tunnel.
2938 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2940 tunnel_delete_ignored_client (t, c);
2941 tunnel_delete_active_client (t, c);
2946 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
2948 * Destroys queue elements of all waiting transmissions and frees all memory
2949 * used by the struct and its elements.
2951 * @param cls Closure (tunnel info).
2952 * @param key Hash of GNUNET_PEER_Id (unused).
2953 * @param value MeshTunnelChildInfo of the child.
2955 * @return always GNUNET_YES, to keep iterating
2958 tunnel_destroy_child (void *cls,
2959 const struct GNUNET_HashCode * key,
2962 struct MeshTunnelChildInfo *cinfo = value;
2963 struct MeshTunnel *t = cls;
2964 struct MeshPeerQueue *q;
2968 for (c = 0; c < cinfo->send_buffer_n; c++)
2970 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
2971 q = cinfo->send_buffer[i];
2972 cinfo->send_buffer[i] = NULL;
2974 queue_destroy (q, GNUNET_YES);
2977 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
2979 GNUNET_free_non_null (cinfo->send_buffer);
2980 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
2982 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
2983 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2985 GNUNET_free (cinfo);
2991 * Callback used to notify a client owner of a tunnel that a peer has
2992 * disconnected, most likely because of a path change.
2994 * @param cls Closure (tunnel this notification is about).
2995 * @param peer_id Short ID of disconnected peer.
2998 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3000 struct MeshTunnel *t = cls;
3001 struct MeshPeerInfo *peer;
3002 struct MeshPathInfo *path_info;
3004 client_notify_peer_disconnected (t->owner, t, peer_id);
3006 peer = peer_info_get_short (peer_id);
3007 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3008 path_info->peer = peer;
3010 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3016 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3017 * needed rescources.
3018 * If peer already exists, reevaluate shortest path and change if different.
3020 * @param t Tunnel we want to add a new peer to
3021 * @param peer PeerInfo of the peer being added
3025 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3027 struct GNUNET_PeerIdentity id;
3028 struct MeshPeerPath *best_p;
3029 struct MeshPeerPath *p;
3030 unsigned int best_cost;
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3034 GNUNET_PEER_resolve (peer->id, &id);
3036 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3039 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3040 GNUNET_assert (GNUNET_OK ==
3041 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3043 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3046 if (NULL != (p = peer->path_head))
3049 best_cost = tree_get_path_cost (t->tree, p);
3052 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3059 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3060 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3061 t->path_refresh_task =
3062 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3066 /* Start a DHT get */
3067 peer_info_connect (peer, t);
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3073 * Add a path to a tunnel which we don't own, just to remember the next hop.
3074 * If destination node was already in the tunnel, the first hop information
3075 * will be replaced with the new path.
3077 * @param t Tunnel we want to add a new peer to
3078 * @param p Path to add
3079 * @param own_pos Position of local node in path.
3083 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3084 unsigned int own_pos)
3086 struct GNUNET_PeerIdentity id;
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3089 GNUNET_assert (0 != own_pos);
3090 tree_add_path (t->tree, p, NULL, NULL);
3091 if (own_pos < p->length - 1)
3093 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3094 tree_update_first_hops (t->tree, myid, &id);
3096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3100 * Add a client to a tunnel, initializing all needed data structures.
3102 * @param t Tunnel to which add the client.
3103 * @param c Client which to add to the tunnel.
3106 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3108 struct MeshTunnelClientInfo clinfo;
3110 GNUNET_array_append (t->clients, t->nclients, c);
3111 clinfo.fwd_ack = t->fwd_pid + 1;
3112 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3113 clinfo.fwd_pid = t->fwd_pid;
3114 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3116 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3121 * Notifies a tunnel that a connection has broken that affects at least
3122 * some of its peers. Sends a notification towards the root of the tree.
3123 * In case the peer is the owner of the tree, notifies the client that owns
3124 * the tunnel and tries to reconnect.
3126 * @param t Tunnel affected.
3127 * @param p1 Peer that got disconnected from p2.
3128 * @param p2 Peer that got disconnected from p1.
3130 * @return Short ID of the peer disconnected (either p1 or p2).
3131 * 0 if the tunnel remained unaffected.
3133 static GNUNET_PEER_Id
3134 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3140 tree_notify_connection_broken (t->tree, p1, p2,
3141 &tunnel_notify_client_peer_disconnected,
3143 if (myid != p1 && myid != p2)
3149 if (tree_get_predecessor (t->tree) != 0)
3151 /* We are the peer still connected, notify owner of the disconnection. */
3152 struct GNUNET_MESH_PathBroken msg;
3153 struct GNUNET_PeerIdentity neighbor;
3155 msg.header.size = htons (sizeof (msg));
3156 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3157 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3158 msg.tid = htonl (t->id.tid);
3159 msg.peer1 = my_full_id;
3160 GNUNET_PEER_resolve (pid, &msg.peer2);
3161 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3162 send_prebuilt_message (&msg.header, &neighbor, t);
3170 * Send a multicast packet to a neighbor.
3172 * @param cls Closure (Info about the multicast packet)
3173 * @param neighbor_id Short ID of the neighbor to send the packet to.
3176 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3178 struct MeshData *mdata = cls;
3179 struct MeshTransmissionDescriptor *info;
3180 struct GNUNET_PeerIdentity neighbor;
3181 struct GNUNET_MessageHeader *msg;
3183 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3185 info->mesh_data = mdata;
3186 (mdata->reference_counter) ++;
3187 info->destination = neighbor_id;
3188 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3190 GNUNET_i2s (&neighbor));
3191 info->peer = peer_info_get (&neighbor);
3192 GNUNET_assert (NULL != info->peer);
3193 msg = (struct GNUNET_MessageHeader *) mdata->data;
3196 info->mesh_data->data_len,
3203 * Queue a message in a tunnel in multicast, sending a copy to each child node
3204 * down the local one in the tunnel tree.
3206 * @param t Tunnel in which to send the data.
3207 * @param msg Message to be sent.
3210 tunnel_send_multicast (struct MeshTunnel *t,
3211 const struct GNUNET_MessageHeader *msg)
3213 struct MeshData *mdata;
3215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3216 " sending a multicast packet...\n");
3218 mdata = GNUNET_malloc (sizeof (struct MeshData));
3219 mdata->data_len = ntohs (msg->size);
3221 mdata->data = GNUNET_malloc (mdata->data_len);
3222 memcpy (mdata->data, msg, mdata->data_len);
3223 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3225 struct GNUNET_MESH_Multicast *mcast;
3227 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3228 if (t->fwd_queue_n >= t->fwd_queue_max)
3231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3233 " message from %s!\n",
3234 GNUNET_i2s(&mcast->oid));
3235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3236 " message at %s!\n",
3237 GNUNET_i2s(&my_full_id));
3238 GNUNET_free (mdata->data);
3239 GNUNET_free (mdata);
3243 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3245 ntohl (mcast->ttl));
3249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3252 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3253 if (mdata->reference_counter == 0)
3255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3256 " no one to send data to\n");
3257 GNUNET_free (mdata->data);
3258 GNUNET_free (mdata);
3259 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3264 mdata->total_out = mdata->reference_counter;
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3267 " sending a multicast packet done\n");
3273 * Increase the SKIP value of all peers that
3274 * have not received a unicast message.
3276 * @param cls Closure (ID of the peer that HAS received the message).
3277 * @param key ID of the neighbor.
3278 * @param value Information about the neighbor.
3280 * @return GNUNET_YES to keep iterating.
3283 tunnel_add_skip (void *cls,
3284 const struct GNUNET_HashCode * key,
3287 struct GNUNET_PeerIdentity *neighbor = cls;
3288 struct MeshTunnelChildInfo *cinfo = value;
3290 /* TODO compare only pointers? key == neighbor? */
3291 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3301 * @brief Get neighbor's Flow Control information.
3303 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3304 * descendant of the local node in a certain tunnel.
3305 * If the info is not yet there (recently created path), creates the data struct
3306 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3309 * @param t Tunnel related.
3310 * @param peer Neighbor whose Flow Control info is needed.
3312 * @return Neighbor's Flow Control info.
3314 static struct MeshTunnelChildInfo *
3315 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3316 const struct GNUNET_PeerIdentity *peer)
3318 struct MeshTunnelChildInfo *cinfo;
3320 if (NULL == t->children_fc)
3323 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3329 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3330 cinfo->id = GNUNET_PEER_intern (peer);
3331 cinfo->skip = t->fwd_pid;
3334 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3335 cinfo->fwd_ack = t->fwd_pid + delta;
3336 cinfo->bck_ack = delta;
3337 cinfo->bck_pid = -1;
3339 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3340 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3342 cinfo->send_buffer =
3343 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3345 GNUNET_assert (GNUNET_OK ==
3346 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3349 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3356 * Get the Flow Control info of a client.
3358 * @param t Tunnel on which to look.
3359 * @param c Client whose ACK to get.
3361 * @return ACK value.
3363 static struct MeshTunnelClientInfo *
3364 tunnel_get_client_fc (struct MeshTunnel *t,
3365 struct MeshClient *c)
3369 for (i = 0; i < t->nclients; i++)
3371 if (t->clients[i] != c)
3373 return &t->clients_fc[i];
3376 return NULL; // avoid compiler / coverity complaints
3381 * Iterator to get the appropiate ACK value from all children nodes.
3383 * @param cls Closue (tunnel).
3384 * @param id Id of the child node.
3387 tunnel_get_child_fwd_ack (void *cls,
3390 struct GNUNET_PeerIdentity peer_id;
3391 struct MeshTunnelChildInfo *cinfo;
3392 struct MeshTunnelChildIteratorContext *ctx = cls;
3393 struct MeshTunnel *t = ctx->t;
3396 GNUNET_PEER_resolve (id, &peer_id);
3397 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3398 ack = cinfo->fwd_ack;
3401 if (GNUNET_NO == ctx->init)
3403 ctx->max_child_ack = ack;
3404 ctx->init = GNUNET_YES;
3407 if (GNUNET_YES == t->speed_min)
3409 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3413 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3420 * Get the maximum PID allowed to transmit to any
3421 * tunnel child of the local peer, depending on the tunnel
3422 * buffering/speed settings.
3426 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3429 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3431 struct MeshTunnelChildIteratorContext ctx;
3433 ctx.max_child_ack = 0;
3435 ctx.init = GNUNET_NO;
3436 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3438 if (0 == ctx.nchildren)
3440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3441 " tunnel has no children, no FWD ACK\n");
3445 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3446 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3448 return (int64_t) ctx.max_child_ack;
3453 * Set the FWD ACK value of a client in a particular tunnel.
3455 * @param t Tunnel affected.
3456 * @param c Client whose ACK to set.
3457 * @param ack ACK value.
3460 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3461 struct MeshClient *c,
3466 for (i = 0; i < t->nclients; i++)
3468 if (t->clients[i] != c)
3470 t->clients_fc[i].fwd_ack = ack;
3478 * Get the highest ACK value of all clients in a particular tunnel,
3479 * according to the buffering/speed settings.
3481 * @param t Tunnel on which to look.
3483 * @return Corresponding ACK value (max uint32_t).
3484 * If no clients are suscribed, -1LL.
3487 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3492 if (0 == t->nclients)
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3495 " tunnel has no clients, no FWD ACK\n");
3499 for (ack = -1LL, i = 0; i < t->nclients; i++)
3502 (GNUNET_YES == t->speed_min &&
3503 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3504 (GNUNET_NO == t->speed_min &&
3505 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3507 ack = t->clients_fc[i].fwd_ack;
3511 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3512 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3514 return (uint32_t) ack;
3519 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3520 * mode and the status of all children nodes.
3524 * @return Maximum PID allowed.
3527 tunnel_get_fwd_ack (struct MeshTunnel *t)
3531 uint32_t buffer_free;
3535 count = t->fwd_pid - t->skip;
3536 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3537 child_ack = tunnel_get_children_fwd_ack (t);
3538 client_ack = tunnel_get_clients_fwd_ack (t);
3539 if (GNUNET_YES == t->nobuffer)
3542 if (-1LL == child_ack)
3543 child_ack = client_ack;
3544 if (-1LL == child_ack)
3547 client_ack = child_ack = ack;
3552 ack = count + buffer_free; // Overflow? OK!
3554 if (-1LL == child_ack)
3556 // Node has no children, child_ack AND core buffer are irrelevant.
3557 if (-1LL == client_ack) // No children AND no clients? Not good!
3559 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3563 return (uint32_t) client_ack;
3565 if (-1LL == client_ack)
3569 if (GNUNET_YES == t->speed_min)
3571 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3572 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3576 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3577 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3580 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3581 count, buffer_free, child_ack, client_ack, ack);
3587 * Build a local ACK message and send it to a local client.
3589 * @param t Tunnel on which to send the ACK.
3590 * @param c Client to whom send the ACK.
3591 * @param ack Value of the ACK.
3594 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3596 struct GNUNET_MESH_LocalAck msg;
3598 msg.header.size = htons (sizeof (msg));
3599 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3600 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3601 msg.max_pid = htonl (ack);
3602 GNUNET_SERVER_notification_context_unicast(nc,
3609 * Build an ACK message and queue it to send to the given peer.
3611 * @param t Tunnel on which to send the ACK.
3612 * @param peer Peer to whom send the ACK.
3613 * @param ack Value of the ACK.
3616 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3618 struct GNUNET_MESH_ACK msg;
3620 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3621 msg.header.size = htons (sizeof (msg));
3622 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3623 msg.pid = htonl (ack);
3624 msg.tid = htonl (t->id.tid);
3626 send_prebuilt_message (&msg.header, peer, t);
3631 * Notify a the owner of a tunnel about how many more
3632 * payload packages will we accept on a given tunnel.
3634 * @param t Tunnel on which to send the ACK.
3637 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3642 "Sending client FWD ACK on tunnel %X\n",
3645 ack = tunnel_get_fwd_ack (t);
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3648 if (t->last_fwd_ack == ack)
3650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3655 t->last_fwd_ack = ack;
3656 send_local_ack (t, t->owner, ack);
3661 * Send an ACK informing the predecessor about the available buffer space.
3662 * In case there is no predecessor, inform the owning client.
3663 * If buffering is off, send only on behalf of children or self if endpoint.
3664 * If buffering is on, send when sent to children and buffer space is free.
3665 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3666 * the ACK itself goes "back" (towards root).
3668 * @param t Tunnel on which to send the ACK.
3669 * @param type Type of message that triggered the ACK transmission.
3672 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3674 struct GNUNET_PeerIdentity id;
3677 if (NULL != t->owner)
3679 tunnel_send_client_fwd_ack (t);
3682 /* Is it after unicast / multicast retransmission? */
3685 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3686 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3688 "ACK due to FWD DATA retransmission\n");
3689 if (GNUNET_YES == t->nobuffer)
3691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3695 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3696 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3698 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3699 t->force_ack = GNUNET_YES;
3705 /* Check if we need to transmit the ACK */
3706 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3707 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
3708 GNUNET_NO == t->force_ack)
3710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3712 " t->qmax: %u, t->qn: %u\n",
3713 t->fwd_queue_max, t->fwd_queue_n);
3714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3715 " t->pid: %u, t->ack: %u\n",
3716 t->fwd_pid, t->last_fwd_ack);
3720 /* Ok, ACK might be necessary, what PID to ACK? */
3721 ack = tunnel_get_fwd_ack (t);
3723 /* If speed_min and not all children have ack'd, dont send yet */
3724 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
3726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3730 t->last_fwd_ack = ack;
3731 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3732 send_ack (t, &id, ack);
3734 t->force_ack = GNUNET_NO;
3739 * Iterator to send a child node a BCK ACK to allow him to send more
3742 * @param cls Closure (tunnel).
3743 * @param id Id of the child node.
3746 tunnel_send_child_bck_ack (void *cls,
3749 struct MeshTunnel *t = cls;
3750 struct MeshTunnelChildInfo *cinfo;
3751 struct GNUNET_PeerIdentity peer;
3754 GNUNET_PEER_resolve (id, &peer);
3755 cinfo = tunnel_get_neighbor_fc (t, &peer);
3756 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
3758 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3761 " Not sending ACK, not needed\n");
3764 cinfo->bck_ack = ack;
3766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3767 " Sending BCK ACK %u (last sent: %u)\n",
3768 ack, cinfo->bck_ack);
3769 send_ack (t, &peer, ack);
3774 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3776 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3778 * FIXME fc: what happens if we have 2 clients but q_size is 1?
3779 * - implement a size 1 buffer in each client_fc AND children_fc
3780 * to hold at least 1 message per "child".
3781 * problem: violates no buffer policy
3782 * - ack 0 and make "children" poll for transmission slots
3783 * problem: big overhead, extra latency even in low traffic
3786 * @param t Tunnel on which to send the BCK ACKs.
3789 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3792 unsigned int tunnel_delta;
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3796 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3799 /* Find client whom to allow to send to origin (with lowest buffer space) */
3800 for (i = 0; i < t->nclients; i++)
3802 struct MeshTunnelClientInfo *clinfo;
3805 clinfo = &t->clients_fc[i];
3806 delta = clinfo->bck_ack - clinfo->bck_pid;
3807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3808 t->clients[i]->id, delta);
3810 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3811 (GNUNET_YES == t->nobuffer && 0 == delta))
3815 ack = clinfo->bck_pid;
3816 ack += t->nobuffer ? 1 : tunnel_delta;
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3818 " sending ack to client %u: %u\n",
3819 t->clients[i]->id, ack);
3820 send_local_ack (t, t->clients[i], ack);
3821 clinfo->bck_ack = ack;
3825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3826 " not sending ack to client %u (td %u, d %u)\n",
3827 t->clients[i]->id, tunnel_delta, delta);
3834 * Send an ACK informing the children nodes and destination clients about
3835 * the available buffer space.
3836 * If buffering is off, send only on behalf of root (can be self).
3837 * If buffering is on, send when sent to predecessor and buffer space is free.
3838 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3839 * the ACK itself goes "forward" (towards children/clients).
3841 * @param t Tunnel on which to send the ACK.
3842 * @param type Type of message that triggered the ACK transmission.
3845 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3849 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3850 /* Is it after data to_origin retransmission? */
3853 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3854 if (GNUNET_YES == t->nobuffer)
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3857 " Not sending ACK, nobuffer\n");
3861 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3862 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3864 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3865 t->force_ack = GNUNET_YES;
3871 tunnel_send_clients_bck_ack (t);
3872 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3873 t->force_ack = GNUNET_NO;
3878 * @brief Re-initiate traffic to this peer if necessary.
3880 * Check if there is traffic queued towards this peer
3881 * and the core transmit handle is NULL (traffic was stalled).
3882 * If so, call core tmt rdy.
3884 * @param cls Closure (unused)
3885 * @param peer_id Short ID of peer to which initiate traffic.
3888 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3890 struct MeshPeerInfo *peer;
3891 struct GNUNET_PeerIdentity id;
3892 struct MeshPeerQueue *q;
3895 peer = peer_info_get_short(peer_id);
3896 if (NULL != peer->core_transmit)
3899 q = queue_get_next(peer);
3902 /* Might br multicast traffic already sent to this particular peer but
3903 * not to other children in this tunnel.
3904 * This way t->queue_n would be > 0 but the queue of this particular peer
3910 GNUNET_PEER_resolve (peer->id, &id);
3911 peer->core_transmit =
3912 GNUNET_CORE_notify_transmit_ready(core_handle,
3915 GNUNET_TIME_UNIT_FOREVER_REL,
3925 * @brief Allow transmission of FWD traffic on this tunnel
3927 * Check if there is traffic queued towards any children
3928 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3930 * @param t Tunnel on which to unlock FWD traffic.
3933 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3935 if (0 == t->fwd_queue_n)
3938 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3943 * @brief Allow transmission of BCK traffic on this tunnel
3945 * Check if there is traffic queued towards the root of the tree
3946 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3948 * @param t Tunnel on which to unlock BCK traffic.
3951 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3953 if (0 == t->bck_queue_n)
3956 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3961 * Send a message to all peers in this tunnel that the tunnel is no longer
3964 * @param t The tunnel whose peers to notify.
3965 * @param parent ID of the parent, in case the tree is already destroyed.
3968 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3970 struct GNUNET_MESH_TunnelDestroy msg;
3971 struct GNUNET_PeerIdentity id;
3973 msg.header.size = htons (sizeof (msg));
3974 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3975 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3976 msg.tid = htonl (t->id.tid);
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3978 " sending tunnel destroy for tunnel: %s [%X]\n",
3979 GNUNET_i2s (&msg.oid), t->id.tid);
3980 if (tree_count_children(t->tree) > 0)
3982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to children\n");
3983 tunnel_send_multicast (t, &msg.header);
3986 parent = tree_get_predecessor (t->tree);
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3991 GNUNET_PEER_resolve (parent, &id);
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3993 " sending back to %s\n",
3995 send_prebuilt_message (&msg.header, &id, t);
4000 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4002 * @param cls Closure (Tunnel which to cancel).
4003 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4006 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4008 struct MeshTunnel *t = cls;
4009 struct MeshPeerInfo *peer_info;
4010 struct MeshPeerQueue *pq;
4011 struct MeshPeerQueue *next;
4013 peer_info = peer_info_get_short (neighbor_id);
4014 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4017 if (pq->tunnel == t)
4019 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4020 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4021 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4023 // Should have been removed on destroy children
4026 queue_destroy (pq, GNUNET_YES);
4029 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4031 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4032 peer_info->core_transmit = NULL;
4037 * Destroy the tunnel and free any allocated resources linked to it.
4039 * @param t the tunnel to destroy
4041 * @return GNUNET_OK on success
4044 tunnel_destroy (struct MeshTunnel *t)
4046 struct MeshClient *c;
4047 struct GNUNET_HashCode hash;
4058 struct GNUNET_PeerIdentity id;
4060 GNUNET_PEER_resolve (t->id.oid, &id);
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4062 GNUNET_i2s (&id), t->id.tid);
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4068 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4069 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4077 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4079 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4086 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4087 for (i = 0; i < t->nclients; i++)
4091 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4097 for (i = 0; i < t->nignore; i++)
4101 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4108 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4109 GNUNET_free_non_null (t->clients);
4110 GNUNET_free_non_null (t->ignore);
4111 GNUNET_free_non_null (t->clients_fc);
4113 if (NULL != t->peers)
4115 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4117 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4120 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4121 &tunnel_destroy_child,
4123 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4124 t->children_fc = NULL;
4126 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4127 tree_destroy (t->tree);
4129 if (NULL != t->regex_search)
4130 GNUNET_REGEX_search_cancel (t->regex_search->search_handle);
4131 if (NULL != t->dht_get_type)
4132 GNUNET_DHT_get_stop (t->dht_get_type);
4133 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4134 GNUNET_SCHEDULER_cancel (t->timeout_task);
4135 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4136 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4139 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4144 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4147 * Tunnel is empty: destroy it.
4149 * @param cls Closure (Tunnel).
4150 * @param tc TaskContext.
4153 tunnel_destroy_empty_delayed (void *cls,
4154 const struct GNUNET_SCHEDULER_TaskContext *tc)
4156 struct MeshTunnel *t = cls;
4158 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4159 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4162 if (0 != t->nclients ||
4163 0 != tree_count_children (t->tree))
4168 struct GNUNET_PeerIdentity id;
4170 GNUNET_PEER_resolve (t->id.oid, &id);
4171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4172 "executing destruction of empty tunnel %s [%X]\n",
4173 GNUNET_i2s (&id), t->id.tid);
4177 tunnel_send_destroy (t, 0);
4178 if (0 == t->pending_messages)
4181 t->destroy = GNUNET_YES;
4186 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4188 * @param t Tunnel to destroy if empty.
4191 tunnel_destroy_empty (struct MeshTunnel *t)
4193 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4195 0 != tree_count_children (t->tree))
4197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4199 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4205 struct GNUNET_PeerIdentity id;
4207 GNUNET_PEER_resolve (t->id.oid, &id);
4208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4209 "scheduling destruction of empty tunnel %s [%X]\n",
4210 GNUNET_i2s (&id), t->id.tid);
4214 t->delayed_destroy =
4215 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4216 &tunnel_destroy_empty_delayed,
4222 * Create a new tunnel
4224 * @param owner Who is the owner of the tunnel (short ID).
4225 * @param tid Tunnel Number of the tunnel.
4226 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4227 * @param local Tunnel Number for the tunnel, for the client point of view.
4229 * @return A new initialized tunnel. NULL on error.
4231 static struct MeshTunnel *
4232 tunnel_new (GNUNET_PEER_Id owner,
4233 MESH_TunnelNumber tid,
4234 struct MeshClient *client,
4235 MESH_TunnelNumber local)
4237 struct MeshTunnel *t;
4238 struct GNUNET_HashCode hash;
4240 if (n_tunnels >= max_tunnels && NULL == client)
4243 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4246 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4247 t->bck_queue_max = t->fwd_queue_max;
4248 t->tree = tree_new (owner);
4250 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4251 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4252 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4253 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4254 t->local_tid = local;
4255 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4257 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4259 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4261 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4262 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4269 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4276 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4278 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4279 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4283 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4292 * Callback when removing children from a tunnel tree. Notify owner.
4294 * @param cls Closure (tunnel).
4295 * @param peer_id Short ID of the peer deleted.
4298 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
4300 struct MeshTunnel *t = cls;
4302 client_notify_peer_disconnected (t->owner, t, peer_id);
4306 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4307 * that are no longer needed, as well as nodes of no longer reachable peers.
4308 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4310 * @param t Tunnel from which to remove the path.
4311 * @param peer Short id of the peer which should be removed.
4314 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4318 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
4321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4322 "Tunnel %u [%u] has no more nodes\n",
4323 t->id.oid, t->id.tid);
4329 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4330 * client when the client disconnects. If the client is not the owner, the
4331 * owner will get notified if no more clients are in the tunnel and the client
4332 * get removed from the tunnel's list.
4334 * @param cls closure (client that is disconnecting)
4335 * @param key the hash of the local tunnel id (used to access the hashmap)
4336 * @param value the value stored at the key (tunnel to destroy)
4338 * @return GNUNET_OK, keep iterating.
4341 tunnel_destroy_iterator (void *cls,
4342 const struct GNUNET_HashCode * key,
4345 struct MeshTunnel *t = value;
4346 struct MeshClient *c = cls;
4348 send_client_tunnel_disconnect (t, c);
4351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4352 tunnel_delete_client (t, c);
4353 client_delete_tunnel (c, t);
4354 tunnel_destroy_empty (t);
4357 tunnel_send_destroy (t, 0);
4359 t->destroy = GNUNET_YES;
4366 * Timeout function, destroys tunnel if called
4368 * @param cls Closure (tunnel to destroy).
4369 * @param tc TaskContext
4372 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4374 struct MeshTunnel *t = cls;
4375 struct GNUNET_PeerIdentity id;
4377 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4378 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4380 GNUNET_PEER_resolve(t->id.oid, &id);
4381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4382 "Tunnel %s [%X] timed out. Destroying.\n",
4383 GNUNET_i2s(&id), t->id.tid);
4384 send_clients_tunnel_destroy (t);
4389 * Resets the tunnel timeout. Starts it if no timeout was running.
4391 * @param t Tunnel whose timeout to reset.
4393 * TODO use heap to improve efficiency of scheduler.
4396 tunnel_reset_timeout (struct MeshTunnel *t)
4398 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4399 GNUNET_SCHEDULER_cancel (t->timeout_task);
4401 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4402 (refresh_path_time, 4), &tunnel_timeout, t);
4406 /******************************************************************************/
4407 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4408 /******************************************************************************/
4411 * Function to send a create path packet to a peer.
4413 * @param cls closure
4414 * @param size number of bytes available in buf
4415 * @param buf where the callee should write the message
4416 * @return number of bytes written to buf
4419 send_core_path_create (void *cls, size_t size, void *buf)
4421 struct MeshPathInfo *info = cls;
4422 struct GNUNET_MESH_ManipulatePath *msg;
4423 struct GNUNET_PeerIdentity *peer_ptr;
4424 struct MeshTunnel *t = info->t;
4425 struct MeshPeerPath *p = info->path;
4430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4432 sizeof (struct GNUNET_MESH_ManipulatePath) +
4433 p->length * sizeof (struct GNUNET_PeerIdentity);
4435 if (size < size_needed || NULL == buf)
4440 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4441 msg->header.size = htons (size_needed);
4442 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4443 msg->tid = ntohl (t->id.tid);
4446 if (GNUNET_YES == t->speed_min)
4447 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4448 if (GNUNET_YES == t->nobuffer)
4449 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4450 msg->opt = htonl(opt);
4453 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4454 for (i = 0; i < p->length; i++)
4456 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4463 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4469 * Fill the core buffer
4471 * @param cls closure (data itself)
4472 * @param size number of bytes available in buf
4473 * @param buf where the callee should write the message
4475 * @return number of bytes written to buf
4478 send_core_data_multicast (void *cls, size_t size, void *buf)
4480 struct MeshTransmissionDescriptor *info = cls;
4483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4484 GNUNET_assert (NULL != info);
4485 GNUNET_assert (NULL != info->peer);
4486 total_size = info->mesh_data->data_len;
4487 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4489 if (total_size > size)
4494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4495 memcpy (buf, info->mesh_data->data, total_size);
4498 struct GNUNET_MESH_Multicast *mc;
4499 struct GNUNET_MessageHeader *mh;
4502 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4504 mc = (struct GNUNET_MESH_Multicast *) mh;
4505 mh = (struct GNUNET_MessageHeader *) &mc[1];
4506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4507 " multicast, payload type %s\n",
4508 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4510 " multicast, payload size %u\n", ntohs (mh->size));
4514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4515 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4519 data_descriptor_decrement_rc (info->mesh_data);
4520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4528 * Creates a path ack message in buf and frees all unused resources.
4530 * @param cls closure (MeshTransmissionDescriptor)
4531 * @param size number of bytes available in buf
4532 * @param buf where the callee should write the message
4533 * @return number of bytes written to buf
4536 send_core_path_ack (void *cls, size_t size, void *buf)
4538 struct MeshTransmissionDescriptor *info = cls;
4539 struct GNUNET_MESH_PathACK *msg = buf;
4541 GNUNET_assert (NULL != info);
4542 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4547 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4548 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4549 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4550 msg->tid = htonl (info->origin->tid);
4551 msg->peer_id = my_full_id;
4554 /* TODO add signature */
4556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4557 return sizeof (struct GNUNET_MESH_PathACK);
4562 * Free a transmission that was already queued with all resources
4563 * associated to the request.
4565 * @param queue Queue handler to cancel.
4566 * @param clear_cls Is it necessary to free associated cls?
4569 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4571 struct MeshTransmissionDescriptor *dd;
4572 struct MeshPathInfo *path_info;
4573 struct MeshTunnelChildInfo *cinfo;
4574 struct GNUNET_PeerIdentity id;
4578 if (GNUNET_YES == clear_cls)
4580 switch (queue->type)
4582 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4583 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4584 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4586 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4587 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4588 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4589 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4590 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4591 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4593 " prebuilt message\n");
4594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4596 GNUNET_MESH_DEBUG_M2S(queue->type));
4598 data_descriptor_decrement_rc (dd->mesh_data);
4600 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4602 path_info = queue->cls;
4603 path_destroy (path_info->path);
4607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4608 " type %s unknown!\n",
4609 GNUNET_MESH_DEBUG_M2S(queue->type));
4611 GNUNET_free_non_null (queue->cls);
4613 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4614 queue->peer->queue_tail,
4617 /* Delete from child_fc in the appropiate tunnel */
4618 max = queue->tunnel->fwd_queue_max;
4619 GNUNET_PEER_resolve (queue->peer->id, &id);
4620 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4623 for (i = 0; i < cinfo->send_buffer_n; i++)
4626 i2 = (cinfo->send_buffer_start + i) % max;
4627 if (cinfo->send_buffer[i2] == queue)
4629 /* Found corresponding entry in the send_buffer. Move all others back. */
4634 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4636 j2 = (cinfo->send_buffer_start + j) % max;
4637 j3 = (cinfo->send_buffer_start + j + 1) % max;
4638 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4641 cinfo->send_buffer[j3] = NULL;
4642 cinfo->send_buffer_n--;
4647 GNUNET_free (queue);
4652 * @brief Get the next transmittable message from the queue.
4654 * This will be the head, except in the case of being a data packet
4655 * not allowed by the destination peer.
4657 * @param peer Destination peer.
4659 * @return The next viable MeshPeerQueue element to send to that peer.
4660 * NULL when there are no transmittable messages.
4662 struct MeshPeerQueue *
4663 queue_get_next (const struct MeshPeerInfo *peer)
4665 struct MeshPeerQueue *q;
4666 struct MeshTunnel *t;
4667 struct MeshTransmissionDescriptor *info;
4668 struct MeshTunnelChildInfo *cinfo;
4669 struct GNUNET_MESH_Unicast *ucast;
4670 struct GNUNET_MESH_ToOrigin *to_orig;
4671 struct GNUNET_MESH_Multicast *mcast;
4672 struct GNUNET_PeerIdentity id;
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4677 for (q = peer->queue_head; NULL != q; q = q->next)
4681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4683 GNUNET_MESH_DEBUG_M2S(q->type));
4686 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4687 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4688 pid = ntohl (ucast->pid);
4689 GNUNET_PEER_resolve (info->peer->id, &id);
4690 cinfo = tunnel_get_neighbor_fc(t, &id);
4691 ack = cinfo->fwd_ack;
4693 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4694 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4695 pid = ntohl (to_orig->pid);
4698 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4699 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4700 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4702 // Not a multicast payload: multicast control traffic (destroy, etc)
4705 pid = ntohl (mcast->pid);
4706 GNUNET_PEER_resolve (info->peer->id, &id);
4707 cinfo = tunnel_get_neighbor_fc(t, &id);
4708 ack = cinfo->fwd_ack;
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4716 "********* ACK: %u, PID: %u\n",
4718 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4727 "********* NEXT!\n");
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4731 "********* nothing found\n");
4737 * Core callback to write a queued packet to core buffer
4739 * @param cls Closure (peer info).
4740 * @param size Number of bytes available in buf.
4741 * @param buf Where the to write the message.
4743 * @return number of bytes written to buf
4746 queue_send (void *cls, size_t size, void *buf)
4748 struct MeshPeerInfo *peer = cls;
4749 struct GNUNET_MessageHeader *msg;
4750 struct MeshPeerQueue *queue;
4751 struct MeshTunnel *t;
4752 struct MeshTunnelChildInfo *cinfo;
4753 struct GNUNET_PeerIdentity dst_id;
4756 peer->core_transmit = NULL;
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4760 queue = queue_get_next (peer);
4762 /* Queue has no internal mesh traffic nor sendable payload */
4765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4766 if (NULL == peer->queue_head)
4767 GNUNET_break (0); // Should've been canceled
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4772 GNUNET_PEER_resolve (peer->id, &dst_id);
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4774 "********* towards %s\n",
4775 GNUNET_i2s(&dst_id));
4776 /* Check if buffer size is enough for the message */
4777 if (queue->size > size)
4779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4780 "********* not enough room, reissue\n");
4781 peer->core_transmit =
4782 GNUNET_CORE_notify_transmit_ready (core_handle,
4785 GNUNET_TIME_UNIT_FOREVER_REL,
4792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4795 GNUNET_assert (0 < t->pending_messages);
4796 t->pending_messages--;
4797 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4801 "********* unicast: t->q (%u/%u)\n",
4802 t->fwd_queue_n, t->fwd_queue_max);
4804 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4811 switch (queue->type)
4814 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4815 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4816 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4817 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4818 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4820 "********* raw: %s\n",
4821 GNUNET_MESH_DEBUG_M2S (queue->type));
4823 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4824 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4825 data_size = send_core_data_raw (queue->cls, size, buf);
4826 msg = (struct GNUNET_MessageHeader *) buf;
4827 switch (ntohs (msg->type)) // Type of preconstructed message
4829 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4830 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4832 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4833 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4839 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4842 struct MeshTransmissionDescriptor *info = queue->cls;
4844 if ((1 == info->mesh_data->reference_counter
4845 && GNUNET_YES == t->speed_min)
4847 (info->mesh_data->total_out == info->mesh_data->reference_counter
4848 && GNUNET_NO == t->speed_min))
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4851 "********* considered sent\n");
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4857 "********* NOT considered sent yet\n");
4858 t->pending_messages++;
4861 data_size = send_core_data_multicast(queue->cls, size, buf);
4862 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4864 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4866 data_size = send_core_path_create (queue->cls, size, buf);
4868 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4870 data_size = send_core_path_ack (queue->cls, size, buf);
4872 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4874 data_size = send_core_data_multicast (queue->cls, size, buf);
4878 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4879 "********* type unknown: %u\n",
4883 switch (queue->type)
4885 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4886 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4887 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4888 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4889 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4892 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4893 "at pos %u (%p) != %p\n",
4894 cinfo->send_buffer_start,
4895 cinfo->send_buffer[cinfo->send_buffer_start],
4898 if (cinfo->send_buffer_n > 0)
4900 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4901 cinfo->send_buffer_n--;
4902 cinfo->send_buffer_start++;
4903 cinfo->send_buffer_start %= t->fwd_queue_max;
4914 /* Free queue, but cls was freed by send_core_* */
4915 queue_destroy (queue, GNUNET_NO);
4917 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4923 /* If more data in queue, send next */
4924 queue = queue_get_next(peer);
4927 struct GNUNET_PeerIdentity id;
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4930 GNUNET_PEER_resolve (peer->id, &id);
4931 peer->core_transmit =
4932 GNUNET_CORE_notify_transmit_ready(core_handle,
4935 GNUNET_TIME_UNIT_FOREVER_REL,
4943 if (NULL != peer->queue_head)
4945 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4946 "********* %s stalled\n",
4947 GNUNET_i2s(&my_full_id));
4949 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4950 // FIXME unify bck/fwd structures, bck does not have cinfo right now
4951 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
4953 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
4954 &tunnel_poll, cinfo);
4958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4964 * @brief Queue and pass message to core when possible.
4966 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4967 * and accounts for it. In case the queue is full, the message is dropped and
4970 * Otherwise, message is treated as internal and allowed to go regardless of
4973 * @param cls Closure (@c type dependant). It will be used by queue_send to
4974 * build the message to be sent if not already prebuilt.
4975 * @param type Type of the message, 0 for a raw message.
4976 * @param size Size of the message.
4977 * @param dst Neighbor to send message to.
4978 * @param t Tunnel this message belongs to.
4981 queue_add (void *cls, uint16_t type, size_t size,
4982 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4984 struct MeshPeerQueue *queue;
4985 struct MeshTunnelChildInfo *cinfo;
4986 struct GNUNET_PeerIdentity id;
4992 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4993 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4995 n = &t->fwd_queue_n;
4996 max = &t->fwd_queue_max;
4998 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5000 n = &t->bck_queue_n;
5001 max = &t->bck_queue_max;
5008 GNUNET_STATISTICS_update(stats,
5009 "# messages dropped (buffer full)",
5011 return; // Drop message
5015 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5021 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5022 GNUNET_PEER_resolve (dst->id, &id);
5023 if (NULL == dst->core_transmit)
5025 dst->core_transmit =
5026 GNUNET_CORE_notify_transmit_ready (core_handle,
5029 GNUNET_TIME_UNIT_FOREVER_REL,
5035 t->pending_messages++;
5036 if (NULL == n) // Is this internal mesh traffic?
5039 // It's payload, keep track of buffer per peer.
5040 cinfo = tunnel_get_neighbor_fc(t, &id);
5041 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5042 if (NULL != cinfo->send_buffer[i])
5044 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5045 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5046 cinfo->send_buffer_start++;
5047 cinfo->send_buffer_start %= t->fwd_queue_max;
5051 cinfo->send_buffer_n++;
5053 cinfo->send_buffer[i] = queue;
5054 if (cinfo->send_buffer_n > t->fwd_queue_max)
5057 cinfo->send_buffer_n = t->fwd_queue_max;
5062 /******************************************************************************/
5063 /******************** MESH NETWORK HANDLERS **************************/
5064 /******************************************************************************/
5068 * Core handler for path creation
5070 * @param cls closure
5071 * @param message message
5072 * @param peer peer identity this notification is about
5073 * @param atsi performance data
5074 * @param atsi_count number of records in 'atsi'
5076 * @return GNUNET_OK to keep the connection open,
5077 * GNUNET_SYSERR to close it (signal serious error)
5080 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5081 const struct GNUNET_MessageHeader *message,
5082 const struct GNUNET_ATS_Information *atsi,
5083 unsigned int atsi_count)
5085 unsigned int own_pos;
5088 MESH_TunnelNumber tid;
5089 struct GNUNET_MESH_ManipulatePath *msg;
5090 struct GNUNET_PeerIdentity *pi;
5091 struct GNUNET_HashCode hash;
5092 struct MeshPeerPath *path;
5093 struct MeshPeerInfo *dest_peer_info;
5094 struct MeshPeerInfo *orig_peer_info;
5095 struct MeshTunnel *t;
5097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5098 "Received a path create msg [%s]\n",
5099 GNUNET_i2s (&my_full_id));
5100 size = ntohs (message->size);
5101 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5103 GNUNET_break_op (0);
5107 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5108 if (size % sizeof (struct GNUNET_PeerIdentity))
5110 GNUNET_break_op (0);
5113 size /= sizeof (struct GNUNET_PeerIdentity);
5116 GNUNET_break_op (0);
5119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5120 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5122 tid = ntohl (msg->tid);
5123 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5125 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5126 t = tunnel_get (pi, tid);
5127 if (NULL == t) // FIXME only for INCOMING tunnels?
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5132 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5135 // FIXME notify failure
5138 opt = ntohl (msg->opt);
5139 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5140 GNUNET_YES : GNUNET_NO;
5141 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5143 t->nobuffer = GNUNET_YES;
5144 t->last_fwd_ack = t->fwd_pid + 1;
5146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5147 " speed_min: %d, nobuffer:%d\n",
5148 t->speed_min, t->nobuffer);
5150 if (GNUNET_YES == t->nobuffer)
5152 t->bck_queue_max = 1;
5153 t->fwd_queue_max = 1;
5156 // FIXME only assign a local tid if a local client is interested (on demand)
5157 while (NULL != tunnel_get_incoming (next_local_tid))
5158 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5159 t->local_tid_dest = next_local_tid++;
5160 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5163 tunnel_reset_timeout (t);
5164 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5166 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5167 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5175 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5176 if (NULL == dest_peer_info)
5178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5179 " Creating PeerInfo for destination.\n");
5180 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5181 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5182 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5184 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5186 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5187 if (NULL == orig_peer_info)
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5190 " Creating PeerInfo for origin.\n");
5191 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5192 orig_peer_info->id = GNUNET_PEER_intern (pi);
5193 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5194 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5197 path = path_new (size);
5199 for (i = 0; i < size; i++)
5201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5202 GNUNET_i2s (&pi[i]));
5203 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5204 if (path->peers[i] == myid)
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5210 /* cannot be self, must be 'not found' */
5211 /* create path: self not found in path through self */
5212 GNUNET_break_op (0);
5213 path_destroy (path);
5217 path_add_to_peers (path, GNUNET_NO);
5218 tunnel_add_path (t, path, own_pos);
5219 if (own_pos == size - 1)
5221 /* It is for us! Send ack. */
5222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5223 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5224 if (NULL == t->peers)
5226 /* New tunnel! Notify clients on first payload message. */
5227 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5229 GNUNET_break (GNUNET_SYSERR !=
5230 GNUNET_CONTAINER_multihashmap_put (t->peers,
5231 &my_full_id.hashPubKey,
5234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5239 struct MeshPeerPath *path2;
5241 /* It's for somebody else! Retransmit. */
5242 path2 = path_duplicate (path);
5243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5244 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5245 path2 = path_duplicate (path);
5246 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5247 send_create_path (dest_peer_info, path, t);
5254 * Core handler for path destruction
5256 * @param cls closure
5257 * @param message message
5258 * @param peer peer identity this notification is about
5259 * @param atsi performance data
5260 * @param atsi_count number of records in 'atsi'
5262 * @return GNUNET_OK to keep the connection open,
5263 * GNUNET_SYSERR to close it (signal serious error)
5266 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5267 const struct GNUNET_MessageHeader *message,
5268 const struct GNUNET_ATS_Information *atsi,
5269 unsigned int atsi_count)
5271 struct GNUNET_MESH_ManipulatePath *msg;
5272 struct GNUNET_PeerIdentity *pi;
5273 struct MeshPeerPath *path;
5274 struct MeshTunnel *t;
5275 unsigned int own_pos;
5279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5280 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5281 size = ntohs (message->size);
5282 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5284 GNUNET_break_op (0);
5288 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5289 if (size % sizeof (struct GNUNET_PeerIdentity))
5291 GNUNET_break_op (0);
5294 size /= sizeof (struct GNUNET_PeerIdentity);
5297 GNUNET_break_op (0);
5300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5302 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5303 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5305 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5307 t = tunnel_get (pi, ntohl (msg->tid));
5310 /* TODO notify back: we don't know this tunnel */
5311 GNUNET_break_op (0);
5314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5315 path = path_new (size);
5317 for (i = 0; i < size; i++)
5319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5320 GNUNET_i2s (&pi[i]));
5321 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5322 if (path->peers[i] == myid)
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5326 if (own_pos < path->length - 1)
5327 send_prebuilt_message (message, &pi[own_pos + 1], t);
5329 send_client_tunnel_disconnect(t, NULL);
5331 tunnel_delete_peer (t, path->peers[path->length - 1]);
5332 path_destroy (path);
5338 * Core handler for notifications of broken paths
5340 * @param cls closure
5341 * @param message message
5342 * @param peer peer identity this notification is about
5343 * @param atsi performance data
5344 * @param atsi_count number of records in 'atsi'
5346 * @return GNUNET_OK to keep the connection open,
5347 * GNUNET_SYSERR to close it (signal serious error)
5350 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5351 const struct GNUNET_MessageHeader *message,
5352 const struct GNUNET_ATS_Information *atsi,
5353 unsigned int atsi_count)
5355 struct GNUNET_MESH_PathBroken *msg;
5356 struct MeshTunnel *t;
5358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5359 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5360 msg = (struct GNUNET_MESH_PathBroken *) message;
5361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5362 GNUNET_i2s (&msg->peer1));
5363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5364 GNUNET_i2s (&msg->peer2));
5365 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5368 GNUNET_break_op (0);
5371 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5372 GNUNET_PEER_search (&msg->peer2));
5379 * Core handler for tunnel destruction
5381 * @param cls closure
5382 * @param message message
5383 * @param peer peer identity this notification is about
5384 * @param atsi performance data
5385 * @param atsi_count number of records in 'atsi'
5387 * @return GNUNET_OK to keep the connection open,
5388 * GNUNET_SYSERR to close it (signal serious error)
5391 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5392 const struct GNUNET_MessageHeader *message,
5393 const struct GNUNET_ATS_Information *atsi,
5394 unsigned int atsi_count)
5396 struct GNUNET_MESH_TunnelDestroy *msg;
5397 struct MeshTunnel *t;
5398 GNUNET_PEER_Id parent;
5401 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5403 "Got a TUNNEL DESTROY packet from %s\n",
5405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5406 " for tunnel %s [%u]\n",
5407 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5408 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5409 /* Check signature */
5412 /* Probably already got the message from another path,
5413 * destroyed the tunnel and retransmitted to children.
5416 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5420 parent = tree_get_predecessor (t->tree);
5421 pid = GNUNET_PEER_search (peer);
5426 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5427 nc = tree_count_children (t->tree);
5428 if (nc > 0 || NULL != t->owner || t->nclients > 0)
5430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5431 "still in use: %u cl, %u ch\n",
5436 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5438 /* Tunnel was incoming, notify clients */
5439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5440 t->local_tid, t->local_tid_dest);
5441 send_clients_tunnel_destroy (t);
5443 tunnel_send_destroy (t, parent);
5444 t->destroy = GNUNET_YES;
5445 // TODO: add timeout to destroy the tunnel anyway
5451 * Core handler for mesh network traffic going from the origin to a peer
5453 * @param cls closure
5454 * @param peer peer identity this notification is about
5455 * @param message message
5456 * @param atsi performance data
5457 * @param atsi_count number of records in 'atsi'
5458 * @return GNUNET_OK to keep the connection open,
5459 * GNUNET_SYSERR to close it (signal serious error)
5462 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5463 const struct GNUNET_MessageHeader *message,
5464 const struct GNUNET_ATS_Information *atsi,
5465 unsigned int atsi_count)
5467 struct GNUNET_MESH_Unicast *msg;
5468 struct GNUNET_PeerIdentity *neighbor;
5469 struct MeshTunnelChildInfo *cinfo;
5470 struct MeshTunnel *t;
5471 GNUNET_PEER_Id dest_id;
5476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5479 size = ntohs (message->size);
5481 sizeof (struct GNUNET_MESH_Unicast) +
5482 sizeof (struct GNUNET_MessageHeader))
5487 msg = (struct GNUNET_MESH_Unicast *) message;
5488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5489 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5491 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5494 /* TODO notify back: we don't know this tunnel */
5495 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5496 GNUNET_break_op (0);
5499 pid = ntohl (msg->pid);
5500 if (t->fwd_pid == pid)
5502 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5504 " Already seen pid %u, DROPPING!\n", pid);
5509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5510 " pid %u not seen yet, forwarding\n", pid);
5513 t->skip += (pid - t->fwd_pid) - 1;
5516 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5518 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5519 GNUNET_break_op (0);
5520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5521 "Received PID %u, ACK %u\n",
5522 pid, t->last_fwd_ack);
5526 tunnel_reset_timeout (t);
5527 dest_id = GNUNET_PEER_search (&msg->destination);
5528 if (dest_id == myid)
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5531 " it's for us! sending to clients...\n");
5532 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5533 send_subscribed_clients (message, &msg[1].header, t);
5534 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5537 ttl = ntohl (msg->ttl);
5538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5541 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5542 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5543 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5547 " not for us, retransmitting...\n");
5549 neighbor = tree_get_first_hop (t->tree, dest_id);
5550 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5551 cinfo->fwd_pid = pid;
5552 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5555 if (GNUNET_YES == t->nobuffer &&
5556 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5558 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5559 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5560 GNUNET_break_op (0);
5563 send_prebuilt_message (message, neighbor, t);
5564 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5570 * Core handler for mesh network traffic going from the origin to all peers
5572 * @param cls closure
5573 * @param message message
5574 * @param peer peer identity this notification is about
5575 * @param atsi performance data
5576 * @param atsi_count number of records in 'atsi'
5577 * @return GNUNET_OK to keep the connection open,
5578 * GNUNET_SYSERR to close it (signal serious error)
5580 * TODO: Check who we got this from, to validate route.
5583 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5584 const struct GNUNET_MessageHeader *message,
5585 const struct GNUNET_ATS_Information *atsi,
5586 unsigned int atsi_count)
5588 struct GNUNET_MESH_Multicast *msg;
5589 struct MeshTunnel *t;
5593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5595 size = ntohs (message->size);
5596 if (sizeof (struct GNUNET_MESH_Multicast) +
5597 sizeof (struct GNUNET_MessageHeader) > size)
5599 GNUNET_break_op (0);
5602 msg = (struct GNUNET_MESH_Multicast *) message;
5603 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5607 /* TODO notify that we dont know that tunnel */
5608 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5609 GNUNET_break_op (0);
5612 pid = ntohl (msg->pid);
5613 if (t->fwd_pid == pid)
5615 /* already seen this packet, drop */
5616 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5618 " Already seen pid %u, DROPPING!\n", pid);
5619 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5625 " pid %u not seen yet, forwarding\n", pid);
5627 t->skip += (pid - t->fwd_pid) - 1;
5629 tunnel_reset_timeout (t);
5631 /* Transmit to locally interested clients */
5632 if (NULL != t->peers &&
5633 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5635 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5636 send_subscribed_clients (message, &msg[1].header, t);
5637 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5640 if (ntohl (msg->ttl) == 0)
5642 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5643 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5644 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5647 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5648 tunnel_send_multicast (t, message);
5654 * Core handler for mesh network traffic toward the owner of a tunnel
5656 * @param cls closure
5657 * @param message message
5658 * @param peer peer identity this notification is about
5659 * @param atsi performance data
5660 * @param atsi_count number of records in 'atsi'
5662 * @return GNUNET_OK to keep the connection open,
5663 * GNUNET_SYSERR to close it (signal serious error)
5666 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5667 const struct GNUNET_MessageHeader *message,
5668 const struct GNUNET_ATS_Information *atsi,
5669 unsigned int atsi_count)
5671 struct GNUNET_MESH_ToOrigin *msg;
5672 struct GNUNET_PeerIdentity id;
5673 struct MeshPeerInfo *peer_info;
5674 struct MeshTunnel *t;
5675 struct MeshTunnelChildInfo *cinfo;
5676 GNUNET_PEER_Id predecessor;
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5682 size = ntohs (message->size);
5683 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5684 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5686 GNUNET_break_op (0);
5689 msg = (struct GNUNET_MESH_ToOrigin *) message;
5690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5691 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5692 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5693 pid = ntohl (msg->pid);
5697 /* TODO notify that we dont know this tunnel (whom)? */
5698 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5700 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
5701 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5705 cinfo = tunnel_get_neighbor_fc(t, peer);
5712 if (cinfo->bck_pid == pid)
5714 /* already seen this packet, drop */
5715 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5717 " Already seen pid %u, DROPPING!\n", pid);
5718 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5723 " pid %u not seen yet, forwarding\n", pid);
5724 cinfo->bck_pid = pid;
5726 if (NULL != t->owner)
5729 struct GNUNET_MESH_ToOrigin *copy;
5731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5732 " it's for us! sending to clients...\n");
5733 /* TODO signature verification */
5734 memcpy (cbuf, message, size);
5735 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5736 copy->tid = htonl (t->local_tid);
5738 copy->pid = htonl (t->bck_pid);
5739 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5740 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5741 ©->header, GNUNET_NO);
5742 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5746 " not for us, retransmitting...\n");
5748 peer_info = peer_info_get (&msg->oid);
5749 if (NULL == peer_info)
5751 /* unknown origin of tunnel */
5755 predecessor = tree_get_predecessor (t->tree);
5756 if (0 == predecessor)
5758 if (GNUNET_YES == t->destroy)
5760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5761 "to orig received on a dying tunnel %s [%X]\n",
5762 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5765 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5766 "unknown to origin at %s\n",
5767 GNUNET_i2s (&my_full_id));
5768 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5772 "for tunnel %s [%X]\n",
5773 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5774 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5776 tree_debug (t->tree);
5779 GNUNET_PEER_resolve (predecessor, &id);
5780 send_prebuilt_message (message, &id, t);
5781 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5788 * Core handler for mesh network traffic point-to-point acks.
5790 * @param cls closure
5791 * @param message message
5792 * @param peer peer identity this notification is about
5793 * @param atsi performance data
5794 * @param atsi_count number of records in 'atsi'
5796 * @return GNUNET_OK to keep the connection open,
5797 * GNUNET_SYSERR to close it (signal serious error)
5800 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5801 const struct GNUNET_MessageHeader *message,
5802 const struct GNUNET_ATS_Information *atsi,
5803 unsigned int atsi_count)
5805 struct GNUNET_MESH_ACK *msg;
5806 struct MeshTunnel *t;
5809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5811 msg = (struct GNUNET_MESH_ACK *) message;
5813 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5817 /* TODO notify that we dont know this tunnel (whom)? */
5818 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5821 ack = ntohl (msg->pid);
5822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5824 /* Is this a forward or backward ACK? */
5825 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5827 struct MeshTunnelChildInfo *cinfo;
5830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5831 cinfo = tunnel_get_neighbor_fc (t, peer);
5832 cinfo->fwd_ack = ack;
5833 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5834 tunnel_unlock_fwd_queues (t);
5835 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
5837 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
5838 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
5839 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
5844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5846 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5847 tunnel_unlock_bck_queue (t);
5854 * Core handler for mesh network traffic point-to-point ack polls.
5856 * @param cls closure
5857 * @param message message
5858 * @param peer peer identity this notification is about
5859 * @param atsi performance data
5860 * @param atsi_count number of records in 'atsi'
5862 * @return GNUNET_OK to keep the connection open,
5863 * GNUNET_SYSERR to close it (signal serious error)
5866 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5867 const struct GNUNET_MessageHeader *message,
5868 const struct GNUNET_ATS_Information *atsi,
5869 unsigned int atsi_count)
5871 struct GNUNET_MESH_Poll *msg;
5872 struct MeshTunnel *t;
5874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5877 msg = (struct GNUNET_MESH_Poll *) message;
5879 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5883 /* TODO notify that we dont know this tunnel (whom)? */
5884 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5885 GNUNET_break_op (0);
5889 /* Is this a forward or backward ACK? */
5890 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5892 struct MeshTunnelChildInfo *cinfo;
5894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5895 cinfo = tunnel_get_neighbor_fc (t, peer);
5896 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5897 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5902 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5909 * Core handler for path ACKs
5911 * @param cls closure
5912 * @param message message
5913 * @param peer peer identity this notification is about
5914 * @param atsi performance data
5915 * @param atsi_count number of records in 'atsi'
5917 * @return GNUNET_OK to keep the connection open,
5918 * GNUNET_SYSERR to close it (signal serious error)
5921 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5922 const struct GNUNET_MessageHeader *message,
5923 const struct GNUNET_ATS_Information *atsi,
5924 unsigned int atsi_count)
5926 struct GNUNET_MESH_PathACK *msg;
5927 struct GNUNET_PeerIdentity id;
5928 struct MeshPeerInfo *peer_info;
5929 struct MeshPeerPath *p;
5930 struct MeshTunnel *t;
5932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5933 GNUNET_i2s (&my_full_id));
5934 msg = (struct GNUNET_MESH_PathACK *) message;
5935 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5938 /* TODO notify that we don't know the tunnel */
5939 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5941 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5945 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5947 peer_info = peer_info_get (&msg->peer_id);
5948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5949 GNUNET_i2s (&msg->peer_id));
5950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5953 if (NULL != t->regex_search && t->regex_search->peer == peer_info->id)
5955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5956 "connect_by_string completed, stopping search\n");
5957 regex_cancel_search (t->regex_search);
5958 t->regex_search = NULL;
5961 /* Add paths to peers? */
5962 p = tree_get_path_to_peer (t->tree, peer_info->id);
5965 path_add_to_peers (p, GNUNET_YES);
5973 /* Message for us? */
5974 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5977 if (NULL == t->owner)
5979 GNUNET_break_op (0);
5982 if (NULL != t->dht_get_type)
5984 GNUNET_DHT_get_stop (t->dht_get_type);
5985 t->dht_get_type = NULL;
5987 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5989 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5990 send_client_peer_connected (t, peer_info->id);
5995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5996 " not for us, retransmitting...\n");
5997 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5998 peer_info = peer_info_get (&msg->oid);
5999 if (NULL == peer_info)
6001 /* If we know the tunnel, we should DEFINITELY know the peer */
6005 send_prebuilt_message (message, &id, t);
6011 * Core handler for mesh keepalives.
6013 * @param cls closure
6014 * @param message message
6015 * @param peer peer identity this notification is about
6016 * @param atsi performance data
6017 * @param atsi_count number of records in 'atsi'
6018 * @return GNUNET_OK to keep the connection open,
6019 * GNUNET_SYSERR to close it (signal serious error)
6021 * TODO: Check who we got this from, to validate route.
6024 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6025 const struct GNUNET_MessageHeader *message,
6026 const struct GNUNET_ATS_Information *atsi,
6027 unsigned int atsi_count)
6029 struct GNUNET_MESH_TunnelKeepAlive *msg;
6030 struct MeshTunnel *t;
6032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6035 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6036 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6040 /* TODO notify that we dont know that tunnel */
6041 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
6046 tunnel_reset_timeout (t);
6048 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6049 tunnel_send_multicast (t, message);
6056 * Functions to handle messages from core
6058 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6059 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6060 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6061 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6062 sizeof (struct GNUNET_MESH_PathBroken)},
6063 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6064 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6065 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6066 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6067 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6068 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6069 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6070 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6071 sizeof (struct GNUNET_MESH_ACK)},
6072 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6073 sizeof (struct GNUNET_MESH_Poll)},
6074 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6075 sizeof (struct GNUNET_MESH_PathACK)},
6081 /******************************************************************************/
6082 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6083 /******************************************************************************/
6086 * deregister_app: iterator for removing each application registered by a client
6088 * @param cls closure
6089 * @param key the hash of the application id (used to access the hashmap)
6090 * @param value the value stored at the key (client)
6092 * @return GNUNET_OK on success
6095 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6097 struct MeshClient *c = cls;
6099 GNUNET_break (GNUNET_YES ==
6100 GNUNET_CONTAINER_multihashmap_remove (applications, key, c));
6106 * notify_client_connection_failure: notify a client that the connection to the
6107 * requested remote peer is not possible (for instance, no route found)
6108 * Function called when the socket is ready to queue more data. "buf" will be
6109 * NULL and "size" zero if the socket was closed for writing in the meantime.
6111 * @param cls closure
6112 * @param size number of bytes available in buf
6113 * @param buf where the callee should write the message
6114 * @return number of bytes written to buf
6117 notify_client_connection_failure (void *cls, size_t size, void *buf)
6120 struct MeshPeerInfo *peer_info;
6121 struct GNUNET_MESH_PeerControl *msg;
6122 struct GNUNET_PeerIdentity id;
6124 if (0 == size && NULL == buf)
6126 // TODO retry? cancel?
6130 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6131 peer_info = (struct MeshPeerInfo *) cls;
6132 msg = (struct GNUNET_MESH_PeerControl *) buf;
6133 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6134 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6135 // msg->tunnel_id = htonl(peer_info->t->tid);
6136 GNUNET_PEER_resolve (peer_info->id, &id);
6137 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6145 * Send keepalive packets for a peer
6147 * @param cls Closure (tunnel for which to send the keepalive).
6148 * @param tc Notification context.
6151 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6153 struct MeshTunnel *t = cls;
6154 struct GNUNET_MESH_TunnelKeepAlive *msg;
6155 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6158 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6159 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6165 "sending keepalive for tunnel %d\n", t->id.tid);
6167 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6168 msg->header.size = htons (size);
6169 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6170 msg->oid = my_full_id;
6171 msg->tid = htonl (t->id.tid);
6172 tunnel_send_multicast (t, &msg->header);
6174 t->path_refresh_task =
6175 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6176 tunnel_reset_timeout(t);
6181 * Function to process paths received for a new peer addition. The recorded
6182 * paths form the initial tunnel, which can be optimized later.
6183 * Called on each result obtained for the DHT search.
6185 * @param cls closure
6186 * @param exp when will this value expire
6187 * @param key key of the result
6188 * @param get_path path of the get request
6189 * @param get_path_length lenght of get_path
6190 * @param put_path path of the put request
6191 * @param put_path_length length of the put_path
6192 * @param type type of the result
6193 * @param size number of bytes in data
6194 * @param data pointer to the result data
6196 * TODO: re-issue the request after certain time? cancel after X results?
6199 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6200 const struct GNUNET_HashCode * key,
6201 const struct GNUNET_PeerIdentity *get_path,
6202 unsigned int get_path_length,
6203 const struct GNUNET_PeerIdentity *put_path,
6204 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6205 size_t size, const void *data)
6207 struct MeshPathInfo *path_info = cls;
6208 struct MeshPeerPath *p;
6209 struct GNUNET_PeerIdentity pi;
6212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6213 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6216 p = path_build_from_dht (get_path, get_path_length, put_path,
6218 path_add_to_peers (p, GNUNET_NO);
6220 for (i = 0; i < path_info->peer->ntunnels; i++)
6222 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6223 peer_info_connect (path_info->peer, path_info->t);
6231 * Function to process paths received for a new peer addition. The recorded
6232 * paths form the initial tunnel, which can be optimized later.
6233 * Called on each result obtained for the DHT search.
6235 * @param cls closure
6236 * @param exp when will this value expire
6237 * @param key key of the result
6238 * @param get_path path of the get request
6239 * @param get_path_length lenght of get_path
6240 * @param put_path path of the put request
6241 * @param put_path_length length of the put_path
6242 * @param type type of the result
6243 * @param size number of bytes in data
6244 * @param data pointer to the result data
6247 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6248 const struct GNUNET_HashCode * key,
6249 const struct GNUNET_PeerIdentity *get_path,
6250 unsigned int get_path_length,
6251 const struct GNUNET_PeerIdentity *put_path,
6252 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6253 size_t size, const void *data)
6255 const struct PBlock *pb = data;
6256 const struct GNUNET_PeerIdentity *pi = &pb->id;
6257 struct MeshTunnel *t = cls;
6258 struct MeshPeerInfo *peer_info;
6259 struct MeshPeerPath *p;
6261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6262 if (size != sizeof (struct PBlock))
6264 GNUNET_break_op (0);
6267 if (ntohl(pb->type) != t->type)
6269 GNUNET_break_op (0);
6272 GNUNET_assert (NULL != t->owner);
6273 peer_info = peer_info_get (pi);
6274 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6276 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6278 p = path_build_from_dht (get_path, get_path_length, put_path,
6280 path_add_to_peers (p, GNUNET_NO);
6282 tunnel_add_peer (t, peer_info);
6283 peer_info_connect (peer_info, t);
6287 /******************************************************************************/
6288 /********************* MESH LOCAL HANDLES **************************/
6289 /******************************************************************************/
6293 * Handler for client disconnection
6295 * @param cls closure
6296 * @param client identification of the client; NULL
6297 * for the last call when the server is destroyed
6300 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6302 struct MeshClient *c;
6303 struct MeshClient *next;
6306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6316 if (c->handle != client)
6318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6324 GNUNET_SERVER_client_drop (c->handle);
6325 c->shutting_down = GNUNET_YES;
6326 GNUNET_assert (NULL != c->own_tunnels);
6327 GNUNET_assert (NULL != c->incoming_tunnels);
6328 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6329 &tunnel_destroy_iterator, c);
6330 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6331 &tunnel_destroy_iterator, c);
6332 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6333 &tunnel_destroy_iterator, c);
6334 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6335 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6336 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6338 /* deregister clients applications */
6339 if (NULL != c->apps)
6341 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c);
6342 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6344 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6345 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6347 GNUNET_SCHEDULER_cancel (announce_applications_task);
6348 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6350 if (NULL != c->types)
6351 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6352 for (i = 0; i < c->n_regex; i++)
6354 GNUNET_free (c->regexes[i].regex);
6355 if (NULL != c->regexes[i].h)
6356 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
6358 GNUNET_free_non_null (c->regexes);
6359 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6360 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6362 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6365 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6374 * Handler for new clients
6376 * @param cls closure
6377 * @param client identification of the client
6378 * @param message the actual message, which includes messages the client wants
6381 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6382 const struct GNUNET_MessageHeader *message)
6384 struct GNUNET_MESH_ClientConnect *cc_msg;
6385 struct MeshClient *c;
6386 GNUNET_MESH_ApplicationType *a;
6393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6394 /* Check data sanity */
6395 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6396 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6397 ntypes = ntohs (cc_msg->types);
6398 napps = ntohs (cc_msg->applications);
6400 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6407 /* Create new client structure */
6408 c = GNUNET_malloc (sizeof (struct MeshClient));
6409 c->id = next_client_id++;
6410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6412 GNUNET_SERVER_client_keep (client);
6413 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6416 GNUNET_MESH_ApplicationType at;
6417 struct GNUNET_HashCode hc;
6419 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6420 for (i = 0; i < napps; i++)
6423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6424 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6425 /* store in clients hashmap */
6426 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6427 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6428 /* store in global hashmap, for announcements */
6429 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6430 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6432 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6433 announce_applications_task =
6434 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6440 struct GNUNET_HashCode hc;
6442 t = (uint16_t *) & a[napps];
6443 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6444 for (i = 0; i < ntypes; i++)
6447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6448 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6450 /* store in clients hashmap */
6451 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6452 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6453 /* store in global hashmap */
6454 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6459 " client has %u+%u subscriptions\n", napps, ntypes);
6461 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6462 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6463 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6464 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6465 GNUNET_SERVER_notification_context_add (nc, client);
6466 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6468 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6474 * Handler for clients announcing available services by a regular expression.
6476 * @param cls closure
6477 * @param client identification of the client
6478 * @param message the actual message, which includes messages the client wants
6481 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6482 const struct GNUNET_MessageHeader *message)
6484 const struct GNUNET_MESH_RegexAnnounce *msg;
6485 struct MeshRegexDescriptor rd;
6486 struct MeshClient *c;
6491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6493 /* Sanity check for client registration */
6494 if (NULL == (c = client_get (client)))
6497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6502 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6504 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6505 if (NULL != c->partial_regex)
6507 regex = c->partial_regex;
6508 offset = strlen (c->partial_regex);
6509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6510 " continuation, already have %u bytes\n",
6519 regex = GNUNET_realloc (regex, offset + len + 1);
6520 memcpy (®ex[offset], &msg[1], len);
6521 regex[offset + len] = '\0';
6522 if (0 == ntohs (msg->last))
6524 c->partial_regex = regex;
6525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6526 " not ended, stored %u bytes for later\n",
6528 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6532 rd.compression = ntohs (msg->compression_characters);
6534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " compr %u\n", ntohs (rd.compression));
6537 GNUNET_array_append (c->regexes, c->n_regex, rd);
6538 c->partial_regex = NULL;
6539 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6541 c->regex_announce_task = GNUNET_SCHEDULER_add_now (®ex_announce, c);
6547 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6553 * Handler for requests of new tunnels
6555 * @param cls closure
6556 * @param client identification of the client
6557 * @param message the actual message
6560 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6561 const struct GNUNET_MessageHeader *message)
6563 struct GNUNET_MESH_TunnelMessage *t_msg;
6564 struct MeshTunnel *t;
6565 struct MeshClient *c;
6566 MESH_TunnelNumber tid;
6568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6570 /* Sanity check for client registration */
6571 if (NULL == (c = client_get (client)))
6574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6579 /* Message sanity check */
6580 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6583 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6587 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6588 /* Sanity check for tunnel numbering */
6589 tid = ntohl (t_msg->tunnel_id);
6590 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6596 /* Sanity check for duplicate tunnel IDs */
6597 if (NULL != tunnel_get_by_local_id (c, tid))
6600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6604 while (NULL != tunnel_get_by_pi (myid, next_tid))
6605 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6606 t = tunnel_new (myid, next_tid++, c, tid);
6609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6611 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6614 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6616 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6617 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6620 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6626 * Handler for requests of deleting tunnels
6628 * @param cls closure
6629 * @param client identification of the client
6630 * @param message the actual message
6633 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6634 const struct GNUNET_MessageHeader *message)
6636 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6637 struct MeshClient *c;
6638 struct MeshTunnel *t;
6639 MESH_TunnelNumber tid;
6641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6642 "Got a DESTROY TUNNEL from client!\n");
6644 /* Sanity check for client registration */
6645 if (NULL == (c = client_get (client)))
6648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6653 /* Message sanity check */
6654 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6661 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6663 /* Retrieve tunnel */
6664 tid = ntohl (tunnel_msg->tunnel_id);
6665 t = tunnel_get_by_local_id(c, tid);
6668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6673 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6675 client_ignore_tunnel (c, t);
6676 tunnel_destroy_empty (t);
6677 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6680 send_client_tunnel_disconnect (t, c);
6681 client_delete_tunnel (c, t);
6683 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6685 tunnel_send_destroy (t, 0);
6686 t->destroy = GNUNET_YES;
6687 /* The tunnel will be destroyed when the last message is transmitted. */
6688 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6694 * Handler for requests of seeting tunnel's speed.
6696 * @param cls Closure (unused).
6697 * @param client Identification of the client.
6698 * @param message The actual message.
6701 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6702 const struct GNUNET_MessageHeader *message)
6704 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6705 struct MeshClient *c;
6706 struct MeshTunnel *t;
6707 MESH_TunnelNumber tid;
6709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6710 "Got a SPEED request from client!\n");
6712 /* Sanity check for client registration */
6713 if (NULL == (c = client_get (client)))
6716 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6722 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6724 /* Retrieve tunnel */
6725 tid = ntohl (tunnel_msg->tunnel_id);
6726 t = tunnel_get_by_local_id(c, tid);
6729 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6735 switch (ntohs(message->type))
6737 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6738 t->speed_min = GNUNET_YES;
6740 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6741 t->speed_min = GNUNET_NO;
6746 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6751 * Handler for requests of seeting tunnel's buffering policy.
6753 * @param cls Closure (unused).
6754 * @param client Identification of the client.
6755 * @param message The actual message.
6758 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6759 const struct GNUNET_MessageHeader *message)
6761 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6762 struct MeshClient *c;
6763 struct MeshTunnel *t;
6764 MESH_TunnelNumber tid;
6766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6767 "Got a BUFFER request from client!\n");
6769 /* Sanity check for client registration */
6770 if (NULL == (c = client_get (client)))
6773 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6778 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6780 /* Retrieve tunnel */
6781 tid = ntohl (tunnel_msg->tunnel_id);
6782 t = tunnel_get_by_local_id(c, tid);
6785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6787 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6791 switch (ntohs(message->type))
6793 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6794 t->nobuffer = GNUNET_NO;
6796 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6797 t->nobuffer = GNUNET_YES;
6803 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6808 * Handler for connection requests to new peers
6810 * @param cls closure
6811 * @param client identification of the client
6812 * @param message the actual message (PeerControl)
6815 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6816 const struct GNUNET_MessageHeader *message)
6818 struct GNUNET_MESH_PeerControl *peer_msg;
6819 struct MeshPeerInfo *peer_info;
6820 struct MeshClient *c;
6821 struct MeshTunnel *t;
6822 MESH_TunnelNumber tid;
6824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6825 /* Sanity check for client registration */
6826 if (NULL == (c = client_get (client)))
6829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6834 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6836 /* Sanity check for message size */
6837 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6844 /* Tunnel exists? */
6845 tid = ntohl (peer_msg->tunnel_id);
6846 t = tunnel_get_by_local_id (c, tid);
6850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6854 /* Does client own tunnel? */
6855 if (t->owner->handle != client)
6858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6862 GNUNET_i2s (&peer_msg->peer));
6863 peer_info = peer_info_get (&peer_msg->peer);
6865 tunnel_add_peer (t, peer_info);
6866 peer_info_connect (peer_info, t);
6868 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6874 * Handler for disconnection requests of peers in a tunnel
6876 * @param cls closure
6877 * @param client identification of the client
6878 * @param message the actual message (PeerControl)
6881 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6882 const struct GNUNET_MessageHeader *message)
6884 struct GNUNET_MESH_PeerControl *peer_msg;
6885 struct MeshPeerInfo *peer_info;
6886 struct MeshClient *c;
6887 struct MeshTunnel *t;
6888 MESH_TunnelNumber tid;
6890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6891 /* Sanity check for client registration */
6892 if (NULL == (c = client_get (client)))
6895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6900 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6902 /* Sanity check for message size */
6903 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6906 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6910 /* Tunnel exists? */
6911 tid = ntohl (peer_msg->tunnel_id);
6912 t = tunnel_get_by_local_id (c, tid);
6916 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6921 /* Does client own tunnel? */
6922 if (t->owner->handle != client)
6925 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6930 GNUNET_i2s (&peer_msg->peer));
6931 /* Is the peer in the tunnel? */
6933 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6934 if (NULL == peer_info)
6937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6941 /* Ok, delete peer from tunnel */
6942 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6943 &peer_msg->peer.hashPubKey);
6945 send_destroy_path (t, peer_info->id);
6946 tunnel_delete_peer (t, peer_info->id);
6947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6952 * Handler for blacklist requests of peers in a tunnel
6954 * @param cls closure
6955 * @param client identification of the client
6956 * @param message the actual message (PeerControl)
6958 * FIXME implement DHT block bloomfilter
6961 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6962 const struct GNUNET_MessageHeader *message)
6964 struct GNUNET_MESH_PeerControl *peer_msg;
6965 struct MeshClient *c;
6966 struct MeshTunnel *t;
6967 MESH_TunnelNumber tid;
6969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6970 /* Sanity check for client registration */
6971 if (NULL == (c = client_get (client)))
6974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6979 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6981 /* Sanity check for message size */
6982 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6985 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6989 /* Tunnel exists? */
6990 tid = ntohl (peer_msg->tunnel_id);
6991 t = tunnel_get_by_local_id (c, tid);
6995 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7000 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7001 GNUNET_PEER_intern(&peer_msg->peer));
7006 * Handler for unblacklist requests of peers in a tunnel
7008 * @param cls closure
7009 * @param client identification of the client
7010 * @param message the actual message (PeerControl)
7013 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7014 const struct GNUNET_MessageHeader *message)
7016 struct GNUNET_MESH_PeerControl *peer_msg;
7017 struct MeshClient *c;
7018 struct MeshTunnel *t;
7019 MESH_TunnelNumber tid;
7023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7024 /* Sanity check for client registration */
7025 if (NULL == (c = client_get (client)))
7028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7033 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7035 /* Sanity check for message size */
7036 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7039 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7043 /* Tunnel exists? */
7044 tid = ntohl (peer_msg->tunnel_id);
7045 t = tunnel_get_by_local_id (c, tid);
7049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7054 /* if peer is not known, complain */
7055 pid = GNUNET_PEER_search (&peer_msg->peer);
7062 /* search and remove from list */
7063 for (i = 0; i < t->nblacklisted; i++)
7065 if (t->blacklisted[i] == pid)
7067 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7068 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7073 /* if peer hasn't been blacklisted, complain */
7079 * Handler for connection requests to new peers by type
7081 * @param cls closure
7082 * @param client identification of the client
7083 * @param message the actual message (ConnectPeerByType)
7086 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7087 const struct GNUNET_MessageHeader *message)
7089 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7090 struct MeshClient *c;
7091 struct MeshTunnel *t;
7092 struct GNUNET_HashCode hash;
7093 MESH_TunnelNumber tid;
7095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7096 /* Sanity check for client registration */
7097 if (NULL == (c = client_get (client)))
7100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7105 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7107 /* Sanity check for message size */
7108 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7109 ntohs (connect_msg->header.size))
7112 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7116 /* Tunnel exists? */
7117 tid = ntohl (connect_msg->tunnel_id);
7118 t = tunnel_get_by_local_id (c, tid);
7122 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7126 /* Does client own tunnel? */
7127 if (t->owner->handle != client)
7130 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7134 /* Do WE have the service? */
7135 t->type = ntohl (connect_msg->type);
7136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7137 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7138 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7141 /* Yes! Fast forward, add ourselves to the tunnel and send the
7142 * good news to the client, and alert the destination client of
7143 * an incoming tunnel.
7145 * FIXME send a path create to self, avoid code duplication
7147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7148 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7149 peer_info_get (&my_full_id),
7150 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7153 send_client_peer_connected (t, myid);
7154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7155 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7157 t->local_tid_dest = next_local_tid++;
7158 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7159 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7160 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7164 /* Ok, lets find a peer offering the service */
7165 if (NULL != t->dht_get_type)
7167 GNUNET_DHT_get_stop (t->dht_get_type);
7169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7170 GNUNET_h2s (&hash));
7172 GNUNET_DHT_get_start (dht_handle,
7173 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7175 dht_replication_level,
7176 GNUNET_DHT_RO_RECORD_ROUTE |
7177 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7179 &dht_get_type_handler, t);
7181 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7187 * Handler for connection requests to new peers by a string service description.
7189 * @param cls closure
7190 * @param client identification of the client
7191 * @param message the actual message, which includes messages the client wants
7194 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7195 const struct GNUNET_MessageHeader *message)
7197 struct GNUNET_MESH_ConnectPeerByString *msg;
7198 struct MeshRegexSearchInfo *info;
7199 struct MeshTunnel *t;
7200 struct MeshClient *c;
7201 MESH_TunnelNumber tid;
7206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7207 "Connect by string started\n");
7208 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7209 size = htons (message->size);
7211 /* Sanity check for client registration */
7212 if (NULL == (c = client_get (client)))
7215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7220 /* Message size sanity check */
7221 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7228 /* Tunnel exists? */
7229 tid = ntohl (msg->tunnel_id);
7230 t = tunnel_get_by_local_id (c, tid);
7234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7238 /* Does client own tunnel? */
7239 if (t->owner->handle != client)
7242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7247 " on tunnel %s [%u]\n",
7248 GNUNET_i2s(&my_full_id),
7251 /* Only one connect_by_string allowed at the same time! */
7252 /* FIXME: allow more, return handle at api level to cancel, document */
7253 if (NULL != t->regex_search)
7256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7260 /* Find string itself */
7261 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7262 string = (const char *) &msg[1];
7264 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7266 info->description = GNUNET_strndup (string, len);
7267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7269 t->regex_search = info;
7271 info->search_handle = GNUNET_REGEX_search (dht_handle,
7273 ®ex_found_handler, info,
7276 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7282 * Handler for client traffic directed to one peer
7284 * @param cls closure
7285 * @param client identification of the client
7286 * @param message the actual message
7289 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7290 const struct GNUNET_MessageHeader *message)
7292 struct MeshClient *c;
7293 struct MeshTunnel *t;
7294 struct MeshPeerInfo *pi;
7295 struct GNUNET_MESH_Unicast *data_msg;
7296 MESH_TunnelNumber tid;
7299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7300 "Got a unicast request from a client!\n");
7302 /* Sanity check for client registration */
7303 if (NULL == (c = client_get (client)))
7306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7311 data_msg = (struct GNUNET_MESH_Unicast *) message;
7313 /* Sanity check for message size */
7314 size = ntohs (message->size);
7315 if (sizeof (struct GNUNET_MESH_Unicast) +
7316 sizeof (struct GNUNET_MessageHeader) > size)
7319 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7323 /* Tunnel exists? */
7324 tid = ntohl (data_msg->tid);
7325 t = tunnel_get_by_local_id (c, tid);
7329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7333 /* Is it a local tunnel? Then, does client own the tunnel? */
7334 if (t->owner->handle != client)
7337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7341 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7342 &data_msg->destination.hashPubKey);
7343 /* Is the selected peer in the tunnel? */
7347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7351 /* PID should be as expected */
7352 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7356 "Unicast PID, expected %u, got %u\n",
7357 t->fwd_pid + 1, ntohl (data_msg->pid));
7358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7362 /* Ok, everything is correct, send the message
7363 * (pretend we got it from a mesh peer)
7366 /* Work around const limitation */
7367 char buf[ntohs (message->size)] GNUNET_ALIGN;
7368 struct GNUNET_MESH_Unicast *copy;
7370 copy = (struct GNUNET_MESH_Unicast *) buf;
7371 memcpy (buf, data_msg, size);
7372 copy->oid = my_full_id;
7373 copy->tid = htonl (t->id.tid);
7374 copy->ttl = htonl (default_ttl);
7375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7376 " calling generic handler...\n");
7377 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7380 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7387 * Handler for client traffic directed to the origin
7389 * @param cls closure
7390 * @param client identification of the client
7391 * @param message the actual message
7394 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7395 const struct GNUNET_MessageHeader *message)
7397 struct GNUNET_MESH_ToOrigin *data_msg;
7398 struct MeshTunnelClientInfo *clinfo;
7399 struct MeshClient *c;
7400 struct MeshTunnel *t;
7401 MESH_TunnelNumber tid;
7404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7405 "Got a ToOrigin request from a client!\n");
7406 /* Sanity check for client registration */
7407 if (NULL == (c = client_get (client)))
7410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7415 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7417 /* Sanity check for message size */
7418 size = ntohs (message->size);
7419 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7420 sizeof (struct GNUNET_MessageHeader) > size)
7423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7427 /* Tunnel exists? */
7428 tid = ntohl (data_msg->tid);
7429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7430 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7436 t = tunnel_get_by_local_id (c, tid);
7439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7440 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7446 /* It should be sent by someone who has this as incoming tunnel. */
7447 if (GNUNET_NO == client_knows_tunnel (c, t))
7450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7454 /* PID should be as expected */
7455 clinfo = tunnel_get_client_fc (t, c);
7456 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7459 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7460 "To Origin PID, expected %u, got %u\n",
7461 clinfo->bck_pid + 1,
7462 ntohl (data_msg->pid));
7463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7468 /* Ok, everything is correct, send the message
7469 * (pretend we got it from a mesh peer)
7472 char buf[ntohs (message->size)] GNUNET_ALIGN;
7473 struct GNUNET_MESH_ToOrigin *copy;
7475 /* Work around const limitation */
7476 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7477 memcpy (buf, data_msg, size);
7478 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7479 copy->tid = htonl (t->id.tid);
7480 copy->ttl = htonl (default_ttl);
7481 copy->pid = htonl (t->bck_pid + 1);
7483 copy->sender = my_full_id;
7484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7485 " calling generic handler...\n");
7486 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7488 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7495 * Handler for client traffic directed to all peers in a tunnel
7497 * @param cls closure
7498 * @param client identification of the client
7499 * @param message the actual message
7502 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7503 const struct GNUNET_MessageHeader *message)
7505 struct MeshClient *c;
7506 struct MeshTunnel *t;
7507 struct GNUNET_MESH_Multicast *data_msg;
7508 MESH_TunnelNumber tid;
7510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7511 "Got a multicast request from a client!\n");
7513 /* Sanity check for client registration */
7514 if (NULL == (c = client_get (client)))
7517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7522 data_msg = (struct GNUNET_MESH_Multicast *) message;
7524 /* Sanity check for message size */
7525 if (sizeof (struct GNUNET_MESH_Multicast) +
7526 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7533 /* Tunnel exists? */
7534 tid = ntohl (data_msg->tid);
7535 t = tunnel_get_by_local_id (c, tid);
7539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7540 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7546 /* Does client own tunnel? */
7547 if (t->owner->handle != client)
7550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7554 /* PID should be as expected */
7555 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7558 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7559 "Multicast PID, expected %u, got %u\n",
7560 t->fwd_pid + 1, ntohl (data_msg->pid));
7561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7566 char buf[ntohs (message->size)] GNUNET_ALIGN;
7567 struct GNUNET_MESH_Multicast *copy;
7569 copy = (struct GNUNET_MESH_Multicast *) buf;
7570 memcpy (buf, message, ntohs (message->size));
7571 copy->oid = my_full_id;
7572 copy->tid = htonl (t->id.tid);
7573 copy->ttl = htonl (default_ttl);
7574 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7576 " calling generic handler...\n");
7577 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7580 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7586 * Handler for client's ACKs for payload traffic.
7588 * @param cls Closure (unused).
7589 * @param client Identification of the client.
7590 * @param message The actual message.
7593 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7594 const struct GNUNET_MessageHeader *message)
7596 struct GNUNET_MESH_LocalAck *msg;
7597 struct MeshTunnel *t;
7598 struct MeshClient *c;
7599 MESH_TunnelNumber tid;
7602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7603 /* Sanity check for client registration */
7604 if (NULL == (c = client_get (client)))
7607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7612 msg = (struct GNUNET_MESH_LocalAck *) message;
7614 /* Tunnel exists? */
7615 tid = ntohl (msg->tunnel_id);
7616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7617 t = tunnel_get_by_local_id (c, tid);
7621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7622 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7627 ack = ntohl (msg->max_pid);
7628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7630 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
7631 if (NULL != t->owner && t->owner->handle == client)
7633 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7635 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7639 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7640 tunnel_set_client_fwd_ack (t, c, ack);
7641 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7644 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7651 * Iterator over all peers to send a monitoring client info about a tunnel.
7653 * @param cls Closure (message being built).
7654 * @param key Key (hashed tunnel ID, unused).
7655 * @param value Peer info.
7657 * @return GNUNET_YES, to keep iterating.
7660 monitor_peers_iterator (void *cls,
7661 const struct GNUNET_HashCode * key,
7664 struct GNUNET_MESH_LocalMonitor *msg = cls;
7665 struct GNUNET_PeerIdentity *id;
7666 struct MeshPeerInfo *info = value;
7668 id = (struct GNUNET_PeerIdentity *) &msg[1];
7669 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
7672 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7673 "* sending info about peer %s [%u]\n",
7674 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
7682 * Iterator over all tunnels to send a monitoring client info about each tunnel.
7684 * @param cls Closure (client handle).
7685 * @param key Key (hashed tunnel ID, unused).
7686 * @param value Tunnel info.
7688 * @return GNUNET_YES, to keep iterating.
7691 monitor_all_tunnels_iterator (void *cls,
7692 const struct GNUNET_HashCode * key,
7695 struct GNUNET_SERVER_Client *client = cls;
7696 struct MeshTunnel *t = value;
7697 struct GNUNET_MESH_LocalMonitor *msg;
7700 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
7701 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
7702 npeers * sizeof (struct GNUNET_PeerIdentity));
7703 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
7704 msg->tunnel_id = htonl (t->id.tid);
7705 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
7706 npeers * sizeof (struct GNUNET_PeerIdentity));
7707 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
7709 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
7710 monitor_peers_iterator,
7713 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7714 "* sending info about tunnel %s [%u] (%u peers)\n",
7715 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
7717 if (msg->npeers != npeers)
7720 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7721 "Get tunnels fail: size %u - iter %u\n",
7722 npeers, msg->npeers);
7725 msg->npeers = htonl (npeers);
7726 GNUNET_SERVER_notification_context_unicast (nc, client,
7727 &msg->header, GNUNET_NO);
7733 * Handler for client's MONITOR request.
7735 * @param cls Closure (unused).
7736 * @param client Identification of the client.
7737 * @param message The actual message.
7740 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
7741 const struct GNUNET_MessageHeader *message)
7743 struct MeshClient *c;
7745 /* Sanity check for client registration */
7746 if (NULL == (c = client_get (client)))
7749 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7753 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7754 "Received get tunnels request from client %u\n",
7756 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
7757 monitor_all_tunnels_iterator,
7759 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7760 "Get tunnels request from client %u completed\n",
7762 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7767 * Data needed to build a Monitor_Tunnel message.
7769 struct MeshMonitorTunnelContext
7772 * Partial message, including peer count.
7774 struct GNUNET_MESH_LocalMonitor *msg;
7777 * Hashmap with positions: peer->position.
7779 struct GNUNET_CONTAINER_MultiHashMap *lookup;
7782 * Index of the parent of each peer in the message, realtive to the absolute
7783 * order in the array (can be in a previous message).
7785 uint32_t parents[1024];
7788 * Peers visited so far in the tree, aka position of the current peer.
7790 unsigned int npeers;
7793 * Client requesting the info.
7795 struct MeshClient *c;
7800 * Send a client a message about the structure of a tunnel.
7802 * @param ctx Context of the tunnel iteration, with info regarding the state
7803 * of the execution and the number of peers visited for this message.
7806 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
7808 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
7809 struct GNUNET_PeerIdentity *pid;
7810 unsigned int *parent;
7813 size = sizeof (struct GNUNET_MESH_LocalMonitor);
7814 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
7815 resp->header.size = htons (size);
7816 pid = (struct GNUNET_PeerIdentity *) &resp[1];
7817 parent = (unsigned int *) &pid[resp->npeers];
7818 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
7819 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
7820 &resp->header, GNUNET_NO);
7824 * Iterator over a tunnel tree to build a message containing all peers
7825 * the in the tunnel, including relay nodes.
7827 * @param cls Closure (pointer to pointer of message being built).
7828 * @param peer Short ID of a peer.
7829 * @param parent Short ID of the @c peer 's parent.
7832 tunnel_tree_iterator (void *cls,
7833 GNUNET_PEER_Id peer,
7834 GNUNET_PEER_Id parent)
7836 struct MeshMonitorTunnelContext *ctx = cls;
7837 struct GNUNET_MESH_LocalMonitor *msg;
7838 struct GNUNET_PeerIdentity *pid;
7839 struct GNUNET_PeerIdentity ppid;
7842 pid = (struct GNUNET_PeerIdentity *) &msg[1];
7843 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
7844 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
7845 &pid[msg->npeers].hashPubKey,
7846 (void *) (long) ctx->npeers,
7847 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7848 GNUNET_PEER_resolve (parent, &ppid);
7849 ctx->parents[msg->npeers] =
7850 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
7856 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
7858 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
7861 send_client_tunnel_info (ctx);
7868 * Handler for client's MONITOR_TUNNEL request.
7870 * @param cls Closure (unused).
7871 * @param client Identification of the client.
7872 * @param message The actual message.
7875 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
7876 const struct GNUNET_MessageHeader *message)
7878 const struct GNUNET_MESH_LocalMonitor *msg;
7879 struct GNUNET_MESH_LocalMonitor *resp;
7880 struct MeshMonitorTunnelContext ctx;
7881 struct MeshClient *c;
7882 struct MeshTunnel *t;
7884 /* Sanity check for client registration */
7885 if (NULL == (c = client_get (client)))
7888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7892 msg = (struct GNUNET_MESH_LocalMonitor *) message;
7893 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7894 "Received tunnel info request from client %u for tunnel %s[%X]\n",
7897 ntohl (msg->tunnel_id));
7898 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
7901 /* We don't know the tunnel */
7902 struct GNUNET_MESH_LocalMonitor warn;
7905 warn.npeers = htonl (UINT_MAX);
7906 GNUNET_SERVER_notification_context_unicast (nc, client,
7909 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7913 /* Initialize context */
7914 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
7918 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
7922 /* Collect and send information */
7923 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
7924 send_client_tunnel_info (&ctx);
7927 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
7930 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7931 "Monitor tunnel request from client %u completed\n",
7933 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7938 * Functions to handle messages from clients
7940 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7941 {&handle_local_new_client, NULL,
7942 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7943 {&handle_local_announce_regex, NULL,
7944 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7945 {&handle_local_tunnel_create, NULL,
7946 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7947 sizeof (struct GNUNET_MESH_TunnelMessage)},
7948 {&handle_local_tunnel_destroy, NULL,
7949 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7950 sizeof (struct GNUNET_MESH_TunnelMessage)},
7951 {&handle_local_tunnel_speed, NULL,
7952 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7953 sizeof (struct GNUNET_MESH_TunnelMessage)},
7954 {&handle_local_tunnel_speed, NULL,
7955 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7956 sizeof (struct GNUNET_MESH_TunnelMessage)},
7957 {&handle_local_tunnel_buffer, NULL,
7958 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7959 sizeof (struct GNUNET_MESH_TunnelMessage)},
7960 {&handle_local_tunnel_buffer, NULL,
7961 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7962 sizeof (struct GNUNET_MESH_TunnelMessage)},
7963 {&handle_local_connect_add, NULL,
7964 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7965 sizeof (struct GNUNET_MESH_PeerControl)},
7966 {&handle_local_connect_del, NULL,
7967 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7968 sizeof (struct GNUNET_MESH_PeerControl)},
7969 {&handle_local_blacklist, NULL,
7970 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7971 sizeof (struct GNUNET_MESH_PeerControl)},
7972 {&handle_local_unblacklist, NULL,
7973 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7974 sizeof (struct GNUNET_MESH_PeerControl)},
7975 {&handle_local_connect_by_type, NULL,
7976 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7977 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7978 {&handle_local_connect_by_string, NULL,
7979 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7980 {&handle_local_unicast, NULL,
7981 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7982 {&handle_local_to_origin, NULL,
7983 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7984 {&handle_local_multicast, NULL,
7985 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7986 {&handle_local_ack, NULL,
7987 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7988 sizeof (struct GNUNET_MESH_LocalAck)},
7989 {&handle_local_get_tunnels, NULL,
7990 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
7991 sizeof (struct GNUNET_MessageHeader)},
7992 {&handle_local_show_tunnel, NULL,
7993 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
7994 sizeof (struct GNUNET_MESH_LocalMonitor)},
8000 * To be called on core init/fail.
8002 * @param cls service closure
8003 * @param server handle to the server for this service
8004 * @param identity the public identity of this peer
8007 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8008 const struct GNUNET_PeerIdentity *identity)
8011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8012 core_handle = server;
8013 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8017 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8019 GNUNET_i2s (identity));
8020 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8022 GNUNET_i2s (&my_full_id));
8023 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8025 GNUNET_abort(); // Try harder
8032 * Method called whenever a given peer connects.
8034 * @param cls closure
8035 * @param peer peer identity this notification is about
8036 * @param atsi performance data for the connection
8037 * @param atsi_count number of records in 'atsi'
8040 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8041 const struct GNUNET_ATS_Information *atsi,
8042 unsigned int atsi_count)
8044 struct MeshPeerInfo *peer_info;
8045 struct MeshPeerPath *path;
8047 DEBUG_CONN ("Peer connected\n");
8048 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8049 peer_info = peer_info_get (peer);
8050 if (myid == peer_info->id)
8052 DEBUG_CONN (" (self)\n");
8057 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8059 path = path_new (2);
8060 path->peers[0] = myid;
8061 path->peers[1] = peer_info->id;
8062 GNUNET_PEER_change_rc (myid, 1);
8063 GNUNET_PEER_change_rc (peer_info->id, 1);
8064 peer_info_add_path (peer_info, path, GNUNET_YES);
8065 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8071 * Method called whenever a peer disconnects.
8073 * @param cls closure
8074 * @param peer peer identity this notification is about
8077 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8079 struct MeshPeerInfo *pi;
8080 struct MeshPeerQueue *q;
8081 struct MeshPeerQueue *n;
8083 DEBUG_CONN ("Peer disconnected\n");
8084 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8094 /* TODO try to reroute this traffic instead */
8095 queue_destroy(q, GNUNET_YES);
8098 if (NULL != pi->core_transmit)
8100 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8101 pi->core_transmit = NULL;
8103 peer_info_remove_path (pi, pi->id, myid);
8106 DEBUG_CONN (" (self)\n");
8108 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8113 /******************************************************************************/
8114 /************************ MAIN FUNCTIONS ****************************/
8115 /******************************************************************************/
8118 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8120 * @param cls closure
8121 * @param key current key code
8122 * @param value value in the hash map
8123 * @return GNUNET_YES if we should continue to iterate,
8127 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8129 struct MeshTunnel *t = value;
8136 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8138 * @param cls closure
8139 * @param key current key code
8140 * @param value value in the hash map
8141 * @return GNUNET_YES if we should continue to iterate,
8145 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8147 struct MeshPeerInfo *p = value;
8148 struct MeshPeerQueue *q;
8149 struct MeshPeerQueue *n;
8157 queue_destroy(q, GNUNET_YES);
8161 peer_info_destroy (p);
8167 * Task run during shutdown.
8173 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8177 if (core_handle != NULL)
8179 GNUNET_CORE_disconnect (core_handle);
8184 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
8187 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8188 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8189 if (dht_handle != NULL)
8191 GNUNET_DHT_disconnect (dht_handle);
8196 GNUNET_SERVER_notification_context_destroy (nc);
8199 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8201 GNUNET_SCHEDULER_cancel (announce_id_task);
8202 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8204 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8206 GNUNET_SCHEDULER_cancel (announce_applications_task);
8207 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8214 * Callback for hostkey read/generation.
8216 * @param cls Closure (Configuration handle).
8217 * @param pk The ECC private key.
8218 * @param emsg Error message, if any.
8221 key_generation_cb (void *cls,
8222 struct GNUNET_CRYPTO_EccPrivateKey *pk,
8225 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8226 struct MeshPeerInfo *peer;
8227 struct MeshPeerPath *p;
8232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8233 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8235 GNUNET_SCHEDULER_shutdown ();
8238 my_private_key = pk;
8239 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
8240 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8241 &my_full_id.hashPubKey);
8242 myid = GNUNET_PEER_intern (&my_full_id);
8243 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8244 "Mesh for peer [%s] starting\n",
8245 GNUNET_i2s(&my_full_id));
8247 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8248 NULL, /* Closure passed to MESH functions */
8249 &core_init, /* Call core_init once connected */
8250 &core_connect, /* Handle connects */
8251 &core_disconnect, /* remove peers on disconnects */
8252 NULL, /* Don't notify about all incoming messages */
8253 GNUNET_NO, /* For header only in notification */
8254 NULL, /* Don't notify about all outbound messages */
8255 GNUNET_NO, /* For header-only out notification */
8256 core_handlers); /* Register these handlers */
8258 if (core_handle == NULL)
8261 GNUNET_SCHEDULER_shutdown ();
8266 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8269 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8270 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8271 GNUNET_SERVER_disconnect_notify (server_handle,
8272 &handle_local_client_disconnect, NULL);
8276 clients_tail = NULL;
8279 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8280 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8282 /* Create a peer_info for the local peer */
8283 peer = peer_info_get (&my_full_id);
8286 GNUNET_PEER_change_rc (myid, 1);
8287 peer_info_add_path (peer, p, GNUNET_YES);
8288 GNUNET_SERVER_resume (server_handle);
8289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8294 * Process mesh requests.
8296 * @param cls closure
8297 * @param server the initialized server
8298 * @param c configuration to use
8301 run (void *cls, struct GNUNET_SERVER_Handle *server,
8302 const struct GNUNET_CONFIGURATION_Handle *c)
8306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8307 server_handle = server;
8310 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
8313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8315 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8316 "mesh", "peer/privatekey");
8317 GNUNET_SCHEDULER_shutdown ();
8322 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8323 &refresh_path_time))
8325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8327 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8328 "mesh", "refresh path time");
8329 GNUNET_SCHEDULER_shutdown ();
8334 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8335 &app_announce_time))
8337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8339 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8340 "mesh", "app announce time");
8341 GNUNET_SCHEDULER_shutdown ();
8344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8345 "APP_ANNOUNCE_TIME %llu ms\n",
8346 app_announce_time.rel_value);
8348 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8353 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8354 "mesh", "id announce time");
8355 GNUNET_SCHEDULER_shutdown ();
8360 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8365 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8366 "mesh", "connect timeout");
8367 GNUNET_SCHEDULER_shutdown ();
8372 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8377 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8378 "mesh", "max msgs queue");
8379 GNUNET_SCHEDULER_shutdown ();
8384 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8389 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8390 "mesh", "max tunnels");
8391 GNUNET_SCHEDULER_shutdown ();
8396 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8401 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8402 "mesh", "default ttl", 64);
8407 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8410 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8411 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8412 "mesh", "max peers", 1000);
8417 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8418 &dht_replication_level))
8420 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8422 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8423 "mesh", "dht replication level", 3);
8424 dht_replication_level = 3;
8427 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8428 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8429 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8430 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8431 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8433 dht_handle = GNUNET_DHT_connect (c, 64);
8434 if (NULL == dht_handle)
8438 stats = GNUNET_STATISTICS_create ("mesh", c);
8440 GNUNET_SERVER_suspend (server_handle);
8441 /* Scheduled the task to clean up when shutdown is called */
8442 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8444 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
8447 GNUNET_free (keyfile);
8452 * The main function for the mesh service.
8454 * @param argc number of arguments from the command line
8455 * @param argv command line arguments
8456 * @return 0 ok, 1 on error
8459 main (int argc, char *const *argv)
8464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8465 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8467 ret = (GNUNET_OK == r) ? 0 : 1;
8468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8472 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8473 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8474 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);