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 * Notify the owner of a tunnel that a peer has disconnected.
2162 * @param c Client (owner of tunnel).
2163 * @param t Tunnel this message is about.
2164 * @param peer_id Short ID of the disconnected peer.
2167 client_notify_peer_disconnected (struct MeshClient *c,
2168 struct MeshTunnel *t,
2169 GNUNET_PEER_Id peer_id)
2171 struct GNUNET_MESH_PeerControl msg;
2173 if (NULL == t->owner || NULL == nc)
2176 msg.header.size = htons (sizeof (msg));
2177 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2178 msg.tunnel_id = htonl (t->local_tid);
2179 GNUNET_PEER_resolve (peer_id, &msg.peer);
2180 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2181 &msg.header, GNUNET_NO);
2186 * Send the message to all clients that have subscribed to its type
2188 * @param msg Pointer to the message itself
2189 * @param payload Pointer to the payload of the message.
2190 * @param t The tunnel to whose clients this message goes.
2192 * @return number of clients this message was sent to
2195 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2196 const struct GNUNET_MessageHeader *payload,
2197 struct MeshTunnel *t)
2199 struct MeshClient *c;
2200 MESH_TunnelNumber *tid;
2203 char cbuf[htons (msg->size)];
2205 type = ntohs (payload->type);
2206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2208 GNUNET_MESH_DEBUG_M2S (type));
2210 memcpy (cbuf, msg, sizeof (cbuf));
2211 switch (htons (msg->type))
2213 struct GNUNET_MESH_Unicast *uc;
2214 struct GNUNET_MESH_Multicast *mc;
2215 struct GNUNET_MESH_ToOrigin *to;
2217 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2218 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2221 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2222 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2225 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2226 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2234 for (count = 0, c = clients; c != NULL; c = c->next)
2236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2237 if (client_is_subscribed (type, c))
2239 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2243 *tid = htonl (t->local_tid);
2247 if (GNUNET_NO == client_knows_tunnel (c, t))
2249 /* This client doesn't know the tunnel */
2250 struct GNUNET_MESH_TunnelNotification tmsg;
2251 struct GNUNET_HashCode hash;
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2254 tmsg.header.size = htons (sizeof (tmsg));
2255 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2256 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2257 tmsg.tunnel_id = htonl (t->local_tid_dest);
2259 if (GNUNET_YES == t->speed_min)
2260 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2261 if (GNUNET_YES == t->nobuffer)
2262 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2263 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2264 &tmsg.header, GNUNET_NO);
2265 tunnel_add_client (t, c);
2266 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2268 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2269 c->incoming_tunnels, &hash, t,
2270 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2272 *tid = htonl (t->local_tid_dest);
2275 /* Check if the client wants to get traffic from the tunnel */
2276 if (GNUNET_NO == client_wants_tunnel(c, t))
2279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2280 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2281 (struct GNUNET_MessageHeader
2282 *) cbuf, GNUNET_NO);
2291 * Notify the client that owns the tunnel that a peer has connected to it
2292 * (the requested path to it has been confirmed).
2294 * @param t Tunnel whose owner to notify
2295 * @param id Short id of the peer that has connected
2298 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2300 struct GNUNET_MESH_PeerControl pc;
2302 if (NULL == t->owner || GNUNET_YES == t->destroy)
2305 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2306 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2307 pc.tunnel_id = htonl (t->local_tid);
2308 GNUNET_PEER_resolve (id, &pc.peer);
2309 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2315 * Notify all clients (not depending on registration status) that the incoming
2316 * tunnel is no longer valid.
2318 * @param t Tunnel that was destroyed.
2321 send_clients_tunnel_destroy (struct MeshTunnel *t)
2323 struct GNUNET_MESH_TunnelMessage msg;
2325 msg.header.size = htons (sizeof (msg));
2326 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2327 msg.tunnel_id = htonl (t->local_tid_dest);
2328 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2333 * Notify clients of tunnel disconnections, if needed.
2334 * In case the origin disconnects, the destination clients get a tunnel destroy
2335 * notification. If the last destination disconnects (only one remaining client
2336 * in tunnel), the origin gets a (local ID) peer disconnected.
2337 * Note that the function must be called BEFORE removing the client from
2340 * @param t Tunnel that was destroyed.
2341 * @param c Client that disconnected.
2344 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2350 struct GNUNET_MESH_TunnelMessage msg;
2352 msg.header.size = htons (sizeof (msg));
2353 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2354 msg.tunnel_id = htonl (t->local_tid_dest);
2355 for (i = 0; i < t->nclients; i++)
2356 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2357 &msg.header, GNUNET_NO);
2359 // FIXME when to disconnect an incoming tunnel?
2360 else if (1 == t->nclients && NULL != t->owner)
2362 struct GNUNET_MESH_PeerControl msg;
2364 msg.header.size = htons (sizeof (msg));
2365 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2366 msg.tunnel_id = htonl (t->local_tid);
2367 msg.peer = my_full_id;
2368 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2369 &msg.header, GNUNET_NO);
2375 * Iterator over all the peers to remove the oldest not-used entry.
2377 * @param cls Closure (unsued).
2378 * @param key ID of the peer.
2379 * @param value Peer_Info of the peer.
2384 peer_info_timeout (void *cls,
2385 const struct GNUNET_HashCode *key,
2392 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2393 * and insert it in the appropiate structures if the peer is not known yet.
2395 * @param peer Full identity of the peer.
2397 * @return Existing or newly created peer info.
2399 static struct MeshPeerInfo *
2400 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2402 struct MeshPeerInfo *peer_info;
2404 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2405 if (NULL == peer_info)
2408 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2409 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2411 GNUNET_CONTAINER_multihashmap_iterate (peers,
2415 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2416 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2417 peer_info->id = GNUNET_PEER_intern (peer);
2419 peer_info->last_contact = GNUNET_TIME_absolute_get();
2426 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2427 * and insert it in the appropiate structures if the peer is not known yet.
2429 * @param peer Short identity of the peer.
2431 * @return Existing or newly created peer info.
2433 static struct MeshPeerInfo *
2434 peer_info_get_short (const GNUNET_PEER_Id peer)
2436 struct GNUNET_PeerIdentity id;
2438 GNUNET_PEER_resolve (peer, &id);
2439 return peer_info_get (&id);
2444 * Iterator to remove the tunnel from the list of tunnels a peer participates
2447 * @param cls Closure (tunnel info)
2448 * @param key GNUNET_PeerIdentity of the peer (unused)
2449 * @param value PeerInfo of the peer
2451 * @return always GNUNET_YES, to keep iterating
2454 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2456 struct MeshTunnel *t = cls;
2457 struct MeshPeerInfo *peer = value;
2460 for (i = 0; i < peer->ntunnels; i++)
2463 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2466 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2467 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2476 * Core callback to write a pre-constructed data packet to core buffer
2478 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2479 * @param size Number of bytes available in buf.
2480 * @param buf Where the to write the message.
2482 * @return number of bytes written to buf
2485 send_core_data_raw (void *cls, size_t size, void *buf)
2487 struct MeshTransmissionDescriptor *info = cls;
2488 struct GNUNET_MessageHeader *msg;
2491 GNUNET_assert (NULL != info);
2492 GNUNET_assert (NULL != info->mesh_data);
2493 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2494 total_size = ntohs (msg->size);
2496 if (total_size > size)
2501 memcpy (buf, msg, total_size);
2502 data_descriptor_decrement_rc (info->mesh_data);
2509 * Sends an already built non-multicast message to a peer,
2510 * properly registrating all used resources.
2512 * @param message Message to send. Function makes a copy of it.
2513 * @param peer Short ID of the neighbor whom to send the message.
2514 * @param t Tunnel on which this message is transmitted.
2517 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2518 const struct GNUNET_PeerIdentity *peer,
2519 struct MeshTunnel *t)
2521 struct MeshTransmissionDescriptor *info;
2522 struct MeshPeerInfo *neighbor;
2523 struct MeshPeerPath *p;
2527 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2529 size = ntohs (message->size);
2530 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2531 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2532 info->mesh_data->data = GNUNET_malloc (size);
2533 memcpy (info->mesh_data->data, message, size);
2534 type = ntohs(message->type);
2537 struct GNUNET_MESH_Unicast *m;
2538 struct GNUNET_MESH_ToOrigin *to;
2540 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2541 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2542 m->ttl = htonl (ntohl (m->ttl) - 1);
2544 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2545 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2547 to->pid = htonl(t->bck_pid);
2549 info->mesh_data->data_len = size;
2550 info->mesh_data->reference_counter = 1;
2551 info->mesh_data->total_out = 1;
2552 neighbor = peer_info_get (peer);
2553 for (p = neighbor->path_head; NULL != p; p = p->next)
2563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2564 " %s IS NOT DIRECTLY CONNECTED\n",
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2569 for (p = neighbor->path_head; NULL != p; p = p->next)
2571 struct GNUNET_PeerIdentity debug_id;
2574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2575 " path with %u hops through:\n",
2577 for (i = 0; i < p->length; i++)
2579 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2582 i, GNUNET_i2s(&debug_id));
2586 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2588 " no direct connection to %s\n",
2590 GNUNET_free (info->mesh_data->data);
2591 GNUNET_free (info->mesh_data);
2595 info->peer = neighbor;
2596 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2607 * Sends a CREATE PATH message for a path to a peer, properly registrating
2608 * all used resources.
2610 * @param peer PeerInfo of the final peer for whom this path is being created.
2611 * @param p Path itself.
2612 * @param t Tunnel for which the path is created.
2615 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2616 struct MeshTunnel *t)
2618 struct GNUNET_PeerIdentity id;
2619 struct MeshPathInfo *path_info;
2620 struct MeshPeerInfo *neighbor;
2626 p = tree_get_path_to_peer (t->tree, peer->id);
2633 for (i = 0; i < p->length; i++)
2635 if (p->peers[i] == myid)
2638 if (i >= p->length - 1)
2644 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2646 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2647 path_info->path = p;
2649 neighbor = peer_info_get (&id);
2650 path_info->peer = neighbor;
2651 queue_add (path_info,
2652 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2653 sizeof (struct GNUNET_MESH_ManipulatePath) +
2654 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2661 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2663 * @param t Tunnel whose path to destroy.
2664 * @param destination Short ID of the peer to whom the path to destroy.
2667 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2669 struct MeshPeerPath *p;
2672 p = tree_get_path_to_peer (t->tree, destination);
2678 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2679 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2681 struct GNUNET_MESH_ManipulatePath *msg;
2682 struct GNUNET_PeerIdentity *pi;
2686 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2687 msg->header.size = htons (size);
2688 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2689 msg->tid = htonl (t->id.tid);
2690 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2691 for (i = 0; i < p->length; i++)
2693 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2695 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2702 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2704 * @param t Tunnel which to confirm.
2707 send_path_ack (struct MeshTunnel *t)
2709 struct MeshTransmissionDescriptor *info;
2710 struct GNUNET_PeerIdentity id;
2711 GNUNET_PEER_Id peer;
2713 peer = tree_get_predecessor (t->tree);
2714 GNUNET_PEER_resolve (peer, &id);
2715 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2716 info->origin = &t->id;
2717 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2718 GNUNET_assert (NULL != info->peer);
2721 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2722 sizeof (struct GNUNET_MESH_PathACK),
2729 * Try to establish a new connection to this peer.
2730 * Use the best path for the given tunnel.
2731 * If the peer doesn't have any path to it yet, try to get one.
2732 * If the peer already has some path, send a CREATE PATH towards it.
2734 * @param peer PeerInfo of the peer.
2735 * @param t Tunnel for which to create the path, if possible.
2738 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2740 struct MeshPeerPath *p;
2741 struct MeshPathInfo *path_info;
2743 if (NULL != peer->path_head)
2745 p = tree_get_path_to_peer (t->tree, peer->id);
2752 // FIXME always send create path to self
2755 send_create_path (peer, p, t);
2759 struct GNUNET_HashCode hash;
2762 send_client_peer_connected (t, myid);
2763 t->local_tid_dest = next_local_tid++;
2764 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2767 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2768 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2775 else if (NULL == peer->dhtget)
2777 struct GNUNET_PeerIdentity id;
2779 GNUNET_PEER_resolve (peer->id, &id);
2780 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2781 path_info->peer = peer;
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2784 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2785 peer->dhtgetcls = path_info;
2786 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2787 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2788 &id.hashPubKey, /* key to search */
2789 dht_replication_level, /* replication level */
2790 GNUNET_DHT_RO_RECORD_ROUTE |
2791 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2792 NULL, /* xquery */ // FIXME BLOOMFILTER
2793 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2794 &dht_get_id_handler, path_info);
2796 /* Otherwise, there is no path but the DHT get is already started. */
2801 * Task to delay the connection of a peer
2803 * @param cls Closure (path info with tunnel and peer to connect).
2804 * Will be free'd on exection.
2805 * @param tc TaskContext
2808 peer_info_connect_task (void *cls,
2809 const struct GNUNET_SCHEDULER_TaskContext *tc)
2811 struct MeshPathInfo *path_info = cls;
2813 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2815 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2820 peer_info_connect (path_info->peer, path_info->t);
2826 * Destroy the peer_info and free any allocated resources linked to it
2828 * @param pi The peer_info to destroy.
2830 * @return GNUNET_OK on success
2833 peer_info_destroy (struct MeshPeerInfo *pi)
2835 struct GNUNET_PeerIdentity id;
2836 struct MeshPeerPath *p;
2837 struct MeshPeerPath *nextp;
2839 GNUNET_PEER_resolve (pi->id, &id);
2840 GNUNET_PEER_change_rc (pi->id, -1);
2843 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2847 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2849 if (NULL != pi->dhtget)
2851 GNUNET_DHT_get_stop (pi->dhtget);
2852 GNUNET_free (pi->dhtgetcls);
2858 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2862 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2864 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2872 * Remove all paths that rely on a direct connection between p1 and p2
2873 * from the peer itself and notify all tunnels about it.
2875 * @param peer PeerInfo of affected peer.
2876 * @param p1 GNUNET_PEER_Id of one peer.
2877 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2880 * TODO: optimize (see below)
2883 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2886 struct MeshPeerPath *p;
2887 struct MeshPeerPath *aux;
2888 struct MeshPeerInfo *peer_d;
2890 unsigned int destroyed;
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2897 p = peer->path_head;
2901 for (i = 0; i < (p->length - 1); i++)
2903 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2904 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2906 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2917 for (i = 0; i < peer->ntunnels; i++)
2919 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2923 * Problem: one or more peers have been deleted from the tunnel tree.
2924 * We don't know who they are to try to add them again.
2925 * We need to try to find a new path for each of the disconnected peers.
2926 * Some of them might already have a path to reach them that does not
2927 * involve p1 and p2. Adding all anew might render in a better tree than
2928 * the trivial immediate fix.
2930 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2931 * its children will be reachable trough him.
2933 peer_d = peer_info_get_short (d);
2936 for (p = peer_d->path_head; NULL != p; p = p->next)
2938 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2946 /* No callback, as peer will be already disconnected and a connection
2947 * scheduled by tunnel_notify_connection_broken.
2949 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2953 peer_info_connect (peer_d, peer->tunnels[i]);
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2961 * Add the path to the peer and update the path used to reach it in case this
2964 * @param peer_info Destination peer to add the path to.
2965 * @param path New path to add. Last peer must be the peer in arg 1.
2966 * Path will be either used of freed if already known.
2967 * @param trusted Do we trust that this path is real?
2970 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2973 struct MeshPeerPath *aux;
2977 if ((NULL == peer_info) || (NULL == path))
2980 path_destroy (path);
2983 if (path->peers[path->length - 1] != peer_info->id)
2986 path_destroy (path);
2989 if (path->length <= 2 && GNUNET_NO == trusted)
2991 /* Only allow CORE to tell us about direct paths */
2992 path_destroy (path);
2995 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2996 for (l = 1; l < path->length; l++)
2998 if (path->peers[l] == myid)
3000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
3001 for (l2 = 0; l2 < path->length - l; l2++)
3003 path->peers[l2] = path->peers[l + l2];
3008 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
3013 struct GNUNET_PeerIdentity id;
3015 GNUNET_PEER_resolve (peer_info->id, &id);
3016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
3017 path->length, GNUNET_i2s (&id));
3020 l = path_get_length (path);
3027 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
3028 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
3030 l2 = path_get_length (aux);
3033 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
3034 peer_info->path_tail, aux, path);
3039 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
3041 path_destroy (path);
3046 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
3053 * Add the path to the origin peer and update the path used to reach it in case
3054 * this is the shortest.
3055 * The path is given in peer_info -> destination, therefore we turn the path
3056 * upside down first.
3058 * @param peer_info Peer to add the path to, being the origin of the path.
3059 * @param path New path to add after being inversed.
3060 * @param trusted Do we trust that this path is real?
3063 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
3064 struct MeshPeerPath *path, int trusted)
3067 peer_info_add_path (peer_info, path, trusted);
3072 * Function called if the connection to the peer has been stalled for a while,
3073 * possibly due to a missed ACK. Poll the peer about its ACK status.
3075 * @param cls Closure (cinfo).
3076 * @param tc TaskContext.
3079 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3081 struct MeshTunnelChildInfo *cinfo = cls;
3082 struct GNUNET_MESH_Poll msg;
3083 struct GNUNET_PeerIdentity id;
3084 struct MeshTunnel *t;
3086 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3087 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3093 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3094 msg.header.size = htons (sizeof (msg));
3095 msg.tid = htonl (t->id.tid);
3096 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3097 msg.last_ack = htonl (cinfo->fwd_ack);
3099 GNUNET_PEER_resolve (cinfo->id, &id);
3100 send_prebuilt_message (&msg.header, &id, cinfo->t);
3101 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
3103 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
3104 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3105 &tunnel_poll, cinfo);
3110 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3111 * to obtain a local peer -> destination path and interning the peer ids.
3113 * @return Newly allocated and created path
3115 static struct MeshPeerPath *
3116 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3117 unsigned int get_path_length,
3118 const struct GNUNET_PeerIdentity *put_path,
3119 unsigned int put_path_length)
3121 struct MeshPeerPath *p;
3127 GNUNET_PEER_change_rc (myid, 1);
3128 i = get_path_length;
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3130 for (i--; i >= 0; i--)
3132 id = GNUNET_PEER_intern (&get_path[i]);
3133 if (p->length > 0 && id == p->peers[p->length - 1])
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3136 GNUNET_PEER_change_rc (id, -1);
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3141 GNUNET_i2s (&get_path[i]));
3143 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3144 p->peers[p->length - 1] = id;
3147 i = put_path_length;
3148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3149 for (i--; i >= 0; i--)
3151 id = GNUNET_PEER_intern (&put_path[i]);
3154 /* PUT path went through us, so discard the path up until now and start
3155 * from here to get a much shorter (and loop-free) path.
3160 if (p->length > 0 && id == p->peers[p->length - 1])
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3163 GNUNET_PEER_change_rc (id, -1);
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3168 GNUNET_i2s (&put_path[i]));
3170 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3171 p->peers[p->length - 1] = id;
3175 if (get_path_length > 0)
3176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3177 GNUNET_i2s (&get_path[0]));
3178 if (put_path_length > 0)
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3180 GNUNET_i2s (&put_path[0]));
3181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3183 for (i = 0; i < p->length; i++)
3185 struct GNUNET_PeerIdentity peer_id;
3187 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3189 GNUNET_i2s (&peer_id));
3197 * Adds a path to the peer_infos of all the peers in the path
3199 * @param p Path to process.
3200 * @param confirmed Whether we know if the path works or not.
3203 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3207 /* TODO: invert and add */
3208 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3209 for (i++; i < p->length; i++)
3211 struct MeshPeerInfo *aux;
3212 struct MeshPeerPath *copy;
3214 aux = peer_info_get_short (p->peers[i]);
3215 copy = path_duplicate (p);
3216 copy->length = i + 1;
3217 peer_info_add_path (aux, copy, GNUNET_NO);
3223 * Send keepalive packets for a peer
3225 * @param cls Closure (tunnel for which to send the keepalive).
3226 * @param tc Notification context.
3228 * TODO: implement explicit multicast keepalive?
3231 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3235 * Search for a tunnel among the incoming tunnels
3237 * @param tid the local id of the tunnel
3239 * @return tunnel handler, NULL if doesn't exist
3241 static struct MeshTunnel *
3242 tunnel_get_incoming (MESH_TunnelNumber tid)
3244 struct GNUNET_HashCode hash;
3246 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3247 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3248 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3253 * Search for a tunnel among the tunnels for a client
3255 * @param c the client whose tunnels to search in
3256 * @param tid the local id of the tunnel
3258 * @return tunnel handler, NULL if doesn't exist
3260 static struct MeshTunnel *
3261 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3263 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3265 return tunnel_get_incoming (tid);
3269 struct GNUNET_HashCode hash;
3271 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3272 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3278 * Search for a tunnel by global ID using PEER_ID
3280 * @param pi owner of the tunnel
3281 * @param tid global tunnel number
3283 * @return tunnel handler, NULL if doesn't exist
3285 static struct MeshTunnel *
3286 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3288 struct MESH_TunnelID id;
3289 struct GNUNET_HashCode hash;
3294 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3295 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3300 * Search for a tunnel by global ID using full PeerIdentities
3302 * @param oid owner of the tunnel
3303 * @param tid global tunnel number
3305 * @return tunnel handler, NULL if doesn't exist
3307 static struct MeshTunnel *
3308 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3310 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3315 * Delete an active client from the tunnel.
3321 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3325 for (i = 0; i < t->nclients; i++)
3327 if (t->clients[i] == c)
3329 t->clients[i] = t->clients[t->nclients - 1];
3330 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3331 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3333 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3341 * Delete an ignored client from the tunnel.
3347 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3351 for (i = 0; i < t->nignore; i++)
3353 if (t->ignore[i] == c)
3355 t->ignore[i] = t->ignore[t->nignore - 1];
3356 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3364 * Delete a client from the tunnel. It should be only done on
3365 * client disconnection, otherwise use client_ignore_tunnel.
3371 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3373 tunnel_delete_ignored_client (t, c);
3374 tunnel_delete_active_client (t, c);
3379 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3381 * Destroys queue elements of all waiting transmissions and frees all memory
3382 * used by the struct and its elements.
3384 * @param cls Closure (tunnel info).
3385 * @param key Hash of GNUNET_PEER_Id (unused).
3386 * @param value MeshTunnelChildInfo of the child.
3388 * @return always GNUNET_YES, to keep iterating
3391 tunnel_destroy_child (void *cls,
3392 const struct GNUNET_HashCode * key,
3395 struct MeshTunnelChildInfo *cinfo = value;
3396 struct MeshTunnel *t = cls;
3397 struct MeshPeerQueue *q;
3401 for (c = 0; c < cinfo->send_buffer_n; c++)
3403 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3404 q = cinfo->send_buffer[i];
3405 cinfo->send_buffer[i] = NULL;
3407 queue_destroy (q, GNUNET_YES);
3410 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3412 GNUNET_free_non_null (cinfo->send_buffer);
3413 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
3415 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
3416 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3418 GNUNET_free (cinfo);
3424 * Callback used to notify a client owner of a tunnel that a peer has
3425 * disconnected, most likely because of a path change.
3427 * @param cls Closure (tunnel this notification is about).
3428 * @param peer_id Short ID of disconnected peer.
3431 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3433 struct MeshTunnel *t = cls;
3434 struct MeshPeerInfo *peer;
3435 struct MeshPathInfo *path_info;
3437 client_notify_peer_disconnected (t->owner, t, peer_id);
3439 peer = peer_info_get_short (peer_id);
3440 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3441 path_info->peer = peer;
3443 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3449 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3450 * needed rescources.
3451 * If peer already exists, reevaluate shortest path and change if different.
3453 * @param t Tunnel we want to add a new peer to
3454 * @param peer PeerInfo of the peer being added
3458 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3460 struct GNUNET_PeerIdentity id;
3461 struct MeshPeerPath *best_p;
3462 struct MeshPeerPath *p;
3463 unsigned int best_cost;
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3467 GNUNET_PEER_resolve (peer->id, &id);
3469 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3472 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3473 GNUNET_assert (GNUNET_OK ==
3474 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3479 if (NULL != (p = peer->path_head))
3482 best_cost = tree_get_path_cost (t->tree, p);
3485 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3492 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3493 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3494 t->path_refresh_task =
3495 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3499 /* Start a DHT get */
3500 peer_info_connect (peer, t);
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3506 * Add a path to a tunnel which we don't own, just to remember the next hop.
3507 * If destination node was already in the tunnel, the first hop information
3508 * will be replaced with the new path.
3510 * @param t Tunnel we want to add a new peer to
3511 * @param p Path to add
3512 * @param own_pos Position of local node in path.
3516 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3517 unsigned int own_pos)
3519 struct GNUNET_PeerIdentity id;
3521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3522 GNUNET_assert (0 != own_pos);
3523 tree_add_path (t->tree, p, NULL, NULL);
3524 if (own_pos < p->length - 1)
3526 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3527 tree_update_first_hops (t->tree, myid, &id);
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3533 * Add a client to a tunnel, initializing all needed data structures.
3535 * @param t Tunnel to which add the client.
3536 * @param c Client which to add to the tunnel.
3539 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3541 struct MeshTunnelClientInfo clinfo;
3543 GNUNET_array_append (t->clients, t->nclients, c);
3544 clinfo.fwd_ack = t->fwd_pid + 1;
3545 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3546 clinfo.fwd_pid = t->fwd_pid;
3547 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3549 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3554 * Notifies a tunnel that a connection has broken that affects at least
3555 * some of its peers. Sends a notification towards the root of the tree.
3556 * In case the peer is the owner of the tree, notifies the client that owns
3557 * the tunnel and tries to reconnect.
3559 * @param t Tunnel affected.
3560 * @param p1 Peer that got disconnected from p2.
3561 * @param p2 Peer that got disconnected from p1.
3563 * @return Short ID of the peer disconnected (either p1 or p2).
3564 * 0 if the tunnel remained unaffected.
3566 static GNUNET_PEER_Id
3567 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3573 tree_notify_connection_broken (t->tree, p1, p2,
3574 &tunnel_notify_client_peer_disconnected,
3576 if (myid != p1 && myid != p2)
3582 if (tree_get_predecessor (t->tree) != 0)
3584 /* We are the peer still connected, notify owner of the disconnection. */
3585 struct GNUNET_MESH_PathBroken msg;
3586 struct GNUNET_PeerIdentity neighbor;
3588 msg.header.size = htons (sizeof (msg));
3589 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3590 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3591 msg.tid = htonl (t->id.tid);
3592 msg.peer1 = my_full_id;
3593 GNUNET_PEER_resolve (pid, &msg.peer2);
3594 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3595 send_prebuilt_message (&msg.header, &neighbor, t);
3603 * Send a multicast packet to a neighbor.
3605 * @param cls Closure (Info about the multicast packet)
3606 * @param neighbor_id Short ID of the neighbor to send the packet to.
3609 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3611 struct MeshData *mdata = cls;
3612 struct MeshTransmissionDescriptor *info;
3613 struct GNUNET_PeerIdentity neighbor;
3614 struct GNUNET_MessageHeader *msg;
3616 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3618 info->mesh_data = mdata;
3619 (mdata->reference_counter) ++;
3620 info->destination = neighbor_id;
3621 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3623 GNUNET_i2s (&neighbor));
3624 info->peer = peer_info_get (&neighbor);
3625 GNUNET_assert (NULL != info->peer);
3626 msg = (struct GNUNET_MessageHeader *) mdata->data;
3629 info->mesh_data->data_len,
3636 * Queue a message in a tunnel in multicast, sending a copy to each child node
3637 * down the local one in the tunnel tree.
3639 * @param t Tunnel in which to send the data.
3640 * @param msg Message to be sent.
3643 tunnel_send_multicast (struct MeshTunnel *t,
3644 const struct GNUNET_MessageHeader *msg)
3646 struct MeshData *mdata;
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3649 " sending a multicast packet...\n");
3651 mdata = GNUNET_malloc (sizeof (struct MeshData));
3652 mdata->data_len = ntohs (msg->size);
3654 mdata->data = GNUNET_malloc (mdata->data_len);
3655 memcpy (mdata->data, msg, mdata->data_len);
3656 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3658 struct GNUNET_MESH_Multicast *mcast;
3660 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3661 if (t->fwd_queue_n >= t->fwd_queue_max)
3664 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3666 " message from %s!\n",
3667 GNUNET_i2s(&mcast->oid));
3668 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3669 " message at %s!\n",
3670 GNUNET_i2s(&my_full_id));
3671 GNUNET_free (mdata->data);
3672 GNUNET_free (mdata);
3676 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3678 ntohl (mcast->ttl));
3682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3685 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3686 if (mdata->reference_counter == 0)
3688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3689 " no one to send data to\n");
3690 GNUNET_free (mdata->data);
3691 GNUNET_free (mdata);
3692 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3697 mdata->total_out = mdata->reference_counter;
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3700 " sending a multicast packet done\n");
3706 * Increase the SKIP value of all peers that
3707 * have not received a unicast message.
3709 * @param cls Closure (ID of the peer that HAS received the message).
3710 * @param key ID of the neighbor.
3711 * @param value Information about the neighbor.
3713 * @return GNUNET_YES to keep iterating.
3716 tunnel_add_skip (void *cls,
3717 const struct GNUNET_HashCode * key,
3720 struct GNUNET_PeerIdentity *neighbor = cls;
3721 struct MeshTunnelChildInfo *cinfo = value;
3723 /* TODO compare only pointers? key == neighbor? */
3724 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3734 * @brief Get neighbor's Flow Control information.
3736 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3737 * descendant of the local node in a certain tunnel.
3738 * If the info is not yet there (recently created path), creates the data struct
3739 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3742 * @param t Tunnel related.
3743 * @param peer Neighbor whose Flow Control info is needed.
3745 * @return Neighbor's Flow Control info.
3747 static struct MeshTunnelChildInfo *
3748 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3749 const struct GNUNET_PeerIdentity *peer)
3751 struct MeshTunnelChildInfo *cinfo;
3753 if (NULL == t->children_fc)
3756 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3762 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3763 cinfo->id = GNUNET_PEER_intern (peer);
3764 cinfo->skip = t->fwd_pid;
3767 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3768 cinfo->fwd_ack = t->fwd_pid + delta;
3769 cinfo->bck_ack = delta;
3770 cinfo->bck_pid = -1;
3772 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3773 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3775 cinfo->send_buffer =
3776 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3778 GNUNET_assert (GNUNET_OK ==
3779 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3782 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3789 * Get the Flow Control info of a client.
3791 * @param t Tunnel on which to look.
3792 * @param c Client whose ACK to get.
3794 * @return ACK value.
3796 static struct MeshTunnelClientInfo *
3797 tunnel_get_client_fc (struct MeshTunnel *t,
3798 struct MeshClient *c)
3802 for (i = 0; i < t->nclients; i++)
3804 if (t->clients[i] != c)
3806 return &t->clients_fc[i];
3809 return NULL; // avoid compiler / coverity complaints
3814 * Iterator to get the appropiate ACK value from all children nodes.
3816 * @param cls Closue (tunnel).
3817 * @param id Id of the child node.
3820 tunnel_get_child_fwd_ack (void *cls,
3823 struct GNUNET_PeerIdentity peer_id;
3824 struct MeshTunnelChildInfo *cinfo;
3825 struct MeshTunnelChildIteratorContext *ctx = cls;
3826 struct MeshTunnel *t = ctx->t;
3829 GNUNET_PEER_resolve (id, &peer_id);
3830 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3831 ack = cinfo->fwd_ack;
3834 if (GNUNET_NO == ctx->init)
3836 ctx->max_child_ack = ack;
3837 ctx->init = GNUNET_YES;
3840 if (GNUNET_YES == t->speed_min)
3842 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3846 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3853 * Get the maximum PID allowed to transmit to any
3854 * tunnel child of the local peer, depending on the tunnel
3855 * buffering/speed settings.
3859 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3862 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3864 struct MeshTunnelChildIteratorContext ctx;
3866 ctx.max_child_ack = 0;
3868 ctx.init = GNUNET_NO;
3869 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3871 if (0 == ctx.nchildren)
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3874 " tunnel has no children, no FWD ACK\n");
3878 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3879 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3881 return (int64_t) ctx.max_child_ack;
3886 * Set the FWD ACK value of a client in a particular tunnel.
3888 * @param t Tunnel affected.
3889 * @param c Client whose ACK to set.
3890 * @param ack ACK value.
3893 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3894 struct MeshClient *c,
3899 for (i = 0; i < t->nclients; i++)
3901 if (t->clients[i] != c)
3903 t->clients_fc[i].fwd_ack = ack;
3911 * Get the highest ACK value of all clients in a particular tunnel,
3912 * according to the buffering/speed settings.
3914 * @param t Tunnel on which to look.
3916 * @return Corresponding ACK value (max uint32_t).
3917 * If no clients are suscribed, -1LL.
3920 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3925 if (0 == t->nclients)
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 " tunnel has no clients, no FWD ACK\n");
3932 for (ack = -1LL, i = 0; i < t->nclients; i++)
3935 (GNUNET_YES == t->speed_min &&
3936 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3937 (GNUNET_NO == t->speed_min &&
3938 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3940 ack = t->clients_fc[i].fwd_ack;
3944 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3945 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3947 return (uint32_t) ack;
3952 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3953 * mode and the status of all children nodes.
3957 * @return Maximum PID allowed.
3960 tunnel_get_fwd_ack (struct MeshTunnel *t)
3964 uint32_t buffer_free;
3968 count = t->fwd_pid - t->skip;
3969 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3970 child_ack = tunnel_get_children_fwd_ack (t);
3971 client_ack = tunnel_get_clients_fwd_ack (t);
3972 if (GNUNET_YES == t->nobuffer)
3975 if (-1LL == child_ack)
3976 child_ack = client_ack;
3977 if (-1LL == child_ack)
3980 client_ack = child_ack = ack;
3985 ack = count + buffer_free; // Overflow? OK!
3987 if (-1LL == child_ack)
3989 // Node has no children, child_ack AND core buffer are irrelevant.
3990 if (-1LL == client_ack) // No children AND no clients? Not good!
3992 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3996 return (uint32_t) client_ack;
3998 if (-1LL == client_ack)
4002 if (GNUNET_YES == t->speed_min)
4004 ack = GMC_min_pid ((uint32_t) child_ack, ack);
4005 ack = GMC_min_pid ((uint32_t) client_ack, ack);
4009 ack = GMC_max_pid ((uint32_t) child_ack, ack);
4010 ack = GMC_max_pid ((uint32_t) client_ack, ack);
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4013 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
4014 count, buffer_free, child_ack, client_ack, ack);
4020 * Build a local ACK message and send it to a local client.
4022 * @param t Tunnel on which to send the ACK.
4023 * @param c Client to whom send the ACK.
4024 * @param ack Value of the ACK.
4027 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
4029 struct GNUNET_MESH_LocalAck msg;
4031 msg.header.size = htons (sizeof (msg));
4032 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4033 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
4034 msg.max_pid = htonl (ack);
4035 GNUNET_SERVER_notification_context_unicast(nc,
4042 * Build an ACK message and queue it to send to the given peer.
4044 * @param t Tunnel on which to send the ACK.
4045 * @param peer Peer to whom send the ACK.
4046 * @param ack Value of the ACK.
4049 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
4051 struct GNUNET_MESH_ACK msg;
4053 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4054 msg.header.size = htons (sizeof (msg));
4055 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
4056 msg.pid = htonl (ack);
4057 msg.tid = htonl (t->id.tid);
4059 send_prebuilt_message (&msg.header, peer, t);
4064 * Notify a the owner of a tunnel about how many more
4065 * payload packages will we accept on a given tunnel.
4067 * @param t Tunnel on which to send the ACK.
4070 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
4074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4075 "Sending client FWD ACK on tunnel %X\n",
4078 ack = tunnel_get_fwd_ack (t);
4080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4081 if (t->last_fwd_ack == ack)
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
4087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
4088 t->last_fwd_ack = ack;
4089 send_local_ack (t, t->owner, ack);
4094 * Send an ACK informing the predecessor about the available buffer space.
4095 * In case there is no predecessor, inform the owning client.
4096 * If buffering is off, send only on behalf of children or self if endpoint.
4097 * If buffering is on, send when sent to children and buffer space is free.
4098 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4099 * the ACK itself goes "back" (towards root).
4101 * @param t Tunnel on which to send the ACK.
4102 * @param type Type of message that triggered the ACK transmission.
4105 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4107 struct GNUNET_PeerIdentity id;
4110 if (NULL != t->owner)
4112 tunnel_send_client_fwd_ack (t);
4115 /* Is it after unicast / multicast retransmission? */
4118 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4119 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4121 "ACK due to FWD DATA retransmission\n");
4122 if (GNUNET_YES == t->nobuffer)
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4128 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4129 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4131 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4132 t->force_ack = GNUNET_YES;
4138 /* Check if we need to transmit the ACK */
4139 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4140 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4141 GNUNET_NO == t->force_ack)
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4145 " t->qmax: %u, t->qn: %u\n",
4146 t->fwd_queue_max, t->fwd_queue_n);
4147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4148 " t->pid: %u, t->ack: %u\n",
4149 t->fwd_pid, t->last_fwd_ack);
4153 /* Ok, ACK might be necessary, what PID to ACK? */
4154 ack = tunnel_get_fwd_ack (t);
4156 /* If speed_min and not all children have ack'd, dont send yet */
4157 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4163 t->last_fwd_ack = ack;
4164 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4165 send_ack (t, &id, ack);
4167 t->force_ack = GNUNET_NO;
4172 * Iterator to send a child node a BCK ACK to allow him to send more
4175 * @param cls Closure (tunnel).
4176 * @param id Id of the child node.
4179 tunnel_send_child_bck_ack (void *cls,
4182 struct MeshTunnel *t = cls;
4183 struct MeshTunnelChildInfo *cinfo;
4184 struct GNUNET_PeerIdentity peer;
4187 GNUNET_PEER_resolve (id, &peer);
4188 cinfo = tunnel_get_neighbor_fc (t, &peer);
4189 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4191 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4194 " Not sending ACK, not needed\n");
4197 cinfo->bck_ack = ack;
4199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4200 " Sending BCK ACK %u (last sent: %u)\n",
4201 ack, cinfo->bck_ack);
4202 send_ack (t, &peer, ack);
4207 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4209 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4211 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4212 * - implement a size 1 buffer in each client_fc AND children_fc
4213 * to hold at least 1 message per "child".
4214 * problem: violates no buffer policy
4215 * - ack 0 and make "children" poll for transmission slots
4216 * problem: big overhead, extra latency even in low traffic
4219 * @param t Tunnel on which to send the BCK ACKs.
4222 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4225 unsigned int tunnel_delta;
4227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4229 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4232 /* Find client whom to allow to send to origin (with lowest buffer space) */
4233 for (i = 0; i < t->nclients; i++)
4235 struct MeshTunnelClientInfo *clinfo;
4238 clinfo = &t->clients_fc[i];
4239 delta = clinfo->bck_ack - clinfo->bck_pid;
4240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4241 t->clients[i]->id, delta);
4243 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4244 (GNUNET_YES == t->nobuffer && 0 == delta))
4248 ack = clinfo->bck_pid;
4249 ack += t->nobuffer ? 1 : tunnel_delta;
4250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4251 " sending ack to client %u: %u\n",
4252 t->clients[i]->id, ack);
4253 send_local_ack (t, t->clients[i], ack);
4254 clinfo->bck_ack = ack;
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4259 " not sending ack to client %u (td %u, d %u)\n",
4260 t->clients[i]->id, tunnel_delta, delta);
4267 * Send an ACK informing the children nodes and destination clients about
4268 * the available buffer space.
4269 * If buffering is off, send only on behalf of root (can be self).
4270 * If buffering is on, send when sent to predecessor and buffer space is free.
4271 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4272 * the ACK itself goes "forward" (towards children/clients).
4274 * @param t Tunnel on which to send the ACK.
4275 * @param type Type of message that triggered the ACK transmission.
4278 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4281 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4282 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4283 /* Is it after data to_origin retransmission? */
4286 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4287 if (GNUNET_YES == t->nobuffer)
4289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4290 " Not sending ACK, nobuffer\n");
4294 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4295 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4297 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4298 t->force_ack = GNUNET_YES;
4304 tunnel_send_clients_bck_ack (t);
4305 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4306 t->force_ack = GNUNET_NO;
4311 * @brief Re-initiate traffic to this peer if necessary.
4313 * Check if there is traffic queued towards this peer
4314 * and the core transmit handle is NULL (traffic was stalled).
4315 * If so, call core tmt rdy.
4317 * @param cls Closure (unused)
4318 * @param peer_id Short ID of peer to which initiate traffic.
4321 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4323 struct MeshPeerInfo *peer;
4324 struct GNUNET_PeerIdentity id;
4325 struct MeshPeerQueue *q;
4328 peer = peer_info_get_short(peer_id);
4329 if (NULL != peer->core_transmit)
4332 q = queue_get_next(peer);
4335 /* Might br multicast traffic already sent to this particular peer but
4336 * not to other children in this tunnel.
4337 * This way t->queue_n would be > 0 but the queue of this particular peer
4343 GNUNET_PEER_resolve (peer->id, &id);
4344 peer->core_transmit =
4345 GNUNET_CORE_notify_transmit_ready(core_handle,
4348 GNUNET_TIME_UNIT_FOREVER_REL,
4358 * @brief Allow transmission of FWD traffic on this tunnel
4360 * Check if there is traffic queued towards any children
4361 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4363 * @param t Tunnel on which to unlock FWD traffic.
4366 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4368 if (0 == t->fwd_queue_n)
4371 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4376 * @brief Allow transmission of BCK traffic on this tunnel
4378 * Check if there is traffic queued towards the root of the tree
4379 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4381 * @param t Tunnel on which to unlock BCK traffic.
4384 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4386 if (0 == t->bck_queue_n)
4389 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4394 * Send a message to all peers in this tunnel that the tunnel is no longer
4397 * @param t The tunnel whose peers to notify.
4398 * @param parent ID of the parent, in case the tree is already destroyed.
4401 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
4403 struct GNUNET_MESH_TunnelDestroy msg;
4404 struct GNUNET_PeerIdentity id;
4406 msg.header.size = htons (sizeof (msg));
4407 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4408 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4409 msg.tid = htonl (t->id.tid);
4410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4411 " sending tunnel destroy for tunnel: %s [%X]\n",
4412 GNUNET_i2s (&msg.oid), t->id.tid);
4413 if (tree_count_children(t->tree) > 0)
4415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to children\n");
4416 tunnel_send_multicast (t, &msg.header);
4419 parent = tree_get_predecessor (t->tree);
4420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
4424 GNUNET_PEER_resolve (parent, &id);
4425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4426 " sending back to %s\n",
4428 send_prebuilt_message (&msg.header, &id, t);
4433 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4435 * @param cls Closure (Tunnel which to cancel).
4436 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4439 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4441 struct MeshTunnel *t = cls;
4442 struct MeshPeerInfo *peer_info;
4443 struct MeshPeerQueue *pq;
4444 struct MeshPeerQueue *next;
4446 peer_info = peer_info_get_short (neighbor_id);
4447 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4450 if (pq->tunnel == t)
4452 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4453 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4454 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4456 // Should have been removed on destroy children
4459 queue_destroy (pq, GNUNET_YES);
4462 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4464 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4465 peer_info->core_transmit = NULL;
4470 * Destroy the tunnel and free any allocated resources linked to it.
4472 * @param t the tunnel to destroy
4474 * @return GNUNET_OK on success
4477 tunnel_destroy (struct MeshTunnel *t)
4479 struct MeshClient *c;
4480 struct GNUNET_HashCode hash;
4491 struct GNUNET_PeerIdentity id;
4493 GNUNET_PEER_resolve (t->id.oid, &id);
4494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4495 GNUNET_i2s (&id), t->id.tid);
4497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4501 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4502 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4510 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4512 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4519 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4520 for (i = 0; i < t->nclients; i++)
4524 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4530 for (i = 0; i < t->nignore; i++)
4534 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4541 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4542 GNUNET_free_non_null (t->clients);
4543 GNUNET_free_non_null (t->ignore);
4544 GNUNET_free_non_null (t->clients_fc);
4546 if (NULL != t->peers)
4548 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4550 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4553 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4554 &tunnel_destroy_child,
4556 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4557 t->children_fc = NULL;
4559 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4560 tree_destroy (t->tree);
4562 if (NULL != t->regex_ctx)
4563 regex_cancel_search (t->regex_ctx);
4564 if (NULL != t->dht_get_type)
4565 GNUNET_DHT_get_stop (t->dht_get_type);
4566 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4567 GNUNET_SCHEDULER_cancel (t->timeout_task);
4568 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4569 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4572 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4577 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4580 * Tunnel is empty: destroy it.
4582 * @param cls Closure (Tunnel).
4583 * @param tc TaskContext.
4586 tunnel_destroy_empty_delayed (void *cls,
4587 const struct GNUNET_SCHEDULER_TaskContext *tc)
4589 struct MeshTunnel *t = cls;
4591 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4595 if (0 != t->nclients ||
4596 0 != tree_count_children (t->tree))
4601 struct GNUNET_PeerIdentity id;
4603 GNUNET_PEER_resolve (t->id.oid, &id);
4604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4605 "executing destruction of empty tunnel %s [%X]\n",
4606 GNUNET_i2s (&id), t->id.tid);
4610 tunnel_send_destroy (t, 0);
4611 if (0 == t->pending_messages)
4614 t->destroy = GNUNET_YES;
4619 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4621 * @param t Tunnel to destroy if empty.
4624 tunnel_destroy_empty (struct MeshTunnel *t)
4626 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4628 0 != tree_count_children (t->tree))
4630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4632 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4638 struct GNUNET_PeerIdentity id;
4640 GNUNET_PEER_resolve (t->id.oid, &id);
4641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4642 "scheduling destruction of empty tunnel %s [%X]\n",
4643 GNUNET_i2s (&id), t->id.tid);
4647 t->delayed_destroy =
4648 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4649 &tunnel_destroy_empty_delayed,
4655 * Create a new tunnel
4657 * @param owner Who is the owner of the tunnel (short ID).
4658 * @param tid Tunnel Number of the tunnel.
4659 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4660 * @param local Tunnel Number for the tunnel, for the client point of view.
4662 * @return A new initialized tunnel. NULL on error.
4664 static struct MeshTunnel *
4665 tunnel_new (GNUNET_PEER_Id owner,
4666 MESH_TunnelNumber tid,
4667 struct MeshClient *client,
4668 MESH_TunnelNumber local)
4670 struct MeshTunnel *t;
4671 struct GNUNET_HashCode hash;
4673 if (n_tunnels >= max_tunnels && NULL == client)
4676 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4679 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4680 t->bck_queue_max = t->fwd_queue_max;
4681 t->tree = tree_new (owner);
4683 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4684 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4685 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4686 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4687 t->local_tid = local;
4688 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4690 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4692 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4694 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4702 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4709 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4711 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4712 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4716 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4725 * Callback when removing children from a tunnel tree. Notify owner.
4727 * @param cls Closure (tunnel).
4728 * @param peer_id Short ID of the peer deleted.
4731 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
4733 struct MeshTunnel *t = cls;
4735 client_notify_peer_disconnected (t->owner, t, peer_id);
4739 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4740 * that are no longer needed, as well as nodes of no longer reachable peers.
4741 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4743 * @param t Tunnel from which to remove the path.
4744 * @param peer Short id of the peer which should be removed.
4747 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4751 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
4754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4755 "Tunnel %u [%u] has no more nodes\n",
4756 t->id.oid, t->id.tid);
4762 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4763 * client when the client disconnects. If the client is not the owner, the
4764 * owner will get notified if no more clients are in the tunnel and the client
4765 * get removed from the tunnel's list.
4767 * @param cls closure (client that is disconnecting)
4768 * @param key the hash of the local tunnel id (used to access the hashmap)
4769 * @param value the value stored at the key (tunnel to destroy)
4771 * @return GNUNET_OK, keep iterating.
4774 tunnel_destroy_iterator (void *cls,
4775 const struct GNUNET_HashCode * key,
4778 struct MeshTunnel *t = value;
4779 struct MeshClient *c = cls;
4781 send_client_tunnel_disconnect (t, c);
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4785 tunnel_delete_client (t, c);
4786 client_delete_tunnel (c, t);
4787 tunnel_destroy_empty (t);
4790 tunnel_send_destroy (t, 0);
4792 t->destroy = GNUNET_YES;
4799 * Timeout function, destroys tunnel if called
4801 * @param cls Closure (tunnel to destroy).
4802 * @param tc TaskContext
4805 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4807 struct MeshTunnel *t = cls;
4808 struct GNUNET_PeerIdentity id;
4810 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4811 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4813 GNUNET_PEER_resolve(t->id.oid, &id);
4814 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4815 "Tunnel %s [%X] timed out. Destroying.\n",
4816 GNUNET_i2s(&id), t->id.tid);
4817 send_clients_tunnel_destroy (t);
4822 * Resets the tunnel timeout. Starts it if no timeout was running.
4824 * @param t Tunnel whose timeout to reset.
4826 * TODO use heap to improve efficiency of scheduler.
4829 tunnel_reset_timeout (struct MeshTunnel *t)
4831 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4832 GNUNET_SCHEDULER_cancel (t->timeout_task);
4834 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4835 (refresh_path_time, 4), &tunnel_timeout, t);
4839 /******************************************************************************/
4840 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4841 /******************************************************************************/
4844 * Function to send a create path packet to a peer.
4846 * @param cls closure
4847 * @param size number of bytes available in buf
4848 * @param buf where the callee should write the message
4849 * @return number of bytes written to buf
4852 send_core_path_create (void *cls, size_t size, void *buf)
4854 struct MeshPathInfo *info = cls;
4855 struct GNUNET_MESH_ManipulatePath *msg;
4856 struct GNUNET_PeerIdentity *peer_ptr;
4857 struct MeshTunnel *t = info->t;
4858 struct MeshPeerPath *p = info->path;
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4865 sizeof (struct GNUNET_MESH_ManipulatePath) +
4866 p->length * sizeof (struct GNUNET_PeerIdentity);
4868 if (size < size_needed || NULL == buf)
4873 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4874 msg->header.size = htons (size_needed);
4875 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4876 msg->tid = ntohl (t->id.tid);
4879 if (GNUNET_YES == t->speed_min)
4880 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4881 if (GNUNET_YES == t->nobuffer)
4882 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4883 msg->opt = htonl(opt);
4886 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4887 for (i = 0; i < p->length; i++)
4889 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4896 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4902 * Fill the core buffer
4904 * @param cls closure (data itself)
4905 * @param size number of bytes available in buf
4906 * @param buf where the callee should write the message
4908 * @return number of bytes written to buf
4911 send_core_data_multicast (void *cls, size_t size, void *buf)
4913 struct MeshTransmissionDescriptor *info = cls;
4916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4917 GNUNET_assert (NULL != info);
4918 GNUNET_assert (NULL != info->peer);
4919 total_size = info->mesh_data->data_len;
4920 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4922 if (total_size > size)
4927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4928 memcpy (buf, info->mesh_data->data, total_size);
4931 struct GNUNET_MESH_Multicast *mc;
4932 struct GNUNET_MessageHeader *mh;
4935 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4937 mc = (struct GNUNET_MESH_Multicast *) mh;
4938 mh = (struct GNUNET_MessageHeader *) &mc[1];
4939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4940 " multicast, payload type %s\n",
4941 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4943 " multicast, payload size %u\n", ntohs (mh->size));
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4948 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4952 data_descriptor_decrement_rc (info->mesh_data);
4953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4961 * Creates a path ack message in buf and frees all unused resources.
4963 * @param cls closure (MeshTransmissionDescriptor)
4964 * @param size number of bytes available in buf
4965 * @param buf where the callee should write the message
4966 * @return number of bytes written to buf
4969 send_core_path_ack (void *cls, size_t size, void *buf)
4971 struct MeshTransmissionDescriptor *info = cls;
4972 struct GNUNET_MESH_PathACK *msg = buf;
4974 GNUNET_assert (NULL != info);
4975 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4980 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4981 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4982 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4983 msg->tid = htonl (info->origin->tid);
4984 msg->peer_id = my_full_id;
4987 /* TODO add signature */
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4990 return sizeof (struct GNUNET_MESH_PathACK);
4995 * Free a transmission that was already queued with all resources
4996 * associated to the request.
4998 * @param queue Queue handler to cancel.
4999 * @param clear_cls Is it necessary to free associated cls?
5002 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
5004 struct MeshTransmissionDescriptor *dd;
5005 struct MeshPathInfo *path_info;
5006 struct MeshTunnelChildInfo *cinfo;
5007 struct GNUNET_PeerIdentity id;
5011 if (GNUNET_YES == clear_cls)
5013 switch (queue->type)
5015 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5016 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
5017 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
5019 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5020 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5021 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5022 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5023 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5024 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5026 " prebuilt message\n");
5027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5029 GNUNET_MESH_DEBUG_M2S(queue->type));
5031 data_descriptor_decrement_rc (dd->mesh_data);
5033 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
5035 path_info = queue->cls;
5036 path_destroy (path_info->path);
5040 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5041 " type %s unknown!\n",
5042 GNUNET_MESH_DEBUG_M2S(queue->type));
5044 GNUNET_free_non_null (queue->cls);
5046 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
5047 queue->peer->queue_tail,
5050 /* Delete from child_fc in the appropiate tunnel */
5051 max = queue->tunnel->fwd_queue_max;
5052 GNUNET_PEER_resolve (queue->peer->id, &id);
5053 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
5056 for (i = 0; i < cinfo->send_buffer_n; i++)
5059 i2 = (cinfo->send_buffer_start + i) % max;
5060 if (cinfo->send_buffer[i2] == queue)
5062 /* Found corresponding entry in the send_buffer. Move all others back. */
5067 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
5069 j2 = (cinfo->send_buffer_start + j) % max;
5070 j3 = (cinfo->send_buffer_start + j + 1) % max;
5071 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
5074 cinfo->send_buffer[j3] = NULL;
5075 cinfo->send_buffer_n--;
5080 GNUNET_free (queue);
5085 * @brief Get the next transmittable message from the queue.
5087 * This will be the head, except in the case of being a data packet
5088 * not allowed by the destination peer.
5090 * @param peer Destination peer.
5092 * @return The next viable MeshPeerQueue element to send to that peer.
5093 * NULL when there are no transmittable messages.
5095 struct MeshPeerQueue *
5096 queue_get_next (const struct MeshPeerInfo *peer)
5098 struct MeshPeerQueue *q;
5099 struct MeshTunnel *t;
5100 struct MeshTransmissionDescriptor *info;
5101 struct MeshTunnelChildInfo *cinfo;
5102 struct GNUNET_MESH_Unicast *ucast;
5103 struct GNUNET_MESH_ToOrigin *to_orig;
5104 struct GNUNET_MESH_Multicast *mcast;
5105 struct GNUNET_PeerIdentity id;
5109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
5110 for (q = peer->queue_head; NULL != q; q = q->next)
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5116 GNUNET_MESH_DEBUG_M2S(q->type));
5119 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5120 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
5121 pid = ntohl (ucast->pid);
5122 GNUNET_PEER_resolve (info->peer->id, &id);
5123 cinfo = tunnel_get_neighbor_fc(t, &id);
5124 ack = cinfo->fwd_ack;
5126 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5127 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
5128 pid = ntohl (to_orig->pid);
5131 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5132 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
5133 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
5135 // Not a multicast payload: multicast control traffic (destroy, etc)
5138 pid = ntohl (mcast->pid);
5139 GNUNET_PEER_resolve (info->peer->id, &id);
5140 cinfo = tunnel_get_neighbor_fc(t, &id);
5141 ack = cinfo->fwd_ack;
5144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5149 "********* ACK: %u, PID: %u\n",
5151 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
5153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5160 "********* NEXT!\n");
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5164 "********* nothing found\n");
5170 * Core callback to write a queued packet to core buffer
5172 * @param cls Closure (peer info).
5173 * @param size Number of bytes available in buf.
5174 * @param buf Where the to write the message.
5176 * @return number of bytes written to buf
5179 queue_send (void *cls, size_t size, void *buf)
5181 struct MeshPeerInfo *peer = cls;
5182 struct GNUNET_MessageHeader *msg;
5183 struct MeshPeerQueue *queue;
5184 struct MeshTunnel *t;
5185 struct MeshTunnelChildInfo *cinfo;
5186 struct GNUNET_PeerIdentity dst_id;
5189 peer->core_transmit = NULL;
5192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5193 queue = queue_get_next (peer);
5195 /* Queue has no internal mesh traffic nor sendable payload */
5198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5199 if (NULL == peer->queue_head)
5200 GNUNET_break (0); // Should've been canceled
5203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5205 GNUNET_PEER_resolve (peer->id, &dst_id);
5206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5207 "********* towards %s\n",
5208 GNUNET_i2s(&dst_id));
5209 /* Check if buffer size is enough for the message */
5210 if (queue->size > size)
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5213 "********* not enough room, reissue\n");
5214 peer->core_transmit =
5215 GNUNET_CORE_notify_transmit_ready (core_handle,
5218 GNUNET_TIME_UNIT_FOREVER_REL,
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5228 GNUNET_assert (0 < t->pending_messages);
5229 t->pending_messages--;
5230 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5234 "********* unicast: t->q (%u/%u)\n",
5235 t->fwd_queue_n, t->fwd_queue_max);
5237 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5244 switch (queue->type)
5247 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5248 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5249 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5250 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5251 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5253 "********* raw: %s\n",
5254 GNUNET_MESH_DEBUG_M2S (queue->type));
5256 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5257 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5258 data_size = send_core_data_raw (queue->cls, size, buf);
5259 msg = (struct GNUNET_MessageHeader *) buf;
5260 switch (ntohs (msg->type)) // Type of preconstructed message
5262 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5263 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5265 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5266 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5272 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5275 struct MeshTransmissionDescriptor *info = queue->cls;
5277 if ((1 == info->mesh_data->reference_counter
5278 && GNUNET_YES == t->speed_min)
5280 (info->mesh_data->total_out == info->mesh_data->reference_counter
5281 && GNUNET_NO == t->speed_min))
5283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5284 "********* considered sent\n");
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5290 "********* NOT considered sent yet\n");
5291 t->pending_messages++;
5294 data_size = send_core_data_multicast(queue->cls, size, buf);
5295 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5297 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5299 data_size = send_core_path_create (queue->cls, size, buf);
5301 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5303 data_size = send_core_path_ack (queue->cls, size, buf);
5305 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5307 data_size = send_core_data_multicast (queue->cls, size, buf);
5311 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5312 "********* type unknown: %u\n",
5316 switch (queue->type)
5318 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5319 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5320 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5321 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5322 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5326 "at pos %u (%p) != %p\n",
5327 cinfo->send_buffer_start,
5328 cinfo->send_buffer[cinfo->send_buffer_start],
5331 if (cinfo->send_buffer_n > 0)
5333 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5334 cinfo->send_buffer_n--;
5335 cinfo->send_buffer_start++;
5336 cinfo->send_buffer_start %= t->fwd_queue_max;
5347 /* Free queue, but cls was freed by send_core_* */
5348 queue_destroy (queue, GNUNET_NO);
5350 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5356 /* If more data in queue, send next */
5357 queue = queue_get_next(peer);
5360 struct GNUNET_PeerIdentity id;
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5363 GNUNET_PEER_resolve (peer->id, &id);
5364 peer->core_transmit =
5365 GNUNET_CORE_notify_transmit_ready(core_handle,
5368 GNUNET_TIME_UNIT_FOREVER_REL,
5376 if (NULL != peer->queue_head)
5378 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5379 "********* %s stalled\n",
5380 GNUNET_i2s(&my_full_id));
5382 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5383 // FIXME unify bck/fwd structures, bck does not have cinfo right now
5384 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5386 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
5387 &tunnel_poll, cinfo);
5391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5397 * @brief Queue and pass message to core when possible.
5399 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5400 * and accounts for it. In case the queue is full, the message is dropped and
5403 * Otherwise, message is treated as internal and allowed to go regardless of
5406 * @param cls Closure (@c type dependant). It will be used by queue_send to
5407 * build the message to be sent if not already prebuilt.
5408 * @param type Type of the message, 0 for a raw message.
5409 * @param size Size of the message.
5410 * @param dst Neighbor to send message to.
5411 * @param t Tunnel this message belongs to.
5414 queue_add (void *cls, uint16_t type, size_t size,
5415 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5417 struct MeshPeerQueue *queue;
5418 struct MeshTunnelChildInfo *cinfo;
5419 struct GNUNET_PeerIdentity id;
5425 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5426 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5428 n = &t->fwd_queue_n;
5429 max = &t->fwd_queue_max;
5431 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5433 n = &t->bck_queue_n;
5434 max = &t->bck_queue_max;
5441 GNUNET_STATISTICS_update(stats,
5442 "# messages dropped (buffer full)",
5444 return; // Drop message
5448 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5454 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5455 GNUNET_PEER_resolve (dst->id, &id);
5456 if (NULL == dst->core_transmit)
5458 dst->core_transmit =
5459 GNUNET_CORE_notify_transmit_ready (core_handle,
5462 GNUNET_TIME_UNIT_FOREVER_REL,
5468 t->pending_messages++;
5469 if (NULL == n) // Is this internal mesh traffic?
5472 // It's payload, keep track of buffer per peer.
5473 cinfo = tunnel_get_neighbor_fc(t, &id);
5474 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5475 if (NULL != cinfo->send_buffer[i])
5477 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5478 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5479 cinfo->send_buffer_start++;
5480 cinfo->send_buffer_start %= t->fwd_queue_max;
5484 cinfo->send_buffer_n++;
5486 cinfo->send_buffer[i] = queue;
5487 if (cinfo->send_buffer_n > t->fwd_queue_max)
5490 cinfo->send_buffer_n = t->fwd_queue_max;
5495 /******************************************************************************/
5496 /******************** MESH NETWORK HANDLERS **************************/
5497 /******************************************************************************/
5501 * Core handler for path creation
5503 * @param cls closure
5504 * @param message message
5505 * @param peer peer identity this notification is about
5506 * @param atsi performance data
5507 * @param atsi_count number of records in 'atsi'
5509 * @return GNUNET_OK to keep the connection open,
5510 * GNUNET_SYSERR to close it (signal serious error)
5513 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5514 const struct GNUNET_MessageHeader *message,
5515 const struct GNUNET_ATS_Information *atsi,
5516 unsigned int atsi_count)
5518 unsigned int own_pos;
5521 MESH_TunnelNumber tid;
5522 struct GNUNET_MESH_ManipulatePath *msg;
5523 struct GNUNET_PeerIdentity *pi;
5524 struct GNUNET_HashCode hash;
5525 struct MeshPeerPath *path;
5526 struct MeshPeerInfo *dest_peer_info;
5527 struct MeshPeerInfo *orig_peer_info;
5528 struct MeshTunnel *t;
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5531 "Received a path create msg [%s]\n",
5532 GNUNET_i2s (&my_full_id));
5533 size = ntohs (message->size);
5534 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5536 GNUNET_break_op (0);
5540 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5541 if (size % sizeof (struct GNUNET_PeerIdentity))
5543 GNUNET_break_op (0);
5546 size /= sizeof (struct GNUNET_PeerIdentity);
5549 GNUNET_break_op (0);
5552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5553 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5555 tid = ntohl (msg->tid);
5556 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5558 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5559 t = tunnel_get (pi, tid);
5560 if (NULL == t) // FIXME only for INCOMING tunnels?
5564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5565 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5568 // FIXME notify failure
5571 opt = ntohl (msg->opt);
5572 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5573 GNUNET_YES : GNUNET_NO;
5574 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5576 t->nobuffer = GNUNET_YES;
5577 t->last_fwd_ack = t->fwd_pid + 1;
5579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5580 " speed_min: %d, nobuffer:%d\n",
5581 t->speed_min, t->nobuffer);
5583 if (GNUNET_YES == t->nobuffer)
5585 t->bck_queue_max = 1;
5586 t->fwd_queue_max = 1;
5589 // FIXME only assign a local tid if a local client is interested (on demand)
5590 while (NULL != tunnel_get_incoming (next_local_tid))
5591 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5592 t->local_tid_dest = next_local_tid++;
5593 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5596 tunnel_reset_timeout (t);
5597 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5599 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5600 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5608 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5609 if (NULL == dest_peer_info)
5611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5612 " Creating PeerInfo for destination.\n");
5613 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5614 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5615 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5619 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5620 if (NULL == orig_peer_info)
5622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5623 " Creating PeerInfo for origin.\n");
5624 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5625 orig_peer_info->id = GNUNET_PEER_intern (pi);
5626 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5627 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5630 path = path_new (size);
5632 for (i = 0; i < size; i++)
5634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5635 GNUNET_i2s (&pi[i]));
5636 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5637 if (path->peers[i] == myid)
5640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5643 /* cannot be self, must be 'not found' */
5644 /* create path: self not found in path through self */
5645 GNUNET_break_op (0);
5646 path_destroy (path);
5650 path_add_to_peers (path, GNUNET_NO);
5651 tunnel_add_path (t, path, own_pos);
5652 if (own_pos == size - 1)
5654 /* It is for us! Send ack. */
5655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5656 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5657 if (NULL == t->peers)
5659 /* New tunnel! Notify clients on first payload message. */
5660 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5662 GNUNET_break (GNUNET_SYSERR !=
5663 GNUNET_CONTAINER_multihashmap_put (t->peers,
5664 &my_full_id.hashPubKey,
5667 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5672 struct MeshPeerPath *path2;
5674 /* It's for somebody else! Retransmit. */
5675 path2 = path_duplicate (path);
5676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5677 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5678 path2 = path_duplicate (path);
5679 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5680 send_create_path (dest_peer_info, path, t);
5687 * Core handler for path destruction
5689 * @param cls closure
5690 * @param message message
5691 * @param peer peer identity this notification is about
5692 * @param atsi performance data
5693 * @param atsi_count number of records in 'atsi'
5695 * @return GNUNET_OK to keep the connection open,
5696 * GNUNET_SYSERR to close it (signal serious error)
5699 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5700 const struct GNUNET_MessageHeader *message,
5701 const struct GNUNET_ATS_Information *atsi,
5702 unsigned int atsi_count)
5704 struct GNUNET_MESH_ManipulatePath *msg;
5705 struct GNUNET_PeerIdentity *pi;
5706 struct MeshPeerPath *path;
5707 struct MeshTunnel *t;
5708 unsigned int own_pos;
5712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5713 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5714 size = ntohs (message->size);
5715 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5717 GNUNET_break_op (0);
5721 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5722 if (size % sizeof (struct GNUNET_PeerIdentity))
5724 GNUNET_break_op (0);
5727 size /= sizeof (struct GNUNET_PeerIdentity);
5730 GNUNET_break_op (0);
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5735 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5736 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5738 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5740 t = tunnel_get (pi, ntohl (msg->tid));
5743 /* TODO notify back: we don't know this tunnel */
5744 GNUNET_break_op (0);
5747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5748 path = path_new (size);
5750 for (i = 0; i < size; i++)
5752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5753 GNUNET_i2s (&pi[i]));
5754 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5755 if (path->peers[i] == myid)
5758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5759 if (own_pos < path->length - 1)
5760 send_prebuilt_message (message, &pi[own_pos + 1], t);
5762 send_client_tunnel_disconnect(t, NULL);
5764 tunnel_delete_peer (t, path->peers[path->length - 1]);
5765 path_destroy (path);
5771 * Core handler for notifications of broken paths
5773 * @param cls closure
5774 * @param message message
5775 * @param peer peer identity this notification is about
5776 * @param atsi performance data
5777 * @param atsi_count number of records in 'atsi'
5779 * @return GNUNET_OK to keep the connection open,
5780 * GNUNET_SYSERR to close it (signal serious error)
5783 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5784 const struct GNUNET_MessageHeader *message,
5785 const struct GNUNET_ATS_Information *atsi,
5786 unsigned int atsi_count)
5788 struct GNUNET_MESH_PathBroken *msg;
5789 struct MeshTunnel *t;
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5792 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5793 msg = (struct GNUNET_MESH_PathBroken *) message;
5794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5795 GNUNET_i2s (&msg->peer1));
5796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5797 GNUNET_i2s (&msg->peer2));
5798 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5801 GNUNET_break_op (0);
5804 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5805 GNUNET_PEER_search (&msg->peer2));
5812 * Core handler for tunnel destruction
5814 * @param cls closure
5815 * @param message message
5816 * @param peer peer identity this notification is about
5817 * @param atsi performance data
5818 * @param atsi_count number of records in 'atsi'
5820 * @return GNUNET_OK to keep the connection open,
5821 * GNUNET_SYSERR to close it (signal serious error)
5824 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5825 const struct GNUNET_MessageHeader *message,
5826 const struct GNUNET_ATS_Information *atsi,
5827 unsigned int atsi_count)
5829 struct GNUNET_MESH_TunnelDestroy *msg;
5830 struct MeshTunnel *t;
5831 GNUNET_PEER_Id parent;
5834 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5836 "Got a TUNNEL DESTROY packet from %s\n",
5838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5839 " for tunnel %s [%u]\n",
5840 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5841 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5842 /* Check signature */
5845 /* Probably already got the message from another path,
5846 * destroyed the tunnel and retransmitted to children.
5849 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5853 parent = tree_get_predecessor(t->tree);
5854 pid = GNUNET_PEER_search (peer);
5859 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5860 nc = tree_count_children(t->tree);
5861 if (nc > 0 || NULL != t->owner || t->nclients > 0)
5863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5864 "still in use: %u cl, %u ch\n",
5869 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5871 /* Tunnel was incoming, notify clients */
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5873 t->local_tid, t->local_tid_dest);
5874 send_clients_tunnel_destroy (t);
5876 tunnel_send_destroy (t, parent);
5877 t->destroy = GNUNET_YES;
5878 // TODO: add timeout to destroy the tunnel anyway
5884 * Core handler for mesh network traffic going from the origin to a peer
5886 * @param cls closure
5887 * @param peer peer identity this notification is about
5888 * @param message message
5889 * @param atsi performance data
5890 * @param atsi_count number of records in 'atsi'
5891 * @return GNUNET_OK to keep the connection open,
5892 * GNUNET_SYSERR to close it (signal serious error)
5895 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5896 const struct GNUNET_MessageHeader *message,
5897 const struct GNUNET_ATS_Information *atsi,
5898 unsigned int atsi_count)
5900 struct GNUNET_MESH_Unicast *msg;
5901 struct GNUNET_PeerIdentity *neighbor;
5902 struct MeshTunnelChildInfo *cinfo;
5903 struct MeshTunnel *t;
5904 GNUNET_PEER_Id dest_id;
5909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5912 size = ntohs (message->size);
5914 sizeof (struct GNUNET_MESH_Unicast) +
5915 sizeof (struct GNUNET_MessageHeader))
5920 msg = (struct GNUNET_MESH_Unicast *) message;
5921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5922 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5924 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5927 /* TODO notify back: we don't know this tunnel */
5928 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5929 GNUNET_break_op (0);
5932 pid = ntohl (msg->pid);
5933 if (t->fwd_pid == pid)
5935 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5936 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5937 " Already seen pid %u, DROPPING!\n", pid);
5942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5943 " pid %u not seen yet, forwarding\n", pid);
5946 t->skip += (pid - t->fwd_pid) - 1;
5949 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5951 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5952 GNUNET_break_op (0);
5953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5954 "Received PID %u, ACK %u\n",
5955 pid, t->last_fwd_ack);
5959 tunnel_reset_timeout (t);
5960 dest_id = GNUNET_PEER_search (&msg->destination);
5961 if (dest_id == myid)
5963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5964 " it's for us! sending to clients...\n");
5965 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5966 send_subscribed_clients (message, &msg[1].header, t);
5967 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5970 ttl = ntohl (msg->ttl);
5971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5974 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5975 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5976 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5980 " not for us, retransmitting...\n");
5982 neighbor = tree_get_first_hop (t->tree, dest_id);
5983 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5984 cinfo->fwd_pid = pid;
5985 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5988 if (GNUNET_YES == t->nobuffer &&
5989 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5991 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5992 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5993 GNUNET_break_op (0);
5996 send_prebuilt_message (message, neighbor, t);
5997 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
6003 * Core handler for mesh network traffic going from the origin to all peers
6005 * @param cls closure
6006 * @param message message
6007 * @param peer peer identity this notification is about
6008 * @param atsi performance data
6009 * @param atsi_count number of records in 'atsi'
6010 * @return GNUNET_OK to keep the connection open,
6011 * GNUNET_SYSERR to close it (signal serious error)
6013 * TODO: Check who we got this from, to validate route.
6016 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
6017 const struct GNUNET_MessageHeader *message,
6018 const struct GNUNET_ATS_Information *atsi,
6019 unsigned int atsi_count)
6021 struct GNUNET_MESH_Multicast *msg;
6022 struct MeshTunnel *t;
6026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
6028 size = ntohs (message->size);
6029 if (sizeof (struct GNUNET_MESH_Multicast) +
6030 sizeof (struct GNUNET_MessageHeader) > size)
6032 GNUNET_break_op (0);
6035 msg = (struct GNUNET_MESH_Multicast *) message;
6036 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6040 /* TODO notify that we dont know that tunnel */
6041 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
6042 GNUNET_break_op (0);
6045 pid = ntohl (msg->pid);
6046 if (t->fwd_pid == pid)
6048 /* already seen this packet, drop */
6049 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
6050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6051 " Already seen pid %u, DROPPING!\n", pid);
6052 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6058 " pid %u not seen yet, forwarding\n", pid);
6060 t->skip += (pid - t->fwd_pid) - 1;
6062 tunnel_reset_timeout (t);
6064 /* Transmit to locally interested clients */
6065 if (NULL != t->peers &&
6066 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
6068 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
6069 send_subscribed_clients (message, &msg[1].header, t);
6070 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
6072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
6073 if (ntohl (msg->ttl) == 0)
6075 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
6076 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
6077 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6080 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
6081 tunnel_send_multicast (t, message);
6087 * Core handler for mesh network traffic toward the owner of a tunnel
6089 * @param cls closure
6090 * @param message message
6091 * @param peer peer identity this notification is about
6092 * @param atsi performance data
6093 * @param atsi_count number of records in 'atsi'
6095 * @return GNUNET_OK to keep the connection open,
6096 * GNUNET_SYSERR to close it (signal serious error)
6099 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
6100 const struct GNUNET_MessageHeader *message,
6101 const struct GNUNET_ATS_Information *atsi,
6102 unsigned int atsi_count)
6104 struct GNUNET_MESH_ToOrigin *msg;
6105 struct GNUNET_PeerIdentity id;
6106 struct MeshPeerInfo *peer_info;
6107 struct MeshTunnel *t;
6108 struct MeshTunnelChildInfo *cinfo;
6109 GNUNET_PEER_Id predecessor;
6113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
6115 size = ntohs (message->size);
6116 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
6117 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
6119 GNUNET_break_op (0);
6122 msg = (struct GNUNET_MESH_ToOrigin *) message;
6123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
6124 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
6125 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6126 pid = ntohl (msg->pid);
6130 /* TODO notify that we dont know this tunnel (whom)? */
6131 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
6132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6133 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
6134 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
6138 cinfo = tunnel_get_neighbor_fc(t, peer);
6145 if (cinfo->bck_pid == pid)
6147 /* already seen this packet, drop */
6148 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
6149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6150 " Already seen pid %u, DROPPING!\n", pid);
6151 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6156 " pid %u not seen yet, forwarding\n", pid);
6157 cinfo->bck_pid = pid;
6159 if (NULL != t->owner)
6162 struct GNUNET_MESH_ToOrigin *copy;
6164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6165 " it's for us! sending to clients...\n");
6166 /* TODO signature verification */
6167 memcpy (cbuf, message, size);
6168 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
6169 copy->tid = htonl (t->local_tid);
6171 copy->pid = htonl (t->bck_pid);
6172 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
6173 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
6174 ©->header, GNUNET_NO);
6175 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6179 " not for us, retransmitting...\n");
6181 peer_info = peer_info_get (&msg->oid);
6182 if (NULL == peer_info)
6184 /* unknown origin of tunnel */
6188 predecessor = tree_get_predecessor (t->tree);
6189 if (0 == predecessor)
6191 if (GNUNET_YES == t->destroy)
6193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6194 "to orig received on a dying tunnel %s [%X]\n",
6195 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6199 "unknown to origin at %s\n",
6200 GNUNET_i2s (&my_full_id));
6201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6205 "for tunnel %s [%X]\n",
6206 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6209 tree_debug (t->tree);
6212 GNUNET_PEER_resolve (predecessor, &id);
6213 send_prebuilt_message (message, &id, t);
6214 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6221 * Core handler for mesh network traffic point-to-point acks.
6223 * @param cls closure
6224 * @param message message
6225 * @param peer peer identity this notification is about
6226 * @param atsi performance data
6227 * @param atsi_count number of records in 'atsi'
6229 * @return GNUNET_OK to keep the connection open,
6230 * GNUNET_SYSERR to close it (signal serious error)
6233 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6234 const struct GNUNET_MessageHeader *message,
6235 const struct GNUNET_ATS_Information *atsi,
6236 unsigned int atsi_count)
6238 struct GNUNET_MESH_ACK *msg;
6239 struct MeshTunnel *t;
6242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6244 msg = (struct GNUNET_MESH_ACK *) message;
6246 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6250 /* TODO notify that we dont know this tunnel (whom)? */
6251 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6254 ack = ntohl (msg->pid);
6255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6257 /* Is this a forward or backward ACK? */
6258 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6260 struct MeshTunnelChildInfo *cinfo;
6263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6264 cinfo = tunnel_get_neighbor_fc (t, peer);
6265 cinfo->fwd_ack = ack;
6266 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6267 tunnel_unlock_fwd_queues (t);
6268 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6270 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6271 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6272 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6279 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6280 tunnel_unlock_bck_queue (t);
6287 * Core handler for mesh network traffic point-to-point ack polls.
6289 * @param cls closure
6290 * @param message message
6291 * @param peer peer identity this notification is about
6292 * @param atsi performance data
6293 * @param atsi_count number of records in 'atsi'
6295 * @return GNUNET_OK to keep the connection open,
6296 * GNUNET_SYSERR to close it (signal serious error)
6299 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6300 const struct GNUNET_MessageHeader *message,
6301 const struct GNUNET_ATS_Information *atsi,
6302 unsigned int atsi_count)
6304 struct GNUNET_MESH_Poll *msg;
6305 struct MeshTunnel *t;
6307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6310 msg = (struct GNUNET_MESH_Poll *) message;
6312 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6316 /* TODO notify that we dont know this tunnel (whom)? */
6317 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6318 GNUNET_break_op (0);
6322 /* Is this a forward or backward ACK? */
6323 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6325 struct MeshTunnelChildInfo *cinfo;
6327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6328 cinfo = tunnel_get_neighbor_fc (t, peer);
6329 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6330 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6335 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6343 * Core handler for path ACKs
6345 * @param cls closure
6346 * @param message message
6347 * @param peer peer identity this notification is about
6348 * @param atsi performance data
6349 * @param atsi_count number of records in 'atsi'
6351 * @return GNUNET_OK to keep the connection open,
6352 * GNUNET_SYSERR to close it (signal serious error)
6355 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6356 const struct GNUNET_MessageHeader *message,
6357 const struct GNUNET_ATS_Information *atsi,
6358 unsigned int atsi_count)
6360 struct GNUNET_MESH_PathACK *msg;
6361 struct GNUNET_PeerIdentity id;
6362 struct MeshPeerInfo *peer_info;
6363 struct MeshPeerPath *p;
6364 struct MeshTunnel *t;
6366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6367 GNUNET_i2s (&my_full_id));
6368 msg = (struct GNUNET_MESH_PathACK *) message;
6369 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6372 /* TODO notify that we don't know the tunnel */
6373 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6375 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6379 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6381 peer_info = peer_info_get (&msg->peer_id);
6382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6383 GNUNET_i2s (&msg->peer_id));
6384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6387 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6390 "connect_by_string completed, stopping search\n");
6391 regex_cancel_search (t->regex_ctx);
6392 t->regex_ctx = NULL;
6395 /* Add paths to peers? */
6396 p = tree_get_path_to_peer (t->tree, peer_info->id);
6399 path_add_to_peers (p, GNUNET_YES);
6407 /* Message for us? */
6408 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6411 if (NULL == t->owner)
6413 GNUNET_break_op (0);
6416 if (NULL != t->dht_get_type)
6418 GNUNET_DHT_get_stop (t->dht_get_type);
6419 t->dht_get_type = NULL;
6421 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6423 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6424 send_client_peer_connected (t, peer_info->id);
6429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6430 " not for us, retransmitting...\n");
6431 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6432 peer_info = peer_info_get (&msg->oid);
6433 if (NULL == peer_info)
6435 /* If we know the tunnel, we should DEFINITELY know the peer */
6439 send_prebuilt_message (message, &id, t);
6445 * Core handler for mesh keepalives.
6447 * @param cls closure
6448 * @param message message
6449 * @param peer peer identity this notification is about
6450 * @param atsi performance data
6451 * @param atsi_count number of records in 'atsi'
6452 * @return GNUNET_OK to keep the connection open,
6453 * GNUNET_SYSERR to close it (signal serious error)
6455 * TODO: Check who we got this from, to validate route.
6458 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6459 const struct GNUNET_MessageHeader *message,
6460 const struct GNUNET_ATS_Information *atsi,
6461 unsigned int atsi_count)
6463 struct GNUNET_MESH_TunnelKeepAlive *msg;
6464 struct MeshTunnel *t;
6466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6469 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6470 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6474 /* TODO notify that we dont know that tunnel */
6475 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
6480 tunnel_reset_timeout (t);
6482 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6483 tunnel_send_multicast (t, message);
6490 * Functions to handle messages from core
6492 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6493 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6494 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6495 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6496 sizeof (struct GNUNET_MESH_PathBroken)},
6497 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6498 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6499 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6500 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6501 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6502 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6503 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6504 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6505 sizeof (struct GNUNET_MESH_ACK)},
6506 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6507 sizeof (struct GNUNET_MESH_Poll)},
6508 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6509 sizeof (struct GNUNET_MESH_PathACK)},
6515 /******************************************************************************/
6516 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6517 /******************************************************************************/
6520 * deregister_app: iterator for removing each application registered by a client
6522 * @param cls closure
6523 * @param key the hash of the application id (used to access the hashmap)
6524 * @param value the value stored at the key (client)
6526 * @return GNUNET_OK on success
6529 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6531 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6532 GNUNET_break (GNUNET_YES ==
6533 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6539 * notify_client_connection_failure: notify a client that the connection to the
6540 * requested remote peer is not possible (for instance, no route found)
6541 * Function called when the socket is ready to queue more data. "buf" will be
6542 * NULL and "size" zero if the socket was closed for writing in the meantime.
6544 * @param cls closure
6545 * @param size number of bytes available in buf
6546 * @param buf where the callee should write the message
6547 * @return number of bytes written to buf
6550 notify_client_connection_failure (void *cls, size_t size, void *buf)
6553 struct MeshPeerInfo *peer_info;
6554 struct GNUNET_MESH_PeerControl *msg;
6555 struct GNUNET_PeerIdentity id;
6557 if (0 == size && NULL == buf)
6559 // TODO retry? cancel?
6563 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6564 peer_info = (struct MeshPeerInfo *) cls;
6565 msg = (struct GNUNET_MESH_PeerControl *) buf;
6566 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6567 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6568 // msg->tunnel_id = htonl(peer_info->t->tid);
6569 GNUNET_PEER_resolve (peer_info->id, &id);
6570 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6578 * Send keepalive packets for a peer
6580 * @param cls Closure (tunnel for which to send the keepalive).
6581 * @param tc Notification context.
6584 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6586 struct MeshTunnel *t = cls;
6587 struct GNUNET_MESH_TunnelKeepAlive *msg;
6588 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6591 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6592 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6598 "sending keepalive for tunnel %d\n", t->id.tid);
6600 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6601 msg->header.size = htons (size);
6602 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6603 msg->oid = my_full_id;
6604 msg->tid = htonl (t->id.tid);
6605 tunnel_send_multicast (t, &msg->header);
6607 t->path_refresh_task =
6608 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6609 tunnel_reset_timeout(t);
6614 * Function to process paths received for a new peer addition. The recorded
6615 * paths form the initial tunnel, which can be optimized later.
6616 * Called on each result obtained for the DHT search.
6618 * @param cls closure
6619 * @param exp when will this value expire
6620 * @param key key of the result
6621 * @param get_path path of the get request
6622 * @param get_path_length lenght of get_path
6623 * @param put_path path of the put request
6624 * @param put_path_length length of the put_path
6625 * @param type type of the result
6626 * @param size number of bytes in data
6627 * @param data pointer to the result data
6629 * TODO: re-issue the request after certain time? cancel after X results?
6632 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6633 const struct GNUNET_HashCode * key,
6634 const struct GNUNET_PeerIdentity *get_path,
6635 unsigned int get_path_length,
6636 const struct GNUNET_PeerIdentity *put_path,
6637 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6638 size_t size, const void *data)
6640 struct MeshPathInfo *path_info = cls;
6641 struct MeshPeerPath *p;
6642 struct GNUNET_PeerIdentity pi;
6645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6646 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6649 p = path_build_from_dht (get_path, get_path_length, put_path,
6651 path_add_to_peers (p, GNUNET_NO);
6653 for (i = 0; i < path_info->peer->ntunnels; i++)
6655 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6656 peer_info_connect (path_info->peer, path_info->t);
6664 * Function to process paths received for a new peer addition. The recorded
6665 * paths form the initial tunnel, which can be optimized later.
6666 * Called on each result obtained for the DHT search.
6668 * @param cls closure
6669 * @param exp when will this value expire
6670 * @param key key of the result
6671 * @param get_path path of the get request
6672 * @param get_path_length lenght of get_path
6673 * @param put_path path of the put request
6674 * @param put_path_length length of the put_path
6675 * @param type type of the result
6676 * @param size number of bytes in data
6677 * @param data pointer to the result data
6680 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6681 const struct GNUNET_HashCode * key,
6682 const struct GNUNET_PeerIdentity *get_path,
6683 unsigned int get_path_length,
6684 const struct GNUNET_PeerIdentity *put_path,
6685 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6686 size_t size, const void *data)
6688 const struct PBlock *pb = data;
6689 const struct GNUNET_PeerIdentity *pi = &pb->id;
6690 struct MeshTunnel *t = cls;
6691 struct MeshPeerInfo *peer_info;
6692 struct MeshPeerPath *p;
6694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6695 if (size != sizeof (struct PBlock))
6697 GNUNET_break_op (0);
6700 if (ntohl(pb->type) != t->type)
6702 GNUNET_break_op (0);
6705 GNUNET_assert (NULL != t->owner);
6706 peer_info = peer_info_get (pi);
6707 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6709 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6711 p = path_build_from_dht (get_path, get_path_length, put_path,
6713 path_add_to_peers (p, GNUNET_NO);
6715 tunnel_add_peer (t, peer_info);
6716 peer_info_connect (peer_info, t);
6721 * Function to process DHT string to regex matching.
6722 * Called on each result obtained for the DHT search.
6724 * @param cls closure (search context)
6725 * @param exp when will this value expire
6726 * @param key key of the result
6727 * @param get_path path of the get request (not used)
6728 * @param get_path_length lenght of get_path (not used)
6729 * @param put_path path of the put request (not used)
6730 * @param put_path_length length of the put_path (not used)
6731 * @param type type of the result
6732 * @param size number of bytes in data
6733 * @param data pointer to the result data
6736 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6737 const struct GNUNET_HashCode * key,
6738 const struct GNUNET_PeerIdentity *get_path,
6739 unsigned int get_path_length,
6740 const struct GNUNET_PeerIdentity *put_path,
6741 unsigned int put_path_length,
6742 enum GNUNET_BLOCK_Type type,
6743 size_t size, const void *data)
6745 const struct MeshRegexAccept *block = data;
6746 struct MeshRegexSearchContext *ctx = cls;
6747 struct MeshRegexSearchInfo *info = ctx->info;
6748 // struct MeshPeerPath *p;
6749 struct MeshPeerInfo *peer_info;
6751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6753 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6755 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6758 peer_info = peer_info_get(&block->id);
6759 // p = path_build_from_dht (get_path, get_path_length, put_path,
6760 // put_path_length);
6761 // path_add_to_peers (p, GNUNET_NO);
6764 tunnel_add_peer (info->t, peer_info);
6765 peer_info_connect (peer_info, info->t);
6766 if (0 == info->peer)
6768 info->peer = peer_info->id;
6772 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6775 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
6778 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6779 ®ex_connect_timeout,
6787 * Function to process DHT string to regex matching.
6788 * Called on each result obtained for the DHT search.
6790 * @param cls closure (search context)
6791 * @param exp when will this value expire
6792 * @param key key of the result
6793 * @param get_path path of the get request (not used)
6794 * @param get_path_length lenght of get_path (not used)
6795 * @param put_path path of the put request (not used)
6796 * @param put_path_length length of the put_path (not used)
6797 * @param type type of the result
6798 * @param size number of bytes in data
6799 * @param data pointer to the result data
6801 * TODO: re-issue the request after certain time? cancel after X results?
6804 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6805 const struct GNUNET_HashCode * key,
6806 const struct GNUNET_PeerIdentity *get_path,
6807 unsigned int get_path_length,
6808 const struct GNUNET_PeerIdentity *put_path,
6809 unsigned int put_path_length,
6810 enum GNUNET_BLOCK_Type type,
6811 size_t size, const void *data)
6813 const struct MeshRegexBlock *block = data;
6814 struct MeshRegexSearchContext *ctx = cls;
6815 struct MeshRegexSearchInfo *info = ctx->info;
6819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6820 "DHT GET STRING RETURNED RESULTS\n");
6821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6822 " key: %s\n", GNUNET_h2s (key));
6824 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6826 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6829 copy = GNUNET_malloc (size);
6830 memcpy (copy, data, size);
6831 GNUNET_break (GNUNET_OK ==
6832 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6833 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6834 len = ntohl (block->n_proof);
6836 char proof[len + 1];
6838 memcpy (proof, &block[1], len);
6840 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6842 GNUNET_break_op (0);
6846 len = strlen (info->description);
6847 if (len == ctx->position) // String processed
6849 if (GNUNET_YES == ntohl (block->accepting))
6851 regex_find_path(key, ctx);
6855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6856 // FIXME REGEX this block not successful, wait for more? start timeout?
6861 regex_next_edge (block, size, ctx);
6866 /******************************************************************************/
6867 /********************* MESH LOCAL HANDLES **************************/
6868 /******************************************************************************/
6872 * Handler for client disconnection
6874 * @param cls closure
6875 * @param client identification of the client; NULL
6876 * for the last call when the server is destroyed
6879 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6881 struct MeshClient *c;
6882 struct MeshClient *next;
6885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6895 if (c->handle != client)
6897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6903 GNUNET_SERVER_client_drop (c->handle);
6904 c->shutting_down = GNUNET_YES;
6905 GNUNET_assert (NULL != c->own_tunnels);
6906 GNUNET_assert (NULL != c->incoming_tunnels);
6907 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6908 &tunnel_destroy_iterator, c);
6909 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6910 &tunnel_destroy_iterator, c);
6911 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6912 &tunnel_destroy_iterator, c);
6913 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6914 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6915 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6917 /* deregister clients applications */
6918 if (NULL != c->apps)
6920 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6921 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6923 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6924 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6926 GNUNET_SCHEDULER_cancel (announce_applications_task);
6927 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6929 if (NULL != c->types)
6930 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6931 for (i = 0; i < c->n_regex; i++)
6933 GNUNET_free (c->regexes[i].regex);
6934 if (NULL != c->regexes[i].dfa)
6935 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6937 GNUNET_free_non_null (c->regexes);
6938 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6939 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6941 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6944 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6953 * Handler for new clients
6955 * @param cls closure
6956 * @param client identification of the client
6957 * @param message the actual message, which includes messages the client wants
6960 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6961 const struct GNUNET_MessageHeader *message)
6963 struct GNUNET_MESH_ClientConnect *cc_msg;
6964 struct MeshClient *c;
6965 GNUNET_MESH_ApplicationType *a;
6972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6973 /* Check data sanity */
6974 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6975 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6976 ntypes = ntohs (cc_msg->types);
6977 napps = ntohs (cc_msg->applications);
6979 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6982 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6986 /* Create new client structure */
6987 c = GNUNET_malloc (sizeof (struct MeshClient));
6988 c->id = next_client_id++;
6989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6991 GNUNET_SERVER_client_keep (client);
6992 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6995 GNUNET_MESH_ApplicationType at;
6996 struct GNUNET_HashCode hc;
6998 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6999 for (i = 0; i < napps; i++)
7002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
7003 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
7004 /* store in clients hashmap */
7005 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
7006 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
7007 /* store in global hashmap, for announcements */
7008 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
7009 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
7011 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
7012 announce_applications_task =
7013 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
7019 struct GNUNET_HashCode hc;
7021 t = (uint16_t *) & a[napps];
7022 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
7023 for (i = 0; i < ntypes; i++)
7026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
7027 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
7029 /* store in clients hashmap */
7030 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
7031 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
7032 /* store in global hashmap */
7033 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
7034 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
7037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7038 " client has %u+%u subscriptions\n", napps, ntypes);
7040 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
7041 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7042 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7043 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7044 GNUNET_SERVER_notification_context_add (nc, client);
7045 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
7047 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
7053 * Handler for clients announcing available services by a regular expression.
7055 * @param cls closure
7056 * @param client identification of the client
7057 * @param message the actual message, which includes messages the client wants
7060 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
7061 const struct GNUNET_MessageHeader *message)
7063 const struct GNUNET_MESH_RegexAnnounce *msg;
7064 struct MeshRegexDescriptor rd;
7065 struct MeshClient *c;
7070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
7072 /* Sanity check for client registration */
7073 if (NULL == (c = client_get (client)))
7076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7081 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
7083 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
7084 if (NULL != c->partial_regex)
7086 regex = c->partial_regex;
7087 offset = strlen (c->partial_regex);
7088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7089 " continuation, already have %u bytes\n",
7098 regex = GNUNET_realloc (regex, offset + len + 1);
7099 memcpy (®ex[offset], &msg[1], len);
7100 regex[offset + len] = '\0';
7101 if (0 == ntohs (msg->last))
7103 c->partial_regex = regex;
7104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7105 " not ended, stored %u bytes for later\n",
7107 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7111 rd.compression = ntohs (msg->compression_characters);
7113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
7114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
7115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
7116 GNUNET_array_append (c->regexes, c->n_regex, rd);
7117 c->partial_regex = NULL;
7118 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
7120 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
7126 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
7132 * Handler for requests of new tunnels
7134 * @param cls closure
7135 * @param client identification of the client
7136 * @param message the actual message
7139 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
7140 const struct GNUNET_MessageHeader *message)
7142 struct GNUNET_MESH_TunnelMessage *t_msg;
7143 struct MeshTunnel *t;
7144 struct MeshClient *c;
7145 MESH_TunnelNumber tid;
7147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
7149 /* Sanity check for client registration */
7150 if (NULL == (c = client_get (client)))
7153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7158 /* Message sanity check */
7159 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7162 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7166 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7167 /* Sanity check for tunnel numbering */
7168 tid = ntohl (t_msg->tunnel_id);
7169 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
7172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7175 /* Sanity check for duplicate tunnel IDs */
7176 if (NULL != tunnel_get_by_local_id (c, tid))
7179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7183 while (NULL != tunnel_get_by_pi (myid, next_tid))
7184 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7185 t = tunnel_new (myid, next_tid++, c, tid);
7188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
7190 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7193 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
7195 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
7196 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
7199 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7205 * Handler for requests of deleting tunnels
7207 * @param cls closure
7208 * @param client identification of the client
7209 * @param message the actual message
7212 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
7213 const struct GNUNET_MessageHeader *message)
7215 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7216 struct MeshClient *c;
7217 struct MeshTunnel *t;
7218 MESH_TunnelNumber tid;
7220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7221 "Got a DESTROY TUNNEL from client!\n");
7223 /* Sanity check for client registration */
7224 if (NULL == (c = client_get (client)))
7227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7232 /* Message sanity check */
7233 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7240 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7242 /* Retrieve tunnel */
7243 tid = ntohl (tunnel_msg->tunnel_id);
7244 t = tunnel_get_by_local_id(c, tid);
7247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7252 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7254 client_ignore_tunnel (c, t);
7255 tunnel_destroy_empty (t);
7256 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7259 send_client_tunnel_disconnect (t, c);
7260 client_delete_tunnel (c, t);
7262 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7264 tunnel_send_destroy (t, 0);
7265 t->destroy = GNUNET_YES;
7266 /* The tunnel will be destroyed when the last message is transmitted. */
7267 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7273 * Handler for requests of seeting tunnel's speed.
7275 * @param cls Closure (unused).
7276 * @param client Identification of the client.
7277 * @param message The actual message.
7280 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7281 const struct GNUNET_MessageHeader *message)
7283 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7284 struct MeshClient *c;
7285 struct MeshTunnel *t;
7286 MESH_TunnelNumber tid;
7288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7289 "Got a SPEED request from client!\n");
7291 /* Sanity check for client registration */
7292 if (NULL == (c = client_get (client)))
7295 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7301 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7303 /* Retrieve tunnel */
7304 tid = ntohl (tunnel_msg->tunnel_id);
7305 t = tunnel_get_by_local_id(c, tid);
7308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7314 switch (ntohs(message->type))
7316 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7317 t->speed_min = GNUNET_YES;
7319 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7320 t->speed_min = GNUNET_NO;
7325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7330 * Handler for requests of seeting tunnel's buffering policy.
7332 * @param cls Closure (unused).
7333 * @param client Identification of the client.
7334 * @param message The actual message.
7337 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7338 const struct GNUNET_MessageHeader *message)
7340 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7341 struct MeshClient *c;
7342 struct MeshTunnel *t;
7343 MESH_TunnelNumber tid;
7345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7346 "Got a BUFFER request from client!\n");
7348 /* Sanity check for client registration */
7349 if (NULL == (c = client_get (client)))
7352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7357 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7359 /* Retrieve tunnel */
7360 tid = ntohl (tunnel_msg->tunnel_id);
7361 t = tunnel_get_by_local_id(c, tid);
7364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7370 switch (ntohs(message->type))
7372 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7373 t->nobuffer = GNUNET_NO;
7375 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7376 t->nobuffer = GNUNET_YES;
7382 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7387 * Handler for connection requests to new peers
7389 * @param cls closure
7390 * @param client identification of the client
7391 * @param message the actual message (PeerControl)
7394 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7395 const struct GNUNET_MessageHeader *message)
7397 struct GNUNET_MESH_PeerControl *peer_msg;
7398 struct MeshPeerInfo *peer_info;
7399 struct MeshClient *c;
7400 struct MeshTunnel *t;
7401 MESH_TunnelNumber tid;
7403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7404 /* Sanity check for client registration */
7405 if (NULL == (c = client_get (client)))
7408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7413 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7415 /* Sanity check for message size */
7416 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7423 /* Tunnel exists? */
7424 tid = ntohl (peer_msg->tunnel_id);
7425 t = tunnel_get_by_local_id (c, tid);
7429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7433 /* Does client own tunnel? */
7434 if (t->owner->handle != client)
7437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7441 GNUNET_i2s (&peer_msg->peer));
7442 peer_info = peer_info_get (&peer_msg->peer);
7444 tunnel_add_peer (t, peer_info);
7445 peer_info_connect (peer_info, t);
7447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7453 * Handler for disconnection requests of peers in a tunnel
7455 * @param cls closure
7456 * @param client identification of the client
7457 * @param message the actual message (PeerControl)
7460 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7461 const struct GNUNET_MessageHeader *message)
7463 struct GNUNET_MESH_PeerControl *peer_msg;
7464 struct MeshPeerInfo *peer_info;
7465 struct MeshClient *c;
7466 struct MeshTunnel *t;
7467 MESH_TunnelNumber tid;
7469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7470 /* Sanity check for client registration */
7471 if (NULL == (c = client_get (client)))
7474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7479 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7481 /* Sanity check for message size */
7482 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7489 /* Tunnel exists? */
7490 tid = ntohl (peer_msg->tunnel_id);
7491 t = tunnel_get_by_local_id (c, tid);
7495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7500 /* Does client own tunnel? */
7501 if (t->owner->handle != client)
7504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7509 GNUNET_i2s (&peer_msg->peer));
7510 /* Is the peer in the tunnel? */
7512 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7513 if (NULL == peer_info)
7516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7520 /* Ok, delete peer from tunnel */
7521 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7522 &peer_msg->peer.hashPubKey);
7524 send_destroy_path (t, peer_info->id);
7525 tunnel_delete_peer (t, peer_info->id);
7526 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7531 * Handler for blacklist requests of peers in a tunnel
7533 * @param cls closure
7534 * @param client identification of the client
7535 * @param message the actual message (PeerControl)
7537 * FIXME implement DHT block bloomfilter
7540 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7541 const struct GNUNET_MessageHeader *message)
7543 struct GNUNET_MESH_PeerControl *peer_msg;
7544 struct MeshClient *c;
7545 struct MeshTunnel *t;
7546 MESH_TunnelNumber tid;
7548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7549 /* Sanity check for client registration */
7550 if (NULL == (c = client_get (client)))
7553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7558 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7560 /* Sanity check for message size */
7561 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7568 /* Tunnel exists? */
7569 tid = ntohl (peer_msg->tunnel_id);
7570 t = tunnel_get_by_local_id (c, tid);
7574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7579 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7580 GNUNET_PEER_intern(&peer_msg->peer));
7585 * Handler for unblacklist requests of peers in a tunnel
7587 * @param cls closure
7588 * @param client identification of the client
7589 * @param message the actual message (PeerControl)
7592 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7593 const struct GNUNET_MessageHeader *message)
7595 struct GNUNET_MESH_PeerControl *peer_msg;
7596 struct MeshClient *c;
7597 struct MeshTunnel *t;
7598 MESH_TunnelNumber tid;
7602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7603 /* Sanity check for client registration */
7604 if (NULL == (c = client_get (client)))
7607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7612 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7614 /* Sanity check for message size */
7615 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7618 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7622 /* Tunnel exists? */
7623 tid = ntohl (peer_msg->tunnel_id);
7624 t = tunnel_get_by_local_id (c, tid);
7628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7633 /* if peer is not known, complain */
7634 pid = GNUNET_PEER_search (&peer_msg->peer);
7641 /* search and remove from list */
7642 for (i = 0; i < t->nblacklisted; i++)
7644 if (t->blacklisted[i] == pid)
7646 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7647 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7652 /* if peer hasn't been blacklisted, complain */
7658 * Handler for connection requests to new peers by type
7660 * @param cls closure
7661 * @param client identification of the client
7662 * @param message the actual message (ConnectPeerByType)
7665 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7666 const struct GNUNET_MessageHeader *message)
7668 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7669 struct MeshClient *c;
7670 struct MeshTunnel *t;
7671 struct GNUNET_HashCode hash;
7672 MESH_TunnelNumber tid;
7674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7675 /* Sanity check for client registration */
7676 if (NULL == (c = client_get (client)))
7679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7684 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7686 /* Sanity check for message size */
7687 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7688 ntohs (connect_msg->header.size))
7691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7695 /* Tunnel exists? */
7696 tid = ntohl (connect_msg->tunnel_id);
7697 t = tunnel_get_by_local_id (c, tid);
7701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7705 /* Does client own tunnel? */
7706 if (t->owner->handle != client)
7709 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7713 /* Do WE have the service? */
7714 t->type = ntohl (connect_msg->type);
7715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7716 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7717 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7720 /* Yes! Fast forward, add ourselves to the tunnel and send the
7721 * good news to the client, and alert the destination client of
7722 * an incoming tunnel.
7724 * FIXME send a path create to self, avoid code duplication
7726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7727 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7728 peer_info_get (&my_full_id),
7729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7732 send_client_peer_connected (t, myid);
7733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7734 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7736 t->local_tid_dest = next_local_tid++;
7737 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7738 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7739 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7743 /* Ok, lets find a peer offering the service */
7744 if (NULL != t->dht_get_type)
7746 GNUNET_DHT_get_stop (t->dht_get_type);
7748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7749 GNUNET_h2s (&hash));
7751 GNUNET_DHT_get_start (dht_handle,
7752 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7754 dht_replication_level,
7755 GNUNET_DHT_RO_RECORD_ROUTE |
7756 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7758 &dht_get_type_handler, t);
7760 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7766 * Handler for connection requests to new peers by a string service description.
7768 * @param cls closure
7769 * @param client identification of the client
7770 * @param message the actual message, which includes messages the client wants
7773 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7774 const struct GNUNET_MessageHeader *message)
7776 struct GNUNET_MESH_ConnectPeerByString *msg;
7777 struct MeshRegexSearchContext *ctx;
7778 struct MeshRegexSearchInfo *info;
7779 struct GNUNET_DHT_GetHandle *get_h;
7780 struct GNUNET_HashCode key;
7781 struct MeshTunnel *t;
7782 struct MeshClient *c;
7783 MESH_TunnelNumber tid;
7788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7789 "Connect by string started\n");
7790 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7791 size = htons (message->size);
7793 /* Sanity check for client registration */
7794 if (NULL == (c = client_get (client)))
7797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7802 /* Message size sanity check */
7803 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7806 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7810 /* Tunnel exists? */
7811 tid = ntohl (msg->tunnel_id);
7812 t = tunnel_get_by_local_id (c, tid);
7816 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7820 /* Does client own tunnel? */
7821 if (t->owner->handle != client)
7824 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7829 " on tunnel %s [%u]\n",
7830 GNUNET_i2s(&my_full_id),
7833 /* Only one connect_by_string allowed at the same time! */
7834 /* FIXME: allow more, return handle at api level to cancel, document */
7835 if (NULL != t->regex_ctx)
7838 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7842 /* Find string itself */
7843 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7844 string = (const char *) &msg[1];
7846 /* Initialize context */
7847 size = GNUNET_REGEX_get_first_key (string, len, &key);
7848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7849 " consumed %u bits out of %u\n", size, len);
7850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7851 " looking for %s\n", GNUNET_h2s (&key));
7853 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7855 info->description = GNUNET_strndup (string, len);
7856 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7857 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7860 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7861 ctx->position = size;
7865 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7867 /* Start search in DHT */
7868 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7869 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7870 &key, /* key to search */
7871 dht_replication_level, /* replication level */
7872 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7873 &info->description[size], /* xquery */
7874 // FIXME add BLOOMFILTER to exclude filtered peers
7875 len + 1 - size, /* xquery bits */
7876 // FIXME add BLOOMFILTER SIZE
7877 &dht_get_string_handler, ctx);
7879 GNUNET_break (GNUNET_OK ==
7880 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7883 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7885 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7891 * Handler for client traffic directed to one peer
7893 * @param cls closure
7894 * @param client identification of the client
7895 * @param message the actual message
7898 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7899 const struct GNUNET_MessageHeader *message)
7901 struct MeshClient *c;
7902 struct MeshTunnel *t;
7903 struct MeshPeerInfo *pi;
7904 struct GNUNET_MESH_Unicast *data_msg;
7905 MESH_TunnelNumber tid;
7908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7909 "Got a unicast request from a client!\n");
7911 /* Sanity check for client registration */
7912 if (NULL == (c = client_get (client)))
7915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7920 data_msg = (struct GNUNET_MESH_Unicast *) message;
7922 /* Sanity check for message size */
7923 size = ntohs (message->size);
7924 if (sizeof (struct GNUNET_MESH_Unicast) +
7925 sizeof (struct GNUNET_MessageHeader) > size)
7928 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7932 /* Tunnel exists? */
7933 tid = ntohl (data_msg->tid);
7934 t = tunnel_get_by_local_id (c, tid);
7938 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7942 /* Is it a local tunnel? Then, does client own the tunnel? */
7943 if (t->owner->handle != client)
7946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7950 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7951 &data_msg->destination.hashPubKey);
7952 /* Is the selected peer in the tunnel? */
7956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7960 /* PID should be as expected */
7961 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7964 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7965 "Unicast PID, expected %u, got %u\n",
7966 t->fwd_pid + 1, ntohl (data_msg->pid));
7967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7971 /* Ok, everything is correct, send the message
7972 * (pretend we got it from a mesh peer)
7975 /* Work around const limitation */
7976 char buf[ntohs (message->size)] GNUNET_ALIGN;
7977 struct GNUNET_MESH_Unicast *copy;
7979 copy = (struct GNUNET_MESH_Unicast *) buf;
7980 memcpy (buf, data_msg, size);
7981 copy->oid = my_full_id;
7982 copy->tid = htonl (t->id.tid);
7983 copy->ttl = htonl (default_ttl);
7984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7985 " calling generic handler...\n");
7986 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7989 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7996 * Handler for client traffic directed to the origin
7998 * @param cls closure
7999 * @param client identification of the client
8000 * @param message the actual message
8003 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
8004 const struct GNUNET_MessageHeader *message)
8006 struct GNUNET_MESH_ToOrigin *data_msg;
8007 struct MeshTunnelClientInfo *clinfo;
8008 struct MeshClient *c;
8009 struct MeshTunnel *t;
8010 MESH_TunnelNumber tid;
8013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8014 "Got a ToOrigin request from a client!\n");
8015 /* Sanity check for client registration */
8016 if (NULL == (c = client_get (client)))
8019 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8024 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
8026 /* Sanity check for message size */
8027 size = ntohs (message->size);
8028 if (sizeof (struct GNUNET_MESH_ToOrigin) +
8029 sizeof (struct GNUNET_MessageHeader) > size)
8032 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8036 /* Tunnel exists? */
8037 tid = ntohl (data_msg->tid);
8038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8039 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
8042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8045 t = tunnel_get_by_local_id (c, tid);
8048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8049 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8051 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8055 /* It should be sent by someone who has this as incoming tunnel. */
8056 if (GNUNET_NO == client_knows_tunnel (c, t))
8059 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8063 /* PID should be as expected */
8064 clinfo = tunnel_get_client_fc (t, c);
8065 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
8068 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8069 "To Origin PID, expected %u, got %u\n",
8070 clinfo->bck_pid + 1,
8071 ntohl (data_msg->pid));
8072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8077 /* Ok, everything is correct, send the message
8078 * (pretend we got it from a mesh peer)
8081 char buf[ntohs (message->size)] GNUNET_ALIGN;
8082 struct GNUNET_MESH_ToOrigin *copy;
8084 /* Work around const limitation */
8085 copy = (struct GNUNET_MESH_ToOrigin *) buf;
8086 memcpy (buf, data_msg, size);
8087 GNUNET_PEER_resolve (t->id.oid, ©->oid);
8088 copy->tid = htonl (t->id.tid);
8089 copy->ttl = htonl (default_ttl);
8090 copy->pid = htonl (t->bck_pid + 1);
8092 copy->sender = my_full_id;
8093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8094 " calling generic handler...\n");
8095 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
8097 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8104 * Handler for client traffic directed to all peers in a tunnel
8106 * @param cls closure
8107 * @param client identification of the client
8108 * @param message the actual message
8111 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
8112 const struct GNUNET_MessageHeader *message)
8114 struct MeshClient *c;
8115 struct MeshTunnel *t;
8116 struct GNUNET_MESH_Multicast *data_msg;
8117 MESH_TunnelNumber tid;
8119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8120 "Got a multicast request from a client!\n");
8122 /* Sanity check for client registration */
8123 if (NULL == (c = client_get (client)))
8126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8131 data_msg = (struct GNUNET_MESH_Multicast *) message;
8133 /* Sanity check for message size */
8134 if (sizeof (struct GNUNET_MESH_Multicast) +
8135 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
8138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8142 /* Tunnel exists? */
8143 tid = ntohl (data_msg->tid);
8144 t = tunnel_get_by_local_id (c, tid);
8148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8149 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8155 /* Does client own tunnel? */
8156 if (t->owner->handle != client)
8159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8163 /* PID should be as expected */
8164 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
8167 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8168 "Multicast PID, expected %u, got %u\n",
8169 t->fwd_pid + 1, ntohl (data_msg->pid));
8170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8175 char buf[ntohs (message->size)] GNUNET_ALIGN;
8176 struct GNUNET_MESH_Multicast *copy;
8178 copy = (struct GNUNET_MESH_Multicast *) buf;
8179 memcpy (buf, message, ntohs (message->size));
8180 copy->oid = my_full_id;
8181 copy->tid = htonl (t->id.tid);
8182 copy->ttl = htonl (default_ttl);
8183 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
8184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8185 " calling generic handler...\n");
8186 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
8189 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
8195 * Handler for client's ACKs for payload traffic.
8197 * @param cls Closure (unused).
8198 * @param client Identification of the client.
8199 * @param message The actual message.
8202 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
8203 const struct GNUNET_MessageHeader *message)
8205 struct GNUNET_MESH_LocalAck *msg;
8206 struct MeshTunnel *t;
8207 struct MeshClient *c;
8208 MESH_TunnelNumber tid;
8211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
8212 /* Sanity check for client registration */
8213 if (NULL == (c = client_get (client)))
8216 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8221 msg = (struct GNUNET_MESH_LocalAck *) message;
8223 /* Tunnel exists? */
8224 tid = ntohl (msg->tunnel_id);
8225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8226 t = tunnel_get_by_local_id (c, tid);
8230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8231 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8236 ack = ntohl (msg->max_pid);
8237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8239 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8240 if (NULL != t->owner && t->owner->handle == client)
8242 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8244 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8248 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8249 tunnel_set_client_fwd_ack (t, c, ack);
8250 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8253 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8260 * Iterator over all peers to send a monitoring client info about a tunnel.
8262 * @param cls Closure (message being built).
8263 * @param key Key (hashed tunnel ID, unused).
8264 * @param value Peer info.
8266 * @return GNUNET_YES, to keep iterating.
8269 monitor_peers_iterator (void *cls,
8270 const struct GNUNET_HashCode * key,
8273 struct GNUNET_MESH_LocalMonitor *msg = cls;
8274 struct GNUNET_PeerIdentity *id;
8275 struct MeshPeerInfo *info = value;
8277 id = (struct GNUNET_PeerIdentity *) &msg[1];
8278 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8281 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8282 "* sending info about peer %s [%u]\n",
8283 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8291 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8293 * @param cls Closure (client handle).
8294 * @param key Key (hashed tunnel ID, unused).
8295 * @param value Tunnel info.
8297 * @return GNUNET_YES, to keep iterating.
8300 monitor_all_tunnels_iterator (void *cls,
8301 const struct GNUNET_HashCode * key,
8304 struct GNUNET_SERVER_Client *client = cls;
8305 struct MeshTunnel *t = value;
8306 struct GNUNET_MESH_LocalMonitor *msg;
8309 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8310 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8311 npeers * sizeof (struct GNUNET_PeerIdentity));
8312 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8313 msg->tunnel_id = htonl (t->id.tid);
8314 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8315 npeers * sizeof (struct GNUNET_PeerIdentity));
8316 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8318 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8319 monitor_peers_iterator,
8322 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8323 "* sending info about tunnel %s [%u] (%u peers)\n",
8324 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8326 if (msg->npeers != npeers)
8329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8330 "Get tunnels fail: size %u - iter %u\n",
8331 npeers, msg->npeers);
8334 msg->npeers = htonl (npeers);
8335 GNUNET_SERVER_notification_context_unicast (nc, client,
8336 &msg->header, GNUNET_NO);
8342 * Handler for client's MONITOR request.
8344 * @param cls Closure (unused).
8345 * @param client Identification of the client.
8346 * @param message The actual message.
8349 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8350 const struct GNUNET_MessageHeader *message)
8352 struct MeshClient *c;
8354 /* Sanity check for client registration */
8355 if (NULL == (c = client_get (client)))
8358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8362 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8363 "Received get tunnels request from client %u\n",
8365 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8366 monitor_all_tunnels_iterator,
8368 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8369 "Get tunnels request from client %u completed\n",
8371 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8376 * Data needed to build a Monitor_Tunnel message.
8378 struct MeshMonitorTunnelContext
8381 * Partial message, including peer count.
8383 struct GNUNET_MESH_LocalMonitor *msg;
8386 * Hashmap with positions: peer->position.
8388 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8391 * Index of the parent of each peer in the message, realtive to the absolute
8392 * order in the array (can be in a previous message).
8394 uint32_t parents[1024];
8397 * Peers visited so far in the tree, aka position of the current peer.
8399 unsigned int npeers;
8402 * Client requesting the info.
8404 struct MeshClient *c;
8409 * Send a client a message about the structure of a tunnel.
8411 * @param ctx Context of the tunnel iteration, with info regarding the state
8412 * of the execution and the number of peers visited for this message.
8415 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8417 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8418 struct GNUNET_PeerIdentity *pid;
8419 unsigned int *parent;
8422 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8423 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8424 resp->header.size = htons (size);
8425 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8426 parent = (unsigned int *) &pid[resp->npeers];
8427 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8428 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8429 &resp->header, GNUNET_NO);
8433 * Iterator over a tunnel tree to build a message containing all peers
8434 * the in the tunnel, including relay nodes.
8436 * @param cls Closure (pointer to pointer of message being built).
8437 * @param peer Short ID of a peer.
8438 * @param parent Short ID of the @c peer 's parent.
8441 tunnel_tree_iterator (void *cls,
8442 GNUNET_PEER_Id peer,
8443 GNUNET_PEER_Id parent)
8445 struct MeshMonitorTunnelContext *ctx = cls;
8446 struct GNUNET_MESH_LocalMonitor *msg;
8447 struct GNUNET_PeerIdentity *pid;
8448 struct GNUNET_PeerIdentity ppid;
8451 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8452 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8453 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8454 &pid[msg->npeers].hashPubKey,
8455 (void *) (long) ctx->npeers,
8456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8457 GNUNET_PEER_resolve (parent, &ppid);
8458 ctx->parents[msg->npeers] =
8459 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8465 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8467 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8470 send_client_tunnel_info (ctx);
8477 * Handler for client's MONITOR_TUNNEL request.
8479 * @param cls Closure (unused).
8480 * @param client Identification of the client.
8481 * @param message The actual message.
8484 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8485 const struct GNUNET_MessageHeader *message)
8487 const struct GNUNET_MESH_LocalMonitor *msg;
8488 struct GNUNET_MESH_LocalMonitor *resp;
8489 struct MeshMonitorTunnelContext ctx;
8490 struct MeshClient *c;
8491 struct MeshTunnel *t;
8493 /* Sanity check for client registration */
8494 if (NULL == (c = client_get (client)))
8497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8501 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8502 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8503 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8506 ntohl (msg->tunnel_id));
8507 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8510 /* We don't know the tunnel */
8511 struct GNUNET_MESH_LocalMonitor warn;
8514 warn.npeers = htonl (UINT_MAX);
8515 GNUNET_SERVER_notification_context_unicast (nc, client,
8518 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8522 /* Initialize context */
8523 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8527 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8531 /* Collect and send information */
8532 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8533 send_client_tunnel_info (&ctx);
8536 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8539 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8540 "Monitor tunnel request from client %u completed\n",
8542 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8547 * Functions to handle messages from clients
8549 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8550 {&handle_local_new_client, NULL,
8551 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8552 {&handle_local_announce_regex, NULL,
8553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8554 {&handle_local_tunnel_create, NULL,
8555 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8556 sizeof (struct GNUNET_MESH_TunnelMessage)},
8557 {&handle_local_tunnel_destroy, NULL,
8558 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8559 sizeof (struct GNUNET_MESH_TunnelMessage)},
8560 {&handle_local_tunnel_speed, NULL,
8561 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8562 sizeof (struct GNUNET_MESH_TunnelMessage)},
8563 {&handle_local_tunnel_speed, NULL,
8564 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8565 sizeof (struct GNUNET_MESH_TunnelMessage)},
8566 {&handle_local_tunnel_buffer, NULL,
8567 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8568 sizeof (struct GNUNET_MESH_TunnelMessage)},
8569 {&handle_local_tunnel_buffer, NULL,
8570 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8571 sizeof (struct GNUNET_MESH_TunnelMessage)},
8572 {&handle_local_connect_add, NULL,
8573 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8574 sizeof (struct GNUNET_MESH_PeerControl)},
8575 {&handle_local_connect_del, NULL,
8576 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8577 sizeof (struct GNUNET_MESH_PeerControl)},
8578 {&handle_local_blacklist, NULL,
8579 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8580 sizeof (struct GNUNET_MESH_PeerControl)},
8581 {&handle_local_unblacklist, NULL,
8582 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8583 sizeof (struct GNUNET_MESH_PeerControl)},
8584 {&handle_local_connect_by_type, NULL,
8585 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8586 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8587 {&handle_local_connect_by_string, NULL,
8588 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8589 {&handle_local_unicast, NULL,
8590 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8591 {&handle_local_to_origin, NULL,
8592 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8593 {&handle_local_multicast, NULL,
8594 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8595 {&handle_local_ack, NULL,
8596 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8597 sizeof (struct GNUNET_MESH_LocalAck)},
8598 {&handle_local_get_tunnels, NULL,
8599 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8600 sizeof (struct GNUNET_MessageHeader)},
8601 {&handle_local_show_tunnel, NULL,
8602 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8603 sizeof (struct GNUNET_MESH_LocalMonitor)},
8609 * To be called on core init/fail.
8611 * @param cls service closure
8612 * @param server handle to the server for this service
8613 * @param identity the public identity of this peer
8616 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8617 const struct GNUNET_PeerIdentity *identity)
8620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8621 core_handle = server;
8622 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8625 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8628 GNUNET_i2s (identity));
8629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8631 GNUNET_i2s (&my_full_id));
8632 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8634 GNUNET_abort(); // Try harder
8641 * Method called whenever a given peer connects.
8643 * @param cls closure
8644 * @param peer peer identity this notification is about
8645 * @param atsi performance data for the connection
8646 * @param atsi_count number of records in 'atsi'
8649 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8650 const struct GNUNET_ATS_Information *atsi,
8651 unsigned int atsi_count)
8653 struct MeshPeerInfo *peer_info;
8654 struct MeshPeerPath *path;
8656 DEBUG_CONN ("Peer connected\n");
8657 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8658 peer_info = peer_info_get (peer);
8659 if (myid == peer_info->id)
8661 DEBUG_CONN (" (self)\n");
8666 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8668 path = path_new (2);
8669 path->peers[0] = myid;
8670 path->peers[1] = peer_info->id;
8671 GNUNET_PEER_change_rc (myid, 1);
8672 GNUNET_PEER_change_rc (peer_info->id, 1);
8673 peer_info_add_path (peer_info, path, GNUNET_YES);
8674 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8680 * Method called whenever a peer disconnects.
8682 * @param cls closure
8683 * @param peer peer identity this notification is about
8686 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8688 struct MeshPeerInfo *pi;
8689 struct MeshPeerQueue *q;
8690 struct MeshPeerQueue *n;
8692 DEBUG_CONN ("Peer disconnected\n");
8693 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8703 /* TODO try to reroute this traffic instead */
8704 queue_destroy(q, GNUNET_YES);
8707 if (NULL != pi->core_transmit)
8709 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8710 pi->core_transmit = NULL;
8712 peer_info_remove_path (pi, pi->id, myid);
8715 DEBUG_CONN (" (self)\n");
8717 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8722 /******************************************************************************/
8723 /************************ MAIN FUNCTIONS ****************************/
8724 /******************************************************************************/
8727 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8729 * @param cls closure
8730 * @param key current key code
8731 * @param value value in the hash map
8732 * @return GNUNET_YES if we should continue to iterate,
8736 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8738 struct MeshTunnel *t = value;
8745 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8747 * @param cls closure
8748 * @param key current key code
8749 * @param value value in the hash map
8750 * @return GNUNET_YES if we should continue to iterate,
8754 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8756 struct MeshPeerInfo *p = value;
8757 struct MeshPeerQueue *q;
8758 struct MeshPeerQueue *n;
8766 queue_destroy(q, GNUNET_YES);
8770 peer_info_destroy (p);
8776 * Task run during shutdown.
8782 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8786 if (core_handle != NULL)
8788 GNUNET_CORE_disconnect (core_handle);
8793 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8796 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8797 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8798 if (dht_handle != NULL)
8800 GNUNET_DHT_disconnect (dht_handle);
8805 GNUNET_SERVER_notification_context_destroy (nc);
8808 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8810 GNUNET_SCHEDULER_cancel (announce_id_task);
8811 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8813 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8815 GNUNET_SCHEDULER_cancel (announce_applications_task);
8816 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8823 * Callback for hostkey read/generation
8825 * @param cls Closure (Configuration handle).
8826 * @param pk the private key
8827 * @param emsg error message
8830 key_generation_cb (void *cls,
8831 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8834 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8835 struct MeshPeerInfo *peer;
8836 struct MeshPeerPath *p;
8841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8842 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8844 GNUNET_SCHEDULER_shutdown ();
8847 my_private_key = pk;
8848 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8849 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8850 &my_full_id.hashPubKey);
8851 myid = GNUNET_PEER_intern (&my_full_id);
8852 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8853 "Mesh for peer [%s] starting\n",
8854 GNUNET_i2s(&my_full_id));
8856 // transport_handle = GNUNET_TRANSPORT_connect(c,
8863 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8864 NULL, /* Closure passed to MESH functions */
8865 &core_init, /* Call core_init once connected */
8866 &core_connect, /* Handle connects */
8867 &core_disconnect, /* remove peers on disconnects */
8868 NULL, /* Don't notify about all incoming messages */
8869 GNUNET_NO, /* For header only in notification */
8870 NULL, /* Don't notify about all outbound messages */
8871 GNUNET_NO, /* For header-only out notification */
8872 core_handlers); /* Register these handlers */
8874 if (core_handle == NULL)
8877 GNUNET_SCHEDULER_shutdown ();
8882 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8885 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8886 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8887 GNUNET_SERVER_disconnect_notify (server_handle,
8888 &handle_local_client_disconnect, NULL);
8892 clients_tail = NULL;
8895 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8896 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8898 /* Create a peer_info for the local peer */
8899 peer = peer_info_get (&my_full_id);
8902 GNUNET_PEER_change_rc (myid, 1);
8903 peer_info_add_path (peer, p, GNUNET_YES);
8904 GNUNET_SERVER_resume (server_handle);
8905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8910 * Process mesh requests.
8912 * @param cls closure
8913 * @param server the initialized server
8914 * @param c configuration to use
8917 run (void *cls, struct GNUNET_SERVER_Handle *server,
8918 const struct GNUNET_CONFIGURATION_Handle *c)
8922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8923 server_handle = server;
8926 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8929 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8931 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8933 GNUNET_SCHEDULER_shutdown ();
8938 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8939 &refresh_path_time))
8941 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8943 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8944 "mesh", "refresh path time");
8945 GNUNET_SCHEDULER_shutdown ();
8950 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8951 &app_announce_time))
8953 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8955 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8956 "mesh", "app announce time");
8957 GNUNET_SCHEDULER_shutdown ();
8960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8961 "APP_ANNOUNCE_TIME %llu ms\n",
8962 app_announce_time.rel_value);
8964 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8967 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8969 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8970 "mesh", "id announce time");
8971 GNUNET_SCHEDULER_shutdown ();
8976 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8979 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8981 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8982 "mesh", "connect timeout");
8983 GNUNET_SCHEDULER_shutdown ();
8988 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8991 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8993 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8994 "mesh", "max msgs queue");
8995 GNUNET_SCHEDULER_shutdown ();
9000 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
9003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
9005 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
9006 "mesh", "max tunnels");
9007 GNUNET_SCHEDULER_shutdown ();
9012 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
9015 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
9017 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
9018 "mesh", "default ttl", 64);
9023 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
9026 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
9027 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
9028 "mesh", "max peers", 1000);
9033 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
9034 &dht_replication_level))
9036 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
9038 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
9039 "mesh", "dht replication level", 3);
9040 dht_replication_level = 3;
9043 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9044 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9045 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9046 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9047 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9049 dht_handle = GNUNET_DHT_connect (c, 64);
9050 if (NULL == dht_handle)
9054 stats = GNUNET_STATISTICS_create ("mesh", c);
9056 GNUNET_SERVER_suspend (server_handle);
9057 /* Scheduled the task to clean up when shutdown is called */
9058 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
9060 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
9063 GNUNET_free (keyfile);
9068 * The main function for the mesh service.
9070 * @param argc number of arguments from the command line
9071 * @param argv command line arguments
9072 * @return 0 ok, 1 on error
9075 main (int argc, char *const *argv)
9080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
9081 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
9083 ret = (GNUNET_OK == r) ? 0 : 1;
9084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
9088 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
9089 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
9090 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);