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, "* tk[0] : %c\n", token[0]);
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 || SIZE_MAX == size);
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;
1776 GNUNET_DHT_put(dht_handle, key,
1777 dht_replication_level,
1778 opt/* | GNUNET_DHT_RO_RECORD_ROUTE*/,
1779 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1782 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: expiration time should be option */
1786 len = strlen(proof);
1787 size = sizeof (struct MeshRegexBlock) + len;
1788 block = GNUNET_malloc (size);
1791 block->n_proof = htonl (len);
1792 block->n_edges = htonl (num_edges);
1793 block->accepting = htonl (accepting);
1795 /* Store the proof at the end of the block. */
1796 aux = (char *) &block[1];
1797 memcpy (aux, proof, len);
1800 /* Store each edge in a variable length MeshEdge struct at the
1801 * very end of the MeshRegexBlock structure.
1803 for (i = 0; i < num_edges; i++)
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806 " edge %s towards %s\n",
1808 GNUNET_h2s(&edges[i].destination));
1810 /* aux points at the end of the last block */
1811 len = strlen (edges[i].label);
1812 size += sizeof (struct MeshRegexEdge) + len;
1813 // Calculate offset FIXME is this ok? use size instead?
1814 offset = aux - (char *) block;
1815 block = GNUNET_realloc (block, size);
1816 aux = &((char *) block)[offset];
1817 block_edge = (struct MeshRegexEdge *) aux;
1818 block_edge->key = edges[i].destination;
1819 block_edge->n_token = htonl (len);
1820 aux = (char *) &block_edge[1];
1821 memcpy (aux, edges[i].label, len);
1825 GNUNET_DHT_put(dht_handle, key,
1826 dht_replication_level,
1828 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1830 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1833 GNUNET_free (block);
1838 * Store the regular expression describing a local service into the DHT.
1840 * @param regex The regular expresion.
1843 regex_put (struct MeshRegexDescriptor *regex)
1845 if (NULL == regex->dfa)
1847 regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1848 strlen (regex->regex),
1849 regex->compression);
1852 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1853 GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL);
1854 DEBUG_DHT (" regex_put (%s) end\n", regex);
1859 * Periodically announce what applications are provided by local clients
1862 * @param cls closure
1863 * @param tc task context
1866 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1868 struct MeshClient *c = cls;
1871 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1872 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1874 DEBUG_DHT ("Starting PUT for regex\n");
1875 for (i = 0; i < c->n_regex; i++)
1876 regex_put (&c->regexes[i]);
1877 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1880 DEBUG_DHT ("Finished PUT for regex\n");
1885 * Periodically announce what applications are provided by local clients
1888 * @param cls closure
1889 * @param tc task context
1892 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1894 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1896 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1900 DEBUG_DHT ("Starting PUT for apps\n");
1902 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1904 announce_applications_task =
1905 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1907 DEBUG_DHT ("Finished PUT for apps\n");
1912 * Periodically announce self id in the DHT
1914 * @param cls closure
1915 * @param tc task context
1918 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1920 struct PBlock block;
1922 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1924 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1928 * - Set data expiration in function of X
1929 * - Adapt X to churn
1931 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1933 block.id = my_full_id;
1934 block.type = htonl (0);
1935 GNUNET_DHT_put (dht_handle, /* DHT handle */
1936 &my_full_id.hashPubKey, /* Key to use */
1937 dht_replication_level, /* Replication level */
1938 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1939 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1940 sizeof (block), /* Size of the data */
1941 (const char *) &block, /* Data itself */
1942 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1943 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1944 NULL, /* Continuation */
1945 NULL); /* Continuation closure */
1947 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1951 /******************************************************************************/
1952 /****************** GENERAL HELPER FUNCTIONS ************************/
1953 /******************************************************************************/
1956 * Decrements the reference counter and frees all resources if needed
1958 * @param mesh_data Data Descriptor used in a multicast message.
1959 * Freed no longer needed (last message).
1962 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1964 if (0 == --(mesh_data->reference_counter))
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1967 GNUNET_free (mesh_data->data);
1968 GNUNET_free (mesh_data);
1974 * Check if client has registered with the service and has not disconnected
1976 * @param client the client to check
1978 * @return non-NULL if client exists in the global DLL
1980 static struct MeshClient *
1981 client_get (struct GNUNET_SERVER_Client *client)
1983 struct MeshClient *c;
1988 if (c->handle == client)
1997 * Checks if a given client has subscribed to certain message type
1999 * @param message_type Type of message to check
2000 * @param c Client to check
2002 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
2004 * FIXME: use of crypto_hash slows it down
2005 * The hash function alone takes 8-10us out of the ~55us for the whole
2006 * process of retransmitting the message from one local client to another.
2007 * Find faster implementation!
2010 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
2012 struct GNUNET_HashCode hc;
2014 if (NULL == c->types)
2017 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
2018 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2023 * Check whether client wants traffic from a tunnel.
2025 * @param c Client to check.
2026 * @param t Tunnel to be found.
2028 * @return GNUNET_YES if client knows tunnel.
2030 * TODO look in client hashmap
2033 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2037 for (i = 0; i < t->nclients; i++)
2038 if (t->clients[i] == c)
2045 * Check whether client has been informed about a tunnel.
2047 * @param c Client to check.
2048 * @param t Tunnel to be found.
2050 * @return GNUNET_YES if client knows tunnel.
2052 * TODO look in client hashmap
2055 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2059 for (i = 0; i < t->nignore; i++)
2060 if (t->ignore[i] == c)
2062 return client_wants_tunnel(c, t);
2067 * Marks a client as uninterested in traffic from the tunnel, updating both
2068 * client and tunnel to reflect this.
2070 * @param c Client that doesn't want traffic anymore.
2071 * @param t Tunnel which should be ignored.
2073 * FIXME when to delete an incoming tunnel?
2076 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2078 struct GNUNET_HashCode hash;
2080 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2081 GNUNET_break (GNUNET_YES ==
2082 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2084 GNUNET_break (GNUNET_YES ==
2085 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2086 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2087 tunnel_delete_active_client (t, c);
2088 GNUNET_array_append (t->ignore, t->nignore, c);
2093 * Deletes a tunnel from a client (either owner or destination). To be used on
2094 * tunnel destroy, otherwise, use client_ignore_tunnel.
2096 * @param c Client whose tunnel to delete.
2097 * @param t Tunnel which should be deleted.
2100 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2102 struct GNUNET_HashCode hash;
2106 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2107 GNUNET_assert (GNUNET_YES ==
2108 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2114 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2116 GNUNET_assert (GNUNET_YES ==
2117 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2121 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2129 * Send the message to all clients that have subscribed to its type
2131 * @param msg Pointer to the message itself
2132 * @param payload Pointer to the payload of the message.
2133 * @param t The tunnel to whose clients this message goes.
2135 * @return number of clients this message was sent to
2138 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2139 const struct GNUNET_MessageHeader *payload,
2140 struct MeshTunnel *t)
2142 struct MeshClient *c;
2143 MESH_TunnelNumber *tid;
2146 char cbuf[htons (msg->size)];
2148 type = ntohs (payload->type);
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2151 GNUNET_MESH_DEBUG_M2S (type));
2153 memcpy (cbuf, msg, sizeof (cbuf));
2154 switch (htons (msg->type))
2156 struct GNUNET_MESH_Unicast *uc;
2157 struct GNUNET_MESH_Multicast *mc;
2158 struct GNUNET_MESH_ToOrigin *to;
2160 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2161 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2164 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2165 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2168 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2169 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2177 for (count = 0, c = clients; c != NULL; c = c->next)
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2180 if (client_is_subscribed (type, c))
2182 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2186 *tid = htonl (t->local_tid);
2190 if (GNUNET_NO == client_knows_tunnel (c, t))
2192 /* This client doesn't know the tunnel */
2193 struct GNUNET_MESH_TunnelNotification tmsg;
2194 struct GNUNET_HashCode hash;
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2197 tmsg.header.size = htons (sizeof (tmsg));
2198 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2199 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2200 tmsg.tunnel_id = htonl (t->local_tid_dest);
2202 if (GNUNET_YES == t->speed_min)
2203 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2204 if (GNUNET_YES == t->nobuffer)
2205 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2206 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2207 &tmsg.header, GNUNET_NO);
2208 tunnel_add_client (t, c);
2209 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2211 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2212 c->incoming_tunnels, &hash, t,
2213 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2215 *tid = htonl (t->local_tid_dest);
2218 /* Check if the client wants to get traffic from the tunnel */
2219 if (GNUNET_NO == client_wants_tunnel(c, t))
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2223 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2224 (struct GNUNET_MessageHeader
2225 *) cbuf, GNUNET_NO);
2234 * Notify the client that owns the tunnel that a peer has connected to it
2235 * (the requested path to it has been confirmed).
2237 * @param t Tunnel whose owner to notify
2238 * @param id Short id of the peer that has connected
2241 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2243 struct GNUNET_MESH_PeerControl pc;
2245 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2246 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2247 pc.tunnel_id = htonl (t->local_tid);
2248 GNUNET_PEER_resolve (id, &pc.peer);
2249 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2255 * Notify all clients (not depending on registration status) that the incoming
2256 * tunnel is no longer valid.
2258 * @param t Tunnel that was destroyed.
2261 send_clients_tunnel_destroy (struct MeshTunnel *t)
2263 struct GNUNET_MESH_TunnelMessage msg;
2265 msg.header.size = htons (sizeof (msg));
2266 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2267 msg.tunnel_id = htonl (t->local_tid_dest);
2268 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2273 * Notify clients of tunnel disconnections, if needed.
2274 * In case the origin disconnects, the destination clients get a tunnel destroy
2275 * notification. If the last destination disconnects (only one remaining client
2276 * in tunnel), the origin gets a (local ID) peer disconnected.
2277 * Note that the function must be called BEFORE removing the client from
2280 * @param t Tunnel that was destroyed.
2281 * @param c Client that disconnected.
2284 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2290 struct GNUNET_MESH_TunnelMessage msg;
2292 msg.header.size = htons (sizeof (msg));
2293 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2294 msg.tunnel_id = htonl (t->local_tid_dest);
2295 for (i = 0; i < t->nclients; i++)
2296 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2297 &msg.header, GNUNET_NO);
2299 // FIXME when to disconnect an incoming tunnel?
2300 else if (1 == t->nclients && NULL != t->owner)
2302 struct GNUNET_MESH_PeerControl msg;
2304 msg.header.size = htons (sizeof (msg));
2305 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2306 msg.tunnel_id = htonl (t->local_tid);
2307 msg.peer = my_full_id;
2308 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2309 &msg.header, GNUNET_NO);
2315 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2316 * and insert it in the appropiate structures if the peer is not known yet.
2318 * @param peer Full identity of the peer.
2320 * @return Existing or newly created peer info.
2322 static struct MeshPeerInfo *
2323 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2325 struct MeshPeerInfo *peer_info;
2327 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2328 if (NULL == peer_info)
2331 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2332 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2334 peer_info->id = GNUNET_PEER_intern (peer);
2342 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2343 * and insert it in the appropiate structures if the peer is not known yet.
2345 * @param peer Short identity of the peer.
2347 * @return Existing or newly created peer info.
2349 static struct MeshPeerInfo *
2350 peer_info_get_short (const GNUNET_PEER_Id peer)
2352 struct GNUNET_PeerIdentity id;
2354 GNUNET_PEER_resolve (peer, &id);
2355 return peer_info_get (&id);
2360 * Iterator to remove the tunnel from the list of tunnels a peer participates
2363 * @param cls Closure (tunnel info)
2364 * @param key GNUNET_PeerIdentity of the peer (unused)
2365 * @param value PeerInfo of the peer
2367 * @return always GNUNET_YES, to keep iterating
2370 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2372 struct MeshTunnel *t = cls;
2373 struct MeshPeerInfo *peer = value;
2376 for (i = 0; i < peer->ntunnels; i++)
2379 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2382 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2383 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2392 * Core callback to write a pre-constructed data packet to core buffer
2394 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2395 * @param size Number of bytes available in buf.
2396 * @param buf Where the to write the message.
2398 * @return number of bytes written to buf
2401 send_core_data_raw (void *cls, size_t size, void *buf)
2403 struct MeshTransmissionDescriptor *info = cls;
2404 struct GNUNET_MessageHeader *msg;
2407 GNUNET_assert (NULL != info);
2408 GNUNET_assert (NULL != info->mesh_data);
2409 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2410 total_size = ntohs (msg->size);
2412 if (total_size > size)
2417 memcpy (buf, msg, total_size);
2418 data_descriptor_decrement_rc (info->mesh_data);
2425 * Sends an already built non-multicast message to a peer,
2426 * properly registrating all used resources.
2428 * @param message Message to send. Function makes a copy of it.
2429 * @param peer Short ID of the neighbor whom to send the message.
2430 * @param t Tunnel on which this message is transmitted.
2433 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2434 const struct GNUNET_PeerIdentity *peer,
2435 struct MeshTunnel *t)
2437 struct MeshTransmissionDescriptor *info;
2438 struct MeshPeerInfo *neighbor;
2439 struct MeshPeerPath *p;
2443 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2445 size = ntohs (message->size);
2446 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2447 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2448 info->mesh_data->data = GNUNET_malloc (size);
2449 memcpy (info->mesh_data->data, message, size);
2450 type = ntohs(message->type);
2453 struct GNUNET_MESH_Unicast *m;
2454 struct GNUNET_MESH_ToOrigin *to;
2456 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2457 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2458 m->ttl = htonl (ntohl (m->ttl) - 1);
2460 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2461 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2463 to->pid = htonl(t->bck_pid);
2465 info->mesh_data->data_len = size;
2466 info->mesh_data->reference_counter = 1;
2467 info->mesh_data->total_out = 1;
2468 neighbor = peer_info_get (peer);
2469 for (p = neighbor->path_head; NULL != p; p = p->next)
2479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2480 " %s IS NOT DIRECTLY CONNECTED\n",
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2485 for (p = neighbor->path_head; NULL != p; p = p->next)
2487 struct GNUNET_PeerIdentity debug_id;
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2491 " path with %u hops through:\n",
2493 for (i = 0; i < p->length; i++)
2495 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2498 i, GNUNET_i2s(&debug_id));
2502 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2503 GNUNET_free (info->mesh_data->data);
2504 GNUNET_free (info->mesh_data);
2508 info->peer = neighbor;
2509 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2520 * Sends a CREATE PATH message for a path to a peer, properly registrating
2521 * all used resources.
2523 * @param peer PeerInfo of the final peer for whom this path is being created.
2524 * @param p Path itself.
2525 * @param t Tunnel for which the path is created.
2528 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2529 struct MeshTunnel *t)
2531 struct GNUNET_PeerIdentity id;
2532 struct MeshPathInfo *path_info;
2533 struct MeshPeerInfo *neighbor;
2539 p = tree_get_path_to_peer (t->tree, peer->id);
2546 for (i = 0; i < p->length; i++)
2548 if (p->peers[i] == myid)
2551 if (i >= p->length - 1)
2557 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2559 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2560 path_info->path = p;
2562 neighbor = peer_info_get (&id);
2563 path_info->peer = neighbor;
2564 queue_add (path_info,
2565 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2566 sizeof (struct GNUNET_MESH_ManipulatePath) +
2567 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2574 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2576 * @param t Tunnel whose path to destroy.
2577 * @param destination Short ID of the peer to whom the path to destroy.
2580 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2582 struct MeshPeerPath *p;
2585 p = tree_get_path_to_peer (t->tree, destination);
2591 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2592 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2594 struct GNUNET_MESH_ManipulatePath *msg;
2595 struct GNUNET_PeerIdentity *pi;
2599 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2600 msg->header.size = htons (size);
2601 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2602 msg->tid = htonl (t->id.tid);
2603 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2604 for (i = 0; i < p->length; i++)
2606 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2608 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2615 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2617 * @param t Tunnel which to confirm.
2620 send_path_ack (struct MeshTunnel *t)
2622 struct MeshTransmissionDescriptor *info;
2623 struct GNUNET_PeerIdentity id;
2624 GNUNET_PEER_Id peer;
2626 peer = tree_get_predecessor (t->tree);
2627 GNUNET_PEER_resolve (peer, &id);
2628 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2629 info->origin = &t->id;
2630 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2631 GNUNET_assert (NULL != info->peer);
2634 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2635 sizeof (struct GNUNET_MESH_PathACK),
2642 * Try to establish a new connection to this peer.
2643 * Use the best path for the given tunnel.
2644 * If the peer doesn't have any path to it yet, try to get one.
2645 * If the peer already has some path, send a CREATE PATH towards it.
2647 * @param peer PeerInfo of the peer.
2648 * @param t Tunnel for which to create the path, if possible.
2651 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2653 struct MeshPeerPath *p;
2654 struct MeshPathInfo *path_info;
2656 if (NULL != peer->path_head)
2658 p = tree_get_path_to_peer (t->tree, peer->id);
2665 // FIXME always send create path to self
2668 send_create_path (peer, p, t);
2672 struct GNUNET_HashCode hash;
2675 send_client_peer_connected (t, myid);
2676 t->local_tid_dest = next_local_tid++;
2677 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2680 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2681 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2688 else if (NULL == peer->dhtget)
2690 struct GNUNET_PeerIdentity id;
2692 GNUNET_PEER_resolve (peer->id, &id);
2693 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2694 path_info->peer = peer;
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2697 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2698 peer->dhtgetcls = path_info;
2699 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2700 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2701 &id.hashPubKey, /* key to search */
2702 dht_replication_level, /* replication level */
2703 GNUNET_DHT_RO_RECORD_ROUTE |
2704 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2705 NULL, /* xquery */ // FIXME BLOOMFILTER
2706 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2707 &dht_get_id_handler, path_info);
2709 /* Otherwise, there is no path but the DHT get is already started. */
2714 * Task to delay the connection of a peer
2716 * @param cls Closure (path info with tunnel and peer to connect).
2717 * Will be free'd on exection.
2718 * @param tc TaskContext
2721 peer_info_connect_task (void *cls,
2722 const struct GNUNET_SCHEDULER_TaskContext *tc)
2724 struct MeshPathInfo *path_info = cls;
2726 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2728 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2733 peer_info_connect (path_info->peer, path_info->t);
2739 * Destroy the peer_info and free any allocated resources linked to it
2741 * @param pi The peer_info to destroy.
2743 * @return GNUNET_OK on success
2746 peer_info_destroy (struct MeshPeerInfo *pi)
2748 struct GNUNET_PeerIdentity id;
2749 struct MeshPeerPath *p;
2750 struct MeshPeerPath *nextp;
2752 GNUNET_PEER_resolve (pi->id, &id);
2753 GNUNET_PEER_change_rc (pi->id, -1);
2756 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2760 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2762 if (NULL != pi->dhtget)
2764 GNUNET_DHT_get_stop (pi->dhtget);
2765 GNUNET_free (pi->dhtgetcls);
2771 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2775 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2777 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2785 * Remove all paths that rely on a direct connection between p1 and p2
2786 * from the peer itself and notify all tunnels about it.
2788 * @param peer PeerInfo of affected peer.
2789 * @param p1 GNUNET_PEER_Id of one peer.
2790 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2793 * TODO: optimize (see below)
2796 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2799 struct MeshPeerPath *p;
2800 struct MeshPeerPath *aux;
2801 struct MeshPeerInfo *peer_d;
2803 unsigned int destroyed;
2808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2810 p = peer->path_head;
2814 for (i = 0; i < (p->length - 1); i++)
2816 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2817 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2819 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2830 for (i = 0; i < peer->ntunnels; i++)
2832 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2836 * Problem: one or more peers have been deleted from the tunnel tree.
2837 * We don't know who they are to try to add them again.
2838 * We need to try to find a new path for each of the disconnected peers.
2839 * Some of them might already have a path to reach them that does not
2840 * involve p1 and p2. Adding all anew might render in a better tree than
2841 * the trivial immediate fix.
2843 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2844 * its children will be reachable trough him.
2846 peer_d = peer_info_get_short (d);
2849 for (p = peer_d->path_head; NULL != p; p = p->next)
2851 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2859 /* No callback, as peer will be already disconnected and a connection
2860 * scheduled by tunnel_notify_connection_broken.
2862 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2866 peer_info_connect (peer_d, peer->tunnels[i]);
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2874 * Add the path to the peer and update the path used to reach it in case this
2877 * @param peer_info Destination peer to add the path to.
2878 * @param path New path to add. Last peer must be the peer in arg 1.
2879 * Path will be either used of freed if already known.
2880 * @param trusted Do we trust that this path is real?
2883 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2886 struct MeshPeerPath *aux;
2890 if ((NULL == peer_info) || (NULL == path))
2893 path_destroy (path);
2896 if (path->peers[path->length - 1] != peer_info->id)
2899 path_destroy (path);
2902 if (path->length <= 2 && GNUNET_NO == trusted)
2904 /* Only allow CORE to tell us about direct paths */
2905 path_destroy (path);
2908 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2909 for (l = 1; l < path->length; l++)
2911 if (path->peers[l] == myid)
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2914 for (l2 = 0; l2 < path->length - l; l2++)
2916 path->peers[l2] = path->peers[l + l2];
2921 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2926 struct GNUNET_PeerIdentity id;
2928 GNUNET_PEER_resolve (peer_info->id, &id);
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2930 path->length, GNUNET_i2s (&id));
2933 l = path_get_length (path);
2940 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2941 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2943 l2 = path_get_length (aux);
2946 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2947 peer_info->path_tail, aux, path);
2952 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2954 path_destroy (path);
2959 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2966 * Add the path to the origin peer and update the path used to reach it in case
2967 * this is the shortest.
2968 * The path is given in peer_info -> destination, therefore we turn the path
2969 * upside down first.
2971 * @param peer_info Peer to add the path to, being the origin of the path.
2972 * @param path New path to add after being inversed.
2973 * @param trusted Do we trust that this path is real?
2976 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2977 struct MeshPeerPath *path, int trusted)
2980 peer_info_add_path (peer_info, path, trusted);
2985 * Function called if the connection to the peer has been stalled for a while,
2986 * possibly due to a missed ACK. Poll the peer about its ACK status.
2988 * @param cls Closure (info about regex search).
2989 * @param tc TaskContext.
2992 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2994 struct MeshTunnelChildInfo *cinfo = cls;
2995 struct GNUNET_MESH_Poll msg;
2996 struct GNUNET_PeerIdentity id;
2997 struct MeshTunnel *t;
2999 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3000 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3006 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3007 msg.header.size = htons (sizeof (msg));
3008 msg.tid = htonl (t->id.tid);
3009 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3010 msg.last_ack = htonl (cinfo->fwd_ack);
3012 GNUNET_PEER_resolve (cinfo->id, &id);
3013 send_prebuilt_message (&msg.header, &id, cinfo->t);
3014 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
3015 &tunnel_poll, cinfo);
3020 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3021 * to obtain a local peer -> destination path and interning the peer ids.
3023 * @return Newly allocated and created path
3025 static struct MeshPeerPath *
3026 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3027 unsigned int get_path_length,
3028 const struct GNUNET_PeerIdentity *put_path,
3029 unsigned int put_path_length)
3031 struct MeshPeerPath *p;
3037 GNUNET_PEER_change_rc (myid, 1);
3038 i = get_path_length;
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3040 for (i--; i >= 0; i--)
3042 id = GNUNET_PEER_intern (&get_path[i]);
3043 if (p->length > 0 && id == p->peers[p->length - 1])
3045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3046 GNUNET_PEER_change_rc (id, -1);
3050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3051 GNUNET_i2s (&get_path[i]));
3053 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3054 p->peers[p->length - 1] = id;
3057 i = put_path_length;
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3059 for (i--; i >= 0; i--)
3061 id = GNUNET_PEER_intern (&put_path[i]);
3064 /* PUT path went through us, so discard the path up until now and start
3065 * from here to get a much shorter (and loop-free) path.
3070 if (p->length > 0 && id == p->peers[p->length - 1])
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3073 GNUNET_PEER_change_rc (id, -1);
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3078 GNUNET_i2s (&put_path[i]));
3080 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3081 p->peers[p->length - 1] = id;
3085 if (get_path_length > 0)
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3087 GNUNET_i2s (&get_path[0]));
3088 if (put_path_length > 0)
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3090 GNUNET_i2s (&put_path[0]));
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3093 for (i = 0; i < p->length; i++)
3095 struct GNUNET_PeerIdentity peer_id;
3097 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3099 GNUNET_i2s (&peer_id));
3107 * Adds a path to the peer_infos of all the peers in the path
3109 * @param p Path to process.
3110 * @param confirmed Whether we know if the path works or not.
3113 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3117 /* TODO: invert and add */
3118 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3119 for (i++; i < p->length; i++)
3121 struct MeshPeerInfo *aux;
3122 struct MeshPeerPath *copy;
3124 aux = peer_info_get_short (p->peers[i]);
3125 copy = path_duplicate (p);
3126 copy->length = i + 1;
3127 peer_info_add_path (aux, copy, GNUNET_NO);
3133 * Send keepalive packets for a peer
3135 * @param cls Closure (tunnel for which to send the keepalive).
3136 * @param tc Notification context.
3138 * TODO: implement explicit multicast keepalive?
3141 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3145 * Search for a tunnel among the incoming tunnels
3147 * @param tid the local id of the tunnel
3149 * @return tunnel handler, NULL if doesn't exist
3151 static struct MeshTunnel *
3152 tunnel_get_incoming (MESH_TunnelNumber tid)
3154 struct GNUNET_HashCode hash;
3156 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3157 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3158 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3163 * Search for a tunnel among the tunnels for a client
3165 * @param c the client whose tunnels to search in
3166 * @param tid the local id of the tunnel
3168 * @return tunnel handler, NULL if doesn't exist
3170 static struct MeshTunnel *
3171 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3173 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3175 return tunnel_get_incoming (tid);
3179 struct GNUNET_HashCode hash;
3181 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3182 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3188 * Search for a tunnel by global ID using PEER_ID
3190 * @param pi owner of the tunnel
3191 * @param tid global tunnel number
3193 * @return tunnel handler, NULL if doesn't exist
3195 static struct MeshTunnel *
3196 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3198 struct MESH_TunnelID id;
3199 struct GNUNET_HashCode hash;
3204 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3205 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3210 * Search for a tunnel by global ID using full PeerIdentities
3212 * @param oid owner of the tunnel
3213 * @param tid global tunnel number
3215 * @return tunnel handler, NULL if doesn't exist
3217 static struct MeshTunnel *
3218 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3220 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3225 * Delete an active client from the tunnel.
3231 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3235 for (i = 0; i < t->nclients; i++)
3237 if (t->clients[i] == c)
3239 t->clients[i] = t->clients[t->nclients - 1];
3240 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3241 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3243 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3251 * Delete an ignored client from the tunnel.
3257 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3261 for (i = 0; i < t->nignore; i++)
3263 if (t->ignore[i] == c)
3265 t->ignore[i] = t->ignore[t->nignore - 1];
3266 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3274 * Delete a client from the tunnel. It should be only done on
3275 * client disconnection, otherwise use client_ignore_tunnel.
3281 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3283 tunnel_delete_ignored_client (t, c);
3284 tunnel_delete_active_client (t, c);
3289 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3291 * Destroys queue elements of all waiting transmissions and frees all memory
3292 * used by the struct and its elements.
3294 * @param cls Closure (tunnel info).
3295 * @param key Hash of GNUNET_PEER_Id (unused).
3296 * @param value MeshTunnelChildInfo of the child.
3298 * @return always GNUNET_YES, to keep iterating
3301 tunnel_destroy_child (void *cls,
3302 const struct GNUNET_HashCode * key,
3305 struct MeshTunnelChildInfo *cinfo = value;
3306 struct MeshTunnel *t = cls;
3307 struct MeshPeerQueue *q;
3311 for (c = 0; c < cinfo->send_buffer_n; c++)
3313 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3314 q = cinfo->send_buffer[i];
3315 cinfo->send_buffer[i] = NULL;
3317 queue_destroy (q, GNUNET_YES);
3320 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3322 GNUNET_free_non_null (cinfo->send_buffer);
3323 GNUNET_free (cinfo);
3329 * Callback used to notify a client owner of a tunnel that a peer has
3330 * disconnected, most likely because of a path change.
3332 * @param cls Closure (tunnel this notification is about).
3333 * @param peer_id Short ID of disconnected peer.
3336 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3338 struct MeshTunnel *t = cls;
3339 struct MeshPeerInfo *peer;
3340 struct MeshPathInfo *path_info;
3342 if (NULL != t->owner && NULL != nc)
3344 struct GNUNET_MESH_PeerControl msg;
3346 msg.header.size = htons (sizeof (msg));
3347 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3348 msg.tunnel_id = htonl (t->local_tid);
3349 GNUNET_PEER_resolve (peer_id, &msg.peer);
3350 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3351 &msg.header, GNUNET_NO);
3353 peer = peer_info_get_short (peer_id);
3354 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3355 path_info->peer = peer;
3357 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3363 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3364 * needed rescources.
3365 * If peer already exists, reevaluate shortest path and change if different.
3367 * @param t Tunnel we want to add a new peer to
3368 * @param peer PeerInfo of the peer being added
3372 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3374 struct GNUNET_PeerIdentity id;
3375 struct MeshPeerPath *best_p;
3376 struct MeshPeerPath *p;
3377 unsigned int best_cost;
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3381 GNUNET_PEER_resolve (peer->id, &id);
3383 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3386 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3387 GNUNET_assert (GNUNET_OK ==
3388 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3390 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3393 if (NULL != (p = peer->path_head))
3396 best_cost = tree_get_path_cost (t->tree, p);
3399 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3406 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3407 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3408 t->path_refresh_task =
3409 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3413 /* Start a DHT get */
3414 peer_info_connect (peer, t);
3416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3420 * Add a path to a tunnel which we don't own, just to remember the next hop.
3421 * If destination node was already in the tunnel, the first hop information
3422 * will be replaced with the new path.
3424 * @param t Tunnel we want to add a new peer to
3425 * @param p Path to add
3426 * @param own_pos Position of local node in path.
3430 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3431 unsigned int own_pos)
3433 struct GNUNET_PeerIdentity id;
3435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3436 GNUNET_assert (0 != own_pos);
3437 tree_add_path (t->tree, p, NULL, NULL);
3438 if (own_pos < p->length - 1)
3440 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3441 tree_update_first_hops (t->tree, myid, &id);
3443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3447 * Add a client to a tunnel, initializing all needed data structures.
3449 * @param t Tunnel to which add the client.
3450 * @param c Client which to add to the tunnel.
3453 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3455 struct MeshTunnelClientInfo clinfo;
3457 GNUNET_array_append (t->clients, t->nclients, c);
3458 clinfo.fwd_ack = t->fwd_pid + 1;
3459 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3460 clinfo.fwd_pid = t->fwd_pid;
3461 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3463 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3468 * Notifies a tunnel that a connection has broken that affects at least
3469 * some of its peers. Sends a notification towards the root of the tree.
3470 * In case the peer is the owner of the tree, notifies the client that owns
3471 * the tunnel and tries to reconnect.
3473 * @param t Tunnel affected.
3474 * @param p1 Peer that got disconnected from p2.
3475 * @param p2 Peer that got disconnected from p1.
3477 * @return Short ID of the peer disconnected (either p1 or p2).
3478 * 0 if the tunnel remained unaffected.
3480 static GNUNET_PEER_Id
3481 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3487 tree_notify_connection_broken (t->tree, p1, p2,
3488 &tunnel_notify_client_peer_disconnected,
3490 if (myid != p1 && myid != p2)
3496 if (tree_get_predecessor (t->tree) != 0)
3498 /* We are the peer still connected, notify owner of the disconnection. */
3499 struct GNUNET_MESH_PathBroken msg;
3500 struct GNUNET_PeerIdentity neighbor;
3502 msg.header.size = htons (sizeof (msg));
3503 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3504 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3505 msg.tid = htonl (t->id.tid);
3506 msg.peer1 = my_full_id;
3507 GNUNET_PEER_resolve (pid, &msg.peer2);
3508 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3509 send_prebuilt_message (&msg.header, &neighbor, t);
3517 * Send a multicast packet to a neighbor.
3519 * @param cls Closure (Info about the multicast packet)
3520 * @param neighbor_id Short ID of the neighbor to send the packet to.
3523 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3525 struct MeshData *mdata = cls;
3526 struct MeshTransmissionDescriptor *info;
3527 struct GNUNET_PeerIdentity neighbor;
3528 struct GNUNET_MessageHeader *msg;
3530 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3532 info->mesh_data = mdata;
3533 (mdata->reference_counter) ++;
3534 info->destination = neighbor_id;
3535 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3537 GNUNET_i2s (&neighbor));
3538 info->peer = peer_info_get (&neighbor);
3539 GNUNET_assert (NULL != info->peer);
3540 msg = (struct GNUNET_MessageHeader *) mdata->data;
3543 info->mesh_data->data_len,
3550 * Queue a message in a tunnel in multicast, sending a copy to each child node
3551 * down the local one in the tunnel tree.
3553 * @param t Tunnel in which to send the data.
3554 * @param msg Message to be sent.
3557 tunnel_send_multicast (struct MeshTunnel *t,
3558 const struct GNUNET_MessageHeader *msg)
3560 struct MeshData *mdata;
3562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3563 " sending a multicast packet...\n");
3565 mdata = GNUNET_malloc (sizeof (struct MeshData));
3566 mdata->data_len = ntohs (msg->size);
3568 mdata->data = GNUNET_malloc (mdata->data_len);
3569 memcpy (mdata->data, msg, mdata->data_len);
3570 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3572 struct GNUNET_MESH_Multicast *mcast;
3574 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3575 if (t->fwd_queue_n >= t->fwd_queue_max)
3578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3580 " message from %s!\n",
3581 GNUNET_i2s(&mcast->oid));
3582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3583 " message at %s!\n",
3584 GNUNET_i2s(&my_full_id));
3585 GNUNET_free (mdata->data);
3586 GNUNET_free (mdata);
3590 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3592 ntohl (mcast->ttl));
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3599 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3600 if (mdata->reference_counter == 0)
3602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3603 " no one to send data to\n");
3604 GNUNET_free (mdata->data);
3605 GNUNET_free (mdata);
3606 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3611 mdata->total_out = mdata->reference_counter;
3613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3614 " sending a multicast packet done\n");
3620 * Increase the SKIP value of all peers that
3621 * have not received a unicast message.
3623 * @param cls Closure (ID of the peer that HAS received the message).
3624 * @param key ID of the neighbor.
3625 * @param value Information about the neighbor.
3627 * @return GNUNET_YES to keep iterating.
3630 tunnel_add_skip (void *cls,
3631 const struct GNUNET_HashCode * key,
3634 struct GNUNET_PeerIdentity *neighbor = cls;
3635 struct MeshTunnelChildInfo *cinfo = value;
3637 /* TODO compare only pointers? key == neighbor? */
3638 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3648 * @brief Get neighbor's Flow Control information.
3650 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3651 * descendant of the local node in a certain tunnel.
3652 * If the info is not yet there (recently created path), creates the data struct
3653 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3656 * @param t Tunnel related.
3657 * @param peer Neighbor whose Flow Control info is needed.
3659 * @return Neighbor's Flow Control info.
3661 static struct MeshTunnelChildInfo *
3662 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3663 const struct GNUNET_PeerIdentity *peer)
3665 struct MeshTunnelChildInfo *cinfo;
3667 if (NULL == t->children_fc)
3670 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3676 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3677 cinfo->id = GNUNET_PEER_intern (peer);
3678 cinfo->skip = t->fwd_pid;
3681 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3682 cinfo->fwd_ack = t->fwd_pid + delta;
3683 cinfo->bck_ack = delta;
3684 cinfo->bck_pid = -1;
3686 cinfo->send_buffer =
3687 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3689 GNUNET_assert (GNUNET_OK ==
3690 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3693 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3700 * Get the Flow Control info of a client.
3702 * @param t Tunnel on which to look.
3703 * @param c Client whose ACK to get.
3705 * @return ACK value.
3707 static struct MeshTunnelClientInfo *
3708 tunnel_get_client_fc (struct MeshTunnel *t,
3709 struct MeshClient *c)
3713 for (i = 0; i < t->nclients; i++)
3715 if (t->clients[i] != c)
3717 return &t->clients_fc[i];
3720 return NULL; // avoid compiler / coverity complaints
3725 * Iterator to get the appropiate ACK value from all children nodes.
3727 * @param cls Closue (tunnel).
3728 * @param id Id of the child node.
3731 tunnel_get_child_fwd_ack (void *cls,
3734 struct GNUNET_PeerIdentity peer_id;
3735 struct MeshTunnelChildInfo *cinfo;
3736 struct MeshTunnelChildIteratorContext *ctx = cls;
3737 struct MeshTunnel *t = ctx->t;
3740 GNUNET_PEER_resolve (id, &peer_id);
3741 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3742 ack = cinfo->fwd_ack;
3745 if (GNUNET_NO == ctx->init)
3747 ctx->max_child_ack = ack;
3748 ctx->init = GNUNET_YES;
3751 if (GNUNET_YES == t->speed_min)
3753 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3757 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3764 * Get the maximum PID allowed to transmit to any
3765 * tunnel child of the local peer, depending on the tunnel
3766 * buffering/speed settings.
3770 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3773 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3775 struct MeshTunnelChildIteratorContext ctx;
3777 ctx.max_child_ack = 0;
3779 ctx.init = GNUNET_NO;
3780 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3782 if (0 == ctx.nchildren)
3784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3785 " tunnel has no children, no FWD ACK\n");
3789 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3790 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3792 return (int64_t) ctx.max_child_ack;
3797 * Set the FWD ACK value of a client in a particular tunnel.
3799 * @param t Tunnel affected.
3800 * @param c Client whose ACK to set.
3801 * @param ack ACK value.
3804 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3805 struct MeshClient *c,
3810 for (i = 0; i < t->nclients; i++)
3812 if (t->clients[i] != c)
3814 t->clients_fc[i].fwd_ack = ack;
3822 * Get the highest ACK value of all clients in a particular tunnel,
3823 * according to the buffering/speed settings.
3825 * @param t Tunnel on which to look.
3827 * @return Corresponding ACK value (max uint32_t).
3828 * If no clients are suscribed, -1LL.
3831 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3836 if (0 == t->nclients)
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3839 " tunnel has no clients, no FWD ACK\n");
3843 for (ack = -1LL, i = 0; i < t->nclients; i++)
3846 (GNUNET_YES == t->speed_min &&
3847 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3848 (GNUNET_NO == t->speed_min &&
3849 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3851 ack = t->clients_fc[i].fwd_ack;
3855 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3856 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3858 return (uint32_t) ack;
3863 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3864 * mode and the status of all children nodes.
3868 * @return Maximum PID allowed.
3871 tunnel_get_fwd_ack (struct MeshTunnel *t)
3875 uint32_t buffer_free;
3879 count = t->fwd_pid - t->skip;
3880 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3881 child_ack = tunnel_get_children_fwd_ack (t);
3882 client_ack = tunnel_get_clients_fwd_ack (t);
3883 if (GNUNET_YES == t->nobuffer)
3886 if (-1LL == child_ack)
3887 child_ack = client_ack;
3888 if (-1LL == child_ack)
3891 client_ack = child_ack = ack;
3896 ack = count + buffer_free; // Overflow? OK!
3898 if (-1LL == child_ack)
3900 // Node has no children, child_ack AND core buffer are irrelevant.
3901 if (-1LL == client_ack) // No children AND no clients? Not good!
3903 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3907 return (uint32_t) client_ack;
3909 if (-1LL == client_ack)
3913 if (GNUNET_YES == t->speed_min)
3915 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3916 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3920 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3921 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3924 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3925 count, buffer_free, child_ack, client_ack, ack);
3931 * Build a local ACK message and send it to a local client.
3933 * @param t Tunnel on which to send the ACK.
3934 * @param c Client to whom send the ACK.
3935 * @param ack Value of the ACK.
3938 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3940 struct GNUNET_MESH_LocalAck msg;
3942 msg.header.size = htons (sizeof (msg));
3943 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3944 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3945 msg.max_pid = htonl (ack);
3946 GNUNET_SERVER_notification_context_unicast(nc,
3953 * Build an ACK message and queue it to send to the given peer.
3955 * @param t Tunnel on which to send the ACK.
3956 * @param peer Peer to whom send the ACK.
3957 * @param ack Value of the ACK.
3960 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3962 struct GNUNET_MESH_ACK msg;
3964 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3965 msg.header.size = htons (sizeof (msg));
3966 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3967 msg.pid = htonl (ack);
3968 msg.tid = htonl (t->id.tid);
3970 send_prebuilt_message (&msg.header, peer, t);
3975 * Notify a the owner of a tunnel about how many more
3976 * payload packages will we accept on a given tunnel.
3978 * @param t Tunnel on which to send the ACK.
3981 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3986 "Sending client FWD ACK on tunnel %X\n",
3989 ack = tunnel_get_fwd_ack (t);
3991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3992 if (t->last_fwd_ack == ack)
3994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3999 t->last_fwd_ack = ack;
4000 send_local_ack (t, t->owner, ack);
4005 * Send an ACK informing the predecessor about the available buffer space.
4006 * In case there is no predecessor, inform the owning client.
4007 * If buffering is off, send only on behalf of children or self if endpoint.
4008 * If buffering is on, send when sent to children and buffer space is free.
4009 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4010 * the ACK itself goes "back" (towards root).
4012 * @param t Tunnel on which to send the ACK.
4013 * @param type Type of message that triggered the ACK transmission.
4016 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4018 struct GNUNET_PeerIdentity id;
4021 if (NULL != t->owner)
4023 tunnel_send_client_fwd_ack (t);
4026 /* Is it after unicast / multicast retransmission? */
4029 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4030 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4032 "ACK due to FWD DATA retransmission\n");
4033 if (GNUNET_YES == t->nobuffer)
4035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4039 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4040 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4042 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4043 t->force_ack = GNUNET_YES;
4049 /* Check if we need to transmit the ACK */
4050 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4051 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4052 GNUNET_NO == t->force_ack)
4054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4056 " t->qmax: %u, t->qn: %u\n",
4057 t->fwd_queue_max, t->fwd_queue_n);
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4059 " t->pid: %u, t->ack: %u\n",
4060 t->fwd_pid, t->last_fwd_ack);
4064 /* Ok, ACK might be necessary, what PID to ACK? */
4065 ack = tunnel_get_fwd_ack (t);
4067 /* If speed_min and not all children have ack'd, dont send yet */
4068 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4074 t->last_fwd_ack = ack;
4075 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4076 send_ack (t, &id, ack);
4078 t->force_ack = GNUNET_NO;
4083 * Iterator to send a child node a BCK ACK to allow him to send more
4086 * @param cls Closure (tunnel).
4087 * @param id Id of the child node.
4090 tunnel_send_child_bck_ack (void *cls,
4093 struct MeshTunnel *t = cls;
4094 struct MeshTunnelChildInfo *cinfo;
4095 struct GNUNET_PeerIdentity peer;
4098 GNUNET_PEER_resolve (id, &peer);
4099 cinfo = tunnel_get_neighbor_fc (t, &peer);
4100 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4102 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4105 " Not sending ACK, not needed\n");
4108 cinfo->bck_ack = ack;
4110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4111 " Sending BCK ACK %u (last sent: %u)\n",
4112 ack, cinfo->bck_ack);
4113 send_ack (t, &peer, ack);
4118 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4120 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4122 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4123 * - implement a size 1 buffer in each client_fc AND children_fc
4124 * to hold at least 1 message per "child".
4125 * problem: violates no buffer policy
4126 * - ack 0 and make "children" poll for transmission slots
4127 * problem: big overhead, extra latency even in low traffic
4130 * @param t Tunnel on which to send the BCK ACKs.
4133 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4136 unsigned int tunnel_delta;
4138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4140 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4143 /* Find client whom to allow to send to origin (with lowest buffer space) */
4144 for (i = 0; i < t->nclients; i++)
4146 struct MeshTunnelClientInfo *clinfo;
4149 clinfo = &t->clients_fc[i];
4150 delta = clinfo->bck_ack - clinfo->bck_pid;
4151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4152 t->clients[i]->id, delta);
4154 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4155 (GNUNET_YES == t->nobuffer && 0 == delta))
4159 ack = clinfo->bck_pid;
4160 ack += t->nobuffer ? 1 : tunnel_delta;
4161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4162 " sending ack to client %u: %u\n",
4163 t->clients[i]->id, ack);
4164 send_local_ack (t, t->clients[i], ack);
4165 clinfo->bck_ack = ack;
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4170 " not sending ack to client %u (td %u, d %u)\n",
4171 t->clients[i]->id, tunnel_delta, delta);
4178 * Send an ACK informing the children nodes and destination clients about
4179 * the available buffer space.
4180 * If buffering is off, send only on behalf of root (can be self).
4181 * If buffering is on, send when sent to predecessor and buffer space is free.
4182 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4183 * the ACK itself goes "forward" (towards children/clients).
4185 * @param t Tunnel on which to send the ACK.
4186 * @param type Type of message that triggered the ACK transmission.
4189 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4192 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4193 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4194 /* Is it after data to_origin retransmission? */
4197 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4198 if (GNUNET_YES == t->nobuffer)
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4201 " Not sending ACK, nobuffer\n");
4205 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4206 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4208 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4209 t->force_ack = GNUNET_YES;
4215 tunnel_send_clients_bck_ack (t);
4216 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4217 t->force_ack = GNUNET_NO;
4222 * @brief Re-initiate traffic to this peer if necessary.
4224 * Check if there is traffic queued towards this peer
4225 * and the core transmit handle is NULL (traffic was stalled).
4226 * If so, call core tmt rdy.
4228 * @param cls Closure (unused)
4229 * @param peer_id Short ID of peer to which initiate traffic.
4232 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4234 struct MeshPeerInfo *peer;
4235 struct GNUNET_PeerIdentity id;
4236 struct MeshPeerQueue *q;
4239 peer = peer_info_get_short(peer_id);
4240 if (NULL != peer->core_transmit)
4243 q = queue_get_next(peer);
4246 /* Might br multicast traffic already sent to this particular peer but
4247 * not to other children in this tunnel.
4248 * This way t->queue_n would be > 0 but the queue of this particular peer
4254 GNUNET_PEER_resolve (peer->id, &id);
4255 peer->core_transmit =
4256 GNUNET_CORE_notify_transmit_ready(core_handle,
4259 GNUNET_TIME_UNIT_FOREVER_REL,
4269 * @brief Allow transmission of FWD traffic on this tunnel
4271 * Check if there is traffic queued towards any children
4272 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4274 * @param t Tunnel on which to unlock FWD traffic.
4277 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4279 if (0 == t->fwd_queue_n)
4282 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4287 * @brief Allow transmission of BCK traffic on this tunnel
4289 * Check if there is traffic queued towards the root of the tree
4290 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4292 * @param t Tunnel on which to unlock BCK traffic.
4295 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4297 if (0 == t->bck_queue_n)
4300 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4305 * Send a message to all peers in this tunnel that the tunnel is no longer
4308 * @param t The tunnel whose peers to notify.
4311 tunnel_send_destroy (struct MeshTunnel *t)
4313 struct GNUNET_MESH_TunnelDestroy msg;
4315 msg.header.size = htons (sizeof (msg));
4316 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4317 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4318 msg.tid = htonl (t->id.tid);
4319 tunnel_send_multicast (t, &msg.header);
4324 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4326 * @param cls Closure (Tunnel which to cancel).
4327 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4330 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4332 struct MeshTunnel *t = cls;
4333 struct MeshPeerInfo *peer_info;
4334 struct MeshPeerQueue *pq;
4335 struct MeshPeerQueue *next;
4337 peer_info = peer_info_get_short (neighbor_id);
4338 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4341 if (pq->tunnel == t)
4343 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4344 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4345 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4347 // Should have been removed on destroy children
4350 queue_destroy (pq, GNUNET_YES);
4353 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4355 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4356 peer_info->core_transmit = NULL;
4361 * Destroy the tunnel and free any allocated resources linked to it.
4363 * @param t the tunnel to destroy
4365 * @return GNUNET_OK on success
4368 tunnel_destroy (struct MeshTunnel *t)
4370 struct MeshClient *c;
4371 struct GNUNET_HashCode hash;
4382 struct GNUNET_PeerIdentity id;
4384 GNUNET_PEER_resolve (t->id.oid, &id);
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4386 GNUNET_i2s (&id), t->id.tid);
4388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4392 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4393 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4401 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4403 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4410 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4411 for (i = 0; i < t->nclients; i++)
4415 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4421 for (i = 0; i < t->nignore; i++)
4425 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4432 if (t->nclients > 0)
4435 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4440 GNUNET_free (t->clients);
4441 GNUNET_free (t->clients_fc);
4444 if (NULL != t->peers)
4446 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4448 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4451 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4452 &tunnel_destroy_child,
4454 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4455 t->children_fc = NULL;
4457 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4458 tree_destroy (t->tree);
4460 if (NULL != t->regex_ctx)
4461 regex_cancel_search (t->regex_ctx);
4462 if (NULL != t->dht_get_type)
4463 GNUNET_DHT_get_stop (t->dht_get_type);
4464 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4465 GNUNET_SCHEDULER_cancel (t->timeout_task);
4466 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4467 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4470 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4477 * Create a new tunnel
4479 * @param owner Who is the owner of the tunnel (short ID).
4480 * @param tid Tunnel Number of the tunnel.
4481 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4482 * @param local Tunnel Number for the tunnel, for the client point of view.
4484 * @return A new initialized tunnel. NULL on error.
4486 static struct MeshTunnel *
4487 tunnel_new (GNUNET_PEER_Id owner,
4488 MESH_TunnelNumber tid,
4489 struct MeshClient *client,
4490 MESH_TunnelNumber local)
4492 struct MeshTunnel *t;
4493 struct GNUNET_HashCode hash;
4495 if (n_tunnels >= max_tunnels && NULL == client)
4498 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4501 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4502 t->bck_queue_max = t->fwd_queue_max;
4503 t->tree = tree_new (owner);
4505 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4506 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4507 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4508 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4509 t->local_tid = local;
4510 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4512 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4514 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4516 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4517 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4524 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4531 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4533 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4534 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4538 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4548 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4549 * that are no longer needed, as well as nodes of no longer reachable peers.
4550 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4552 * @param t Tunnel from which to remove the path.
4553 * @param peer Short id of the peer which should be removed.
4556 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4560 r = tree_del_peer (t->tree, peer, NULL, NULL);
4563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4564 "Tunnel %u [%u] has no more nodes\n",
4565 t->id.oid, t->id.tid);
4571 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4572 * client when the client disconnects. If the client is not the owner, the
4573 * owner will get notified if no more clients are in the tunnel and the client
4574 * get removed from the tunnel's list.
4576 * @param cls closure (client that is disconnecting)
4577 * @param key the hash of the local tunnel id (used to access the hashmap)
4578 * @param value the value stored at the key (tunnel to destroy)
4580 * @return GNUNET_OK, keep iterating.
4583 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4585 struct MeshTunnel *t = value;
4586 struct MeshClient *c = cls;
4588 send_client_tunnel_disconnect(t, c);
4591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4592 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4593 tunnel_delete_client(t, c);
4594 client_delete_tunnel(c, t);
4597 tunnel_send_destroy(t);
4599 t->destroy = GNUNET_YES;
4606 * Timeout function, destroys tunnel if called
4608 * @param cls Closure (tunnel to destroy).
4609 * @param tc TaskContext
4612 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4614 struct MeshTunnel *t = cls;
4615 struct GNUNET_PeerIdentity id;
4617 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4618 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4620 GNUNET_PEER_resolve(t->id.oid, &id);
4621 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4622 "Tunnel %s [%X] timed out. Destroying.\n",
4623 GNUNET_i2s(&id), t->id.tid);
4624 send_clients_tunnel_destroy (t);
4629 * Resets the tunnel timeout. Starts it if no timeout was running.
4631 * @param t Tunnel whose timeout to reset.
4633 * TODO use heap to improve efficiency of scheduler.
4636 tunnel_reset_timeout (struct MeshTunnel *t)
4638 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4639 GNUNET_SCHEDULER_cancel (t->timeout_task);
4641 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4642 (refresh_path_time, 4), &tunnel_timeout, t);
4646 /******************************************************************************/
4647 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4648 /******************************************************************************/
4651 * Function to send a create path packet to a peer.
4653 * @param cls closure
4654 * @param size number of bytes available in buf
4655 * @param buf where the callee should write the message
4656 * @return number of bytes written to buf
4659 send_core_path_create (void *cls, size_t size, void *buf)
4661 struct MeshPathInfo *info = cls;
4662 struct GNUNET_MESH_ManipulatePath *msg;
4663 struct GNUNET_PeerIdentity *peer_ptr;
4664 struct MeshTunnel *t = info->t;
4665 struct MeshPeerPath *p = info->path;
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4672 sizeof (struct GNUNET_MESH_ManipulatePath) +
4673 p->length * sizeof (struct GNUNET_PeerIdentity);
4675 if (size < size_needed || NULL == buf)
4680 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4681 msg->header.size = htons (size_needed);
4682 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4683 msg->tid = ntohl (t->id.tid);
4686 if (GNUNET_YES == t->speed_min)
4687 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4688 if (GNUNET_YES == t->nobuffer)
4689 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4690 msg->opt = htonl(opt);
4693 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4694 for (i = 0; i < p->length; i++)
4696 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4703 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4709 * Fill the core buffer
4711 * @param cls closure (data itself)
4712 * @param size number of bytes available in buf
4713 * @param buf where the callee should write the message
4715 * @return number of bytes written to buf
4718 send_core_data_multicast (void *cls, size_t size, void *buf)
4720 struct MeshTransmissionDescriptor *info = cls;
4723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4724 GNUNET_assert (NULL != info);
4725 GNUNET_assert (NULL != info->peer);
4726 total_size = info->mesh_data->data_len;
4727 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4729 if (total_size > size)
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4735 memcpy (buf, info->mesh_data->data, total_size);
4738 struct GNUNET_MESH_Multicast *mc;
4739 struct GNUNET_MessageHeader *mh;
4742 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4744 mc = (struct GNUNET_MESH_Multicast *) mh;
4745 mh = (struct GNUNET_MessageHeader *) &mc[1];
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4747 " multicast, payload type %s\n",
4748 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4750 " multicast, payload size %u\n", ntohs (mh->size));
4754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4755 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4759 data_descriptor_decrement_rc (info->mesh_data);
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4768 * Creates a path ack message in buf and frees all unused resources.
4770 * @param cls closure (MeshTransmissionDescriptor)
4771 * @param size number of bytes available in buf
4772 * @param buf where the callee should write the message
4773 * @return number of bytes written to buf
4776 send_core_path_ack (void *cls, size_t size, void *buf)
4778 struct MeshTransmissionDescriptor *info = cls;
4779 struct GNUNET_MESH_PathACK *msg = buf;
4781 GNUNET_assert (NULL != info);
4782 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4787 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4788 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4789 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4790 msg->tid = htonl (info->origin->tid);
4791 msg->peer_id = my_full_id;
4794 /* TODO add signature */
4796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4797 return sizeof (struct GNUNET_MESH_PathACK);
4802 * Free a transmission that was already queued with all resources
4803 * associated to the request.
4805 * @param queue Queue handler to cancel.
4806 * @param clear_cls Is it necessary to free associated cls?
4809 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4811 struct MeshTransmissionDescriptor *dd;
4812 struct MeshPathInfo *path_info;
4813 struct MeshTunnelChildInfo *cinfo;
4814 struct GNUNET_PeerIdentity id;
4818 if (GNUNET_YES == clear_cls)
4820 switch (queue->type)
4822 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4823 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4824 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4826 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4827 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4828 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4829 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4830 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4831 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4833 " prebuilt message\n");
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4836 GNUNET_MESH_DEBUG_M2S(queue->type));
4838 data_descriptor_decrement_rc (dd->mesh_data);
4840 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4842 path_info = queue->cls;
4843 path_destroy (path_info->path);
4847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4848 " type %s unknown!\n",
4849 GNUNET_MESH_DEBUG_M2S(queue->type));
4851 GNUNET_free_non_null (queue->cls);
4853 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4854 queue->peer->queue_tail,
4857 /* Delete from child_fc in the appropiate tunnel */
4858 max = queue->tunnel->fwd_queue_max;
4859 GNUNET_PEER_resolve (queue->peer->id, &id);
4860 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4863 for (i = 0; i < cinfo->send_buffer_n; i++)
4866 i2 = (cinfo->send_buffer_start + i) % max;
4867 if (cinfo->send_buffer[i2] == queue)
4869 /* Found corresponding entry in the send_buffer. Move all others back. */
4874 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4876 j2 = (cinfo->send_buffer_start + j) % max;
4877 j3 = (cinfo->send_buffer_start + j + 1) % max;
4878 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4881 cinfo->send_buffer[j3] = NULL;
4882 cinfo->send_buffer_n--;
4887 GNUNET_free (queue);
4892 * @brief Get the next transmittable message from the queue.
4894 * This will be the head, except in the case of being a data packet
4895 * not allowed by the destination peer.
4897 * @param peer Destination peer.
4899 * @return The next viable MeshPeerQueue element to send to that peer.
4900 * NULL when there are no transmittable messages.
4902 struct MeshPeerQueue *
4903 queue_get_next (const struct MeshPeerInfo *peer)
4905 struct MeshPeerQueue *q;
4906 struct MeshTunnel *t;
4907 struct MeshTransmissionDescriptor *info;
4908 struct MeshTunnelChildInfo *cinfo;
4909 struct GNUNET_MESH_Unicast *ucast;
4910 struct GNUNET_MESH_ToOrigin *to_orig;
4911 struct GNUNET_MESH_Multicast *mcast;
4912 struct GNUNET_PeerIdentity id;
4916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4917 for (q = peer->queue_head; NULL != q; q = q->next)
4921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4923 GNUNET_MESH_DEBUG_M2S(q->type));
4926 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4927 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4928 pid = ntohl (ucast->pid);
4929 GNUNET_PEER_resolve (info->peer->id, &id);
4930 cinfo = tunnel_get_neighbor_fc(t, &id);
4931 ack = cinfo->fwd_ack;
4933 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4934 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4935 pid = ntohl (to_orig->pid);
4938 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4939 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4940 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4942 // Not a multicast payload: multicast control traffic (destroy, etc)
4945 pid = ntohl (mcast->pid);
4946 GNUNET_PEER_resolve (info->peer->id, &id);
4947 cinfo = tunnel_get_neighbor_fc(t, &id);
4948 ack = cinfo->fwd_ack;
4951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4956 "********* ACK: %u, PID: %u\n",
4958 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4967 "********* NEXT!\n");
4970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4971 "********* nothing found\n");
4977 * Core callback to write a queued packet to core buffer
4979 * @param cls Closure (peer info).
4980 * @param size Number of bytes available in buf.
4981 * @param buf Where the to write the message.
4983 * @return number of bytes written to buf
4986 queue_send (void *cls, size_t size, void *buf)
4988 struct MeshPeerInfo *peer = cls;
4989 struct GNUNET_MessageHeader *msg;
4990 struct MeshPeerQueue *queue;
4991 struct MeshTunnel *t;
4992 struct MeshTunnelChildInfo *cinfo;
4993 struct GNUNET_PeerIdentity dst_id;
4996 peer->core_transmit = NULL;
4999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5000 queue = queue_get_next (peer);
5002 /* Queue has no internal mesh traffic nor sendable payload */
5005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5006 if (NULL == peer->queue_head)
5007 GNUNET_break (0); // Should've been canceled
5010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5012 GNUNET_PEER_resolve (peer->id, &dst_id);
5013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5014 "********* towards %s\n",
5015 GNUNET_i2s(&dst_id));
5016 /* Check if buffer size is enough for the message */
5017 if (queue->size > size)
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5020 "********* not enough room, reissue\n");
5021 peer->core_transmit =
5022 GNUNET_CORE_notify_transmit_ready (core_handle,
5025 GNUNET_TIME_UNIT_FOREVER_REL,
5032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5035 GNUNET_assert (0 < t->pending_messages);
5036 t->pending_messages--;
5037 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5041 "********* unicast: t->q (%u/%u)\n",
5042 t->fwd_queue_n, t->fwd_queue_max);
5044 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5051 switch (queue->type)
5054 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5055 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5056 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5057 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5058 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5060 "********* raw: %s\n",
5061 GNUNET_MESH_DEBUG_M2S (queue->type));
5063 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5064 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5065 data_size = send_core_data_raw (queue->cls, size, buf);
5066 msg = (struct GNUNET_MessageHeader *) buf;
5067 switch (ntohs (msg->type)) // Type of preconstructed message
5069 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5070 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5072 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5073 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5079 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5082 struct MeshTransmissionDescriptor *info = queue->cls;
5084 if ((1 == info->mesh_data->reference_counter
5085 && GNUNET_YES == t->speed_min)
5087 (info->mesh_data->total_out == info->mesh_data->reference_counter
5088 && GNUNET_NO == t->speed_min))
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5091 "********* considered sent\n");
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5097 "********* NOT considered sent yet\n");
5098 t->pending_messages++;
5101 data_size = send_core_data_multicast(queue->cls, size, buf);
5102 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5104 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5106 data_size = send_core_path_create (queue->cls, size, buf);
5108 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5110 data_size = send_core_path_ack (queue->cls, size, buf);
5112 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5114 data_size = send_core_data_multicast (queue->cls, size, buf);
5118 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5119 "********* type unknown: %u\n",
5123 switch (queue->type)
5125 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5126 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5127 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5128 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5129 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5133 "at pos %u (%p) != %p\n",
5134 cinfo->send_buffer_start,
5135 cinfo->send_buffer[cinfo->send_buffer_start],
5138 if (cinfo->send_buffer_n > 0)
5140 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5141 cinfo->send_buffer_n--;
5142 cinfo->send_buffer_start++;
5143 cinfo->send_buffer_start %= t->fwd_queue_max;
5154 /* Free queue, but cls was freed by send_core_* */
5155 queue_destroy (queue, GNUNET_NO);
5157 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5163 /* If more data in queue, send next */
5164 queue = queue_get_next(peer);
5167 struct GNUNET_PeerIdentity id;
5169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5170 GNUNET_PEER_resolve (peer->id, &id);
5171 peer->core_transmit =
5172 GNUNET_CORE_notify_transmit_ready(core_handle,
5175 GNUNET_TIME_UNIT_FOREVER_REL,
5183 if (NULL != peer->queue_head)
5185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5186 "********* %s stalled\n",
5187 GNUNET_i2s(&my_full_id));
5189 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5190 if (GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5191 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(MESH_POLL_TIME,
5192 &tunnel_poll, cinfo);
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5201 * @brief Queue and pass message to core when possible.
5203 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5204 * and accounts for it. In case the queue is full, the message is dropped and
5207 * Otherwise, message is treated as internal and allowed to go regardless of
5210 * @param cls Closure (@c type dependant). It will be used by queue_send to
5211 * build the message to be sent if not already prebuilt.
5212 * @param type Type of the message, 0 for a raw message.
5213 * @param size Size of the message.
5214 * @param dst Neighbor to send message to.
5215 * @param t Tunnel this message belongs to.
5218 queue_add (void *cls, uint16_t type, size_t size,
5219 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5221 struct MeshPeerQueue *queue;
5222 struct MeshTunnelChildInfo *cinfo;
5223 struct GNUNET_PeerIdentity id;
5229 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5230 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5232 n = &t->fwd_queue_n;
5233 max = &t->fwd_queue_max;
5235 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5237 n = &t->bck_queue_n;
5238 max = &t->bck_queue_max;
5245 GNUNET_STATISTICS_update(stats,
5246 "# messages dropped (buffer full)",
5248 return; // Drop message
5252 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5258 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5259 GNUNET_PEER_resolve (dst->id, &id);
5260 if (NULL == dst->core_transmit)
5262 dst->core_transmit =
5263 GNUNET_CORE_notify_transmit_ready (core_handle,
5266 GNUNET_TIME_UNIT_FOREVER_REL,
5272 t->pending_messages++;
5273 if (NULL == n) // Is this internal mesh traffic?
5276 // It's payload, keep track of buffer per peer.
5277 cinfo = tunnel_get_neighbor_fc(t, &id);
5278 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5279 if (NULL != cinfo->send_buffer[i])
5281 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5282 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5283 cinfo->send_buffer_start++;
5284 cinfo->send_buffer_start %= t->fwd_queue_max;
5288 cinfo->send_buffer_n++;
5290 cinfo->send_buffer[i] = queue;
5291 if (cinfo->send_buffer_n > t->fwd_queue_max)
5294 cinfo->send_buffer_n = t->fwd_queue_max;
5299 /******************************************************************************/
5300 /******************** MESH NETWORK HANDLERS **************************/
5301 /******************************************************************************/
5305 * Core handler for path creation
5307 * @param cls closure
5308 * @param message message
5309 * @param peer peer identity this notification is about
5310 * @param atsi performance data
5311 * @param atsi_count number of records in 'atsi'
5313 * @return GNUNET_OK to keep the connection open,
5314 * GNUNET_SYSERR to close it (signal serious error)
5317 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5318 const struct GNUNET_MessageHeader *message,
5319 const struct GNUNET_ATS_Information *atsi,
5320 unsigned int atsi_count)
5322 unsigned int own_pos;
5325 MESH_TunnelNumber tid;
5326 struct GNUNET_MESH_ManipulatePath *msg;
5327 struct GNUNET_PeerIdentity *pi;
5328 struct GNUNET_HashCode hash;
5329 struct MeshPeerPath *path;
5330 struct MeshPeerInfo *dest_peer_info;
5331 struct MeshPeerInfo *orig_peer_info;
5332 struct MeshTunnel *t;
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5335 "Received a path create msg [%s]\n",
5336 GNUNET_i2s (&my_full_id));
5337 size = ntohs (message->size);
5338 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5340 GNUNET_break_op (0);
5344 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5345 if (size % sizeof (struct GNUNET_PeerIdentity))
5347 GNUNET_break_op (0);
5350 size /= sizeof (struct GNUNET_PeerIdentity);
5353 GNUNET_break_op (0);
5356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5357 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5359 tid = ntohl (msg->tid);
5360 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5362 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5363 t = tunnel_get (pi, tid);
5364 if (NULL == t) // FIXME only for INCOMING tunnels?
5368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5369 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5372 // FIXME notify failure
5375 opt = ntohl (msg->opt);
5376 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5377 GNUNET_YES : GNUNET_NO;
5378 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5380 t->nobuffer = GNUNET_YES;
5381 t->last_fwd_ack = t->fwd_pid + 1;
5383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5384 " speed_min: %d, nobuffer:%d\n",
5385 t->speed_min, t->nobuffer);
5387 if (GNUNET_YES == t->nobuffer)
5389 t->bck_queue_max = 1;
5390 t->fwd_queue_max = 1;
5393 // FIXME only assign a local tid if a local client is interested (on demand)
5394 while (NULL != tunnel_get_incoming (next_local_tid))
5395 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5396 t->local_tid_dest = next_local_tid++;
5397 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5400 tunnel_reset_timeout (t);
5401 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5403 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5404 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5412 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5413 if (NULL == dest_peer_info)
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5416 " Creating PeerInfo for destination.\n");
5417 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5418 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5419 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5421 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5423 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5424 if (NULL == orig_peer_info)
5426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5427 " Creating PeerInfo for origin.\n");
5428 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5429 orig_peer_info->id = GNUNET_PEER_intern (pi);
5430 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5431 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5434 path = path_new (size);
5436 for (i = 0; i < size; i++)
5438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5439 GNUNET_i2s (&pi[i]));
5440 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5441 if (path->peers[i] == myid)
5444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5447 /* cannot be self, must be 'not found' */
5448 /* create path: self not found in path through self */
5449 GNUNET_break_op (0);
5450 path_destroy (path);
5454 path_add_to_peers (path, GNUNET_NO);
5455 tunnel_add_path (t, path, own_pos);
5456 if (own_pos == size - 1)
5458 /* It is for us! Send ack. */
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5460 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5461 if (NULL == t->peers)
5463 /* New tunnel! Notify clients on first payload message. */
5464 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5466 GNUNET_break (GNUNET_SYSERR !=
5467 GNUNET_CONTAINER_multihashmap_put (t->peers,
5468 &my_full_id.hashPubKey,
5471 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5476 struct MeshPeerPath *path2;
5478 /* It's for somebody else! Retransmit. */
5479 path2 = path_duplicate (path);
5480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5481 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5482 path2 = path_duplicate (path);
5483 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5484 send_create_path (dest_peer_info, path, t);
5491 * Core handler for path destruction
5493 * @param cls closure
5494 * @param message message
5495 * @param peer peer identity this notification is about
5496 * @param atsi performance data
5497 * @param atsi_count number of records in 'atsi'
5499 * @return GNUNET_OK to keep the connection open,
5500 * GNUNET_SYSERR to close it (signal serious error)
5503 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5504 const struct GNUNET_MessageHeader *message,
5505 const struct GNUNET_ATS_Information *atsi,
5506 unsigned int atsi_count)
5508 struct GNUNET_MESH_ManipulatePath *msg;
5509 struct GNUNET_PeerIdentity *pi;
5510 struct MeshPeerPath *path;
5511 struct MeshTunnel *t;
5512 unsigned int own_pos;
5516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5517 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5518 size = ntohs (message->size);
5519 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5521 GNUNET_break_op (0);
5525 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5526 if (size % sizeof (struct GNUNET_PeerIdentity))
5528 GNUNET_break_op (0);
5531 size /= sizeof (struct GNUNET_PeerIdentity);
5534 GNUNET_break_op (0);
5537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5539 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5540 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5542 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5544 t = tunnel_get (pi, ntohl (msg->tid));
5547 /* TODO notify back: we don't know this tunnel */
5548 GNUNET_break_op (0);
5551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5552 path = path_new (size);
5554 for (i = 0; i < size; i++)
5556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5557 GNUNET_i2s (&pi[i]));
5558 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5559 if (path->peers[i] == myid)
5562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5563 if (own_pos < path->length - 1)
5564 send_prebuilt_message (message, &pi[own_pos + 1], t);
5566 send_client_tunnel_disconnect(t, NULL);
5568 tunnel_delete_peer (t, path->peers[path->length - 1]);
5569 path_destroy (path);
5575 * Core handler for notifications of broken paths
5577 * @param cls closure
5578 * @param message message
5579 * @param peer peer identity this notification is about
5580 * @param atsi performance data
5581 * @param atsi_count number of records in 'atsi'
5583 * @return GNUNET_OK to keep the connection open,
5584 * GNUNET_SYSERR to close it (signal serious error)
5587 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5588 const struct GNUNET_MessageHeader *message,
5589 const struct GNUNET_ATS_Information *atsi,
5590 unsigned int atsi_count)
5592 struct GNUNET_MESH_PathBroken *msg;
5593 struct MeshTunnel *t;
5595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5596 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5597 msg = (struct GNUNET_MESH_PathBroken *) message;
5598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5599 GNUNET_i2s (&msg->peer1));
5600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5601 GNUNET_i2s (&msg->peer2));
5602 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5605 GNUNET_break_op (0);
5608 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5609 GNUNET_PEER_search (&msg->peer2));
5616 * Core handler for tunnel destruction
5618 * @param cls closure
5619 * @param message message
5620 * @param peer peer identity this notification is about
5621 * @param atsi performance data
5622 * @param atsi_count number of records in 'atsi'
5624 * @return GNUNET_OK to keep the connection open,
5625 * GNUNET_SYSERR to close it (signal serious error)
5628 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5629 const struct GNUNET_MessageHeader *message,
5630 const struct GNUNET_ATS_Information *atsi,
5631 unsigned int atsi_count)
5633 struct GNUNET_MESH_TunnelDestroy *msg;
5634 struct MeshTunnel *t;
5636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5637 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5638 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5640 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5641 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5644 /* Probably already got the message from another path,
5645 * destroyed the tunnel and retransmitted to children.
5648 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5651 if (t->id.oid == myid)
5653 GNUNET_break_op (0);
5656 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5658 /* Tunnel was incoming, notify clients */
5659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5660 t->local_tid, t->local_tid_dest);
5661 send_clients_tunnel_destroy (t);
5663 tunnel_send_destroy (t);
5664 t->destroy = GNUNET_YES;
5665 // TODO: add timeout to destroy the tunnel anyway
5671 * Core handler for mesh network traffic going from the origin to a peer
5673 * @param cls closure
5674 * @param peer peer identity this notification is about
5675 * @param message message
5676 * @param atsi performance data
5677 * @param atsi_count number of records in 'atsi'
5678 * @return GNUNET_OK to keep the connection open,
5679 * GNUNET_SYSERR to close it (signal serious error)
5682 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5683 const struct GNUNET_MessageHeader *message,
5684 const struct GNUNET_ATS_Information *atsi,
5685 unsigned int atsi_count)
5687 struct GNUNET_MESH_Unicast *msg;
5688 struct GNUNET_PeerIdentity *neighbor;
5689 struct MeshTunnelChildInfo *cinfo;
5690 struct MeshTunnel *t;
5691 GNUNET_PEER_Id dest_id;
5696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5699 size = ntohs (message->size);
5701 sizeof (struct GNUNET_MESH_Unicast) +
5702 sizeof (struct GNUNET_MessageHeader))
5707 msg = (struct GNUNET_MESH_Unicast *) message;
5708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5709 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5711 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5714 /* TODO notify back: we don't know this tunnel */
5715 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5716 GNUNET_break_op (0);
5719 pid = ntohl (msg->pid);
5720 if (t->fwd_pid == pid)
5722 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5724 " Already seen pid %u, DROPPING!\n", pid);
5729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5730 " pid %u not seen yet, forwarding\n", pid);
5733 t->skip += (pid - t->fwd_pid) - 1;
5736 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5738 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5739 GNUNET_break_op (0);
5740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5741 "Received PID %u, ACK %u\n",
5742 pid, t->last_fwd_ack);
5746 tunnel_reset_timeout (t);
5747 dest_id = GNUNET_PEER_search (&msg->destination);
5748 if (dest_id == myid)
5750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5751 " it's for us! sending to clients...\n");
5752 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5753 send_subscribed_clients (message, &msg[1].header, t);
5754 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5757 ttl = ntohl (msg->ttl);
5758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5761 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5762 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5763 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5767 " not for us, retransmitting...\n");
5769 neighbor = tree_get_first_hop (t->tree, dest_id);
5770 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5771 cinfo->fwd_pid = pid;
5772 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5775 if (GNUNET_YES == t->nobuffer &&
5776 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5778 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5779 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5780 GNUNET_break_op (0);
5783 send_prebuilt_message (message, neighbor, t);
5784 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5790 * Core handler for mesh network traffic going from the origin to all peers
5792 * @param cls closure
5793 * @param message message
5794 * @param peer peer identity this notification is about
5795 * @param atsi performance data
5796 * @param atsi_count number of records in 'atsi'
5797 * @return GNUNET_OK to keep the connection open,
5798 * GNUNET_SYSERR to close it (signal serious error)
5800 * TODO: Check who we got this from, to validate route.
5803 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5804 const struct GNUNET_MessageHeader *message,
5805 const struct GNUNET_ATS_Information *atsi,
5806 unsigned int atsi_count)
5808 struct GNUNET_MESH_Multicast *msg;
5809 struct MeshTunnel *t;
5813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5815 size = ntohs (message->size);
5816 if (sizeof (struct GNUNET_MESH_Multicast) +
5817 sizeof (struct GNUNET_MessageHeader) > size)
5819 GNUNET_break_op (0);
5822 msg = (struct GNUNET_MESH_Multicast *) message;
5823 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5827 /* TODO notify that we dont know that tunnel */
5828 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5829 GNUNET_break_op (0);
5832 pid = ntohl (msg->pid);
5833 if (t->fwd_pid == pid)
5835 /* already seen this packet, drop */
5836 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5838 " Already seen pid %u, DROPPING!\n", pid);
5839 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5845 " pid %u not seen yet, forwarding\n", pid);
5847 t->skip += (pid - t->fwd_pid) - 1;
5849 tunnel_reset_timeout (t);
5851 /* Transmit to locally interested clients */
5852 if (NULL != t->peers &&
5853 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5855 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5856 send_subscribed_clients (message, &msg[1].header, t);
5857 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5860 if (ntohl (msg->ttl) == 0)
5862 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5863 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5864 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5867 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5868 tunnel_send_multicast (t, message);
5874 * Core handler for mesh network traffic toward the owner of a tunnel
5876 * @param cls closure
5877 * @param message message
5878 * @param peer peer identity this notification is about
5879 * @param atsi performance data
5880 * @param atsi_count number of records in 'atsi'
5882 * @return GNUNET_OK to keep the connection open,
5883 * GNUNET_SYSERR to close it (signal serious error)
5886 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5887 const struct GNUNET_MessageHeader *message,
5888 const struct GNUNET_ATS_Information *atsi,
5889 unsigned int atsi_count)
5891 struct GNUNET_MESH_ToOrigin *msg;
5892 struct GNUNET_PeerIdentity id;
5893 struct MeshPeerInfo *peer_info;
5894 struct MeshTunnel *t;
5895 struct MeshTunnelChildInfo *cinfo;
5899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5901 size = ntohs (message->size);
5902 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5903 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5905 GNUNET_break_op (0);
5908 msg = (struct GNUNET_MESH_ToOrigin *) message;
5909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5910 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5911 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5912 pid = ntohl (msg->pid);
5916 /* TODO notify that we dont know this tunnel (whom)? */
5917 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5918 GNUNET_break_op (0);
5919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5920 "Received to_origin with PID %u on unknown tunnel\n",
5925 cinfo = tunnel_get_neighbor_fc(t, peer);
5932 if (cinfo->bck_pid == pid)
5934 /* already seen this packet, drop */
5935 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5937 " Already seen pid %u, DROPPING!\n", pid);
5938 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5943 " pid %u not seen yet, forwarding\n", pid);
5944 cinfo->bck_pid = pid;
5946 if (NULL != t->owner)
5949 struct GNUNET_MESH_ToOrigin *copy;
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5952 " it's for us! sending to clients...\n");
5953 /* TODO signature verification */
5954 memcpy (cbuf, message, size);
5955 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5956 copy->tid = htonl (t->local_tid);
5958 copy->pid = htonl (t->bck_pid);
5959 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5960 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5961 ©->header, GNUNET_NO);
5962 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5966 " not for us, retransmitting...\n");
5968 peer_info = peer_info_get (&msg->oid);
5969 if (NULL == peer_info)
5971 /* unknown origin of tunnel */
5975 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5976 send_prebuilt_message (message, &id, t);
5977 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5984 * Core handler for mesh network traffic point-to-point acks.
5986 * @param cls closure
5987 * @param message message
5988 * @param peer peer identity this notification is about
5989 * @param atsi performance data
5990 * @param atsi_count number of records in 'atsi'
5992 * @return GNUNET_OK to keep the connection open,
5993 * GNUNET_SYSERR to close it (signal serious error)
5996 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5997 const struct GNUNET_MessageHeader *message,
5998 const struct GNUNET_ATS_Information *atsi,
5999 unsigned int atsi_count)
6001 struct GNUNET_MESH_ACK *msg;
6002 struct MeshTunnel *t;
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6007 msg = (struct GNUNET_MESH_ACK *) message;
6009 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6013 /* TODO notify that we dont know this tunnel (whom)? */
6014 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6017 ack = ntohl (msg->pid);
6018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6020 /* Is this a forward or backward ACK? */
6021 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6023 struct MeshTunnelChildInfo *cinfo;
6026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6027 cinfo = tunnel_get_neighbor_fc (t, peer);
6028 cinfo->fwd_ack = ack;
6029 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6030 tunnel_unlock_fwd_queues (t);
6034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6036 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6037 tunnel_unlock_bck_queue (t);
6044 * Core handler for mesh network traffic point-to-point ack polls.
6046 * @param cls closure
6047 * @param message message
6048 * @param peer peer identity this notification is about
6049 * @param atsi performance data
6050 * @param atsi_count number of records in 'atsi'
6052 * @return GNUNET_OK to keep the connection open,
6053 * GNUNET_SYSERR to close it (signal serious error)
6056 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6057 const struct GNUNET_MessageHeader *message,
6058 const struct GNUNET_ATS_Information *atsi,
6059 unsigned int atsi_count)
6061 struct GNUNET_MESH_Poll *msg;
6062 struct MeshTunnel *t;
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6067 msg = (struct GNUNET_MESH_Poll *) message;
6069 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6073 /* TODO notify that we dont know this tunnel (whom)? */
6074 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6075 GNUNET_break_op (0);
6079 /* Is this a forward or backward ACK? */
6080 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6082 struct MeshTunnelChildInfo *cinfo;
6084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6085 cinfo = tunnel_get_neighbor_fc (t, peer);
6086 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6087 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6092 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6100 * Core handler for path ACKs
6102 * @param cls closure
6103 * @param message message
6104 * @param peer peer identity this notification is about
6105 * @param atsi performance data
6106 * @param atsi_count number of records in 'atsi'
6108 * @return GNUNET_OK to keep the connection open,
6109 * GNUNET_SYSERR to close it (signal serious error)
6112 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6113 const struct GNUNET_MessageHeader *message,
6114 const struct GNUNET_ATS_Information *atsi,
6115 unsigned int atsi_count)
6117 struct GNUNET_MESH_PathACK *msg;
6118 struct GNUNET_PeerIdentity id;
6119 struct MeshPeerInfo *peer_info;
6120 struct MeshPeerPath *p;
6121 struct MeshTunnel *t;
6123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6124 GNUNET_i2s (&my_full_id));
6125 msg = (struct GNUNET_MESH_PathACK *) message;
6126 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6129 /* TODO notify that we don't know the tunnel */
6130 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6132 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6136 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6138 peer_info = peer_info_get (&msg->peer_id);
6139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6140 GNUNET_i2s (&msg->peer_id));
6141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6144 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6147 "connect_by_string completed, stopping search\n");
6148 regex_cancel_search (t->regex_ctx);
6149 t->regex_ctx = NULL;
6152 /* Add paths to peers? */
6153 p = tree_get_path_to_peer (t->tree, peer_info->id);
6156 path_add_to_peers (p, GNUNET_YES);
6164 /* Message for us? */
6165 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6168 if (NULL == t->owner)
6170 GNUNET_break_op (0);
6173 if (NULL != t->dht_get_type)
6175 GNUNET_DHT_get_stop (t->dht_get_type);
6176 t->dht_get_type = NULL;
6178 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6180 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6181 send_client_peer_connected (t, peer_info->id);
6186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6187 " not for us, retransmitting...\n");
6188 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6189 peer_info = peer_info_get (&msg->oid);
6190 if (NULL == peer_info)
6192 /* If we know the tunnel, we should DEFINITELY know the peer */
6196 send_prebuilt_message (message, &id, t);
6202 * Core handler for mesh keepalives.
6204 * @param cls closure
6205 * @param message message
6206 * @param peer peer identity this notification is about
6207 * @param atsi performance data
6208 * @param atsi_count number of records in 'atsi'
6209 * @return GNUNET_OK to keep the connection open,
6210 * GNUNET_SYSERR to close it (signal serious error)
6212 * TODO: Check who we got this from, to validate route.
6215 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6216 const struct GNUNET_MessageHeader *message,
6217 const struct GNUNET_ATS_Information *atsi,
6218 unsigned int atsi_count)
6220 struct GNUNET_MESH_TunnelKeepAlive *msg;
6221 struct MeshTunnel *t;
6223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6226 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6227 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6231 /* TODO notify that we dont know that tunnel */
6232 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6233 GNUNET_break_op (0);
6237 tunnel_reset_timeout (t);
6239 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6240 tunnel_send_multicast (t, message);
6247 * Functions to handle messages from core
6249 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6250 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6251 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6252 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6253 sizeof (struct GNUNET_MESH_PathBroken)},
6254 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6255 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6256 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6257 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6258 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6259 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6260 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6261 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6262 sizeof (struct GNUNET_MESH_ACK)},
6263 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6264 sizeof (struct GNUNET_MESH_Poll)},
6265 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6266 sizeof (struct GNUNET_MESH_PathACK)},
6272 /******************************************************************************/
6273 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6274 /******************************************************************************/
6277 * deregister_app: iterator for removing each application registered by a client
6279 * @param cls closure
6280 * @param key the hash of the application id (used to access the hashmap)
6281 * @param value the value stored at the key (client)
6283 * @return GNUNET_OK on success
6286 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6288 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6289 GNUNET_break (GNUNET_YES ==
6290 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6296 * notify_client_connection_failure: notify a client that the connection to the
6297 * requested remote peer is not possible (for instance, no route found)
6298 * Function called when the socket is ready to queue more data. "buf" will be
6299 * NULL and "size" zero if the socket was closed for writing in the meantime.
6301 * @param cls closure
6302 * @param size number of bytes available in buf
6303 * @param buf where the callee should write the message
6304 * @return number of bytes written to buf
6307 notify_client_connection_failure (void *cls, size_t size, void *buf)
6310 struct MeshPeerInfo *peer_info;
6311 struct GNUNET_MESH_PeerControl *msg;
6312 struct GNUNET_PeerIdentity id;
6314 if (0 == size && NULL == buf)
6316 // TODO retry? cancel?
6320 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6321 peer_info = (struct MeshPeerInfo *) cls;
6322 msg = (struct GNUNET_MESH_PeerControl *) buf;
6323 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6324 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6325 // msg->tunnel_id = htonl(peer_info->t->tid);
6326 GNUNET_PEER_resolve (peer_info->id, &id);
6327 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6335 * Send keepalive packets for a peer
6337 * @param cls Closure (tunnel for which to send the keepalive).
6338 * @param tc Notification context.
6341 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6343 struct MeshTunnel *t = cls;
6344 struct GNUNET_MESH_TunnelKeepAlive *msg;
6345 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6348 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6349 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6355 "sending keepalive for tunnel %d\n", t->id.tid);
6357 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6358 msg->header.size = htons (size);
6359 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6360 msg->oid = my_full_id;
6361 msg->tid = htonl (t->id.tid);
6362 tunnel_send_multicast (t, &msg->header);
6364 t->path_refresh_task =
6365 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6366 tunnel_reset_timeout(t);
6371 * Function to process paths received for a new peer addition. The recorded
6372 * paths form the initial tunnel, which can be optimized later.
6373 * Called on each result obtained for the DHT search.
6375 * @param cls closure
6376 * @param exp when will this value expire
6377 * @param key key of the result
6378 * @param get_path path of the get request
6379 * @param get_path_length lenght of get_path
6380 * @param put_path path of the put request
6381 * @param put_path_length length of the put_path
6382 * @param type type of the result
6383 * @param size number of bytes in data
6384 * @param data pointer to the result data
6386 * TODO: re-issue the request after certain time? cancel after X results?
6389 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6390 const struct GNUNET_HashCode * key,
6391 const struct GNUNET_PeerIdentity *get_path,
6392 unsigned int get_path_length,
6393 const struct GNUNET_PeerIdentity *put_path,
6394 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6395 size_t size, const void *data)
6397 struct MeshPathInfo *path_info = cls;
6398 struct MeshPeerPath *p;
6399 struct GNUNET_PeerIdentity pi;
6402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6403 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6406 p = path_build_from_dht (get_path, get_path_length, put_path,
6408 path_add_to_peers (p, GNUNET_NO);
6410 for (i = 0; i < path_info->peer->ntunnels; i++)
6412 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6413 peer_info_connect (path_info->peer, path_info->t);
6421 * Function to process paths received for a new peer addition. The recorded
6422 * paths form the initial tunnel, which can be optimized later.
6423 * Called on each result obtained for the DHT search.
6425 * @param cls closure
6426 * @param exp when will this value expire
6427 * @param key key of the result
6428 * @param get_path path of the get request
6429 * @param get_path_length lenght of get_path
6430 * @param put_path path of the put request
6431 * @param put_path_length length of the put_path
6432 * @param type type of the result
6433 * @param size number of bytes in data
6434 * @param data pointer to the result data
6437 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6438 const struct GNUNET_HashCode * key,
6439 const struct GNUNET_PeerIdentity *get_path,
6440 unsigned int get_path_length,
6441 const struct GNUNET_PeerIdentity *put_path,
6442 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6443 size_t size, const void *data)
6445 const struct PBlock *pb = data;
6446 const struct GNUNET_PeerIdentity *pi = &pb->id;
6447 struct MeshTunnel *t = cls;
6448 struct MeshPeerInfo *peer_info;
6449 struct MeshPeerPath *p;
6451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6452 if (size != sizeof (struct PBlock))
6454 GNUNET_break_op (0);
6457 if (ntohl(pb->type) != t->type)
6459 GNUNET_break_op (0);
6462 GNUNET_assert (NULL != t->owner);
6463 peer_info = peer_info_get (pi);
6464 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6466 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6468 p = path_build_from_dht (get_path, get_path_length, put_path,
6470 path_add_to_peers (p, GNUNET_NO);
6472 tunnel_add_peer (t, peer_info);
6473 peer_info_connect (peer_info, t);
6478 * Function to process DHT string to regex matching.
6479 * Called on each result obtained for the DHT search.
6481 * @param cls closure (search context)
6482 * @param exp when will this value expire
6483 * @param key key of the result
6484 * @param get_path path of the get request (not used)
6485 * @param get_path_length lenght of get_path (not used)
6486 * @param put_path path of the put request (not used)
6487 * @param put_path_length length of the put_path (not used)
6488 * @param type type of the result
6489 * @param size number of bytes in data
6490 * @param data pointer to the result data
6493 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6494 const struct GNUNET_HashCode * key,
6495 const struct GNUNET_PeerIdentity *get_path,
6496 unsigned int get_path_length,
6497 const struct GNUNET_PeerIdentity *put_path,
6498 unsigned int put_path_length,
6499 enum GNUNET_BLOCK_Type type,
6500 size_t size, const void *data)
6502 const struct MeshRegexAccept *block = data;
6503 struct MeshRegexSearchContext *ctx = cls;
6504 struct MeshRegexSearchInfo *info = ctx->info;
6505 struct MeshPeerPath *p;
6506 struct MeshPeerInfo *peer_info;
6508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6511 peer_info = peer_info_get(&block->id);
6512 // p = path_build_from_dht (get_path, get_path_length, put_path,
6513 // put_path_length);
6514 // path_add_to_peers (p, GNUNET_NO);
6517 tunnel_add_peer (info->t, peer_info);
6518 peer_info_connect (peer_info, info->t);
6519 if (0 == info->peer)
6521 info->peer = peer_info->id;
6525 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6528 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6529 ®ex_connect_timeout,
6537 * Function to process DHT string to regex matching.
6538 * Called on each result obtained for the DHT search.
6540 * @param cls closure (search context)
6541 * @param exp when will this value expire
6542 * @param key key of the result
6543 * @param get_path path of the get request (not used)
6544 * @param get_path_length lenght of get_path (not used)
6545 * @param put_path path of the put request (not used)
6546 * @param put_path_length length of the put_path (not used)
6547 * @param type type of the result
6548 * @param size number of bytes in data
6549 * @param data pointer to the result data
6551 * TODO: re-issue the request after certain time? cancel after X results?
6554 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6555 const struct GNUNET_HashCode * key,
6556 const struct GNUNET_PeerIdentity *get_path,
6557 unsigned int get_path_length,
6558 const struct GNUNET_PeerIdentity *put_path,
6559 unsigned int put_path_length,
6560 enum GNUNET_BLOCK_Type type,
6561 size_t size, const void *data)
6563 const struct MeshRegexBlock *block = data;
6564 struct MeshRegexSearchContext *ctx = cls;
6565 struct MeshRegexSearchInfo *info = ctx->info;
6569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6570 "DHT GET STRING RETURNED RESULTS\n");
6571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6572 " key: %s\n", GNUNET_h2s (key));
6574 copy = GNUNET_malloc (size);
6575 memcpy (copy, data, size);
6576 GNUNET_break (GNUNET_OK ==
6577 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6578 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6579 len = ntohl (block->n_proof);
6581 char proof[len + 1];
6583 memcpy (proof, &block[1], len);
6585 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6587 GNUNET_break_op (0);
6591 len = strlen (info->description);
6592 if (len == ctx->position) // String processed
6594 if (GNUNET_YES == ntohl (block->accepting))
6596 regex_find_path(key, ctx);
6600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6601 // FIXME REGEX this block not successful, wait for more? start timeout?
6606 regex_next_edge (block, size, ctx);
6611 /******************************************************************************/
6612 /********************* MESH LOCAL HANDLES **************************/
6613 /******************************************************************************/
6617 * Handler for client disconnection
6619 * @param cls closure
6620 * @param client identification of the client; NULL
6621 * for the last call when the server is destroyed
6624 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6626 struct MeshClient *c;
6627 struct MeshClient *next;
6630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6637 // return; uncomment for regex_profiler
6641 if (c->handle != client)
6643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6649 GNUNET_SERVER_client_drop (c->handle);
6650 c->shutting_down = GNUNET_YES;
6651 GNUNET_assert (NULL != c->own_tunnels);
6652 GNUNET_assert (NULL != c->incoming_tunnels);
6653 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6654 &tunnel_destroy_iterator, c);
6655 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6656 &tunnel_destroy_iterator, c);
6657 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6658 &tunnel_destroy_iterator, c);
6659 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6660 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6661 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6663 /* deregister clients applications */
6664 if (NULL != c->apps)
6666 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6667 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6669 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6670 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6672 GNUNET_SCHEDULER_cancel (announce_applications_task);
6673 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6675 if (NULL != c->types)
6676 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6677 for (i = 0; i < c->n_regex; i++)
6679 GNUNET_free (c->regexes[i].regex);
6680 if (NULL != c->regexes[i].dfa)
6681 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6683 GNUNET_free_non_null (c->regexes);
6684 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6685 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6687 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6690 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6699 * Handler for new clients
6701 * @param cls closure
6702 * @param client identification of the client
6703 * @param message the actual message, which includes messages the client wants
6706 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6707 const struct GNUNET_MessageHeader *message)
6709 struct GNUNET_MESH_ClientConnect *cc_msg;
6710 struct MeshClient *c;
6711 GNUNET_MESH_ApplicationType *a;
6718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6719 /* Check data sanity */
6720 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6721 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6722 ntypes = ntohs (cc_msg->types);
6723 napps = ntohs (cc_msg->applications);
6725 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6732 /* Create new client structure */
6733 c = GNUNET_malloc (sizeof (struct MeshClient));
6734 c->id = next_client_id++;
6735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6737 GNUNET_SERVER_client_keep (client);
6738 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6741 GNUNET_MESH_ApplicationType at;
6742 struct GNUNET_HashCode hc;
6744 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6745 for (i = 0; i < napps; i++)
6748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6749 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6750 /* store in clients hashmap */
6751 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6752 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6753 /* store in global hashmap, for announcements */
6754 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6755 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6757 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6758 announce_applications_task =
6759 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6765 struct GNUNET_HashCode hc;
6767 t = (uint16_t *) & a[napps];
6768 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6769 for (i = 0; i < ntypes; i++)
6772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6773 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6775 /* store in clients hashmap */
6776 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6777 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6778 /* store in global hashmap */
6779 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6780 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6784 " client has %u+%u subscriptions\n", napps, ntypes);
6786 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6787 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6788 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6789 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6790 GNUNET_SERVER_notification_context_add (nc, client);
6791 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6799 * Handler for clients announcing available services by a regular expression.
6801 * @param cls closure
6802 * @param client identification of the client
6803 * @param message the actual message, which includes messages the client wants
6806 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6807 const struct GNUNET_MessageHeader *message)
6809 const struct GNUNET_MESH_RegexAnnounce *msg;
6810 struct MeshRegexDescriptor rd;
6811 struct MeshClient *c;
6815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6817 /* Sanity check for client registration */
6818 if (NULL == (c = client_get (client)))
6821 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6826 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6827 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6828 regex = GNUNET_malloc (len + 1);
6829 memcpy (regex, &msg[1], len);
6832 rd.compression = ntohs (msg->compression_characters);
6834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6837 GNUNET_array_append (c->regexes, c->n_regex, rd);
6838 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6840 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6846 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6852 * Handler for requests of new tunnels
6854 * @param cls closure
6855 * @param client identification of the client
6856 * @param message the actual message
6859 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6860 const struct GNUNET_MessageHeader *message)
6862 struct GNUNET_MESH_TunnelMessage *t_msg;
6863 struct MeshTunnel *t;
6864 struct MeshClient *c;
6865 MESH_TunnelNumber tid;
6867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6869 /* Sanity check for client registration */
6870 if (NULL == (c = client_get (client)))
6873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6878 /* Message sanity check */
6879 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6882 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6886 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6887 /* Sanity check for tunnel numbering */
6888 tid = ntohl (t_msg->tunnel_id);
6889 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6895 /* Sanity check for duplicate tunnel IDs */
6896 if (NULL != tunnel_get_by_local_id (c, tid))
6899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6903 while (NULL != tunnel_get_by_pi (myid, next_tid))
6904 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6905 t = tunnel_new (myid, next_tid++, c, tid);
6908 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6913 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6915 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6916 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6919 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6925 * Handler for requests of deleting tunnels
6927 * @param cls closure
6928 * @param client identification of the client
6929 * @param message the actual message
6932 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6933 const struct GNUNET_MessageHeader *message)
6935 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6936 struct MeshClient *c;
6937 struct MeshTunnel *t;
6938 MESH_TunnelNumber tid;
6940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6941 "Got a DESTROY TUNNEL from client!\n");
6943 /* Sanity check for client registration */
6944 if (NULL == (c = client_get (client)))
6947 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6952 /* Message sanity check */
6953 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6960 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6962 /* Retrieve tunnel */
6963 tid = ntohl (tunnel_msg->tunnel_id);
6964 t = tunnel_get_by_local_id(c, tid);
6967 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6969 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6972 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6974 client_ignore_tunnel (c, t);
6976 // TODO: when to destroy incoming tunnel?
6977 if (t->nclients == 0)
6979 GNUNET_assert (GNUNET_YES ==
6980 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6982 GNUNET_assert (GNUNET_YES ==
6983 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6984 &my_full_id.hashPubKey,
6988 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6991 send_client_tunnel_disconnect(t, c);
6992 client_delete_tunnel(c, t);
6994 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6996 tunnel_send_destroy (t);
6997 t->destroy = GNUNET_YES;
6998 // The tunnel will be destroyed when the last message is transmitted.
6999 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7005 * Handler for requests of seeting tunnel's speed.
7007 * @param cls Closure (unused).
7008 * @param client Identification of the client.
7009 * @param message The actual message.
7012 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7013 const struct GNUNET_MessageHeader *message)
7015 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7016 struct MeshClient *c;
7017 struct MeshTunnel *t;
7018 MESH_TunnelNumber tid;
7020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7021 "Got a SPEED request from client!\n");
7023 /* Sanity check for client registration */
7024 if (NULL == (c = client_get (client)))
7027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7033 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7035 /* Retrieve tunnel */
7036 tid = ntohl (tunnel_msg->tunnel_id);
7037 t = tunnel_get_by_local_id(c, tid);
7040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7046 switch (ntohs(message->type))
7048 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7049 t->speed_min = GNUNET_YES;
7051 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7052 t->speed_min = GNUNET_NO;
7057 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7062 * Handler for requests of seeting tunnel's buffering policy.
7064 * @param cls Closure (unused).
7065 * @param client Identification of the client.
7066 * @param message The actual message.
7069 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7070 const struct GNUNET_MessageHeader *message)
7072 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7073 struct MeshClient *c;
7074 struct MeshTunnel *t;
7075 MESH_TunnelNumber tid;
7077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7078 "Got a BUFFER request from client!\n");
7080 /* Sanity check for client registration */
7081 if (NULL == (c = client_get (client)))
7084 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7089 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7091 /* Retrieve tunnel */
7092 tid = ntohl (tunnel_msg->tunnel_id);
7093 t = tunnel_get_by_local_id(c, tid);
7096 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7102 switch (ntohs(message->type))
7104 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7105 t->nobuffer = GNUNET_NO;
7107 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7108 t->nobuffer = GNUNET_YES;
7114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7119 * Handler for connection requests to new peers
7121 * @param cls closure
7122 * @param client identification of the client
7123 * @param message the actual message (PeerControl)
7126 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7127 const struct GNUNET_MessageHeader *message)
7129 struct GNUNET_MESH_PeerControl *peer_msg;
7130 struct MeshPeerInfo *peer_info;
7131 struct MeshClient *c;
7132 struct MeshTunnel *t;
7133 MESH_TunnelNumber tid;
7135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7136 /* Sanity check for client registration */
7137 if (NULL == (c = client_get (client)))
7140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7145 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7147 /* Sanity check for message size */
7148 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7155 /* Tunnel exists? */
7156 tid = ntohl (peer_msg->tunnel_id);
7157 t = tunnel_get_by_local_id (c, tid);
7161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7165 /* Does client own tunnel? */
7166 if (t->owner->handle != client)
7169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7173 GNUNET_i2s (&peer_msg->peer));
7174 peer_info = peer_info_get (&peer_msg->peer);
7176 tunnel_add_peer (t, peer_info);
7177 peer_info_connect (peer_info, t);
7179 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7185 * Handler for disconnection requests of peers in a tunnel
7187 * @param cls closure
7188 * @param client identification of the client
7189 * @param message the actual message (PeerControl)
7192 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7193 const struct GNUNET_MessageHeader *message)
7195 struct GNUNET_MESH_PeerControl *peer_msg;
7196 struct MeshPeerInfo *peer_info;
7197 struct MeshClient *c;
7198 struct MeshTunnel *t;
7199 MESH_TunnelNumber tid;
7201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7202 /* Sanity check for client registration */
7203 if (NULL == (c = client_get (client)))
7206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7211 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7213 /* Sanity check for message size */
7214 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7221 /* Tunnel exists? */
7222 tid = ntohl (peer_msg->tunnel_id);
7223 t = tunnel_get_by_local_id (c, tid);
7227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7232 /* Does client own tunnel? */
7233 if (t->owner->handle != client)
7236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7241 GNUNET_i2s (&peer_msg->peer));
7242 /* Is the peer in the tunnel? */
7244 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7245 if (NULL == peer_info)
7248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7252 /* Ok, delete peer from tunnel */
7253 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7254 &peer_msg->peer.hashPubKey);
7256 send_destroy_path (t, peer_info->id);
7257 tunnel_delete_peer (t, peer_info->id);
7258 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7263 * Handler for blacklist requests of peers in a tunnel
7265 * @param cls closure
7266 * @param client identification of the client
7267 * @param message the actual message (PeerControl)
7269 * FIXME implement DHT block bloomfilter
7272 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7273 const struct GNUNET_MessageHeader *message)
7275 struct GNUNET_MESH_PeerControl *peer_msg;
7276 struct MeshClient *c;
7277 struct MeshTunnel *t;
7278 MESH_TunnelNumber tid;
7280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7281 /* Sanity check for client registration */
7282 if (NULL == (c = client_get (client)))
7285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7290 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7292 /* Sanity check for message size */
7293 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7300 /* Tunnel exists? */
7301 tid = ntohl (peer_msg->tunnel_id);
7302 t = tunnel_get_by_local_id (c, tid);
7306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7311 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7312 GNUNET_PEER_intern(&peer_msg->peer));
7317 * Handler for unblacklist requests of peers in a tunnel
7319 * @param cls closure
7320 * @param client identification of the client
7321 * @param message the actual message (PeerControl)
7324 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7325 const struct GNUNET_MessageHeader *message)
7327 struct GNUNET_MESH_PeerControl *peer_msg;
7328 struct MeshClient *c;
7329 struct MeshTunnel *t;
7330 MESH_TunnelNumber tid;
7334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7335 /* Sanity check for client registration */
7336 if (NULL == (c = client_get (client)))
7339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7344 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7346 /* Sanity check for message size */
7347 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7350 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7354 /* Tunnel exists? */
7355 tid = ntohl (peer_msg->tunnel_id);
7356 t = tunnel_get_by_local_id (c, tid);
7360 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7365 /* if peer is not known, complain */
7366 pid = GNUNET_PEER_search (&peer_msg->peer);
7373 /* search and remove from list */
7374 for (i = 0; i < t->nblacklisted; i++)
7376 if (t->blacklisted[i] == pid)
7378 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7379 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7384 /* if peer hasn't been blacklisted, complain */
7390 * Handler for connection requests to new peers by type
7392 * @param cls closure
7393 * @param client identification of the client
7394 * @param message the actual message (ConnectPeerByType)
7397 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7398 const struct GNUNET_MessageHeader *message)
7400 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7401 struct MeshClient *c;
7402 struct MeshTunnel *t;
7403 struct GNUNET_HashCode hash;
7404 MESH_TunnelNumber tid;
7406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7407 /* Sanity check for client registration */
7408 if (NULL == (c = client_get (client)))
7411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7416 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7418 /* Sanity check for message size */
7419 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7420 ntohs (connect_msg->header.size))
7423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7427 /* Tunnel exists? */
7428 tid = ntohl (connect_msg->tunnel_id);
7429 t = tunnel_get_by_local_id (c, tid);
7433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7437 /* Does client own tunnel? */
7438 if (t->owner->handle != client)
7441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7445 /* Do WE have the service? */
7446 t->type = ntohl (connect_msg->type);
7447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7448 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7449 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7452 /* Yes! Fast forward, add ourselves to the tunnel and send the
7453 * good news to the client, and alert the destination client of
7454 * an incoming tunnel.
7456 * FIXME send a path create to self, avoid code duplication
7458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7459 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7460 peer_info_get (&my_full_id),
7461 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7464 send_client_peer_connected (t, myid);
7465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7466 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7468 t->local_tid_dest = next_local_tid++;
7469 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7470 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7471 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7475 /* Ok, lets find a peer offering the service */
7476 if (NULL != t->dht_get_type)
7478 GNUNET_DHT_get_stop (t->dht_get_type);
7480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7481 GNUNET_h2s (&hash));
7483 GNUNET_DHT_get_start (dht_handle,
7484 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7486 dht_replication_level,
7487 GNUNET_DHT_RO_RECORD_ROUTE |
7488 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7490 &dht_get_type_handler, t);
7492 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7498 * Handler for connection requests to new peers by a string service description.
7500 * @param cls closure
7501 * @param client identification of the client
7502 * @param message the actual message, which includes messages the client wants
7505 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7506 const struct GNUNET_MessageHeader *message)
7508 struct GNUNET_MESH_ConnectPeerByString *msg;
7509 struct MeshRegexSearchContext *ctx;
7510 struct MeshRegexSearchInfo *info;
7511 struct GNUNET_DHT_GetHandle *get_h;
7512 struct GNUNET_HashCode key;
7513 struct MeshTunnel *t;
7514 struct MeshClient *c;
7515 MESH_TunnelNumber tid;
7520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7521 "Connect by string started\n");
7522 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7523 size = htons (message->size);
7525 /* Sanity check for client registration */
7526 if (NULL == (c = client_get (client)))
7529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7534 /* Message size sanity check */
7535 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7542 /* Tunnel exists? */
7543 tid = ntohl (msg->tunnel_id);
7544 t = tunnel_get_by_local_id (c, tid);
7548 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7552 /* Does client own tunnel? */
7553 if (t->owner->handle != client)
7556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7561 " on tunnel %s [%u]\n",
7562 GNUNET_i2s(&my_full_id),
7565 /* Only one connect_by_string allowed at the same time! */
7566 /* FIXME: allow more, return handle at api level to cancel, document */
7567 if (NULL != t->regex_ctx)
7570 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7574 /* Find string itself */
7575 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7576 string = (const char *) &msg[1];
7578 /* Initialize context */
7579 size = GNUNET_REGEX_get_first_key (string, len, &key);
7580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7581 " consumed %u bits out of %u\n", size, len);
7582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7583 " looking for %s\n", GNUNET_h2s (&key));
7585 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7587 info->description = GNUNET_strndup (string, len);
7588 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7589 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7592 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7593 ctx->position = size;
7597 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7599 /* Start search in DHT */
7600 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7601 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7602 &key, /* key to search */
7603 dht_replication_level, /* replication level */
7604 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7605 &info->description[size], /* xquery */
7606 // FIXME add BLOOMFILTER to exclude filtered peers
7607 len + 1 - size, /* xquery bits */
7608 // FIXME add BLOOMFILTER SIZE
7609 &dht_get_string_handler, ctx);
7611 GNUNET_break (GNUNET_OK ==
7612 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7615 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7617 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7623 * Handler for client traffic directed to one peer
7625 * @param cls closure
7626 * @param client identification of the client
7627 * @param message the actual message
7630 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7631 const struct GNUNET_MessageHeader *message)
7633 struct MeshClient *c;
7634 struct MeshTunnel *t;
7635 struct MeshPeerInfo *pi;
7636 struct GNUNET_MESH_Unicast *data_msg;
7637 MESH_TunnelNumber tid;
7640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7641 "Got a unicast request from a client!\n");
7643 /* Sanity check for client registration */
7644 if (NULL == (c = client_get (client)))
7647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7652 data_msg = (struct GNUNET_MESH_Unicast *) message;
7654 /* Sanity check for message size */
7655 size = ntohs (message->size);
7656 if (sizeof (struct GNUNET_MESH_Unicast) +
7657 sizeof (struct GNUNET_MessageHeader) > size)
7660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7664 /* Tunnel exists? */
7665 tid = ntohl (data_msg->tid);
7666 t = tunnel_get_by_local_id (c, tid);
7670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7674 /* Is it a local tunnel? Then, does client own the tunnel? */
7675 if (t->owner->handle != client)
7678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7682 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7683 &data_msg->destination.hashPubKey);
7684 /* Is the selected peer in the tunnel? */
7688 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7692 /* PID should be as expected */
7693 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7696 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7697 "Unicast PID, expected %u, got %u\n",
7698 t->fwd_pid + 1, ntohl (data_msg->pid));
7699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7703 /* Ok, everything is correct, send the message
7704 * (pretend we got it from a mesh peer)
7707 /* Work around const limitation */
7708 char buf[ntohs (message->size)] GNUNET_ALIGN;
7709 struct GNUNET_MESH_Unicast *copy;
7711 copy = (struct GNUNET_MESH_Unicast *) buf;
7712 memcpy (buf, data_msg, size);
7713 copy->oid = my_full_id;
7714 copy->tid = htonl (t->id.tid);
7715 copy->ttl = htonl (default_ttl);
7716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7717 " calling generic handler...\n");
7718 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7721 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7728 * Handler for client traffic directed to the origin
7730 * @param cls closure
7731 * @param client identification of the client
7732 * @param message the actual message
7735 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7736 const struct GNUNET_MessageHeader *message)
7738 struct GNUNET_MESH_ToOrigin *data_msg;
7739 struct MeshTunnelClientInfo *clinfo;
7740 struct MeshClient *c;
7741 struct MeshTunnel *t;
7742 MESH_TunnelNumber tid;
7745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7746 "Got a ToOrigin request from a client!\n");
7747 /* Sanity check for client registration */
7748 if (NULL == (c = client_get (client)))
7751 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7756 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7758 /* Sanity check for message size */
7759 size = ntohs (message->size);
7760 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7761 sizeof (struct GNUNET_MessageHeader) > size)
7764 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7768 /* Tunnel exists? */
7769 tid = ntohl (data_msg->tid);
7770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7771 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7777 t = tunnel_get_by_local_id (c, tid);
7780 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7781 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7783 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7787 /* It should be sent by someone who has this as incoming tunnel. */
7788 if (GNUNET_NO == client_knows_tunnel (c, t))
7791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7795 /* PID should be as expected */
7796 clinfo = tunnel_get_client_fc (t, c);
7797 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7800 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7801 "To Origin PID, expected %u, got %u\n",
7802 clinfo->bck_pid + 1,
7803 ntohl (data_msg->pid));
7804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7809 /* Ok, everything is correct, send the message
7810 * (pretend we got it from a mesh peer)
7813 char buf[ntohs (message->size)] GNUNET_ALIGN;
7814 struct GNUNET_MESH_ToOrigin *copy;
7816 /* Work around const limitation */
7817 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7818 memcpy (buf, data_msg, size);
7819 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7820 copy->tid = htonl (t->id.tid);
7821 copy->ttl = htonl (default_ttl);
7822 copy->pid = htonl (t->bck_pid + 1);
7824 copy->sender = my_full_id;
7825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7826 " calling generic handler...\n");
7827 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7836 * Handler for client traffic directed to all peers in a tunnel
7838 * @param cls closure
7839 * @param client identification of the client
7840 * @param message the actual message
7843 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7844 const struct GNUNET_MessageHeader *message)
7846 struct MeshClient *c;
7847 struct MeshTunnel *t;
7848 struct GNUNET_MESH_Multicast *data_msg;
7849 MESH_TunnelNumber tid;
7851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7852 "Got a multicast request from a client!\n");
7854 /* Sanity check for client registration */
7855 if (NULL == (c = client_get (client)))
7858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7863 data_msg = (struct GNUNET_MESH_Multicast *) message;
7865 /* Sanity check for message size */
7866 if (sizeof (struct GNUNET_MESH_Multicast) +
7867 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7870 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7874 /* Tunnel exists? */
7875 tid = ntohl (data_msg->tid);
7876 t = tunnel_get_by_local_id (c, tid);
7880 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7887 /* Does client own tunnel? */
7888 if (t->owner->handle != client)
7891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7895 /* PID should be as expected */
7896 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7899 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7900 "Multicast PID, expected %u, got %u\n",
7901 t->fwd_pid + 1, ntohl (data_msg->pid));
7902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7907 char buf[ntohs (message->size)] GNUNET_ALIGN;
7908 struct GNUNET_MESH_Multicast *copy;
7910 copy = (struct GNUNET_MESH_Multicast *) buf;
7911 memcpy (buf, message, ntohs (message->size));
7912 copy->oid = my_full_id;
7913 copy->tid = htonl (t->id.tid);
7914 copy->ttl = htonl (default_ttl);
7915 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7917 " calling generic handler...\n");
7918 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7921 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7927 * Handler for client's ACKs for payload traffic.
7929 * @param cls Closure (unused).
7930 * @param client Identification of the client.
7931 * @param message The actual message.
7934 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7935 const struct GNUNET_MessageHeader *message)
7937 struct GNUNET_MESH_LocalAck *msg;
7938 struct MeshTunnel *t;
7939 struct MeshClient *c;
7940 MESH_TunnelNumber tid;
7943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7944 /* Sanity check for client registration */
7945 if (NULL == (c = client_get (client)))
7948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7953 msg = (struct GNUNET_MESH_LocalAck *) message;
7955 /* Tunnel exists? */
7956 tid = ntohl (msg->tunnel_id);
7957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7958 t = tunnel_get_by_local_id (c, tid);
7962 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7963 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7968 ack = ntohl (msg->max_pid);
7969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7971 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7972 if (NULL != t->owner && t->owner->handle == client)
7974 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7976 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7980 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7981 tunnel_set_client_fwd_ack (t, c, ack);
7982 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7985 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7992 * Functions to handle messages from clients
7994 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7995 {&handle_local_new_client, NULL,
7996 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7997 {&handle_local_announce_regex, NULL,
7998 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7999 {&handle_local_tunnel_create, NULL,
8000 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8001 sizeof (struct GNUNET_MESH_TunnelMessage)},
8002 {&handle_local_tunnel_destroy, NULL,
8003 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8004 sizeof (struct GNUNET_MESH_TunnelMessage)},
8005 {&handle_local_tunnel_speed, NULL,
8006 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8007 sizeof (struct GNUNET_MESH_TunnelMessage)},
8008 {&handle_local_tunnel_speed, NULL,
8009 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8010 sizeof (struct GNUNET_MESH_TunnelMessage)},
8011 {&handle_local_tunnel_buffer, NULL,
8012 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8013 sizeof (struct GNUNET_MESH_TunnelMessage)},
8014 {&handle_local_tunnel_buffer, NULL,
8015 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8016 sizeof (struct GNUNET_MESH_TunnelMessage)},
8017 {&handle_local_connect_add, NULL,
8018 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8019 sizeof (struct GNUNET_MESH_PeerControl)},
8020 {&handle_local_connect_del, NULL,
8021 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8022 sizeof (struct GNUNET_MESH_PeerControl)},
8023 {&handle_local_blacklist, NULL,
8024 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8025 sizeof (struct GNUNET_MESH_PeerControl)},
8026 {&handle_local_unblacklist, NULL,
8027 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8028 sizeof (struct GNUNET_MESH_PeerControl)},
8029 {&handle_local_connect_by_type, NULL,
8030 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8031 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8032 {&handle_local_connect_by_string, NULL,
8033 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8034 {&handle_local_unicast, NULL,
8035 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8036 {&handle_local_to_origin, NULL,
8037 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8038 {&handle_local_multicast, NULL,
8039 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8040 {&handle_local_ack, NULL,
8041 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8042 sizeof (struct GNUNET_MESH_LocalAck)},
8048 * To be called on core init/fail.
8050 * @param cls service closure
8051 * @param server handle to the server for this service
8052 * @param identity the public identity of this peer
8055 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8056 const struct GNUNET_PeerIdentity *identity)
8059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8060 core_handle = server;
8061 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8065 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8067 GNUNET_abort(); // Try harder
8074 * Method called whenever a given peer connects.
8076 * @param cls closure
8077 * @param peer peer identity this notification is about
8078 * @param atsi performance data for the connection
8079 * @param atsi_count number of records in 'atsi'
8082 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8083 const struct GNUNET_ATS_Information *atsi,
8084 unsigned int atsi_count)
8086 struct MeshPeerInfo *peer_info;
8087 struct MeshPeerPath *path;
8089 DEBUG_CONN ("Peer connected\n");
8090 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8091 peer_info = peer_info_get (peer);
8092 if (myid == peer_info->id)
8094 DEBUG_CONN (" (self)\n");
8099 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8101 path = path_new (2);
8102 path->peers[0] = myid;
8103 path->peers[1] = peer_info->id;
8104 GNUNET_PEER_change_rc (myid, 1);
8105 GNUNET_PEER_change_rc (peer_info->id, 1);
8106 peer_info_add_path (peer_info, path, GNUNET_YES);
8107 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8113 * Method called whenever a peer disconnects.
8115 * @param cls closure
8116 * @param peer peer identity this notification is about
8119 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8121 struct MeshPeerInfo *pi;
8122 struct MeshPeerQueue *q;
8123 struct MeshPeerQueue *n;
8125 DEBUG_CONN ("Peer disconnected\n");
8126 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8136 /* TODO try to reroute this traffic instead */
8137 queue_destroy(q, GNUNET_YES);
8140 if (NULL != pi->core_transmit)
8142 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8143 pi->core_transmit = NULL;
8145 peer_info_remove_path (pi, pi->id, myid);
8148 DEBUG_CONN (" (self)\n");
8150 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8155 /******************************************************************************/
8156 /************************ MAIN FUNCTIONS ****************************/
8157 /******************************************************************************/
8160 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8162 * @param cls closure
8163 * @param key current key code
8164 * @param value value in the hash map
8165 * @return GNUNET_YES if we should continue to iterate,
8169 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8171 struct MeshTunnel *t = value;
8178 * Iterator over peer 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_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8189 struct MeshPeerInfo *p = value;
8190 struct MeshPeerQueue *q;
8191 struct MeshPeerQueue *n;
8199 queue_destroy(q, GNUNET_YES);
8203 peer_info_destroy (p);
8209 * Task run during shutdown.
8215 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8219 if (core_handle != NULL)
8221 GNUNET_CORE_disconnect (core_handle);
8226 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8229 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8230 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8231 if (dht_handle != NULL)
8233 GNUNET_DHT_disconnect (dht_handle);
8238 GNUNET_SERVER_notification_context_destroy (nc);
8241 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8243 GNUNET_SCHEDULER_cancel (announce_id_task);
8244 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8251 * Callback for hostkey read/generation
8254 * @param pk the private key
8255 * @param emsg error message
8258 key_generation_cb (void *cls,
8259 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8262 struct MeshPeerInfo *peer;
8263 struct MeshPeerPath *p;
8268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8269 _("Mesh service could not access hostkey. Exiting.\n"));
8270 GNUNET_SCHEDULER_shutdown ();
8273 my_private_key = pk;
8274 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8275 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8276 &my_full_id.hashPubKey);
8277 myid = GNUNET_PEER_intern (&my_full_id);
8278 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8279 "Mesh for peer [%s] starting\n",
8280 GNUNET_i2s(&my_full_id));
8282 // transport_handle = GNUNET_TRANSPORT_connect(c,
8292 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8295 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8296 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8297 GNUNET_SERVER_disconnect_notify (server_handle,
8298 &handle_local_client_disconnect, NULL);
8302 clients_tail = NULL;
8305 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8306 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8308 /* Create a peer_info for the local peer */
8309 peer = peer_info_get (&my_full_id);
8312 GNUNET_PEER_change_rc (myid, 1);
8313 peer_info_add_path (peer, p, GNUNET_YES);
8314 GNUNET_SERVER_resume (server_handle);
8315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8320 * Process mesh requests.
8322 * @param cls closure
8323 * @param server the initialized server
8324 * @param c configuration to use
8327 run (void *cls, struct GNUNET_SERVER_Handle *server,
8328 const struct GNUNET_CONFIGURATION_Handle *c)
8332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8333 server_handle = server;
8334 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8335 NULL, /* Closure passed to MESH functions */
8336 &core_init, /* Call core_init once connected */
8337 &core_connect, /* Handle connects */
8338 &core_disconnect, /* remove peers on disconnects */
8339 NULL, /* Don't notify about all incoming messages */
8340 GNUNET_NO, /* For header only in notification */
8341 NULL, /* Don't notify about all outbound messages */
8342 GNUNET_NO, /* For header-only out notification */
8343 core_handlers); /* Register these handlers */
8345 if (core_handle == NULL)
8348 GNUNET_SCHEDULER_shutdown ();
8353 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8358 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8360 GNUNET_SCHEDULER_shutdown ();
8365 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8366 &refresh_path_time))
8368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8370 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8371 "mesh", "refresh path time");
8372 GNUNET_SCHEDULER_shutdown ();
8377 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8378 &app_announce_time))
8380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8382 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8383 "mesh", "app announce time");
8384 GNUNET_SCHEDULER_shutdown ();
8387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8388 "APP_ANNOUNCE_TIME %llu ms\n",
8389 app_announce_time.rel_value);
8391 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8396 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8397 "mesh", "id announce time");
8398 GNUNET_SCHEDULER_shutdown ();
8403 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8408 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8409 "mesh", "connect timeout");
8410 GNUNET_SCHEDULER_shutdown ();
8415 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8420 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8421 "mesh", "max msgs queue");
8422 GNUNET_SCHEDULER_shutdown ();
8427 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8432 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8433 "mesh", "max tunnels");
8434 GNUNET_SCHEDULER_shutdown ();
8439 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8444 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8445 "mesh", "default ttl", 64);
8450 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8451 &dht_replication_level))
8453 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8455 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8456 "mesh", "dht replication level", 10);
8457 dht_replication_level = 10;
8460 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8461 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8462 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8463 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8464 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8466 dht_handle = GNUNET_DHT_connect (c, 64);
8467 if (NULL == dht_handle)
8471 stats = GNUNET_STATISTICS_create ("mesh", c);
8473 GNUNET_SERVER_suspend (server_handle);
8474 /* Scheduled the task to clean up when shutdown is called */
8475 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8477 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8478 GNUNET_free (keyfile);
8483 * The main function for the mesh service.
8485 * @param argc number of arguments from the command line
8486 * @param argv command line arguments
8487 * @return 0 ok, 1 on error
8490 main (int argc, char *const *argv)
8495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8496 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8498 ret = (GNUNET_OK == r) ? 0 : 1;
8499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8503 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8504 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8505 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);