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_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
65 GNUNET_TIME_UNIT_MINUTES,\
68 #if MESH_DEBUG_CONNECTION
69 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
71 #define DEBUG_CONN(...)
75 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
77 #define DEBUG_DHT(...)
84 struct timespec __mesh_start;
85 struct timespec __mesh_end;
86 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
87 #define INTERVAL_END \
89 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
90 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
91 if (__diff < 0) __diff += 1000000000;\
95 #define INTERVAL_SHOW \
97 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
99 #define INTERVAL_START
101 #define INTERVAL_SHOW
104 /******************************************************************************/
105 /************************ DATA STRUCTURES ****************************/
106 /******************************************************************************/
108 /** FWD declaration */
114 * Struct representing a piece of data being sent to other peers
118 /** Tunnel it belongs to. */
119 struct MeshTunnel *t;
121 /** How many remaining neighbors still hav't got it. */
122 unsigned int reference_counter;
124 /** How many remaining neighbors we need to send this to. */
125 unsigned int total_out;
127 /** Size of the data. */
136 * Struct containing info about a queued transmission to this peer
143 struct MeshPeerQueue *next;
148 struct MeshPeerQueue *prev;
151 * Peer this transmission is directed to.
153 struct MeshPeerInfo *peer;
156 * Tunnel this message belongs to.
158 struct MeshTunnel *tunnel;
161 * Pointer to info stucture used as cls.
171 * Size of the message
178 * Struct to store regex information announced by clients.
180 struct MeshRegexDescriptor
183 * Regular expression itself.
188 * Cached DFA for regular expression 'regex'.
190 struct GNUNET_REGEX_Automaton *dfa;
193 * How many characters per edge can we squeeze?
195 uint16_t compression;
199 * Struct containing all info possibly needed to build a package when called
202 struct MeshTransmissionDescriptor
204 /** ID of the tunnel this packet travels in */
205 struct MESH_TunnelID *origin;
207 /** Who was this message being sent to */
208 struct MeshPeerInfo *peer;
210 /** Ultimate destination of the packet */
211 GNUNET_PEER_Id destination;
213 /** Data descriptor */
214 struct MeshData* mesh_data;
219 * Struct containing all information regarding a given peer
229 * Last time we heard from this peer
231 struct GNUNET_TIME_Absolute last_contact;
234 * Task handler for delayed connect task;
236 GNUNET_SCHEDULER_TaskIdentifier connect_task;
239 * Number of attempts to reconnect so far
241 int n_reconnect_attempts;
244 * Paths to reach the peer, ordered by ascending hop count
246 struct MeshPeerPath *path_head;
249 * Paths to reach the peer, ordered by ascending hop count
251 struct MeshPeerPath *path_tail;
254 * Handle to stop the DHT search for a path to this peer
256 struct GNUNET_DHT_GetHandle *dhtget;
259 * Closure given to the DHT GET
261 struct MeshPathInfo *dhtgetcls;
264 * Array of tunnels this peer participates in
265 * (most probably a small amount, therefore not a hashmap)
266 * When the path to the peer changes, notify these tunnels to let them
267 * re-adjust their path trees.
269 struct MeshTunnel **tunnels;
272 * Number of tunnels this peers participates in
274 unsigned int ntunnels;
277 * Transmission queue to core DLL head
279 struct MeshPeerQueue *queue_head;
282 * Transmission queue to core DLL tail
284 struct MeshPeerQueue *queue_tail;
287 * How many messages are in the queue to this peer.
289 unsigned int queue_n;
292 * Handle to for queued transmissions
294 struct GNUNET_CORE_TransmitHandle *core_transmit;
299 * Globally unique tunnel identification (owner + number)
300 * DO NOT USE OVER THE NETWORK
305 * Node that owns the tunnel
310 * Tunnel number to differentiate all the tunnels owned by the node oid
311 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
313 MESH_TunnelNumber tid;
318 * Struct containing all information regarding a tunnel
319 * For an intermediate node the improtant info used will be:
320 * - id Tunnel unique identification
321 * - paths[0] To know where to send it next
322 * - metainfo: ready, speeds, accounting
329 struct MESH_TunnelID id;
332 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
334 MESH_TunnelNumber local_tid;
337 * Local tunnel number for local destination clients (incoming number)
338 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
341 MESH_TunnelNumber local_tid_dest;
344 * Is the speed on the tunnel limited to the slowest peer?
349 * Is the tunnel bufferless (minimum latency)?
354 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
359 * Packet ID of the last bck packet sent (unique counter per hop).
364 * SKIP value for this tunnel.
369 * Force sending ACK? Flag to allow duplicate ACK on POLL.
374 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
375 * Contains the Flow Control info: FWD ACK value received,
376 * last BCK ACK sent, PID and SKIP values.
378 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
381 * Last ACK sent towards the origin (for traffic towards leaf node).
383 uint32_t last_fwd_ack;
386 * BCK ACK value received from the hop towards the owner of the tunnel,
387 * (previous node / owner): up to what message PID can we sent back to him.
392 * How many messages are in the forward queue (towards leaves).
394 unsigned int fwd_queue_n;
397 * How many messages do we accept in the forward queue.
399 unsigned int fwd_queue_max;
402 * How many messages are in the backward queue (towards origin).
404 unsigned int bck_queue_n;
407 * How many messages do we accept in the backward queue.
409 unsigned int bck_queue_max;
412 * Task to poll peer in case of a stall.
414 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
417 * Last time the tunnel was used
419 struct GNUNET_TIME_Absolute timestamp;
422 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
423 * containing peers added by id or by type, not intermediate peers.
425 struct GNUNET_CONTAINER_MultiHashMap *peers;
428 * Number of peers that are connected and potentially ready to receive data
430 unsigned int peers_ready;
433 * Number of peers that have been added to the tunnel
435 unsigned int peers_total;
438 * Client owner of the tunnel, if any
440 struct MeshClient *owner;
443 * Clients that have been informed about and want to stay in the tunnel.
445 struct MeshClient **clients;
448 * Flow control info for each client.
450 struct MeshTunnelClientInfo *clients_fc;
453 * Number of elements in clients/clients_fc
455 unsigned int nclients;
458 * Clients that have been informed but requested to leave the tunnel.
460 struct MeshClient **ignore;
463 * Number of elements in clients
465 unsigned int nignore;
470 GNUNET_PEER_Id *blacklisted;
473 * Number of elements in blacklisted
475 unsigned int nblacklisted;
478 * Bloomfilter (for peer identities) to stop circular routes
480 char bloomfilter[MESH_BLOOM_SIZE];
485 struct MeshTunnelTree *tree;
488 * Application type we are looking for in this tunnel
490 GNUNET_MESH_ApplicationType type;
493 * Used to search peers offering a service
495 struct GNUNET_DHT_GetHandle *dht_get_type;
498 * Initial context of the regex search for a connect_by_string
500 struct MeshRegexSearchContext *regex_ctx;
503 * Task to keep the used paths alive
505 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
508 * Task to destroy the tunnel after timeout
510 * FIXME: merge the two? a tunnel will have either
511 * a path refresh OR a timeout, never both!
513 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
516 * Flag to signal the destruction of the tunnel.
517 * If this is set GNUNET_YES the tunnel will be destroyed
518 * when the queue is empty.
523 * Total messages pending for this tunnels, payload or not.
525 unsigned int pending_messages;
528 * If the tunnel is empty, destoy it.
530 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
535 * Info about a child node in a tunnel, needed to perform flow control.
537 struct MeshTunnelChildInfo
540 * ID of the child node.
560 * Maximum PID allowed (FWD ACK received).
565 * Last ACK sent to that child (BCK ACK).
570 * Circular buffer pointing to MeshPeerQueue elements for all
571 * payload traffic going to this child.
572 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
574 struct MeshPeerQueue **send_buffer;
577 * Index of the oldest element in the send_buffer.
579 unsigned int send_buffer_start;
582 * How many elements are already in the buffer.
584 unsigned int send_buffer_n;
587 * Tunnel this info is about
589 struct MeshTunnel *t;
592 * Task to poll peer in case of a stall.
594 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
597 * Time to use for next polling call.
599 struct GNUNET_TIME_Relative fc_poll_time;
604 * Info about a leaf client of a tunnel, needed to perform flow control.
606 struct MeshTunnelClientInfo
609 * PID of the last packet sent to the client (FWD).
614 * PID of the last packet received from the client (BCK).
619 * Maximum PID allowed (FWD ACK received).
624 * Last ACK sent to that child (BCK ACK).
632 * Info collected during iteration of child nodes in order to get the ACK value
635 struct MeshTunnelChildIteratorContext
638 * Tunnel whose info is being collected.
640 struct MeshTunnel *t;
643 * Is this context initialized? Is the value in max_child_ack valid?
648 * Maximum child ACK so far.
650 uint32_t max_child_ack;
653 * Number of children nodes
655 unsigned int nchildren;
660 * Info needed to work with tunnel paths and peers
667 struct MeshTunnel *t;
670 * Neighbouring peer to whom we send the packet to
672 struct MeshPeerInfo *peer;
677 struct MeshPeerPath *path;
682 * Struct containing information about a client of the service
689 struct MeshClient *next;
694 struct MeshClient *prev;
697 * Tunnels that belong to this client, indexed by local id
699 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
702 * Tunnels this client has accepted, indexed by incoming local id
704 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
707 * Tunnels this client has rejected, indexed by incoming local id
709 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
711 * Handle to communicate with the client
713 struct GNUNET_SERVER_Client *handle;
716 * Applications that this client has claimed to provide
718 struct GNUNET_CONTAINER_MultiHashMap *apps;
721 * Messages that this client has declared interest in
723 struct GNUNET_CONTAINER_MultiHashMap *types;
726 * Whether the client is active or shutting down (don't send confirmations
727 * to a client that is shutting down.
732 * ID of the client, mainly for debug messages
737 * Regular expressions describing the services offered by this client.
739 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
742 * Number of regular expressions in regexes.
744 unsigned int n_regex;
747 * Task to refresh all regular expresions in the DHT.
749 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
755 * Struct to keep information of searches of services described by a regex
756 * using a user-provided string service description.
758 struct MeshRegexSearchInfo
761 * Which tunnel is this for
763 struct MeshTunnel *t;
766 * User provided description of the searched service.
771 * Part of the description already consumed by the search.
778 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
781 * Results from running DHT GETs.
783 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
786 * Contexts, for each running DHT GET. Free all on end of search.
788 struct MeshRegexSearchContext **contexts;
791 * Number of contexts (branches/steps in search).
793 unsigned int n_contexts;
796 * Peer that is connecting via connect_by_string. When connected, free ctx.
801 * Other peers that are found but not yet being connected to.
803 GNUNET_PEER_Id *peers;
806 * Number of elements in peers.
808 unsigned int n_peers;
811 * Next peer to try to connect to.
816 * Timeout for a connect attempt.
817 * When reached, try to connect to a different peer, if any. If not,
818 * try the same peer again.
820 GNUNET_SCHEDULER_TaskIdentifier timeout;
825 * Struct to keep state of running searches that have consumed a part of
828 struct MeshRegexSearchContext
831 * Part of the description already consumed by
832 * this particular search branch.
837 * Information about the search.
839 struct MeshRegexSearchInfo *info;
842 * We just want to look for one edge, the longer the better.
845 unsigned int longest_match;
848 * Destination hash of the longest match.
850 struct GNUNET_HashCode hash;
853 /******************************************************************************/
854 /************************ DEBUG FUNCTIONS ****************************/
855 /******************************************************************************/
859 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
860 * @param cls string to print
861 * @param success GNUNET_OK if the PUT was transmitted,
862 * GNUNET_NO on timeout,
863 * GNUNET_SYSERR on disconnect from service
864 * after the PUT message was transmitted
865 * (so we don't know if it was received or not)
870 mesh_debug (void *cls, int success)
874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
878 unsigned int debug_fwd_ack;
879 unsigned int debug_bck_ack;
883 /******************************************************************************/
884 /*********************** GLOBAL VARIABLES ****************************/
885 /******************************************************************************/
887 /************************** Configuration parameters **************************/
890 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
892 static struct GNUNET_TIME_Relative refresh_path_time;
895 * How often to PUT local application numbers in the DHT.
897 static struct GNUNET_TIME_Relative app_announce_time;
900 * How often to PUT own ID in the DHT.
902 static struct GNUNET_TIME_Relative id_announce_time;
905 * Maximum time allowed to connect to a peer found by string.
907 static struct GNUNET_TIME_Relative connect_timeout;
910 * Default TTL for payload packets.
912 static unsigned long long default_ttl;
915 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
917 static unsigned long long dht_replication_level;
920 * How many tunnels are we willing to maintain.
921 * Local tunnels are always allowed, even if there are more tunnels than max.
923 static unsigned long long max_tunnels;
926 * How many messages *in total* are we willing to queue, divided by number of
927 * tunnels to get tunnel queue size.
929 static unsigned long long max_msgs_queue;
932 * How many peers do we want to remember?
934 static unsigned long long max_peers;
937 /*************************** Static global variables **************************/
940 * Hostkey generation context
942 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
945 * DLL with all the clients, head.
947 static struct MeshClient *clients;
950 * DLL with all the clients, tail.
952 static struct MeshClient *clients_tail;
955 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
957 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
960 * Number of tunnels known.
962 static unsigned long long n_tunnels;
965 * Tunnels incoming, indexed by MESH_TunnelNumber
966 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
968 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
971 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
973 static struct GNUNET_CONTAINER_MultiHashMap *peers;
976 * Handle to communicate with transport
978 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
981 * Handle to communicate with core.
983 static struct GNUNET_CORE_Handle *core_handle;
988 static struct GNUNET_DHT_Handle *dht_handle;
993 static struct GNUNET_SERVER_Handle *server_handle;
996 * Handle to the statistics service.
998 static struct GNUNET_STATISTICS_Handle *stats;
1001 * Notification context, to send messages to local clients.
1003 static struct GNUNET_SERVER_NotificationContext *nc;
1006 * Local peer own ID (memory efficient handle).
1008 static GNUNET_PEER_Id myid;
1011 * Local peer own ID (full value).
1013 static struct GNUNET_PeerIdentity my_full_id;
1018 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1023 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1026 * Tunnel ID for the next created tunnel (global tunnel number).
1028 static MESH_TunnelNumber next_tid;
1031 * Tunnel ID for the next incoming tunnel (local tunnel number).
1033 static MESH_TunnelNumber next_local_tid;
1036 * All application types provided by this peer.
1038 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1041 * All message types clients of this peer are interested in.
1043 static struct GNUNET_CONTAINER_MultiHashMap *types;
1046 * Task to periodically announce provided applications.
1048 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1051 * Task to periodically announce itself in the network.
1053 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1056 * Next ID to assign to a client.
1058 unsigned int next_client_id;
1061 /******************************************************************************/
1062 /*********************** DECLARATIONS **************************/
1063 /******************************************************************************/
1066 * Function to process paths received for a new peer addition. The recorded
1067 * paths form the initial tunnel, which can be optimized later.
1068 * Called on each result obtained for the DHT search.
1070 * @param cls closure
1071 * @param exp when will this value expire
1072 * @param key key of the result
1073 * @param type type of the result
1074 * @param size number of bytes in data
1075 * @param data pointer to the result data
1078 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1079 const struct GNUNET_HashCode * key,
1080 const struct GNUNET_PeerIdentity *get_path,
1081 unsigned int get_path_length,
1082 const struct GNUNET_PeerIdentity *put_path,
1083 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1084 size_t size, const void *data);
1088 * Function to process DHT string to regex matching.
1089 * Called on each result obtained for the DHT search.
1091 * @param cls closure (search context)
1092 * @param exp when will this value expire
1093 * @param key key of the result
1094 * @param get_path path of the get request (not used)
1095 * @param get_path_length lenght of get_path (not used)
1096 * @param put_path path of the put request (not used)
1097 * @param put_path_length length of the put_path (not used)
1098 * @param type type of the result
1099 * @param size number of bytes in data
1100 * @param data pointer to the result data
1102 * TODO: re-issue the request after certain time? cancel after X results?
1105 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1106 const struct GNUNET_HashCode * key,
1107 const struct GNUNET_PeerIdentity *get_path,
1108 unsigned int get_path_length,
1109 const struct GNUNET_PeerIdentity *put_path,
1110 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1111 size_t size, const void *data);
1115 * Function to process DHT string to regex matching.
1116 * Called on each result obtained for the DHT search.
1118 * @param cls closure (search context)
1119 * @param exp when will this value expire
1120 * @param key key of the result
1121 * @param get_path path of the get request (not used)
1122 * @param get_path_length lenght of get_path (not used)
1123 * @param put_path path of the put request (not used)
1124 * @param put_path_length length of the put_path (not used)
1125 * @param type type of the result
1126 * @param size number of bytes in data
1127 * @param data pointer to the result data
1130 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1131 const struct GNUNET_HashCode * key,
1132 const struct GNUNET_PeerIdentity *get_path,
1133 unsigned int get_path_length,
1134 const struct GNUNET_PeerIdentity *put_path,
1135 unsigned int put_path_length,
1136 enum GNUNET_BLOCK_Type type,
1137 size_t size, const void *data);
1141 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1142 * and insert it in the appropiate structures if the peer is not known yet.
1144 * @param peer Short identity of the peer.
1146 * @return Existing or newly created peer info.
1148 static struct MeshPeerInfo *
1149 peer_info_get_short (const GNUNET_PEER_Id peer);
1153 * Try to establish a new connection to this peer.
1154 * Use the best path for the given tunnel.
1155 * If the peer doesn't have any path to it yet, try to get one.
1156 * If the peer already has some path, send a CREATE PATH towards it.
1158 * @param peer PeerInfo of the peer.
1159 * @param t Tunnel for which to create the path, if possible.
1162 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1166 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1167 * needed rescources.
1168 * If peer already exists, reevaluate shortest path and change if different.
1170 * @param t Tunnel we want to add a new peer to
1171 * @param peer PeerInfo of the peer being added
1175 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1179 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1180 * that are no longer needed, as well as nodes of no longer reachable peers.
1181 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1183 * @param t Tunnel from which to remove the path.
1184 * @param peer Short id of the peer which should be removed.
1187 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1191 * Search for a tunnel by global ID using full PeerIdentities.
1193 * @param oid owner of the tunnel.
1194 * @param tid global tunnel number.
1196 * @return tunnel handler, NULL if doesn't exist.
1198 static struct MeshTunnel *
1199 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1203 * Delete an active client from the tunnel.
1209 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1212 * Notify a tunnel that a connection has broken that affects at least
1213 * some of its peers.
1215 * @param t Tunnel affected.
1216 * @param p1 Peer that got disconnected from p2.
1217 * @param p2 Peer that got disconnected from p1.
1219 * @return Short ID of the peer disconnected (either p1 or p2).
1220 * 0 if the tunnel remained unaffected.
1222 static GNUNET_PEER_Id
1223 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1228 * Get the current ack value for a tunnel, for data going from root to leaves,
1229 * taking in account the tunnel mode and the status of all children and clients.
1233 * @return Maximum PID allowed.
1236 tunnel_get_fwd_ack (struct MeshTunnel *t);
1240 * Add a client to a tunnel, initializing all needed data structures.
1242 * @param t Tunnel to which add the client.
1243 * @param c Client which to add to the tunnel.
1246 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1250 * Jump to the next edge, with the longest matching token.
1252 * @param block Block found in the DHT.
1253 * @param size Size of the block.
1254 * @param ctx Context of the search.
1256 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1259 regex_next_edge (const struct MeshRegexBlock *block,
1261 struct MeshRegexSearchContext *ctx);
1265 * Find a path to a peer that offers a regex servcie compatible
1266 * with a given string.
1268 * @param key The key of the accepting state.
1269 * @param ctx Context containing info about the string, tunnel, etc.
1272 regex_find_path (const struct GNUNET_HashCode *key,
1273 struct MeshRegexSearchContext *ctx);
1277 * @brief Queue and pass message to core when possible.
1279 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1280 * and accounts for it. In case the queue is full, the message is dropped and
1283 * Otherwise, message is treated as internal and allowed to go regardless of
1286 * @param cls Closure (@c type dependant). It will be used by queue_send to
1287 * build the message to be sent if not already prebuilt.
1288 * @param type Type of the message, 0 for a raw message.
1289 * @param size Size of the message.
1290 * @param dst Neighbor to send message to.
1291 * @param t Tunnel this message belongs to.
1294 queue_add (void *cls, uint16_t type, size_t size,
1295 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1299 * Free a transmission that was already queued with all resources
1300 * associated to the request.
1302 * @param queue Queue handler to cancel.
1303 * @param clear_cls Is it necessary to free associated cls?
1306 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1310 * @brief Get the next transmittable message from the queue.
1312 * This will be the head, except in the case of being a data packet
1313 * not allowed by the destination peer.
1315 * @param peer Destination peer.
1317 * @return The next viable MeshPeerQueue element to send to that peer.
1318 * NULL when there are no transmittable messages.
1320 struct MeshPeerQueue *
1321 queue_get_next (const struct MeshPeerInfo *peer);
1325 * Core callback to write a queued packet to core buffer
1327 * @param cls Closure (peer info).
1328 * @param size Number of bytes available in buf.
1329 * @param buf Where the to write the message.
1331 * @return number of bytes written to buf
1334 queue_send (void *cls, size_t size, void *buf);
1336 /******************************************************************************/
1337 /************************ ITERATORS ****************************/
1338 /******************************************************************************/
1341 * Iterator over found existing mesh regex blocks that match an ongoing search.
1343 * @param cls closure
1344 * @param key current key code
1345 * @param value value in the hash map
1346 * @return GNUNET_YES if we should continue to iterate,
1350 regex_result_iterator (void *cls,
1351 const struct GNUNET_HashCode * key,
1354 struct MeshRegexBlock *block = value;
1355 struct MeshRegexSearchContext *ctx = cls;
1357 if (GNUNET_YES == ntohl(block->accepting) &&
1358 ctx->position == strlen (ctx->info->description))
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1361 regex_find_path (key, ctx);
1362 return GNUNET_YES; // We found an accept state!
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1367 ctx->position, strlen(ctx->info->description),
1368 ntohl(block->accepting));
1371 regex_next_edge(block, SIZE_MAX, ctx);
1373 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1380 * Iterator over edges in a regex block retrieved from the DHT.
1382 * @param cls Closure (context of the search).
1383 * @param token Token that follows to next state.
1384 * @param len Lenght of token.
1385 * @param key Hash of next state.
1387 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1390 regex_edge_iterator (void *cls,
1393 const struct GNUNET_HashCode *key)
1395 struct MeshRegexSearchContext *ctx = cls;
1396 struct MeshRegexSearchInfo *info = ctx->info;
1400 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1405 current = &info->description[ctx->position];
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1407 current_len = strlen (info->description) - ctx->position;
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* token : %.*s\n", len, token);
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1412 if (len > current_len)
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1415 return GNUNET_YES; // Token too long, wont match
1417 if (0 != strncmp (current, token, len))
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1420 return GNUNET_YES; // Token doesn't match
1423 if (len > ctx->longest_match)
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1426 ctx->longest_match = len;
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1440 * Jump to the next edge, with the longest matching token.
1442 * @param block Block found in the DHT.
1443 * @param size Size of the block.
1444 * @param ctx Context of the search.
1446 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1449 regex_next_edge (const struct MeshRegexBlock *block,
1451 struct MeshRegexSearchContext *ctx)
1453 struct MeshRegexSearchContext *new_ctx;
1454 struct MeshRegexSearchInfo *info = ctx->info;
1455 struct GNUNET_DHT_GetHandle *get_h;
1459 /* Find the longest match for the current string position,
1460 * among tokens in the given block */
1461 ctx->longest_match = 0;
1462 result = GNUNET_MESH_regex_block_iterate (block, size,
1463 ®ex_edge_iterator, ctx);
1464 GNUNET_break (GNUNET_OK == result);
1466 /* Did anything match? */
1467 if (0 == ctx->longest_match)
1470 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1471 new_ctx->info = info;
1472 new_ctx->position = ctx->position + ctx->longest_match;
1473 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1475 /* Check whether we already have a DHT GET running for it */
1477 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1480 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1482 ®ex_result_iterator,
1484 // FIXME: "leaks" new_ctx? avoid keeping it around?
1485 return; // We are already looking for it
1488 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1490 /* Start search in DHT */
1491 rest = &new_ctx->info->description[new_ctx->position];
1493 GNUNET_DHT_get_start (dht_handle, /* handle */
1494 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1495 &ctx->hash, /* key to search */
1496 dht_replication_level, /* replication level */
1497 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1499 // FIXME add BLOOMFILTER to exclude filtered peers
1500 strlen(rest) + 1, /* xquery bits */
1501 // FIXME add BLOOMFILTER SIZE
1502 &dht_get_string_handler, new_ctx);
1504 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1507 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1516 * Iterator over hash map entries to cancel DHT GET requests after a
1517 * successful connect_by_string.
1519 * @param cls Closure (unused).
1520 * @param key Current key code (unused).
1521 * @param value Value in the hash map (get handle).
1522 * @return GNUNET_YES if we should continue to iterate,
1526 regex_cancel_dht_get (void *cls,
1527 const struct GNUNET_HashCode * key,
1530 struct GNUNET_DHT_GetHandle *h = value;
1532 GNUNET_DHT_get_stop (h);
1538 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1539 * search for connect_by_string.
1541 * @param cls Closure (unused).
1542 * @param key Current key code (unused).
1543 * @param value MeshRegexBlock in the hash map.
1544 * @return GNUNET_YES if we should continue to iterate,
1548 regex_free_result (void *cls,
1549 const struct GNUNET_HashCode * key,
1553 GNUNET_free (value);
1559 * Find a path to a peer that offers a regex servcie compatible
1560 * with a given string.
1562 * @param key The key of the accepting state.
1563 * @param ctx Context containing info about the string, tunnel, etc.
1566 regex_find_path (const struct GNUNET_HashCode *key,
1567 struct MeshRegexSearchContext *ctx)
1569 struct GNUNET_DHT_GetHandle *get_h;
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1572 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1573 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1574 key, /* key to search */
1575 dht_replication_level, /* replication level */
1576 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE/* |
1577 GNUNET_DHT_RO_RECORD_ROUTE*/,
1579 // FIXME add BLOOMFILTER to exclude filtered peers
1580 0, /* xquery bits */
1581 // FIXME add BLOOMFILTER SIZE
1582 &dht_get_string_accept_handler, ctx);
1583 GNUNET_break (GNUNET_OK ==
1584 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1592 * Function called if the connect attempt to a peer found via
1593 * connect_by_string times out. Try to connect to another peer, if any.
1594 * Otherwise try to reconnect to the same peer.
1596 * @param cls Closure (info about regex search).
1597 * @param tc TaskContext.
1600 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1602 struct MeshRegexSearchInfo *info = cls;
1603 struct MeshPeerInfo *peer_info;
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1608 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1609 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1617 if (0 < info->n_peers)
1619 // Select next peer, put current in that spot.
1620 id = info->peers[info->i_peer];
1621 info->peers[info->i_peer] = info->peer;
1622 info->i_peer = (info->i_peer + 1) % info->n_peers;
1626 // Try to connect to same peer again.
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1631 peer_info = peer_info_get_short(id);
1632 tunnel_add_peer (info->t, peer_info);
1634 tunnel_delete_peer (info->t, old);
1635 peer_info_connect (peer_info, info->t);
1636 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1637 ®ex_connect_timeout,
1639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1644 * Cancel an ongoing regex search in the DHT and free all resources.
1646 * @param ctx The search context.
1649 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1651 struct MeshRegexSearchInfo *info = ctx->info;
1654 GNUNET_free (info->description);
1655 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1656 ®ex_cancel_dht_get, NULL);
1657 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1658 ®ex_free_result, NULL);
1659 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1660 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1661 info->t->regex_ctx = NULL;
1662 for (i = 0; i < info->n_contexts; i++)
1664 GNUNET_free (info->contexts[i]);
1666 if (0 < info->n_contexts)
1667 GNUNET_free (info->contexts);
1668 if (0 < info->n_peers)
1669 GNUNET_free (info->peers);
1670 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1672 GNUNET_SCHEDULER_cancel(info->timeout);
1678 /******************************************************************************/
1679 /************************ PERIODIC FUNCTIONS ****************************/
1680 /******************************************************************************/
1683 * Announce iterator over for each application provided by the peer
1685 * @param cls closure
1686 * @param key current key code
1687 * @param value value in the hash map
1688 * @return GNUNET_YES if we should continue to
1693 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1695 struct PBlock block;
1696 struct MeshClient *c;
1698 block.id = my_full_id;
1699 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1700 GNUNET_assert(NULL != c);
1701 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1702 if (0 == block.type)
1707 block.type = htonl (block.type);
1709 GNUNET_break (NULL !=
1710 GNUNET_DHT_put (dht_handle, key,
1711 dht_replication_level,
1712 GNUNET_DHT_RO_RECORD_ROUTE |
1713 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1714 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1716 (const char *) &block,
1717 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1718 app_announce_time, NULL, NULL));
1725 * Function called when the DHT regex put is complete.
1727 * @param the 'struct MeshClient' for which we were PUTting
1728 * @param success GNUNET_OK if the PUT was transmitted,
1729 * GNUNET_NO on timeout,
1730 * GNUNET_SYSERR on disconnect from service
1731 * after the PUT message was transmitted
1732 * (so we don't know if it was received or not)
1735 announce_regex_done (void *cls,
1738 struct MeshClient *c = cls;
1745 * Regex callback iterator to store own service description in the DHT.
1747 * @param cls closure.
1748 * @param key hash for current state.
1749 * @param proof proof for current state.
1750 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1751 * @param num_edges number of edges leaving current state.
1752 * @param edges edges leaving current state.
1755 regex_iterator (void *cls,
1756 const struct GNUNET_HashCode *key,
1759 unsigned int num_edges,
1760 const struct GNUNET_REGEX_Edge *edges)
1762 struct MeshRegexBlock *block;
1763 struct MeshRegexEdge *block_edge;
1764 enum GNUNET_DHT_RouteOption opt;
1768 unsigned int offset;
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772 " regex dht put for state %s\n",
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1782 if (GNUNET_YES == accepting)
1784 struct MeshRegexAccept block;
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1787 " state %s is accepting, putting own id\n",
1789 size = sizeof (block);
1791 block.id = my_full_id;
1792 GNUNET_STATISTICS_update (stats, "# regex accepting blocks stored",
1794 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes stored",
1795 sizeof (block), GNUNET_NO);
1797 GNUNET_DHT_put(dht_handle, key,
1798 dht_replication_level,
1799 opt/* | GNUNET_DHT_RO_RECORD_ROUTE*/,
1800 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1803 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: expiration time should be option */
1807 len = strlen(proof);
1808 size = sizeof (struct MeshRegexBlock) + len;
1809 block = GNUNET_malloc (size);
1812 block->n_proof = htonl (len);
1813 block->n_edges = htonl (num_edges);
1814 block->accepting = htonl (accepting);
1816 /* Store the proof at the end of the block. */
1817 aux = (char *) &block[1];
1818 memcpy (aux, proof, len);
1821 /* Store each edge in a variable length MeshEdge struct at the
1822 * very end of the MeshRegexBlock structure.
1824 for (i = 0; i < num_edges; i++)
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1827 " edge %s towards %s\n",
1829 GNUNET_h2s(&edges[i].destination));
1831 /* aux points at the end of the last block */
1832 len = strlen (edges[i].label);
1833 size += sizeof (struct MeshRegexEdge) + len;
1834 // Calculate offset FIXME is this ok? use size instead?
1835 offset = aux - (char *) block;
1836 block = GNUNET_realloc (block, size);
1837 aux = &((char *) block)[offset];
1838 block_edge = (struct MeshRegexEdge *) aux;
1839 block_edge->key = edges[i].destination;
1840 block_edge->n_token = htonl (len);
1841 aux = (char *) &block_edge[1];
1842 memcpy (aux, edges[i].label, len);
1846 GNUNET_DHT_put(dht_handle, key,
1847 dht_replication_level,
1849 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1851 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1854 GNUNET_STATISTICS_update (stats, "# regex blocks stored",
1856 GNUNET_STATISTICS_update (stats, "# regex block bytes stored",
1859 GNUNET_free (block);
1864 * Store the regular expression describing a local service into the DHT.
1866 * @param regex The regular expresion.
1869 regex_put (struct MeshRegexDescriptor *regex)
1871 if (NULL == regex->dfa)
1873 regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1874 strlen (regex->regex),
1875 regex->compression);
1878 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1879 GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL);
1880 DEBUG_DHT (" regex_put (%s) end\n", regex->regex);
1885 * Periodically announce what applications are provided by local clients
1888 * @param cls closure
1889 * @param tc task context
1892 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1894 struct MeshClient *c = cls;
1897 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1898 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1900 DEBUG_DHT ("Starting PUT for regex\n");
1901 for (i = 0; i < c->n_regex; i++)
1902 regex_put (&c->regexes[i]);
1903 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1906 DEBUG_DHT ("Finished PUT for regex\n");
1911 * Periodically announce what applications are provided by local clients
1914 * @param cls closure
1915 * @param tc task context
1918 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1920 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1922 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1926 DEBUG_DHT ("Starting PUT for apps\n");
1928 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1930 announce_applications_task =
1931 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1933 DEBUG_DHT ("Finished PUT for apps\n");
1938 * Periodically announce self id in the DHT
1940 * @param cls closure
1941 * @param tc task context
1944 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1946 struct PBlock block;
1948 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1950 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1954 * - Set data expiration in function of X
1955 * - Adapt X to churn
1957 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1959 block.id = my_full_id;
1960 block.type = htonl (0);
1961 GNUNET_DHT_put (dht_handle, /* DHT handle */
1962 &my_full_id.hashPubKey, /* Key to use */
1963 dht_replication_level, /* Replication level */
1964 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1965 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1966 sizeof (block), /* Size of the data */
1967 (const char *) &block, /* Data itself */
1968 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1969 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1970 NULL, /* Continuation */
1971 NULL); /* Continuation closure */
1973 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1977 /******************************************************************************/
1978 /****************** GENERAL HELPER FUNCTIONS ************************/
1979 /******************************************************************************/
1982 * Decrements the reference counter and frees all resources if needed
1984 * @param mesh_data Data Descriptor used in a multicast message.
1985 * Freed no longer needed (last message).
1988 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1990 if (0 == --(mesh_data->reference_counter))
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1993 GNUNET_free (mesh_data->data);
1994 GNUNET_free (mesh_data);
2000 * Check if client has registered with the service and has not disconnected
2002 * @param client the client to check
2004 * @return non-NULL if client exists in the global DLL
2006 static struct MeshClient *
2007 client_get (struct GNUNET_SERVER_Client *client)
2009 struct MeshClient *c;
2014 if (c->handle == client)
2023 * Checks if a given client has subscribed to certain message type
2025 * @param message_type Type of message to check
2026 * @param c Client to check
2028 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
2030 * FIXME: use of crypto_hash slows it down
2031 * The hash function alone takes 8-10us out of the ~55us for the whole
2032 * process of retransmitting the message from one local client to another.
2033 * Find faster implementation!
2036 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
2038 struct GNUNET_HashCode hc;
2040 if (NULL == c->types)
2043 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
2044 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2049 * Check whether client wants traffic from a tunnel.
2051 * @param c Client to check.
2052 * @param t Tunnel to be found.
2054 * @return GNUNET_YES if client knows tunnel.
2056 * TODO look in client hashmap
2059 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2063 for (i = 0; i < t->nclients; i++)
2064 if (t->clients[i] == c)
2071 * Check whether client has been informed about a tunnel.
2073 * @param c Client to check.
2074 * @param t Tunnel to be found.
2076 * @return GNUNET_YES if client knows tunnel.
2078 * TODO look in client hashmap
2081 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2085 for (i = 0; i < t->nignore; i++)
2086 if (t->ignore[i] == c)
2088 return client_wants_tunnel(c, t);
2093 * Marks a client as uninterested in traffic from the tunnel, updating both
2094 * client and tunnel to reflect this.
2096 * @param c Client that doesn't want traffic anymore.
2097 * @param t Tunnel which should be ignored.
2099 * FIXME when to delete an incoming tunnel?
2102 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2104 struct GNUNET_HashCode hash;
2106 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2107 GNUNET_break (GNUNET_YES ==
2108 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2110 GNUNET_break (GNUNET_YES ==
2111 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2112 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2113 tunnel_delete_active_client (t, c);
2114 GNUNET_array_append (t->ignore, t->nignore, c);
2119 * Deletes a tunnel from a client (either owner or destination). To be used on
2120 * tunnel destroy, otherwise, use client_ignore_tunnel.
2122 * @param c Client whose tunnel to delete.
2123 * @param t Tunnel which should be deleted.
2126 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2128 struct GNUNET_HashCode hash;
2132 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2133 GNUNET_assert (GNUNET_YES ==
2134 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2140 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2142 GNUNET_assert (GNUNET_YES ==
2143 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2147 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2155 * Send the message to all clients that have subscribed to its type
2157 * @param msg Pointer to the message itself
2158 * @param payload Pointer to the payload of the message.
2159 * @param t The tunnel to whose clients this message goes.
2161 * @return number of clients this message was sent to
2164 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2165 const struct GNUNET_MessageHeader *payload,
2166 struct MeshTunnel *t)
2168 struct MeshClient *c;
2169 MESH_TunnelNumber *tid;
2172 char cbuf[htons (msg->size)];
2174 type = ntohs (payload->type);
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2177 GNUNET_MESH_DEBUG_M2S (type));
2179 memcpy (cbuf, msg, sizeof (cbuf));
2180 switch (htons (msg->type))
2182 struct GNUNET_MESH_Unicast *uc;
2183 struct GNUNET_MESH_Multicast *mc;
2184 struct GNUNET_MESH_ToOrigin *to;
2186 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2187 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2190 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2191 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2194 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2195 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2203 for (count = 0, c = clients; c != NULL; c = c->next)
2205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2206 if (client_is_subscribed (type, c))
2208 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2212 *tid = htonl (t->local_tid);
2216 if (GNUNET_NO == client_knows_tunnel (c, t))
2218 /* This client doesn't know the tunnel */
2219 struct GNUNET_MESH_TunnelNotification tmsg;
2220 struct GNUNET_HashCode hash;
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2223 tmsg.header.size = htons (sizeof (tmsg));
2224 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2225 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2226 tmsg.tunnel_id = htonl (t->local_tid_dest);
2228 if (GNUNET_YES == t->speed_min)
2229 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2230 if (GNUNET_YES == t->nobuffer)
2231 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2232 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2233 &tmsg.header, GNUNET_NO);
2234 tunnel_add_client (t, c);
2235 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2237 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2238 c->incoming_tunnels, &hash, t,
2239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2241 *tid = htonl (t->local_tid_dest);
2244 /* Check if the client wants to get traffic from the tunnel */
2245 if (GNUNET_NO == client_wants_tunnel(c, t))
2248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2249 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2250 (struct GNUNET_MessageHeader
2251 *) cbuf, GNUNET_NO);
2260 * Notify the client that owns the tunnel that a peer has connected to it
2261 * (the requested path to it has been confirmed).
2263 * @param t Tunnel whose owner to notify
2264 * @param id Short id of the peer that has connected
2267 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2269 struct GNUNET_MESH_PeerControl pc;
2271 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2272 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2273 pc.tunnel_id = htonl (t->local_tid);
2274 GNUNET_PEER_resolve (id, &pc.peer);
2275 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2281 * Notify all clients (not depending on registration status) that the incoming
2282 * tunnel is no longer valid.
2284 * @param t Tunnel that was destroyed.
2287 send_clients_tunnel_destroy (struct MeshTunnel *t)
2289 struct GNUNET_MESH_TunnelMessage msg;
2291 msg.header.size = htons (sizeof (msg));
2292 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2293 msg.tunnel_id = htonl (t->local_tid_dest);
2294 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2299 * Notify clients of tunnel disconnections, if needed.
2300 * In case the origin disconnects, the destination clients get a tunnel destroy
2301 * notification. If the last destination disconnects (only one remaining client
2302 * in tunnel), the origin gets a (local ID) peer disconnected.
2303 * Note that the function must be called BEFORE removing the client from
2306 * @param t Tunnel that was destroyed.
2307 * @param c Client that disconnected.
2310 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2316 struct GNUNET_MESH_TunnelMessage msg;
2318 msg.header.size = htons (sizeof (msg));
2319 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2320 msg.tunnel_id = htonl (t->local_tid_dest);
2321 for (i = 0; i < t->nclients; i++)
2322 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2323 &msg.header, GNUNET_NO);
2325 // FIXME when to disconnect an incoming tunnel?
2326 else if (1 == t->nclients && NULL != t->owner)
2328 struct GNUNET_MESH_PeerControl msg;
2330 msg.header.size = htons (sizeof (msg));
2331 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2332 msg.tunnel_id = htonl (t->local_tid);
2333 msg.peer = my_full_id;
2334 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2335 &msg.header, GNUNET_NO);
2341 * Iterator over all the peers to remove the oldest not-used entry.
2343 * @param cls Closure (unsued).
2344 * @param key ID of the peer.
2345 * @param value Peer_Info of the peer.
2350 peer_info_timeout (void *cls,
2351 const struct GNUNET_HashCode *key,
2358 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2359 * and insert it in the appropiate structures if the peer is not known yet.
2361 * @param peer Full identity of the peer.
2363 * @return Existing or newly created peer info.
2365 static struct MeshPeerInfo *
2366 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2368 struct MeshPeerInfo *peer_info;
2370 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2371 if (NULL == peer_info)
2374 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2375 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2377 GNUNET_CONTAINER_multihashmap_iterate (peers,
2381 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2383 peer_info->id = GNUNET_PEER_intern (peer);
2385 peer_info->last_contact = GNUNET_TIME_absolute_get();
2392 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2393 * and insert it in the appropiate structures if the peer is not known yet.
2395 * @param peer Short identity of the peer.
2397 * @return Existing or newly created peer info.
2399 static struct MeshPeerInfo *
2400 peer_info_get_short (const GNUNET_PEER_Id peer)
2402 struct GNUNET_PeerIdentity id;
2404 GNUNET_PEER_resolve (peer, &id);
2405 return peer_info_get (&id);
2410 * Iterator to remove the tunnel from the list of tunnels a peer participates
2413 * @param cls Closure (tunnel info)
2414 * @param key GNUNET_PeerIdentity of the peer (unused)
2415 * @param value PeerInfo of the peer
2417 * @return always GNUNET_YES, to keep iterating
2420 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2422 struct MeshTunnel *t = cls;
2423 struct MeshPeerInfo *peer = value;
2426 for (i = 0; i < peer->ntunnels; i++)
2429 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2432 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2433 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2442 * Core callback to write a pre-constructed data packet to core buffer
2444 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2445 * @param size Number of bytes available in buf.
2446 * @param buf Where the to write the message.
2448 * @return number of bytes written to buf
2451 send_core_data_raw (void *cls, size_t size, void *buf)
2453 struct MeshTransmissionDescriptor *info = cls;
2454 struct GNUNET_MessageHeader *msg;
2457 GNUNET_assert (NULL != info);
2458 GNUNET_assert (NULL != info->mesh_data);
2459 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2460 total_size = ntohs (msg->size);
2462 if (total_size > size)
2467 memcpy (buf, msg, total_size);
2468 data_descriptor_decrement_rc (info->mesh_data);
2475 * Sends an already built non-multicast message to a peer,
2476 * properly registrating all used resources.
2478 * @param message Message to send. Function makes a copy of it.
2479 * @param peer Short ID of the neighbor whom to send the message.
2480 * @param t Tunnel on which this message is transmitted.
2483 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2484 const struct GNUNET_PeerIdentity *peer,
2485 struct MeshTunnel *t)
2487 struct MeshTransmissionDescriptor *info;
2488 struct MeshPeerInfo *neighbor;
2489 struct MeshPeerPath *p;
2493 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2495 size = ntohs (message->size);
2496 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2497 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2498 info->mesh_data->data = GNUNET_malloc (size);
2499 memcpy (info->mesh_data->data, message, size);
2500 type = ntohs(message->type);
2503 struct GNUNET_MESH_Unicast *m;
2504 struct GNUNET_MESH_ToOrigin *to;
2506 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2507 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2508 m->ttl = htonl (ntohl (m->ttl) - 1);
2510 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2511 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2513 to->pid = htonl(t->bck_pid);
2515 info->mesh_data->data_len = size;
2516 info->mesh_data->reference_counter = 1;
2517 info->mesh_data->total_out = 1;
2518 neighbor = peer_info_get (peer);
2519 for (p = neighbor->path_head; NULL != p; p = p->next)
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 " %s IS NOT DIRECTLY CONNECTED\n",
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 for (p = neighbor->path_head; NULL != p; p = p->next)
2537 struct GNUNET_PeerIdentity debug_id;
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 " path with %u hops through:\n",
2543 for (i = 0; i < p->length; i++)
2545 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2548 i, GNUNET_i2s(&debug_id));
2552 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2554 " no direct connection to %s\n",
2556 GNUNET_free (info->mesh_data->data);
2557 GNUNET_free (info->mesh_data);
2561 info->peer = neighbor;
2562 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2573 * Sends a CREATE PATH message for a path to a peer, properly registrating
2574 * all used resources.
2576 * @param peer PeerInfo of the final peer for whom this path is being created.
2577 * @param p Path itself.
2578 * @param t Tunnel for which the path is created.
2581 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2582 struct MeshTunnel *t)
2584 struct GNUNET_PeerIdentity id;
2585 struct MeshPathInfo *path_info;
2586 struct MeshPeerInfo *neighbor;
2592 p = tree_get_path_to_peer (t->tree, peer->id);
2599 for (i = 0; i < p->length; i++)
2601 if (p->peers[i] == myid)
2604 if (i >= p->length - 1)
2610 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2612 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2613 path_info->path = p;
2615 neighbor = peer_info_get (&id);
2616 path_info->peer = neighbor;
2617 queue_add (path_info,
2618 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2619 sizeof (struct GNUNET_MESH_ManipulatePath) +
2620 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2627 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2629 * @param t Tunnel whose path to destroy.
2630 * @param destination Short ID of the peer to whom the path to destroy.
2633 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2635 struct MeshPeerPath *p;
2638 p = tree_get_path_to_peer (t->tree, destination);
2644 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2645 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2647 struct GNUNET_MESH_ManipulatePath *msg;
2648 struct GNUNET_PeerIdentity *pi;
2652 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2653 msg->header.size = htons (size);
2654 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2655 msg->tid = htonl (t->id.tid);
2656 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2657 for (i = 0; i < p->length; i++)
2659 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2661 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2668 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2670 * @param t Tunnel which to confirm.
2673 send_path_ack (struct MeshTunnel *t)
2675 struct MeshTransmissionDescriptor *info;
2676 struct GNUNET_PeerIdentity id;
2677 GNUNET_PEER_Id peer;
2679 peer = tree_get_predecessor (t->tree);
2680 GNUNET_PEER_resolve (peer, &id);
2681 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2682 info->origin = &t->id;
2683 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2684 GNUNET_assert (NULL != info->peer);
2687 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2688 sizeof (struct GNUNET_MESH_PathACK),
2695 * Try to establish a new connection to this peer.
2696 * Use the best path for the given tunnel.
2697 * If the peer doesn't have any path to it yet, try to get one.
2698 * If the peer already has some path, send a CREATE PATH towards it.
2700 * @param peer PeerInfo of the peer.
2701 * @param t Tunnel for which to create the path, if possible.
2704 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2706 struct MeshPeerPath *p;
2707 struct MeshPathInfo *path_info;
2709 if (NULL != peer->path_head)
2711 p = tree_get_path_to_peer (t->tree, peer->id);
2718 // FIXME always send create path to self
2721 send_create_path (peer, p, t);
2725 struct GNUNET_HashCode hash;
2728 send_client_peer_connected (t, myid);
2729 t->local_tid_dest = next_local_tid++;
2730 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2733 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2734 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2741 else if (NULL == peer->dhtget)
2743 struct GNUNET_PeerIdentity id;
2745 GNUNET_PEER_resolve (peer->id, &id);
2746 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2747 path_info->peer = peer;
2749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2750 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2751 peer->dhtgetcls = path_info;
2752 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2753 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2754 &id.hashPubKey, /* key to search */
2755 dht_replication_level, /* replication level */
2756 GNUNET_DHT_RO_RECORD_ROUTE |
2757 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2758 NULL, /* xquery */ // FIXME BLOOMFILTER
2759 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2760 &dht_get_id_handler, path_info);
2762 /* Otherwise, there is no path but the DHT get is already started. */
2767 * Task to delay the connection of a peer
2769 * @param cls Closure (path info with tunnel and peer to connect).
2770 * Will be free'd on exection.
2771 * @param tc TaskContext
2774 peer_info_connect_task (void *cls,
2775 const struct GNUNET_SCHEDULER_TaskContext *tc)
2777 struct MeshPathInfo *path_info = cls;
2779 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2781 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2786 peer_info_connect (path_info->peer, path_info->t);
2792 * Destroy the peer_info and free any allocated resources linked to it
2794 * @param pi The peer_info to destroy.
2796 * @return GNUNET_OK on success
2799 peer_info_destroy (struct MeshPeerInfo *pi)
2801 struct GNUNET_PeerIdentity id;
2802 struct MeshPeerPath *p;
2803 struct MeshPeerPath *nextp;
2805 GNUNET_PEER_resolve (pi->id, &id);
2806 GNUNET_PEER_change_rc (pi->id, -1);
2809 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2812 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2813 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2815 if (NULL != pi->dhtget)
2817 GNUNET_DHT_get_stop (pi->dhtget);
2818 GNUNET_free (pi->dhtgetcls);
2824 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2828 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2830 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2838 * Remove all paths that rely on a direct connection between p1 and p2
2839 * from the peer itself and notify all tunnels about it.
2841 * @param peer PeerInfo of affected peer.
2842 * @param p1 GNUNET_PEER_Id of one peer.
2843 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2846 * TODO: optimize (see below)
2849 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2852 struct MeshPeerPath *p;
2853 struct MeshPeerPath *aux;
2854 struct MeshPeerInfo *peer_d;
2856 unsigned int destroyed;
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2863 p = peer->path_head;
2867 for (i = 0; i < (p->length - 1); i++)
2869 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2870 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2872 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2883 for (i = 0; i < peer->ntunnels; i++)
2885 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2889 * Problem: one or more peers have been deleted from the tunnel tree.
2890 * We don't know who they are to try to add them again.
2891 * We need to try to find a new path for each of the disconnected peers.
2892 * Some of them might already have a path to reach them that does not
2893 * involve p1 and p2. Adding all anew might render in a better tree than
2894 * the trivial immediate fix.
2896 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2897 * its children will be reachable trough him.
2899 peer_d = peer_info_get_short (d);
2902 for (p = peer_d->path_head; NULL != p; p = p->next)
2904 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2912 /* No callback, as peer will be already disconnected and a connection
2913 * scheduled by tunnel_notify_connection_broken.
2915 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2919 peer_info_connect (peer_d, peer->tunnels[i]);
2922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2927 * Add the path to the peer and update the path used to reach it in case this
2930 * @param peer_info Destination peer to add the path to.
2931 * @param path New path to add. Last peer must be the peer in arg 1.
2932 * Path will be either used of freed if already known.
2933 * @param trusted Do we trust that this path is real?
2936 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2939 struct MeshPeerPath *aux;
2943 if ((NULL == peer_info) || (NULL == path))
2946 path_destroy (path);
2949 if (path->peers[path->length - 1] != peer_info->id)
2952 path_destroy (path);
2955 if (path->length <= 2 && GNUNET_NO == trusted)
2957 /* Only allow CORE to tell us about direct paths */
2958 path_destroy (path);
2961 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2962 for (l = 1; l < path->length; l++)
2964 if (path->peers[l] == myid)
2966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2967 for (l2 = 0; l2 < path->length - l; l2++)
2969 path->peers[l2] = path->peers[l + l2];
2974 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2979 struct GNUNET_PeerIdentity id;
2981 GNUNET_PEER_resolve (peer_info->id, &id);
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2983 path->length, GNUNET_i2s (&id));
2986 l = path_get_length (path);
2993 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2994 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2996 l2 = path_get_length (aux);
2999 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
3000 peer_info->path_tail, aux, path);
3005 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
3007 path_destroy (path);
3012 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
3019 * Add the path to the origin peer and update the path used to reach it in case
3020 * this is the shortest.
3021 * The path is given in peer_info -> destination, therefore we turn the path
3022 * upside down first.
3024 * @param peer_info Peer to add the path to, being the origin of the path.
3025 * @param path New path to add after being inversed.
3026 * @param trusted Do we trust that this path is real?
3029 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
3030 struct MeshPeerPath *path, int trusted)
3033 peer_info_add_path (peer_info, path, trusted);
3038 * Function called if the connection to the peer has been stalled for a while,
3039 * possibly due to a missed ACK. Poll the peer about its ACK status.
3041 * @param cls Closure (cinfo).
3042 * @param tc TaskContext.
3045 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3047 struct MeshTunnelChildInfo *cinfo = cls;
3048 struct GNUNET_MESH_Poll msg;
3049 struct GNUNET_PeerIdentity id;
3050 struct MeshTunnel *t;
3052 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3053 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3059 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3060 msg.header.size = htons (sizeof (msg));
3061 msg.tid = htonl (t->id.tid);
3062 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3063 msg.last_ack = htonl (cinfo->fwd_ack);
3065 GNUNET_PEER_resolve (cinfo->id, &id);
3066 send_prebuilt_message (&msg.header, &id, cinfo->t);
3067 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
3069 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
3070 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3071 &tunnel_poll, cinfo);
3076 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3077 * to obtain a local peer -> destination path and interning the peer ids.
3079 * @return Newly allocated and created path
3081 static struct MeshPeerPath *
3082 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3083 unsigned int get_path_length,
3084 const struct GNUNET_PeerIdentity *put_path,
3085 unsigned int put_path_length)
3087 struct MeshPeerPath *p;
3093 GNUNET_PEER_change_rc (myid, 1);
3094 i = get_path_length;
3095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3096 for (i--; i >= 0; i--)
3098 id = GNUNET_PEER_intern (&get_path[i]);
3099 if (p->length > 0 && id == p->peers[p->length - 1])
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3102 GNUNET_PEER_change_rc (id, -1);
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3107 GNUNET_i2s (&get_path[i]));
3109 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3110 p->peers[p->length - 1] = id;
3113 i = put_path_length;
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3115 for (i--; i >= 0; i--)
3117 id = GNUNET_PEER_intern (&put_path[i]);
3120 /* PUT path went through us, so discard the path up until now and start
3121 * from here to get a much shorter (and loop-free) path.
3126 if (p->length > 0 && id == p->peers[p->length - 1])
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3129 GNUNET_PEER_change_rc (id, -1);
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3134 GNUNET_i2s (&put_path[i]));
3136 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3137 p->peers[p->length - 1] = id;
3141 if (get_path_length > 0)
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3143 GNUNET_i2s (&get_path[0]));
3144 if (put_path_length > 0)
3145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3146 GNUNET_i2s (&put_path[0]));
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3149 for (i = 0; i < p->length; i++)
3151 struct GNUNET_PeerIdentity peer_id;
3153 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3155 GNUNET_i2s (&peer_id));
3163 * Adds a path to the peer_infos of all the peers in the path
3165 * @param p Path to process.
3166 * @param confirmed Whether we know if the path works or not.
3169 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3173 /* TODO: invert and add */
3174 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3175 for (i++; i < p->length; i++)
3177 struct MeshPeerInfo *aux;
3178 struct MeshPeerPath *copy;
3180 aux = peer_info_get_short (p->peers[i]);
3181 copy = path_duplicate (p);
3182 copy->length = i + 1;
3183 peer_info_add_path (aux, copy, GNUNET_NO);
3189 * Send keepalive packets for a peer
3191 * @param cls Closure (tunnel for which to send the keepalive).
3192 * @param tc Notification context.
3194 * TODO: implement explicit multicast keepalive?
3197 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3201 * Search for a tunnel among the incoming tunnels
3203 * @param tid the local id of the tunnel
3205 * @return tunnel handler, NULL if doesn't exist
3207 static struct MeshTunnel *
3208 tunnel_get_incoming (MESH_TunnelNumber tid)
3210 struct GNUNET_HashCode hash;
3212 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3213 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3214 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3219 * Search for a tunnel among the tunnels for a client
3221 * @param c the client whose tunnels to search in
3222 * @param tid the local id of the tunnel
3224 * @return tunnel handler, NULL if doesn't exist
3226 static struct MeshTunnel *
3227 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3229 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3231 return tunnel_get_incoming (tid);
3235 struct GNUNET_HashCode hash;
3237 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3238 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3244 * Search for a tunnel by global ID using PEER_ID
3246 * @param pi owner of the tunnel
3247 * @param tid global tunnel number
3249 * @return tunnel handler, NULL if doesn't exist
3251 static struct MeshTunnel *
3252 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3254 struct MESH_TunnelID id;
3255 struct GNUNET_HashCode hash;
3260 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3261 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3266 * Search for a tunnel by global ID using full PeerIdentities
3268 * @param oid owner of the tunnel
3269 * @param tid global tunnel number
3271 * @return tunnel handler, NULL if doesn't exist
3273 static struct MeshTunnel *
3274 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3276 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3281 * Delete an active client from the tunnel.
3287 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3291 for (i = 0; i < t->nclients; i++)
3293 if (t->clients[i] == c)
3295 t->clients[i] = t->clients[t->nclients - 1];
3296 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3297 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3299 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3307 * Delete an ignored client from the tunnel.
3313 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3317 for (i = 0; i < t->nignore; i++)
3319 if (t->ignore[i] == c)
3321 t->ignore[i] = t->ignore[t->nignore - 1];
3322 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3330 * Delete a client from the tunnel. It should be only done on
3331 * client disconnection, otherwise use client_ignore_tunnel.
3337 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3339 tunnel_delete_ignored_client (t, c);
3340 tunnel_delete_active_client (t, c);
3345 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3347 * Destroys queue elements of all waiting transmissions and frees all memory
3348 * used by the struct and its elements.
3350 * @param cls Closure (tunnel info).
3351 * @param key Hash of GNUNET_PEER_Id (unused).
3352 * @param value MeshTunnelChildInfo of the child.
3354 * @return always GNUNET_YES, to keep iterating
3357 tunnel_destroy_child (void *cls,
3358 const struct GNUNET_HashCode * key,
3361 struct MeshTunnelChildInfo *cinfo = value;
3362 struct MeshTunnel *t = cls;
3363 struct MeshPeerQueue *q;
3367 for (c = 0; c < cinfo->send_buffer_n; c++)
3369 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3370 q = cinfo->send_buffer[i];
3371 cinfo->send_buffer[i] = NULL;
3373 queue_destroy (q, GNUNET_YES);
3376 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3378 GNUNET_free_non_null (cinfo->send_buffer);
3379 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
3381 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
3382 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3384 GNUNET_free (cinfo);
3390 * Callback used to notify a client owner of a tunnel that a peer has
3391 * disconnected, most likely because of a path change.
3393 * @param cls Closure (tunnel this notification is about).
3394 * @param peer_id Short ID of disconnected peer.
3397 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3399 struct MeshTunnel *t = cls;
3400 struct MeshPeerInfo *peer;
3401 struct MeshPathInfo *path_info;
3403 if (NULL != t->owner && NULL != nc)
3405 struct GNUNET_MESH_PeerControl msg;
3407 msg.header.size = htons (sizeof (msg));
3408 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3409 msg.tunnel_id = htonl (t->local_tid);
3410 GNUNET_PEER_resolve (peer_id, &msg.peer);
3411 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3412 &msg.header, GNUNET_NO);
3414 peer = peer_info_get_short (peer_id);
3415 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3416 path_info->peer = peer;
3418 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3424 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3425 * needed rescources.
3426 * If peer already exists, reevaluate shortest path and change if different.
3428 * @param t Tunnel we want to add a new peer to
3429 * @param peer PeerInfo of the peer being added
3433 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3435 struct GNUNET_PeerIdentity id;
3436 struct MeshPeerPath *best_p;
3437 struct MeshPeerPath *p;
3438 unsigned int best_cost;
3441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3442 GNUNET_PEER_resolve (peer->id, &id);
3444 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3447 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3448 GNUNET_assert (GNUNET_OK ==
3449 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3451 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3454 if (NULL != (p = peer->path_head))
3457 best_cost = tree_get_path_cost (t->tree, p);
3460 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3467 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3468 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3469 t->path_refresh_task =
3470 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3474 /* Start a DHT get */
3475 peer_info_connect (peer, t);
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3481 * Add a path to a tunnel which we don't own, just to remember the next hop.
3482 * If destination node was already in the tunnel, the first hop information
3483 * will be replaced with the new path.
3485 * @param t Tunnel we want to add a new peer to
3486 * @param p Path to add
3487 * @param own_pos Position of local node in path.
3491 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3492 unsigned int own_pos)
3494 struct GNUNET_PeerIdentity id;
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3497 GNUNET_assert (0 != own_pos);
3498 tree_add_path (t->tree, p, NULL, NULL);
3499 if (own_pos < p->length - 1)
3501 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3502 tree_update_first_hops (t->tree, myid, &id);
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3508 * Add a client to a tunnel, initializing all needed data structures.
3510 * @param t Tunnel to which add the client.
3511 * @param c Client which to add to the tunnel.
3514 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3516 struct MeshTunnelClientInfo clinfo;
3518 GNUNET_array_append (t->clients, t->nclients, c);
3519 clinfo.fwd_ack = t->fwd_pid + 1;
3520 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3521 clinfo.fwd_pid = t->fwd_pid;
3522 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3524 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3529 * Notifies a tunnel that a connection has broken that affects at least
3530 * some of its peers. Sends a notification towards the root of the tree.
3531 * In case the peer is the owner of the tree, notifies the client that owns
3532 * the tunnel and tries to reconnect.
3534 * @param t Tunnel affected.
3535 * @param p1 Peer that got disconnected from p2.
3536 * @param p2 Peer that got disconnected from p1.
3538 * @return Short ID of the peer disconnected (either p1 or p2).
3539 * 0 if the tunnel remained unaffected.
3541 static GNUNET_PEER_Id
3542 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3548 tree_notify_connection_broken (t->tree, p1, p2,
3549 &tunnel_notify_client_peer_disconnected,
3551 if (myid != p1 && myid != p2)
3557 if (tree_get_predecessor (t->tree) != 0)
3559 /* We are the peer still connected, notify owner of the disconnection. */
3560 struct GNUNET_MESH_PathBroken msg;
3561 struct GNUNET_PeerIdentity neighbor;
3563 msg.header.size = htons (sizeof (msg));
3564 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3565 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3566 msg.tid = htonl (t->id.tid);
3567 msg.peer1 = my_full_id;
3568 GNUNET_PEER_resolve (pid, &msg.peer2);
3569 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3570 send_prebuilt_message (&msg.header, &neighbor, t);
3578 * Send a multicast packet to a neighbor.
3580 * @param cls Closure (Info about the multicast packet)
3581 * @param neighbor_id Short ID of the neighbor to send the packet to.
3584 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3586 struct MeshData *mdata = cls;
3587 struct MeshTransmissionDescriptor *info;
3588 struct GNUNET_PeerIdentity neighbor;
3589 struct GNUNET_MessageHeader *msg;
3591 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3593 info->mesh_data = mdata;
3594 (mdata->reference_counter) ++;
3595 info->destination = neighbor_id;
3596 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3598 GNUNET_i2s (&neighbor));
3599 info->peer = peer_info_get (&neighbor);
3600 GNUNET_assert (NULL != info->peer);
3601 msg = (struct GNUNET_MessageHeader *) mdata->data;
3604 info->mesh_data->data_len,
3611 * Queue a message in a tunnel in multicast, sending a copy to each child node
3612 * down the local one in the tunnel tree.
3614 * @param t Tunnel in which to send the data.
3615 * @param msg Message to be sent.
3618 tunnel_send_multicast (struct MeshTunnel *t,
3619 const struct GNUNET_MessageHeader *msg)
3621 struct MeshData *mdata;
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3624 " sending a multicast packet...\n");
3626 mdata = GNUNET_malloc (sizeof (struct MeshData));
3627 mdata->data_len = ntohs (msg->size);
3629 mdata->data = GNUNET_malloc (mdata->data_len);
3630 memcpy (mdata->data, msg, mdata->data_len);
3631 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3633 struct GNUNET_MESH_Multicast *mcast;
3635 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3636 if (t->fwd_queue_n >= t->fwd_queue_max)
3639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3641 " message from %s!\n",
3642 GNUNET_i2s(&mcast->oid));
3643 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3644 " message at %s!\n",
3645 GNUNET_i2s(&my_full_id));
3646 GNUNET_free (mdata->data);
3647 GNUNET_free (mdata);
3651 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3653 ntohl (mcast->ttl));
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3660 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3661 if (mdata->reference_counter == 0)
3663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3664 " no one to send data to\n");
3665 GNUNET_free (mdata->data);
3666 GNUNET_free (mdata);
3667 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3672 mdata->total_out = mdata->reference_counter;
3674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3675 " sending a multicast packet done\n");
3681 * Increase the SKIP value of all peers that
3682 * have not received a unicast message.
3684 * @param cls Closure (ID of the peer that HAS received the message).
3685 * @param key ID of the neighbor.
3686 * @param value Information about the neighbor.
3688 * @return GNUNET_YES to keep iterating.
3691 tunnel_add_skip (void *cls,
3692 const struct GNUNET_HashCode * key,
3695 struct GNUNET_PeerIdentity *neighbor = cls;
3696 struct MeshTunnelChildInfo *cinfo = value;
3698 /* TODO compare only pointers? key == neighbor? */
3699 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3709 * @brief Get neighbor's Flow Control information.
3711 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3712 * descendant of the local node in a certain tunnel.
3713 * If the info is not yet there (recently created path), creates the data struct
3714 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3717 * @param t Tunnel related.
3718 * @param peer Neighbor whose Flow Control info is needed.
3720 * @return Neighbor's Flow Control info.
3722 static struct MeshTunnelChildInfo *
3723 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3724 const struct GNUNET_PeerIdentity *peer)
3726 struct MeshTunnelChildInfo *cinfo;
3728 if (NULL == t->children_fc)
3731 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3737 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3738 cinfo->id = GNUNET_PEER_intern (peer);
3739 cinfo->skip = t->fwd_pid;
3742 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3743 cinfo->fwd_ack = t->fwd_pid + delta;
3744 cinfo->bck_ack = delta;
3745 cinfo->bck_pid = -1;
3747 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3748 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3750 cinfo->send_buffer =
3751 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3753 GNUNET_assert (GNUNET_OK ==
3754 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3757 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3764 * Get the Flow Control info of a client.
3766 * @param t Tunnel on which to look.
3767 * @param c Client whose ACK to get.
3769 * @return ACK value.
3771 static struct MeshTunnelClientInfo *
3772 tunnel_get_client_fc (struct MeshTunnel *t,
3773 struct MeshClient *c)
3777 for (i = 0; i < t->nclients; i++)
3779 if (t->clients[i] != c)
3781 return &t->clients_fc[i];
3784 return NULL; // avoid compiler / coverity complaints
3789 * Iterator to get the appropiate ACK value from all children nodes.
3791 * @param cls Closue (tunnel).
3792 * @param id Id of the child node.
3795 tunnel_get_child_fwd_ack (void *cls,
3798 struct GNUNET_PeerIdentity peer_id;
3799 struct MeshTunnelChildInfo *cinfo;
3800 struct MeshTunnelChildIteratorContext *ctx = cls;
3801 struct MeshTunnel *t = ctx->t;
3804 GNUNET_PEER_resolve (id, &peer_id);
3805 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3806 ack = cinfo->fwd_ack;
3809 if (GNUNET_NO == ctx->init)
3811 ctx->max_child_ack = ack;
3812 ctx->init = GNUNET_YES;
3815 if (GNUNET_YES == t->speed_min)
3817 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3821 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3828 * Get the maximum PID allowed to transmit to any
3829 * tunnel child of the local peer, depending on the tunnel
3830 * buffering/speed settings.
3834 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3837 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3839 struct MeshTunnelChildIteratorContext ctx;
3841 ctx.max_child_ack = 0;
3843 ctx.init = GNUNET_NO;
3844 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3846 if (0 == ctx.nchildren)
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3849 " tunnel has no children, no FWD ACK\n");
3853 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3854 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3856 return (int64_t) ctx.max_child_ack;
3861 * Set the FWD ACK value of a client in a particular tunnel.
3863 * @param t Tunnel affected.
3864 * @param c Client whose ACK to set.
3865 * @param ack ACK value.
3868 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3869 struct MeshClient *c,
3874 for (i = 0; i < t->nclients; i++)
3876 if (t->clients[i] != c)
3878 t->clients_fc[i].fwd_ack = ack;
3886 * Get the highest ACK value of all clients in a particular tunnel,
3887 * according to the buffering/speed settings.
3889 * @param t Tunnel on which to look.
3891 * @return Corresponding ACK value (max uint32_t).
3892 * If no clients are suscribed, -1LL.
3895 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3900 if (0 == t->nclients)
3902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3903 " tunnel has no clients, no FWD ACK\n");
3907 for (ack = -1LL, i = 0; i < t->nclients; i++)
3910 (GNUNET_YES == t->speed_min &&
3911 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3912 (GNUNET_NO == t->speed_min &&
3913 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3915 ack = t->clients_fc[i].fwd_ack;
3919 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3920 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3922 return (uint32_t) ack;
3927 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3928 * mode and the status of all children nodes.
3932 * @return Maximum PID allowed.
3935 tunnel_get_fwd_ack (struct MeshTunnel *t)
3939 uint32_t buffer_free;
3943 count = t->fwd_pid - t->skip;
3944 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3945 child_ack = tunnel_get_children_fwd_ack (t);
3946 client_ack = tunnel_get_clients_fwd_ack (t);
3947 if (GNUNET_YES == t->nobuffer)
3950 if (-1LL == child_ack)
3951 child_ack = client_ack;
3952 if (-1LL == child_ack)
3955 client_ack = child_ack = ack;
3960 ack = count + buffer_free; // Overflow? OK!
3962 if (-1LL == child_ack)
3964 // Node has no children, child_ack AND core buffer are irrelevant.
3965 if (-1LL == client_ack) // No children AND no clients? Not good!
3967 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3971 return (uint32_t) client_ack;
3973 if (-1LL == client_ack)
3977 if (GNUNET_YES == t->speed_min)
3979 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3980 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3984 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3985 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3988 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3989 count, buffer_free, child_ack, client_ack, ack);
3995 * Build a local ACK message and send it to a local client.
3997 * @param t Tunnel on which to send the ACK.
3998 * @param c Client to whom send the ACK.
3999 * @param ack Value of the ACK.
4002 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
4004 struct GNUNET_MESH_LocalAck msg;
4006 msg.header.size = htons (sizeof (msg));
4007 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4008 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
4009 msg.max_pid = htonl (ack);
4010 GNUNET_SERVER_notification_context_unicast(nc,
4017 * Build an ACK message and queue it to send to the given peer.
4019 * @param t Tunnel on which to send the ACK.
4020 * @param peer Peer to whom send the ACK.
4021 * @param ack Value of the ACK.
4024 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
4026 struct GNUNET_MESH_ACK msg;
4028 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4029 msg.header.size = htons (sizeof (msg));
4030 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
4031 msg.pid = htonl (ack);
4032 msg.tid = htonl (t->id.tid);
4034 send_prebuilt_message (&msg.header, peer, t);
4039 * Notify a the owner of a tunnel about how many more
4040 * payload packages will we accept on a given tunnel.
4042 * @param t Tunnel on which to send the ACK.
4045 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
4049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4050 "Sending client FWD ACK on tunnel %X\n",
4053 ack = tunnel_get_fwd_ack (t);
4055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4056 if (t->last_fwd_ack == ack)
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
4063 t->last_fwd_ack = ack;
4064 send_local_ack (t, t->owner, ack);
4069 * Send an ACK informing the predecessor about the available buffer space.
4070 * In case there is no predecessor, inform the owning client.
4071 * If buffering is off, send only on behalf of children or self if endpoint.
4072 * If buffering is on, send when sent to children and buffer space is free.
4073 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4074 * the ACK itself goes "back" (towards root).
4076 * @param t Tunnel on which to send the ACK.
4077 * @param type Type of message that triggered the ACK transmission.
4080 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4082 struct GNUNET_PeerIdentity id;
4085 if (NULL != t->owner)
4087 tunnel_send_client_fwd_ack (t);
4090 /* Is it after unicast / multicast retransmission? */
4093 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4094 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4096 "ACK due to FWD DATA retransmission\n");
4097 if (GNUNET_YES == t->nobuffer)
4099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4103 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4104 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4106 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4107 t->force_ack = GNUNET_YES;
4113 /* Check if we need to transmit the ACK */
4114 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4115 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4116 GNUNET_NO == t->force_ack)
4118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4120 " t->qmax: %u, t->qn: %u\n",
4121 t->fwd_queue_max, t->fwd_queue_n);
4122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4123 " t->pid: %u, t->ack: %u\n",
4124 t->fwd_pid, t->last_fwd_ack);
4128 /* Ok, ACK might be necessary, what PID to ACK? */
4129 ack = tunnel_get_fwd_ack (t);
4131 /* If speed_min and not all children have ack'd, dont send yet */
4132 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4138 t->last_fwd_ack = ack;
4139 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4140 send_ack (t, &id, ack);
4142 t->force_ack = GNUNET_NO;
4147 * Iterator to send a child node a BCK ACK to allow him to send more
4150 * @param cls Closure (tunnel).
4151 * @param id Id of the child node.
4154 tunnel_send_child_bck_ack (void *cls,
4157 struct MeshTunnel *t = cls;
4158 struct MeshTunnelChildInfo *cinfo;
4159 struct GNUNET_PeerIdentity peer;
4162 GNUNET_PEER_resolve (id, &peer);
4163 cinfo = tunnel_get_neighbor_fc (t, &peer);
4164 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4166 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4169 " Not sending ACK, not needed\n");
4172 cinfo->bck_ack = ack;
4174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4175 " Sending BCK ACK %u (last sent: %u)\n",
4176 ack, cinfo->bck_ack);
4177 send_ack (t, &peer, ack);
4182 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4184 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4186 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4187 * - implement a size 1 buffer in each client_fc AND children_fc
4188 * to hold at least 1 message per "child".
4189 * problem: violates no buffer policy
4190 * - ack 0 and make "children" poll for transmission slots
4191 * problem: big overhead, extra latency even in low traffic
4194 * @param t Tunnel on which to send the BCK ACKs.
4197 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4200 unsigned int tunnel_delta;
4202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4204 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4207 /* Find client whom to allow to send to origin (with lowest buffer space) */
4208 for (i = 0; i < t->nclients; i++)
4210 struct MeshTunnelClientInfo *clinfo;
4213 clinfo = &t->clients_fc[i];
4214 delta = clinfo->bck_ack - clinfo->bck_pid;
4215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4216 t->clients[i]->id, delta);
4218 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4219 (GNUNET_YES == t->nobuffer && 0 == delta))
4223 ack = clinfo->bck_pid;
4224 ack += t->nobuffer ? 1 : tunnel_delta;
4225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4226 " sending ack to client %u: %u\n",
4227 t->clients[i]->id, ack);
4228 send_local_ack (t, t->clients[i], ack);
4229 clinfo->bck_ack = ack;
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4234 " not sending ack to client %u (td %u, d %u)\n",
4235 t->clients[i]->id, tunnel_delta, delta);
4242 * Send an ACK informing the children nodes and destination clients about
4243 * the available buffer space.
4244 * If buffering is off, send only on behalf of root (can be self).
4245 * If buffering is on, send when sent to predecessor and buffer space is free.
4246 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4247 * the ACK itself goes "forward" (towards children/clients).
4249 * @param t Tunnel on which to send the ACK.
4250 * @param type Type of message that triggered the ACK transmission.
4253 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4256 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4257 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4258 /* Is it after data to_origin retransmission? */
4261 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4262 if (GNUNET_YES == t->nobuffer)
4264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4265 " Not sending ACK, nobuffer\n");
4269 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4270 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4272 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4273 t->force_ack = GNUNET_YES;
4279 tunnel_send_clients_bck_ack (t);
4280 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4281 t->force_ack = GNUNET_NO;
4286 * @brief Re-initiate traffic to this peer if necessary.
4288 * Check if there is traffic queued towards this peer
4289 * and the core transmit handle is NULL (traffic was stalled).
4290 * If so, call core tmt rdy.
4292 * @param cls Closure (unused)
4293 * @param peer_id Short ID of peer to which initiate traffic.
4296 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4298 struct MeshPeerInfo *peer;
4299 struct GNUNET_PeerIdentity id;
4300 struct MeshPeerQueue *q;
4303 peer = peer_info_get_short(peer_id);
4304 if (NULL != peer->core_transmit)
4307 q = queue_get_next(peer);
4310 /* Might br multicast traffic already sent to this particular peer but
4311 * not to other children in this tunnel.
4312 * This way t->queue_n would be > 0 but the queue of this particular peer
4318 GNUNET_PEER_resolve (peer->id, &id);
4319 peer->core_transmit =
4320 GNUNET_CORE_notify_transmit_ready(core_handle,
4323 GNUNET_TIME_UNIT_FOREVER_REL,
4333 * @brief Allow transmission of FWD traffic on this tunnel
4335 * Check if there is traffic queued towards any children
4336 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4338 * @param t Tunnel on which to unlock FWD traffic.
4341 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4343 if (0 == t->fwd_queue_n)
4346 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4351 * @brief Allow transmission of BCK traffic on this tunnel
4353 * Check if there is traffic queued towards the root of the tree
4354 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4356 * @param t Tunnel on which to unlock BCK traffic.
4359 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4361 if (0 == t->bck_queue_n)
4364 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4369 * Send a message to all peers in this tunnel that the tunnel is no longer
4372 * @param t The tunnel whose peers to notify.
4375 tunnel_send_destroy (struct MeshTunnel *t)
4377 struct GNUNET_MESH_TunnelDestroy msg;
4379 msg.header.size = htons (sizeof (msg));
4380 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4381 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4382 msg.tid = htonl (t->id.tid);
4383 tunnel_send_multicast (t, &msg.header);
4388 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4390 * @param cls Closure (Tunnel which to cancel).
4391 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4394 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4396 struct MeshTunnel *t = cls;
4397 struct MeshPeerInfo *peer_info;
4398 struct MeshPeerQueue *pq;
4399 struct MeshPeerQueue *next;
4401 peer_info = peer_info_get_short (neighbor_id);
4402 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4405 if (pq->tunnel == t)
4407 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4408 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4409 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4411 // Should have been removed on destroy children
4414 queue_destroy (pq, GNUNET_YES);
4417 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4419 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4420 peer_info->core_transmit = NULL;
4425 * Destroy the tunnel and free any allocated resources linked to it.
4427 * @param t the tunnel to destroy
4429 * @return GNUNET_OK on success
4432 tunnel_destroy (struct MeshTunnel *t)
4434 struct MeshClient *c;
4435 struct GNUNET_HashCode hash;
4446 struct GNUNET_PeerIdentity id;
4448 GNUNET_PEER_resolve (t->id.oid, &id);
4449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4450 GNUNET_i2s (&id), t->id.tid);
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4456 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4457 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4465 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4467 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4474 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4475 for (i = 0; i < t->nclients; i++)
4479 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4485 for (i = 0; i < t->nignore; i++)
4489 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4496 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4497 GNUNET_free_non_null (t->clients);
4498 GNUNET_free_non_null (t->clients_fc);
4500 if (NULL != t->peers)
4502 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4504 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4507 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4508 &tunnel_destroy_child,
4510 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4511 t->children_fc = NULL;
4513 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4514 tree_destroy (t->tree);
4516 if (NULL != t->regex_ctx)
4517 regex_cancel_search (t->regex_ctx);
4518 if (NULL != t->dht_get_type)
4519 GNUNET_DHT_get_stop (t->dht_get_type);
4520 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4521 GNUNET_SCHEDULER_cancel (t->timeout_task);
4522 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4523 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4526 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4531 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4534 * Tunnel is empty: destroy it.
4536 * @param cls Closure (Tunnel).
4537 * @param tc TaskContext.
4540 tunnel_destroy_empty_delayed (void *cls,
4541 const struct GNUNET_SCHEDULER_TaskContext *tc)
4543 struct MeshTunnel *t = cls;
4545 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4546 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4549 if (0 != t->nclients ||
4550 0 != tree_count_children (t->tree))
4555 struct GNUNET_PeerIdentity id;
4557 GNUNET_PEER_resolve (t->id.oid, &id);
4558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4559 "executing destruction of empty tunnel %s [%X]\n",
4560 GNUNET_i2s (&id), t->id.tid);
4569 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4571 * @param t Tunnel to destroy if empty.
4574 tunnel_destroy_empty (struct MeshTunnel *t)
4576 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4578 0 != tree_count_children (t->tree))
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4582 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4588 struct GNUNET_PeerIdentity id;
4590 GNUNET_PEER_resolve (t->id.oid, &id);
4591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4592 "scheduling destruction of empty tunnel %s [%X]\n",
4593 GNUNET_i2s (&id), t->id.tid);
4597 t->delayed_destroy =
4598 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4599 &tunnel_destroy_empty_delayed,
4605 * Create a new tunnel
4607 * @param owner Who is the owner of the tunnel (short ID).
4608 * @param tid Tunnel Number of the tunnel.
4609 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4610 * @param local Tunnel Number for the tunnel, for the client point of view.
4612 * @return A new initialized tunnel. NULL on error.
4614 static struct MeshTunnel *
4615 tunnel_new (GNUNET_PEER_Id owner,
4616 MESH_TunnelNumber tid,
4617 struct MeshClient *client,
4618 MESH_TunnelNumber local)
4620 struct MeshTunnel *t;
4621 struct GNUNET_HashCode hash;
4623 if (n_tunnels >= max_tunnels && NULL == client)
4626 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4629 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4630 t->bck_queue_max = t->fwd_queue_max;
4631 t->tree = tree_new (owner);
4633 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4634 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4635 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4636 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4637 t->local_tid = local;
4638 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4640 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4642 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4644 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4645 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4652 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4659 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4661 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4662 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4666 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4676 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4677 * that are no longer needed, as well as nodes of no longer reachable peers.
4678 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4680 * @param t Tunnel from which to remove the path.
4681 * @param peer Short id of the peer which should be removed.
4684 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4688 r = tree_del_peer (t->tree, peer, NULL, NULL);
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4692 "Tunnel %u [%u] has no more nodes\n",
4693 t->id.oid, t->id.tid);
4699 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4700 * client when the client disconnects. If the client is not the owner, the
4701 * owner will get notified if no more clients are in the tunnel and the client
4702 * get removed from the tunnel's list.
4704 * @param cls closure (client that is disconnecting)
4705 * @param key the hash of the local tunnel id (used to access the hashmap)
4706 * @param value the value stored at the key (tunnel to destroy)
4708 * @return GNUNET_OK, keep iterating.
4711 tunnel_destroy_iterator (void *cls,
4712 const struct GNUNET_HashCode * key,
4715 struct MeshTunnel *t = value;
4716 struct MeshClient *c = cls;
4718 send_client_tunnel_disconnect (t, c);
4721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4722 tunnel_delete_client (t, c);
4723 client_delete_tunnel (c, t);
4724 tunnel_destroy_empty (t);
4727 tunnel_send_destroy (t);
4729 t->destroy = GNUNET_YES;
4736 * Timeout function, destroys tunnel if called
4738 * @param cls Closure (tunnel to destroy).
4739 * @param tc TaskContext
4742 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4744 struct MeshTunnel *t = cls;
4745 struct GNUNET_PeerIdentity id;
4747 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4748 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4750 GNUNET_PEER_resolve(t->id.oid, &id);
4751 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4752 "Tunnel %s [%X] timed out. Destroying.\n",
4753 GNUNET_i2s(&id), t->id.tid);
4754 send_clients_tunnel_destroy (t);
4759 * Resets the tunnel timeout. Starts it if no timeout was running.
4761 * @param t Tunnel whose timeout to reset.
4763 * TODO use heap to improve efficiency of scheduler.
4766 tunnel_reset_timeout (struct MeshTunnel *t)
4768 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4769 GNUNET_SCHEDULER_cancel (t->timeout_task);
4771 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4772 (refresh_path_time, 4), &tunnel_timeout, t);
4776 /******************************************************************************/
4777 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4778 /******************************************************************************/
4781 * Function to send a create path packet to a peer.
4783 * @param cls closure
4784 * @param size number of bytes available in buf
4785 * @param buf where the callee should write the message
4786 * @return number of bytes written to buf
4789 send_core_path_create (void *cls, size_t size, void *buf)
4791 struct MeshPathInfo *info = cls;
4792 struct GNUNET_MESH_ManipulatePath *msg;
4793 struct GNUNET_PeerIdentity *peer_ptr;
4794 struct MeshTunnel *t = info->t;
4795 struct MeshPeerPath *p = info->path;
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4802 sizeof (struct GNUNET_MESH_ManipulatePath) +
4803 p->length * sizeof (struct GNUNET_PeerIdentity);
4805 if (size < size_needed || NULL == buf)
4810 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4811 msg->header.size = htons (size_needed);
4812 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4813 msg->tid = ntohl (t->id.tid);
4816 if (GNUNET_YES == t->speed_min)
4817 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4818 if (GNUNET_YES == t->nobuffer)
4819 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4820 msg->opt = htonl(opt);
4823 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4824 for (i = 0; i < p->length; i++)
4826 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4833 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4839 * Fill the core buffer
4841 * @param cls closure (data itself)
4842 * @param size number of bytes available in buf
4843 * @param buf where the callee should write the message
4845 * @return number of bytes written to buf
4848 send_core_data_multicast (void *cls, size_t size, void *buf)
4850 struct MeshTransmissionDescriptor *info = cls;
4853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4854 GNUNET_assert (NULL != info);
4855 GNUNET_assert (NULL != info->peer);
4856 total_size = info->mesh_data->data_len;
4857 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4859 if (total_size > size)
4864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4865 memcpy (buf, info->mesh_data->data, total_size);
4868 struct GNUNET_MESH_Multicast *mc;
4869 struct GNUNET_MessageHeader *mh;
4872 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4874 mc = (struct GNUNET_MESH_Multicast *) mh;
4875 mh = (struct GNUNET_MessageHeader *) &mc[1];
4876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4877 " multicast, payload type %s\n",
4878 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4880 " multicast, payload size %u\n", ntohs (mh->size));
4884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4885 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4889 data_descriptor_decrement_rc (info->mesh_data);
4890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4898 * Creates a path ack message in buf and frees all unused resources.
4900 * @param cls closure (MeshTransmissionDescriptor)
4901 * @param size number of bytes available in buf
4902 * @param buf where the callee should write the message
4903 * @return number of bytes written to buf
4906 send_core_path_ack (void *cls, size_t size, void *buf)
4908 struct MeshTransmissionDescriptor *info = cls;
4909 struct GNUNET_MESH_PathACK *msg = buf;
4911 GNUNET_assert (NULL != info);
4912 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4917 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4918 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4919 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4920 msg->tid = htonl (info->origin->tid);
4921 msg->peer_id = my_full_id;
4924 /* TODO add signature */
4926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4927 return sizeof (struct GNUNET_MESH_PathACK);
4932 * Free a transmission that was already queued with all resources
4933 * associated to the request.
4935 * @param queue Queue handler to cancel.
4936 * @param clear_cls Is it necessary to free associated cls?
4939 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4941 struct MeshTransmissionDescriptor *dd;
4942 struct MeshPathInfo *path_info;
4943 struct MeshTunnelChildInfo *cinfo;
4944 struct GNUNET_PeerIdentity id;
4948 if (GNUNET_YES == clear_cls)
4950 switch (queue->type)
4952 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4953 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4954 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4956 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4957 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4958 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4959 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4960 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4961 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4963 " prebuilt message\n");
4964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4966 GNUNET_MESH_DEBUG_M2S(queue->type));
4968 data_descriptor_decrement_rc (dd->mesh_data);
4970 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4972 path_info = queue->cls;
4973 path_destroy (path_info->path);
4977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4978 " type %s unknown!\n",
4979 GNUNET_MESH_DEBUG_M2S(queue->type));
4981 GNUNET_free_non_null (queue->cls);
4983 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4984 queue->peer->queue_tail,
4987 /* Delete from child_fc in the appropiate tunnel */
4988 max = queue->tunnel->fwd_queue_max;
4989 GNUNET_PEER_resolve (queue->peer->id, &id);
4990 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4993 for (i = 0; i < cinfo->send_buffer_n; i++)
4996 i2 = (cinfo->send_buffer_start + i) % max;
4997 if (cinfo->send_buffer[i2] == queue)
4999 /* Found corresponding entry in the send_buffer. Move all others back. */
5004 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
5006 j2 = (cinfo->send_buffer_start + j) % max;
5007 j3 = (cinfo->send_buffer_start + j + 1) % max;
5008 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
5011 cinfo->send_buffer[j3] = NULL;
5012 cinfo->send_buffer_n--;
5017 GNUNET_free (queue);
5022 * @brief Get the next transmittable message from the queue.
5024 * This will be the head, except in the case of being a data packet
5025 * not allowed by the destination peer.
5027 * @param peer Destination peer.
5029 * @return The next viable MeshPeerQueue element to send to that peer.
5030 * NULL when there are no transmittable messages.
5032 struct MeshPeerQueue *
5033 queue_get_next (const struct MeshPeerInfo *peer)
5035 struct MeshPeerQueue *q;
5036 struct MeshTunnel *t;
5037 struct MeshTransmissionDescriptor *info;
5038 struct MeshTunnelChildInfo *cinfo;
5039 struct GNUNET_MESH_Unicast *ucast;
5040 struct GNUNET_MESH_ToOrigin *to_orig;
5041 struct GNUNET_MESH_Multicast *mcast;
5042 struct GNUNET_PeerIdentity id;
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
5047 for (q = peer->queue_head; NULL != q; q = q->next)
5051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5053 GNUNET_MESH_DEBUG_M2S(q->type));
5056 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5057 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
5058 pid = ntohl (ucast->pid);
5059 GNUNET_PEER_resolve (info->peer->id, &id);
5060 cinfo = tunnel_get_neighbor_fc(t, &id);
5061 ack = cinfo->fwd_ack;
5063 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5064 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
5065 pid = ntohl (to_orig->pid);
5068 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5069 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
5070 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
5072 // Not a multicast payload: multicast control traffic (destroy, etc)
5075 pid = ntohl (mcast->pid);
5076 GNUNET_PEER_resolve (info->peer->id, &id);
5077 cinfo = tunnel_get_neighbor_fc(t, &id);
5078 ack = cinfo->fwd_ack;
5081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5086 "********* ACK: %u, PID: %u\n",
5088 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5097 "********* NEXT!\n");
5100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5101 "********* nothing found\n");
5107 * Core callback to write a queued packet to core buffer
5109 * @param cls Closure (peer info).
5110 * @param size Number of bytes available in buf.
5111 * @param buf Where the to write the message.
5113 * @return number of bytes written to buf
5116 queue_send (void *cls, size_t size, void *buf)
5118 struct MeshPeerInfo *peer = cls;
5119 struct GNUNET_MessageHeader *msg;
5120 struct MeshPeerQueue *queue;
5121 struct MeshTunnel *t;
5122 struct MeshTunnelChildInfo *cinfo;
5123 struct GNUNET_PeerIdentity dst_id;
5126 peer->core_transmit = NULL;
5129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5130 queue = queue_get_next (peer);
5132 /* Queue has no internal mesh traffic nor sendable payload */
5135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5136 if (NULL == peer->queue_head)
5137 GNUNET_break (0); // Should've been canceled
5140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5142 GNUNET_PEER_resolve (peer->id, &dst_id);
5143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5144 "********* towards %s\n",
5145 GNUNET_i2s(&dst_id));
5146 /* Check if buffer size is enough for the message */
5147 if (queue->size > size)
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5150 "********* not enough room, reissue\n");
5151 peer->core_transmit =
5152 GNUNET_CORE_notify_transmit_ready (core_handle,
5155 GNUNET_TIME_UNIT_FOREVER_REL,
5162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5165 GNUNET_assert (0 < t->pending_messages);
5166 t->pending_messages--;
5167 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5171 "********* unicast: t->q (%u/%u)\n",
5172 t->fwd_queue_n, t->fwd_queue_max);
5174 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5181 switch (queue->type)
5184 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5185 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5186 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5187 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5188 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5190 "********* raw: %s\n",
5191 GNUNET_MESH_DEBUG_M2S (queue->type));
5193 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5194 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5195 data_size = send_core_data_raw (queue->cls, size, buf);
5196 msg = (struct GNUNET_MessageHeader *) buf;
5197 switch (ntohs (msg->type)) // Type of preconstructed message
5199 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5200 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5202 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5203 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5209 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5212 struct MeshTransmissionDescriptor *info = queue->cls;
5214 if ((1 == info->mesh_data->reference_counter
5215 && GNUNET_YES == t->speed_min)
5217 (info->mesh_data->total_out == info->mesh_data->reference_counter
5218 && GNUNET_NO == t->speed_min))
5220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5221 "********* considered sent\n");
5226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5227 "********* NOT considered sent yet\n");
5228 t->pending_messages++;
5231 data_size = send_core_data_multicast(queue->cls, size, buf);
5232 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5234 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5236 data_size = send_core_path_create (queue->cls, size, buf);
5238 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5240 data_size = send_core_path_ack (queue->cls, size, buf);
5242 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5244 data_size = send_core_data_multicast (queue->cls, size, buf);
5248 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5249 "********* type unknown: %u\n",
5253 switch (queue->type)
5255 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5256 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5257 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5258 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5259 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5263 "at pos %u (%p) != %p\n",
5264 cinfo->send_buffer_start,
5265 cinfo->send_buffer[cinfo->send_buffer_start],
5268 if (cinfo->send_buffer_n > 0)
5270 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5271 cinfo->send_buffer_n--;
5272 cinfo->send_buffer_start++;
5273 cinfo->send_buffer_start %= t->fwd_queue_max;
5284 /* Free queue, but cls was freed by send_core_* */
5285 queue_destroy (queue, GNUNET_NO);
5287 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5293 /* If more data in queue, send next */
5294 queue = queue_get_next(peer);
5297 struct GNUNET_PeerIdentity id;
5299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5300 GNUNET_PEER_resolve (peer->id, &id);
5301 peer->core_transmit =
5302 GNUNET_CORE_notify_transmit_ready(core_handle,
5305 GNUNET_TIME_UNIT_FOREVER_REL,
5313 if (NULL != peer->queue_head)
5315 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5316 "********* %s stalled\n",
5317 GNUNET_i2s(&my_full_id));
5319 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5320 // FIXME unify bck/fwd structures, bck does not have cinfo right now
5321 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5323 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
5324 &tunnel_poll, cinfo);
5328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5334 * @brief Queue and pass message to core when possible.
5336 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5337 * and accounts for it. In case the queue is full, the message is dropped and
5340 * Otherwise, message is treated as internal and allowed to go regardless of
5343 * @param cls Closure (@c type dependant). It will be used by queue_send to
5344 * build the message to be sent if not already prebuilt.
5345 * @param type Type of the message, 0 for a raw message.
5346 * @param size Size of the message.
5347 * @param dst Neighbor to send message to.
5348 * @param t Tunnel this message belongs to.
5351 queue_add (void *cls, uint16_t type, size_t size,
5352 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5354 struct MeshPeerQueue *queue;
5355 struct MeshTunnelChildInfo *cinfo;
5356 struct GNUNET_PeerIdentity id;
5362 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5363 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5365 n = &t->fwd_queue_n;
5366 max = &t->fwd_queue_max;
5368 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5370 n = &t->bck_queue_n;
5371 max = &t->bck_queue_max;
5378 GNUNET_STATISTICS_update(stats,
5379 "# messages dropped (buffer full)",
5381 return; // Drop message
5385 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5391 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5392 GNUNET_PEER_resolve (dst->id, &id);
5393 if (NULL == dst->core_transmit)
5395 dst->core_transmit =
5396 GNUNET_CORE_notify_transmit_ready (core_handle,
5399 GNUNET_TIME_UNIT_FOREVER_REL,
5405 t->pending_messages++;
5406 if (NULL == n) // Is this internal mesh traffic?
5409 // It's payload, keep track of buffer per peer.
5410 cinfo = tunnel_get_neighbor_fc(t, &id);
5411 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5412 if (NULL != cinfo->send_buffer[i])
5414 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5415 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5416 cinfo->send_buffer_start++;
5417 cinfo->send_buffer_start %= t->fwd_queue_max;
5421 cinfo->send_buffer_n++;
5423 cinfo->send_buffer[i] = queue;
5424 if (cinfo->send_buffer_n > t->fwd_queue_max)
5427 cinfo->send_buffer_n = t->fwd_queue_max;
5432 /******************************************************************************/
5433 /******************** MESH NETWORK HANDLERS **************************/
5434 /******************************************************************************/
5438 * Core handler for path creation
5440 * @param cls closure
5441 * @param message message
5442 * @param peer peer identity this notification is about
5443 * @param atsi performance data
5444 * @param atsi_count number of records in 'atsi'
5446 * @return GNUNET_OK to keep the connection open,
5447 * GNUNET_SYSERR to close it (signal serious error)
5450 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5451 const struct GNUNET_MessageHeader *message,
5452 const struct GNUNET_ATS_Information *atsi,
5453 unsigned int atsi_count)
5455 unsigned int own_pos;
5458 MESH_TunnelNumber tid;
5459 struct GNUNET_MESH_ManipulatePath *msg;
5460 struct GNUNET_PeerIdentity *pi;
5461 struct GNUNET_HashCode hash;
5462 struct MeshPeerPath *path;
5463 struct MeshPeerInfo *dest_peer_info;
5464 struct MeshPeerInfo *orig_peer_info;
5465 struct MeshTunnel *t;
5467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5468 "Received a path create msg [%s]\n",
5469 GNUNET_i2s (&my_full_id));
5470 size = ntohs (message->size);
5471 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5473 GNUNET_break_op (0);
5477 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5478 if (size % sizeof (struct GNUNET_PeerIdentity))
5480 GNUNET_break_op (0);
5483 size /= sizeof (struct GNUNET_PeerIdentity);
5486 GNUNET_break_op (0);
5489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5490 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5492 tid = ntohl (msg->tid);
5493 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5495 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5496 t = tunnel_get (pi, tid);
5497 if (NULL == t) // FIXME only for INCOMING tunnels?
5501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5502 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5505 // FIXME notify failure
5508 opt = ntohl (msg->opt);
5509 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5510 GNUNET_YES : GNUNET_NO;
5511 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5513 t->nobuffer = GNUNET_YES;
5514 t->last_fwd_ack = t->fwd_pid + 1;
5516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5517 " speed_min: %d, nobuffer:%d\n",
5518 t->speed_min, t->nobuffer);
5520 if (GNUNET_YES == t->nobuffer)
5522 t->bck_queue_max = 1;
5523 t->fwd_queue_max = 1;
5526 // FIXME only assign a local tid if a local client is interested (on demand)
5527 while (NULL != tunnel_get_incoming (next_local_tid))
5528 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5529 t->local_tid_dest = next_local_tid++;
5530 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5533 tunnel_reset_timeout (t);
5534 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5536 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5537 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5545 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5546 if (NULL == dest_peer_info)
5548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5549 " Creating PeerInfo for destination.\n");
5550 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5551 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5552 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5554 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5556 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5557 if (NULL == orig_peer_info)
5559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5560 " Creating PeerInfo for origin.\n");
5561 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5562 orig_peer_info->id = GNUNET_PEER_intern (pi);
5563 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5564 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5567 path = path_new (size);
5569 for (i = 0; i < size; i++)
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5572 GNUNET_i2s (&pi[i]));
5573 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5574 if (path->peers[i] == myid)
5577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5580 /* cannot be self, must be 'not found' */
5581 /* create path: self not found in path through self */
5582 GNUNET_break_op (0);
5583 path_destroy (path);
5587 path_add_to_peers (path, GNUNET_NO);
5588 tunnel_add_path (t, path, own_pos);
5589 if (own_pos == size - 1)
5591 /* It is for us! Send ack. */
5592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5593 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5594 if (NULL == t->peers)
5596 /* New tunnel! Notify clients on first payload message. */
5597 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5599 GNUNET_break (GNUNET_SYSERR !=
5600 GNUNET_CONTAINER_multihashmap_put (t->peers,
5601 &my_full_id.hashPubKey,
5604 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5609 struct MeshPeerPath *path2;
5611 /* It's for somebody else! Retransmit. */
5612 path2 = path_duplicate (path);
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5614 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5615 path2 = path_duplicate (path);
5616 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5617 send_create_path (dest_peer_info, path, t);
5624 * Core handler for path destruction
5626 * @param cls closure
5627 * @param message message
5628 * @param peer peer identity this notification is about
5629 * @param atsi performance data
5630 * @param atsi_count number of records in 'atsi'
5632 * @return GNUNET_OK to keep the connection open,
5633 * GNUNET_SYSERR to close it (signal serious error)
5636 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5637 const struct GNUNET_MessageHeader *message,
5638 const struct GNUNET_ATS_Information *atsi,
5639 unsigned int atsi_count)
5641 struct GNUNET_MESH_ManipulatePath *msg;
5642 struct GNUNET_PeerIdentity *pi;
5643 struct MeshPeerPath *path;
5644 struct MeshTunnel *t;
5645 unsigned int own_pos;
5649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5650 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5651 size = ntohs (message->size);
5652 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5654 GNUNET_break_op (0);
5658 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5659 if (size % sizeof (struct GNUNET_PeerIdentity))
5661 GNUNET_break_op (0);
5664 size /= sizeof (struct GNUNET_PeerIdentity);
5667 GNUNET_break_op (0);
5670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5672 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5673 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5675 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5677 t = tunnel_get (pi, ntohl (msg->tid));
5680 /* TODO notify back: we don't know this tunnel */
5681 GNUNET_break_op (0);
5684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5685 path = path_new (size);
5687 for (i = 0; i < size; i++)
5689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5690 GNUNET_i2s (&pi[i]));
5691 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5692 if (path->peers[i] == myid)
5695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5696 if (own_pos < path->length - 1)
5697 send_prebuilt_message (message, &pi[own_pos + 1], t);
5699 send_client_tunnel_disconnect(t, NULL);
5701 tunnel_delete_peer (t, path->peers[path->length - 1]);
5702 path_destroy (path);
5708 * Core handler for notifications of broken paths
5710 * @param cls closure
5711 * @param message message
5712 * @param peer peer identity this notification is about
5713 * @param atsi performance data
5714 * @param atsi_count number of records in 'atsi'
5716 * @return GNUNET_OK to keep the connection open,
5717 * GNUNET_SYSERR to close it (signal serious error)
5720 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5721 const struct GNUNET_MessageHeader *message,
5722 const struct GNUNET_ATS_Information *atsi,
5723 unsigned int atsi_count)
5725 struct GNUNET_MESH_PathBroken *msg;
5726 struct MeshTunnel *t;
5728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5729 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5730 msg = (struct GNUNET_MESH_PathBroken *) message;
5731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5732 GNUNET_i2s (&msg->peer1));
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5734 GNUNET_i2s (&msg->peer2));
5735 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5738 GNUNET_break_op (0);
5741 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5742 GNUNET_PEER_search (&msg->peer2));
5749 * Core handler for tunnel destruction
5751 * @param cls closure
5752 * @param message message
5753 * @param peer peer identity this notification is about
5754 * @param atsi performance data
5755 * @param atsi_count number of records in 'atsi'
5757 * @return GNUNET_OK to keep the connection open,
5758 * GNUNET_SYSERR to close it (signal serious error)
5761 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5762 const struct GNUNET_MessageHeader *message,
5763 const struct GNUNET_ATS_Information *atsi,
5764 unsigned int atsi_count)
5766 struct GNUNET_MESH_TunnelDestroy *msg;
5767 struct MeshTunnel *t;
5769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5770 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5771 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5773 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5774 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5777 /* Probably already got the message from another path,
5778 * destroyed the tunnel and retransmitted to children.
5781 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5784 if (t->id.oid == myid)
5786 GNUNET_break_op (0);
5789 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5791 /* Tunnel was incoming, notify clients */
5792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5793 t->local_tid, t->local_tid_dest);
5794 send_clients_tunnel_destroy (t);
5796 tunnel_send_destroy (t);
5797 t->destroy = GNUNET_YES;
5798 // TODO: add timeout to destroy the tunnel anyway
5804 * Core handler for mesh network traffic going from the origin to a peer
5806 * @param cls closure
5807 * @param peer peer identity this notification is about
5808 * @param message message
5809 * @param atsi performance data
5810 * @param atsi_count number of records in 'atsi'
5811 * @return GNUNET_OK to keep the connection open,
5812 * GNUNET_SYSERR to close it (signal serious error)
5815 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5816 const struct GNUNET_MessageHeader *message,
5817 const struct GNUNET_ATS_Information *atsi,
5818 unsigned int atsi_count)
5820 struct GNUNET_MESH_Unicast *msg;
5821 struct GNUNET_PeerIdentity *neighbor;
5822 struct MeshTunnelChildInfo *cinfo;
5823 struct MeshTunnel *t;
5824 GNUNET_PEER_Id dest_id;
5829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5832 size = ntohs (message->size);
5834 sizeof (struct GNUNET_MESH_Unicast) +
5835 sizeof (struct GNUNET_MessageHeader))
5840 msg = (struct GNUNET_MESH_Unicast *) message;
5841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5842 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5844 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5847 /* TODO notify back: we don't know this tunnel */
5848 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5849 GNUNET_break_op (0);
5852 pid = ntohl (msg->pid);
5853 if (t->fwd_pid == pid)
5855 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5856 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5857 " Already seen pid %u, DROPPING!\n", pid);
5862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5863 " pid %u not seen yet, forwarding\n", pid);
5866 t->skip += (pid - t->fwd_pid) - 1;
5869 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5871 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5872 GNUNET_break_op (0);
5873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5874 "Received PID %u, ACK %u\n",
5875 pid, t->last_fwd_ack);
5879 tunnel_reset_timeout (t);
5880 dest_id = GNUNET_PEER_search (&msg->destination);
5881 if (dest_id == myid)
5883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5884 " it's for us! sending to clients...\n");
5885 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5886 send_subscribed_clients (message, &msg[1].header, t);
5887 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5890 ttl = ntohl (msg->ttl);
5891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5894 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5895 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5896 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5900 " not for us, retransmitting...\n");
5902 neighbor = tree_get_first_hop (t->tree, dest_id);
5903 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5904 cinfo->fwd_pid = pid;
5905 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5908 if (GNUNET_YES == t->nobuffer &&
5909 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5911 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5912 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5913 GNUNET_break_op (0);
5916 send_prebuilt_message (message, neighbor, t);
5917 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5923 * Core handler for mesh network traffic going from the origin to all peers
5925 * @param cls closure
5926 * @param message message
5927 * @param peer peer identity this notification is about
5928 * @param atsi performance data
5929 * @param atsi_count number of records in 'atsi'
5930 * @return GNUNET_OK to keep the connection open,
5931 * GNUNET_SYSERR to close it (signal serious error)
5933 * TODO: Check who we got this from, to validate route.
5936 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5937 const struct GNUNET_MessageHeader *message,
5938 const struct GNUNET_ATS_Information *atsi,
5939 unsigned int atsi_count)
5941 struct GNUNET_MESH_Multicast *msg;
5942 struct MeshTunnel *t;
5946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5948 size = ntohs (message->size);
5949 if (sizeof (struct GNUNET_MESH_Multicast) +
5950 sizeof (struct GNUNET_MessageHeader) > size)
5952 GNUNET_break_op (0);
5955 msg = (struct GNUNET_MESH_Multicast *) message;
5956 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5960 /* TODO notify that we dont know that tunnel */
5961 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5962 GNUNET_break_op (0);
5965 pid = ntohl (msg->pid);
5966 if (t->fwd_pid == pid)
5968 /* already seen this packet, drop */
5969 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5971 " Already seen pid %u, DROPPING!\n", pid);
5972 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5978 " pid %u not seen yet, forwarding\n", pid);
5980 t->skip += (pid - t->fwd_pid) - 1;
5982 tunnel_reset_timeout (t);
5984 /* Transmit to locally interested clients */
5985 if (NULL != t->peers &&
5986 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5988 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5989 send_subscribed_clients (message, &msg[1].header, t);
5990 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5993 if (ntohl (msg->ttl) == 0)
5995 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5996 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5997 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6000 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
6001 tunnel_send_multicast (t, message);
6007 * Core handler for mesh network traffic toward the owner of a tunnel
6009 * @param cls closure
6010 * @param message message
6011 * @param peer peer identity this notification is about
6012 * @param atsi performance data
6013 * @param atsi_count number of records in 'atsi'
6015 * @return GNUNET_OK to keep the connection open,
6016 * GNUNET_SYSERR to close it (signal serious error)
6019 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
6020 const struct GNUNET_MessageHeader *message,
6021 const struct GNUNET_ATS_Information *atsi,
6022 unsigned int atsi_count)
6024 struct GNUNET_MESH_ToOrigin *msg;
6025 struct GNUNET_PeerIdentity id;
6026 struct MeshPeerInfo *peer_info;
6027 struct MeshTunnel *t;
6028 struct MeshTunnelChildInfo *cinfo;
6029 GNUNET_PEER_Id predecessor;
6033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
6035 size = ntohs (message->size);
6036 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
6037 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
6039 GNUNET_break_op (0);
6042 msg = (struct GNUNET_MESH_ToOrigin *) message;
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
6044 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
6045 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6046 pid = ntohl (msg->pid);
6050 /* TODO notify that we dont know this tunnel (whom)? */
6051 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
6052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6053 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
6054 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
6058 cinfo = tunnel_get_neighbor_fc(t, peer);
6065 if (cinfo->bck_pid == pid)
6067 /* already seen this packet, drop */
6068 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
6069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6070 " Already seen pid %u, DROPPING!\n", pid);
6071 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6076 " pid %u not seen yet, forwarding\n", pid);
6077 cinfo->bck_pid = pid;
6079 if (NULL != t->owner)
6082 struct GNUNET_MESH_ToOrigin *copy;
6084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6085 " it's for us! sending to clients...\n");
6086 /* TODO signature verification */
6087 memcpy (cbuf, message, size);
6088 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
6089 copy->tid = htonl (t->local_tid);
6091 copy->pid = htonl (t->bck_pid);
6092 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
6093 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
6094 ©->header, GNUNET_NO);
6095 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6099 " not for us, retransmitting...\n");
6101 peer_info = peer_info_get (&msg->oid);
6102 if (NULL == peer_info)
6104 /* unknown origin of tunnel */
6108 predecessor = tree_get_predecessor (t->tree);
6109 if (0 == predecessor)
6111 if (GNUNET_YES == t->destroy)
6113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6114 "to orig received on a dying tunnel %s [%X]\n",
6115 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6119 "unknown to origin at %s\n",
6120 GNUNET_i2s (&my_full_id));
6121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6125 "for tunnel %s [%X]\n",
6126 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6127 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6129 tree_debug (t->tree);
6132 GNUNET_PEER_resolve (predecessor, &id);
6133 send_prebuilt_message (message, &id, t);
6134 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6141 * Core handler for mesh network traffic point-to-point acks.
6143 * @param cls closure
6144 * @param message message
6145 * @param peer peer identity this notification is about
6146 * @param atsi performance data
6147 * @param atsi_count number of records in 'atsi'
6149 * @return GNUNET_OK to keep the connection open,
6150 * GNUNET_SYSERR to close it (signal serious error)
6153 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6154 const struct GNUNET_MessageHeader *message,
6155 const struct GNUNET_ATS_Information *atsi,
6156 unsigned int atsi_count)
6158 struct GNUNET_MESH_ACK *msg;
6159 struct MeshTunnel *t;
6162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6164 msg = (struct GNUNET_MESH_ACK *) message;
6166 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6170 /* TODO notify that we dont know this tunnel (whom)? */
6171 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6174 ack = ntohl (msg->pid);
6175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6177 /* Is this a forward or backward ACK? */
6178 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6180 struct MeshTunnelChildInfo *cinfo;
6183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6184 cinfo = tunnel_get_neighbor_fc (t, peer);
6185 cinfo->fwd_ack = ack;
6186 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6187 tunnel_unlock_fwd_queues (t);
6188 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6190 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6191 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6192 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6199 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6200 tunnel_unlock_bck_queue (t);
6207 * Core handler for mesh network traffic point-to-point ack polls.
6209 * @param cls closure
6210 * @param message message
6211 * @param peer peer identity this notification is about
6212 * @param atsi performance data
6213 * @param atsi_count number of records in 'atsi'
6215 * @return GNUNET_OK to keep the connection open,
6216 * GNUNET_SYSERR to close it (signal serious error)
6219 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6220 const struct GNUNET_MessageHeader *message,
6221 const struct GNUNET_ATS_Information *atsi,
6222 unsigned int atsi_count)
6224 struct GNUNET_MESH_Poll *msg;
6225 struct MeshTunnel *t;
6227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6230 msg = (struct GNUNET_MESH_Poll *) message;
6232 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6236 /* TODO notify that we dont know this tunnel (whom)? */
6237 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6238 GNUNET_break_op (0);
6242 /* Is this a forward or backward ACK? */
6243 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6245 struct MeshTunnelChildInfo *cinfo;
6247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6248 cinfo = tunnel_get_neighbor_fc (t, peer);
6249 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6250 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6255 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6263 * Core handler for path ACKs
6265 * @param cls closure
6266 * @param message message
6267 * @param peer peer identity this notification is about
6268 * @param atsi performance data
6269 * @param atsi_count number of records in 'atsi'
6271 * @return GNUNET_OK to keep the connection open,
6272 * GNUNET_SYSERR to close it (signal serious error)
6275 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6276 const struct GNUNET_MessageHeader *message,
6277 const struct GNUNET_ATS_Information *atsi,
6278 unsigned int atsi_count)
6280 struct GNUNET_MESH_PathACK *msg;
6281 struct GNUNET_PeerIdentity id;
6282 struct MeshPeerInfo *peer_info;
6283 struct MeshPeerPath *p;
6284 struct MeshTunnel *t;
6286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6287 GNUNET_i2s (&my_full_id));
6288 msg = (struct GNUNET_MESH_PathACK *) message;
6289 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6292 /* TODO notify that we don't know the tunnel */
6293 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6295 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6299 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6301 peer_info = peer_info_get (&msg->peer_id);
6302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6303 GNUNET_i2s (&msg->peer_id));
6304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6307 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6310 "connect_by_string completed, stopping search\n");
6311 regex_cancel_search (t->regex_ctx);
6312 t->regex_ctx = NULL;
6315 /* Add paths to peers? */
6316 p = tree_get_path_to_peer (t->tree, peer_info->id);
6319 path_add_to_peers (p, GNUNET_YES);
6327 /* Message for us? */
6328 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6331 if (NULL == t->owner)
6333 GNUNET_break_op (0);
6336 if (NULL != t->dht_get_type)
6338 GNUNET_DHT_get_stop (t->dht_get_type);
6339 t->dht_get_type = NULL;
6341 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6343 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6344 send_client_peer_connected (t, peer_info->id);
6349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6350 " not for us, retransmitting...\n");
6351 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6352 peer_info = peer_info_get (&msg->oid);
6353 if (NULL == peer_info)
6355 /* If we know the tunnel, we should DEFINITELY know the peer */
6359 send_prebuilt_message (message, &id, t);
6365 * Core handler for mesh keepalives.
6367 * @param cls closure
6368 * @param message message
6369 * @param peer peer identity this notification is about
6370 * @param atsi performance data
6371 * @param atsi_count number of records in 'atsi'
6372 * @return GNUNET_OK to keep the connection open,
6373 * GNUNET_SYSERR to close it (signal serious error)
6375 * TODO: Check who we got this from, to validate route.
6378 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6379 const struct GNUNET_MessageHeader *message,
6380 const struct GNUNET_ATS_Information *atsi,
6381 unsigned int atsi_count)
6383 struct GNUNET_MESH_TunnelKeepAlive *msg;
6384 struct MeshTunnel *t;
6386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6389 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6390 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6394 /* TODO notify that we dont know that tunnel */
6395 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6396 GNUNET_break_op (0);
6400 tunnel_reset_timeout (t);
6402 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6403 tunnel_send_multicast (t, message);
6410 * Functions to handle messages from core
6412 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6413 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6414 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6415 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6416 sizeof (struct GNUNET_MESH_PathBroken)},
6417 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6418 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6419 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6420 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6421 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6422 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6423 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6424 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6425 sizeof (struct GNUNET_MESH_ACK)},
6426 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6427 sizeof (struct GNUNET_MESH_Poll)},
6428 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6429 sizeof (struct GNUNET_MESH_PathACK)},
6435 /******************************************************************************/
6436 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6437 /******************************************************************************/
6440 * deregister_app: iterator for removing each application registered by a client
6442 * @param cls closure
6443 * @param key the hash of the application id (used to access the hashmap)
6444 * @param value the value stored at the key (client)
6446 * @return GNUNET_OK on success
6449 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6451 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6452 GNUNET_break (GNUNET_YES ==
6453 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6459 * notify_client_connection_failure: notify a client that the connection to the
6460 * requested remote peer is not possible (for instance, no route found)
6461 * Function called when the socket is ready to queue more data. "buf" will be
6462 * NULL and "size" zero if the socket was closed for writing in the meantime.
6464 * @param cls closure
6465 * @param size number of bytes available in buf
6466 * @param buf where the callee should write the message
6467 * @return number of bytes written to buf
6470 notify_client_connection_failure (void *cls, size_t size, void *buf)
6473 struct MeshPeerInfo *peer_info;
6474 struct GNUNET_MESH_PeerControl *msg;
6475 struct GNUNET_PeerIdentity id;
6477 if (0 == size && NULL == buf)
6479 // TODO retry? cancel?
6483 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6484 peer_info = (struct MeshPeerInfo *) cls;
6485 msg = (struct GNUNET_MESH_PeerControl *) buf;
6486 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6487 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6488 // msg->tunnel_id = htonl(peer_info->t->tid);
6489 GNUNET_PEER_resolve (peer_info->id, &id);
6490 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6498 * Send keepalive packets for a peer
6500 * @param cls Closure (tunnel for which to send the keepalive).
6501 * @param tc Notification context.
6504 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6506 struct MeshTunnel *t = cls;
6507 struct GNUNET_MESH_TunnelKeepAlive *msg;
6508 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6511 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6512 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6518 "sending keepalive for tunnel %d\n", t->id.tid);
6520 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6521 msg->header.size = htons (size);
6522 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6523 msg->oid = my_full_id;
6524 msg->tid = htonl (t->id.tid);
6525 tunnel_send_multicast (t, &msg->header);
6527 t->path_refresh_task =
6528 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6529 tunnel_reset_timeout(t);
6534 * Function to process paths received for a new peer addition. The recorded
6535 * paths form the initial tunnel, which can be optimized later.
6536 * Called on each result obtained for the DHT search.
6538 * @param cls closure
6539 * @param exp when will this value expire
6540 * @param key key of the result
6541 * @param get_path path of the get request
6542 * @param get_path_length lenght of get_path
6543 * @param put_path path of the put request
6544 * @param put_path_length length of the put_path
6545 * @param type type of the result
6546 * @param size number of bytes in data
6547 * @param data pointer to the result data
6549 * TODO: re-issue the request after certain time? cancel after X results?
6552 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6553 const struct GNUNET_HashCode * key,
6554 const struct GNUNET_PeerIdentity *get_path,
6555 unsigned int get_path_length,
6556 const struct GNUNET_PeerIdentity *put_path,
6557 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6558 size_t size, const void *data)
6560 struct MeshPathInfo *path_info = cls;
6561 struct MeshPeerPath *p;
6562 struct GNUNET_PeerIdentity pi;
6565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6566 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6569 p = path_build_from_dht (get_path, get_path_length, put_path,
6571 path_add_to_peers (p, GNUNET_NO);
6573 for (i = 0; i < path_info->peer->ntunnels; i++)
6575 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6576 peer_info_connect (path_info->peer, path_info->t);
6584 * Function to process paths received for a new peer addition. The recorded
6585 * paths form the initial tunnel, which can be optimized later.
6586 * Called on each result obtained for the DHT search.
6588 * @param cls closure
6589 * @param exp when will this value expire
6590 * @param key key of the result
6591 * @param get_path path of the get request
6592 * @param get_path_length lenght of get_path
6593 * @param put_path path of the put request
6594 * @param put_path_length length of the put_path
6595 * @param type type of the result
6596 * @param size number of bytes in data
6597 * @param data pointer to the result data
6600 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6601 const struct GNUNET_HashCode * key,
6602 const struct GNUNET_PeerIdentity *get_path,
6603 unsigned int get_path_length,
6604 const struct GNUNET_PeerIdentity *put_path,
6605 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6606 size_t size, const void *data)
6608 const struct PBlock *pb = data;
6609 const struct GNUNET_PeerIdentity *pi = &pb->id;
6610 struct MeshTunnel *t = cls;
6611 struct MeshPeerInfo *peer_info;
6612 struct MeshPeerPath *p;
6614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6615 if (size != sizeof (struct PBlock))
6617 GNUNET_break_op (0);
6620 if (ntohl(pb->type) != t->type)
6622 GNUNET_break_op (0);
6625 GNUNET_assert (NULL != t->owner);
6626 peer_info = peer_info_get (pi);
6627 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6629 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6631 p = path_build_from_dht (get_path, get_path_length, put_path,
6633 path_add_to_peers (p, GNUNET_NO);
6635 tunnel_add_peer (t, peer_info);
6636 peer_info_connect (peer_info, t);
6641 * Function to process DHT string to regex matching.
6642 * Called on each result obtained for the DHT search.
6644 * @param cls closure (search context)
6645 * @param exp when will this value expire
6646 * @param key key of the result
6647 * @param get_path path of the get request (not used)
6648 * @param get_path_length lenght of get_path (not used)
6649 * @param put_path path of the put request (not used)
6650 * @param put_path_length length of the put_path (not used)
6651 * @param type type of the result
6652 * @param size number of bytes in data
6653 * @param data pointer to the result data
6656 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6657 const struct GNUNET_HashCode * key,
6658 const struct GNUNET_PeerIdentity *get_path,
6659 unsigned int get_path_length,
6660 const struct GNUNET_PeerIdentity *put_path,
6661 unsigned int put_path_length,
6662 enum GNUNET_BLOCK_Type type,
6663 size_t size, const void *data)
6665 const struct MeshRegexAccept *block = data;
6666 struct MeshRegexSearchContext *ctx = cls;
6667 struct MeshRegexSearchInfo *info = ctx->info;
6668 // struct MeshPeerPath *p;
6669 struct MeshPeerInfo *peer_info;
6671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6673 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6675 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6678 peer_info = peer_info_get(&block->id);
6679 // p = path_build_from_dht (get_path, get_path_length, put_path,
6680 // put_path_length);
6681 // path_add_to_peers (p, GNUNET_NO);
6684 tunnel_add_peer (info->t, peer_info);
6685 peer_info_connect (peer_info, info->t);
6686 if (0 == info->peer)
6688 info->peer = peer_info->id;
6692 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6695 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6696 ®ex_connect_timeout,
6704 * Function to process DHT string to regex matching.
6705 * Called on each result obtained for the DHT search.
6707 * @param cls closure (search context)
6708 * @param exp when will this value expire
6709 * @param key key of the result
6710 * @param get_path path of the get request (not used)
6711 * @param get_path_length lenght of get_path (not used)
6712 * @param put_path path of the put request (not used)
6713 * @param put_path_length length of the put_path (not used)
6714 * @param type type of the result
6715 * @param size number of bytes in data
6716 * @param data pointer to the result data
6718 * TODO: re-issue the request after certain time? cancel after X results?
6721 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6722 const struct GNUNET_HashCode * key,
6723 const struct GNUNET_PeerIdentity *get_path,
6724 unsigned int get_path_length,
6725 const struct GNUNET_PeerIdentity *put_path,
6726 unsigned int put_path_length,
6727 enum GNUNET_BLOCK_Type type,
6728 size_t size, const void *data)
6730 const struct MeshRegexBlock *block = data;
6731 struct MeshRegexSearchContext *ctx = cls;
6732 struct MeshRegexSearchInfo *info = ctx->info;
6736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6737 "DHT GET STRING RETURNED RESULTS\n");
6738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6739 " key: %s\n", GNUNET_h2s (key));
6741 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6743 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6746 copy = GNUNET_malloc (size);
6747 memcpy (copy, data, size);
6748 GNUNET_break (GNUNET_OK ==
6749 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6750 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6751 len = ntohl (block->n_proof);
6753 char proof[len + 1];
6755 memcpy (proof, &block[1], len);
6757 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6759 GNUNET_break_op (0);
6763 len = strlen (info->description);
6764 if (len == ctx->position) // String processed
6766 if (GNUNET_YES == ntohl (block->accepting))
6768 regex_find_path(key, ctx);
6772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6773 // FIXME REGEX this block not successful, wait for more? start timeout?
6778 regex_next_edge (block, size, ctx);
6783 /******************************************************************************/
6784 /********************* MESH LOCAL HANDLES **************************/
6785 /******************************************************************************/
6789 * Handler for client disconnection
6791 * @param cls closure
6792 * @param client identification of the client; NULL
6793 * for the last call when the server is destroyed
6796 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6798 struct MeshClient *c;
6799 struct MeshClient *next;
6802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6812 if (c->handle != client)
6814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6820 GNUNET_SERVER_client_drop (c->handle);
6821 c->shutting_down = GNUNET_YES;
6822 GNUNET_assert (NULL != c->own_tunnels);
6823 GNUNET_assert (NULL != c->incoming_tunnels);
6824 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6825 &tunnel_destroy_iterator, c);
6826 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6827 &tunnel_destroy_iterator, c);
6828 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6829 &tunnel_destroy_iterator, c);
6830 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6831 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6832 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6834 /* deregister clients applications */
6835 if (NULL != c->apps)
6837 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6838 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6840 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6841 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6843 GNUNET_SCHEDULER_cancel (announce_applications_task);
6844 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6846 if (NULL != c->types)
6847 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6848 for (i = 0; i < c->n_regex; i++)
6850 GNUNET_free (c->regexes[i].regex);
6851 if (NULL != c->regexes[i].dfa)
6852 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6854 GNUNET_free_non_null (c->regexes);
6855 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6856 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6858 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6861 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6870 * Handler for new clients
6872 * @param cls closure
6873 * @param client identification of the client
6874 * @param message the actual message, which includes messages the client wants
6877 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6878 const struct GNUNET_MessageHeader *message)
6880 struct GNUNET_MESH_ClientConnect *cc_msg;
6881 struct MeshClient *c;
6882 GNUNET_MESH_ApplicationType *a;
6889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6890 /* Check data sanity */
6891 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6892 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6893 ntypes = ntohs (cc_msg->types);
6894 napps = ntohs (cc_msg->applications);
6896 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6903 /* Create new client structure */
6904 c = GNUNET_malloc (sizeof (struct MeshClient));
6905 c->id = next_client_id++;
6906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6908 GNUNET_SERVER_client_keep (client);
6909 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6912 GNUNET_MESH_ApplicationType at;
6913 struct GNUNET_HashCode hc;
6915 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6916 for (i = 0; i < napps; i++)
6919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6920 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6921 /* store in clients hashmap */
6922 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6923 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6924 /* store in global hashmap, for announcements */
6925 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6926 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6928 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6929 announce_applications_task =
6930 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6936 struct GNUNET_HashCode hc;
6938 t = (uint16_t *) & a[napps];
6939 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6940 for (i = 0; i < ntypes; i++)
6943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6944 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6946 /* store in clients hashmap */
6947 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6948 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6949 /* store in global hashmap */
6950 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6951 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6955 " client has %u+%u subscriptions\n", napps, ntypes);
6957 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6958 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6959 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6960 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6961 GNUNET_SERVER_notification_context_add (nc, client);
6962 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6964 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6970 * Handler for clients announcing available services by a regular expression.
6972 * @param cls closure
6973 * @param client identification of the client
6974 * @param message the actual message, which includes messages the client wants
6977 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6978 const struct GNUNET_MessageHeader *message)
6980 const struct GNUNET_MESH_RegexAnnounce *msg;
6981 struct MeshRegexDescriptor rd;
6982 struct MeshClient *c;
6986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6988 /* Sanity check for client registration */
6989 if (NULL == (c = client_get (client)))
6992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6997 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6998 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6999 regex = GNUNET_malloc (len + 1);
7000 memcpy (regex, &msg[1], len);
7003 rd.compression = ntohs (msg->compression_characters);
7005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
7006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
7007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
7008 GNUNET_array_append (c->regexes, c->n_regex, rd);
7009 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
7011 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
7017 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
7023 * Handler for requests of new tunnels
7025 * @param cls closure
7026 * @param client identification of the client
7027 * @param message the actual message
7030 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
7031 const struct GNUNET_MessageHeader *message)
7033 struct GNUNET_MESH_TunnelMessage *t_msg;
7034 struct MeshTunnel *t;
7035 struct MeshClient *c;
7036 MESH_TunnelNumber tid;
7038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
7040 /* Sanity check for client registration */
7041 if (NULL == (c = client_get (client)))
7044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7049 /* Message sanity check */
7050 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7057 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7058 /* Sanity check for tunnel numbering */
7059 tid = ntohl (t_msg->tunnel_id);
7060 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
7063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7066 /* Sanity check for duplicate tunnel IDs */
7067 if (NULL != tunnel_get_by_local_id (c, tid))
7070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7074 while (NULL != tunnel_get_by_pi (myid, next_tid))
7075 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7076 t = tunnel_new (myid, next_tid++, c, tid);
7079 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
7081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7084 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
7086 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
7087 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
7090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7096 * Handler for requests of deleting tunnels
7098 * @param cls closure
7099 * @param client identification of the client
7100 * @param message the actual message
7103 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
7104 const struct GNUNET_MessageHeader *message)
7106 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7107 struct MeshClient *c;
7108 struct MeshTunnel *t;
7109 MESH_TunnelNumber tid;
7111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7112 "Got a DESTROY TUNNEL from client!\n");
7114 /* Sanity check for client registration */
7115 if (NULL == (c = client_get (client)))
7118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7123 /* Message sanity check */
7124 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7131 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7133 /* Retrieve tunnel */
7134 tid = ntohl (tunnel_msg->tunnel_id);
7135 t = tunnel_get_by_local_id(c, tid);
7138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7143 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7145 client_ignore_tunnel (c, t);
7146 tunnel_destroy_empty (t);
7147 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7150 send_client_tunnel_disconnect(t, c);
7151 client_delete_tunnel(c, t);
7153 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7155 tunnel_send_destroy (t);
7156 t->destroy = GNUNET_YES;
7157 // The tunnel will be destroyed when the last message is transmitted.
7158 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7164 * Handler for requests of seeting tunnel's speed.
7166 * @param cls Closure (unused).
7167 * @param client Identification of the client.
7168 * @param message The actual message.
7171 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7172 const struct GNUNET_MessageHeader *message)
7174 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7175 struct MeshClient *c;
7176 struct MeshTunnel *t;
7177 MESH_TunnelNumber tid;
7179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7180 "Got a SPEED request from client!\n");
7182 /* Sanity check for client registration */
7183 if (NULL == (c = client_get (client)))
7186 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7192 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7194 /* Retrieve tunnel */
7195 tid = ntohl (tunnel_msg->tunnel_id);
7196 t = tunnel_get_by_local_id(c, tid);
7199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7205 switch (ntohs(message->type))
7207 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7208 t->speed_min = GNUNET_YES;
7210 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7211 t->speed_min = GNUNET_NO;
7216 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7221 * Handler for requests of seeting tunnel's buffering policy.
7223 * @param cls Closure (unused).
7224 * @param client Identification of the client.
7225 * @param message The actual message.
7228 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7229 const struct GNUNET_MessageHeader *message)
7231 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7232 struct MeshClient *c;
7233 struct MeshTunnel *t;
7234 MESH_TunnelNumber tid;
7236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7237 "Got a BUFFER request from client!\n");
7239 /* Sanity check for client registration */
7240 if (NULL == (c = client_get (client)))
7243 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7248 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7250 /* Retrieve tunnel */
7251 tid = ntohl (tunnel_msg->tunnel_id);
7252 t = tunnel_get_by_local_id(c, tid);
7255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7261 switch (ntohs(message->type))
7263 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7264 t->nobuffer = GNUNET_NO;
7266 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7267 t->nobuffer = GNUNET_YES;
7273 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7278 * Handler for connection requests to new peers
7280 * @param cls closure
7281 * @param client identification of the client
7282 * @param message the actual message (PeerControl)
7285 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7286 const struct GNUNET_MessageHeader *message)
7288 struct GNUNET_MESH_PeerControl *peer_msg;
7289 struct MeshPeerInfo *peer_info;
7290 struct MeshClient *c;
7291 struct MeshTunnel *t;
7292 MESH_TunnelNumber tid;
7294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7295 /* Sanity check for client registration */
7296 if (NULL == (c = client_get (client)))
7299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7304 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7306 /* Sanity check for message size */
7307 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7314 /* Tunnel exists? */
7315 tid = ntohl (peer_msg->tunnel_id);
7316 t = tunnel_get_by_local_id (c, tid);
7320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7324 /* Does client own tunnel? */
7325 if (t->owner->handle != client)
7328 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7332 GNUNET_i2s (&peer_msg->peer));
7333 peer_info = peer_info_get (&peer_msg->peer);
7335 tunnel_add_peer (t, peer_info);
7336 peer_info_connect (peer_info, t);
7338 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7344 * Handler for disconnection requests of peers in a tunnel
7346 * @param cls closure
7347 * @param client identification of the client
7348 * @param message the actual message (PeerControl)
7351 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7352 const struct GNUNET_MessageHeader *message)
7354 struct GNUNET_MESH_PeerControl *peer_msg;
7355 struct MeshPeerInfo *peer_info;
7356 struct MeshClient *c;
7357 struct MeshTunnel *t;
7358 MESH_TunnelNumber tid;
7360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7361 /* Sanity check for client registration */
7362 if (NULL == (c = client_get (client)))
7365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7370 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7372 /* Sanity check for message size */
7373 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7376 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7380 /* Tunnel exists? */
7381 tid = ntohl (peer_msg->tunnel_id);
7382 t = tunnel_get_by_local_id (c, tid);
7386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7391 /* Does client own tunnel? */
7392 if (t->owner->handle != client)
7395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7400 GNUNET_i2s (&peer_msg->peer));
7401 /* Is the peer in the tunnel? */
7403 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7404 if (NULL == peer_info)
7407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7411 /* Ok, delete peer from tunnel */
7412 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7413 &peer_msg->peer.hashPubKey);
7415 send_destroy_path (t, peer_info->id);
7416 tunnel_delete_peer (t, peer_info->id);
7417 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7422 * Handler for blacklist requests of peers in a tunnel
7424 * @param cls closure
7425 * @param client identification of the client
7426 * @param message the actual message (PeerControl)
7428 * FIXME implement DHT block bloomfilter
7431 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7432 const struct GNUNET_MessageHeader *message)
7434 struct GNUNET_MESH_PeerControl *peer_msg;
7435 struct MeshClient *c;
7436 struct MeshTunnel *t;
7437 MESH_TunnelNumber tid;
7439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7440 /* Sanity check for client registration */
7441 if (NULL == (c = client_get (client)))
7444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7449 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7451 /* Sanity check for message size */
7452 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7459 /* Tunnel exists? */
7460 tid = ntohl (peer_msg->tunnel_id);
7461 t = tunnel_get_by_local_id (c, tid);
7465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7470 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7471 GNUNET_PEER_intern(&peer_msg->peer));
7476 * Handler for unblacklist requests of peers in a tunnel
7478 * @param cls closure
7479 * @param client identification of the client
7480 * @param message the actual message (PeerControl)
7483 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7484 const struct GNUNET_MessageHeader *message)
7486 struct GNUNET_MESH_PeerControl *peer_msg;
7487 struct MeshClient *c;
7488 struct MeshTunnel *t;
7489 MESH_TunnelNumber tid;
7493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7494 /* Sanity check for client registration */
7495 if (NULL == (c = client_get (client)))
7498 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7503 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7505 /* Sanity check for message size */
7506 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7513 /* Tunnel exists? */
7514 tid = ntohl (peer_msg->tunnel_id);
7515 t = tunnel_get_by_local_id (c, tid);
7519 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7524 /* if peer is not known, complain */
7525 pid = GNUNET_PEER_search (&peer_msg->peer);
7532 /* search and remove from list */
7533 for (i = 0; i < t->nblacklisted; i++)
7535 if (t->blacklisted[i] == pid)
7537 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7538 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7543 /* if peer hasn't been blacklisted, complain */
7549 * Handler for connection requests to new peers by type
7551 * @param cls closure
7552 * @param client identification of the client
7553 * @param message the actual message (ConnectPeerByType)
7556 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7557 const struct GNUNET_MessageHeader *message)
7559 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7560 struct MeshClient *c;
7561 struct MeshTunnel *t;
7562 struct GNUNET_HashCode hash;
7563 MESH_TunnelNumber tid;
7565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7566 /* Sanity check for client registration */
7567 if (NULL == (c = client_get (client)))
7570 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7575 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7577 /* Sanity check for message size */
7578 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7579 ntohs (connect_msg->header.size))
7582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7586 /* Tunnel exists? */
7587 tid = ntohl (connect_msg->tunnel_id);
7588 t = tunnel_get_by_local_id (c, tid);
7592 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7596 /* Does client own tunnel? */
7597 if (t->owner->handle != client)
7600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7604 /* Do WE have the service? */
7605 t->type = ntohl (connect_msg->type);
7606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7607 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7608 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7611 /* Yes! Fast forward, add ourselves to the tunnel and send the
7612 * good news to the client, and alert the destination client of
7613 * an incoming tunnel.
7615 * FIXME send a path create to self, avoid code duplication
7617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7618 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7619 peer_info_get (&my_full_id),
7620 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7623 send_client_peer_connected (t, myid);
7624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7625 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7627 t->local_tid_dest = next_local_tid++;
7628 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7629 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7630 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7634 /* Ok, lets find a peer offering the service */
7635 if (NULL != t->dht_get_type)
7637 GNUNET_DHT_get_stop (t->dht_get_type);
7639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7640 GNUNET_h2s (&hash));
7642 GNUNET_DHT_get_start (dht_handle,
7643 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7645 dht_replication_level,
7646 GNUNET_DHT_RO_RECORD_ROUTE |
7647 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7649 &dht_get_type_handler, t);
7651 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7657 * Handler for connection requests to new peers by a string service description.
7659 * @param cls closure
7660 * @param client identification of the client
7661 * @param message the actual message, which includes messages the client wants
7664 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7665 const struct GNUNET_MessageHeader *message)
7667 struct GNUNET_MESH_ConnectPeerByString *msg;
7668 struct MeshRegexSearchContext *ctx;
7669 struct MeshRegexSearchInfo *info;
7670 struct GNUNET_DHT_GetHandle *get_h;
7671 struct GNUNET_HashCode key;
7672 struct MeshTunnel *t;
7673 struct MeshClient *c;
7674 MESH_TunnelNumber tid;
7679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7680 "Connect by string started\n");
7681 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7682 size = htons (message->size);
7684 /* Sanity check for client registration */
7685 if (NULL == (c = client_get (client)))
7688 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7693 /* Message size sanity check */
7694 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7697 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7701 /* Tunnel exists? */
7702 tid = ntohl (msg->tunnel_id);
7703 t = tunnel_get_by_local_id (c, tid);
7707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7711 /* Does client own tunnel? */
7712 if (t->owner->handle != client)
7715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7720 " on tunnel %s [%u]\n",
7721 GNUNET_i2s(&my_full_id),
7724 /* Only one connect_by_string allowed at the same time! */
7725 /* FIXME: allow more, return handle at api level to cancel, document */
7726 if (NULL != t->regex_ctx)
7729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7733 /* Find string itself */
7734 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7735 string = (const char *) &msg[1];
7737 /* Initialize context */
7738 size = GNUNET_REGEX_get_first_key (string, len, &key);
7739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7740 " consumed %u bits out of %u\n", size, len);
7741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7742 " looking for %s\n", GNUNET_h2s (&key));
7744 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7746 info->description = GNUNET_strndup (string, len);
7747 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7748 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7751 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7752 ctx->position = size;
7756 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7758 /* Start search in DHT */
7759 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7760 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7761 &key, /* key to search */
7762 dht_replication_level, /* replication level */
7763 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7764 &info->description[size], /* xquery */
7765 // FIXME add BLOOMFILTER to exclude filtered peers
7766 len + 1 - size, /* xquery bits */
7767 // FIXME add BLOOMFILTER SIZE
7768 &dht_get_string_handler, ctx);
7770 GNUNET_break (GNUNET_OK ==
7771 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7774 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7776 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7782 * Handler for client traffic directed to one peer
7784 * @param cls closure
7785 * @param client identification of the client
7786 * @param message the actual message
7789 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7790 const struct GNUNET_MessageHeader *message)
7792 struct MeshClient *c;
7793 struct MeshTunnel *t;
7794 struct MeshPeerInfo *pi;
7795 struct GNUNET_MESH_Unicast *data_msg;
7796 MESH_TunnelNumber tid;
7799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7800 "Got a unicast request from a client!\n");
7802 /* Sanity check for client registration */
7803 if (NULL == (c = client_get (client)))
7806 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7811 data_msg = (struct GNUNET_MESH_Unicast *) message;
7813 /* Sanity check for message size */
7814 size = ntohs (message->size);
7815 if (sizeof (struct GNUNET_MESH_Unicast) +
7816 sizeof (struct GNUNET_MessageHeader) > size)
7819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7823 /* Tunnel exists? */
7824 tid = ntohl (data_msg->tid);
7825 t = tunnel_get_by_local_id (c, tid);
7829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7833 /* Is it a local tunnel? Then, does client own the tunnel? */
7834 if (t->owner->handle != client)
7837 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7841 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7842 &data_msg->destination.hashPubKey);
7843 /* Is the selected peer in the tunnel? */
7847 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7851 /* PID should be as expected */
7852 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7856 "Unicast PID, expected %u, got %u\n",
7857 t->fwd_pid + 1, ntohl (data_msg->pid));
7858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7862 /* Ok, everything is correct, send the message
7863 * (pretend we got it from a mesh peer)
7866 /* Work around const limitation */
7867 char buf[ntohs (message->size)] GNUNET_ALIGN;
7868 struct GNUNET_MESH_Unicast *copy;
7870 copy = (struct GNUNET_MESH_Unicast *) buf;
7871 memcpy (buf, data_msg, size);
7872 copy->oid = my_full_id;
7873 copy->tid = htonl (t->id.tid);
7874 copy->ttl = htonl (default_ttl);
7875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7876 " calling generic handler...\n");
7877 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7880 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7887 * Handler for client traffic directed to the origin
7889 * @param cls closure
7890 * @param client identification of the client
7891 * @param message the actual message
7894 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7895 const struct GNUNET_MessageHeader *message)
7897 struct GNUNET_MESH_ToOrigin *data_msg;
7898 struct MeshTunnelClientInfo *clinfo;
7899 struct MeshClient *c;
7900 struct MeshTunnel *t;
7901 MESH_TunnelNumber tid;
7904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7905 "Got a ToOrigin request from a client!\n");
7906 /* Sanity check for client registration */
7907 if (NULL == (c = client_get (client)))
7910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7915 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7917 /* Sanity check for message size */
7918 size = ntohs (message->size);
7919 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7920 sizeof (struct GNUNET_MessageHeader) > size)
7923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7927 /* Tunnel exists? */
7928 tid = ntohl (data_msg->tid);
7929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7930 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7936 t = tunnel_get_by_local_id (c, tid);
7939 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7940 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7946 /* It should be sent by someone who has this as incoming tunnel. */
7947 if (GNUNET_NO == client_knows_tunnel (c, t))
7950 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7954 /* PID should be as expected */
7955 clinfo = tunnel_get_client_fc (t, c);
7956 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7959 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7960 "To Origin PID, expected %u, got %u\n",
7961 clinfo->bck_pid + 1,
7962 ntohl (data_msg->pid));
7963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7968 /* Ok, everything is correct, send the message
7969 * (pretend we got it from a mesh peer)
7972 char buf[ntohs (message->size)] GNUNET_ALIGN;
7973 struct GNUNET_MESH_ToOrigin *copy;
7975 /* Work around const limitation */
7976 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7977 memcpy (buf, data_msg, size);
7978 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7979 copy->tid = htonl (t->id.tid);
7980 copy->ttl = htonl (default_ttl);
7981 copy->pid = htonl (t->bck_pid + 1);
7983 copy->sender = my_full_id;
7984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7985 " calling generic handler...\n");
7986 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7988 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7995 * Handler for client traffic directed to all peers in a tunnel
7997 * @param cls closure
7998 * @param client identification of the client
7999 * @param message the actual message
8002 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
8003 const struct GNUNET_MessageHeader *message)
8005 struct MeshClient *c;
8006 struct MeshTunnel *t;
8007 struct GNUNET_MESH_Multicast *data_msg;
8008 MESH_TunnelNumber tid;
8010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8011 "Got a multicast request from a client!\n");
8013 /* Sanity check for client registration */
8014 if (NULL == (c = client_get (client)))
8017 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8022 data_msg = (struct GNUNET_MESH_Multicast *) message;
8024 /* Sanity check for message size */
8025 if (sizeof (struct GNUNET_MESH_Multicast) +
8026 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
8029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8033 /* Tunnel exists? */
8034 tid = ntohl (data_msg->tid);
8035 t = tunnel_get_by_local_id (c, tid);
8039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8046 /* Does client own tunnel? */
8047 if (t->owner->handle != client)
8050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8054 /* PID should be as expected */
8055 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
8058 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8059 "Multicast PID, expected %u, got %u\n",
8060 t->fwd_pid + 1, ntohl (data_msg->pid));
8061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8066 char buf[ntohs (message->size)] GNUNET_ALIGN;
8067 struct GNUNET_MESH_Multicast *copy;
8069 copy = (struct GNUNET_MESH_Multicast *) buf;
8070 memcpy (buf, message, ntohs (message->size));
8071 copy->oid = my_full_id;
8072 copy->tid = htonl (t->id.tid);
8073 copy->ttl = htonl (default_ttl);
8074 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
8075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8076 " calling generic handler...\n");
8077 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
8080 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
8086 * Handler for client's ACKs for payload traffic.
8088 * @param cls Closure (unused).
8089 * @param client Identification of the client.
8090 * @param message The actual message.
8093 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
8094 const struct GNUNET_MessageHeader *message)
8096 struct GNUNET_MESH_LocalAck *msg;
8097 struct MeshTunnel *t;
8098 struct MeshClient *c;
8099 MESH_TunnelNumber tid;
8102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
8103 /* Sanity check for client registration */
8104 if (NULL == (c = client_get (client)))
8107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8112 msg = (struct GNUNET_MESH_LocalAck *) message;
8114 /* Tunnel exists? */
8115 tid = ntohl (msg->tunnel_id);
8116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8117 t = tunnel_get_by_local_id (c, tid);
8121 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8122 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8127 ack = ntohl (msg->max_pid);
8128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8130 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8131 if (NULL != t->owner && t->owner->handle == client)
8133 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8135 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8139 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8140 tunnel_set_client_fwd_ack (t, c, ack);
8141 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8144 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8151 * Iterator over all peers to send a monitoring client info about a tunnel.
8153 * @param cls Closure (message being built).
8154 * @param key Key (hashed tunnel ID, unused).
8155 * @param value Peer info.
8157 * @return GNUNET_YES, to keep iterating.
8160 monitor_peers_iterator (void *cls,
8161 const struct GNUNET_HashCode * key,
8164 struct GNUNET_MESH_LocalMonitor *msg = cls;
8165 struct GNUNET_PeerIdentity *id;
8166 struct MeshPeerInfo *info = value;
8168 id = (struct GNUNET_PeerIdentity *) &msg[1];
8169 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8172 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8173 "* sending info about peer %s [%u]\n",
8174 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8182 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8184 * @param cls Closure (client handle).
8185 * @param key Key (hashed tunnel ID, unused).
8186 * @param value Tunnel info.
8188 * @return GNUNET_YES, to keep iterating.
8191 monitor_all_tunnels_iterator (void *cls,
8192 const struct GNUNET_HashCode * key,
8195 struct GNUNET_SERVER_Client *client = cls;
8196 struct MeshTunnel *t = value;
8197 struct GNUNET_MESH_LocalMonitor *msg;
8200 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8201 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8202 npeers * sizeof (struct GNUNET_PeerIdentity));
8203 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8204 msg->tunnel_id = htonl (t->id.tid);
8205 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8206 npeers * sizeof (struct GNUNET_PeerIdentity));
8207 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8209 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8210 monitor_peers_iterator,
8213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8214 "* sending info about tunnel %s [%u] (%u peers)\n",
8215 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8217 if (msg->npeers != npeers)
8220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8221 "Get tunnels fail: size %u - iter %u\n",
8222 npeers, msg->npeers);
8225 msg->npeers = htonl (npeers);
8226 GNUNET_SERVER_notification_context_unicast (nc, client,
8227 &msg->header, GNUNET_NO);
8233 * Handler for client's MONITOR request.
8235 * @param cls Closure (unused).
8236 * @param client Identification of the client.
8237 * @param message The actual message.
8240 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8241 const struct GNUNET_MessageHeader *message)
8243 struct MeshClient *c;
8245 /* Sanity check for client registration */
8246 if (NULL == (c = client_get (client)))
8249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8254 "Received get tunnels request from client %u\n",
8256 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8257 monitor_all_tunnels_iterator,
8259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8260 "Get tunnels request from client %u completed\n",
8262 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8267 * Data needed to build a Monitor_Tunnel message.
8269 struct MeshMonitorTunnelContext
8272 * Partial message, including peer count.
8274 struct GNUNET_MESH_LocalMonitor *msg;
8277 * Hashmap with positions: peer->position.
8279 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8282 * Index of the parent of each peer in the message, realtive to the absolute
8283 * order in the array (can be in a previous message).
8285 uint32_t parents[1024];
8288 * Peers visited so far in the tree, aka position of the current peer.
8290 unsigned int npeers;
8293 * Client requesting the info.
8295 struct MeshClient *c;
8300 * Send a client a message about the structure of a tunnel.
8302 * @param ctx Context of the tunnel iteration, with info regarding the state
8303 * of the execution and the number of peers visited for this message.
8306 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8308 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8309 struct GNUNET_PeerIdentity *pid;
8310 unsigned int *parent;
8313 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8314 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8315 resp->header.size = htons (size);
8316 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8317 parent = (unsigned int *) &pid[resp->npeers];
8318 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8319 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8320 &resp->header, GNUNET_NO);
8324 * Iterator over a tunnel tree to build a message containing all peers
8325 * the in the tunnel, including relay nodes.
8327 * @param cls Closure (pointer to pointer of message being built).
8328 * @param peer Short ID of a peer.
8329 * @param parent Short ID of the @c peer 's parent.
8332 tunnel_tree_iterator (void *cls,
8333 GNUNET_PEER_Id peer,
8334 GNUNET_PEER_Id parent)
8336 struct MeshMonitorTunnelContext *ctx = cls;
8337 struct GNUNET_MESH_LocalMonitor *msg;
8338 struct GNUNET_PeerIdentity *pid;
8339 struct GNUNET_PeerIdentity ppid;
8342 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8343 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8344 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8345 &pid[msg->npeers].hashPubKey,
8346 (void *) (long) ctx->npeers,
8347 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8348 GNUNET_PEER_resolve (parent, &ppid);
8349 ctx->parents[msg->npeers] =
8350 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8356 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8358 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8361 send_client_tunnel_info (ctx);
8368 * Handler for client's MONITOR_TUNNEL request.
8370 * @param cls Closure (unused).
8371 * @param client Identification of the client.
8372 * @param message The actual message.
8375 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8376 const struct GNUNET_MessageHeader *message)
8378 const struct GNUNET_MESH_LocalMonitor *msg;
8379 struct GNUNET_MESH_LocalMonitor *resp;
8380 struct MeshMonitorTunnelContext ctx;
8381 struct MeshClient *c;
8382 struct MeshTunnel *t;
8384 /* Sanity check for client registration */
8385 if (NULL == (c = client_get (client)))
8388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8392 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8393 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8394 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8397 ntohl (msg->tunnel_id));
8398 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8401 /* We don't know the tunnel */
8402 struct GNUNET_MESH_LocalMonitor warn;
8405 warn.npeers = htonl (UINT_MAX);
8406 GNUNET_SERVER_notification_context_unicast (nc, client,
8409 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8413 /* Initialize context */
8414 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8418 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8422 /* Collect and send information */
8423 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8424 send_client_tunnel_info (&ctx);
8427 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8430 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8431 "Monitor tunnel request from client %u completed\n",
8433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8438 * Functions to handle messages from clients
8440 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8441 {&handle_local_new_client, NULL,
8442 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8443 {&handle_local_announce_regex, NULL,
8444 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8445 {&handle_local_tunnel_create, NULL,
8446 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8447 sizeof (struct GNUNET_MESH_TunnelMessage)},
8448 {&handle_local_tunnel_destroy, NULL,
8449 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8450 sizeof (struct GNUNET_MESH_TunnelMessage)},
8451 {&handle_local_tunnel_speed, NULL,
8452 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8453 sizeof (struct GNUNET_MESH_TunnelMessage)},
8454 {&handle_local_tunnel_speed, NULL,
8455 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8456 sizeof (struct GNUNET_MESH_TunnelMessage)},
8457 {&handle_local_tunnel_buffer, NULL,
8458 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8459 sizeof (struct GNUNET_MESH_TunnelMessage)},
8460 {&handle_local_tunnel_buffer, NULL,
8461 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8462 sizeof (struct GNUNET_MESH_TunnelMessage)},
8463 {&handle_local_connect_add, NULL,
8464 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8465 sizeof (struct GNUNET_MESH_PeerControl)},
8466 {&handle_local_connect_del, NULL,
8467 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8468 sizeof (struct GNUNET_MESH_PeerControl)},
8469 {&handle_local_blacklist, NULL,
8470 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8471 sizeof (struct GNUNET_MESH_PeerControl)},
8472 {&handle_local_unblacklist, NULL,
8473 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8474 sizeof (struct GNUNET_MESH_PeerControl)},
8475 {&handle_local_connect_by_type, NULL,
8476 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8477 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8478 {&handle_local_connect_by_string, NULL,
8479 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8480 {&handle_local_unicast, NULL,
8481 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8482 {&handle_local_to_origin, NULL,
8483 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8484 {&handle_local_multicast, NULL,
8485 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8486 {&handle_local_ack, NULL,
8487 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8488 sizeof (struct GNUNET_MESH_LocalAck)},
8489 {&handle_local_get_tunnels, NULL,
8490 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8491 sizeof (struct GNUNET_MessageHeader)},
8492 {&handle_local_show_tunnel, NULL,
8493 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8494 sizeof (struct GNUNET_MESH_LocalMonitor)},
8500 * To be called on core init/fail.
8502 * @param cls service closure
8503 * @param server handle to the server for this service
8504 * @param identity the public identity of this peer
8507 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8508 const struct GNUNET_PeerIdentity *identity)
8511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8512 core_handle = server;
8513 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8519 GNUNET_i2s (identity));
8520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8522 GNUNET_i2s (&my_full_id));
8523 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8525 GNUNET_abort(); // Try harder
8532 * Method called whenever a given peer connects.
8534 * @param cls closure
8535 * @param peer peer identity this notification is about
8536 * @param atsi performance data for the connection
8537 * @param atsi_count number of records in 'atsi'
8540 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8541 const struct GNUNET_ATS_Information *atsi,
8542 unsigned int atsi_count)
8544 struct MeshPeerInfo *peer_info;
8545 struct MeshPeerPath *path;
8547 DEBUG_CONN ("Peer connected\n");
8548 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8549 peer_info = peer_info_get (peer);
8550 if (myid == peer_info->id)
8552 DEBUG_CONN (" (self)\n");
8557 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8559 path = path_new (2);
8560 path->peers[0] = myid;
8561 path->peers[1] = peer_info->id;
8562 GNUNET_PEER_change_rc (myid, 1);
8563 GNUNET_PEER_change_rc (peer_info->id, 1);
8564 peer_info_add_path (peer_info, path, GNUNET_YES);
8565 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8571 * Method called whenever a peer disconnects.
8573 * @param cls closure
8574 * @param peer peer identity this notification is about
8577 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8579 struct MeshPeerInfo *pi;
8580 struct MeshPeerQueue *q;
8581 struct MeshPeerQueue *n;
8583 DEBUG_CONN ("Peer disconnected\n");
8584 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8594 /* TODO try to reroute this traffic instead */
8595 queue_destroy(q, GNUNET_YES);
8598 if (NULL != pi->core_transmit)
8600 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8601 pi->core_transmit = NULL;
8603 peer_info_remove_path (pi, pi->id, myid);
8606 DEBUG_CONN (" (self)\n");
8608 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8613 /******************************************************************************/
8614 /************************ MAIN FUNCTIONS ****************************/
8615 /******************************************************************************/
8618 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8620 * @param cls closure
8621 * @param key current key code
8622 * @param value value in the hash map
8623 * @return GNUNET_YES if we should continue to iterate,
8627 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8629 struct MeshTunnel *t = value;
8636 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8638 * @param cls closure
8639 * @param key current key code
8640 * @param value value in the hash map
8641 * @return GNUNET_YES if we should continue to iterate,
8645 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8647 struct MeshPeerInfo *p = value;
8648 struct MeshPeerQueue *q;
8649 struct MeshPeerQueue *n;
8657 queue_destroy(q, GNUNET_YES);
8661 peer_info_destroy (p);
8667 * Task run during shutdown.
8673 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8677 if (core_handle != NULL)
8679 GNUNET_CORE_disconnect (core_handle);
8684 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8687 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8688 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8689 if (dht_handle != NULL)
8691 GNUNET_DHT_disconnect (dht_handle);
8696 GNUNET_SERVER_notification_context_destroy (nc);
8699 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8701 GNUNET_SCHEDULER_cancel (announce_id_task);
8702 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8704 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8706 GNUNET_SCHEDULER_cancel (announce_applications_task);
8707 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8714 * Callback for hostkey read/generation
8716 * @param cls Closure (Configuration handle).
8717 * @param pk the private key
8718 * @param emsg error message
8721 key_generation_cb (void *cls,
8722 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8725 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8726 struct MeshPeerInfo *peer;
8727 struct MeshPeerPath *p;
8732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8733 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8735 GNUNET_SCHEDULER_shutdown ();
8738 my_private_key = pk;
8739 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8740 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8741 &my_full_id.hashPubKey);
8742 myid = GNUNET_PEER_intern (&my_full_id);
8743 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8744 "Mesh for peer [%s] starting\n",
8745 GNUNET_i2s(&my_full_id));
8747 // transport_handle = GNUNET_TRANSPORT_connect(c,
8754 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8755 NULL, /* Closure passed to MESH functions */
8756 &core_init, /* Call core_init once connected */
8757 &core_connect, /* Handle connects */
8758 &core_disconnect, /* remove peers on disconnects */
8759 NULL, /* Don't notify about all incoming messages */
8760 GNUNET_NO, /* For header only in notification */
8761 NULL, /* Don't notify about all outbound messages */
8762 GNUNET_NO, /* For header-only out notification */
8763 core_handlers); /* Register these handlers */
8765 if (core_handle == NULL)
8768 GNUNET_SCHEDULER_shutdown ();
8773 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8776 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8777 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8778 GNUNET_SERVER_disconnect_notify (server_handle,
8779 &handle_local_client_disconnect, NULL);
8783 clients_tail = NULL;
8786 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8787 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8789 /* Create a peer_info for the local peer */
8790 peer = peer_info_get (&my_full_id);
8793 GNUNET_PEER_change_rc (myid, 1);
8794 peer_info_add_path (peer, p, GNUNET_YES);
8795 GNUNET_SERVER_resume (server_handle);
8796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8801 * Process mesh requests.
8803 * @param cls closure
8804 * @param server the initialized server
8805 * @param c configuration to use
8808 run (void *cls, struct GNUNET_SERVER_Handle *server,
8809 const struct GNUNET_CONFIGURATION_Handle *c)
8813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8814 server_handle = server;
8817 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8822 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8824 GNUNET_SCHEDULER_shutdown ();
8829 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8830 &refresh_path_time))
8832 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8834 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8835 "mesh", "refresh path time");
8836 GNUNET_SCHEDULER_shutdown ();
8841 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8842 &app_announce_time))
8844 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8846 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8847 "mesh", "app announce time");
8848 GNUNET_SCHEDULER_shutdown ();
8851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8852 "APP_ANNOUNCE_TIME %llu ms\n",
8853 app_announce_time.rel_value);
8855 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8858 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8860 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8861 "mesh", "id announce time");
8862 GNUNET_SCHEDULER_shutdown ();
8867 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8870 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8872 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8873 "mesh", "connect timeout");
8874 GNUNET_SCHEDULER_shutdown ();
8879 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8882 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8884 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8885 "mesh", "max msgs queue");
8886 GNUNET_SCHEDULER_shutdown ();
8891 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8894 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8896 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8897 "mesh", "max tunnels");
8898 GNUNET_SCHEDULER_shutdown ();
8903 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8906 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8908 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8909 "mesh", "default ttl", 64);
8914 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8917 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8918 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8919 "mesh", "max peers", 1000);
8924 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8925 &dht_replication_level))
8927 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8929 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8930 "mesh", "dht replication level", 3);
8931 dht_replication_level = 3;
8934 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8935 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8936 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8937 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8938 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8940 dht_handle = GNUNET_DHT_connect (c, 64);
8941 if (NULL == dht_handle)
8945 stats = GNUNET_STATISTICS_create ("mesh", c);
8947 GNUNET_SERVER_suspend (server_handle);
8948 /* Scheduled the task to clean up when shutdown is called */
8949 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8951 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
8954 GNUNET_free (keyfile);
8959 * The main function for the mesh service.
8961 * @param argc number of arguments from the command line
8962 * @param argv command line arguments
8963 * @return 0 ok, 1 on error
8966 main (int argc, char *const *argv)
8971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8972 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8974 ret = (GNUNET_OK == r) ? 0 : 1;
8975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8979 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8980 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8981 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);