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);
3586 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3591 mdata->total_out = mdata->reference_counter;
3593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3594 " sending a multicast packet done\n");
3600 * Increase the SKIP value of all peers that
3601 * have not received a unicast message.
3603 * @param cls Closure (ID of the peer that HAS received the message).
3604 * @param key ID of the neighbor.
3605 * @param value Information about the neighbor.
3607 * @return GNUNET_YES to keep iterating.
3610 tunnel_add_skip (void *cls,
3611 const struct GNUNET_HashCode * key,
3614 struct GNUNET_PeerIdentity *neighbor = cls;
3615 struct MeshTunnelChildInfo *cinfo = value;
3617 /* TODO compare only pointers? key == neighbor? */
3618 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3628 * @brief Get neighbor's Flow Control information.
3630 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3631 * descendant of the local node in a certain tunnel.
3632 * If the info is not yet there (recently created path), creates the data struct
3633 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3636 * @param t Tunnel related.
3637 * @param peer Neighbor whose Flow Control info is needed.
3639 * @return Neighbor's Flow Control info.
3641 static struct MeshTunnelChildInfo *
3642 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3643 const struct GNUNET_PeerIdentity *peer)
3645 struct MeshTunnelChildInfo *cinfo;
3647 if (NULL == t->children_fc)
3650 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3656 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3657 cinfo->id = GNUNET_PEER_intern (peer);
3658 cinfo->skip = t->fwd_pid;
3661 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3662 cinfo->fwd_ack = t->fwd_pid + delta;
3663 cinfo->bck_ack = delta;
3664 cinfo->bck_pid = -1;
3666 cinfo->send_buffer =
3667 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3669 GNUNET_assert (GNUNET_OK ==
3670 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3673 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3680 * Get the Flow Control info of a client.
3682 * @param t Tunnel on which to look.
3683 * @param c Client whose ACK to get.
3685 * @return ACK value.
3687 static struct MeshTunnelClientInfo *
3688 tunnel_get_client_fc (struct MeshTunnel *t,
3689 struct MeshClient *c)
3693 for (i = 0; i < t->nclients; i++)
3695 if (t->clients[i] != c)
3697 return &t->clients_fc[i];
3700 return NULL; // avoid compiler / coverity complaints
3705 * Iterator to get the appropiate ACK value from all children nodes.
3707 * @param cls Closue (tunnel).
3708 * @param id Id of the child node.
3711 tunnel_get_child_fwd_ack (void *cls,
3714 struct GNUNET_PeerIdentity peer_id;
3715 struct MeshTunnelChildInfo *cinfo;
3716 struct MeshTunnelChildIteratorContext *ctx = cls;
3717 struct MeshTunnel *t = ctx->t;
3720 GNUNET_PEER_resolve (id, &peer_id);
3721 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3722 ack = cinfo->fwd_ack;
3725 if (GNUNET_NO == ctx->init)
3727 ctx->max_child_ack = ack;
3728 ctx->init = GNUNET_YES;
3731 if (GNUNET_YES == t->speed_min)
3733 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3737 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3744 * Get the maximum PID allowed to transmit to any
3745 * tunnel child of the local peer, depending on the tunnel
3746 * buffering/speed settings.
3750 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3753 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3755 struct MeshTunnelChildIteratorContext ctx;
3757 ctx.max_child_ack = 0;
3759 ctx.init = GNUNET_NO;
3760 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3762 if (0 == ctx.nchildren)
3764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3765 " tunnel has no children, no FWD ACK\n");
3769 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3770 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3772 return (int64_t) ctx.max_child_ack;
3777 * Set the FWD ACK value of a client in a particular tunnel.
3779 * @param t Tunnel affected.
3780 * @param c Client whose ACK to set.
3781 * @param ack ACK value.
3784 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3785 struct MeshClient *c,
3790 for (i = 0; i < t->nclients; i++)
3792 if (t->clients[i] != c)
3794 t->clients_fc[i].fwd_ack = ack;
3802 * Get the highest ACK value of all clients in a particular tunnel,
3803 * according to the buffering/speed settings.
3805 * @param t Tunnel on which to look.
3807 * @return Corresponding ACK value (max uint32_t).
3808 * If no clients are suscribed, -1LL.
3811 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3816 if (0 == t->nclients)
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3819 " tunnel has no clients, no FWD ACK\n");
3823 for (ack = -1LL, i = 0; i < t->nclients; i++)
3826 (GNUNET_YES == t->speed_min &&
3827 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3828 (GNUNET_NO == t->speed_min &&
3829 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3831 ack = t->clients_fc[i].fwd_ack;
3835 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3836 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3838 return (uint32_t) ack;
3843 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3844 * mode and the status of all children nodes.
3848 * @return Maximum PID allowed.
3851 tunnel_get_fwd_ack (struct MeshTunnel *t)
3855 uint32_t buffer_free;
3859 count = t->fwd_pid - t->skip;
3860 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3861 child_ack = tunnel_get_children_fwd_ack (t);
3862 client_ack = tunnel_get_clients_fwd_ack (t);
3863 if (GNUNET_YES == t->nobuffer)
3866 if (-1LL == child_ack)
3867 child_ack = client_ack;
3868 if (-1LL == child_ack)
3871 client_ack = child_ack = ack;
3876 ack = count + buffer_free; // Overflow? OK!
3878 if (-1LL == child_ack)
3880 // Node has no children, child_ack AND core buffer are irrelevant.
3881 if (-1LL == client_ack) // No children AND no clients? Not good!
3883 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3887 return (uint32_t) client_ack;
3889 if (-1LL == client_ack)
3893 if (GNUNET_YES == t->speed_min)
3895 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3896 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3900 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3901 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3904 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3905 count, buffer_free, child_ack, client_ack, ack);
3911 * Build a local ACK message and send it to a local client.
3913 * @param t Tunnel on which to send the ACK.
3914 * @param c Client to whom send the ACK.
3915 * @param ack Value of the ACK.
3918 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3920 struct GNUNET_MESH_LocalAck msg;
3922 msg.header.size = htons (sizeof (msg));
3923 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3924 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3925 msg.max_pid = htonl (ack);
3926 GNUNET_SERVER_notification_context_unicast(nc,
3933 * Build an ACK message and queue it to send to the given peer.
3935 * @param t Tunnel on which to send the ACK.
3936 * @param peer Peer to whom send the ACK.
3937 * @param ack Value of the ACK.
3940 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3942 struct GNUNET_MESH_ACK msg;
3944 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3945 msg.header.size = htons (sizeof (msg));
3946 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3947 msg.pid = htonl (ack);
3948 msg.tid = htonl (t->id.tid);
3950 send_prebuilt_message (&msg.header, peer, t);
3955 * Notify a the owner of a tunnel about how many more
3956 * payload packages will we accept on a given tunnel.
3958 * @param t Tunnel on which to send the ACK.
3961 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3966 "Sending client FWD ACK on tunnel %X\n",
3969 ack = tunnel_get_fwd_ack (t);
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3972 if (t->last_fwd_ack == ack)
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3979 t->last_fwd_ack = ack;
3980 send_local_ack (t, t->owner, ack);
3985 * Send an ACK informing the predecessor about the available buffer space.
3986 * In case there is no predecessor, inform the owning client.
3987 * If buffering is off, send only on behalf of children or self if endpoint.
3988 * If buffering is on, send when sent to children and buffer space is free.
3989 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3990 * the ACK itself goes "back" (towards root).
3992 * @param t Tunnel on which to send the ACK.
3993 * @param type Type of message that triggered the ACK transmission.
3996 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3998 struct GNUNET_PeerIdentity id;
4001 if (NULL != t->owner)
4003 tunnel_send_client_fwd_ack (t);
4006 /* Is it after unicast / multicast retransmission? */
4009 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4010 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4012 "ACK due to FWD DATA retransmission\n");
4013 if (GNUNET_YES == t->nobuffer)
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4019 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4020 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4022 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4023 t->force_ack = GNUNET_YES;
4029 /* Check if we need to transmit the ACK */
4030 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4031 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4032 GNUNET_NO == t->force_ack)
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4036 " t->qmax: %u, t->qn: %u\n",
4037 t->fwd_queue_max, t->fwd_queue_n);
4038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4039 " t->pid: %u, t->ack: %u\n",
4040 t->fwd_pid, t->last_fwd_ack);
4044 /* Ok, ACK might be necessary, what PID to ACK? */
4045 ack = tunnel_get_fwd_ack (t);
4047 /* If speed_min and not all children have ack'd, dont send yet */
4048 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4054 t->last_fwd_ack = ack;
4055 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4056 send_ack (t, &id, ack);
4058 t->force_ack = GNUNET_NO;
4063 * Iterator to send a child node a BCK ACK to allow him to send more
4066 * @param cls Closure (tunnel).
4067 * @param id Id of the child node.
4070 tunnel_send_child_bck_ack (void *cls,
4073 struct MeshTunnel *t = cls;
4074 struct MeshTunnelChildInfo *cinfo;
4075 struct GNUNET_PeerIdentity peer;
4078 GNUNET_PEER_resolve (id, &peer);
4079 cinfo = tunnel_get_neighbor_fc (t, &peer);
4080 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4082 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4085 " Not sending ACK, not needed\n");
4088 cinfo->bck_ack = ack;
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4091 " Sending BCK ACK %u (last sent: %u)\n",
4092 ack, cinfo->bck_ack);
4093 send_ack (t, &peer, ack);
4098 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4100 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4102 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4103 * - implement a size 1 buffer in each client_fc AND children_fc
4104 * to hold at least 1 message per "child".
4105 * problem: violates no buffer policy
4106 * - ack 0 and make "children" poll for transmission slots
4107 * problem: big overhead, extra latency even in low traffic
4110 * @param t Tunnel on which to send the BCK ACKs.
4113 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4116 unsigned int tunnel_delta;
4118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4120 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4123 /* Find client whom to allow to send to origin (with lowest buffer space) */
4124 for (i = 0; i < t->nclients; i++)
4126 struct MeshTunnelClientInfo *clinfo;
4129 clinfo = &t->clients_fc[i];
4130 delta = clinfo->bck_ack - clinfo->bck_pid;
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4132 t->clients[i]->id, delta);
4134 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4135 (GNUNET_YES == t->nobuffer && 0 == delta))
4139 ack = clinfo->bck_pid;
4140 ack += t->nobuffer ? 1 : tunnel_delta;
4141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4142 " sending ack to client %u: %u\n",
4143 t->clients[i]->id, ack);
4144 send_local_ack (t, t->clients[i], ack);
4145 clinfo->bck_ack = ack;
4149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4150 " not sending ack to client %u (td %u, d %u)\n",
4151 t->clients[i]->id, tunnel_delta, delta);
4158 * Send an ACK informing the children nodes and destination clients about
4159 * the available buffer space.
4160 * If buffering is off, send only on behalf of root (can be self).
4161 * If buffering is on, send when sent to predecessor and buffer space is free.
4162 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4163 * the ACK itself goes "forward" (towards children/clients).
4165 * @param t Tunnel on which to send the ACK.
4166 * @param type Type of message that triggered the ACK transmission.
4169 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4172 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4173 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4174 /* Is it after data to_origin retransmission? */
4177 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4178 if (GNUNET_YES == t->nobuffer)
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4181 " Not sending ACK, nobuffer\n");
4185 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4186 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4188 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4189 t->force_ack = GNUNET_YES;
4195 tunnel_send_clients_bck_ack (t);
4196 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4197 t->force_ack = GNUNET_NO;
4202 * @brief Re-initiate traffic to this peer if necessary.
4204 * Check if there is traffic queued towards this peer
4205 * and the core transmit handle is NULL (traffic was stalled).
4206 * If so, call core tmt rdy.
4208 * @param cls Closure (unused)
4209 * @param peer_id Short ID of peer to which initiate traffic.
4212 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4214 struct MeshPeerInfo *peer;
4215 struct GNUNET_PeerIdentity id;
4216 struct MeshPeerQueue *q;
4219 peer = peer_info_get_short(peer_id);
4220 if (NULL != peer->core_transmit)
4223 q = queue_get_next(peer);
4226 /* Might br multicast traffic already sent to this particular peer but
4227 * not to other children in this tunnel.
4228 * This way t->queue_n would be > 0 but the queue of this particular peer
4234 GNUNET_PEER_resolve (peer->id, &id);
4235 peer->core_transmit =
4236 GNUNET_CORE_notify_transmit_ready(core_handle,
4239 GNUNET_TIME_UNIT_FOREVER_REL,
4249 * @brief Allow transmission of FWD traffic on this tunnel
4251 * Check if there is traffic queued towards any children
4252 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4254 * @param t Tunnel on which to unlock FWD traffic.
4257 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4259 if (0 == t->fwd_queue_n)
4262 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4267 * @brief Allow transmission of BCK traffic on this tunnel
4269 * Check if there is traffic queued towards the root of the tree
4270 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4272 * @param t Tunnel on which to unlock BCK traffic.
4275 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4277 if (0 == t->bck_queue_n)
4280 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4285 * Send a message to all peers in this tunnel that the tunnel is no longer
4288 * @param t The tunnel whose peers to notify.
4291 tunnel_send_destroy (struct MeshTunnel *t)
4293 struct GNUNET_MESH_TunnelDestroy msg;
4295 msg.header.size = htons (sizeof (msg));
4296 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4297 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4298 msg.tid = htonl (t->id.tid);
4299 tunnel_send_multicast (t, &msg.header);
4304 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4306 * @param cls Closure (Tunnel which to cancel).
4307 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4310 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4312 struct MeshTunnel *t = cls;
4313 struct MeshPeerInfo *peer_info;
4314 struct MeshPeerQueue *pq;
4315 struct MeshPeerQueue *next;
4317 peer_info = peer_info_get_short (neighbor_id);
4318 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4321 if (pq->tunnel == t)
4323 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4324 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4325 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4327 // Should have been removed on destroy children
4330 queue_destroy (pq, GNUNET_YES);
4333 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4335 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4336 peer_info->core_transmit = NULL;
4341 * Destroy the tunnel and free any allocated resources linked to it.
4343 * @param t the tunnel to destroy
4345 * @return GNUNET_OK on success
4348 tunnel_destroy (struct MeshTunnel *t)
4350 struct MeshClient *c;
4351 struct GNUNET_HashCode hash;
4362 struct GNUNET_PeerIdentity id;
4364 GNUNET_PEER_resolve (t->id.oid, &id);
4365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4366 GNUNET_i2s (&id), t->id.tid);
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4372 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4373 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4381 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4383 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4390 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4391 for (i = 0; i < t->nclients; i++)
4395 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4401 for (i = 0; i < t->nignore; i++)
4405 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4412 if (t->nclients > 0)
4415 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4420 GNUNET_free (t->clients);
4421 GNUNET_free (t->clients_fc);
4424 if (NULL != t->peers)
4426 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4428 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4431 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4432 &tunnel_destroy_child,
4434 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4435 t->children_fc = NULL;
4437 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4438 tree_destroy (t->tree);
4440 if (NULL != t->regex_ctx)
4441 regex_cancel_search (t->regex_ctx);
4442 if (NULL != t->dht_get_type)
4443 GNUNET_DHT_get_stop (t->dht_get_type);
4444 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4445 GNUNET_SCHEDULER_cancel (t->timeout_task);
4446 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4447 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4450 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4457 * Create a new tunnel
4459 * @param owner Who is the owner of the tunnel (short ID).
4460 * @param tid Tunnel Number of the tunnel.
4461 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4462 * @param local Tunnel Number for the tunnel, for the client point of view.
4464 * @return A new initialized tunnel. NULL on error.
4466 static struct MeshTunnel *
4467 tunnel_new (GNUNET_PEER_Id owner,
4468 MESH_TunnelNumber tid,
4469 struct MeshClient *client,
4470 MESH_TunnelNumber local)
4472 struct MeshTunnel *t;
4473 struct GNUNET_HashCode hash;
4475 if (n_tunnels >= max_tunnels && NULL == client)
4478 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4481 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4482 t->bck_queue_max = t->fwd_queue_max;
4483 t->tree = tree_new (owner);
4485 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4486 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4487 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4488 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4489 t->local_tid = local;
4490 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4492 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4494 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4496 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4504 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4511 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4513 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4514 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4518 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4528 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4529 * that are no longer needed, as well as nodes of no longer reachable peers.
4530 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4532 * @param t Tunnel from which to remove the path.
4533 * @param peer Short id of the peer which should be removed.
4536 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4540 r = tree_del_peer (t->tree, peer, NULL, NULL);
4543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4544 "Tunnel %u [%u] has no more nodes\n",
4545 t->id.oid, t->id.tid);
4551 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4552 * client when the client disconnects. If the client is not the owner, the
4553 * owner will get notified if no more clients are in the tunnel and the client
4554 * get removed from the tunnel's list.
4556 * @param cls closure (client that is disconnecting)
4557 * @param key the hash of the local tunnel id (used to access the hashmap)
4558 * @param value the value stored at the key (tunnel to destroy)
4560 * @return GNUNET_OK, keep iterating.
4563 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4565 struct MeshTunnel *t = value;
4566 struct MeshClient *c = cls;
4568 send_client_tunnel_disconnect(t, c);
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4572 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4573 tunnel_delete_client(t, c);
4574 client_delete_tunnel(c, t);
4577 tunnel_send_destroy(t);
4579 t->destroy = GNUNET_YES;
4586 * Timeout function, destroys tunnel if called
4588 * @param cls Closure (tunnel to destroy).
4589 * @param tc TaskContext
4592 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4594 struct MeshTunnel *t = cls;
4595 struct GNUNET_PeerIdentity id;
4597 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4598 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4600 GNUNET_PEER_resolve(t->id.oid, &id);
4601 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4602 "Tunnel %s [%X] timed out. Destroying.\n",
4603 GNUNET_i2s(&id), t->id.tid);
4604 send_clients_tunnel_destroy (t);
4609 * Resets the tunnel timeout. Starts it if no timeout was running.
4611 * @param t Tunnel whose timeout to reset.
4613 * TODO use heap to improve efficiency of scheduler.
4616 tunnel_reset_timeout (struct MeshTunnel *t)
4618 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4619 GNUNET_SCHEDULER_cancel (t->timeout_task);
4621 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4622 (refresh_path_time, 4), &tunnel_timeout, t);
4626 /******************************************************************************/
4627 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4628 /******************************************************************************/
4631 * Function to send a create path packet to a peer.
4633 * @param cls closure
4634 * @param size number of bytes available in buf
4635 * @param buf where the callee should write the message
4636 * @return number of bytes written to buf
4639 send_core_path_create (void *cls, size_t size, void *buf)
4641 struct MeshPathInfo *info = cls;
4642 struct GNUNET_MESH_ManipulatePath *msg;
4643 struct GNUNET_PeerIdentity *peer_ptr;
4644 struct MeshTunnel *t = info->t;
4645 struct MeshPeerPath *p = info->path;
4650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4652 sizeof (struct GNUNET_MESH_ManipulatePath) +
4653 p->length * sizeof (struct GNUNET_PeerIdentity);
4655 if (size < size_needed || NULL == buf)
4660 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4661 msg->header.size = htons (size_needed);
4662 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4663 msg->tid = ntohl (t->id.tid);
4666 if (GNUNET_YES == t->speed_min)
4667 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4668 if (GNUNET_YES == t->nobuffer)
4669 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4670 msg->opt = htonl(opt);
4673 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4674 for (i = 0; i < p->length; i++)
4676 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4683 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4689 * Fill the core buffer
4691 * @param cls closure (data itself)
4692 * @param size number of bytes available in buf
4693 * @param buf where the callee should write the message
4695 * @return number of bytes written to buf
4698 send_core_data_multicast (void *cls, size_t size, void *buf)
4700 struct MeshTransmissionDescriptor *info = cls;
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4704 GNUNET_assert (NULL != info);
4705 GNUNET_assert (NULL != info->peer);
4706 total_size = info->mesh_data->data_len;
4707 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4709 if (total_size > size)
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4715 memcpy (buf, info->mesh_data->data, total_size);
4718 struct GNUNET_MESH_Multicast *mc;
4719 struct GNUNET_MessageHeader *mh;
4722 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4724 mc = (struct GNUNET_MESH_Multicast *) mh;
4725 mh = (struct GNUNET_MessageHeader *) &mc[1];
4726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4727 " multicast, payload type %s\n",
4728 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4730 " multicast, payload size %u\n", ntohs (mh->size));
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4735 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4739 data_descriptor_decrement_rc (info->mesh_data);
4740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4748 * Creates a path ack message in buf and frees all unused resources.
4750 * @param cls closure (MeshTransmissionDescriptor)
4751 * @param size number of bytes available in buf
4752 * @param buf where the callee should write the message
4753 * @return number of bytes written to buf
4756 send_core_path_ack (void *cls, size_t size, void *buf)
4758 struct MeshTransmissionDescriptor *info = cls;
4759 struct GNUNET_MESH_PathACK *msg = buf;
4761 GNUNET_assert (NULL != info);
4762 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4767 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4768 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4769 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4770 msg->tid = htonl (info->origin->tid);
4771 msg->peer_id = my_full_id;
4774 /* TODO add signature */
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4777 return sizeof (struct GNUNET_MESH_PathACK);
4782 * Free a transmission that was already queued with all resources
4783 * associated to the request.
4785 * @param queue Queue handler to cancel.
4786 * @param clear_cls Is it necessary to free associated cls?
4789 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4791 struct MeshTransmissionDescriptor *dd;
4792 struct MeshPathInfo *path_info;
4793 struct MeshTunnelChildInfo *cinfo;
4794 struct GNUNET_PeerIdentity id;
4798 if (GNUNET_YES == clear_cls)
4800 switch (queue->type)
4802 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4803 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4804 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4806 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4807 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4808 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4809 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4810 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4811 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4813 " prebuilt message\n");
4814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4816 GNUNET_MESH_DEBUG_M2S(queue->type));
4818 data_descriptor_decrement_rc (dd->mesh_data);
4820 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4822 path_info = queue->cls;
4823 path_destroy (path_info->path);
4827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4828 " type %s unknown!\n",
4829 GNUNET_MESH_DEBUG_M2S(queue->type));
4831 GNUNET_free_non_null (queue->cls);
4833 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4834 queue->peer->queue_tail,
4837 /* Delete from child_fc in the appropiate tunnel */
4838 max = queue->tunnel->fwd_queue_max;
4839 GNUNET_PEER_resolve (queue->peer->id, &id);
4840 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4843 for (i = 0; i < cinfo->send_buffer_n; i++)
4846 i2 = (cinfo->send_buffer_start + i) % max;
4847 if (cinfo->send_buffer[i2] == queue)
4849 /* Found corresponding entry in the send_buffer. Move all others back. */
4854 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4856 j2 = (cinfo->send_buffer_start + j) % max;
4857 j3 = (cinfo->send_buffer_start + j + 1) % max;
4858 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4861 cinfo->send_buffer[j3] = NULL;
4862 cinfo->send_buffer_n--;
4867 GNUNET_free (queue);
4872 * @brief Get the next transmittable message from the queue.
4874 * This will be the head, except in the case of being a data packet
4875 * not allowed by the destination peer.
4877 * @param peer Destination peer.
4879 * @return The next viable MeshPeerQueue element to send to that peer.
4880 * NULL when there are no transmittable messages.
4882 struct MeshPeerQueue *
4883 queue_get_next (const struct MeshPeerInfo *peer)
4885 struct MeshPeerQueue *q;
4886 struct MeshTunnel *t;
4887 struct MeshTransmissionDescriptor *info;
4888 struct MeshTunnelChildInfo *cinfo;
4889 struct GNUNET_MESH_Unicast *ucast;
4890 struct GNUNET_MESH_ToOrigin *to_orig;
4891 struct GNUNET_MESH_Multicast *mcast;
4892 struct GNUNET_PeerIdentity id;
4896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4897 for (q = peer->queue_head; NULL != q; q = q->next)
4901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4903 GNUNET_MESH_DEBUG_M2S(q->type));
4906 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4907 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4908 pid = ntohl (ucast->pid);
4909 GNUNET_PEER_resolve (info->peer->id, &id);
4910 cinfo = tunnel_get_neighbor_fc(t, &id);
4911 ack = cinfo->fwd_ack;
4913 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4914 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4915 pid = ntohl (to_orig->pid);
4918 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4919 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4920 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4922 // Not a multicast payload: multicast control traffic (destroy, etc)
4925 pid = ntohl (mcast->pid);
4926 GNUNET_PEER_resolve (info->peer->id, &id);
4927 cinfo = tunnel_get_neighbor_fc(t, &id);
4928 ack = cinfo->fwd_ack;
4931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4936 "********* ACK: %u, PID: %u\n",
4938 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4947 "********* NEXT!\n");
4950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4951 "********* nothing found\n");
4957 * Core callback to write a queued packet to core buffer
4959 * @param cls Closure (peer info).
4960 * @param size Number of bytes available in buf.
4961 * @param buf Where the to write the message.
4963 * @return number of bytes written to buf
4966 queue_send (void *cls, size_t size, void *buf)
4968 struct MeshPeerInfo *peer = cls;
4969 struct GNUNET_MessageHeader *msg;
4970 struct MeshPeerQueue *queue;
4971 struct MeshTunnel *t;
4972 struct MeshTunnelChildInfo *cinfo;
4973 struct GNUNET_PeerIdentity dst_id;
4976 peer->core_transmit = NULL;
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4980 queue = queue_get_next (peer);
4982 /* Queue has no internal mesh traffic nor sendable payload */
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4986 if (NULL == peer->queue_head)
4987 GNUNET_break (0); // Should've been canceled
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4992 GNUNET_PEER_resolve (peer->id, &dst_id);
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4994 "********* towards %s\n",
4995 GNUNET_i2s(&dst_id));
4996 /* Check if buffer size is enough for the message */
4997 if (queue->size > size)
4999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5000 "********* not enough room, reissue\n");
5001 peer->core_transmit =
5002 GNUNET_CORE_notify_transmit_ready (core_handle,
5005 GNUNET_TIME_UNIT_FOREVER_REL,
5012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5015 GNUNET_assert (0 < t->pending_messages);
5016 t->pending_messages--;
5017 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5021 "********* unicast: t->q (%u/%u)\n",
5022 t->fwd_queue_n, t->fwd_queue_max);
5024 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5031 switch (queue->type)
5034 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5035 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5036 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5037 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5038 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5040 "********* raw: %s\n",
5041 GNUNET_MESH_DEBUG_M2S (queue->type));
5043 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5044 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5045 data_size = send_core_data_raw (queue->cls, size, buf);
5046 msg = (struct GNUNET_MessageHeader *) buf;
5047 switch (ntohs (msg->type)) // Type of preconstructed message
5049 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5050 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5052 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5053 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5059 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5062 struct MeshTransmissionDescriptor *info = queue->cls;
5064 if ((1 == info->mesh_data->reference_counter
5065 && GNUNET_YES == t->speed_min)
5067 (info->mesh_data->total_out == info->mesh_data->reference_counter
5068 && GNUNET_NO == t->speed_min))
5070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5071 "********* considered sent\n");
5076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5077 "********* NOT considered sent yet\n");
5078 t->pending_messages++;
5081 data_size = send_core_data_multicast(queue->cls, size, buf);
5082 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5084 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5086 data_size = send_core_path_create (queue->cls, size, buf);
5088 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5090 data_size = send_core_path_ack (queue->cls, size, buf);
5092 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5094 data_size = send_core_data_multicast (queue->cls, size, buf);
5098 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5099 "********* type unknown: %u\n",
5103 switch (queue->type)
5105 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5106 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5107 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5108 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5109 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5113 "at pos %u (%p) != %p\n",
5114 cinfo->send_buffer_start,
5115 cinfo->send_buffer[cinfo->send_buffer_start],
5118 if (cinfo->send_buffer_n > 0)
5120 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5121 cinfo->send_buffer_n--;
5122 cinfo->send_buffer_start++;
5123 cinfo->send_buffer_start %= t->fwd_queue_max;
5134 /* Free queue, but cls was freed by send_core_* */
5135 queue_destroy (queue, GNUNET_NO);
5137 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5143 /* If more data in queue, send next */
5144 queue = queue_get_next(peer);
5147 struct GNUNET_PeerIdentity id;
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5150 GNUNET_PEER_resolve (peer->id, &id);
5151 peer->core_transmit =
5152 GNUNET_CORE_notify_transmit_ready(core_handle,
5155 GNUNET_TIME_UNIT_FOREVER_REL,
5163 if (NULL != peer->queue_head)
5165 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5166 "********* %s stalled\n",
5167 GNUNET_i2s(&my_full_id));
5169 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5170 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(MESH_POLL_TIME,
5171 &tunnel_poll, cinfo);
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5180 * @brief Queue and pass message to core when possible.
5182 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5183 * and accounts for it. In case the queue is full, the message is dropped and
5186 * Otherwise, message is treated as internal and allowed to go regardless of
5189 * @param cls Closure (@c type dependant). It will be used by queue_send to
5190 * build the message to be sent if not already prebuilt.
5191 * @param type Type of the message, 0 for a raw message.
5192 * @param size Size of the message.
5193 * @param dst Neighbor to send message to.
5194 * @param t Tunnel this message belongs to.
5197 queue_add (void *cls, uint16_t type, size_t size,
5198 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5200 struct MeshPeerQueue *queue;
5201 struct MeshTunnelChildInfo *cinfo;
5202 struct GNUNET_PeerIdentity id;
5208 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5209 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5211 n = &t->fwd_queue_n;
5212 max = &t->fwd_queue_max;
5214 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5216 n = &t->bck_queue_n;
5217 max = &t->bck_queue_max;
5224 GNUNET_STATISTICS_update(stats,
5225 "# messages dropped (buffer full)",
5227 return; // Drop message
5231 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5237 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5238 GNUNET_PEER_resolve (dst->id, &id);
5239 if (NULL == dst->core_transmit)
5241 dst->core_transmit =
5242 GNUNET_CORE_notify_transmit_ready (core_handle,
5245 GNUNET_TIME_UNIT_FOREVER_REL,
5251 t->pending_messages++;
5252 if (NULL == n) // Is this internal mesh traffic?
5255 // It's payload, keep track of buffer per peer.
5256 cinfo = tunnel_get_neighbor_fc(t, &id);
5257 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5258 if (NULL != cinfo->send_buffer[i])
5260 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5261 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5262 cinfo->send_buffer_start++;
5263 cinfo->send_buffer_start %= t->fwd_queue_max;
5267 cinfo->send_buffer_n++;
5269 cinfo->send_buffer[i] = queue;
5270 if (cinfo->send_buffer_n > t->fwd_queue_max)
5273 cinfo->send_buffer_n = t->fwd_queue_max;
5278 /******************************************************************************/
5279 /******************** MESH NETWORK HANDLERS **************************/
5280 /******************************************************************************/
5284 * Core handler for path creation
5286 * @param cls closure
5287 * @param message message
5288 * @param peer peer identity this notification is about
5289 * @param atsi performance data
5290 * @param atsi_count number of records in 'atsi'
5292 * @return GNUNET_OK to keep the connection open,
5293 * GNUNET_SYSERR to close it (signal serious error)
5296 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5297 const struct GNUNET_MessageHeader *message,
5298 const struct GNUNET_ATS_Information *atsi,
5299 unsigned int atsi_count)
5301 unsigned int own_pos;
5304 MESH_TunnelNumber tid;
5305 struct GNUNET_MESH_ManipulatePath *msg;
5306 struct GNUNET_PeerIdentity *pi;
5307 struct GNUNET_HashCode hash;
5308 struct MeshPeerPath *path;
5309 struct MeshPeerInfo *dest_peer_info;
5310 struct MeshPeerInfo *orig_peer_info;
5311 struct MeshTunnel *t;
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5314 "Received a path create msg [%s]\n",
5315 GNUNET_i2s (&my_full_id));
5316 size = ntohs (message->size);
5317 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5319 GNUNET_break_op (0);
5323 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5324 if (size % sizeof (struct GNUNET_PeerIdentity))
5326 GNUNET_break_op (0);
5329 size /= sizeof (struct GNUNET_PeerIdentity);
5332 GNUNET_break_op (0);
5335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5336 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5338 tid = ntohl (msg->tid);
5339 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5341 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5342 t = tunnel_get (pi, tid);
5343 if (NULL == t) // FIXME only for INCOMING tunnels?
5347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5348 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5351 // FIXME notify failure
5354 opt = ntohl (msg->opt);
5355 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5356 GNUNET_YES : GNUNET_NO;
5357 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5359 t->nobuffer = GNUNET_YES;
5360 t->last_fwd_ack = t->fwd_pid + 1;
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5363 " speed_min: %d, nobuffer:%d\n",
5364 t->speed_min, t->nobuffer);
5366 if (GNUNET_YES == t->nobuffer)
5368 t->bck_queue_max = 1;
5369 t->fwd_queue_max = 1;
5372 // FIXME only assign a local tid if a local client is interested (on demand)
5373 while (NULL != tunnel_get_incoming (next_local_tid))
5374 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5375 t->local_tid_dest = next_local_tid++;
5376 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5379 tunnel_reset_timeout (t);
5380 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5382 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5391 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5392 if (NULL == dest_peer_info)
5394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5395 " Creating PeerInfo for destination.\n");
5396 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5397 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5398 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5400 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5402 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5403 if (NULL == orig_peer_info)
5405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5406 " Creating PeerInfo for origin.\n");
5407 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5408 orig_peer_info->id = GNUNET_PEER_intern (pi);
5409 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5410 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5413 path = path_new (size);
5415 for (i = 0; i < size; i++)
5417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5418 GNUNET_i2s (&pi[i]));
5419 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5420 if (path->peers[i] == myid)
5423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5426 /* cannot be self, must be 'not found' */
5427 /* create path: self not found in path through self */
5428 GNUNET_break_op (0);
5429 path_destroy (path);
5433 path_add_to_peers (path, GNUNET_NO);
5434 tunnel_add_path (t, path, own_pos);
5435 if (own_pos == size - 1)
5437 /* It is for us! Send ack. */
5438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5439 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5440 if (NULL == t->peers)
5442 /* New tunnel! Notify clients on first payload message. */
5443 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5445 GNUNET_break (GNUNET_SYSERR !=
5446 GNUNET_CONTAINER_multihashmap_put (t->peers,
5447 &my_full_id.hashPubKey,
5450 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5455 struct MeshPeerPath *path2;
5457 /* It's for somebody else! Retransmit. */
5458 path2 = path_duplicate (path);
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5460 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5461 path2 = path_duplicate (path);
5462 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5463 send_create_path (dest_peer_info, path, t);
5470 * Core handler for path destruction
5472 * @param cls closure
5473 * @param message message
5474 * @param peer peer identity this notification is about
5475 * @param atsi performance data
5476 * @param atsi_count number of records in 'atsi'
5478 * @return GNUNET_OK to keep the connection open,
5479 * GNUNET_SYSERR to close it (signal serious error)
5482 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5483 const struct GNUNET_MessageHeader *message,
5484 const struct GNUNET_ATS_Information *atsi,
5485 unsigned int atsi_count)
5487 struct GNUNET_MESH_ManipulatePath *msg;
5488 struct GNUNET_PeerIdentity *pi;
5489 struct MeshPeerPath *path;
5490 struct MeshTunnel *t;
5491 unsigned int own_pos;
5495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5496 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5497 size = ntohs (message->size);
5498 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5500 GNUNET_break_op (0);
5504 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5505 if (size % sizeof (struct GNUNET_PeerIdentity))
5507 GNUNET_break_op (0);
5510 size /= sizeof (struct GNUNET_PeerIdentity);
5513 GNUNET_break_op (0);
5516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5518 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5519 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5521 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5523 t = tunnel_get (pi, ntohl (msg->tid));
5526 /* TODO notify back: we don't know this tunnel */
5527 GNUNET_break_op (0);
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5531 path = path_new (size);
5533 for (i = 0; i < size; i++)
5535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5536 GNUNET_i2s (&pi[i]));
5537 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5538 if (path->peers[i] == myid)
5541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5542 if (own_pos < path->length - 1)
5543 send_prebuilt_message (message, &pi[own_pos + 1], t);
5545 send_client_tunnel_disconnect(t, NULL);
5547 tunnel_delete_peer (t, path->peers[path->length - 1]);
5548 path_destroy (path);
5554 * Core handler for notifications of broken paths
5556 * @param cls closure
5557 * @param message message
5558 * @param peer peer identity this notification is about
5559 * @param atsi performance data
5560 * @param atsi_count number of records in 'atsi'
5562 * @return GNUNET_OK to keep the connection open,
5563 * GNUNET_SYSERR to close it (signal serious error)
5566 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5567 const struct GNUNET_MessageHeader *message,
5568 const struct GNUNET_ATS_Information *atsi,
5569 unsigned int atsi_count)
5571 struct GNUNET_MESH_PathBroken *msg;
5572 struct MeshTunnel *t;
5574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5575 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5576 msg = (struct GNUNET_MESH_PathBroken *) message;
5577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5578 GNUNET_i2s (&msg->peer1));
5579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5580 GNUNET_i2s (&msg->peer2));
5581 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5584 GNUNET_break_op (0);
5587 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5588 GNUNET_PEER_search (&msg->peer2));
5595 * Core handler for tunnel destruction
5597 * @param cls closure
5598 * @param message message
5599 * @param peer peer identity this notification is about
5600 * @param atsi performance data
5601 * @param atsi_count number of records in 'atsi'
5603 * @return GNUNET_OK to keep the connection open,
5604 * GNUNET_SYSERR to close it (signal serious error)
5607 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5608 const struct GNUNET_MessageHeader *message,
5609 const struct GNUNET_ATS_Information *atsi,
5610 unsigned int atsi_count)
5612 struct GNUNET_MESH_TunnelDestroy *msg;
5613 struct MeshTunnel *t;
5615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5616 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5617 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5619 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5620 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5623 /* Probably already got the message from another path,
5624 * destroyed the tunnel and retransmitted to children.
5627 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5630 if (t->id.oid == myid)
5632 GNUNET_break_op (0);
5635 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5637 /* Tunnel was incoming, notify clients */
5638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5639 t->local_tid, t->local_tid_dest);
5640 send_clients_tunnel_destroy (t);
5642 tunnel_send_destroy (t);
5643 t->destroy = GNUNET_YES;
5644 // TODO: add timeout to destroy the tunnel anyway
5650 * Core handler for mesh network traffic going from the origin to a peer
5652 * @param cls closure
5653 * @param peer peer identity this notification is about
5654 * @param message message
5655 * @param atsi performance data
5656 * @param atsi_count number of records in 'atsi'
5657 * @return GNUNET_OK to keep the connection open,
5658 * GNUNET_SYSERR to close it (signal serious error)
5661 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5662 const struct GNUNET_MessageHeader *message,
5663 const struct GNUNET_ATS_Information *atsi,
5664 unsigned int atsi_count)
5666 struct GNUNET_MESH_Unicast *msg;
5667 struct GNUNET_PeerIdentity *neighbor;
5668 struct MeshTunnelChildInfo *cinfo;
5669 struct MeshTunnel *t;
5670 GNUNET_PEER_Id dest_id;
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5678 size = ntohs (message->size);
5680 sizeof (struct GNUNET_MESH_Unicast) +
5681 sizeof (struct GNUNET_MessageHeader))
5686 msg = (struct GNUNET_MESH_Unicast *) message;
5687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5688 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5690 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5693 /* TODO notify back: we don't know this tunnel */
5694 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5695 GNUNET_break_op (0);
5698 pid = ntohl (msg->pid);
5699 if (t->fwd_pid == pid)
5701 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5703 " Already seen pid %u, DROPPING!\n", pid);
5708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5709 " pid %u not seen yet, forwarding\n", pid);
5712 t->skip += (pid - t->fwd_pid) - 1;
5715 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5717 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5718 GNUNET_break_op (0);
5719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5720 "Received PID %u, ACK %u\n",
5721 pid, t->last_fwd_ack);
5725 tunnel_reset_timeout (t);
5726 dest_id = GNUNET_PEER_search (&msg->destination);
5727 if (dest_id == myid)
5729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5730 " it's for us! sending to clients...\n");
5731 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5732 send_subscribed_clients (message, &msg[1].header, t);
5733 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5736 ttl = ntohl (msg->ttl);
5737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5740 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5741 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5742 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5746 " not for us, retransmitting...\n");
5748 neighbor = tree_get_first_hop (t->tree, dest_id);
5749 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5750 cinfo->fwd_pid = pid;
5751 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5754 if (GNUNET_YES == t->nobuffer &&
5755 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5757 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5758 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5759 GNUNET_break_op (0);
5762 send_prebuilt_message (message, neighbor, t);
5763 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5769 * Core handler for mesh network traffic going from the origin to all peers
5771 * @param cls closure
5772 * @param message message
5773 * @param peer peer identity this notification is about
5774 * @param atsi performance data
5775 * @param atsi_count number of records in 'atsi'
5776 * @return GNUNET_OK to keep the connection open,
5777 * GNUNET_SYSERR to close it (signal serious error)
5779 * TODO: Check who we got this from, to validate route.
5782 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5783 const struct GNUNET_MessageHeader *message,
5784 const struct GNUNET_ATS_Information *atsi,
5785 unsigned int atsi_count)
5787 struct GNUNET_MESH_Multicast *msg;
5788 struct MeshTunnel *t;
5792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5794 size = ntohs (message->size);
5795 if (sizeof (struct GNUNET_MESH_Multicast) +
5796 sizeof (struct GNUNET_MessageHeader) > size)
5798 GNUNET_break_op (0);
5801 msg = (struct GNUNET_MESH_Multicast *) message;
5802 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5806 /* TODO notify that we dont know that tunnel */
5807 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5808 GNUNET_break_op (0);
5811 pid = ntohl (msg->pid);
5812 if (t->fwd_pid == pid)
5814 /* already seen this packet, drop */
5815 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5817 " Already seen pid %u, DROPPING!\n", pid);
5818 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5824 " pid %u not seen yet, forwarding\n", pid);
5826 t->skip += (pid - t->fwd_pid) - 1;
5828 tunnel_reset_timeout (t);
5830 /* Transmit to locally interested clients */
5831 if (NULL != t->peers &&
5832 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5834 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5835 send_subscribed_clients (message, &msg[1].header, t);
5836 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5839 if (ntohl (msg->ttl) == 0)
5841 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5842 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5843 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5846 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5847 tunnel_send_multicast (t, message);
5853 * Core handler for mesh network traffic toward the owner of a tunnel
5855 * @param cls closure
5856 * @param message message
5857 * @param peer peer identity this notification is about
5858 * @param atsi performance data
5859 * @param atsi_count number of records in 'atsi'
5861 * @return GNUNET_OK to keep the connection open,
5862 * GNUNET_SYSERR to close it (signal serious error)
5865 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5866 const struct GNUNET_MessageHeader *message,
5867 const struct GNUNET_ATS_Information *atsi,
5868 unsigned int atsi_count)
5870 struct GNUNET_MESH_ToOrigin *msg;
5871 struct GNUNET_PeerIdentity id;
5872 struct MeshPeerInfo *peer_info;
5873 struct MeshTunnel *t;
5874 struct MeshTunnelChildInfo *cinfo;
5878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5880 size = ntohs (message->size);
5881 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5882 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5884 GNUNET_break_op (0);
5887 msg = (struct GNUNET_MESH_ToOrigin *) message;
5888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5889 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5890 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5891 pid = ntohl (msg->pid);
5895 /* TODO notify that we dont know this tunnel (whom)? */
5896 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5897 GNUNET_break_op (0);
5898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5899 "Received to_origin with PID %u on unknown tunnel\n",
5904 cinfo = tunnel_get_neighbor_fc(t, peer);
5911 if (cinfo->bck_pid == pid)
5913 /* already seen this packet, drop */
5914 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5916 " Already seen pid %u, DROPPING!\n", pid);
5917 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5922 " pid %u not seen yet, forwarding\n", pid);
5923 cinfo->bck_pid = pid;
5925 if (NULL != t->owner)
5928 struct GNUNET_MESH_ToOrigin *copy;
5930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5931 " it's for us! sending to clients...\n");
5932 /* TODO signature verification */
5933 memcpy (cbuf, message, size);
5934 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5935 copy->tid = htonl (t->local_tid);
5937 copy->pid = htonl (t->bck_pid);
5938 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5939 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5940 ©->header, GNUNET_NO);
5941 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5945 " not for us, retransmitting...\n");
5947 peer_info = peer_info_get (&msg->oid);
5948 if (NULL == peer_info)
5950 /* unknown origin of tunnel */
5954 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5955 send_prebuilt_message (message, &id, t);
5956 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5963 * Core handler for mesh network traffic point-to-point acks.
5965 * @param cls closure
5966 * @param message message
5967 * @param peer peer identity this notification is about
5968 * @param atsi performance data
5969 * @param atsi_count number of records in 'atsi'
5971 * @return GNUNET_OK to keep the connection open,
5972 * GNUNET_SYSERR to close it (signal serious error)
5975 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5976 const struct GNUNET_MessageHeader *message,
5977 const struct GNUNET_ATS_Information *atsi,
5978 unsigned int atsi_count)
5980 struct GNUNET_MESH_ACK *msg;
5981 struct MeshTunnel *t;
5984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5986 msg = (struct GNUNET_MESH_ACK *) message;
5988 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5992 /* TODO notify that we dont know this tunnel (whom)? */
5993 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5996 ack = ntohl (msg->pid);
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5999 /* Is this a forward or backward ACK? */
6000 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6002 struct MeshTunnelChildInfo *cinfo;
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6006 cinfo = tunnel_get_neighbor_fc (t, peer);
6007 cinfo->fwd_ack = ack;
6008 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6009 tunnel_unlock_fwd_queues (t);
6013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6015 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6016 tunnel_unlock_bck_queue (t);
6023 * Core handler for mesh network traffic point-to-point ack polls.
6025 * @param cls closure
6026 * @param message message
6027 * @param peer peer identity this notification is about
6028 * @param atsi performance data
6029 * @param atsi_count number of records in 'atsi'
6031 * @return GNUNET_OK to keep the connection open,
6032 * GNUNET_SYSERR to close it (signal serious error)
6035 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6036 const struct GNUNET_MessageHeader *message,
6037 const struct GNUNET_ATS_Information *atsi,
6038 unsigned int atsi_count)
6040 struct GNUNET_MESH_Poll *msg;
6041 struct MeshTunnel *t;
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6046 msg = (struct GNUNET_MESH_Poll *) message;
6048 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6052 /* TODO notify that we dont know this tunnel (whom)? */
6053 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6054 GNUNET_break_op (0);
6058 /* Is this a forward or backward ACK? */
6059 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6061 struct MeshTunnelChildInfo *cinfo;
6063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6064 cinfo = tunnel_get_neighbor_fc (t, peer);
6065 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6066 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6071 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6079 * Core handler for path ACKs
6081 * @param cls closure
6082 * @param message message
6083 * @param peer peer identity this notification is about
6084 * @param atsi performance data
6085 * @param atsi_count number of records in 'atsi'
6087 * @return GNUNET_OK to keep the connection open,
6088 * GNUNET_SYSERR to close it (signal serious error)
6091 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6092 const struct GNUNET_MessageHeader *message,
6093 const struct GNUNET_ATS_Information *atsi,
6094 unsigned int atsi_count)
6096 struct GNUNET_MESH_PathACK *msg;
6097 struct GNUNET_PeerIdentity id;
6098 struct MeshPeerInfo *peer_info;
6099 struct MeshPeerPath *p;
6100 struct MeshTunnel *t;
6102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6103 GNUNET_i2s (&my_full_id));
6104 msg = (struct GNUNET_MESH_PathACK *) message;
6105 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6108 /* TODO notify that we don't know the tunnel */
6109 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6111 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6115 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6117 peer_info = peer_info_get (&msg->peer_id);
6118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6119 GNUNET_i2s (&msg->peer_id));
6120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6123 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6126 "connect_by_string completed, stopping search\n");
6127 regex_cancel_search (t->regex_ctx);
6128 t->regex_ctx = NULL;
6131 /* Add paths to peers? */
6132 p = tree_get_path_to_peer (t->tree, peer_info->id);
6135 path_add_to_peers (p, GNUNET_YES);
6143 /* Message for us? */
6144 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6147 if (NULL == t->owner)
6149 GNUNET_break_op (0);
6152 if (NULL != t->dht_get_type)
6154 GNUNET_DHT_get_stop (t->dht_get_type);
6155 t->dht_get_type = NULL;
6157 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6159 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6160 send_client_peer_connected (t, peer_info->id);
6165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6166 " not for us, retransmitting...\n");
6167 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6168 peer_info = peer_info_get (&msg->oid);
6169 if (NULL == peer_info)
6171 /* If we know the tunnel, we should DEFINITELY know the peer */
6175 send_prebuilt_message (message, &id, t);
6181 * Core handler for mesh keepalives.
6183 * @param cls closure
6184 * @param message message
6185 * @param peer peer identity this notification is about
6186 * @param atsi performance data
6187 * @param atsi_count number of records in 'atsi'
6188 * @return GNUNET_OK to keep the connection open,
6189 * GNUNET_SYSERR to close it (signal serious error)
6191 * TODO: Check who we got this from, to validate route.
6194 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6195 const struct GNUNET_MessageHeader *message,
6196 const struct GNUNET_ATS_Information *atsi,
6197 unsigned int atsi_count)
6199 struct GNUNET_MESH_TunnelKeepAlive *msg;
6200 struct MeshTunnel *t;
6202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6205 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6206 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6210 /* TODO notify that we dont know that tunnel */
6211 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6212 GNUNET_break_op (0);
6216 tunnel_reset_timeout (t);
6218 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6219 tunnel_send_multicast (t, message);
6226 * Functions to handle messages from core
6228 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6229 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6230 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6231 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6232 sizeof (struct GNUNET_MESH_PathBroken)},
6233 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6234 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6235 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6236 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6237 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6238 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6239 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6240 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6241 sizeof (struct GNUNET_MESH_ACK)},
6242 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6243 sizeof (struct GNUNET_MESH_Poll)},
6244 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6245 sizeof (struct GNUNET_MESH_PathACK)},
6251 /******************************************************************************/
6252 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6253 /******************************************************************************/
6256 * deregister_app: iterator for removing each application registered by a client
6258 * @param cls closure
6259 * @param key the hash of the application id (used to access the hashmap)
6260 * @param value the value stored at the key (client)
6262 * @return GNUNET_OK on success
6265 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6267 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6268 GNUNET_break (GNUNET_YES ==
6269 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6275 * notify_client_connection_failure: notify a client that the connection to the
6276 * requested remote peer is not possible (for instance, no route found)
6277 * Function called when the socket is ready to queue more data. "buf" will be
6278 * NULL and "size" zero if the socket was closed for writing in the meantime.
6280 * @param cls closure
6281 * @param size number of bytes available in buf
6282 * @param buf where the callee should write the message
6283 * @return number of bytes written to buf
6286 notify_client_connection_failure (void *cls, size_t size, void *buf)
6289 struct MeshPeerInfo *peer_info;
6290 struct GNUNET_MESH_PeerControl *msg;
6291 struct GNUNET_PeerIdentity id;
6293 if (0 == size && NULL == buf)
6295 // TODO retry? cancel?
6299 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6300 peer_info = (struct MeshPeerInfo *) cls;
6301 msg = (struct GNUNET_MESH_PeerControl *) buf;
6302 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6303 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6304 // msg->tunnel_id = htonl(peer_info->t->tid);
6305 GNUNET_PEER_resolve (peer_info->id, &id);
6306 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6314 * Send keepalive packets for a peer
6316 * @param cls Closure (tunnel for which to send the keepalive).
6317 * @param tc Notification context.
6320 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6322 struct MeshTunnel *t = cls;
6323 struct GNUNET_MESH_TunnelKeepAlive *msg;
6324 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6327 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6328 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6334 "sending keepalive for tunnel %d\n", t->id.tid);
6336 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6337 msg->header.size = htons (size);
6338 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6339 msg->oid = my_full_id;
6340 msg->tid = htonl (t->id.tid);
6341 tunnel_send_multicast (t, &msg->header);
6343 t->path_refresh_task =
6344 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6345 tunnel_reset_timeout(t);
6350 * Function to process paths received for a new peer addition. The recorded
6351 * paths form the initial tunnel, which can be optimized later.
6352 * Called on each result obtained for the DHT search.
6354 * @param cls closure
6355 * @param exp when will this value expire
6356 * @param key key of the result
6357 * @param get_path path of the get request
6358 * @param get_path_length lenght of get_path
6359 * @param put_path path of the put request
6360 * @param put_path_length length of the put_path
6361 * @param type type of the result
6362 * @param size number of bytes in data
6363 * @param data pointer to the result data
6365 * TODO: re-issue the request after certain time? cancel after X results?
6368 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6369 const struct GNUNET_HashCode * key,
6370 const struct GNUNET_PeerIdentity *get_path,
6371 unsigned int get_path_length,
6372 const struct GNUNET_PeerIdentity *put_path,
6373 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6374 size_t size, const void *data)
6376 struct MeshPathInfo *path_info = cls;
6377 struct MeshPeerPath *p;
6378 struct GNUNET_PeerIdentity pi;
6381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6382 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6385 p = path_build_from_dht (get_path, get_path_length, put_path,
6387 path_add_to_peers (p, GNUNET_NO);
6389 for (i = 0; i < path_info->peer->ntunnels; i++)
6391 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6392 peer_info_connect (path_info->peer, path_info->t);
6400 * Function to process paths received for a new peer addition. The recorded
6401 * paths form the initial tunnel, which can be optimized later.
6402 * Called on each result obtained for the DHT search.
6404 * @param cls closure
6405 * @param exp when will this value expire
6406 * @param key key of the result
6407 * @param get_path path of the get request
6408 * @param get_path_length lenght of get_path
6409 * @param put_path path of the put request
6410 * @param put_path_length length of the put_path
6411 * @param type type of the result
6412 * @param size number of bytes in data
6413 * @param data pointer to the result data
6416 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6417 const struct GNUNET_HashCode * key,
6418 const struct GNUNET_PeerIdentity *get_path,
6419 unsigned int get_path_length,
6420 const struct GNUNET_PeerIdentity *put_path,
6421 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6422 size_t size, const void *data)
6424 const struct PBlock *pb = data;
6425 const struct GNUNET_PeerIdentity *pi = &pb->id;
6426 struct MeshTunnel *t = cls;
6427 struct MeshPeerInfo *peer_info;
6428 struct MeshPeerPath *p;
6430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6431 if (size != sizeof (struct PBlock))
6433 GNUNET_break_op (0);
6436 if (ntohl(pb->type) != t->type)
6438 GNUNET_break_op (0);
6441 GNUNET_assert (NULL != t->owner);
6442 peer_info = peer_info_get (pi);
6443 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6445 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6447 p = path_build_from_dht (get_path, get_path_length, put_path,
6449 path_add_to_peers (p, GNUNET_NO);
6451 tunnel_add_peer (t, peer_info);
6452 peer_info_connect (peer_info, t);
6457 * Function to process DHT string to regex matching.
6458 * Called on each result obtained for the DHT search.
6460 * @param cls closure (search context)
6461 * @param exp when will this value expire
6462 * @param key key of the result
6463 * @param get_path path of the get request (not used)
6464 * @param get_path_length lenght of get_path (not used)
6465 * @param put_path path of the put request (not used)
6466 * @param put_path_length length of the put_path (not used)
6467 * @param type type of the result
6468 * @param size number of bytes in data
6469 * @param data pointer to the result data
6472 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6473 const struct GNUNET_HashCode * key,
6474 const struct GNUNET_PeerIdentity *get_path,
6475 unsigned int get_path_length,
6476 const struct GNUNET_PeerIdentity *put_path,
6477 unsigned int put_path_length,
6478 enum GNUNET_BLOCK_Type type,
6479 size_t size, const void *data)
6481 const struct MeshRegexAccept *block = data;
6482 struct MeshRegexSearchContext *ctx = cls;
6483 struct MeshRegexSearchInfo *info = ctx->info;
6484 struct MeshPeerPath *p;
6485 struct MeshPeerInfo *peer_info;
6487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6490 peer_info = peer_info_get(&block->id);
6491 p = path_build_from_dht (get_path, get_path_length, put_path,
6493 path_add_to_peers (p, GNUNET_NO);
6496 tunnel_add_peer (info->t, peer_info);
6497 peer_info_connect (peer_info, info->t);
6498 if (0 == info->peer)
6500 info->peer = peer_info->id;
6504 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6507 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6508 ®ex_connect_timeout,
6516 * Function to process DHT string to regex matching.
6517 * Called on each result obtained for the DHT search.
6519 * @param cls closure (search context)
6520 * @param exp when will this value expire
6521 * @param key key of the result
6522 * @param get_path path of the get request (not used)
6523 * @param get_path_length lenght of get_path (not used)
6524 * @param put_path path of the put request (not used)
6525 * @param put_path_length length of the put_path (not used)
6526 * @param type type of the result
6527 * @param size number of bytes in data
6528 * @param data pointer to the result data
6530 * TODO: re-issue the request after certain time? cancel after X results?
6533 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6534 const struct GNUNET_HashCode * key,
6535 const struct GNUNET_PeerIdentity *get_path,
6536 unsigned int get_path_length,
6537 const struct GNUNET_PeerIdentity *put_path,
6538 unsigned int put_path_length,
6539 enum GNUNET_BLOCK_Type type,
6540 size_t size, const void *data)
6542 const struct MeshRegexBlock *block = data;
6543 struct MeshRegexSearchContext *ctx = cls;
6544 struct MeshRegexSearchInfo *info = ctx->info;
6548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6549 "DHT GET STRING RETURNED RESULTS\n");
6550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6551 " key: %s\n", GNUNET_h2s (key));
6553 copy = GNUNET_malloc (size);
6554 memcpy (copy, data, size);
6555 GNUNET_break (GNUNET_OK ==
6556 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6557 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6558 len = ntohl (block->n_proof);
6560 char proof[len + 1];
6562 memcpy (proof, &block[1], len);
6564 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6566 GNUNET_break_op (0);
6570 len = strlen (info->description);
6571 if (len == ctx->position) // String processed
6573 if (GNUNET_YES == ntohl (block->accepting))
6575 regex_find_path(key, ctx);
6579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6580 // FIXME REGEX this block not successful, wait for more? start timeout?
6585 regex_next_edge (block, size, ctx);
6590 /******************************************************************************/
6591 /********************* MESH LOCAL HANDLES **************************/
6592 /******************************************************************************/
6596 * Handler for client disconnection
6598 * @param cls closure
6599 * @param client identification of the client; NULL
6600 * for the last call when the server is destroyed
6603 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6605 struct MeshClient *c;
6606 struct MeshClient *next;
6609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6616 // return; uncomment for regex_profiler
6620 if (c->handle != client)
6622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6628 GNUNET_SERVER_client_drop (c->handle);
6629 c->shutting_down = GNUNET_YES;
6630 GNUNET_assert (NULL != c->own_tunnels);
6631 GNUNET_assert (NULL != c->incoming_tunnels);
6632 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6633 &tunnel_destroy_iterator, c);
6634 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6635 &tunnel_destroy_iterator, c);
6636 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6637 &tunnel_destroy_iterator, c);
6638 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6639 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6640 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6642 /* deregister clients applications */
6643 if (NULL != c->apps)
6645 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6646 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6648 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6649 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6651 GNUNET_SCHEDULER_cancel (announce_applications_task);
6652 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6654 if (NULL != c->types)
6655 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6656 for (i = 0; i < c->n_regex; i++)
6658 GNUNET_free (c->regexes[i].regex);
6660 GNUNET_free_non_null (c->regexes);
6661 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6662 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6664 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6667 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6676 * Handler for new clients
6678 * @param cls closure
6679 * @param client identification of the client
6680 * @param message the actual message, which includes messages the client wants
6683 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6684 const struct GNUNET_MessageHeader *message)
6686 struct GNUNET_MESH_ClientConnect *cc_msg;
6687 struct MeshClient *c;
6688 GNUNET_MESH_ApplicationType *a;
6695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6696 /* Check data sanity */
6697 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6698 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6699 ntypes = ntohs (cc_msg->types);
6700 napps = ntohs (cc_msg->applications);
6702 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6705 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6709 /* Create new client structure */
6710 c = GNUNET_malloc (sizeof (struct MeshClient));
6711 c->id = next_client_id++;
6712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6714 GNUNET_SERVER_client_keep (client);
6715 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6718 GNUNET_MESH_ApplicationType at;
6719 struct GNUNET_HashCode hc;
6721 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6722 for (i = 0; i < napps; i++)
6725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6726 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6727 /* store in clients hashmap */
6728 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6730 /* store in global hashmap, for announcements */
6731 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6732 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6734 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6735 announce_applications_task =
6736 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6742 struct GNUNET_HashCode hc;
6744 t = (uint16_t *) & a[napps];
6745 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6746 for (i = 0; i < ntypes; i++)
6749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6750 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6752 /* store in clients hashmap */
6753 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6754 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6755 /* store in global hashmap */
6756 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6757 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6761 " client has %u+%u subscriptions\n", napps, ntypes);
6763 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6764 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6765 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6766 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6767 GNUNET_SERVER_notification_context_add (nc, client);
6768 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6770 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6776 * Handler for clients announcing available services by a regular expression.
6778 * @param cls closure
6779 * @param client identification of the client
6780 * @param message the actual message, which includes messages the client wants
6783 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6784 const struct GNUNET_MessageHeader *message)
6786 struct GNUNET_MESH_RegexAnnounce *msg;
6787 struct MeshRegexDescriptor rd;
6788 struct MeshClient *c;
6792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6794 /* Sanity check for client registration */
6795 if (NULL == (c = client_get (client)))
6798 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6803 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6804 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6805 regex = GNUNET_malloc (len + 1);
6806 memcpy (regex, &msg[1], len);
6809 rd.compression = ntohs (msg->compression_characters);
6810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6813 GNUNET_array_append (c->regexes, c->n_regex, rd);
6814 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6816 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6822 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6828 * Handler for requests of new tunnels
6830 * @param cls closure
6831 * @param client identification of the client
6832 * @param message the actual message
6835 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6836 const struct GNUNET_MessageHeader *message)
6838 struct GNUNET_MESH_TunnelMessage *t_msg;
6839 struct MeshTunnel *t;
6840 struct MeshClient *c;
6841 MESH_TunnelNumber tid;
6843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6845 /* Sanity check for client registration */
6846 if (NULL == (c = client_get (client)))
6849 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6854 /* Message sanity check */
6855 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6862 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6863 /* Sanity check for tunnel numbering */
6864 tid = ntohl (t_msg->tunnel_id);
6865 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6871 /* Sanity check for duplicate tunnel IDs */
6872 if (NULL != tunnel_get_by_local_id (c, tid))
6875 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6879 while (NULL != tunnel_get_by_pi (myid, next_tid))
6880 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6881 t = tunnel_new (myid, next_tid++, c, tid);
6884 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6889 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6891 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6892 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6895 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6901 * Handler for requests of deleting tunnels
6903 * @param cls closure
6904 * @param client identification of the client
6905 * @param message the actual message
6908 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6909 const struct GNUNET_MessageHeader *message)
6911 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6912 struct MeshClient *c;
6913 struct MeshTunnel *t;
6914 MESH_TunnelNumber tid;
6916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6917 "Got a DESTROY TUNNEL from client!\n");
6919 /* Sanity check for client registration */
6920 if (NULL == (c = client_get (client)))
6923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6928 /* Message sanity check */
6929 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6936 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6938 /* Retrieve tunnel */
6939 tid = ntohl (tunnel_msg->tunnel_id);
6940 t = tunnel_get_by_local_id(c, tid);
6943 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6948 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6950 client_ignore_tunnel (c, t);
6952 // TODO: when to destroy incoming tunnel?
6953 if (t->nclients == 0)
6955 GNUNET_assert (GNUNET_YES ==
6956 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6958 GNUNET_assert (GNUNET_YES ==
6959 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6960 &my_full_id.hashPubKey,
6964 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6967 send_client_tunnel_disconnect(t, c);
6968 client_delete_tunnel(c, t);
6970 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6972 tunnel_send_destroy (t);
6973 t->destroy = GNUNET_YES;
6974 // The tunnel will be destroyed when the last message is transmitted.
6975 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6981 * Handler for requests of seeting tunnel's speed.
6983 * @param cls Closure (unused).
6984 * @param client Identification of the client.
6985 * @param message The actual message.
6988 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6989 const struct GNUNET_MessageHeader *message)
6991 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6992 struct MeshClient *c;
6993 struct MeshTunnel *t;
6994 MESH_TunnelNumber tid;
6996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6997 "Got a SPEED request from client!\n");
6999 /* Sanity check for client registration */
7000 if (NULL == (c = client_get (client)))
7003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7009 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7011 /* Retrieve tunnel */
7012 tid = ntohl (tunnel_msg->tunnel_id);
7013 t = tunnel_get_by_local_id(c, tid);
7016 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7022 switch (ntohs(message->type))
7024 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7025 t->speed_min = GNUNET_YES;
7027 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7028 t->speed_min = GNUNET_NO;
7033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7038 * Handler for requests of seeting tunnel's buffering policy.
7040 * @param cls Closure (unused).
7041 * @param client Identification of the client.
7042 * @param message The actual message.
7045 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7046 const struct GNUNET_MessageHeader *message)
7048 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7049 struct MeshClient *c;
7050 struct MeshTunnel *t;
7051 MESH_TunnelNumber tid;
7053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7054 "Got a BUFFER request from client!\n");
7056 /* Sanity check for client registration */
7057 if (NULL == (c = client_get (client)))
7060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7065 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7067 /* Retrieve tunnel */
7068 tid = ntohl (tunnel_msg->tunnel_id);
7069 t = tunnel_get_by_local_id(c, tid);
7072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7074 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7078 switch (ntohs(message->type))
7080 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7081 t->nobuffer = GNUNET_NO;
7083 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7084 t->nobuffer = GNUNET_YES;
7090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7095 * Handler for connection requests to new peers
7097 * @param cls closure
7098 * @param client identification of the client
7099 * @param message the actual message (PeerControl)
7102 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7103 const struct GNUNET_MessageHeader *message)
7105 struct GNUNET_MESH_PeerControl *peer_msg;
7106 struct MeshPeerInfo *peer_info;
7107 struct MeshClient *c;
7108 struct MeshTunnel *t;
7109 MESH_TunnelNumber tid;
7111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7112 /* Sanity check for client registration */
7113 if (NULL == (c = client_get (client)))
7116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7121 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7123 /* Sanity check for message size */
7124 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7131 /* Tunnel exists? */
7132 tid = ntohl (peer_msg->tunnel_id);
7133 t = tunnel_get_by_local_id (c, tid);
7137 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7141 /* Does client own tunnel? */
7142 if (t->owner->handle != client)
7145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7149 GNUNET_i2s (&peer_msg->peer));
7150 peer_info = peer_info_get (&peer_msg->peer);
7152 tunnel_add_peer (t, peer_info);
7153 peer_info_connect (peer_info, t);
7155 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7161 * Handler for disconnection requests of peers in a tunnel
7163 * @param cls closure
7164 * @param client identification of the client
7165 * @param message the actual message (PeerControl)
7168 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7169 const struct GNUNET_MessageHeader *message)
7171 struct GNUNET_MESH_PeerControl *peer_msg;
7172 struct MeshPeerInfo *peer_info;
7173 struct MeshClient *c;
7174 struct MeshTunnel *t;
7175 MESH_TunnelNumber tid;
7177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7178 /* Sanity check for client registration */
7179 if (NULL == (c = client_get (client)))
7182 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7187 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7189 /* Sanity check for message size */
7190 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7197 /* Tunnel exists? */
7198 tid = ntohl (peer_msg->tunnel_id);
7199 t = tunnel_get_by_local_id (c, tid);
7203 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7208 /* Does client own tunnel? */
7209 if (t->owner->handle != client)
7212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7217 GNUNET_i2s (&peer_msg->peer));
7218 /* Is the peer in the tunnel? */
7220 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7221 if (NULL == peer_info)
7224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7228 /* Ok, delete peer from tunnel */
7229 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7230 &peer_msg->peer.hashPubKey);
7232 send_destroy_path (t, peer_info->id);
7233 tunnel_delete_peer (t, peer_info->id);
7234 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7239 * Handler for blacklist requests of peers in a tunnel
7241 * @param cls closure
7242 * @param client identification of the client
7243 * @param message the actual message (PeerControl)
7245 * FIXME implement DHT block bloomfilter
7248 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7249 const struct GNUNET_MessageHeader *message)
7251 struct GNUNET_MESH_PeerControl *peer_msg;
7252 struct MeshClient *c;
7253 struct MeshTunnel *t;
7254 MESH_TunnelNumber tid;
7256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7257 /* Sanity check for client registration */
7258 if (NULL == (c = client_get (client)))
7261 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7266 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7268 /* Sanity check for message size */
7269 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7276 /* Tunnel exists? */
7277 tid = ntohl (peer_msg->tunnel_id);
7278 t = tunnel_get_by_local_id (c, tid);
7282 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7287 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7288 GNUNET_PEER_intern(&peer_msg->peer));
7293 * Handler for unblacklist requests of peers in a tunnel
7295 * @param cls closure
7296 * @param client identification of the client
7297 * @param message the actual message (PeerControl)
7300 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7301 const struct GNUNET_MessageHeader *message)
7303 struct GNUNET_MESH_PeerControl *peer_msg;
7304 struct MeshClient *c;
7305 struct MeshTunnel *t;
7306 MESH_TunnelNumber tid;
7310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7311 /* Sanity check for client registration */
7312 if (NULL == (c = client_get (client)))
7315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7320 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7322 /* Sanity check for message size */
7323 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7330 /* Tunnel exists? */
7331 tid = ntohl (peer_msg->tunnel_id);
7332 t = tunnel_get_by_local_id (c, tid);
7336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7341 /* if peer is not known, complain */
7342 pid = GNUNET_PEER_search (&peer_msg->peer);
7349 /* search and remove from list */
7350 for (i = 0; i < t->nblacklisted; i++)
7352 if (t->blacklisted[i] == pid)
7354 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7355 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7360 /* if peer hasn't been blacklisted, complain */
7366 * Handler for connection requests to new peers by type
7368 * @param cls closure
7369 * @param client identification of the client
7370 * @param message the actual message (ConnectPeerByType)
7373 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7374 const struct GNUNET_MessageHeader *message)
7376 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7377 struct MeshClient *c;
7378 struct MeshTunnel *t;
7379 struct GNUNET_HashCode hash;
7380 MESH_TunnelNumber tid;
7382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7383 /* Sanity check for client registration */
7384 if (NULL == (c = client_get (client)))
7387 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7392 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7394 /* Sanity check for message size */
7395 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7396 ntohs (connect_msg->header.size))
7399 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7403 /* Tunnel exists? */
7404 tid = ntohl (connect_msg->tunnel_id);
7405 t = tunnel_get_by_local_id (c, tid);
7409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7413 /* Does client own tunnel? */
7414 if (t->owner->handle != client)
7417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7421 /* Do WE have the service? */
7422 t->type = ntohl (connect_msg->type);
7423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7424 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7425 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7428 /* Yes! Fast forward, add ourselves to the tunnel and send the
7429 * good news to the client, and alert the destination client of
7430 * an incoming tunnel.
7432 * FIXME send a path create to self, avoid code duplication
7434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7435 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7436 peer_info_get (&my_full_id),
7437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7440 send_client_peer_connected (t, myid);
7441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7442 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7444 t->local_tid_dest = next_local_tid++;
7445 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7446 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7451 /* Ok, lets find a peer offering the service */
7452 if (NULL != t->dht_get_type)
7454 GNUNET_DHT_get_stop (t->dht_get_type);
7456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7457 GNUNET_h2s (&hash));
7459 GNUNET_DHT_get_start (dht_handle,
7460 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7462 dht_replication_level,
7463 GNUNET_DHT_RO_RECORD_ROUTE |
7464 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7466 &dht_get_type_handler, t);
7468 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7474 * Handler for connection requests to new peers by a string service description.
7476 * @param cls closure
7477 * @param client identification of the client
7478 * @param message the actual message, which includes messages the client wants
7481 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7482 const struct GNUNET_MessageHeader *message)
7484 struct GNUNET_MESH_ConnectPeerByString *msg;
7485 struct MeshRegexSearchContext *ctx;
7486 struct MeshRegexSearchInfo *info;
7487 struct GNUNET_DHT_GetHandle *get_h;
7488 struct GNUNET_HashCode key;
7489 struct MeshTunnel *t;
7490 struct MeshClient *c;
7491 MESH_TunnelNumber tid;
7495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7496 "Connect by string started\n");
7497 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7498 size = htons (message->size);
7500 /* Sanity check for client registration */
7501 if (NULL == (c = client_get (client)))
7504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7509 /* Message size sanity check */
7510 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7513 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7517 /* Tunnel exists? */
7518 tid = ntohl (msg->tunnel_id);
7519 t = tunnel_get_by_local_id (c, tid);
7523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7527 /* Does client own tunnel? */
7528 if (t->owner->handle != client)
7531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7536 " on tunnel %s [%u]\n",
7537 GNUNET_i2s(&my_full_id),
7540 /* Only one connect_by_string allowed at the same time! */
7541 /* FIXME: allow more, return handle at api level to cancel, document */
7542 if (NULL != t->regex_ctx)
7545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7549 /* Find string itself */
7550 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7551 string = (const char *) &msg[1];
7553 /* Initialize context */
7554 size = GNUNET_REGEX_get_first_key(string, len, &key);
7555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7556 " consumed %u bits out of %u\n", size, len);
7557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7558 " looking for %s\n", GNUNET_h2s (&key));
7560 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7562 info->description = GNUNET_malloc (len + 1);
7563 memcpy (info->description, string, len);
7564 info->description[len] = '\0';
7565 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7566 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7569 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7570 ctx->position = size;
7574 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7576 /* Start search in DHT */
7577 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7578 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7579 &key, /* key to search */
7580 dht_replication_level, /* replication level */
7581 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7582 NULL, /* xquery */ // FIXME BLOOMFILTER
7583 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7584 &dht_get_string_handler, ctx);
7586 GNUNET_break (GNUNET_OK ==
7587 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7590 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7592 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7598 * Handler for client traffic directed to one peer
7600 * @param cls closure
7601 * @param client identification of the client
7602 * @param message the actual message
7605 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7606 const struct GNUNET_MessageHeader *message)
7608 struct MeshClient *c;
7609 struct MeshTunnel *t;
7610 struct MeshPeerInfo *pi;
7611 struct GNUNET_MESH_Unicast *data_msg;
7612 MESH_TunnelNumber tid;
7615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7616 "Got a unicast request from a client!\n");
7618 /* Sanity check for client registration */
7619 if (NULL == (c = client_get (client)))
7622 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7627 data_msg = (struct GNUNET_MESH_Unicast *) message;
7629 /* Sanity check for message size */
7630 size = ntohs (message->size);
7631 if (sizeof (struct GNUNET_MESH_Unicast) +
7632 sizeof (struct GNUNET_MessageHeader) > size)
7635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7639 /* Tunnel exists? */
7640 tid = ntohl (data_msg->tid);
7641 t = tunnel_get_by_local_id (c, tid);
7645 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7649 /* Is it a local tunnel? Then, does client own the tunnel? */
7650 if (t->owner->handle != client)
7653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7657 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7658 &data_msg->destination.hashPubKey);
7659 /* Is the selected peer in the tunnel? */
7663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7667 /* PID should be as expected */
7668 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7671 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7672 "Unicast PID, expected %u, got %u\n",
7673 t->fwd_pid + 1, ntohl (data_msg->pid));
7674 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7678 /* Ok, everything is correct, send the message
7679 * (pretend we got it from a mesh peer)
7682 /* Work around const limitation */
7683 char buf[ntohs (message->size)] GNUNET_ALIGN;
7684 struct GNUNET_MESH_Unicast *copy;
7686 copy = (struct GNUNET_MESH_Unicast *) buf;
7687 memcpy (buf, data_msg, size);
7688 copy->oid = my_full_id;
7689 copy->tid = htonl (t->id.tid);
7690 copy->ttl = htonl (default_ttl);
7691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7692 " calling generic handler...\n");
7693 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7696 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7703 * Handler for client traffic directed to the origin
7705 * @param cls closure
7706 * @param client identification of the client
7707 * @param message the actual message
7710 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7711 const struct GNUNET_MessageHeader *message)
7713 struct GNUNET_MESH_ToOrigin *data_msg;
7714 struct MeshTunnelClientInfo *clinfo;
7715 struct MeshClient *c;
7716 struct MeshTunnel *t;
7717 MESH_TunnelNumber tid;
7720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7721 "Got a ToOrigin request from a client!\n");
7722 /* Sanity check for client registration */
7723 if (NULL == (c = client_get (client)))
7726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7731 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7733 /* Sanity check for message size */
7734 size = ntohs (message->size);
7735 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7736 sizeof (struct GNUNET_MessageHeader) > size)
7739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7743 /* Tunnel exists? */
7744 tid = ntohl (data_msg->tid);
7745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7746 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7749 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7752 t = tunnel_get_by_local_id (c, tid);
7755 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7756 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7758 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7762 /* It should be sent by someone who has this as incoming tunnel. */
7763 if (GNUNET_NO == client_knows_tunnel (c, t))
7766 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7770 /* PID should be as expected */
7771 clinfo = tunnel_get_client_fc (t, c);
7772 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7776 "To Origin PID, expected %u, got %u\n",
7777 clinfo->bck_pid + 1,
7778 ntohl (data_msg->pid));
7779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7784 /* Ok, everything is correct, send the message
7785 * (pretend we got it from a mesh peer)
7788 char buf[ntohs (message->size)] GNUNET_ALIGN;
7789 struct GNUNET_MESH_ToOrigin *copy;
7791 /* Work around const limitation */
7792 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7793 memcpy (buf, data_msg, size);
7794 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7795 copy->tid = htonl (t->id.tid);
7796 copy->ttl = htonl (default_ttl);
7797 copy->pid = htonl (t->bck_pid + 1);
7799 copy->sender = my_full_id;
7800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7801 " calling generic handler...\n");
7802 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7804 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7811 * Handler for client traffic directed to all peers in a tunnel
7813 * @param cls closure
7814 * @param client identification of the client
7815 * @param message the actual message
7818 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7819 const struct GNUNET_MessageHeader *message)
7821 struct MeshClient *c;
7822 struct MeshTunnel *t;
7823 struct GNUNET_MESH_Multicast *data_msg;
7824 MESH_TunnelNumber tid;
7826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7827 "Got a multicast request from a client!\n");
7829 /* Sanity check for client registration */
7830 if (NULL == (c = client_get (client)))
7833 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7838 data_msg = (struct GNUNET_MESH_Multicast *) message;
7840 /* Sanity check for message size */
7841 if (sizeof (struct GNUNET_MESH_Multicast) +
7842 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7845 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7849 /* Tunnel exists? */
7850 tid = ntohl (data_msg->tid);
7851 t = tunnel_get_by_local_id (c, tid);
7855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7856 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7862 /* Does client own tunnel? */
7863 if (t->owner->handle != client)
7866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7870 /* PID should be as expected */
7871 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7874 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7875 "Multicast PID, expected %u, got %u\n",
7876 t->fwd_pid + 1, ntohl (data_msg->pid));
7877 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7882 char buf[ntohs (message->size)] GNUNET_ALIGN;
7883 struct GNUNET_MESH_Multicast *copy;
7885 copy = (struct GNUNET_MESH_Multicast *) buf;
7886 memcpy (buf, message, ntohs (message->size));
7887 copy->oid = my_full_id;
7888 copy->tid = htonl (t->id.tid);
7889 copy->ttl = htonl (default_ttl);
7890 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7892 " calling generic handler...\n");
7893 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7896 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7902 * Handler for client's ACKs for payload traffic.
7904 * @param cls Closure (unused).
7905 * @param client Identification of the client.
7906 * @param message The actual message.
7909 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7910 const struct GNUNET_MessageHeader *message)
7912 struct GNUNET_MESH_LocalAck *msg;
7913 struct MeshTunnel *t;
7914 struct MeshClient *c;
7915 MESH_TunnelNumber tid;
7918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7919 /* Sanity check for client registration */
7920 if (NULL == (c = client_get (client)))
7923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7928 msg = (struct GNUNET_MESH_LocalAck *) message;
7930 /* Tunnel exists? */
7931 tid = ntohl (msg->tunnel_id);
7932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7933 t = tunnel_get_by_local_id (c, tid);
7937 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7938 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7943 ack = ntohl (msg->max_pid);
7944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7946 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7947 if (NULL != t->owner && t->owner->handle == client)
7949 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7951 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7955 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7956 tunnel_set_client_fwd_ack (t, c, ack);
7957 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7960 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7967 * Functions to handle messages from clients
7969 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7970 {&handle_local_new_client, NULL,
7971 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7972 {&handle_local_announce_regex, NULL,
7973 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7974 {&handle_local_tunnel_create, NULL,
7975 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7976 sizeof (struct GNUNET_MESH_TunnelMessage)},
7977 {&handle_local_tunnel_destroy, NULL,
7978 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7979 sizeof (struct GNUNET_MESH_TunnelMessage)},
7980 {&handle_local_tunnel_speed, NULL,
7981 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7982 sizeof (struct GNUNET_MESH_TunnelMessage)},
7983 {&handle_local_tunnel_speed, NULL,
7984 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7985 sizeof (struct GNUNET_MESH_TunnelMessage)},
7986 {&handle_local_tunnel_buffer, NULL,
7987 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7988 sizeof (struct GNUNET_MESH_TunnelMessage)},
7989 {&handle_local_tunnel_buffer, NULL,
7990 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7991 sizeof (struct GNUNET_MESH_TunnelMessage)},
7992 {&handle_local_connect_add, NULL,
7993 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7994 sizeof (struct GNUNET_MESH_PeerControl)},
7995 {&handle_local_connect_del, NULL,
7996 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7997 sizeof (struct GNUNET_MESH_PeerControl)},
7998 {&handle_local_blacklist, NULL,
7999 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8000 sizeof (struct GNUNET_MESH_PeerControl)},
8001 {&handle_local_unblacklist, NULL,
8002 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8003 sizeof (struct GNUNET_MESH_PeerControl)},
8004 {&handle_local_connect_by_type, NULL,
8005 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8006 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8007 {&handle_local_connect_by_string, NULL,
8008 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8009 {&handle_local_unicast, NULL,
8010 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8011 {&handle_local_to_origin, NULL,
8012 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8013 {&handle_local_multicast, NULL,
8014 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8015 {&handle_local_ack, NULL,
8016 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8017 sizeof (struct GNUNET_MESH_LocalAck)},
8023 * To be called on core init/fail.
8025 * @param cls service closure
8026 * @param server handle to the server for this service
8027 * @param identity the public identity of this peer
8030 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8031 const struct GNUNET_PeerIdentity *identity)
8034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8035 core_handle = server;
8036 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8040 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8042 GNUNET_abort(); // Try harder
8049 * Method called whenever a given peer connects.
8051 * @param cls closure
8052 * @param peer peer identity this notification is about
8053 * @param atsi performance data for the connection
8054 * @param atsi_count number of records in 'atsi'
8057 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8058 const struct GNUNET_ATS_Information *atsi,
8059 unsigned int atsi_count)
8061 struct MeshPeerInfo *peer_info;
8062 struct MeshPeerPath *path;
8064 DEBUG_CONN ("Peer connected\n");
8065 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8066 peer_info = peer_info_get (peer);
8067 if (myid == peer_info->id)
8069 DEBUG_CONN (" (self)\n");
8074 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8076 path = path_new (2);
8077 path->peers[0] = myid;
8078 path->peers[1] = peer_info->id;
8079 GNUNET_PEER_change_rc (myid, 1);
8080 GNUNET_PEER_change_rc (peer_info->id, 1);
8081 peer_info_add_path (peer_info, path, GNUNET_YES);
8082 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8088 * Method called whenever a peer disconnects.
8090 * @param cls closure
8091 * @param peer peer identity this notification is about
8094 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8096 struct MeshPeerInfo *pi;
8097 struct MeshPeerQueue *q;
8098 struct MeshPeerQueue *n;
8100 DEBUG_CONN ("Peer disconnected\n");
8101 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8111 /* TODO try to reroute this traffic instead */
8112 queue_destroy(q, GNUNET_YES);
8115 if (NULL != pi->core_transmit)
8117 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8118 pi->core_transmit = NULL;
8120 peer_info_remove_path (pi, pi->id, myid);
8123 DEBUG_CONN (" (self)\n");
8125 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8130 /******************************************************************************/
8131 /************************ MAIN FUNCTIONS ****************************/
8132 /******************************************************************************/
8135 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8137 * @param cls closure
8138 * @param key current key code
8139 * @param value value in the hash map
8140 * @return GNUNET_YES if we should continue to iterate,
8144 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8146 struct MeshTunnel *t = value;
8153 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8155 * @param cls closure
8156 * @param key current key code
8157 * @param value value in the hash map
8158 * @return GNUNET_YES if we should continue to iterate,
8162 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8164 struct MeshPeerInfo *p = value;
8165 struct MeshPeerQueue *q;
8166 struct MeshPeerQueue *n;
8174 queue_destroy(q, GNUNET_YES);
8178 peer_info_destroy (p);
8184 * Task run during shutdown.
8190 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8194 if (core_handle != NULL)
8196 GNUNET_CORE_disconnect (core_handle);
8201 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8204 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8205 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8206 if (dht_handle != NULL)
8208 GNUNET_DHT_disconnect (dht_handle);
8213 GNUNET_SERVER_notification_context_destroy (nc);
8216 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8218 GNUNET_SCHEDULER_cancel (announce_id_task);
8219 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8226 * Callback for hostkey read/generation
8229 * @param pk the private key
8230 * @param emsg error message
8233 key_generation_cb (void *cls,
8234 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8237 struct MeshPeerInfo *peer;
8238 struct MeshPeerPath *p;
8243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8244 _("Mesh service could not access hostkey. Exiting.\n"));
8245 GNUNET_SCHEDULER_shutdown ();
8248 my_private_key = pk;
8249 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8250 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8251 &my_full_id.hashPubKey);
8252 myid = GNUNET_PEER_intern (&my_full_id);
8253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8254 "Mesh for peer [%s] starting\n",
8255 GNUNET_i2s(&my_full_id));
8257 // transport_handle = GNUNET_TRANSPORT_connect(c,
8267 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8270 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8271 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8272 GNUNET_SERVER_disconnect_notify (server_handle,
8273 &handle_local_client_disconnect, NULL);
8277 clients_tail = NULL;
8280 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8281 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8283 /* Create a peer_info for the local peer */
8284 peer = peer_info_get (&my_full_id);
8287 GNUNET_PEER_change_rc (myid, 1);
8288 peer_info_add_path (peer, p, GNUNET_YES);
8289 GNUNET_SERVER_resume (server_handle);
8290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8295 * Process mesh requests.
8297 * @param cls closure
8298 * @param server the initialized server
8299 * @param c configuration to use
8302 run (void *cls, struct GNUNET_SERVER_Handle *server,
8303 const struct GNUNET_CONFIGURATION_Handle *c)
8307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8308 server_handle = server;
8309 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8310 NULL, /* Closure passed to MESH functions */
8311 &core_init, /* Call core_init once connected */
8312 &core_connect, /* Handle connects */
8313 &core_disconnect, /* remove peers on disconnects */
8314 NULL, /* Don't notify about all incoming messages */
8315 GNUNET_NO, /* For header only in notification */
8316 NULL, /* Don't notify about all outbound messages */
8317 GNUNET_NO, /* For header-only out notification */
8318 core_handlers); /* Register these handlers */
8320 if (core_handle == NULL)
8323 GNUNET_SCHEDULER_shutdown ();
8328 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8333 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8335 GNUNET_SCHEDULER_shutdown ();
8340 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8341 &refresh_path_time))
8343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8345 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8346 "mesh", "refresh path time");
8347 GNUNET_SCHEDULER_shutdown ();
8352 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8353 &app_announce_time))
8355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8357 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8358 "mesh", "app announce time");
8359 GNUNET_SCHEDULER_shutdown ();
8362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8365 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8370 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8371 "mesh", "id announce time");
8372 GNUNET_SCHEDULER_shutdown ();
8380 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8385 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8386 "mesh", "connect timeout");
8387 GNUNET_SCHEDULER_shutdown ();
8392 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8397 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8398 "mesh", "max msgs queue");
8399 GNUNET_SCHEDULER_shutdown ();
8404 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8409 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8410 "mesh", "max tunnels");
8411 GNUNET_SCHEDULER_shutdown ();
8416 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8419 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8421 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8422 "mesh", "default ttl", 64);
8427 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8428 &dht_replication_level))
8430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8432 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8433 "mesh", "dht replication level", 10);
8434 dht_replication_level = 10;
8437 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8438 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8439 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8440 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8441 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8443 dht_handle = GNUNET_DHT_connect (c, 64);
8444 if (NULL == dht_handle)
8448 stats = GNUNET_STATISTICS_create ("mesh", c);
8450 GNUNET_SERVER_suspend (server_handle);
8451 /* Scheduled the task to clean up when shutdown is called */
8452 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8454 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8455 GNUNET_free (keyfile);
8460 * The main function for the mesh service.
8462 * @param argc number of arguments from the command line
8463 * @param argv command line arguments
8464 * @return 0 ok, 1 on error
8467 main (int argc, char *const *argv)
8472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8473 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8475 ret = (GNUNET_OK == r) ? 0 : 1;
8476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8480 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8481 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8482 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);