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 send_back Do we need to notify our parent node?
4401 tunnel_send_destroy (struct MeshTunnel *t, int send_back)
4403 struct GNUNET_MESH_TunnelDestroy msg;
4404 struct GNUNET_PeerIdentity id;
4405 GNUNET_PEER_Id parent;
4407 msg.header.size = htons (sizeof (msg));
4408 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4409 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4410 msg.tid = htonl (t->id.tid);
4411 if (tree_count_children(t->tree) > 0)
4413 tunnel_send_multicast (t, &msg.header);
4415 parent = tree_get_predecessor(t->tree);
4416 if (GNUNET_NO == send_back || 0 == parent)
4419 GNUNET_PEER_resolve (parent, &id);
4420 send_prebuilt_message(&msg.header, &id, t);
4425 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4427 * @param cls Closure (Tunnel which to cancel).
4428 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4431 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4433 struct MeshTunnel *t = cls;
4434 struct MeshPeerInfo *peer_info;
4435 struct MeshPeerQueue *pq;
4436 struct MeshPeerQueue *next;
4438 peer_info = peer_info_get_short (neighbor_id);
4439 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4442 if (pq->tunnel == t)
4444 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4445 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4446 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4448 // Should have been removed on destroy children
4451 queue_destroy (pq, GNUNET_YES);
4454 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4456 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4457 peer_info->core_transmit = NULL;
4462 * Destroy the tunnel and free any allocated resources linked to it.
4464 * @param t the tunnel to destroy
4466 * @return GNUNET_OK on success
4469 tunnel_destroy (struct MeshTunnel *t)
4471 struct MeshClient *c;
4472 struct GNUNET_HashCode hash;
4483 struct GNUNET_PeerIdentity id;
4485 GNUNET_PEER_resolve (t->id.oid, &id);
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4487 GNUNET_i2s (&id), t->id.tid);
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4493 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4494 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4502 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4504 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4511 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4512 for (i = 0; i < t->nclients; i++)
4516 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4522 for (i = 0; i < t->nignore; i++)
4526 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4533 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4534 GNUNET_free_non_null (t->clients);
4535 GNUNET_free_non_null (t->ignore);
4536 GNUNET_free_non_null (t->clients_fc);
4538 if (NULL != t->peers)
4540 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4542 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4545 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4546 &tunnel_destroy_child,
4548 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4549 t->children_fc = NULL;
4551 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4552 tree_destroy (t->tree);
4554 if (NULL != t->regex_ctx)
4555 regex_cancel_search (t->regex_ctx);
4556 if (NULL != t->dht_get_type)
4557 GNUNET_DHT_get_stop (t->dht_get_type);
4558 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4559 GNUNET_SCHEDULER_cancel (t->timeout_task);
4560 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4561 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4564 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4569 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4572 * Tunnel is empty: destroy it.
4574 * @param cls Closure (Tunnel).
4575 * @param tc TaskContext.
4578 tunnel_destroy_empty_delayed (void *cls,
4579 const struct GNUNET_SCHEDULER_TaskContext *tc)
4581 struct MeshTunnel *t = cls;
4583 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4584 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4587 if (0 != t->nclients ||
4588 0 != tree_count_children (t->tree))
4593 struct GNUNET_PeerIdentity id;
4595 GNUNET_PEER_resolve (t->id.oid, &id);
4596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4597 "executing destruction of empty tunnel %s [%X]\n",
4598 GNUNET_i2s (&id), t->id.tid);
4602 tunnel_send_destroy (t, GNUNET_YES);
4603 if (0 == t->pending_messages)
4606 t->destroy = GNUNET_YES;
4611 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4613 * @param t Tunnel to destroy if empty.
4616 tunnel_destroy_empty (struct MeshTunnel *t)
4618 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4620 0 != tree_count_children (t->tree))
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4624 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4630 struct GNUNET_PeerIdentity id;
4632 GNUNET_PEER_resolve (t->id.oid, &id);
4633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4634 "scheduling destruction of empty tunnel %s [%X]\n",
4635 GNUNET_i2s (&id), t->id.tid);
4639 t->delayed_destroy =
4640 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4641 &tunnel_destroy_empty_delayed,
4647 * Create a new tunnel
4649 * @param owner Who is the owner of the tunnel (short ID).
4650 * @param tid Tunnel Number of the tunnel.
4651 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4652 * @param local Tunnel Number for the tunnel, for the client point of view.
4654 * @return A new initialized tunnel. NULL on error.
4656 static struct MeshTunnel *
4657 tunnel_new (GNUNET_PEER_Id owner,
4658 MESH_TunnelNumber tid,
4659 struct MeshClient *client,
4660 MESH_TunnelNumber local)
4662 struct MeshTunnel *t;
4663 struct GNUNET_HashCode hash;
4665 if (n_tunnels >= max_tunnels && NULL == client)
4668 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4671 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4672 t->bck_queue_max = t->fwd_queue_max;
4673 t->tree = tree_new (owner);
4675 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4676 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4677 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4678 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4679 t->local_tid = local;
4680 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4682 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4684 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4686 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4687 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4694 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4701 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4703 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4704 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4708 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4717 * Callback when removing children from a tunnel tree. Notify owner.
4719 * @param cls Closure (tunnel).
4720 * @param peer_id Short ID of the peer deleted.
4723 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
4725 struct MeshTunnel *t = cls;
4727 client_notify_peer_disconnected (t->owner, t, peer_id);
4731 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4732 * that are no longer needed, as well as nodes of no longer reachable peers.
4733 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4735 * @param t Tunnel from which to remove the path.
4736 * @param peer Short id of the peer which should be removed.
4739 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4743 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4747 "Tunnel %u [%u] has no more nodes\n",
4748 t->id.oid, t->id.tid);
4754 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4755 * client when the client disconnects. If the client is not the owner, the
4756 * owner will get notified if no more clients are in the tunnel and the client
4757 * get removed from the tunnel's list.
4759 * @param cls closure (client that is disconnecting)
4760 * @param key the hash of the local tunnel id (used to access the hashmap)
4761 * @param value the value stored at the key (tunnel to destroy)
4763 * @return GNUNET_OK, keep iterating.
4766 tunnel_destroy_iterator (void *cls,
4767 const struct GNUNET_HashCode * key,
4770 struct MeshTunnel *t = value;
4771 struct MeshClient *c = cls;
4773 send_client_tunnel_disconnect (t, c);
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4777 tunnel_delete_client (t, c);
4778 client_delete_tunnel (c, t);
4779 tunnel_destroy_empty (t);
4782 tunnel_send_destroy (t, GNUNET_YES);
4784 t->destroy = GNUNET_YES;
4791 * Timeout function, destroys tunnel if called
4793 * @param cls Closure (tunnel to destroy).
4794 * @param tc TaskContext
4797 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4799 struct MeshTunnel *t = cls;
4800 struct GNUNET_PeerIdentity id;
4802 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4803 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4805 GNUNET_PEER_resolve(t->id.oid, &id);
4806 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4807 "Tunnel %s [%X] timed out. Destroying.\n",
4808 GNUNET_i2s(&id), t->id.tid);
4809 send_clients_tunnel_destroy (t);
4814 * Resets the tunnel timeout. Starts it if no timeout was running.
4816 * @param t Tunnel whose timeout to reset.
4818 * TODO use heap to improve efficiency of scheduler.
4821 tunnel_reset_timeout (struct MeshTunnel *t)
4823 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4824 GNUNET_SCHEDULER_cancel (t->timeout_task);
4826 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4827 (refresh_path_time, 4), &tunnel_timeout, t);
4831 /******************************************************************************/
4832 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4833 /******************************************************************************/
4836 * Function to send a create path packet to a peer.
4838 * @param cls closure
4839 * @param size number of bytes available in buf
4840 * @param buf where the callee should write the message
4841 * @return number of bytes written to buf
4844 send_core_path_create (void *cls, size_t size, void *buf)
4846 struct MeshPathInfo *info = cls;
4847 struct GNUNET_MESH_ManipulatePath *msg;
4848 struct GNUNET_PeerIdentity *peer_ptr;
4849 struct MeshTunnel *t = info->t;
4850 struct MeshPeerPath *p = info->path;
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4857 sizeof (struct GNUNET_MESH_ManipulatePath) +
4858 p->length * sizeof (struct GNUNET_PeerIdentity);
4860 if (size < size_needed || NULL == buf)
4865 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4866 msg->header.size = htons (size_needed);
4867 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4868 msg->tid = ntohl (t->id.tid);
4871 if (GNUNET_YES == t->speed_min)
4872 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4873 if (GNUNET_YES == t->nobuffer)
4874 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4875 msg->opt = htonl(opt);
4878 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4879 for (i = 0; i < p->length; i++)
4881 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4888 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4894 * Fill the core buffer
4896 * @param cls closure (data itself)
4897 * @param size number of bytes available in buf
4898 * @param buf where the callee should write the message
4900 * @return number of bytes written to buf
4903 send_core_data_multicast (void *cls, size_t size, void *buf)
4905 struct MeshTransmissionDescriptor *info = cls;
4908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4909 GNUNET_assert (NULL != info);
4910 GNUNET_assert (NULL != info->peer);
4911 total_size = info->mesh_data->data_len;
4912 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4914 if (total_size > size)
4919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4920 memcpy (buf, info->mesh_data->data, total_size);
4923 struct GNUNET_MESH_Multicast *mc;
4924 struct GNUNET_MessageHeader *mh;
4927 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4929 mc = (struct GNUNET_MESH_Multicast *) mh;
4930 mh = (struct GNUNET_MessageHeader *) &mc[1];
4931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4932 " multicast, payload type %s\n",
4933 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4935 " multicast, payload size %u\n", ntohs (mh->size));
4939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4940 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4944 data_descriptor_decrement_rc (info->mesh_data);
4945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4953 * Creates a path ack message in buf and frees all unused resources.
4955 * @param cls closure (MeshTransmissionDescriptor)
4956 * @param size number of bytes available in buf
4957 * @param buf where the callee should write the message
4958 * @return number of bytes written to buf
4961 send_core_path_ack (void *cls, size_t size, void *buf)
4963 struct MeshTransmissionDescriptor *info = cls;
4964 struct GNUNET_MESH_PathACK *msg = buf;
4966 GNUNET_assert (NULL != info);
4967 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4972 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4973 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4974 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4975 msg->tid = htonl (info->origin->tid);
4976 msg->peer_id = my_full_id;
4979 /* TODO add signature */
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4982 return sizeof (struct GNUNET_MESH_PathACK);
4987 * Free a transmission that was already queued with all resources
4988 * associated to the request.
4990 * @param queue Queue handler to cancel.
4991 * @param clear_cls Is it necessary to free associated cls?
4994 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4996 struct MeshTransmissionDescriptor *dd;
4997 struct MeshPathInfo *path_info;
4998 struct MeshTunnelChildInfo *cinfo;
4999 struct GNUNET_PeerIdentity id;
5003 if (GNUNET_YES == clear_cls)
5005 switch (queue->type)
5007 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5008 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
5009 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
5011 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5012 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5013 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5014 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5015 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5016 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5018 " prebuilt message\n");
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5021 GNUNET_MESH_DEBUG_M2S(queue->type));
5023 data_descriptor_decrement_rc (dd->mesh_data);
5025 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
5027 path_info = queue->cls;
5028 path_destroy (path_info->path);
5032 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5033 " type %s unknown!\n",
5034 GNUNET_MESH_DEBUG_M2S(queue->type));
5036 GNUNET_free_non_null (queue->cls);
5038 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
5039 queue->peer->queue_tail,
5042 /* Delete from child_fc in the appropiate tunnel */
5043 max = queue->tunnel->fwd_queue_max;
5044 GNUNET_PEER_resolve (queue->peer->id, &id);
5045 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
5048 for (i = 0; i < cinfo->send_buffer_n; i++)
5051 i2 = (cinfo->send_buffer_start + i) % max;
5052 if (cinfo->send_buffer[i2] == queue)
5054 /* Found corresponding entry in the send_buffer. Move all others back. */
5059 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
5061 j2 = (cinfo->send_buffer_start + j) % max;
5062 j3 = (cinfo->send_buffer_start + j + 1) % max;
5063 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
5066 cinfo->send_buffer[j3] = NULL;
5067 cinfo->send_buffer_n--;
5072 GNUNET_free (queue);
5077 * @brief Get the next transmittable message from the queue.
5079 * This will be the head, except in the case of being a data packet
5080 * not allowed by the destination peer.
5082 * @param peer Destination peer.
5084 * @return The next viable MeshPeerQueue element to send to that peer.
5085 * NULL when there are no transmittable messages.
5087 struct MeshPeerQueue *
5088 queue_get_next (const struct MeshPeerInfo *peer)
5090 struct MeshPeerQueue *q;
5091 struct MeshTunnel *t;
5092 struct MeshTransmissionDescriptor *info;
5093 struct MeshTunnelChildInfo *cinfo;
5094 struct GNUNET_MESH_Unicast *ucast;
5095 struct GNUNET_MESH_ToOrigin *to_orig;
5096 struct GNUNET_MESH_Multicast *mcast;
5097 struct GNUNET_PeerIdentity id;
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
5102 for (q = peer->queue_head; NULL != q; q = q->next)
5106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5108 GNUNET_MESH_DEBUG_M2S(q->type));
5111 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5112 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
5113 pid = ntohl (ucast->pid);
5114 GNUNET_PEER_resolve (info->peer->id, &id);
5115 cinfo = tunnel_get_neighbor_fc(t, &id);
5116 ack = cinfo->fwd_ack;
5118 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5119 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
5120 pid = ntohl (to_orig->pid);
5123 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5124 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
5125 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
5127 // Not a multicast payload: multicast control traffic (destroy, etc)
5130 pid = ntohl (mcast->pid);
5131 GNUNET_PEER_resolve (info->peer->id, &id);
5132 cinfo = tunnel_get_neighbor_fc(t, &id);
5133 ack = cinfo->fwd_ack;
5136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5141 "********* ACK: %u, PID: %u\n",
5143 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
5145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5152 "********* NEXT!\n");
5155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5156 "********* nothing found\n");
5162 * Core callback to write a queued packet to core buffer
5164 * @param cls Closure (peer info).
5165 * @param size Number of bytes available in buf.
5166 * @param buf Where the to write the message.
5168 * @return number of bytes written to buf
5171 queue_send (void *cls, size_t size, void *buf)
5173 struct MeshPeerInfo *peer = cls;
5174 struct GNUNET_MessageHeader *msg;
5175 struct MeshPeerQueue *queue;
5176 struct MeshTunnel *t;
5177 struct MeshTunnelChildInfo *cinfo;
5178 struct GNUNET_PeerIdentity dst_id;
5181 peer->core_transmit = NULL;
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5185 queue = queue_get_next (peer);
5187 /* Queue has no internal mesh traffic nor sendable payload */
5190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5191 if (NULL == peer->queue_head)
5192 GNUNET_break (0); // Should've been canceled
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5197 GNUNET_PEER_resolve (peer->id, &dst_id);
5198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5199 "********* towards %s\n",
5200 GNUNET_i2s(&dst_id));
5201 /* Check if buffer size is enough for the message */
5202 if (queue->size > size)
5204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5205 "********* not enough room, reissue\n");
5206 peer->core_transmit =
5207 GNUNET_CORE_notify_transmit_ready (core_handle,
5210 GNUNET_TIME_UNIT_FOREVER_REL,
5217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5220 GNUNET_assert (0 < t->pending_messages);
5221 t->pending_messages--;
5222 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5226 "********* unicast: t->q (%u/%u)\n",
5227 t->fwd_queue_n, t->fwd_queue_max);
5229 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5236 switch (queue->type)
5239 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5240 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5241 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5242 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5243 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5245 "********* raw: %s\n",
5246 GNUNET_MESH_DEBUG_M2S (queue->type));
5248 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5249 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5250 data_size = send_core_data_raw (queue->cls, size, buf);
5251 msg = (struct GNUNET_MessageHeader *) buf;
5252 switch (ntohs (msg->type)) // Type of preconstructed message
5254 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5255 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5257 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5258 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5264 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5267 struct MeshTransmissionDescriptor *info = queue->cls;
5269 if ((1 == info->mesh_data->reference_counter
5270 && GNUNET_YES == t->speed_min)
5272 (info->mesh_data->total_out == info->mesh_data->reference_counter
5273 && GNUNET_NO == t->speed_min))
5275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5276 "********* considered sent\n");
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5282 "********* NOT considered sent yet\n");
5283 t->pending_messages++;
5286 data_size = send_core_data_multicast(queue->cls, size, buf);
5287 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5289 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5291 data_size = send_core_path_create (queue->cls, size, buf);
5293 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5295 data_size = send_core_path_ack (queue->cls, size, buf);
5297 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5299 data_size = send_core_data_multicast (queue->cls, size, buf);
5303 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5304 "********* type unknown: %u\n",
5308 switch (queue->type)
5310 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5311 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5312 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5313 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5314 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5318 "at pos %u (%p) != %p\n",
5319 cinfo->send_buffer_start,
5320 cinfo->send_buffer[cinfo->send_buffer_start],
5323 if (cinfo->send_buffer_n > 0)
5325 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5326 cinfo->send_buffer_n--;
5327 cinfo->send_buffer_start++;
5328 cinfo->send_buffer_start %= t->fwd_queue_max;
5339 /* Free queue, but cls was freed by send_core_* */
5340 queue_destroy (queue, GNUNET_NO);
5342 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5348 /* If more data in queue, send next */
5349 queue = queue_get_next(peer);
5352 struct GNUNET_PeerIdentity id;
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5355 GNUNET_PEER_resolve (peer->id, &id);
5356 peer->core_transmit =
5357 GNUNET_CORE_notify_transmit_ready(core_handle,
5360 GNUNET_TIME_UNIT_FOREVER_REL,
5368 if (NULL != peer->queue_head)
5370 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5371 "********* %s stalled\n",
5372 GNUNET_i2s(&my_full_id));
5374 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5375 // FIXME unify bck/fwd structures, bck does not have cinfo right now
5376 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5378 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
5379 &tunnel_poll, cinfo);
5383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5389 * @brief Queue and pass message to core when possible.
5391 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5392 * and accounts for it. In case the queue is full, the message is dropped and
5395 * Otherwise, message is treated as internal and allowed to go regardless of
5398 * @param cls Closure (@c type dependant). It will be used by queue_send to
5399 * build the message to be sent if not already prebuilt.
5400 * @param type Type of the message, 0 for a raw message.
5401 * @param size Size of the message.
5402 * @param dst Neighbor to send message to.
5403 * @param t Tunnel this message belongs to.
5406 queue_add (void *cls, uint16_t type, size_t size,
5407 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5409 struct MeshPeerQueue *queue;
5410 struct MeshTunnelChildInfo *cinfo;
5411 struct GNUNET_PeerIdentity id;
5417 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5418 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5420 n = &t->fwd_queue_n;
5421 max = &t->fwd_queue_max;
5423 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5425 n = &t->bck_queue_n;
5426 max = &t->bck_queue_max;
5433 GNUNET_STATISTICS_update(stats,
5434 "# messages dropped (buffer full)",
5436 return; // Drop message
5440 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5446 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5447 GNUNET_PEER_resolve (dst->id, &id);
5448 if (NULL == dst->core_transmit)
5450 dst->core_transmit =
5451 GNUNET_CORE_notify_transmit_ready (core_handle,
5454 GNUNET_TIME_UNIT_FOREVER_REL,
5460 t->pending_messages++;
5461 if (NULL == n) // Is this internal mesh traffic?
5464 // It's payload, keep track of buffer per peer.
5465 cinfo = tunnel_get_neighbor_fc(t, &id);
5466 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5467 if (NULL != cinfo->send_buffer[i])
5469 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5470 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5471 cinfo->send_buffer_start++;
5472 cinfo->send_buffer_start %= t->fwd_queue_max;
5476 cinfo->send_buffer_n++;
5478 cinfo->send_buffer[i] = queue;
5479 if (cinfo->send_buffer_n > t->fwd_queue_max)
5482 cinfo->send_buffer_n = t->fwd_queue_max;
5487 /******************************************************************************/
5488 /******************** MESH NETWORK HANDLERS **************************/
5489 /******************************************************************************/
5493 * Core handler for path creation
5495 * @param cls closure
5496 * @param message message
5497 * @param peer peer identity this notification is about
5498 * @param atsi performance data
5499 * @param atsi_count number of records in 'atsi'
5501 * @return GNUNET_OK to keep the connection open,
5502 * GNUNET_SYSERR to close it (signal serious error)
5505 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5506 const struct GNUNET_MessageHeader *message,
5507 const struct GNUNET_ATS_Information *atsi,
5508 unsigned int atsi_count)
5510 unsigned int own_pos;
5513 MESH_TunnelNumber tid;
5514 struct GNUNET_MESH_ManipulatePath *msg;
5515 struct GNUNET_PeerIdentity *pi;
5516 struct GNUNET_HashCode hash;
5517 struct MeshPeerPath *path;
5518 struct MeshPeerInfo *dest_peer_info;
5519 struct MeshPeerInfo *orig_peer_info;
5520 struct MeshTunnel *t;
5522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5523 "Received a path create msg [%s]\n",
5524 GNUNET_i2s (&my_full_id));
5525 size = ntohs (message->size);
5526 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5528 GNUNET_break_op (0);
5532 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5533 if (size % sizeof (struct GNUNET_PeerIdentity))
5535 GNUNET_break_op (0);
5538 size /= sizeof (struct GNUNET_PeerIdentity);
5541 GNUNET_break_op (0);
5544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5545 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5547 tid = ntohl (msg->tid);
5548 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5550 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5551 t = tunnel_get (pi, tid);
5552 if (NULL == t) // FIXME only for INCOMING tunnels?
5556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5557 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5560 // FIXME notify failure
5563 opt = ntohl (msg->opt);
5564 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5565 GNUNET_YES : GNUNET_NO;
5566 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5568 t->nobuffer = GNUNET_YES;
5569 t->last_fwd_ack = t->fwd_pid + 1;
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5572 " speed_min: %d, nobuffer:%d\n",
5573 t->speed_min, t->nobuffer);
5575 if (GNUNET_YES == t->nobuffer)
5577 t->bck_queue_max = 1;
5578 t->fwd_queue_max = 1;
5581 // FIXME only assign a local tid if a local client is interested (on demand)
5582 while (NULL != tunnel_get_incoming (next_local_tid))
5583 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5584 t->local_tid_dest = next_local_tid++;
5585 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5588 tunnel_reset_timeout (t);
5589 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5591 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5592 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5600 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5601 if (NULL == dest_peer_info)
5603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5604 " Creating PeerInfo for destination.\n");
5605 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5606 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5607 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5609 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5611 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5612 if (NULL == orig_peer_info)
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5615 " Creating PeerInfo for origin.\n");
5616 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5617 orig_peer_info->id = GNUNET_PEER_intern (pi);
5618 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5622 path = path_new (size);
5624 for (i = 0; i < size; i++)
5626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5627 GNUNET_i2s (&pi[i]));
5628 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5629 if (path->peers[i] == myid)
5632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5635 /* cannot be self, must be 'not found' */
5636 /* create path: self not found in path through self */
5637 GNUNET_break_op (0);
5638 path_destroy (path);
5642 path_add_to_peers (path, GNUNET_NO);
5643 tunnel_add_path (t, path, own_pos);
5644 if (own_pos == size - 1)
5646 /* It is for us! Send ack. */
5647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5648 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5649 if (NULL == t->peers)
5651 /* New tunnel! Notify clients on first payload message. */
5652 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5654 GNUNET_break (GNUNET_SYSERR !=
5655 GNUNET_CONTAINER_multihashmap_put (t->peers,
5656 &my_full_id.hashPubKey,
5659 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5664 struct MeshPeerPath *path2;
5666 /* It's for somebody else! Retransmit. */
5667 path2 = path_duplicate (path);
5668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5669 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5670 path2 = path_duplicate (path);
5671 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5672 send_create_path (dest_peer_info, path, t);
5679 * Core handler for path destruction
5681 * @param cls closure
5682 * @param message message
5683 * @param peer peer identity this notification is about
5684 * @param atsi performance data
5685 * @param atsi_count number of records in 'atsi'
5687 * @return GNUNET_OK to keep the connection open,
5688 * GNUNET_SYSERR to close it (signal serious error)
5691 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5692 const struct GNUNET_MessageHeader *message,
5693 const struct GNUNET_ATS_Information *atsi,
5694 unsigned int atsi_count)
5696 struct GNUNET_MESH_ManipulatePath *msg;
5697 struct GNUNET_PeerIdentity *pi;
5698 struct MeshPeerPath *path;
5699 struct MeshTunnel *t;
5700 unsigned int own_pos;
5704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5705 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5706 size = ntohs (message->size);
5707 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5709 GNUNET_break_op (0);
5713 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5714 if (size % sizeof (struct GNUNET_PeerIdentity))
5716 GNUNET_break_op (0);
5719 size /= sizeof (struct GNUNET_PeerIdentity);
5722 GNUNET_break_op (0);
5725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5727 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5728 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5730 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5732 t = tunnel_get (pi, ntohl (msg->tid));
5735 /* TODO notify back: we don't know this tunnel */
5736 GNUNET_break_op (0);
5739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5740 path = path_new (size);
5742 for (i = 0; i < size; i++)
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5745 GNUNET_i2s (&pi[i]));
5746 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5747 if (path->peers[i] == myid)
5750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5751 if (own_pos < path->length - 1)
5752 send_prebuilt_message (message, &pi[own_pos + 1], t);
5754 send_client_tunnel_disconnect(t, NULL);
5756 tunnel_delete_peer (t, path->peers[path->length - 1]);
5757 path_destroy (path);
5763 * Core handler for notifications of broken paths
5765 * @param cls closure
5766 * @param message message
5767 * @param peer peer identity this notification is about
5768 * @param atsi performance data
5769 * @param atsi_count number of records in 'atsi'
5771 * @return GNUNET_OK to keep the connection open,
5772 * GNUNET_SYSERR to close it (signal serious error)
5775 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5776 const struct GNUNET_MessageHeader *message,
5777 const struct GNUNET_ATS_Information *atsi,
5778 unsigned int atsi_count)
5780 struct GNUNET_MESH_PathBroken *msg;
5781 struct MeshTunnel *t;
5783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5784 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5785 msg = (struct GNUNET_MESH_PathBroken *) message;
5786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5787 GNUNET_i2s (&msg->peer1));
5788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5789 GNUNET_i2s (&msg->peer2));
5790 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5793 GNUNET_break_op (0);
5796 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5797 GNUNET_PEER_search (&msg->peer2));
5804 * Core handler for tunnel destruction
5806 * @param cls closure
5807 * @param message message
5808 * @param peer peer identity this notification is about
5809 * @param atsi performance data
5810 * @param atsi_count number of records in 'atsi'
5812 * @return GNUNET_OK to keep the connection open,
5813 * GNUNET_SYSERR to close it (signal serious error)
5816 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5817 const struct GNUNET_MessageHeader *message,
5818 const struct GNUNET_ATS_Information *atsi,
5819 unsigned int atsi_count)
5821 struct GNUNET_MESH_TunnelDestroy *msg;
5822 struct MeshTunnel *t;
5823 GNUNET_PEER_Id parent;
5826 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5828 "Got a TUNNEL DESTROY packet from %s\n",
5830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5831 " for tunnel %s [%u]\n",
5832 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5833 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5834 /* Check signature */
5837 /* Probably already got the message from another path,
5838 * destroyed the tunnel and retransmitted to children.
5841 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5845 parent = tree_get_predecessor(t->tree);
5846 pid = GNUNET_PEER_search (peer);
5849 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
5850 if (tree_count_children(t->tree) > 0 ||
5855 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5857 /* Tunnel was incoming, notify clients */
5858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5859 t->local_tid, t->local_tid_dest);
5860 send_clients_tunnel_destroy (t);
5862 tunnel_send_destroy (t, GNUNET_YES);
5863 t->destroy = GNUNET_YES;
5864 // TODO: add timeout to destroy the tunnel anyway
5870 * Core handler for mesh network traffic going from the origin to a peer
5872 * @param cls closure
5873 * @param peer peer identity this notification is about
5874 * @param message message
5875 * @param atsi performance data
5876 * @param atsi_count number of records in 'atsi'
5877 * @return GNUNET_OK to keep the connection open,
5878 * GNUNET_SYSERR to close it (signal serious error)
5881 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5882 const struct GNUNET_MessageHeader *message,
5883 const struct GNUNET_ATS_Information *atsi,
5884 unsigned int atsi_count)
5886 struct GNUNET_MESH_Unicast *msg;
5887 struct GNUNET_PeerIdentity *neighbor;
5888 struct MeshTunnelChildInfo *cinfo;
5889 struct MeshTunnel *t;
5890 GNUNET_PEER_Id dest_id;
5895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5898 size = ntohs (message->size);
5900 sizeof (struct GNUNET_MESH_Unicast) +
5901 sizeof (struct GNUNET_MessageHeader))
5906 msg = (struct GNUNET_MESH_Unicast *) message;
5907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5908 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5910 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5913 /* TODO notify back: we don't know this tunnel */
5914 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5915 GNUNET_break_op (0);
5918 pid = ntohl (msg->pid);
5919 if (t->fwd_pid == pid)
5921 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5923 " Already seen pid %u, DROPPING!\n", pid);
5928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5929 " pid %u not seen yet, forwarding\n", pid);
5932 t->skip += (pid - t->fwd_pid) - 1;
5935 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5937 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5938 GNUNET_break_op (0);
5939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5940 "Received PID %u, ACK %u\n",
5941 pid, t->last_fwd_ack);
5945 tunnel_reset_timeout (t);
5946 dest_id = GNUNET_PEER_search (&msg->destination);
5947 if (dest_id == myid)
5949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5950 " it's for us! sending to clients...\n");
5951 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5952 send_subscribed_clients (message, &msg[1].header, t);
5953 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5956 ttl = ntohl (msg->ttl);
5957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5960 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5961 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5962 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5966 " not for us, retransmitting...\n");
5968 neighbor = tree_get_first_hop (t->tree, dest_id);
5969 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5970 cinfo->fwd_pid = pid;
5971 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5974 if (GNUNET_YES == t->nobuffer &&
5975 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5977 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5978 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5979 GNUNET_break_op (0);
5982 send_prebuilt_message (message, neighbor, t);
5983 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5989 * Core handler for mesh network traffic going from the origin to all peers
5991 * @param cls closure
5992 * @param message message
5993 * @param peer peer identity this notification is about
5994 * @param atsi performance data
5995 * @param atsi_count number of records in 'atsi'
5996 * @return GNUNET_OK to keep the connection open,
5997 * GNUNET_SYSERR to close it (signal serious error)
5999 * TODO: Check who we got this from, to validate route.
6002 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
6003 const struct GNUNET_MessageHeader *message,
6004 const struct GNUNET_ATS_Information *atsi,
6005 unsigned int atsi_count)
6007 struct GNUNET_MESH_Multicast *msg;
6008 struct MeshTunnel *t;
6012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
6014 size = ntohs (message->size);
6015 if (sizeof (struct GNUNET_MESH_Multicast) +
6016 sizeof (struct GNUNET_MessageHeader) > size)
6018 GNUNET_break_op (0);
6021 msg = (struct GNUNET_MESH_Multicast *) message;
6022 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6026 /* TODO notify that we dont know that tunnel */
6027 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
6028 GNUNET_break_op (0);
6031 pid = ntohl (msg->pid);
6032 if (t->fwd_pid == pid)
6034 /* already seen this packet, drop */
6035 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
6036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6037 " Already seen pid %u, DROPPING!\n", pid);
6038 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6044 " pid %u not seen yet, forwarding\n", pid);
6046 t->skip += (pid - t->fwd_pid) - 1;
6048 tunnel_reset_timeout (t);
6050 /* Transmit to locally interested clients */
6051 if (NULL != t->peers &&
6052 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
6054 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
6055 send_subscribed_clients (message, &msg[1].header, t);
6056 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
6058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
6059 if (ntohl (msg->ttl) == 0)
6061 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
6062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
6063 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6066 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
6067 tunnel_send_multicast (t, message);
6073 * Core handler for mesh network traffic toward the owner of a tunnel
6075 * @param cls closure
6076 * @param message message
6077 * @param peer peer identity this notification is about
6078 * @param atsi performance data
6079 * @param atsi_count number of records in 'atsi'
6081 * @return GNUNET_OK to keep the connection open,
6082 * GNUNET_SYSERR to close it (signal serious error)
6085 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
6086 const struct GNUNET_MessageHeader *message,
6087 const struct GNUNET_ATS_Information *atsi,
6088 unsigned int atsi_count)
6090 struct GNUNET_MESH_ToOrigin *msg;
6091 struct GNUNET_PeerIdentity id;
6092 struct MeshPeerInfo *peer_info;
6093 struct MeshTunnel *t;
6094 struct MeshTunnelChildInfo *cinfo;
6095 GNUNET_PEER_Id predecessor;
6099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
6101 size = ntohs (message->size);
6102 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
6103 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
6105 GNUNET_break_op (0);
6108 msg = (struct GNUNET_MESH_ToOrigin *) message;
6109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
6110 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
6111 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6112 pid = ntohl (msg->pid);
6116 /* TODO notify that we dont know this tunnel (whom)? */
6117 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
6118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6119 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
6120 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
6124 cinfo = tunnel_get_neighbor_fc(t, peer);
6131 if (cinfo->bck_pid == pid)
6133 /* already seen this packet, drop */
6134 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
6135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6136 " Already seen pid %u, DROPPING!\n", pid);
6137 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6142 " pid %u not seen yet, forwarding\n", pid);
6143 cinfo->bck_pid = pid;
6145 if (NULL != t->owner)
6148 struct GNUNET_MESH_ToOrigin *copy;
6150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6151 " it's for us! sending to clients...\n");
6152 /* TODO signature verification */
6153 memcpy (cbuf, message, size);
6154 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
6155 copy->tid = htonl (t->local_tid);
6157 copy->pid = htonl (t->bck_pid);
6158 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
6159 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
6160 ©->header, GNUNET_NO);
6161 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6165 " not for us, retransmitting...\n");
6167 peer_info = peer_info_get (&msg->oid);
6168 if (NULL == peer_info)
6170 /* unknown origin of tunnel */
6174 predecessor = tree_get_predecessor (t->tree);
6175 if (0 == predecessor)
6177 if (GNUNET_YES == t->destroy)
6179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6180 "to orig received on a dying tunnel %s [%X]\n",
6181 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6185 "unknown to origin at %s\n",
6186 GNUNET_i2s (&my_full_id));
6187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6191 "for tunnel %s [%X]\n",
6192 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6195 tree_debug (t->tree);
6198 GNUNET_PEER_resolve (predecessor, &id);
6199 send_prebuilt_message (message, &id, t);
6200 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6207 * Core handler for mesh network traffic point-to-point acks.
6209 * @param cls closure
6210 * @param message message
6211 * @param peer peer identity this notification is about
6212 * @param atsi performance data
6213 * @param atsi_count number of records in 'atsi'
6215 * @return GNUNET_OK to keep the connection open,
6216 * GNUNET_SYSERR to close it (signal serious error)
6219 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6220 const struct GNUNET_MessageHeader *message,
6221 const struct GNUNET_ATS_Information *atsi,
6222 unsigned int atsi_count)
6224 struct GNUNET_MESH_ACK *msg;
6225 struct MeshTunnel *t;
6228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6230 msg = (struct GNUNET_MESH_ACK *) message;
6232 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6236 /* TODO notify that we dont know this tunnel (whom)? */
6237 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6240 ack = ntohl (msg->pid);
6241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6243 /* Is this a forward or backward ACK? */
6244 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6246 struct MeshTunnelChildInfo *cinfo;
6249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6250 cinfo = tunnel_get_neighbor_fc (t, peer);
6251 cinfo->fwd_ack = ack;
6252 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6253 tunnel_unlock_fwd_queues (t);
6254 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6256 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6257 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6258 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6265 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6266 tunnel_unlock_bck_queue (t);
6273 * Core handler for mesh network traffic point-to-point ack polls.
6275 * @param cls closure
6276 * @param message message
6277 * @param peer peer identity this notification is about
6278 * @param atsi performance data
6279 * @param atsi_count number of records in 'atsi'
6281 * @return GNUNET_OK to keep the connection open,
6282 * GNUNET_SYSERR to close it (signal serious error)
6285 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6286 const struct GNUNET_MessageHeader *message,
6287 const struct GNUNET_ATS_Information *atsi,
6288 unsigned int atsi_count)
6290 struct GNUNET_MESH_Poll *msg;
6291 struct MeshTunnel *t;
6293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6296 msg = (struct GNUNET_MESH_Poll *) message;
6298 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6302 /* TODO notify that we dont know this tunnel (whom)? */
6303 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6304 GNUNET_break_op (0);
6308 /* Is this a forward or backward ACK? */
6309 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6311 struct MeshTunnelChildInfo *cinfo;
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6314 cinfo = tunnel_get_neighbor_fc (t, peer);
6315 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6316 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6321 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6329 * Core handler for path ACKs
6331 * @param cls closure
6332 * @param message message
6333 * @param peer peer identity this notification is about
6334 * @param atsi performance data
6335 * @param atsi_count number of records in 'atsi'
6337 * @return GNUNET_OK to keep the connection open,
6338 * GNUNET_SYSERR to close it (signal serious error)
6341 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6342 const struct GNUNET_MessageHeader *message,
6343 const struct GNUNET_ATS_Information *atsi,
6344 unsigned int atsi_count)
6346 struct GNUNET_MESH_PathACK *msg;
6347 struct GNUNET_PeerIdentity id;
6348 struct MeshPeerInfo *peer_info;
6349 struct MeshPeerPath *p;
6350 struct MeshTunnel *t;
6352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6353 GNUNET_i2s (&my_full_id));
6354 msg = (struct GNUNET_MESH_PathACK *) message;
6355 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6358 /* TODO notify that we don't know the tunnel */
6359 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6361 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6365 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6367 peer_info = peer_info_get (&msg->peer_id);
6368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6369 GNUNET_i2s (&msg->peer_id));
6370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6373 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6376 "connect_by_string completed, stopping search\n");
6377 regex_cancel_search (t->regex_ctx);
6378 t->regex_ctx = NULL;
6381 /* Add paths to peers? */
6382 p = tree_get_path_to_peer (t->tree, peer_info->id);
6385 path_add_to_peers (p, GNUNET_YES);
6393 /* Message for us? */
6394 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6397 if (NULL == t->owner)
6399 GNUNET_break_op (0);
6402 if (NULL != t->dht_get_type)
6404 GNUNET_DHT_get_stop (t->dht_get_type);
6405 t->dht_get_type = NULL;
6407 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6409 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6410 send_client_peer_connected (t, peer_info->id);
6415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6416 " not for us, retransmitting...\n");
6417 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6418 peer_info = peer_info_get (&msg->oid);
6419 if (NULL == peer_info)
6421 /* If we know the tunnel, we should DEFINITELY know the peer */
6425 send_prebuilt_message (message, &id, t);
6431 * Core handler for mesh keepalives.
6433 * @param cls closure
6434 * @param message message
6435 * @param peer peer identity this notification is about
6436 * @param atsi performance data
6437 * @param atsi_count number of records in 'atsi'
6438 * @return GNUNET_OK to keep the connection open,
6439 * GNUNET_SYSERR to close it (signal serious error)
6441 * TODO: Check who we got this from, to validate route.
6444 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6445 const struct GNUNET_MessageHeader *message,
6446 const struct GNUNET_ATS_Information *atsi,
6447 unsigned int atsi_count)
6449 struct GNUNET_MESH_TunnelKeepAlive *msg;
6450 struct MeshTunnel *t;
6452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6455 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6456 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6460 /* TODO notify that we dont know that tunnel */
6461 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
6466 tunnel_reset_timeout (t);
6468 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6469 tunnel_send_multicast (t, message);
6476 * Functions to handle messages from core
6478 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6479 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6480 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6481 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6482 sizeof (struct GNUNET_MESH_PathBroken)},
6483 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6484 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6485 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6486 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6487 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6488 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6489 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6490 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6491 sizeof (struct GNUNET_MESH_ACK)},
6492 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6493 sizeof (struct GNUNET_MESH_Poll)},
6494 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6495 sizeof (struct GNUNET_MESH_PathACK)},
6501 /******************************************************************************/
6502 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6503 /******************************************************************************/
6506 * deregister_app: iterator for removing each application registered by a client
6508 * @param cls closure
6509 * @param key the hash of the application id (used to access the hashmap)
6510 * @param value the value stored at the key (client)
6512 * @return GNUNET_OK on success
6515 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6517 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6518 GNUNET_break (GNUNET_YES ==
6519 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6525 * notify_client_connection_failure: notify a client that the connection to the
6526 * requested remote peer is not possible (for instance, no route found)
6527 * Function called when the socket is ready to queue more data. "buf" will be
6528 * NULL and "size" zero if the socket was closed for writing in the meantime.
6530 * @param cls closure
6531 * @param size number of bytes available in buf
6532 * @param buf where the callee should write the message
6533 * @return number of bytes written to buf
6536 notify_client_connection_failure (void *cls, size_t size, void *buf)
6539 struct MeshPeerInfo *peer_info;
6540 struct GNUNET_MESH_PeerControl *msg;
6541 struct GNUNET_PeerIdentity id;
6543 if (0 == size && NULL == buf)
6545 // TODO retry? cancel?
6549 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6550 peer_info = (struct MeshPeerInfo *) cls;
6551 msg = (struct GNUNET_MESH_PeerControl *) buf;
6552 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6553 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6554 // msg->tunnel_id = htonl(peer_info->t->tid);
6555 GNUNET_PEER_resolve (peer_info->id, &id);
6556 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6564 * Send keepalive packets for a peer
6566 * @param cls Closure (tunnel for which to send the keepalive).
6567 * @param tc Notification context.
6570 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6572 struct MeshTunnel *t = cls;
6573 struct GNUNET_MESH_TunnelKeepAlive *msg;
6574 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6577 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6578 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6584 "sending keepalive for tunnel %d\n", t->id.tid);
6586 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6587 msg->header.size = htons (size);
6588 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6589 msg->oid = my_full_id;
6590 msg->tid = htonl (t->id.tid);
6591 tunnel_send_multicast (t, &msg->header);
6593 t->path_refresh_task =
6594 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6595 tunnel_reset_timeout(t);
6600 * Function to process paths received for a new peer addition. The recorded
6601 * paths form the initial tunnel, which can be optimized later.
6602 * Called on each result obtained for the DHT search.
6604 * @param cls closure
6605 * @param exp when will this value expire
6606 * @param key key of the result
6607 * @param get_path path of the get request
6608 * @param get_path_length lenght of get_path
6609 * @param put_path path of the put request
6610 * @param put_path_length length of the put_path
6611 * @param type type of the result
6612 * @param size number of bytes in data
6613 * @param data pointer to the result data
6615 * TODO: re-issue the request after certain time? cancel after X results?
6618 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6619 const struct GNUNET_HashCode * key,
6620 const struct GNUNET_PeerIdentity *get_path,
6621 unsigned int get_path_length,
6622 const struct GNUNET_PeerIdentity *put_path,
6623 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6624 size_t size, const void *data)
6626 struct MeshPathInfo *path_info = cls;
6627 struct MeshPeerPath *p;
6628 struct GNUNET_PeerIdentity pi;
6631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6632 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6635 p = path_build_from_dht (get_path, get_path_length, put_path,
6637 path_add_to_peers (p, GNUNET_NO);
6639 for (i = 0; i < path_info->peer->ntunnels; i++)
6641 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6642 peer_info_connect (path_info->peer, path_info->t);
6650 * Function to process paths received for a new peer addition. The recorded
6651 * paths form the initial tunnel, which can be optimized later.
6652 * Called on each result obtained for the DHT search.
6654 * @param cls closure
6655 * @param exp when will this value expire
6656 * @param key key of the result
6657 * @param get_path path of the get request
6658 * @param get_path_length lenght of get_path
6659 * @param put_path path of the put request
6660 * @param put_path_length length of the put_path
6661 * @param type type of the result
6662 * @param size number of bytes in data
6663 * @param data pointer to the result data
6666 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6667 const struct GNUNET_HashCode * key,
6668 const struct GNUNET_PeerIdentity *get_path,
6669 unsigned int get_path_length,
6670 const struct GNUNET_PeerIdentity *put_path,
6671 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6672 size_t size, const void *data)
6674 const struct PBlock *pb = data;
6675 const struct GNUNET_PeerIdentity *pi = &pb->id;
6676 struct MeshTunnel *t = cls;
6677 struct MeshPeerInfo *peer_info;
6678 struct MeshPeerPath *p;
6680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6681 if (size != sizeof (struct PBlock))
6683 GNUNET_break_op (0);
6686 if (ntohl(pb->type) != t->type)
6688 GNUNET_break_op (0);
6691 GNUNET_assert (NULL != t->owner);
6692 peer_info = peer_info_get (pi);
6693 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6695 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6697 p = path_build_from_dht (get_path, get_path_length, put_path,
6699 path_add_to_peers (p, GNUNET_NO);
6701 tunnel_add_peer (t, peer_info);
6702 peer_info_connect (peer_info, t);
6707 * Function to process DHT string to regex matching.
6708 * Called on each result obtained for the DHT search.
6710 * @param cls closure (search context)
6711 * @param exp when will this value expire
6712 * @param key key of the result
6713 * @param get_path path of the get request (not used)
6714 * @param get_path_length lenght of get_path (not used)
6715 * @param put_path path of the put request (not used)
6716 * @param put_path_length length of the put_path (not used)
6717 * @param type type of the result
6718 * @param size number of bytes in data
6719 * @param data pointer to the result data
6722 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6723 const struct GNUNET_HashCode * key,
6724 const struct GNUNET_PeerIdentity *get_path,
6725 unsigned int get_path_length,
6726 const struct GNUNET_PeerIdentity *put_path,
6727 unsigned int put_path_length,
6728 enum GNUNET_BLOCK_Type type,
6729 size_t size, const void *data)
6731 const struct MeshRegexAccept *block = data;
6732 struct MeshRegexSearchContext *ctx = cls;
6733 struct MeshRegexSearchInfo *info = ctx->info;
6734 // struct MeshPeerPath *p;
6735 struct MeshPeerInfo *peer_info;
6737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6739 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6741 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6744 peer_info = peer_info_get(&block->id);
6745 // p = path_build_from_dht (get_path, get_path_length, put_path,
6746 // put_path_length);
6747 // path_add_to_peers (p, GNUNET_NO);
6750 tunnel_add_peer (info->t, peer_info);
6751 peer_info_connect (peer_info, info->t);
6752 if (0 == info->peer)
6754 info->peer = peer_info->id;
6758 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6761 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
6764 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6765 ®ex_connect_timeout,
6773 * Function to process DHT string to regex matching.
6774 * Called on each result obtained for the DHT search.
6776 * @param cls closure (search context)
6777 * @param exp when will this value expire
6778 * @param key key of the result
6779 * @param get_path path of the get request (not used)
6780 * @param get_path_length lenght of get_path (not used)
6781 * @param put_path path of the put request (not used)
6782 * @param put_path_length length of the put_path (not used)
6783 * @param type type of the result
6784 * @param size number of bytes in data
6785 * @param data pointer to the result data
6787 * TODO: re-issue the request after certain time? cancel after X results?
6790 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6791 const struct GNUNET_HashCode * key,
6792 const struct GNUNET_PeerIdentity *get_path,
6793 unsigned int get_path_length,
6794 const struct GNUNET_PeerIdentity *put_path,
6795 unsigned int put_path_length,
6796 enum GNUNET_BLOCK_Type type,
6797 size_t size, const void *data)
6799 const struct MeshRegexBlock *block = data;
6800 struct MeshRegexSearchContext *ctx = cls;
6801 struct MeshRegexSearchInfo *info = ctx->info;
6805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6806 "DHT GET STRING RETURNED RESULTS\n");
6807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6808 " key: %s\n", GNUNET_h2s (key));
6810 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6812 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6815 copy = GNUNET_malloc (size);
6816 memcpy (copy, data, size);
6817 GNUNET_break (GNUNET_OK ==
6818 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6820 len = ntohl (block->n_proof);
6822 char proof[len + 1];
6824 memcpy (proof, &block[1], len);
6826 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6828 GNUNET_break_op (0);
6832 len = strlen (info->description);
6833 if (len == ctx->position) // String processed
6835 if (GNUNET_YES == ntohl (block->accepting))
6837 regex_find_path(key, ctx);
6841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6842 // FIXME REGEX this block not successful, wait for more? start timeout?
6847 regex_next_edge (block, size, ctx);
6852 /******************************************************************************/
6853 /********************* MESH LOCAL HANDLES **************************/
6854 /******************************************************************************/
6858 * Handler for client disconnection
6860 * @param cls closure
6861 * @param client identification of the client; NULL
6862 * for the last call when the server is destroyed
6865 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6867 struct MeshClient *c;
6868 struct MeshClient *next;
6871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6881 if (c->handle != client)
6883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6889 GNUNET_SERVER_client_drop (c->handle);
6890 c->shutting_down = GNUNET_YES;
6891 GNUNET_assert (NULL != c->own_tunnels);
6892 GNUNET_assert (NULL != c->incoming_tunnels);
6893 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6894 &tunnel_destroy_iterator, c);
6895 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6896 &tunnel_destroy_iterator, c);
6897 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6898 &tunnel_destroy_iterator, c);
6899 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6900 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6901 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6903 /* deregister clients applications */
6904 if (NULL != c->apps)
6906 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6907 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6909 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6910 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6912 GNUNET_SCHEDULER_cancel (announce_applications_task);
6913 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6915 if (NULL != c->types)
6916 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6917 for (i = 0; i < c->n_regex; i++)
6919 GNUNET_free (c->regexes[i].regex);
6920 if (NULL != c->regexes[i].dfa)
6921 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6923 GNUNET_free_non_null (c->regexes);
6924 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6925 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6927 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6930 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6939 * Handler for new clients
6941 * @param cls closure
6942 * @param client identification of the client
6943 * @param message the actual message, which includes messages the client wants
6946 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6947 const struct GNUNET_MessageHeader *message)
6949 struct GNUNET_MESH_ClientConnect *cc_msg;
6950 struct MeshClient *c;
6951 GNUNET_MESH_ApplicationType *a;
6958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6959 /* Check data sanity */
6960 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6961 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6962 ntypes = ntohs (cc_msg->types);
6963 napps = ntohs (cc_msg->applications);
6965 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6968 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6972 /* Create new client structure */
6973 c = GNUNET_malloc (sizeof (struct MeshClient));
6974 c->id = next_client_id++;
6975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6977 GNUNET_SERVER_client_keep (client);
6978 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6981 GNUNET_MESH_ApplicationType at;
6982 struct GNUNET_HashCode hc;
6984 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6985 for (i = 0; i < napps; i++)
6988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6989 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6990 /* store in clients hashmap */
6991 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6993 /* store in global hashmap, for announcements */
6994 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6995 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6997 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6998 announce_applications_task =
6999 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
7005 struct GNUNET_HashCode hc;
7007 t = (uint16_t *) & a[napps];
7008 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
7009 for (i = 0; i < ntypes; i++)
7012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
7013 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
7015 /* store in clients hashmap */
7016 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
7017 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
7018 /* store in global hashmap */
7019 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
7020 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
7023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7024 " client has %u+%u subscriptions\n", napps, ntypes);
7026 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
7027 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7028 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7029 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7030 GNUNET_SERVER_notification_context_add (nc, client);
7031 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
7033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
7039 * Handler for clients announcing available services by a regular expression.
7041 * @param cls closure
7042 * @param client identification of the client
7043 * @param message the actual message, which includes messages the client wants
7046 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
7047 const struct GNUNET_MessageHeader *message)
7049 const struct GNUNET_MESH_RegexAnnounce *msg;
7050 struct MeshRegexDescriptor rd;
7051 struct MeshClient *c;
7056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
7058 /* Sanity check for client registration */
7059 if (NULL == (c = client_get (client)))
7062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7067 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
7069 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
7070 if (NULL != c->partial_regex)
7072 regex = c->partial_regex;
7073 offset = strlen (c->partial_regex);
7074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7075 " continuation, already have %u bytes\n",
7084 regex = GNUNET_realloc (regex, offset + len + 1);
7085 memcpy (®ex[offset], &msg[1], len);
7086 regex[offset + len] = '\0';
7087 if (0 == ntohs (msg->last))
7089 c->partial_regex = regex;
7090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7091 " not ended, stored %u bytes for later\n",
7093 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7097 rd.compression = ntohs (msg->compression_characters);
7099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
7100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
7101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
7102 GNUNET_array_append (c->regexes, c->n_regex, rd);
7103 c->partial_regex = NULL;
7104 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
7106 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
7112 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
7118 * Handler for requests of new tunnels
7120 * @param cls closure
7121 * @param client identification of the client
7122 * @param message the actual message
7125 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
7126 const struct GNUNET_MessageHeader *message)
7128 struct GNUNET_MESH_TunnelMessage *t_msg;
7129 struct MeshTunnel *t;
7130 struct MeshClient *c;
7131 MESH_TunnelNumber tid;
7133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
7135 /* Sanity check for client registration */
7136 if (NULL == (c = client_get (client)))
7139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7144 /* Message sanity check */
7145 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7152 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7153 /* Sanity check for tunnel numbering */
7154 tid = ntohl (t_msg->tunnel_id);
7155 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
7158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7161 /* Sanity check for duplicate tunnel IDs */
7162 if (NULL != tunnel_get_by_local_id (c, tid))
7165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7169 while (NULL != tunnel_get_by_pi (myid, next_tid))
7170 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7171 t = tunnel_new (myid, next_tid++, c, tid);
7174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
7176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7179 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
7181 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
7182 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
7185 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7191 * Handler for requests of deleting tunnels
7193 * @param cls closure
7194 * @param client identification of the client
7195 * @param message the actual message
7198 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
7199 const struct GNUNET_MessageHeader *message)
7201 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7202 struct MeshClient *c;
7203 struct MeshTunnel *t;
7204 MESH_TunnelNumber tid;
7206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7207 "Got a DESTROY TUNNEL from client!\n");
7209 /* Sanity check for client registration */
7210 if (NULL == (c = client_get (client)))
7213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7218 /* Message sanity check */
7219 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7222 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7226 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7228 /* Retrieve tunnel */
7229 tid = ntohl (tunnel_msg->tunnel_id);
7230 t = tunnel_get_by_local_id(c, tid);
7233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7238 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7240 client_ignore_tunnel (c, t);
7241 tunnel_destroy_empty (t);
7242 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7245 send_client_tunnel_disconnect(t, c);
7246 client_delete_tunnel(c, t);
7248 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7250 tunnel_send_destroy (t, GNUNET_YES);
7251 t->destroy = GNUNET_YES;
7252 // The tunnel will be destroyed when the last message is transmitted.
7253 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7259 * Handler for requests of seeting tunnel's speed.
7261 * @param cls Closure (unused).
7262 * @param client Identification of the client.
7263 * @param message The actual message.
7266 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7267 const struct GNUNET_MessageHeader *message)
7269 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7270 struct MeshClient *c;
7271 struct MeshTunnel *t;
7272 MESH_TunnelNumber tid;
7274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7275 "Got a SPEED request from client!\n");
7277 /* Sanity check for client registration */
7278 if (NULL == (c = client_get (client)))
7281 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7287 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7289 /* Retrieve tunnel */
7290 tid = ntohl (tunnel_msg->tunnel_id);
7291 t = tunnel_get_by_local_id(c, tid);
7294 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7300 switch (ntohs(message->type))
7302 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7303 t->speed_min = GNUNET_YES;
7305 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7306 t->speed_min = GNUNET_NO;
7311 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7316 * Handler for requests of seeting tunnel's buffering policy.
7318 * @param cls Closure (unused).
7319 * @param client Identification of the client.
7320 * @param message The actual message.
7323 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7324 const struct GNUNET_MessageHeader *message)
7326 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7327 struct MeshClient *c;
7328 struct MeshTunnel *t;
7329 MESH_TunnelNumber tid;
7331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7332 "Got a BUFFER request from client!\n");
7334 /* Sanity check for client registration */
7335 if (NULL == (c = client_get (client)))
7338 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7343 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7345 /* Retrieve tunnel */
7346 tid = ntohl (tunnel_msg->tunnel_id);
7347 t = tunnel_get_by_local_id(c, tid);
7350 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7356 switch (ntohs(message->type))
7358 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7359 t->nobuffer = GNUNET_NO;
7361 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7362 t->nobuffer = GNUNET_YES;
7368 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7373 * Handler for connection requests to new peers
7375 * @param cls closure
7376 * @param client identification of the client
7377 * @param message the actual message (PeerControl)
7380 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7381 const struct GNUNET_MessageHeader *message)
7383 struct GNUNET_MESH_PeerControl *peer_msg;
7384 struct MeshPeerInfo *peer_info;
7385 struct MeshClient *c;
7386 struct MeshTunnel *t;
7387 MESH_TunnelNumber tid;
7389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7390 /* Sanity check for client registration */
7391 if (NULL == (c = client_get (client)))
7394 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7399 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7401 /* Sanity check for message size */
7402 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7409 /* Tunnel exists? */
7410 tid = ntohl (peer_msg->tunnel_id);
7411 t = tunnel_get_by_local_id (c, tid);
7415 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7419 /* Does client own tunnel? */
7420 if (t->owner->handle != client)
7423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7427 GNUNET_i2s (&peer_msg->peer));
7428 peer_info = peer_info_get (&peer_msg->peer);
7430 tunnel_add_peer (t, peer_info);
7431 peer_info_connect (peer_info, t);
7433 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7439 * Handler for disconnection requests of peers in a tunnel
7441 * @param cls closure
7442 * @param client identification of the client
7443 * @param message the actual message (PeerControl)
7446 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7447 const struct GNUNET_MessageHeader *message)
7449 struct GNUNET_MESH_PeerControl *peer_msg;
7450 struct MeshPeerInfo *peer_info;
7451 struct MeshClient *c;
7452 struct MeshTunnel *t;
7453 MESH_TunnelNumber tid;
7455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7456 /* Sanity check for client registration */
7457 if (NULL == (c = client_get (client)))
7460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7465 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7467 /* Sanity check for message size */
7468 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7475 /* Tunnel exists? */
7476 tid = ntohl (peer_msg->tunnel_id);
7477 t = tunnel_get_by_local_id (c, tid);
7481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7486 /* Does client own tunnel? */
7487 if (t->owner->handle != client)
7490 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7495 GNUNET_i2s (&peer_msg->peer));
7496 /* Is the peer in the tunnel? */
7498 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7499 if (NULL == peer_info)
7502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7506 /* Ok, delete peer from tunnel */
7507 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7508 &peer_msg->peer.hashPubKey);
7510 send_destroy_path (t, peer_info->id);
7511 tunnel_delete_peer (t, peer_info->id);
7512 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7517 * Handler for blacklist requests of peers in a tunnel
7519 * @param cls closure
7520 * @param client identification of the client
7521 * @param message the actual message (PeerControl)
7523 * FIXME implement DHT block bloomfilter
7526 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7527 const struct GNUNET_MessageHeader *message)
7529 struct GNUNET_MESH_PeerControl *peer_msg;
7530 struct MeshClient *c;
7531 struct MeshTunnel *t;
7532 MESH_TunnelNumber tid;
7534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7535 /* Sanity check for client registration */
7536 if (NULL == (c = client_get (client)))
7539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7544 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7546 /* Sanity check for message size */
7547 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7554 /* Tunnel exists? */
7555 tid = ntohl (peer_msg->tunnel_id);
7556 t = tunnel_get_by_local_id (c, tid);
7560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7565 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7566 GNUNET_PEER_intern(&peer_msg->peer));
7571 * Handler for unblacklist requests of peers in a tunnel
7573 * @param cls closure
7574 * @param client identification of the client
7575 * @param message the actual message (PeerControl)
7578 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7579 const struct GNUNET_MessageHeader *message)
7581 struct GNUNET_MESH_PeerControl *peer_msg;
7582 struct MeshClient *c;
7583 struct MeshTunnel *t;
7584 MESH_TunnelNumber tid;
7588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7589 /* Sanity check for client registration */
7590 if (NULL == (c = client_get (client)))
7593 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7598 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7600 /* Sanity check for message size */
7601 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7608 /* Tunnel exists? */
7609 tid = ntohl (peer_msg->tunnel_id);
7610 t = tunnel_get_by_local_id (c, tid);
7614 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7619 /* if peer is not known, complain */
7620 pid = GNUNET_PEER_search (&peer_msg->peer);
7627 /* search and remove from list */
7628 for (i = 0; i < t->nblacklisted; i++)
7630 if (t->blacklisted[i] == pid)
7632 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7633 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7638 /* if peer hasn't been blacklisted, complain */
7644 * Handler for connection requests to new peers by type
7646 * @param cls closure
7647 * @param client identification of the client
7648 * @param message the actual message (ConnectPeerByType)
7651 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7652 const struct GNUNET_MessageHeader *message)
7654 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7655 struct MeshClient *c;
7656 struct MeshTunnel *t;
7657 struct GNUNET_HashCode hash;
7658 MESH_TunnelNumber tid;
7660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7661 /* Sanity check for client registration */
7662 if (NULL == (c = client_get (client)))
7665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7670 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7672 /* Sanity check for message size */
7673 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7674 ntohs (connect_msg->header.size))
7677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7681 /* Tunnel exists? */
7682 tid = ntohl (connect_msg->tunnel_id);
7683 t = tunnel_get_by_local_id (c, tid);
7687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7691 /* Does client own tunnel? */
7692 if (t->owner->handle != client)
7695 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7699 /* Do WE have the service? */
7700 t->type = ntohl (connect_msg->type);
7701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7702 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7703 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7706 /* Yes! Fast forward, add ourselves to the tunnel and send the
7707 * good news to the client, and alert the destination client of
7708 * an incoming tunnel.
7710 * FIXME send a path create to self, avoid code duplication
7712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7713 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7714 peer_info_get (&my_full_id),
7715 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7718 send_client_peer_connected (t, myid);
7719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7720 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7722 t->local_tid_dest = next_local_tid++;
7723 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7724 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7725 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7729 /* Ok, lets find a peer offering the service */
7730 if (NULL != t->dht_get_type)
7732 GNUNET_DHT_get_stop (t->dht_get_type);
7734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7735 GNUNET_h2s (&hash));
7737 GNUNET_DHT_get_start (dht_handle,
7738 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7740 dht_replication_level,
7741 GNUNET_DHT_RO_RECORD_ROUTE |
7742 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7744 &dht_get_type_handler, t);
7746 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7752 * Handler for connection requests to new peers by a string service description.
7754 * @param cls closure
7755 * @param client identification of the client
7756 * @param message the actual message, which includes messages the client wants
7759 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7760 const struct GNUNET_MessageHeader *message)
7762 struct GNUNET_MESH_ConnectPeerByString *msg;
7763 struct MeshRegexSearchContext *ctx;
7764 struct MeshRegexSearchInfo *info;
7765 struct GNUNET_DHT_GetHandle *get_h;
7766 struct GNUNET_HashCode key;
7767 struct MeshTunnel *t;
7768 struct MeshClient *c;
7769 MESH_TunnelNumber tid;
7774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7775 "Connect by string started\n");
7776 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7777 size = htons (message->size);
7779 /* Sanity check for client registration */
7780 if (NULL == (c = client_get (client)))
7783 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7788 /* Message size sanity check */
7789 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7796 /* Tunnel exists? */
7797 tid = ntohl (msg->tunnel_id);
7798 t = tunnel_get_by_local_id (c, tid);
7802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7806 /* Does client own tunnel? */
7807 if (t->owner->handle != client)
7810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7815 " on tunnel %s [%u]\n",
7816 GNUNET_i2s(&my_full_id),
7819 /* Only one connect_by_string allowed at the same time! */
7820 /* FIXME: allow more, return handle at api level to cancel, document */
7821 if (NULL != t->regex_ctx)
7824 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7828 /* Find string itself */
7829 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7830 string = (const char *) &msg[1];
7832 /* Initialize context */
7833 size = GNUNET_REGEX_get_first_key (string, len, &key);
7834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7835 " consumed %u bits out of %u\n", size, len);
7836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7837 " looking for %s\n", GNUNET_h2s (&key));
7839 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7841 info->description = GNUNET_strndup (string, len);
7842 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7843 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7846 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7847 ctx->position = size;
7851 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7853 /* Start search in DHT */
7854 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7855 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7856 &key, /* key to search */
7857 dht_replication_level, /* replication level */
7858 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7859 &info->description[size], /* xquery */
7860 // FIXME add BLOOMFILTER to exclude filtered peers
7861 len + 1 - size, /* xquery bits */
7862 // FIXME add BLOOMFILTER SIZE
7863 &dht_get_string_handler, ctx);
7865 GNUNET_break (GNUNET_OK ==
7866 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7869 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7871 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7877 * Handler for client traffic directed to one peer
7879 * @param cls closure
7880 * @param client identification of the client
7881 * @param message the actual message
7884 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7885 const struct GNUNET_MessageHeader *message)
7887 struct MeshClient *c;
7888 struct MeshTunnel *t;
7889 struct MeshPeerInfo *pi;
7890 struct GNUNET_MESH_Unicast *data_msg;
7891 MESH_TunnelNumber tid;
7894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7895 "Got a unicast request from a client!\n");
7897 /* Sanity check for client registration */
7898 if (NULL == (c = client_get (client)))
7901 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7906 data_msg = (struct GNUNET_MESH_Unicast *) message;
7908 /* Sanity check for message size */
7909 size = ntohs (message->size);
7910 if (sizeof (struct GNUNET_MESH_Unicast) +
7911 sizeof (struct GNUNET_MessageHeader) > size)
7914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7918 /* Tunnel exists? */
7919 tid = ntohl (data_msg->tid);
7920 t = tunnel_get_by_local_id (c, tid);
7924 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7928 /* Is it a local tunnel? Then, does client own the tunnel? */
7929 if (t->owner->handle != client)
7932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7936 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7937 &data_msg->destination.hashPubKey);
7938 /* Is the selected peer in the tunnel? */
7942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7946 /* PID should be as expected */
7947 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7951 "Unicast PID, expected %u, got %u\n",
7952 t->fwd_pid + 1, ntohl (data_msg->pid));
7953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7957 /* Ok, everything is correct, send the message
7958 * (pretend we got it from a mesh peer)
7961 /* Work around const limitation */
7962 char buf[ntohs (message->size)] GNUNET_ALIGN;
7963 struct GNUNET_MESH_Unicast *copy;
7965 copy = (struct GNUNET_MESH_Unicast *) buf;
7966 memcpy (buf, data_msg, size);
7967 copy->oid = my_full_id;
7968 copy->tid = htonl (t->id.tid);
7969 copy->ttl = htonl (default_ttl);
7970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7971 " calling generic handler...\n");
7972 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7975 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7982 * Handler for client traffic directed to the origin
7984 * @param cls closure
7985 * @param client identification of the client
7986 * @param message the actual message
7989 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7990 const struct GNUNET_MessageHeader *message)
7992 struct GNUNET_MESH_ToOrigin *data_msg;
7993 struct MeshTunnelClientInfo *clinfo;
7994 struct MeshClient *c;
7995 struct MeshTunnel *t;
7996 MESH_TunnelNumber tid;
7999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8000 "Got a ToOrigin request from a client!\n");
8001 /* Sanity check for client registration */
8002 if (NULL == (c = client_get (client)))
8005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8010 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
8012 /* Sanity check for message size */
8013 size = ntohs (message->size);
8014 if (sizeof (struct GNUNET_MESH_ToOrigin) +
8015 sizeof (struct GNUNET_MessageHeader) > size)
8018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8022 /* Tunnel exists? */
8023 tid = ntohl (data_msg->tid);
8024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8025 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
8028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8031 t = tunnel_get_by_local_id (c, tid);
8034 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8035 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8041 /* It should be sent by someone who has this as incoming tunnel. */
8042 if (GNUNET_NO == client_knows_tunnel (c, t))
8045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8049 /* PID should be as expected */
8050 clinfo = tunnel_get_client_fc (t, c);
8051 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
8054 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8055 "To Origin PID, expected %u, got %u\n",
8056 clinfo->bck_pid + 1,
8057 ntohl (data_msg->pid));
8058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8063 /* Ok, everything is correct, send the message
8064 * (pretend we got it from a mesh peer)
8067 char buf[ntohs (message->size)] GNUNET_ALIGN;
8068 struct GNUNET_MESH_ToOrigin *copy;
8070 /* Work around const limitation */
8071 copy = (struct GNUNET_MESH_ToOrigin *) buf;
8072 memcpy (buf, data_msg, size);
8073 GNUNET_PEER_resolve (t->id.oid, ©->oid);
8074 copy->tid = htonl (t->id.tid);
8075 copy->ttl = htonl (default_ttl);
8076 copy->pid = htonl (t->bck_pid + 1);
8078 copy->sender = my_full_id;
8079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8080 " calling generic handler...\n");
8081 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
8083 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8090 * Handler for client traffic directed to all peers in a tunnel
8092 * @param cls closure
8093 * @param client identification of the client
8094 * @param message the actual message
8097 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
8098 const struct GNUNET_MessageHeader *message)
8100 struct MeshClient *c;
8101 struct MeshTunnel *t;
8102 struct GNUNET_MESH_Multicast *data_msg;
8103 MESH_TunnelNumber tid;
8105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8106 "Got a multicast request from a client!\n");
8108 /* Sanity check for client registration */
8109 if (NULL == (c = client_get (client)))
8112 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8117 data_msg = (struct GNUNET_MESH_Multicast *) message;
8119 /* Sanity check for message size */
8120 if (sizeof (struct GNUNET_MESH_Multicast) +
8121 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
8124 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8128 /* Tunnel exists? */
8129 tid = ntohl (data_msg->tid);
8130 t = tunnel_get_by_local_id (c, tid);
8134 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8135 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8137 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8141 /* Does client own tunnel? */
8142 if (t->owner->handle != client)
8145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8149 /* PID should be as expected */
8150 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
8153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8154 "Multicast PID, expected %u, got %u\n",
8155 t->fwd_pid + 1, ntohl (data_msg->pid));
8156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8161 char buf[ntohs (message->size)] GNUNET_ALIGN;
8162 struct GNUNET_MESH_Multicast *copy;
8164 copy = (struct GNUNET_MESH_Multicast *) buf;
8165 memcpy (buf, message, ntohs (message->size));
8166 copy->oid = my_full_id;
8167 copy->tid = htonl (t->id.tid);
8168 copy->ttl = htonl (default_ttl);
8169 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
8170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8171 " calling generic handler...\n");
8172 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
8175 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
8181 * Handler for client's ACKs for payload traffic.
8183 * @param cls Closure (unused).
8184 * @param client Identification of the client.
8185 * @param message The actual message.
8188 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
8189 const struct GNUNET_MessageHeader *message)
8191 struct GNUNET_MESH_LocalAck *msg;
8192 struct MeshTunnel *t;
8193 struct MeshClient *c;
8194 MESH_TunnelNumber tid;
8197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
8198 /* Sanity check for client registration */
8199 if (NULL == (c = client_get (client)))
8202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8207 msg = (struct GNUNET_MESH_LocalAck *) message;
8209 /* Tunnel exists? */
8210 tid = ntohl (msg->tunnel_id);
8211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8212 t = tunnel_get_by_local_id (c, tid);
8216 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8222 ack = ntohl (msg->max_pid);
8223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8225 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8226 if (NULL != t->owner && t->owner->handle == client)
8228 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8230 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8234 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8235 tunnel_set_client_fwd_ack (t, c, ack);
8236 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8239 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8246 * Iterator over all peers to send a monitoring client info about a tunnel.
8248 * @param cls Closure (message being built).
8249 * @param key Key (hashed tunnel ID, unused).
8250 * @param value Peer info.
8252 * @return GNUNET_YES, to keep iterating.
8255 monitor_peers_iterator (void *cls,
8256 const struct GNUNET_HashCode * key,
8259 struct GNUNET_MESH_LocalMonitor *msg = cls;
8260 struct GNUNET_PeerIdentity *id;
8261 struct MeshPeerInfo *info = value;
8263 id = (struct GNUNET_PeerIdentity *) &msg[1];
8264 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8267 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8268 "* sending info about peer %s [%u]\n",
8269 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8277 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8279 * @param cls Closure (client handle).
8280 * @param key Key (hashed tunnel ID, unused).
8281 * @param value Tunnel info.
8283 * @return GNUNET_YES, to keep iterating.
8286 monitor_all_tunnels_iterator (void *cls,
8287 const struct GNUNET_HashCode * key,
8290 struct GNUNET_SERVER_Client *client = cls;
8291 struct MeshTunnel *t = value;
8292 struct GNUNET_MESH_LocalMonitor *msg;
8295 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8296 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8297 npeers * sizeof (struct GNUNET_PeerIdentity));
8298 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8299 msg->tunnel_id = htonl (t->id.tid);
8300 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8301 npeers * sizeof (struct GNUNET_PeerIdentity));
8302 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8304 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8305 monitor_peers_iterator,
8308 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8309 "* sending info about tunnel %s [%u] (%u peers)\n",
8310 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8312 if (msg->npeers != npeers)
8315 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8316 "Get tunnels fail: size %u - iter %u\n",
8317 npeers, msg->npeers);
8320 msg->npeers = htonl (npeers);
8321 GNUNET_SERVER_notification_context_unicast (nc, client,
8322 &msg->header, GNUNET_NO);
8328 * Handler for client's MONITOR request.
8330 * @param cls Closure (unused).
8331 * @param client Identification of the client.
8332 * @param message The actual message.
8335 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8336 const struct GNUNET_MessageHeader *message)
8338 struct MeshClient *c;
8340 /* Sanity check for client registration */
8341 if (NULL == (c = client_get (client)))
8344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8348 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8349 "Received get tunnels request from client %u\n",
8351 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8352 monitor_all_tunnels_iterator,
8354 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8355 "Get tunnels request from client %u completed\n",
8357 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8362 * Data needed to build a Monitor_Tunnel message.
8364 struct MeshMonitorTunnelContext
8367 * Partial message, including peer count.
8369 struct GNUNET_MESH_LocalMonitor *msg;
8372 * Hashmap with positions: peer->position.
8374 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8377 * Index of the parent of each peer in the message, realtive to the absolute
8378 * order in the array (can be in a previous message).
8380 uint32_t parents[1024];
8383 * Peers visited so far in the tree, aka position of the current peer.
8385 unsigned int npeers;
8388 * Client requesting the info.
8390 struct MeshClient *c;
8395 * Send a client a message about the structure of a tunnel.
8397 * @param ctx Context of the tunnel iteration, with info regarding the state
8398 * of the execution and the number of peers visited for this message.
8401 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8403 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8404 struct GNUNET_PeerIdentity *pid;
8405 unsigned int *parent;
8408 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8409 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8410 resp->header.size = htons (size);
8411 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8412 parent = (unsigned int *) &pid[resp->npeers];
8413 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8414 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8415 &resp->header, GNUNET_NO);
8419 * Iterator over a tunnel tree to build a message containing all peers
8420 * the in the tunnel, including relay nodes.
8422 * @param cls Closure (pointer to pointer of message being built).
8423 * @param peer Short ID of a peer.
8424 * @param parent Short ID of the @c peer 's parent.
8427 tunnel_tree_iterator (void *cls,
8428 GNUNET_PEER_Id peer,
8429 GNUNET_PEER_Id parent)
8431 struct MeshMonitorTunnelContext *ctx = cls;
8432 struct GNUNET_MESH_LocalMonitor *msg;
8433 struct GNUNET_PeerIdentity *pid;
8434 struct GNUNET_PeerIdentity ppid;
8437 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8438 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8439 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8440 &pid[msg->npeers].hashPubKey,
8441 (void *) (long) ctx->npeers,
8442 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8443 GNUNET_PEER_resolve (parent, &ppid);
8444 ctx->parents[msg->npeers] =
8445 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8451 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8453 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8456 send_client_tunnel_info (ctx);
8463 * Handler for client's MONITOR_TUNNEL request.
8465 * @param cls Closure (unused).
8466 * @param client Identification of the client.
8467 * @param message The actual message.
8470 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8471 const struct GNUNET_MessageHeader *message)
8473 const struct GNUNET_MESH_LocalMonitor *msg;
8474 struct GNUNET_MESH_LocalMonitor *resp;
8475 struct MeshMonitorTunnelContext ctx;
8476 struct MeshClient *c;
8477 struct MeshTunnel *t;
8479 /* Sanity check for client registration */
8480 if (NULL == (c = client_get (client)))
8483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8487 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8488 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8489 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8492 ntohl (msg->tunnel_id));
8493 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8496 /* We don't know the tunnel */
8497 struct GNUNET_MESH_LocalMonitor warn;
8500 warn.npeers = htonl (UINT_MAX);
8501 GNUNET_SERVER_notification_context_unicast (nc, client,
8504 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8508 /* Initialize context */
8509 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8513 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8517 /* Collect and send information */
8518 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8519 send_client_tunnel_info (&ctx);
8522 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8525 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8526 "Monitor tunnel request from client %u completed\n",
8528 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8533 * Functions to handle messages from clients
8535 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8536 {&handle_local_new_client, NULL,
8537 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8538 {&handle_local_announce_regex, NULL,
8539 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8540 {&handle_local_tunnel_create, NULL,
8541 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8542 sizeof (struct GNUNET_MESH_TunnelMessage)},
8543 {&handle_local_tunnel_destroy, NULL,
8544 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8545 sizeof (struct GNUNET_MESH_TunnelMessage)},
8546 {&handle_local_tunnel_speed, NULL,
8547 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8548 sizeof (struct GNUNET_MESH_TunnelMessage)},
8549 {&handle_local_tunnel_speed, NULL,
8550 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8551 sizeof (struct GNUNET_MESH_TunnelMessage)},
8552 {&handle_local_tunnel_buffer, NULL,
8553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8554 sizeof (struct GNUNET_MESH_TunnelMessage)},
8555 {&handle_local_tunnel_buffer, NULL,
8556 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8557 sizeof (struct GNUNET_MESH_TunnelMessage)},
8558 {&handle_local_connect_add, NULL,
8559 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8560 sizeof (struct GNUNET_MESH_PeerControl)},
8561 {&handle_local_connect_del, NULL,
8562 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8563 sizeof (struct GNUNET_MESH_PeerControl)},
8564 {&handle_local_blacklist, NULL,
8565 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8566 sizeof (struct GNUNET_MESH_PeerControl)},
8567 {&handle_local_unblacklist, NULL,
8568 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8569 sizeof (struct GNUNET_MESH_PeerControl)},
8570 {&handle_local_connect_by_type, NULL,
8571 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8572 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8573 {&handle_local_connect_by_string, NULL,
8574 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8575 {&handle_local_unicast, NULL,
8576 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8577 {&handle_local_to_origin, NULL,
8578 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8579 {&handle_local_multicast, NULL,
8580 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8581 {&handle_local_ack, NULL,
8582 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8583 sizeof (struct GNUNET_MESH_LocalAck)},
8584 {&handle_local_get_tunnels, NULL,
8585 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8586 sizeof (struct GNUNET_MessageHeader)},
8587 {&handle_local_show_tunnel, NULL,
8588 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8589 sizeof (struct GNUNET_MESH_LocalMonitor)},
8595 * To be called on core init/fail.
8597 * @param cls service closure
8598 * @param server handle to the server for this service
8599 * @param identity the public identity of this peer
8602 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8603 const struct GNUNET_PeerIdentity *identity)
8606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8607 core_handle = server;
8608 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8614 GNUNET_i2s (identity));
8615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8617 GNUNET_i2s (&my_full_id));
8618 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8620 GNUNET_abort(); // Try harder
8627 * Method called whenever a given peer connects.
8629 * @param cls closure
8630 * @param peer peer identity this notification is about
8631 * @param atsi performance data for the connection
8632 * @param atsi_count number of records in 'atsi'
8635 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8636 const struct GNUNET_ATS_Information *atsi,
8637 unsigned int atsi_count)
8639 struct MeshPeerInfo *peer_info;
8640 struct MeshPeerPath *path;
8642 DEBUG_CONN ("Peer connected\n");
8643 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8644 peer_info = peer_info_get (peer);
8645 if (myid == peer_info->id)
8647 DEBUG_CONN (" (self)\n");
8652 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8654 path = path_new (2);
8655 path->peers[0] = myid;
8656 path->peers[1] = peer_info->id;
8657 GNUNET_PEER_change_rc (myid, 1);
8658 GNUNET_PEER_change_rc (peer_info->id, 1);
8659 peer_info_add_path (peer_info, path, GNUNET_YES);
8660 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8666 * Method called whenever a peer disconnects.
8668 * @param cls closure
8669 * @param peer peer identity this notification is about
8672 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8674 struct MeshPeerInfo *pi;
8675 struct MeshPeerQueue *q;
8676 struct MeshPeerQueue *n;
8678 DEBUG_CONN ("Peer disconnected\n");
8679 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8689 /* TODO try to reroute this traffic instead */
8690 queue_destroy(q, GNUNET_YES);
8693 if (NULL != pi->core_transmit)
8695 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8696 pi->core_transmit = NULL;
8698 peer_info_remove_path (pi, pi->id, myid);
8701 DEBUG_CONN (" (self)\n");
8703 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8708 /******************************************************************************/
8709 /************************ MAIN FUNCTIONS ****************************/
8710 /******************************************************************************/
8713 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8715 * @param cls closure
8716 * @param key current key code
8717 * @param value value in the hash map
8718 * @return GNUNET_YES if we should continue to iterate,
8722 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8724 struct MeshTunnel *t = value;
8731 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8733 * @param cls closure
8734 * @param key current key code
8735 * @param value value in the hash map
8736 * @return GNUNET_YES if we should continue to iterate,
8740 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8742 struct MeshPeerInfo *p = value;
8743 struct MeshPeerQueue *q;
8744 struct MeshPeerQueue *n;
8752 queue_destroy(q, GNUNET_YES);
8756 peer_info_destroy (p);
8762 * Task run during shutdown.
8768 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8772 if (core_handle != NULL)
8774 GNUNET_CORE_disconnect (core_handle);
8779 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8782 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8783 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8784 if (dht_handle != NULL)
8786 GNUNET_DHT_disconnect (dht_handle);
8791 GNUNET_SERVER_notification_context_destroy (nc);
8794 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8796 GNUNET_SCHEDULER_cancel (announce_id_task);
8797 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8799 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8801 GNUNET_SCHEDULER_cancel (announce_applications_task);
8802 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8809 * Callback for hostkey read/generation
8811 * @param cls Closure (Configuration handle).
8812 * @param pk the private key
8813 * @param emsg error message
8816 key_generation_cb (void *cls,
8817 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8820 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8821 struct MeshPeerInfo *peer;
8822 struct MeshPeerPath *p;
8827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8828 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8830 GNUNET_SCHEDULER_shutdown ();
8833 my_private_key = pk;
8834 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8835 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8836 &my_full_id.hashPubKey);
8837 myid = GNUNET_PEER_intern (&my_full_id);
8838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8839 "Mesh for peer [%s] starting\n",
8840 GNUNET_i2s(&my_full_id));
8842 // transport_handle = GNUNET_TRANSPORT_connect(c,
8849 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8850 NULL, /* Closure passed to MESH functions */
8851 &core_init, /* Call core_init once connected */
8852 &core_connect, /* Handle connects */
8853 &core_disconnect, /* remove peers on disconnects */
8854 NULL, /* Don't notify about all incoming messages */
8855 GNUNET_NO, /* For header only in notification */
8856 NULL, /* Don't notify about all outbound messages */
8857 GNUNET_NO, /* For header-only out notification */
8858 core_handlers); /* Register these handlers */
8860 if (core_handle == NULL)
8863 GNUNET_SCHEDULER_shutdown ();
8868 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8871 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8872 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8873 GNUNET_SERVER_disconnect_notify (server_handle,
8874 &handle_local_client_disconnect, NULL);
8878 clients_tail = NULL;
8881 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8882 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8884 /* Create a peer_info for the local peer */
8885 peer = peer_info_get (&my_full_id);
8888 GNUNET_PEER_change_rc (myid, 1);
8889 peer_info_add_path (peer, p, GNUNET_YES);
8890 GNUNET_SERVER_resume (server_handle);
8891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8896 * Process mesh requests.
8898 * @param cls closure
8899 * @param server the initialized server
8900 * @param c configuration to use
8903 run (void *cls, struct GNUNET_SERVER_Handle *server,
8904 const struct GNUNET_CONFIGURATION_Handle *c)
8908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8909 server_handle = server;
8912 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8915 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8917 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8919 GNUNET_SCHEDULER_shutdown ();
8924 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8925 &refresh_path_time))
8927 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8929 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8930 "mesh", "refresh path time");
8931 GNUNET_SCHEDULER_shutdown ();
8936 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8937 &app_announce_time))
8939 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8941 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8942 "mesh", "app announce time");
8943 GNUNET_SCHEDULER_shutdown ();
8946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8947 "APP_ANNOUNCE_TIME %llu ms\n",
8948 app_announce_time.rel_value);
8950 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8953 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8955 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8956 "mesh", "id announce time");
8957 GNUNET_SCHEDULER_shutdown ();
8962 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8965 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8967 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8968 "mesh", "connect timeout");
8969 GNUNET_SCHEDULER_shutdown ();
8974 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8979 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8980 "mesh", "max msgs queue");
8981 GNUNET_SCHEDULER_shutdown ();
8986 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8989 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8991 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8992 "mesh", "max tunnels");
8993 GNUNET_SCHEDULER_shutdown ();
8998 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
9001 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
9003 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
9004 "mesh", "default ttl", 64);
9009 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
9012 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
9013 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
9014 "mesh", "max peers", 1000);
9019 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
9020 &dht_replication_level))
9022 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
9024 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
9025 "mesh", "dht replication level", 3);
9026 dht_replication_level = 3;
9029 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9030 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9031 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9032 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9033 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
9035 dht_handle = GNUNET_DHT_connect (c, 64);
9036 if (NULL == dht_handle)
9040 stats = GNUNET_STATISTICS_create ("mesh", c);
9042 GNUNET_SERVER_suspend (server_handle);
9043 /* Scheduled the task to clean up when shutdown is called */
9044 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
9046 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
9049 GNUNET_free (keyfile);
9054 * The main function for the mesh service.
9056 * @param argc number of arguments from the command line
9057 * @param argv command line arguments
9058 * @return 0 ok, 1 on error
9061 main (int argc, char *const *argv)
9066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
9067 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
9069 ret = (GNUNET_OK == r) ? 0 : 1;
9070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
9074 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
9075 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
9076 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);