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 if (-1LL == client_ack) // No children AND no clients? Not good!
3882 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3886 return (uint32_t) client_ack;
3888 if (-1LL == client_ack)
3892 if (GNUNET_YES == t->speed_min)
3894 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3895 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3899 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3900 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3903 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3904 count, buffer_free, child_ack, client_ack, ack);
3910 * Build a local ACK message and send it to a local client.
3912 * @param t Tunnel on which to send the ACK.
3913 * @param c Client to whom send the ACK.
3914 * @param ack Value of the ACK.
3917 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3919 struct GNUNET_MESH_LocalAck msg;
3921 msg.header.size = htons (sizeof (msg));
3922 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3923 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3924 msg.max_pid = htonl (ack);
3925 GNUNET_SERVER_notification_context_unicast(nc,
3932 * Build an ACK message and queue it to send to the given peer.
3934 * @param t Tunnel on which to send the ACK.
3935 * @param peer Peer to whom send the ACK.
3936 * @param ack Value of the ACK.
3939 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3941 struct GNUNET_MESH_ACK msg;
3943 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3944 msg.header.size = htons (sizeof (msg));
3945 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3946 msg.pid = htonl (ack);
3947 msg.tid = htonl (t->id.tid);
3949 send_prebuilt_message (&msg.header, peer, t);
3954 * Notify a the owner of a tunnel about how many more
3955 * payload packages will we accept on a given tunnel.
3957 * @param t Tunnel on which to send the ACK.
3960 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3965 "Sending client FWD ACK on tunnel %X\n",
3968 ack = tunnel_get_fwd_ack (t);
3970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3971 if (t->last_fwd_ack == ack)
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3978 t->last_fwd_ack = ack;
3979 send_local_ack (t, t->owner, ack);
3984 * Send an ACK informing the predecessor about the available buffer space.
3985 * In case there is no predecessor, inform the owning client.
3986 * If buffering is off, send only on behalf of children or self if endpoint.
3987 * If buffering is on, send when sent to children and buffer space is free.
3988 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3989 * the ACK itself goes "back" (towards root).
3991 * @param t Tunnel on which to send the ACK.
3992 * @param type Type of message that triggered the ACK transmission.
3995 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3997 struct GNUNET_PeerIdentity id;
4000 if (NULL != t->owner)
4002 tunnel_send_client_fwd_ack (t);
4005 /* Is it after unicast / multicast retransmission? */
4008 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4009 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4011 "ACK due to FWD DATA retransmission\n");
4012 if (GNUNET_YES == t->nobuffer)
4014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4018 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4019 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4021 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4022 t->force_ack = GNUNET_YES;
4028 /* Check if we need to transmit the ACK */
4029 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4030 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4031 GNUNET_NO == t->force_ack)
4033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4035 " t->qmax: %u, t->qn: %u\n",
4036 t->fwd_queue_max, t->fwd_queue_n);
4037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4038 " t->pid: %u, t->ack: %u\n",
4039 t->fwd_pid, t->last_fwd_ack);
4043 /* Ok, ACK might be necessary, what PID to ACK? */
4044 ack = tunnel_get_fwd_ack (t);
4046 /* If speed_min and not all children have ack'd, dont send yet */
4047 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4053 t->last_fwd_ack = ack;
4054 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4055 send_ack (t, &id, ack);
4057 t->force_ack = GNUNET_NO;
4062 * Iterator to send a child node a BCK ACK to allow him to send more
4065 * @param cls Closure (tunnel).
4066 * @param id Id of the child node.
4069 tunnel_send_child_bck_ack (void *cls,
4072 struct MeshTunnel *t = cls;
4073 struct MeshTunnelChildInfo *cinfo;
4074 struct GNUNET_PeerIdentity peer;
4077 GNUNET_PEER_resolve (id, &peer);
4078 cinfo = tunnel_get_neighbor_fc (t, &peer);
4079 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4081 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4084 " Not sending ACK, not needed\n");
4087 cinfo->bck_ack = ack;
4089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4090 " Sending BCK ACK %u (last sent: %u)\n",
4091 ack, cinfo->bck_ack);
4092 send_ack (t, &peer, ack);
4097 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4099 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4101 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4102 * - implement a size 1 buffer in each client_fc AND children_fc
4103 * to hold at least 1 message per "child".
4104 * problem: violates no buffer policy
4105 * - ack 0 and make "children" poll for transmission slots
4106 * problem: big overhead, extra latency even in low traffic
4109 * @param t Tunnel on which to send the BCK ACKs.
4112 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4115 unsigned int tunnel_delta;
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4119 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4122 /* Find client whom to allow to send to origin (with lowest buffer space) */
4123 for (i = 0; i < t->nclients; i++)
4125 struct MeshTunnelClientInfo *clinfo;
4128 clinfo = &t->clients_fc[i];
4129 delta = clinfo->bck_ack - clinfo->bck_pid;
4130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4131 t->clients[i]->id, delta);
4133 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4134 (GNUNET_YES == t->nobuffer && 0 == delta))
4138 ack = clinfo->bck_pid;
4139 ack += t->nobuffer ? 1 : tunnel_delta;
4140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4141 " sending ack to client %u: %u\n",
4142 t->clients[i]->id, ack);
4143 send_local_ack (t, t->clients[i], ack);
4144 clinfo->bck_ack = ack;
4148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4149 " not sending ack to client %u (td %u, d %u)\n",
4150 t->clients[i]->id, tunnel_delta, delta);
4157 * Send an ACK informing the children nodes and destination clients about
4158 * the available buffer space.
4159 * If buffering is off, send only on behalf of root (can be self).
4160 * If buffering is on, send when sent to predecessor and buffer space is free.
4161 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4162 * the ACK itself goes "forward" (towards children/clients).
4164 * @param t Tunnel on which to send the ACK.
4165 * @param type Type of message that triggered the ACK transmission.
4168 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4171 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4172 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4173 /* Is it after data to_origin retransmission? */
4176 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4177 if (GNUNET_YES == t->nobuffer)
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4180 " Not sending ACK, nobuffer\n");
4184 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4185 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4187 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4188 t->force_ack = GNUNET_YES;
4194 tunnel_send_clients_bck_ack (t);
4195 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4196 t->force_ack = GNUNET_NO;
4201 * @brief Re-initiate traffic to this peer if necessary.
4203 * Check if there is traffic queued towards this peer
4204 * and the core transmit handle is NULL (traffic was stalled).
4205 * If so, call core tmt rdy.
4207 * @param cls Closure (unused)
4208 * @param peer_id Short ID of peer to which initiate traffic.
4211 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4213 struct MeshPeerInfo *peer;
4214 struct GNUNET_PeerIdentity id;
4215 struct MeshPeerQueue *q;
4218 peer = peer_info_get_short(peer_id);
4219 if (NULL != peer->core_transmit)
4222 q = queue_get_next(peer);
4225 /* Might br multicast traffic already sent to this particular peer but
4226 * not to other children in this tunnel.
4227 * This way t->queue_n would be > 0 but the queue of this particular peer
4233 GNUNET_PEER_resolve (peer->id, &id);
4234 peer->core_transmit =
4235 GNUNET_CORE_notify_transmit_ready(core_handle,
4238 GNUNET_TIME_UNIT_FOREVER_REL,
4248 * @brief Allow transmission of FWD traffic on this tunnel
4250 * Check if there is traffic queued towards any children
4251 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4253 * @param t Tunnel on which to unlock FWD traffic.
4256 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4258 if (0 == t->fwd_queue_n)
4261 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4266 * @brief Allow transmission of BCK traffic on this tunnel
4268 * Check if there is traffic queued towards the root of the tree
4269 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4271 * @param t Tunnel on which to unlock BCK traffic.
4274 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4276 if (0 == t->bck_queue_n)
4279 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4284 * Send a message to all peers in this tunnel that the tunnel is no longer
4287 * @param t The tunnel whose peers to notify.
4290 tunnel_send_destroy (struct MeshTunnel *t)
4292 struct GNUNET_MESH_TunnelDestroy msg;
4294 msg.header.size = htons (sizeof (msg));
4295 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4296 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4297 msg.tid = htonl (t->id.tid);
4298 tunnel_send_multicast (t, &msg.header);
4303 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4305 * @param cls Closure (Tunnel which to cancel).
4306 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4309 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4311 struct MeshTunnel *t = cls;
4312 struct MeshPeerInfo *peer_info;
4313 struct MeshPeerQueue *pq;
4314 struct MeshPeerQueue *next;
4316 peer_info = peer_info_get_short (neighbor_id);
4317 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4320 if (pq->tunnel == t)
4322 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4323 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4324 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4326 // Should have been removed on destroy children
4329 queue_destroy (pq, GNUNET_YES);
4332 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4334 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4335 peer_info->core_transmit = NULL;
4340 * Destroy the tunnel and free any allocated resources linked to it.
4342 * @param t the tunnel to destroy
4344 * @return GNUNET_OK on success
4347 tunnel_destroy (struct MeshTunnel *t)
4349 struct MeshClient *c;
4350 struct GNUNET_HashCode hash;
4361 struct GNUNET_PeerIdentity id;
4363 GNUNET_PEER_resolve (t->id.oid, &id);
4364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4365 GNUNET_i2s (&id), t->id.tid);
4367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4371 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4372 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4380 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4382 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4389 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4390 for (i = 0; i < t->nclients; i++)
4394 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4400 for (i = 0; i < t->nignore; i++)
4404 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4411 if (t->nclients > 0)
4414 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4419 GNUNET_free (t->clients);
4420 GNUNET_free (t->clients_fc);
4423 if (NULL != t->peers)
4425 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4427 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4430 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4431 &tunnel_destroy_child,
4433 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4434 t->children_fc = NULL;
4436 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4437 tree_destroy (t->tree);
4439 if (NULL != t->regex_ctx)
4440 regex_cancel_search (t->regex_ctx);
4441 if (NULL != t->dht_get_type)
4442 GNUNET_DHT_get_stop (t->dht_get_type);
4443 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4444 GNUNET_SCHEDULER_cancel (t->timeout_task);
4445 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4446 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4449 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4456 * Create a new tunnel
4458 * @param owner Who is the owner of the tunnel (short ID).
4459 * @param tid Tunnel Number of the tunnel.
4460 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4461 * @param local Tunnel Number for the tunnel, for the client point of view.
4463 * @return A new initialized tunnel. NULL on error.
4465 static struct MeshTunnel *
4466 tunnel_new (GNUNET_PEER_Id owner,
4467 MESH_TunnelNumber tid,
4468 struct MeshClient *client,
4469 MESH_TunnelNumber local)
4471 struct MeshTunnel *t;
4472 struct GNUNET_HashCode hash;
4474 if (n_tunnels >= max_tunnels && NULL == client)
4477 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4480 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4481 t->bck_queue_max = t->fwd_queue_max;
4482 t->tree = tree_new (owner);
4484 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4485 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4486 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4487 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4488 t->local_tid = local;
4489 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4491 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4493 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4495 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4496 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4503 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4510 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4512 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4513 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4517 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4527 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4528 * that are no longer needed, as well as nodes of no longer reachable peers.
4529 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4531 * @param t Tunnel from which to remove the path.
4532 * @param peer Short id of the peer which should be removed.
4535 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4539 r = tree_del_peer (t->tree, peer, NULL, NULL);
4542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4543 "Tunnel %u [%u] has no more nodes\n",
4544 t->id.oid, t->id.tid);
4550 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4551 * client when the client disconnects. If the client is not the owner, the
4552 * owner will get notified if no more clients are in the tunnel and the client
4553 * get removed from the tunnel's list.
4555 * @param cls closure (client that is disconnecting)
4556 * @param key the hash of the local tunnel id (used to access the hashmap)
4557 * @param value the value stored at the key (tunnel to destroy)
4559 * @return GNUNET_OK, keep iterating.
4562 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4564 struct MeshTunnel *t = value;
4565 struct MeshClient *c = cls;
4567 send_client_tunnel_disconnect(t, c);
4570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4571 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4572 tunnel_delete_client(t, c);
4573 client_delete_tunnel(c, t);
4576 tunnel_send_destroy(t);
4578 t->destroy = GNUNET_YES;
4585 * Timeout function, destroys tunnel if called
4587 * @param cls Closure (tunnel to destroy).
4588 * @param tc TaskContext
4591 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4593 struct MeshTunnel *t = cls;
4594 struct GNUNET_PeerIdentity id;
4596 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4597 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4599 GNUNET_PEER_resolve(t->id.oid, &id);
4600 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4601 "Tunnel %s [%X] timed out. Destroying.\n",
4602 GNUNET_i2s(&id), t->id.tid);
4603 send_clients_tunnel_destroy (t);
4608 * Resets the tunnel timeout. Starts it if no timeout was running.
4610 * @param t Tunnel whose timeout to reset.
4612 * TODO use heap to improve efficiency of scheduler.
4615 tunnel_reset_timeout (struct MeshTunnel *t)
4617 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4618 GNUNET_SCHEDULER_cancel (t->timeout_task);
4620 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4621 (refresh_path_time, 4), &tunnel_timeout, t);
4625 /******************************************************************************/
4626 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4627 /******************************************************************************/
4630 * Function to send a create path packet to a peer.
4632 * @param cls closure
4633 * @param size number of bytes available in buf
4634 * @param buf where the callee should write the message
4635 * @return number of bytes written to buf
4638 send_core_path_create (void *cls, size_t size, void *buf)
4640 struct MeshPathInfo *info = cls;
4641 struct GNUNET_MESH_ManipulatePath *msg;
4642 struct GNUNET_PeerIdentity *peer_ptr;
4643 struct MeshTunnel *t = info->t;
4644 struct MeshPeerPath *p = info->path;
4649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4651 sizeof (struct GNUNET_MESH_ManipulatePath) +
4652 p->length * sizeof (struct GNUNET_PeerIdentity);
4654 if (size < size_needed || NULL == buf)
4659 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4660 msg->header.size = htons (size_needed);
4661 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4662 msg->tid = ntohl (t->id.tid);
4665 if (GNUNET_YES == t->speed_min)
4666 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4667 if (GNUNET_YES == t->nobuffer)
4668 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4669 msg->opt = htonl(opt);
4672 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4673 for (i = 0; i < p->length; i++)
4675 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4682 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4688 * Fill the core buffer
4690 * @param cls closure (data itself)
4691 * @param size number of bytes available in buf
4692 * @param buf where the callee should write the message
4694 * @return number of bytes written to buf
4697 send_core_data_multicast (void *cls, size_t size, void *buf)
4699 struct MeshTransmissionDescriptor *info = cls;
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4703 GNUNET_assert (NULL != info);
4704 GNUNET_assert (NULL != info->peer);
4705 total_size = info->mesh_data->data_len;
4706 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4708 if (total_size > size)
4713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4714 memcpy (buf, info->mesh_data->data, total_size);
4717 struct GNUNET_MESH_Multicast *mc;
4718 struct GNUNET_MessageHeader *mh;
4721 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4723 mc = (struct GNUNET_MESH_Multicast *) mh;
4724 mh = (struct GNUNET_MessageHeader *) &mc[1];
4725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4726 " multicast, payload type %s\n",
4727 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4729 " multicast, payload size %u\n", ntohs (mh->size));
4733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4734 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4738 data_descriptor_decrement_rc (info->mesh_data);
4739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4747 * Creates a path ack message in buf and frees all unused resources.
4749 * @param cls closure (MeshTransmissionDescriptor)
4750 * @param size number of bytes available in buf
4751 * @param buf where the callee should write the message
4752 * @return number of bytes written to buf
4755 send_core_path_ack (void *cls, size_t size, void *buf)
4757 struct MeshTransmissionDescriptor *info = cls;
4758 struct GNUNET_MESH_PathACK *msg = buf;
4760 GNUNET_assert (NULL != info);
4761 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4766 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4767 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4768 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4769 msg->tid = htonl (info->origin->tid);
4770 msg->peer_id = my_full_id;
4773 /* TODO add signature */
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4776 return sizeof (struct GNUNET_MESH_PathACK);
4781 * Free a transmission that was already queued with all resources
4782 * associated to the request.
4784 * @param queue Queue handler to cancel.
4785 * @param clear_cls Is it necessary to free associated cls?
4788 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4790 struct MeshTransmissionDescriptor *dd;
4791 struct MeshPathInfo *path_info;
4792 struct MeshTunnelChildInfo *cinfo;
4793 struct GNUNET_PeerIdentity id;
4797 if (GNUNET_YES == clear_cls)
4799 switch (queue->type)
4801 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4802 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4803 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4805 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4806 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4807 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4808 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4809 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4810 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4812 " prebuilt message\n");
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4815 GNUNET_MESH_DEBUG_M2S(queue->type));
4817 data_descriptor_decrement_rc (dd->mesh_data);
4819 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4821 path_info = queue->cls;
4822 path_destroy (path_info->path);
4826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4827 " type %s unknown!\n",
4828 GNUNET_MESH_DEBUG_M2S(queue->type));
4830 GNUNET_free_non_null (queue->cls);
4832 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4833 queue->peer->queue_tail,
4836 /* Delete from child_fc in the appropiate tunnel */
4837 max = queue->tunnel->fwd_queue_max;
4838 GNUNET_PEER_resolve (queue->peer->id, &id);
4839 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4842 for (i = 0; i < cinfo->send_buffer_n; i++)
4845 i2 = (cinfo->send_buffer_start + i) % max;
4846 if (cinfo->send_buffer[i2] == queue)
4848 /* Found corresponding entry in the send_buffer. Move all others back. */
4853 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4855 j2 = (cinfo->send_buffer_start + j) % max;
4856 j3 = (cinfo->send_buffer_start + j + 1) % max;
4857 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4860 cinfo->send_buffer[j3] = NULL;
4861 cinfo->send_buffer_n--;
4866 GNUNET_free (queue);
4871 * @brief Get the next transmittable message from the queue.
4873 * This will be the head, except in the case of being a data packet
4874 * not allowed by the destination peer.
4876 * @param peer Destination peer.
4878 * @return The next viable MeshPeerQueue element to send to that peer.
4879 * NULL when there are no transmittable messages.
4881 struct MeshPeerQueue *
4882 queue_get_next (const struct MeshPeerInfo *peer)
4884 struct MeshPeerQueue *q;
4885 struct MeshTunnel *t;
4886 struct MeshTransmissionDescriptor *info;
4887 struct MeshTunnelChildInfo *cinfo;
4888 struct GNUNET_MESH_Unicast *ucast;
4889 struct GNUNET_MESH_ToOrigin *to_orig;
4890 struct GNUNET_MESH_Multicast *mcast;
4891 struct GNUNET_PeerIdentity id;
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4896 for (q = peer->queue_head; NULL != q; q = q->next)
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4902 GNUNET_MESH_DEBUG_M2S(q->type));
4905 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4906 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4907 pid = ntohl (ucast->pid);
4908 GNUNET_PEER_resolve (info->peer->id, &id);
4909 cinfo = tunnel_get_neighbor_fc(t, &id);
4910 ack = cinfo->fwd_ack;
4912 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4913 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4914 pid = ntohl (to_orig->pid);
4917 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4918 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4919 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4921 // Not a multicast payload: multicast control traffic (destroy, etc)
4924 pid = ntohl (mcast->pid);
4925 GNUNET_PEER_resolve (info->peer->id, &id);
4926 cinfo = tunnel_get_neighbor_fc(t, &id);
4927 ack = cinfo->fwd_ack;
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4935 "********* ACK: %u, PID: %u\n",
4937 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4946 "********* NEXT!\n");
4949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4950 "********* nothing found\n");
4956 * Core callback to write a queued packet to core buffer
4958 * @param cls Closure (peer info).
4959 * @param size Number of bytes available in buf.
4960 * @param buf Where the to write the message.
4962 * @return number of bytes written to buf
4965 queue_send (void *cls, size_t size, void *buf)
4967 struct MeshPeerInfo *peer = cls;
4968 struct GNUNET_MessageHeader *msg;
4969 struct MeshPeerQueue *queue;
4970 struct MeshTunnel *t;
4971 struct MeshTunnelChildInfo *cinfo;
4972 struct GNUNET_PeerIdentity dst_id;
4975 peer->core_transmit = NULL;
4978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4979 queue = queue_get_next (peer);
4981 /* Queue has no internal mesh traffic nor sendable payload */
4984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4985 if (NULL == peer->queue_head)
4986 GNUNET_break (0); // Should've been canceled
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4991 GNUNET_PEER_resolve (peer->id, &dst_id);
4992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4993 "********* towards %s\n",
4994 GNUNET_i2s(&dst_id));
4995 /* Check if buffer size is enough for the message */
4996 if (queue->size > size)
4998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4999 "********* not enough room, reissue\n");
5000 peer->core_transmit =
5001 GNUNET_CORE_notify_transmit_ready (core_handle,
5004 GNUNET_TIME_UNIT_FOREVER_REL,
5011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5014 GNUNET_assert (0 < t->pending_messages);
5015 t->pending_messages--;
5016 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5020 "********* unicast: t->q (%u/%u)\n",
5021 t->fwd_queue_n, t->fwd_queue_max);
5023 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5030 switch (queue->type)
5033 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5034 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5035 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5036 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5037 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5039 "********* raw: %s\n",
5040 GNUNET_MESH_DEBUG_M2S (queue->type));
5042 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5043 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5044 data_size = send_core_data_raw (queue->cls, size, buf);
5045 msg = (struct GNUNET_MessageHeader *) buf;
5046 switch (ntohs (msg->type)) // Type of preconstructed message
5048 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5049 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5051 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5052 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5058 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5061 struct MeshTransmissionDescriptor *info = queue->cls;
5063 if ((1 == info->mesh_data->reference_counter
5064 && GNUNET_YES == t->speed_min)
5066 (info->mesh_data->total_out == info->mesh_data->reference_counter
5067 && GNUNET_NO == t->speed_min))
5069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5070 "********* considered sent\n");
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5076 "********* NOT considered sent yet\n");
5077 t->pending_messages++;
5080 data_size = send_core_data_multicast(queue->cls, size, buf);
5081 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5083 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5085 data_size = send_core_path_create (queue->cls, size, buf);
5087 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5089 data_size = send_core_path_ack (queue->cls, size, buf);
5091 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5093 data_size = send_core_data_multicast (queue->cls, size, buf);
5097 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5098 "********* type unknown: %u\n",
5102 switch (queue->type)
5104 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5105 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5106 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5107 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5108 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5112 "at pos %u (%p) != %p\n",
5113 cinfo->send_buffer_start,
5114 cinfo->send_buffer[cinfo->send_buffer_start],
5117 if (cinfo->send_buffer_n > 0)
5119 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5120 cinfo->send_buffer_n--;
5121 cinfo->send_buffer_start++;
5122 cinfo->send_buffer_start %= t->fwd_queue_max;
5133 /* Free queue, but cls was freed by send_core_* */
5134 queue_destroy (queue, GNUNET_NO);
5136 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5142 /* If more data in queue, send next */
5143 queue = queue_get_next(peer);
5146 struct GNUNET_PeerIdentity id;
5148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5149 GNUNET_PEER_resolve (peer->id, &id);
5150 peer->core_transmit =
5151 GNUNET_CORE_notify_transmit_ready(core_handle,
5154 GNUNET_TIME_UNIT_FOREVER_REL,
5162 if (NULL != peer->queue_head)
5164 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5165 "********* %s stalled\n",
5166 GNUNET_i2s(&my_full_id));
5168 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5169 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(MESH_POLL_TIME,
5170 &tunnel_poll, cinfo);
5173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5179 * @brief Queue and pass message to core when possible.
5181 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5182 * and accounts for it. In case the queue is full, the message is dropped and
5185 * Otherwise, message is treated as internal and allowed to go regardless of
5188 * @param cls Closure (@c type dependant). It will be used by queue_send to
5189 * build the message to be sent if not already prebuilt.
5190 * @param type Type of the message, 0 for a raw message.
5191 * @param size Size of the message.
5192 * @param dst Neighbor to send message to.
5193 * @param t Tunnel this message belongs to.
5196 queue_add (void *cls, uint16_t type, size_t size,
5197 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5199 struct MeshPeerQueue *queue;
5200 struct MeshTunnelChildInfo *cinfo;
5201 struct GNUNET_PeerIdentity id;
5207 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5208 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5210 n = &t->fwd_queue_n;
5211 max = &t->fwd_queue_max;
5213 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5215 n = &t->bck_queue_n;
5216 max = &t->bck_queue_max;
5223 GNUNET_STATISTICS_update(stats,
5224 "# messages dropped (buffer full)",
5226 return; // Drop message
5230 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5236 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5237 GNUNET_PEER_resolve (dst->id, &id);
5238 if (NULL == dst->core_transmit)
5240 dst->core_transmit =
5241 GNUNET_CORE_notify_transmit_ready (core_handle,
5244 GNUNET_TIME_UNIT_FOREVER_REL,
5250 t->pending_messages++;
5251 if (NULL == n) // Is this internal mesh traffic?
5254 // It's payload, keep track of buffer per peer.
5255 cinfo = tunnel_get_neighbor_fc(t, &id);
5256 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5257 if (NULL != cinfo->send_buffer[i])
5259 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5260 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5261 cinfo->send_buffer_start++;
5262 cinfo->send_buffer_start %= t->fwd_queue_max;
5266 cinfo->send_buffer_n++;
5268 cinfo->send_buffer[i] = queue;
5269 if (cinfo->send_buffer_n > t->fwd_queue_max)
5272 cinfo->send_buffer_n = t->fwd_queue_max;
5277 /******************************************************************************/
5278 /******************** MESH NETWORK HANDLERS **************************/
5279 /******************************************************************************/
5283 * Core handler for path creation
5285 * @param cls closure
5286 * @param message message
5287 * @param peer peer identity this notification is about
5288 * @param atsi performance data
5289 * @param atsi_count number of records in 'atsi'
5291 * @return GNUNET_OK to keep the connection open,
5292 * GNUNET_SYSERR to close it (signal serious error)
5295 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5296 const struct GNUNET_MessageHeader *message,
5297 const struct GNUNET_ATS_Information *atsi,
5298 unsigned int atsi_count)
5300 unsigned int own_pos;
5303 MESH_TunnelNumber tid;
5304 struct GNUNET_MESH_ManipulatePath *msg;
5305 struct GNUNET_PeerIdentity *pi;
5306 struct GNUNET_HashCode hash;
5307 struct MeshPeerPath *path;
5308 struct MeshPeerInfo *dest_peer_info;
5309 struct MeshPeerInfo *orig_peer_info;
5310 struct MeshTunnel *t;
5312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5313 "Received a path create msg [%s]\n",
5314 GNUNET_i2s (&my_full_id));
5315 size = ntohs (message->size);
5316 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5318 GNUNET_break_op (0);
5322 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5323 if (size % sizeof (struct GNUNET_PeerIdentity))
5325 GNUNET_break_op (0);
5328 size /= sizeof (struct GNUNET_PeerIdentity);
5331 GNUNET_break_op (0);
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5335 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5337 tid = ntohl (msg->tid);
5338 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5340 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5341 t = tunnel_get (pi, tid);
5342 if (NULL == t) // FIXME only for INCOMING tunnels?
5346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5347 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5350 // FIXME notify failure
5353 opt = ntohl (msg->opt);
5354 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5355 GNUNET_YES : GNUNET_NO;
5356 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5358 t->nobuffer = GNUNET_YES;
5359 t->last_fwd_ack = t->fwd_pid + 1;
5361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5362 " speed_min: %d, nobuffer:%d\n",
5363 t->speed_min, t->nobuffer);
5365 if (GNUNET_YES == t->nobuffer)
5367 t->bck_queue_max = 1;
5368 t->fwd_queue_max = 1;
5371 // FIXME only assign a local tid if a local client is interested (on demand)
5372 while (NULL != tunnel_get_incoming (next_local_tid))
5373 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5374 t->local_tid_dest = next_local_tid++;
5375 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5378 tunnel_reset_timeout (t);
5379 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5381 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5390 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5391 if (NULL == dest_peer_info)
5393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5394 " Creating PeerInfo for destination.\n");
5395 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5396 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5397 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5399 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5401 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5402 if (NULL == orig_peer_info)
5404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5405 " Creating PeerInfo for origin.\n");
5406 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5407 orig_peer_info->id = GNUNET_PEER_intern (pi);
5408 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5412 path = path_new (size);
5414 for (i = 0; i < size; i++)
5416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5417 GNUNET_i2s (&pi[i]));
5418 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5419 if (path->peers[i] == myid)
5422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5425 /* cannot be self, must be 'not found' */
5426 /* create path: self not found in path through self */
5427 GNUNET_break_op (0);
5428 path_destroy (path);
5432 path_add_to_peers (path, GNUNET_NO);
5433 tunnel_add_path (t, path, own_pos);
5434 if (own_pos == size - 1)
5436 /* It is for us! Send ack. */
5437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5438 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5439 if (NULL == t->peers)
5441 /* New tunnel! Notify clients on first payload message. */
5442 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5444 GNUNET_break (GNUNET_SYSERR !=
5445 GNUNET_CONTAINER_multihashmap_put (t->peers,
5446 &my_full_id.hashPubKey,
5449 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5454 struct MeshPeerPath *path2;
5456 /* It's for somebody else! Retransmit. */
5457 path2 = path_duplicate (path);
5458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5459 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5460 path2 = path_duplicate (path);
5461 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5462 send_create_path (dest_peer_info, path, t);
5469 * Core handler for path destruction
5471 * @param cls closure
5472 * @param message message
5473 * @param peer peer identity this notification is about
5474 * @param atsi performance data
5475 * @param atsi_count number of records in 'atsi'
5477 * @return GNUNET_OK to keep the connection open,
5478 * GNUNET_SYSERR to close it (signal serious error)
5481 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5482 const struct GNUNET_MessageHeader *message,
5483 const struct GNUNET_ATS_Information *atsi,
5484 unsigned int atsi_count)
5486 struct GNUNET_MESH_ManipulatePath *msg;
5487 struct GNUNET_PeerIdentity *pi;
5488 struct MeshPeerPath *path;
5489 struct MeshTunnel *t;
5490 unsigned int own_pos;
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5495 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5496 size = ntohs (message->size);
5497 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5499 GNUNET_break_op (0);
5503 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5504 if (size % sizeof (struct GNUNET_PeerIdentity))
5506 GNUNET_break_op (0);
5509 size /= sizeof (struct GNUNET_PeerIdentity);
5512 GNUNET_break_op (0);
5515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5517 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5518 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5520 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5522 t = tunnel_get (pi, ntohl (msg->tid));
5525 /* TODO notify back: we don't know this tunnel */
5526 GNUNET_break_op (0);
5529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5530 path = path_new (size);
5532 for (i = 0; i < size; i++)
5534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5535 GNUNET_i2s (&pi[i]));
5536 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5537 if (path->peers[i] == myid)
5540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5541 if (own_pos < path->length - 1)
5542 send_prebuilt_message (message, &pi[own_pos + 1], t);
5544 send_client_tunnel_disconnect(t, NULL);
5546 tunnel_delete_peer (t, path->peers[path->length - 1]);
5547 path_destroy (path);
5553 * Core handler for notifications of broken paths
5555 * @param cls closure
5556 * @param message message
5557 * @param peer peer identity this notification is about
5558 * @param atsi performance data
5559 * @param atsi_count number of records in 'atsi'
5561 * @return GNUNET_OK to keep the connection open,
5562 * GNUNET_SYSERR to close it (signal serious error)
5565 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5566 const struct GNUNET_MessageHeader *message,
5567 const struct GNUNET_ATS_Information *atsi,
5568 unsigned int atsi_count)
5570 struct GNUNET_MESH_PathBroken *msg;
5571 struct MeshTunnel *t;
5573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5574 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5575 msg = (struct GNUNET_MESH_PathBroken *) message;
5576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5577 GNUNET_i2s (&msg->peer1));
5578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5579 GNUNET_i2s (&msg->peer2));
5580 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5583 GNUNET_break_op (0);
5586 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5587 GNUNET_PEER_search (&msg->peer2));
5594 * Core handler for tunnel destruction
5596 * @param cls closure
5597 * @param message message
5598 * @param peer peer identity this notification is about
5599 * @param atsi performance data
5600 * @param atsi_count number of records in 'atsi'
5602 * @return GNUNET_OK to keep the connection open,
5603 * GNUNET_SYSERR to close it (signal serious error)
5606 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5607 const struct GNUNET_MessageHeader *message,
5608 const struct GNUNET_ATS_Information *atsi,
5609 unsigned int atsi_count)
5611 struct GNUNET_MESH_TunnelDestroy *msg;
5612 struct MeshTunnel *t;
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5615 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5616 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5618 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5619 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5622 /* Probably already got the message from another path,
5623 * destroyed the tunnel and retransmitted to children.
5626 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5629 if (t->id.oid == myid)
5631 GNUNET_break_op (0);
5634 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5636 /* Tunnel was incoming, notify clients */
5637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5638 t->local_tid, t->local_tid_dest);
5639 send_clients_tunnel_destroy (t);
5641 tunnel_send_destroy (t);
5642 t->destroy = GNUNET_YES;
5643 // TODO: add timeout to destroy the tunnel anyway
5649 * Core handler for mesh network traffic going from the origin to a peer
5651 * @param cls closure
5652 * @param peer peer identity this notification is about
5653 * @param message message
5654 * @param atsi performance data
5655 * @param atsi_count number of records in 'atsi'
5656 * @return GNUNET_OK to keep the connection open,
5657 * GNUNET_SYSERR to close it (signal serious error)
5660 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5661 const struct GNUNET_MessageHeader *message,
5662 const struct GNUNET_ATS_Information *atsi,
5663 unsigned int atsi_count)
5665 struct GNUNET_MESH_Unicast *msg;
5666 struct GNUNET_PeerIdentity *neighbor;
5667 struct MeshTunnelChildInfo *cinfo;
5668 struct MeshTunnel *t;
5669 GNUNET_PEER_Id dest_id;
5674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5677 size = ntohs (message->size);
5679 sizeof (struct GNUNET_MESH_Unicast) +
5680 sizeof (struct GNUNET_MessageHeader))
5685 msg = (struct GNUNET_MESH_Unicast *) message;
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5687 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5689 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5692 /* TODO notify back: we don't know this tunnel */
5693 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5694 GNUNET_break_op (0);
5697 pid = ntohl (msg->pid);
5698 if (t->fwd_pid == pid)
5700 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5701 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5702 " Already seen pid %u, DROPPING!\n", pid);
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5708 " pid %u not seen yet, forwarding\n", pid);
5711 t->skip += (pid - t->fwd_pid) - 1;
5714 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5716 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5717 GNUNET_break_op (0);
5718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5719 "Received PID %u, ACK %u\n",
5720 pid, t->last_fwd_ack);
5724 tunnel_reset_timeout (t);
5725 dest_id = GNUNET_PEER_search (&msg->destination);
5726 if (dest_id == myid)
5728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5729 " it's for us! sending to clients...\n");
5730 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5731 send_subscribed_clients (message, &msg[1].header, t);
5732 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5735 ttl = ntohl (msg->ttl);
5736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5739 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5740 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5741 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5745 " not for us, retransmitting...\n");
5747 neighbor = tree_get_first_hop (t->tree, dest_id);
5748 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5749 cinfo->fwd_pid = pid;
5750 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5753 if (GNUNET_YES == t->nobuffer &&
5754 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5756 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5757 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5758 GNUNET_break_op (0);
5761 send_prebuilt_message (message, neighbor, t);
5762 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5768 * Core handler for mesh network traffic going from the origin to all peers
5770 * @param cls closure
5771 * @param message message
5772 * @param peer peer identity this notification is about
5773 * @param atsi performance data
5774 * @param atsi_count number of records in 'atsi'
5775 * @return GNUNET_OK to keep the connection open,
5776 * GNUNET_SYSERR to close it (signal serious error)
5778 * TODO: Check who we got this from, to validate route.
5781 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5782 const struct GNUNET_MessageHeader *message,
5783 const struct GNUNET_ATS_Information *atsi,
5784 unsigned int atsi_count)
5786 struct GNUNET_MESH_Multicast *msg;
5787 struct MeshTunnel *t;
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5793 size = ntohs (message->size);
5794 if (sizeof (struct GNUNET_MESH_Multicast) +
5795 sizeof (struct GNUNET_MessageHeader) > size)
5797 GNUNET_break_op (0);
5800 msg = (struct GNUNET_MESH_Multicast *) message;
5801 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5805 /* TODO notify that we dont know that tunnel */
5806 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5807 GNUNET_break_op (0);
5810 pid = ntohl (msg->pid);
5811 if (t->fwd_pid == pid)
5813 /* already seen this packet, drop */
5814 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5816 " Already seen pid %u, DROPPING!\n", pid);
5817 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5823 " pid %u not seen yet, forwarding\n", pid);
5825 t->skip += (pid - t->fwd_pid) - 1;
5827 tunnel_reset_timeout (t);
5829 /* Transmit to locally interested clients */
5830 if (NULL != t->peers &&
5831 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5833 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5834 send_subscribed_clients (message, &msg[1].header, t);
5835 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5838 if (ntohl (msg->ttl) == 0)
5840 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5841 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5842 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5845 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5846 tunnel_send_multicast (t, message);
5852 * Core handler for mesh network traffic toward the owner of a tunnel
5854 * @param cls closure
5855 * @param message message
5856 * @param peer peer identity this notification is about
5857 * @param atsi performance data
5858 * @param atsi_count number of records in 'atsi'
5860 * @return GNUNET_OK to keep the connection open,
5861 * GNUNET_SYSERR to close it (signal serious error)
5864 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5865 const struct GNUNET_MessageHeader *message,
5866 const struct GNUNET_ATS_Information *atsi,
5867 unsigned int atsi_count)
5869 struct GNUNET_MESH_ToOrigin *msg;
5870 struct GNUNET_PeerIdentity id;
5871 struct MeshPeerInfo *peer_info;
5872 struct MeshTunnel *t;
5873 struct MeshTunnelChildInfo *cinfo;
5877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5879 size = ntohs (message->size);
5880 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5881 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5883 GNUNET_break_op (0);
5886 msg = (struct GNUNET_MESH_ToOrigin *) message;
5887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5888 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5889 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5890 pid = ntohl (msg->pid);
5894 /* TODO notify that we dont know this tunnel (whom)? */
5895 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5896 GNUNET_break_op (0);
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5898 "Received to_origin with PID %u on unknown tunnel\n",
5903 cinfo = tunnel_get_neighbor_fc(t, peer);
5910 if (cinfo->bck_pid == pid)
5912 /* already seen this packet, drop */
5913 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5915 " Already seen pid %u, DROPPING!\n", pid);
5916 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5921 " pid %u not seen yet, forwarding\n", pid);
5922 cinfo->bck_pid = pid;
5924 if (NULL != t->owner)
5927 struct GNUNET_MESH_ToOrigin *copy;
5929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5930 " it's for us! sending to clients...\n");
5931 /* TODO signature verification */
5932 memcpy (cbuf, message, size);
5933 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5934 copy->tid = htonl (t->local_tid);
5936 copy->pid = htonl (t->bck_pid);
5937 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5938 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5939 ©->header, GNUNET_NO);
5940 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5944 " not for us, retransmitting...\n");
5946 peer_info = peer_info_get (&msg->oid);
5947 if (NULL == peer_info)
5949 /* unknown origin of tunnel */
5953 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5954 send_prebuilt_message (message, &id, t);
5955 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5962 * Core handler for mesh network traffic point-to-point acks.
5964 * @param cls closure
5965 * @param message message
5966 * @param peer peer identity this notification is about
5967 * @param atsi performance data
5968 * @param atsi_count number of records in 'atsi'
5970 * @return GNUNET_OK to keep the connection open,
5971 * GNUNET_SYSERR to close it (signal serious error)
5974 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5975 const struct GNUNET_MessageHeader *message,
5976 const struct GNUNET_ATS_Information *atsi,
5977 unsigned int atsi_count)
5979 struct GNUNET_MESH_ACK *msg;
5980 struct MeshTunnel *t;
5983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5985 msg = (struct GNUNET_MESH_ACK *) message;
5987 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5991 /* TODO notify that we dont know this tunnel (whom)? */
5992 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5995 ack = ntohl (msg->pid);
5996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5998 /* Is this a forward or backward ACK? */
5999 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6001 struct MeshTunnelChildInfo *cinfo;
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6005 cinfo = tunnel_get_neighbor_fc (t, peer);
6006 cinfo->fwd_ack = ack;
6007 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6008 tunnel_unlock_fwd_queues (t);
6012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6014 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6015 tunnel_unlock_bck_queue (t);
6022 * Core handler for mesh network traffic point-to-point ack polls.
6024 * @param cls closure
6025 * @param message message
6026 * @param peer peer identity this notification is about
6027 * @param atsi performance data
6028 * @param atsi_count number of records in 'atsi'
6030 * @return GNUNET_OK to keep the connection open,
6031 * GNUNET_SYSERR to close it (signal serious error)
6034 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6035 const struct GNUNET_MessageHeader *message,
6036 const struct GNUNET_ATS_Information *atsi,
6037 unsigned int atsi_count)
6039 struct GNUNET_MESH_Poll *msg;
6040 struct MeshTunnel *t;
6042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6045 msg = (struct GNUNET_MESH_Poll *) message;
6047 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6051 /* TODO notify that we dont know this tunnel (whom)? */
6052 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6053 GNUNET_break_op (0);
6057 /* Is this a forward or backward ACK? */
6058 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6060 struct MeshTunnelChildInfo *cinfo;
6062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6063 cinfo = tunnel_get_neighbor_fc (t, peer);
6064 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6065 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6070 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6078 * Core handler for path ACKs
6080 * @param cls closure
6081 * @param message message
6082 * @param peer peer identity this notification is about
6083 * @param atsi performance data
6084 * @param atsi_count number of records in 'atsi'
6086 * @return GNUNET_OK to keep the connection open,
6087 * GNUNET_SYSERR to close it (signal serious error)
6090 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6091 const struct GNUNET_MessageHeader *message,
6092 const struct GNUNET_ATS_Information *atsi,
6093 unsigned int atsi_count)
6095 struct GNUNET_MESH_PathACK *msg;
6096 struct GNUNET_PeerIdentity id;
6097 struct MeshPeerInfo *peer_info;
6098 struct MeshPeerPath *p;
6099 struct MeshTunnel *t;
6101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6102 GNUNET_i2s (&my_full_id));
6103 msg = (struct GNUNET_MESH_PathACK *) message;
6104 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6107 /* TODO notify that we don't know the tunnel */
6108 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6110 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6114 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6116 peer_info = peer_info_get (&msg->peer_id);
6117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6118 GNUNET_i2s (&msg->peer_id));
6119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6122 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6125 "connect_by_string completed, stopping search\n");
6126 regex_cancel_search (t->regex_ctx);
6127 t->regex_ctx = NULL;
6130 /* Add paths to peers? */
6131 p = tree_get_path_to_peer (t->tree, peer_info->id);
6134 path_add_to_peers (p, GNUNET_YES);
6142 /* Message for us? */
6143 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6146 if (NULL == t->owner)
6148 GNUNET_break_op (0);
6151 if (NULL != t->dht_get_type)
6153 GNUNET_DHT_get_stop (t->dht_get_type);
6154 t->dht_get_type = NULL;
6156 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6158 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6159 send_client_peer_connected (t, peer_info->id);
6164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6165 " not for us, retransmitting...\n");
6166 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6167 peer_info = peer_info_get (&msg->oid);
6168 if (NULL == peer_info)
6170 /* If we know the tunnel, we should DEFINITELY know the peer */
6174 send_prebuilt_message (message, &id, t);
6180 * Core handler for mesh keepalives.
6182 * @param cls closure
6183 * @param message message
6184 * @param peer peer identity this notification is about
6185 * @param atsi performance data
6186 * @param atsi_count number of records in 'atsi'
6187 * @return GNUNET_OK to keep the connection open,
6188 * GNUNET_SYSERR to close it (signal serious error)
6190 * TODO: Check who we got this from, to validate route.
6193 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6194 const struct GNUNET_MessageHeader *message,
6195 const struct GNUNET_ATS_Information *atsi,
6196 unsigned int atsi_count)
6198 struct GNUNET_MESH_TunnelKeepAlive *msg;
6199 struct MeshTunnel *t;
6201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6204 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6205 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6209 /* TODO notify that we dont know that tunnel */
6210 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6211 GNUNET_break_op (0);
6215 tunnel_reset_timeout (t);
6217 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6218 tunnel_send_multicast (t, message);
6225 * Functions to handle messages from core
6227 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6228 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6229 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6230 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6231 sizeof (struct GNUNET_MESH_PathBroken)},
6232 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6233 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6234 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6235 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6236 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6237 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6238 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6239 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6240 sizeof (struct GNUNET_MESH_ACK)},
6241 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6242 sizeof (struct GNUNET_MESH_Poll)},
6243 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6244 sizeof (struct GNUNET_MESH_PathACK)},
6250 /******************************************************************************/
6251 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6252 /******************************************************************************/
6255 * deregister_app: iterator for removing each application registered by a client
6257 * @param cls closure
6258 * @param key the hash of the application id (used to access the hashmap)
6259 * @param value the value stored at the key (client)
6261 * @return GNUNET_OK on success
6264 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6266 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6267 GNUNET_break (GNUNET_YES ==
6268 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6274 * notify_client_connection_failure: notify a client that the connection to the
6275 * requested remote peer is not possible (for instance, no route found)
6276 * Function called when the socket is ready to queue more data. "buf" will be
6277 * NULL and "size" zero if the socket was closed for writing in the meantime.
6279 * @param cls closure
6280 * @param size number of bytes available in buf
6281 * @param buf where the callee should write the message
6282 * @return number of bytes written to buf
6285 notify_client_connection_failure (void *cls, size_t size, void *buf)
6288 struct MeshPeerInfo *peer_info;
6289 struct GNUNET_MESH_PeerControl *msg;
6290 struct GNUNET_PeerIdentity id;
6292 if (0 == size && NULL == buf)
6294 // TODO retry? cancel?
6298 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6299 peer_info = (struct MeshPeerInfo *) cls;
6300 msg = (struct GNUNET_MESH_PeerControl *) buf;
6301 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6302 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6303 // msg->tunnel_id = htonl(peer_info->t->tid);
6304 GNUNET_PEER_resolve (peer_info->id, &id);
6305 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6313 * Send keepalive packets for a peer
6315 * @param cls Closure (tunnel for which to send the keepalive).
6316 * @param tc Notification context.
6319 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6321 struct MeshTunnel *t = cls;
6322 struct GNUNET_MESH_TunnelKeepAlive *msg;
6323 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6326 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6327 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6333 "sending keepalive for tunnel %d\n", t->id.tid);
6335 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6336 msg->header.size = htons (size);
6337 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6338 msg->oid = my_full_id;
6339 msg->tid = htonl (t->id.tid);
6340 tunnel_send_multicast (t, &msg->header);
6342 t->path_refresh_task =
6343 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6344 tunnel_reset_timeout(t);
6349 * Function to process paths received for a new peer addition. The recorded
6350 * paths form the initial tunnel, which can be optimized later.
6351 * Called on each result obtained for the DHT search.
6353 * @param cls closure
6354 * @param exp when will this value expire
6355 * @param key key of the result
6356 * @param get_path path of the get request
6357 * @param get_path_length lenght of get_path
6358 * @param put_path path of the put request
6359 * @param put_path_length length of the put_path
6360 * @param type type of the result
6361 * @param size number of bytes in data
6362 * @param data pointer to the result data
6364 * TODO: re-issue the request after certain time? cancel after X results?
6367 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6368 const struct GNUNET_HashCode * key,
6369 const struct GNUNET_PeerIdentity *get_path,
6370 unsigned int get_path_length,
6371 const struct GNUNET_PeerIdentity *put_path,
6372 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6373 size_t size, const void *data)
6375 struct MeshPathInfo *path_info = cls;
6376 struct MeshPeerPath *p;
6377 struct GNUNET_PeerIdentity pi;
6380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6381 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6384 p = path_build_from_dht (get_path, get_path_length, put_path,
6386 path_add_to_peers (p, GNUNET_NO);
6388 for (i = 0; i < path_info->peer->ntunnels; i++)
6390 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6391 peer_info_connect (path_info->peer, path_info->t);
6399 * Function to process paths received for a new peer addition. The recorded
6400 * paths form the initial tunnel, which can be optimized later.
6401 * Called on each result obtained for the DHT search.
6403 * @param cls closure
6404 * @param exp when will this value expire
6405 * @param key key of the result
6406 * @param get_path path of the get request
6407 * @param get_path_length lenght of get_path
6408 * @param put_path path of the put request
6409 * @param put_path_length length of the put_path
6410 * @param type type of the result
6411 * @param size number of bytes in data
6412 * @param data pointer to the result data
6415 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6416 const struct GNUNET_HashCode * key,
6417 const struct GNUNET_PeerIdentity *get_path,
6418 unsigned int get_path_length,
6419 const struct GNUNET_PeerIdentity *put_path,
6420 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6421 size_t size, const void *data)
6423 const struct PBlock *pb = data;
6424 const struct GNUNET_PeerIdentity *pi = &pb->id;
6425 struct MeshTunnel *t = cls;
6426 struct MeshPeerInfo *peer_info;
6427 struct MeshPeerPath *p;
6429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6430 if (size != sizeof (struct PBlock))
6432 GNUNET_break_op (0);
6435 if (ntohl(pb->type) != t->type)
6437 GNUNET_break_op (0);
6440 GNUNET_assert (NULL != t->owner);
6441 peer_info = peer_info_get (pi);
6442 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6444 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6446 p = path_build_from_dht (get_path, get_path_length, put_path,
6448 path_add_to_peers (p, GNUNET_NO);
6450 tunnel_add_peer (t, peer_info);
6451 peer_info_connect (peer_info, t);
6456 * Function to process DHT string to regex matching.
6457 * Called on each result obtained for the DHT search.
6459 * @param cls closure (search context)
6460 * @param exp when will this value expire
6461 * @param key key of the result
6462 * @param get_path path of the get request (not used)
6463 * @param get_path_length lenght of get_path (not used)
6464 * @param put_path path of the put request (not used)
6465 * @param put_path_length length of the put_path (not used)
6466 * @param type type of the result
6467 * @param size number of bytes in data
6468 * @param data pointer to the result data
6471 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6472 const struct GNUNET_HashCode * key,
6473 const struct GNUNET_PeerIdentity *get_path,
6474 unsigned int get_path_length,
6475 const struct GNUNET_PeerIdentity *put_path,
6476 unsigned int put_path_length,
6477 enum GNUNET_BLOCK_Type type,
6478 size_t size, const void *data)
6480 const struct MeshRegexAccept *block = data;
6481 struct MeshRegexSearchContext *ctx = cls;
6482 struct MeshRegexSearchInfo *info = ctx->info;
6483 struct MeshPeerPath *p;
6484 struct MeshPeerInfo *peer_info;
6486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6489 peer_info = peer_info_get(&block->id);
6490 p = path_build_from_dht (get_path, get_path_length, put_path,
6492 path_add_to_peers (p, GNUNET_NO);
6495 tunnel_add_peer (info->t, peer_info);
6496 peer_info_connect (peer_info, info->t);
6497 if (0 == info->peer)
6499 info->peer = peer_info->id;
6503 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6506 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6507 ®ex_connect_timeout,
6515 * Function to process DHT string to regex matching.
6516 * Called on each result obtained for the DHT search.
6518 * @param cls closure (search context)
6519 * @param exp when will this value expire
6520 * @param key key of the result
6521 * @param get_path path of the get request (not used)
6522 * @param get_path_length lenght of get_path (not used)
6523 * @param put_path path of the put request (not used)
6524 * @param put_path_length length of the put_path (not used)
6525 * @param type type of the result
6526 * @param size number of bytes in data
6527 * @param data pointer to the result data
6529 * TODO: re-issue the request after certain time? cancel after X results?
6532 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6533 const struct GNUNET_HashCode * key,
6534 const struct GNUNET_PeerIdentity *get_path,
6535 unsigned int get_path_length,
6536 const struct GNUNET_PeerIdentity *put_path,
6537 unsigned int put_path_length,
6538 enum GNUNET_BLOCK_Type type,
6539 size_t size, const void *data)
6541 const struct MeshRegexBlock *block = data;
6542 struct MeshRegexSearchContext *ctx = cls;
6543 struct MeshRegexSearchInfo *info = ctx->info;
6547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6548 "DHT GET STRING RETURNED RESULTS\n");
6549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6550 " key: %s\n", GNUNET_h2s (key));
6552 copy = GNUNET_malloc (size);
6553 memcpy (copy, data, size);
6554 GNUNET_break (GNUNET_OK ==
6555 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6556 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6557 len = ntohl (block->n_proof);
6559 char proof[len + 1];
6561 memcpy (proof, &block[1], len);
6563 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6565 GNUNET_break_op (0);
6569 len = strlen (info->description);
6570 if (len == ctx->position) // String processed
6572 if (GNUNET_YES == ntohl (block->accepting))
6574 regex_find_path(key, ctx);
6578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6579 // FIXME REGEX this block not successful, wait for more? start timeout?
6584 regex_next_edge (block, size, ctx);
6589 /******************************************************************************/
6590 /********************* MESH LOCAL HANDLES **************************/
6591 /******************************************************************************/
6595 * Handler for client disconnection
6597 * @param cls closure
6598 * @param client identification of the client; NULL
6599 * for the last call when the server is destroyed
6602 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6604 struct MeshClient *c;
6605 struct MeshClient *next;
6608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6615 // return; uncomment for regex_profiler
6619 if (c->handle != client)
6621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6627 GNUNET_SERVER_client_drop (c->handle);
6628 c->shutting_down = GNUNET_YES;
6629 GNUNET_assert (NULL != c->own_tunnels);
6630 GNUNET_assert (NULL != c->incoming_tunnels);
6631 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6632 &tunnel_destroy_iterator, c);
6633 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6634 &tunnel_destroy_iterator, c);
6635 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6636 &tunnel_destroy_iterator, c);
6637 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6638 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6639 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6641 /* deregister clients applications */
6642 if (NULL != c->apps)
6644 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6645 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6647 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6648 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6650 GNUNET_SCHEDULER_cancel (announce_applications_task);
6651 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6653 if (NULL != c->types)
6654 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6655 for (i = 0; i < c->n_regex; i++)
6657 GNUNET_free (c->regexes[i].regex);
6659 GNUNET_free_non_null (c->regexes);
6660 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6661 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6663 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6666 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6675 * Handler for new clients
6677 * @param cls closure
6678 * @param client identification of the client
6679 * @param message the actual message, which includes messages the client wants
6682 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6683 const struct GNUNET_MessageHeader *message)
6685 struct GNUNET_MESH_ClientConnect *cc_msg;
6686 struct MeshClient *c;
6687 GNUNET_MESH_ApplicationType *a;
6694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6695 /* Check data sanity */
6696 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6697 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6698 ntypes = ntohs (cc_msg->types);
6699 napps = ntohs (cc_msg->applications);
6701 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6704 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6708 /* Create new client structure */
6709 c = GNUNET_malloc (sizeof (struct MeshClient));
6710 c->id = next_client_id++;
6711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6713 GNUNET_SERVER_client_keep (client);
6714 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6717 GNUNET_MESH_ApplicationType at;
6718 struct GNUNET_HashCode hc;
6720 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6721 for (i = 0; i < napps; i++)
6724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6725 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6726 /* store in clients hashmap */
6727 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6728 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6729 /* store in global hashmap, for announcements */
6730 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6731 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6733 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6734 announce_applications_task =
6735 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6741 struct GNUNET_HashCode hc;
6743 t = (uint16_t *) & a[napps];
6744 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6745 for (i = 0; i < ntypes; i++)
6748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6749 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6751 /* store in clients hashmap */
6752 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6753 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6754 /* store in global hashmap */
6755 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6756 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6760 " client has %u+%u subscriptions\n", napps, ntypes);
6762 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6763 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6764 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6765 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6766 GNUNET_SERVER_notification_context_add (nc, client);
6767 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6769 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6775 * Handler for clients announcing available services by a regular expression.
6777 * @param cls closure
6778 * @param client identification of the client
6779 * @param message the actual message, which includes messages the client wants
6782 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6783 const struct GNUNET_MessageHeader *message)
6785 struct GNUNET_MESH_RegexAnnounce *msg;
6786 struct MeshRegexDescriptor rd;
6787 struct MeshClient *c;
6791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6793 /* Sanity check for client registration */
6794 if (NULL == (c = client_get (client)))
6797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6802 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6803 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6804 regex = GNUNET_malloc (len + 1);
6805 memcpy (regex, &msg[1], len);
6808 rd.compression = ntohs (msg->compression_characters);
6809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6812 GNUNET_array_append (c->regexes, c->n_regex, rd);
6813 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6815 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6821 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6827 * Handler for requests of new tunnels
6829 * @param cls closure
6830 * @param client identification of the client
6831 * @param message the actual message
6834 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6835 const struct GNUNET_MessageHeader *message)
6837 struct GNUNET_MESH_TunnelMessage *t_msg;
6838 struct MeshTunnel *t;
6839 struct MeshClient *c;
6840 MESH_TunnelNumber tid;
6842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6844 /* Sanity check for client registration */
6845 if (NULL == (c = client_get (client)))
6848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6853 /* Message sanity check */
6854 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6861 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6862 /* Sanity check for tunnel numbering */
6863 tid = ntohl (t_msg->tunnel_id);
6864 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6870 /* Sanity check for duplicate tunnel IDs */
6871 if (NULL != tunnel_get_by_local_id (c, tid))
6874 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6878 while (NULL != tunnel_get_by_pi (myid, next_tid))
6879 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6880 t = tunnel_new (myid, next_tid++, c, tid);
6883 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6885 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6888 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6890 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6891 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6900 * Handler for requests of deleting tunnels
6902 * @param cls closure
6903 * @param client identification of the client
6904 * @param message the actual message
6907 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6908 const struct GNUNET_MessageHeader *message)
6910 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6911 struct MeshClient *c;
6912 struct MeshTunnel *t;
6913 MESH_TunnelNumber tid;
6915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6916 "Got a DESTROY TUNNEL from client!\n");
6918 /* Sanity check for client registration */
6919 if (NULL == (c = client_get (client)))
6922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6927 /* Message sanity check */
6928 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6931 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6935 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6937 /* Retrieve tunnel */
6938 tid = ntohl (tunnel_msg->tunnel_id);
6939 t = tunnel_get_by_local_id(c, tid);
6942 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6947 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6949 client_ignore_tunnel (c, t);
6951 // TODO: when to destroy incoming tunnel?
6952 if (t->nclients == 0)
6954 GNUNET_assert (GNUNET_YES ==
6955 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6957 GNUNET_assert (GNUNET_YES ==
6958 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6959 &my_full_id.hashPubKey,
6963 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6966 send_client_tunnel_disconnect(t, c);
6967 client_delete_tunnel(c, t);
6969 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6971 tunnel_send_destroy (t);
6972 t->destroy = GNUNET_YES;
6973 // The tunnel will be destroyed when the last message is transmitted.
6974 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6980 * Handler for requests of seeting tunnel's speed.
6982 * @param cls Closure (unused).
6983 * @param client Identification of the client.
6984 * @param message The actual message.
6987 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6988 const struct GNUNET_MessageHeader *message)
6990 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6991 struct MeshClient *c;
6992 struct MeshTunnel *t;
6993 MESH_TunnelNumber tid;
6995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6996 "Got a SPEED request from client!\n");
6998 /* Sanity check for client registration */
6999 if (NULL == (c = client_get (client)))
7002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7008 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7010 /* Retrieve tunnel */
7011 tid = ntohl (tunnel_msg->tunnel_id);
7012 t = tunnel_get_by_local_id(c, tid);
7015 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7017 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7021 switch (ntohs(message->type))
7023 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7024 t->speed_min = GNUNET_YES;
7026 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7027 t->speed_min = GNUNET_NO;
7032 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7037 * Handler for requests of seeting tunnel's buffering policy.
7039 * @param cls Closure (unused).
7040 * @param client Identification of the client.
7041 * @param message The actual message.
7044 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7045 const struct GNUNET_MessageHeader *message)
7047 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7048 struct MeshClient *c;
7049 struct MeshTunnel *t;
7050 MESH_TunnelNumber tid;
7052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7053 "Got a BUFFER request from client!\n");
7055 /* Sanity check for client registration */
7056 if (NULL == (c = client_get (client)))
7059 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7064 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7066 /* Retrieve tunnel */
7067 tid = ntohl (tunnel_msg->tunnel_id);
7068 t = tunnel_get_by_local_id(c, tid);
7071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7077 switch (ntohs(message->type))
7079 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7080 t->nobuffer = GNUNET_NO;
7082 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7083 t->nobuffer = GNUNET_YES;
7089 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7094 * Handler for connection requests to new peers
7096 * @param cls closure
7097 * @param client identification of the client
7098 * @param message the actual message (PeerControl)
7101 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7102 const struct GNUNET_MessageHeader *message)
7104 struct GNUNET_MESH_PeerControl *peer_msg;
7105 struct MeshPeerInfo *peer_info;
7106 struct MeshClient *c;
7107 struct MeshTunnel *t;
7108 MESH_TunnelNumber tid;
7110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7111 /* Sanity check for client registration */
7112 if (NULL == (c = client_get (client)))
7115 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7120 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7122 /* Sanity check for message size */
7123 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7130 /* Tunnel exists? */
7131 tid = ntohl (peer_msg->tunnel_id);
7132 t = tunnel_get_by_local_id (c, tid);
7136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7140 /* Does client own tunnel? */
7141 if (t->owner->handle != client)
7144 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7148 GNUNET_i2s (&peer_msg->peer));
7149 peer_info = peer_info_get (&peer_msg->peer);
7151 tunnel_add_peer (t, peer_info);
7152 peer_info_connect (peer_info, t);
7154 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7160 * Handler for disconnection requests of peers in a tunnel
7162 * @param cls closure
7163 * @param client identification of the client
7164 * @param message the actual message (PeerControl)
7167 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7168 const struct GNUNET_MessageHeader *message)
7170 struct GNUNET_MESH_PeerControl *peer_msg;
7171 struct MeshPeerInfo *peer_info;
7172 struct MeshClient *c;
7173 struct MeshTunnel *t;
7174 MESH_TunnelNumber tid;
7176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7177 /* Sanity check for client registration */
7178 if (NULL == (c = client_get (client)))
7181 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7186 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7188 /* Sanity check for message size */
7189 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7196 /* Tunnel exists? */
7197 tid = ntohl (peer_msg->tunnel_id);
7198 t = tunnel_get_by_local_id (c, tid);
7202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7207 /* Does client own tunnel? */
7208 if (t->owner->handle != client)
7211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7216 GNUNET_i2s (&peer_msg->peer));
7217 /* Is the peer in the tunnel? */
7219 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7220 if (NULL == peer_info)
7223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7227 /* Ok, delete peer from tunnel */
7228 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7229 &peer_msg->peer.hashPubKey);
7231 send_destroy_path (t, peer_info->id);
7232 tunnel_delete_peer (t, peer_info->id);
7233 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7238 * Handler for blacklist requests of peers in a tunnel
7240 * @param cls closure
7241 * @param client identification of the client
7242 * @param message the actual message (PeerControl)
7244 * FIXME implement DHT block bloomfilter
7247 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7248 const struct GNUNET_MessageHeader *message)
7250 struct GNUNET_MESH_PeerControl *peer_msg;
7251 struct MeshClient *c;
7252 struct MeshTunnel *t;
7253 MESH_TunnelNumber tid;
7255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7256 /* Sanity check for client registration */
7257 if (NULL == (c = client_get (client)))
7260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7265 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7267 /* Sanity check for message size */
7268 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7271 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7275 /* Tunnel exists? */
7276 tid = ntohl (peer_msg->tunnel_id);
7277 t = tunnel_get_by_local_id (c, tid);
7281 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7286 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7287 GNUNET_PEER_intern(&peer_msg->peer));
7292 * Handler for unblacklist requests of peers in a tunnel
7294 * @param cls closure
7295 * @param client identification of the client
7296 * @param message the actual message (PeerControl)
7299 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7300 const struct GNUNET_MessageHeader *message)
7302 struct GNUNET_MESH_PeerControl *peer_msg;
7303 struct MeshClient *c;
7304 struct MeshTunnel *t;
7305 MESH_TunnelNumber tid;
7309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7310 /* Sanity check for client registration */
7311 if (NULL == (c = client_get (client)))
7314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7319 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7321 /* Sanity check for message size */
7322 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7329 /* Tunnel exists? */
7330 tid = ntohl (peer_msg->tunnel_id);
7331 t = tunnel_get_by_local_id (c, tid);
7335 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7340 /* if peer is not known, complain */
7341 pid = GNUNET_PEER_search (&peer_msg->peer);
7348 /* search and remove from list */
7349 for (i = 0; i < t->nblacklisted; i++)
7351 if (t->blacklisted[i] == pid)
7353 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7354 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7359 /* if peer hasn't been blacklisted, complain */
7365 * Handler for connection requests to new peers by type
7367 * @param cls closure
7368 * @param client identification of the client
7369 * @param message the actual message (ConnectPeerByType)
7372 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7373 const struct GNUNET_MessageHeader *message)
7375 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7376 struct MeshClient *c;
7377 struct MeshTunnel *t;
7378 struct GNUNET_HashCode hash;
7379 MESH_TunnelNumber tid;
7381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7382 /* Sanity check for client registration */
7383 if (NULL == (c = client_get (client)))
7386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7391 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7393 /* Sanity check for message size */
7394 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7395 ntohs (connect_msg->header.size))
7398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7402 /* Tunnel exists? */
7403 tid = ntohl (connect_msg->tunnel_id);
7404 t = tunnel_get_by_local_id (c, tid);
7408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7412 /* Does client own tunnel? */
7413 if (t->owner->handle != client)
7416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7420 /* Do WE have the service? */
7421 t->type = ntohl (connect_msg->type);
7422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7423 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7424 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7427 /* Yes! Fast forward, add ourselves to the tunnel and send the
7428 * good news to the client, and alert the destination client of
7429 * an incoming tunnel.
7431 * FIXME send a path create to self, avoid code duplication
7433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7434 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7435 peer_info_get (&my_full_id),
7436 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7439 send_client_peer_connected (t, myid);
7440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7443 t->local_tid_dest = next_local_tid++;
7444 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7445 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7450 /* Ok, lets find a peer offering the service */
7451 if (NULL != t->dht_get_type)
7453 GNUNET_DHT_get_stop (t->dht_get_type);
7455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7456 GNUNET_h2s (&hash));
7458 GNUNET_DHT_get_start (dht_handle,
7459 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7461 dht_replication_level,
7462 GNUNET_DHT_RO_RECORD_ROUTE |
7463 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7465 &dht_get_type_handler, t);
7467 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7473 * Handler for connection requests to new peers by a string service description.
7475 * @param cls closure
7476 * @param client identification of the client
7477 * @param message the actual message, which includes messages the client wants
7480 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7481 const struct GNUNET_MessageHeader *message)
7483 struct GNUNET_MESH_ConnectPeerByString *msg;
7484 struct MeshRegexSearchContext *ctx;
7485 struct MeshRegexSearchInfo *info;
7486 struct GNUNET_DHT_GetHandle *get_h;
7487 struct GNUNET_HashCode key;
7488 struct MeshTunnel *t;
7489 struct MeshClient *c;
7490 MESH_TunnelNumber tid;
7494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7495 "Connect by string started\n");
7496 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7497 size = htons (message->size);
7499 /* Sanity check for client registration */
7500 if (NULL == (c = client_get (client)))
7503 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7508 /* Message size sanity check */
7509 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7516 /* Tunnel exists? */
7517 tid = ntohl (msg->tunnel_id);
7518 t = tunnel_get_by_local_id (c, tid);
7522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7526 /* Does client own tunnel? */
7527 if (t->owner->handle != client)
7530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7535 " on tunnel %s [%u]\n",
7536 GNUNET_i2s(&my_full_id),
7539 /* Only one connect_by_string allowed at the same time! */
7540 /* FIXME: allow more, return handle at api level to cancel, document */
7541 if (NULL != t->regex_ctx)
7544 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7548 /* Find string itself */
7549 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7550 string = (const char *) &msg[1];
7552 /* Initialize context */
7553 size = GNUNET_REGEX_get_first_key(string, len, &key);
7554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7555 " consumed %u bits out of %u\n", size, len);
7556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7557 " looking for %s\n", GNUNET_h2s (&key));
7559 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7561 info->description = GNUNET_malloc (len + 1);
7562 memcpy (info->description, string, len);
7563 info->description[len] = '\0';
7564 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7565 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7568 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7569 ctx->position = size;
7573 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7575 /* Start search in DHT */
7576 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7577 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7578 &key, /* key to search */
7579 dht_replication_level, /* replication level */
7580 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7581 NULL, /* xquery */ // FIXME BLOOMFILTER
7582 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7583 &dht_get_string_handler, ctx);
7585 GNUNET_break (GNUNET_OK ==
7586 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7589 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7591 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7597 * Handler for client traffic directed to one peer
7599 * @param cls closure
7600 * @param client identification of the client
7601 * @param message the actual message
7604 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7605 const struct GNUNET_MessageHeader *message)
7607 struct MeshClient *c;
7608 struct MeshTunnel *t;
7609 struct MeshPeerInfo *pi;
7610 struct GNUNET_MESH_Unicast *data_msg;
7611 MESH_TunnelNumber tid;
7614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7615 "Got a unicast request from a client!\n");
7617 /* Sanity check for client registration */
7618 if (NULL == (c = client_get (client)))
7621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7626 data_msg = (struct GNUNET_MESH_Unicast *) message;
7628 /* Sanity check for message size */
7629 size = ntohs (message->size);
7630 if (sizeof (struct GNUNET_MESH_Unicast) +
7631 sizeof (struct GNUNET_MessageHeader) > size)
7634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7638 /* Tunnel exists? */
7639 tid = ntohl (data_msg->tid);
7640 t = tunnel_get_by_local_id (c, tid);
7644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7648 /* Is it a local tunnel? Then, does client own the tunnel? */
7649 if (t->owner->handle != client)
7652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7656 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7657 &data_msg->destination.hashPubKey);
7658 /* Is the selected peer in the tunnel? */
7662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7666 /* PID should be as expected */
7667 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7670 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7671 "Unicast PID, expected %u, got %u\n",
7672 t->fwd_pid + 1, ntohl (data_msg->pid));
7673 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7677 /* Ok, everything is correct, send the message
7678 * (pretend we got it from a mesh peer)
7681 /* Work around const limitation */
7682 char buf[ntohs (message->size)] GNUNET_ALIGN;
7683 struct GNUNET_MESH_Unicast *copy;
7685 copy = (struct GNUNET_MESH_Unicast *) buf;
7686 memcpy (buf, data_msg, size);
7687 copy->oid = my_full_id;
7688 copy->tid = htonl (t->id.tid);
7689 copy->ttl = htonl (default_ttl);
7690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7691 " calling generic handler...\n");
7692 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7695 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7702 * Handler for client traffic directed to the origin
7704 * @param cls closure
7705 * @param client identification of the client
7706 * @param message the actual message
7709 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7710 const struct GNUNET_MessageHeader *message)
7712 struct GNUNET_MESH_ToOrigin *data_msg;
7713 struct MeshTunnelClientInfo *clinfo;
7714 struct MeshClient *c;
7715 struct MeshTunnel *t;
7716 MESH_TunnelNumber tid;
7719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7720 "Got a ToOrigin request from a client!\n");
7721 /* Sanity check for client registration */
7722 if (NULL == (c = client_get (client)))
7725 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7730 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7732 /* Sanity check for message size */
7733 size = ntohs (message->size);
7734 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7735 sizeof (struct GNUNET_MessageHeader) > size)
7738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7742 /* Tunnel exists? */
7743 tid = ntohl (data_msg->tid);
7744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7745 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7748 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7751 t = tunnel_get_by_local_id (c, tid);
7754 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7755 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7757 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7761 /* It should be sent by someone who has this as incoming tunnel. */
7762 if (GNUNET_NO == client_knows_tunnel (c, t))
7765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7769 /* PID should be as expected */
7770 clinfo = tunnel_get_client_fc (t, c);
7771 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7774 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7775 "To Origin PID, expected %u, got %u\n",
7776 clinfo->bck_pid + 1,
7777 ntohl (data_msg->pid));
7778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7783 /* Ok, everything is correct, send the message
7784 * (pretend we got it from a mesh peer)
7787 char buf[ntohs (message->size)] GNUNET_ALIGN;
7788 struct GNUNET_MESH_ToOrigin *copy;
7790 /* Work around const limitation */
7791 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7792 memcpy (buf, data_msg, size);
7793 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7794 copy->tid = htonl (t->id.tid);
7795 copy->ttl = htonl (default_ttl);
7796 copy->pid = htonl (t->bck_pid + 1);
7798 copy->sender = my_full_id;
7799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7800 " calling generic handler...\n");
7801 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7803 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7810 * Handler for client traffic directed to all peers in a tunnel
7812 * @param cls closure
7813 * @param client identification of the client
7814 * @param message the actual message
7817 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7818 const struct GNUNET_MessageHeader *message)
7820 struct MeshClient *c;
7821 struct MeshTunnel *t;
7822 struct GNUNET_MESH_Multicast *data_msg;
7823 MESH_TunnelNumber tid;
7825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7826 "Got a multicast request from a client!\n");
7828 /* Sanity check for client registration */
7829 if (NULL == (c = client_get (client)))
7832 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7837 data_msg = (struct GNUNET_MESH_Multicast *) message;
7839 /* Sanity check for message size */
7840 if (sizeof (struct GNUNET_MESH_Multicast) +
7841 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7844 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7848 /* Tunnel exists? */
7849 tid = ntohl (data_msg->tid);
7850 t = tunnel_get_by_local_id (c, tid);
7854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7861 /* Does client own tunnel? */
7862 if (t->owner->handle != client)
7865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7869 /* PID should be as expected */
7870 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7873 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7874 "Multicast PID, expected %u, got %u\n",
7875 t->fwd_pid + 1, ntohl (data_msg->pid));
7876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7881 char buf[ntohs (message->size)] GNUNET_ALIGN;
7882 struct GNUNET_MESH_Multicast *copy;
7884 copy = (struct GNUNET_MESH_Multicast *) buf;
7885 memcpy (buf, message, ntohs (message->size));
7886 copy->oid = my_full_id;
7887 copy->tid = htonl (t->id.tid);
7888 copy->ttl = htonl (default_ttl);
7889 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7891 " calling generic handler...\n");
7892 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7895 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7901 * Handler for client's ACKs for payload traffic.
7903 * @param cls Closure (unused).
7904 * @param client Identification of the client.
7905 * @param message The actual message.
7908 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7909 const struct GNUNET_MessageHeader *message)
7911 struct GNUNET_MESH_LocalAck *msg;
7912 struct MeshTunnel *t;
7913 struct MeshClient *c;
7914 MESH_TunnelNumber tid;
7917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7918 /* Sanity check for client registration */
7919 if (NULL == (c = client_get (client)))
7922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7927 msg = (struct GNUNET_MESH_LocalAck *) message;
7929 /* Tunnel exists? */
7930 tid = ntohl (msg->tunnel_id);
7931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7932 t = tunnel_get_by_local_id (c, tid);
7936 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7937 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7942 ack = ntohl (msg->max_pid);
7943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7945 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7946 if (NULL != t->owner && t->owner->handle == client)
7948 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7950 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7954 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7955 tunnel_set_client_fwd_ack (t, c, ack);
7956 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7959 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7966 * Functions to handle messages from clients
7968 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7969 {&handle_local_new_client, NULL,
7970 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7971 {&handle_local_announce_regex, NULL,
7972 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7973 {&handle_local_tunnel_create, NULL,
7974 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7975 sizeof (struct GNUNET_MESH_TunnelMessage)},
7976 {&handle_local_tunnel_destroy, NULL,
7977 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7978 sizeof (struct GNUNET_MESH_TunnelMessage)},
7979 {&handle_local_tunnel_speed, NULL,
7980 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7981 sizeof (struct GNUNET_MESH_TunnelMessage)},
7982 {&handle_local_tunnel_speed, NULL,
7983 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7984 sizeof (struct GNUNET_MESH_TunnelMessage)},
7985 {&handle_local_tunnel_buffer, NULL,
7986 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7987 sizeof (struct GNUNET_MESH_TunnelMessage)},
7988 {&handle_local_tunnel_buffer, NULL,
7989 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7990 sizeof (struct GNUNET_MESH_TunnelMessage)},
7991 {&handle_local_connect_add, NULL,
7992 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7993 sizeof (struct GNUNET_MESH_PeerControl)},
7994 {&handle_local_connect_del, NULL,
7995 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7996 sizeof (struct GNUNET_MESH_PeerControl)},
7997 {&handle_local_blacklist, NULL,
7998 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7999 sizeof (struct GNUNET_MESH_PeerControl)},
8000 {&handle_local_unblacklist, NULL,
8001 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8002 sizeof (struct GNUNET_MESH_PeerControl)},
8003 {&handle_local_connect_by_type, NULL,
8004 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8005 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8006 {&handle_local_connect_by_string, NULL,
8007 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8008 {&handle_local_unicast, NULL,
8009 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8010 {&handle_local_to_origin, NULL,
8011 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8012 {&handle_local_multicast, NULL,
8013 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8014 {&handle_local_ack, NULL,
8015 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8016 sizeof (struct GNUNET_MESH_LocalAck)},
8022 * To be called on core init/fail.
8024 * @param cls service closure
8025 * @param server handle to the server for this service
8026 * @param identity the public identity of this peer
8029 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8030 const struct GNUNET_PeerIdentity *identity)
8033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8034 core_handle = server;
8035 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8038 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8039 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8041 GNUNET_abort(); // Try harder
8048 * Method called whenever a given peer connects.
8050 * @param cls closure
8051 * @param peer peer identity this notification is about
8052 * @param atsi performance data for the connection
8053 * @param atsi_count number of records in 'atsi'
8056 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8057 const struct GNUNET_ATS_Information *atsi,
8058 unsigned int atsi_count)
8060 struct MeshPeerInfo *peer_info;
8061 struct MeshPeerPath *path;
8063 DEBUG_CONN ("Peer connected\n");
8064 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8065 peer_info = peer_info_get (peer);
8066 if (myid == peer_info->id)
8068 DEBUG_CONN (" (self)\n");
8073 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8075 path = path_new (2);
8076 path->peers[0] = myid;
8077 path->peers[1] = peer_info->id;
8078 GNUNET_PEER_change_rc (myid, 1);
8079 GNUNET_PEER_change_rc (peer_info->id, 1);
8080 peer_info_add_path (peer_info, path, GNUNET_YES);
8081 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8087 * Method called whenever a peer disconnects.
8089 * @param cls closure
8090 * @param peer peer identity this notification is about
8093 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8095 struct MeshPeerInfo *pi;
8096 struct MeshPeerQueue *q;
8097 struct MeshPeerQueue *n;
8099 DEBUG_CONN ("Peer disconnected\n");
8100 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8110 /* TODO try to reroute this traffic instead */
8111 queue_destroy(q, GNUNET_YES);
8114 if (NULL != pi->core_transmit)
8116 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8117 pi->core_transmit = NULL;
8119 peer_info_remove_path (pi, pi->id, myid);
8122 DEBUG_CONN (" (self)\n");
8124 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8129 /******************************************************************************/
8130 /************************ MAIN FUNCTIONS ****************************/
8131 /******************************************************************************/
8134 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8136 * @param cls closure
8137 * @param key current key code
8138 * @param value value in the hash map
8139 * @return GNUNET_YES if we should continue to iterate,
8143 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8145 struct MeshTunnel *t = value;
8152 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8154 * @param cls closure
8155 * @param key current key code
8156 * @param value value in the hash map
8157 * @return GNUNET_YES if we should continue to iterate,
8161 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8163 struct MeshPeerInfo *p = value;
8164 struct MeshPeerQueue *q;
8165 struct MeshPeerQueue *n;
8173 queue_destroy(q, GNUNET_YES);
8177 peer_info_destroy (p);
8183 * Task run during shutdown.
8189 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8193 if (core_handle != NULL)
8195 GNUNET_CORE_disconnect (core_handle);
8200 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8203 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8204 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8205 if (dht_handle != NULL)
8207 GNUNET_DHT_disconnect (dht_handle);
8212 GNUNET_SERVER_notification_context_destroy (nc);
8215 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8217 GNUNET_SCHEDULER_cancel (announce_id_task);
8218 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8225 * Callback for hostkey read/generation
8228 * @param pk the private key
8229 * @param emsg error message
8232 key_generation_cb (void *cls,
8233 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8236 struct MeshPeerInfo *peer;
8237 struct MeshPeerPath *p;
8242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8243 _("Mesh service could not access hostkey. Exiting.\n"));
8244 GNUNET_SCHEDULER_shutdown ();
8247 my_private_key = pk;
8248 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8249 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8250 &my_full_id.hashPubKey);
8251 myid = GNUNET_PEER_intern (&my_full_id);
8252 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8253 "Mesh for peer [%s] starting\n",
8254 GNUNET_i2s(&my_full_id));
8256 // transport_handle = GNUNET_TRANSPORT_connect(c,
8266 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8269 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8270 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8271 GNUNET_SERVER_disconnect_notify (server_handle,
8272 &handle_local_client_disconnect, NULL);
8276 clients_tail = NULL;
8279 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8280 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8282 /* Create a peer_info for the local peer */
8283 peer = peer_info_get (&my_full_id);
8286 GNUNET_PEER_change_rc (myid, 1);
8287 peer_info_add_path (peer, p, GNUNET_YES);
8288 GNUNET_SERVER_resume (server_handle);
8289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8294 * Process mesh requests.
8296 * @param cls closure
8297 * @param server the initialized server
8298 * @param c configuration to use
8301 run (void *cls, struct GNUNET_SERVER_Handle *server,
8302 const struct GNUNET_CONFIGURATION_Handle *c)
8306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8307 server_handle = server;
8308 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8309 NULL, /* Closure passed to MESH functions */
8310 &core_init, /* Call core_init once connected */
8311 &core_connect, /* Handle connects */
8312 &core_disconnect, /* remove peers on disconnects */
8313 NULL, /* Don't notify about all incoming messages */
8314 GNUNET_NO, /* For header only in notification */
8315 NULL, /* Don't notify about all outbound messages */
8316 GNUNET_NO, /* For header-only out notification */
8317 core_handlers); /* Register these handlers */
8319 if (core_handle == NULL)
8322 GNUNET_SCHEDULER_shutdown ();
8327 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8332 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8334 GNUNET_SCHEDULER_shutdown ();
8339 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8340 &refresh_path_time))
8342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8344 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8345 "mesh", "refresh path time");
8346 GNUNET_SCHEDULER_shutdown ();
8351 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8352 &app_announce_time))
8354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8356 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8357 "mesh", "app announce time");
8358 GNUNET_SCHEDULER_shutdown ();
8361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8364 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8369 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8370 "mesh", "id announce time");
8371 GNUNET_SCHEDULER_shutdown ();
8379 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8382 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8384 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8385 "mesh", "connect timeout");
8386 GNUNET_SCHEDULER_shutdown ();
8391 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8396 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8397 "mesh", "max msgs queue");
8398 GNUNET_SCHEDULER_shutdown ();
8403 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8408 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8409 "mesh", "max tunnels");
8410 GNUNET_SCHEDULER_shutdown ();
8415 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8418 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8420 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8421 "mesh", "default ttl", 64);
8426 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8427 &dht_replication_level))
8429 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8431 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8432 "mesh", "dht replication level", 10);
8433 dht_replication_level = 10;
8436 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8437 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8438 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8439 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8440 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8442 dht_handle = GNUNET_DHT_connect (c, 64);
8443 if (NULL == dht_handle)
8447 stats = GNUNET_STATISTICS_create ("mesh", c);
8449 GNUNET_SERVER_suspend (server_handle);
8450 /* Scheduled the task to clean up when shutdown is called */
8451 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8453 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8454 GNUNET_free (keyfile);
8459 * The main function for the mesh service.
8461 * @param argc number of arguments from the command line
8462 * @param argv command line arguments
8463 * @return 0 ok, 1 on error
8466 main (int argc, char *const *argv)
8471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8472 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8474 ret = (GNUNET_OK == r) ? 0 : 1;
8475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8479 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8480 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8481 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);