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 "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
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_POLL_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
102 /******************************************************************************/
103 /************************ DATA STRUCTURES ****************************/
104 /******************************************************************************/
106 /** FWD declaration */
112 * Struct representing a piece of data being sent to other peers
116 /** Tunnel it belongs to. */
117 struct MeshTunnel *t;
119 /** How many remaining neighbors still hav't got it. */
120 unsigned int reference_counter;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int total_out;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct to store regex information announced by clients.
178 struct MeshRegexDescriptor
181 * Regular expression itself.
186 * How many characters per edge can we squeeze?
188 uint16_t compression;
192 * Struct containing all info possibly needed to build a package when called
195 struct MeshTransmissionDescriptor
197 /** ID of the tunnel this packet travels in */
198 struct MESH_TunnelID *origin;
200 /** Who was this message being sent to */
201 struct MeshPeerInfo *peer;
203 /** Ultimate destination of the packet */
204 GNUNET_PEER_Id destination;
206 /** Data descriptor */
207 struct MeshData* mesh_data;
212 * Struct containing all information regarding a given peer
222 * Last time we heard from this peer
224 struct GNUNET_TIME_Absolute last_contact;
227 * Task handler for delayed connect task;
229 GNUNET_SCHEDULER_TaskIdentifier connect_task;
232 * Number of attempts to reconnect so far
234 int n_reconnect_attempts;
237 * Paths to reach the peer, ordered by ascending hop count
239 struct MeshPeerPath *path_head;
242 * Paths to reach the peer, ordered by ascending hop count
244 struct MeshPeerPath *path_tail;
247 * Handle to stop the DHT search for a path to this peer
249 struct GNUNET_DHT_GetHandle *dhtget;
252 * Closure given to the DHT GET
254 struct MeshPathInfo *dhtgetcls;
257 * Array of tunnels this peer participates in
258 * (most probably a small amount, therefore not a hashmap)
259 * When the path to the peer changes, notify these tunnels to let them
260 * re-adjust their path trees.
262 struct MeshTunnel **tunnels;
265 * Number of tunnels this peers participates in
267 unsigned int ntunnels;
270 * Transmission queue to core DLL head
272 struct MeshPeerQueue *queue_head;
275 * Transmission queue to core DLL tail
277 struct MeshPeerQueue *queue_tail;
280 * How many messages are in the queue to this peer.
282 unsigned int queue_n;
285 * Handle to for queued transmissions
287 struct GNUNET_CORE_TransmitHandle *core_transmit;
292 * Globally unique tunnel identification (owner + number)
293 * DO NOT USE OVER THE NETWORK
298 * Node that owns the tunnel
303 * Tunnel number to differentiate all the tunnels owned by the node oid
304 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
306 MESH_TunnelNumber tid;
311 * Struct containing all information regarding a tunnel
312 * For an intermediate node the improtant info used will be:
313 * - id Tunnel unique identification
314 * - paths[0] To know where to send it next
315 * - metainfo: ready, speeds, accounting
322 struct MESH_TunnelID id;
325 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
327 MESH_TunnelNumber local_tid;
330 * Local tunnel number for local destination clients (incoming number)
331 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
334 MESH_TunnelNumber local_tid_dest;
337 * Is the speed on the tunnel limited to the slowest peer?
342 * Is the tunnel bufferless (minimum latency)?
347 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
352 * Packet ID of the last bck packet sent (unique counter per hop).
357 * SKIP value for this tunnel.
362 * Force sending ACK? Flag to allow duplicate ACK on POLL.
367 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
368 * Contains the Flow Control info: FWD ACK value received,
369 * last BCK ACK sent, PID and SKIP values.
371 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
374 * Last ACK sent towards the origin (for traffic towards leaf node).
376 uint32_t last_fwd_ack;
379 * BCK ACK value received from the hop towards the owner of the tunnel,
380 * (previous node / owner): up to what message PID can we sent back to him.
385 * How many messages are in the forward queue (towards leaves).
387 unsigned int fwd_queue_n;
390 * How many messages do we accept in the forward queue.
392 unsigned int fwd_queue_max;
395 * How many messages are in the backward queue (towards origin).
397 unsigned int bck_queue_n;
400 * How many messages do we accept in the backward queue.
402 unsigned int bck_queue_max;
405 * Task to poll peer in case of a stall.
407 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
410 * Last time the tunnel was used
412 struct GNUNET_TIME_Absolute timestamp;
415 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
416 * containing peers added by id or by type, not intermediate peers.
418 struct GNUNET_CONTAINER_MultiHashMap *peers;
421 * Number of peers that are connected and potentially ready to receive data
423 unsigned int peers_ready;
426 * Number of peers that have been added to the tunnel
428 unsigned int peers_total;
431 * Client owner of the tunnel, if any
433 struct MeshClient *owner;
436 * Clients that have been informed about and want to stay in the tunnel.
438 struct MeshClient **clients;
441 * Flow control info for each client.
443 struct MeshTunnelClientInfo *clients_fc;
446 * Number of elements in clients/clients_fc
448 unsigned int nclients;
451 * Clients that have been informed but requested to leave the tunnel.
453 struct MeshClient **ignore;
456 * Number of elements in clients
458 unsigned int nignore;
463 GNUNET_PEER_Id *blacklisted;
466 * Number of elements in blacklisted
468 unsigned int nblacklisted;
471 * Bloomfilter (for peer identities) to stop circular routes
473 char bloomfilter[MESH_BLOOM_SIZE];
478 struct MeshTunnelTree *tree;
481 * Application type we are looking for in this tunnel
483 GNUNET_MESH_ApplicationType type;
486 * Used to search peers offering a service
488 struct GNUNET_DHT_GetHandle *dht_get_type;
491 * Initial context of the regex search for a connect_by_string
493 struct MeshRegexSearchContext *regex_ctx;
496 * Task to keep the used paths alive
498 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
501 * Task to destroy the tunnel after timeout
503 * FIXME: merge the two? a tunnel will have either
504 * a path refresh OR a timeout, never both!
506 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
509 * Flag to signal the destruction of the tunnel.
510 * If this is set GNUNET_YES the tunnel will be destroyed
511 * when the queue is empty.
516 * Total messages pending for this tunnels, payload or not.
518 unsigned int pending_messages;
523 * Info about a child node in a tunnel, needed to perform flow control.
525 struct MeshTunnelChildInfo
528 * ID of the child node.
548 * Maximum PID allowed (FWD ACK received).
553 * Last ACK sent to that child (BCK ACK).
558 * Circular buffer pointing to MeshPeerQueue elements for all
559 * payload traffic going to this child.
560 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
562 struct MeshPeerQueue **send_buffer;
565 * Index of the oldest element in the send_buffer.
567 unsigned int send_buffer_start;
570 * How many elements are already in the buffer.
572 unsigned int send_buffer_n;
575 * Tunnel this info is about
577 struct MeshTunnel *t;
580 * Task to poll peer in case of a stall.
582 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
587 * Info about a leaf client of a tunnel, needed to perform flow control.
589 struct MeshTunnelClientInfo
592 * PID of the last packet sent to the client (FWD).
597 * PID of the last packet received from the client (BCK).
602 * Maximum PID allowed (FWD ACK received).
607 * Last ACK sent to that child (BCK ACK).
615 * Info collected during iteration of child nodes in order to get the ACK value
618 struct MeshTunnelChildIteratorContext
621 * Tunnel whose info is being collected.
623 struct MeshTunnel *t;
626 * Is this context initialized? Is the value in max_child_ack valid?
631 * Maximum child ACK so far.
633 uint32_t max_child_ack;
636 * Number of children nodes
638 unsigned int nchildren;
643 * Info needed to work with tunnel paths and peers
650 struct MeshTunnel *t;
653 * Neighbouring peer to whom we send the packet to
655 struct MeshPeerInfo *peer;
660 struct MeshPeerPath *path;
665 * Struct containing information about a client of the service
672 struct MeshClient *next;
677 struct MeshClient *prev;
680 * Tunnels that belong to this client, indexed by local id
682 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
685 * Tunnels this client has accepted, indexed by incoming local id
687 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
690 * Tunnels this client has rejected, indexed by incoming local id
692 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
694 * Handle to communicate with the client
696 struct GNUNET_SERVER_Client *handle;
699 * Applications that this client has claimed to provide
701 struct GNUNET_CONTAINER_MultiHashMap *apps;
704 * Messages that this client has declared interest in
706 struct GNUNET_CONTAINER_MultiHashMap *types;
709 * Whether the client is active or shutting down (don't send confirmations
710 * to a client that is shutting down.
715 * ID of the client, mainly for debug messages
720 * Regular expressions describing the services offered by this client.
722 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
725 * Number of regular expressions in regexes.
727 unsigned int n_regex;
730 * Task to refresh all regular expresions in the DHT.
732 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
738 * Struct to keep information of searches of services described by a regex
739 * using a user-provided string service description.
741 struct MeshRegexSearchInfo
744 * Which tunnel is this for
746 struct MeshTunnel *t;
749 * User provided description of the searched service.
754 * Part of the description already consumed by the search.
761 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
764 * Results from running DHT GETs.
766 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
769 * Contexts, for each running DHT GET. Free all on end of search.
771 struct MeshRegexSearchContext **contexts;
774 * Number of contexts (branches/steps in search).
776 unsigned int n_contexts;
779 * Peer that is connecting via connect_by_string. When connected, free ctx.
784 * Other peers that are found but not yet being connected to.
786 GNUNET_PEER_Id *peers;
789 * Number of elements in peers.
791 unsigned int n_peers;
794 * Next peer to try to connect to.
799 * Timeout for a connect attempt.
800 * When reached, try to connect to a different peer, if any. If not,
801 * try the same peer again.
803 GNUNET_SCHEDULER_TaskIdentifier timeout;
808 * Struct to keep state of running searches that have consumed a part of
811 struct MeshRegexSearchContext
814 * Part of the description already consumed by
815 * this particular search branch.
820 * Information about the search.
822 struct MeshRegexSearchInfo *info;
825 * We just want to look for one edge, the longer the better.
828 unsigned int longest_match;
831 * Destination hash of the longest match.
833 struct GNUNET_HashCode hash;
836 /******************************************************************************/
837 /************************ DEBUG FUNCTIONS ****************************/
838 /******************************************************************************/
842 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
843 * @param cls string to print
844 * @param success GNUNET_OK if the PUT was transmitted,
845 * GNUNET_NO on timeout,
846 * GNUNET_SYSERR on disconnect from service
847 * after the PUT message was transmitted
848 * (so we don't know if it was received or not)
853 mesh_debug (void *cls, int success)
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
861 unsigned int debug_fwd_ack;
862 unsigned int debug_bck_ack;
866 /******************************************************************************/
867 /*********************** GLOBAL VARIABLES ****************************/
868 /******************************************************************************/
870 /************************** Configuration parameters **************************/
873 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
875 static struct GNUNET_TIME_Relative refresh_path_time;
878 * How often to PUT local application numbers in the DHT.
880 static struct GNUNET_TIME_Relative app_announce_time;
883 * How often to PUT own ID in the DHT.
885 static struct GNUNET_TIME_Relative id_announce_time;
888 * Maximum time allowed to connect to a peer found by string.
890 static struct GNUNET_TIME_Relative connect_timeout;
893 * Default TTL for payload packets.
895 static unsigned long long default_ttl;
898 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
900 static unsigned long long dht_replication_level;
903 * How many tunnels are we willing to maintain.
904 * Local tunnels are always allowed, even if there are more tunnels than max.
906 static unsigned long long max_tunnels;
909 * How many messages *in total* are we willing to queue, divided by number of
910 * tunnels to get tunnel queue size.
912 static unsigned long long max_msgs_queue;
915 /*************************** Static global variables **************************/
918 * Hostkey generation context
920 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
923 * DLL with all the clients, head.
925 static struct MeshClient *clients;
928 * DLL with all the clients, tail.
930 static struct MeshClient *clients_tail;
933 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
935 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
938 * Number of tunnels known.
940 static unsigned long long n_tunnels;
943 * Tunnels incoming, indexed by MESH_TunnelNumber
944 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
946 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
949 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
951 static struct GNUNET_CONTAINER_MultiHashMap *peers;
954 * Handle to communicate with transport
956 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
959 * Handle to communicate with core.
961 static struct GNUNET_CORE_Handle *core_handle;
966 static struct GNUNET_DHT_Handle *dht_handle;
971 static struct GNUNET_SERVER_Handle *server_handle;
974 * Handle to the statistics service.
976 static struct GNUNET_STATISTICS_Handle *stats;
979 * Notification context, to send messages to local clients.
981 static struct GNUNET_SERVER_NotificationContext *nc;
984 * Local peer own ID (memory efficient handle).
986 static GNUNET_PEER_Id myid;
989 * Local peer own ID (full value).
991 static struct GNUNET_PeerIdentity my_full_id;
996 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1001 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1004 * Tunnel ID for the next created tunnel (global tunnel number).
1006 static MESH_TunnelNumber next_tid;
1009 * Tunnel ID for the next incoming tunnel (local tunnel number).
1011 static MESH_TunnelNumber next_local_tid;
1014 * All application types provided by this peer.
1016 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1019 * All message types clients of this peer are interested in.
1021 static struct GNUNET_CONTAINER_MultiHashMap *types;
1024 * Task to periodically announce provided applications.
1026 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1029 * Task to periodically announce itself in the network.
1031 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1034 * Next ID to assign to a client.
1036 unsigned int next_client_id;
1039 /******************************************************************************/
1040 /*********************** DECLARATIONS **************************/
1041 /******************************************************************************/
1044 * Function to process paths received for a new peer addition. The recorded
1045 * paths form the initial tunnel, which can be optimized later.
1046 * Called on each result obtained for the DHT search.
1048 * @param cls closure
1049 * @param exp when will this value expire
1050 * @param key key of the result
1051 * @param type type of the result
1052 * @param size number of bytes in data
1053 * @param data pointer to the result data
1056 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1057 const struct GNUNET_HashCode * key,
1058 const struct GNUNET_PeerIdentity *get_path,
1059 unsigned int get_path_length,
1060 const struct GNUNET_PeerIdentity *put_path,
1061 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1062 size_t size, const void *data);
1066 * Function to process DHT string to regex matching.
1067 * Called on each result obtained for the DHT search.
1069 * @param cls closure (search context)
1070 * @param exp when will this value expire
1071 * @param key key of the result
1072 * @param get_path path of the get request (not used)
1073 * @param get_path_length lenght of get_path (not used)
1074 * @param put_path path of the put request (not used)
1075 * @param put_path_length length of the put_path (not used)
1076 * @param type type of the result
1077 * @param size number of bytes in data
1078 * @param data pointer to the result data
1080 * TODO: re-issue the request after certain time? cancel after X results?
1083 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1084 const struct GNUNET_HashCode * key,
1085 const struct GNUNET_PeerIdentity *get_path,
1086 unsigned int get_path_length,
1087 const struct GNUNET_PeerIdentity *put_path,
1088 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1089 size_t size, const void *data);
1093 * Function to process DHT string to regex matching.
1094 * Called on each result obtained for the DHT search.
1096 * @param cls closure (search context)
1097 * @param exp when will this value expire
1098 * @param key key of the result
1099 * @param get_path path of the get request (not used)
1100 * @param get_path_length lenght of get_path (not used)
1101 * @param put_path path of the put request (not used)
1102 * @param put_path_length length of the put_path (not used)
1103 * @param type type of the result
1104 * @param size number of bytes in data
1105 * @param data pointer to the result data
1108 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1109 const struct GNUNET_HashCode * key,
1110 const struct GNUNET_PeerIdentity *get_path,
1111 unsigned int get_path_length,
1112 const struct GNUNET_PeerIdentity *put_path,
1113 unsigned int put_path_length,
1114 enum GNUNET_BLOCK_Type type,
1115 size_t size, const void *data);
1119 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1120 * and insert it in the appropiate structures if the peer is not known yet.
1122 * @param peer Short identity of the peer.
1124 * @return Existing or newly created peer info.
1126 static struct MeshPeerInfo *
1127 peer_info_get_short (const GNUNET_PEER_Id peer);
1131 * Try to establish a new connection to this peer.
1132 * Use the best path for the given tunnel.
1133 * If the peer doesn't have any path to it yet, try to get one.
1134 * If the peer already has some path, send a CREATE PATH towards it.
1136 * @param peer PeerInfo of the peer.
1137 * @param t Tunnel for which to create the path, if possible.
1140 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1144 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1145 * needed rescources.
1146 * If peer already exists, reevaluate shortest path and change if different.
1148 * @param t Tunnel we want to add a new peer to
1149 * @param peer PeerInfo of the peer being added
1153 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1157 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1158 * that are no longer needed, as well as nodes of no longer reachable peers.
1159 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1161 * @param t Tunnel from which to remove the path.
1162 * @param peer Short id of the peer which should be removed.
1165 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1169 * Search for a tunnel by global ID using full PeerIdentities.
1171 * @param oid owner of the tunnel.
1172 * @param tid global tunnel number.
1174 * @return tunnel handler, NULL if doesn't exist.
1176 static struct MeshTunnel *
1177 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1181 * Delete an active client from the tunnel.
1187 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1190 * Notify a tunnel that a connection has broken that affects at least
1191 * some of its peers.
1193 * @param t Tunnel affected.
1194 * @param p1 Peer that got disconnected from p2.
1195 * @param p2 Peer that got disconnected from p1.
1197 * @return Short ID of the peer disconnected (either p1 or p2).
1198 * 0 if the tunnel remained unaffected.
1200 static GNUNET_PEER_Id
1201 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1206 * Get the current ack value for a tunnel, for data going from root to leaves,
1207 * taking in account the tunnel mode and the status of all children and clients.
1211 * @return Maximum PID allowed.
1214 tunnel_get_fwd_ack (struct MeshTunnel *t);
1218 * Add a client to a tunnel, initializing all needed data structures.
1220 * @param t Tunnel to which add the client.
1221 * @param c Client which to add to the tunnel.
1224 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1228 * Jump to the next edge, with the longest matching token.
1230 * @param block Block found in the DHT.
1231 * @param size Size of the block.
1232 * @param ctx Context of the search.
1234 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1237 regex_next_edge (const struct MeshRegexBlock *block,
1239 struct MeshRegexSearchContext *ctx);
1243 * Find a path to a peer that offers a regex servcie compatible
1244 * with a given string.
1246 * @param key The key of the accepting state.
1247 * @param ctx Context containing info about the string, tunnel, etc.
1250 regex_find_path (const struct GNUNET_HashCode *key,
1251 struct MeshRegexSearchContext *ctx);
1255 * @brief Queue and pass message to core when possible.
1257 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1258 * and accounts for it. In case the queue is full, the message is dropped and
1261 * Otherwise, message is treated as internal and allowed to go regardless of
1264 * @param cls Closure (@c type dependant). It will be used by queue_send to
1265 * build the message to be sent if not already prebuilt.
1266 * @param type Type of the message, 0 for a raw message.
1267 * @param size Size of the message.
1268 * @param dst Neighbor to send message to.
1269 * @param t Tunnel this message belongs to.
1272 queue_add (void *cls, uint16_t type, size_t size,
1273 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1277 * Free a transmission that was already queued with all resources
1278 * associated to the request.
1280 * @param queue Queue handler to cancel.
1281 * @param clear_cls Is it necessary to free associated cls?
1284 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1288 * @brief Get the next transmittable message from the queue.
1290 * This will be the head, except in the case of being a data packet
1291 * not allowed by the destination peer.
1293 * @param peer Destination peer.
1295 * @return The next viable MeshPeerQueue element to send to that peer.
1296 * NULL when there are no transmittable messages.
1298 struct MeshPeerQueue *
1299 queue_get_next (const struct MeshPeerInfo *peer);
1303 * Core callback to write a queued packet to core buffer
1305 * @param cls Closure (peer info).
1306 * @param size Number of bytes available in buf.
1307 * @param buf Where the to write the message.
1309 * @return number of bytes written to buf
1312 queue_send (void *cls, size_t size, void *buf);
1314 /******************************************************************************/
1315 /************************ ITERATORS ****************************/
1316 /******************************************************************************/
1319 * Iterator over found existing mesh regex blocks that match an ongoing search.
1321 * @param cls closure
1322 * @param key current key code
1323 * @param value value in the hash map
1324 * @return GNUNET_YES if we should continue to iterate,
1328 regex_result_iterator (void *cls,
1329 const struct GNUNET_HashCode * key,
1332 struct MeshRegexBlock *block = value;
1333 struct MeshRegexSearchContext *ctx = cls;
1335 if (GNUNET_YES == ntohl(block->accepting) &&
1336 ctx->position == strlen (ctx->info->description))
1338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1339 regex_find_path (key, ctx);
1340 return GNUNET_YES; // We found an accept state!
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1345 ctx->position, strlen(ctx->info->description),
1346 ntohl(block->accepting));
1349 regex_next_edge(block, SIZE_MAX, ctx);
1351 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1358 * Iterator over edges in a regex block retrieved from the DHT.
1360 * @param cls Closure (context of the search).
1361 * @param token Token that follows to next state.
1362 * @param len Lenght of token.
1363 * @param key Hash of next state.
1365 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1368 regex_edge_iterator (void *cls,
1371 const struct GNUNET_HashCode *key)
1373 struct MeshRegexSearchContext *ctx = cls;
1374 struct MeshRegexSearchInfo *info = ctx->info;
1378 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1383 current = &info->description[ctx->position];
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1385 current_len = strlen (info->description) - ctx->position;
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1390 if (len > current_len)
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1393 return GNUNET_YES; // Token too long, wont match
1395 if (0 != strncmp (current, token, len))
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1398 return GNUNET_YES; // Token doesn't match
1401 if (len > ctx->longest_match)
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1404 ctx->longest_match = len;
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1418 * Jump to the next edge, with the longest matching token.
1420 * @param block Block found in the DHT.
1421 * @param size Size of the block.
1422 * @param ctx Context of the search.
1424 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1427 regex_next_edge (const struct MeshRegexBlock *block,
1429 struct MeshRegexSearchContext *ctx)
1431 struct MeshRegexSearchContext *new_ctx;
1432 struct MeshRegexSearchInfo *info = ctx->info;
1433 struct GNUNET_DHT_GetHandle *get_h;
1437 /* Find the longest match for the current string position,
1438 * among tokens in the given block */
1439 ctx->longest_match = 0;
1440 result = GNUNET_MESH_regex_block_iterate (block, size,
1441 ®ex_edge_iterator, ctx);
1442 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1444 /* Did anything match? */
1445 if (0 == ctx->longest_match)
1448 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1449 new_ctx->info = info;
1450 new_ctx->position = ctx->position + ctx->longest_match;
1451 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1453 /* Check whether we already have a DHT GET running for it */
1455 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1458 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1460 ®ex_result_iterator,
1462 return; // We are already looking for it
1465 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1467 /* Start search in DHT */
1469 GNUNET_DHT_get_start (dht_handle, /* handle */
1470 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1471 &ctx->hash, /* key to search */
1472 dht_replication_level, /* replication level */
1473 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1474 NULL, /* xquery */ // FIXME BLOOMFILTER
1475 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1476 &dht_get_string_handler, new_ctx);
1478 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1481 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1490 * Iterator over hash map entries to cancel DHT GET requests after a
1491 * successful connect_by_string.
1493 * @param cls Closure (unused).
1494 * @param key Current key code (unused).
1495 * @param value Value in the hash map (get handle).
1496 * @return GNUNET_YES if we should continue to iterate,
1500 regex_cancel_dht_get (void *cls,
1501 const struct GNUNET_HashCode * key,
1504 struct GNUNET_DHT_GetHandle *h = value;
1506 GNUNET_DHT_get_stop (h);
1512 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1513 * search for connect_by_string.
1515 * @param cls Closure (unused).
1516 * @param key Current key code (unused).
1517 * @param value MeshRegexBlock in the hash map.
1518 * @return GNUNET_YES if we should continue to iterate,
1522 regex_free_result (void *cls,
1523 const struct GNUNET_HashCode * key,
1527 GNUNET_free (value);
1533 * Regex callback iterator to store own service description in the DHT.
1535 * @param cls closure.
1536 * @param key hash for current state.
1537 * @param proof proof for current state.
1538 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1539 * @param num_edges number of edges leaving current state.
1540 * @param edges edges leaving current state.
1543 regex_iterator (void *cls,
1544 const struct GNUNET_HashCode *key,
1547 unsigned int num_edges,
1548 const struct GNUNET_REGEX_Edge *edges)
1550 struct MeshRegexBlock *block;
1551 struct MeshRegexEdge *block_edge;
1552 enum GNUNET_DHT_RouteOption opt;
1556 unsigned int offset;
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 " regex dht put for state %s\n",
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1570 if (GNUNET_YES == accepting)
1572 struct MeshRegexAccept block;
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575 " state %s is accepting, putting own id\n",
1577 size = sizeof (block);
1579 block.id = my_full_id;
1581 GNUNET_DHT_put(dht_handle, key,
1582 dht_replication_level,
1583 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1584 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1587 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1592 len = strlen(proof);
1593 size = sizeof (struct MeshRegexBlock) + len;
1594 block = GNUNET_malloc (size);
1597 block->n_proof = htonl (len);
1598 block->n_edges = htonl (num_edges);
1599 block->accepting = htonl (accepting);
1601 /* Store the proof at the end of the block. */
1602 aux = (char *) &block[1];
1603 memcpy (aux, proof, len);
1606 /* Store each edge in a variable length MeshEdge struct at the
1607 * very end of the MeshRegexBlock structure.
1609 for (i = 0; i < num_edges; i++)
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 " edge %s towards %s\n",
1614 GNUNET_h2s(&edges[i].destination));
1616 /* aux points at the end of the last block */
1617 len = strlen (edges[i].label);
1618 size += sizeof (struct MeshRegexEdge) + len;
1619 // Calculate offset FIXME is this ok? use size instead?
1620 offset = aux - (char *) block;
1621 block = GNUNET_realloc (block, size);
1622 aux = &((char *) block)[offset];
1623 block_edge = (struct MeshRegexEdge *) aux;
1624 block_edge->key = edges[i].destination;
1625 block_edge->n_token = htonl (len);
1626 aux = (char *) &block_edge[1];
1627 memcpy (aux, edges[i].label, len);
1631 GNUNET_DHT_put(dht_handle, key,
1632 dht_replication_level,
1634 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1636 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1640 GNUNET_free (block);
1645 * Store the regular expression describing a local service into the DHT.
1647 * @param regex The regular expresion.
1650 regex_put (const struct MeshRegexDescriptor *regex)
1652 struct GNUNET_REGEX_Automaton *dfa;
1654 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1655 dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1656 strlen(regex->regex),
1657 regex->compression);
1658 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1659 GNUNET_REGEX_automaton_destroy (dfa);
1660 DEBUG_DHT (" regex_put (%s) end\n", regex);
1665 * Find a path to a peer that offers a regex servcie compatible
1666 * with a given string.
1668 * @param key The key of the accepting state.
1669 * @param ctx Context containing info about the string, tunnel, etc.
1672 regex_find_path (const struct GNUNET_HashCode *key,
1673 struct MeshRegexSearchContext *ctx)
1675 struct GNUNET_DHT_GetHandle *get_h;
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1678 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1679 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1680 key, /* key to search */
1681 dht_replication_level, /* replication level */
1682 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1683 GNUNET_DHT_RO_RECORD_ROUTE,
1684 NULL, /* xquery */ // FIXME BLOOMFILTER
1685 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1686 &dht_get_string_accept_handler, ctx);
1687 GNUNET_break (GNUNET_OK ==
1688 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1696 * Function called if the connect attempt to a peer found via
1697 * connect_by_string times out. Try to connect to another peer, if any.
1698 * Otherwise try to reconnect to the same peer.
1700 * @param cls Closure (info about regex search).
1701 * @param tc TaskContext.
1704 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1706 struct MeshRegexSearchInfo *info = cls;
1707 struct MeshPeerInfo *peer_info;
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1712 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1713 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1721 if (0 < info->n_peers)
1723 // Select next peer, put current in that spot.
1724 id = info->peers[info->i_peer];
1725 info->peers[info->i_peer] = info->peer;
1726 info->i_peer = (info->i_peer + 1) % info->n_peers;
1730 // Try to connect to same peer again.
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1735 peer_info = peer_info_get_short(id);
1736 tunnel_add_peer (info->t, peer_info);
1738 tunnel_delete_peer (info->t, old);
1739 peer_info_connect (peer_info, info->t);
1740 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1741 ®ex_connect_timeout,
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1748 * Cancel an ongoing regex search in the DHT and free all resources.
1750 * @param ctx The search context.
1753 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1755 struct MeshRegexSearchInfo *info = ctx->info;
1758 GNUNET_free (info->description);
1759 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1760 ®ex_cancel_dht_get, NULL);
1761 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1762 ®ex_free_result, NULL);
1763 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1764 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1765 info->t->regex_ctx = NULL;
1766 for (i = 0; i < info->n_contexts; i++)
1768 GNUNET_free (info->contexts[i]);
1770 if (0 < info->n_contexts)
1771 GNUNET_free (info->contexts);
1772 if (0 < info->n_peers)
1773 GNUNET_free (info->peers);
1774 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1776 GNUNET_SCHEDULER_cancel(info->timeout);
1782 /******************************************************************************/
1783 /************************ PERIODIC FUNCTIONS ****************************/
1784 /******************************************************************************/
1787 * Announce iterator over for each application provided by the peer
1789 * @param cls closure
1790 * @param key current key code
1791 * @param value value in the hash map
1792 * @return GNUNET_YES if we should continue to
1797 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1799 struct PBlock block;
1800 struct MeshClient *c;
1802 block.id = my_full_id;
1803 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1804 GNUNET_assert(NULL != c);
1805 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1806 if (0 == block.type)
1811 block.type = htonl (block.type);
1813 GNUNET_break (NULL !=
1814 GNUNET_DHT_put (dht_handle, key,
1815 dht_replication_level,
1816 GNUNET_DHT_RO_RECORD_ROUTE |
1817 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1818 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1820 (const char *) &block,
1821 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1823 app_announce_time, NULL, NULL));
1829 * Periodically announce what applications are provided by local clients
1832 * @param cls closure
1833 * @param tc task context
1836 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1838 struct MeshClient *c = cls;
1841 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1842 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1847 DEBUG_DHT ("Starting PUT for regex\n");
1849 for (i = 0; i < c->n_regex; i++)
1851 regex_put (&c->regexes[i]);
1853 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1856 DEBUG_DHT ("Finished PUT for regex\n");
1863 * Periodically announce what applications are provided by local clients
1866 * @param cls closure
1867 * @param tc task context
1870 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1872 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1874 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1878 DEBUG_DHT ("Starting PUT for apps\n");
1880 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1882 announce_applications_task =
1883 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1885 DEBUG_DHT ("Finished PUT for apps\n");
1892 * Periodically announce self id in the DHT
1894 * @param cls closure
1895 * @param tc task context
1898 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1900 struct PBlock block;
1902 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1904 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1908 * - Set data expiration in function of X
1909 * - Adapt X to churn
1911 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1913 block.id = my_full_id;
1914 block.type = htonl (0);
1915 GNUNET_DHT_put (dht_handle, /* DHT handle */
1916 &my_full_id.hashPubKey, /* Key to use */
1917 dht_replication_level, /* Replication level */
1918 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1919 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1920 sizeof (block), /* Size of the data */
1921 (const char *) &block, /* Data itself */
1922 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1923 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1924 NULL, /* Continuation */
1925 NULL); /* Continuation closure */
1927 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1931 /******************************************************************************/
1932 /****************** GENERAL HELPER FUNCTIONS ************************/
1933 /******************************************************************************/
1936 * Decrements the reference counter and frees all resources if needed
1938 * @param mesh_data Data Descriptor used in a multicast message.
1939 * Freed no longer needed (last message).
1942 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1944 if (0 == --(mesh_data->reference_counter))
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1947 GNUNET_free (mesh_data->data);
1948 GNUNET_free (mesh_data);
1954 * Check if client has registered with the service and has not disconnected
1956 * @param client the client to check
1958 * @return non-NULL if client exists in the global DLL
1960 static struct MeshClient *
1961 client_get (struct GNUNET_SERVER_Client *client)
1963 struct MeshClient *c;
1968 if (c->handle == client)
1977 * Checks if a given client has subscribed to certain message type
1979 * @param message_type Type of message to check
1980 * @param c Client to check
1982 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1984 * FIXME: use of crypto_hash slows it down
1985 * The hash function alone takes 8-10us out of the ~55us for the whole
1986 * process of retransmitting the message from one local client to another.
1987 * Find faster implementation!
1990 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1992 struct GNUNET_HashCode hc;
1994 if (NULL == c->types)
1997 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1998 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2003 * Check whether client wants traffic from a tunnel.
2005 * @param c Client to check.
2006 * @param t Tunnel to be found.
2008 * @return GNUNET_YES if client knows tunnel.
2010 * TODO look in client hashmap
2013 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2017 for (i = 0; i < t->nclients; i++)
2018 if (t->clients[i] == c)
2025 * Check whether client has been informed about a tunnel.
2027 * @param c Client to check.
2028 * @param t Tunnel to be found.
2030 * @return GNUNET_YES if client knows tunnel.
2032 * TODO look in client hashmap
2035 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2039 for (i = 0; i < t->nignore; i++)
2040 if (t->ignore[i] == c)
2042 return client_wants_tunnel(c, t);
2047 * Marks a client as uninterested in traffic from the tunnel, updating both
2048 * client and tunnel to reflect this.
2050 * @param c Client that doesn't want traffic anymore.
2051 * @param t Tunnel which should be ignored.
2053 * FIXME when to delete an incoming tunnel?
2056 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2058 struct GNUNET_HashCode hash;
2060 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2061 GNUNET_break (GNUNET_YES ==
2062 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2064 GNUNET_break (GNUNET_YES ==
2065 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2067 tunnel_delete_active_client (t, c);
2068 GNUNET_array_append (t->ignore, t->nignore, c);
2073 * Deletes a tunnel from a client (either owner or destination). To be used on
2074 * tunnel destroy, otherwise, use client_ignore_tunnel.
2076 * @param c Client whose tunnel to delete.
2077 * @param t Tunnel which should be deleted.
2080 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2082 struct GNUNET_HashCode hash;
2086 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2087 GNUNET_assert (GNUNET_YES ==
2088 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2094 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2096 GNUNET_assert (GNUNET_YES ==
2097 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2101 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2109 * Send the message to all clients that have subscribed to its type
2111 * @param msg Pointer to the message itself
2112 * @param payload Pointer to the payload of the message.
2113 * @param t The tunnel to whose clients this message goes.
2115 * @return number of clients this message was sent to
2118 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2119 const struct GNUNET_MessageHeader *payload,
2120 struct MeshTunnel *t)
2122 struct MeshClient *c;
2123 MESH_TunnelNumber *tid;
2126 char cbuf[htons (msg->size)];
2128 type = ntohs (payload->type);
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2131 GNUNET_MESH_DEBUG_M2S (type));
2133 memcpy (cbuf, msg, sizeof (cbuf));
2134 switch (htons (msg->type))
2136 struct GNUNET_MESH_Unicast *uc;
2137 struct GNUNET_MESH_Multicast *mc;
2138 struct GNUNET_MESH_ToOrigin *to;
2140 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2141 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2144 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2145 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2148 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2149 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2157 for (count = 0, c = clients; c != NULL; c = c->next)
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2160 if (client_is_subscribed (type, c))
2162 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2166 *tid = htonl (t->local_tid);
2170 if (GNUNET_NO == client_knows_tunnel (c, t))
2172 /* This client doesn't know the tunnel */
2173 struct GNUNET_MESH_TunnelNotification tmsg;
2174 struct GNUNET_HashCode hash;
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2177 tmsg.header.size = htons (sizeof (tmsg));
2178 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2179 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2180 tmsg.tunnel_id = htonl (t->local_tid_dest);
2182 if (GNUNET_YES == t->speed_min)
2183 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2184 if (GNUNET_YES == t->nobuffer)
2185 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2186 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2187 &tmsg.header, GNUNET_NO);
2188 tunnel_add_client (t, c);
2189 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2191 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2192 c->incoming_tunnels, &hash, t,
2193 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2195 *tid = htonl (t->local_tid_dest);
2198 /* Check if the client wants to get traffic from the tunnel */
2199 if (GNUNET_NO == client_wants_tunnel(c, t))
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2203 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2204 (struct GNUNET_MessageHeader
2205 *) cbuf, GNUNET_NO);
2214 * Notify the client that owns the tunnel that a peer has connected to it
2215 * (the requested path to it has been confirmed).
2217 * @param t Tunnel whose owner to notify
2218 * @param id Short id of the peer that has connected
2221 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2223 struct GNUNET_MESH_PeerControl pc;
2225 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2226 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2227 pc.tunnel_id = htonl (t->local_tid);
2228 GNUNET_PEER_resolve (id, &pc.peer);
2229 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2235 * Notify all clients (not depending on registration status) that the incoming
2236 * tunnel is no longer valid.
2238 * @param t Tunnel that was destroyed.
2241 send_clients_tunnel_destroy (struct MeshTunnel *t)
2243 struct GNUNET_MESH_TunnelMessage msg;
2245 msg.header.size = htons (sizeof (msg));
2246 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2247 msg.tunnel_id = htonl (t->local_tid_dest);
2248 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2253 * Notify clients of tunnel disconnections, if needed.
2254 * In case the origin disconnects, the destination clients get a tunnel destroy
2255 * notification. If the last destination disconnects (only one remaining client
2256 * in tunnel), the origin gets a (local ID) peer disconnected.
2257 * Note that the function must be called BEFORE removing the client from
2260 * @param t Tunnel that was destroyed.
2261 * @param c Client that disconnected.
2264 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2270 struct GNUNET_MESH_TunnelMessage msg;
2272 msg.header.size = htons (sizeof (msg));
2273 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2274 msg.tunnel_id = htonl (t->local_tid_dest);
2275 for (i = 0; i < t->nclients; i++)
2276 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2277 &msg.header, GNUNET_NO);
2279 // FIXME when to disconnect an incoming tunnel?
2280 else if (1 == t->nclients && NULL != t->owner)
2282 struct GNUNET_MESH_PeerControl msg;
2284 msg.header.size = htons (sizeof (msg));
2285 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2286 msg.tunnel_id = htonl (t->local_tid);
2287 msg.peer = my_full_id;
2288 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2289 &msg.header, GNUNET_NO);
2295 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2296 * and insert it in the appropiate structures if the peer is not known yet.
2298 * @param peer Full identity of the peer.
2300 * @return Existing or newly created peer info.
2302 static struct MeshPeerInfo *
2303 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2305 struct MeshPeerInfo *peer_info;
2307 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2308 if (NULL == peer_info)
2311 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2312 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2314 peer_info->id = GNUNET_PEER_intern (peer);
2322 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2323 * and insert it in the appropiate structures if the peer is not known yet.
2325 * @param peer Short identity of the peer.
2327 * @return Existing or newly created peer info.
2329 static struct MeshPeerInfo *
2330 peer_info_get_short (const GNUNET_PEER_Id peer)
2332 struct GNUNET_PeerIdentity id;
2334 GNUNET_PEER_resolve (peer, &id);
2335 return peer_info_get (&id);
2340 * Iterator to remove the tunnel from the list of tunnels a peer participates
2343 * @param cls Closure (tunnel info)
2344 * @param key GNUNET_PeerIdentity of the peer (unused)
2345 * @param value PeerInfo of the peer
2347 * @return always GNUNET_YES, to keep iterating
2350 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2352 struct MeshTunnel *t = cls;
2353 struct MeshPeerInfo *peer = value;
2356 for (i = 0; i < peer->ntunnels; i++)
2359 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2362 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2363 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2372 * Core callback to write a pre-constructed data packet to core buffer
2374 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2375 * @param size Number of bytes available in buf.
2376 * @param buf Where the to write the message.
2378 * @return number of bytes written to buf
2381 send_core_data_raw (void *cls, size_t size, void *buf)
2383 struct MeshTransmissionDescriptor *info = cls;
2384 struct GNUNET_MessageHeader *msg;
2387 GNUNET_assert (NULL != info);
2388 GNUNET_assert (NULL != info->mesh_data);
2389 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2390 total_size = ntohs (msg->size);
2392 if (total_size > size)
2397 memcpy (buf, msg, total_size);
2398 data_descriptor_decrement_rc (info->mesh_data);
2405 * Sends an already built non-multicast message to a peer,
2406 * properly registrating all used resources.
2408 * @param message Message to send. Function makes a copy of it.
2409 * @param peer Short ID of the neighbor whom to send the message.
2410 * @param t Tunnel on which this message is transmitted.
2413 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2414 const struct GNUNET_PeerIdentity *peer,
2415 struct MeshTunnel *t)
2417 struct MeshTransmissionDescriptor *info;
2418 struct MeshPeerInfo *neighbor;
2419 struct MeshPeerPath *p;
2423 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2425 size = ntohs (message->size);
2426 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2427 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2428 info->mesh_data->data = GNUNET_malloc (size);
2429 memcpy (info->mesh_data->data, message, size);
2430 type = ntohs(message->type);
2433 struct GNUNET_MESH_Unicast *m;
2434 struct GNUNET_MESH_ToOrigin *to;
2436 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2437 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2438 m->ttl = htonl (ntohl (m->ttl) - 1);
2440 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2441 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2443 to->pid = htonl(t->bck_pid);
2445 info->mesh_data->data_len = size;
2446 info->mesh_data->reference_counter = 1;
2447 info->mesh_data->total_out = 1;
2448 neighbor = peer_info_get (peer);
2449 for (p = neighbor->path_head; NULL != p; p = p->next)
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 " %s IS NOT DIRECTLY CONNECTED\n",
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2465 for (p = neighbor->path_head; NULL != p; p = p->next)
2467 struct GNUNET_PeerIdentity debug_id;
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 " path with %u hops through:\n",
2473 for (i = 0; i < p->length; i++)
2475 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2478 i, GNUNET_i2s(&debug_id));
2482 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2483 GNUNET_free (info->mesh_data->data);
2484 GNUNET_free (info->mesh_data);
2488 info->peer = neighbor;
2489 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2500 * Sends a CREATE PATH message for a path to a peer, properly registrating
2501 * all used resources.
2503 * @param peer PeerInfo of the final peer for whom this path is being created.
2504 * @param p Path itself.
2505 * @param t Tunnel for which the path is created.
2508 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2509 struct MeshTunnel *t)
2511 struct GNUNET_PeerIdentity id;
2512 struct MeshPathInfo *path_info;
2513 struct MeshPeerInfo *neighbor;
2519 p = tree_get_path_to_peer (t->tree, peer->id);
2526 for (i = 0; i < p->length; i++)
2528 if (p->peers[i] == myid)
2531 if (i >= p->length - 1)
2537 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2539 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2540 path_info->path = p;
2542 neighbor = peer_info_get (&id);
2543 path_info->peer = neighbor;
2544 queue_add (path_info,
2545 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2546 sizeof (struct GNUNET_MESH_ManipulatePath) +
2547 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2554 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2556 * @param t Tunnel whose path to destroy.
2557 * @param destination Short ID of the peer to whom the path to destroy.
2560 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2562 struct MeshPeerPath *p;
2565 p = tree_get_path_to_peer (t->tree, destination);
2571 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2572 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2574 struct GNUNET_MESH_ManipulatePath *msg;
2575 struct GNUNET_PeerIdentity *pi;
2579 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2580 msg->header.size = htons (size);
2581 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2582 msg->tid = htonl (t->id.tid);
2583 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2584 for (i = 0; i < p->length; i++)
2586 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2588 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2595 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2597 * @param t Tunnel which to confirm.
2600 send_path_ack (struct MeshTunnel *t)
2602 struct MeshTransmissionDescriptor *info;
2603 struct GNUNET_PeerIdentity id;
2604 GNUNET_PEER_Id peer;
2606 peer = tree_get_predecessor (t->tree);
2607 GNUNET_PEER_resolve (peer, &id);
2608 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2609 info->origin = &t->id;
2610 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2611 GNUNET_assert (NULL != info->peer);
2614 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2615 sizeof (struct GNUNET_MESH_PathACK),
2622 * Try to establish a new connection to this peer.
2623 * Use the best path for the given tunnel.
2624 * If the peer doesn't have any path to it yet, try to get one.
2625 * If the peer already has some path, send a CREATE PATH towards it.
2627 * @param peer PeerInfo of the peer.
2628 * @param t Tunnel for which to create the path, if possible.
2631 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2633 struct MeshPeerPath *p;
2634 struct MeshPathInfo *path_info;
2636 if (NULL != peer->path_head)
2638 p = tree_get_path_to_peer (t->tree, peer->id);
2645 // FIXME always send create path to self
2648 send_create_path (peer, p, t);
2652 struct GNUNET_HashCode hash;
2655 send_client_peer_connected (t, myid);
2656 t->local_tid_dest = next_local_tid++;
2657 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2660 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2668 else if (NULL == peer->dhtget)
2670 struct GNUNET_PeerIdentity id;
2672 GNUNET_PEER_resolve (peer->id, &id);
2673 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2674 path_info->peer = peer;
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2678 peer->dhtgetcls = path_info;
2679 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2680 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2681 &id.hashPubKey, /* key to search */
2682 dht_replication_level, /* replication level */
2683 GNUNET_DHT_RO_RECORD_ROUTE |
2684 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2685 NULL, /* xquery */ // FIXME BLOOMFILTER
2686 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2687 &dht_get_id_handler, path_info);
2689 /* Otherwise, there is no path but the DHT get is already started. */
2694 * Task to delay the connection of a peer
2696 * @param cls Closure (path info with tunnel and peer to connect).
2697 * Will be free'd on exection.
2698 * @param tc TaskContext
2701 peer_info_connect_task (void *cls,
2702 const struct GNUNET_SCHEDULER_TaskContext *tc)
2704 struct MeshPathInfo *path_info = cls;
2706 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2708 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2713 peer_info_connect (path_info->peer, path_info->t);
2719 * Destroy the peer_info and free any allocated resources linked to it
2721 * @param pi The peer_info to destroy.
2723 * @return GNUNET_OK on success
2726 peer_info_destroy (struct MeshPeerInfo *pi)
2728 struct GNUNET_PeerIdentity id;
2729 struct MeshPeerPath *p;
2730 struct MeshPeerPath *nextp;
2732 GNUNET_PEER_resolve (pi->id, &id);
2733 GNUNET_PEER_change_rc (pi->id, -1);
2736 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2739 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2740 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2742 if (NULL != pi->dhtget)
2744 GNUNET_DHT_get_stop (pi->dhtget);
2745 GNUNET_free (pi->dhtgetcls);
2751 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2755 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2757 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2765 * Remove all paths that rely on a direct connection between p1 and p2
2766 * from the peer itself and notify all tunnels about it.
2768 * @param peer PeerInfo of affected peer.
2769 * @param p1 GNUNET_PEER_Id of one peer.
2770 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2773 * TODO: optimize (see below)
2776 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2779 struct MeshPeerPath *p;
2780 struct MeshPeerPath *aux;
2781 struct MeshPeerInfo *peer_d;
2783 unsigned int destroyed;
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2790 p = peer->path_head;
2794 for (i = 0; i < (p->length - 1); i++)
2796 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2797 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2799 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2810 for (i = 0; i < peer->ntunnels; i++)
2812 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2816 * Problem: one or more peers have been deleted from the tunnel tree.
2817 * We don't know who they are to try to add them again.
2818 * We need to try to find a new path for each of the disconnected peers.
2819 * Some of them might already have a path to reach them that does not
2820 * involve p1 and p2. Adding all anew might render in a better tree than
2821 * the trivial immediate fix.
2823 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2824 * its children will be reachable trough him.
2826 peer_d = peer_info_get_short (d);
2829 for (p = peer_d->path_head; NULL != p; p = p->next)
2831 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2839 /* No callback, as peer will be already disconnected and a connection
2840 * scheduled by tunnel_notify_connection_broken.
2842 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2846 peer_info_connect (peer_d, peer->tunnels[i]);
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2854 * Add the path to the peer and update the path used to reach it in case this
2857 * @param peer_info Destination peer to add the path to.
2858 * @param path New path to add. Last peer must be the peer in arg 1.
2859 * Path will be either used of freed if already known.
2860 * @param trusted Do we trust that this path is real?
2863 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2866 struct MeshPeerPath *aux;
2870 if ((NULL == peer_info) || (NULL == path))
2873 path_destroy (path);
2876 if (path->peers[path->length - 1] != peer_info->id)
2879 path_destroy (path);
2882 if (path->length <= 2 && GNUNET_NO == trusted)
2884 /* Only allow CORE to tell us about direct paths */
2885 path_destroy (path);
2888 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2889 for (l = 1; l < path->length; l++)
2891 if (path->peers[l] == myid)
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2894 for (l2 = 0; l2 < path->length - l; l2++)
2896 path->peers[l2] = path->peers[l + l2];
2901 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2906 struct GNUNET_PeerIdentity id;
2908 GNUNET_PEER_resolve (peer_info->id, &id);
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2910 path->length, GNUNET_i2s (&id));
2913 l = path_get_length (path);
2920 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2921 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2923 l2 = path_get_length (aux);
2926 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2927 peer_info->path_tail, aux, path);
2932 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2934 path_destroy (path);
2939 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2946 * Add the path to the origin peer and update the path used to reach it in case
2947 * this is the shortest.
2948 * The path is given in peer_info -> destination, therefore we turn the path
2949 * upside down first.
2951 * @param peer_info Peer to add the path to, being the origin of the path.
2952 * @param path New path to add after being inversed.
2953 * @param trusted Do we trust that this path is real?
2956 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2957 struct MeshPeerPath *path, int trusted)
2960 peer_info_add_path (peer_info, path, trusted);
2965 * Function called if the connection to the peer has been stalled for a while,
2966 * possibly due to a missed ACK. Poll the peer about its ACK status.
2968 * @param cls Closure (info about regex search).
2969 * @param tc TaskContext.
2972 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2974 struct MeshTunnelChildInfo *cinfo = cls;
2975 struct GNUNET_MESH_Poll msg;
2976 struct GNUNET_PeerIdentity id;
2977 struct MeshTunnel *t;
2979 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2980 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2986 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2987 msg.header.size = htons (sizeof (msg));
2988 msg.tid = htonl (t->id.tid);
2989 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2990 msg.last_ack = htonl (cinfo->fwd_ack);
2992 GNUNET_PEER_resolve (cinfo->id, &id);
2993 send_prebuilt_message (&msg.header, &id, cinfo->t);
2994 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2995 &tunnel_poll, cinfo);
3000 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3001 * to obtain a local peer -> destination path and interning the peer ids.
3003 * @return Newly allocated and created path
3005 static struct MeshPeerPath *
3006 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3007 unsigned int get_path_length,
3008 const struct GNUNET_PeerIdentity *put_path,
3009 unsigned int put_path_length)
3011 struct MeshPeerPath *p;
3017 GNUNET_PEER_change_rc (myid, 1);
3018 i = get_path_length;
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3020 for (i--; i >= 0; i--)
3022 id = GNUNET_PEER_intern (&get_path[i]);
3023 if (p->length > 0 && id == p->peers[p->length - 1])
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3026 GNUNET_PEER_change_rc (id, -1);
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3031 GNUNET_i2s (&get_path[i]));
3033 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3034 p->peers[p->length - 1] = id;
3037 i = put_path_length;
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3039 for (i--; i >= 0; i--)
3041 id = GNUNET_PEER_intern (&put_path[i]);
3044 /* PUT path went through us, so discard the path up until now and start
3045 * from here to get a much shorter (and loop-free) path.
3050 if (p->length > 0 && id == p->peers[p->length - 1])
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3053 GNUNET_PEER_change_rc (id, -1);
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3058 GNUNET_i2s (&put_path[i]));
3060 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3061 p->peers[p->length - 1] = id;
3065 if (get_path_length > 0)
3066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3067 GNUNET_i2s (&get_path[0]));
3068 if (put_path_length > 0)
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3070 GNUNET_i2s (&put_path[0]));
3071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3073 for (i = 0; i < p->length; i++)
3075 struct GNUNET_PeerIdentity peer_id;
3077 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3079 GNUNET_i2s (&peer_id));
3087 * Adds a path to the peer_infos of all the peers in the path
3089 * @param p Path to process.
3090 * @param confirmed Whether we know if the path works or not.
3093 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3097 /* TODO: invert and add */
3098 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3099 for (i++; i < p->length; i++)
3101 struct MeshPeerInfo *aux;
3102 struct MeshPeerPath *copy;
3104 aux = peer_info_get_short (p->peers[i]);
3105 copy = path_duplicate (p);
3106 copy->length = i + 1;
3107 peer_info_add_path (aux, copy, GNUNET_NO);
3113 * Send keepalive packets for a peer
3115 * @param cls Closure (tunnel for which to send the keepalive).
3116 * @param tc Notification context.
3118 * TODO: implement explicit multicast keepalive?
3121 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3125 * Search for a tunnel among the incoming tunnels
3127 * @param tid the local id of the tunnel
3129 * @return tunnel handler, NULL if doesn't exist
3131 static struct MeshTunnel *
3132 tunnel_get_incoming (MESH_TunnelNumber tid)
3134 struct GNUNET_HashCode hash;
3136 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3137 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3138 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3143 * Search for a tunnel among the tunnels for a client
3145 * @param c the client whose tunnels to search in
3146 * @param tid the local id of the tunnel
3148 * @return tunnel handler, NULL if doesn't exist
3150 static struct MeshTunnel *
3151 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3153 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3155 return tunnel_get_incoming (tid);
3159 struct GNUNET_HashCode hash;
3161 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3162 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3168 * Search for a tunnel by global ID using PEER_ID
3170 * @param pi owner of the tunnel
3171 * @param tid global tunnel number
3173 * @return tunnel handler, NULL if doesn't exist
3175 static struct MeshTunnel *
3176 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3178 struct MESH_TunnelID id;
3179 struct GNUNET_HashCode hash;
3184 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3185 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3190 * Search for a tunnel by global ID using full PeerIdentities
3192 * @param oid owner of the tunnel
3193 * @param tid global tunnel number
3195 * @return tunnel handler, NULL if doesn't exist
3197 static struct MeshTunnel *
3198 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3200 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3205 * Delete an active client from the tunnel.
3211 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3215 for (i = 0; i < t->nclients; i++)
3217 if (t->clients[i] == c)
3219 t->clients[i] = t->clients[t->nclients - 1];
3220 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3221 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3223 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3231 * Delete an ignored client from the tunnel.
3237 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3241 for (i = 0; i < t->nignore; i++)
3243 if (t->ignore[i] == c)
3245 t->ignore[i] = t->ignore[t->nignore - 1];
3246 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3254 * Delete a client from the tunnel. It should be only done on
3255 * client disconnection, otherwise use client_ignore_tunnel.
3261 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3263 tunnel_delete_ignored_client (t, c);
3264 tunnel_delete_active_client (t, c);
3269 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3271 * Destroys queue elements of all waiting transmissions and frees all memory
3272 * used by the struct and its elements.
3274 * @param cls Closure (tunnel info).
3275 * @param key Hash of GNUNET_PEER_Id (unused).
3276 * @param value MeshTunnelChildInfo of the child.
3278 * @return always GNUNET_YES, to keep iterating
3281 tunnel_destroy_child (void *cls,
3282 const struct GNUNET_HashCode * key,
3285 struct MeshTunnelChildInfo *cinfo = value;
3286 struct MeshTunnel *t = cls;
3287 struct MeshPeerQueue *q;
3291 for (c = 0; c < cinfo->send_buffer_n; c++)
3293 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3294 q = cinfo->send_buffer[i];
3295 cinfo->send_buffer[i] = NULL;
3297 queue_destroy (q, GNUNET_YES);
3300 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3302 GNUNET_free_non_null (cinfo->send_buffer);
3303 GNUNET_free (cinfo);
3309 * Callback used to notify a client owner of a tunnel that a peer has
3310 * disconnected, most likely because of a path change.
3312 * @param cls Closure (tunnel this notification is about).
3313 * @param peer_id Short ID of disconnected peer.
3316 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3318 struct MeshTunnel *t = cls;
3319 struct MeshPeerInfo *peer;
3320 struct MeshPathInfo *path_info;
3322 if (NULL != t->owner && NULL != nc)
3324 struct GNUNET_MESH_PeerControl msg;
3326 msg.header.size = htons (sizeof (msg));
3327 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3328 msg.tunnel_id = htonl (t->local_tid);
3329 GNUNET_PEER_resolve (peer_id, &msg.peer);
3330 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3331 &msg.header, GNUNET_NO);
3333 peer = peer_info_get_short (peer_id);
3334 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3335 path_info->peer = peer;
3337 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3343 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3344 * needed rescources.
3345 * If peer already exists, reevaluate shortest path and change if different.
3347 * @param t Tunnel we want to add a new peer to
3348 * @param peer PeerInfo of the peer being added
3352 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3354 struct GNUNET_PeerIdentity id;
3355 struct MeshPeerPath *best_p;
3356 struct MeshPeerPath *p;
3357 unsigned int best_cost;
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3361 GNUNET_PEER_resolve (peer->id, &id);
3363 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3366 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3367 GNUNET_assert (GNUNET_OK ==
3368 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3373 if (NULL != (p = peer->path_head))
3376 best_cost = tree_get_path_cost (t->tree, p);
3379 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3386 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3387 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3388 t->path_refresh_task =
3389 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3393 /* Start a DHT get */
3394 peer_info_connect (peer, t);
3396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3400 * Add a path to a tunnel which we don't own, just to remember the next hop.
3401 * If destination node was already in the tunnel, the first hop information
3402 * will be replaced with the new path.
3404 * @param t Tunnel we want to add a new peer to
3405 * @param p Path to add
3406 * @param own_pos Position of local node in path.
3410 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3411 unsigned int own_pos)
3413 struct GNUNET_PeerIdentity id;
3415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3416 GNUNET_assert (0 != own_pos);
3417 tree_add_path (t->tree, p, NULL, NULL);
3418 if (own_pos < p->length - 1)
3420 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3421 tree_update_first_hops (t->tree, myid, &id);
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3427 * Add a client to a tunnel, initializing all needed data structures.
3429 * @param t Tunnel to which add the client.
3430 * @param c Client which to add to the tunnel.
3433 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3435 struct MeshTunnelClientInfo clinfo;
3437 GNUNET_array_append (t->clients, t->nclients, c);
3438 clinfo.fwd_ack = t->fwd_pid + 1;
3439 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3440 clinfo.fwd_pid = t->fwd_pid;
3441 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3443 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3448 * Notifies a tunnel that a connection has broken that affects at least
3449 * some of its peers. Sends a notification towards the root of the tree.
3450 * In case the peer is the owner of the tree, notifies the client that owns
3451 * the tunnel and tries to reconnect.
3453 * @param t Tunnel affected.
3454 * @param p1 Peer that got disconnected from p2.
3455 * @param p2 Peer that got disconnected from p1.
3457 * @return Short ID of the peer disconnected (either p1 or p2).
3458 * 0 if the tunnel remained unaffected.
3460 static GNUNET_PEER_Id
3461 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3467 tree_notify_connection_broken (t->tree, p1, p2,
3468 &tunnel_notify_client_peer_disconnected,
3470 if (myid != p1 && myid != p2)
3476 if (tree_get_predecessor (t->tree) != 0)
3478 /* We are the peer still connected, notify owner of the disconnection. */
3479 struct GNUNET_MESH_PathBroken msg;
3480 struct GNUNET_PeerIdentity neighbor;
3482 msg.header.size = htons (sizeof (msg));
3483 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3484 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3485 msg.tid = htonl (t->id.tid);
3486 msg.peer1 = my_full_id;
3487 GNUNET_PEER_resolve (pid, &msg.peer2);
3488 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3489 send_prebuilt_message (&msg.header, &neighbor, t);
3497 * Send a multicast packet to a neighbor.
3499 * @param cls Closure (Info about the multicast packet)
3500 * @param neighbor_id Short ID of the neighbor to send the packet to.
3503 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3505 struct MeshData *mdata = cls;
3506 struct MeshTransmissionDescriptor *info;
3507 struct GNUNET_PeerIdentity neighbor;
3508 struct GNUNET_MessageHeader *msg;
3510 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3512 info->mesh_data = mdata;
3513 (mdata->reference_counter) ++;
3514 info->destination = neighbor_id;
3515 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3517 GNUNET_i2s (&neighbor));
3518 info->peer = peer_info_get (&neighbor);
3519 GNUNET_assert (NULL != info->peer);
3520 msg = (struct GNUNET_MessageHeader *) mdata->data;
3523 info->mesh_data->data_len,
3530 * Queue a message in a tunnel in multicast, sending a copy to each child node
3531 * down the local one in the tunnel tree.
3533 * @param t Tunnel in which to send the data.
3534 * @param msg Message to be sent.
3537 tunnel_send_multicast (struct MeshTunnel *t,
3538 const struct GNUNET_MessageHeader *msg)
3540 struct MeshData *mdata;
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3543 " sending a multicast packet...\n");
3545 mdata = GNUNET_malloc (sizeof (struct MeshData));
3546 mdata->data_len = ntohs (msg->size);
3548 mdata->data = GNUNET_malloc (mdata->data_len);
3549 memcpy (mdata->data, msg, mdata->data_len);
3550 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3552 struct GNUNET_MESH_Multicast *mcast;
3554 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3555 if (t->fwd_queue_n >= t->fwd_queue_max)
3558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3560 " message from %s!\n",
3561 GNUNET_i2s(&mcast->oid));
3562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3563 " message at %s!\n",
3564 GNUNET_i2s(&my_full_id));
3565 GNUNET_free (mdata->data);
3566 GNUNET_free (mdata);
3570 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3572 ntohl (mcast->ttl));
3576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3579 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3580 if (mdata->reference_counter == 0)
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3583 " no one to send data to\n");
3584 GNUNET_free (mdata->data);
3585 GNUNET_free (mdata);
3590 mdata->total_out = mdata->reference_counter;
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 " sending a multicast packet done\n");
3599 * Increase the SKIP value of all peers that
3600 * have not received a unicast message.
3602 * @param cls Closure (ID of the peer that HAS received the message).
3603 * @param key ID of the neighbor.
3604 * @param value Information about the neighbor.
3606 * @return GNUNET_YES to keep iterating.
3609 tunnel_add_skip (void *cls,
3610 const struct GNUNET_HashCode * key,
3613 struct GNUNET_PeerIdentity *neighbor = cls;
3614 struct MeshTunnelChildInfo *cinfo = value;
3616 /* TODO compare only pointers? key == neighbor? */
3617 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3627 * @brief Get neighbor's Flow Control information.
3629 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3630 * descendant of the local node in a certain tunnel.
3631 * If the info is not yet there (recently created path), creates the data struct
3632 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3635 * @param t Tunnel related.
3636 * @param peer Neighbor whose Flow Control info is needed.
3638 * @return Neighbor's Flow Control info.
3640 static struct MeshTunnelChildInfo *
3641 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3642 const struct GNUNET_PeerIdentity *peer)
3644 struct MeshTunnelChildInfo *cinfo;
3646 if (NULL == t->children_fc)
3649 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3655 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3656 cinfo->id = GNUNET_PEER_intern (peer);
3657 cinfo->skip = t->fwd_pid;
3660 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3661 cinfo->fwd_ack = t->fwd_pid + delta;
3662 cinfo->bck_ack = delta;
3663 cinfo->bck_pid = -1;
3665 cinfo->send_buffer =
3666 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3668 GNUNET_assert (GNUNET_OK ==
3669 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3672 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3679 * Get the Flow Control info of a client.
3681 * @param t Tunnel on which to look.
3682 * @param c Client whose ACK to get.
3684 * @return ACK value.
3686 static struct MeshTunnelClientInfo *
3687 tunnel_get_client_fc (struct MeshTunnel *t,
3688 struct MeshClient *c)
3692 for (i = 0; i < t->nclients; i++)
3694 if (t->clients[i] != c)
3696 return &t->clients_fc[i];
3699 return NULL; // avoid compiler / coverity complaints
3704 * Iterator to get the appropiate ACK value from all children nodes.
3706 * @param cls Closue (tunnel).
3707 * @param id Id of the child node.
3710 tunnel_get_child_fwd_ack (void *cls,
3713 struct GNUNET_PeerIdentity peer_id;
3714 struct MeshTunnelChildInfo *cinfo;
3715 struct MeshTunnelChildIteratorContext *ctx = cls;
3716 struct MeshTunnel *t = ctx->t;
3719 GNUNET_PEER_resolve (id, &peer_id);
3720 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3721 ack = cinfo->fwd_ack;
3724 if (GNUNET_NO == ctx->init)
3726 ctx->max_child_ack = ack;
3727 ctx->init = GNUNET_YES;
3730 if (GNUNET_YES == t->speed_min)
3732 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3736 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3743 * Get the maximum PID allowed to transmit to any
3744 * tunnel child of the local peer, depending on the tunnel
3745 * buffering/speed settings.
3749 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3752 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3754 struct MeshTunnelChildIteratorContext ctx;
3756 ctx.max_child_ack = 0;
3758 ctx.init = GNUNET_NO;
3759 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3761 if (0 == ctx.nchildren)
3763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3764 " tunnel has no children, no FWD ACK\n");
3768 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3769 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3771 return (int64_t) ctx.max_child_ack;
3776 * Set the FWD ACK value of a client in a particular tunnel.
3778 * @param t Tunnel affected.
3779 * @param c Client whose ACK to set.
3780 * @param ack ACK value.
3783 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3784 struct MeshClient *c,
3789 for (i = 0; i < t->nclients; i++)
3791 if (t->clients[i] != c)
3793 t->clients_fc[i].fwd_ack = ack;
3801 * Get the highest ACK value of all clients in a particular tunnel,
3802 * according to the buffering/speed settings.
3804 * @param t Tunnel on which to look.
3806 * @return Corresponding ACK value (max uint32_t).
3807 * If no clients are suscribed, -1LL.
3810 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3815 if (0 == t->nclients)
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3818 " tunnel has no clients, no FWD ACK\n");
3822 for (ack = -1LL, i = 0; i < t->nclients; i++)
3825 (GNUNET_YES == t->speed_min &&
3826 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3827 (GNUNET_NO == t->speed_min &&
3828 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3830 ack = t->clients_fc[i].fwd_ack;
3834 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3835 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3837 return (uint32_t) ack;
3842 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3843 * mode and the status of all children nodes.
3847 * @return Maximum PID allowed.
3850 tunnel_get_fwd_ack (struct MeshTunnel *t)
3854 uint32_t buffer_free;
3858 count = t->fwd_pid - t->skip;
3859 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3860 child_ack = tunnel_get_children_fwd_ack (t);
3861 client_ack = tunnel_get_clients_fwd_ack (t);
3862 if (GNUNET_YES == t->nobuffer)
3865 if (-1LL == child_ack)
3866 child_ack = client_ack;
3867 if (-1LL == child_ack)
3870 client_ack = child_ack = ack;
3875 ack = count + buffer_free; // Overflow? OK!
3877 if (-1LL == child_ack)
3879 // Node has no children, child_ack AND core buffer are irrelevant.
3880 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3881 return (uint32_t) client_ack;
3883 if (-1LL == client_ack)
3887 if (GNUNET_YES == t->speed_min)
3889 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3890 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3894 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3895 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3899 count, buffer_free, child_ack, client_ack, ack);
3905 * Build a local ACK message and send it to a local client.
3907 * @param t Tunnel on which to send the ACK.
3908 * @param c Client to whom send the ACK.
3909 * @param ack Value of the ACK.
3912 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3914 struct GNUNET_MESH_LocalAck msg;
3916 msg.header.size = htons (sizeof (msg));
3917 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3918 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3919 msg.max_pid = htonl (ack);
3920 GNUNET_SERVER_notification_context_unicast(nc,
3927 * Build an ACK message and queue it to send to the given peer.
3929 * @param t Tunnel on which to send the ACK.
3930 * @param peer Peer to whom send the ACK.
3931 * @param ack Value of the ACK.
3934 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3936 struct GNUNET_MESH_ACK msg;
3938 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3939 msg.header.size = htons (sizeof (msg));
3940 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3941 msg.pid = htonl (ack);
3942 msg.tid = htonl (t->id.tid);
3944 send_prebuilt_message (&msg.header, peer, t);
3949 * Notify a the owner of a tunnel about how many more
3950 * payload packages will we accept on a given tunnel.
3952 * @param t Tunnel on which to send the ACK.
3955 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3960 "Sending client FWD ACK on tunnel %X\n",
3963 ack = tunnel_get_fwd_ack (t);
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3966 if (t->last_fwd_ack == ack)
3968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3973 t->last_fwd_ack = ack;
3974 send_local_ack (t, t->owner, ack);
3979 * Send an ACK informing the predecessor about the available buffer space.
3980 * In case there is no predecessor, inform the owning client.
3981 * If buffering is off, send only on behalf of children or self if endpoint.
3982 * If buffering is on, send when sent to children and buffer space is free.
3983 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3984 * the ACK itself goes "back" (towards root).
3986 * @param t Tunnel on which to send the ACK.
3987 * @param type Type of message that triggered the ACK transmission.
3990 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3992 struct GNUNET_PeerIdentity id;
3995 if (NULL != t->owner)
3997 tunnel_send_client_fwd_ack (t);
4000 /* Is it after unicast / multicast retransmission? */
4003 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4004 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 "ACK due to FWD DATA retransmission\n");
4007 if (GNUNET_YES == t->nobuffer)
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4013 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4014 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4016 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4017 t->force_ack = GNUNET_YES;
4023 /* Check if we need to transmit the ACK */
4024 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4025 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4026 GNUNET_NO == t->force_ack)
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4030 " t->qmax: %u, t->qn: %u\n",
4031 t->fwd_queue_max, t->fwd_queue_n);
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4033 " t->pid: %u, t->ack: %u\n",
4034 t->fwd_pid, t->last_fwd_ack);
4038 /* Ok, ACK might be necessary, what PID to ACK? */
4039 ack = tunnel_get_fwd_ack (t);
4041 /* If speed_min and not all children have ack'd, dont send yet */
4042 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4048 t->last_fwd_ack = ack;
4049 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4050 send_ack (t, &id, ack);
4052 t->force_ack = GNUNET_NO;
4057 * Iterator to send a child node a BCK ACK to allow him to send more
4060 * @param cls Closure (tunnel).
4061 * @param id Id of the child node.
4064 tunnel_send_child_bck_ack (void *cls,
4067 struct MeshTunnel *t = cls;
4068 struct MeshTunnelChildInfo *cinfo;
4069 struct GNUNET_PeerIdentity peer;
4072 GNUNET_PEER_resolve (id, &peer);
4073 cinfo = tunnel_get_neighbor_fc (t, &peer);
4074 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4076 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4079 " Not sending ACK, not needed\n");
4082 cinfo->bck_ack = ack;
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4085 " Sending BCK ACK %u (last sent: %u)\n",
4086 ack, cinfo->bck_ack);
4087 send_ack (t, &peer, ack);
4092 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4094 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4096 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4097 * - implement a size 1 buffer in each client_fc AND children_fc
4098 * to hold at least 1 message per "child".
4099 * problem: violates no buffer policy
4100 * - ack 0 and make "children" poll for transmission slots
4101 * problem: big overhead, extra latency even in low traffic
4104 * @param t Tunnel on which to send the BCK ACKs.
4107 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4110 unsigned int tunnel_delta;
4112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4114 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4117 /* Find client whom to allow to send to origin (with lowest buffer space) */
4118 for (i = 0; i < t->nclients; i++)
4120 struct MeshTunnelClientInfo *clinfo;
4123 clinfo = &t->clients_fc[i];
4124 delta = clinfo->bck_ack - clinfo->bck_pid;
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4126 t->clients[i]->id, delta);
4128 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4129 (GNUNET_YES == t->nobuffer && 0 == delta))
4133 ack = clinfo->bck_pid;
4134 ack += t->nobuffer ? 1 : tunnel_delta;
4135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4136 " sending ack to client %u: %u\n",
4137 t->clients[i]->id, ack);
4138 send_local_ack (t, t->clients[i], ack);
4139 clinfo->bck_ack = ack;
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 " not sending ack to client %u (td %u, d %u)\n",
4145 t->clients[i]->id, tunnel_delta, delta);
4152 * Send an ACK informing the children nodes and destination clients about
4153 * the available buffer space.
4154 * If buffering is off, send only on behalf of root (can be self).
4155 * If buffering is on, send when sent to predecessor and buffer space is free.
4156 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4157 * the ACK itself goes "forward" (towards children/clients).
4159 * @param t Tunnel on which to send the ACK.
4160 * @param type Type of message that triggered the ACK transmission.
4163 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4166 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4167 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4168 /* Is it after data to_origin retransmission? */
4171 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4172 if (GNUNET_YES == t->nobuffer)
4174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4175 " Not sending ACK, nobuffer\n");
4179 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4180 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4182 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4183 t->force_ack = GNUNET_YES;
4189 tunnel_send_clients_bck_ack (t);
4190 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4191 t->force_ack = GNUNET_NO;
4196 * @brief Re-initiate traffic to this peer if necessary.
4198 * Check if there is traffic queued towards this peer
4199 * and the core transmit handle is NULL (traffic was stalled).
4200 * If so, call core tmt rdy.
4202 * @param cls Closure (unused)
4203 * @param peer_id Short ID of peer to which initiate traffic.
4206 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4208 struct MeshPeerInfo *peer;
4209 struct GNUNET_PeerIdentity id;
4210 struct MeshPeerQueue *q;
4213 peer = peer_info_get_short(peer_id);
4214 if (NULL != peer->core_transmit)
4217 q = queue_get_next(peer);
4220 /* Might br multicast traffic already sent to this particular peer but
4221 * not to other children in this tunnel.
4222 * This way t->queue_n would be > 0 but the queue of this particular peer
4228 GNUNET_PEER_resolve (peer->id, &id);
4229 peer->core_transmit =
4230 GNUNET_CORE_notify_transmit_ready(core_handle,
4233 GNUNET_TIME_UNIT_FOREVER_REL,
4243 * @brief Allow transmission of FWD traffic on this tunnel
4245 * Check if there is traffic queued towards any children
4246 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4248 * @param t Tunnel on which to unlock FWD traffic.
4251 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4253 if (0 == t->fwd_queue_n)
4256 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4261 * @brief Allow transmission of BCK traffic on this tunnel
4263 * Check if there is traffic queued towards the root of the tree
4264 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4266 * @param t Tunnel on which to unlock BCK traffic.
4269 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4271 if (0 == t->bck_queue_n)
4274 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4279 * Send a message to all peers in this tunnel that the tunnel is no longer
4282 * @param t The tunnel whose peers to notify.
4285 tunnel_send_destroy (struct MeshTunnel *t)
4287 struct GNUNET_MESH_TunnelDestroy msg;
4289 msg.header.size = htons (sizeof (msg));
4290 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4291 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4292 msg.tid = htonl (t->id.tid);
4293 tunnel_send_multicast (t, &msg.header);
4298 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4300 * @param cls Closure (Tunnel which to cancel).
4301 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4304 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4306 struct MeshTunnel *t = cls;
4307 struct MeshPeerInfo *peer_info;
4308 struct MeshPeerQueue *pq;
4309 struct MeshPeerQueue *next;
4311 peer_info = peer_info_get_short (neighbor_id);
4312 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4315 if (pq->tunnel == t)
4317 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4318 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4319 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4321 // Should have been removed on destroy children
4324 queue_destroy (pq, GNUNET_YES);
4327 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4329 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4330 peer_info->core_transmit = NULL;
4335 * Destroy the tunnel and free any allocated resources linked to it.
4337 * @param t the tunnel to destroy
4339 * @return GNUNET_OK on success
4342 tunnel_destroy (struct MeshTunnel *t)
4344 struct MeshClient *c;
4345 struct GNUNET_HashCode hash;
4356 struct GNUNET_PeerIdentity id;
4358 GNUNET_PEER_resolve (t->id.oid, &id);
4359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4360 GNUNET_i2s (&id), t->id.tid);
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4366 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4367 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4375 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4377 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4384 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4385 for (i = 0; i < t->nclients; i++)
4389 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4395 for (i = 0; i < t->nignore; i++)
4399 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4406 if (t->nclients > 0)
4409 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4414 GNUNET_free (t->clients);
4415 GNUNET_free (t->clients_fc);
4418 if (NULL != t->peers)
4420 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4422 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4425 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4426 &tunnel_destroy_child,
4428 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4429 t->children_fc = NULL;
4431 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4432 tree_destroy (t->tree);
4434 if (NULL != t->regex_ctx)
4435 regex_cancel_search (t->regex_ctx);
4436 if (NULL != t->dht_get_type)
4437 GNUNET_DHT_get_stop (t->dht_get_type);
4438 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4439 GNUNET_SCHEDULER_cancel (t->timeout_task);
4440 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4441 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4444 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4451 * Create a new tunnel
4453 * @param owner Who is the owner of the tunnel (short ID).
4454 * @param tid Tunnel Number of the tunnel.
4455 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4456 * @param local Tunnel Number for the tunnel, for the client point of view.
4458 * @return A new initialized tunnel. NULL on error.
4460 static struct MeshTunnel *
4461 tunnel_new (GNUNET_PEER_Id owner,
4462 MESH_TunnelNumber tid,
4463 struct MeshClient *client,
4464 MESH_TunnelNumber local)
4466 struct MeshTunnel *t;
4467 struct GNUNET_HashCode hash;
4469 if (n_tunnels >= max_tunnels && NULL == client)
4472 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4475 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4476 t->bck_queue_max = t->fwd_queue_max;
4477 t->tree = tree_new (owner);
4479 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4480 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4481 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4482 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4483 t->local_tid = local;
4484 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4486 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4488 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4490 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4491 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4498 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4505 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4507 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4508 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4512 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4522 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4523 * that are no longer needed, as well as nodes of no longer reachable peers.
4524 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4526 * @param t Tunnel from which to remove the path.
4527 * @param peer Short id of the peer which should be removed.
4530 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4534 r = tree_del_peer (t->tree, peer, NULL, NULL);
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4538 "Tunnel %u [%u] has no more nodes\n",
4539 t->id.oid, t->id.tid);
4545 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4546 * client when the client disconnects. If the client is not the owner, the
4547 * owner will get notified if no more clients are in the tunnel and the client
4548 * get removed from the tunnel's list.
4550 * @param cls closure (client that is disconnecting)
4551 * @param key the hash of the local tunnel id (used to access the hashmap)
4552 * @param value the value stored at the key (tunnel to destroy)
4554 * @return GNUNET_OK, keep iterating.
4557 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4559 struct MeshTunnel *t = value;
4560 struct MeshClient *c = cls;
4562 send_client_tunnel_disconnect(t, c);
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4567 tunnel_delete_client(t, c);
4568 client_delete_tunnel(c, t);
4571 tunnel_send_destroy(t);
4573 t->destroy = GNUNET_YES;
4580 * Timeout function, destroys tunnel if called
4582 * @param cls Closure (tunnel to destroy).
4583 * @param tc TaskContext
4586 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4588 struct MeshTunnel *t = cls;
4589 struct GNUNET_PeerIdentity id;
4591 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4594 GNUNET_PEER_resolve(t->id.oid, &id);
4595 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4596 "Tunnel %s [%X] timed out. Destroying.\n",
4597 GNUNET_i2s(&id), t->id.tid);
4598 send_clients_tunnel_destroy (t);
4603 * Resets the tunnel timeout. Starts it if no timeout was running.
4605 * @param t Tunnel whose timeout to reset.
4607 * TODO use heap to improve efficiency of scheduler.
4610 tunnel_reset_timeout (struct MeshTunnel *t)
4612 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4613 GNUNET_SCHEDULER_cancel (t->timeout_task);
4615 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4616 (refresh_path_time, 4), &tunnel_timeout, t);
4620 /******************************************************************************/
4621 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4622 /******************************************************************************/
4625 * Function to send a create path packet to a peer.
4627 * @param cls closure
4628 * @param size number of bytes available in buf
4629 * @param buf where the callee should write the message
4630 * @return number of bytes written to buf
4633 send_core_path_create (void *cls, size_t size, void *buf)
4635 struct MeshPathInfo *info = cls;
4636 struct GNUNET_MESH_ManipulatePath *msg;
4637 struct GNUNET_PeerIdentity *peer_ptr;
4638 struct MeshTunnel *t = info->t;
4639 struct MeshPeerPath *p = info->path;
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4646 sizeof (struct GNUNET_MESH_ManipulatePath) +
4647 p->length * sizeof (struct GNUNET_PeerIdentity);
4649 if (size < size_needed || NULL == buf)
4654 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4655 msg->header.size = htons (size_needed);
4656 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4657 msg->tid = ntohl (t->id.tid);
4660 if (GNUNET_YES == t->speed_min)
4661 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4662 if (GNUNET_YES == t->nobuffer)
4663 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4664 msg->opt = htonl(opt);
4667 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4668 for (i = 0; i < p->length; i++)
4670 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4677 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4683 * Fill the core buffer
4685 * @param cls closure (data itself)
4686 * @param size number of bytes available in buf
4687 * @param buf where the callee should write the message
4689 * @return number of bytes written to buf
4692 send_core_data_multicast (void *cls, size_t size, void *buf)
4694 struct MeshTransmissionDescriptor *info = cls;
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4698 GNUNET_assert (NULL != info);
4699 GNUNET_assert (NULL != info->peer);
4700 total_size = info->mesh_data->data_len;
4701 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4703 if (total_size > size)
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4709 memcpy (buf, info->mesh_data->data, total_size);
4712 struct GNUNET_MESH_Multicast *mc;
4713 struct GNUNET_MessageHeader *mh;
4716 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4718 mc = (struct GNUNET_MESH_Multicast *) mh;
4719 mh = (struct GNUNET_MessageHeader *) &mc[1];
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4721 " multicast, payload type %s\n",
4722 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4724 " multicast, payload size %u\n", ntohs (mh->size));
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4729 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4733 data_descriptor_decrement_rc (info->mesh_data);
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4742 * Creates a path ack message in buf and frees all unused resources.
4744 * @param cls closure (MeshTransmissionDescriptor)
4745 * @param size number of bytes available in buf
4746 * @param buf where the callee should write the message
4747 * @return number of bytes written to buf
4750 send_core_path_ack (void *cls, size_t size, void *buf)
4752 struct MeshTransmissionDescriptor *info = cls;
4753 struct GNUNET_MESH_PathACK *msg = buf;
4755 GNUNET_assert (NULL != info);
4756 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4761 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4762 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4763 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4764 msg->tid = htonl (info->origin->tid);
4765 msg->peer_id = my_full_id;
4768 /* TODO add signature */
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4771 return sizeof (struct GNUNET_MESH_PathACK);
4776 * Free a transmission that was already queued with all resources
4777 * associated to the request.
4779 * @param queue Queue handler to cancel.
4780 * @param clear_cls Is it necessary to free associated cls?
4783 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4785 struct MeshTransmissionDescriptor *dd;
4786 struct MeshPathInfo *path_info;
4787 struct MeshTunnelChildInfo *cinfo;
4788 struct GNUNET_PeerIdentity id;
4792 if (GNUNET_YES == clear_cls)
4794 switch (queue->type)
4796 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4797 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4798 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4800 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4801 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4802 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4803 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4804 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4805 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4807 " prebuilt message\n");
4808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4810 GNUNET_MESH_DEBUG_M2S(queue->type));
4812 data_descriptor_decrement_rc (dd->mesh_data);
4814 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4816 path_info = queue->cls;
4817 path_destroy (path_info->path);
4821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4822 " type %s unknown!\n",
4823 GNUNET_MESH_DEBUG_M2S(queue->type));
4825 GNUNET_free_non_null (queue->cls);
4827 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4828 queue->peer->queue_tail,
4831 /* Delete from child_fc in the appropiate tunnel */
4832 max = queue->tunnel->fwd_queue_max;
4833 GNUNET_PEER_resolve (queue->peer->id, &id);
4834 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4837 for (i = 0; i < cinfo->send_buffer_n; i++)
4840 i2 = (cinfo->send_buffer_start + i) % max;
4841 if (cinfo->send_buffer[i2] == queue)
4843 /* Found corresponding entry in the send_buffer. Move all others back. */
4848 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4850 j2 = (cinfo->send_buffer_start + j) % max;
4851 j3 = (cinfo->send_buffer_start + j + 1) % max;
4852 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4855 cinfo->send_buffer[j3] = NULL;
4856 cinfo->send_buffer_n--;
4861 GNUNET_free (queue);
4866 * @brief Get the next transmittable message from the queue.
4868 * This will be the head, except in the case of being a data packet
4869 * not allowed by the destination peer.
4871 * @param peer Destination peer.
4873 * @return The next viable MeshPeerQueue element to send to that peer.
4874 * NULL when there are no transmittable messages.
4876 struct MeshPeerQueue *
4877 queue_get_next (const struct MeshPeerInfo *peer)
4879 struct MeshPeerQueue *q;
4880 struct MeshTunnel *t;
4881 struct MeshTransmissionDescriptor *info;
4882 struct MeshTunnelChildInfo *cinfo;
4883 struct GNUNET_MESH_Unicast *ucast;
4884 struct GNUNET_MESH_ToOrigin *to_orig;
4885 struct GNUNET_MESH_Multicast *mcast;
4886 struct GNUNET_PeerIdentity id;
4890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4891 for (q = peer->queue_head; NULL != q; q = q->next)
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4897 GNUNET_MESH_DEBUG_M2S(q->type));
4900 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4901 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4902 pid = ntohl (ucast->pid);
4903 GNUNET_PEER_resolve (info->peer->id, &id);
4904 cinfo = tunnel_get_neighbor_fc(t, &id);
4905 ack = cinfo->fwd_ack;
4907 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4908 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4909 pid = ntohl (to_orig->pid);
4912 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4913 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4914 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4916 // Not a multicast payload: multicast control traffic (destroy, etc)
4919 pid = ntohl (mcast->pid);
4920 GNUNET_PEER_resolve (info->peer->id, &id);
4921 cinfo = tunnel_get_neighbor_fc(t, &id);
4922 ack = cinfo->fwd_ack;
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4930 "********* ACK: %u, PID: %u\n",
4932 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4941 "********* NEXT!\n");
4944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4945 "********* nothing found\n");
4951 * Core callback to write a queued packet to core buffer
4953 * @param cls Closure (peer info).
4954 * @param size Number of bytes available in buf.
4955 * @param buf Where the to write the message.
4957 * @return number of bytes written to buf
4960 queue_send (void *cls, size_t size, void *buf)
4962 struct MeshPeerInfo *peer = cls;
4963 struct GNUNET_MessageHeader *msg;
4964 struct MeshPeerQueue *queue;
4965 struct MeshTunnel *t;
4966 struct MeshTunnelChildInfo *cinfo;
4967 struct GNUNET_PeerIdentity dst_id;
4970 peer->core_transmit = NULL;
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4974 queue = queue_get_next (peer);
4976 /* Queue has no internal mesh traffic nor sendable payload */
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4980 if (NULL == peer->queue_head)
4981 GNUNET_break (0); // Should've been canceled
4984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4986 GNUNET_PEER_resolve (peer->id, &dst_id);
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4988 "********* towards %s\n",
4989 GNUNET_i2s(&dst_id));
4990 /* Check if buffer size is enough for the message */
4991 if (queue->size > size)
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4994 "********* not enough room, reissue\n");
4995 peer->core_transmit =
4996 GNUNET_CORE_notify_transmit_ready (core_handle,
4999 GNUNET_TIME_UNIT_FOREVER_REL,
5006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5009 GNUNET_assert (0 < t->pending_messages);
5010 t->pending_messages--;
5011 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5015 "********* unicast: t->q (%u/%u)\n",
5016 t->fwd_queue_n, t->fwd_queue_max);
5018 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5025 switch (queue->type)
5028 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5029 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5030 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5031 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5032 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5034 "********* raw: %s\n",
5035 GNUNET_MESH_DEBUG_M2S (queue->type));
5037 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5038 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5039 data_size = send_core_data_raw (queue->cls, size, buf);
5040 msg = (struct GNUNET_MessageHeader *) buf;
5041 switch (ntohs (msg->type)) // Type of preconstructed message
5043 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5044 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5046 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5047 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5053 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5056 struct MeshTransmissionDescriptor *info = queue->cls;
5058 if ((1 == info->mesh_data->reference_counter
5059 && GNUNET_YES == t->speed_min)
5061 (info->mesh_data->total_out == info->mesh_data->reference_counter
5062 && GNUNET_NO == t->speed_min))
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5065 "********* considered sent\n");
5070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5071 "********* NOT considered sent yet\n");
5072 t->pending_messages++;
5075 data_size = send_core_data_multicast(queue->cls, size, buf);
5076 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5078 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5080 data_size = send_core_path_create (queue->cls, size, buf);
5082 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5084 data_size = send_core_path_ack (queue->cls, size, buf);
5086 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5088 data_size = send_core_data_multicast (queue->cls, size, buf);
5092 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5093 "********* type unknown: %u\n",
5097 switch (queue->type)
5099 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5100 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5101 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5102 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5103 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5107 "at pos %u (%p) != %p\n",
5108 cinfo->send_buffer_start,
5109 cinfo->send_buffer[cinfo->send_buffer_start],
5112 if (cinfo->send_buffer_n > 0)
5114 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5115 cinfo->send_buffer_n--;
5116 cinfo->send_buffer_start++;
5117 cinfo->send_buffer_start %= t->fwd_queue_max;
5128 /* Free queue, but cls was freed by send_core_* */
5129 queue_destroy (queue, GNUNET_NO);
5131 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5137 /* If more data in queue, send next */
5138 queue = queue_get_next(peer);
5141 struct GNUNET_PeerIdentity id;
5143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5144 GNUNET_PEER_resolve (peer->id, &id);
5145 peer->core_transmit =
5146 GNUNET_CORE_notify_transmit_ready(core_handle,
5149 GNUNET_TIME_UNIT_FOREVER_REL,
5157 if (NULL != peer->queue_head)
5159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5160 "********* %s stalled\n",
5161 GNUNET_i2s(&my_full_id));
5163 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5164 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(MESH_POLL_TIME,
5165 &tunnel_poll, cinfo);
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5174 * @brief Queue and pass message to core when possible.
5176 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5177 * and accounts for it. In case the queue is full, the message is dropped and
5180 * Otherwise, message is treated as internal and allowed to go regardless of
5183 * @param cls Closure (@c type dependant). It will be used by queue_send to
5184 * build the message to be sent if not already prebuilt.
5185 * @param type Type of the message, 0 for a raw message.
5186 * @param size Size of the message.
5187 * @param dst Neighbor to send message to.
5188 * @param t Tunnel this message belongs to.
5191 queue_add (void *cls, uint16_t type, size_t size,
5192 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5194 struct MeshPeerQueue *queue;
5195 struct MeshTunnelChildInfo *cinfo;
5196 struct GNUNET_PeerIdentity id;
5202 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5203 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5205 n = &t->fwd_queue_n;
5206 max = &t->fwd_queue_max;
5208 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5210 n = &t->bck_queue_n;
5211 max = &t->bck_queue_max;
5218 GNUNET_STATISTICS_update(stats,
5219 "# messages dropped (buffer full)",
5221 return; // Drop message
5225 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5231 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5232 GNUNET_PEER_resolve (dst->id, &id);
5233 if (NULL == dst->core_transmit)
5235 dst->core_transmit =
5236 GNUNET_CORE_notify_transmit_ready (core_handle,
5239 GNUNET_TIME_UNIT_FOREVER_REL,
5245 t->pending_messages++;
5246 if (NULL == n) // Is this internal mesh traffic?
5249 // It's payload, keep track of buffer per peer.
5250 cinfo = tunnel_get_neighbor_fc(t, &id);
5251 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5252 if (NULL != cinfo->send_buffer[i])
5254 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5255 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5256 cinfo->send_buffer_start++;
5257 cinfo->send_buffer_start %= t->fwd_queue_max;
5261 cinfo->send_buffer_n++;
5263 cinfo->send_buffer[i] = queue;
5264 if (cinfo->send_buffer_n > t->fwd_queue_max)
5267 cinfo->send_buffer_n = t->fwd_queue_max;
5272 /******************************************************************************/
5273 /******************** MESH NETWORK HANDLERS **************************/
5274 /******************************************************************************/
5278 * Core handler for path creation
5280 * @param cls closure
5281 * @param message message
5282 * @param peer peer identity this notification is about
5283 * @param atsi performance data
5284 * @param atsi_count number of records in 'atsi'
5286 * @return GNUNET_OK to keep the connection open,
5287 * GNUNET_SYSERR to close it (signal serious error)
5290 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5291 const struct GNUNET_MessageHeader *message,
5292 const struct GNUNET_ATS_Information *atsi,
5293 unsigned int atsi_count)
5295 unsigned int own_pos;
5298 MESH_TunnelNumber tid;
5299 struct GNUNET_MESH_ManipulatePath *msg;
5300 struct GNUNET_PeerIdentity *pi;
5301 struct GNUNET_HashCode hash;
5302 struct MeshPeerPath *path;
5303 struct MeshPeerInfo *dest_peer_info;
5304 struct MeshPeerInfo *orig_peer_info;
5305 struct MeshTunnel *t;
5307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5308 "Received a path create msg [%s]\n",
5309 GNUNET_i2s (&my_full_id));
5310 size = ntohs (message->size);
5311 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5313 GNUNET_break_op (0);
5317 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5318 if (size % sizeof (struct GNUNET_PeerIdentity))
5320 GNUNET_break_op (0);
5323 size /= sizeof (struct GNUNET_PeerIdentity);
5326 GNUNET_break_op (0);
5329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5330 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5332 tid = ntohl (msg->tid);
5333 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5335 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5336 t = tunnel_get (pi, tid);
5337 if (NULL == t) // FIXME only for INCOMING tunnels?
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5342 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5345 // FIXME notify failure
5348 opt = ntohl (msg->opt);
5349 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5350 GNUNET_YES : GNUNET_NO;
5351 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5353 t->nobuffer = GNUNET_YES;
5354 t->last_fwd_ack = t->fwd_pid + 1;
5356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5357 " speed_min: %d, nobuffer:%d\n",
5358 t->speed_min, t->nobuffer);
5360 if (GNUNET_YES == t->nobuffer)
5362 t->bck_queue_max = 1;
5363 t->fwd_queue_max = 1;
5366 // FIXME only assign a local tid if a local client is interested (on demand)
5367 while (NULL != tunnel_get_incoming (next_local_tid))
5368 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5369 t->local_tid_dest = next_local_tid++;
5370 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5373 tunnel_reset_timeout (t);
5374 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5376 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5377 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5385 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5386 if (NULL == dest_peer_info)
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5389 " Creating PeerInfo for destination.\n");
5390 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5391 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5392 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5394 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5396 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5397 if (NULL == orig_peer_info)
5399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5400 " Creating PeerInfo for origin.\n");
5401 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5402 orig_peer_info->id = GNUNET_PEER_intern (pi);
5403 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5404 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5407 path = path_new (size);
5409 for (i = 0; i < size; i++)
5411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5412 GNUNET_i2s (&pi[i]));
5413 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5414 if (path->peers[i] == myid)
5417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5420 /* cannot be self, must be 'not found' */
5421 /* create path: self not found in path through self */
5422 GNUNET_break_op (0);
5423 path_destroy (path);
5427 path_add_to_peers (path, GNUNET_NO);
5428 tunnel_add_path (t, path, own_pos);
5429 if (own_pos == size - 1)
5431 /* It is for us! Send ack. */
5432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5433 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5434 if (NULL == t->peers)
5436 /* New tunnel! Notify clients on first payload message. */
5437 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5439 GNUNET_break (GNUNET_SYSERR !=
5440 GNUNET_CONTAINER_multihashmap_put (t->peers,
5441 &my_full_id.hashPubKey,
5444 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5449 struct MeshPeerPath *path2;
5451 /* It's for somebody else! Retransmit. */
5452 path2 = path_duplicate (path);
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5454 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5455 path2 = path_duplicate (path);
5456 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5457 send_create_path (dest_peer_info, path, t);
5464 * Core handler for path destruction
5466 * @param cls closure
5467 * @param message message
5468 * @param peer peer identity this notification is about
5469 * @param atsi performance data
5470 * @param atsi_count number of records in 'atsi'
5472 * @return GNUNET_OK to keep the connection open,
5473 * GNUNET_SYSERR to close it (signal serious error)
5476 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5477 const struct GNUNET_MessageHeader *message,
5478 const struct GNUNET_ATS_Information *atsi,
5479 unsigned int atsi_count)
5481 struct GNUNET_MESH_ManipulatePath *msg;
5482 struct GNUNET_PeerIdentity *pi;
5483 struct MeshPeerPath *path;
5484 struct MeshTunnel *t;
5485 unsigned int own_pos;
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5490 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5491 size = ntohs (message->size);
5492 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5494 GNUNET_break_op (0);
5498 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5499 if (size % sizeof (struct GNUNET_PeerIdentity))
5501 GNUNET_break_op (0);
5504 size /= sizeof (struct GNUNET_PeerIdentity);
5507 GNUNET_break_op (0);
5510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5512 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5513 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5515 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5517 t = tunnel_get (pi, ntohl (msg->tid));
5520 /* TODO notify back: we don't know this tunnel */
5521 GNUNET_break_op (0);
5524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5525 path = path_new (size);
5527 for (i = 0; i < size; i++)
5529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5530 GNUNET_i2s (&pi[i]));
5531 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5532 if (path->peers[i] == myid)
5535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5536 if (own_pos < path->length - 1)
5537 send_prebuilt_message (message, &pi[own_pos + 1], t);
5539 send_client_tunnel_disconnect(t, NULL);
5541 tunnel_delete_peer (t, path->peers[path->length - 1]);
5542 path_destroy (path);
5548 * Core handler for notifications of broken paths
5550 * @param cls closure
5551 * @param message message
5552 * @param peer peer identity this notification is about
5553 * @param atsi performance data
5554 * @param atsi_count number of records in 'atsi'
5556 * @return GNUNET_OK to keep the connection open,
5557 * GNUNET_SYSERR to close it (signal serious error)
5560 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5561 const struct GNUNET_MessageHeader *message,
5562 const struct GNUNET_ATS_Information *atsi,
5563 unsigned int atsi_count)
5565 struct GNUNET_MESH_PathBroken *msg;
5566 struct MeshTunnel *t;
5568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5569 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5570 msg = (struct GNUNET_MESH_PathBroken *) message;
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5572 GNUNET_i2s (&msg->peer1));
5573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5574 GNUNET_i2s (&msg->peer2));
5575 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5578 GNUNET_break_op (0);
5581 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5582 GNUNET_PEER_search (&msg->peer2));
5589 * Core handler for tunnel destruction
5591 * @param cls closure
5592 * @param message message
5593 * @param peer peer identity this notification is about
5594 * @param atsi performance data
5595 * @param atsi_count number of records in 'atsi'
5597 * @return GNUNET_OK to keep the connection open,
5598 * GNUNET_SYSERR to close it (signal serious error)
5601 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5602 const struct GNUNET_MessageHeader *message,
5603 const struct GNUNET_ATS_Information *atsi,
5604 unsigned int atsi_count)
5606 struct GNUNET_MESH_TunnelDestroy *msg;
5607 struct MeshTunnel *t;
5609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5610 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5611 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5613 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5614 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5617 /* Probably already got the message from another path,
5618 * destroyed the tunnel and retransmitted to children.
5621 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5624 if (t->id.oid == myid)
5626 GNUNET_break_op (0);
5629 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5631 /* Tunnel was incoming, notify clients */
5632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5633 t->local_tid, t->local_tid_dest);
5634 send_clients_tunnel_destroy (t);
5636 tunnel_send_destroy (t);
5637 t->destroy = GNUNET_YES;
5638 // TODO: add timeout to destroy the tunnel anyway
5644 * Core handler for mesh network traffic going from the origin to a peer
5646 * @param cls closure
5647 * @param peer peer identity this notification is about
5648 * @param message message
5649 * @param atsi performance data
5650 * @param atsi_count number of records in 'atsi'
5651 * @return GNUNET_OK to keep the connection open,
5652 * GNUNET_SYSERR to close it (signal serious error)
5655 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5656 const struct GNUNET_MessageHeader *message,
5657 const struct GNUNET_ATS_Information *atsi,
5658 unsigned int atsi_count)
5660 struct GNUNET_MESH_Unicast *msg;
5661 struct GNUNET_PeerIdentity *neighbor;
5662 struct MeshTunnelChildInfo *cinfo;
5663 struct MeshTunnel *t;
5664 GNUNET_PEER_Id dest_id;
5669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5672 size = ntohs (message->size);
5674 sizeof (struct GNUNET_MESH_Unicast) +
5675 sizeof (struct GNUNET_MessageHeader))
5680 msg = (struct GNUNET_MESH_Unicast *) message;
5681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5682 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5684 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5687 /* TODO notify back: we don't know this tunnel */
5688 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5689 GNUNET_break_op (0);
5692 pid = ntohl (msg->pid);
5693 if (t->fwd_pid == pid)
5695 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5697 " Already seen pid %u, DROPPING!\n", pid);
5702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5703 " pid %u not seen yet, forwarding\n", pid);
5706 t->skip += (pid - t->fwd_pid) - 1;
5709 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5711 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5712 GNUNET_break_op (0);
5713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5714 "Received PID %u, ACK %u\n",
5715 pid, t->last_fwd_ack);
5719 tunnel_reset_timeout (t);
5720 dest_id = GNUNET_PEER_search (&msg->destination);
5721 if (dest_id == myid)
5723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5724 " it's for us! sending to clients...\n");
5725 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5726 send_subscribed_clients (message, &msg[1].header, t);
5727 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5730 ttl = ntohl (msg->ttl);
5731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5734 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5735 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5736 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5740 " not for us, retransmitting...\n");
5742 neighbor = tree_get_first_hop (t->tree, dest_id);
5743 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5744 cinfo->fwd_pid = pid;
5745 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5748 if (GNUNET_YES == t->nobuffer &&
5749 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5751 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5752 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5753 GNUNET_break_op (0);
5756 send_prebuilt_message (message, neighbor, t);
5757 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5763 * Core handler for mesh network traffic going from the origin to all peers
5765 * @param cls closure
5766 * @param message message
5767 * @param peer peer identity this notification is about
5768 * @param atsi performance data
5769 * @param atsi_count number of records in 'atsi'
5770 * @return GNUNET_OK to keep the connection open,
5771 * GNUNET_SYSERR to close it (signal serious error)
5773 * TODO: Check who we got this from, to validate route.
5776 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5777 const struct GNUNET_MessageHeader *message,
5778 const struct GNUNET_ATS_Information *atsi,
5779 unsigned int atsi_count)
5781 struct GNUNET_MESH_Multicast *msg;
5782 struct MeshTunnel *t;
5786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5788 size = ntohs (message->size);
5789 if (sizeof (struct GNUNET_MESH_Multicast) +
5790 sizeof (struct GNUNET_MessageHeader) > size)
5792 GNUNET_break_op (0);
5795 msg = (struct GNUNET_MESH_Multicast *) message;
5796 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5800 /* TODO notify that we dont know that tunnel */
5801 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5802 GNUNET_break_op (0);
5805 pid = ntohl (msg->pid);
5806 if (t->fwd_pid == pid)
5808 /* already seen this packet, drop */
5809 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5811 " Already seen pid %u, DROPPING!\n", pid);
5812 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5818 " pid %u not seen yet, forwarding\n", pid);
5820 t->skip += (pid - t->fwd_pid) - 1;
5822 tunnel_reset_timeout (t);
5824 /* Transmit to locally interested clients */
5825 if (NULL != t->peers &&
5826 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5828 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5829 send_subscribed_clients (message, &msg[1].header, t);
5830 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5833 if (ntohl (msg->ttl) == 0)
5835 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5836 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5837 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5840 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5841 tunnel_send_multicast (t, message);
5847 * Core handler for mesh network traffic toward the owner of a tunnel
5849 * @param cls closure
5850 * @param message message
5851 * @param peer peer identity this notification is about
5852 * @param atsi performance data
5853 * @param atsi_count number of records in 'atsi'
5855 * @return GNUNET_OK to keep the connection open,
5856 * GNUNET_SYSERR to close it (signal serious error)
5859 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5860 const struct GNUNET_MessageHeader *message,
5861 const struct GNUNET_ATS_Information *atsi,
5862 unsigned int atsi_count)
5864 struct GNUNET_MESH_ToOrigin *msg;
5865 struct GNUNET_PeerIdentity id;
5866 struct MeshPeerInfo *peer_info;
5867 struct MeshTunnel *t;
5868 struct MeshTunnelChildInfo *cinfo;
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5874 size = ntohs (message->size);
5875 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5876 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5878 GNUNET_break_op (0);
5881 msg = (struct GNUNET_MESH_ToOrigin *) message;
5882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5883 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5884 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5885 pid = ntohl (msg->pid);
5889 /* TODO notify that we dont know this tunnel (whom)? */
5890 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5891 GNUNET_break_op (0);
5892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5893 "Received to_origin with PID %u on unknown tunnel\n",
5898 cinfo = tunnel_get_neighbor_fc(t, peer);
5905 if (cinfo->bck_pid == pid)
5907 /* already seen this packet, drop */
5908 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5910 " Already seen pid %u, DROPPING!\n", pid);
5911 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5916 " pid %u not seen yet, forwarding\n", pid);
5917 cinfo->bck_pid = pid;
5919 if (NULL != t->owner)
5922 struct GNUNET_MESH_ToOrigin *copy;
5924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5925 " it's for us! sending to clients...\n");
5926 /* TODO signature verification */
5927 memcpy (cbuf, message, size);
5928 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5929 copy->tid = htonl (t->local_tid);
5931 copy->pid = htonl (t->bck_pid);
5932 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5933 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5934 ©->header, GNUNET_NO);
5935 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5939 " not for us, retransmitting...\n");
5941 peer_info = peer_info_get (&msg->oid);
5942 if (NULL == peer_info)
5944 /* unknown origin of tunnel */
5948 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5949 send_prebuilt_message (message, &id, t);
5950 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5957 * Core handler for mesh network traffic point-to-point acks.
5959 * @param cls closure
5960 * @param message message
5961 * @param peer peer identity this notification is about
5962 * @param atsi performance data
5963 * @param atsi_count number of records in 'atsi'
5965 * @return GNUNET_OK to keep the connection open,
5966 * GNUNET_SYSERR to close it (signal serious error)
5969 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5970 const struct GNUNET_MessageHeader *message,
5971 const struct GNUNET_ATS_Information *atsi,
5972 unsigned int atsi_count)
5974 struct GNUNET_MESH_ACK *msg;
5975 struct MeshTunnel *t;
5978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5980 msg = (struct GNUNET_MESH_ACK *) message;
5982 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5986 /* TODO notify that we dont know this tunnel (whom)? */
5987 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5990 ack = ntohl (msg->pid);
5991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5993 /* Is this a forward or backward ACK? */
5994 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5996 struct MeshTunnelChildInfo *cinfo;
5999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6000 cinfo = tunnel_get_neighbor_fc (t, peer);
6001 cinfo->fwd_ack = ack;
6002 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6003 tunnel_unlock_fwd_queues (t);
6007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6009 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6010 tunnel_unlock_bck_queue (t);
6017 * Core handler for mesh network traffic point-to-point ack polls.
6019 * @param cls closure
6020 * @param message message
6021 * @param peer peer identity this notification is about
6022 * @param atsi performance data
6023 * @param atsi_count number of records in 'atsi'
6025 * @return GNUNET_OK to keep the connection open,
6026 * GNUNET_SYSERR to close it (signal serious error)
6029 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6030 const struct GNUNET_MessageHeader *message,
6031 const struct GNUNET_ATS_Information *atsi,
6032 unsigned int atsi_count)
6034 struct GNUNET_MESH_Poll *msg;
6035 struct MeshTunnel *t;
6037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6040 msg = (struct GNUNET_MESH_Poll *) message;
6042 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6046 /* TODO notify that we dont know this tunnel (whom)? */
6047 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6048 GNUNET_break_op (0);
6052 /* Is this a forward or backward ACK? */
6053 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6055 struct MeshTunnelChildInfo *cinfo;
6057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6058 cinfo = tunnel_get_neighbor_fc (t, peer);
6059 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6060 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6065 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6073 * Core handler for path ACKs
6075 * @param cls closure
6076 * @param message message
6077 * @param peer peer identity this notification is about
6078 * @param atsi performance data
6079 * @param atsi_count number of records in 'atsi'
6081 * @return GNUNET_OK to keep the connection open,
6082 * GNUNET_SYSERR to close it (signal serious error)
6085 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6086 const struct GNUNET_MessageHeader *message,
6087 const struct GNUNET_ATS_Information *atsi,
6088 unsigned int atsi_count)
6090 struct GNUNET_MESH_PathACK *msg;
6091 struct GNUNET_PeerIdentity id;
6092 struct MeshPeerInfo *peer_info;
6093 struct MeshPeerPath *p;
6094 struct MeshTunnel *t;
6096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6097 GNUNET_i2s (&my_full_id));
6098 msg = (struct GNUNET_MESH_PathACK *) message;
6099 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6102 /* TODO notify that we don't know the tunnel */
6103 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6105 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6109 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6111 peer_info = peer_info_get (&msg->peer_id);
6112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6113 GNUNET_i2s (&msg->peer_id));
6114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6117 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6120 "connect_by_string completed, stopping search\n");
6121 regex_cancel_search (t->regex_ctx);
6122 t->regex_ctx = NULL;
6125 /* Add paths to peers? */
6126 p = tree_get_path_to_peer (t->tree, peer_info->id);
6129 path_add_to_peers (p, GNUNET_YES);
6137 /* Message for us? */
6138 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6141 if (NULL == t->owner)
6143 GNUNET_break_op (0);
6146 if (NULL != t->dht_get_type)
6148 GNUNET_DHT_get_stop (t->dht_get_type);
6149 t->dht_get_type = NULL;
6151 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6153 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6154 send_client_peer_connected (t, peer_info->id);
6159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6160 " not for us, retransmitting...\n");
6161 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6162 peer_info = peer_info_get (&msg->oid);
6163 if (NULL == peer_info)
6165 /* If we know the tunnel, we should DEFINITELY know the peer */
6169 send_prebuilt_message (message, &id, t);
6175 * Core handler for mesh keepalives.
6177 * @param cls closure
6178 * @param message message
6179 * @param peer peer identity this notification is about
6180 * @param atsi performance data
6181 * @param atsi_count number of records in 'atsi'
6182 * @return GNUNET_OK to keep the connection open,
6183 * GNUNET_SYSERR to close it (signal serious error)
6185 * TODO: Check who we got this from, to validate route.
6188 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6189 const struct GNUNET_MessageHeader *message,
6190 const struct GNUNET_ATS_Information *atsi,
6191 unsigned int atsi_count)
6193 struct GNUNET_MESH_TunnelKeepAlive *msg;
6194 struct MeshTunnel *t;
6196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6199 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6200 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6204 /* TODO notify that we dont know that tunnel */
6205 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6206 GNUNET_break_op (0);
6210 tunnel_reset_timeout (t);
6212 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6213 tunnel_send_multicast (t, message);
6220 * Functions to handle messages from core
6222 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6223 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6224 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6225 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6226 sizeof (struct GNUNET_MESH_PathBroken)},
6227 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6228 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6229 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6230 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6231 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6232 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6233 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6234 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6235 sizeof (struct GNUNET_MESH_ACK)},
6236 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6237 sizeof (struct GNUNET_MESH_Poll)},
6238 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6239 sizeof (struct GNUNET_MESH_PathACK)},
6245 /******************************************************************************/
6246 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6247 /******************************************************************************/
6250 * deregister_app: iterator for removing each application registered by a client
6252 * @param cls closure
6253 * @param key the hash of the application id (used to access the hashmap)
6254 * @param value the value stored at the key (client)
6256 * @return GNUNET_OK on success
6259 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6261 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6262 GNUNET_break (GNUNET_YES ==
6263 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6269 * notify_client_connection_failure: notify a client that the connection to the
6270 * requested remote peer is not possible (for instance, no route found)
6271 * Function called when the socket is ready to queue more data. "buf" will be
6272 * NULL and "size" zero if the socket was closed for writing in the meantime.
6274 * @param cls closure
6275 * @param size number of bytes available in buf
6276 * @param buf where the callee should write the message
6277 * @return number of bytes written to buf
6280 notify_client_connection_failure (void *cls, size_t size, void *buf)
6283 struct MeshPeerInfo *peer_info;
6284 struct GNUNET_MESH_PeerControl *msg;
6285 struct GNUNET_PeerIdentity id;
6287 if (0 == size && NULL == buf)
6289 // TODO retry? cancel?
6293 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6294 peer_info = (struct MeshPeerInfo *) cls;
6295 msg = (struct GNUNET_MESH_PeerControl *) buf;
6296 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6297 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6298 // msg->tunnel_id = htonl(peer_info->t->tid);
6299 GNUNET_PEER_resolve (peer_info->id, &id);
6300 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6308 * Send keepalive packets for a peer
6310 * @param cls Closure (tunnel for which to send the keepalive).
6311 * @param tc Notification context.
6314 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6316 struct MeshTunnel *t = cls;
6317 struct GNUNET_MESH_TunnelKeepAlive *msg;
6318 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6321 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6322 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6328 "sending keepalive for tunnel %d\n", t->id.tid);
6330 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6331 msg->header.size = htons (size);
6332 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6333 msg->oid = my_full_id;
6334 msg->tid = htonl (t->id.tid);
6335 tunnel_send_multicast (t, &msg->header);
6337 t->path_refresh_task =
6338 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6339 tunnel_reset_timeout(t);
6344 * Function to process paths received for a new peer addition. The recorded
6345 * paths form the initial tunnel, which can be optimized later.
6346 * Called on each result obtained for the DHT search.
6348 * @param cls closure
6349 * @param exp when will this value expire
6350 * @param key key of the result
6351 * @param get_path path of the get request
6352 * @param get_path_length lenght of get_path
6353 * @param put_path path of the put request
6354 * @param put_path_length length of the put_path
6355 * @param type type of the result
6356 * @param size number of bytes in data
6357 * @param data pointer to the result data
6359 * TODO: re-issue the request after certain time? cancel after X results?
6362 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6363 const struct GNUNET_HashCode * key,
6364 const struct GNUNET_PeerIdentity *get_path,
6365 unsigned int get_path_length,
6366 const struct GNUNET_PeerIdentity *put_path,
6367 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6368 size_t size, const void *data)
6370 struct MeshPathInfo *path_info = cls;
6371 struct MeshPeerPath *p;
6372 struct GNUNET_PeerIdentity pi;
6375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6376 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6379 p = path_build_from_dht (get_path, get_path_length, put_path,
6381 path_add_to_peers (p, GNUNET_NO);
6383 for (i = 0; i < path_info->peer->ntunnels; i++)
6385 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6386 peer_info_connect (path_info->peer, path_info->t);
6394 * Function to process paths received for a new peer addition. The recorded
6395 * paths form the initial tunnel, which can be optimized later.
6396 * Called on each result obtained for the DHT search.
6398 * @param cls closure
6399 * @param exp when will this value expire
6400 * @param key key of the result
6401 * @param get_path path of the get request
6402 * @param get_path_length lenght of get_path
6403 * @param put_path path of the put request
6404 * @param put_path_length length of the put_path
6405 * @param type type of the result
6406 * @param size number of bytes in data
6407 * @param data pointer to the result data
6410 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6411 const struct GNUNET_HashCode * key,
6412 const struct GNUNET_PeerIdentity *get_path,
6413 unsigned int get_path_length,
6414 const struct GNUNET_PeerIdentity *put_path,
6415 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6416 size_t size, const void *data)
6418 const struct PBlock *pb = data;
6419 const struct GNUNET_PeerIdentity *pi = &pb->id;
6420 struct MeshTunnel *t = cls;
6421 struct MeshPeerInfo *peer_info;
6422 struct MeshPeerPath *p;
6424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6425 if (size != sizeof (struct PBlock))
6427 GNUNET_break_op (0);
6430 if (ntohl(pb->type) != t->type)
6432 GNUNET_break_op (0);
6435 GNUNET_assert (NULL != t->owner);
6436 peer_info = peer_info_get (pi);
6437 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6439 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6441 p = path_build_from_dht (get_path, get_path_length, put_path,
6443 path_add_to_peers (p, GNUNET_NO);
6445 tunnel_add_peer (t, peer_info);
6446 peer_info_connect (peer_info, t);
6451 * Function to process DHT string to regex matching.
6452 * Called on each result obtained for the DHT search.
6454 * @param cls closure (search context)
6455 * @param exp when will this value expire
6456 * @param key key of the result
6457 * @param get_path path of the get request (not used)
6458 * @param get_path_length lenght of get_path (not used)
6459 * @param put_path path of the put request (not used)
6460 * @param put_path_length length of the put_path (not used)
6461 * @param type type of the result
6462 * @param size number of bytes in data
6463 * @param data pointer to the result data
6466 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6467 const struct GNUNET_HashCode * key,
6468 const struct GNUNET_PeerIdentity *get_path,
6469 unsigned int get_path_length,
6470 const struct GNUNET_PeerIdentity *put_path,
6471 unsigned int put_path_length,
6472 enum GNUNET_BLOCK_Type type,
6473 size_t size, const void *data)
6475 const struct MeshRegexAccept *block = data;
6476 struct MeshRegexSearchContext *ctx = cls;
6477 struct MeshRegexSearchInfo *info = ctx->info;
6478 struct MeshPeerPath *p;
6479 struct MeshPeerInfo *peer_info;
6481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6484 peer_info = peer_info_get(&block->id);
6485 p = path_build_from_dht (get_path, get_path_length, put_path,
6487 path_add_to_peers (p, GNUNET_NO);
6490 tunnel_add_peer (info->t, peer_info);
6491 peer_info_connect (peer_info, info->t);
6492 if (0 == info->peer)
6494 info->peer = peer_info->id;
6498 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6501 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6502 ®ex_connect_timeout,
6510 * Function to process DHT string to regex matching.
6511 * Called on each result obtained for the DHT search.
6513 * @param cls closure (search context)
6514 * @param exp when will this value expire
6515 * @param key key of the result
6516 * @param get_path path of the get request (not used)
6517 * @param get_path_length lenght of get_path (not used)
6518 * @param put_path path of the put request (not used)
6519 * @param put_path_length length of the put_path (not used)
6520 * @param type type of the result
6521 * @param size number of bytes in data
6522 * @param data pointer to the result data
6524 * TODO: re-issue the request after certain time? cancel after X results?
6527 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6528 const struct GNUNET_HashCode * key,
6529 const struct GNUNET_PeerIdentity *get_path,
6530 unsigned int get_path_length,
6531 const struct GNUNET_PeerIdentity *put_path,
6532 unsigned int put_path_length,
6533 enum GNUNET_BLOCK_Type type,
6534 size_t size, const void *data)
6536 const struct MeshRegexBlock *block = data;
6537 struct MeshRegexSearchContext *ctx = cls;
6538 struct MeshRegexSearchInfo *info = ctx->info;
6542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6543 "DHT GET STRING RETURNED RESULTS\n");
6544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6545 " key: %s\n", GNUNET_h2s (key));
6547 copy = GNUNET_malloc (size);
6548 memcpy (copy, data, size);
6549 GNUNET_break (GNUNET_OK ==
6550 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6551 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6552 len = ntohl (block->n_proof);
6554 char proof[len + 1];
6556 memcpy (proof, &block[1], len);
6558 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6560 GNUNET_break_op (0);
6564 len = strlen (info->description);
6565 if (len == ctx->position) // String processed
6567 if (GNUNET_YES == ntohl (block->accepting))
6569 regex_find_path(key, ctx);
6573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6574 // FIXME REGEX this block not successful, wait for more? start timeout?
6579 regex_next_edge (block, size, ctx);
6584 /******************************************************************************/
6585 /********************* MESH LOCAL HANDLES **************************/
6586 /******************************************************************************/
6590 * Handler for client disconnection
6592 * @param cls closure
6593 * @param client identification of the client; NULL
6594 * for the last call when the server is destroyed
6597 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6599 struct MeshClient *c;
6600 struct MeshClient *next;
6603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6610 // return; uncomment for regex_profiler
6614 if (c->handle != client)
6616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6622 GNUNET_SERVER_client_drop (c->handle);
6623 c->shutting_down = GNUNET_YES;
6624 GNUNET_assert (NULL != c->own_tunnels);
6625 GNUNET_assert (NULL != c->incoming_tunnels);
6626 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6627 &tunnel_destroy_iterator, c);
6628 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6629 &tunnel_destroy_iterator, c);
6630 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6631 &tunnel_destroy_iterator, c);
6632 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6633 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6634 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6636 /* deregister clients applications */
6637 if (NULL != c->apps)
6639 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6640 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6642 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6643 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6645 GNUNET_SCHEDULER_cancel (announce_applications_task);
6646 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6648 if (NULL != c->types)
6649 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6650 for (i = 0; i < c->n_regex; i++)
6652 GNUNET_free (c->regexes[i].regex);
6654 GNUNET_free_non_null (c->regexes);
6655 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6656 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6658 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6661 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6670 * Handler for new clients
6672 * @param cls closure
6673 * @param client identification of the client
6674 * @param message the actual message, which includes messages the client wants
6677 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6678 const struct GNUNET_MessageHeader *message)
6680 struct GNUNET_MESH_ClientConnect *cc_msg;
6681 struct MeshClient *c;
6682 GNUNET_MESH_ApplicationType *a;
6689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6690 /* Check data sanity */
6691 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6692 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6693 ntypes = ntohs (cc_msg->types);
6694 napps = ntohs (cc_msg->applications);
6696 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6703 /* Create new client structure */
6704 c = GNUNET_malloc (sizeof (struct MeshClient));
6705 c->id = next_client_id++;
6706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6708 GNUNET_SERVER_client_keep (client);
6709 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6712 GNUNET_MESH_ApplicationType at;
6713 struct GNUNET_HashCode hc;
6715 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6716 for (i = 0; i < napps; i++)
6719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6720 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6721 /* store in clients hashmap */
6722 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6723 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6724 /* store in global hashmap, for announcements */
6725 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6726 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6728 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6729 announce_applications_task =
6730 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6736 struct GNUNET_HashCode hc;
6738 t = (uint16_t *) & a[napps];
6739 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6740 for (i = 0; i < ntypes; i++)
6743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6744 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6746 /* store in clients hashmap */
6747 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6748 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6749 /* store in global hashmap */
6750 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6755 " client has %u+%u subscriptions\n", napps, ntypes);
6757 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6758 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6759 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6760 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6761 GNUNET_SERVER_notification_context_add (nc, client);
6762 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6764 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6770 * Handler for clients announcing available services by a regular expression.
6772 * @param cls closure
6773 * @param client identification of the client
6774 * @param message the actual message, which includes messages the client wants
6777 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6778 const struct GNUNET_MessageHeader *message)
6780 struct GNUNET_MESH_RegexAnnounce *msg;
6781 struct MeshRegexDescriptor rd;
6782 struct MeshClient *c;
6786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6788 /* Sanity check for client registration */
6789 if (NULL == (c = client_get (client)))
6792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6797 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6798 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6799 regex = GNUNET_malloc (len + 1);
6800 memcpy (regex, &msg[1], len);
6803 rd.compression = ntohs (msg->compression_characters);
6804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6807 GNUNET_array_append (c->regexes, c->n_regex, rd);
6808 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6810 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6816 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6822 * Handler for requests of new tunnels
6824 * @param cls closure
6825 * @param client identification of the client
6826 * @param message the actual message
6829 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6830 const struct GNUNET_MessageHeader *message)
6832 struct GNUNET_MESH_TunnelMessage *t_msg;
6833 struct MeshTunnel *t;
6834 struct MeshClient *c;
6835 MESH_TunnelNumber tid;
6837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6839 /* Sanity check for client registration */
6840 if (NULL == (c = client_get (client)))
6843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6848 /* Message sanity check */
6849 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6856 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6857 /* Sanity check for tunnel numbering */
6858 tid = ntohl (t_msg->tunnel_id);
6859 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6862 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6865 /* Sanity check for duplicate tunnel IDs */
6866 if (NULL != tunnel_get_by_local_id (c, tid))
6869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6873 while (NULL != tunnel_get_by_pi (myid, next_tid))
6874 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6875 t = tunnel_new (myid, next_tid++, c, tid);
6878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6880 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6883 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6885 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6886 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6889 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6895 * Handler for requests of deleting tunnels
6897 * @param cls closure
6898 * @param client identification of the client
6899 * @param message the actual message
6902 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6903 const struct GNUNET_MessageHeader *message)
6905 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6906 struct MeshClient *c;
6907 struct MeshTunnel *t;
6908 MESH_TunnelNumber tid;
6910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6911 "Got a DESTROY TUNNEL from client!\n");
6913 /* Sanity check for client registration */
6914 if (NULL == (c = client_get (client)))
6917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6922 /* Message sanity check */
6923 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6930 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6932 /* Retrieve tunnel */
6933 tid = ntohl (tunnel_msg->tunnel_id);
6934 t = tunnel_get_by_local_id(c, tid);
6937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6942 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6944 client_ignore_tunnel (c, t);
6946 // TODO: when to destroy incoming tunnel?
6947 if (t->nclients == 0)
6949 GNUNET_assert (GNUNET_YES ==
6950 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6952 GNUNET_assert (GNUNET_YES ==
6953 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6954 &my_full_id.hashPubKey,
6958 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6961 send_client_tunnel_disconnect(t, c);
6962 client_delete_tunnel(c, t);
6964 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6966 tunnel_send_destroy (t);
6967 t->destroy = GNUNET_YES;
6968 // The tunnel will be destroyed when the last message is transmitted.
6969 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6975 * Handler for requests of seeting tunnel's speed.
6977 * @param cls Closure (unused).
6978 * @param client Identification of the client.
6979 * @param message The actual message.
6982 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6983 const struct GNUNET_MessageHeader *message)
6985 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6986 struct MeshClient *c;
6987 struct MeshTunnel *t;
6988 MESH_TunnelNumber tid;
6990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6991 "Got a SPEED request from client!\n");
6993 /* Sanity check for client registration */
6994 if (NULL == (c = client_get (client)))
6997 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7003 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7005 /* Retrieve tunnel */
7006 tid = ntohl (tunnel_msg->tunnel_id);
7007 t = tunnel_get_by_local_id(c, tid);
7010 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7016 switch (ntohs(message->type))
7018 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7019 t->speed_min = GNUNET_YES;
7021 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7022 t->speed_min = GNUNET_NO;
7027 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7032 * Handler for requests of seeting tunnel's buffering policy.
7034 * @param cls Closure (unused).
7035 * @param client Identification of the client.
7036 * @param message The actual message.
7039 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7040 const struct GNUNET_MessageHeader *message)
7042 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7043 struct MeshClient *c;
7044 struct MeshTunnel *t;
7045 MESH_TunnelNumber tid;
7047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7048 "Got a BUFFER request from client!\n");
7050 /* Sanity check for client registration */
7051 if (NULL == (c = client_get (client)))
7054 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7059 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7061 /* Retrieve tunnel */
7062 tid = ntohl (tunnel_msg->tunnel_id);
7063 t = tunnel_get_by_local_id(c, tid);
7066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7072 switch (ntohs(message->type))
7074 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7075 t->nobuffer = GNUNET_NO;
7077 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7078 t->nobuffer = GNUNET_YES;
7084 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7089 * Handler for connection requests to new peers
7091 * @param cls closure
7092 * @param client identification of the client
7093 * @param message the actual message (PeerControl)
7096 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7097 const struct GNUNET_MessageHeader *message)
7099 struct GNUNET_MESH_PeerControl *peer_msg;
7100 struct MeshPeerInfo *peer_info;
7101 struct MeshClient *c;
7102 struct MeshTunnel *t;
7103 MESH_TunnelNumber tid;
7105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7106 /* Sanity check for client registration */
7107 if (NULL == (c = client_get (client)))
7110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7115 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7117 /* Sanity check for message size */
7118 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7125 /* Tunnel exists? */
7126 tid = ntohl (peer_msg->tunnel_id);
7127 t = tunnel_get_by_local_id (c, tid);
7131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7135 /* Does client own tunnel? */
7136 if (t->owner->handle != client)
7139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7143 GNUNET_i2s (&peer_msg->peer));
7144 peer_info = peer_info_get (&peer_msg->peer);
7146 tunnel_add_peer (t, peer_info);
7147 peer_info_connect (peer_info, t);
7149 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7155 * Handler for disconnection requests of peers in a tunnel
7157 * @param cls closure
7158 * @param client identification of the client
7159 * @param message the actual message (PeerControl)
7162 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7163 const struct GNUNET_MessageHeader *message)
7165 struct GNUNET_MESH_PeerControl *peer_msg;
7166 struct MeshPeerInfo *peer_info;
7167 struct MeshClient *c;
7168 struct MeshTunnel *t;
7169 MESH_TunnelNumber tid;
7171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7172 /* Sanity check for client registration */
7173 if (NULL == (c = client_get (client)))
7176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7181 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7183 /* Sanity check for message size */
7184 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7191 /* Tunnel exists? */
7192 tid = ntohl (peer_msg->tunnel_id);
7193 t = tunnel_get_by_local_id (c, tid);
7197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7202 /* Does client own tunnel? */
7203 if (t->owner->handle != client)
7206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7211 GNUNET_i2s (&peer_msg->peer));
7212 /* Is the peer in the tunnel? */
7214 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7215 if (NULL == peer_info)
7218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7222 /* Ok, delete peer from tunnel */
7223 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7224 &peer_msg->peer.hashPubKey);
7226 send_destroy_path (t, peer_info->id);
7227 tunnel_delete_peer (t, peer_info->id);
7228 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7233 * Handler for blacklist requests of peers in a tunnel
7235 * @param cls closure
7236 * @param client identification of the client
7237 * @param message the actual message (PeerControl)
7239 * FIXME implement DHT block bloomfilter
7242 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7243 const struct GNUNET_MessageHeader *message)
7245 struct GNUNET_MESH_PeerControl *peer_msg;
7246 struct MeshClient *c;
7247 struct MeshTunnel *t;
7248 MESH_TunnelNumber tid;
7250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7251 /* Sanity check for client registration */
7252 if (NULL == (c = client_get (client)))
7255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7260 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7262 /* Sanity check for message size */
7263 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7270 /* Tunnel exists? */
7271 tid = ntohl (peer_msg->tunnel_id);
7272 t = tunnel_get_by_local_id (c, tid);
7276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7281 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7282 GNUNET_PEER_intern(&peer_msg->peer));
7287 * Handler for unblacklist requests of peers in a tunnel
7289 * @param cls closure
7290 * @param client identification of the client
7291 * @param message the actual message (PeerControl)
7294 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7295 const struct GNUNET_MessageHeader *message)
7297 struct GNUNET_MESH_PeerControl *peer_msg;
7298 struct MeshClient *c;
7299 struct MeshTunnel *t;
7300 MESH_TunnelNumber tid;
7304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7305 /* Sanity check for client registration */
7306 if (NULL == (c = client_get (client)))
7309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7314 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7316 /* Sanity check for message size */
7317 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7324 /* Tunnel exists? */
7325 tid = ntohl (peer_msg->tunnel_id);
7326 t = tunnel_get_by_local_id (c, tid);
7330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7335 /* if peer is not known, complain */
7336 pid = GNUNET_PEER_search (&peer_msg->peer);
7343 /* search and remove from list */
7344 for (i = 0; i < t->nblacklisted; i++)
7346 if (t->blacklisted[i] == pid)
7348 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7349 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7354 /* if peer hasn't been blacklisted, complain */
7360 * Handler for connection requests to new peers by type
7362 * @param cls closure
7363 * @param client identification of the client
7364 * @param message the actual message (ConnectPeerByType)
7367 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7368 const struct GNUNET_MessageHeader *message)
7370 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7371 struct MeshClient *c;
7372 struct MeshTunnel *t;
7373 struct GNUNET_HashCode hash;
7374 MESH_TunnelNumber tid;
7376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7377 /* Sanity check for client registration */
7378 if (NULL == (c = client_get (client)))
7381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7386 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7388 /* Sanity check for message size */
7389 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7390 ntohs (connect_msg->header.size))
7393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7397 /* Tunnel exists? */
7398 tid = ntohl (connect_msg->tunnel_id);
7399 t = tunnel_get_by_local_id (c, tid);
7403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7407 /* Does client own tunnel? */
7408 if (t->owner->handle != client)
7411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7415 /* Do WE have the service? */
7416 t->type = ntohl (connect_msg->type);
7417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7418 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7419 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7422 /* Yes! Fast forward, add ourselves to the tunnel and send the
7423 * good news to the client, and alert the destination client of
7424 * an incoming tunnel.
7426 * FIXME send a path create to self, avoid code duplication
7428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7429 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7430 peer_info_get (&my_full_id),
7431 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7434 send_client_peer_connected (t, myid);
7435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7436 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7438 t->local_tid_dest = next_local_tid++;
7439 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7440 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7441 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7445 /* Ok, lets find a peer offering the service */
7446 if (NULL != t->dht_get_type)
7448 GNUNET_DHT_get_stop (t->dht_get_type);
7450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7451 GNUNET_h2s (&hash));
7453 GNUNET_DHT_get_start (dht_handle,
7454 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7456 dht_replication_level,
7457 GNUNET_DHT_RO_RECORD_ROUTE |
7458 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7460 &dht_get_type_handler, t);
7462 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7468 * Handler for connection requests to new peers by a string service description.
7470 * @param cls closure
7471 * @param client identification of the client
7472 * @param message the actual message, which includes messages the client wants
7475 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7476 const struct GNUNET_MessageHeader *message)
7478 struct GNUNET_MESH_ConnectPeerByString *msg;
7479 struct MeshRegexSearchContext *ctx;
7480 struct MeshRegexSearchInfo *info;
7481 struct GNUNET_DHT_GetHandle *get_h;
7482 struct GNUNET_HashCode key;
7483 struct MeshTunnel *t;
7484 struct MeshClient *c;
7485 MESH_TunnelNumber tid;
7489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7490 "Connect by string started\n");
7491 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7492 size = htons (message->size);
7494 /* Sanity check for client registration */
7495 if (NULL == (c = client_get (client)))
7498 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7503 /* Message size sanity check */
7504 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7511 /* Tunnel exists? */
7512 tid = ntohl (msg->tunnel_id);
7513 t = tunnel_get_by_local_id (c, tid);
7517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7521 /* Does client own tunnel? */
7522 if (t->owner->handle != client)
7525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7530 " on tunnel %s [%u]\n",
7531 GNUNET_i2s(&my_full_id),
7534 /* Only one connect_by_string allowed at the same time! */
7535 /* FIXME: allow more, return handle at api level to cancel, document */
7536 if (NULL != t->regex_ctx)
7539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7543 /* Find string itself */
7544 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7545 string = (const char *) &msg[1];
7547 /* Initialize context */
7548 size = GNUNET_REGEX_get_first_key(string, len, &key);
7549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7550 " consumed %u bits out of %u\n", size, len);
7551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7552 " looking for %s\n", GNUNET_h2s (&key));
7554 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7556 info->description = GNUNET_malloc (len + 1);
7557 memcpy (info->description, string, len);
7558 info->description[len] = '\0';
7559 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7560 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7563 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7564 ctx->position = size;
7568 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7570 /* Start search in DHT */
7571 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7572 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7573 &key, /* key to search */
7574 dht_replication_level, /* replication level */
7575 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7576 NULL, /* xquery */ // FIXME BLOOMFILTER
7577 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7578 &dht_get_string_handler, ctx);
7580 GNUNET_break (GNUNET_OK ==
7581 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7584 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7586 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7592 * Handler for client traffic directed to one peer
7594 * @param cls closure
7595 * @param client identification of the client
7596 * @param message the actual message
7599 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7600 const struct GNUNET_MessageHeader *message)
7602 struct MeshClient *c;
7603 struct MeshTunnel *t;
7604 struct MeshPeerInfo *pi;
7605 struct GNUNET_MESH_Unicast *data_msg;
7606 MESH_TunnelNumber tid;
7609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7610 "Got a unicast request from a client!\n");
7612 /* Sanity check for client registration */
7613 if (NULL == (c = client_get (client)))
7616 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7621 data_msg = (struct GNUNET_MESH_Unicast *) message;
7623 /* Sanity check for message size */
7624 size = ntohs (message->size);
7625 if (sizeof (struct GNUNET_MESH_Unicast) +
7626 sizeof (struct GNUNET_MessageHeader) > size)
7629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7633 /* Tunnel exists? */
7634 tid = ntohl (data_msg->tid);
7635 t = tunnel_get_by_local_id (c, tid);
7639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7643 /* Is it a local tunnel? Then, does client own the tunnel? */
7644 if (t->owner->handle != client)
7647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7651 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7652 &data_msg->destination.hashPubKey);
7653 /* Is the selected peer in the tunnel? */
7657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7661 /* PID should be as expected */
7662 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7665 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7666 "Unicast PID, expected %u, got %u\n",
7667 t->fwd_pid + 1, ntohl (data_msg->pid));
7668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7672 /* Ok, everything is correct, send the message
7673 * (pretend we got it from a mesh peer)
7676 /* Work around const limitation */
7677 char buf[ntohs (message->size)] GNUNET_ALIGN;
7678 struct GNUNET_MESH_Unicast *copy;
7680 copy = (struct GNUNET_MESH_Unicast *) buf;
7681 memcpy (buf, data_msg, size);
7682 copy->oid = my_full_id;
7683 copy->tid = htonl (t->id.tid);
7684 copy->ttl = htonl (default_ttl);
7685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7686 " calling generic handler...\n");
7687 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7690 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7697 * Handler for client traffic directed to the origin
7699 * @param cls closure
7700 * @param client identification of the client
7701 * @param message the actual message
7704 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7705 const struct GNUNET_MessageHeader *message)
7707 struct GNUNET_MESH_ToOrigin *data_msg;
7708 struct MeshTunnelClientInfo *clinfo;
7709 struct MeshClient *c;
7710 struct MeshTunnel *t;
7711 MESH_TunnelNumber tid;
7714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7715 "Got a ToOrigin request from a client!\n");
7716 /* Sanity check for client registration */
7717 if (NULL == (c = client_get (client)))
7720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7725 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7727 /* Sanity check for message size */
7728 size = ntohs (message->size);
7729 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7730 sizeof (struct GNUNET_MessageHeader) > size)
7733 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7737 /* Tunnel exists? */
7738 tid = ntohl (data_msg->tid);
7739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7740 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7743 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7746 t = tunnel_get_by_local_id (c, tid);
7749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7750 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7756 /* It should be sent by someone who has this as incoming tunnel. */
7757 if (GNUNET_NO == client_knows_tunnel (c, t))
7760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7764 /* PID should be as expected */
7765 clinfo = tunnel_get_client_fc (t, c);
7766 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7770 "To Origin PID, expected %u, got %u\n",
7771 clinfo->bck_pid + 1,
7772 ntohl (data_msg->pid));
7773 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7778 /* Ok, everything is correct, send the message
7779 * (pretend we got it from a mesh peer)
7782 char buf[ntohs (message->size)] GNUNET_ALIGN;
7783 struct GNUNET_MESH_ToOrigin *copy;
7785 /* Work around const limitation */
7786 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7787 memcpy (buf, data_msg, size);
7788 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7789 copy->tid = htonl (t->id.tid);
7790 copy->ttl = htonl (default_ttl);
7791 copy->pid = htonl (t->bck_pid + 1);
7793 copy->sender = my_full_id;
7794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7795 " calling generic handler...\n");
7796 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7798 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7805 * Handler for client traffic directed to all peers in a tunnel
7807 * @param cls closure
7808 * @param client identification of the client
7809 * @param message the actual message
7812 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7813 const struct GNUNET_MessageHeader *message)
7815 struct MeshClient *c;
7816 struct MeshTunnel *t;
7817 struct GNUNET_MESH_Multicast *data_msg;
7818 MESH_TunnelNumber tid;
7820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7821 "Got a multicast request from a client!\n");
7823 /* Sanity check for client registration */
7824 if (NULL == (c = client_get (client)))
7827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7832 data_msg = (struct GNUNET_MESH_Multicast *) message;
7834 /* Sanity check for message size */
7835 if (sizeof (struct GNUNET_MESH_Multicast) +
7836 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7843 /* Tunnel exists? */
7844 tid = ntohl (data_msg->tid);
7845 t = tunnel_get_by_local_id (c, tid);
7849 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7856 /* Does client own tunnel? */
7857 if (t->owner->handle != client)
7860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7864 /* PID should be as expected */
7865 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7868 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7869 "Multicast PID, expected %u, got %u\n",
7870 t->fwd_pid + 1, ntohl (data_msg->pid));
7871 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7876 char buf[ntohs (message->size)] GNUNET_ALIGN;
7877 struct GNUNET_MESH_Multicast *copy;
7879 copy = (struct GNUNET_MESH_Multicast *) buf;
7880 memcpy (buf, message, ntohs (message->size));
7881 copy->oid = my_full_id;
7882 copy->tid = htonl (t->id.tid);
7883 copy->ttl = htonl (default_ttl);
7884 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7886 " calling generic handler...\n");
7887 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7890 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7896 * Handler for client's ACKs for payload traffic.
7898 * @param cls Closure (unused).
7899 * @param client Identification of the client.
7900 * @param message The actual message.
7903 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7904 const struct GNUNET_MessageHeader *message)
7906 struct GNUNET_MESH_LocalAck *msg;
7907 struct MeshTunnel *t;
7908 struct MeshClient *c;
7909 MESH_TunnelNumber tid;
7912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7913 /* Sanity check for client registration */
7914 if (NULL == (c = client_get (client)))
7917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7922 msg = (struct GNUNET_MESH_LocalAck *) message;
7924 /* Tunnel exists? */
7925 tid = ntohl (msg->tunnel_id);
7926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7927 t = tunnel_get_by_local_id (c, tid);
7931 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7932 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7937 ack = ntohl (msg->max_pid);
7938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7940 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7941 if (NULL != t->owner && t->owner->handle == client)
7943 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7945 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7949 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7950 tunnel_set_client_fwd_ack (t, c, ack);
7951 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7954 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7961 * Functions to handle messages from clients
7963 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7964 {&handle_local_new_client, NULL,
7965 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7966 {&handle_local_announce_regex, NULL,
7967 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7968 {&handle_local_tunnel_create, NULL,
7969 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7970 sizeof (struct GNUNET_MESH_TunnelMessage)},
7971 {&handle_local_tunnel_destroy, NULL,
7972 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7973 sizeof (struct GNUNET_MESH_TunnelMessage)},
7974 {&handle_local_tunnel_speed, NULL,
7975 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7976 sizeof (struct GNUNET_MESH_TunnelMessage)},
7977 {&handle_local_tunnel_speed, NULL,
7978 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7979 sizeof (struct GNUNET_MESH_TunnelMessage)},
7980 {&handle_local_tunnel_buffer, NULL,
7981 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7982 sizeof (struct GNUNET_MESH_TunnelMessage)},
7983 {&handle_local_tunnel_buffer, NULL,
7984 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7985 sizeof (struct GNUNET_MESH_TunnelMessage)},
7986 {&handle_local_connect_add, NULL,
7987 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7988 sizeof (struct GNUNET_MESH_PeerControl)},
7989 {&handle_local_connect_del, NULL,
7990 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7991 sizeof (struct GNUNET_MESH_PeerControl)},
7992 {&handle_local_blacklist, NULL,
7993 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7994 sizeof (struct GNUNET_MESH_PeerControl)},
7995 {&handle_local_unblacklist, NULL,
7996 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7997 sizeof (struct GNUNET_MESH_PeerControl)},
7998 {&handle_local_connect_by_type, NULL,
7999 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8000 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8001 {&handle_local_connect_by_string, NULL,
8002 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8003 {&handle_local_unicast, NULL,
8004 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8005 {&handle_local_to_origin, NULL,
8006 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8007 {&handle_local_multicast, NULL,
8008 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8009 {&handle_local_ack, NULL,
8010 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8011 sizeof (struct GNUNET_MESH_LocalAck)},
8017 * To be called on core init/fail.
8019 * @param cls service closure
8020 * @param server handle to the server for this service
8021 * @param identity the public identity of this peer
8024 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8025 const struct GNUNET_PeerIdentity *identity)
8028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8029 core_handle = server;
8030 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8033 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8034 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8036 GNUNET_abort(); // Try harder
8043 * Method called whenever a given peer connects.
8045 * @param cls closure
8046 * @param peer peer identity this notification is about
8047 * @param atsi performance data for the connection
8048 * @param atsi_count number of records in 'atsi'
8051 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8052 const struct GNUNET_ATS_Information *atsi,
8053 unsigned int atsi_count)
8055 struct MeshPeerInfo *peer_info;
8056 struct MeshPeerPath *path;
8058 DEBUG_CONN ("Peer connected\n");
8059 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8060 peer_info = peer_info_get (peer);
8061 if (myid == peer_info->id)
8063 DEBUG_CONN (" (self)\n");
8068 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8070 path = path_new (2);
8071 path->peers[0] = myid;
8072 path->peers[1] = peer_info->id;
8073 GNUNET_PEER_change_rc (myid, 1);
8074 GNUNET_PEER_change_rc (peer_info->id, 1);
8075 peer_info_add_path (peer_info, path, GNUNET_YES);
8076 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8082 * Method called whenever a peer disconnects.
8084 * @param cls closure
8085 * @param peer peer identity this notification is about
8088 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8090 struct MeshPeerInfo *pi;
8091 struct MeshPeerQueue *q;
8092 struct MeshPeerQueue *n;
8094 DEBUG_CONN ("Peer disconnected\n");
8095 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8105 /* TODO try to reroute this traffic instead */
8106 queue_destroy(q, GNUNET_YES);
8109 if (NULL != pi->core_transmit)
8111 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8112 pi->core_transmit = NULL;
8114 peer_info_remove_path (pi, pi->id, myid);
8117 DEBUG_CONN (" (self)\n");
8119 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8124 /******************************************************************************/
8125 /************************ MAIN FUNCTIONS ****************************/
8126 /******************************************************************************/
8129 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8131 * @param cls closure
8132 * @param key current key code
8133 * @param value value in the hash map
8134 * @return GNUNET_YES if we should continue to iterate,
8138 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8140 struct MeshTunnel *t = value;
8147 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8149 * @param cls closure
8150 * @param key current key code
8151 * @param value value in the hash map
8152 * @return GNUNET_YES if we should continue to iterate,
8156 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8158 struct MeshPeerInfo *p = value;
8159 struct MeshPeerQueue *q;
8160 struct MeshPeerQueue *n;
8168 queue_destroy(q, GNUNET_YES);
8172 peer_info_destroy (p);
8178 * Task run during shutdown.
8184 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8188 if (core_handle != NULL)
8190 GNUNET_CORE_disconnect (core_handle);
8195 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8198 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8199 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8200 if (dht_handle != NULL)
8202 GNUNET_DHT_disconnect (dht_handle);
8207 GNUNET_SERVER_notification_context_destroy (nc);
8210 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8212 GNUNET_SCHEDULER_cancel (announce_id_task);
8213 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8220 * Callback for hostkey read/generation
8223 * @param pk the private key
8224 * @param emsg error message
8227 key_generation_cb (void *cls,
8228 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8231 struct MeshPeerInfo *peer;
8232 struct MeshPeerPath *p;
8237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8238 _("Mesh service could not access hostkey. Exiting.\n"));
8239 GNUNET_SCHEDULER_shutdown ();
8242 my_private_key = pk;
8243 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8244 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8245 &my_full_id.hashPubKey);
8246 myid = GNUNET_PEER_intern (&my_full_id);
8247 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8248 "Mesh for peer [%s] starting\n",
8249 GNUNET_i2s(&my_full_id));
8251 // transport_handle = GNUNET_TRANSPORT_connect(c,
8261 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8264 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8265 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8266 GNUNET_SERVER_disconnect_notify (server_handle,
8267 &handle_local_client_disconnect, NULL);
8271 clients_tail = NULL;
8274 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8275 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8277 /* Create a peer_info for the local peer */
8278 peer = peer_info_get (&my_full_id);
8281 GNUNET_PEER_change_rc (myid, 1);
8282 peer_info_add_path (peer, p, GNUNET_YES);
8283 GNUNET_SERVER_resume (server_handle);
8284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8289 * Process mesh requests.
8291 * @param cls closure
8292 * @param server the initialized server
8293 * @param c configuration to use
8296 run (void *cls, struct GNUNET_SERVER_Handle *server,
8297 const struct GNUNET_CONFIGURATION_Handle *c)
8301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8302 server_handle = server;
8303 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8304 NULL, /* Closure passed to MESH functions */
8305 &core_init, /* Call core_init once connected */
8306 &core_connect, /* Handle connects */
8307 &core_disconnect, /* remove peers on disconnects */
8308 NULL, /* Don't notify about all incoming messages */
8309 GNUNET_NO, /* For header only in notification */
8310 NULL, /* Don't notify about all outbound messages */
8311 GNUNET_NO, /* For header-only out notification */
8312 core_handlers); /* Register these handlers */
8314 if (core_handle == NULL)
8317 GNUNET_SCHEDULER_shutdown ();
8322 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8327 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8329 GNUNET_SCHEDULER_shutdown ();
8334 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8335 &refresh_path_time))
8337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8339 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8340 "mesh", "refresh path time");
8341 GNUNET_SCHEDULER_shutdown ();
8346 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8347 &app_announce_time))
8349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8351 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8352 "mesh", "app announce time");
8353 GNUNET_SCHEDULER_shutdown ();
8356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8359 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8362 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8364 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8365 "mesh", "id announce time");
8366 GNUNET_SCHEDULER_shutdown ();
8374 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8379 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8380 "mesh", "connect timeout");
8381 GNUNET_SCHEDULER_shutdown ();
8386 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8391 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8392 "mesh", "max msgs queue");
8393 GNUNET_SCHEDULER_shutdown ();
8398 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8401 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8403 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8404 "mesh", "max tunnels");
8405 GNUNET_SCHEDULER_shutdown ();
8410 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8413 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8415 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8416 "mesh", "default ttl", 64);
8421 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8422 &dht_replication_level))
8424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8426 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8427 "mesh", "dht replication level", 10);
8428 dht_replication_level = 10;
8431 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8432 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8433 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8434 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8435 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8437 dht_handle = GNUNET_DHT_connect (c, 64);
8438 if (NULL == dht_handle)
8442 stats = GNUNET_STATISTICS_create ("mesh", c);
8444 GNUNET_SERVER_suspend (server_handle);
8445 /* Scheduled the task to clean up when shutdown is called */
8446 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8448 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8449 GNUNET_free (keyfile);
8454 * The main function for the mesh service.
8456 * @param argc number of arguments from the command line
8457 * @param argv command line arguments
8458 * @return 0 ok, 1 on error
8461 main (int argc, char *const *argv)
8466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8467 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8469 ret = (GNUNET_OK == r) ? 0 : 1;
8470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8474 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8475 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8476 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);