2 This file is part of GNUnet.
3 (C) 2001-2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
64 #define MESH_POLL_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
102 /******************************************************************************/
103 /************************ DATA STRUCTURES ****************************/
104 /******************************************************************************/
106 /** FWD declaration */
112 * Struct representing a piece of data being sent to other peers
116 /** Tunnel it belongs to. */
117 struct MeshTunnel *t;
119 /** How many remaining neighbors still hav't got it. */
120 unsigned int reference_counter;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int total_out;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct to store regex information announced by clients.
178 struct MeshRegexDescriptor
181 * Regular expression itself.
186 * How many characters per edge can we squeeze?
188 uint16_t compression;
192 * Struct containing all info possibly needed to build a package when called
195 struct MeshTransmissionDescriptor
197 /** ID of the tunnel this packet travels in */
198 struct MESH_TunnelID *origin;
200 /** Who was this message being sent to */
201 struct MeshPeerInfo *peer;
203 /** Ultimate destination of the packet */
204 GNUNET_PEER_Id destination;
206 /** Data descriptor */
207 struct MeshData* mesh_data;
212 * Struct containing all information regarding a given peer
222 * Last time we heard from this peer
224 struct GNUNET_TIME_Absolute last_contact;
227 * Task handler for delayed connect task;
229 GNUNET_SCHEDULER_TaskIdentifier connect_task;
232 * Number of attempts to reconnect so far
234 int n_reconnect_attempts;
237 * Paths to reach the peer, ordered by ascending hop count
239 struct MeshPeerPath *path_head;
242 * Paths to reach the peer, ordered by ascending hop count
244 struct MeshPeerPath *path_tail;
247 * Handle to stop the DHT search for a path to this peer
249 struct GNUNET_DHT_GetHandle *dhtget;
252 * Closure given to the DHT GET
254 struct MeshPathInfo *dhtgetcls;
257 * Array of tunnels this peer participates in
258 * (most probably a small amount, therefore not a hashmap)
259 * When the path to the peer changes, notify these tunnels to let them
260 * re-adjust their path trees.
262 struct MeshTunnel **tunnels;
265 * Number of tunnels this peers participates in
267 unsigned int ntunnels;
270 * Transmission queue to core DLL head
272 struct MeshPeerQueue *queue_head;
275 * Transmission queue to core DLL tail
277 struct MeshPeerQueue *queue_tail;
280 * How many messages are in the queue to this peer.
282 unsigned int queue_n;
285 * Handle to for queued transmissions
287 struct GNUNET_CORE_TransmitHandle *core_transmit;
292 * Globally unique tunnel identification (owner + number)
293 * DO NOT USE OVER THE NETWORK
298 * Node that owns the tunnel
303 * Tunnel number to differentiate all the tunnels owned by the node oid
304 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
306 MESH_TunnelNumber tid;
311 * Struct containing all information regarding a tunnel
312 * For an intermediate node the improtant info used will be:
313 * - id Tunnel unique identification
314 * - paths[0] To know where to send it next
315 * - metainfo: ready, speeds, accounting
322 struct MESH_TunnelID id;
325 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
327 MESH_TunnelNumber local_tid;
330 * Local tunnel number for local destination clients (incoming number)
331 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
334 MESH_TunnelNumber local_tid_dest;
337 * Is the speed on the tunnel limited to the slowest peer?
342 * Is the tunnel bufferless (minimum latency)?
347 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
352 * Packet ID of the last bck packet sent (unique counter per hop).
357 * SKIP value for this tunnel.
362 * Force sending ACK? Flag to allow duplicate ACK on POLL.
367 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
368 * Contains the Flow Control info: FWD ACK value received,
369 * last BCK ACK sent, PID and SKIP values.
371 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
374 * Last ACK sent towards the origin (for traffic towards leaf node).
376 uint32_t last_fwd_ack;
379 * BCK ACK value received from the hop towards the owner of the tunnel,
380 * (previous node / owner): up to what message PID can we sent back to him.
385 * How many messages are in the forward queue (towards leaves).
387 unsigned int fwd_queue_n;
390 * How many messages do we accept in the forward queue.
392 unsigned int fwd_queue_max;
395 * How many messages are in the backward queue (towards origin).
397 unsigned int bck_queue_n;
400 * How many messages do we accept in the backward queue.
402 unsigned int bck_queue_max;
405 * Task to poll peer in case of a stall.
407 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
410 * Last time the tunnel was used
412 struct GNUNET_TIME_Absolute timestamp;
415 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
416 * containing peers added by id or by type, not intermediate peers.
418 struct GNUNET_CONTAINER_MultiHashMap *peers;
421 * Number of peers that are connected and potentially ready to receive data
423 unsigned int peers_ready;
426 * Number of peers that have been added to the tunnel
428 unsigned int peers_total;
431 * Client owner of the tunnel, if any
433 struct MeshClient *owner;
436 * Clients that have been informed about and want to stay in the tunnel.
438 struct MeshClient **clients;
441 * Flow control info for each client.
443 struct MeshTunnelClientInfo *clients_fc;
446 * Number of elements in clients/clients_fc
448 unsigned int nclients;
451 * Clients that have been informed but requested to leave the tunnel.
453 struct MeshClient **ignore;
456 * Number of elements in clients
458 unsigned int nignore;
463 GNUNET_PEER_Id *blacklisted;
466 * Number of elements in blacklisted
468 unsigned int nblacklisted;
471 * Bloomfilter (for peer identities) to stop circular routes
473 char bloomfilter[MESH_BLOOM_SIZE];
478 struct MeshTunnelTree *tree;
481 * Application type we are looking for in this tunnel
483 GNUNET_MESH_ApplicationType type;
486 * Used to search peers offering a service
488 struct GNUNET_DHT_GetHandle *dht_get_type;
491 * Initial context of the regex search for a connect_by_string
493 struct MeshRegexSearchContext *regex_ctx;
496 * Task to keep the used paths alive
498 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
501 * Task to destroy the tunnel after timeout
503 * FIXME: merge the two? a tunnel will have either
504 * a path refresh OR a timeout, never both!
506 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
509 * Flag to signal the destruction of the tunnel.
510 * If this is set GNUNET_YES the tunnel will be destroyed
511 * when the queue is empty.
516 * Total messages pending for this tunnels, payload or not.
518 unsigned int pending_messages;
523 * Info about a child node in a tunnel, needed to perform flow control.
525 struct MeshTunnelChildInfo
528 * ID of the child node.
548 * Maximum PID allowed (FWD ACK received).
553 * Last ACK sent to that child (BCK ACK).
558 * Circular buffer pointing to MeshPeerQueue elements for all
559 * payload traffic going to this child.
560 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
562 struct MeshPeerQueue **send_buffer;
565 * Index of the oldest element in the send_buffer.
567 unsigned int send_buffer_start;
570 * How many elements are already in the buffer.
572 unsigned int send_buffer_n;
575 * Tunnel this info is about
577 struct MeshTunnel *t;
580 * Task to poll peer in case of a stall.
582 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
587 * Info about a leaf client of a tunnel, needed to perform flow control.
589 struct MeshTunnelClientInfo
592 * PID of the last packet sent to the client (FWD).
597 * PID of the last packet received from the client (BCK).
602 * Maximum PID allowed (FWD ACK received).
607 * Last ACK sent to that child (BCK ACK).
615 * Info collected during iteration of child nodes in order to get the ACK value
618 struct MeshTunnelChildIteratorContext
621 * Tunnel whose info is being collected.
623 struct MeshTunnel *t;
626 * Is this context initialized? Is the value in max_child_ack valid?
631 * Maximum child ACK so far.
633 uint32_t max_child_ack;
636 * Number of children nodes
638 unsigned int nchildren;
643 * Info needed to work with tunnel paths and peers
650 struct MeshTunnel *t;
653 * Neighbouring peer to whom we send the packet to
655 struct MeshPeerInfo *peer;
660 struct MeshPeerPath *path;
665 * Struct containing information about a client of the service
672 struct MeshClient *next;
677 struct MeshClient *prev;
680 * Tunnels that belong to this client, indexed by local id
682 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
685 * Tunnels this client has accepted, indexed by incoming local id
687 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
690 * Tunnels this client has rejected, indexed by incoming local id
692 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
694 * Handle to communicate with the client
696 struct GNUNET_SERVER_Client *handle;
699 * Applications that this client has claimed to provide
701 struct GNUNET_CONTAINER_MultiHashMap *apps;
704 * Messages that this client has declared interest in
706 struct GNUNET_CONTAINER_MultiHashMap *types;
709 * Whether the client is active or shutting down (don't send confirmations
710 * to a client that is shutting down.
715 * ID of the client, mainly for debug messages
720 * Regular expressions describing the services offered by this client.
722 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
725 * Number of regular expressions in regexes.
727 unsigned int n_regex;
730 * Task to refresh all regular expresions in the DHT.
732 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
738 * Struct to keep information of searches of services described by a regex
739 * using a user-provided string service description.
741 struct MeshRegexSearchInfo
744 * Which tunnel is this for
746 struct MeshTunnel *t;
749 * User provided description of the searched service.
754 * Part of the description already consumed by the search.
761 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
764 * Results from running DHT GETs.
766 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
769 * Contexts, for each running DHT GET. Free all on end of search.
771 struct MeshRegexSearchContext **contexts;
774 * Number of contexts (branches/steps in search).
776 unsigned int n_contexts;
779 * Peer that is connecting via connect_by_string. When connected, free ctx.
784 * Other peers that are found but not yet being connected to.
786 GNUNET_PEER_Id *peers;
789 * Number of elements in peers.
791 unsigned int n_peers;
794 * Next peer to try to connect to.
799 * Timeout for a connect attempt.
800 * When reached, try to connect to a different peer, if any. If not,
801 * try the same peer again.
803 GNUNET_SCHEDULER_TaskIdentifier timeout;
808 * Struct to keep state of running searches that have consumed a part of
811 struct MeshRegexSearchContext
814 * Part of the description already consumed by
815 * this particular search branch.
820 * Information about the search.
822 struct MeshRegexSearchInfo *info;
825 * We just want to look for one edge, the longer the better.
828 unsigned int longest_match;
831 * Destination hash of the longest match.
833 struct GNUNET_HashCode hash;
836 /******************************************************************************/
837 /************************ DEBUG FUNCTIONS ****************************/
838 /******************************************************************************/
842 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
843 * @param cls string to print
844 * @param success GNUNET_OK if the PUT was transmitted,
845 * GNUNET_NO on timeout,
846 * GNUNET_SYSERR on disconnect from service
847 * after the PUT message was transmitted
848 * (so we don't know if it was received or not)
853 mesh_debug (void *cls, int success)
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
861 unsigned int debug_fwd_ack;
862 unsigned int debug_bck_ack;
866 /******************************************************************************/
867 /*********************** GLOBAL VARIABLES ****************************/
868 /******************************************************************************/
870 /************************** Configuration parameters **************************/
873 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
875 static struct GNUNET_TIME_Relative refresh_path_time;
878 * How often to PUT local application numbers in the DHT.
880 static struct GNUNET_TIME_Relative app_announce_time;
883 * How often to PUT own ID in the DHT.
885 static struct GNUNET_TIME_Relative id_announce_time;
888 * Maximum time allowed to connect to a peer found by string.
890 static struct GNUNET_TIME_Relative connect_timeout;
893 * Default TTL for payload packets.
895 static unsigned long long default_ttl;
898 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
900 static unsigned long long dht_replication_level;
903 * How many tunnels are we willing to maintain.
904 * Local tunnels are always allowed, even if there are more tunnels than max.
906 static unsigned long long max_tunnels;
909 * How many messages *in total* are we willing to queue, divided by number of
910 * tunnels to get tunnel queue size.
912 static unsigned long long max_msgs_queue;
915 /*************************** Static global variables **************************/
918 * Hostkey generation context
920 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
923 * DLL with all the clients, head.
925 static struct MeshClient *clients;
928 * DLL with all the clients, tail.
930 static struct MeshClient *clients_tail;
933 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
935 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
938 * Number of tunnels known.
940 static unsigned long long n_tunnels;
943 * Tunnels incoming, indexed by MESH_TunnelNumber
944 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
946 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
949 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
951 static struct GNUNET_CONTAINER_MultiHashMap *peers;
954 * Handle to communicate with transport
956 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
959 * Handle to communicate with core.
961 static struct GNUNET_CORE_Handle *core_handle;
966 static struct GNUNET_DHT_Handle *dht_handle;
971 static struct GNUNET_SERVER_Handle *server_handle;
974 * Handle to the statistics service.
976 static struct GNUNET_STATISTICS_Handle *stats;
979 * Notification context, to send messages to local clients.
981 static struct GNUNET_SERVER_NotificationContext *nc;
984 * Local peer own ID (memory efficient handle).
986 static GNUNET_PEER_Id myid;
989 * Local peer own ID (full value).
991 static struct GNUNET_PeerIdentity my_full_id;
996 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1001 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1004 * Tunnel ID for the next created tunnel (global tunnel number).
1006 static MESH_TunnelNumber next_tid;
1009 * Tunnel ID for the next incoming tunnel (local tunnel number).
1011 static MESH_TunnelNumber next_local_tid;
1014 * All application types provided by this peer.
1016 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1019 * All message types clients of this peer are interested in.
1021 static struct GNUNET_CONTAINER_MultiHashMap *types;
1024 * Task to periodically announce provided applications.
1026 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1029 * Task to periodically announce itself in the network.
1031 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1034 * Next ID to assign to a client.
1036 unsigned int next_client_id;
1039 /******************************************************************************/
1040 /*********************** DECLARATIONS **************************/
1041 /******************************************************************************/
1044 * Function to process paths received for a new peer addition. The recorded
1045 * paths form the initial tunnel, which can be optimized later.
1046 * Called on each result obtained for the DHT search.
1048 * @param cls closure
1049 * @param exp when will this value expire
1050 * @param key key of the result
1051 * @param type type of the result
1052 * @param size number of bytes in data
1053 * @param data pointer to the result data
1056 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1057 const struct GNUNET_HashCode * key,
1058 const struct GNUNET_PeerIdentity *get_path,
1059 unsigned int get_path_length,
1060 const struct GNUNET_PeerIdentity *put_path,
1061 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1062 size_t size, const void *data);
1066 * Function to process DHT string to regex matching.
1067 * Called on each result obtained for the DHT search.
1069 * @param cls closure (search context)
1070 * @param exp when will this value expire
1071 * @param key key of the result
1072 * @param get_path path of the get request (not used)
1073 * @param get_path_length lenght of get_path (not used)
1074 * @param put_path path of the put request (not used)
1075 * @param put_path_length length of the put_path (not used)
1076 * @param type type of the result
1077 * @param size number of bytes in data
1078 * @param data pointer to the result data
1080 * TODO: re-issue the request after certain time? cancel after X results?
1083 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1084 const struct GNUNET_HashCode * key,
1085 const struct GNUNET_PeerIdentity *get_path,
1086 unsigned int get_path_length,
1087 const struct GNUNET_PeerIdentity *put_path,
1088 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1089 size_t size, const void *data);
1093 * Function to process DHT string to regex matching.
1094 * Called on each result obtained for the DHT search.
1096 * @param cls closure (search context)
1097 * @param exp when will this value expire
1098 * @param key key of the result
1099 * @param get_path path of the get request (not used)
1100 * @param get_path_length lenght of get_path (not used)
1101 * @param put_path path of the put request (not used)
1102 * @param put_path_length length of the put_path (not used)
1103 * @param type type of the result
1104 * @param size number of bytes in data
1105 * @param data pointer to the result data
1108 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1109 const struct GNUNET_HashCode * key,
1110 const struct GNUNET_PeerIdentity *get_path,
1111 unsigned int get_path_length,
1112 const struct GNUNET_PeerIdentity *put_path,
1113 unsigned int put_path_length,
1114 enum GNUNET_BLOCK_Type type,
1115 size_t size, const void *data);
1119 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1120 * and insert it in the appropiate structures if the peer is not known yet.
1122 * @param peer Short identity of the peer.
1124 * @return Existing or newly created peer info.
1126 static struct MeshPeerInfo *
1127 peer_info_get_short (const GNUNET_PEER_Id peer);
1131 * Try to establish a new connection to this peer.
1132 * Use the best path for the given tunnel.
1133 * If the peer doesn't have any path to it yet, try to get one.
1134 * If the peer already has some path, send a CREATE PATH towards it.
1136 * @param peer PeerInfo of the peer.
1137 * @param t Tunnel for which to create the path, if possible.
1140 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1144 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1145 * needed rescources.
1146 * If peer already exists, reevaluate shortest path and change if different.
1148 * @param t Tunnel we want to add a new peer to
1149 * @param peer PeerInfo of the peer being added
1153 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1157 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1158 * that are no longer needed, as well as nodes of no longer reachable peers.
1159 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1161 * @param t Tunnel from which to remove the path.
1162 * @param peer Short id of the peer which should be removed.
1165 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1169 * Search for a tunnel by global ID using full PeerIdentities.
1171 * @param oid owner of the tunnel.
1172 * @param tid global tunnel number.
1174 * @return tunnel handler, NULL if doesn't exist.
1176 static struct MeshTunnel *
1177 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1181 * Delete an active client from the tunnel.
1187 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1190 * Notify a tunnel that a connection has broken that affects at least
1191 * some of its peers.
1193 * @param t Tunnel affected.
1194 * @param p1 Peer that got disconnected from p2.
1195 * @param p2 Peer that got disconnected from p1.
1197 * @return Short ID of the peer disconnected (either p1 or p2).
1198 * 0 if the tunnel remained unaffected.
1200 static GNUNET_PEER_Id
1201 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1206 * Get the current ack value for a tunnel, for data going from root to leaves,
1207 * taking in account the tunnel mode and the status of all children and clients.
1211 * @return Maximum PID allowed.
1214 tunnel_get_fwd_ack (struct MeshTunnel *t);
1218 * Add a client to a tunnel, initializing all needed data structures.
1220 * @param t Tunnel to which add the client.
1221 * @param c Client which to add to the tunnel.
1224 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1228 * Jump to the next edge, with the longest matching token.
1230 * @param block Block found in the DHT.
1231 * @param size Size of the block.
1232 * @param ctx Context of the search.
1234 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1237 regex_next_edge (const struct MeshRegexBlock *block,
1239 struct MeshRegexSearchContext *ctx);
1243 * Find a path to a peer that offers a regex servcie compatible
1244 * with a given string.
1246 * @param key The key of the accepting state.
1247 * @param ctx Context containing info about the string, tunnel, etc.
1250 regex_find_path (const struct GNUNET_HashCode *key,
1251 struct MeshRegexSearchContext *ctx);
1255 * @brief Queue and pass message to core when possible.
1257 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1258 * and accounts for it. In case the queue is full, the message is dropped and
1261 * Otherwise, message is treated as internal and allowed to go regardless of
1264 * @param cls Closure (@c type dependant). It will be used by queue_send to
1265 * build the message to be sent if not already prebuilt.
1266 * @param type Type of the message, 0 for a raw message.
1267 * @param size Size of the message.
1268 * @param dst Neighbor to send message to.
1269 * @param t Tunnel this message belongs to.
1272 queue_add (void *cls, uint16_t type, size_t size,
1273 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1277 * Free a transmission that was already queued with all resources
1278 * associated to the request.
1280 * @param queue Queue handler to cancel.
1281 * @param clear_cls Is it necessary to free associated cls?
1284 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1288 * @brief Get the next transmittable message from the queue.
1290 * This will be the head, except in the case of being a data packet
1291 * not allowed by the destination peer.
1293 * @param peer Destination peer.
1295 * @return The next viable MeshPeerQueue element to send to that peer.
1296 * NULL when there are no transmittable messages.
1298 struct MeshPeerQueue *
1299 queue_get_next (const struct MeshPeerInfo *peer);
1303 * Core callback to write a queued packet to core buffer
1305 * @param cls Closure (peer info).
1306 * @param size Number of bytes available in buf.
1307 * @param buf Where the to write the message.
1309 * @return number of bytes written to buf
1312 queue_send (void *cls, size_t size, void *buf);
1314 /******************************************************************************/
1315 /************************ ITERATORS ****************************/
1316 /******************************************************************************/
1319 * Iterator over found existing mesh regex blocks that match an ongoing search.
1321 * @param cls closure
1322 * @param key current key code
1323 * @param value value in the hash map
1324 * @return GNUNET_YES if we should continue to iterate,
1328 regex_result_iterator (void *cls,
1329 const struct GNUNET_HashCode * key,
1332 struct MeshRegexBlock *block = value;
1333 struct MeshRegexSearchContext *ctx = cls;
1335 if (GNUNET_YES == ntohl(block->accepting) &&
1336 ctx->position == strlen (ctx->info->description))
1338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1339 regex_find_path (key, ctx);
1340 return GNUNET_YES; // We found an accept state!
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1345 ctx->position, strlen(ctx->info->description),
1346 ntohl(block->accepting));
1349 regex_next_edge(block, SIZE_MAX, ctx);
1351 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1358 * Iterator over edges in a regex block retrieved from the DHT.
1360 * @param cls Closure (context of the search).
1361 * @param token Token that follows to next state.
1362 * @param len Lenght of token.
1363 * @param key Hash of next state.
1365 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1368 regex_edge_iterator (void *cls,
1371 const struct GNUNET_HashCode *key)
1373 struct MeshRegexSearchContext *ctx = cls;
1374 struct MeshRegexSearchInfo *info = ctx->info;
1378 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1383 current = &info->description[ctx->position];
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1385 current_len = strlen (info->description) - ctx->position;
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1390 if (len > current_len)
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1393 return GNUNET_YES; // Token too long, wont match
1395 if (0 != strncmp (current, token, len))
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1398 return GNUNET_YES; // Token doesn't match
1401 if (len > ctx->longest_match)
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1404 ctx->longest_match = len;
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1418 * Jump to the next edge, with the longest matching token.
1420 * @param block Block found in the DHT.
1421 * @param size Size of the block.
1422 * @param ctx Context of the search.
1424 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1427 regex_next_edge (const struct MeshRegexBlock *block,
1429 struct MeshRegexSearchContext *ctx)
1431 struct MeshRegexSearchContext *new_ctx;
1432 struct MeshRegexSearchInfo *info = ctx->info;
1433 struct GNUNET_DHT_GetHandle *get_h;
1437 /* Find the longest match for the current string position,
1438 * among tokens in the given block */
1439 ctx->longest_match = 0;
1440 result = GNUNET_MESH_regex_block_iterate (block, size,
1441 ®ex_edge_iterator, ctx);
1442 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1444 /* Did anything match? */
1445 if (0 == ctx->longest_match)
1448 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1449 new_ctx->info = info;
1450 new_ctx->position = ctx->position + ctx->longest_match;
1451 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1453 /* Check whether we already have a DHT GET running for it */
1455 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1458 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1460 ®ex_result_iterator,
1462 return; // We are already looking for it
1465 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1467 /* Start search in DHT */
1469 GNUNET_DHT_get_start (dht_handle, /* handle */
1470 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1471 &ctx->hash, /* key to search */
1472 dht_replication_level, /* replication level */
1473 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1474 NULL, /* xquery */ // FIXME BLOOMFILTER
1475 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1476 &dht_get_string_handler, new_ctx);
1478 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1481 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1490 * Iterator over hash map entries to cancel DHT GET requests after a
1491 * successful connect_by_string.
1493 * @param cls Closure (unused).
1494 * @param key Current key code (unused).
1495 * @param value Value in the hash map (get handle).
1496 * @return GNUNET_YES if we should continue to iterate,
1500 regex_cancel_dht_get (void *cls,
1501 const struct GNUNET_HashCode * key,
1504 struct GNUNET_DHT_GetHandle *h = value;
1506 GNUNET_DHT_get_stop (h);
1512 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1513 * search for connect_by_string.
1515 * @param cls Closure (unused).
1516 * @param key Current key code (unused).
1517 * @param value MeshRegexBlock in the hash map.
1518 * @return GNUNET_YES if we should continue to iterate,
1522 regex_free_result (void *cls,
1523 const struct GNUNET_HashCode * key,
1527 GNUNET_free (value);
1533 * Regex callback iterator to store own service description in the DHT.
1535 * @param cls closure.
1536 * @param key hash for current state.
1537 * @param proof proof for current state.
1538 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1539 * @param num_edges number of edges leaving current state.
1540 * @param edges edges leaving current state.
1543 regex_iterator (void *cls,
1544 const struct GNUNET_HashCode *key,
1547 unsigned int num_edges,
1548 const struct GNUNET_REGEX_Edge *edges)
1550 struct MeshRegexBlock *block;
1551 struct MeshRegexEdge *block_edge;
1552 enum GNUNET_DHT_RouteOption opt;
1556 unsigned int offset;
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 " regex dht put for state %s\n",
1562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1570 if (GNUNET_YES == accepting)
1572 struct MeshRegexAccept block;
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575 " state %s is accepting, putting own id\n",
1577 size = sizeof (block);
1579 block.id = my_full_id;
1581 GNUNET_DHT_put(dht_handle, key,
1582 dht_replication_level,
1583 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1584 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1587 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1592 len = strlen(proof);
1593 size = sizeof (struct MeshRegexBlock) + len;
1594 block = GNUNET_malloc (size);
1597 block->n_proof = htonl (len);
1598 block->n_edges = htonl (num_edges);
1599 block->accepting = htonl (accepting);
1601 /* Store the proof at the end of the block. */
1602 aux = (char *) &block[1];
1603 memcpy (aux, proof, len);
1606 /* Store each edge in a variable length MeshEdge struct at the
1607 * very end of the MeshRegexBlock structure.
1609 for (i = 0; i < num_edges; i++)
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 " edge %s towards %s\n",
1614 GNUNET_h2s(&edges[i].destination));
1616 /* aux points at the end of the last block */
1617 len = strlen (edges[i].label);
1618 size += sizeof (struct MeshRegexEdge) + len;
1619 // Calculate offset FIXME is this ok? use size instead?
1620 offset = aux - (char *) block;
1621 block = GNUNET_realloc (block, size);
1622 aux = &((char *) block)[offset];
1623 block_edge = (struct MeshRegexEdge *) aux;
1624 block_edge->key = edges[i].destination;
1625 block_edge->n_token = htonl (len);
1626 aux = (char *) &block_edge[1];
1627 memcpy (aux, edges[i].label, len);
1631 GNUNET_DHT_put(dht_handle, key,
1632 dht_replication_level,
1634 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1636 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1640 GNUNET_free (block);
1645 * Store the regular expression describing a local service into the DHT.
1647 * @param regex The regular expresion.
1650 regex_put (const struct MeshRegexDescriptor *regex)
1652 struct GNUNET_REGEX_Automaton *dfa;
1654 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1655 dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1656 strlen(regex->regex),
1657 regex->compression);
1658 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1659 GNUNET_REGEX_automaton_destroy (dfa);
1660 DEBUG_DHT (" regex_put (%s) end\n", regex);
1665 * Find a path to a peer that offers a regex servcie compatible
1666 * with a given string.
1668 * @param key The key of the accepting state.
1669 * @param ctx Context containing info about the string, tunnel, etc.
1672 regex_find_path (const struct GNUNET_HashCode *key,
1673 struct MeshRegexSearchContext *ctx)
1675 struct GNUNET_DHT_GetHandle *get_h;
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1678 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1679 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1680 key, /* key to search */
1681 dht_replication_level, /* replication level */
1682 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1683 GNUNET_DHT_RO_RECORD_ROUTE,
1684 NULL, /* xquery */ // FIXME BLOOMFILTER
1685 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1686 &dht_get_string_accept_handler, ctx);
1687 GNUNET_break (GNUNET_OK ==
1688 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1696 * Function called if the connect attempt to a peer found via
1697 * connect_by_string times out. Try to connect to another peer, if any.
1698 * Otherwise try to reconnect to the same peer.
1700 * @param cls Closure (info about regex search).
1701 * @param tc TaskContext.
1704 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1706 struct MeshRegexSearchInfo *info = cls;
1707 struct MeshPeerInfo *peer_info;
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1712 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1713 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1721 if (0 < info->n_peers)
1723 // Select next peer, put current in that spot.
1724 id = info->peers[info->i_peer];
1725 info->peers[info->i_peer] = info->peer;
1726 info->i_peer = (info->i_peer + 1) % info->n_peers;
1730 // Try to connect to same peer again.
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1735 peer_info = peer_info_get_short(id);
1736 tunnel_add_peer (info->t, peer_info);
1738 tunnel_delete_peer (info->t, old);
1739 peer_info_connect (peer_info, info->t);
1740 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1741 ®ex_connect_timeout,
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1748 * Cancel an ongoing regex search in the DHT and free all resources.
1750 * @param ctx The search context.
1753 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1755 struct MeshRegexSearchInfo *info = ctx->info;
1758 GNUNET_free (info->description);
1759 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1760 ®ex_cancel_dht_get, NULL);
1761 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1762 ®ex_free_result, NULL);
1763 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1764 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1765 info->t->regex_ctx = NULL;
1766 for (i = 0; i < info->n_contexts; i++)
1768 GNUNET_free (info->contexts[i]);
1770 if (0 < info->n_contexts)
1771 GNUNET_free (info->contexts);
1772 if (0 < info->n_peers)
1773 GNUNET_free (info->peers);
1774 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1776 GNUNET_SCHEDULER_cancel(info->timeout);
1782 /******************************************************************************/
1783 /************************ PERIODIC FUNCTIONS ****************************/
1784 /******************************************************************************/
1787 * Announce iterator over for each application provided by the peer
1789 * @param cls closure
1790 * @param key current key code
1791 * @param value value in the hash map
1792 * @return GNUNET_YES if we should continue to
1797 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1799 struct PBlock block;
1800 struct MeshClient *c;
1802 block.id = my_full_id;
1803 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1804 GNUNET_assert(NULL != c);
1805 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1806 if (0 == block.type)
1811 block.type = htonl (block.type);
1813 GNUNET_break (NULL !=
1814 GNUNET_DHT_put (dht_handle, key,
1815 dht_replication_level,
1816 GNUNET_DHT_RO_RECORD_ROUTE |
1817 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1818 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1820 (const char *) &block,
1821 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1823 app_announce_time, NULL, NULL));
1829 * Periodically announce what applications are provided by local clients
1832 * @param cls closure
1833 * @param tc task context
1836 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1838 struct MeshClient *c = cls;
1841 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1842 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1847 DEBUG_DHT ("Starting PUT for regex\n");
1849 for (i = 0; i < c->n_regex; i++)
1851 regex_put (&c->regexes[i]);
1853 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1856 DEBUG_DHT ("Finished PUT for regex\n");
1863 * Periodically announce what applications are provided by local clients
1866 * @param cls closure
1867 * @param tc task context
1870 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1872 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1874 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1878 DEBUG_DHT ("Starting PUT for apps\n");
1880 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1882 announce_applications_task =
1883 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1885 DEBUG_DHT ("Finished PUT for apps\n");
1892 * Periodically announce self id in the DHT
1894 * @param cls closure
1895 * @param tc task context
1898 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1900 struct PBlock block;
1902 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1904 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1908 * - Set data expiration in function of X
1909 * - Adapt X to churn
1911 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1913 block.id = my_full_id;
1914 block.type = htonl (0);
1915 GNUNET_DHT_put (dht_handle, /* DHT handle */
1916 &my_full_id.hashPubKey, /* Key to use */
1917 dht_replication_level, /* Replication level */
1918 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1919 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1920 sizeof (block), /* Size of the data */
1921 (const char *) &block, /* Data itself */
1922 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1923 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1924 NULL, /* Continuation */
1925 NULL); /* Continuation closure */
1927 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1931 /******************************************************************************/
1932 /****************** GENERAL HELPER FUNCTIONS ************************/
1933 /******************************************************************************/
1936 * Decrements the reference counter and frees all resources if needed
1938 * @param mesh_data Data Descriptor used in a multicast message.
1939 * Freed no longer needed (last message).
1942 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1944 if (0 == --(mesh_data->reference_counter))
1946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1947 GNUNET_free (mesh_data->data);
1948 GNUNET_free (mesh_data);
1954 * Check if client has registered with the service and has not disconnected
1956 * @param client the client to check
1958 * @return non-NULL if client exists in the global DLL
1960 static struct MeshClient *
1961 client_get (struct GNUNET_SERVER_Client *client)
1963 struct MeshClient *c;
1968 if (c->handle == client)
1977 * Checks if a given client has subscribed to certain message type
1979 * @param message_type Type of message to check
1980 * @param c Client to check
1982 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1984 * FIXME: use of crypto_hash slows it down
1985 * The hash function alone takes 8-10us out of the ~55us for the whole
1986 * process of retransmitting the message from one local client to another.
1987 * Find faster implementation!
1990 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1992 struct GNUNET_HashCode hc;
1994 if (NULL == c->types)
1997 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1998 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2003 * Check whether client wants traffic from a tunnel.
2005 * @param c Client to check.
2006 * @param t Tunnel to be found.
2008 * @return GNUNET_YES if client knows tunnel.
2010 * TODO look in client hashmap
2013 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2017 for (i = 0; i < t->nclients; i++)
2018 if (t->clients[i] == c)
2025 * Check whether client has been informed about a tunnel.
2027 * @param c Client to check.
2028 * @param t Tunnel to be found.
2030 * @return GNUNET_YES if client knows tunnel.
2032 * TODO look in client hashmap
2035 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2039 for (i = 0; i < t->nignore; i++)
2040 if (t->ignore[i] == c)
2042 return client_wants_tunnel(c, t);
2047 * Marks a client as uninterested in traffic from the tunnel, updating both
2048 * client and tunnel to reflect this.
2050 * @param c Client that doesn't want traffic anymore.
2051 * @param t Tunnel which should be ignored.
2053 * FIXME when to delete an incoming tunnel?
2056 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2058 struct GNUNET_HashCode hash;
2060 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2061 GNUNET_break (GNUNET_YES ==
2062 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2064 GNUNET_break (GNUNET_YES ==
2065 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2067 tunnel_delete_active_client (t, c);
2068 GNUNET_array_append (t->ignore, t->nignore, c);
2073 * Deletes a tunnel from a client (either owner or destination). To be used on
2074 * tunnel destroy, otherwise, use client_ignore_tunnel.
2076 * @param c Client whose tunnel to delete.
2077 * @param t Tunnel which should be deleted.
2080 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2082 struct GNUNET_HashCode hash;
2086 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2087 GNUNET_assert (GNUNET_YES ==
2088 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2094 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2096 GNUNET_assert (GNUNET_YES ==
2097 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2101 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2109 * Send the message to all clients that have subscribed to its type
2111 * @param msg Pointer to the message itself
2112 * @param payload Pointer to the payload of the message.
2113 * @param t The tunnel to whose clients this message goes.
2115 * @return number of clients this message was sent to
2118 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2119 const struct GNUNET_MessageHeader *payload,
2120 struct MeshTunnel *t)
2122 struct MeshClient *c;
2123 MESH_TunnelNumber *tid;
2126 char cbuf[htons (msg->size)];
2128 type = ntohs (payload->type);
2129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2131 GNUNET_MESH_DEBUG_M2S (type));
2133 memcpy (cbuf, msg, sizeof (cbuf));
2134 switch (htons (msg->type))
2136 struct GNUNET_MESH_Unicast *uc;
2137 struct GNUNET_MESH_Multicast *mc;
2138 struct GNUNET_MESH_ToOrigin *to;
2140 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2141 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2144 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2145 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2148 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2149 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2157 for (count = 0, c = clients; c != NULL; c = c->next)
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2160 if (client_is_subscribed (type, c))
2162 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2166 *tid = htonl (t->local_tid);
2170 if (GNUNET_NO == client_knows_tunnel (c, t))
2172 /* This client doesn't know the tunnel */
2173 struct GNUNET_MESH_TunnelNotification tmsg;
2174 struct GNUNET_HashCode hash;
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2177 tmsg.header.size = htons (sizeof (tmsg));
2178 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2179 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2180 tmsg.tunnel_id = htonl (t->local_tid_dest);
2182 if (GNUNET_YES == t->speed_min)
2183 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2184 if (GNUNET_YES == t->nobuffer)
2185 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2186 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2187 &tmsg.header, GNUNET_NO);
2188 tunnel_add_client (t, c);
2189 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2191 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2192 c->incoming_tunnels, &hash, t,
2193 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2195 *tid = htonl (t->local_tid_dest);
2198 /* Check if the client wants to get traffic from the tunnel */
2199 if (GNUNET_NO == client_wants_tunnel(c, t))
2202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2203 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2204 (struct GNUNET_MessageHeader
2205 *) cbuf, GNUNET_NO);
2214 * Notify the client that owns the tunnel that a peer has connected to it
2215 * (the requested path to it has been confirmed).
2217 * @param t Tunnel whose owner to notify
2218 * @param id Short id of the peer that has connected
2221 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2223 struct GNUNET_MESH_PeerControl pc;
2225 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2226 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2227 pc.tunnel_id = htonl (t->local_tid);
2228 GNUNET_PEER_resolve (id, &pc.peer);
2229 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2235 * Notify all clients (not depending on registration status) that the incoming
2236 * tunnel is no longer valid.
2238 * @param t Tunnel that was destroyed.
2241 send_clients_tunnel_destroy (struct MeshTunnel *t)
2243 struct GNUNET_MESH_TunnelMessage msg;
2245 msg.header.size = htons (sizeof (msg));
2246 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2247 msg.tunnel_id = htonl (t->local_tid_dest);
2248 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2253 * Notify clients of tunnel disconnections, if needed.
2254 * In case the origin disconnects, the destination clients get a tunnel destroy
2255 * notification. If the last destination disconnects (only one remaining client
2256 * in tunnel), the origin gets a (local ID) peer disconnected.
2257 * Note that the function must be called BEFORE removing the client from
2260 * @param t Tunnel that was destroyed.
2261 * @param c Client that disconnected.
2264 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2270 struct GNUNET_MESH_TunnelMessage msg;
2272 msg.header.size = htons (sizeof (msg));
2273 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2274 msg.tunnel_id = htonl (t->local_tid_dest);
2275 for (i = 0; i < t->nclients; i++)
2276 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2277 &msg.header, GNUNET_NO);
2279 // FIXME when to disconnect an incoming tunnel?
2280 else if (1 == t->nclients && NULL != t->owner)
2282 struct GNUNET_MESH_PeerControl msg;
2284 msg.header.size = htons (sizeof (msg));
2285 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2286 msg.tunnel_id = htonl (t->local_tid);
2287 msg.peer = my_full_id;
2288 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2289 &msg.header, GNUNET_NO);
2295 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2296 * and insert it in the appropiate structures if the peer is not known yet.
2298 * @param peer Full identity of the peer.
2300 * @return Existing or newly created peer info.
2302 static struct MeshPeerInfo *
2303 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2305 struct MeshPeerInfo *peer_info;
2307 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2308 if (NULL == peer_info)
2311 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2312 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2314 peer_info->id = GNUNET_PEER_intern (peer);
2322 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2323 * and insert it in the appropiate structures if the peer is not known yet.
2325 * @param peer Short identity of the peer.
2327 * @return Existing or newly created peer info.
2329 static struct MeshPeerInfo *
2330 peer_info_get_short (const GNUNET_PEER_Id peer)
2332 struct GNUNET_PeerIdentity id;
2334 GNUNET_PEER_resolve (peer, &id);
2335 return peer_info_get (&id);
2340 * Iterator to remove the tunnel from the list of tunnels a peer participates
2343 * @param cls Closure (tunnel info)
2344 * @param key GNUNET_PeerIdentity of the peer (unused)
2345 * @param value PeerInfo of the peer
2347 * @return always GNUNET_YES, to keep iterating
2350 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2352 struct MeshTunnel *t = cls;
2353 struct MeshPeerInfo *peer = value;
2356 for (i = 0; i < peer->ntunnels; i++)
2359 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2362 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2363 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2372 * Core callback to write a pre-constructed data packet to core buffer
2374 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2375 * @param size Number of bytes available in buf.
2376 * @param buf Where the to write the message.
2378 * @return number of bytes written to buf
2381 send_core_data_raw (void *cls, size_t size, void *buf)
2383 struct MeshTransmissionDescriptor *info = cls;
2384 struct GNUNET_MessageHeader *msg;
2387 GNUNET_assert (NULL != info);
2388 GNUNET_assert (NULL != info->mesh_data);
2389 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2390 total_size = ntohs (msg->size);
2392 if (total_size > size)
2397 memcpy (buf, msg, total_size);
2398 data_descriptor_decrement_rc (info->mesh_data);
2405 * Sends an already built non-multicast message to a peer,
2406 * properly registrating all used resources.
2408 * @param message Message to send. Function makes a copy of it.
2409 * @param peer Short ID of the neighbor whom to send the message.
2410 * @param t Tunnel on which this message is transmitted.
2413 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2414 const struct GNUNET_PeerIdentity *peer,
2415 struct MeshTunnel *t)
2417 struct MeshTransmissionDescriptor *info;
2418 struct MeshPeerInfo *neighbor;
2419 struct MeshPeerPath *p;
2423 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2425 size = ntohs (message->size);
2426 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2427 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2428 info->mesh_data->data = GNUNET_malloc (size);
2429 memcpy (info->mesh_data->data, message, size);
2430 type = ntohs(message->type);
2433 struct GNUNET_MESH_Unicast *m;
2434 struct GNUNET_MESH_ToOrigin *to;
2436 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2437 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2438 m->ttl = htonl (ntohl (m->ttl) - 1);
2440 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2441 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2443 to->pid = htonl(t->bck_pid);
2445 info->mesh_data->data_len = size;
2446 info->mesh_data->reference_counter = 1;
2447 info->mesh_data->total_out = 1;
2448 neighbor = peer_info_get (peer);
2449 for (p = neighbor->path_head; NULL != p; p = p->next)
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 " %s IS NOT DIRECTLY CONNECTED\n",
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2465 for (p = neighbor->path_head; NULL != p; p = p->next)
2467 struct GNUNET_PeerIdentity debug_id;
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 " path with %u hops through:\n",
2473 for (i = 0; i < p->length; i++)
2475 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2478 i, GNUNET_i2s(&debug_id));
2482 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2483 GNUNET_free (info->mesh_data->data);
2484 GNUNET_free (info->mesh_data);
2488 info->peer = neighbor;
2489 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2500 * Sends a CREATE PATH message for a path to a peer, properly registrating
2501 * all used resources.
2503 * @param peer PeerInfo of the final peer for whom this path is being created.
2504 * @param p Path itself.
2505 * @param t Tunnel for which the path is created.
2508 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2509 struct MeshTunnel *t)
2511 struct GNUNET_PeerIdentity id;
2512 struct MeshPathInfo *path_info;
2513 struct MeshPeerInfo *neighbor;
2519 p = tree_get_path_to_peer (t->tree, peer->id);
2526 for (i = 0; i < p->length; i++)
2528 if (p->peers[i] == myid)
2531 if (i >= p->length - 1)
2537 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2539 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2540 path_info->path = p;
2542 neighbor = peer_info_get (&id);
2543 path_info->peer = neighbor;
2544 queue_add (path_info,
2545 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2546 sizeof (struct GNUNET_MESH_ManipulatePath) +
2547 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2554 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2556 * @param t Tunnel whose path to destroy.
2557 * @param destination Short ID of the peer to whom the path to destroy.
2560 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2562 struct MeshPeerPath *p;
2565 p = tree_get_path_to_peer (t->tree, destination);
2571 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2572 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2574 struct GNUNET_MESH_ManipulatePath *msg;
2575 struct GNUNET_PeerIdentity *pi;
2579 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2580 msg->header.size = htons (size);
2581 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2582 msg->tid = htonl (t->id.tid);
2583 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2584 for (i = 0; i < p->length; i++)
2586 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2588 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2595 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2597 * @param t Tunnel which to confirm.
2600 send_path_ack (struct MeshTunnel *t)
2602 struct MeshTransmissionDescriptor *info;
2603 struct GNUNET_PeerIdentity id;
2604 GNUNET_PEER_Id peer;
2606 peer = tree_get_predecessor (t->tree);
2607 GNUNET_PEER_resolve (peer, &id);
2608 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2609 info->origin = &t->id;
2610 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2611 GNUNET_assert (NULL != info->peer);
2614 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2615 sizeof (struct GNUNET_MESH_PathACK),
2622 * Try to establish a new connection to this peer.
2623 * Use the best path for the given tunnel.
2624 * If the peer doesn't have any path to it yet, try to get one.
2625 * If the peer already has some path, send a CREATE PATH towards it.
2627 * @param peer PeerInfo of the peer.
2628 * @param t Tunnel for which to create the path, if possible.
2631 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2633 struct MeshPeerPath *p;
2634 struct MeshPathInfo *path_info;
2636 if (NULL != peer->path_head)
2638 p = tree_get_path_to_peer (t->tree, peer->id);
2645 // FIXME always send create path to self
2648 send_create_path (peer, p, t);
2652 struct GNUNET_HashCode hash;
2655 send_client_peer_connected (t, myid);
2656 t->local_tid_dest = next_local_tid++;
2657 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2660 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2668 else if (NULL == peer->dhtget)
2670 struct GNUNET_PeerIdentity id;
2672 GNUNET_PEER_resolve (peer->id, &id);
2673 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2674 path_info->peer = peer;
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2678 peer->dhtgetcls = path_info;
2679 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2680 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2681 &id.hashPubKey, /* key to search */
2682 dht_replication_level, /* replication level */
2683 GNUNET_DHT_RO_RECORD_ROUTE |
2684 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2685 NULL, /* xquery */ // FIXME BLOOMFILTER
2686 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2687 &dht_get_id_handler, path_info);
2689 /* Otherwise, there is no path but the DHT get is already started. */
2694 * Task to delay the connection of a peer
2696 * @param cls Closure (path info with tunnel and peer to connect).
2697 * Will be free'd on exection.
2698 * @param tc TaskContext
2701 peer_info_connect_task (void *cls,
2702 const struct GNUNET_SCHEDULER_TaskContext *tc)
2704 struct MeshPathInfo *path_info = cls;
2706 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2708 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2713 peer_info_connect (path_info->peer, path_info->t);
2719 * Destroy the peer_info and free any allocated resources linked to it
2721 * @param pi The peer_info to destroy.
2723 * @return GNUNET_OK on success
2726 peer_info_destroy (struct MeshPeerInfo *pi)
2728 struct GNUNET_PeerIdentity id;
2729 struct MeshPeerPath *p;
2730 struct MeshPeerPath *nextp;
2732 GNUNET_PEER_resolve (pi->id, &id);
2733 GNUNET_PEER_change_rc (pi->id, -1);
2736 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2739 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2740 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2742 if (NULL != pi->dhtget)
2744 GNUNET_DHT_get_stop (pi->dhtget);
2745 GNUNET_free (pi->dhtgetcls);
2751 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2755 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2757 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2765 * Remove all paths that rely on a direct connection between p1 and p2
2766 * from the peer itself and notify all tunnels about it.
2768 * @param peer PeerInfo of affected peer.
2769 * @param p1 GNUNET_PEER_Id of one peer.
2770 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2773 * TODO: optimize (see below)
2776 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2779 struct MeshPeerPath *p;
2780 struct MeshPeerPath *aux;
2781 struct MeshPeerInfo *peer_d;
2783 unsigned int destroyed;
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2790 p = peer->path_head;
2794 for (i = 0; i < (p->length - 1); i++)
2796 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2797 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2799 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2810 for (i = 0; i < peer->ntunnels; i++)
2812 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2816 * Problem: one or more peers have been deleted from the tunnel tree.
2817 * We don't know who they are to try to add them again.
2818 * We need to try to find a new path for each of the disconnected peers.
2819 * Some of them might already have a path to reach them that does not
2820 * involve p1 and p2. Adding all anew might render in a better tree than
2821 * the trivial immediate fix.
2823 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2824 * its children will be reachable trough him.
2826 peer_d = peer_info_get_short (d);
2829 for (p = peer_d->path_head; NULL != p; p = p->next)
2831 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2839 /* No callback, as peer will be already disconnected and a connection
2840 * scheduled by tunnel_notify_connection_broken.
2842 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2846 peer_info_connect (peer_d, peer->tunnels[i]);
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2854 * Add the path to the peer and update the path used to reach it in case this
2857 * @param peer_info Destination peer to add the path to.
2858 * @param path New path to add. Last peer must be the peer in arg 1.
2859 * Path will be either used of freed if already known.
2860 * @param trusted Do we trust that this path is real?
2863 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2866 struct MeshPeerPath *aux;
2870 if ((NULL == peer_info) || (NULL == path))
2873 path_destroy (path);
2876 if (path->peers[path->length - 1] != peer_info->id)
2879 path_destroy (path);
2882 if (path->length <= 2 && GNUNET_NO == trusted)
2884 /* Only allow CORE to tell us about direct paths */
2885 path_destroy (path);
2888 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2889 for (l = 1; l < path->length; l++)
2891 if (path->peers[l] == myid)
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2894 for (l2 = 0; l2 < path->length - l; l2++)
2896 path->peers[l2] = path->peers[l + l2];
2901 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2906 struct GNUNET_PeerIdentity id;
2908 GNUNET_PEER_resolve (peer_info->id, &id);
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2910 path->length, GNUNET_i2s (&id));
2913 l = path_get_length (path);
2920 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2921 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2923 l2 = path_get_length (aux);
2926 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2927 peer_info->path_tail, aux, path);
2932 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2934 path_destroy (path);
2939 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2946 * Add the path to the origin peer and update the path used to reach it in case
2947 * this is the shortest.
2948 * The path is given in peer_info -> destination, therefore we turn the path
2949 * upside down first.
2951 * @param peer_info Peer to add the path to, being the origin of the path.
2952 * @param path New path to add after being inversed.
2953 * @param trusted Do we trust that this path is real?
2956 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2957 struct MeshPeerPath *path, int trusted)
2960 peer_info_add_path (peer_info, path, trusted);
2965 * Function called if the connection to the peer has been stalled for a while,
2966 * possibly due to a missed ACK. Poll the peer about its ACK status.
2968 * @param cls Closure (info about regex search).
2969 * @param tc TaskContext.
2972 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2974 struct MeshTunnelChildInfo *cinfo = cls;
2975 struct GNUNET_MESH_Poll msg;
2976 struct GNUNET_PeerIdentity id;
2977 struct MeshTunnel *t;
2979 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2980 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2986 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2987 msg.header.size = htons (sizeof (msg));
2988 msg.tid = htonl (t->id.tid);
2989 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2990 msg.last_ack = htonl (cinfo->fwd_ack);
2992 GNUNET_PEER_resolve (cinfo->id, &id);
2993 send_prebuilt_message (&msg.header, &id, cinfo->t);
2994 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2995 &tunnel_poll, cinfo);
3000 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3001 * to obtain a local peer -> destination path and interning the peer ids.
3003 * @return Newly allocated and created path
3005 static struct MeshPeerPath *
3006 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3007 unsigned int get_path_length,
3008 const struct GNUNET_PeerIdentity *put_path,
3009 unsigned int put_path_length)
3011 struct MeshPeerPath *p;
3017 GNUNET_PEER_change_rc (myid, 1);
3018 i = get_path_length;
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3020 for (i--; i >= 0; i--)
3022 id = GNUNET_PEER_intern (&get_path[i]);
3023 if (p->length > 0 && id == p->peers[p->length - 1])
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3026 GNUNET_PEER_change_rc (id, -1);
3030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3031 GNUNET_i2s (&get_path[i]));
3033 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3034 p->peers[p->length - 1] = id;
3037 i = put_path_length;
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3039 for (i--; i >= 0; i--)
3041 id = GNUNET_PEER_intern (&put_path[i]);
3044 /* PUT path went through us, so discard the path up until now and start
3045 * from here to get a much shorter (and loop-free) path.
3050 if (p->length > 0 && id == p->peers[p->length - 1])
3052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3053 GNUNET_PEER_change_rc (id, -1);
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3058 GNUNET_i2s (&put_path[i]));
3060 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3061 p->peers[p->length - 1] = id;
3065 if (get_path_length > 0)
3066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3067 GNUNET_i2s (&get_path[0]));
3068 if (put_path_length > 0)
3069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3070 GNUNET_i2s (&put_path[0]));
3071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3073 for (i = 0; i < p->length; i++)
3075 struct GNUNET_PeerIdentity peer_id;
3077 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3079 GNUNET_i2s (&peer_id));
3087 * Adds a path to the peer_infos of all the peers in the path
3089 * @param p Path to process.
3090 * @param confirmed Whether we know if the path works or not.
3093 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3097 /* TODO: invert and add */
3098 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3099 for (i++; i < p->length; i++)
3101 struct MeshPeerInfo *aux;
3102 struct MeshPeerPath *copy;
3104 aux = peer_info_get_short (p->peers[i]);
3105 copy = path_duplicate (p);
3106 copy->length = i + 1;
3107 peer_info_add_path (aux, copy, GNUNET_NO);
3113 * Send keepalive packets for a peer
3115 * @param cls Closure (tunnel for which to send the keepalive).
3116 * @param tc Notification context.
3118 * TODO: implement explicit multicast keepalive?
3121 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3125 * Search for a tunnel among the incoming tunnels
3127 * @param tid the local id of the tunnel
3129 * @return tunnel handler, NULL if doesn't exist
3131 static struct MeshTunnel *
3132 tunnel_get_incoming (MESH_TunnelNumber tid)
3134 struct GNUNET_HashCode hash;
3136 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3137 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3138 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3143 * Search for a tunnel among the tunnels for a client
3145 * @param c the client whose tunnels to search in
3146 * @param tid the local id of the tunnel
3148 * @return tunnel handler, NULL if doesn't exist
3150 static struct MeshTunnel *
3151 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3153 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3155 return tunnel_get_incoming (tid);
3159 struct GNUNET_HashCode hash;
3161 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3162 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3168 * Search for a tunnel by global ID using PEER_ID
3170 * @param pi owner of the tunnel
3171 * @param tid global tunnel number
3173 * @return tunnel handler, NULL if doesn't exist
3175 static struct MeshTunnel *
3176 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3178 struct MESH_TunnelID id;
3179 struct GNUNET_HashCode hash;
3184 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3185 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3190 * Search for a tunnel by global ID using full PeerIdentities
3192 * @param oid owner of the tunnel
3193 * @param tid global tunnel number
3195 * @return tunnel handler, NULL if doesn't exist
3197 static struct MeshTunnel *
3198 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3200 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3205 * Delete an active client from the tunnel.
3211 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3215 for (i = 0; i < t->nclients; i++)
3217 if (t->clients[i] == c)
3219 t->clients[i] = t->clients[t->nclients - 1];
3220 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3221 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3223 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3231 * Delete an ignored client from the tunnel.
3237 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3241 for (i = 0; i < t->nignore; i++)
3243 if (t->ignore[i] == c)
3245 t->ignore[i] = t->ignore[t->nignore - 1];
3246 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3254 * Delete a client from the tunnel. It should be only done on
3255 * client disconnection, otherwise use client_ignore_tunnel.
3261 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3263 tunnel_delete_ignored_client (t, c);
3264 tunnel_delete_active_client (t, c);
3269 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3271 * Destroys queue elements of all waiting transmissions and frees all memory
3272 * used by the struct and its elements.
3274 * @param cls Closure (tunnel info).
3275 * @param key Hash of GNUNET_PEER_Id (unused).
3276 * @param value MeshTunnelChildInfo of the child.
3278 * @return always GNUNET_YES, to keep iterating
3281 tunnel_destroy_child (void *cls,
3282 const struct GNUNET_HashCode * key,
3285 struct MeshTunnelChildInfo *cinfo = value;
3286 struct MeshTunnel *t = cls;
3287 struct MeshPeerQueue *q;
3291 for (c = 0; c < cinfo->send_buffer_n; c++)
3293 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3294 q = cinfo->send_buffer[i];
3295 cinfo->send_buffer[i] = NULL;
3297 queue_destroy (q, GNUNET_YES);
3300 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3302 GNUNET_free_non_null (cinfo->send_buffer);
3303 GNUNET_free (cinfo);
3309 * Callback used to notify a client owner of a tunnel that a peer has
3310 * disconnected, most likely because of a path change.
3312 * @param cls Closure (tunnel this notification is about).
3313 * @param peer_id Short ID of disconnected peer.
3316 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3318 struct MeshTunnel *t = cls;
3319 struct MeshPeerInfo *peer;
3320 struct MeshPathInfo *path_info;
3322 if (NULL != t->owner && NULL != nc)
3324 struct GNUNET_MESH_PeerControl msg;
3326 msg.header.size = htons (sizeof (msg));
3327 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3328 msg.tunnel_id = htonl (t->local_tid);
3329 GNUNET_PEER_resolve (peer_id, &msg.peer);
3330 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3331 &msg.header, GNUNET_NO);
3333 peer = peer_info_get_short (peer_id);
3334 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3335 path_info->peer = peer;
3337 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3343 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3344 * needed rescources.
3345 * If peer already exists, reevaluate shortest path and change if different.
3347 * @param t Tunnel we want to add a new peer to
3348 * @param peer PeerInfo of the peer being added
3352 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3354 struct GNUNET_PeerIdentity id;
3355 struct MeshPeerPath *best_p;
3356 struct MeshPeerPath *p;
3357 unsigned int best_cost;
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3361 GNUNET_PEER_resolve (peer->id, &id);
3363 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3366 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3367 GNUNET_assert (GNUNET_OK ==
3368 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3373 if (NULL != (p = peer->path_head))
3376 best_cost = tree_get_path_cost (t->tree, p);
3379 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3386 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3387 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3388 t->path_refresh_task =
3389 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3393 /* Start a DHT get */
3394 peer_info_connect (peer, t);
3396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3400 * Add a path to a tunnel which we don't own, just to remember the next hop.
3401 * If destination node was already in the tunnel, the first hop information
3402 * will be replaced with the new path.
3404 * @param t Tunnel we want to add a new peer to
3405 * @param p Path to add
3406 * @param own_pos Position of local node in path.
3410 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3411 unsigned int own_pos)
3413 struct GNUNET_PeerIdentity id;
3415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3416 GNUNET_assert (0 != own_pos);
3417 tree_add_path (t->tree, p, NULL, NULL);
3418 if (own_pos < p->length - 1)
3420 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3421 tree_update_first_hops (t->tree, myid, &id);
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3427 * Add a client to a tunnel, initializing all needed data structures.
3429 * @param t Tunnel to which add the client.
3430 * @param c Client which to add to the tunnel.
3433 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3435 struct MeshTunnelClientInfo clinfo;
3437 GNUNET_array_append (t->clients, t->nclients, c);
3438 clinfo.fwd_ack = t->fwd_pid + 1;
3439 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3440 clinfo.fwd_pid = t->fwd_pid;
3441 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3443 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3448 * Notifies a tunnel that a connection has broken that affects at least
3449 * some of its peers. Sends a notification towards the root of the tree.
3450 * In case the peer is the owner of the tree, notifies the client that owns
3451 * the tunnel and tries to reconnect.
3453 * @param t Tunnel affected.
3454 * @param p1 Peer that got disconnected from p2.
3455 * @param p2 Peer that got disconnected from p1.
3457 * @return Short ID of the peer disconnected (either p1 or p2).
3458 * 0 if the tunnel remained unaffected.
3460 static GNUNET_PEER_Id
3461 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3467 tree_notify_connection_broken (t->tree, p1, p2,
3468 &tunnel_notify_client_peer_disconnected,
3470 if (myid != p1 && myid != p2)
3476 if (tree_get_predecessor (t->tree) != 0)
3478 /* We are the peer still connected, notify owner of the disconnection. */
3479 struct GNUNET_MESH_PathBroken msg;
3480 struct GNUNET_PeerIdentity neighbor;
3482 msg.header.size = htons (sizeof (msg));
3483 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3484 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3485 msg.tid = htonl (t->id.tid);
3486 msg.peer1 = my_full_id;
3487 GNUNET_PEER_resolve (pid, &msg.peer2);
3488 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3489 send_prebuilt_message (&msg.header, &neighbor, t);
3497 * Send a multicast packet to a neighbor.
3499 * @param cls Closure (Info about the multicast packet)
3500 * @param neighbor_id Short ID of the neighbor to send the packet to.
3503 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3505 struct MeshData *mdata = cls;
3506 struct MeshTransmissionDescriptor *info;
3507 struct GNUNET_PeerIdentity neighbor;
3508 struct GNUNET_MessageHeader *msg;
3510 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3512 info->mesh_data = mdata;
3513 (mdata->reference_counter) ++;
3514 info->destination = neighbor_id;
3515 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3517 GNUNET_i2s (&neighbor));
3518 info->peer = peer_info_get (&neighbor);
3519 GNUNET_assert (NULL != info->peer);
3520 msg = (struct GNUNET_MessageHeader *) mdata->data;
3523 info->mesh_data->data_len,
3530 * Queue a message in a tunnel in multicast, sending a copy to each child node
3531 * down the local one in the tunnel tree.
3533 * @param t Tunnel in which to send the data.
3534 * @param msg Message to be sent.
3537 tunnel_send_multicast (struct MeshTunnel *t,
3538 const struct GNUNET_MessageHeader *msg)
3540 struct MeshData *mdata;
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3543 " sending a multicast packet...\n");
3545 mdata = GNUNET_malloc (sizeof (struct MeshData));
3546 mdata->data_len = ntohs (msg->size);
3548 mdata->data = GNUNET_malloc (mdata->data_len);
3549 memcpy (mdata->data, msg, mdata->data_len);
3550 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3552 struct GNUNET_MESH_Multicast *mcast;
3554 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3555 if (t->fwd_queue_n >= t->fwd_queue_max)
3558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3560 " message from %s!\n",
3561 GNUNET_i2s(&mcast->oid));
3562 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3563 " message at %s!\n",
3564 GNUNET_i2s(&my_full_id));
3565 GNUNET_free (mdata->data);
3566 GNUNET_free (mdata);
3570 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3572 ntohl (mcast->ttl));
3576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3579 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3580 if (mdata->reference_counter == 0)
3582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3583 " no one to send data to\n");
3584 GNUNET_free (mdata->data);
3585 GNUNET_free (mdata);
3590 mdata->total_out = mdata->reference_counter;
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 " sending a multicast packet done\n");
3599 * Increase the SKIP value of all peers that
3600 * have not received a unicast message.
3602 * @param cls Closure (ID of the peer that HAS received the message).
3603 * @param key ID of the neighbor.
3604 * @param value Information about the neighbor.
3606 * @return GNUNET_YES to keep iterating.
3609 tunnel_add_skip (void *cls,
3610 const struct GNUNET_HashCode * key,
3613 struct GNUNET_PeerIdentity *neighbor = cls;
3614 struct MeshTunnelChildInfo *cinfo = value;
3616 /* TODO compare only pointers? key == neighbor? */
3617 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3627 * @brief Get neighbor's Flow Control information.
3629 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3630 * descendant of the local node in a certain tunnel.
3631 * If the info is not yet there (recently created path), creates the data struct
3632 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3635 * @param t Tunnel related.
3636 * @param peer Neighbor whose Flow Control info is needed.
3638 * @return Neighbor's Flow Control info.
3640 static struct MeshTunnelChildInfo *
3641 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3642 const struct GNUNET_PeerIdentity *peer)
3644 struct MeshTunnelChildInfo *cinfo;
3646 if (NULL == t->children_fc)
3649 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3655 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3656 cinfo->id = GNUNET_PEER_intern (peer);
3657 cinfo->skip = t->fwd_pid;
3660 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3661 cinfo->fwd_ack = t->fwd_pid + delta;
3662 cinfo->bck_ack = delta;
3663 cinfo->bck_pid = -1;
3665 cinfo->send_buffer =
3666 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3668 GNUNET_assert (GNUNET_OK ==
3669 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3672 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3679 * Get the Flow Control info of a client.
3681 * @param t Tunnel on which to look.
3682 * @param c Client whose ACK to get.
3684 * @return ACK value.
3686 static struct MeshTunnelClientInfo *
3687 tunnel_get_client_fc (struct MeshTunnel *t,
3688 struct MeshClient *c)
3692 for (i = 0; i < t->nclients; i++)
3694 if (t->clients[i] != c)
3696 return &t->clients_fc[i];
3699 return NULL; // avoid compiler / coverity complaints
3704 * Iterator to get the appropiate ACK value from all children nodes.
3706 * @param cls Closue (tunnel).
3707 * @param id Id of the child node.
3710 tunnel_get_child_fwd_ack (void *cls,
3713 struct GNUNET_PeerIdentity peer_id;
3714 struct MeshTunnelChildInfo *cinfo;
3715 struct MeshTunnelChildIteratorContext *ctx = cls;
3716 struct MeshTunnel *t = ctx->t;
3719 GNUNET_PEER_resolve (id, &peer_id);
3720 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3721 ack = cinfo->fwd_ack;
3724 if (GNUNET_NO == ctx->init)
3726 ctx->max_child_ack = ack;
3727 ctx->init = GNUNET_YES;
3730 if (GNUNET_YES == t->speed_min)
3732 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3736 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3743 * Get the maximum PID allowed to transmit to any
3744 * tunnel child of the local peer, depending on the tunnel
3745 * buffering/speed settings.
3749 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3752 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3754 struct MeshTunnelChildIteratorContext ctx;
3756 ctx.max_child_ack = 0;
3758 ctx.init = GNUNET_NO;
3759 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3761 if (0 == ctx.nchildren)
3763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3764 " tunnel has no children, no FWD ACK\n");
3768 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3769 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3771 return (int64_t) ctx.max_child_ack;
3776 * Set the FWD ACK value of a client in a particular tunnel.
3778 * @param t Tunnel affected.
3779 * @param c Client whose ACK to set.
3780 * @param ack ACK value.
3783 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3784 struct MeshClient *c,
3789 for (i = 0; i < t->nclients; i++)
3791 if (t->clients[i] != c)
3793 t->clients_fc[i].fwd_ack = ack;
3801 * Get the highest ACK value of all clients in a particular tunnel,
3802 * according to the buffering/speed settings.
3804 * @param t Tunnel on which to look.
3806 * @return Corresponding ACK value (max uint32_t).
3807 * If no clients are suscribed, -1LL.
3810 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3815 if (0 == t->nclients)
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3818 " tunnel has no clients, no FWD ACK\n");
3822 for (ack = -1LL, i = 0; i < t->nclients; i++)
3825 (GNUNET_YES == t->speed_min &&
3826 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3827 (GNUNET_NO == t->speed_min &&
3828 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3830 ack = t->clients_fc[i].fwd_ack;
3834 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3835 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3837 return (uint32_t) ack;
3842 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3843 * mode and the status of all children nodes.
3847 * @return Maximum PID allowed.
3850 tunnel_get_fwd_ack (struct MeshTunnel *t)
3854 uint32_t buffer_free;
3858 count = t->fwd_pid - t->skip;
3859 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3860 child_ack = tunnel_get_children_fwd_ack (t);
3861 client_ack = tunnel_get_clients_fwd_ack (t);
3862 if (GNUNET_YES == t->nobuffer)
3865 if (-1LL == child_ack)
3866 child_ack = client_ack;
3867 if (-1LL == child_ack)
3870 client_ack = child_ack = ack;
3875 ack = count + buffer_free; // Overflow? OK!
3877 if (-1LL == child_ack)
3879 // Node has no children, child_ack AND core buffer are irrelevant.
3880 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3881 return (uint32_t) client_ack;
3883 if (-1LL == client_ack)
3887 if (GNUNET_YES == t->speed_min)
3889 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3890 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3894 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3895 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3899 count, buffer_free, child_ack, client_ack, ack);
3905 * Build a local ACK message and send it to a local client.
3907 * @param t Tunnel on which to send the ACK.
3908 * @param c Client to whom send the ACK.
3909 * @param ack Value of the ACK.
3912 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3914 struct GNUNET_MESH_LocalAck msg;
3916 msg.header.size = htons (sizeof (msg));
3917 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3918 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3919 msg.max_pid = htonl (ack);
3920 GNUNET_SERVER_notification_context_unicast(nc,
3927 * Build an ACK message and queue it to send to the given peer.
3929 * @param t Tunnel on which to send the ACK.
3930 * @param peer Peer to whom send the ACK.
3931 * @param ack Value of the ACK.
3934 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3936 struct GNUNET_MESH_ACK msg;
3938 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3939 msg.header.size = htons (sizeof (msg));
3940 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3941 msg.pid = htonl (ack);
3942 msg.tid = htonl (t->id.tid);
3944 send_prebuilt_message (&msg.header, peer, t);
3949 * Notify a the owner of a tunnel about how many more
3950 * payload packages will we accept on a given tunnel.
3952 * @param t Tunnel on which to send the ACK.
3955 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3960 "Sending client FWD ACK on tunnel %X\n",
3963 ack = tunnel_get_fwd_ack (t);
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3966 if (t->last_fwd_ack == ack)
3968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3973 t->last_fwd_ack = ack;
3974 send_local_ack (t, t->owner, ack);
3979 * Send an ACK informing the predecessor about the available buffer space.
3980 * In case there is no predecessor, inform the owning client.
3981 * If buffering is off, send only on behalf of children or self if endpoint.
3982 * If buffering is on, send when sent to children and buffer space is free.
3983 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3984 * the ACK itself goes "back" (towards root).
3986 * @param t Tunnel on which to send the ACK.
3987 * @param type Type of message that triggered the ACK transmission.
3990 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3992 struct GNUNET_PeerIdentity id;
3995 if (NULL != t->owner)
3997 tunnel_send_client_fwd_ack (t);
4000 /* Is it after unicast / multicast retransmission? */
4003 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4004 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4006 "ACK due to FWD DATA retransmission\n");
4007 if (GNUNET_YES == t->nobuffer)
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4013 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4014 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4016 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4017 t->force_ack = GNUNET_YES;
4023 /* Check if we need to transmit the ACK */
4024 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4025 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4026 GNUNET_NO == t->force_ack)
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4030 " t->qmax: %u, t->qn: %u\n",
4031 t->fwd_queue_max, t->fwd_queue_n);
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4033 " t->pid: %u, t->ack: %u\n",
4034 t->fwd_pid, t->last_fwd_ack);
4038 /* Ok, ACK might be necessary, what PID to ACK? */
4039 ack = tunnel_get_fwd_ack (t);
4041 /* If speed_min and not all children have ack'd, dont send yet */
4042 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4048 t->last_fwd_ack = ack;
4049 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4050 send_ack (t, &id, ack);
4052 t->force_ack = GNUNET_NO;
4057 * Iterator to send a child node a BCK ACK to allow him to send more
4060 * @param cls Closure (tunnel).
4061 * @param id Id of the child node.
4064 tunnel_send_child_bck_ack (void *cls,
4067 struct MeshTunnel *t = cls;
4068 struct MeshTunnelChildInfo *cinfo;
4069 struct GNUNET_PeerIdentity peer;
4072 GNUNET_PEER_resolve (id, &peer);
4073 cinfo = tunnel_get_neighbor_fc (t, &peer);
4074 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4076 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4079 " Not sending ACK, not needed\n");
4082 cinfo->bck_ack = ack;
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4085 " Sending BCK ACK %u (last sent: %u)\n",
4086 ack, cinfo->bck_ack);
4087 send_ack (t, &peer, ack);
4092 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4094 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4096 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4097 * - implement a size 1 buffer in each client_fc AND children_fc
4098 * to hold at least 1 message per "child".
4099 * problem: violates no buffer policy
4100 * - ack 0 and make "children" poll for transmission slots
4101 * problem: big overhead, extra latency even in low traffic
4104 * @param t Tunnel on which to send the BCK ACKs.
4107 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4110 unsigned int tunnel_delta;
4112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4114 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4117 /* Find client whom to allow to send to origin (with lowest buffer space) */
4118 for (i = 0; i < t->nclients; i++)
4120 struct MeshTunnelClientInfo *clinfo;
4123 clinfo = &t->clients_fc[i];
4124 delta = clinfo->bck_ack - clinfo->bck_pid;
4125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4126 t->clients[i]->id, delta);
4128 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4129 (GNUNET_YES == t->nobuffer && 0 == delta))
4133 ack = clinfo->bck_pid;
4134 ack += t->nobuffer ? 1 : tunnel_delta;
4135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4136 " sending ack to client %u: %u\n",
4137 t->clients[i]->id, ack);
4138 send_local_ack (t, t->clients[i], ack);
4139 clinfo->bck_ack = ack;
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 " not sending ack to client %u (td %u, d %u)\n",
4145 t->clients[i]->id, tunnel_delta, delta);
4152 * Send an ACK informing the children nodes and destination clients about
4153 * the available buffer space.
4154 * If buffering is off, send only on behalf of root (can be self).
4155 * If buffering is on, send when sent to predecessor and buffer space is free.
4156 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4157 * the ACK itself goes "forward" (towards children/clients).
4159 * @param t Tunnel on which to send the ACK.
4160 * @param type Type of message that triggered the ACK transmission.
4163 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4166 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4167 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4168 /* Is it after data to_origin retransmission? */
4171 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4172 if (GNUNET_YES == t->nobuffer)
4174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4175 " Not sending ACK, nobuffer\n");
4179 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4180 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4182 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4183 t->force_ack = GNUNET_YES;
4189 tunnel_send_clients_bck_ack (t);
4190 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4191 t->force_ack = GNUNET_NO;
4196 * @brief Re-initiate traffic to this peer if necessary.
4198 * Check if there is traffic queued towards this peer
4199 * and the core transmit handle is NULL (traffic was stalled).
4200 * If so, call core tmt rdy.
4202 * @param cls Closure (unused)
4203 * @param peer_id Short ID of peer to which initiate traffic.
4206 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4208 struct MeshPeerInfo *peer;
4209 struct GNUNET_PeerIdentity id;
4210 struct MeshPeerQueue *q;
4213 peer = peer_info_get_short(peer_id);
4214 if (NULL != peer->core_transmit)
4217 q = queue_get_next(peer);
4220 /* Might br multicast traffic already sent to this particular peer but
4221 * not to other children in this tunnel.
4222 * This way t->queue_n would be > 0 but the queue of this particular peer
4228 GNUNET_PEER_resolve (peer->id, &id);
4229 peer->core_transmit =
4230 GNUNET_CORE_notify_transmit_ready(core_handle,
4233 GNUNET_TIME_UNIT_FOREVER_REL,
4243 * @brief Allow transmission of FWD traffic on this tunnel
4245 * Check if there is traffic queued towards any children
4246 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4248 * @param t Tunnel on which to unlock FWD traffic.
4251 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4253 if (0 == t->fwd_queue_n)
4256 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4261 * @brief Allow transmission of BCK traffic on this tunnel
4263 * Check if there is traffic queued towards the root of the tree
4264 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4266 * @param t Tunnel on which to unlock BCK traffic.
4269 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4271 if (0 == t->bck_queue_n)
4274 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4279 * Send a message to all peers in this tunnel that the tunnel is no longer
4282 * @param t The tunnel whose peers to notify.
4285 tunnel_send_destroy (struct MeshTunnel *t)
4287 struct GNUNET_MESH_TunnelDestroy msg;
4289 msg.header.size = htons (sizeof (msg));
4290 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4291 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4292 msg.tid = htonl (t->id.tid);
4293 tunnel_send_multicast (t, &msg.header);
4298 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4300 * @param cls Closure (Tunnel which to cancel).
4301 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4304 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4306 struct MeshTunnel *t = cls;
4307 struct MeshPeerInfo *peer_info;
4308 struct MeshPeerQueue *pq;
4309 struct MeshPeerQueue *next;
4311 peer_info = peer_info_get_short (neighbor_id);
4312 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4315 if (pq->tunnel == t)
4317 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4318 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4319 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4321 // Should have been removed on destroy children
4324 queue_destroy (pq, GNUNET_YES);
4327 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4329 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4330 peer_info->core_transmit = NULL;
4335 * Destroy the tunnel and free any allocated resources linked to it.
4337 * @param t the tunnel to destroy
4339 * @return GNUNET_OK on success
4342 tunnel_destroy (struct MeshTunnel *t)
4344 struct MeshClient *c;
4345 struct GNUNET_HashCode hash;
4356 struct GNUNET_PeerIdentity id;
4358 GNUNET_PEER_resolve (t->id.oid, &id);
4359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4360 GNUNET_i2s (&id), t->id.tid);
4362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4366 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4367 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4375 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4377 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4384 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4385 for (i = 0; i < t->nclients; i++)
4389 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4395 for (i = 0; i < t->nignore; i++)
4399 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4406 if (t->nclients > 0)
4409 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4414 GNUNET_free (t->clients);
4415 GNUNET_free (t->clients_fc);
4418 if (NULL != t->peers)
4420 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4422 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4425 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4426 &tunnel_destroy_child,
4428 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4429 t->children_fc = NULL;
4431 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4432 tree_destroy (t->tree);
4434 if (NULL != t->regex_ctx)
4435 regex_cancel_search (t->regex_ctx);
4436 if (NULL != t->dht_get_type)
4437 GNUNET_DHT_get_stop (t->dht_get_type);
4438 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4439 GNUNET_SCHEDULER_cancel (t->timeout_task);
4440 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4441 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4444 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4451 * Create a new tunnel
4453 * @param owner Who is the owner of the tunnel (short ID).
4454 * @param tid Tunnel Number of the tunnel.
4455 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4456 * @param local Tunnel Number for the tunnel, for the client point of view.
4458 * @return A new initialized tunnel. NULL on error.
4460 static struct MeshTunnel *
4461 tunnel_new (GNUNET_PEER_Id owner,
4462 MESH_TunnelNumber tid,
4463 struct MeshClient *client,
4464 MESH_TunnelNumber local)
4466 struct MeshTunnel *t;
4467 struct GNUNET_HashCode hash;
4469 if (n_tunnels >= max_tunnels && NULL == client)
4472 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4475 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4476 t->bck_queue_max = t->fwd_queue_max;
4477 t->tree = tree_new (owner);
4479 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4480 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4481 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4482 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4483 t->local_tid = local;
4484 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4486 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4488 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4490 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4491 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4498 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4505 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4507 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4508 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4512 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4522 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4523 * that are no longer needed, as well as nodes of no longer reachable peers.
4524 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4526 * @param t Tunnel from which to remove the path.
4527 * @param peer Short id of the peer which should be removed.
4530 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4534 r = tree_del_peer (t->tree, peer, NULL, NULL);
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4538 "Tunnel %u [%u] has no more nodes\n",
4539 t->id.oid, t->id.tid);
4545 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4546 * client when the client disconnects. If the client is not the owner, the
4547 * owner will get notified if no more clients are in the tunnel and the client
4548 * get removed from the tunnel's list.
4550 * @param cls closure (client that is disconnecting)
4551 * @param key the hash of the local tunnel id (used to access the hashmap)
4552 * @param value the value stored at the key (tunnel to destroy)
4554 * @return GNUNET_OK, keep iterating.
4557 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4559 struct MeshTunnel *t = value;
4560 struct MeshClient *c = cls;
4562 send_client_tunnel_disconnect(t, c);
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4567 tunnel_delete_client(t, c);
4568 client_delete_tunnel(c, t);
4571 tunnel_send_destroy(t);
4573 t->destroy = GNUNET_YES;
4580 * Timeout function, destroys tunnel if called
4582 * @param cls Closure (tunnel to destroy).
4583 * @param tc TaskContext
4586 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4588 struct MeshTunnel *t = cls;
4589 struct GNUNET_PeerIdentity id;
4591 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4594 GNUNET_PEER_resolve(t->id.oid, &id);
4595 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4596 "Tunnel %s [%X] timed out. Destroying.\n",
4597 GNUNET_i2s(&id), t->id.tid);
4598 send_clients_tunnel_destroy (t);
4603 * Resets the tunnel timeout. Starts it if no timeout was running.
4605 * @param t Tunnel whose timeout to reset.
4607 * TODO use heap to improve efficiency of scheduler.
4610 tunnel_reset_timeout (struct MeshTunnel *t)
4612 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4613 GNUNET_SCHEDULER_cancel (t->timeout_task);
4615 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4616 (refresh_path_time, 4), &tunnel_timeout, t);
4620 /******************************************************************************/
4621 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4622 /******************************************************************************/
4625 * Function to send a create path packet to a peer.
4627 * @param cls closure
4628 * @param size number of bytes available in buf
4629 * @param buf where the callee should write the message
4630 * @return number of bytes written to buf
4633 send_core_path_create (void *cls, size_t size, void *buf)
4635 struct MeshPathInfo *info = cls;
4636 struct GNUNET_MESH_ManipulatePath *msg;
4637 struct GNUNET_PeerIdentity *peer_ptr;
4638 struct MeshTunnel *t = info->t;
4639 struct MeshPeerPath *p = info->path;
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4646 sizeof (struct GNUNET_MESH_ManipulatePath) +
4647 p->length * sizeof (struct GNUNET_PeerIdentity);
4649 if (size < size_needed || NULL == buf)
4654 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4655 msg->header.size = htons (size_needed);
4656 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4657 msg->tid = ntohl (t->id.tid);
4660 if (GNUNET_YES == t->speed_min)
4661 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4662 if (GNUNET_YES == t->nobuffer)
4663 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4664 msg->opt = htonl(opt);
4667 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4668 for (i = 0; i < p->length; i++)
4670 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4677 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4683 * Fill the core buffer
4685 * @param cls closure (data itself)
4686 * @param size number of bytes available in buf
4687 * @param buf where the callee should write the message
4689 * @return number of bytes written to buf
4692 send_core_data_multicast (void *cls, size_t size, void *buf)
4694 struct MeshTransmissionDescriptor *info = cls;
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4698 GNUNET_assert (NULL != info);
4699 GNUNET_assert (NULL != info->peer);
4700 total_size = info->mesh_data->data_len;
4701 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4703 if (total_size > size)
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4709 memcpy (buf, info->mesh_data->data, total_size);
4712 struct GNUNET_MESH_Multicast *mc;
4713 struct GNUNET_MessageHeader *mh;
4716 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4718 mc = (struct GNUNET_MESH_Multicast *) mh;
4719 mh = (struct GNUNET_MessageHeader *) &mc[1];
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4721 " multicast, payload type %s\n",
4722 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4724 " multicast, payload size %u\n", ntohs (mh->size));
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4729 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4733 data_descriptor_decrement_rc (info->mesh_data);
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4742 * Creates a path ack message in buf and frees all unused resources.
4744 * @param cls closure (MeshTransmissionDescriptor)
4745 * @param size number of bytes available in buf
4746 * @param buf where the callee should write the message
4747 * @return number of bytes written to buf
4750 send_core_path_ack (void *cls, size_t size, void *buf)
4752 struct MeshTransmissionDescriptor *info = cls;
4753 struct GNUNET_MESH_PathACK *msg = buf;
4755 GNUNET_assert (NULL != info);
4756 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4761 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4762 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4763 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4764 msg->tid = htonl (info->origin->tid);
4765 msg->peer_id = my_full_id;
4768 /* TODO add signature */
4770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4771 return sizeof (struct GNUNET_MESH_PathACK);
4776 * Free a transmission that was already queued with all resources
4777 * associated to the request.
4779 * @param queue Queue handler to cancel.
4780 * @param clear_cls Is it necessary to free associated cls?
4783 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4785 struct MeshTransmissionDescriptor *dd;
4786 struct MeshPathInfo *path_info;
4787 struct MeshTunnelChildInfo *cinfo;
4788 struct GNUNET_PeerIdentity id;
4792 if (GNUNET_YES == clear_cls)
4794 switch (queue->type)
4796 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4798 GNUNET_assert (GNUNET_YES == queue->tunnel->destroy);
4799 /* FIXME: don't cancel, send and destroy tunnel in queue_send */
4801 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4802 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4803 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4804 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4805 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4806 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4808 " prebuilt message\n");
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4811 GNUNET_MESH_DEBUG_M2S(queue->type));
4813 data_descriptor_decrement_rc (dd->mesh_data);
4815 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4817 path_info = queue->cls;
4818 path_destroy (path_info->path);
4822 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4823 " type %s unknown!\n",
4824 GNUNET_MESH_DEBUG_M2S(queue->type));
4826 GNUNET_free_non_null (queue->cls);
4828 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4829 queue->peer->queue_tail,
4832 /* Delete from child_fc in the appropiate tunnel */
4833 max = queue->tunnel->fwd_queue_max;
4834 GNUNET_PEER_resolve (queue->peer->id, &id);
4835 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4838 for (i = 0; i < cinfo->send_buffer_n; i++)
4841 i2 = (cinfo->send_buffer_start + i) % max;
4842 if (cinfo->send_buffer[i2] == queue)
4844 /* Found corresponding entry in the send_buffer. Move all others back. */
4849 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4851 j2 = (cinfo->send_buffer_start + j) % max;
4852 j3 = (cinfo->send_buffer_start + j + 1) % max;
4853 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4856 cinfo->send_buffer[j3] = NULL;
4857 cinfo->send_buffer_n--;
4862 GNUNET_free (queue);
4867 * @brief Get the next transmittable message from the queue.
4869 * This will be the head, except in the case of being a data packet
4870 * not allowed by the destination peer.
4872 * @param peer Destination peer.
4874 * @return The next viable MeshPeerQueue element to send to that peer.
4875 * NULL when there are no transmittable messages.
4877 struct MeshPeerQueue *
4878 queue_get_next (const struct MeshPeerInfo *peer)
4880 struct MeshPeerQueue *q;
4881 struct MeshTunnel *t;
4882 struct MeshTransmissionDescriptor *info;
4883 struct MeshTunnelChildInfo *cinfo;
4884 struct GNUNET_MESH_Unicast *ucast;
4885 struct GNUNET_MESH_ToOrigin *to_orig;
4886 struct GNUNET_MESH_Multicast *mcast;
4887 struct GNUNET_PeerIdentity id;
4891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4892 for (q = peer->queue_head; NULL != q; q = q->next)
4896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4898 GNUNET_MESH_DEBUG_M2S(q->type));
4901 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4902 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4903 pid = ntohl (ucast->pid);
4904 GNUNET_PEER_resolve (info->peer->id, &id);
4905 cinfo = tunnel_get_neighbor_fc(t, &id);
4906 ack = cinfo->fwd_ack;
4908 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4909 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4910 pid = ntohl (to_orig->pid);
4913 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4914 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4915 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4917 // Not a multicast payload: multicast control traffic (destroy, etc)
4920 pid = ntohl (mcast->pid);
4921 GNUNET_PEER_resolve (info->peer->id, &id);
4922 cinfo = tunnel_get_neighbor_fc(t, &id);
4923 ack = cinfo->fwd_ack;
4926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4931 "********* ACK: %u, PID: %u\n",
4933 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4942 "********* NEXT!\n");
4945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4946 "********* nothing found\n");
4952 * Core callback to write a queued packet to core buffer
4954 * @param cls Closure (peer info).
4955 * @param size Number of bytes available in buf.
4956 * @param buf Where the to write the message.
4958 * @return number of bytes written to buf
4961 queue_send (void *cls, size_t size, void *buf)
4963 struct MeshPeerInfo *peer = cls;
4964 struct GNUNET_MessageHeader *msg;
4965 struct MeshPeerQueue *queue;
4966 struct MeshTunnel *t;
4967 struct MeshTunnelChildInfo *cinfo;
4968 struct GNUNET_PeerIdentity dst_id;
4971 peer->core_transmit = NULL;
4974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4975 queue = queue_get_next (peer);
4977 /* Queue has no internal mesh traffic nor sendable payload */
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4981 if (NULL == peer->queue_head)
4982 GNUNET_break (0); // Should've been canceled
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4987 GNUNET_PEER_resolve (peer->id, &dst_id);
4988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4989 "********* towards %s\n",
4990 GNUNET_i2s(&dst_id));
4991 /* Check if buffer size is enough for the message */
4992 if (queue->size > size)
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4995 "********* not enough room, reissue\n");
4996 peer->core_transmit =
4997 GNUNET_CORE_notify_transmit_ready (core_handle,
5000 GNUNET_TIME_UNIT_FOREVER_REL,
5007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5010 GNUNET_assert (0 < t->pending_messages);
5011 t->pending_messages--;
5012 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5016 "********* unicast: t->q (%u/%u)\n",
5017 t->fwd_queue_n, t->fwd_queue_max);
5019 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5026 switch (queue->type)
5029 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5030 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5031 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5032 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5033 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5035 "********* raw: %s\n",
5036 GNUNET_MESH_DEBUG_M2S (queue->type));
5038 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5039 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5040 data_size = send_core_data_raw (queue->cls, size, buf);
5041 msg = (struct GNUNET_MessageHeader *) buf;
5042 switch (ntohs (msg->type)) // Type of preconstructed message
5044 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5045 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5047 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5048 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5054 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5057 struct MeshTransmissionDescriptor *info = queue->cls;
5059 if ((1 == info->mesh_data->reference_counter
5060 && GNUNET_YES == t->speed_min)
5062 (info->mesh_data->total_out == info->mesh_data->reference_counter
5063 && GNUNET_NO == t->speed_min))
5065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5066 "********* considered sent\n");
5071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5072 "********* NOT considered sent yet\n");
5073 t->pending_messages++;
5076 data_size = send_core_data_multicast(queue->cls, size, buf);
5077 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5079 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5081 data_size = send_core_path_create (queue->cls, size, buf);
5083 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5085 data_size = send_core_path_ack (queue->cls, size, buf);
5087 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5089 data_size = send_core_data_multicast (queue->cls, size, buf);
5093 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5094 "********* type unknown: %u\n",
5098 switch (queue->type)
5100 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5101 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5102 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5103 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5104 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5108 "at pos %u (%p) != %p\n",
5109 cinfo->send_buffer_start,
5110 cinfo->send_buffer[cinfo->send_buffer_start],
5113 if (cinfo->send_buffer_n > 0)
5115 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5116 cinfo->send_buffer_n--;
5117 cinfo->send_buffer_start++;
5118 cinfo->send_buffer_start %= t->fwd_queue_max;
5129 /* Free queue, but cls was freed by send_core_* */
5130 queue_destroy (queue, GNUNET_NO);
5132 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5138 /* If more data in queue, send next */
5139 queue = queue_get_next(peer);
5142 struct GNUNET_PeerIdentity id;
5144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5145 GNUNET_PEER_resolve (peer->id, &id);
5146 peer->core_transmit =
5147 GNUNET_CORE_notify_transmit_ready(core_handle,
5150 GNUNET_TIME_UNIT_FOREVER_REL,
5158 if (NULL != peer->queue_head)
5160 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5161 "********* %s stalled\n",
5162 GNUNET_i2s(&my_full_id));
5164 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5165 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(MESH_POLL_TIME,
5166 &tunnel_poll, cinfo);
5169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5175 * @brief Queue and pass message to core when possible.
5177 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5178 * and accounts for it. In case the queue is full, the message is dropped and
5181 * Otherwise, message is treated as internal and allowed to go regardless of
5184 * @param cls Closure (@c type dependant). It will be used by queue_send to
5185 * build the message to be sent if not already prebuilt.
5186 * @param type Type of the message, 0 for a raw message.
5187 * @param size Size of the message.
5188 * @param dst Neighbor to send message to.
5189 * @param t Tunnel this message belongs to.
5192 queue_add (void *cls, uint16_t type, size_t size,
5193 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5195 struct MeshPeerQueue *queue;
5196 struct MeshTunnelChildInfo *cinfo;
5197 struct GNUNET_PeerIdentity id;
5203 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5204 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5206 n = &t->fwd_queue_n;
5207 max = &t->fwd_queue_max;
5209 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5211 n = &t->bck_queue_n;
5212 max = &t->bck_queue_max;
5219 GNUNET_STATISTICS_update(stats,
5220 "# messages dropped (buffer full)",
5222 return; // Drop message
5226 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5232 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5233 GNUNET_PEER_resolve (dst->id, &id);
5234 if (NULL == dst->core_transmit)
5236 dst->core_transmit =
5237 GNUNET_CORE_notify_transmit_ready (core_handle,
5240 GNUNET_TIME_UNIT_FOREVER_REL,
5246 t->pending_messages++;
5247 if (NULL == n) // Is this internal mesh traffic?
5250 // It's payload, keep track of buffer per peer.
5251 cinfo = tunnel_get_neighbor_fc(t, &id);
5252 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5253 if (NULL != cinfo->send_buffer[i])
5255 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5256 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5257 cinfo->send_buffer_start++;
5258 cinfo->send_buffer_start %= t->fwd_queue_max;
5262 cinfo->send_buffer_n++;
5264 cinfo->send_buffer[i] = queue;
5265 if (cinfo->send_buffer_n > t->fwd_queue_max)
5268 cinfo->send_buffer_n = t->fwd_queue_max;
5273 /******************************************************************************/
5274 /******************** MESH NETWORK HANDLERS **************************/
5275 /******************************************************************************/
5279 * Core handler for path creation
5281 * @param cls closure
5282 * @param message message
5283 * @param peer peer identity this notification is about
5284 * @param atsi performance data
5285 * @param atsi_count number of records in 'atsi'
5287 * @return GNUNET_OK to keep the connection open,
5288 * GNUNET_SYSERR to close it (signal serious error)
5291 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5292 const struct GNUNET_MessageHeader *message,
5293 const struct GNUNET_ATS_Information *atsi,
5294 unsigned int atsi_count)
5296 unsigned int own_pos;
5299 MESH_TunnelNumber tid;
5300 struct GNUNET_MESH_ManipulatePath *msg;
5301 struct GNUNET_PeerIdentity *pi;
5302 struct GNUNET_HashCode hash;
5303 struct MeshPeerPath *path;
5304 struct MeshPeerInfo *dest_peer_info;
5305 struct MeshPeerInfo *orig_peer_info;
5306 struct MeshTunnel *t;
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5309 "Received a path create msg [%s]\n",
5310 GNUNET_i2s (&my_full_id));
5311 size = ntohs (message->size);
5312 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5314 GNUNET_break_op (0);
5318 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5319 if (size % sizeof (struct GNUNET_PeerIdentity))
5321 GNUNET_break_op (0);
5324 size /= sizeof (struct GNUNET_PeerIdentity);
5327 GNUNET_break_op (0);
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5331 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5333 tid = ntohl (msg->tid);
5334 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5336 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5337 t = tunnel_get (pi, tid);
5338 if (NULL == t) // FIXME only for INCOMING tunnels?
5342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5343 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5346 // FIXME notify failure
5349 opt = ntohl (msg->opt);
5350 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5351 GNUNET_YES : GNUNET_NO;
5352 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5354 t->nobuffer = GNUNET_YES;
5355 t->last_fwd_ack = t->fwd_pid + 1;
5357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5358 " speed_min: %d, nobuffer:%d\n",
5359 t->speed_min, t->nobuffer);
5361 if (GNUNET_YES == t->nobuffer)
5363 t->bck_queue_max = 1;
5364 t->fwd_queue_max = 1;
5367 // FIXME only assign a local tid if a local client is interested (on demand)
5368 while (NULL != tunnel_get_incoming (next_local_tid))
5369 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5370 t->local_tid_dest = next_local_tid++;
5371 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5374 tunnel_reset_timeout (t);
5375 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5377 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5386 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5387 if (NULL == dest_peer_info)
5389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5390 " Creating PeerInfo for destination.\n");
5391 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5392 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5393 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5395 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5397 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5398 if (NULL == orig_peer_info)
5400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5401 " Creating PeerInfo for origin.\n");
5402 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5403 orig_peer_info->id = GNUNET_PEER_intern (pi);
5404 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5408 path = path_new (size);
5410 for (i = 0; i < size; i++)
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5413 GNUNET_i2s (&pi[i]));
5414 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5415 if (path->peers[i] == myid)
5418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5421 /* cannot be self, must be 'not found' */
5422 /* create path: self not found in path through self */
5423 GNUNET_break_op (0);
5424 path_destroy (path);
5428 path_add_to_peers (path, GNUNET_NO);
5429 tunnel_add_path (t, path, own_pos);
5430 if (own_pos == size - 1)
5432 /* It is for us! Send ack. */
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5434 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5435 if (NULL == t->peers)
5437 /* New tunnel! Notify clients on first payload message. */
5438 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5440 GNUNET_break (GNUNET_SYSERR !=
5441 GNUNET_CONTAINER_multihashmap_put (t->peers,
5442 &my_full_id.hashPubKey,
5445 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5450 struct MeshPeerPath *path2;
5452 /* It's for somebody else! Retransmit. */
5453 path2 = path_duplicate (path);
5454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5455 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5456 path2 = path_duplicate (path);
5457 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5458 send_create_path (dest_peer_info, path, t);
5465 * Core handler for path destruction
5467 * @param cls closure
5468 * @param message message
5469 * @param peer peer identity this notification is about
5470 * @param atsi performance data
5471 * @param atsi_count number of records in 'atsi'
5473 * @return GNUNET_OK to keep the connection open,
5474 * GNUNET_SYSERR to close it (signal serious error)
5477 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5478 const struct GNUNET_MessageHeader *message,
5479 const struct GNUNET_ATS_Information *atsi,
5480 unsigned int atsi_count)
5482 struct GNUNET_MESH_ManipulatePath *msg;
5483 struct GNUNET_PeerIdentity *pi;
5484 struct MeshPeerPath *path;
5485 struct MeshTunnel *t;
5486 unsigned int own_pos;
5490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5491 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5492 size = ntohs (message->size);
5493 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5495 GNUNET_break_op (0);
5499 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5500 if (size % sizeof (struct GNUNET_PeerIdentity))
5502 GNUNET_break_op (0);
5505 size /= sizeof (struct GNUNET_PeerIdentity);
5508 GNUNET_break_op (0);
5511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5513 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5514 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5516 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5518 t = tunnel_get (pi, ntohl (msg->tid));
5521 /* TODO notify back: we don't know this tunnel */
5522 GNUNET_break_op (0);
5525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5526 path = path_new (size);
5528 for (i = 0; i < size; i++)
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5531 GNUNET_i2s (&pi[i]));
5532 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5533 if (path->peers[i] == myid)
5536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5537 if (own_pos < path->length - 1)
5538 send_prebuilt_message (message, &pi[own_pos + 1], t);
5540 send_client_tunnel_disconnect(t, NULL);
5542 tunnel_delete_peer (t, path->peers[path->length - 1]);
5543 path_destroy (path);
5549 * Core handler for notifications of broken paths
5551 * @param cls closure
5552 * @param message message
5553 * @param peer peer identity this notification is about
5554 * @param atsi performance data
5555 * @param atsi_count number of records in 'atsi'
5557 * @return GNUNET_OK to keep the connection open,
5558 * GNUNET_SYSERR to close it (signal serious error)
5561 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5562 const struct GNUNET_MessageHeader *message,
5563 const struct GNUNET_ATS_Information *atsi,
5564 unsigned int atsi_count)
5566 struct GNUNET_MESH_PathBroken *msg;
5567 struct MeshTunnel *t;
5569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5570 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5571 msg = (struct GNUNET_MESH_PathBroken *) message;
5572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5573 GNUNET_i2s (&msg->peer1));
5574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5575 GNUNET_i2s (&msg->peer2));
5576 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5579 GNUNET_break_op (0);
5582 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5583 GNUNET_PEER_search (&msg->peer2));
5590 * Core handler for tunnel destruction
5592 * @param cls closure
5593 * @param message message
5594 * @param peer peer identity this notification is about
5595 * @param atsi performance data
5596 * @param atsi_count number of records in 'atsi'
5598 * @return GNUNET_OK to keep the connection open,
5599 * GNUNET_SYSERR to close it (signal serious error)
5602 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5603 const struct GNUNET_MessageHeader *message,
5604 const struct GNUNET_ATS_Information *atsi,
5605 unsigned int atsi_count)
5607 struct GNUNET_MESH_TunnelDestroy *msg;
5608 struct MeshTunnel *t;
5610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5611 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5612 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5614 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5615 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5618 /* Probably already got the message from another path,
5619 * destroyed the tunnel and retransmitted to children.
5622 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5625 if (t->id.oid == myid)
5627 GNUNET_break_op (0);
5630 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5632 /* Tunnel was incoming, notify clients */
5633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5634 t->local_tid, t->local_tid_dest);
5635 send_clients_tunnel_destroy (t);
5637 tunnel_send_destroy (t);
5638 t->destroy = GNUNET_YES;
5639 // TODO: add timeout to destroy the tunnel anyway
5645 * Core handler for mesh network traffic going from the origin to a peer
5647 * @param cls closure
5648 * @param peer peer identity this notification is about
5649 * @param message message
5650 * @param atsi performance data
5651 * @param atsi_count number of records in 'atsi'
5652 * @return GNUNET_OK to keep the connection open,
5653 * GNUNET_SYSERR to close it (signal serious error)
5656 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5657 const struct GNUNET_MessageHeader *message,
5658 const struct GNUNET_ATS_Information *atsi,
5659 unsigned int atsi_count)
5661 struct GNUNET_MESH_Unicast *msg;
5662 struct GNUNET_PeerIdentity *neighbor;
5663 struct MeshTunnelChildInfo *cinfo;
5664 struct MeshTunnel *t;
5665 GNUNET_PEER_Id dest_id;
5670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5673 size = ntohs (message->size);
5675 sizeof (struct GNUNET_MESH_Unicast) +
5676 sizeof (struct GNUNET_MessageHeader))
5681 msg = (struct GNUNET_MESH_Unicast *) message;
5682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5683 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5685 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5688 /* TODO notify back: we don't know this tunnel */
5689 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5690 GNUNET_break_op (0);
5693 pid = ntohl (msg->pid);
5694 if (t->fwd_pid == pid)
5696 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5698 " Already seen pid %u, DROPPING!\n", pid);
5703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5704 " pid %u not seen yet, forwarding\n", pid);
5707 t->skip += (pid - t->fwd_pid) - 1;
5710 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5712 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5713 GNUNET_break_op (0);
5714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5715 "Received PID %u, ACK %u\n",
5716 pid, t->last_fwd_ack);
5720 tunnel_reset_timeout (t);
5721 dest_id = GNUNET_PEER_search (&msg->destination);
5722 if (dest_id == myid)
5724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5725 " it's for us! sending to clients...\n");
5726 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5727 send_subscribed_clients (message, &msg[1].header, t);
5728 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5731 ttl = ntohl (msg->ttl);
5732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5735 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5736 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5737 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5741 " not for us, retransmitting...\n");
5743 neighbor = tree_get_first_hop (t->tree, dest_id);
5744 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5745 cinfo->fwd_pid = pid;
5746 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5749 if (GNUNET_YES == t->nobuffer &&
5750 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5752 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5753 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5754 GNUNET_break_op (0);
5757 send_prebuilt_message (message, neighbor, t);
5758 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5764 * Core handler for mesh network traffic going from the origin to all peers
5766 * @param cls closure
5767 * @param message message
5768 * @param peer peer identity this notification is about
5769 * @param atsi performance data
5770 * @param atsi_count number of records in 'atsi'
5771 * @return GNUNET_OK to keep the connection open,
5772 * GNUNET_SYSERR to close it (signal serious error)
5774 * TODO: Check who we got this from, to validate route.
5777 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5778 const struct GNUNET_MessageHeader *message,
5779 const struct GNUNET_ATS_Information *atsi,
5780 unsigned int atsi_count)
5782 struct GNUNET_MESH_Multicast *msg;
5783 struct MeshTunnel *t;
5787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5789 size = ntohs (message->size);
5790 if (sizeof (struct GNUNET_MESH_Multicast) +
5791 sizeof (struct GNUNET_MessageHeader) > size)
5793 GNUNET_break_op (0);
5796 msg = (struct GNUNET_MESH_Multicast *) message;
5797 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5801 /* TODO notify that we dont know that tunnel */
5802 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5803 GNUNET_break_op (0);
5806 pid = ntohl (msg->pid);
5807 if (t->fwd_pid == pid)
5809 /* already seen this packet, drop */
5810 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5812 " Already seen pid %u, DROPPING!\n", pid);
5813 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5819 " pid %u not seen yet, forwarding\n", pid);
5821 t->skip += (pid - t->fwd_pid) - 1;
5823 tunnel_reset_timeout (t);
5825 /* Transmit to locally interested clients */
5826 if (NULL != t->peers &&
5827 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5829 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5830 send_subscribed_clients (message, &msg[1].header, t);
5831 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5834 if (ntohl (msg->ttl) == 0)
5836 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5837 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5838 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5841 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5842 tunnel_send_multicast (t, message);
5848 * Core handler for mesh network traffic toward the owner of a tunnel
5850 * @param cls closure
5851 * @param message message
5852 * @param peer peer identity this notification is about
5853 * @param atsi performance data
5854 * @param atsi_count number of records in 'atsi'
5856 * @return GNUNET_OK to keep the connection open,
5857 * GNUNET_SYSERR to close it (signal serious error)
5860 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5861 const struct GNUNET_MessageHeader *message,
5862 const struct GNUNET_ATS_Information *atsi,
5863 unsigned int atsi_count)
5865 struct GNUNET_MESH_ToOrigin *msg;
5866 struct GNUNET_PeerIdentity id;
5867 struct MeshPeerInfo *peer_info;
5868 struct MeshTunnel *t;
5869 struct MeshTunnelChildInfo *cinfo;
5873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5875 size = ntohs (message->size);
5876 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5877 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5879 GNUNET_break_op (0);
5882 msg = (struct GNUNET_MESH_ToOrigin *) message;
5883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5884 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5885 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5886 pid = ntohl (msg->pid);
5890 /* TODO notify that we dont know this tunnel (whom)? */
5891 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5892 GNUNET_break_op (0);
5893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5894 "Received to_origin with PID %u on unknown tunnel\n",
5899 cinfo = tunnel_get_neighbor_fc(t, peer);
5906 if (cinfo->bck_pid == pid)
5908 /* already seen this packet, drop */
5909 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5911 " Already seen pid %u, DROPPING!\n", pid);
5912 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5917 " pid %u not seen yet, forwarding\n", pid);
5918 cinfo->bck_pid = pid;
5920 if (NULL != t->owner)
5923 struct GNUNET_MESH_ToOrigin *copy;
5925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5926 " it's for us! sending to clients...\n");
5927 /* TODO signature verification */
5928 memcpy (cbuf, message, size);
5929 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5930 copy->tid = htonl (t->local_tid);
5932 copy->pid = htonl (t->bck_pid);
5933 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5934 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5935 ©->header, GNUNET_NO);
5936 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5940 " not for us, retransmitting...\n");
5942 peer_info = peer_info_get (&msg->oid);
5943 if (NULL == peer_info)
5945 /* unknown origin of tunnel */
5949 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5950 send_prebuilt_message (message, &id, t);
5951 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5958 * Core handler for mesh network traffic point-to-point acks.
5960 * @param cls closure
5961 * @param message message
5962 * @param peer peer identity this notification is about
5963 * @param atsi performance data
5964 * @param atsi_count number of records in 'atsi'
5966 * @return GNUNET_OK to keep the connection open,
5967 * GNUNET_SYSERR to close it (signal serious error)
5970 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5971 const struct GNUNET_MessageHeader *message,
5972 const struct GNUNET_ATS_Information *atsi,
5973 unsigned int atsi_count)
5975 struct GNUNET_MESH_ACK *msg;
5976 struct MeshTunnel *t;
5979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5981 msg = (struct GNUNET_MESH_ACK *) message;
5983 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5987 /* TODO notify that we dont know this tunnel (whom)? */
5988 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5991 ack = ntohl (msg->pid);
5992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5994 /* Is this a forward or backward ACK? */
5995 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5997 struct MeshTunnelChildInfo *cinfo;
6000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6001 cinfo = tunnel_get_neighbor_fc (t, peer);
6002 cinfo->fwd_ack = ack;
6003 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6004 tunnel_unlock_fwd_queues (t);
6008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6010 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6011 tunnel_unlock_bck_queue (t);
6018 * Core handler for mesh network traffic point-to-point ack polls.
6020 * @param cls closure
6021 * @param message message
6022 * @param peer peer identity this notification is about
6023 * @param atsi performance data
6024 * @param atsi_count number of records in 'atsi'
6026 * @return GNUNET_OK to keep the connection open,
6027 * GNUNET_SYSERR to close it (signal serious error)
6030 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6031 const struct GNUNET_MessageHeader *message,
6032 const struct GNUNET_ATS_Information *atsi,
6033 unsigned int atsi_count)
6035 struct GNUNET_MESH_Poll *msg;
6036 struct MeshTunnel *t;
6038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6041 msg = (struct GNUNET_MESH_Poll *) message;
6043 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6047 /* TODO notify that we dont know this tunnel (whom)? */
6048 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6049 GNUNET_break_op (0);
6053 /* Is this a forward or backward ACK? */
6054 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6056 struct MeshTunnelChildInfo *cinfo;
6058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6059 cinfo = tunnel_get_neighbor_fc (t, peer);
6060 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6061 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6066 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6074 * Core handler for path ACKs
6076 * @param cls closure
6077 * @param message message
6078 * @param peer peer identity this notification is about
6079 * @param atsi performance data
6080 * @param atsi_count number of records in 'atsi'
6082 * @return GNUNET_OK to keep the connection open,
6083 * GNUNET_SYSERR to close it (signal serious error)
6086 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6087 const struct GNUNET_MessageHeader *message,
6088 const struct GNUNET_ATS_Information *atsi,
6089 unsigned int atsi_count)
6091 struct GNUNET_MESH_PathACK *msg;
6092 struct GNUNET_PeerIdentity id;
6093 struct MeshPeerInfo *peer_info;
6094 struct MeshPeerPath *p;
6095 struct MeshTunnel *t;
6097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6098 GNUNET_i2s (&my_full_id));
6099 msg = (struct GNUNET_MESH_PathACK *) message;
6100 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6103 /* TODO notify that we don't know the tunnel */
6104 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6106 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6110 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6112 peer_info = peer_info_get (&msg->peer_id);
6113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6114 GNUNET_i2s (&msg->peer_id));
6115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6118 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6121 "connect_by_string completed, stopping search\n");
6122 regex_cancel_search (t->regex_ctx);
6123 t->regex_ctx = NULL;
6126 /* Add paths to peers? */
6127 p = tree_get_path_to_peer (t->tree, peer_info->id);
6130 path_add_to_peers (p, GNUNET_YES);
6138 /* Message for us? */
6139 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6142 if (NULL == t->owner)
6144 GNUNET_break_op (0);
6147 if (NULL != t->dht_get_type)
6149 GNUNET_DHT_get_stop (t->dht_get_type);
6150 t->dht_get_type = NULL;
6152 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6154 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6155 send_client_peer_connected (t, peer_info->id);
6160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6161 " not for us, retransmitting...\n");
6162 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6163 peer_info = peer_info_get (&msg->oid);
6164 if (NULL == peer_info)
6166 /* If we know the tunnel, we should DEFINITELY know the peer */
6170 send_prebuilt_message (message, &id, t);
6176 * Core handler for mesh keepalives.
6178 * @param cls closure
6179 * @param message message
6180 * @param peer peer identity this notification is about
6181 * @param atsi performance data
6182 * @param atsi_count number of records in 'atsi'
6183 * @return GNUNET_OK to keep the connection open,
6184 * GNUNET_SYSERR to close it (signal serious error)
6186 * TODO: Check who we got this from, to validate route.
6189 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6190 const struct GNUNET_MessageHeader *message,
6191 const struct GNUNET_ATS_Information *atsi,
6192 unsigned int atsi_count)
6194 struct GNUNET_MESH_TunnelKeepAlive *msg;
6195 struct MeshTunnel *t;
6197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6200 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6201 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6205 /* TODO notify that we dont know that tunnel */
6206 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6207 GNUNET_break_op (0);
6211 tunnel_reset_timeout (t);
6213 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6214 tunnel_send_multicast (t, message);
6221 * Functions to handle messages from core
6223 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6224 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6225 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6226 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6227 sizeof (struct GNUNET_MESH_PathBroken)},
6228 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6229 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6230 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6231 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6232 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6233 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6234 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6235 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6236 sizeof (struct GNUNET_MESH_ACK)},
6237 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6238 sizeof (struct GNUNET_MESH_Poll)},
6239 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6240 sizeof (struct GNUNET_MESH_PathACK)},
6246 /******************************************************************************/
6247 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6248 /******************************************************************************/
6251 * deregister_app: iterator for removing each application registered by a client
6253 * @param cls closure
6254 * @param key the hash of the application id (used to access the hashmap)
6255 * @param value the value stored at the key (client)
6257 * @return GNUNET_OK on success
6260 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6262 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6263 GNUNET_break (GNUNET_YES ==
6264 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6270 * notify_client_connection_failure: notify a client that the connection to the
6271 * requested remote peer is not possible (for instance, no route found)
6272 * Function called when the socket is ready to queue more data. "buf" will be
6273 * NULL and "size" zero if the socket was closed for writing in the meantime.
6275 * @param cls closure
6276 * @param size number of bytes available in buf
6277 * @param buf where the callee should write the message
6278 * @return number of bytes written to buf
6281 notify_client_connection_failure (void *cls, size_t size, void *buf)
6284 struct MeshPeerInfo *peer_info;
6285 struct GNUNET_MESH_PeerControl *msg;
6286 struct GNUNET_PeerIdentity id;
6288 if (0 == size && NULL == buf)
6290 // TODO retry? cancel?
6294 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6295 peer_info = (struct MeshPeerInfo *) cls;
6296 msg = (struct GNUNET_MESH_PeerControl *) buf;
6297 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6298 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6299 // msg->tunnel_id = htonl(peer_info->t->tid);
6300 GNUNET_PEER_resolve (peer_info->id, &id);
6301 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6309 * Send keepalive packets for a peer
6311 * @param cls Closure (tunnel for which to send the keepalive).
6312 * @param tc Notification context.
6315 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6317 struct MeshTunnel *t = cls;
6318 struct GNUNET_MESH_TunnelKeepAlive *msg;
6319 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6322 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6323 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6329 "sending keepalive for tunnel %d\n", t->id.tid);
6331 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6332 msg->header.size = htons (size);
6333 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6334 msg->oid = my_full_id;
6335 msg->tid = htonl (t->id.tid);
6336 tunnel_send_multicast (t, &msg->header);
6338 t->path_refresh_task =
6339 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6340 tunnel_reset_timeout(t);
6345 * Function to process paths received for a new peer addition. The recorded
6346 * paths form the initial tunnel, which can be optimized later.
6347 * Called on each result obtained for the DHT search.
6349 * @param cls closure
6350 * @param exp when will this value expire
6351 * @param key key of the result
6352 * @param get_path path of the get request
6353 * @param get_path_length lenght of get_path
6354 * @param put_path path of the put request
6355 * @param put_path_length length of the put_path
6356 * @param type type of the result
6357 * @param size number of bytes in data
6358 * @param data pointer to the result data
6360 * TODO: re-issue the request after certain time? cancel after X results?
6363 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6364 const struct GNUNET_HashCode * key,
6365 const struct GNUNET_PeerIdentity *get_path,
6366 unsigned int get_path_length,
6367 const struct GNUNET_PeerIdentity *put_path,
6368 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6369 size_t size, const void *data)
6371 struct MeshPathInfo *path_info = cls;
6372 struct MeshPeerPath *p;
6373 struct GNUNET_PeerIdentity pi;
6376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6377 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6380 p = path_build_from_dht (get_path, get_path_length, put_path,
6382 path_add_to_peers (p, GNUNET_NO);
6384 for (i = 0; i < path_info->peer->ntunnels; i++)
6386 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6387 peer_info_connect (path_info->peer, path_info->t);
6395 * Function to process paths received for a new peer addition. The recorded
6396 * paths form the initial tunnel, which can be optimized later.
6397 * Called on each result obtained for the DHT search.
6399 * @param cls closure
6400 * @param exp when will this value expire
6401 * @param key key of the result
6402 * @param get_path path of the get request
6403 * @param get_path_length lenght of get_path
6404 * @param put_path path of the put request
6405 * @param put_path_length length of the put_path
6406 * @param type type of the result
6407 * @param size number of bytes in data
6408 * @param data pointer to the result data
6411 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6412 const struct GNUNET_HashCode * key,
6413 const struct GNUNET_PeerIdentity *get_path,
6414 unsigned int get_path_length,
6415 const struct GNUNET_PeerIdentity *put_path,
6416 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6417 size_t size, const void *data)
6419 const struct PBlock *pb = data;
6420 const struct GNUNET_PeerIdentity *pi = &pb->id;
6421 struct MeshTunnel *t = cls;
6422 struct MeshPeerInfo *peer_info;
6423 struct MeshPeerPath *p;
6425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6426 if (size != sizeof (struct PBlock))
6428 GNUNET_break_op (0);
6431 if (ntohl(pb->type) != t->type)
6433 GNUNET_break_op (0);
6436 GNUNET_assert (NULL != t->owner);
6437 peer_info = peer_info_get (pi);
6438 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6440 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6442 p = path_build_from_dht (get_path, get_path_length, put_path,
6444 path_add_to_peers (p, GNUNET_NO);
6446 tunnel_add_peer (t, peer_info);
6447 peer_info_connect (peer_info, t);
6452 * Function to process DHT string to regex matching.
6453 * Called on each result obtained for the DHT search.
6455 * @param cls closure (search context)
6456 * @param exp when will this value expire
6457 * @param key key of the result
6458 * @param get_path path of the get request (not used)
6459 * @param get_path_length lenght of get_path (not used)
6460 * @param put_path path of the put request (not used)
6461 * @param put_path_length length of the put_path (not used)
6462 * @param type type of the result
6463 * @param size number of bytes in data
6464 * @param data pointer to the result data
6467 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6468 const struct GNUNET_HashCode * key,
6469 const struct GNUNET_PeerIdentity *get_path,
6470 unsigned int get_path_length,
6471 const struct GNUNET_PeerIdentity *put_path,
6472 unsigned int put_path_length,
6473 enum GNUNET_BLOCK_Type type,
6474 size_t size, const void *data)
6476 const struct MeshRegexAccept *block = data;
6477 struct MeshRegexSearchContext *ctx = cls;
6478 struct MeshRegexSearchInfo *info = ctx->info;
6479 struct MeshPeerPath *p;
6480 struct MeshPeerInfo *peer_info;
6482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6485 peer_info = peer_info_get(&block->id);
6486 p = path_build_from_dht (get_path, get_path_length, put_path,
6488 path_add_to_peers (p, GNUNET_NO);
6491 tunnel_add_peer (info->t, peer_info);
6492 peer_info_connect (peer_info, info->t);
6493 if (0 == info->peer)
6495 info->peer = peer_info->id;
6499 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6502 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6503 ®ex_connect_timeout,
6511 * Function to process DHT string to regex matching.
6512 * Called on each result obtained for the DHT search.
6514 * @param cls closure (search context)
6515 * @param exp when will this value expire
6516 * @param key key of the result
6517 * @param get_path path of the get request (not used)
6518 * @param get_path_length lenght of get_path (not used)
6519 * @param put_path path of the put request (not used)
6520 * @param put_path_length length of the put_path (not used)
6521 * @param type type of the result
6522 * @param size number of bytes in data
6523 * @param data pointer to the result data
6525 * TODO: re-issue the request after certain time? cancel after X results?
6528 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6529 const struct GNUNET_HashCode * key,
6530 const struct GNUNET_PeerIdentity *get_path,
6531 unsigned int get_path_length,
6532 const struct GNUNET_PeerIdentity *put_path,
6533 unsigned int put_path_length,
6534 enum GNUNET_BLOCK_Type type,
6535 size_t size, const void *data)
6537 const struct MeshRegexBlock *block = data;
6538 struct MeshRegexSearchContext *ctx = cls;
6539 struct MeshRegexSearchInfo *info = ctx->info;
6543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6544 "DHT GET STRING RETURNED RESULTS\n");
6545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6546 " key: %s\n", GNUNET_h2s (key));
6548 copy = GNUNET_malloc (size);
6549 memcpy (copy, data, size);
6550 GNUNET_break (GNUNET_OK ==
6551 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6552 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6553 len = ntohl (block->n_proof);
6555 char proof[len + 1];
6557 memcpy (proof, &block[1], len);
6559 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6561 GNUNET_break_op (0);
6565 len = strlen (info->description);
6566 if (len == ctx->position) // String processed
6568 if (GNUNET_YES == ntohl (block->accepting))
6570 regex_find_path(key, ctx);
6574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6575 // FIXME REGEX this block not successful, wait for more? start timeout?
6580 regex_next_edge (block, size, ctx);
6585 /******************************************************************************/
6586 /********************* MESH LOCAL HANDLES **************************/
6587 /******************************************************************************/
6591 * Handler for client disconnection
6593 * @param cls closure
6594 * @param client identification of the client; NULL
6595 * for the last call when the server is destroyed
6598 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6600 struct MeshClient *c;
6601 struct MeshClient *next;
6604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6611 // return; uncomment for regex_profiler
6615 if (c->handle != client)
6617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6623 GNUNET_SERVER_client_drop (c->handle);
6624 c->shutting_down = GNUNET_YES;
6625 GNUNET_assert (NULL != c->own_tunnels);
6626 GNUNET_assert (NULL != c->incoming_tunnels);
6627 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6628 &tunnel_destroy_iterator, c);
6629 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6630 &tunnel_destroy_iterator, c);
6631 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6632 &tunnel_destroy_iterator, c);
6633 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6634 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6635 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6637 /* deregister clients applications */
6638 if (NULL != c->apps)
6640 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6641 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6643 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6644 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6646 GNUNET_SCHEDULER_cancel (announce_applications_task);
6647 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6649 if (NULL != c->types)
6650 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6651 for (i = 0; i < c->n_regex; i++)
6653 GNUNET_free (c->regexes[i].regex);
6655 GNUNET_free_non_null (c->regexes);
6656 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6657 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6659 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6662 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6671 * Handler for new clients
6673 * @param cls closure
6674 * @param client identification of the client
6675 * @param message the actual message, which includes messages the client wants
6678 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6679 const struct GNUNET_MessageHeader *message)
6681 struct GNUNET_MESH_ClientConnect *cc_msg;
6682 struct MeshClient *c;
6683 GNUNET_MESH_ApplicationType *a;
6690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6691 /* Check data sanity */
6692 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6693 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6694 ntypes = ntohs (cc_msg->types);
6695 napps = ntohs (cc_msg->applications);
6697 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6704 /* Create new client structure */
6705 c = GNUNET_malloc (sizeof (struct MeshClient));
6706 c->id = next_client_id++;
6707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6709 GNUNET_SERVER_client_keep (client);
6710 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6713 GNUNET_MESH_ApplicationType at;
6714 struct GNUNET_HashCode hc;
6716 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6717 for (i = 0; i < napps; i++)
6720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6721 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6722 /* store in clients hashmap */
6723 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6724 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6725 /* store in global hashmap, for announcements */
6726 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6727 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6729 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6730 announce_applications_task =
6731 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6737 struct GNUNET_HashCode hc;
6739 t = (uint16_t *) & a[napps];
6740 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6741 for (i = 0; i < ntypes; i++)
6744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6745 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6747 /* store in clients hashmap */
6748 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6749 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6750 /* store in global hashmap */
6751 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6752 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6756 " client has %u+%u subscriptions\n", napps, ntypes);
6758 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6759 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6760 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6761 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6762 GNUNET_SERVER_notification_context_add (nc, client);
6763 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6765 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6771 * Handler for clients announcing available services by a regular expression.
6773 * @param cls closure
6774 * @param client identification of the client
6775 * @param message the actual message, which includes messages the client wants
6778 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6779 const struct GNUNET_MessageHeader *message)
6781 struct GNUNET_MESH_RegexAnnounce *msg;
6782 struct MeshRegexDescriptor rd;
6783 struct MeshClient *c;
6787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6789 /* Sanity check for client registration */
6790 if (NULL == (c = client_get (client)))
6793 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6798 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6799 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6800 regex = GNUNET_malloc (len + 1);
6801 memcpy (regex, &msg[1], len);
6804 rd.compression = ntohs (msg->compression_characters);
6805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6808 GNUNET_array_append (c->regexes, c->n_regex, rd);
6809 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6811 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6817 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6823 * Handler for requests of new tunnels
6825 * @param cls closure
6826 * @param client identification of the client
6827 * @param message the actual message
6830 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6831 const struct GNUNET_MessageHeader *message)
6833 struct GNUNET_MESH_TunnelMessage *t_msg;
6834 struct MeshTunnel *t;
6835 struct MeshClient *c;
6836 MESH_TunnelNumber tid;
6838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6840 /* Sanity check for client registration */
6841 if (NULL == (c = client_get (client)))
6844 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6849 /* Message sanity check */
6850 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6857 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6858 /* Sanity check for tunnel numbering */
6859 tid = ntohl (t_msg->tunnel_id);
6860 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6866 /* Sanity check for duplicate tunnel IDs */
6867 if (NULL != tunnel_get_by_local_id (c, tid))
6870 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6874 while (NULL != tunnel_get_by_pi (myid, next_tid))
6875 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6876 t = tunnel_new (myid, next_tid++, c, tid);
6879 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6884 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6886 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6887 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6890 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6896 * Handler for requests of deleting tunnels
6898 * @param cls closure
6899 * @param client identification of the client
6900 * @param message the actual message
6903 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6904 const struct GNUNET_MessageHeader *message)
6906 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6907 struct MeshClient *c;
6908 struct MeshTunnel *t;
6909 MESH_TunnelNumber tid;
6911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6912 "Got a DESTROY TUNNEL from client!\n");
6914 /* Sanity check for client registration */
6915 if (NULL == (c = client_get (client)))
6918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6923 /* Message sanity check */
6924 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6931 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6933 /* Retrieve tunnel */
6934 tid = ntohl (tunnel_msg->tunnel_id);
6935 t = tunnel_get_by_local_id(c, tid);
6938 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6940 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6943 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6945 client_ignore_tunnel (c, t);
6947 // TODO: when to destroy incoming tunnel?
6948 if (t->nclients == 0)
6950 GNUNET_assert (GNUNET_YES ==
6951 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6953 GNUNET_assert (GNUNET_YES ==
6954 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6955 &my_full_id.hashPubKey,
6959 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6962 send_client_tunnel_disconnect(t, c);
6963 client_delete_tunnel(c, t);
6965 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6967 tunnel_send_destroy (t);
6968 t->destroy = GNUNET_YES;
6969 // The tunnel will be destroyed when the last message is transmitted.
6970 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6976 * Handler for requests of seeting tunnel's speed.
6978 * @param cls Closure (unused).
6979 * @param client Identification of the client.
6980 * @param message The actual message.
6983 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6984 const struct GNUNET_MessageHeader *message)
6986 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6987 struct MeshClient *c;
6988 struct MeshTunnel *t;
6989 MESH_TunnelNumber tid;
6991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6992 "Got a SPEED request from client!\n");
6994 /* Sanity check for client registration */
6995 if (NULL == (c = client_get (client)))
6998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7004 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7006 /* Retrieve tunnel */
7007 tid = ntohl (tunnel_msg->tunnel_id);
7008 t = tunnel_get_by_local_id(c, tid);
7011 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7013 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7017 switch (ntohs(message->type))
7019 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7020 t->speed_min = GNUNET_YES;
7022 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7023 t->speed_min = GNUNET_NO;
7028 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7033 * Handler for requests of seeting tunnel's buffering policy.
7035 * @param cls Closure (unused).
7036 * @param client Identification of the client.
7037 * @param message The actual message.
7040 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7041 const struct GNUNET_MessageHeader *message)
7043 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7044 struct MeshClient *c;
7045 struct MeshTunnel *t;
7046 MESH_TunnelNumber tid;
7048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7049 "Got a BUFFER request from client!\n");
7051 /* Sanity check for client registration */
7052 if (NULL == (c = client_get (client)))
7055 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7060 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7062 /* Retrieve tunnel */
7063 tid = ntohl (tunnel_msg->tunnel_id);
7064 t = tunnel_get_by_local_id(c, tid);
7067 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7069 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7073 switch (ntohs(message->type))
7075 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7076 t->nobuffer = GNUNET_NO;
7078 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7079 t->nobuffer = GNUNET_YES;
7085 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7090 * Handler for connection requests to new peers
7092 * @param cls closure
7093 * @param client identification of the client
7094 * @param message the actual message (PeerControl)
7097 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7098 const struct GNUNET_MessageHeader *message)
7100 struct GNUNET_MESH_PeerControl *peer_msg;
7101 struct MeshPeerInfo *peer_info;
7102 struct MeshClient *c;
7103 struct MeshTunnel *t;
7104 MESH_TunnelNumber tid;
7106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7107 /* Sanity check for client registration */
7108 if (NULL == (c = client_get (client)))
7111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7116 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7118 /* Sanity check for message size */
7119 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7122 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7126 /* Tunnel exists? */
7127 tid = ntohl (peer_msg->tunnel_id);
7128 t = tunnel_get_by_local_id (c, tid);
7132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7136 /* Does client own tunnel? */
7137 if (t->owner->handle != client)
7140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7144 GNUNET_i2s (&peer_msg->peer));
7145 peer_info = peer_info_get (&peer_msg->peer);
7147 tunnel_add_peer (t, peer_info);
7148 peer_info_connect (peer_info, t);
7150 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7156 * Handler for disconnection requests of peers in a tunnel
7158 * @param cls closure
7159 * @param client identification of the client
7160 * @param message the actual message (PeerControl)
7163 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7164 const struct GNUNET_MessageHeader *message)
7166 struct GNUNET_MESH_PeerControl *peer_msg;
7167 struct MeshPeerInfo *peer_info;
7168 struct MeshClient *c;
7169 struct MeshTunnel *t;
7170 MESH_TunnelNumber tid;
7172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7173 /* Sanity check for client registration */
7174 if (NULL == (c = client_get (client)))
7177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7182 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7184 /* Sanity check for message size */
7185 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7192 /* Tunnel exists? */
7193 tid = ntohl (peer_msg->tunnel_id);
7194 t = tunnel_get_by_local_id (c, tid);
7198 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7203 /* Does client own tunnel? */
7204 if (t->owner->handle != client)
7207 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7212 GNUNET_i2s (&peer_msg->peer));
7213 /* Is the peer in the tunnel? */
7215 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7216 if (NULL == peer_info)
7219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7223 /* Ok, delete peer from tunnel */
7224 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7225 &peer_msg->peer.hashPubKey);
7227 send_destroy_path (t, peer_info->id);
7228 tunnel_delete_peer (t, peer_info->id);
7229 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7234 * Handler for blacklist requests of peers in a tunnel
7236 * @param cls closure
7237 * @param client identification of the client
7238 * @param message the actual message (PeerControl)
7240 * FIXME implement DHT block bloomfilter
7243 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7244 const struct GNUNET_MessageHeader *message)
7246 struct GNUNET_MESH_PeerControl *peer_msg;
7247 struct MeshClient *c;
7248 struct MeshTunnel *t;
7249 MESH_TunnelNumber tid;
7251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7252 /* Sanity check for client registration */
7253 if (NULL == (c = client_get (client)))
7256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7261 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7263 /* Sanity check for message size */
7264 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7267 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7271 /* Tunnel exists? */
7272 tid = ntohl (peer_msg->tunnel_id);
7273 t = tunnel_get_by_local_id (c, tid);
7277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7282 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7283 GNUNET_PEER_intern(&peer_msg->peer));
7288 * Handler for unblacklist requests of peers in a tunnel
7290 * @param cls closure
7291 * @param client identification of the client
7292 * @param message the actual message (PeerControl)
7295 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7296 const struct GNUNET_MessageHeader *message)
7298 struct GNUNET_MESH_PeerControl *peer_msg;
7299 struct MeshClient *c;
7300 struct MeshTunnel *t;
7301 MESH_TunnelNumber tid;
7305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7306 /* Sanity check for client registration */
7307 if (NULL == (c = client_get (client)))
7310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7315 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7317 /* Sanity check for message size */
7318 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7325 /* Tunnel exists? */
7326 tid = ntohl (peer_msg->tunnel_id);
7327 t = tunnel_get_by_local_id (c, tid);
7331 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7336 /* if peer is not known, complain */
7337 pid = GNUNET_PEER_search (&peer_msg->peer);
7344 /* search and remove from list */
7345 for (i = 0; i < t->nblacklisted; i++)
7347 if (t->blacklisted[i] == pid)
7349 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7350 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7355 /* if peer hasn't been blacklisted, complain */
7361 * Handler for connection requests to new peers by type
7363 * @param cls closure
7364 * @param client identification of the client
7365 * @param message the actual message (ConnectPeerByType)
7368 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7369 const struct GNUNET_MessageHeader *message)
7371 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7372 struct MeshClient *c;
7373 struct MeshTunnel *t;
7374 struct GNUNET_HashCode hash;
7375 MESH_TunnelNumber tid;
7377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7378 /* Sanity check for client registration */
7379 if (NULL == (c = client_get (client)))
7382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7387 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7389 /* Sanity check for message size */
7390 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7391 ntohs (connect_msg->header.size))
7394 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7398 /* Tunnel exists? */
7399 tid = ntohl (connect_msg->tunnel_id);
7400 t = tunnel_get_by_local_id (c, tid);
7404 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7408 /* Does client own tunnel? */
7409 if (t->owner->handle != client)
7412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7416 /* Do WE have the service? */
7417 t->type = ntohl (connect_msg->type);
7418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7419 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7420 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7423 /* Yes! Fast forward, add ourselves to the tunnel and send the
7424 * good news to the client, and alert the destination client of
7425 * an incoming tunnel.
7427 * FIXME send a path create to self, avoid code duplication
7429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7430 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7431 peer_info_get (&my_full_id),
7432 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7435 send_client_peer_connected (t, myid);
7436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7437 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7439 t->local_tid_dest = next_local_tid++;
7440 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7441 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7442 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7446 /* Ok, lets find a peer offering the service */
7447 if (NULL != t->dht_get_type)
7449 GNUNET_DHT_get_stop (t->dht_get_type);
7451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7452 GNUNET_h2s (&hash));
7454 GNUNET_DHT_get_start (dht_handle,
7455 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7457 dht_replication_level,
7458 GNUNET_DHT_RO_RECORD_ROUTE |
7459 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7461 &dht_get_type_handler, t);
7463 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7469 * Handler for connection requests to new peers by a string service description.
7471 * @param cls closure
7472 * @param client identification of the client
7473 * @param message the actual message, which includes messages the client wants
7476 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7477 const struct GNUNET_MessageHeader *message)
7479 struct GNUNET_MESH_ConnectPeerByString *msg;
7480 struct MeshRegexSearchContext *ctx;
7481 struct MeshRegexSearchInfo *info;
7482 struct GNUNET_DHT_GetHandle *get_h;
7483 struct GNUNET_HashCode key;
7484 struct MeshTunnel *t;
7485 struct MeshClient *c;
7486 MESH_TunnelNumber tid;
7490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7491 "Connect by string started\n");
7492 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7493 size = htons (message->size);
7495 /* Sanity check for client registration */
7496 if (NULL == (c = client_get (client)))
7499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7504 /* Message size sanity check */
7505 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7512 /* Tunnel exists? */
7513 tid = ntohl (msg->tunnel_id);
7514 t = tunnel_get_by_local_id (c, tid);
7518 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7522 /* Does client own tunnel? */
7523 if (t->owner->handle != client)
7526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7531 " on tunnel %s [%u]\n",
7532 GNUNET_i2s(&my_full_id),
7535 /* Only one connect_by_string allowed at the same time! */
7536 /* FIXME: allow more, return handle at api level to cancel, document */
7537 if (NULL != t->regex_ctx)
7540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7544 /* Find string itself */
7545 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7546 string = (const char *) &msg[1];
7548 /* Initialize context */
7549 size = GNUNET_REGEX_get_first_key(string, len, &key);
7550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7551 " consumed %u bits out of %u\n", size, len);
7552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7553 " looking for %s\n", GNUNET_h2s (&key));
7555 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7557 info->description = GNUNET_malloc (len + 1);
7558 memcpy (info->description, string, len);
7559 info->description[len] = '\0';
7560 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7561 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7564 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7565 ctx->position = size;
7569 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7571 /* Start search in DHT */
7572 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7573 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7574 &key, /* key to search */
7575 dht_replication_level, /* replication level */
7576 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7577 NULL, /* xquery */ // FIXME BLOOMFILTER
7578 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7579 &dht_get_string_handler, ctx);
7581 GNUNET_break (GNUNET_OK ==
7582 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7585 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7587 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7593 * Handler for client traffic directed to one peer
7595 * @param cls closure
7596 * @param client identification of the client
7597 * @param message the actual message
7600 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7601 const struct GNUNET_MessageHeader *message)
7603 struct MeshClient *c;
7604 struct MeshTunnel *t;
7605 struct MeshPeerInfo *pi;
7606 struct GNUNET_MESH_Unicast *data_msg;
7607 MESH_TunnelNumber tid;
7610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7611 "Got a unicast request from a client!\n");
7613 /* Sanity check for client registration */
7614 if (NULL == (c = client_get (client)))
7617 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7622 data_msg = (struct GNUNET_MESH_Unicast *) message;
7624 /* Sanity check for message size */
7625 size = ntohs (message->size);
7626 if (sizeof (struct GNUNET_MESH_Unicast) +
7627 sizeof (struct GNUNET_MessageHeader) > size)
7630 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7634 /* Tunnel exists? */
7635 tid = ntohl (data_msg->tid);
7636 t = tunnel_get_by_local_id (c, tid);
7640 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7644 /* Is it a local tunnel? Then, does client own the tunnel? */
7645 if (t->owner->handle != client)
7648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7652 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7653 &data_msg->destination.hashPubKey);
7654 /* Is the selected peer in the tunnel? */
7658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7662 /* PID should be as expected */
7663 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7667 "Unicast PID, expected %u, got %u\n",
7668 t->fwd_pid + 1, ntohl (data_msg->pid));
7669 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7673 /* Ok, everything is correct, send the message
7674 * (pretend we got it from a mesh peer)
7677 /* Work around const limitation */
7678 char buf[ntohs (message->size)] GNUNET_ALIGN;
7679 struct GNUNET_MESH_Unicast *copy;
7681 copy = (struct GNUNET_MESH_Unicast *) buf;
7682 memcpy (buf, data_msg, size);
7683 copy->oid = my_full_id;
7684 copy->tid = htonl (t->id.tid);
7685 copy->ttl = htonl (default_ttl);
7686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7687 " calling generic handler...\n");
7688 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7691 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7698 * Handler for client traffic directed to the origin
7700 * @param cls closure
7701 * @param client identification of the client
7702 * @param message the actual message
7705 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7706 const struct GNUNET_MessageHeader *message)
7708 struct GNUNET_MESH_ToOrigin *data_msg;
7709 struct MeshTunnelClientInfo *clinfo;
7710 struct MeshClient *c;
7711 struct MeshTunnel *t;
7712 MESH_TunnelNumber tid;
7715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7716 "Got a ToOrigin request from a client!\n");
7717 /* Sanity check for client registration */
7718 if (NULL == (c = client_get (client)))
7721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7726 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7728 /* Sanity check for message size */
7729 size = ntohs (message->size);
7730 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7731 sizeof (struct GNUNET_MessageHeader) > size)
7734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7738 /* Tunnel exists? */
7739 tid = ntohl (data_msg->tid);
7740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7741 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7747 t = tunnel_get_by_local_id (c, tid);
7750 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7751 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7753 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7757 /* It should be sent by someone who has this as incoming tunnel. */
7758 if (GNUNET_NO == client_knows_tunnel (c, t))
7761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7765 /* PID should be as expected */
7766 clinfo = tunnel_get_client_fc (t, c);
7767 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7770 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7771 "To Origin PID, expected %u, got %u\n",
7772 clinfo->bck_pid + 1,
7773 ntohl (data_msg->pid));
7774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7779 /* Ok, everything is correct, send the message
7780 * (pretend we got it from a mesh peer)
7783 char buf[ntohs (message->size)] GNUNET_ALIGN;
7784 struct GNUNET_MESH_ToOrigin *copy;
7786 /* Work around const limitation */
7787 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7788 memcpy (buf, data_msg, size);
7789 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7790 copy->tid = htonl (t->id.tid);
7791 copy->ttl = htonl (default_ttl);
7792 copy->pid = htonl (t->bck_pid + 1);
7794 copy->sender = my_full_id;
7795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7796 " calling generic handler...\n");
7797 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7799 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7806 * Handler for client traffic directed to all peers in a tunnel
7808 * @param cls closure
7809 * @param client identification of the client
7810 * @param message the actual message
7813 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7814 const struct GNUNET_MessageHeader *message)
7816 struct MeshClient *c;
7817 struct MeshTunnel *t;
7818 struct GNUNET_MESH_Multicast *data_msg;
7819 MESH_TunnelNumber tid;
7821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7822 "Got a multicast request from a client!\n");
7824 /* Sanity check for client registration */
7825 if (NULL == (c = client_get (client)))
7828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7833 data_msg = (struct GNUNET_MESH_Multicast *) message;
7835 /* Sanity check for message size */
7836 if (sizeof (struct GNUNET_MESH_Multicast) +
7837 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7844 /* Tunnel exists? */
7845 tid = ntohl (data_msg->tid);
7846 t = tunnel_get_by_local_id (c, tid);
7850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7851 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7857 /* Does client own tunnel? */
7858 if (t->owner->handle != client)
7861 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7865 /* PID should be as expected */
7866 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7870 "Multicast PID, expected %u, got %u\n",
7871 t->fwd_pid + 1, ntohl (data_msg->pid));
7872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7877 char buf[ntohs (message->size)] GNUNET_ALIGN;
7878 struct GNUNET_MESH_Multicast *copy;
7880 copy = (struct GNUNET_MESH_Multicast *) buf;
7881 memcpy (buf, message, ntohs (message->size));
7882 copy->oid = my_full_id;
7883 copy->tid = htonl (t->id.tid);
7884 copy->ttl = htonl (default_ttl);
7885 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7887 " calling generic handler...\n");
7888 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7891 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7897 * Handler for client's ACKs for payload traffic.
7899 * @param cls Closure (unused).
7900 * @param client Identification of the client.
7901 * @param message The actual message.
7904 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7905 const struct GNUNET_MessageHeader *message)
7907 struct GNUNET_MESH_LocalAck *msg;
7908 struct MeshTunnel *t;
7909 struct MeshClient *c;
7910 MESH_TunnelNumber tid;
7913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7914 /* Sanity check for client registration */
7915 if (NULL == (c = client_get (client)))
7918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7923 msg = (struct GNUNET_MESH_LocalAck *) message;
7925 /* Tunnel exists? */
7926 tid = ntohl (msg->tunnel_id);
7927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7928 t = tunnel_get_by_local_id (c, tid);
7932 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7933 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7938 ack = ntohl (msg->max_pid);
7939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7941 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7942 if (NULL != t->owner && t->owner->handle == client)
7944 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7946 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7950 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7951 tunnel_set_client_fwd_ack (t, c, ack);
7952 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7955 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7962 * Functions to handle messages from clients
7964 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7965 {&handle_local_new_client, NULL,
7966 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7967 {&handle_local_announce_regex, NULL,
7968 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7969 {&handle_local_tunnel_create, NULL,
7970 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7971 sizeof (struct GNUNET_MESH_TunnelMessage)},
7972 {&handle_local_tunnel_destroy, NULL,
7973 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7974 sizeof (struct GNUNET_MESH_TunnelMessage)},
7975 {&handle_local_tunnel_speed, NULL,
7976 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7977 sizeof (struct GNUNET_MESH_TunnelMessage)},
7978 {&handle_local_tunnel_speed, NULL,
7979 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7980 sizeof (struct GNUNET_MESH_TunnelMessage)},
7981 {&handle_local_tunnel_buffer, NULL,
7982 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7983 sizeof (struct GNUNET_MESH_TunnelMessage)},
7984 {&handle_local_tunnel_buffer, NULL,
7985 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7986 sizeof (struct GNUNET_MESH_TunnelMessage)},
7987 {&handle_local_connect_add, NULL,
7988 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7989 sizeof (struct GNUNET_MESH_PeerControl)},
7990 {&handle_local_connect_del, NULL,
7991 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7992 sizeof (struct GNUNET_MESH_PeerControl)},
7993 {&handle_local_blacklist, NULL,
7994 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7995 sizeof (struct GNUNET_MESH_PeerControl)},
7996 {&handle_local_unblacklist, NULL,
7997 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7998 sizeof (struct GNUNET_MESH_PeerControl)},
7999 {&handle_local_connect_by_type, NULL,
8000 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8001 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8002 {&handle_local_connect_by_string, NULL,
8003 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8004 {&handle_local_unicast, NULL,
8005 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8006 {&handle_local_to_origin, NULL,
8007 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8008 {&handle_local_multicast, NULL,
8009 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8010 {&handle_local_ack, NULL,
8011 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8012 sizeof (struct GNUNET_MESH_LocalAck)},
8018 * To be called on core init/fail.
8020 * @param cls service closure
8021 * @param server handle to the server for this service
8022 * @param identity the public identity of this peer
8025 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8026 const struct GNUNET_PeerIdentity *identity)
8029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8030 core_handle = server;
8031 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8034 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8035 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8037 GNUNET_abort(); // Try harder
8044 * Method called whenever a given peer connects.
8046 * @param cls closure
8047 * @param peer peer identity this notification is about
8048 * @param atsi performance data for the connection
8049 * @param atsi_count number of records in 'atsi'
8052 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8053 const struct GNUNET_ATS_Information *atsi,
8054 unsigned int atsi_count)
8056 struct MeshPeerInfo *peer_info;
8057 struct MeshPeerPath *path;
8059 DEBUG_CONN ("Peer connected\n");
8060 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8061 peer_info = peer_info_get (peer);
8062 if (myid == peer_info->id)
8064 DEBUG_CONN (" (self)\n");
8069 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8071 path = path_new (2);
8072 path->peers[0] = myid;
8073 path->peers[1] = peer_info->id;
8074 GNUNET_PEER_change_rc (myid, 1);
8075 GNUNET_PEER_change_rc (peer_info->id, 1);
8076 peer_info_add_path (peer_info, path, GNUNET_YES);
8077 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8083 * Method called whenever a peer disconnects.
8085 * @param cls closure
8086 * @param peer peer identity this notification is about
8089 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8091 struct MeshPeerInfo *pi;
8092 struct MeshPeerQueue *q;
8093 struct MeshPeerQueue *n;
8095 DEBUG_CONN ("Peer disconnected\n");
8096 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8106 /* TODO try to reroute this traffic instead */
8107 queue_destroy(q, GNUNET_YES);
8110 if (NULL != pi->core_transmit)
8112 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8113 pi->core_transmit = NULL;
8115 peer_info_remove_path (pi, pi->id, myid);
8118 DEBUG_CONN (" (self)\n");
8120 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8125 /******************************************************************************/
8126 /************************ MAIN FUNCTIONS ****************************/
8127 /******************************************************************************/
8130 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8132 * @param cls closure
8133 * @param key current key code
8134 * @param value value in the hash map
8135 * @return GNUNET_YES if we should continue to iterate,
8139 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8141 struct MeshTunnel *t = value;
8148 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8150 * @param cls closure
8151 * @param key current key code
8152 * @param value value in the hash map
8153 * @return GNUNET_YES if we should continue to iterate,
8157 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8159 struct MeshPeerInfo *p = value;
8160 struct MeshPeerQueue *q;
8161 struct MeshPeerQueue *n;
8169 queue_destroy(q, GNUNET_YES);
8173 peer_info_destroy (p);
8179 * Task run during shutdown.
8185 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8189 if (core_handle != NULL)
8191 GNUNET_CORE_disconnect (core_handle);
8196 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8199 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8200 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8201 if (dht_handle != NULL)
8203 GNUNET_DHT_disconnect (dht_handle);
8208 GNUNET_SERVER_notification_context_destroy (nc);
8211 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8213 GNUNET_SCHEDULER_cancel (announce_id_task);
8214 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8221 * Callback for hostkey read/generation
8224 * @param pk the private key
8225 * @param emsg error message
8228 key_generation_cb (void *cls,
8229 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8232 struct MeshPeerInfo *peer;
8233 struct MeshPeerPath *p;
8238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8239 _("Mesh service could not access hostkey. Exiting.\n"));
8240 GNUNET_SCHEDULER_shutdown ();
8243 my_private_key = pk;
8244 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8245 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8246 &my_full_id.hashPubKey);
8247 myid = GNUNET_PEER_intern (&my_full_id);
8248 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8249 "Mesh for peer [%s] starting\n",
8250 GNUNET_i2s(&my_full_id));
8252 // transport_handle = GNUNET_TRANSPORT_connect(c,
8262 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8265 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8266 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8267 GNUNET_SERVER_disconnect_notify (server_handle,
8268 &handle_local_client_disconnect, NULL);
8272 clients_tail = NULL;
8275 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8276 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8278 /* Create a peer_info for the local peer */
8279 peer = peer_info_get (&my_full_id);
8282 GNUNET_PEER_change_rc (myid, 1);
8283 peer_info_add_path (peer, p, GNUNET_YES);
8284 GNUNET_SERVER_resume (server_handle);
8285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8290 * Process mesh requests.
8292 * @param cls closure
8293 * @param server the initialized server
8294 * @param c configuration to use
8297 run (void *cls, struct GNUNET_SERVER_Handle *server,
8298 const struct GNUNET_CONFIGURATION_Handle *c)
8302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8303 server_handle = server;
8304 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8305 NULL, /* Closure passed to MESH functions */
8306 &core_init, /* Call core_init once connected */
8307 &core_connect, /* Handle connects */
8308 &core_disconnect, /* remove peers on disconnects */
8309 NULL, /* Don't notify about all incoming messages */
8310 GNUNET_NO, /* For header only in notification */
8311 NULL, /* Don't notify about all outbound messages */
8312 GNUNET_NO, /* For header-only out notification */
8313 core_handlers); /* Register these handlers */
8315 if (core_handle == NULL)
8318 GNUNET_SCHEDULER_shutdown ();
8323 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8328 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8330 GNUNET_SCHEDULER_shutdown ();
8335 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8336 &refresh_path_time))
8338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8340 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8341 "mesh", "refresh path time");
8342 GNUNET_SCHEDULER_shutdown ();
8347 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8348 &app_announce_time))
8350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8352 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8353 "mesh", "app announce time");
8354 GNUNET_SCHEDULER_shutdown ();
8357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8360 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8363 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8365 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8366 "mesh", "id announce time");
8367 GNUNET_SCHEDULER_shutdown ();
8375 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8380 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8381 "mesh", "connect timeout");
8382 GNUNET_SCHEDULER_shutdown ();
8387 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8392 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8393 "mesh", "max msgs queue");
8394 GNUNET_SCHEDULER_shutdown ();
8399 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8402 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8404 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8405 "mesh", "max tunnels");
8406 GNUNET_SCHEDULER_shutdown ();
8411 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8414 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8416 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8417 "mesh", "default ttl", 64);
8422 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8423 &dht_replication_level))
8425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8427 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8428 "mesh", "dht replication level", 10);
8429 dht_replication_level = 10;
8432 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8433 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8434 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8435 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8436 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8438 dht_handle = GNUNET_DHT_connect (c, 64);
8439 if (NULL == dht_handle)
8443 stats = GNUNET_STATISTICS_create ("mesh", c);
8445 GNUNET_SERVER_suspend (server_handle);
8446 /* Scheduled the task to clean up when shutdown is called */
8447 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8449 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8450 GNUNET_free (keyfile);
8455 * The main function for the mesh service.
8457 * @param argc number of arguments from the command line
8458 * @param argv command line arguments
8459 * @return 0 ok, 1 on error
8462 main (int argc, char *const *argv)
8467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8468 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8470 ret = (GNUNET_OK == r) ? 0 : 1;
8471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8476 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8477 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);