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;
752 * Tmp store for partially retrieved regex.
760 * Struct to keep information of searches of services described by a regex
761 * using a user-provided string service description.
763 struct MeshRegexSearchInfo
766 * Which tunnel is this for
768 struct MeshTunnel *t;
771 * User provided description of the searched service.
776 * Part of the description already consumed by the search.
783 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
786 * Results from running DHT GETs.
788 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
791 * Contexts, for each running DHT GET. Free all on end of search.
793 struct MeshRegexSearchContext **contexts;
796 * Number of contexts (branches/steps in search).
798 unsigned int n_contexts;
801 * Peer that is connecting via connect_by_string. When connected, free ctx.
806 * Other peers that are found but not yet being connected to.
808 GNUNET_PEER_Id *peers;
811 * Number of elements in peers.
813 unsigned int n_peers;
816 * Next peer to try to connect to.
821 * Timeout for a connect attempt.
822 * When reached, try to connect to a different peer, if any. If not,
823 * try the same peer again.
825 GNUNET_SCHEDULER_TaskIdentifier timeout;
830 * Struct to keep state of running searches that have consumed a part of
833 struct MeshRegexSearchContext
836 * Part of the description already consumed by
837 * this particular search branch.
842 * Information about the search.
844 struct MeshRegexSearchInfo *info;
847 * We just want to look for one edge, the longer the better.
850 unsigned int longest_match;
853 * Destination hash of the longest match.
855 struct GNUNET_HashCode hash;
858 /******************************************************************************/
859 /************************ DEBUG FUNCTIONS ****************************/
860 /******************************************************************************/
864 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
865 * @param cls string to print
866 * @param success GNUNET_OK if the PUT was transmitted,
867 * GNUNET_NO on timeout,
868 * GNUNET_SYSERR on disconnect from service
869 * after the PUT message was transmitted
870 * (so we don't know if it was received or not)
875 mesh_debug (void *cls, int success)
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
883 unsigned int debug_fwd_ack;
884 unsigned int debug_bck_ack;
888 /******************************************************************************/
889 /*********************** GLOBAL VARIABLES ****************************/
890 /******************************************************************************/
892 /************************** Configuration parameters **************************/
895 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
897 static struct GNUNET_TIME_Relative refresh_path_time;
900 * How often to PUT local application numbers in the DHT.
902 static struct GNUNET_TIME_Relative app_announce_time;
905 * How often to PUT own ID in the DHT.
907 static struct GNUNET_TIME_Relative id_announce_time;
910 * Maximum time allowed to connect to a peer found by string.
912 static struct GNUNET_TIME_Relative connect_timeout;
915 * Default TTL for payload packets.
917 static unsigned long long default_ttl;
920 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
922 static unsigned long long dht_replication_level;
925 * How many tunnels are we willing to maintain.
926 * Local tunnels are always allowed, even if there are more tunnels than max.
928 static unsigned long long max_tunnels;
931 * How many messages *in total* are we willing to queue, divided by number of
932 * tunnels to get tunnel queue size.
934 static unsigned long long max_msgs_queue;
937 * How many peers do we want to remember?
939 static unsigned long long max_peers;
942 /*************************** Static global variables **************************/
945 * Hostkey generation context
947 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
950 * DLL with all the clients, head.
952 static struct MeshClient *clients;
955 * DLL with all the clients, tail.
957 static struct MeshClient *clients_tail;
960 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
962 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
965 * Number of tunnels known.
967 static unsigned long long n_tunnels;
970 * Tunnels incoming, indexed by MESH_TunnelNumber
971 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
973 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
976 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
978 static struct GNUNET_CONTAINER_MultiHashMap *peers;
981 * Handle to communicate with transport
983 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
986 * Handle to communicate with core.
988 static struct GNUNET_CORE_Handle *core_handle;
993 static struct GNUNET_DHT_Handle *dht_handle;
998 static struct GNUNET_SERVER_Handle *server_handle;
1001 * Handle to the statistics service.
1003 static struct GNUNET_STATISTICS_Handle *stats;
1006 * Notification context, to send messages to local clients.
1008 static struct GNUNET_SERVER_NotificationContext *nc;
1011 * Local peer own ID (memory efficient handle).
1013 static GNUNET_PEER_Id myid;
1016 * Local peer own ID (full value).
1018 static struct GNUNET_PeerIdentity my_full_id;
1023 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1028 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1031 * Tunnel ID for the next created tunnel (global tunnel number).
1033 static MESH_TunnelNumber next_tid;
1036 * Tunnel ID for the next incoming tunnel (local tunnel number).
1038 static MESH_TunnelNumber next_local_tid;
1041 * All application types provided by this peer.
1043 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1046 * All message types clients of this peer are interested in.
1048 static struct GNUNET_CONTAINER_MultiHashMap *types;
1051 * Task to periodically announce provided applications.
1053 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1056 * Task to periodically announce itself in the network.
1058 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1061 * Next ID to assign to a client.
1063 unsigned int next_client_id;
1066 /******************************************************************************/
1067 /*********************** DECLARATIONS **************************/
1068 /******************************************************************************/
1071 * Function to process paths received for a new peer addition. The recorded
1072 * paths form the initial tunnel, which can be optimized later.
1073 * Called on each result obtained for the DHT search.
1075 * @param cls closure
1076 * @param exp when will this value expire
1077 * @param key key of the result
1078 * @param type type of the result
1079 * @param size number of bytes in data
1080 * @param data pointer to the result data
1083 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1084 const struct GNUNET_HashCode * key,
1085 const struct GNUNET_PeerIdentity *get_path,
1086 unsigned int get_path_length,
1087 const struct GNUNET_PeerIdentity *put_path,
1088 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1089 size_t size, const void *data);
1093 * Function to process DHT string to regex matching.
1094 * Called on each result obtained for the DHT search.
1096 * @param cls closure (search context)
1097 * @param exp when will this value expire
1098 * @param key key of the result
1099 * @param get_path path of the get request (not used)
1100 * @param get_path_length lenght of get_path (not used)
1101 * @param put_path path of the put request (not used)
1102 * @param put_path_length length of the put_path (not used)
1103 * @param type type of the result
1104 * @param size number of bytes in data
1105 * @param data pointer to the result data
1107 * TODO: re-issue the request after certain time? cancel after X results?
1110 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1111 const struct GNUNET_HashCode * key,
1112 const struct GNUNET_PeerIdentity *get_path,
1113 unsigned int get_path_length,
1114 const struct GNUNET_PeerIdentity *put_path,
1115 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1116 size_t size, const void *data);
1120 * Function to process DHT string to regex matching.
1121 * Called on each result obtained for the DHT search.
1123 * @param cls closure (search context)
1124 * @param exp when will this value expire
1125 * @param key key of the result
1126 * @param get_path path of the get request (not used)
1127 * @param get_path_length lenght of get_path (not used)
1128 * @param put_path path of the put request (not used)
1129 * @param put_path_length length of the put_path (not used)
1130 * @param type type of the result
1131 * @param size number of bytes in data
1132 * @param data pointer to the result data
1135 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1136 const struct GNUNET_HashCode * key,
1137 const struct GNUNET_PeerIdentity *get_path,
1138 unsigned int get_path_length,
1139 const struct GNUNET_PeerIdentity *put_path,
1140 unsigned int put_path_length,
1141 enum GNUNET_BLOCK_Type type,
1142 size_t size, const void *data);
1146 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1147 * and insert it in the appropiate structures if the peer is not known yet.
1149 * @param peer Short identity of the peer.
1151 * @return Existing or newly created peer info.
1153 static struct MeshPeerInfo *
1154 peer_info_get_short (const GNUNET_PEER_Id peer);
1158 * Try to establish a new connection to this peer.
1159 * Use the best path for the given tunnel.
1160 * If the peer doesn't have any path to it yet, try to get one.
1161 * If the peer already has some path, send a CREATE PATH towards it.
1163 * @param peer PeerInfo of the peer.
1164 * @param t Tunnel for which to create the path, if possible.
1167 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1171 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1172 * needed rescources.
1173 * If peer already exists, reevaluate shortest path and change if different.
1175 * @param t Tunnel we want to add a new peer to
1176 * @param peer PeerInfo of the peer being added
1180 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1184 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1185 * that are no longer needed, as well as nodes of no longer reachable peers.
1186 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1188 * @param t Tunnel from which to remove the path.
1189 * @param peer Short id of the peer which should be removed.
1192 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1196 * Search for a tunnel by global ID using full PeerIdentities.
1198 * @param oid owner of the tunnel.
1199 * @param tid global tunnel number.
1201 * @return tunnel handler, NULL if doesn't exist.
1203 static struct MeshTunnel *
1204 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1208 * Delete an active client from the tunnel.
1214 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1217 * Notify a tunnel that a connection has broken that affects at least
1218 * some of its peers.
1220 * @param t Tunnel affected.
1221 * @param p1 Peer that got disconnected from p2.
1222 * @param p2 Peer that got disconnected from p1.
1224 * @return Short ID of the peer disconnected (either p1 or p2).
1225 * 0 if the tunnel remained unaffected.
1227 static GNUNET_PEER_Id
1228 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1233 * Get the current ack value for a tunnel, for data going from root to leaves,
1234 * taking in account the tunnel mode and the status of all children and clients.
1238 * @return Maximum PID allowed.
1241 tunnel_get_fwd_ack (struct MeshTunnel *t);
1245 * Add a client to a tunnel, initializing all needed data structures.
1247 * @param t Tunnel to which add the client.
1248 * @param c Client which to add to the tunnel.
1251 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1255 * Jump to the next edge, with the longest matching token.
1257 * @param block Block found in the DHT.
1258 * @param size Size of the block.
1259 * @param ctx Context of the search.
1261 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1264 regex_next_edge (const struct MeshRegexBlock *block,
1266 struct MeshRegexSearchContext *ctx);
1270 * Find a path to a peer that offers a regex servcie compatible
1271 * with a given string.
1273 * @param key The key of the accepting state.
1274 * @param ctx Context containing info about the string, tunnel, etc.
1277 regex_find_path (const struct GNUNET_HashCode *key,
1278 struct MeshRegexSearchContext *ctx);
1282 * @brief Queue and pass message to core when possible.
1284 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1285 * and accounts for it. In case the queue is full, the message is dropped and
1288 * Otherwise, message is treated as internal and allowed to go regardless of
1291 * @param cls Closure (@c type dependant). It will be used by queue_send to
1292 * build the message to be sent if not already prebuilt.
1293 * @param type Type of the message, 0 for a raw message.
1294 * @param size Size of the message.
1295 * @param dst Neighbor to send message to.
1296 * @param t Tunnel this message belongs to.
1299 queue_add (void *cls, uint16_t type, size_t size,
1300 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1304 * Free a transmission that was already queued with all resources
1305 * associated to the request.
1307 * @param queue Queue handler to cancel.
1308 * @param clear_cls Is it necessary to free associated cls?
1311 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1315 * @brief Get the next transmittable message from the queue.
1317 * This will be the head, except in the case of being a data packet
1318 * not allowed by the destination peer.
1320 * @param peer Destination peer.
1322 * @return The next viable MeshPeerQueue element to send to that peer.
1323 * NULL when there are no transmittable messages.
1325 struct MeshPeerQueue *
1326 queue_get_next (const struct MeshPeerInfo *peer);
1330 * Core callback to write a queued packet to core buffer
1332 * @param cls Closure (peer info).
1333 * @param size Number of bytes available in buf.
1334 * @param buf Where the to write the message.
1336 * @return number of bytes written to buf
1339 queue_send (void *cls, size_t size, void *buf);
1341 /******************************************************************************/
1342 /************************ ITERATORS ****************************/
1343 /******************************************************************************/
1346 * Iterator over found existing mesh regex blocks that match an ongoing search.
1348 * @param cls closure
1349 * @param key current key code
1350 * @param value value in the hash map
1351 * @return GNUNET_YES if we should continue to iterate,
1355 regex_result_iterator (void *cls,
1356 const struct GNUNET_HashCode * key,
1359 struct MeshRegexBlock *block = value;
1360 struct MeshRegexSearchContext *ctx = cls;
1362 if (GNUNET_YES == ntohl(block->accepting) &&
1363 ctx->position == strlen (ctx->info->description))
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1366 regex_find_path (key, ctx);
1367 return GNUNET_YES; // We found an accept state!
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1372 ctx->position, strlen(ctx->info->description),
1373 ntohl(block->accepting));
1376 regex_next_edge(block, SIZE_MAX, ctx);
1378 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1385 * Iterator over edges in a regex block retrieved from the DHT.
1387 * @param cls Closure (context of the search).
1388 * @param token Token that follows to next state.
1389 * @param len Lenght of token.
1390 * @param key Hash of next state.
1392 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1395 regex_edge_iterator (void *cls,
1398 const struct GNUNET_HashCode *key)
1400 struct MeshRegexSearchContext *ctx = cls;
1401 struct MeshRegexSearchInfo *info = ctx->info;
1405 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1410 current = &info->description[ctx->position];
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1412 current_len = strlen (info->description) - ctx->position;
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* token : %.*s\n", len, token);
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1417 if (len > current_len)
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1420 return GNUNET_YES; // Token too long, wont match
1422 if (0 != strncmp (current, token, len))
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1425 return GNUNET_YES; // Token doesn't match
1428 if (len > ctx->longest_match)
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1431 ctx->longest_match = len;
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1445 * Jump to the next edge, with the longest matching token.
1447 * @param block Block found in the DHT.
1448 * @param size Size of the block.
1449 * @param ctx Context of the search.
1451 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1454 regex_next_edge (const struct MeshRegexBlock *block,
1456 struct MeshRegexSearchContext *ctx)
1458 struct MeshRegexSearchContext *new_ctx;
1459 struct MeshRegexSearchInfo *info = ctx->info;
1460 struct GNUNET_DHT_GetHandle *get_h;
1464 /* Find the longest match for the current string position,
1465 * among tokens in the given block */
1466 ctx->longest_match = 0;
1467 result = GNUNET_MESH_regex_block_iterate (block, size,
1468 ®ex_edge_iterator, ctx);
1469 GNUNET_break (GNUNET_OK == result);
1471 /* Did anything match? */
1472 if (0 == ctx->longest_match)
1475 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1476 new_ctx->info = info;
1477 new_ctx->position = ctx->position + ctx->longest_match;
1478 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1480 /* Check whether we already have a DHT GET running for it */
1482 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1485 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1487 ®ex_result_iterator,
1489 // FIXME: "leaks" new_ctx? avoid keeping it around?
1490 return; // We are already looking for it
1493 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1495 /* Start search in DHT */
1496 rest = &new_ctx->info->description[new_ctx->position];
1498 GNUNET_DHT_get_start (dht_handle, /* handle */
1499 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1500 &ctx->hash, /* key to search */
1501 dht_replication_level, /* replication level */
1502 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1504 // FIXME add BLOOMFILTER to exclude filtered peers
1505 strlen(rest) + 1, /* xquery bits */
1506 // FIXME add BLOOMFILTER SIZE
1507 &dht_get_string_handler, new_ctx);
1509 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1512 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1521 * Iterator over hash map entries to cancel DHT GET requests after a
1522 * successful connect_by_string.
1524 * @param cls Closure (unused).
1525 * @param key Current key code (unused).
1526 * @param value Value in the hash map (get handle).
1527 * @return GNUNET_YES if we should continue to iterate,
1531 regex_cancel_dht_get (void *cls,
1532 const struct GNUNET_HashCode * key,
1535 struct GNUNET_DHT_GetHandle *h = value;
1537 GNUNET_DHT_get_stop (h);
1543 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1544 * search for connect_by_string.
1546 * @param cls Closure (unused).
1547 * @param key Current key code (unused).
1548 * @param value MeshRegexBlock in the hash map.
1549 * @return GNUNET_YES if we should continue to iterate,
1553 regex_free_result (void *cls,
1554 const struct GNUNET_HashCode * key,
1558 GNUNET_free (value);
1564 * Find a path to a peer that offers a regex servcie compatible
1565 * with a given string.
1567 * @param key The key of the accepting state.
1568 * @param ctx Context containing info about the string, tunnel, etc.
1571 regex_find_path (const struct GNUNET_HashCode *key,
1572 struct MeshRegexSearchContext *ctx)
1574 struct GNUNET_DHT_GetHandle *get_h;
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1577 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1578 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1579 key, /* key to search */
1580 dht_replication_level, /* replication level */
1581 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE/* |
1582 GNUNET_DHT_RO_RECORD_ROUTE*/,
1584 // FIXME add BLOOMFILTER to exclude filtered peers
1585 0, /* xquery bits */
1586 // FIXME add BLOOMFILTER SIZE
1587 &dht_get_string_accept_handler, ctx);
1588 GNUNET_break (GNUNET_OK ==
1589 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1592 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1597 * Function called if the connect attempt to a peer found via
1598 * connect_by_string times out. Try to connect to another peer, if any.
1599 * Otherwise try to reconnect to the same peer.
1601 * @param cls Closure (info about regex search).
1602 * @param tc TaskContext.
1605 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1607 struct MeshRegexSearchInfo *info = cls;
1608 struct MeshPeerInfo *peer_info;
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1613 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1614 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1622 if (0 < info->n_peers)
1624 // Select next peer, put current in that spot.
1625 id = info->peers[info->i_peer];
1626 info->peers[info->i_peer] = info->peer;
1627 info->i_peer = (info->i_peer + 1) % info->n_peers;
1631 // Try to connect to same peer again.
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1636 peer_info = peer_info_get_short(id);
1637 tunnel_add_peer (info->t, peer_info);
1639 tunnel_delete_peer (info->t, old);
1640 peer_info_connect (peer_info, info->t);
1641 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1642 ®ex_connect_timeout,
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1649 * Cancel an ongoing regex search in the DHT and free all resources.
1651 * @param ctx The search context.
1654 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1656 struct MeshRegexSearchInfo *info = ctx->info;
1659 GNUNET_free (info->description);
1660 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1661 ®ex_cancel_dht_get, NULL);
1662 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1663 ®ex_free_result, NULL);
1664 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1665 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1666 info->t->regex_ctx = NULL;
1667 for (i = 0; i < info->n_contexts; i++)
1669 GNUNET_free (info->contexts[i]);
1671 if (0 < info->n_contexts)
1672 GNUNET_free (info->contexts);
1673 if (0 < info->n_peers)
1674 GNUNET_free (info->peers);
1675 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1677 GNUNET_SCHEDULER_cancel(info->timeout);
1683 /******************************************************************************/
1684 /************************ PERIODIC FUNCTIONS ****************************/
1685 /******************************************************************************/
1688 * Announce iterator over for each application provided by the peer
1690 * @param cls closure
1691 * @param key current key code
1692 * @param value value in the hash map
1693 * @return GNUNET_YES if we should continue to
1698 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1700 struct PBlock block;
1701 struct MeshClient *c;
1703 block.id = my_full_id;
1704 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1705 GNUNET_assert(NULL != c);
1706 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1707 if (0 == block.type)
1712 block.type = htonl (block.type);
1714 GNUNET_break (NULL !=
1715 GNUNET_DHT_put (dht_handle, key,
1716 dht_replication_level,
1717 GNUNET_DHT_RO_RECORD_ROUTE |
1718 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1719 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1721 (const char *) &block,
1722 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1723 app_announce_time, NULL, NULL));
1730 * Function called when the DHT regex put is complete.
1732 * @param the 'struct MeshClient' for which we were PUTting
1733 * @param success GNUNET_OK if the PUT was transmitted,
1734 * GNUNET_NO on timeout,
1735 * GNUNET_SYSERR on disconnect from service
1736 * after the PUT message was transmitted
1737 * (so we don't know if it was received or not)
1740 announce_regex_done (void *cls,
1743 struct MeshClient *c = cls;
1750 * Regex callback iterator to store own service description in the DHT.
1752 * @param cls closure.
1753 * @param key hash for current state.
1754 * @param proof proof for current state.
1755 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1756 * @param num_edges number of edges leaving current state.
1757 * @param edges edges leaving current state.
1760 regex_iterator (void *cls,
1761 const struct GNUNET_HashCode *key,
1764 unsigned int num_edges,
1765 const struct GNUNET_REGEX_Edge *edges)
1767 struct MeshRegexBlock *block;
1768 struct MeshRegexEdge *block_edge;
1769 enum GNUNET_DHT_RouteOption opt;
1773 unsigned int offset;
1776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1777 " regex dht put for state %s\n",
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1787 if (GNUNET_YES == accepting)
1789 struct MeshRegexAccept block;
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1792 " state %s is accepting, putting own id\n",
1794 size = sizeof (block);
1796 block.id = my_full_id;
1797 GNUNET_STATISTICS_update (stats, "# regex accepting blocks stored",
1799 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes stored",
1800 sizeof (block), GNUNET_NO);
1802 GNUNET_DHT_put(dht_handle, key,
1803 dht_replication_level,
1804 opt/* | GNUNET_DHT_RO_RECORD_ROUTE*/,
1805 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1808 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: expiration time should be option */
1812 len = strlen(proof);
1813 size = sizeof (struct MeshRegexBlock) + len;
1814 block = GNUNET_malloc (size);
1817 block->n_proof = htonl (len);
1818 block->n_edges = htonl (num_edges);
1819 block->accepting = htonl (accepting);
1821 /* Store the proof at the end of the block. */
1822 aux = (char *) &block[1];
1823 memcpy (aux, proof, len);
1826 /* Store each edge in a variable length MeshEdge struct at the
1827 * very end of the MeshRegexBlock structure.
1829 for (i = 0; i < num_edges; i++)
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832 " edge %s towards %s\n",
1834 GNUNET_h2s(&edges[i].destination));
1836 /* aux points at the end of the last block */
1837 len = strlen (edges[i].label);
1838 size += sizeof (struct MeshRegexEdge) + len;
1839 // Calculate offset FIXME is this ok? use size instead?
1840 offset = aux - (char *) block;
1841 block = GNUNET_realloc (block, size);
1842 aux = &((char *) block)[offset];
1843 block_edge = (struct MeshRegexEdge *) aux;
1844 block_edge->key = edges[i].destination;
1845 block_edge->n_token = htonl (len);
1846 aux = (char *) &block_edge[1];
1847 memcpy (aux, edges[i].label, len);
1851 GNUNET_DHT_put(dht_handle, key,
1852 dht_replication_level,
1854 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1856 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1859 GNUNET_STATISTICS_update (stats, "# regex blocks stored",
1861 GNUNET_STATISTICS_update (stats, "# regex block bytes stored",
1864 GNUNET_free (block);
1869 * Store the regular expression describing a local service into the DHT.
1871 * @param regex The regular expresion.
1874 regex_put (struct MeshRegexDescriptor *regex)
1876 if (NULL == regex->dfa)
1878 regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1879 strlen (regex->regex),
1880 regex->compression);
1883 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1884 GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL);
1885 DEBUG_DHT (" regex_put (%s) end\n", regex->regex);
1890 * Periodically announce what applications are provided by local clients
1893 * @param cls closure
1894 * @param tc task context
1897 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1899 struct MeshClient *c = cls;
1902 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1903 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1905 DEBUG_DHT ("Starting PUT for regex\n");
1906 for (i = 0; i < c->n_regex; i++)
1907 regex_put (&c->regexes[i]);
1908 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1911 DEBUG_DHT ("Finished PUT for regex\n");
1916 * Periodically announce what applications are provided by local clients
1919 * @param cls closure
1920 * @param tc task context
1923 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1925 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1927 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1931 DEBUG_DHT ("Starting PUT for apps\n");
1933 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1935 announce_applications_task =
1936 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1938 DEBUG_DHT ("Finished PUT for apps\n");
1943 * Periodically announce self id in the DHT
1945 * @param cls closure
1946 * @param tc task context
1949 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1951 struct PBlock block;
1953 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1955 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1959 * - Set data expiration in function of X
1960 * - Adapt X to churn
1962 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1964 block.id = my_full_id;
1965 block.type = htonl (0);
1966 GNUNET_DHT_put (dht_handle, /* DHT handle */
1967 &my_full_id.hashPubKey, /* Key to use */
1968 dht_replication_level, /* Replication level */
1969 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1970 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1971 sizeof (block), /* Size of the data */
1972 (const char *) &block, /* Data itself */
1973 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1974 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1975 NULL, /* Continuation */
1976 NULL); /* Continuation closure */
1978 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1982 /******************************************************************************/
1983 /****************** GENERAL HELPER FUNCTIONS ************************/
1984 /******************************************************************************/
1987 * Decrements the reference counter and frees all resources if needed
1989 * @param mesh_data Data Descriptor used in a multicast message.
1990 * Freed no longer needed (last message).
1993 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1995 if (0 == --(mesh_data->reference_counter))
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1998 GNUNET_free (mesh_data->data);
1999 GNUNET_free (mesh_data);
2005 * Check if client has registered with the service and has not disconnected
2007 * @param client the client to check
2009 * @return non-NULL if client exists in the global DLL
2011 static struct MeshClient *
2012 client_get (struct GNUNET_SERVER_Client *client)
2014 struct MeshClient *c;
2019 if (c->handle == client)
2028 * Checks if a given client has subscribed to certain message type
2030 * @param message_type Type of message to check
2031 * @param c Client to check
2033 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
2035 * FIXME: use of crypto_hash slows it down
2036 * The hash function alone takes 8-10us out of the ~55us for the whole
2037 * process of retransmitting the message from one local client to another.
2038 * Find faster implementation!
2041 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
2043 struct GNUNET_HashCode hc;
2045 if (NULL == c->types)
2048 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
2049 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2054 * Check whether client wants traffic from a tunnel.
2056 * @param c Client to check.
2057 * @param t Tunnel to be found.
2059 * @return GNUNET_YES if client knows tunnel.
2061 * TODO look in client hashmap
2064 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2068 for (i = 0; i < t->nclients; i++)
2069 if (t->clients[i] == c)
2076 * Check whether client has been informed about a tunnel.
2078 * @param c Client to check.
2079 * @param t Tunnel to be found.
2081 * @return GNUNET_YES if client knows tunnel.
2083 * TODO look in client hashmap
2086 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2090 for (i = 0; i < t->nignore; i++)
2091 if (t->ignore[i] == c)
2093 return client_wants_tunnel(c, t);
2098 * Marks a client as uninterested in traffic from the tunnel, updating both
2099 * client and tunnel to reflect this.
2101 * @param c Client that doesn't want traffic anymore.
2102 * @param t Tunnel which should be ignored.
2104 * FIXME when to delete an incoming tunnel?
2107 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2109 struct GNUNET_HashCode hash;
2111 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2112 GNUNET_break (GNUNET_YES ==
2113 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2115 GNUNET_break (GNUNET_YES ==
2116 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2117 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2118 tunnel_delete_active_client (t, c);
2119 GNUNET_array_append (t->ignore, t->nignore, c);
2124 * Deletes a tunnel from a client (either owner or destination). To be used on
2125 * tunnel destroy, otherwise, use client_ignore_tunnel.
2127 * @param c Client whose tunnel to delete.
2128 * @param t Tunnel which should be deleted.
2131 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2133 struct GNUNET_HashCode hash;
2137 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2138 GNUNET_assert (GNUNET_YES ==
2139 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2145 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2147 GNUNET_assert (GNUNET_YES ==
2148 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2152 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2160 * Send the message to all clients that have subscribed to its type
2162 * @param msg Pointer to the message itself
2163 * @param payload Pointer to the payload of the message.
2164 * @param t The tunnel to whose clients this message goes.
2166 * @return number of clients this message was sent to
2169 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2170 const struct GNUNET_MessageHeader *payload,
2171 struct MeshTunnel *t)
2173 struct MeshClient *c;
2174 MESH_TunnelNumber *tid;
2177 char cbuf[htons (msg->size)];
2179 type = ntohs (payload->type);
2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2182 GNUNET_MESH_DEBUG_M2S (type));
2184 memcpy (cbuf, msg, sizeof (cbuf));
2185 switch (htons (msg->type))
2187 struct GNUNET_MESH_Unicast *uc;
2188 struct GNUNET_MESH_Multicast *mc;
2189 struct GNUNET_MESH_ToOrigin *to;
2191 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2192 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2195 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2196 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2199 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2200 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2208 for (count = 0, c = clients; c != NULL; c = c->next)
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2211 if (client_is_subscribed (type, c))
2213 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2217 *tid = htonl (t->local_tid);
2221 if (GNUNET_NO == client_knows_tunnel (c, t))
2223 /* This client doesn't know the tunnel */
2224 struct GNUNET_MESH_TunnelNotification tmsg;
2225 struct GNUNET_HashCode hash;
2227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2228 tmsg.header.size = htons (sizeof (tmsg));
2229 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2230 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2231 tmsg.tunnel_id = htonl (t->local_tid_dest);
2233 if (GNUNET_YES == t->speed_min)
2234 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2235 if (GNUNET_YES == t->nobuffer)
2236 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2237 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2238 &tmsg.header, GNUNET_NO);
2239 tunnel_add_client (t, c);
2240 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2242 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2243 c->incoming_tunnels, &hash, t,
2244 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2246 *tid = htonl (t->local_tid_dest);
2249 /* Check if the client wants to get traffic from the tunnel */
2250 if (GNUNET_NO == client_wants_tunnel(c, t))
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2254 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2255 (struct GNUNET_MessageHeader
2256 *) cbuf, GNUNET_NO);
2265 * Notify the client that owns the tunnel that a peer has connected to it
2266 * (the requested path to it has been confirmed).
2268 * @param t Tunnel whose owner to notify
2269 * @param id Short id of the peer that has connected
2272 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2274 struct GNUNET_MESH_PeerControl pc;
2276 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2277 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2278 pc.tunnel_id = htonl (t->local_tid);
2279 GNUNET_PEER_resolve (id, &pc.peer);
2280 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2286 * Notify all clients (not depending on registration status) that the incoming
2287 * tunnel is no longer valid.
2289 * @param t Tunnel that was destroyed.
2292 send_clients_tunnel_destroy (struct MeshTunnel *t)
2294 struct GNUNET_MESH_TunnelMessage msg;
2296 msg.header.size = htons (sizeof (msg));
2297 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2298 msg.tunnel_id = htonl (t->local_tid_dest);
2299 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2304 * Notify clients of tunnel disconnections, if needed.
2305 * In case the origin disconnects, the destination clients get a tunnel destroy
2306 * notification. If the last destination disconnects (only one remaining client
2307 * in tunnel), the origin gets a (local ID) peer disconnected.
2308 * Note that the function must be called BEFORE removing the client from
2311 * @param t Tunnel that was destroyed.
2312 * @param c Client that disconnected.
2315 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2321 struct GNUNET_MESH_TunnelMessage msg;
2323 msg.header.size = htons (sizeof (msg));
2324 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2325 msg.tunnel_id = htonl (t->local_tid_dest);
2326 for (i = 0; i < t->nclients; i++)
2327 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2328 &msg.header, GNUNET_NO);
2330 // FIXME when to disconnect an incoming tunnel?
2331 else if (1 == t->nclients && NULL != t->owner)
2333 struct GNUNET_MESH_PeerControl msg;
2335 msg.header.size = htons (sizeof (msg));
2336 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2337 msg.tunnel_id = htonl (t->local_tid);
2338 msg.peer = my_full_id;
2339 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2340 &msg.header, GNUNET_NO);
2346 * Iterator over all the peers to remove the oldest not-used entry.
2348 * @param cls Closure (unsued).
2349 * @param key ID of the peer.
2350 * @param value Peer_Info of the peer.
2355 peer_info_timeout (void *cls,
2356 const struct GNUNET_HashCode *key,
2363 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2364 * and insert it in the appropiate structures if the peer is not known yet.
2366 * @param peer Full identity of the peer.
2368 * @return Existing or newly created peer info.
2370 static struct MeshPeerInfo *
2371 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2373 struct MeshPeerInfo *peer_info;
2375 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2376 if (NULL == peer_info)
2379 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2380 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2382 GNUNET_CONTAINER_multihashmap_iterate (peers,
2386 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2388 peer_info->id = GNUNET_PEER_intern (peer);
2390 peer_info->last_contact = GNUNET_TIME_absolute_get();
2397 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2398 * and insert it in the appropiate structures if the peer is not known yet.
2400 * @param peer Short identity of the peer.
2402 * @return Existing or newly created peer info.
2404 static struct MeshPeerInfo *
2405 peer_info_get_short (const GNUNET_PEER_Id peer)
2407 struct GNUNET_PeerIdentity id;
2409 GNUNET_PEER_resolve (peer, &id);
2410 return peer_info_get (&id);
2415 * Iterator to remove the tunnel from the list of tunnels a peer participates
2418 * @param cls Closure (tunnel info)
2419 * @param key GNUNET_PeerIdentity of the peer (unused)
2420 * @param value PeerInfo of the peer
2422 * @return always GNUNET_YES, to keep iterating
2425 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2427 struct MeshTunnel *t = cls;
2428 struct MeshPeerInfo *peer = value;
2431 for (i = 0; i < peer->ntunnels; i++)
2434 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2437 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2438 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2447 * Core callback to write a pre-constructed data packet to core buffer
2449 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2450 * @param size Number of bytes available in buf.
2451 * @param buf Where the to write the message.
2453 * @return number of bytes written to buf
2456 send_core_data_raw (void *cls, size_t size, void *buf)
2458 struct MeshTransmissionDescriptor *info = cls;
2459 struct GNUNET_MessageHeader *msg;
2462 GNUNET_assert (NULL != info);
2463 GNUNET_assert (NULL != info->mesh_data);
2464 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2465 total_size = ntohs (msg->size);
2467 if (total_size > size)
2472 memcpy (buf, msg, total_size);
2473 data_descriptor_decrement_rc (info->mesh_data);
2480 * Sends an already built non-multicast message to a peer,
2481 * properly registrating all used resources.
2483 * @param message Message to send. Function makes a copy of it.
2484 * @param peer Short ID of the neighbor whom to send the message.
2485 * @param t Tunnel on which this message is transmitted.
2488 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2489 const struct GNUNET_PeerIdentity *peer,
2490 struct MeshTunnel *t)
2492 struct MeshTransmissionDescriptor *info;
2493 struct MeshPeerInfo *neighbor;
2494 struct MeshPeerPath *p;
2498 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2500 size = ntohs (message->size);
2501 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2502 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2503 info->mesh_data->data = GNUNET_malloc (size);
2504 memcpy (info->mesh_data->data, message, size);
2505 type = ntohs(message->type);
2508 struct GNUNET_MESH_Unicast *m;
2509 struct GNUNET_MESH_ToOrigin *to;
2511 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2512 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2513 m->ttl = htonl (ntohl (m->ttl) - 1);
2515 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2516 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2518 to->pid = htonl(t->bck_pid);
2520 info->mesh_data->data_len = size;
2521 info->mesh_data->reference_counter = 1;
2522 info->mesh_data->total_out = 1;
2523 neighbor = peer_info_get (peer);
2524 for (p = neighbor->path_head; NULL != p; p = p->next)
2534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 " %s IS NOT DIRECTLY CONNECTED\n",
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2540 for (p = neighbor->path_head; NULL != p; p = p->next)
2542 struct GNUNET_PeerIdentity debug_id;
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2546 " path with %u hops through:\n",
2548 for (i = 0; i < p->length; i++)
2550 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 i, GNUNET_i2s(&debug_id));
2557 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2559 " no direct connection to %s\n",
2561 GNUNET_free (info->mesh_data->data);
2562 GNUNET_free (info->mesh_data);
2566 info->peer = neighbor;
2567 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2578 * Sends a CREATE PATH message for a path to a peer, properly registrating
2579 * all used resources.
2581 * @param peer PeerInfo of the final peer for whom this path is being created.
2582 * @param p Path itself.
2583 * @param t Tunnel for which the path is created.
2586 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2587 struct MeshTunnel *t)
2589 struct GNUNET_PeerIdentity id;
2590 struct MeshPathInfo *path_info;
2591 struct MeshPeerInfo *neighbor;
2597 p = tree_get_path_to_peer (t->tree, peer->id);
2604 for (i = 0; i < p->length; i++)
2606 if (p->peers[i] == myid)
2609 if (i >= p->length - 1)
2615 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2617 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2618 path_info->path = p;
2620 neighbor = peer_info_get (&id);
2621 path_info->peer = neighbor;
2622 queue_add (path_info,
2623 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2624 sizeof (struct GNUNET_MESH_ManipulatePath) +
2625 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2632 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2634 * @param t Tunnel whose path to destroy.
2635 * @param destination Short ID of the peer to whom the path to destroy.
2638 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2640 struct MeshPeerPath *p;
2643 p = tree_get_path_to_peer (t->tree, destination);
2649 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2650 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2652 struct GNUNET_MESH_ManipulatePath *msg;
2653 struct GNUNET_PeerIdentity *pi;
2657 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2658 msg->header.size = htons (size);
2659 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2660 msg->tid = htonl (t->id.tid);
2661 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2662 for (i = 0; i < p->length; i++)
2664 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2666 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2673 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2675 * @param t Tunnel which to confirm.
2678 send_path_ack (struct MeshTunnel *t)
2680 struct MeshTransmissionDescriptor *info;
2681 struct GNUNET_PeerIdentity id;
2682 GNUNET_PEER_Id peer;
2684 peer = tree_get_predecessor (t->tree);
2685 GNUNET_PEER_resolve (peer, &id);
2686 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2687 info->origin = &t->id;
2688 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2689 GNUNET_assert (NULL != info->peer);
2692 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2693 sizeof (struct GNUNET_MESH_PathACK),
2700 * Try to establish a new connection to this peer.
2701 * Use the best path for the given tunnel.
2702 * If the peer doesn't have any path to it yet, try to get one.
2703 * If the peer already has some path, send a CREATE PATH towards it.
2705 * @param peer PeerInfo of the peer.
2706 * @param t Tunnel for which to create the path, if possible.
2709 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2711 struct MeshPeerPath *p;
2712 struct MeshPathInfo *path_info;
2714 if (NULL != peer->path_head)
2716 p = tree_get_path_to_peer (t->tree, peer->id);
2723 // FIXME always send create path to self
2726 send_create_path (peer, p, t);
2730 struct GNUNET_HashCode hash;
2733 send_client_peer_connected (t, myid);
2734 t->local_tid_dest = next_local_tid++;
2735 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2738 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2739 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2746 else if (NULL == peer->dhtget)
2748 struct GNUNET_PeerIdentity id;
2750 GNUNET_PEER_resolve (peer->id, &id);
2751 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2752 path_info->peer = peer;
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2755 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2756 peer->dhtgetcls = path_info;
2757 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2758 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2759 &id.hashPubKey, /* key to search */
2760 dht_replication_level, /* replication level */
2761 GNUNET_DHT_RO_RECORD_ROUTE |
2762 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2763 NULL, /* xquery */ // FIXME BLOOMFILTER
2764 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2765 &dht_get_id_handler, path_info);
2767 /* Otherwise, there is no path but the DHT get is already started. */
2772 * Task to delay the connection of a peer
2774 * @param cls Closure (path info with tunnel and peer to connect).
2775 * Will be free'd on exection.
2776 * @param tc TaskContext
2779 peer_info_connect_task (void *cls,
2780 const struct GNUNET_SCHEDULER_TaskContext *tc)
2782 struct MeshPathInfo *path_info = cls;
2784 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2786 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2791 peer_info_connect (path_info->peer, path_info->t);
2797 * Destroy the peer_info and free any allocated resources linked to it
2799 * @param pi The peer_info to destroy.
2801 * @return GNUNET_OK on success
2804 peer_info_destroy (struct MeshPeerInfo *pi)
2806 struct GNUNET_PeerIdentity id;
2807 struct MeshPeerPath *p;
2808 struct MeshPeerPath *nextp;
2810 GNUNET_PEER_resolve (pi->id, &id);
2811 GNUNET_PEER_change_rc (pi->id, -1);
2814 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2818 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2820 if (NULL != pi->dhtget)
2822 GNUNET_DHT_get_stop (pi->dhtget);
2823 GNUNET_free (pi->dhtgetcls);
2829 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2833 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2835 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2843 * Remove all paths that rely on a direct connection between p1 and p2
2844 * from the peer itself and notify all tunnels about it.
2846 * @param peer PeerInfo of affected peer.
2847 * @param p1 GNUNET_PEER_Id of one peer.
2848 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2851 * TODO: optimize (see below)
2854 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2857 struct MeshPeerPath *p;
2858 struct MeshPeerPath *aux;
2859 struct MeshPeerInfo *peer_d;
2861 unsigned int destroyed;
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2868 p = peer->path_head;
2872 for (i = 0; i < (p->length - 1); i++)
2874 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2875 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2877 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2888 for (i = 0; i < peer->ntunnels; i++)
2890 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2894 * Problem: one or more peers have been deleted from the tunnel tree.
2895 * We don't know who they are to try to add them again.
2896 * We need to try to find a new path for each of the disconnected peers.
2897 * Some of them might already have a path to reach them that does not
2898 * involve p1 and p2. Adding all anew might render in a better tree than
2899 * the trivial immediate fix.
2901 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2902 * its children will be reachable trough him.
2904 peer_d = peer_info_get_short (d);
2907 for (p = peer_d->path_head; NULL != p; p = p->next)
2909 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2917 /* No callback, as peer will be already disconnected and a connection
2918 * scheduled by tunnel_notify_connection_broken.
2920 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2924 peer_info_connect (peer_d, peer->tunnels[i]);
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2932 * Add the path to the peer and update the path used to reach it in case this
2935 * @param peer_info Destination peer to add the path to.
2936 * @param path New path to add. Last peer must be the peer in arg 1.
2937 * Path will be either used of freed if already known.
2938 * @param trusted Do we trust that this path is real?
2941 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2944 struct MeshPeerPath *aux;
2948 if ((NULL == peer_info) || (NULL == path))
2951 path_destroy (path);
2954 if (path->peers[path->length - 1] != peer_info->id)
2957 path_destroy (path);
2960 if (path->length <= 2 && GNUNET_NO == trusted)
2962 /* Only allow CORE to tell us about direct paths */
2963 path_destroy (path);
2966 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2967 for (l = 1; l < path->length; l++)
2969 if (path->peers[l] == myid)
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2972 for (l2 = 0; l2 < path->length - l; l2++)
2974 path->peers[l2] = path->peers[l + l2];
2979 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2984 struct GNUNET_PeerIdentity id;
2986 GNUNET_PEER_resolve (peer_info->id, &id);
2987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2988 path->length, GNUNET_i2s (&id));
2991 l = path_get_length (path);
2998 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2999 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
3001 l2 = path_get_length (aux);
3004 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
3005 peer_info->path_tail, aux, path);
3010 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
3012 path_destroy (path);
3017 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
3024 * Add the path to the origin peer and update the path used to reach it in case
3025 * this is the shortest.
3026 * The path is given in peer_info -> destination, therefore we turn the path
3027 * upside down first.
3029 * @param peer_info Peer to add the path to, being the origin of the path.
3030 * @param path New path to add after being inversed.
3031 * @param trusted Do we trust that this path is real?
3034 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
3035 struct MeshPeerPath *path, int trusted)
3038 peer_info_add_path (peer_info, path, trusted);
3043 * Function called if the connection to the peer has been stalled for a while,
3044 * possibly due to a missed ACK. Poll the peer about its ACK status.
3046 * @param cls Closure (cinfo).
3047 * @param tc TaskContext.
3050 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3052 struct MeshTunnelChildInfo *cinfo = cls;
3053 struct GNUNET_MESH_Poll msg;
3054 struct GNUNET_PeerIdentity id;
3055 struct MeshTunnel *t;
3057 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3058 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3064 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3065 msg.header.size = htons (sizeof (msg));
3066 msg.tid = htonl (t->id.tid);
3067 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3068 msg.last_ack = htonl (cinfo->fwd_ack);
3070 GNUNET_PEER_resolve (cinfo->id, &id);
3071 send_prebuilt_message (&msg.header, &id, cinfo->t);
3072 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
3074 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
3075 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3076 &tunnel_poll, cinfo);
3081 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3082 * to obtain a local peer -> destination path and interning the peer ids.
3084 * @return Newly allocated and created path
3086 static struct MeshPeerPath *
3087 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3088 unsigned int get_path_length,
3089 const struct GNUNET_PeerIdentity *put_path,
3090 unsigned int put_path_length)
3092 struct MeshPeerPath *p;
3098 GNUNET_PEER_change_rc (myid, 1);
3099 i = get_path_length;
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3101 for (i--; i >= 0; i--)
3103 id = GNUNET_PEER_intern (&get_path[i]);
3104 if (p->length > 0 && id == p->peers[p->length - 1])
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3107 GNUNET_PEER_change_rc (id, -1);
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3112 GNUNET_i2s (&get_path[i]));
3114 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3115 p->peers[p->length - 1] = id;
3118 i = put_path_length;
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3120 for (i--; i >= 0; i--)
3122 id = GNUNET_PEER_intern (&put_path[i]);
3125 /* PUT path went through us, so discard the path up until now and start
3126 * from here to get a much shorter (and loop-free) path.
3131 if (p->length > 0 && id == p->peers[p->length - 1])
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3134 GNUNET_PEER_change_rc (id, -1);
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3139 GNUNET_i2s (&put_path[i]));
3141 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3142 p->peers[p->length - 1] = id;
3146 if (get_path_length > 0)
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3148 GNUNET_i2s (&get_path[0]));
3149 if (put_path_length > 0)
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3151 GNUNET_i2s (&put_path[0]));
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3154 for (i = 0; i < p->length; i++)
3156 struct GNUNET_PeerIdentity peer_id;
3158 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3160 GNUNET_i2s (&peer_id));
3168 * Adds a path to the peer_infos of all the peers in the path
3170 * @param p Path to process.
3171 * @param confirmed Whether we know if the path works or not.
3174 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3178 /* TODO: invert and add */
3179 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3180 for (i++; i < p->length; i++)
3182 struct MeshPeerInfo *aux;
3183 struct MeshPeerPath *copy;
3185 aux = peer_info_get_short (p->peers[i]);
3186 copy = path_duplicate (p);
3187 copy->length = i + 1;
3188 peer_info_add_path (aux, copy, GNUNET_NO);
3194 * Send keepalive packets for a peer
3196 * @param cls Closure (tunnel for which to send the keepalive).
3197 * @param tc Notification context.
3199 * TODO: implement explicit multicast keepalive?
3202 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3206 * Search for a tunnel among the incoming tunnels
3208 * @param tid the local id of the tunnel
3210 * @return tunnel handler, NULL if doesn't exist
3212 static struct MeshTunnel *
3213 tunnel_get_incoming (MESH_TunnelNumber tid)
3215 struct GNUNET_HashCode hash;
3217 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3218 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3219 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3224 * Search for a tunnel among the tunnels for a client
3226 * @param c the client whose tunnels to search in
3227 * @param tid the local id of the tunnel
3229 * @return tunnel handler, NULL if doesn't exist
3231 static struct MeshTunnel *
3232 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3234 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3236 return tunnel_get_incoming (tid);
3240 struct GNUNET_HashCode hash;
3242 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3243 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3249 * Search for a tunnel by global ID using PEER_ID
3251 * @param pi owner of the tunnel
3252 * @param tid global tunnel number
3254 * @return tunnel handler, NULL if doesn't exist
3256 static struct MeshTunnel *
3257 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3259 struct MESH_TunnelID id;
3260 struct GNUNET_HashCode hash;
3265 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3266 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3271 * Search for a tunnel by global ID using full PeerIdentities
3273 * @param oid owner of the tunnel
3274 * @param tid global tunnel number
3276 * @return tunnel handler, NULL if doesn't exist
3278 static struct MeshTunnel *
3279 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3281 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3286 * Delete an active client from the tunnel.
3292 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3296 for (i = 0; i < t->nclients; i++)
3298 if (t->clients[i] == c)
3300 t->clients[i] = t->clients[t->nclients - 1];
3301 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3302 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3304 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3312 * Delete an ignored client from the tunnel.
3318 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3322 for (i = 0; i < t->nignore; i++)
3324 if (t->ignore[i] == c)
3326 t->ignore[i] = t->ignore[t->nignore - 1];
3327 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3335 * Delete a client from the tunnel. It should be only done on
3336 * client disconnection, otherwise use client_ignore_tunnel.
3342 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3344 tunnel_delete_ignored_client (t, c);
3345 tunnel_delete_active_client (t, c);
3350 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3352 * Destroys queue elements of all waiting transmissions and frees all memory
3353 * used by the struct and its elements.
3355 * @param cls Closure (tunnel info).
3356 * @param key Hash of GNUNET_PEER_Id (unused).
3357 * @param value MeshTunnelChildInfo of the child.
3359 * @return always GNUNET_YES, to keep iterating
3362 tunnel_destroy_child (void *cls,
3363 const struct GNUNET_HashCode * key,
3366 struct MeshTunnelChildInfo *cinfo = value;
3367 struct MeshTunnel *t = cls;
3368 struct MeshPeerQueue *q;
3372 for (c = 0; c < cinfo->send_buffer_n; c++)
3374 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3375 q = cinfo->send_buffer[i];
3376 cinfo->send_buffer[i] = NULL;
3378 queue_destroy (q, GNUNET_YES);
3381 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3383 GNUNET_free_non_null (cinfo->send_buffer);
3384 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
3386 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
3387 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3389 GNUNET_free (cinfo);
3395 * Callback used to notify a client owner of a tunnel that a peer has
3396 * disconnected, most likely because of a path change.
3398 * @param cls Closure (tunnel this notification is about).
3399 * @param peer_id Short ID of disconnected peer.
3402 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3404 struct MeshTunnel *t = cls;
3405 struct MeshPeerInfo *peer;
3406 struct MeshPathInfo *path_info;
3408 if (NULL != t->owner && NULL != nc)
3410 struct GNUNET_MESH_PeerControl msg;
3412 msg.header.size = htons (sizeof (msg));
3413 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3414 msg.tunnel_id = htonl (t->local_tid);
3415 GNUNET_PEER_resolve (peer_id, &msg.peer);
3416 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3417 &msg.header, GNUNET_NO);
3419 peer = peer_info_get_short (peer_id);
3420 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3421 path_info->peer = peer;
3423 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3429 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3430 * needed rescources.
3431 * If peer already exists, reevaluate shortest path and change if different.
3433 * @param t Tunnel we want to add a new peer to
3434 * @param peer PeerInfo of the peer being added
3438 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3440 struct GNUNET_PeerIdentity id;
3441 struct MeshPeerPath *best_p;
3442 struct MeshPeerPath *p;
3443 unsigned int best_cost;
3446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3447 GNUNET_PEER_resolve (peer->id, &id);
3449 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3452 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3453 GNUNET_assert (GNUNET_OK ==
3454 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3459 if (NULL != (p = peer->path_head))
3462 best_cost = tree_get_path_cost (t->tree, p);
3465 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3472 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3473 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3474 t->path_refresh_task =
3475 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3479 /* Start a DHT get */
3480 peer_info_connect (peer, t);
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3486 * Add a path to a tunnel which we don't own, just to remember the next hop.
3487 * If destination node was already in the tunnel, the first hop information
3488 * will be replaced with the new path.
3490 * @param t Tunnel we want to add a new peer to
3491 * @param p Path to add
3492 * @param own_pos Position of local node in path.
3496 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3497 unsigned int own_pos)
3499 struct GNUNET_PeerIdentity id;
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3502 GNUNET_assert (0 != own_pos);
3503 tree_add_path (t->tree, p, NULL, NULL);
3504 if (own_pos < p->length - 1)
3506 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3507 tree_update_first_hops (t->tree, myid, &id);
3509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3513 * Add a client to a tunnel, initializing all needed data structures.
3515 * @param t Tunnel to which add the client.
3516 * @param c Client which to add to the tunnel.
3519 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3521 struct MeshTunnelClientInfo clinfo;
3523 GNUNET_array_append (t->clients, t->nclients, c);
3524 clinfo.fwd_ack = t->fwd_pid + 1;
3525 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3526 clinfo.fwd_pid = t->fwd_pid;
3527 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3529 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3534 * Notifies a tunnel that a connection has broken that affects at least
3535 * some of its peers. Sends a notification towards the root of the tree.
3536 * In case the peer is the owner of the tree, notifies the client that owns
3537 * the tunnel and tries to reconnect.
3539 * @param t Tunnel affected.
3540 * @param p1 Peer that got disconnected from p2.
3541 * @param p2 Peer that got disconnected from p1.
3543 * @return Short ID of the peer disconnected (either p1 or p2).
3544 * 0 if the tunnel remained unaffected.
3546 static GNUNET_PEER_Id
3547 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3553 tree_notify_connection_broken (t->tree, p1, p2,
3554 &tunnel_notify_client_peer_disconnected,
3556 if (myid != p1 && myid != p2)
3562 if (tree_get_predecessor (t->tree) != 0)
3564 /* We are the peer still connected, notify owner of the disconnection. */
3565 struct GNUNET_MESH_PathBroken msg;
3566 struct GNUNET_PeerIdentity neighbor;
3568 msg.header.size = htons (sizeof (msg));
3569 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3570 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3571 msg.tid = htonl (t->id.tid);
3572 msg.peer1 = my_full_id;
3573 GNUNET_PEER_resolve (pid, &msg.peer2);
3574 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3575 send_prebuilt_message (&msg.header, &neighbor, t);
3583 * Send a multicast packet to a neighbor.
3585 * @param cls Closure (Info about the multicast packet)
3586 * @param neighbor_id Short ID of the neighbor to send the packet to.
3589 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3591 struct MeshData *mdata = cls;
3592 struct MeshTransmissionDescriptor *info;
3593 struct GNUNET_PeerIdentity neighbor;
3594 struct GNUNET_MessageHeader *msg;
3596 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3598 info->mesh_data = mdata;
3599 (mdata->reference_counter) ++;
3600 info->destination = neighbor_id;
3601 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3603 GNUNET_i2s (&neighbor));
3604 info->peer = peer_info_get (&neighbor);
3605 GNUNET_assert (NULL != info->peer);
3606 msg = (struct GNUNET_MessageHeader *) mdata->data;
3609 info->mesh_data->data_len,
3616 * Queue a message in a tunnel in multicast, sending a copy to each child node
3617 * down the local one in the tunnel tree.
3619 * @param t Tunnel in which to send the data.
3620 * @param msg Message to be sent.
3623 tunnel_send_multicast (struct MeshTunnel *t,
3624 const struct GNUNET_MessageHeader *msg)
3626 struct MeshData *mdata;
3628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3629 " sending a multicast packet...\n");
3631 mdata = GNUNET_malloc (sizeof (struct MeshData));
3632 mdata->data_len = ntohs (msg->size);
3634 mdata->data = GNUNET_malloc (mdata->data_len);
3635 memcpy (mdata->data, msg, mdata->data_len);
3636 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3638 struct GNUNET_MESH_Multicast *mcast;
3640 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3641 if (t->fwd_queue_n >= t->fwd_queue_max)
3644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3646 " message from %s!\n",
3647 GNUNET_i2s(&mcast->oid));
3648 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3649 " message at %s!\n",
3650 GNUNET_i2s(&my_full_id));
3651 GNUNET_free (mdata->data);
3652 GNUNET_free (mdata);
3656 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3658 ntohl (mcast->ttl));
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3665 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3666 if (mdata->reference_counter == 0)
3668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3669 " no one to send data to\n");
3670 GNUNET_free (mdata->data);
3671 GNUNET_free (mdata);
3672 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3677 mdata->total_out = mdata->reference_counter;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3680 " sending a multicast packet done\n");
3686 * Increase the SKIP value of all peers that
3687 * have not received a unicast message.
3689 * @param cls Closure (ID of the peer that HAS received the message).
3690 * @param key ID of the neighbor.
3691 * @param value Information about the neighbor.
3693 * @return GNUNET_YES to keep iterating.
3696 tunnel_add_skip (void *cls,
3697 const struct GNUNET_HashCode * key,
3700 struct GNUNET_PeerIdentity *neighbor = cls;
3701 struct MeshTunnelChildInfo *cinfo = value;
3703 /* TODO compare only pointers? key == neighbor? */
3704 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3714 * @brief Get neighbor's Flow Control information.
3716 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3717 * descendant of the local node in a certain tunnel.
3718 * If the info is not yet there (recently created path), creates the data struct
3719 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3722 * @param t Tunnel related.
3723 * @param peer Neighbor whose Flow Control info is needed.
3725 * @return Neighbor's Flow Control info.
3727 static struct MeshTunnelChildInfo *
3728 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3729 const struct GNUNET_PeerIdentity *peer)
3731 struct MeshTunnelChildInfo *cinfo;
3733 if (NULL == t->children_fc)
3736 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3742 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3743 cinfo->id = GNUNET_PEER_intern (peer);
3744 cinfo->skip = t->fwd_pid;
3747 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3748 cinfo->fwd_ack = t->fwd_pid + delta;
3749 cinfo->bck_ack = delta;
3750 cinfo->bck_pid = -1;
3752 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3753 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3755 cinfo->send_buffer =
3756 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3758 GNUNET_assert (GNUNET_OK ==
3759 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3762 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3769 * Get the Flow Control info of a client.
3771 * @param t Tunnel on which to look.
3772 * @param c Client whose ACK to get.
3774 * @return ACK value.
3776 static struct MeshTunnelClientInfo *
3777 tunnel_get_client_fc (struct MeshTunnel *t,
3778 struct MeshClient *c)
3782 for (i = 0; i < t->nclients; i++)
3784 if (t->clients[i] != c)
3786 return &t->clients_fc[i];
3789 return NULL; // avoid compiler / coverity complaints
3794 * Iterator to get the appropiate ACK value from all children nodes.
3796 * @param cls Closue (tunnel).
3797 * @param id Id of the child node.
3800 tunnel_get_child_fwd_ack (void *cls,
3803 struct GNUNET_PeerIdentity peer_id;
3804 struct MeshTunnelChildInfo *cinfo;
3805 struct MeshTunnelChildIteratorContext *ctx = cls;
3806 struct MeshTunnel *t = ctx->t;
3809 GNUNET_PEER_resolve (id, &peer_id);
3810 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3811 ack = cinfo->fwd_ack;
3814 if (GNUNET_NO == ctx->init)
3816 ctx->max_child_ack = ack;
3817 ctx->init = GNUNET_YES;
3820 if (GNUNET_YES == t->speed_min)
3822 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3826 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3833 * Get the maximum PID allowed to transmit to any
3834 * tunnel child of the local peer, depending on the tunnel
3835 * buffering/speed settings.
3839 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3842 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3844 struct MeshTunnelChildIteratorContext ctx;
3846 ctx.max_child_ack = 0;
3848 ctx.init = GNUNET_NO;
3849 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3851 if (0 == ctx.nchildren)
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3854 " tunnel has no children, no FWD ACK\n");
3858 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3859 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3861 return (int64_t) ctx.max_child_ack;
3866 * Set the FWD ACK value of a client in a particular tunnel.
3868 * @param t Tunnel affected.
3869 * @param c Client whose ACK to set.
3870 * @param ack ACK value.
3873 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3874 struct MeshClient *c,
3879 for (i = 0; i < t->nclients; i++)
3881 if (t->clients[i] != c)
3883 t->clients_fc[i].fwd_ack = ack;
3891 * Get the highest ACK value of all clients in a particular tunnel,
3892 * according to the buffering/speed settings.
3894 * @param t Tunnel on which to look.
3896 * @return Corresponding ACK value (max uint32_t).
3897 * If no clients are suscribed, -1LL.
3900 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3905 if (0 == t->nclients)
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3908 " tunnel has no clients, no FWD ACK\n");
3912 for (ack = -1LL, i = 0; i < t->nclients; i++)
3915 (GNUNET_YES == t->speed_min &&
3916 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3917 (GNUNET_NO == t->speed_min &&
3918 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3920 ack = t->clients_fc[i].fwd_ack;
3924 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3925 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3927 return (uint32_t) ack;
3932 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3933 * mode and the status of all children nodes.
3937 * @return Maximum PID allowed.
3940 tunnel_get_fwd_ack (struct MeshTunnel *t)
3944 uint32_t buffer_free;
3948 count = t->fwd_pid - t->skip;
3949 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3950 child_ack = tunnel_get_children_fwd_ack (t);
3951 client_ack = tunnel_get_clients_fwd_ack (t);
3952 if (GNUNET_YES == t->nobuffer)
3955 if (-1LL == child_ack)
3956 child_ack = client_ack;
3957 if (-1LL == child_ack)
3960 client_ack = child_ack = ack;
3965 ack = count + buffer_free; // Overflow? OK!
3967 if (-1LL == child_ack)
3969 // Node has no children, child_ack AND core buffer are irrelevant.
3970 if (-1LL == client_ack) // No children AND no clients? Not good!
3972 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3976 return (uint32_t) client_ack;
3978 if (-1LL == client_ack)
3982 if (GNUNET_YES == t->speed_min)
3984 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3985 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3989 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3990 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3993 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3994 count, buffer_free, child_ack, client_ack, ack);
4000 * Build a local ACK message and send it to a local client.
4002 * @param t Tunnel on which to send the ACK.
4003 * @param c Client to whom send the ACK.
4004 * @param ack Value of the ACK.
4007 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
4009 struct GNUNET_MESH_LocalAck msg;
4011 msg.header.size = htons (sizeof (msg));
4012 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4013 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
4014 msg.max_pid = htonl (ack);
4015 GNUNET_SERVER_notification_context_unicast(nc,
4022 * Build an ACK message and queue it to send to the given peer.
4024 * @param t Tunnel on which to send the ACK.
4025 * @param peer Peer to whom send the ACK.
4026 * @param ack Value of the ACK.
4029 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
4031 struct GNUNET_MESH_ACK msg;
4033 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4034 msg.header.size = htons (sizeof (msg));
4035 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
4036 msg.pid = htonl (ack);
4037 msg.tid = htonl (t->id.tid);
4039 send_prebuilt_message (&msg.header, peer, t);
4044 * Notify a the owner of a tunnel about how many more
4045 * payload packages will we accept on a given tunnel.
4047 * @param t Tunnel on which to send the ACK.
4050 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
4054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4055 "Sending client FWD ACK on tunnel %X\n",
4058 ack = tunnel_get_fwd_ack (t);
4060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4061 if (t->last_fwd_ack == ack)
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
4068 t->last_fwd_ack = ack;
4069 send_local_ack (t, t->owner, ack);
4074 * Send an ACK informing the predecessor about the available buffer space.
4075 * In case there is no predecessor, inform the owning client.
4076 * If buffering is off, send only on behalf of children or self if endpoint.
4077 * If buffering is on, send when sent to children and buffer space is free.
4078 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4079 * the ACK itself goes "back" (towards root).
4081 * @param t Tunnel on which to send the ACK.
4082 * @param type Type of message that triggered the ACK transmission.
4085 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4087 struct GNUNET_PeerIdentity id;
4090 if (NULL != t->owner)
4092 tunnel_send_client_fwd_ack (t);
4095 /* Is it after unicast / multicast retransmission? */
4098 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4099 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4101 "ACK due to FWD DATA retransmission\n");
4102 if (GNUNET_YES == t->nobuffer)
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4108 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4109 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4111 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4112 t->force_ack = GNUNET_YES;
4118 /* Check if we need to transmit the ACK */
4119 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4120 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4121 GNUNET_NO == t->force_ack)
4123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4125 " t->qmax: %u, t->qn: %u\n",
4126 t->fwd_queue_max, t->fwd_queue_n);
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4128 " t->pid: %u, t->ack: %u\n",
4129 t->fwd_pid, t->last_fwd_ack);
4133 /* Ok, ACK might be necessary, what PID to ACK? */
4134 ack = tunnel_get_fwd_ack (t);
4136 /* If speed_min and not all children have ack'd, dont send yet */
4137 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4143 t->last_fwd_ack = ack;
4144 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4145 send_ack (t, &id, ack);
4147 t->force_ack = GNUNET_NO;
4152 * Iterator to send a child node a BCK ACK to allow him to send more
4155 * @param cls Closure (tunnel).
4156 * @param id Id of the child node.
4159 tunnel_send_child_bck_ack (void *cls,
4162 struct MeshTunnel *t = cls;
4163 struct MeshTunnelChildInfo *cinfo;
4164 struct GNUNET_PeerIdentity peer;
4167 GNUNET_PEER_resolve (id, &peer);
4168 cinfo = tunnel_get_neighbor_fc (t, &peer);
4169 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4171 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4174 " Not sending ACK, not needed\n");
4177 cinfo->bck_ack = ack;
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4180 " Sending BCK ACK %u (last sent: %u)\n",
4181 ack, cinfo->bck_ack);
4182 send_ack (t, &peer, ack);
4187 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4189 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4191 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4192 * - implement a size 1 buffer in each client_fc AND children_fc
4193 * to hold at least 1 message per "child".
4194 * problem: violates no buffer policy
4195 * - ack 0 and make "children" poll for transmission slots
4196 * problem: big overhead, extra latency even in low traffic
4199 * @param t Tunnel on which to send the BCK ACKs.
4202 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4205 unsigned int tunnel_delta;
4207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4209 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4212 /* Find client whom to allow to send to origin (with lowest buffer space) */
4213 for (i = 0; i < t->nclients; i++)
4215 struct MeshTunnelClientInfo *clinfo;
4218 clinfo = &t->clients_fc[i];
4219 delta = clinfo->bck_ack - clinfo->bck_pid;
4220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4221 t->clients[i]->id, delta);
4223 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4224 (GNUNET_YES == t->nobuffer && 0 == delta))
4228 ack = clinfo->bck_pid;
4229 ack += t->nobuffer ? 1 : tunnel_delta;
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4231 " sending ack to client %u: %u\n",
4232 t->clients[i]->id, ack);
4233 send_local_ack (t, t->clients[i], ack);
4234 clinfo->bck_ack = ack;
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4239 " not sending ack to client %u (td %u, d %u)\n",
4240 t->clients[i]->id, tunnel_delta, delta);
4247 * Send an ACK informing the children nodes and destination clients about
4248 * the available buffer space.
4249 * If buffering is off, send only on behalf of root (can be self).
4250 * If buffering is on, send when sent to predecessor and buffer space is free.
4251 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4252 * the ACK itself goes "forward" (towards children/clients).
4254 * @param t Tunnel on which to send the ACK.
4255 * @param type Type of message that triggered the ACK transmission.
4258 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4261 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4262 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4263 /* Is it after data to_origin retransmission? */
4266 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4267 if (GNUNET_YES == t->nobuffer)
4269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4270 " Not sending ACK, nobuffer\n");
4274 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4275 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4277 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4278 t->force_ack = GNUNET_YES;
4284 tunnel_send_clients_bck_ack (t);
4285 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4286 t->force_ack = GNUNET_NO;
4291 * @brief Re-initiate traffic to this peer if necessary.
4293 * Check if there is traffic queued towards this peer
4294 * and the core transmit handle is NULL (traffic was stalled).
4295 * If so, call core tmt rdy.
4297 * @param cls Closure (unused)
4298 * @param peer_id Short ID of peer to which initiate traffic.
4301 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4303 struct MeshPeerInfo *peer;
4304 struct GNUNET_PeerIdentity id;
4305 struct MeshPeerQueue *q;
4308 peer = peer_info_get_short(peer_id);
4309 if (NULL != peer->core_transmit)
4312 q = queue_get_next(peer);
4315 /* Might br multicast traffic already sent to this particular peer but
4316 * not to other children in this tunnel.
4317 * This way t->queue_n would be > 0 but the queue of this particular peer
4323 GNUNET_PEER_resolve (peer->id, &id);
4324 peer->core_transmit =
4325 GNUNET_CORE_notify_transmit_ready(core_handle,
4328 GNUNET_TIME_UNIT_FOREVER_REL,
4338 * @brief Allow transmission of FWD traffic on this tunnel
4340 * Check if there is traffic queued towards any children
4341 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4343 * @param t Tunnel on which to unlock FWD traffic.
4346 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4348 if (0 == t->fwd_queue_n)
4351 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4356 * @brief Allow transmission of BCK traffic on this tunnel
4358 * Check if there is traffic queued towards the root of the tree
4359 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4361 * @param t Tunnel on which to unlock BCK traffic.
4364 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4366 if (0 == t->bck_queue_n)
4369 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4374 * Send a message to all peers in this tunnel that the tunnel is no longer
4377 * @param t The tunnel whose peers to notify.
4378 * @param send_back Do we need to notify our parent node?
4381 tunnel_send_destroy (struct MeshTunnel *t, int send_back)
4383 struct GNUNET_MESH_TunnelDestroy msg;
4384 struct GNUNET_PeerIdentity id;
4385 GNUNET_PEER_Id parent;
4387 if (tree_count_children(t->tree) > 0)
4389 msg.header.size = htons (sizeof (msg));
4390 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4391 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4392 msg.tid = htonl (t->id.tid);
4393 tunnel_send_multicast (t, &msg.header);
4395 parent = tree_get_predecessor(t->tree);
4396 if (GNUNET_NO == send_back || 0 == parent)
4399 msg.header.size = htons (sizeof (msg));
4400 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4401 send_prebuilt_message(&msg.header, &id, t);
4406 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4408 * @param cls Closure (Tunnel which to cancel).
4409 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4412 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4414 struct MeshTunnel *t = cls;
4415 struct MeshPeerInfo *peer_info;
4416 struct MeshPeerQueue *pq;
4417 struct MeshPeerQueue *next;
4419 peer_info = peer_info_get_short (neighbor_id);
4420 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4423 if (pq->tunnel == t)
4425 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4426 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4427 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4429 // Should have been removed on destroy children
4432 queue_destroy (pq, GNUNET_YES);
4435 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4437 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4438 peer_info->core_transmit = NULL;
4443 * Destroy the tunnel and free any allocated resources linked to it.
4445 * @param t the tunnel to destroy
4447 * @return GNUNET_OK on success
4450 tunnel_destroy (struct MeshTunnel *t)
4452 struct MeshClient *c;
4453 struct GNUNET_HashCode hash;
4464 struct GNUNET_PeerIdentity id;
4466 GNUNET_PEER_resolve (t->id.oid, &id);
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4468 GNUNET_i2s (&id), t->id.tid);
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4474 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4475 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4483 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4485 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4492 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4493 for (i = 0; i < t->nclients; i++)
4497 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4503 for (i = 0; i < t->nignore; i++)
4507 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4514 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4515 GNUNET_free_non_null (t->clients);
4516 GNUNET_free_non_null (t->clients_fc);
4518 if (NULL != t->peers)
4520 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4522 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4525 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4526 &tunnel_destroy_child,
4528 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4529 t->children_fc = NULL;
4531 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4532 tree_destroy (t->tree);
4534 if (NULL != t->regex_ctx)
4535 regex_cancel_search (t->regex_ctx);
4536 if (NULL != t->dht_get_type)
4537 GNUNET_DHT_get_stop (t->dht_get_type);
4538 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4539 GNUNET_SCHEDULER_cancel (t->timeout_task);
4540 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4541 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4544 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4549 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4552 * Tunnel is empty: destroy it.
4554 * @param cls Closure (Tunnel).
4555 * @param tc TaskContext.
4558 tunnel_destroy_empty_delayed (void *cls,
4559 const struct GNUNET_SCHEDULER_TaskContext *tc)
4561 struct MeshTunnel *t = cls;
4563 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4564 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4567 if (0 != t->nclients ||
4568 0 != tree_count_children (t->tree))
4573 struct GNUNET_PeerIdentity id;
4575 GNUNET_PEER_resolve (t->id.oid, &id);
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4577 "executing destruction of empty tunnel %s [%X]\n",
4578 GNUNET_i2s (&id), t->id.tid);
4587 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4589 * @param t Tunnel to destroy if empty.
4592 tunnel_destroy_empty (struct MeshTunnel *t)
4594 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4596 0 != tree_count_children (t->tree))
4598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4600 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4606 struct GNUNET_PeerIdentity id;
4608 GNUNET_PEER_resolve (t->id.oid, &id);
4609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4610 "scheduling destruction of empty tunnel %s [%X]\n",
4611 GNUNET_i2s (&id), t->id.tid);
4615 t->delayed_destroy =
4616 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4617 &tunnel_destroy_empty_delayed,
4623 * Create a new tunnel
4625 * @param owner Who is the owner of the tunnel (short ID).
4626 * @param tid Tunnel Number of the tunnel.
4627 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4628 * @param local Tunnel Number for the tunnel, for the client point of view.
4630 * @return A new initialized tunnel. NULL on error.
4632 static struct MeshTunnel *
4633 tunnel_new (GNUNET_PEER_Id owner,
4634 MESH_TunnelNumber tid,
4635 struct MeshClient *client,
4636 MESH_TunnelNumber local)
4638 struct MeshTunnel *t;
4639 struct GNUNET_HashCode hash;
4641 if (n_tunnels >= max_tunnels && NULL == client)
4644 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4647 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4648 t->bck_queue_max = t->fwd_queue_max;
4649 t->tree = tree_new (owner);
4651 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4652 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4653 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4654 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4655 t->local_tid = local;
4656 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4658 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4660 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4662 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4663 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4670 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4677 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4679 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4680 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4684 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4694 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4695 * that are no longer needed, as well as nodes of no longer reachable peers.
4696 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4698 * @param t Tunnel from which to remove the path.
4699 * @param peer Short id of the peer which should be removed.
4702 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4706 r = tree_del_peer (t->tree, peer, NULL, NULL);
4709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4710 "Tunnel %u [%u] has no more nodes\n",
4711 t->id.oid, t->id.tid);
4717 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4718 * client when the client disconnects. If the client is not the owner, the
4719 * owner will get notified if no more clients are in the tunnel and the client
4720 * get removed from the tunnel's list.
4722 * @param cls closure (client that is disconnecting)
4723 * @param key the hash of the local tunnel id (used to access the hashmap)
4724 * @param value the value stored at the key (tunnel to destroy)
4726 * @return GNUNET_OK, keep iterating.
4729 tunnel_destroy_iterator (void *cls,
4730 const struct GNUNET_HashCode * key,
4733 struct MeshTunnel *t = value;
4734 struct MeshClient *c = cls;
4736 send_client_tunnel_disconnect (t, c);
4739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4740 tunnel_delete_client (t, c);
4741 client_delete_tunnel (c, t);
4742 tunnel_destroy_empty (t);
4745 tunnel_send_destroy (t, GNUNET_YES);
4747 t->destroy = GNUNET_YES;
4754 * Timeout function, destroys tunnel if called
4756 * @param cls Closure (tunnel to destroy).
4757 * @param tc TaskContext
4760 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4762 struct MeshTunnel *t = cls;
4763 struct GNUNET_PeerIdentity id;
4765 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4766 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4768 GNUNET_PEER_resolve(t->id.oid, &id);
4769 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4770 "Tunnel %s [%X] timed out. Destroying.\n",
4771 GNUNET_i2s(&id), t->id.tid);
4772 send_clients_tunnel_destroy (t);
4777 * Resets the tunnel timeout. Starts it if no timeout was running.
4779 * @param t Tunnel whose timeout to reset.
4781 * TODO use heap to improve efficiency of scheduler.
4784 tunnel_reset_timeout (struct MeshTunnel *t)
4786 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4787 GNUNET_SCHEDULER_cancel (t->timeout_task);
4789 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4790 (refresh_path_time, 4), &tunnel_timeout, t);
4794 /******************************************************************************/
4795 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4796 /******************************************************************************/
4799 * Function to send a create path packet to a peer.
4801 * @param cls closure
4802 * @param size number of bytes available in buf
4803 * @param buf where the callee should write the message
4804 * @return number of bytes written to buf
4807 send_core_path_create (void *cls, size_t size, void *buf)
4809 struct MeshPathInfo *info = cls;
4810 struct GNUNET_MESH_ManipulatePath *msg;
4811 struct GNUNET_PeerIdentity *peer_ptr;
4812 struct MeshTunnel *t = info->t;
4813 struct MeshPeerPath *p = info->path;
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4820 sizeof (struct GNUNET_MESH_ManipulatePath) +
4821 p->length * sizeof (struct GNUNET_PeerIdentity);
4823 if (size < size_needed || NULL == buf)
4828 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4829 msg->header.size = htons (size_needed);
4830 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4831 msg->tid = ntohl (t->id.tid);
4834 if (GNUNET_YES == t->speed_min)
4835 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4836 if (GNUNET_YES == t->nobuffer)
4837 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4838 msg->opt = htonl(opt);
4841 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4842 for (i = 0; i < p->length; i++)
4844 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4851 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4857 * Fill the core buffer
4859 * @param cls closure (data itself)
4860 * @param size number of bytes available in buf
4861 * @param buf where the callee should write the message
4863 * @return number of bytes written to buf
4866 send_core_data_multicast (void *cls, size_t size, void *buf)
4868 struct MeshTransmissionDescriptor *info = cls;
4871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4872 GNUNET_assert (NULL != info);
4873 GNUNET_assert (NULL != info->peer);
4874 total_size = info->mesh_data->data_len;
4875 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4877 if (total_size > size)
4882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4883 memcpy (buf, info->mesh_data->data, total_size);
4886 struct GNUNET_MESH_Multicast *mc;
4887 struct GNUNET_MessageHeader *mh;
4890 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4892 mc = (struct GNUNET_MESH_Multicast *) mh;
4893 mh = (struct GNUNET_MessageHeader *) &mc[1];
4894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4895 " multicast, payload type %s\n",
4896 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4898 " multicast, payload size %u\n", ntohs (mh->size));
4902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4903 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4907 data_descriptor_decrement_rc (info->mesh_data);
4908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4916 * Creates a path ack message in buf and frees all unused resources.
4918 * @param cls closure (MeshTransmissionDescriptor)
4919 * @param size number of bytes available in buf
4920 * @param buf where the callee should write the message
4921 * @return number of bytes written to buf
4924 send_core_path_ack (void *cls, size_t size, void *buf)
4926 struct MeshTransmissionDescriptor *info = cls;
4927 struct GNUNET_MESH_PathACK *msg = buf;
4929 GNUNET_assert (NULL != info);
4930 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4935 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4936 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4937 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4938 msg->tid = htonl (info->origin->tid);
4939 msg->peer_id = my_full_id;
4942 /* TODO add signature */
4944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4945 return sizeof (struct GNUNET_MESH_PathACK);
4950 * Free a transmission that was already queued with all resources
4951 * associated to the request.
4953 * @param queue Queue handler to cancel.
4954 * @param clear_cls Is it necessary to free associated cls?
4957 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4959 struct MeshTransmissionDescriptor *dd;
4960 struct MeshPathInfo *path_info;
4961 struct MeshTunnelChildInfo *cinfo;
4962 struct GNUNET_PeerIdentity id;
4966 if (GNUNET_YES == clear_cls)
4968 switch (queue->type)
4970 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4971 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4972 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4974 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4975 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4976 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4977 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4978 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4979 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4981 " prebuilt message\n");
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4984 GNUNET_MESH_DEBUG_M2S(queue->type));
4986 data_descriptor_decrement_rc (dd->mesh_data);
4988 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4990 path_info = queue->cls;
4991 path_destroy (path_info->path);
4995 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4996 " type %s unknown!\n",
4997 GNUNET_MESH_DEBUG_M2S(queue->type));
4999 GNUNET_free_non_null (queue->cls);
5001 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
5002 queue->peer->queue_tail,
5005 /* Delete from child_fc in the appropiate tunnel */
5006 max = queue->tunnel->fwd_queue_max;
5007 GNUNET_PEER_resolve (queue->peer->id, &id);
5008 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
5011 for (i = 0; i < cinfo->send_buffer_n; i++)
5014 i2 = (cinfo->send_buffer_start + i) % max;
5015 if (cinfo->send_buffer[i2] == queue)
5017 /* Found corresponding entry in the send_buffer. Move all others back. */
5022 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
5024 j2 = (cinfo->send_buffer_start + j) % max;
5025 j3 = (cinfo->send_buffer_start + j + 1) % max;
5026 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
5029 cinfo->send_buffer[j3] = NULL;
5030 cinfo->send_buffer_n--;
5035 GNUNET_free (queue);
5040 * @brief Get the next transmittable message from the queue.
5042 * This will be the head, except in the case of being a data packet
5043 * not allowed by the destination peer.
5045 * @param peer Destination peer.
5047 * @return The next viable MeshPeerQueue element to send to that peer.
5048 * NULL when there are no transmittable messages.
5050 struct MeshPeerQueue *
5051 queue_get_next (const struct MeshPeerInfo *peer)
5053 struct MeshPeerQueue *q;
5054 struct MeshTunnel *t;
5055 struct MeshTransmissionDescriptor *info;
5056 struct MeshTunnelChildInfo *cinfo;
5057 struct GNUNET_MESH_Unicast *ucast;
5058 struct GNUNET_MESH_ToOrigin *to_orig;
5059 struct GNUNET_MESH_Multicast *mcast;
5060 struct GNUNET_PeerIdentity id;
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
5065 for (q = peer->queue_head; NULL != q; q = q->next)
5069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5071 GNUNET_MESH_DEBUG_M2S(q->type));
5074 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5075 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
5076 pid = ntohl (ucast->pid);
5077 GNUNET_PEER_resolve (info->peer->id, &id);
5078 cinfo = tunnel_get_neighbor_fc(t, &id);
5079 ack = cinfo->fwd_ack;
5081 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5082 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
5083 pid = ntohl (to_orig->pid);
5086 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5087 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
5088 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
5090 // Not a multicast payload: multicast control traffic (destroy, etc)
5093 pid = ntohl (mcast->pid);
5094 GNUNET_PEER_resolve (info->peer->id, &id);
5095 cinfo = tunnel_get_neighbor_fc(t, &id);
5096 ack = cinfo->fwd_ack;
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5104 "********* ACK: %u, PID: %u\n",
5106 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
5108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5115 "********* NEXT!\n");
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5119 "********* nothing found\n");
5125 * Core callback to write a queued packet to core buffer
5127 * @param cls Closure (peer info).
5128 * @param size Number of bytes available in buf.
5129 * @param buf Where the to write the message.
5131 * @return number of bytes written to buf
5134 queue_send (void *cls, size_t size, void *buf)
5136 struct MeshPeerInfo *peer = cls;
5137 struct GNUNET_MessageHeader *msg;
5138 struct MeshPeerQueue *queue;
5139 struct MeshTunnel *t;
5140 struct MeshTunnelChildInfo *cinfo;
5141 struct GNUNET_PeerIdentity dst_id;
5144 peer->core_transmit = NULL;
5147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5148 queue = queue_get_next (peer);
5150 /* Queue has no internal mesh traffic nor sendable payload */
5153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5154 if (NULL == peer->queue_head)
5155 GNUNET_break (0); // Should've been canceled
5158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5160 GNUNET_PEER_resolve (peer->id, &dst_id);
5161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5162 "********* towards %s\n",
5163 GNUNET_i2s(&dst_id));
5164 /* Check if buffer size is enough for the message */
5165 if (queue->size > size)
5167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5168 "********* not enough room, reissue\n");
5169 peer->core_transmit =
5170 GNUNET_CORE_notify_transmit_ready (core_handle,
5173 GNUNET_TIME_UNIT_FOREVER_REL,
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5183 GNUNET_assert (0 < t->pending_messages);
5184 t->pending_messages--;
5185 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5189 "********* unicast: t->q (%u/%u)\n",
5190 t->fwd_queue_n, t->fwd_queue_max);
5192 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5199 switch (queue->type)
5202 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5203 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5204 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5205 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5206 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5208 "********* raw: %s\n",
5209 GNUNET_MESH_DEBUG_M2S (queue->type));
5211 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5212 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5213 data_size = send_core_data_raw (queue->cls, size, buf);
5214 msg = (struct GNUNET_MessageHeader *) buf;
5215 switch (ntohs (msg->type)) // Type of preconstructed message
5217 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5218 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5220 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5221 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5227 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5230 struct MeshTransmissionDescriptor *info = queue->cls;
5232 if ((1 == info->mesh_data->reference_counter
5233 && GNUNET_YES == t->speed_min)
5235 (info->mesh_data->total_out == info->mesh_data->reference_counter
5236 && GNUNET_NO == t->speed_min))
5238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5239 "********* considered sent\n");
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5245 "********* NOT considered sent yet\n");
5246 t->pending_messages++;
5249 data_size = send_core_data_multicast(queue->cls, size, buf);
5250 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5252 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5254 data_size = send_core_path_create (queue->cls, size, buf);
5256 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5258 data_size = send_core_path_ack (queue->cls, size, buf);
5260 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5262 data_size = send_core_data_multicast (queue->cls, size, buf);
5266 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5267 "********* type unknown: %u\n",
5271 switch (queue->type)
5273 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5274 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5275 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5276 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5277 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5281 "at pos %u (%p) != %p\n",
5282 cinfo->send_buffer_start,
5283 cinfo->send_buffer[cinfo->send_buffer_start],
5286 if (cinfo->send_buffer_n > 0)
5288 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5289 cinfo->send_buffer_n--;
5290 cinfo->send_buffer_start++;
5291 cinfo->send_buffer_start %= t->fwd_queue_max;
5302 /* Free queue, but cls was freed by send_core_* */
5303 queue_destroy (queue, GNUNET_NO);
5305 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5311 /* If more data in queue, send next */
5312 queue = queue_get_next(peer);
5315 struct GNUNET_PeerIdentity id;
5317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5318 GNUNET_PEER_resolve (peer->id, &id);
5319 peer->core_transmit =
5320 GNUNET_CORE_notify_transmit_ready(core_handle,
5323 GNUNET_TIME_UNIT_FOREVER_REL,
5331 if (NULL != peer->queue_head)
5333 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5334 "********* %s stalled\n",
5335 GNUNET_i2s(&my_full_id));
5337 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5338 // FIXME unify bck/fwd structures, bck does not have cinfo right now
5339 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5341 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
5342 &tunnel_poll, cinfo);
5346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5352 * @brief Queue and pass message to core when possible.
5354 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5355 * and accounts for it. In case the queue is full, the message is dropped and
5358 * Otherwise, message is treated as internal and allowed to go regardless of
5361 * @param cls Closure (@c type dependant). It will be used by queue_send to
5362 * build the message to be sent if not already prebuilt.
5363 * @param type Type of the message, 0 for a raw message.
5364 * @param size Size of the message.
5365 * @param dst Neighbor to send message to.
5366 * @param t Tunnel this message belongs to.
5369 queue_add (void *cls, uint16_t type, size_t size,
5370 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5372 struct MeshPeerQueue *queue;
5373 struct MeshTunnelChildInfo *cinfo;
5374 struct GNUNET_PeerIdentity id;
5380 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5381 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5383 n = &t->fwd_queue_n;
5384 max = &t->fwd_queue_max;
5386 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5388 n = &t->bck_queue_n;
5389 max = &t->bck_queue_max;
5396 GNUNET_STATISTICS_update(stats,
5397 "# messages dropped (buffer full)",
5399 return; // Drop message
5403 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5409 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5410 GNUNET_PEER_resolve (dst->id, &id);
5411 if (NULL == dst->core_transmit)
5413 dst->core_transmit =
5414 GNUNET_CORE_notify_transmit_ready (core_handle,
5417 GNUNET_TIME_UNIT_FOREVER_REL,
5423 t->pending_messages++;
5424 if (NULL == n) // Is this internal mesh traffic?
5427 // It's payload, keep track of buffer per peer.
5428 cinfo = tunnel_get_neighbor_fc(t, &id);
5429 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5430 if (NULL != cinfo->send_buffer[i])
5432 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5433 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5434 cinfo->send_buffer_start++;
5435 cinfo->send_buffer_start %= t->fwd_queue_max;
5439 cinfo->send_buffer_n++;
5441 cinfo->send_buffer[i] = queue;
5442 if (cinfo->send_buffer_n > t->fwd_queue_max)
5445 cinfo->send_buffer_n = t->fwd_queue_max;
5450 /******************************************************************************/
5451 /******************** MESH NETWORK HANDLERS **************************/
5452 /******************************************************************************/
5456 * Core handler for path creation
5458 * @param cls closure
5459 * @param message message
5460 * @param peer peer identity this notification is about
5461 * @param atsi performance data
5462 * @param atsi_count number of records in 'atsi'
5464 * @return GNUNET_OK to keep the connection open,
5465 * GNUNET_SYSERR to close it (signal serious error)
5468 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5469 const struct GNUNET_MessageHeader *message,
5470 const struct GNUNET_ATS_Information *atsi,
5471 unsigned int atsi_count)
5473 unsigned int own_pos;
5476 MESH_TunnelNumber tid;
5477 struct GNUNET_MESH_ManipulatePath *msg;
5478 struct GNUNET_PeerIdentity *pi;
5479 struct GNUNET_HashCode hash;
5480 struct MeshPeerPath *path;
5481 struct MeshPeerInfo *dest_peer_info;
5482 struct MeshPeerInfo *orig_peer_info;
5483 struct MeshTunnel *t;
5485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5486 "Received a path create msg [%s]\n",
5487 GNUNET_i2s (&my_full_id));
5488 size = ntohs (message->size);
5489 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5491 GNUNET_break_op (0);
5495 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5496 if (size % sizeof (struct GNUNET_PeerIdentity))
5498 GNUNET_break_op (0);
5501 size /= sizeof (struct GNUNET_PeerIdentity);
5504 GNUNET_break_op (0);
5507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5508 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5510 tid = ntohl (msg->tid);
5511 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5513 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5514 t = tunnel_get (pi, tid);
5515 if (NULL == t) // FIXME only for INCOMING tunnels?
5519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5520 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5523 // FIXME notify failure
5526 opt = ntohl (msg->opt);
5527 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5528 GNUNET_YES : GNUNET_NO;
5529 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5531 t->nobuffer = GNUNET_YES;
5532 t->last_fwd_ack = t->fwd_pid + 1;
5534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5535 " speed_min: %d, nobuffer:%d\n",
5536 t->speed_min, t->nobuffer);
5538 if (GNUNET_YES == t->nobuffer)
5540 t->bck_queue_max = 1;
5541 t->fwd_queue_max = 1;
5544 // FIXME only assign a local tid if a local client is interested (on demand)
5545 while (NULL != tunnel_get_incoming (next_local_tid))
5546 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5547 t->local_tid_dest = next_local_tid++;
5548 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5551 tunnel_reset_timeout (t);
5552 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5554 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5555 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5563 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5564 if (NULL == dest_peer_info)
5566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5567 " Creating PeerInfo for destination.\n");
5568 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5569 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5570 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5572 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5574 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5575 if (NULL == orig_peer_info)
5577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5578 " Creating PeerInfo for origin.\n");
5579 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5580 orig_peer_info->id = GNUNET_PEER_intern (pi);
5581 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5582 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5585 path = path_new (size);
5587 for (i = 0; i < size; i++)
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5590 GNUNET_i2s (&pi[i]));
5591 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5592 if (path->peers[i] == myid)
5595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5598 /* cannot be self, must be 'not found' */
5599 /* create path: self not found in path through self */
5600 GNUNET_break_op (0);
5601 path_destroy (path);
5605 path_add_to_peers (path, GNUNET_NO);
5606 tunnel_add_path (t, path, own_pos);
5607 if (own_pos == size - 1)
5609 /* It is for us! Send ack. */
5610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5611 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5612 if (NULL == t->peers)
5614 /* New tunnel! Notify clients on first payload message. */
5615 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5617 GNUNET_break (GNUNET_SYSERR !=
5618 GNUNET_CONTAINER_multihashmap_put (t->peers,
5619 &my_full_id.hashPubKey,
5622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5627 struct MeshPeerPath *path2;
5629 /* It's for somebody else! Retransmit. */
5630 path2 = path_duplicate (path);
5631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5632 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5633 path2 = path_duplicate (path);
5634 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5635 send_create_path (dest_peer_info, path, t);
5642 * Core handler for path destruction
5644 * @param cls closure
5645 * @param message message
5646 * @param peer peer identity this notification is about
5647 * @param atsi performance data
5648 * @param atsi_count number of records in 'atsi'
5650 * @return GNUNET_OK to keep the connection open,
5651 * GNUNET_SYSERR to close it (signal serious error)
5654 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5655 const struct GNUNET_MessageHeader *message,
5656 const struct GNUNET_ATS_Information *atsi,
5657 unsigned int atsi_count)
5659 struct GNUNET_MESH_ManipulatePath *msg;
5660 struct GNUNET_PeerIdentity *pi;
5661 struct MeshPeerPath *path;
5662 struct MeshTunnel *t;
5663 unsigned int own_pos;
5667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5668 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5669 size = ntohs (message->size);
5670 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5672 GNUNET_break_op (0);
5676 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5677 if (size % sizeof (struct GNUNET_PeerIdentity))
5679 GNUNET_break_op (0);
5682 size /= sizeof (struct GNUNET_PeerIdentity);
5685 GNUNET_break_op (0);
5688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5690 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5691 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5693 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5695 t = tunnel_get (pi, ntohl (msg->tid));
5698 /* TODO notify back: we don't know this tunnel */
5699 GNUNET_break_op (0);
5702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5703 path = path_new (size);
5705 for (i = 0; i < size; i++)
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5708 GNUNET_i2s (&pi[i]));
5709 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5710 if (path->peers[i] == myid)
5713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5714 if (own_pos < path->length - 1)
5715 send_prebuilt_message (message, &pi[own_pos + 1], t);
5717 send_client_tunnel_disconnect(t, NULL);
5719 tunnel_delete_peer (t, path->peers[path->length - 1]);
5720 path_destroy (path);
5726 * Core handler for notifications of broken paths
5728 * @param cls closure
5729 * @param message message
5730 * @param peer peer identity this notification is about
5731 * @param atsi performance data
5732 * @param atsi_count number of records in 'atsi'
5734 * @return GNUNET_OK to keep the connection open,
5735 * GNUNET_SYSERR to close it (signal serious error)
5738 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5739 const struct GNUNET_MessageHeader *message,
5740 const struct GNUNET_ATS_Information *atsi,
5741 unsigned int atsi_count)
5743 struct GNUNET_MESH_PathBroken *msg;
5744 struct MeshTunnel *t;
5746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5747 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5748 msg = (struct GNUNET_MESH_PathBroken *) message;
5749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5750 GNUNET_i2s (&msg->peer1));
5751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5752 GNUNET_i2s (&msg->peer2));
5753 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5756 GNUNET_break_op (0);
5759 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5760 GNUNET_PEER_search (&msg->peer2));
5767 * Core handler for tunnel destruction
5769 * @param cls closure
5770 * @param message message
5771 * @param peer peer identity this notification is about
5772 * @param atsi performance data
5773 * @param atsi_count number of records in 'atsi'
5775 * @return GNUNET_OK to keep the connection open,
5776 * GNUNET_SYSERR to close it (signal serious error)
5779 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5780 const struct GNUNET_MessageHeader *message,
5781 const struct GNUNET_ATS_Information *atsi,
5782 unsigned int atsi_count)
5784 struct GNUNET_MESH_TunnelDestroy *msg;
5785 struct MeshTunnel *t;
5787 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5789 "Got a TUNNEL DESTROY packet from %s\n",
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5792 " for tunnel %s [%u]\n",
5793 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5794 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5797 /* Probably already got the message from another path,
5798 * destroyed the tunnel and retransmitted to children.
5801 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5805 if (t->id.oid == myid)
5807 GNUNET_break_op (0);
5810 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5812 /* Tunnel was incoming, notify clients */
5813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5814 t->local_tid, t->local_tid_dest);
5815 send_clients_tunnel_destroy (t);
5817 tunnel_send_destroy (t, GNUNET_YES);
5818 t->destroy = GNUNET_YES;
5819 // TODO: add timeout to destroy the tunnel anyway
5825 * Core handler for mesh network traffic going from the origin to a peer
5827 * @param cls closure
5828 * @param peer peer identity this notification is about
5829 * @param message message
5830 * @param atsi performance data
5831 * @param atsi_count number of records in 'atsi'
5832 * @return GNUNET_OK to keep the connection open,
5833 * GNUNET_SYSERR to close it (signal serious error)
5836 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5837 const struct GNUNET_MessageHeader *message,
5838 const struct GNUNET_ATS_Information *atsi,
5839 unsigned int atsi_count)
5841 struct GNUNET_MESH_Unicast *msg;
5842 struct GNUNET_PeerIdentity *neighbor;
5843 struct MeshTunnelChildInfo *cinfo;
5844 struct MeshTunnel *t;
5845 GNUNET_PEER_Id dest_id;
5850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5853 size = ntohs (message->size);
5855 sizeof (struct GNUNET_MESH_Unicast) +
5856 sizeof (struct GNUNET_MessageHeader))
5861 msg = (struct GNUNET_MESH_Unicast *) message;
5862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5863 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5865 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5868 /* TODO notify back: we don't know this tunnel */
5869 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5870 GNUNET_break_op (0);
5873 pid = ntohl (msg->pid);
5874 if (t->fwd_pid == pid)
5876 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5878 " Already seen pid %u, DROPPING!\n", pid);
5883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5884 " pid %u not seen yet, forwarding\n", pid);
5887 t->skip += (pid - t->fwd_pid) - 1;
5890 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5892 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5893 GNUNET_break_op (0);
5894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5895 "Received PID %u, ACK %u\n",
5896 pid, t->last_fwd_ack);
5900 tunnel_reset_timeout (t);
5901 dest_id = GNUNET_PEER_search (&msg->destination);
5902 if (dest_id == myid)
5904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5905 " it's for us! sending to clients...\n");
5906 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5907 send_subscribed_clients (message, &msg[1].header, t);
5908 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5911 ttl = ntohl (msg->ttl);
5912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5915 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5916 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5917 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5921 " not for us, retransmitting...\n");
5923 neighbor = tree_get_first_hop (t->tree, dest_id);
5924 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5925 cinfo->fwd_pid = pid;
5926 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5929 if (GNUNET_YES == t->nobuffer &&
5930 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5932 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5933 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5934 GNUNET_break_op (0);
5937 send_prebuilt_message (message, neighbor, t);
5938 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5944 * Core handler for mesh network traffic going from the origin to all peers
5946 * @param cls closure
5947 * @param message message
5948 * @param peer peer identity this notification is about
5949 * @param atsi performance data
5950 * @param atsi_count number of records in 'atsi'
5951 * @return GNUNET_OK to keep the connection open,
5952 * GNUNET_SYSERR to close it (signal serious error)
5954 * TODO: Check who we got this from, to validate route.
5957 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5958 const struct GNUNET_MessageHeader *message,
5959 const struct GNUNET_ATS_Information *atsi,
5960 unsigned int atsi_count)
5962 struct GNUNET_MESH_Multicast *msg;
5963 struct MeshTunnel *t;
5967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5969 size = ntohs (message->size);
5970 if (sizeof (struct GNUNET_MESH_Multicast) +
5971 sizeof (struct GNUNET_MessageHeader) > size)
5973 GNUNET_break_op (0);
5976 msg = (struct GNUNET_MESH_Multicast *) message;
5977 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5981 /* TODO notify that we dont know that tunnel */
5982 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5983 GNUNET_break_op (0);
5986 pid = ntohl (msg->pid);
5987 if (t->fwd_pid == pid)
5989 /* already seen this packet, drop */
5990 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5992 " Already seen pid %u, DROPPING!\n", pid);
5993 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5999 " pid %u not seen yet, forwarding\n", pid);
6001 t->skip += (pid - t->fwd_pid) - 1;
6003 tunnel_reset_timeout (t);
6005 /* Transmit to locally interested clients */
6006 if (NULL != t->peers &&
6007 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
6009 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
6010 send_subscribed_clients (message, &msg[1].header, t);
6011 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
6013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
6014 if (ntohl (msg->ttl) == 0)
6016 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
6017 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
6018 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6021 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
6022 tunnel_send_multicast (t, message);
6028 * Core handler for mesh network traffic toward the owner of a tunnel
6030 * @param cls closure
6031 * @param message message
6032 * @param peer peer identity this notification is about
6033 * @param atsi performance data
6034 * @param atsi_count number of records in 'atsi'
6036 * @return GNUNET_OK to keep the connection open,
6037 * GNUNET_SYSERR to close it (signal serious error)
6040 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
6041 const struct GNUNET_MessageHeader *message,
6042 const struct GNUNET_ATS_Information *atsi,
6043 unsigned int atsi_count)
6045 struct GNUNET_MESH_ToOrigin *msg;
6046 struct GNUNET_PeerIdentity id;
6047 struct MeshPeerInfo *peer_info;
6048 struct MeshTunnel *t;
6049 struct MeshTunnelChildInfo *cinfo;
6050 GNUNET_PEER_Id predecessor;
6054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
6056 size = ntohs (message->size);
6057 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
6058 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
6060 GNUNET_break_op (0);
6063 msg = (struct GNUNET_MESH_ToOrigin *) message;
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
6065 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
6066 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6067 pid = ntohl (msg->pid);
6071 /* TODO notify that we dont know this tunnel (whom)? */
6072 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
6073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6074 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
6075 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
6079 cinfo = tunnel_get_neighbor_fc(t, peer);
6086 if (cinfo->bck_pid == pid)
6088 /* already seen this packet, drop */
6089 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
6090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6091 " Already seen pid %u, DROPPING!\n", pid);
6092 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6097 " pid %u not seen yet, forwarding\n", pid);
6098 cinfo->bck_pid = pid;
6100 if (NULL != t->owner)
6103 struct GNUNET_MESH_ToOrigin *copy;
6105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6106 " it's for us! sending to clients...\n");
6107 /* TODO signature verification */
6108 memcpy (cbuf, message, size);
6109 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
6110 copy->tid = htonl (t->local_tid);
6112 copy->pid = htonl (t->bck_pid);
6113 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
6114 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
6115 ©->header, GNUNET_NO);
6116 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6120 " not for us, retransmitting...\n");
6122 peer_info = peer_info_get (&msg->oid);
6123 if (NULL == peer_info)
6125 /* unknown origin of tunnel */
6129 predecessor = tree_get_predecessor (t->tree);
6130 if (0 == predecessor)
6132 if (GNUNET_YES == t->destroy)
6134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6135 "to orig received on a dying tunnel %s [%X]\n",
6136 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6140 "unknown to origin at %s\n",
6141 GNUNET_i2s (&my_full_id));
6142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6146 "for tunnel %s [%X]\n",
6147 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6150 tree_debug (t->tree);
6153 GNUNET_PEER_resolve (predecessor, &id);
6154 send_prebuilt_message (message, &id, t);
6155 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6162 * Core handler for mesh network traffic point-to-point acks.
6164 * @param cls closure
6165 * @param message message
6166 * @param peer peer identity this notification is about
6167 * @param atsi performance data
6168 * @param atsi_count number of records in 'atsi'
6170 * @return GNUNET_OK to keep the connection open,
6171 * GNUNET_SYSERR to close it (signal serious error)
6174 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6175 const struct GNUNET_MessageHeader *message,
6176 const struct GNUNET_ATS_Information *atsi,
6177 unsigned int atsi_count)
6179 struct GNUNET_MESH_ACK *msg;
6180 struct MeshTunnel *t;
6183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6185 msg = (struct GNUNET_MESH_ACK *) message;
6187 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6191 /* TODO notify that we dont know this tunnel (whom)? */
6192 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6195 ack = ntohl (msg->pid);
6196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6198 /* Is this a forward or backward ACK? */
6199 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6201 struct MeshTunnelChildInfo *cinfo;
6204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6205 cinfo = tunnel_get_neighbor_fc (t, peer);
6206 cinfo->fwd_ack = ack;
6207 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6208 tunnel_unlock_fwd_queues (t);
6209 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6211 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6212 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6213 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6220 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6221 tunnel_unlock_bck_queue (t);
6228 * Core handler for mesh network traffic point-to-point ack polls.
6230 * @param cls closure
6231 * @param message message
6232 * @param peer peer identity this notification is about
6233 * @param atsi performance data
6234 * @param atsi_count number of records in 'atsi'
6236 * @return GNUNET_OK to keep the connection open,
6237 * GNUNET_SYSERR to close it (signal serious error)
6240 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6241 const struct GNUNET_MessageHeader *message,
6242 const struct GNUNET_ATS_Information *atsi,
6243 unsigned int atsi_count)
6245 struct GNUNET_MESH_Poll *msg;
6246 struct MeshTunnel *t;
6248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6251 msg = (struct GNUNET_MESH_Poll *) message;
6253 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6257 /* TODO notify that we dont know this tunnel (whom)? */
6258 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6259 GNUNET_break_op (0);
6263 /* Is this a forward or backward ACK? */
6264 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6266 struct MeshTunnelChildInfo *cinfo;
6268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6269 cinfo = tunnel_get_neighbor_fc (t, peer);
6270 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6271 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6276 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6284 * Core handler for path ACKs
6286 * @param cls closure
6287 * @param message message
6288 * @param peer peer identity this notification is about
6289 * @param atsi performance data
6290 * @param atsi_count number of records in 'atsi'
6292 * @return GNUNET_OK to keep the connection open,
6293 * GNUNET_SYSERR to close it (signal serious error)
6296 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6297 const struct GNUNET_MessageHeader *message,
6298 const struct GNUNET_ATS_Information *atsi,
6299 unsigned int atsi_count)
6301 struct GNUNET_MESH_PathACK *msg;
6302 struct GNUNET_PeerIdentity id;
6303 struct MeshPeerInfo *peer_info;
6304 struct MeshPeerPath *p;
6305 struct MeshTunnel *t;
6307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6308 GNUNET_i2s (&my_full_id));
6309 msg = (struct GNUNET_MESH_PathACK *) message;
6310 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6313 /* TODO notify that we don't know the tunnel */
6314 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6316 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6320 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6322 peer_info = peer_info_get (&msg->peer_id);
6323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6324 GNUNET_i2s (&msg->peer_id));
6325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6328 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6331 "connect_by_string completed, stopping search\n");
6332 regex_cancel_search (t->regex_ctx);
6333 t->regex_ctx = NULL;
6336 /* Add paths to peers? */
6337 p = tree_get_path_to_peer (t->tree, peer_info->id);
6340 path_add_to_peers (p, GNUNET_YES);
6348 /* Message for us? */
6349 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6352 if (NULL == t->owner)
6354 GNUNET_break_op (0);
6357 if (NULL != t->dht_get_type)
6359 GNUNET_DHT_get_stop (t->dht_get_type);
6360 t->dht_get_type = NULL;
6362 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6364 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6365 send_client_peer_connected (t, peer_info->id);
6370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6371 " not for us, retransmitting...\n");
6372 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6373 peer_info = peer_info_get (&msg->oid);
6374 if (NULL == peer_info)
6376 /* If we know the tunnel, we should DEFINITELY know the peer */
6380 send_prebuilt_message (message, &id, t);
6386 * Core handler for mesh keepalives.
6388 * @param cls closure
6389 * @param message message
6390 * @param peer peer identity this notification is about
6391 * @param atsi performance data
6392 * @param atsi_count number of records in 'atsi'
6393 * @return GNUNET_OK to keep the connection open,
6394 * GNUNET_SYSERR to close it (signal serious error)
6396 * TODO: Check who we got this from, to validate route.
6399 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6400 const struct GNUNET_MessageHeader *message,
6401 const struct GNUNET_ATS_Information *atsi,
6402 unsigned int atsi_count)
6404 struct GNUNET_MESH_TunnelKeepAlive *msg;
6405 struct MeshTunnel *t;
6407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6410 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6411 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6415 /* TODO notify that we dont know that tunnel */
6416 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6417 GNUNET_break_op (0);
6421 tunnel_reset_timeout (t);
6423 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6424 tunnel_send_multicast (t, message);
6431 * Functions to handle messages from core
6433 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6434 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6435 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6436 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6437 sizeof (struct GNUNET_MESH_PathBroken)},
6438 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6439 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6440 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6441 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6442 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6443 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6444 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6445 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6446 sizeof (struct GNUNET_MESH_ACK)},
6447 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6448 sizeof (struct GNUNET_MESH_Poll)},
6449 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6450 sizeof (struct GNUNET_MESH_PathACK)},
6456 /******************************************************************************/
6457 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6458 /******************************************************************************/
6461 * deregister_app: iterator for removing each application registered by a client
6463 * @param cls closure
6464 * @param key the hash of the application id (used to access the hashmap)
6465 * @param value the value stored at the key (client)
6467 * @return GNUNET_OK on success
6470 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6472 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6473 GNUNET_break (GNUNET_YES ==
6474 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6480 * notify_client_connection_failure: notify a client that the connection to the
6481 * requested remote peer is not possible (for instance, no route found)
6482 * Function called when the socket is ready to queue more data. "buf" will be
6483 * NULL and "size" zero if the socket was closed for writing in the meantime.
6485 * @param cls closure
6486 * @param size number of bytes available in buf
6487 * @param buf where the callee should write the message
6488 * @return number of bytes written to buf
6491 notify_client_connection_failure (void *cls, size_t size, void *buf)
6494 struct MeshPeerInfo *peer_info;
6495 struct GNUNET_MESH_PeerControl *msg;
6496 struct GNUNET_PeerIdentity id;
6498 if (0 == size && NULL == buf)
6500 // TODO retry? cancel?
6504 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6505 peer_info = (struct MeshPeerInfo *) cls;
6506 msg = (struct GNUNET_MESH_PeerControl *) buf;
6507 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6508 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6509 // msg->tunnel_id = htonl(peer_info->t->tid);
6510 GNUNET_PEER_resolve (peer_info->id, &id);
6511 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6519 * Send keepalive packets for a peer
6521 * @param cls Closure (tunnel for which to send the keepalive).
6522 * @param tc Notification context.
6525 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6527 struct MeshTunnel *t = cls;
6528 struct GNUNET_MESH_TunnelKeepAlive *msg;
6529 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6532 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6533 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6539 "sending keepalive for tunnel %d\n", t->id.tid);
6541 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6542 msg->header.size = htons (size);
6543 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6544 msg->oid = my_full_id;
6545 msg->tid = htonl (t->id.tid);
6546 tunnel_send_multicast (t, &msg->header);
6548 t->path_refresh_task =
6549 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6550 tunnel_reset_timeout(t);
6555 * Function to process paths received for a new peer addition. The recorded
6556 * paths form the initial tunnel, which can be optimized later.
6557 * Called on each result obtained for the DHT search.
6559 * @param cls closure
6560 * @param exp when will this value expire
6561 * @param key key of the result
6562 * @param get_path path of the get request
6563 * @param get_path_length lenght of get_path
6564 * @param put_path path of the put request
6565 * @param put_path_length length of the put_path
6566 * @param type type of the result
6567 * @param size number of bytes in data
6568 * @param data pointer to the result data
6570 * TODO: re-issue the request after certain time? cancel after X results?
6573 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6574 const struct GNUNET_HashCode * key,
6575 const struct GNUNET_PeerIdentity *get_path,
6576 unsigned int get_path_length,
6577 const struct GNUNET_PeerIdentity *put_path,
6578 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6579 size_t size, const void *data)
6581 struct MeshPathInfo *path_info = cls;
6582 struct MeshPeerPath *p;
6583 struct GNUNET_PeerIdentity pi;
6586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6587 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6590 p = path_build_from_dht (get_path, get_path_length, put_path,
6592 path_add_to_peers (p, GNUNET_NO);
6594 for (i = 0; i < path_info->peer->ntunnels; i++)
6596 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6597 peer_info_connect (path_info->peer, path_info->t);
6605 * Function to process paths received for a new peer addition. The recorded
6606 * paths form the initial tunnel, which can be optimized later.
6607 * Called on each result obtained for the DHT search.
6609 * @param cls closure
6610 * @param exp when will this value expire
6611 * @param key key of the result
6612 * @param get_path path of the get request
6613 * @param get_path_length lenght of get_path
6614 * @param put_path path of the put request
6615 * @param put_path_length length of the put_path
6616 * @param type type of the result
6617 * @param size number of bytes in data
6618 * @param data pointer to the result data
6621 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6622 const struct GNUNET_HashCode * key,
6623 const struct GNUNET_PeerIdentity *get_path,
6624 unsigned int get_path_length,
6625 const struct GNUNET_PeerIdentity *put_path,
6626 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6627 size_t size, const void *data)
6629 const struct PBlock *pb = data;
6630 const struct GNUNET_PeerIdentity *pi = &pb->id;
6631 struct MeshTunnel *t = cls;
6632 struct MeshPeerInfo *peer_info;
6633 struct MeshPeerPath *p;
6635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6636 if (size != sizeof (struct PBlock))
6638 GNUNET_break_op (0);
6641 if (ntohl(pb->type) != t->type)
6643 GNUNET_break_op (0);
6646 GNUNET_assert (NULL != t->owner);
6647 peer_info = peer_info_get (pi);
6648 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6650 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6652 p = path_build_from_dht (get_path, get_path_length, put_path,
6654 path_add_to_peers (p, GNUNET_NO);
6656 tunnel_add_peer (t, peer_info);
6657 peer_info_connect (peer_info, t);
6662 * Function to process DHT string to regex matching.
6663 * Called on each result obtained for the DHT search.
6665 * @param cls closure (search context)
6666 * @param exp when will this value expire
6667 * @param key key of the result
6668 * @param get_path path of the get request (not used)
6669 * @param get_path_length lenght of get_path (not used)
6670 * @param put_path path of the put request (not used)
6671 * @param put_path_length length of the put_path (not used)
6672 * @param type type of the result
6673 * @param size number of bytes in data
6674 * @param data pointer to the result data
6677 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6678 const struct GNUNET_HashCode * key,
6679 const struct GNUNET_PeerIdentity *get_path,
6680 unsigned int get_path_length,
6681 const struct GNUNET_PeerIdentity *put_path,
6682 unsigned int put_path_length,
6683 enum GNUNET_BLOCK_Type type,
6684 size_t size, const void *data)
6686 const struct MeshRegexAccept *block = data;
6687 struct MeshRegexSearchContext *ctx = cls;
6688 struct MeshRegexSearchInfo *info = ctx->info;
6689 // struct MeshPeerPath *p;
6690 struct MeshPeerInfo *peer_info;
6692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6694 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6696 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6699 peer_info = peer_info_get(&block->id);
6700 // p = path_build_from_dht (get_path, get_path_length, put_path,
6701 // put_path_length);
6702 // path_add_to_peers (p, GNUNET_NO);
6705 tunnel_add_peer (info->t, peer_info);
6706 peer_info_connect (peer_info, info->t);
6707 if (0 == info->peer)
6709 info->peer = peer_info->id;
6713 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6716 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6717 ®ex_connect_timeout,
6725 * Function to process DHT string to regex matching.
6726 * Called on each result obtained for the DHT search.
6728 * @param cls closure (search context)
6729 * @param exp when will this value expire
6730 * @param key key of the result
6731 * @param get_path path of the get request (not used)
6732 * @param get_path_length lenght of get_path (not used)
6733 * @param put_path path of the put request (not used)
6734 * @param put_path_length length of the put_path (not used)
6735 * @param type type of the result
6736 * @param size number of bytes in data
6737 * @param data pointer to the result data
6739 * TODO: re-issue the request after certain time? cancel after X results?
6742 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6743 const struct GNUNET_HashCode * key,
6744 const struct GNUNET_PeerIdentity *get_path,
6745 unsigned int get_path_length,
6746 const struct GNUNET_PeerIdentity *put_path,
6747 unsigned int put_path_length,
6748 enum GNUNET_BLOCK_Type type,
6749 size_t size, const void *data)
6751 const struct MeshRegexBlock *block = data;
6752 struct MeshRegexSearchContext *ctx = cls;
6753 struct MeshRegexSearchInfo *info = ctx->info;
6757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6758 "DHT GET STRING RETURNED RESULTS\n");
6759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6760 " key: %s\n", GNUNET_h2s (key));
6762 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6764 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6767 copy = GNUNET_malloc (size);
6768 memcpy (copy, data, size);
6769 GNUNET_break (GNUNET_OK ==
6770 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6771 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6772 len = ntohl (block->n_proof);
6774 char proof[len + 1];
6776 memcpy (proof, &block[1], len);
6778 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6780 GNUNET_break_op (0);
6784 len = strlen (info->description);
6785 if (len == ctx->position) // String processed
6787 if (GNUNET_YES == ntohl (block->accepting))
6789 regex_find_path(key, ctx);
6793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6794 // FIXME REGEX this block not successful, wait for more? start timeout?
6799 regex_next_edge (block, size, ctx);
6804 /******************************************************************************/
6805 /********************* MESH LOCAL HANDLES **************************/
6806 /******************************************************************************/
6810 * Handler for client disconnection
6812 * @param cls closure
6813 * @param client identification of the client; NULL
6814 * for the last call when the server is destroyed
6817 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6819 struct MeshClient *c;
6820 struct MeshClient *next;
6823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6833 if (c->handle != client)
6835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6841 GNUNET_SERVER_client_drop (c->handle);
6842 c->shutting_down = GNUNET_YES;
6843 GNUNET_assert (NULL != c->own_tunnels);
6844 GNUNET_assert (NULL != c->incoming_tunnels);
6845 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6846 &tunnel_destroy_iterator, c);
6847 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6848 &tunnel_destroy_iterator, c);
6849 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6850 &tunnel_destroy_iterator, c);
6851 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6852 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6853 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6855 /* deregister clients applications */
6856 if (NULL != c->apps)
6858 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6859 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6861 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6862 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6864 GNUNET_SCHEDULER_cancel (announce_applications_task);
6865 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6867 if (NULL != c->types)
6868 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6869 for (i = 0; i < c->n_regex; i++)
6871 GNUNET_free (c->regexes[i].regex);
6872 if (NULL != c->regexes[i].dfa)
6873 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6875 GNUNET_free_non_null (c->regexes);
6876 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6877 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6879 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6882 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6891 * Handler for new clients
6893 * @param cls closure
6894 * @param client identification of the client
6895 * @param message the actual message, which includes messages the client wants
6898 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6899 const struct GNUNET_MessageHeader *message)
6901 struct GNUNET_MESH_ClientConnect *cc_msg;
6902 struct MeshClient *c;
6903 GNUNET_MESH_ApplicationType *a;
6910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6911 /* Check data sanity */
6912 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6913 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6914 ntypes = ntohs (cc_msg->types);
6915 napps = ntohs (cc_msg->applications);
6917 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6920 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6924 /* Create new client structure */
6925 c = GNUNET_malloc (sizeof (struct MeshClient));
6926 c->id = next_client_id++;
6927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6929 GNUNET_SERVER_client_keep (client);
6930 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6933 GNUNET_MESH_ApplicationType at;
6934 struct GNUNET_HashCode hc;
6936 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6937 for (i = 0; i < napps; i++)
6940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6941 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6942 /* store in clients hashmap */
6943 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6945 /* store in global hashmap, for announcements */
6946 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6947 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6949 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6950 announce_applications_task =
6951 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6957 struct GNUNET_HashCode hc;
6959 t = (uint16_t *) & a[napps];
6960 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6961 for (i = 0; i < ntypes; i++)
6964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6965 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6967 /* store in clients hashmap */
6968 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6969 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6970 /* store in global hashmap */
6971 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6972 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6976 " client has %u+%u subscriptions\n", napps, ntypes);
6978 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6979 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6980 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6981 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6982 GNUNET_SERVER_notification_context_add (nc, client);
6983 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6985 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6991 * Handler for clients announcing available services by a regular expression.
6993 * @param cls closure
6994 * @param client identification of the client
6995 * @param message the actual message, which includes messages the client wants
6998 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6999 const struct GNUNET_MessageHeader *message)
7001 const struct GNUNET_MESH_RegexAnnounce *msg;
7002 struct MeshRegexDescriptor rd;
7003 struct MeshClient *c;
7008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
7010 /* Sanity check for client registration */
7011 if (NULL == (c = client_get (client)))
7014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7019 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
7021 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
7022 if (NULL != c->partial_regex)
7024 regex = c->partial_regex;
7025 offset = strlen (c->partial_regex);
7026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7027 " continuation, already have %u bytes\n",
7036 regex = GNUNET_realloc (regex, offset + len + 1);
7037 memcpy (®ex[offset], &msg[1], len);
7038 regex[offset + len] = '\0';
7039 if (0 == ntohs (msg->last))
7041 c->partial_regex = regex;
7042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7043 " not ended, stored %u bytes for later\n",
7045 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7049 rd.compression = ntohs (msg->compression_characters);
7051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
7052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
7053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
7054 GNUNET_array_append (c->regexes, c->n_regex, rd);
7055 c->partial_regex = NULL;
7056 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
7058 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
7064 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
7070 * Handler for requests of new tunnels
7072 * @param cls closure
7073 * @param client identification of the client
7074 * @param message the actual message
7077 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
7078 const struct GNUNET_MessageHeader *message)
7080 struct GNUNET_MESH_TunnelMessage *t_msg;
7081 struct MeshTunnel *t;
7082 struct MeshClient *c;
7083 MESH_TunnelNumber tid;
7085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
7087 /* Sanity check for client registration */
7088 if (NULL == (c = client_get (client)))
7091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7096 /* Message sanity check */
7097 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7100 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7104 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7105 /* Sanity check for tunnel numbering */
7106 tid = ntohl (t_msg->tunnel_id);
7107 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
7110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7113 /* Sanity check for duplicate tunnel IDs */
7114 if (NULL != tunnel_get_by_local_id (c, tid))
7117 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7121 while (NULL != tunnel_get_by_pi (myid, next_tid))
7122 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7123 t = tunnel_new (myid, next_tid++, c, tid);
7126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
7128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7131 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
7133 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
7134 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
7137 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7143 * Handler for requests of deleting tunnels
7145 * @param cls closure
7146 * @param client identification of the client
7147 * @param message the actual message
7150 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
7151 const struct GNUNET_MessageHeader *message)
7153 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7154 struct MeshClient *c;
7155 struct MeshTunnel *t;
7156 MESH_TunnelNumber tid;
7158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7159 "Got a DESTROY TUNNEL from client!\n");
7161 /* Sanity check for client registration */
7162 if (NULL == (c = client_get (client)))
7165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7170 /* Message sanity check */
7171 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7174 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7178 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7180 /* Retrieve tunnel */
7181 tid = ntohl (tunnel_msg->tunnel_id);
7182 t = tunnel_get_by_local_id(c, tid);
7185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7190 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7192 client_ignore_tunnel (c, t);
7193 tunnel_destroy_empty (t);
7194 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7197 send_client_tunnel_disconnect(t, c);
7198 client_delete_tunnel(c, t);
7200 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7202 tunnel_send_destroy (t, GNUNET_YES);
7203 t->destroy = GNUNET_YES;
7204 // The tunnel will be destroyed when the last message is transmitted.
7205 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7211 * Handler for requests of seeting tunnel's speed.
7213 * @param cls Closure (unused).
7214 * @param client Identification of the client.
7215 * @param message The actual message.
7218 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7219 const struct GNUNET_MessageHeader *message)
7221 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7222 struct MeshClient *c;
7223 struct MeshTunnel *t;
7224 MESH_TunnelNumber tid;
7226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7227 "Got a SPEED request from client!\n");
7229 /* Sanity check for client registration */
7230 if (NULL == (c = client_get (client)))
7233 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7239 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7241 /* Retrieve tunnel */
7242 tid = ntohl (tunnel_msg->tunnel_id);
7243 t = tunnel_get_by_local_id(c, tid);
7246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7252 switch (ntohs(message->type))
7254 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7255 t->speed_min = GNUNET_YES;
7257 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7258 t->speed_min = GNUNET_NO;
7263 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7268 * Handler for requests of seeting tunnel's buffering policy.
7270 * @param cls Closure (unused).
7271 * @param client Identification of the client.
7272 * @param message The actual message.
7275 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7276 const struct GNUNET_MessageHeader *message)
7278 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7279 struct MeshClient *c;
7280 struct MeshTunnel *t;
7281 MESH_TunnelNumber tid;
7283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7284 "Got a BUFFER request from client!\n");
7286 /* Sanity check for client registration */
7287 if (NULL == (c = client_get (client)))
7290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7295 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7297 /* Retrieve tunnel */
7298 tid = ntohl (tunnel_msg->tunnel_id);
7299 t = tunnel_get_by_local_id(c, tid);
7302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7308 switch (ntohs(message->type))
7310 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7311 t->nobuffer = GNUNET_NO;
7313 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7314 t->nobuffer = GNUNET_YES;
7320 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7325 * Handler for connection requests to new peers
7327 * @param cls closure
7328 * @param client identification of the client
7329 * @param message the actual message (PeerControl)
7332 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7333 const struct GNUNET_MessageHeader *message)
7335 struct GNUNET_MESH_PeerControl *peer_msg;
7336 struct MeshPeerInfo *peer_info;
7337 struct MeshClient *c;
7338 struct MeshTunnel *t;
7339 MESH_TunnelNumber tid;
7341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7342 /* Sanity check for client registration */
7343 if (NULL == (c = client_get (client)))
7346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7351 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7353 /* Sanity check for message size */
7354 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7361 /* Tunnel exists? */
7362 tid = ntohl (peer_msg->tunnel_id);
7363 t = tunnel_get_by_local_id (c, tid);
7367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7371 /* Does client own tunnel? */
7372 if (t->owner->handle != client)
7375 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7379 GNUNET_i2s (&peer_msg->peer));
7380 peer_info = peer_info_get (&peer_msg->peer);
7382 tunnel_add_peer (t, peer_info);
7383 peer_info_connect (peer_info, t);
7385 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7391 * Handler for disconnection requests of peers in a tunnel
7393 * @param cls closure
7394 * @param client identification of the client
7395 * @param message the actual message (PeerControl)
7398 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7399 const struct GNUNET_MessageHeader *message)
7401 struct GNUNET_MESH_PeerControl *peer_msg;
7402 struct MeshPeerInfo *peer_info;
7403 struct MeshClient *c;
7404 struct MeshTunnel *t;
7405 MESH_TunnelNumber tid;
7407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7408 /* Sanity check for client registration */
7409 if (NULL == (c = client_get (client)))
7412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7417 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7419 /* Sanity check for message size */
7420 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7427 /* Tunnel exists? */
7428 tid = ntohl (peer_msg->tunnel_id);
7429 t = tunnel_get_by_local_id (c, tid);
7433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7438 /* Does client own tunnel? */
7439 if (t->owner->handle != client)
7442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7447 GNUNET_i2s (&peer_msg->peer));
7448 /* Is the peer in the tunnel? */
7450 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7451 if (NULL == peer_info)
7454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7458 /* Ok, delete peer from tunnel */
7459 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7460 &peer_msg->peer.hashPubKey);
7462 send_destroy_path (t, peer_info->id);
7463 tunnel_delete_peer (t, peer_info->id);
7464 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7469 * Handler for blacklist requests of peers in a tunnel
7471 * @param cls closure
7472 * @param client identification of the client
7473 * @param message the actual message (PeerControl)
7475 * FIXME implement DHT block bloomfilter
7478 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7479 const struct GNUNET_MessageHeader *message)
7481 struct GNUNET_MESH_PeerControl *peer_msg;
7482 struct MeshClient *c;
7483 struct MeshTunnel *t;
7484 MESH_TunnelNumber tid;
7486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7487 /* Sanity check for client registration */
7488 if (NULL == (c = client_get (client)))
7491 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7496 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7498 /* Sanity check for message size */
7499 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7506 /* Tunnel exists? */
7507 tid = ntohl (peer_msg->tunnel_id);
7508 t = tunnel_get_by_local_id (c, tid);
7512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7517 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7518 GNUNET_PEER_intern(&peer_msg->peer));
7523 * Handler for unblacklist requests of peers in a tunnel
7525 * @param cls closure
7526 * @param client identification of the client
7527 * @param message the actual message (PeerControl)
7530 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7531 const struct GNUNET_MessageHeader *message)
7533 struct GNUNET_MESH_PeerControl *peer_msg;
7534 struct MeshClient *c;
7535 struct MeshTunnel *t;
7536 MESH_TunnelNumber tid;
7540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7541 /* Sanity check for client registration */
7542 if (NULL == (c = client_get (client)))
7545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7550 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7552 /* Sanity check for message size */
7553 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7560 /* Tunnel exists? */
7561 tid = ntohl (peer_msg->tunnel_id);
7562 t = tunnel_get_by_local_id (c, tid);
7566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7571 /* if peer is not known, complain */
7572 pid = GNUNET_PEER_search (&peer_msg->peer);
7579 /* search and remove from list */
7580 for (i = 0; i < t->nblacklisted; i++)
7582 if (t->blacklisted[i] == pid)
7584 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7585 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7590 /* if peer hasn't been blacklisted, complain */
7596 * Handler for connection requests to new peers by type
7598 * @param cls closure
7599 * @param client identification of the client
7600 * @param message the actual message (ConnectPeerByType)
7603 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7604 const struct GNUNET_MessageHeader *message)
7606 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7607 struct MeshClient *c;
7608 struct MeshTunnel *t;
7609 struct GNUNET_HashCode hash;
7610 MESH_TunnelNumber tid;
7612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7613 /* Sanity check for client registration */
7614 if (NULL == (c = client_get (client)))
7617 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7622 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7624 /* Sanity check for message size */
7625 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7626 ntohs (connect_msg->header.size))
7629 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7633 /* Tunnel exists? */
7634 tid = ntohl (connect_msg->tunnel_id);
7635 t = tunnel_get_by_local_id (c, tid);
7639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7643 /* Does client own tunnel? */
7644 if (t->owner->handle != client)
7647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7651 /* Do WE have the service? */
7652 t->type = ntohl (connect_msg->type);
7653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7654 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7655 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7658 /* Yes! Fast forward, add ourselves to the tunnel and send the
7659 * good news to the client, and alert the destination client of
7660 * an incoming tunnel.
7662 * FIXME send a path create to self, avoid code duplication
7664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7665 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7666 peer_info_get (&my_full_id),
7667 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7670 send_client_peer_connected (t, myid);
7671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7672 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7674 t->local_tid_dest = next_local_tid++;
7675 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7676 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7677 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7681 /* Ok, lets find a peer offering the service */
7682 if (NULL != t->dht_get_type)
7684 GNUNET_DHT_get_stop (t->dht_get_type);
7686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7687 GNUNET_h2s (&hash));
7689 GNUNET_DHT_get_start (dht_handle,
7690 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7692 dht_replication_level,
7693 GNUNET_DHT_RO_RECORD_ROUTE |
7694 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7696 &dht_get_type_handler, t);
7698 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7704 * Handler for connection requests to new peers by a string service description.
7706 * @param cls closure
7707 * @param client identification of the client
7708 * @param message the actual message, which includes messages the client wants
7711 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7712 const struct GNUNET_MessageHeader *message)
7714 struct GNUNET_MESH_ConnectPeerByString *msg;
7715 struct MeshRegexSearchContext *ctx;
7716 struct MeshRegexSearchInfo *info;
7717 struct GNUNET_DHT_GetHandle *get_h;
7718 struct GNUNET_HashCode key;
7719 struct MeshTunnel *t;
7720 struct MeshClient *c;
7721 MESH_TunnelNumber tid;
7726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7727 "Connect by string started\n");
7728 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7729 size = htons (message->size);
7731 /* Sanity check for client registration */
7732 if (NULL == (c = client_get (client)))
7735 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7740 /* Message size sanity check */
7741 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7748 /* Tunnel exists? */
7749 tid = ntohl (msg->tunnel_id);
7750 t = tunnel_get_by_local_id (c, tid);
7754 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7758 /* Does client own tunnel? */
7759 if (t->owner->handle != client)
7762 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7767 " on tunnel %s [%u]\n",
7768 GNUNET_i2s(&my_full_id),
7771 /* Only one connect_by_string allowed at the same time! */
7772 /* FIXME: allow more, return handle at api level to cancel, document */
7773 if (NULL != t->regex_ctx)
7776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7780 /* Find string itself */
7781 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7782 string = (const char *) &msg[1];
7784 /* Initialize context */
7785 size = GNUNET_REGEX_get_first_key (string, len, &key);
7786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7787 " consumed %u bits out of %u\n", size, len);
7788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7789 " looking for %s\n", GNUNET_h2s (&key));
7791 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7793 info->description = GNUNET_strndup (string, len);
7794 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7795 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7798 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7799 ctx->position = size;
7803 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7805 /* Start search in DHT */
7806 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7807 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7808 &key, /* key to search */
7809 dht_replication_level, /* replication level */
7810 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7811 &info->description[size], /* xquery */
7812 // FIXME add BLOOMFILTER to exclude filtered peers
7813 len + 1 - size, /* xquery bits */
7814 // FIXME add BLOOMFILTER SIZE
7815 &dht_get_string_handler, ctx);
7817 GNUNET_break (GNUNET_OK ==
7818 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7821 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7823 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7829 * Handler for client traffic directed to one peer
7831 * @param cls closure
7832 * @param client identification of the client
7833 * @param message the actual message
7836 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7837 const struct GNUNET_MessageHeader *message)
7839 struct MeshClient *c;
7840 struct MeshTunnel *t;
7841 struct MeshPeerInfo *pi;
7842 struct GNUNET_MESH_Unicast *data_msg;
7843 MESH_TunnelNumber tid;
7846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7847 "Got a unicast request from a client!\n");
7849 /* Sanity check for client registration */
7850 if (NULL == (c = client_get (client)))
7853 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7858 data_msg = (struct GNUNET_MESH_Unicast *) message;
7860 /* Sanity check for message size */
7861 size = ntohs (message->size);
7862 if (sizeof (struct GNUNET_MESH_Unicast) +
7863 sizeof (struct GNUNET_MessageHeader) > size)
7866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7870 /* Tunnel exists? */
7871 tid = ntohl (data_msg->tid);
7872 t = tunnel_get_by_local_id (c, tid);
7876 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7880 /* Is it a local tunnel? Then, does client own the tunnel? */
7881 if (t->owner->handle != client)
7884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7888 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7889 &data_msg->destination.hashPubKey);
7890 /* Is the selected peer in the tunnel? */
7894 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7898 /* PID should be as expected */
7899 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7902 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7903 "Unicast PID, expected %u, got %u\n",
7904 t->fwd_pid + 1, ntohl (data_msg->pid));
7905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7909 /* Ok, everything is correct, send the message
7910 * (pretend we got it from a mesh peer)
7913 /* Work around const limitation */
7914 char buf[ntohs (message->size)] GNUNET_ALIGN;
7915 struct GNUNET_MESH_Unicast *copy;
7917 copy = (struct GNUNET_MESH_Unicast *) buf;
7918 memcpy (buf, data_msg, size);
7919 copy->oid = my_full_id;
7920 copy->tid = htonl (t->id.tid);
7921 copy->ttl = htonl (default_ttl);
7922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7923 " calling generic handler...\n");
7924 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7927 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7934 * Handler for client traffic directed to the origin
7936 * @param cls closure
7937 * @param client identification of the client
7938 * @param message the actual message
7941 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7942 const struct GNUNET_MessageHeader *message)
7944 struct GNUNET_MESH_ToOrigin *data_msg;
7945 struct MeshTunnelClientInfo *clinfo;
7946 struct MeshClient *c;
7947 struct MeshTunnel *t;
7948 MESH_TunnelNumber tid;
7951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7952 "Got a ToOrigin request from a client!\n");
7953 /* Sanity check for client registration */
7954 if (NULL == (c = client_get (client)))
7957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7962 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7964 /* Sanity check for message size */
7965 size = ntohs (message->size);
7966 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7967 sizeof (struct GNUNET_MessageHeader) > size)
7970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7974 /* Tunnel exists? */
7975 tid = ntohl (data_msg->tid);
7976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7977 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7980 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7983 t = tunnel_get_by_local_id (c, tid);
7986 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7987 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7989 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7993 /* It should be sent by someone who has this as incoming tunnel. */
7994 if (GNUNET_NO == client_knows_tunnel (c, t))
7997 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8001 /* PID should be as expected */
8002 clinfo = tunnel_get_client_fc (t, c);
8003 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
8006 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8007 "To Origin PID, expected %u, got %u\n",
8008 clinfo->bck_pid + 1,
8009 ntohl (data_msg->pid));
8010 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8015 /* Ok, everything is correct, send the message
8016 * (pretend we got it from a mesh peer)
8019 char buf[ntohs (message->size)] GNUNET_ALIGN;
8020 struct GNUNET_MESH_ToOrigin *copy;
8022 /* Work around const limitation */
8023 copy = (struct GNUNET_MESH_ToOrigin *) buf;
8024 memcpy (buf, data_msg, size);
8025 GNUNET_PEER_resolve (t->id.oid, ©->oid);
8026 copy->tid = htonl (t->id.tid);
8027 copy->ttl = htonl (default_ttl);
8028 copy->pid = htonl (t->bck_pid + 1);
8030 copy->sender = my_full_id;
8031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8032 " calling generic handler...\n");
8033 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
8035 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8042 * Handler for client traffic directed to all peers in a tunnel
8044 * @param cls closure
8045 * @param client identification of the client
8046 * @param message the actual message
8049 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
8050 const struct GNUNET_MessageHeader *message)
8052 struct MeshClient *c;
8053 struct MeshTunnel *t;
8054 struct GNUNET_MESH_Multicast *data_msg;
8055 MESH_TunnelNumber tid;
8057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8058 "Got a multicast request from a client!\n");
8060 /* Sanity check for client registration */
8061 if (NULL == (c = client_get (client)))
8064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8069 data_msg = (struct GNUNET_MESH_Multicast *) message;
8071 /* Sanity check for message size */
8072 if (sizeof (struct GNUNET_MESH_Multicast) +
8073 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
8076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8080 /* Tunnel exists? */
8081 tid = ntohl (data_msg->tid);
8082 t = tunnel_get_by_local_id (c, tid);
8086 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8087 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8089 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8093 /* Does client own tunnel? */
8094 if (t->owner->handle != client)
8097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8101 /* PID should be as expected */
8102 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
8105 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8106 "Multicast PID, expected %u, got %u\n",
8107 t->fwd_pid + 1, ntohl (data_msg->pid));
8108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8113 char buf[ntohs (message->size)] GNUNET_ALIGN;
8114 struct GNUNET_MESH_Multicast *copy;
8116 copy = (struct GNUNET_MESH_Multicast *) buf;
8117 memcpy (buf, message, ntohs (message->size));
8118 copy->oid = my_full_id;
8119 copy->tid = htonl (t->id.tid);
8120 copy->ttl = htonl (default_ttl);
8121 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
8122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8123 " calling generic handler...\n");
8124 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
8127 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
8133 * Handler for client's ACKs for payload traffic.
8135 * @param cls Closure (unused).
8136 * @param client Identification of the client.
8137 * @param message The actual message.
8140 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
8141 const struct GNUNET_MessageHeader *message)
8143 struct GNUNET_MESH_LocalAck *msg;
8144 struct MeshTunnel *t;
8145 struct MeshClient *c;
8146 MESH_TunnelNumber tid;
8149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
8150 /* Sanity check for client registration */
8151 if (NULL == (c = client_get (client)))
8154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8159 msg = (struct GNUNET_MESH_LocalAck *) message;
8161 /* Tunnel exists? */
8162 tid = ntohl (msg->tunnel_id);
8163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8164 t = tunnel_get_by_local_id (c, tid);
8168 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8169 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8174 ack = ntohl (msg->max_pid);
8175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8177 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8178 if (NULL != t->owner && t->owner->handle == client)
8180 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8182 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8186 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8187 tunnel_set_client_fwd_ack (t, c, ack);
8188 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8198 * Iterator over all peers to send a monitoring client info about a tunnel.
8200 * @param cls Closure (message being built).
8201 * @param key Key (hashed tunnel ID, unused).
8202 * @param value Peer info.
8204 * @return GNUNET_YES, to keep iterating.
8207 monitor_peers_iterator (void *cls,
8208 const struct GNUNET_HashCode * key,
8211 struct GNUNET_MESH_LocalMonitor *msg = cls;
8212 struct GNUNET_PeerIdentity *id;
8213 struct MeshPeerInfo *info = value;
8215 id = (struct GNUNET_PeerIdentity *) &msg[1];
8216 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8219 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8220 "* sending info about peer %s [%u]\n",
8221 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8229 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8231 * @param cls Closure (client handle).
8232 * @param key Key (hashed tunnel ID, unused).
8233 * @param value Tunnel info.
8235 * @return GNUNET_YES, to keep iterating.
8238 monitor_all_tunnels_iterator (void *cls,
8239 const struct GNUNET_HashCode * key,
8242 struct GNUNET_SERVER_Client *client = cls;
8243 struct MeshTunnel *t = value;
8244 struct GNUNET_MESH_LocalMonitor *msg;
8247 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8248 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8249 npeers * sizeof (struct GNUNET_PeerIdentity));
8250 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8251 msg->tunnel_id = htonl (t->id.tid);
8252 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8253 npeers * sizeof (struct GNUNET_PeerIdentity));
8254 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8256 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8257 monitor_peers_iterator,
8260 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8261 "* sending info about tunnel %s [%u] (%u peers)\n",
8262 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8264 if (msg->npeers != npeers)
8267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8268 "Get tunnels fail: size %u - iter %u\n",
8269 npeers, msg->npeers);
8272 msg->npeers = htonl (npeers);
8273 GNUNET_SERVER_notification_context_unicast (nc, client,
8274 &msg->header, GNUNET_NO);
8280 * Handler for client's MONITOR request.
8282 * @param cls Closure (unused).
8283 * @param client Identification of the client.
8284 * @param message The actual message.
8287 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8288 const struct GNUNET_MessageHeader *message)
8290 struct MeshClient *c;
8292 /* Sanity check for client registration */
8293 if (NULL == (c = client_get (client)))
8296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8300 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8301 "Received get tunnels request from client %u\n",
8303 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8304 monitor_all_tunnels_iterator,
8306 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8307 "Get tunnels request from client %u completed\n",
8309 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8314 * Data needed to build a Monitor_Tunnel message.
8316 struct MeshMonitorTunnelContext
8319 * Partial message, including peer count.
8321 struct GNUNET_MESH_LocalMonitor *msg;
8324 * Hashmap with positions: peer->position.
8326 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8329 * Index of the parent of each peer in the message, realtive to the absolute
8330 * order in the array (can be in a previous message).
8332 uint32_t parents[1024];
8335 * Peers visited so far in the tree, aka position of the current peer.
8337 unsigned int npeers;
8340 * Client requesting the info.
8342 struct MeshClient *c;
8347 * Send a client a message about the structure of a tunnel.
8349 * @param ctx Context of the tunnel iteration, with info regarding the state
8350 * of the execution and the number of peers visited for this message.
8353 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8355 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8356 struct GNUNET_PeerIdentity *pid;
8357 unsigned int *parent;
8360 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8361 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8362 resp->header.size = htons (size);
8363 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8364 parent = (unsigned int *) &pid[resp->npeers];
8365 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8366 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8367 &resp->header, GNUNET_NO);
8371 * Iterator over a tunnel tree to build a message containing all peers
8372 * the in the tunnel, including relay nodes.
8374 * @param cls Closure (pointer to pointer of message being built).
8375 * @param peer Short ID of a peer.
8376 * @param parent Short ID of the @c peer 's parent.
8379 tunnel_tree_iterator (void *cls,
8380 GNUNET_PEER_Id peer,
8381 GNUNET_PEER_Id parent)
8383 struct MeshMonitorTunnelContext *ctx = cls;
8384 struct GNUNET_MESH_LocalMonitor *msg;
8385 struct GNUNET_PeerIdentity *pid;
8386 struct GNUNET_PeerIdentity ppid;
8389 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8390 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8391 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8392 &pid[msg->npeers].hashPubKey,
8393 (void *) (long) ctx->npeers,
8394 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8395 GNUNET_PEER_resolve (parent, &ppid);
8396 ctx->parents[msg->npeers] =
8397 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8403 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8405 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8408 send_client_tunnel_info (ctx);
8415 * Handler for client's MONITOR_TUNNEL request.
8417 * @param cls Closure (unused).
8418 * @param client Identification of the client.
8419 * @param message The actual message.
8422 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8423 const struct GNUNET_MessageHeader *message)
8425 const struct GNUNET_MESH_LocalMonitor *msg;
8426 struct GNUNET_MESH_LocalMonitor *resp;
8427 struct MeshMonitorTunnelContext ctx;
8428 struct MeshClient *c;
8429 struct MeshTunnel *t;
8431 /* Sanity check for client registration */
8432 if (NULL == (c = client_get (client)))
8435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8439 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8440 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8441 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8444 ntohl (msg->tunnel_id));
8445 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8448 /* We don't know the tunnel */
8449 struct GNUNET_MESH_LocalMonitor warn;
8452 warn.npeers = htonl (UINT_MAX);
8453 GNUNET_SERVER_notification_context_unicast (nc, client,
8456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8460 /* Initialize context */
8461 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8465 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8469 /* Collect and send information */
8470 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8471 send_client_tunnel_info (&ctx);
8474 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8477 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8478 "Monitor tunnel request from client %u completed\n",
8480 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8485 * Functions to handle messages from clients
8487 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8488 {&handle_local_new_client, NULL,
8489 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8490 {&handle_local_announce_regex, NULL,
8491 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8492 {&handle_local_tunnel_create, NULL,
8493 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8494 sizeof (struct GNUNET_MESH_TunnelMessage)},
8495 {&handle_local_tunnel_destroy, NULL,
8496 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8497 sizeof (struct GNUNET_MESH_TunnelMessage)},
8498 {&handle_local_tunnel_speed, NULL,
8499 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8500 sizeof (struct GNUNET_MESH_TunnelMessage)},
8501 {&handle_local_tunnel_speed, NULL,
8502 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8503 sizeof (struct GNUNET_MESH_TunnelMessage)},
8504 {&handle_local_tunnel_buffer, NULL,
8505 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8506 sizeof (struct GNUNET_MESH_TunnelMessage)},
8507 {&handle_local_tunnel_buffer, NULL,
8508 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8509 sizeof (struct GNUNET_MESH_TunnelMessage)},
8510 {&handle_local_connect_add, NULL,
8511 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8512 sizeof (struct GNUNET_MESH_PeerControl)},
8513 {&handle_local_connect_del, NULL,
8514 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8515 sizeof (struct GNUNET_MESH_PeerControl)},
8516 {&handle_local_blacklist, NULL,
8517 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8518 sizeof (struct GNUNET_MESH_PeerControl)},
8519 {&handle_local_unblacklist, NULL,
8520 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8521 sizeof (struct GNUNET_MESH_PeerControl)},
8522 {&handle_local_connect_by_type, NULL,
8523 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8524 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8525 {&handle_local_connect_by_string, NULL,
8526 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8527 {&handle_local_unicast, NULL,
8528 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8529 {&handle_local_to_origin, NULL,
8530 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8531 {&handle_local_multicast, NULL,
8532 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8533 {&handle_local_ack, NULL,
8534 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8535 sizeof (struct GNUNET_MESH_LocalAck)},
8536 {&handle_local_get_tunnels, NULL,
8537 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8538 sizeof (struct GNUNET_MessageHeader)},
8539 {&handle_local_show_tunnel, NULL,
8540 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8541 sizeof (struct GNUNET_MESH_LocalMonitor)},
8547 * To be called on core init/fail.
8549 * @param cls service closure
8550 * @param server handle to the server for this service
8551 * @param identity the public identity of this peer
8554 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8555 const struct GNUNET_PeerIdentity *identity)
8558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8559 core_handle = server;
8560 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8564 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8566 GNUNET_i2s (identity));
8567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8569 GNUNET_i2s (&my_full_id));
8570 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8572 GNUNET_abort(); // Try harder
8579 * Method called whenever a given peer connects.
8581 * @param cls closure
8582 * @param peer peer identity this notification is about
8583 * @param atsi performance data for the connection
8584 * @param atsi_count number of records in 'atsi'
8587 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8588 const struct GNUNET_ATS_Information *atsi,
8589 unsigned int atsi_count)
8591 struct MeshPeerInfo *peer_info;
8592 struct MeshPeerPath *path;
8594 DEBUG_CONN ("Peer connected\n");
8595 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8596 peer_info = peer_info_get (peer);
8597 if (myid == peer_info->id)
8599 DEBUG_CONN (" (self)\n");
8604 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8606 path = path_new (2);
8607 path->peers[0] = myid;
8608 path->peers[1] = peer_info->id;
8609 GNUNET_PEER_change_rc (myid, 1);
8610 GNUNET_PEER_change_rc (peer_info->id, 1);
8611 peer_info_add_path (peer_info, path, GNUNET_YES);
8612 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8618 * Method called whenever a peer disconnects.
8620 * @param cls closure
8621 * @param peer peer identity this notification is about
8624 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8626 struct MeshPeerInfo *pi;
8627 struct MeshPeerQueue *q;
8628 struct MeshPeerQueue *n;
8630 DEBUG_CONN ("Peer disconnected\n");
8631 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8641 /* TODO try to reroute this traffic instead */
8642 queue_destroy(q, GNUNET_YES);
8645 if (NULL != pi->core_transmit)
8647 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8648 pi->core_transmit = NULL;
8650 peer_info_remove_path (pi, pi->id, myid);
8653 DEBUG_CONN (" (self)\n");
8655 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8660 /******************************************************************************/
8661 /************************ MAIN FUNCTIONS ****************************/
8662 /******************************************************************************/
8665 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8667 * @param cls closure
8668 * @param key current key code
8669 * @param value value in the hash map
8670 * @return GNUNET_YES if we should continue to iterate,
8674 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8676 struct MeshTunnel *t = value;
8683 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8685 * @param cls closure
8686 * @param key current key code
8687 * @param value value in the hash map
8688 * @return GNUNET_YES if we should continue to iterate,
8692 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8694 struct MeshPeerInfo *p = value;
8695 struct MeshPeerQueue *q;
8696 struct MeshPeerQueue *n;
8704 queue_destroy(q, GNUNET_YES);
8708 peer_info_destroy (p);
8714 * Task run during shutdown.
8720 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8724 if (core_handle != NULL)
8726 GNUNET_CORE_disconnect (core_handle);
8731 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8734 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8735 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8736 if (dht_handle != NULL)
8738 GNUNET_DHT_disconnect (dht_handle);
8743 GNUNET_SERVER_notification_context_destroy (nc);
8746 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8748 GNUNET_SCHEDULER_cancel (announce_id_task);
8749 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8751 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8753 GNUNET_SCHEDULER_cancel (announce_applications_task);
8754 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8761 * Callback for hostkey read/generation
8763 * @param cls Closure (Configuration handle).
8764 * @param pk the private key
8765 * @param emsg error message
8768 key_generation_cb (void *cls,
8769 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8772 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8773 struct MeshPeerInfo *peer;
8774 struct MeshPeerPath *p;
8779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8780 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8782 GNUNET_SCHEDULER_shutdown ();
8785 my_private_key = pk;
8786 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8787 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8788 &my_full_id.hashPubKey);
8789 myid = GNUNET_PEER_intern (&my_full_id);
8790 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8791 "Mesh for peer [%s] starting\n",
8792 GNUNET_i2s(&my_full_id));
8794 // transport_handle = GNUNET_TRANSPORT_connect(c,
8801 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8802 NULL, /* Closure passed to MESH functions */
8803 &core_init, /* Call core_init once connected */
8804 &core_connect, /* Handle connects */
8805 &core_disconnect, /* remove peers on disconnects */
8806 NULL, /* Don't notify about all incoming messages */
8807 GNUNET_NO, /* For header only in notification */
8808 NULL, /* Don't notify about all outbound messages */
8809 GNUNET_NO, /* For header-only out notification */
8810 core_handlers); /* Register these handlers */
8812 if (core_handle == NULL)
8815 GNUNET_SCHEDULER_shutdown ();
8820 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8823 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8824 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8825 GNUNET_SERVER_disconnect_notify (server_handle,
8826 &handle_local_client_disconnect, NULL);
8830 clients_tail = NULL;
8833 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8834 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8836 /* Create a peer_info for the local peer */
8837 peer = peer_info_get (&my_full_id);
8840 GNUNET_PEER_change_rc (myid, 1);
8841 peer_info_add_path (peer, p, GNUNET_YES);
8842 GNUNET_SERVER_resume (server_handle);
8843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8848 * Process mesh requests.
8850 * @param cls closure
8851 * @param server the initialized server
8852 * @param c configuration to use
8855 run (void *cls, struct GNUNET_SERVER_Handle *server,
8856 const struct GNUNET_CONFIGURATION_Handle *c)
8860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8861 server_handle = server;
8864 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8867 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8869 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8871 GNUNET_SCHEDULER_shutdown ();
8876 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8877 &refresh_path_time))
8879 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8881 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8882 "mesh", "refresh path time");
8883 GNUNET_SCHEDULER_shutdown ();
8888 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8889 &app_announce_time))
8891 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8893 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8894 "mesh", "app announce time");
8895 GNUNET_SCHEDULER_shutdown ();
8898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8899 "APP_ANNOUNCE_TIME %llu ms\n",
8900 app_announce_time.rel_value);
8902 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8905 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8907 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8908 "mesh", "id announce time");
8909 GNUNET_SCHEDULER_shutdown ();
8914 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8919 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8920 "mesh", "connect timeout");
8921 GNUNET_SCHEDULER_shutdown ();
8926 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8931 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8932 "mesh", "max msgs queue");
8933 GNUNET_SCHEDULER_shutdown ();
8938 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8941 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8943 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8944 "mesh", "max tunnels");
8945 GNUNET_SCHEDULER_shutdown ();
8950 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8955 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8956 "mesh", "default ttl", 64);
8961 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8964 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8965 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8966 "mesh", "max peers", 1000);
8971 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8972 &dht_replication_level))
8974 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8976 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8977 "mesh", "dht replication level", 3);
8978 dht_replication_level = 3;
8981 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8982 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8983 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8984 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8985 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8987 dht_handle = GNUNET_DHT_connect (c, 64);
8988 if (NULL == dht_handle)
8992 stats = GNUNET_STATISTICS_create ("mesh", c);
8994 GNUNET_SERVER_suspend (server_handle);
8995 /* Scheduled the task to clean up when shutdown is called */
8996 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8998 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
9001 GNUNET_free (keyfile);
9006 * The main function for the mesh service.
9008 * @param argc number of arguments from the command line
9009 * @param argv command line arguments
9010 * @return 0 ok, 1 on error
9013 main (int argc, char *const *argv)
9018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
9019 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
9021 ret = (GNUNET_OK == r) ? 0 : 1;
9022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
9026 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
9027 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
9028 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);