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
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_RsaKeyGenerationContext *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_RsaPrivateKey *my_private_key;
987 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded 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.
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 GNUNET_CONTAINER_MultiHashMap *h = cls;
6098 GNUNET_break (GNUNET_YES ==
6099 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6105 * notify_client_connection_failure: notify a client that the connection to the
6106 * requested remote peer is not possible (for instance, no route found)
6107 * Function called when the socket is ready to queue more data. "buf" will be
6108 * NULL and "size" zero if the socket was closed for writing in the meantime.
6110 * @param cls closure
6111 * @param size number of bytes available in buf
6112 * @param buf where the callee should write the message
6113 * @return number of bytes written to buf
6116 notify_client_connection_failure (void *cls, size_t size, void *buf)
6119 struct MeshPeerInfo *peer_info;
6120 struct GNUNET_MESH_PeerControl *msg;
6121 struct GNUNET_PeerIdentity id;
6123 if (0 == size && NULL == buf)
6125 // TODO retry? cancel?
6129 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6130 peer_info = (struct MeshPeerInfo *) cls;
6131 msg = (struct GNUNET_MESH_PeerControl *) buf;
6132 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6133 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6134 // msg->tunnel_id = htonl(peer_info->t->tid);
6135 GNUNET_PEER_resolve (peer_info->id, &id);
6136 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6144 * Send keepalive packets for a peer
6146 * @param cls Closure (tunnel for which to send the keepalive).
6147 * @param tc Notification context.
6150 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6152 struct MeshTunnel *t = cls;
6153 struct GNUNET_MESH_TunnelKeepAlive *msg;
6154 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6157 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6158 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6164 "sending keepalive for tunnel %d\n", t->id.tid);
6166 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6167 msg->header.size = htons (size);
6168 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6169 msg->oid = my_full_id;
6170 msg->tid = htonl (t->id.tid);
6171 tunnel_send_multicast (t, &msg->header);
6173 t->path_refresh_task =
6174 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6175 tunnel_reset_timeout(t);
6180 * Function to process paths received for a new peer addition. The recorded
6181 * paths form the initial tunnel, which can be optimized later.
6182 * Called on each result obtained for the DHT search.
6184 * @param cls closure
6185 * @param exp when will this value expire
6186 * @param key key of the result
6187 * @param get_path path of the get request
6188 * @param get_path_length lenght of get_path
6189 * @param put_path path of the put request
6190 * @param put_path_length length of the put_path
6191 * @param type type of the result
6192 * @param size number of bytes in data
6193 * @param data pointer to the result data
6195 * TODO: re-issue the request after certain time? cancel after X results?
6198 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6199 const struct GNUNET_HashCode * key,
6200 const struct GNUNET_PeerIdentity *get_path,
6201 unsigned int get_path_length,
6202 const struct GNUNET_PeerIdentity *put_path,
6203 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6204 size_t size, const void *data)
6206 struct MeshPathInfo *path_info = cls;
6207 struct MeshPeerPath *p;
6208 struct GNUNET_PeerIdentity pi;
6211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6212 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6215 p = path_build_from_dht (get_path, get_path_length, put_path,
6217 path_add_to_peers (p, GNUNET_NO);
6219 for (i = 0; i < path_info->peer->ntunnels; i++)
6221 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6222 peer_info_connect (path_info->peer, path_info->t);
6230 * Function to process paths received for a new peer addition. The recorded
6231 * paths form the initial tunnel, which can be optimized later.
6232 * Called on each result obtained for the DHT search.
6234 * @param cls closure
6235 * @param exp when will this value expire
6236 * @param key key of the result
6237 * @param get_path path of the get request
6238 * @param get_path_length lenght of get_path
6239 * @param put_path path of the put request
6240 * @param put_path_length length of the put_path
6241 * @param type type of the result
6242 * @param size number of bytes in data
6243 * @param data pointer to the result data
6246 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6247 const struct GNUNET_HashCode * key,
6248 const struct GNUNET_PeerIdentity *get_path,
6249 unsigned int get_path_length,
6250 const struct GNUNET_PeerIdentity *put_path,
6251 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6252 size_t size, const void *data)
6254 const struct PBlock *pb = data;
6255 const struct GNUNET_PeerIdentity *pi = &pb->id;
6256 struct MeshTunnel *t = cls;
6257 struct MeshPeerInfo *peer_info;
6258 struct MeshPeerPath *p;
6260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6261 if (size != sizeof (struct PBlock))
6263 GNUNET_break_op (0);
6266 if (ntohl(pb->type) != t->type)
6268 GNUNET_break_op (0);
6271 GNUNET_assert (NULL != t->owner);
6272 peer_info = peer_info_get (pi);
6273 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6275 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6277 p = path_build_from_dht (get_path, get_path_length, put_path,
6279 path_add_to_peers (p, GNUNET_NO);
6281 tunnel_add_peer (t, peer_info);
6282 peer_info_connect (peer_info, t);
6286 /******************************************************************************/
6287 /********************* MESH LOCAL HANDLES **************************/
6288 /******************************************************************************/
6292 * Handler for client disconnection
6294 * @param cls closure
6295 * @param client identification of the client; NULL
6296 * for the last call when the server is destroyed
6299 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6301 struct MeshClient *c;
6302 struct MeshClient *next;
6305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6315 if (c->handle != client)
6317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6323 GNUNET_SERVER_client_drop (c->handle);
6324 c->shutting_down = GNUNET_YES;
6325 GNUNET_assert (NULL != c->own_tunnels);
6326 GNUNET_assert (NULL != c->incoming_tunnels);
6327 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6328 &tunnel_destroy_iterator, c);
6329 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6330 &tunnel_destroy_iterator, c);
6331 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6332 &tunnel_destroy_iterator, c);
6333 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6334 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6335 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6337 /* deregister clients applications */
6338 if (NULL != c->apps)
6340 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6341 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6343 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6344 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6346 GNUNET_SCHEDULER_cancel (announce_applications_task);
6347 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6349 if (NULL != c->types)
6350 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6351 for (i = 0; i < c->n_regex; i++)
6353 GNUNET_free (c->regexes[i].regex);
6354 if (NULL != c->regexes[i].h)
6355 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
6357 GNUNET_free_non_null (c->regexes);
6358 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6359 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6361 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6364 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6373 * Handler for new clients
6375 * @param cls closure
6376 * @param client identification of the client
6377 * @param message the actual message, which includes messages the client wants
6380 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6381 const struct GNUNET_MessageHeader *message)
6383 struct GNUNET_MESH_ClientConnect *cc_msg;
6384 struct MeshClient *c;
6385 GNUNET_MESH_ApplicationType *a;
6392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6393 /* Check data sanity */
6394 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6395 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6396 ntypes = ntohs (cc_msg->types);
6397 napps = ntohs (cc_msg->applications);
6399 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6406 /* Create new client structure */
6407 c = GNUNET_malloc (sizeof (struct MeshClient));
6408 c->id = next_client_id++;
6409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6411 GNUNET_SERVER_client_keep (client);
6412 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6415 GNUNET_MESH_ApplicationType at;
6416 struct GNUNET_HashCode hc;
6418 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6419 for (i = 0; i < napps; i++)
6422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6423 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6424 /* store in clients hashmap */
6425 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6426 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6427 /* store in global hashmap, for announcements */
6428 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6429 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6431 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6432 announce_applications_task =
6433 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6439 struct GNUNET_HashCode hc;
6441 t = (uint16_t *) & a[napps];
6442 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6443 for (i = 0; i < ntypes; i++)
6446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6447 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6449 /* store in clients hashmap */
6450 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6451 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6452 /* store in global hashmap */
6453 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6454 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6458 " client has %u+%u subscriptions\n", napps, ntypes);
6460 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6461 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6462 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6463 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6464 GNUNET_SERVER_notification_context_add (nc, client);
6465 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6467 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6473 * Handler for clients announcing available services by a regular expression.
6475 * @param cls closure
6476 * @param client identification of the client
6477 * @param message the actual message, which includes messages the client wants
6480 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6481 const struct GNUNET_MessageHeader *message)
6483 const struct GNUNET_MESH_RegexAnnounce *msg;
6484 struct MeshRegexDescriptor rd;
6485 struct MeshClient *c;
6490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6492 /* Sanity check for client registration */
6493 if (NULL == (c = client_get (client)))
6496 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6501 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6503 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6504 if (NULL != c->partial_regex)
6506 regex = c->partial_regex;
6507 offset = strlen (c->partial_regex);
6508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6509 " continuation, already have %u bytes\n",
6518 regex = GNUNET_realloc (regex, offset + len + 1);
6519 memcpy (®ex[offset], &msg[1], len);
6520 regex[offset + len] = '\0';
6521 if (0 == ntohs (msg->last))
6523 c->partial_regex = regex;
6524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6525 " not ended, stored %u bytes for later\n",
6527 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6531 rd.compression = ntohs (msg->compression_characters);
6533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " compr %u\n", ntohs (rd.compression));
6536 GNUNET_array_append (c->regexes, c->n_regex, rd);
6537 c->partial_regex = NULL;
6538 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6540 c->regex_announce_task = GNUNET_SCHEDULER_add_now (®ex_announce, c);
6546 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6552 * Handler for requests of new tunnels
6554 * @param cls closure
6555 * @param client identification of the client
6556 * @param message the actual message
6559 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6560 const struct GNUNET_MessageHeader *message)
6562 struct GNUNET_MESH_TunnelMessage *t_msg;
6563 struct MeshTunnel *t;
6564 struct MeshClient *c;
6565 MESH_TunnelNumber tid;
6567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6569 /* Sanity check for client registration */
6570 if (NULL == (c = client_get (client)))
6573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6578 /* Message sanity check */
6579 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6586 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6587 /* Sanity check for tunnel numbering */
6588 tid = ntohl (t_msg->tunnel_id);
6589 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6595 /* Sanity check for duplicate tunnel IDs */
6596 if (NULL != tunnel_get_by_local_id (c, tid))
6599 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6603 while (NULL != tunnel_get_by_pi (myid, next_tid))
6604 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6605 t = tunnel_new (myid, next_tid++, c, tid);
6608 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6610 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6613 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6615 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6616 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6619 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6625 * Handler for requests of deleting tunnels
6627 * @param cls closure
6628 * @param client identification of the client
6629 * @param message the actual message
6632 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6633 const struct GNUNET_MessageHeader *message)
6635 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6636 struct MeshClient *c;
6637 struct MeshTunnel *t;
6638 MESH_TunnelNumber tid;
6640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6641 "Got a DESTROY TUNNEL from client!\n");
6643 /* Sanity check for client registration */
6644 if (NULL == (c = client_get (client)))
6647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6652 /* Message sanity check */
6653 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6660 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6662 /* Retrieve tunnel */
6663 tid = ntohl (tunnel_msg->tunnel_id);
6664 t = tunnel_get_by_local_id(c, tid);
6667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6669 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6672 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6674 client_ignore_tunnel (c, t);
6675 tunnel_destroy_empty (t);
6676 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6679 send_client_tunnel_disconnect (t, c);
6680 client_delete_tunnel (c, t);
6682 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6684 tunnel_send_destroy (t, 0);
6685 t->destroy = GNUNET_YES;
6686 /* The tunnel will be destroyed when the last message is transmitted. */
6687 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6693 * Handler for requests of seeting tunnel's speed.
6695 * @param cls Closure (unused).
6696 * @param client Identification of the client.
6697 * @param message The actual message.
6700 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6701 const struct GNUNET_MessageHeader *message)
6703 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6704 struct MeshClient *c;
6705 struct MeshTunnel *t;
6706 MESH_TunnelNumber tid;
6708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6709 "Got a SPEED request from client!\n");
6711 /* Sanity check for client registration */
6712 if (NULL == (c = client_get (client)))
6715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6721 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6723 /* Retrieve tunnel */
6724 tid = ntohl (tunnel_msg->tunnel_id);
6725 t = tunnel_get_by_local_id(c, tid);
6728 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6734 switch (ntohs(message->type))
6736 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6737 t->speed_min = GNUNET_YES;
6739 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6740 t->speed_min = GNUNET_NO;
6745 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6750 * Handler for requests of seeting tunnel's buffering policy.
6752 * @param cls Closure (unused).
6753 * @param client Identification of the client.
6754 * @param message The actual message.
6757 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6758 const struct GNUNET_MessageHeader *message)
6760 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6761 struct MeshClient *c;
6762 struct MeshTunnel *t;
6763 MESH_TunnelNumber tid;
6765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6766 "Got a BUFFER request from client!\n");
6768 /* Sanity check for client registration */
6769 if (NULL == (c = client_get (client)))
6772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6777 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6779 /* Retrieve tunnel */
6780 tid = ntohl (tunnel_msg->tunnel_id);
6781 t = tunnel_get_by_local_id(c, tid);
6784 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6790 switch (ntohs(message->type))
6792 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6793 t->nobuffer = GNUNET_NO;
6795 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6796 t->nobuffer = GNUNET_YES;
6802 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6807 * Handler for connection requests to new peers
6809 * @param cls closure
6810 * @param client identification of the client
6811 * @param message the actual message (PeerControl)
6814 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6815 const struct GNUNET_MessageHeader *message)
6817 struct GNUNET_MESH_PeerControl *peer_msg;
6818 struct MeshPeerInfo *peer_info;
6819 struct MeshClient *c;
6820 struct MeshTunnel *t;
6821 MESH_TunnelNumber tid;
6823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6824 /* Sanity check for client registration */
6825 if (NULL == (c = client_get (client)))
6828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6833 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6835 /* Sanity check for message size */
6836 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6843 /* Tunnel exists? */
6844 tid = ntohl (peer_msg->tunnel_id);
6845 t = tunnel_get_by_local_id (c, tid);
6849 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6853 /* Does client own tunnel? */
6854 if (t->owner->handle != client)
6857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6861 GNUNET_i2s (&peer_msg->peer));
6862 peer_info = peer_info_get (&peer_msg->peer);
6864 tunnel_add_peer (t, peer_info);
6865 peer_info_connect (peer_info, t);
6867 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6873 * Handler for disconnection requests of peers in a tunnel
6875 * @param cls closure
6876 * @param client identification of the client
6877 * @param message the actual message (PeerControl)
6880 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6881 const struct GNUNET_MessageHeader *message)
6883 struct GNUNET_MESH_PeerControl *peer_msg;
6884 struct MeshPeerInfo *peer_info;
6885 struct MeshClient *c;
6886 struct MeshTunnel *t;
6887 MESH_TunnelNumber tid;
6889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6890 /* Sanity check for client registration */
6891 if (NULL == (c = client_get (client)))
6894 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6899 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6901 /* Sanity check for message size */
6902 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6909 /* Tunnel exists? */
6910 tid = ntohl (peer_msg->tunnel_id);
6911 t = tunnel_get_by_local_id (c, tid);
6915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6920 /* Does client own tunnel? */
6921 if (t->owner->handle != client)
6924 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6929 GNUNET_i2s (&peer_msg->peer));
6930 /* Is the peer in the tunnel? */
6932 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6933 if (NULL == peer_info)
6936 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6940 /* Ok, delete peer from tunnel */
6941 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6942 &peer_msg->peer.hashPubKey);
6944 send_destroy_path (t, peer_info->id);
6945 tunnel_delete_peer (t, peer_info->id);
6946 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6951 * Handler for blacklist requests of peers in a tunnel
6953 * @param cls closure
6954 * @param client identification of the client
6955 * @param message the actual message (PeerControl)
6957 * FIXME implement DHT block bloomfilter
6960 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6961 const struct GNUNET_MessageHeader *message)
6963 struct GNUNET_MESH_PeerControl *peer_msg;
6964 struct MeshClient *c;
6965 struct MeshTunnel *t;
6966 MESH_TunnelNumber tid;
6968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6969 /* Sanity check for client registration */
6970 if (NULL == (c = client_get (client)))
6973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6978 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6980 /* Sanity check for message size */
6981 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6988 /* Tunnel exists? */
6989 tid = ntohl (peer_msg->tunnel_id);
6990 t = tunnel_get_by_local_id (c, tid);
6994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6999 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7000 GNUNET_PEER_intern(&peer_msg->peer));
7005 * Handler for unblacklist requests of peers in a tunnel
7007 * @param cls closure
7008 * @param client identification of the client
7009 * @param message the actual message (PeerControl)
7012 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7013 const struct GNUNET_MessageHeader *message)
7015 struct GNUNET_MESH_PeerControl *peer_msg;
7016 struct MeshClient *c;
7017 struct MeshTunnel *t;
7018 MESH_TunnelNumber tid;
7022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7023 /* Sanity check for client registration */
7024 if (NULL == (c = client_get (client)))
7027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7032 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7034 /* Sanity check for message size */
7035 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7042 /* Tunnel exists? */
7043 tid = ntohl (peer_msg->tunnel_id);
7044 t = tunnel_get_by_local_id (c, tid);
7048 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7053 /* if peer is not known, complain */
7054 pid = GNUNET_PEER_search (&peer_msg->peer);
7061 /* search and remove from list */
7062 for (i = 0; i < t->nblacklisted; i++)
7064 if (t->blacklisted[i] == pid)
7066 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7067 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7072 /* if peer hasn't been blacklisted, complain */
7078 * Handler for connection requests to new peers by type
7080 * @param cls closure
7081 * @param client identification of the client
7082 * @param message the actual message (ConnectPeerByType)
7085 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7086 const struct GNUNET_MessageHeader *message)
7088 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7089 struct MeshClient *c;
7090 struct MeshTunnel *t;
7091 struct GNUNET_HashCode hash;
7092 MESH_TunnelNumber tid;
7094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7095 /* Sanity check for client registration */
7096 if (NULL == (c = client_get (client)))
7099 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7104 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7106 /* Sanity check for message size */
7107 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7108 ntohs (connect_msg->header.size))
7111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7115 /* Tunnel exists? */
7116 tid = ntohl (connect_msg->tunnel_id);
7117 t = tunnel_get_by_local_id (c, tid);
7121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7125 /* Does client own tunnel? */
7126 if (t->owner->handle != client)
7129 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7133 /* Do WE have the service? */
7134 t->type = ntohl (connect_msg->type);
7135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7136 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7137 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7140 /* Yes! Fast forward, add ourselves to the tunnel and send the
7141 * good news to the client, and alert the destination client of
7142 * an incoming tunnel.
7144 * FIXME send a path create to self, avoid code duplication
7146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7147 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7148 peer_info_get (&my_full_id),
7149 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7152 send_client_peer_connected (t, myid);
7153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7154 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7156 t->local_tid_dest = next_local_tid++;
7157 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7158 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7159 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7163 /* Ok, lets find a peer offering the service */
7164 if (NULL != t->dht_get_type)
7166 GNUNET_DHT_get_stop (t->dht_get_type);
7168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7169 GNUNET_h2s (&hash));
7171 GNUNET_DHT_get_start (dht_handle,
7172 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7174 dht_replication_level,
7175 GNUNET_DHT_RO_RECORD_ROUTE |
7176 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7178 &dht_get_type_handler, t);
7180 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7186 * Handler for connection requests to new peers by a string service description.
7188 * @param cls closure
7189 * @param client identification of the client
7190 * @param message the actual message, which includes messages the client wants
7193 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7194 const struct GNUNET_MessageHeader *message)
7196 struct GNUNET_MESH_ConnectPeerByString *msg;
7197 struct MeshRegexSearchInfo *info;
7198 struct MeshTunnel *t;
7199 struct MeshClient *c;
7200 MESH_TunnelNumber tid;
7205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7206 "Connect by string started\n");
7207 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7208 size = htons (message->size);
7210 /* Sanity check for client registration */
7211 if (NULL == (c = client_get (client)))
7214 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7219 /* Message size sanity check */
7220 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7227 /* Tunnel exists? */
7228 tid = ntohl (msg->tunnel_id);
7229 t = tunnel_get_by_local_id (c, tid);
7233 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7237 /* Does client own tunnel? */
7238 if (t->owner->handle != client)
7241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7246 " on tunnel %s [%u]\n",
7247 GNUNET_i2s(&my_full_id),
7250 /* Only one connect_by_string allowed at the same time! */
7251 /* FIXME: allow more, return handle at api level to cancel, document */
7252 if (NULL != t->regex_search)
7255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7259 /* Find string itself */
7260 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7261 string = (const char *) &msg[1];
7263 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7265 info->description = GNUNET_strndup (string, len);
7266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7268 t->regex_search = info;
7270 info->search_handle = GNUNET_REGEX_search (dht_handle,
7272 ®ex_found_handler, info,
7275 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7281 * Handler for client traffic directed to one peer
7283 * @param cls closure
7284 * @param client identification of the client
7285 * @param message the actual message
7288 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7289 const struct GNUNET_MessageHeader *message)
7291 struct MeshClient *c;
7292 struct MeshTunnel *t;
7293 struct MeshPeerInfo *pi;
7294 struct GNUNET_MESH_Unicast *data_msg;
7295 MESH_TunnelNumber tid;
7298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7299 "Got a unicast request from a client!\n");
7301 /* Sanity check for client registration */
7302 if (NULL == (c = client_get (client)))
7305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7310 data_msg = (struct GNUNET_MESH_Unicast *) message;
7312 /* Sanity check for message size */
7313 size = ntohs (message->size);
7314 if (sizeof (struct GNUNET_MESH_Unicast) +
7315 sizeof (struct GNUNET_MessageHeader) > size)
7318 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7322 /* Tunnel exists? */
7323 tid = ntohl (data_msg->tid);
7324 t = tunnel_get_by_local_id (c, tid);
7328 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7332 /* Is it a local tunnel? Then, does client own the tunnel? */
7333 if (t->owner->handle != client)
7336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7340 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7341 &data_msg->destination.hashPubKey);
7342 /* Is the selected peer in the tunnel? */
7346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7350 /* PID should be as expected */
7351 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7354 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7355 "Unicast PID, expected %u, got %u\n",
7356 t->fwd_pid + 1, ntohl (data_msg->pid));
7357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7361 /* Ok, everything is correct, send the message
7362 * (pretend we got it from a mesh peer)
7365 /* Work around const limitation */
7366 char buf[ntohs (message->size)] GNUNET_ALIGN;
7367 struct GNUNET_MESH_Unicast *copy;
7369 copy = (struct GNUNET_MESH_Unicast *) buf;
7370 memcpy (buf, data_msg, size);
7371 copy->oid = my_full_id;
7372 copy->tid = htonl (t->id.tid);
7373 copy->ttl = htonl (default_ttl);
7374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7375 " calling generic handler...\n");
7376 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7379 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7386 * Handler for client traffic directed to the origin
7388 * @param cls closure
7389 * @param client identification of the client
7390 * @param message the actual message
7393 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7394 const struct GNUNET_MessageHeader *message)
7396 struct GNUNET_MESH_ToOrigin *data_msg;
7397 struct MeshTunnelClientInfo *clinfo;
7398 struct MeshClient *c;
7399 struct MeshTunnel *t;
7400 MESH_TunnelNumber tid;
7403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7404 "Got a ToOrigin request from a client!\n");
7405 /* Sanity check for client registration */
7406 if (NULL == (c = client_get (client)))
7409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7414 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7416 /* Sanity check for message size */
7417 size = ntohs (message->size);
7418 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7419 sizeof (struct GNUNET_MessageHeader) > size)
7422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7426 /* Tunnel exists? */
7427 tid = ntohl (data_msg->tid);
7428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7429 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7435 t = tunnel_get_by_local_id (c, tid);
7438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7445 /* It should be sent by someone who has this as incoming tunnel. */
7446 if (GNUNET_NO == client_knows_tunnel (c, t))
7449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7453 /* PID should be as expected */
7454 clinfo = tunnel_get_client_fc (t, c);
7455 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7458 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7459 "To Origin PID, expected %u, got %u\n",
7460 clinfo->bck_pid + 1,
7461 ntohl (data_msg->pid));
7462 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7467 /* Ok, everything is correct, send the message
7468 * (pretend we got it from a mesh peer)
7471 char buf[ntohs (message->size)] GNUNET_ALIGN;
7472 struct GNUNET_MESH_ToOrigin *copy;
7474 /* Work around const limitation */
7475 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7476 memcpy (buf, data_msg, size);
7477 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7478 copy->tid = htonl (t->id.tid);
7479 copy->ttl = htonl (default_ttl);
7480 copy->pid = htonl (t->bck_pid + 1);
7482 copy->sender = my_full_id;
7483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7484 " calling generic handler...\n");
7485 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7487 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7494 * Handler for client traffic directed to all peers in a tunnel
7496 * @param cls closure
7497 * @param client identification of the client
7498 * @param message the actual message
7501 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7502 const struct GNUNET_MessageHeader *message)
7504 struct MeshClient *c;
7505 struct MeshTunnel *t;
7506 struct GNUNET_MESH_Multicast *data_msg;
7507 MESH_TunnelNumber tid;
7509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7510 "Got a multicast request from a client!\n");
7512 /* Sanity check for client registration */
7513 if (NULL == (c = client_get (client)))
7516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7521 data_msg = (struct GNUNET_MESH_Multicast *) message;
7523 /* Sanity check for message size */
7524 if (sizeof (struct GNUNET_MESH_Multicast) +
7525 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7528 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7532 /* Tunnel exists? */
7533 tid = ntohl (data_msg->tid);
7534 t = tunnel_get_by_local_id (c, tid);
7538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7541 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7545 /* Does client own tunnel? */
7546 if (t->owner->handle != client)
7549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7553 /* PID should be as expected */
7554 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7557 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7558 "Multicast PID, expected %u, got %u\n",
7559 t->fwd_pid + 1, ntohl (data_msg->pid));
7560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7565 char buf[ntohs (message->size)] GNUNET_ALIGN;
7566 struct GNUNET_MESH_Multicast *copy;
7568 copy = (struct GNUNET_MESH_Multicast *) buf;
7569 memcpy (buf, message, ntohs (message->size));
7570 copy->oid = my_full_id;
7571 copy->tid = htonl (t->id.tid);
7572 copy->ttl = htonl (default_ttl);
7573 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7575 " calling generic handler...\n");
7576 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7579 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7585 * Handler for client's ACKs for payload traffic.
7587 * @param cls Closure (unused).
7588 * @param client Identification of the client.
7589 * @param message The actual message.
7592 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7593 const struct GNUNET_MessageHeader *message)
7595 struct GNUNET_MESH_LocalAck *msg;
7596 struct MeshTunnel *t;
7597 struct MeshClient *c;
7598 MESH_TunnelNumber tid;
7601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7602 /* Sanity check for client registration */
7603 if (NULL == (c = client_get (client)))
7606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7611 msg = (struct GNUNET_MESH_LocalAck *) message;
7613 /* Tunnel exists? */
7614 tid = ntohl (msg->tunnel_id);
7615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7616 t = tunnel_get_by_local_id (c, tid);
7620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7622 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7626 ack = ntohl (msg->max_pid);
7627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7629 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
7630 if (NULL != t->owner && t->owner->handle == client)
7632 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7634 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7638 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7639 tunnel_set_client_fwd_ack (t, c, ack);
7640 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7643 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7650 * Iterator over all peers to send a monitoring client info about a tunnel.
7652 * @param cls Closure (message being built).
7653 * @param key Key (hashed tunnel ID, unused).
7654 * @param value Peer info.
7656 * @return GNUNET_YES, to keep iterating.
7659 monitor_peers_iterator (void *cls,
7660 const struct GNUNET_HashCode * key,
7663 struct GNUNET_MESH_LocalMonitor *msg = cls;
7664 struct GNUNET_PeerIdentity *id;
7665 struct MeshPeerInfo *info = value;
7667 id = (struct GNUNET_PeerIdentity *) &msg[1];
7668 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
7671 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7672 "* sending info about peer %s [%u]\n",
7673 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
7681 * Iterator over all tunnels to send a monitoring client info about each tunnel.
7683 * @param cls Closure (client handle).
7684 * @param key Key (hashed tunnel ID, unused).
7685 * @param value Tunnel info.
7687 * @return GNUNET_YES, to keep iterating.
7690 monitor_all_tunnels_iterator (void *cls,
7691 const struct GNUNET_HashCode * key,
7694 struct GNUNET_SERVER_Client *client = cls;
7695 struct MeshTunnel *t = value;
7696 struct GNUNET_MESH_LocalMonitor *msg;
7699 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
7700 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
7701 npeers * sizeof (struct GNUNET_PeerIdentity));
7702 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
7703 msg->tunnel_id = htonl (t->id.tid);
7704 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
7705 npeers * sizeof (struct GNUNET_PeerIdentity));
7706 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
7708 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
7709 monitor_peers_iterator,
7712 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7713 "* sending info about tunnel %s [%u] (%u peers)\n",
7714 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
7716 if (msg->npeers != npeers)
7719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7720 "Get tunnels fail: size %u - iter %u\n",
7721 npeers, msg->npeers);
7724 msg->npeers = htonl (npeers);
7725 GNUNET_SERVER_notification_context_unicast (nc, client,
7726 &msg->header, GNUNET_NO);
7732 * Handler for client's MONITOR request.
7734 * @param cls Closure (unused).
7735 * @param client Identification of the client.
7736 * @param message The actual message.
7739 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
7740 const struct GNUNET_MessageHeader *message)
7742 struct MeshClient *c;
7744 /* Sanity check for client registration */
7745 if (NULL == (c = client_get (client)))
7748 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7752 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7753 "Received get tunnels request from client %u\n",
7755 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
7756 monitor_all_tunnels_iterator,
7758 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7759 "Get tunnels request from client %u completed\n",
7761 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7766 * Data needed to build a Monitor_Tunnel message.
7768 struct MeshMonitorTunnelContext
7771 * Partial message, including peer count.
7773 struct GNUNET_MESH_LocalMonitor *msg;
7776 * Hashmap with positions: peer->position.
7778 struct GNUNET_CONTAINER_MultiHashMap *lookup;
7781 * Index of the parent of each peer in the message, realtive to the absolute
7782 * order in the array (can be in a previous message).
7784 uint32_t parents[1024];
7787 * Peers visited so far in the tree, aka position of the current peer.
7789 unsigned int npeers;
7792 * Client requesting the info.
7794 struct MeshClient *c;
7799 * Send a client a message about the structure of a tunnel.
7801 * @param ctx Context of the tunnel iteration, with info regarding the state
7802 * of the execution and the number of peers visited for this message.
7805 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
7807 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
7808 struct GNUNET_PeerIdentity *pid;
7809 unsigned int *parent;
7812 size = sizeof (struct GNUNET_MESH_LocalMonitor);
7813 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
7814 resp->header.size = htons (size);
7815 pid = (struct GNUNET_PeerIdentity *) &resp[1];
7816 parent = (unsigned int *) &pid[resp->npeers];
7817 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
7818 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
7819 &resp->header, GNUNET_NO);
7823 * Iterator over a tunnel tree to build a message containing all peers
7824 * the in the tunnel, including relay nodes.
7826 * @param cls Closure (pointer to pointer of message being built).
7827 * @param peer Short ID of a peer.
7828 * @param parent Short ID of the @c peer 's parent.
7831 tunnel_tree_iterator (void *cls,
7832 GNUNET_PEER_Id peer,
7833 GNUNET_PEER_Id parent)
7835 struct MeshMonitorTunnelContext *ctx = cls;
7836 struct GNUNET_MESH_LocalMonitor *msg;
7837 struct GNUNET_PeerIdentity *pid;
7838 struct GNUNET_PeerIdentity ppid;
7841 pid = (struct GNUNET_PeerIdentity *) &msg[1];
7842 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
7843 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
7844 &pid[msg->npeers].hashPubKey,
7845 (void *) (long) ctx->npeers,
7846 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7847 GNUNET_PEER_resolve (parent, &ppid);
7848 ctx->parents[msg->npeers] =
7849 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
7855 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
7857 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
7860 send_client_tunnel_info (ctx);
7867 * Handler for client's MONITOR_TUNNEL request.
7869 * @param cls Closure (unused).
7870 * @param client Identification of the client.
7871 * @param message The actual message.
7874 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
7875 const struct GNUNET_MessageHeader *message)
7877 const struct GNUNET_MESH_LocalMonitor *msg;
7878 struct GNUNET_MESH_LocalMonitor *resp;
7879 struct MeshMonitorTunnelContext ctx;
7880 struct MeshClient *c;
7881 struct MeshTunnel *t;
7883 /* Sanity check for client registration */
7884 if (NULL == (c = client_get (client)))
7887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7891 msg = (struct GNUNET_MESH_LocalMonitor *) message;
7892 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7893 "Received tunnel info request from client %u for tunnel %s[%X]\n",
7896 ntohl (msg->tunnel_id));
7897 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
7900 /* We don't know the tunnel */
7901 struct GNUNET_MESH_LocalMonitor warn;
7904 warn.npeers = htonl (UINT_MAX);
7905 GNUNET_SERVER_notification_context_unicast (nc, client,
7908 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7912 /* Initialize context */
7913 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
7917 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
7921 /* Collect and send information */
7922 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
7923 send_client_tunnel_info (&ctx);
7926 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
7929 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7930 "Monitor tunnel request from client %u completed\n",
7932 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7937 * Functions to handle messages from clients
7939 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7940 {&handle_local_new_client, NULL,
7941 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7942 {&handle_local_announce_regex, NULL,
7943 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7944 {&handle_local_tunnel_create, NULL,
7945 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7946 sizeof (struct GNUNET_MESH_TunnelMessage)},
7947 {&handle_local_tunnel_destroy, NULL,
7948 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7949 sizeof (struct GNUNET_MESH_TunnelMessage)},
7950 {&handle_local_tunnel_speed, NULL,
7951 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7952 sizeof (struct GNUNET_MESH_TunnelMessage)},
7953 {&handle_local_tunnel_speed, NULL,
7954 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7955 sizeof (struct GNUNET_MESH_TunnelMessage)},
7956 {&handle_local_tunnel_buffer, NULL,
7957 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7958 sizeof (struct GNUNET_MESH_TunnelMessage)},
7959 {&handle_local_tunnel_buffer, NULL,
7960 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7961 sizeof (struct GNUNET_MESH_TunnelMessage)},
7962 {&handle_local_connect_add, NULL,
7963 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7964 sizeof (struct GNUNET_MESH_PeerControl)},
7965 {&handle_local_connect_del, NULL,
7966 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7967 sizeof (struct GNUNET_MESH_PeerControl)},
7968 {&handle_local_blacklist, NULL,
7969 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7970 sizeof (struct GNUNET_MESH_PeerControl)},
7971 {&handle_local_unblacklist, NULL,
7972 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7973 sizeof (struct GNUNET_MESH_PeerControl)},
7974 {&handle_local_connect_by_type, NULL,
7975 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7976 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7977 {&handle_local_connect_by_string, NULL,
7978 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7979 {&handle_local_unicast, NULL,
7980 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7981 {&handle_local_to_origin, NULL,
7982 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7983 {&handle_local_multicast, NULL,
7984 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7985 {&handle_local_ack, NULL,
7986 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7987 sizeof (struct GNUNET_MESH_LocalAck)},
7988 {&handle_local_get_tunnels, NULL,
7989 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
7990 sizeof (struct GNUNET_MessageHeader)},
7991 {&handle_local_show_tunnel, NULL,
7992 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
7993 sizeof (struct GNUNET_MESH_LocalMonitor)},
7999 * To be called on core init/fail.
8001 * @param cls service closure
8002 * @param server handle to the server for this service
8003 * @param identity the public identity of this peer
8006 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8007 const struct GNUNET_PeerIdentity *identity)
8010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8011 core_handle = server;
8012 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8018 GNUNET_i2s (identity));
8019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8021 GNUNET_i2s (&my_full_id));
8022 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8024 GNUNET_abort(); // Try harder
8031 * Method called whenever a given peer connects.
8033 * @param cls closure
8034 * @param peer peer identity this notification is about
8035 * @param atsi performance data for the connection
8036 * @param atsi_count number of records in 'atsi'
8039 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8040 const struct GNUNET_ATS_Information *atsi,
8041 unsigned int atsi_count)
8043 struct MeshPeerInfo *peer_info;
8044 struct MeshPeerPath *path;
8046 DEBUG_CONN ("Peer connected\n");
8047 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8048 peer_info = peer_info_get (peer);
8049 if (myid == peer_info->id)
8051 DEBUG_CONN (" (self)\n");
8056 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8058 path = path_new (2);
8059 path->peers[0] = myid;
8060 path->peers[1] = peer_info->id;
8061 GNUNET_PEER_change_rc (myid, 1);
8062 GNUNET_PEER_change_rc (peer_info->id, 1);
8063 peer_info_add_path (peer_info, path, GNUNET_YES);
8064 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8070 * Method called whenever a peer disconnects.
8072 * @param cls closure
8073 * @param peer peer identity this notification is about
8076 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8078 struct MeshPeerInfo *pi;
8079 struct MeshPeerQueue *q;
8080 struct MeshPeerQueue *n;
8082 DEBUG_CONN ("Peer disconnected\n");
8083 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8093 /* TODO try to reroute this traffic instead */
8094 queue_destroy(q, GNUNET_YES);
8097 if (NULL != pi->core_transmit)
8099 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8100 pi->core_transmit = NULL;
8102 peer_info_remove_path (pi, pi->id, myid);
8105 DEBUG_CONN (" (self)\n");
8107 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8112 /******************************************************************************/
8113 /************************ MAIN FUNCTIONS ****************************/
8114 /******************************************************************************/
8117 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8119 * @param cls closure
8120 * @param key current key code
8121 * @param value value in the hash map
8122 * @return GNUNET_YES if we should continue to iterate,
8126 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8128 struct MeshTunnel *t = value;
8135 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8137 * @param cls closure
8138 * @param key current key code
8139 * @param value value in the hash map
8140 * @return GNUNET_YES if we should continue to iterate,
8144 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8146 struct MeshPeerInfo *p = value;
8147 struct MeshPeerQueue *q;
8148 struct MeshPeerQueue *n;
8156 queue_destroy(q, GNUNET_YES);
8160 peer_info_destroy (p);
8166 * Task run during shutdown.
8172 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8176 if (core_handle != NULL)
8178 GNUNET_CORE_disconnect (core_handle);
8183 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8186 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8187 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8188 if (dht_handle != NULL)
8190 GNUNET_DHT_disconnect (dht_handle);
8195 GNUNET_SERVER_notification_context_destroy (nc);
8198 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8200 GNUNET_SCHEDULER_cancel (announce_id_task);
8201 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8203 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8205 GNUNET_SCHEDULER_cancel (announce_applications_task);
8206 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8213 * Callback for hostkey read/generation
8215 * @param cls Closure (Configuration handle).
8216 * @param pk the private key
8217 * @param emsg error message
8220 key_generation_cb (void *cls,
8221 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8224 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8225 struct MeshPeerInfo *peer;
8226 struct MeshPeerPath *p;
8231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8232 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8234 GNUNET_SCHEDULER_shutdown ();
8237 my_private_key = pk;
8238 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8239 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8240 &my_full_id.hashPubKey);
8241 myid = GNUNET_PEER_intern (&my_full_id);
8242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8243 "Mesh for peer [%s] starting\n",
8244 GNUNET_i2s(&my_full_id));
8246 // transport_handle = GNUNET_TRANSPORT_connect(c,
8253 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8254 NULL, /* Closure passed to MESH functions */
8255 &core_init, /* Call core_init once connected */
8256 &core_connect, /* Handle connects */
8257 &core_disconnect, /* remove peers on disconnects */
8258 NULL, /* Don't notify about all incoming messages */
8259 GNUNET_NO, /* For header only in notification */
8260 NULL, /* Don't notify about all outbound messages */
8261 GNUNET_NO, /* For header-only out notification */
8262 core_handlers); /* Register these handlers */
8264 if (core_handle == NULL)
8267 GNUNET_SCHEDULER_shutdown ();
8272 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8275 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8276 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8277 GNUNET_SERVER_disconnect_notify (server_handle,
8278 &handle_local_client_disconnect, NULL);
8282 clients_tail = NULL;
8285 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8286 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8288 /* Create a peer_info for the local peer */
8289 peer = peer_info_get (&my_full_id);
8292 GNUNET_PEER_change_rc (myid, 1);
8293 peer_info_add_path (peer, p, GNUNET_YES);
8294 GNUNET_SERVER_resume (server_handle);
8295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8300 * Process mesh requests.
8302 * @param cls closure
8303 * @param server the initialized server
8304 * @param c configuration to use
8307 run (void *cls, struct GNUNET_SERVER_Handle *server,
8308 const struct GNUNET_CONFIGURATION_Handle *c)
8312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8313 server_handle = server;
8316 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8321 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8323 GNUNET_SCHEDULER_shutdown ();
8328 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8329 &refresh_path_time))
8331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8333 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8334 "mesh", "refresh path time");
8335 GNUNET_SCHEDULER_shutdown ();
8340 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8341 &app_announce_time))
8343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8345 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8346 "mesh", "app announce time");
8347 GNUNET_SCHEDULER_shutdown ();
8350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8351 "APP_ANNOUNCE_TIME %llu ms\n",
8352 app_announce_time.rel_value);
8354 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8359 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8360 "mesh", "id announce time");
8361 GNUNET_SCHEDULER_shutdown ();
8366 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8371 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8372 "mesh", "connect timeout");
8373 GNUNET_SCHEDULER_shutdown ();
8378 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8383 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8384 "mesh", "max msgs queue");
8385 GNUNET_SCHEDULER_shutdown ();
8390 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8393 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8395 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8396 "mesh", "max tunnels");
8397 GNUNET_SCHEDULER_shutdown ();
8402 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8407 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8408 "mesh", "default ttl", 64);
8413 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8417 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8418 "mesh", "max peers", 1000);
8423 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8424 &dht_replication_level))
8426 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8428 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8429 "mesh", "dht replication level", 3);
8430 dht_replication_level = 3;
8433 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8434 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8435 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8436 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8437 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8439 dht_handle = GNUNET_DHT_connect (c, 64);
8440 if (NULL == dht_handle)
8444 stats = GNUNET_STATISTICS_create ("mesh", c);
8446 GNUNET_SERVER_suspend (server_handle);
8447 /* Scheduled the task to clean up when shutdown is called */
8448 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8450 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
8453 GNUNET_free (keyfile);
8458 * The main function for the mesh service.
8460 * @param argc number of arguments from the command line
8461 * @param argv command line arguments
8462 * @return 0 ok, 1 on error
8465 main (int argc, char *const *argv)
8470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8471 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8473 ret = (GNUNET_OK == r) ? 0 : 1;
8474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8478 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8479 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8480 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);