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 * Cached DFA for regular expression 'regex'.
188 struct GNUNET_REGEX_Automaton *dfa;
191 * How many characters per edge can we squeeze?
193 uint16_t compression;
197 * Struct containing all info possibly needed to build a package when called
200 struct MeshTransmissionDescriptor
202 /** ID of the tunnel this packet travels in */
203 struct MESH_TunnelID *origin;
205 /** Who was this message being sent to */
206 struct MeshPeerInfo *peer;
208 /** Ultimate destination of the packet */
209 GNUNET_PEER_Id destination;
211 /** Data descriptor */
212 struct MeshData* mesh_data;
217 * Struct containing all information regarding a given peer
227 * Last time we heard from this peer
229 struct GNUNET_TIME_Absolute last_contact;
232 * Task handler for delayed connect task;
234 GNUNET_SCHEDULER_TaskIdentifier connect_task;
237 * Number of attempts to reconnect so far
239 int n_reconnect_attempts;
242 * Paths to reach the peer, ordered by ascending hop count
244 struct MeshPeerPath *path_head;
247 * Paths to reach the peer, ordered by ascending hop count
249 struct MeshPeerPath *path_tail;
252 * Handle to stop the DHT search for a path to this peer
254 struct GNUNET_DHT_GetHandle *dhtget;
257 * Closure given to the DHT GET
259 struct MeshPathInfo *dhtgetcls;
262 * Array of tunnels this peer participates in
263 * (most probably a small amount, therefore not a hashmap)
264 * When the path to the peer changes, notify these tunnels to let them
265 * re-adjust their path trees.
267 struct MeshTunnel **tunnels;
270 * Number of tunnels this peers participates in
272 unsigned int ntunnels;
275 * Transmission queue to core DLL head
277 struct MeshPeerQueue *queue_head;
280 * Transmission queue to core DLL tail
282 struct MeshPeerQueue *queue_tail;
285 * How many messages are in the queue to this peer.
287 unsigned int queue_n;
290 * Handle to for queued transmissions
292 struct GNUNET_CORE_TransmitHandle *core_transmit;
297 * Globally unique tunnel identification (owner + number)
298 * DO NOT USE OVER THE NETWORK
303 * Node that owns the tunnel
308 * Tunnel number to differentiate all the tunnels owned by the node oid
309 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
311 MESH_TunnelNumber tid;
316 * Struct containing all information regarding a tunnel
317 * For an intermediate node the improtant info used will be:
318 * - id Tunnel unique identification
319 * - paths[0] To know where to send it next
320 * - metainfo: ready, speeds, accounting
327 struct MESH_TunnelID id;
330 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
332 MESH_TunnelNumber local_tid;
335 * Local tunnel number for local destination clients (incoming number)
336 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
339 MESH_TunnelNumber local_tid_dest;
342 * Is the speed on the tunnel limited to the slowest peer?
347 * Is the tunnel bufferless (minimum latency)?
352 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
357 * Packet ID of the last bck packet sent (unique counter per hop).
362 * SKIP value for this tunnel.
367 * Force sending ACK? Flag to allow duplicate ACK on POLL.
372 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
373 * Contains the Flow Control info: FWD ACK value received,
374 * last BCK ACK sent, PID and SKIP values.
376 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
379 * Last ACK sent towards the origin (for traffic towards leaf node).
381 uint32_t last_fwd_ack;
384 * BCK ACK value received from the hop towards the owner of the tunnel,
385 * (previous node / owner): up to what message PID can we sent back to him.
390 * How many messages are in the forward queue (towards leaves).
392 unsigned int fwd_queue_n;
395 * How many messages do we accept in the forward queue.
397 unsigned int fwd_queue_max;
400 * How many messages are in the backward queue (towards origin).
402 unsigned int bck_queue_n;
405 * How many messages do we accept in the backward queue.
407 unsigned int bck_queue_max;
410 * Task to poll peer in case of a stall.
412 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
415 * Last time the tunnel was used
417 struct GNUNET_TIME_Absolute timestamp;
420 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
421 * containing peers added by id or by type, not intermediate peers.
423 struct GNUNET_CONTAINER_MultiHashMap *peers;
426 * Number of peers that are connected and potentially ready to receive data
428 unsigned int peers_ready;
431 * Number of peers that have been added to the tunnel
433 unsigned int peers_total;
436 * Client owner of the tunnel, if any
438 struct MeshClient *owner;
441 * Clients that have been informed about and want to stay in the tunnel.
443 struct MeshClient **clients;
446 * Flow control info for each client.
448 struct MeshTunnelClientInfo *clients_fc;
451 * Number of elements in clients/clients_fc
453 unsigned int nclients;
456 * Clients that have been informed but requested to leave the tunnel.
458 struct MeshClient **ignore;
461 * Number of elements in clients
463 unsigned int nignore;
468 GNUNET_PEER_Id *blacklisted;
471 * Number of elements in blacklisted
473 unsigned int nblacklisted;
476 * Bloomfilter (for peer identities) to stop circular routes
478 char bloomfilter[MESH_BLOOM_SIZE];
483 struct MeshTunnelTree *tree;
486 * Application type we are looking for in this tunnel
488 GNUNET_MESH_ApplicationType type;
491 * Used to search peers offering a service
493 struct GNUNET_DHT_GetHandle *dht_get_type;
496 * Initial context of the regex search for a connect_by_string
498 struct MeshRegexSearchContext *regex_ctx;
501 * Task to keep the used paths alive
503 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
506 * Task to destroy the tunnel after timeout
508 * FIXME: merge the two? a tunnel will have either
509 * a path refresh OR a timeout, never both!
511 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
514 * Flag to signal the destruction of the tunnel.
515 * If this is set GNUNET_YES the tunnel will be destroyed
516 * when the queue is empty.
521 * Total messages pending for this tunnels, payload or not.
523 unsigned int pending_messages;
528 * Info about a child node in a tunnel, needed to perform flow control.
530 struct MeshTunnelChildInfo
533 * ID of the child node.
553 * Maximum PID allowed (FWD ACK received).
558 * Last ACK sent to that child (BCK ACK).
563 * Circular buffer pointing to MeshPeerQueue elements for all
564 * payload traffic going to this child.
565 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
567 struct MeshPeerQueue **send_buffer;
570 * Index of the oldest element in the send_buffer.
572 unsigned int send_buffer_start;
575 * How many elements are already in the buffer.
577 unsigned int send_buffer_n;
580 * Tunnel this info is about
582 struct MeshTunnel *t;
585 * Task to poll peer in case of a stall.
587 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
592 * Info about a leaf client of a tunnel, needed to perform flow control.
594 struct MeshTunnelClientInfo
597 * PID of the last packet sent to the client (FWD).
602 * PID of the last packet received from the client (BCK).
607 * Maximum PID allowed (FWD ACK received).
612 * Last ACK sent to that child (BCK ACK).
620 * Info collected during iteration of child nodes in order to get the ACK value
623 struct MeshTunnelChildIteratorContext
626 * Tunnel whose info is being collected.
628 struct MeshTunnel *t;
631 * Is this context initialized? Is the value in max_child_ack valid?
636 * Maximum child ACK so far.
638 uint32_t max_child_ack;
641 * Number of children nodes
643 unsigned int nchildren;
648 * Info needed to work with tunnel paths and peers
655 struct MeshTunnel *t;
658 * Neighbouring peer to whom we send the packet to
660 struct MeshPeerInfo *peer;
665 struct MeshPeerPath *path;
670 * Struct containing information about a client of the service
677 struct MeshClient *next;
682 struct MeshClient *prev;
685 * Tunnels that belong to this client, indexed by local id
687 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
690 * Tunnels this client has accepted, indexed by incoming local id
692 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
695 * Tunnels this client has rejected, indexed by incoming local id
697 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
699 * Handle to communicate with the client
701 struct GNUNET_SERVER_Client *handle;
704 * Applications that this client has claimed to provide
706 struct GNUNET_CONTAINER_MultiHashMap *apps;
709 * Messages that this client has declared interest in
711 struct GNUNET_CONTAINER_MultiHashMap *types;
714 * Whether the client is active or shutting down (don't send confirmations
715 * to a client that is shutting down.
720 * ID of the client, mainly for debug messages
725 * Regular expressions describing the services offered by this client.
727 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
730 * Number of regular expressions in regexes.
732 unsigned int n_regex;
735 * Task to refresh all regular expresions in the DHT.
737 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
743 * Struct to keep information of searches of services described by a regex
744 * using a user-provided string service description.
746 struct MeshRegexSearchInfo
749 * Which tunnel is this for
751 struct MeshTunnel *t;
754 * User provided description of the searched service.
759 * Part of the description already consumed by the search.
766 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
769 * Results from running DHT GETs.
771 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
774 * Contexts, for each running DHT GET. Free all on end of search.
776 struct MeshRegexSearchContext **contexts;
779 * Number of contexts (branches/steps in search).
781 unsigned int n_contexts;
784 * Peer that is connecting via connect_by_string. When connected, free ctx.
789 * Other peers that are found but not yet being connected to.
791 GNUNET_PEER_Id *peers;
794 * Number of elements in peers.
796 unsigned int n_peers;
799 * Next peer to try to connect to.
804 * Timeout for a connect attempt.
805 * When reached, try to connect to a different peer, if any. If not,
806 * try the same peer again.
808 GNUNET_SCHEDULER_TaskIdentifier timeout;
813 * Struct to keep state of running searches that have consumed a part of
816 struct MeshRegexSearchContext
819 * Part of the description already consumed by
820 * this particular search branch.
825 * Information about the search.
827 struct MeshRegexSearchInfo *info;
830 * We just want to look for one edge, the longer the better.
833 unsigned int longest_match;
836 * Destination hash of the longest match.
838 struct GNUNET_HashCode hash;
841 /******************************************************************************/
842 /************************ DEBUG FUNCTIONS ****************************/
843 /******************************************************************************/
847 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
848 * @param cls string to print
849 * @param success GNUNET_OK if the PUT was transmitted,
850 * GNUNET_NO on timeout,
851 * GNUNET_SYSERR on disconnect from service
852 * after the PUT message was transmitted
853 * (so we don't know if it was received or not)
858 mesh_debug (void *cls, int success)
862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
866 unsigned int debug_fwd_ack;
867 unsigned int debug_bck_ack;
871 /******************************************************************************/
872 /*********************** GLOBAL VARIABLES ****************************/
873 /******************************************************************************/
875 /************************** Configuration parameters **************************/
878 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
880 static struct GNUNET_TIME_Relative refresh_path_time;
883 * How often to PUT local application numbers in the DHT.
885 static struct GNUNET_TIME_Relative app_announce_time;
888 * How often to PUT own ID in the DHT.
890 static struct GNUNET_TIME_Relative id_announce_time;
893 * Maximum time allowed to connect to a peer found by string.
895 static struct GNUNET_TIME_Relative connect_timeout;
898 * Default TTL for payload packets.
900 static unsigned long long default_ttl;
903 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
905 static unsigned long long dht_replication_level;
908 * How many tunnels are we willing to maintain.
909 * Local tunnels are always allowed, even if there are more tunnels than max.
911 static unsigned long long max_tunnels;
914 * How many messages *in total* are we willing to queue, divided by number of
915 * tunnels to get tunnel queue size.
917 static unsigned long long max_msgs_queue;
920 /*************************** Static global variables **************************/
923 * Hostkey generation context
925 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
928 * DLL with all the clients, head.
930 static struct MeshClient *clients;
933 * DLL with all the clients, tail.
935 static struct MeshClient *clients_tail;
938 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
940 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
943 * Number of tunnels known.
945 static unsigned long long n_tunnels;
948 * Tunnels incoming, indexed by MESH_TunnelNumber
949 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
951 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
954 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
956 static struct GNUNET_CONTAINER_MultiHashMap *peers;
959 * Handle to communicate with transport
961 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
964 * Handle to communicate with core.
966 static struct GNUNET_CORE_Handle *core_handle;
971 static struct GNUNET_DHT_Handle *dht_handle;
976 static struct GNUNET_SERVER_Handle *server_handle;
979 * Handle to the statistics service.
981 static struct GNUNET_STATISTICS_Handle *stats;
984 * Notification context, to send messages to local clients.
986 static struct GNUNET_SERVER_NotificationContext *nc;
989 * Local peer own ID (memory efficient handle).
991 static GNUNET_PEER_Id myid;
994 * Local peer own ID (full value).
996 static struct GNUNET_PeerIdentity my_full_id;
1001 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1006 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1009 * Tunnel ID for the next created tunnel (global tunnel number).
1011 static MESH_TunnelNumber next_tid;
1014 * Tunnel ID for the next incoming tunnel (local tunnel number).
1016 static MESH_TunnelNumber next_local_tid;
1019 * All application types provided by this peer.
1021 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1024 * All message types clients of this peer are interested in.
1026 static struct GNUNET_CONTAINER_MultiHashMap *types;
1029 * Task to periodically announce provided applications.
1031 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1034 * Task to periodically announce itself in the network.
1036 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1039 * Next ID to assign to a client.
1041 unsigned int next_client_id;
1044 /******************************************************************************/
1045 /*********************** DECLARATIONS **************************/
1046 /******************************************************************************/
1049 * Function to process paths received for a new peer addition. The recorded
1050 * paths form the initial tunnel, which can be optimized later.
1051 * Called on each result obtained for the DHT search.
1053 * @param cls closure
1054 * @param exp when will this value expire
1055 * @param key key of the result
1056 * @param type type of the result
1057 * @param size number of bytes in data
1058 * @param data pointer to the result data
1061 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1062 const struct GNUNET_HashCode * key,
1063 const struct GNUNET_PeerIdentity *get_path,
1064 unsigned int get_path_length,
1065 const struct GNUNET_PeerIdentity *put_path,
1066 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1067 size_t size, const void *data);
1071 * Function to process DHT string to regex matching.
1072 * Called on each result obtained for the DHT search.
1074 * @param cls closure (search context)
1075 * @param exp when will this value expire
1076 * @param key key of the result
1077 * @param get_path path of the get request (not used)
1078 * @param get_path_length lenght of get_path (not used)
1079 * @param put_path path of the put request (not used)
1080 * @param put_path_length length of the put_path (not used)
1081 * @param type type of the result
1082 * @param size number of bytes in data
1083 * @param data pointer to the result data
1085 * TODO: re-issue the request after certain time? cancel after X results?
1088 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1089 const struct GNUNET_HashCode * key,
1090 const struct GNUNET_PeerIdentity *get_path,
1091 unsigned int get_path_length,
1092 const struct GNUNET_PeerIdentity *put_path,
1093 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1094 size_t size, const void *data);
1098 * Function to process DHT string to regex matching.
1099 * Called on each result obtained for the DHT search.
1101 * @param cls closure (search context)
1102 * @param exp when will this value expire
1103 * @param key key of the result
1104 * @param get_path path of the get request (not used)
1105 * @param get_path_length lenght of get_path (not used)
1106 * @param put_path path of the put request (not used)
1107 * @param put_path_length length of the put_path (not used)
1108 * @param type type of the result
1109 * @param size number of bytes in data
1110 * @param data pointer to the result data
1113 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1114 const struct GNUNET_HashCode * key,
1115 const struct GNUNET_PeerIdentity *get_path,
1116 unsigned int get_path_length,
1117 const struct GNUNET_PeerIdentity *put_path,
1118 unsigned int put_path_length,
1119 enum GNUNET_BLOCK_Type type,
1120 size_t size, const void *data);
1124 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1125 * and insert it in the appropiate structures if the peer is not known yet.
1127 * @param peer Short identity of the peer.
1129 * @return Existing or newly created peer info.
1131 static struct MeshPeerInfo *
1132 peer_info_get_short (const GNUNET_PEER_Id peer);
1136 * Try to establish a new connection to this peer.
1137 * Use the best path for the given tunnel.
1138 * If the peer doesn't have any path to it yet, try to get one.
1139 * If the peer already has some path, send a CREATE PATH towards it.
1141 * @param peer PeerInfo of the peer.
1142 * @param t Tunnel for which to create the path, if possible.
1145 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1149 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1150 * needed rescources.
1151 * If peer already exists, reevaluate shortest path and change if different.
1153 * @param t Tunnel we want to add a new peer to
1154 * @param peer PeerInfo of the peer being added
1158 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1162 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1163 * that are no longer needed, as well as nodes of no longer reachable peers.
1164 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1166 * @param t Tunnel from which to remove the path.
1167 * @param peer Short id of the peer which should be removed.
1170 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1174 * Search for a tunnel by global ID using full PeerIdentities.
1176 * @param oid owner of the tunnel.
1177 * @param tid global tunnel number.
1179 * @return tunnel handler, NULL if doesn't exist.
1181 static struct MeshTunnel *
1182 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1186 * Delete an active client from the tunnel.
1192 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1195 * Notify a tunnel that a connection has broken that affects at least
1196 * some of its peers.
1198 * @param t Tunnel affected.
1199 * @param p1 Peer that got disconnected from p2.
1200 * @param p2 Peer that got disconnected from p1.
1202 * @return Short ID of the peer disconnected (either p1 or p2).
1203 * 0 if the tunnel remained unaffected.
1205 static GNUNET_PEER_Id
1206 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1211 * Get the current ack value for a tunnel, for data going from root to leaves,
1212 * taking in account the tunnel mode and the status of all children and clients.
1216 * @return Maximum PID allowed.
1219 tunnel_get_fwd_ack (struct MeshTunnel *t);
1223 * Add a client to a tunnel, initializing all needed data structures.
1225 * @param t Tunnel to which add the client.
1226 * @param c Client which to add to the tunnel.
1229 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1233 * Jump to the next edge, with the longest matching token.
1235 * @param block Block found in the DHT.
1236 * @param size Size of the block.
1237 * @param ctx Context of the search.
1239 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1242 regex_next_edge (const struct MeshRegexBlock *block,
1244 struct MeshRegexSearchContext *ctx);
1248 * Find a path to a peer that offers a regex servcie compatible
1249 * with a given string.
1251 * @param key The key of the accepting state.
1252 * @param ctx Context containing info about the string, tunnel, etc.
1255 regex_find_path (const struct GNUNET_HashCode *key,
1256 struct MeshRegexSearchContext *ctx);
1260 * @brief Queue and pass message to core when possible.
1262 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1263 * and accounts for it. In case the queue is full, the message is dropped and
1266 * Otherwise, message is treated as internal and allowed to go regardless of
1269 * @param cls Closure (@c type dependant). It will be used by queue_send to
1270 * build the message to be sent if not already prebuilt.
1271 * @param type Type of the message, 0 for a raw message.
1272 * @param size Size of the message.
1273 * @param dst Neighbor to send message to.
1274 * @param t Tunnel this message belongs to.
1277 queue_add (void *cls, uint16_t type, size_t size,
1278 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1282 * Free a transmission that was already queued with all resources
1283 * associated to the request.
1285 * @param queue Queue handler to cancel.
1286 * @param clear_cls Is it necessary to free associated cls?
1289 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1293 * @brief Get the next transmittable message from the queue.
1295 * This will be the head, except in the case of being a data packet
1296 * not allowed by the destination peer.
1298 * @param peer Destination peer.
1300 * @return The next viable MeshPeerQueue element to send to that peer.
1301 * NULL when there are no transmittable messages.
1303 struct MeshPeerQueue *
1304 queue_get_next (const struct MeshPeerInfo *peer);
1308 * Core callback to write a queued packet to core buffer
1310 * @param cls Closure (peer info).
1311 * @param size Number of bytes available in buf.
1312 * @param buf Where the to write the message.
1314 * @return number of bytes written to buf
1317 queue_send (void *cls, size_t size, void *buf);
1319 /******************************************************************************/
1320 /************************ ITERATORS ****************************/
1321 /******************************************************************************/
1324 * Iterator over found existing mesh regex blocks that match an ongoing search.
1326 * @param cls closure
1327 * @param key current key code
1328 * @param value value in the hash map
1329 * @return GNUNET_YES if we should continue to iterate,
1333 regex_result_iterator (void *cls,
1334 const struct GNUNET_HashCode * key,
1337 struct MeshRegexBlock *block = value;
1338 struct MeshRegexSearchContext *ctx = cls;
1340 if (GNUNET_YES == ntohl(block->accepting) &&
1341 ctx->position == strlen (ctx->info->description))
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1344 regex_find_path (key, ctx);
1345 return GNUNET_YES; // We found an accept state!
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1350 ctx->position, strlen(ctx->info->description),
1351 ntohl(block->accepting));
1354 regex_next_edge(block, SIZE_MAX, ctx);
1356 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1363 * Iterator over edges in a regex block retrieved from the DHT.
1365 * @param cls Closure (context of the search).
1366 * @param token Token that follows to next state.
1367 * @param len Lenght of token.
1368 * @param key Hash of next state.
1370 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1373 regex_edge_iterator (void *cls,
1376 const struct GNUNET_HashCode *key)
1378 struct MeshRegexSearchContext *ctx = cls;
1379 struct MeshRegexSearchInfo *info = ctx->info;
1383 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1388 current = &info->description[ctx->position];
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1390 current_len = strlen (info->description) - ctx->position;
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* token : %.*s\n", len, token);
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1395 if (len > current_len)
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1398 return GNUNET_YES; // Token too long, wont match
1400 if (0 != strncmp (current, token, len))
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1403 return GNUNET_YES; // Token doesn't match
1406 if (len > ctx->longest_match)
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1409 ctx->longest_match = len;
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1423 * Jump to the next edge, with the longest matching token.
1425 * @param block Block found in the DHT.
1426 * @param size Size of the block.
1427 * @param ctx Context of the search.
1429 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1432 regex_next_edge (const struct MeshRegexBlock *block,
1434 struct MeshRegexSearchContext *ctx)
1436 struct MeshRegexSearchContext *new_ctx;
1437 struct MeshRegexSearchInfo *info = ctx->info;
1438 struct GNUNET_DHT_GetHandle *get_h;
1442 /* Find the longest match for the current string position,
1443 * among tokens in the given block */
1444 ctx->longest_match = 0;
1445 result = GNUNET_MESH_regex_block_iterate (block, size,
1446 ®ex_edge_iterator, ctx);
1447 GNUNET_break (GNUNET_OK == result);
1449 /* Did anything match? */
1450 if (0 == ctx->longest_match)
1453 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1454 new_ctx->info = info;
1455 new_ctx->position = ctx->position + ctx->longest_match;
1456 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1458 /* Check whether we already have a DHT GET running for it */
1460 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1463 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1465 ®ex_result_iterator,
1467 // FIXME: "leaks" new_ctx? avoid keeping it around?
1468 return; // We are already looking for it
1471 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1473 /* Start search in DHT */
1474 rest = &new_ctx->info->description[new_ctx->position];
1476 GNUNET_DHT_get_start (dht_handle, /* handle */
1477 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1478 &ctx->hash, /* key to search */
1479 dht_replication_level, /* replication level */
1480 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1482 // FIXME add BLOOMFILTER to exclude filtered peers
1483 strlen(rest) + 1, /* xquery bits */
1484 // FIXME add BLOOMFILTER SIZE
1485 &dht_get_string_handler, new_ctx);
1487 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1490 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1499 * Iterator over hash map entries to cancel DHT GET requests after a
1500 * successful connect_by_string.
1502 * @param cls Closure (unused).
1503 * @param key Current key code (unused).
1504 * @param value Value in the hash map (get handle).
1505 * @return GNUNET_YES if we should continue to iterate,
1509 regex_cancel_dht_get (void *cls,
1510 const struct GNUNET_HashCode * key,
1513 struct GNUNET_DHT_GetHandle *h = value;
1515 GNUNET_DHT_get_stop (h);
1521 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1522 * search for connect_by_string.
1524 * @param cls Closure (unused).
1525 * @param key Current key code (unused).
1526 * @param value MeshRegexBlock in the hash map.
1527 * @return GNUNET_YES if we should continue to iterate,
1531 regex_free_result (void *cls,
1532 const struct GNUNET_HashCode * key,
1536 GNUNET_free (value);
1542 * Find a path to a peer that offers a regex servcie compatible
1543 * with a given string.
1545 * @param key The key of the accepting state.
1546 * @param ctx Context containing info about the string, tunnel, etc.
1549 regex_find_path (const struct GNUNET_HashCode *key,
1550 struct MeshRegexSearchContext *ctx)
1552 struct GNUNET_DHT_GetHandle *get_h;
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1555 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1556 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1557 key, /* key to search */
1558 dht_replication_level, /* replication level */
1559 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE/* |
1560 GNUNET_DHT_RO_RECORD_ROUTE*/,
1562 // FIXME add BLOOMFILTER to exclude filtered peers
1563 0, /* xquery bits */
1564 // FIXME add BLOOMFILTER SIZE
1565 &dht_get_string_accept_handler, ctx);
1566 GNUNET_break (GNUNET_OK ==
1567 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1570 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1575 * Function called if the connect attempt to a peer found via
1576 * connect_by_string times out. Try to connect to another peer, if any.
1577 * Otherwise try to reconnect to the same peer.
1579 * @param cls Closure (info about regex search).
1580 * @param tc TaskContext.
1583 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1585 struct MeshRegexSearchInfo *info = cls;
1586 struct MeshPeerInfo *peer_info;
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1591 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1600 if (0 < info->n_peers)
1602 // Select next peer, put current in that spot.
1603 id = info->peers[info->i_peer];
1604 info->peers[info->i_peer] = info->peer;
1605 info->i_peer = (info->i_peer + 1) % info->n_peers;
1609 // Try to connect to same peer again.
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1614 peer_info = peer_info_get_short(id);
1615 tunnel_add_peer (info->t, peer_info);
1617 tunnel_delete_peer (info->t, old);
1618 peer_info_connect (peer_info, info->t);
1619 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1620 ®ex_connect_timeout,
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1627 * Cancel an ongoing regex search in the DHT and free all resources.
1629 * @param ctx The search context.
1632 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1634 struct MeshRegexSearchInfo *info = ctx->info;
1637 GNUNET_free (info->description);
1638 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1639 ®ex_cancel_dht_get, NULL);
1640 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1641 ®ex_free_result, NULL);
1642 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1643 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1644 info->t->regex_ctx = NULL;
1645 for (i = 0; i < info->n_contexts; i++)
1647 GNUNET_free (info->contexts[i]);
1649 if (0 < info->n_contexts)
1650 GNUNET_free (info->contexts);
1651 if (0 < info->n_peers)
1652 GNUNET_free (info->peers);
1653 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1655 GNUNET_SCHEDULER_cancel(info->timeout);
1661 /******************************************************************************/
1662 /************************ PERIODIC FUNCTIONS ****************************/
1663 /******************************************************************************/
1666 * Announce iterator over for each application provided by the peer
1668 * @param cls closure
1669 * @param key current key code
1670 * @param value value in the hash map
1671 * @return GNUNET_YES if we should continue to
1676 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1678 struct PBlock block;
1679 struct MeshClient *c;
1681 block.id = my_full_id;
1682 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1683 GNUNET_assert(NULL != c);
1684 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1685 if (0 == block.type)
1690 block.type = htonl (block.type);
1692 GNUNET_break (NULL !=
1693 GNUNET_DHT_put (dht_handle, key,
1694 dht_replication_level,
1695 GNUNET_DHT_RO_RECORD_ROUTE |
1696 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1697 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1699 (const char *) &block,
1700 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1701 app_announce_time, NULL, NULL));
1708 * Function called when the DHT regex put is complete.
1710 * @param the 'struct MeshClient' for which we were PUTting
1711 * @param success GNUNET_OK if the PUT was transmitted,
1712 * GNUNET_NO on timeout,
1713 * GNUNET_SYSERR on disconnect from service
1714 * after the PUT message was transmitted
1715 * (so we don't know if it was received or not)
1718 announce_regex_done (void *cls,
1721 struct MeshClient *c = cls;
1728 * Regex callback iterator to store own service description in the DHT.
1730 * @param cls closure.
1731 * @param key hash for current state.
1732 * @param proof proof for current state.
1733 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1734 * @param num_edges number of edges leaving current state.
1735 * @param edges edges leaving current state.
1738 regex_iterator (void *cls,
1739 const struct GNUNET_HashCode *key,
1742 unsigned int num_edges,
1743 const struct GNUNET_REGEX_Edge *edges)
1745 struct MeshRegexBlock *block;
1746 struct MeshRegexEdge *block_edge;
1747 enum GNUNET_DHT_RouteOption opt;
1751 unsigned int offset;
1754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1755 " regex dht put for state %s\n",
1757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1764 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1765 if (GNUNET_YES == accepting)
1767 struct MeshRegexAccept block;
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 " state %s is accepting, putting own id\n",
1772 size = sizeof (block);
1774 block.id = my_full_id;
1775 GNUNET_STATISTICS_update (stats, "# regex accepting blocks stored",
1777 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes stored",
1778 sizeof (block), GNUNET_NO);
1780 GNUNET_DHT_put(dht_handle, key,
1781 dht_replication_level,
1782 opt/* | GNUNET_DHT_RO_RECORD_ROUTE*/,
1783 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1786 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: expiration time should be option */
1790 len = strlen(proof);
1791 size = sizeof (struct MeshRegexBlock) + len;
1792 block = GNUNET_malloc (size);
1795 block->n_proof = htonl (len);
1796 block->n_edges = htonl (num_edges);
1797 block->accepting = htonl (accepting);
1799 /* Store the proof at the end of the block. */
1800 aux = (char *) &block[1];
1801 memcpy (aux, proof, len);
1804 /* Store each edge in a variable length MeshEdge struct at the
1805 * very end of the MeshRegexBlock structure.
1807 for (i = 0; i < num_edges; i++)
1809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1810 " edge %s towards %s\n",
1812 GNUNET_h2s(&edges[i].destination));
1814 /* aux points at the end of the last block */
1815 len = strlen (edges[i].label);
1816 size += sizeof (struct MeshRegexEdge) + len;
1817 // Calculate offset FIXME is this ok? use size instead?
1818 offset = aux - (char *) block;
1819 block = GNUNET_realloc (block, size);
1820 aux = &((char *) block)[offset];
1821 block_edge = (struct MeshRegexEdge *) aux;
1822 block_edge->key = edges[i].destination;
1823 block_edge->n_token = htonl (len);
1824 aux = (char *) &block_edge[1];
1825 memcpy (aux, edges[i].label, len);
1829 GNUNET_DHT_put(dht_handle, key,
1830 dht_replication_level,
1832 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1834 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1837 GNUNET_STATISTICS_update (stats, "# regex blocks stored",
1839 GNUNET_STATISTICS_update (stats, "# regex block bytes stored",
1842 GNUNET_free (block);
1847 * Store the regular expression describing a local service into the DHT.
1849 * @param regex The regular expresion.
1852 regex_put (struct MeshRegexDescriptor *regex)
1854 if (NULL == regex->dfa)
1856 regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1857 strlen (regex->regex),
1858 regex->compression);
1861 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1862 GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL);
1863 DEBUG_DHT (" regex_put (%s) end\n", regex->regex);
1868 * Periodically announce what applications are provided by local clients
1871 * @param cls closure
1872 * @param tc task context
1875 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1877 struct MeshClient *c = cls;
1880 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1881 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1883 DEBUG_DHT ("Starting PUT for regex\n");
1884 for (i = 0; i < c->n_regex; i++)
1885 regex_put (&c->regexes[i]);
1886 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1889 DEBUG_DHT ("Finished PUT for regex\n");
1894 * Periodically announce what applications are provided by local clients
1897 * @param cls closure
1898 * @param tc task context
1901 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1903 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1905 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1909 DEBUG_DHT ("Starting PUT for apps\n");
1911 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1913 announce_applications_task =
1914 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1916 DEBUG_DHT ("Finished PUT for apps\n");
1921 * Periodically announce self id in the DHT
1923 * @param cls closure
1924 * @param tc task context
1927 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1929 struct PBlock block;
1931 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1933 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1937 * - Set data expiration in function of X
1938 * - Adapt X to churn
1940 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1942 block.id = my_full_id;
1943 block.type = htonl (0);
1944 GNUNET_DHT_put (dht_handle, /* DHT handle */
1945 &my_full_id.hashPubKey, /* Key to use */
1946 dht_replication_level, /* Replication level */
1947 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1948 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1949 sizeof (block), /* Size of the data */
1950 (const char *) &block, /* Data itself */
1951 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1952 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1953 NULL, /* Continuation */
1954 NULL); /* Continuation closure */
1956 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1960 /******************************************************************************/
1961 /****************** GENERAL HELPER FUNCTIONS ************************/
1962 /******************************************************************************/
1965 * Decrements the reference counter and frees all resources if needed
1967 * @param mesh_data Data Descriptor used in a multicast message.
1968 * Freed no longer needed (last message).
1971 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1973 if (0 == --(mesh_data->reference_counter))
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1976 GNUNET_free (mesh_data->data);
1977 GNUNET_free (mesh_data);
1983 * Check if client has registered with the service and has not disconnected
1985 * @param client the client to check
1987 * @return non-NULL if client exists in the global DLL
1989 static struct MeshClient *
1990 client_get (struct GNUNET_SERVER_Client *client)
1992 struct MeshClient *c;
1997 if (c->handle == client)
2006 * Checks if a given client has subscribed to certain message type
2008 * @param message_type Type of message to check
2009 * @param c Client to check
2011 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
2013 * FIXME: use of crypto_hash slows it down
2014 * The hash function alone takes 8-10us out of the ~55us for the whole
2015 * process of retransmitting the message from one local client to another.
2016 * Find faster implementation!
2019 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
2021 struct GNUNET_HashCode hc;
2023 if (NULL == c->types)
2026 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
2027 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2032 * Check whether client wants traffic from a tunnel.
2034 * @param c Client to check.
2035 * @param t Tunnel to be found.
2037 * @return GNUNET_YES if client knows tunnel.
2039 * TODO look in client hashmap
2042 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2046 for (i = 0; i < t->nclients; i++)
2047 if (t->clients[i] == c)
2054 * Check whether client has been informed about a tunnel.
2056 * @param c Client to check.
2057 * @param t Tunnel to be found.
2059 * @return GNUNET_YES if client knows tunnel.
2061 * TODO look in client hashmap
2064 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2068 for (i = 0; i < t->nignore; i++)
2069 if (t->ignore[i] == c)
2071 return client_wants_tunnel(c, t);
2076 * Marks a client as uninterested in traffic from the tunnel, updating both
2077 * client and tunnel to reflect this.
2079 * @param c Client that doesn't want traffic anymore.
2080 * @param t Tunnel which should be ignored.
2082 * FIXME when to delete an incoming tunnel?
2085 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2087 struct GNUNET_HashCode hash;
2089 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2090 GNUNET_break (GNUNET_YES ==
2091 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2093 GNUNET_break (GNUNET_YES ==
2094 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2095 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2096 tunnel_delete_active_client (t, c);
2097 GNUNET_array_append (t->ignore, t->nignore, c);
2102 * Deletes a tunnel from a client (either owner or destination). To be used on
2103 * tunnel destroy, otherwise, use client_ignore_tunnel.
2105 * @param c Client whose tunnel to delete.
2106 * @param t Tunnel which should be deleted.
2109 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2111 struct GNUNET_HashCode hash;
2115 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2116 GNUNET_assert (GNUNET_YES ==
2117 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2123 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2125 GNUNET_assert (GNUNET_YES ==
2126 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2130 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2138 * Send the message to all clients that have subscribed to its type
2140 * @param msg Pointer to the message itself
2141 * @param payload Pointer to the payload of the message.
2142 * @param t The tunnel to whose clients this message goes.
2144 * @return number of clients this message was sent to
2147 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2148 const struct GNUNET_MessageHeader *payload,
2149 struct MeshTunnel *t)
2151 struct MeshClient *c;
2152 MESH_TunnelNumber *tid;
2155 char cbuf[htons (msg->size)];
2157 type = ntohs (payload->type);
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2160 GNUNET_MESH_DEBUG_M2S (type));
2162 memcpy (cbuf, msg, sizeof (cbuf));
2163 switch (htons (msg->type))
2165 struct GNUNET_MESH_Unicast *uc;
2166 struct GNUNET_MESH_Multicast *mc;
2167 struct GNUNET_MESH_ToOrigin *to;
2169 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2170 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2173 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2174 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2177 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2178 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2186 for (count = 0, c = clients; c != NULL; c = c->next)
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2189 if (client_is_subscribed (type, c))
2191 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2195 *tid = htonl (t->local_tid);
2199 if (GNUNET_NO == client_knows_tunnel (c, t))
2201 /* This client doesn't know the tunnel */
2202 struct GNUNET_MESH_TunnelNotification tmsg;
2203 struct GNUNET_HashCode hash;
2205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2206 tmsg.header.size = htons (sizeof (tmsg));
2207 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2208 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2209 tmsg.tunnel_id = htonl (t->local_tid_dest);
2211 if (GNUNET_YES == t->speed_min)
2212 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2213 if (GNUNET_YES == t->nobuffer)
2214 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2215 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2216 &tmsg.header, GNUNET_NO);
2217 tunnel_add_client (t, c);
2218 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2220 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2221 c->incoming_tunnels, &hash, t,
2222 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2224 *tid = htonl (t->local_tid_dest);
2227 /* Check if the client wants to get traffic from the tunnel */
2228 if (GNUNET_NO == client_wants_tunnel(c, t))
2231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2232 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2233 (struct GNUNET_MessageHeader
2234 *) cbuf, GNUNET_NO);
2243 * Notify the client that owns the tunnel that a peer has connected to it
2244 * (the requested path to it has been confirmed).
2246 * @param t Tunnel whose owner to notify
2247 * @param id Short id of the peer that has connected
2250 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2252 struct GNUNET_MESH_PeerControl pc;
2254 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2255 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2256 pc.tunnel_id = htonl (t->local_tid);
2257 GNUNET_PEER_resolve (id, &pc.peer);
2258 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2264 * Notify all clients (not depending on registration status) that the incoming
2265 * tunnel is no longer valid.
2267 * @param t Tunnel that was destroyed.
2270 send_clients_tunnel_destroy (struct MeshTunnel *t)
2272 struct GNUNET_MESH_TunnelMessage msg;
2274 msg.header.size = htons (sizeof (msg));
2275 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2276 msg.tunnel_id = htonl (t->local_tid_dest);
2277 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2282 * Notify clients of tunnel disconnections, if needed.
2283 * In case the origin disconnects, the destination clients get a tunnel destroy
2284 * notification. If the last destination disconnects (only one remaining client
2285 * in tunnel), the origin gets a (local ID) peer disconnected.
2286 * Note that the function must be called BEFORE removing the client from
2289 * @param t Tunnel that was destroyed.
2290 * @param c Client that disconnected.
2293 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2299 struct GNUNET_MESH_TunnelMessage msg;
2301 msg.header.size = htons (sizeof (msg));
2302 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2303 msg.tunnel_id = htonl (t->local_tid_dest);
2304 for (i = 0; i < t->nclients; i++)
2305 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2306 &msg.header, GNUNET_NO);
2308 // FIXME when to disconnect an incoming tunnel?
2309 else if (1 == t->nclients && NULL != t->owner)
2311 struct GNUNET_MESH_PeerControl msg;
2313 msg.header.size = htons (sizeof (msg));
2314 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2315 msg.tunnel_id = htonl (t->local_tid);
2316 msg.peer = my_full_id;
2317 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2318 &msg.header, GNUNET_NO);
2324 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2325 * and insert it in the appropiate structures if the peer is not known yet.
2327 * @param peer Full identity of the peer.
2329 * @return Existing or newly created peer info.
2331 static struct MeshPeerInfo *
2332 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2334 struct MeshPeerInfo *peer_info;
2336 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2337 if (NULL == peer_info)
2340 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2341 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2342 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2343 peer_info->id = GNUNET_PEER_intern (peer);
2351 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2352 * and insert it in the appropiate structures if the peer is not known yet.
2354 * @param peer Short identity of the peer.
2356 * @return Existing or newly created peer info.
2358 static struct MeshPeerInfo *
2359 peer_info_get_short (const GNUNET_PEER_Id peer)
2361 struct GNUNET_PeerIdentity id;
2363 GNUNET_PEER_resolve (peer, &id);
2364 return peer_info_get (&id);
2369 * Iterator to remove the tunnel from the list of tunnels a peer participates
2372 * @param cls Closure (tunnel info)
2373 * @param key GNUNET_PeerIdentity of the peer (unused)
2374 * @param value PeerInfo of the peer
2376 * @return always GNUNET_YES, to keep iterating
2379 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2381 struct MeshTunnel *t = cls;
2382 struct MeshPeerInfo *peer = value;
2385 for (i = 0; i < peer->ntunnels; i++)
2388 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2391 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2392 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2401 * Core callback to write a pre-constructed data packet to core buffer
2403 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2404 * @param size Number of bytes available in buf.
2405 * @param buf Where the to write the message.
2407 * @return number of bytes written to buf
2410 send_core_data_raw (void *cls, size_t size, void *buf)
2412 struct MeshTransmissionDescriptor *info = cls;
2413 struct GNUNET_MessageHeader *msg;
2416 GNUNET_assert (NULL != info);
2417 GNUNET_assert (NULL != info->mesh_data);
2418 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2419 total_size = ntohs (msg->size);
2421 if (total_size > size)
2426 memcpy (buf, msg, total_size);
2427 data_descriptor_decrement_rc (info->mesh_data);
2434 * Sends an already built non-multicast message to a peer,
2435 * properly registrating all used resources.
2437 * @param message Message to send. Function makes a copy of it.
2438 * @param peer Short ID of the neighbor whom to send the message.
2439 * @param t Tunnel on which this message is transmitted.
2442 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2443 const struct GNUNET_PeerIdentity *peer,
2444 struct MeshTunnel *t)
2446 struct MeshTransmissionDescriptor *info;
2447 struct MeshPeerInfo *neighbor;
2448 struct MeshPeerPath *p;
2452 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2454 size = ntohs (message->size);
2455 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2456 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2457 info->mesh_data->data = GNUNET_malloc (size);
2458 memcpy (info->mesh_data->data, message, size);
2459 type = ntohs(message->type);
2462 struct GNUNET_MESH_Unicast *m;
2463 struct GNUNET_MESH_ToOrigin *to;
2465 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2466 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2467 m->ttl = htonl (ntohl (m->ttl) - 1);
2469 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2470 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2472 to->pid = htonl(t->bck_pid);
2474 info->mesh_data->data_len = size;
2475 info->mesh_data->reference_counter = 1;
2476 info->mesh_data->total_out = 1;
2477 neighbor = peer_info_get (peer);
2478 for (p = neighbor->path_head; NULL != p; p = p->next)
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 " %s IS NOT DIRECTLY CONNECTED\n",
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2494 for (p = neighbor->path_head; NULL != p; p = p->next)
2496 struct GNUNET_PeerIdentity debug_id;
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 " path with %u hops through:\n",
2502 for (i = 0; i < p->length; i++)
2504 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2507 i, GNUNET_i2s(&debug_id));
2511 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2512 GNUNET_free (info->mesh_data->data);
2513 GNUNET_free (info->mesh_data);
2517 info->peer = neighbor;
2518 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2529 * Sends a CREATE PATH message for a path to a peer, properly registrating
2530 * all used resources.
2532 * @param peer PeerInfo of the final peer for whom this path is being created.
2533 * @param p Path itself.
2534 * @param t Tunnel for which the path is created.
2537 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2538 struct MeshTunnel *t)
2540 struct GNUNET_PeerIdentity id;
2541 struct MeshPathInfo *path_info;
2542 struct MeshPeerInfo *neighbor;
2548 p = tree_get_path_to_peer (t->tree, peer->id);
2555 for (i = 0; i < p->length; i++)
2557 if (p->peers[i] == myid)
2560 if (i >= p->length - 1)
2566 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2568 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2569 path_info->path = p;
2571 neighbor = peer_info_get (&id);
2572 path_info->peer = neighbor;
2573 queue_add (path_info,
2574 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2575 sizeof (struct GNUNET_MESH_ManipulatePath) +
2576 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2583 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2585 * @param t Tunnel whose path to destroy.
2586 * @param destination Short ID of the peer to whom the path to destroy.
2589 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2591 struct MeshPeerPath *p;
2594 p = tree_get_path_to_peer (t->tree, destination);
2600 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2601 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2603 struct GNUNET_MESH_ManipulatePath *msg;
2604 struct GNUNET_PeerIdentity *pi;
2608 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2609 msg->header.size = htons (size);
2610 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2611 msg->tid = htonl (t->id.tid);
2612 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2613 for (i = 0; i < p->length; i++)
2615 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2617 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2624 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2626 * @param t Tunnel which to confirm.
2629 send_path_ack (struct MeshTunnel *t)
2631 struct MeshTransmissionDescriptor *info;
2632 struct GNUNET_PeerIdentity id;
2633 GNUNET_PEER_Id peer;
2635 peer = tree_get_predecessor (t->tree);
2636 GNUNET_PEER_resolve (peer, &id);
2637 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2638 info->origin = &t->id;
2639 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2640 GNUNET_assert (NULL != info->peer);
2643 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2644 sizeof (struct GNUNET_MESH_PathACK),
2651 * Try to establish a new connection to this peer.
2652 * Use the best path for the given tunnel.
2653 * If the peer doesn't have any path to it yet, try to get one.
2654 * If the peer already has some path, send a CREATE PATH towards it.
2656 * @param peer PeerInfo of the peer.
2657 * @param t Tunnel for which to create the path, if possible.
2660 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2662 struct MeshPeerPath *p;
2663 struct MeshPathInfo *path_info;
2665 if (NULL != peer->path_head)
2667 p = tree_get_path_to_peer (t->tree, peer->id);
2674 // FIXME always send create path to self
2677 send_create_path (peer, p, t);
2681 struct GNUNET_HashCode hash;
2684 send_client_peer_connected (t, myid);
2685 t->local_tid_dest = next_local_tid++;
2686 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2689 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2690 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2697 else if (NULL == peer->dhtget)
2699 struct GNUNET_PeerIdentity id;
2701 GNUNET_PEER_resolve (peer->id, &id);
2702 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2703 path_info->peer = peer;
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2706 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2707 peer->dhtgetcls = path_info;
2708 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2709 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2710 &id.hashPubKey, /* key to search */
2711 dht_replication_level, /* replication level */
2712 GNUNET_DHT_RO_RECORD_ROUTE |
2713 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2714 NULL, /* xquery */ // FIXME BLOOMFILTER
2715 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2716 &dht_get_id_handler, path_info);
2718 /* Otherwise, there is no path but the DHT get is already started. */
2723 * Task to delay the connection of a peer
2725 * @param cls Closure (path info with tunnel and peer to connect).
2726 * Will be free'd on exection.
2727 * @param tc TaskContext
2730 peer_info_connect_task (void *cls,
2731 const struct GNUNET_SCHEDULER_TaskContext *tc)
2733 struct MeshPathInfo *path_info = cls;
2735 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2737 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2742 peer_info_connect (path_info->peer, path_info->t);
2748 * Destroy the peer_info and free any allocated resources linked to it
2750 * @param pi The peer_info to destroy.
2752 * @return GNUNET_OK on success
2755 peer_info_destroy (struct MeshPeerInfo *pi)
2757 struct GNUNET_PeerIdentity id;
2758 struct MeshPeerPath *p;
2759 struct MeshPeerPath *nextp;
2761 GNUNET_PEER_resolve (pi->id, &id);
2762 GNUNET_PEER_change_rc (pi->id, -1);
2765 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2769 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2771 if (NULL != pi->dhtget)
2773 GNUNET_DHT_get_stop (pi->dhtget);
2774 GNUNET_free (pi->dhtgetcls);
2780 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2784 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2786 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2794 * Remove all paths that rely on a direct connection between p1 and p2
2795 * from the peer itself and notify all tunnels about it.
2797 * @param peer PeerInfo of affected peer.
2798 * @param p1 GNUNET_PEER_Id of one peer.
2799 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2802 * TODO: optimize (see below)
2805 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2808 struct MeshPeerPath *p;
2809 struct MeshPeerPath *aux;
2810 struct MeshPeerInfo *peer_d;
2812 unsigned int destroyed;
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2819 p = peer->path_head;
2823 for (i = 0; i < (p->length - 1); i++)
2825 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2826 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2828 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2839 for (i = 0; i < peer->ntunnels; i++)
2841 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2845 * Problem: one or more peers have been deleted from the tunnel tree.
2846 * We don't know who they are to try to add them again.
2847 * We need to try to find a new path for each of the disconnected peers.
2848 * Some of them might already have a path to reach them that does not
2849 * involve p1 and p2. Adding all anew might render in a better tree than
2850 * the trivial immediate fix.
2852 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2853 * its children will be reachable trough him.
2855 peer_d = peer_info_get_short (d);
2858 for (p = peer_d->path_head; NULL != p; p = p->next)
2860 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2868 /* No callback, as peer will be already disconnected and a connection
2869 * scheduled by tunnel_notify_connection_broken.
2871 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2875 peer_info_connect (peer_d, peer->tunnels[i]);
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2883 * Add the path to the peer and update the path used to reach it in case this
2886 * @param peer_info Destination peer to add the path to.
2887 * @param path New path to add. Last peer must be the peer in arg 1.
2888 * Path will be either used of freed if already known.
2889 * @param trusted Do we trust that this path is real?
2892 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2895 struct MeshPeerPath *aux;
2899 if ((NULL == peer_info) || (NULL == path))
2902 path_destroy (path);
2905 if (path->peers[path->length - 1] != peer_info->id)
2908 path_destroy (path);
2911 if (path->length <= 2 && GNUNET_NO == trusted)
2913 /* Only allow CORE to tell us about direct paths */
2914 path_destroy (path);
2917 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2918 for (l = 1; l < path->length; l++)
2920 if (path->peers[l] == myid)
2922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2923 for (l2 = 0; l2 < path->length - l; l2++)
2925 path->peers[l2] = path->peers[l + l2];
2930 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2935 struct GNUNET_PeerIdentity id;
2937 GNUNET_PEER_resolve (peer_info->id, &id);
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2939 path->length, GNUNET_i2s (&id));
2942 l = path_get_length (path);
2949 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2950 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2952 l2 = path_get_length (aux);
2955 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2956 peer_info->path_tail, aux, path);
2961 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2963 path_destroy (path);
2968 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2975 * Add the path to the origin peer and update the path used to reach it in case
2976 * this is the shortest.
2977 * The path is given in peer_info -> destination, therefore we turn the path
2978 * upside down first.
2980 * @param peer_info Peer to add the path to, being the origin of the path.
2981 * @param path New path to add after being inversed.
2982 * @param trusted Do we trust that this path is real?
2985 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2986 struct MeshPeerPath *path, int trusted)
2989 peer_info_add_path (peer_info, path, trusted);
2994 * Function called if the connection to the peer has been stalled for a while,
2995 * possibly due to a missed ACK. Poll the peer about its ACK status.
2997 * @param cls Closure (info about regex search).
2998 * @param tc TaskContext.
3001 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3003 struct MeshTunnelChildInfo *cinfo = cls;
3004 struct GNUNET_MESH_Poll msg;
3005 struct GNUNET_PeerIdentity id;
3006 struct MeshTunnel *t;
3008 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3009 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3015 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3016 msg.header.size = htons (sizeof (msg));
3017 msg.tid = htonl (t->id.tid);
3018 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3019 msg.last_ack = htonl (cinfo->fwd_ack);
3021 GNUNET_PEER_resolve (cinfo->id, &id);
3022 send_prebuilt_message (&msg.header, &id, cinfo->t);
3023 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
3024 &tunnel_poll, cinfo);
3029 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3030 * to obtain a local peer -> destination path and interning the peer ids.
3032 * @return Newly allocated and created path
3034 static struct MeshPeerPath *
3035 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3036 unsigned int get_path_length,
3037 const struct GNUNET_PeerIdentity *put_path,
3038 unsigned int put_path_length)
3040 struct MeshPeerPath *p;
3046 GNUNET_PEER_change_rc (myid, 1);
3047 i = get_path_length;
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3049 for (i--; i >= 0; i--)
3051 id = GNUNET_PEER_intern (&get_path[i]);
3052 if (p->length > 0 && id == p->peers[p->length - 1])
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3055 GNUNET_PEER_change_rc (id, -1);
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3060 GNUNET_i2s (&get_path[i]));
3062 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3063 p->peers[p->length - 1] = id;
3066 i = put_path_length;
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3068 for (i--; i >= 0; i--)
3070 id = GNUNET_PEER_intern (&put_path[i]);
3073 /* PUT path went through us, so discard the path up until now and start
3074 * from here to get a much shorter (and loop-free) path.
3079 if (p->length > 0 && id == p->peers[p->length - 1])
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3082 GNUNET_PEER_change_rc (id, -1);
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3087 GNUNET_i2s (&put_path[i]));
3089 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3090 p->peers[p->length - 1] = id;
3094 if (get_path_length > 0)
3095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3096 GNUNET_i2s (&get_path[0]));
3097 if (put_path_length > 0)
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3099 GNUNET_i2s (&put_path[0]));
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3102 for (i = 0; i < p->length; i++)
3104 struct GNUNET_PeerIdentity peer_id;
3106 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3108 GNUNET_i2s (&peer_id));
3116 * Adds a path to the peer_infos of all the peers in the path
3118 * @param p Path to process.
3119 * @param confirmed Whether we know if the path works or not.
3122 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3126 /* TODO: invert and add */
3127 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3128 for (i++; i < p->length; i++)
3130 struct MeshPeerInfo *aux;
3131 struct MeshPeerPath *copy;
3133 aux = peer_info_get_short (p->peers[i]);
3134 copy = path_duplicate (p);
3135 copy->length = i + 1;
3136 peer_info_add_path (aux, copy, GNUNET_NO);
3142 * Send keepalive packets for a peer
3144 * @param cls Closure (tunnel for which to send the keepalive).
3145 * @param tc Notification context.
3147 * TODO: implement explicit multicast keepalive?
3150 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3154 * Search for a tunnel among the incoming tunnels
3156 * @param tid the local id of the tunnel
3158 * @return tunnel handler, NULL if doesn't exist
3160 static struct MeshTunnel *
3161 tunnel_get_incoming (MESH_TunnelNumber tid)
3163 struct GNUNET_HashCode hash;
3165 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3166 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3167 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3172 * Search for a tunnel among the tunnels for a client
3174 * @param c the client whose tunnels to search in
3175 * @param tid the local id of the tunnel
3177 * @return tunnel handler, NULL if doesn't exist
3179 static struct MeshTunnel *
3180 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3182 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3184 return tunnel_get_incoming (tid);
3188 struct GNUNET_HashCode hash;
3190 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3191 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3197 * Search for a tunnel by global ID using PEER_ID
3199 * @param pi owner of the tunnel
3200 * @param tid global tunnel number
3202 * @return tunnel handler, NULL if doesn't exist
3204 static struct MeshTunnel *
3205 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3207 struct MESH_TunnelID id;
3208 struct GNUNET_HashCode hash;
3213 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3214 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3219 * Search for a tunnel by global ID using full PeerIdentities
3221 * @param oid owner of the tunnel
3222 * @param tid global tunnel number
3224 * @return tunnel handler, NULL if doesn't exist
3226 static struct MeshTunnel *
3227 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3229 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3234 * Delete an active client from the tunnel.
3240 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3244 for (i = 0; i < t->nclients; i++)
3246 if (t->clients[i] == c)
3248 t->clients[i] = t->clients[t->nclients - 1];
3249 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3250 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3252 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3260 * Delete an ignored client from the tunnel.
3266 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3270 for (i = 0; i < t->nignore; i++)
3272 if (t->ignore[i] == c)
3274 t->ignore[i] = t->ignore[t->nignore - 1];
3275 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3283 * Delete a client from the tunnel. It should be only done on
3284 * client disconnection, otherwise use client_ignore_tunnel.
3290 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3292 tunnel_delete_ignored_client (t, c);
3293 tunnel_delete_active_client (t, c);
3298 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3300 * Destroys queue elements of all waiting transmissions and frees all memory
3301 * used by the struct and its elements.
3303 * @param cls Closure (tunnel info).
3304 * @param key Hash of GNUNET_PEER_Id (unused).
3305 * @param value MeshTunnelChildInfo of the child.
3307 * @return always GNUNET_YES, to keep iterating
3310 tunnel_destroy_child (void *cls,
3311 const struct GNUNET_HashCode * key,
3314 struct MeshTunnelChildInfo *cinfo = value;
3315 struct MeshTunnel *t = cls;
3316 struct MeshPeerQueue *q;
3320 for (c = 0; c < cinfo->send_buffer_n; c++)
3322 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3323 q = cinfo->send_buffer[i];
3324 cinfo->send_buffer[i] = NULL;
3326 queue_destroy (q, GNUNET_YES);
3329 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3331 GNUNET_free_non_null (cinfo->send_buffer);
3332 GNUNET_free (cinfo);
3338 * Callback used to notify a client owner of a tunnel that a peer has
3339 * disconnected, most likely because of a path change.
3341 * @param cls Closure (tunnel this notification is about).
3342 * @param peer_id Short ID of disconnected peer.
3345 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3347 struct MeshTunnel *t = cls;
3348 struct MeshPeerInfo *peer;
3349 struct MeshPathInfo *path_info;
3351 if (NULL != t->owner && NULL != nc)
3353 struct GNUNET_MESH_PeerControl msg;
3355 msg.header.size = htons (sizeof (msg));
3356 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3357 msg.tunnel_id = htonl (t->local_tid);
3358 GNUNET_PEER_resolve (peer_id, &msg.peer);
3359 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3360 &msg.header, GNUNET_NO);
3362 peer = peer_info_get_short (peer_id);
3363 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3364 path_info->peer = peer;
3366 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3372 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3373 * needed rescources.
3374 * If peer already exists, reevaluate shortest path and change if different.
3376 * @param t Tunnel we want to add a new peer to
3377 * @param peer PeerInfo of the peer being added
3381 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3383 struct GNUNET_PeerIdentity id;
3384 struct MeshPeerPath *best_p;
3385 struct MeshPeerPath *p;
3386 unsigned int best_cost;
3389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3390 GNUNET_PEER_resolve (peer->id, &id);
3392 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3395 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3396 GNUNET_assert (GNUNET_OK ==
3397 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3399 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3402 if (NULL != (p = peer->path_head))
3405 best_cost = tree_get_path_cost (t->tree, p);
3408 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3415 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3416 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3417 t->path_refresh_task =
3418 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3422 /* Start a DHT get */
3423 peer_info_connect (peer, t);
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3429 * Add a path to a tunnel which we don't own, just to remember the next hop.
3430 * If destination node was already in the tunnel, the first hop information
3431 * will be replaced with the new path.
3433 * @param t Tunnel we want to add a new peer to
3434 * @param p Path to add
3435 * @param own_pos Position of local node in path.
3439 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3440 unsigned int own_pos)
3442 struct GNUNET_PeerIdentity id;
3444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3445 GNUNET_assert (0 != own_pos);
3446 tree_add_path (t->tree, p, NULL, NULL);
3447 if (own_pos < p->length - 1)
3449 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3450 tree_update_first_hops (t->tree, myid, &id);
3452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3456 * Add a client to a tunnel, initializing all needed data structures.
3458 * @param t Tunnel to which add the client.
3459 * @param c Client which to add to the tunnel.
3462 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3464 struct MeshTunnelClientInfo clinfo;
3466 GNUNET_array_append (t->clients, t->nclients, c);
3467 clinfo.fwd_ack = t->fwd_pid + 1;
3468 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3469 clinfo.fwd_pid = t->fwd_pid;
3470 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3472 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3477 * Notifies a tunnel that a connection has broken that affects at least
3478 * some of its peers. Sends a notification towards the root of the tree.
3479 * In case the peer is the owner of the tree, notifies the client that owns
3480 * the tunnel and tries to reconnect.
3482 * @param t Tunnel affected.
3483 * @param p1 Peer that got disconnected from p2.
3484 * @param p2 Peer that got disconnected from p1.
3486 * @return Short ID of the peer disconnected (either p1 or p2).
3487 * 0 if the tunnel remained unaffected.
3489 static GNUNET_PEER_Id
3490 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3496 tree_notify_connection_broken (t->tree, p1, p2,
3497 &tunnel_notify_client_peer_disconnected,
3499 if (myid != p1 && myid != p2)
3505 if (tree_get_predecessor (t->tree) != 0)
3507 /* We are the peer still connected, notify owner of the disconnection. */
3508 struct GNUNET_MESH_PathBroken msg;
3509 struct GNUNET_PeerIdentity neighbor;
3511 msg.header.size = htons (sizeof (msg));
3512 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3513 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3514 msg.tid = htonl (t->id.tid);
3515 msg.peer1 = my_full_id;
3516 GNUNET_PEER_resolve (pid, &msg.peer2);
3517 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3518 send_prebuilt_message (&msg.header, &neighbor, t);
3526 * Send a multicast packet to a neighbor.
3528 * @param cls Closure (Info about the multicast packet)
3529 * @param neighbor_id Short ID of the neighbor to send the packet to.
3532 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3534 struct MeshData *mdata = cls;
3535 struct MeshTransmissionDescriptor *info;
3536 struct GNUNET_PeerIdentity neighbor;
3537 struct GNUNET_MessageHeader *msg;
3539 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3541 info->mesh_data = mdata;
3542 (mdata->reference_counter) ++;
3543 info->destination = neighbor_id;
3544 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3546 GNUNET_i2s (&neighbor));
3547 info->peer = peer_info_get (&neighbor);
3548 GNUNET_assert (NULL != info->peer);
3549 msg = (struct GNUNET_MessageHeader *) mdata->data;
3552 info->mesh_data->data_len,
3559 * Queue a message in a tunnel in multicast, sending a copy to each child node
3560 * down the local one in the tunnel tree.
3562 * @param t Tunnel in which to send the data.
3563 * @param msg Message to be sent.
3566 tunnel_send_multicast (struct MeshTunnel *t,
3567 const struct GNUNET_MessageHeader *msg)
3569 struct MeshData *mdata;
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 " sending a multicast packet...\n");
3574 mdata = GNUNET_malloc (sizeof (struct MeshData));
3575 mdata->data_len = ntohs (msg->size);
3577 mdata->data = GNUNET_malloc (mdata->data_len);
3578 memcpy (mdata->data, msg, mdata->data_len);
3579 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3581 struct GNUNET_MESH_Multicast *mcast;
3583 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3584 if (t->fwd_queue_n >= t->fwd_queue_max)
3587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3589 " message from %s!\n",
3590 GNUNET_i2s(&mcast->oid));
3591 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3592 " message at %s!\n",
3593 GNUNET_i2s(&my_full_id));
3594 GNUNET_free (mdata->data);
3595 GNUNET_free (mdata);
3599 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3601 ntohl (mcast->ttl));
3605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3608 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3609 if (mdata->reference_counter == 0)
3611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3612 " no one to send data to\n");
3613 GNUNET_free (mdata->data);
3614 GNUNET_free (mdata);
3615 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3620 mdata->total_out = mdata->reference_counter;
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 " sending a multicast packet done\n");
3629 * Increase the SKIP value of all peers that
3630 * have not received a unicast message.
3632 * @param cls Closure (ID of the peer that HAS received the message).
3633 * @param key ID of the neighbor.
3634 * @param value Information about the neighbor.
3636 * @return GNUNET_YES to keep iterating.
3639 tunnel_add_skip (void *cls,
3640 const struct GNUNET_HashCode * key,
3643 struct GNUNET_PeerIdentity *neighbor = cls;
3644 struct MeshTunnelChildInfo *cinfo = value;
3646 /* TODO compare only pointers? key == neighbor? */
3647 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3657 * @brief Get neighbor's Flow Control information.
3659 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3660 * descendant of the local node in a certain tunnel.
3661 * If the info is not yet there (recently created path), creates the data struct
3662 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3665 * @param t Tunnel related.
3666 * @param peer Neighbor whose Flow Control info is needed.
3668 * @return Neighbor's Flow Control info.
3670 static struct MeshTunnelChildInfo *
3671 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3672 const struct GNUNET_PeerIdentity *peer)
3674 struct MeshTunnelChildInfo *cinfo;
3676 if (NULL == t->children_fc)
3679 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3685 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3686 cinfo->id = GNUNET_PEER_intern (peer);
3687 cinfo->skip = t->fwd_pid;
3690 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3691 cinfo->fwd_ack = t->fwd_pid + delta;
3692 cinfo->bck_ack = delta;
3693 cinfo->bck_pid = -1;
3695 cinfo->send_buffer =
3696 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3698 GNUNET_assert (GNUNET_OK ==
3699 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3702 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3709 * Get the Flow Control info of a client.
3711 * @param t Tunnel on which to look.
3712 * @param c Client whose ACK to get.
3714 * @return ACK value.
3716 static struct MeshTunnelClientInfo *
3717 tunnel_get_client_fc (struct MeshTunnel *t,
3718 struct MeshClient *c)
3722 for (i = 0; i < t->nclients; i++)
3724 if (t->clients[i] != c)
3726 return &t->clients_fc[i];
3729 return NULL; // avoid compiler / coverity complaints
3734 * Iterator to get the appropiate ACK value from all children nodes.
3736 * @param cls Closue (tunnel).
3737 * @param id Id of the child node.
3740 tunnel_get_child_fwd_ack (void *cls,
3743 struct GNUNET_PeerIdentity peer_id;
3744 struct MeshTunnelChildInfo *cinfo;
3745 struct MeshTunnelChildIteratorContext *ctx = cls;
3746 struct MeshTunnel *t = ctx->t;
3749 GNUNET_PEER_resolve (id, &peer_id);
3750 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3751 ack = cinfo->fwd_ack;
3754 if (GNUNET_NO == ctx->init)
3756 ctx->max_child_ack = ack;
3757 ctx->init = GNUNET_YES;
3760 if (GNUNET_YES == t->speed_min)
3762 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3766 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3773 * Get the maximum PID allowed to transmit to any
3774 * tunnel child of the local peer, depending on the tunnel
3775 * buffering/speed settings.
3779 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3782 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3784 struct MeshTunnelChildIteratorContext ctx;
3786 ctx.max_child_ack = 0;
3788 ctx.init = GNUNET_NO;
3789 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3791 if (0 == ctx.nchildren)
3793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3794 " tunnel has no children, no FWD ACK\n");
3798 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3799 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3801 return (int64_t) ctx.max_child_ack;
3806 * Set the FWD ACK value of a client in a particular tunnel.
3808 * @param t Tunnel affected.
3809 * @param c Client whose ACK to set.
3810 * @param ack ACK value.
3813 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3814 struct MeshClient *c,
3819 for (i = 0; i < t->nclients; i++)
3821 if (t->clients[i] != c)
3823 t->clients_fc[i].fwd_ack = ack;
3831 * Get the highest ACK value of all clients in a particular tunnel,
3832 * according to the buffering/speed settings.
3834 * @param t Tunnel on which to look.
3836 * @return Corresponding ACK value (max uint32_t).
3837 * If no clients are suscribed, -1LL.
3840 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3845 if (0 == t->nclients)
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 " tunnel has no clients, no FWD ACK\n");
3852 for (ack = -1LL, i = 0; i < t->nclients; i++)
3855 (GNUNET_YES == t->speed_min &&
3856 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3857 (GNUNET_NO == t->speed_min &&
3858 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3860 ack = t->clients_fc[i].fwd_ack;
3864 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3865 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3867 return (uint32_t) ack;
3872 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3873 * mode and the status of all children nodes.
3877 * @return Maximum PID allowed.
3880 tunnel_get_fwd_ack (struct MeshTunnel *t)
3884 uint32_t buffer_free;
3888 count = t->fwd_pid - t->skip;
3889 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3890 child_ack = tunnel_get_children_fwd_ack (t);
3891 client_ack = tunnel_get_clients_fwd_ack (t);
3892 if (GNUNET_YES == t->nobuffer)
3895 if (-1LL == child_ack)
3896 child_ack = client_ack;
3897 if (-1LL == child_ack)
3900 client_ack = child_ack = ack;
3905 ack = count + buffer_free; // Overflow? OK!
3907 if (-1LL == child_ack)
3909 // Node has no children, child_ack AND core buffer are irrelevant.
3910 if (-1LL == client_ack) // No children AND no clients? Not good!
3912 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3916 return (uint32_t) client_ack;
3918 if (-1LL == client_ack)
3922 if (GNUNET_YES == t->speed_min)
3924 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3925 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3929 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3930 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3933 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3934 count, buffer_free, child_ack, client_ack, ack);
3940 * Build a local ACK message and send it to a local client.
3942 * @param t Tunnel on which to send the ACK.
3943 * @param c Client to whom send the ACK.
3944 * @param ack Value of the ACK.
3947 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3949 struct GNUNET_MESH_LocalAck msg;
3951 msg.header.size = htons (sizeof (msg));
3952 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3953 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3954 msg.max_pid = htonl (ack);
3955 GNUNET_SERVER_notification_context_unicast(nc,
3962 * Build an ACK message and queue it to send to the given peer.
3964 * @param t Tunnel on which to send the ACK.
3965 * @param peer Peer to whom send the ACK.
3966 * @param ack Value of the ACK.
3969 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3971 struct GNUNET_MESH_ACK msg;
3973 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3974 msg.header.size = htons (sizeof (msg));
3975 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3976 msg.pid = htonl (ack);
3977 msg.tid = htonl (t->id.tid);
3979 send_prebuilt_message (&msg.header, peer, t);
3984 * Notify a the owner of a tunnel about how many more
3985 * payload packages will we accept on a given tunnel.
3987 * @param t Tunnel on which to send the ACK.
3990 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3995 "Sending client FWD ACK on tunnel %X\n",
3998 ack = tunnel_get_fwd_ack (t);
4000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4001 if (t->last_fwd_ack == ack)
4003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
4007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
4008 t->last_fwd_ack = ack;
4009 send_local_ack (t, t->owner, ack);
4014 * Send an ACK informing the predecessor about the available buffer space.
4015 * In case there is no predecessor, inform the owning client.
4016 * If buffering is off, send only on behalf of children or self if endpoint.
4017 * If buffering is on, send when sent to children and buffer space is free.
4018 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4019 * the ACK itself goes "back" (towards root).
4021 * @param t Tunnel on which to send the ACK.
4022 * @param type Type of message that triggered the ACK transmission.
4025 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4027 struct GNUNET_PeerIdentity id;
4030 if (NULL != t->owner)
4032 tunnel_send_client_fwd_ack (t);
4035 /* Is it after unicast / multicast retransmission? */
4038 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4039 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4041 "ACK due to FWD DATA retransmission\n");
4042 if (GNUNET_YES == t->nobuffer)
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4048 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4049 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4051 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4052 t->force_ack = GNUNET_YES;
4058 /* Check if we need to transmit the ACK */
4059 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4060 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4061 GNUNET_NO == t->force_ack)
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4065 " t->qmax: %u, t->qn: %u\n",
4066 t->fwd_queue_max, t->fwd_queue_n);
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4068 " t->pid: %u, t->ack: %u\n",
4069 t->fwd_pid, t->last_fwd_ack);
4073 /* Ok, ACK might be necessary, what PID to ACK? */
4074 ack = tunnel_get_fwd_ack (t);
4076 /* If speed_min and not all children have ack'd, dont send yet */
4077 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4083 t->last_fwd_ack = ack;
4084 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4085 send_ack (t, &id, ack);
4087 t->force_ack = GNUNET_NO;
4092 * Iterator to send a child node a BCK ACK to allow him to send more
4095 * @param cls Closure (tunnel).
4096 * @param id Id of the child node.
4099 tunnel_send_child_bck_ack (void *cls,
4102 struct MeshTunnel *t = cls;
4103 struct MeshTunnelChildInfo *cinfo;
4104 struct GNUNET_PeerIdentity peer;
4107 GNUNET_PEER_resolve (id, &peer);
4108 cinfo = tunnel_get_neighbor_fc (t, &peer);
4109 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4111 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4114 " Not sending ACK, not needed\n");
4117 cinfo->bck_ack = ack;
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4120 " Sending BCK ACK %u (last sent: %u)\n",
4121 ack, cinfo->bck_ack);
4122 send_ack (t, &peer, ack);
4127 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4129 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4131 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4132 * - implement a size 1 buffer in each client_fc AND children_fc
4133 * to hold at least 1 message per "child".
4134 * problem: violates no buffer policy
4135 * - ack 0 and make "children" poll for transmission slots
4136 * problem: big overhead, extra latency even in low traffic
4139 * @param t Tunnel on which to send the BCK ACKs.
4142 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4145 unsigned int tunnel_delta;
4147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4149 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4152 /* Find client whom to allow to send to origin (with lowest buffer space) */
4153 for (i = 0; i < t->nclients; i++)
4155 struct MeshTunnelClientInfo *clinfo;
4158 clinfo = &t->clients_fc[i];
4159 delta = clinfo->bck_ack - clinfo->bck_pid;
4160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4161 t->clients[i]->id, delta);
4163 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4164 (GNUNET_YES == t->nobuffer && 0 == delta))
4168 ack = clinfo->bck_pid;
4169 ack += t->nobuffer ? 1 : tunnel_delta;
4170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4171 " sending ack to client %u: %u\n",
4172 t->clients[i]->id, ack);
4173 send_local_ack (t, t->clients[i], ack);
4174 clinfo->bck_ack = ack;
4178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4179 " not sending ack to client %u (td %u, d %u)\n",
4180 t->clients[i]->id, tunnel_delta, delta);
4187 * Send an ACK informing the children nodes and destination clients about
4188 * the available buffer space.
4189 * If buffering is off, send only on behalf of root (can be self).
4190 * If buffering is on, send when sent to predecessor and buffer space is free.
4191 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4192 * the ACK itself goes "forward" (towards children/clients).
4194 * @param t Tunnel on which to send the ACK.
4195 * @param type Type of message that triggered the ACK transmission.
4198 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4202 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4203 /* Is it after data to_origin retransmission? */
4206 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4207 if (GNUNET_YES == t->nobuffer)
4209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4210 " Not sending ACK, nobuffer\n");
4214 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4215 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4217 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4218 t->force_ack = GNUNET_YES;
4224 tunnel_send_clients_bck_ack (t);
4225 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4226 t->force_ack = GNUNET_NO;
4231 * @brief Re-initiate traffic to this peer if necessary.
4233 * Check if there is traffic queued towards this peer
4234 * and the core transmit handle is NULL (traffic was stalled).
4235 * If so, call core tmt rdy.
4237 * @param cls Closure (unused)
4238 * @param peer_id Short ID of peer to which initiate traffic.
4241 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4243 struct MeshPeerInfo *peer;
4244 struct GNUNET_PeerIdentity id;
4245 struct MeshPeerQueue *q;
4248 peer = peer_info_get_short(peer_id);
4249 if (NULL != peer->core_transmit)
4252 q = queue_get_next(peer);
4255 /* Might br multicast traffic already sent to this particular peer but
4256 * not to other children in this tunnel.
4257 * This way t->queue_n would be > 0 but the queue of this particular peer
4263 GNUNET_PEER_resolve (peer->id, &id);
4264 peer->core_transmit =
4265 GNUNET_CORE_notify_transmit_ready(core_handle,
4268 GNUNET_TIME_UNIT_FOREVER_REL,
4278 * @brief Allow transmission of FWD traffic on this tunnel
4280 * Check if there is traffic queued towards any children
4281 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4283 * @param t Tunnel on which to unlock FWD traffic.
4286 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4288 if (0 == t->fwd_queue_n)
4291 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4296 * @brief Allow transmission of BCK traffic on this tunnel
4298 * Check if there is traffic queued towards the root of the tree
4299 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4301 * @param t Tunnel on which to unlock BCK traffic.
4304 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4306 if (0 == t->bck_queue_n)
4309 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4314 * Send a message to all peers in this tunnel that the tunnel is no longer
4317 * @param t The tunnel whose peers to notify.
4320 tunnel_send_destroy (struct MeshTunnel *t)
4322 struct GNUNET_MESH_TunnelDestroy msg;
4324 msg.header.size = htons (sizeof (msg));
4325 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4326 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4327 msg.tid = htonl (t->id.tid);
4328 tunnel_send_multicast (t, &msg.header);
4333 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4335 * @param cls Closure (Tunnel which to cancel).
4336 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4339 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4341 struct MeshTunnel *t = cls;
4342 struct MeshPeerInfo *peer_info;
4343 struct MeshPeerQueue *pq;
4344 struct MeshPeerQueue *next;
4346 peer_info = peer_info_get_short (neighbor_id);
4347 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4350 if (pq->tunnel == t)
4352 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4353 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4354 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4356 // Should have been removed on destroy children
4359 queue_destroy (pq, GNUNET_YES);
4362 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4364 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4365 peer_info->core_transmit = NULL;
4370 * Destroy the tunnel and free any allocated resources linked to it.
4372 * @param t the tunnel to destroy
4374 * @return GNUNET_OK on success
4377 tunnel_destroy (struct MeshTunnel *t)
4379 struct MeshClient *c;
4380 struct GNUNET_HashCode hash;
4391 struct GNUNET_PeerIdentity id;
4393 GNUNET_PEER_resolve (t->id.oid, &id);
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4395 GNUNET_i2s (&id), t->id.tid);
4397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4401 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4402 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4410 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4412 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4419 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4420 for (i = 0; i < t->nclients; i++)
4424 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4430 for (i = 0; i < t->nignore; i++)
4434 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4441 if (t->nclients > 0)
4444 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4449 GNUNET_free (t->clients);
4450 GNUNET_free (t->clients_fc);
4453 if (NULL != t->peers)
4455 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4457 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4460 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4461 &tunnel_destroy_child,
4463 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4464 t->children_fc = NULL;
4466 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4467 tree_destroy (t->tree);
4469 if (NULL != t->regex_ctx)
4470 regex_cancel_search (t->regex_ctx);
4471 if (NULL != t->dht_get_type)
4472 GNUNET_DHT_get_stop (t->dht_get_type);
4473 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4474 GNUNET_SCHEDULER_cancel (t->timeout_task);
4475 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4476 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4479 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4486 * Create a new tunnel
4488 * @param owner Who is the owner of the tunnel (short ID).
4489 * @param tid Tunnel Number of the tunnel.
4490 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4491 * @param local Tunnel Number for the tunnel, for the client point of view.
4493 * @return A new initialized tunnel. NULL on error.
4495 static struct MeshTunnel *
4496 tunnel_new (GNUNET_PEER_Id owner,
4497 MESH_TunnelNumber tid,
4498 struct MeshClient *client,
4499 MESH_TunnelNumber local)
4501 struct MeshTunnel *t;
4502 struct GNUNET_HashCode hash;
4504 if (n_tunnels >= max_tunnels && NULL == client)
4507 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4510 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4511 t->bck_queue_max = t->fwd_queue_max;
4512 t->tree = tree_new (owner);
4514 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4515 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4516 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4517 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4518 t->local_tid = local;
4519 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4521 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4523 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4525 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4526 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4533 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4540 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4542 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4543 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4547 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4557 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4558 * that are no longer needed, as well as nodes of no longer reachable peers.
4559 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4561 * @param t Tunnel from which to remove the path.
4562 * @param peer Short id of the peer which should be removed.
4565 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4569 r = tree_del_peer (t->tree, peer, NULL, NULL);
4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4573 "Tunnel %u [%u] has no more nodes\n",
4574 t->id.oid, t->id.tid);
4580 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4581 * client when the client disconnects. If the client is not the owner, the
4582 * owner will get notified if no more clients are in the tunnel and the client
4583 * get removed from the tunnel's list.
4585 * @param cls closure (client that is disconnecting)
4586 * @param key the hash of the local tunnel id (used to access the hashmap)
4587 * @param value the value stored at the key (tunnel to destroy)
4589 * @return GNUNET_OK, keep iterating.
4592 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4594 struct MeshTunnel *t = value;
4595 struct MeshClient *c = cls;
4597 send_client_tunnel_disconnect(t, c);
4600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4601 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4602 tunnel_delete_client(t, c);
4603 client_delete_tunnel(c, t);
4606 tunnel_send_destroy(t);
4608 t->destroy = GNUNET_YES;
4615 * Timeout function, destroys tunnel if called
4617 * @param cls Closure (tunnel to destroy).
4618 * @param tc TaskContext
4621 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4623 struct MeshTunnel *t = cls;
4624 struct GNUNET_PeerIdentity id;
4626 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4627 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4629 GNUNET_PEER_resolve(t->id.oid, &id);
4630 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4631 "Tunnel %s [%X] timed out. Destroying.\n",
4632 GNUNET_i2s(&id), t->id.tid);
4633 send_clients_tunnel_destroy (t);
4638 * Resets the tunnel timeout. Starts it if no timeout was running.
4640 * @param t Tunnel whose timeout to reset.
4642 * TODO use heap to improve efficiency of scheduler.
4645 tunnel_reset_timeout (struct MeshTunnel *t)
4647 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4648 GNUNET_SCHEDULER_cancel (t->timeout_task);
4650 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4651 (refresh_path_time, 4), &tunnel_timeout, t);
4655 /******************************************************************************/
4656 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4657 /******************************************************************************/
4660 * Function to send a create path packet to a peer.
4662 * @param cls closure
4663 * @param size number of bytes available in buf
4664 * @param buf where the callee should write the message
4665 * @return number of bytes written to buf
4668 send_core_path_create (void *cls, size_t size, void *buf)
4670 struct MeshPathInfo *info = cls;
4671 struct GNUNET_MESH_ManipulatePath *msg;
4672 struct GNUNET_PeerIdentity *peer_ptr;
4673 struct MeshTunnel *t = info->t;
4674 struct MeshPeerPath *p = info->path;
4679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4681 sizeof (struct GNUNET_MESH_ManipulatePath) +
4682 p->length * sizeof (struct GNUNET_PeerIdentity);
4684 if (size < size_needed || NULL == buf)
4689 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4690 msg->header.size = htons (size_needed);
4691 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4692 msg->tid = ntohl (t->id.tid);
4695 if (GNUNET_YES == t->speed_min)
4696 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4697 if (GNUNET_YES == t->nobuffer)
4698 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4699 msg->opt = htonl(opt);
4702 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4703 for (i = 0; i < p->length; i++)
4705 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4712 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4718 * Fill the core buffer
4720 * @param cls closure (data itself)
4721 * @param size number of bytes available in buf
4722 * @param buf where the callee should write the message
4724 * @return number of bytes written to buf
4727 send_core_data_multicast (void *cls, size_t size, void *buf)
4729 struct MeshTransmissionDescriptor *info = cls;
4732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4733 GNUNET_assert (NULL != info);
4734 GNUNET_assert (NULL != info->peer);
4735 total_size = info->mesh_data->data_len;
4736 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4738 if (total_size > size)
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4744 memcpy (buf, info->mesh_data->data, total_size);
4747 struct GNUNET_MESH_Multicast *mc;
4748 struct GNUNET_MessageHeader *mh;
4751 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4753 mc = (struct GNUNET_MESH_Multicast *) mh;
4754 mh = (struct GNUNET_MessageHeader *) &mc[1];
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4756 " multicast, payload type %s\n",
4757 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4759 " multicast, payload size %u\n", ntohs (mh->size));
4763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4764 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4768 data_descriptor_decrement_rc (info->mesh_data);
4769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4777 * Creates a path ack message in buf and frees all unused resources.
4779 * @param cls closure (MeshTransmissionDescriptor)
4780 * @param size number of bytes available in buf
4781 * @param buf where the callee should write the message
4782 * @return number of bytes written to buf
4785 send_core_path_ack (void *cls, size_t size, void *buf)
4787 struct MeshTransmissionDescriptor *info = cls;
4788 struct GNUNET_MESH_PathACK *msg = buf;
4790 GNUNET_assert (NULL != info);
4791 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4796 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4797 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4798 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4799 msg->tid = htonl (info->origin->tid);
4800 msg->peer_id = my_full_id;
4803 /* TODO add signature */
4805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4806 return sizeof (struct GNUNET_MESH_PathACK);
4811 * Free a transmission that was already queued with all resources
4812 * associated to the request.
4814 * @param queue Queue handler to cancel.
4815 * @param clear_cls Is it necessary to free associated cls?
4818 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4820 struct MeshTransmissionDescriptor *dd;
4821 struct MeshPathInfo *path_info;
4822 struct MeshTunnelChildInfo *cinfo;
4823 struct GNUNET_PeerIdentity id;
4827 if (GNUNET_YES == clear_cls)
4829 switch (queue->type)
4831 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4832 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4833 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4835 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4836 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4837 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4838 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4839 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4840 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4842 " prebuilt message\n");
4843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4845 GNUNET_MESH_DEBUG_M2S(queue->type));
4847 data_descriptor_decrement_rc (dd->mesh_data);
4849 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4851 path_info = queue->cls;
4852 path_destroy (path_info->path);
4856 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4857 " type %s unknown!\n",
4858 GNUNET_MESH_DEBUG_M2S(queue->type));
4860 GNUNET_free_non_null (queue->cls);
4862 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4863 queue->peer->queue_tail,
4866 /* Delete from child_fc in the appropiate tunnel */
4867 max = queue->tunnel->fwd_queue_max;
4868 GNUNET_PEER_resolve (queue->peer->id, &id);
4869 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4872 for (i = 0; i < cinfo->send_buffer_n; i++)
4875 i2 = (cinfo->send_buffer_start + i) % max;
4876 if (cinfo->send_buffer[i2] == queue)
4878 /* Found corresponding entry in the send_buffer. Move all others back. */
4883 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4885 j2 = (cinfo->send_buffer_start + j) % max;
4886 j3 = (cinfo->send_buffer_start + j + 1) % max;
4887 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4890 cinfo->send_buffer[j3] = NULL;
4891 cinfo->send_buffer_n--;
4896 GNUNET_free (queue);
4901 * @brief Get the next transmittable message from the queue.
4903 * This will be the head, except in the case of being a data packet
4904 * not allowed by the destination peer.
4906 * @param peer Destination peer.
4908 * @return The next viable MeshPeerQueue element to send to that peer.
4909 * NULL when there are no transmittable messages.
4911 struct MeshPeerQueue *
4912 queue_get_next (const struct MeshPeerInfo *peer)
4914 struct MeshPeerQueue *q;
4915 struct MeshTunnel *t;
4916 struct MeshTransmissionDescriptor *info;
4917 struct MeshTunnelChildInfo *cinfo;
4918 struct GNUNET_MESH_Unicast *ucast;
4919 struct GNUNET_MESH_ToOrigin *to_orig;
4920 struct GNUNET_MESH_Multicast *mcast;
4921 struct GNUNET_PeerIdentity id;
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4926 for (q = peer->queue_head; NULL != q; q = q->next)
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4932 GNUNET_MESH_DEBUG_M2S(q->type));
4935 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4936 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4937 pid = ntohl (ucast->pid);
4938 GNUNET_PEER_resolve (info->peer->id, &id);
4939 cinfo = tunnel_get_neighbor_fc(t, &id);
4940 ack = cinfo->fwd_ack;
4942 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4943 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4944 pid = ntohl (to_orig->pid);
4947 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4948 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4949 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4951 // Not a multicast payload: multicast control traffic (destroy, etc)
4954 pid = ntohl (mcast->pid);
4955 GNUNET_PEER_resolve (info->peer->id, &id);
4956 cinfo = tunnel_get_neighbor_fc(t, &id);
4957 ack = cinfo->fwd_ack;
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4965 "********* ACK: %u, PID: %u\n",
4967 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4976 "********* NEXT!\n");
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4980 "********* nothing found\n");
4986 * Core callback to write a queued packet to core buffer
4988 * @param cls Closure (peer info).
4989 * @param size Number of bytes available in buf.
4990 * @param buf Where the to write the message.
4992 * @return number of bytes written to buf
4995 queue_send (void *cls, size_t size, void *buf)
4997 struct MeshPeerInfo *peer = cls;
4998 struct GNUNET_MessageHeader *msg;
4999 struct MeshPeerQueue *queue;
5000 struct MeshTunnel *t;
5001 struct MeshTunnelChildInfo *cinfo;
5002 struct GNUNET_PeerIdentity dst_id;
5005 peer->core_transmit = NULL;
5008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5009 queue = queue_get_next (peer);
5011 /* Queue has no internal mesh traffic nor sendable payload */
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5015 if (NULL == peer->queue_head)
5016 GNUNET_break (0); // Should've been canceled
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5021 GNUNET_PEER_resolve (peer->id, &dst_id);
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5023 "********* towards %s\n",
5024 GNUNET_i2s(&dst_id));
5025 /* Check if buffer size is enough for the message */
5026 if (queue->size > size)
5028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5029 "********* not enough room, reissue\n");
5030 peer->core_transmit =
5031 GNUNET_CORE_notify_transmit_ready (core_handle,
5034 GNUNET_TIME_UNIT_FOREVER_REL,
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5044 GNUNET_assert (0 < t->pending_messages);
5045 t->pending_messages--;
5046 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5050 "********* unicast: t->q (%u/%u)\n",
5051 t->fwd_queue_n, t->fwd_queue_max);
5053 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5060 switch (queue->type)
5063 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5064 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5065 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5066 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5067 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5069 "********* raw: %s\n",
5070 GNUNET_MESH_DEBUG_M2S (queue->type));
5072 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5073 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5074 data_size = send_core_data_raw (queue->cls, size, buf);
5075 msg = (struct GNUNET_MessageHeader *) buf;
5076 switch (ntohs (msg->type)) // Type of preconstructed message
5078 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5079 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5081 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5082 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5088 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5091 struct MeshTransmissionDescriptor *info = queue->cls;
5093 if ((1 == info->mesh_data->reference_counter
5094 && GNUNET_YES == t->speed_min)
5096 (info->mesh_data->total_out == info->mesh_data->reference_counter
5097 && GNUNET_NO == t->speed_min))
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5100 "********* considered sent\n");
5105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5106 "********* NOT considered sent yet\n");
5107 t->pending_messages++;
5110 data_size = send_core_data_multicast(queue->cls, size, buf);
5111 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5113 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5115 data_size = send_core_path_create (queue->cls, size, buf);
5117 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5119 data_size = send_core_path_ack (queue->cls, size, buf);
5121 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5123 data_size = send_core_data_multicast (queue->cls, size, buf);
5127 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5128 "********* type unknown: %u\n",
5132 switch (queue->type)
5134 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5135 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5136 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5137 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5138 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5142 "at pos %u (%p) != %p\n",
5143 cinfo->send_buffer_start,
5144 cinfo->send_buffer[cinfo->send_buffer_start],
5147 if (cinfo->send_buffer_n > 0)
5149 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5150 cinfo->send_buffer_n--;
5151 cinfo->send_buffer_start++;
5152 cinfo->send_buffer_start %= t->fwd_queue_max;
5163 /* Free queue, but cls was freed by send_core_* */
5164 queue_destroy (queue, GNUNET_NO);
5166 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5172 /* If more data in queue, send next */
5173 queue = queue_get_next(peer);
5176 struct GNUNET_PeerIdentity id;
5178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5179 GNUNET_PEER_resolve (peer->id, &id);
5180 peer->core_transmit =
5181 GNUNET_CORE_notify_transmit_ready(core_handle,
5184 GNUNET_TIME_UNIT_FOREVER_REL,
5192 if (NULL != peer->queue_head)
5194 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5195 "********* %s stalled\n",
5196 GNUNET_i2s(&my_full_id));
5198 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5199 if (GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5200 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(MESH_POLL_TIME,
5201 &tunnel_poll, cinfo);
5204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5210 * @brief Queue and pass message to core when possible.
5212 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5213 * and accounts for it. In case the queue is full, the message is dropped and
5216 * Otherwise, message is treated as internal and allowed to go regardless of
5219 * @param cls Closure (@c type dependant). It will be used by queue_send to
5220 * build the message to be sent if not already prebuilt.
5221 * @param type Type of the message, 0 for a raw message.
5222 * @param size Size of the message.
5223 * @param dst Neighbor to send message to.
5224 * @param t Tunnel this message belongs to.
5227 queue_add (void *cls, uint16_t type, size_t size,
5228 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5230 struct MeshPeerQueue *queue;
5231 struct MeshTunnelChildInfo *cinfo;
5232 struct GNUNET_PeerIdentity id;
5238 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5239 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5241 n = &t->fwd_queue_n;
5242 max = &t->fwd_queue_max;
5244 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5246 n = &t->bck_queue_n;
5247 max = &t->bck_queue_max;
5254 GNUNET_STATISTICS_update(stats,
5255 "# messages dropped (buffer full)",
5257 return; // Drop message
5261 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5267 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5268 GNUNET_PEER_resolve (dst->id, &id);
5269 if (NULL == dst->core_transmit)
5271 dst->core_transmit =
5272 GNUNET_CORE_notify_transmit_ready (core_handle,
5275 GNUNET_TIME_UNIT_FOREVER_REL,
5281 t->pending_messages++;
5282 if (NULL == n) // Is this internal mesh traffic?
5285 // It's payload, keep track of buffer per peer.
5286 cinfo = tunnel_get_neighbor_fc(t, &id);
5287 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5288 if (NULL != cinfo->send_buffer[i])
5290 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5291 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5292 cinfo->send_buffer_start++;
5293 cinfo->send_buffer_start %= t->fwd_queue_max;
5297 cinfo->send_buffer_n++;
5299 cinfo->send_buffer[i] = queue;
5300 if (cinfo->send_buffer_n > t->fwd_queue_max)
5303 cinfo->send_buffer_n = t->fwd_queue_max;
5308 /******************************************************************************/
5309 /******************** MESH NETWORK HANDLERS **************************/
5310 /******************************************************************************/
5314 * Core handler for path creation
5316 * @param cls closure
5317 * @param message message
5318 * @param peer peer identity this notification is about
5319 * @param atsi performance data
5320 * @param atsi_count number of records in 'atsi'
5322 * @return GNUNET_OK to keep the connection open,
5323 * GNUNET_SYSERR to close it (signal serious error)
5326 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5327 const struct GNUNET_MessageHeader *message,
5328 const struct GNUNET_ATS_Information *atsi,
5329 unsigned int atsi_count)
5331 unsigned int own_pos;
5334 MESH_TunnelNumber tid;
5335 struct GNUNET_MESH_ManipulatePath *msg;
5336 struct GNUNET_PeerIdentity *pi;
5337 struct GNUNET_HashCode hash;
5338 struct MeshPeerPath *path;
5339 struct MeshPeerInfo *dest_peer_info;
5340 struct MeshPeerInfo *orig_peer_info;
5341 struct MeshTunnel *t;
5343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5344 "Received a path create msg [%s]\n",
5345 GNUNET_i2s (&my_full_id));
5346 size = ntohs (message->size);
5347 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5349 GNUNET_break_op (0);
5353 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5354 if (size % sizeof (struct GNUNET_PeerIdentity))
5356 GNUNET_break_op (0);
5359 size /= sizeof (struct GNUNET_PeerIdentity);
5362 GNUNET_break_op (0);
5365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5366 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5368 tid = ntohl (msg->tid);
5369 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5371 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5372 t = tunnel_get (pi, tid);
5373 if (NULL == t) // FIXME only for INCOMING tunnels?
5377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5378 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5381 // FIXME notify failure
5384 opt = ntohl (msg->opt);
5385 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5386 GNUNET_YES : GNUNET_NO;
5387 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5389 t->nobuffer = GNUNET_YES;
5390 t->last_fwd_ack = t->fwd_pid + 1;
5392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5393 " speed_min: %d, nobuffer:%d\n",
5394 t->speed_min, t->nobuffer);
5396 if (GNUNET_YES == t->nobuffer)
5398 t->bck_queue_max = 1;
5399 t->fwd_queue_max = 1;
5402 // FIXME only assign a local tid if a local client is interested (on demand)
5403 while (NULL != tunnel_get_incoming (next_local_tid))
5404 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5405 t->local_tid_dest = next_local_tid++;
5406 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5409 tunnel_reset_timeout (t);
5410 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5412 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5413 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5421 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5422 if (NULL == dest_peer_info)
5424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5425 " Creating PeerInfo for destination.\n");
5426 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5427 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5428 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5430 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5432 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5433 if (NULL == orig_peer_info)
5435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5436 " Creating PeerInfo for origin.\n");
5437 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5438 orig_peer_info->id = GNUNET_PEER_intern (pi);
5439 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5440 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5443 path = path_new (size);
5445 for (i = 0; i < size; i++)
5447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5448 GNUNET_i2s (&pi[i]));
5449 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5450 if (path->peers[i] == myid)
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5456 /* cannot be self, must be 'not found' */
5457 /* create path: self not found in path through self */
5458 GNUNET_break_op (0);
5459 path_destroy (path);
5463 path_add_to_peers (path, GNUNET_NO);
5464 tunnel_add_path (t, path, own_pos);
5465 if (own_pos == size - 1)
5467 /* It is for us! Send ack. */
5468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5469 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5470 if (NULL == t->peers)
5472 /* New tunnel! Notify clients on first payload message. */
5473 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5475 GNUNET_break (GNUNET_SYSERR !=
5476 GNUNET_CONTAINER_multihashmap_put (t->peers,
5477 &my_full_id.hashPubKey,
5480 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5485 struct MeshPeerPath *path2;
5487 /* It's for somebody else! Retransmit. */
5488 path2 = path_duplicate (path);
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5490 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5491 path2 = path_duplicate (path);
5492 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5493 send_create_path (dest_peer_info, path, t);
5500 * Core handler for path destruction
5502 * @param cls closure
5503 * @param message message
5504 * @param peer peer identity this notification is about
5505 * @param atsi performance data
5506 * @param atsi_count number of records in 'atsi'
5508 * @return GNUNET_OK to keep the connection open,
5509 * GNUNET_SYSERR to close it (signal serious error)
5512 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5513 const struct GNUNET_MessageHeader *message,
5514 const struct GNUNET_ATS_Information *atsi,
5515 unsigned int atsi_count)
5517 struct GNUNET_MESH_ManipulatePath *msg;
5518 struct GNUNET_PeerIdentity *pi;
5519 struct MeshPeerPath *path;
5520 struct MeshTunnel *t;
5521 unsigned int own_pos;
5525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5526 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5527 size = ntohs (message->size);
5528 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5530 GNUNET_break_op (0);
5534 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5535 if (size % sizeof (struct GNUNET_PeerIdentity))
5537 GNUNET_break_op (0);
5540 size /= sizeof (struct GNUNET_PeerIdentity);
5543 GNUNET_break_op (0);
5546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5548 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5549 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5551 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5553 t = tunnel_get (pi, ntohl (msg->tid));
5556 /* TODO notify back: we don't know this tunnel */
5557 GNUNET_break_op (0);
5560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5561 path = path_new (size);
5563 for (i = 0; i < size; i++)
5565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5566 GNUNET_i2s (&pi[i]));
5567 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5568 if (path->peers[i] == myid)
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5572 if (own_pos < path->length - 1)
5573 send_prebuilt_message (message, &pi[own_pos + 1], t);
5575 send_client_tunnel_disconnect(t, NULL);
5577 tunnel_delete_peer (t, path->peers[path->length - 1]);
5578 path_destroy (path);
5584 * Core handler for notifications of broken paths
5586 * @param cls closure
5587 * @param message message
5588 * @param peer peer identity this notification is about
5589 * @param atsi performance data
5590 * @param atsi_count number of records in 'atsi'
5592 * @return GNUNET_OK to keep the connection open,
5593 * GNUNET_SYSERR to close it (signal serious error)
5596 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5597 const struct GNUNET_MessageHeader *message,
5598 const struct GNUNET_ATS_Information *atsi,
5599 unsigned int atsi_count)
5601 struct GNUNET_MESH_PathBroken *msg;
5602 struct MeshTunnel *t;
5604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5605 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5606 msg = (struct GNUNET_MESH_PathBroken *) message;
5607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5608 GNUNET_i2s (&msg->peer1));
5609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5610 GNUNET_i2s (&msg->peer2));
5611 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5614 GNUNET_break_op (0);
5617 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5618 GNUNET_PEER_search (&msg->peer2));
5625 * Core handler for tunnel destruction
5627 * @param cls closure
5628 * @param message message
5629 * @param peer peer identity this notification is about
5630 * @param atsi performance data
5631 * @param atsi_count number of records in 'atsi'
5633 * @return GNUNET_OK to keep the connection open,
5634 * GNUNET_SYSERR to close it (signal serious error)
5637 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5638 const struct GNUNET_MessageHeader *message,
5639 const struct GNUNET_ATS_Information *atsi,
5640 unsigned int atsi_count)
5642 struct GNUNET_MESH_TunnelDestroy *msg;
5643 struct MeshTunnel *t;
5645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5646 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5647 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5649 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5650 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5653 /* Probably already got the message from another path,
5654 * destroyed the tunnel and retransmitted to children.
5657 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5660 if (t->id.oid == myid)
5662 GNUNET_break_op (0);
5665 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5667 /* Tunnel was incoming, notify clients */
5668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5669 t->local_tid, t->local_tid_dest);
5670 send_clients_tunnel_destroy (t);
5672 tunnel_send_destroy (t);
5673 t->destroy = GNUNET_YES;
5674 // TODO: add timeout to destroy the tunnel anyway
5680 * Core handler for mesh network traffic going from the origin to a peer
5682 * @param cls closure
5683 * @param peer peer identity this notification is about
5684 * @param message message
5685 * @param atsi performance data
5686 * @param atsi_count number of records in 'atsi'
5687 * @return GNUNET_OK to keep the connection open,
5688 * GNUNET_SYSERR to close it (signal serious error)
5691 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5692 const struct GNUNET_MessageHeader *message,
5693 const struct GNUNET_ATS_Information *atsi,
5694 unsigned int atsi_count)
5696 struct GNUNET_MESH_Unicast *msg;
5697 struct GNUNET_PeerIdentity *neighbor;
5698 struct MeshTunnelChildInfo *cinfo;
5699 struct MeshTunnel *t;
5700 GNUNET_PEER_Id dest_id;
5705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5708 size = ntohs (message->size);
5710 sizeof (struct GNUNET_MESH_Unicast) +
5711 sizeof (struct GNUNET_MessageHeader))
5716 msg = (struct GNUNET_MESH_Unicast *) message;
5717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5718 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5720 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5723 /* TODO notify back: we don't know this tunnel */
5724 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5725 GNUNET_break_op (0);
5728 pid = ntohl (msg->pid);
5729 if (t->fwd_pid == pid)
5731 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5732 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5733 " Already seen pid %u, DROPPING!\n", pid);
5738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5739 " pid %u not seen yet, forwarding\n", pid);
5742 t->skip += (pid - t->fwd_pid) - 1;
5745 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5747 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5748 GNUNET_break_op (0);
5749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5750 "Received PID %u, ACK %u\n",
5751 pid, t->last_fwd_ack);
5755 tunnel_reset_timeout (t);
5756 dest_id = GNUNET_PEER_search (&msg->destination);
5757 if (dest_id == myid)
5759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5760 " it's for us! sending to clients...\n");
5761 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5762 send_subscribed_clients (message, &msg[1].header, t);
5763 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5766 ttl = ntohl (msg->ttl);
5767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5770 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5771 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5772 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5776 " not for us, retransmitting...\n");
5778 neighbor = tree_get_first_hop (t->tree, dest_id);
5779 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5780 cinfo->fwd_pid = pid;
5781 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5784 if (GNUNET_YES == t->nobuffer &&
5785 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5787 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5788 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5789 GNUNET_break_op (0);
5792 send_prebuilt_message (message, neighbor, t);
5793 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5799 * Core handler for mesh network traffic going from the origin to all peers
5801 * @param cls closure
5802 * @param message message
5803 * @param peer peer identity this notification is about
5804 * @param atsi performance data
5805 * @param atsi_count number of records in 'atsi'
5806 * @return GNUNET_OK to keep the connection open,
5807 * GNUNET_SYSERR to close it (signal serious error)
5809 * TODO: Check who we got this from, to validate route.
5812 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5813 const struct GNUNET_MessageHeader *message,
5814 const struct GNUNET_ATS_Information *atsi,
5815 unsigned int atsi_count)
5817 struct GNUNET_MESH_Multicast *msg;
5818 struct MeshTunnel *t;
5822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5824 size = ntohs (message->size);
5825 if (sizeof (struct GNUNET_MESH_Multicast) +
5826 sizeof (struct GNUNET_MessageHeader) > size)
5828 GNUNET_break_op (0);
5831 msg = (struct GNUNET_MESH_Multicast *) message;
5832 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5836 /* TODO notify that we dont know that tunnel */
5837 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5838 GNUNET_break_op (0);
5841 pid = ntohl (msg->pid);
5842 if (t->fwd_pid == pid)
5844 /* already seen this packet, drop */
5845 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5847 " Already seen pid %u, DROPPING!\n", pid);
5848 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5854 " pid %u not seen yet, forwarding\n", pid);
5856 t->skip += (pid - t->fwd_pid) - 1;
5858 tunnel_reset_timeout (t);
5860 /* Transmit to locally interested clients */
5861 if (NULL != t->peers &&
5862 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5864 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5865 send_subscribed_clients (message, &msg[1].header, t);
5866 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5869 if (ntohl (msg->ttl) == 0)
5871 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5873 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5876 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5877 tunnel_send_multicast (t, message);
5883 * Core handler for mesh network traffic toward the owner of a tunnel
5885 * @param cls closure
5886 * @param message message
5887 * @param peer peer identity this notification is about
5888 * @param atsi performance data
5889 * @param atsi_count number of records in 'atsi'
5891 * @return GNUNET_OK to keep the connection open,
5892 * GNUNET_SYSERR to close it (signal serious error)
5895 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5896 const struct GNUNET_MessageHeader *message,
5897 const struct GNUNET_ATS_Information *atsi,
5898 unsigned int atsi_count)
5900 struct GNUNET_MESH_ToOrigin *msg;
5901 struct GNUNET_PeerIdentity id;
5902 struct MeshPeerInfo *peer_info;
5903 struct MeshTunnel *t;
5904 struct MeshTunnelChildInfo *cinfo;
5908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5910 size = ntohs (message->size);
5911 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5912 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5914 GNUNET_break_op (0);
5917 msg = (struct GNUNET_MESH_ToOrigin *) message;
5918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5919 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5920 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5921 pid = ntohl (msg->pid);
5925 /* TODO notify that we dont know this tunnel (whom)? */
5926 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5927 GNUNET_break_op (0);
5928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5929 "Received to_origin with PID %u on unknown tunnel\n",
5934 cinfo = tunnel_get_neighbor_fc(t, peer);
5941 if (cinfo->bck_pid == pid)
5943 /* already seen this packet, drop */
5944 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5946 " Already seen pid %u, DROPPING!\n", pid);
5947 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5952 " pid %u not seen yet, forwarding\n", pid);
5953 cinfo->bck_pid = pid;
5955 if (NULL != t->owner)
5958 struct GNUNET_MESH_ToOrigin *copy;
5960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5961 " it's for us! sending to clients...\n");
5962 /* TODO signature verification */
5963 memcpy (cbuf, message, size);
5964 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5965 copy->tid = htonl (t->local_tid);
5967 copy->pid = htonl (t->bck_pid);
5968 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5969 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5970 ©->header, GNUNET_NO);
5971 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5975 " not for us, retransmitting...\n");
5977 peer_info = peer_info_get (&msg->oid);
5978 if (NULL == peer_info)
5980 /* unknown origin of tunnel */
5984 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5985 send_prebuilt_message (message, &id, t);
5986 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5993 * Core handler for mesh network traffic point-to-point acks.
5995 * @param cls closure
5996 * @param message message
5997 * @param peer peer identity this notification is about
5998 * @param atsi performance data
5999 * @param atsi_count number of records in 'atsi'
6001 * @return GNUNET_OK to keep the connection open,
6002 * GNUNET_SYSERR to close it (signal serious error)
6005 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6006 const struct GNUNET_MessageHeader *message,
6007 const struct GNUNET_ATS_Information *atsi,
6008 unsigned int atsi_count)
6010 struct GNUNET_MESH_ACK *msg;
6011 struct MeshTunnel *t;
6014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6016 msg = (struct GNUNET_MESH_ACK *) message;
6018 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6022 /* TODO notify that we dont know this tunnel (whom)? */
6023 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6026 ack = ntohl (msg->pid);
6027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6029 /* Is this a forward or backward ACK? */
6030 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6032 struct MeshTunnelChildInfo *cinfo;
6035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6036 cinfo = tunnel_get_neighbor_fc (t, peer);
6037 cinfo->fwd_ack = ack;
6038 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6039 tunnel_unlock_fwd_queues (t);
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6045 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6046 tunnel_unlock_bck_queue (t);
6053 * Core handler for mesh network traffic point-to-point ack polls.
6055 * @param cls closure
6056 * @param message message
6057 * @param peer peer identity this notification is about
6058 * @param atsi performance data
6059 * @param atsi_count number of records in 'atsi'
6061 * @return GNUNET_OK to keep the connection open,
6062 * GNUNET_SYSERR to close it (signal serious error)
6065 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6066 const struct GNUNET_MessageHeader *message,
6067 const struct GNUNET_ATS_Information *atsi,
6068 unsigned int atsi_count)
6070 struct GNUNET_MESH_Poll *msg;
6071 struct MeshTunnel *t;
6073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6076 msg = (struct GNUNET_MESH_Poll *) message;
6078 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6082 /* TODO notify that we dont know this tunnel (whom)? */
6083 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6084 GNUNET_break_op (0);
6088 /* Is this a forward or backward ACK? */
6089 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6091 struct MeshTunnelChildInfo *cinfo;
6093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6094 cinfo = tunnel_get_neighbor_fc (t, peer);
6095 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6096 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6101 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6109 * Core handler for path ACKs
6111 * @param cls closure
6112 * @param message message
6113 * @param peer peer identity this notification is about
6114 * @param atsi performance data
6115 * @param atsi_count number of records in 'atsi'
6117 * @return GNUNET_OK to keep the connection open,
6118 * GNUNET_SYSERR to close it (signal serious error)
6121 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6122 const struct GNUNET_MessageHeader *message,
6123 const struct GNUNET_ATS_Information *atsi,
6124 unsigned int atsi_count)
6126 struct GNUNET_MESH_PathACK *msg;
6127 struct GNUNET_PeerIdentity id;
6128 struct MeshPeerInfo *peer_info;
6129 struct MeshPeerPath *p;
6130 struct MeshTunnel *t;
6132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6133 GNUNET_i2s (&my_full_id));
6134 msg = (struct GNUNET_MESH_PathACK *) message;
6135 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6138 /* TODO notify that we don't know the tunnel */
6139 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6141 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6145 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6147 peer_info = peer_info_get (&msg->peer_id);
6148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6149 GNUNET_i2s (&msg->peer_id));
6150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6153 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6156 "connect_by_string completed, stopping search\n");
6157 regex_cancel_search (t->regex_ctx);
6158 t->regex_ctx = NULL;
6161 /* Add paths to peers? */
6162 p = tree_get_path_to_peer (t->tree, peer_info->id);
6165 path_add_to_peers (p, GNUNET_YES);
6173 /* Message for us? */
6174 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6177 if (NULL == t->owner)
6179 GNUNET_break_op (0);
6182 if (NULL != t->dht_get_type)
6184 GNUNET_DHT_get_stop (t->dht_get_type);
6185 t->dht_get_type = NULL;
6187 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6189 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6190 send_client_peer_connected (t, peer_info->id);
6195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6196 " not for us, retransmitting...\n");
6197 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6198 peer_info = peer_info_get (&msg->oid);
6199 if (NULL == peer_info)
6201 /* If we know the tunnel, we should DEFINITELY know the peer */
6205 send_prebuilt_message (message, &id, t);
6211 * Core handler for mesh keepalives.
6213 * @param cls closure
6214 * @param message message
6215 * @param peer peer identity this notification is about
6216 * @param atsi performance data
6217 * @param atsi_count number of records in 'atsi'
6218 * @return GNUNET_OK to keep the connection open,
6219 * GNUNET_SYSERR to close it (signal serious error)
6221 * TODO: Check who we got this from, to validate route.
6224 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6225 const struct GNUNET_MessageHeader *message,
6226 const struct GNUNET_ATS_Information *atsi,
6227 unsigned int atsi_count)
6229 struct GNUNET_MESH_TunnelKeepAlive *msg;
6230 struct MeshTunnel *t;
6232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6235 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6236 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6240 /* TODO notify that we dont know that tunnel */
6241 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6242 GNUNET_break_op (0);
6246 tunnel_reset_timeout (t);
6248 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6249 tunnel_send_multicast (t, message);
6256 * Functions to handle messages from core
6258 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6259 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6260 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6261 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6262 sizeof (struct GNUNET_MESH_PathBroken)},
6263 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6264 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6265 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6266 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6267 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6268 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6269 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6270 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6271 sizeof (struct GNUNET_MESH_ACK)},
6272 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6273 sizeof (struct GNUNET_MESH_Poll)},
6274 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6275 sizeof (struct GNUNET_MESH_PathACK)},
6281 /******************************************************************************/
6282 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6283 /******************************************************************************/
6286 * deregister_app: iterator for removing each application registered by a client
6288 * @param cls closure
6289 * @param key the hash of the application id (used to access the hashmap)
6290 * @param value the value stored at the key (client)
6292 * @return GNUNET_OK on success
6295 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6297 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6298 GNUNET_break (GNUNET_YES ==
6299 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6305 * notify_client_connection_failure: notify a client that the connection to the
6306 * requested remote peer is not possible (for instance, no route found)
6307 * Function called when the socket is ready to queue more data. "buf" will be
6308 * NULL and "size" zero if the socket was closed for writing in the meantime.
6310 * @param cls closure
6311 * @param size number of bytes available in buf
6312 * @param buf where the callee should write the message
6313 * @return number of bytes written to buf
6316 notify_client_connection_failure (void *cls, size_t size, void *buf)
6319 struct MeshPeerInfo *peer_info;
6320 struct GNUNET_MESH_PeerControl *msg;
6321 struct GNUNET_PeerIdentity id;
6323 if (0 == size && NULL == buf)
6325 // TODO retry? cancel?
6329 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6330 peer_info = (struct MeshPeerInfo *) cls;
6331 msg = (struct GNUNET_MESH_PeerControl *) buf;
6332 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6333 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6334 // msg->tunnel_id = htonl(peer_info->t->tid);
6335 GNUNET_PEER_resolve (peer_info->id, &id);
6336 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6344 * Send keepalive packets for a peer
6346 * @param cls Closure (tunnel for which to send the keepalive).
6347 * @param tc Notification context.
6350 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6352 struct MeshTunnel *t = cls;
6353 struct GNUNET_MESH_TunnelKeepAlive *msg;
6354 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6357 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6358 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6364 "sending keepalive for tunnel %d\n", t->id.tid);
6366 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6367 msg->header.size = htons (size);
6368 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6369 msg->oid = my_full_id;
6370 msg->tid = htonl (t->id.tid);
6371 tunnel_send_multicast (t, &msg->header);
6373 t->path_refresh_task =
6374 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6375 tunnel_reset_timeout(t);
6380 * Function to process paths received for a new peer addition. The recorded
6381 * paths form the initial tunnel, which can be optimized later.
6382 * Called on each result obtained for the DHT search.
6384 * @param cls closure
6385 * @param exp when will this value expire
6386 * @param key key of the result
6387 * @param get_path path of the get request
6388 * @param get_path_length lenght of get_path
6389 * @param put_path path of the put request
6390 * @param put_path_length length of the put_path
6391 * @param type type of the result
6392 * @param size number of bytes in data
6393 * @param data pointer to the result data
6395 * TODO: re-issue the request after certain time? cancel after X results?
6398 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6399 const struct GNUNET_HashCode * key,
6400 const struct GNUNET_PeerIdentity *get_path,
6401 unsigned int get_path_length,
6402 const struct GNUNET_PeerIdentity *put_path,
6403 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6404 size_t size, const void *data)
6406 struct MeshPathInfo *path_info = cls;
6407 struct MeshPeerPath *p;
6408 struct GNUNET_PeerIdentity pi;
6411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6412 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6415 p = path_build_from_dht (get_path, get_path_length, put_path,
6417 path_add_to_peers (p, GNUNET_NO);
6419 for (i = 0; i < path_info->peer->ntunnels; i++)
6421 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6422 peer_info_connect (path_info->peer, path_info->t);
6430 * Function to process paths received for a new peer addition. The recorded
6431 * paths form the initial tunnel, which can be optimized later.
6432 * Called on each result obtained for the DHT search.
6434 * @param cls closure
6435 * @param exp when will this value expire
6436 * @param key key of the result
6437 * @param get_path path of the get request
6438 * @param get_path_length lenght of get_path
6439 * @param put_path path of the put request
6440 * @param put_path_length length of the put_path
6441 * @param type type of the result
6442 * @param size number of bytes in data
6443 * @param data pointer to the result data
6446 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6447 const struct GNUNET_HashCode * key,
6448 const struct GNUNET_PeerIdentity *get_path,
6449 unsigned int get_path_length,
6450 const struct GNUNET_PeerIdentity *put_path,
6451 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6452 size_t size, const void *data)
6454 const struct PBlock *pb = data;
6455 const struct GNUNET_PeerIdentity *pi = &pb->id;
6456 struct MeshTunnel *t = cls;
6457 struct MeshPeerInfo *peer_info;
6458 struct MeshPeerPath *p;
6460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6461 if (size != sizeof (struct PBlock))
6463 GNUNET_break_op (0);
6466 if (ntohl(pb->type) != t->type)
6468 GNUNET_break_op (0);
6471 GNUNET_assert (NULL != t->owner);
6472 peer_info = peer_info_get (pi);
6473 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6475 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6477 p = path_build_from_dht (get_path, get_path_length, put_path,
6479 path_add_to_peers (p, GNUNET_NO);
6481 tunnel_add_peer (t, peer_info);
6482 peer_info_connect (peer_info, t);
6487 * Function to process DHT string to regex matching.
6488 * Called on each result obtained for the DHT search.
6490 * @param cls closure (search context)
6491 * @param exp when will this value expire
6492 * @param key key of the result
6493 * @param get_path path of the get request (not used)
6494 * @param get_path_length lenght of get_path (not used)
6495 * @param put_path path of the put request (not used)
6496 * @param put_path_length length of the put_path (not used)
6497 * @param type type of the result
6498 * @param size number of bytes in data
6499 * @param data pointer to the result data
6502 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6503 const struct GNUNET_HashCode * key,
6504 const struct GNUNET_PeerIdentity *get_path,
6505 unsigned int get_path_length,
6506 const struct GNUNET_PeerIdentity *put_path,
6507 unsigned int put_path_length,
6508 enum GNUNET_BLOCK_Type type,
6509 size_t size, const void *data)
6511 const struct MeshRegexAccept *block = data;
6512 struct MeshRegexSearchContext *ctx = cls;
6513 struct MeshRegexSearchInfo *info = ctx->info;
6514 // struct MeshPeerPath *p;
6515 struct MeshPeerInfo *peer_info;
6517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6519 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6521 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6524 peer_info = peer_info_get(&block->id);
6525 // p = path_build_from_dht (get_path, get_path_length, put_path,
6526 // put_path_length);
6527 // path_add_to_peers (p, GNUNET_NO);
6530 tunnel_add_peer (info->t, peer_info);
6531 peer_info_connect (peer_info, info->t);
6532 if (0 == info->peer)
6534 info->peer = peer_info->id;
6538 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6541 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6542 ®ex_connect_timeout,
6550 * Function to process DHT string to regex matching.
6551 * Called on each result obtained for the DHT search.
6553 * @param cls closure (search context)
6554 * @param exp when will this value expire
6555 * @param key key of the result
6556 * @param get_path path of the get request (not used)
6557 * @param get_path_length lenght of get_path (not used)
6558 * @param put_path path of the put request (not used)
6559 * @param put_path_length length of the put_path (not used)
6560 * @param type type of the result
6561 * @param size number of bytes in data
6562 * @param data pointer to the result data
6564 * TODO: re-issue the request after certain time? cancel after X results?
6567 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6568 const struct GNUNET_HashCode * key,
6569 const struct GNUNET_PeerIdentity *get_path,
6570 unsigned int get_path_length,
6571 const struct GNUNET_PeerIdentity *put_path,
6572 unsigned int put_path_length,
6573 enum GNUNET_BLOCK_Type type,
6574 size_t size, const void *data)
6576 const struct MeshRegexBlock *block = data;
6577 struct MeshRegexSearchContext *ctx = cls;
6578 struct MeshRegexSearchInfo *info = ctx->info;
6582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6583 "DHT GET STRING RETURNED RESULTS\n");
6584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6585 " key: %s\n", GNUNET_h2s (key));
6587 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6589 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6592 copy = GNUNET_malloc (size);
6593 memcpy (copy, data, size);
6594 GNUNET_break (GNUNET_OK ==
6595 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6597 len = ntohl (block->n_proof);
6599 char proof[len + 1];
6601 memcpy (proof, &block[1], len);
6603 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6605 GNUNET_break_op (0);
6609 len = strlen (info->description);
6610 if (len == ctx->position) // String processed
6612 if (GNUNET_YES == ntohl (block->accepting))
6614 regex_find_path(key, ctx);
6618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6619 // FIXME REGEX this block not successful, wait for more? start timeout?
6624 regex_next_edge (block, size, ctx);
6629 /******************************************************************************/
6630 /********************* MESH LOCAL HANDLES **************************/
6631 /******************************************************************************/
6635 * Handler for client disconnection
6637 * @param cls closure
6638 * @param client identification of the client; NULL
6639 * for the last call when the server is destroyed
6642 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6644 struct MeshClient *c;
6645 struct MeshClient *next;
6648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6655 // return; uncomment for regex_profiler
6659 if (c->handle != client)
6661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6667 GNUNET_SERVER_client_drop (c->handle);
6668 c->shutting_down = GNUNET_YES;
6669 GNUNET_assert (NULL != c->own_tunnels);
6670 GNUNET_assert (NULL != c->incoming_tunnels);
6671 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6672 &tunnel_destroy_iterator, c);
6673 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6674 &tunnel_destroy_iterator, c);
6675 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6676 &tunnel_destroy_iterator, c);
6677 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6678 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6679 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6681 /* deregister clients applications */
6682 if (NULL != c->apps)
6684 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6685 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6687 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6688 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6690 GNUNET_SCHEDULER_cancel (announce_applications_task);
6691 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6693 if (NULL != c->types)
6694 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6695 for (i = 0; i < c->n_regex; i++)
6697 GNUNET_free (c->regexes[i].regex);
6698 if (NULL != c->regexes[i].dfa)
6699 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6701 GNUNET_free_non_null (c->regexes);
6702 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6703 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6705 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6708 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6717 * Handler for new clients
6719 * @param cls closure
6720 * @param client identification of the client
6721 * @param message the actual message, which includes messages the client wants
6724 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6725 const struct GNUNET_MessageHeader *message)
6727 struct GNUNET_MESH_ClientConnect *cc_msg;
6728 struct MeshClient *c;
6729 GNUNET_MESH_ApplicationType *a;
6736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6737 /* Check data sanity */
6738 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6739 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6740 ntypes = ntohs (cc_msg->types);
6741 napps = ntohs (cc_msg->applications);
6743 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6746 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6750 /* Create new client structure */
6751 c = GNUNET_malloc (sizeof (struct MeshClient));
6752 c->id = next_client_id++;
6753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6755 GNUNET_SERVER_client_keep (client);
6756 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6759 GNUNET_MESH_ApplicationType at;
6760 struct GNUNET_HashCode hc;
6762 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6763 for (i = 0; i < napps; i++)
6766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6767 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6768 /* store in clients hashmap */
6769 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6770 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6771 /* store in global hashmap, for announcements */
6772 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6773 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6775 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6776 announce_applications_task =
6777 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6783 struct GNUNET_HashCode hc;
6785 t = (uint16_t *) & a[napps];
6786 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6787 for (i = 0; i < ntypes; i++)
6790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6791 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6793 /* store in clients hashmap */
6794 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6795 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6796 /* store in global hashmap */
6797 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6798 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6802 " client has %u+%u subscriptions\n", napps, ntypes);
6804 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6805 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6806 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6807 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6808 GNUNET_SERVER_notification_context_add (nc, client);
6809 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6811 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6817 * Handler for clients announcing available services by a regular expression.
6819 * @param cls closure
6820 * @param client identification of the client
6821 * @param message the actual message, which includes messages the client wants
6824 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6825 const struct GNUNET_MessageHeader *message)
6827 const struct GNUNET_MESH_RegexAnnounce *msg;
6828 struct MeshRegexDescriptor rd;
6829 struct MeshClient *c;
6833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6835 /* Sanity check for client registration */
6836 if (NULL == (c = client_get (client)))
6839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6844 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6845 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6846 regex = GNUNET_malloc (len + 1);
6847 memcpy (regex, &msg[1], len);
6850 rd.compression = ntohs (msg->compression_characters);
6852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6855 GNUNET_array_append (c->regexes, c->n_regex, rd);
6856 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6858 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6870 * Handler for requests of new tunnels
6872 * @param cls closure
6873 * @param client identification of the client
6874 * @param message the actual message
6877 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6878 const struct GNUNET_MessageHeader *message)
6880 struct GNUNET_MESH_TunnelMessage *t_msg;
6881 struct MeshTunnel *t;
6882 struct MeshClient *c;
6883 MESH_TunnelNumber tid;
6885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6887 /* Sanity check for client registration */
6888 if (NULL == (c = client_get (client)))
6891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6896 /* Message sanity check */
6897 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6900 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6904 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6905 /* Sanity check for tunnel numbering */
6906 tid = ntohl (t_msg->tunnel_id);
6907 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6913 /* Sanity check for duplicate tunnel IDs */
6914 if (NULL != tunnel_get_by_local_id (c, tid))
6917 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6921 while (NULL != tunnel_get_by_pi (myid, next_tid))
6922 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6923 t = tunnel_new (myid, next_tid++, c, tid);
6926 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6928 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6931 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6933 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6934 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6937 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6943 * Handler for requests of deleting tunnels
6945 * @param cls closure
6946 * @param client identification of the client
6947 * @param message the actual message
6950 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6951 const struct GNUNET_MessageHeader *message)
6953 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6954 struct MeshClient *c;
6955 struct MeshTunnel *t;
6956 MESH_TunnelNumber tid;
6958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6959 "Got a DESTROY TUNNEL from client!\n");
6961 /* Sanity check for client registration */
6962 if (NULL == (c = client_get (client)))
6965 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6970 /* Message sanity check */
6971 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6978 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6980 /* Retrieve tunnel */
6981 tid = ntohl (tunnel_msg->tunnel_id);
6982 t = tunnel_get_by_local_id(c, tid);
6985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6987 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6990 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6992 client_ignore_tunnel (c, t);
6994 // TODO: when to destroy incoming tunnel?
6995 if (t->nclients == 0)
6997 GNUNET_assert (GNUNET_YES ==
6998 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
7000 GNUNET_assert (GNUNET_YES ==
7001 GNUNET_CONTAINER_multihashmap_remove (t->peers,
7002 &my_full_id.hashPubKey,
7006 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7009 send_client_tunnel_disconnect(t, c);
7010 client_delete_tunnel(c, t);
7012 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7014 tunnel_send_destroy (t);
7015 t->destroy = GNUNET_YES;
7016 // The tunnel will be destroyed when the last message is transmitted.
7017 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7023 * Handler for requests of seeting tunnel's speed.
7025 * @param cls Closure (unused).
7026 * @param client Identification of the client.
7027 * @param message The actual message.
7030 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7031 const struct GNUNET_MessageHeader *message)
7033 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7034 struct MeshClient *c;
7035 struct MeshTunnel *t;
7036 MESH_TunnelNumber tid;
7038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7039 "Got a SPEED request from client!\n");
7041 /* Sanity check for client registration */
7042 if (NULL == (c = client_get (client)))
7045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7051 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7053 /* Retrieve tunnel */
7054 tid = ntohl (tunnel_msg->tunnel_id);
7055 t = tunnel_get_by_local_id(c, tid);
7058 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7064 switch (ntohs(message->type))
7066 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7067 t->speed_min = GNUNET_YES;
7069 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7070 t->speed_min = GNUNET_NO;
7075 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7080 * Handler for requests of seeting tunnel's buffering policy.
7082 * @param cls Closure (unused).
7083 * @param client Identification of the client.
7084 * @param message The actual message.
7087 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7088 const struct GNUNET_MessageHeader *message)
7090 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7091 struct MeshClient *c;
7092 struct MeshTunnel *t;
7093 MESH_TunnelNumber tid;
7095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7096 "Got a BUFFER request from client!\n");
7098 /* Sanity check for client registration */
7099 if (NULL == (c = client_get (client)))
7102 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7107 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7109 /* Retrieve tunnel */
7110 tid = ntohl (tunnel_msg->tunnel_id);
7111 t = tunnel_get_by_local_id(c, tid);
7114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7120 switch (ntohs(message->type))
7122 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7123 t->nobuffer = GNUNET_NO;
7125 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7126 t->nobuffer = GNUNET_YES;
7132 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7137 * Handler for connection requests to new peers
7139 * @param cls closure
7140 * @param client identification of the client
7141 * @param message the actual message (PeerControl)
7144 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7145 const struct GNUNET_MessageHeader *message)
7147 struct GNUNET_MESH_PeerControl *peer_msg;
7148 struct MeshPeerInfo *peer_info;
7149 struct MeshClient *c;
7150 struct MeshTunnel *t;
7151 MESH_TunnelNumber tid;
7153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7154 /* Sanity check for client registration */
7155 if (NULL == (c = client_get (client)))
7158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7163 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7165 /* Sanity check for message size */
7166 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7173 /* Tunnel exists? */
7174 tid = ntohl (peer_msg->tunnel_id);
7175 t = tunnel_get_by_local_id (c, tid);
7179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7183 /* Does client own tunnel? */
7184 if (t->owner->handle != client)
7187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7191 GNUNET_i2s (&peer_msg->peer));
7192 peer_info = peer_info_get (&peer_msg->peer);
7194 tunnel_add_peer (t, peer_info);
7195 peer_info_connect (peer_info, t);
7197 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7203 * Handler for disconnection requests of peers in a tunnel
7205 * @param cls closure
7206 * @param client identification of the client
7207 * @param message the actual message (PeerControl)
7210 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7211 const struct GNUNET_MessageHeader *message)
7213 struct GNUNET_MESH_PeerControl *peer_msg;
7214 struct MeshPeerInfo *peer_info;
7215 struct MeshClient *c;
7216 struct MeshTunnel *t;
7217 MESH_TunnelNumber tid;
7219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7220 /* Sanity check for client registration */
7221 if (NULL == (c = client_get (client)))
7224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7229 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7231 /* Sanity check for message size */
7232 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7239 /* Tunnel exists? */
7240 tid = ntohl (peer_msg->tunnel_id);
7241 t = tunnel_get_by_local_id (c, tid);
7245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7250 /* Does client own tunnel? */
7251 if (t->owner->handle != client)
7254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7259 GNUNET_i2s (&peer_msg->peer));
7260 /* Is the peer in the tunnel? */
7262 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7263 if (NULL == peer_info)
7266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7270 /* Ok, delete peer from tunnel */
7271 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7272 &peer_msg->peer.hashPubKey);
7274 send_destroy_path (t, peer_info->id);
7275 tunnel_delete_peer (t, peer_info->id);
7276 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7281 * Handler for blacklist requests of peers in a tunnel
7283 * @param cls closure
7284 * @param client identification of the client
7285 * @param message the actual message (PeerControl)
7287 * FIXME implement DHT block bloomfilter
7290 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7291 const struct GNUNET_MessageHeader *message)
7293 struct GNUNET_MESH_PeerControl *peer_msg;
7294 struct MeshClient *c;
7295 struct MeshTunnel *t;
7296 MESH_TunnelNumber tid;
7298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7299 /* Sanity check for client registration */
7300 if (NULL == (c = client_get (client)))
7303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7308 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7310 /* Sanity check for message size */
7311 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7318 /* Tunnel exists? */
7319 tid = ntohl (peer_msg->tunnel_id);
7320 t = tunnel_get_by_local_id (c, tid);
7324 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7329 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7330 GNUNET_PEER_intern(&peer_msg->peer));
7335 * Handler for unblacklist requests of peers in a tunnel
7337 * @param cls closure
7338 * @param client identification of the client
7339 * @param message the actual message (PeerControl)
7342 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7343 const struct GNUNET_MessageHeader *message)
7345 struct GNUNET_MESH_PeerControl *peer_msg;
7346 struct MeshClient *c;
7347 struct MeshTunnel *t;
7348 MESH_TunnelNumber tid;
7352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7353 /* Sanity check for client registration */
7354 if (NULL == (c = client_get (client)))
7357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7362 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7364 /* Sanity check for message size */
7365 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7372 /* Tunnel exists? */
7373 tid = ntohl (peer_msg->tunnel_id);
7374 t = tunnel_get_by_local_id (c, tid);
7378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7383 /* if peer is not known, complain */
7384 pid = GNUNET_PEER_search (&peer_msg->peer);
7391 /* search and remove from list */
7392 for (i = 0; i < t->nblacklisted; i++)
7394 if (t->blacklisted[i] == pid)
7396 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7397 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7402 /* if peer hasn't been blacklisted, complain */
7408 * Handler for connection requests to new peers by type
7410 * @param cls closure
7411 * @param client identification of the client
7412 * @param message the actual message (ConnectPeerByType)
7415 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7416 const struct GNUNET_MessageHeader *message)
7418 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7419 struct MeshClient *c;
7420 struct MeshTunnel *t;
7421 struct GNUNET_HashCode hash;
7422 MESH_TunnelNumber tid;
7424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7425 /* Sanity check for client registration */
7426 if (NULL == (c = client_get (client)))
7429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7434 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7436 /* Sanity check for message size */
7437 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7438 ntohs (connect_msg->header.size))
7441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7445 /* Tunnel exists? */
7446 tid = ntohl (connect_msg->tunnel_id);
7447 t = tunnel_get_by_local_id (c, tid);
7451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7455 /* Does client own tunnel? */
7456 if (t->owner->handle != client)
7459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7463 /* Do WE have the service? */
7464 t->type = ntohl (connect_msg->type);
7465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7466 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7467 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7470 /* Yes! Fast forward, add ourselves to the tunnel and send the
7471 * good news to the client, and alert the destination client of
7472 * an incoming tunnel.
7474 * FIXME send a path create to self, avoid code duplication
7476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7477 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7478 peer_info_get (&my_full_id),
7479 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7482 send_client_peer_connected (t, myid);
7483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7484 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7486 t->local_tid_dest = next_local_tid++;
7487 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7488 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7493 /* Ok, lets find a peer offering the service */
7494 if (NULL != t->dht_get_type)
7496 GNUNET_DHT_get_stop (t->dht_get_type);
7498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7499 GNUNET_h2s (&hash));
7501 GNUNET_DHT_get_start (dht_handle,
7502 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7504 dht_replication_level,
7505 GNUNET_DHT_RO_RECORD_ROUTE |
7506 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7508 &dht_get_type_handler, t);
7510 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7516 * Handler for connection requests to new peers by a string service description.
7518 * @param cls closure
7519 * @param client identification of the client
7520 * @param message the actual message, which includes messages the client wants
7523 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7524 const struct GNUNET_MessageHeader *message)
7526 struct GNUNET_MESH_ConnectPeerByString *msg;
7527 struct MeshRegexSearchContext *ctx;
7528 struct MeshRegexSearchInfo *info;
7529 struct GNUNET_DHT_GetHandle *get_h;
7530 struct GNUNET_HashCode key;
7531 struct MeshTunnel *t;
7532 struct MeshClient *c;
7533 MESH_TunnelNumber tid;
7538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7539 "Connect by string started\n");
7540 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7541 size = htons (message->size);
7543 /* Sanity check for client registration */
7544 if (NULL == (c = client_get (client)))
7547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7552 /* Message size sanity check */
7553 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7560 /* Tunnel exists? */
7561 tid = ntohl (msg->tunnel_id);
7562 t = tunnel_get_by_local_id (c, tid);
7566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7570 /* Does client own tunnel? */
7571 if (t->owner->handle != client)
7574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7579 " on tunnel %s [%u]\n",
7580 GNUNET_i2s(&my_full_id),
7583 /* Only one connect_by_string allowed at the same time! */
7584 /* FIXME: allow more, return handle at api level to cancel, document */
7585 if (NULL != t->regex_ctx)
7588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7592 /* Find string itself */
7593 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7594 string = (const char *) &msg[1];
7596 /* Initialize context */
7597 size = GNUNET_REGEX_get_first_key (string, len, &key);
7598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7599 " consumed %u bits out of %u\n", size, len);
7600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7601 " looking for %s\n", GNUNET_h2s (&key));
7603 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7605 info->description = GNUNET_strndup (string, len);
7606 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7607 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7610 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7611 ctx->position = size;
7615 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7617 /* Start search in DHT */
7618 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7619 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7620 &key, /* key to search */
7621 dht_replication_level, /* replication level */
7622 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7623 &info->description[size], /* xquery */
7624 // FIXME add BLOOMFILTER to exclude filtered peers
7625 len + 1 - size, /* xquery bits */
7626 // FIXME add BLOOMFILTER SIZE
7627 &dht_get_string_handler, ctx);
7629 GNUNET_break (GNUNET_OK ==
7630 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7633 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7635 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7641 * Handler for client traffic directed to one peer
7643 * @param cls closure
7644 * @param client identification of the client
7645 * @param message the actual message
7648 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7649 const struct GNUNET_MessageHeader *message)
7651 struct MeshClient *c;
7652 struct MeshTunnel *t;
7653 struct MeshPeerInfo *pi;
7654 struct GNUNET_MESH_Unicast *data_msg;
7655 MESH_TunnelNumber tid;
7658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7659 "Got a unicast request from a client!\n");
7661 /* Sanity check for client registration */
7662 if (NULL == (c = client_get (client)))
7665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7670 data_msg = (struct GNUNET_MESH_Unicast *) message;
7672 /* Sanity check for message size */
7673 size = ntohs (message->size);
7674 if (sizeof (struct GNUNET_MESH_Unicast) +
7675 sizeof (struct GNUNET_MessageHeader) > size)
7678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7682 /* Tunnel exists? */
7683 tid = ntohl (data_msg->tid);
7684 t = tunnel_get_by_local_id (c, tid);
7688 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7692 /* Is it a local tunnel? Then, does client own the tunnel? */
7693 if (t->owner->handle != client)
7696 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7700 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7701 &data_msg->destination.hashPubKey);
7702 /* Is the selected peer in the tunnel? */
7706 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7710 /* PID should be as expected */
7711 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7714 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7715 "Unicast PID, expected %u, got %u\n",
7716 t->fwd_pid + 1, ntohl (data_msg->pid));
7717 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7721 /* Ok, everything is correct, send the message
7722 * (pretend we got it from a mesh peer)
7725 /* Work around const limitation */
7726 char buf[ntohs (message->size)] GNUNET_ALIGN;
7727 struct GNUNET_MESH_Unicast *copy;
7729 copy = (struct GNUNET_MESH_Unicast *) buf;
7730 memcpy (buf, data_msg, size);
7731 copy->oid = my_full_id;
7732 copy->tid = htonl (t->id.tid);
7733 copy->ttl = htonl (default_ttl);
7734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7735 " calling generic handler...\n");
7736 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7739 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7746 * Handler for client traffic directed to the origin
7748 * @param cls closure
7749 * @param client identification of the client
7750 * @param message the actual message
7753 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7754 const struct GNUNET_MessageHeader *message)
7756 struct GNUNET_MESH_ToOrigin *data_msg;
7757 struct MeshTunnelClientInfo *clinfo;
7758 struct MeshClient *c;
7759 struct MeshTunnel *t;
7760 MESH_TunnelNumber tid;
7763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7764 "Got a ToOrigin request from a client!\n");
7765 /* Sanity check for client registration */
7766 if (NULL == (c = client_get (client)))
7769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7774 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7776 /* Sanity check for message size */
7777 size = ntohs (message->size);
7778 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7779 sizeof (struct GNUNET_MessageHeader) > size)
7782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7786 /* Tunnel exists? */
7787 tid = ntohl (data_msg->tid);
7788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7789 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7795 t = tunnel_get_by_local_id (c, tid);
7798 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7799 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7805 /* It should be sent by someone who has this as incoming tunnel. */
7806 if (GNUNET_NO == client_knows_tunnel (c, t))
7809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7813 /* PID should be as expected */
7814 clinfo = tunnel_get_client_fc (t, c);
7815 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7818 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7819 "To Origin PID, expected %u, got %u\n",
7820 clinfo->bck_pid + 1,
7821 ntohl (data_msg->pid));
7822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7827 /* Ok, everything is correct, send the message
7828 * (pretend we got it from a mesh peer)
7831 char buf[ntohs (message->size)] GNUNET_ALIGN;
7832 struct GNUNET_MESH_ToOrigin *copy;
7834 /* Work around const limitation */
7835 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7836 memcpy (buf, data_msg, size);
7837 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7838 copy->tid = htonl (t->id.tid);
7839 copy->ttl = htonl (default_ttl);
7840 copy->pid = htonl (t->bck_pid + 1);
7842 copy->sender = my_full_id;
7843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7844 " calling generic handler...\n");
7845 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7847 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7854 * Handler for client traffic directed to all peers in a tunnel
7856 * @param cls closure
7857 * @param client identification of the client
7858 * @param message the actual message
7861 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7862 const struct GNUNET_MessageHeader *message)
7864 struct MeshClient *c;
7865 struct MeshTunnel *t;
7866 struct GNUNET_MESH_Multicast *data_msg;
7867 MESH_TunnelNumber tid;
7869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7870 "Got a multicast request from a client!\n");
7872 /* Sanity check for client registration */
7873 if (NULL == (c = client_get (client)))
7876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7881 data_msg = (struct GNUNET_MESH_Multicast *) message;
7883 /* Sanity check for message size */
7884 if (sizeof (struct GNUNET_MESH_Multicast) +
7885 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7892 /* Tunnel exists? */
7893 tid = ntohl (data_msg->tid);
7894 t = tunnel_get_by_local_id (c, tid);
7898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7899 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7901 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7905 /* Does client own tunnel? */
7906 if (t->owner->handle != client)
7909 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7913 /* PID should be as expected */
7914 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7917 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7918 "Multicast PID, expected %u, got %u\n",
7919 t->fwd_pid + 1, ntohl (data_msg->pid));
7920 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7925 char buf[ntohs (message->size)] GNUNET_ALIGN;
7926 struct GNUNET_MESH_Multicast *copy;
7928 copy = (struct GNUNET_MESH_Multicast *) buf;
7929 memcpy (buf, message, ntohs (message->size));
7930 copy->oid = my_full_id;
7931 copy->tid = htonl (t->id.tid);
7932 copy->ttl = htonl (default_ttl);
7933 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7935 " calling generic handler...\n");
7936 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7939 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7945 * Handler for client's ACKs for payload traffic.
7947 * @param cls Closure (unused).
7948 * @param client Identification of the client.
7949 * @param message The actual message.
7952 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7953 const struct GNUNET_MessageHeader *message)
7955 struct GNUNET_MESH_LocalAck *msg;
7956 struct MeshTunnel *t;
7957 struct MeshClient *c;
7958 MESH_TunnelNumber tid;
7961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7962 /* Sanity check for client registration */
7963 if (NULL == (c = client_get (client)))
7966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7971 msg = (struct GNUNET_MESH_LocalAck *) message;
7973 /* Tunnel exists? */
7974 tid = ntohl (msg->tunnel_id);
7975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7976 t = tunnel_get_by_local_id (c, tid);
7980 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7986 ack = ntohl (msg->max_pid);
7987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7989 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7990 if (NULL != t->owner && t->owner->handle == client)
7992 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7994 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7998 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7999 tunnel_set_client_fwd_ack (t, c, ack);
8000 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8003 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8010 * Functions to handle messages from clients
8012 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8013 {&handle_local_new_client, NULL,
8014 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8015 {&handle_local_announce_regex, NULL,
8016 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8017 {&handle_local_tunnel_create, NULL,
8018 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8019 sizeof (struct GNUNET_MESH_TunnelMessage)},
8020 {&handle_local_tunnel_destroy, NULL,
8021 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8022 sizeof (struct GNUNET_MESH_TunnelMessage)},
8023 {&handle_local_tunnel_speed, NULL,
8024 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8025 sizeof (struct GNUNET_MESH_TunnelMessage)},
8026 {&handle_local_tunnel_speed, NULL,
8027 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8028 sizeof (struct GNUNET_MESH_TunnelMessage)},
8029 {&handle_local_tunnel_buffer, NULL,
8030 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8031 sizeof (struct GNUNET_MESH_TunnelMessage)},
8032 {&handle_local_tunnel_buffer, NULL,
8033 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8034 sizeof (struct GNUNET_MESH_TunnelMessage)},
8035 {&handle_local_connect_add, NULL,
8036 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8037 sizeof (struct GNUNET_MESH_PeerControl)},
8038 {&handle_local_connect_del, NULL,
8039 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8040 sizeof (struct GNUNET_MESH_PeerControl)},
8041 {&handle_local_blacklist, NULL,
8042 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8043 sizeof (struct GNUNET_MESH_PeerControl)},
8044 {&handle_local_unblacklist, NULL,
8045 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8046 sizeof (struct GNUNET_MESH_PeerControl)},
8047 {&handle_local_connect_by_type, NULL,
8048 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8049 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8050 {&handle_local_connect_by_string, NULL,
8051 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8052 {&handle_local_unicast, NULL,
8053 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8054 {&handle_local_to_origin, NULL,
8055 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8056 {&handle_local_multicast, NULL,
8057 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8058 {&handle_local_ack, NULL,
8059 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8060 sizeof (struct GNUNET_MESH_LocalAck)},
8066 * To be called on core init/fail.
8068 * @param cls service closure
8069 * @param server handle to the server for this service
8070 * @param identity the public identity of this peer
8073 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8074 const struct GNUNET_PeerIdentity *identity)
8077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8078 core_handle = server;
8079 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8082 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8083 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8085 GNUNET_abort(); // Try harder
8092 * Method called whenever a given peer connects.
8094 * @param cls closure
8095 * @param peer peer identity this notification is about
8096 * @param atsi performance data for the connection
8097 * @param atsi_count number of records in 'atsi'
8100 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8101 const struct GNUNET_ATS_Information *atsi,
8102 unsigned int atsi_count)
8104 struct MeshPeerInfo *peer_info;
8105 struct MeshPeerPath *path;
8107 DEBUG_CONN ("Peer connected\n");
8108 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8109 peer_info = peer_info_get (peer);
8110 if (myid == peer_info->id)
8112 DEBUG_CONN (" (self)\n");
8117 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8119 path = path_new (2);
8120 path->peers[0] = myid;
8121 path->peers[1] = peer_info->id;
8122 GNUNET_PEER_change_rc (myid, 1);
8123 GNUNET_PEER_change_rc (peer_info->id, 1);
8124 peer_info_add_path (peer_info, path, GNUNET_YES);
8125 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8131 * Method called whenever a peer disconnects.
8133 * @param cls closure
8134 * @param peer peer identity this notification is about
8137 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8139 struct MeshPeerInfo *pi;
8140 struct MeshPeerQueue *q;
8141 struct MeshPeerQueue *n;
8143 DEBUG_CONN ("Peer disconnected\n");
8144 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8154 /* TODO try to reroute this traffic instead */
8155 queue_destroy(q, GNUNET_YES);
8158 if (NULL != pi->core_transmit)
8160 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8161 pi->core_transmit = NULL;
8163 peer_info_remove_path (pi, pi->id, myid);
8166 DEBUG_CONN (" (self)\n");
8168 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8173 /******************************************************************************/
8174 /************************ MAIN FUNCTIONS ****************************/
8175 /******************************************************************************/
8178 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8180 * @param cls closure
8181 * @param key current key code
8182 * @param value value in the hash map
8183 * @return GNUNET_YES if we should continue to iterate,
8187 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8189 struct MeshTunnel *t = value;
8196 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8198 * @param cls closure
8199 * @param key current key code
8200 * @param value value in the hash map
8201 * @return GNUNET_YES if we should continue to iterate,
8205 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8207 struct MeshPeerInfo *p = value;
8208 struct MeshPeerQueue *q;
8209 struct MeshPeerQueue *n;
8217 queue_destroy(q, GNUNET_YES);
8221 peer_info_destroy (p);
8227 * Task run during shutdown.
8233 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8237 if (core_handle != NULL)
8239 GNUNET_CORE_disconnect (core_handle);
8244 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8247 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8248 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8249 if (dht_handle != NULL)
8251 GNUNET_DHT_disconnect (dht_handle);
8256 GNUNET_SERVER_notification_context_destroy (nc);
8259 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8261 GNUNET_SCHEDULER_cancel (announce_id_task);
8262 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8269 * Callback for hostkey read/generation
8272 * @param pk the private key
8273 * @param emsg error message
8276 key_generation_cb (void *cls,
8277 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8280 struct MeshPeerInfo *peer;
8281 struct MeshPeerPath *p;
8286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8287 _("Mesh service could not access hostkey. Exiting.\n"));
8288 GNUNET_SCHEDULER_shutdown ();
8291 my_private_key = pk;
8292 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8293 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8294 &my_full_id.hashPubKey);
8295 myid = GNUNET_PEER_intern (&my_full_id);
8296 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8297 "Mesh for peer [%s] starting\n",
8298 GNUNET_i2s(&my_full_id));
8300 // transport_handle = GNUNET_TRANSPORT_connect(c,
8310 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8313 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8314 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8315 GNUNET_SERVER_disconnect_notify (server_handle,
8316 &handle_local_client_disconnect, NULL);
8320 clients_tail = NULL;
8323 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8324 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8326 /* Create a peer_info for the local peer */
8327 peer = peer_info_get (&my_full_id);
8330 GNUNET_PEER_change_rc (myid, 1);
8331 peer_info_add_path (peer, p, GNUNET_YES);
8332 GNUNET_SERVER_resume (server_handle);
8333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8338 * Process mesh requests.
8340 * @param cls closure
8341 * @param server the initialized server
8342 * @param c configuration to use
8345 run (void *cls, struct GNUNET_SERVER_Handle *server,
8346 const struct GNUNET_CONFIGURATION_Handle *c)
8350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8351 server_handle = server;
8352 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8353 NULL, /* Closure passed to MESH functions */
8354 &core_init, /* Call core_init once connected */
8355 &core_connect, /* Handle connects */
8356 &core_disconnect, /* remove peers on disconnects */
8357 NULL, /* Don't notify about all incoming messages */
8358 GNUNET_NO, /* For header only in notification */
8359 NULL, /* Don't notify about all outbound messages */
8360 GNUNET_NO, /* For header-only out notification */
8361 core_handlers); /* Register these handlers */
8363 if (core_handle == NULL)
8366 GNUNET_SCHEDULER_shutdown ();
8371 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8376 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8378 GNUNET_SCHEDULER_shutdown ();
8383 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8384 &refresh_path_time))
8386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8388 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8389 "mesh", "refresh path time");
8390 GNUNET_SCHEDULER_shutdown ();
8395 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8396 &app_announce_time))
8398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8400 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8401 "mesh", "app announce time");
8402 GNUNET_SCHEDULER_shutdown ();
8405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8406 "APP_ANNOUNCE_TIME %llu ms\n",
8407 app_announce_time.rel_value);
8409 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8414 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8415 "mesh", "id announce time");
8416 GNUNET_SCHEDULER_shutdown ();
8421 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8424 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8426 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8427 "mesh", "connect timeout");
8428 GNUNET_SCHEDULER_shutdown ();
8433 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8438 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8439 "mesh", "max msgs queue");
8440 GNUNET_SCHEDULER_shutdown ();
8445 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8450 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8451 "mesh", "max tunnels");
8452 GNUNET_SCHEDULER_shutdown ();
8457 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8462 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8463 "mesh", "default ttl", 64);
8468 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8469 &dht_replication_level))
8471 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8473 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8474 "mesh", "dht replication level", 3);
8475 dht_replication_level = 3;
8478 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8479 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8480 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8481 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8482 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8484 dht_handle = GNUNET_DHT_connect (c, 64);
8485 if (NULL == dht_handle)
8489 stats = GNUNET_STATISTICS_create ("mesh", c);
8491 GNUNET_SERVER_suspend (server_handle);
8492 /* Scheduled the task to clean up when shutdown is called */
8493 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8495 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8496 GNUNET_free (keyfile);
8501 * The main function for the mesh service.
8503 * @param argc number of arguments from the command line
8504 * @param argv command line arguments
8505 * @return 0 ok, 1 on error
8508 main (int argc, char *const *argv)
8513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8514 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8516 ret = (GNUNET_OK == r) ? 0 : 1;
8517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8521 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8522 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8523 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);