2 This file is part of GNUnet.
3 (C) 2001-2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
64 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
65 GNUNET_TIME_UNIT_MINUTES,\
68 #if MESH_DEBUG_CONNECTION
69 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
71 #define DEBUG_CONN(...)
75 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
77 #define DEBUG_DHT(...)
84 struct timespec __mesh_start;
85 struct timespec __mesh_end;
86 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
87 #define INTERVAL_END \
89 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
90 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
91 if (__diff < 0) __diff += 1000000000;\
95 #define INTERVAL_SHOW \
97 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
99 #define INTERVAL_START
101 #define INTERVAL_SHOW
104 /******************************************************************************/
105 /************************ DATA STRUCTURES ****************************/
106 /******************************************************************************/
108 /** FWD declaration */
114 * Struct representing a piece of data being sent to other peers
118 /** Tunnel it belongs to. */
119 struct MeshTunnel *t;
121 /** How many remaining neighbors still hav't got it. */
122 unsigned int reference_counter;
124 /** How many remaining neighbors we need to send this to. */
125 unsigned int total_out;
127 /** Size of the data. */
136 * Struct containing info about a queued transmission to this peer
143 struct MeshPeerQueue *next;
148 struct MeshPeerQueue *prev;
151 * Peer this transmission is directed to.
153 struct MeshPeerInfo *peer;
156 * Tunnel this message belongs to.
158 struct MeshTunnel *tunnel;
161 * Pointer to info stucture used as cls.
171 * Size of the message
178 * Struct to store regex information announced by clients.
180 struct MeshRegexDescriptor
183 * Regular expression itself.
188 * Cached DFA for regular expression 'regex'.
190 struct GNUNET_REGEX_Automaton *dfa;
193 * How many characters per edge can we squeeze?
195 uint16_t compression;
199 * Struct containing all info possibly needed to build a package when called
202 struct MeshTransmissionDescriptor
204 /** ID of the tunnel this packet travels in */
205 struct MESH_TunnelID *origin;
207 /** Who was this message being sent to */
208 struct MeshPeerInfo *peer;
210 /** Ultimate destination of the packet */
211 GNUNET_PEER_Id destination;
213 /** Data descriptor */
214 struct MeshData* mesh_data;
219 * Struct containing all information regarding a given peer
229 * Last time we heard from this peer
231 struct GNUNET_TIME_Absolute last_contact;
234 * Task handler for delayed connect task;
236 GNUNET_SCHEDULER_TaskIdentifier connect_task;
239 * Number of attempts to reconnect so far
241 int n_reconnect_attempts;
244 * Paths to reach the peer, ordered by ascending hop count
246 struct MeshPeerPath *path_head;
249 * Paths to reach the peer, ordered by ascending hop count
251 struct MeshPeerPath *path_tail;
254 * Handle to stop the DHT search for a path to this peer
256 struct GNUNET_DHT_GetHandle *dhtget;
259 * Closure given to the DHT GET
261 struct MeshPathInfo *dhtgetcls;
264 * Array of tunnels this peer participates in
265 * (most probably a small amount, therefore not a hashmap)
266 * When the path to the peer changes, notify these tunnels to let them
267 * re-adjust their path trees.
269 struct MeshTunnel **tunnels;
272 * Number of tunnels this peers participates in
274 unsigned int ntunnels;
277 * Transmission queue to core DLL head
279 struct MeshPeerQueue *queue_head;
282 * Transmission queue to core DLL tail
284 struct MeshPeerQueue *queue_tail;
287 * How many messages are in the queue to this peer.
289 unsigned int queue_n;
292 * Handle to for queued transmissions
294 struct GNUNET_CORE_TransmitHandle *core_transmit;
299 * Globally unique tunnel identification (owner + number)
300 * DO NOT USE OVER THE NETWORK
305 * Node that owns the tunnel
310 * Tunnel number to differentiate all the tunnels owned by the node oid
311 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
313 MESH_TunnelNumber tid;
318 * Struct containing all information regarding a tunnel
319 * For an intermediate node the improtant info used will be:
320 * - id Tunnel unique identification
321 * - paths[0] To know where to send it next
322 * - metainfo: ready, speeds, accounting
329 struct MESH_TunnelID id;
332 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
334 MESH_TunnelNumber local_tid;
337 * Local tunnel number for local destination clients (incoming number)
338 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
341 MESH_TunnelNumber local_tid_dest;
344 * Is the speed on the tunnel limited to the slowest peer?
349 * Is the tunnel bufferless (minimum latency)?
354 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
359 * Packet ID of the last bck packet sent (unique counter per hop).
364 * SKIP value for this tunnel.
369 * Force sending ACK? Flag to allow duplicate ACK on POLL.
374 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
375 * Contains the Flow Control info: FWD ACK value received,
376 * last BCK ACK sent, PID and SKIP values.
378 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
381 * Last ACK sent towards the origin (for traffic towards leaf node).
383 uint32_t last_fwd_ack;
386 * BCK ACK value received from the hop towards the owner of the tunnel,
387 * (previous node / owner): up to what message PID can we sent back to him.
392 * How many messages are in the forward queue (towards leaves).
394 unsigned int fwd_queue_n;
397 * How many messages do we accept in the forward queue.
399 unsigned int fwd_queue_max;
402 * How many messages are in the backward queue (towards origin).
404 unsigned int bck_queue_n;
407 * How many messages do we accept in the backward queue.
409 unsigned int bck_queue_max;
412 * Task to poll peer in case of a stall.
414 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
417 * Last time the tunnel was used
419 struct GNUNET_TIME_Absolute timestamp;
422 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
423 * containing peers added by id or by type, not intermediate peers.
425 struct GNUNET_CONTAINER_MultiHashMap *peers;
428 * Number of peers that are connected and potentially ready to receive data
430 unsigned int peers_ready;
433 * Number of peers that have been added to the tunnel
435 unsigned int peers_total;
438 * Client owner of the tunnel, if any
440 struct MeshClient *owner;
443 * Clients that have been informed about and want to stay in the tunnel.
445 struct MeshClient **clients;
448 * Flow control info for each client.
450 struct MeshTunnelClientInfo *clients_fc;
453 * Number of elements in clients/clients_fc
455 unsigned int nclients;
458 * Clients that have been informed but requested to leave the tunnel.
460 struct MeshClient **ignore;
463 * Number of elements in clients
465 unsigned int nignore;
470 GNUNET_PEER_Id *blacklisted;
473 * Number of elements in blacklisted
475 unsigned int nblacklisted;
478 * Bloomfilter (for peer identities) to stop circular routes
480 char bloomfilter[MESH_BLOOM_SIZE];
485 struct MeshTunnelTree *tree;
488 * Application type we are looking for in this tunnel
490 GNUNET_MESH_ApplicationType type;
493 * Used to search peers offering a service
495 struct GNUNET_DHT_GetHandle *dht_get_type;
498 * Initial context of the regex search for a connect_by_string
500 struct MeshRegexSearchContext *regex_ctx;
503 * Task to keep the used paths alive
505 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
508 * Task to destroy the tunnel after timeout
510 * FIXME: merge the two? a tunnel will have either
511 * a path refresh OR a timeout, never both!
513 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
516 * Flag to signal the destruction of the tunnel.
517 * If this is set GNUNET_YES the tunnel will be destroyed
518 * when the queue is empty.
523 * Total messages pending for this tunnels, payload or not.
525 unsigned int pending_messages;
528 * If the tunnel is empty, destoy it.
530 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
535 * Info about a child node in a tunnel, needed to perform flow control.
537 struct MeshTunnelChildInfo
540 * ID of the child node.
560 * Maximum PID allowed (FWD ACK received).
565 * Last ACK sent to that child (BCK ACK).
570 * Circular buffer pointing to MeshPeerQueue elements for all
571 * payload traffic going to this child.
572 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
574 struct MeshPeerQueue **send_buffer;
577 * Index of the oldest element in the send_buffer.
579 unsigned int send_buffer_start;
582 * How many elements are already in the buffer.
584 unsigned int send_buffer_n;
587 * Tunnel this info is about
589 struct MeshTunnel *t;
592 * Task to poll peer in case of a stall.
594 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
597 * Time to use for next polling call.
599 struct GNUNET_TIME_Relative fc_poll_time;
604 * Info about a leaf client of a tunnel, needed to perform flow control.
606 struct MeshTunnelClientInfo
609 * PID of the last packet sent to the client (FWD).
614 * PID of the last packet received from the client (BCK).
619 * Maximum PID allowed (FWD ACK received).
624 * Last ACK sent to that child (BCK ACK).
632 * Info collected during iteration of child nodes in order to get the ACK value
635 struct MeshTunnelChildIteratorContext
638 * Tunnel whose info is being collected.
640 struct MeshTunnel *t;
643 * Is this context initialized? Is the value in max_child_ack valid?
648 * Maximum child ACK so far.
650 uint32_t max_child_ack;
653 * Number of children nodes
655 unsigned int nchildren;
660 * Info needed to work with tunnel paths and peers
667 struct MeshTunnel *t;
670 * Neighbouring peer to whom we send the packet to
672 struct MeshPeerInfo *peer;
677 struct MeshPeerPath *path;
682 * Struct containing information about a client of the service
689 struct MeshClient *next;
694 struct MeshClient *prev;
697 * Tunnels that belong to this client, indexed by local id
699 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
702 * Tunnels this client has accepted, indexed by incoming local id
704 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
707 * Tunnels this client has rejected, indexed by incoming local id
709 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
711 * Handle to communicate with the client
713 struct GNUNET_SERVER_Client *handle;
716 * Applications that this client has claimed to provide
718 struct GNUNET_CONTAINER_MultiHashMap *apps;
721 * Messages that this client has declared interest in
723 struct GNUNET_CONTAINER_MultiHashMap *types;
726 * Whether the client is active or shutting down (don't send confirmations
727 * to a client that is shutting down.
732 * ID of the client, mainly for debug messages
737 * Regular expressions describing the services offered by this client.
739 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
742 * Number of regular expressions in regexes.
744 unsigned int n_regex;
747 * Task to refresh all regular expresions in the DHT.
749 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
752 * Tmp store for partially retrieved regex.
760 * Struct to keep information of searches of services described by a regex
761 * using a user-provided string service description.
763 struct MeshRegexSearchInfo
766 * Which tunnel is this for
768 struct MeshTunnel *t;
771 * User provided description of the searched service.
776 * Part of the description already consumed by the search.
783 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
786 * Results from running DHT GETs.
788 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
791 * Contexts, for each running DHT GET. Free all on end of search.
793 struct MeshRegexSearchContext **contexts;
796 * Number of contexts (branches/steps in search).
798 unsigned int n_contexts;
801 * Peer that is connecting via connect_by_string. When connected, free ctx.
806 * Other peers that are found but not yet being connected to.
808 GNUNET_PEER_Id *peers;
811 * Number of elements in peers.
813 unsigned int n_peers;
816 * Next peer to try to connect to.
821 * Timeout for a connect attempt.
822 * When reached, try to connect to a different peer, if any. If not,
823 * try the same peer again.
825 GNUNET_SCHEDULER_TaskIdentifier timeout;
830 * Struct to keep state of running searches that have consumed a part of
833 struct MeshRegexSearchContext
836 * Part of the description already consumed by
837 * this particular search branch.
842 * Information about the search.
844 struct MeshRegexSearchInfo *info;
847 * We just want to look for one edge, the longer the better.
850 unsigned int longest_match;
853 * Destination hash of the longest match.
855 struct GNUNET_HashCode hash;
858 /******************************************************************************/
859 /************************ DEBUG FUNCTIONS ****************************/
860 /******************************************************************************/
864 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
865 * @param cls string to print
866 * @param success GNUNET_OK if the PUT was transmitted,
867 * GNUNET_NO on timeout,
868 * GNUNET_SYSERR on disconnect from service
869 * after the PUT message was transmitted
870 * (so we don't know if it was received or not)
875 mesh_debug (void *cls, int success)
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
883 unsigned int debug_fwd_ack;
884 unsigned int debug_bck_ack;
888 /******************************************************************************/
889 /*********************** GLOBAL VARIABLES ****************************/
890 /******************************************************************************/
892 /************************** Configuration parameters **************************/
895 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
897 static struct GNUNET_TIME_Relative refresh_path_time;
900 * How often to PUT local application numbers in the DHT.
902 static struct GNUNET_TIME_Relative app_announce_time;
905 * How often to PUT own ID in the DHT.
907 static struct GNUNET_TIME_Relative id_announce_time;
910 * Maximum time allowed to connect to a peer found by string.
912 static struct GNUNET_TIME_Relative connect_timeout;
915 * Default TTL for payload packets.
917 static unsigned long long default_ttl;
920 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
922 static unsigned long long dht_replication_level;
925 * How many tunnels are we willing to maintain.
926 * Local tunnels are always allowed, even if there are more tunnels than max.
928 static unsigned long long max_tunnels;
931 * How many messages *in total* are we willing to queue, divided by number of
932 * tunnels to get tunnel queue size.
934 static unsigned long long max_msgs_queue;
937 * How many peers do we want to remember?
939 static unsigned long long max_peers;
942 /*************************** Static global variables **************************/
945 * Hostkey generation context
947 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
950 * DLL with all the clients, head.
952 static struct MeshClient *clients;
955 * DLL with all the clients, tail.
957 static struct MeshClient *clients_tail;
960 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
962 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
965 * Number of tunnels known.
967 static unsigned long long n_tunnels;
970 * Tunnels incoming, indexed by MESH_TunnelNumber
971 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
973 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
976 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
978 static struct GNUNET_CONTAINER_MultiHashMap *peers;
981 * Handle to communicate with transport
983 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
986 * Handle to communicate with core.
988 static struct GNUNET_CORE_Handle *core_handle;
993 static struct GNUNET_DHT_Handle *dht_handle;
998 static struct GNUNET_SERVER_Handle *server_handle;
1001 * Handle to the statistics service.
1003 static struct GNUNET_STATISTICS_Handle *stats;
1006 * Notification context, to send messages to local clients.
1008 static struct GNUNET_SERVER_NotificationContext *nc;
1011 * Local peer own ID (memory efficient handle).
1013 static GNUNET_PEER_Id myid;
1016 * Local peer own ID (full value).
1018 static struct GNUNET_PeerIdentity my_full_id;
1023 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1028 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1031 * Tunnel ID for the next created tunnel (global tunnel number).
1033 static MESH_TunnelNumber next_tid;
1036 * Tunnel ID for the next incoming tunnel (local tunnel number).
1038 static MESH_TunnelNumber next_local_tid;
1041 * All application types provided by this peer.
1043 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1046 * All message types clients of this peer are interested in.
1048 static struct GNUNET_CONTAINER_MultiHashMap *types;
1051 * Task to periodically announce provided applications.
1053 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1056 * Task to periodically announce itself in the network.
1058 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1061 * Next ID to assign to a client.
1063 unsigned int next_client_id;
1066 /******************************************************************************/
1067 /*********************** DECLARATIONS **************************/
1068 /******************************************************************************/
1071 * Function to process paths received for a new peer addition. The recorded
1072 * paths form the initial tunnel, which can be optimized later.
1073 * Called on each result obtained for the DHT search.
1075 * @param cls closure
1076 * @param exp when will this value expire
1077 * @param key key of the result
1078 * @param type type of the result
1079 * @param size number of bytes in data
1080 * @param data pointer to the result data
1083 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1084 const struct GNUNET_HashCode * key,
1085 const struct GNUNET_PeerIdentity *get_path,
1086 unsigned int get_path_length,
1087 const struct GNUNET_PeerIdentity *put_path,
1088 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1089 size_t size, const void *data);
1093 * Function to process DHT string to regex matching.
1094 * Called on each result obtained for the DHT search.
1096 * @param cls closure (search context)
1097 * @param exp when will this value expire
1098 * @param key key of the result
1099 * @param get_path path of the get request (not used)
1100 * @param get_path_length lenght of get_path (not used)
1101 * @param put_path path of the put request (not used)
1102 * @param put_path_length length of the put_path (not used)
1103 * @param type type of the result
1104 * @param size number of bytes in data
1105 * @param data pointer to the result data
1107 * TODO: re-issue the request after certain time? cancel after X results?
1110 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1111 const struct GNUNET_HashCode * key,
1112 const struct GNUNET_PeerIdentity *get_path,
1113 unsigned int get_path_length,
1114 const struct GNUNET_PeerIdentity *put_path,
1115 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1116 size_t size, const void *data);
1120 * Function to process DHT string to regex matching.
1121 * Called on each result obtained for the DHT search.
1123 * @param cls closure (search context)
1124 * @param exp when will this value expire
1125 * @param key key of the result
1126 * @param get_path path of the get request (not used)
1127 * @param get_path_length lenght of get_path (not used)
1128 * @param put_path path of the put request (not used)
1129 * @param put_path_length length of the put_path (not used)
1130 * @param type type of the result
1131 * @param size number of bytes in data
1132 * @param data pointer to the result data
1135 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1136 const struct GNUNET_HashCode * key,
1137 const struct GNUNET_PeerIdentity *get_path,
1138 unsigned int get_path_length,
1139 const struct GNUNET_PeerIdentity *put_path,
1140 unsigned int put_path_length,
1141 enum GNUNET_BLOCK_Type type,
1142 size_t size, const void *data);
1146 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1147 * and insert it in the appropiate structures if the peer is not known yet.
1149 * @param peer Short identity of the peer.
1151 * @return Existing or newly created peer info.
1153 static struct MeshPeerInfo *
1154 peer_info_get_short (const GNUNET_PEER_Id peer);
1158 * Try to establish a new connection to this peer.
1159 * Use the best path for the given tunnel.
1160 * If the peer doesn't have any path to it yet, try to get one.
1161 * If the peer already has some path, send a CREATE PATH towards it.
1163 * @param peer PeerInfo of the peer.
1164 * @param t Tunnel for which to create the path, if possible.
1167 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1171 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1172 * needed rescources.
1173 * If peer already exists, reevaluate shortest path and change if different.
1175 * @param t Tunnel we want to add a new peer to
1176 * @param peer PeerInfo of the peer being added
1180 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1184 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1185 * that are no longer needed, as well as nodes of no longer reachable peers.
1186 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1188 * @param t Tunnel from which to remove the path.
1189 * @param peer Short id of the peer which should be removed.
1192 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1196 * Search for a tunnel by global ID using full PeerIdentities.
1198 * @param oid owner of the tunnel.
1199 * @param tid global tunnel number.
1201 * @return tunnel handler, NULL if doesn't exist.
1203 static struct MeshTunnel *
1204 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1208 * Delete an active client from the tunnel.
1214 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1217 * Notify a tunnel that a connection has broken that affects at least
1218 * some of its peers.
1220 * @param t Tunnel affected.
1221 * @param p1 Peer that got disconnected from p2.
1222 * @param p2 Peer that got disconnected from p1.
1224 * @return Short ID of the peer disconnected (either p1 or p2).
1225 * 0 if the tunnel remained unaffected.
1227 static GNUNET_PEER_Id
1228 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1233 * Get the current ack value for a tunnel, for data going from root to leaves,
1234 * taking in account the tunnel mode and the status of all children and clients.
1238 * @return Maximum PID allowed.
1241 tunnel_get_fwd_ack (struct MeshTunnel *t);
1245 * Add a client to a tunnel, initializing all needed data structures.
1247 * @param t Tunnel to which add the client.
1248 * @param c Client which to add to the tunnel.
1251 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1255 * Jump to the next edge, with the longest matching token.
1257 * @param block Block found in the DHT.
1258 * @param size Size of the block.
1259 * @param ctx Context of the search.
1261 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1264 regex_next_edge (const struct MeshRegexBlock *block,
1266 struct MeshRegexSearchContext *ctx);
1270 * Find a path to a peer that offers a regex servcie compatible
1271 * with a given string.
1273 * @param key The key of the accepting state.
1274 * @param ctx Context containing info about the string, tunnel, etc.
1277 regex_find_path (const struct GNUNET_HashCode *key,
1278 struct MeshRegexSearchContext *ctx);
1282 * @brief Queue and pass message to core when possible.
1284 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1285 * and accounts for it. In case the queue is full, the message is dropped and
1288 * Otherwise, message is treated as internal and allowed to go regardless of
1291 * @param cls Closure (@c type dependant). It will be used by queue_send to
1292 * build the message to be sent if not already prebuilt.
1293 * @param type Type of the message, 0 for a raw message.
1294 * @param size Size of the message.
1295 * @param dst Neighbor to send message to.
1296 * @param t Tunnel this message belongs to.
1299 queue_add (void *cls, uint16_t type, size_t size,
1300 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1304 * Free a transmission that was already queued with all resources
1305 * associated to the request.
1307 * @param queue Queue handler to cancel.
1308 * @param clear_cls Is it necessary to free associated cls?
1311 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1315 * @brief Get the next transmittable message from the queue.
1317 * This will be the head, except in the case of being a data packet
1318 * not allowed by the destination peer.
1320 * @param peer Destination peer.
1322 * @return The next viable MeshPeerQueue element to send to that peer.
1323 * NULL when there are no transmittable messages.
1325 struct MeshPeerQueue *
1326 queue_get_next (const struct MeshPeerInfo *peer);
1330 * Core callback to write a queued packet to core buffer
1332 * @param cls Closure (peer info).
1333 * @param size Number of bytes available in buf.
1334 * @param buf Where the to write the message.
1336 * @return number of bytes written to buf
1339 queue_send (void *cls, size_t size, void *buf);
1341 /******************************************************************************/
1342 /************************ ITERATORS ****************************/
1343 /******************************************************************************/
1346 * Iterator over found existing mesh regex blocks that match an ongoing search.
1348 * @param cls closure
1349 * @param key current key code
1350 * @param value value in the hash map
1351 * @return GNUNET_YES if we should continue to iterate,
1355 regex_result_iterator (void *cls,
1356 const struct GNUNET_HashCode * key,
1359 struct MeshRegexBlock *block = value;
1360 struct MeshRegexSearchContext *ctx = cls;
1362 if (GNUNET_YES == ntohl(block->accepting) &&
1363 ctx->position == strlen (ctx->info->description))
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1366 regex_find_path (key, ctx);
1367 return GNUNET_YES; // We found an accept state!
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1372 ctx->position, strlen(ctx->info->description),
1373 ntohl(block->accepting));
1376 regex_next_edge(block, SIZE_MAX, ctx);
1378 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1385 * Iterator over edges in a regex block retrieved from the DHT.
1387 * @param cls Closure (context of the search).
1388 * @param token Token that follows to next state.
1389 * @param len Lenght of token.
1390 * @param key Hash of next state.
1392 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1395 regex_edge_iterator (void *cls,
1398 const struct GNUNET_HashCode *key)
1400 struct MeshRegexSearchContext *ctx = cls;
1401 struct MeshRegexSearchInfo *info = ctx->info;
1405 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1410 current = &info->description[ctx->position];
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1412 current_len = strlen (info->description) - ctx->position;
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* token : %.*s\n", len, token);
1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1417 if (len > current_len)
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1420 return GNUNET_YES; // Token too long, wont match
1422 if (0 != strncmp (current, token, len))
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1425 return GNUNET_YES; // Token doesn't match
1428 if (len > ctx->longest_match)
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1431 ctx->longest_match = len;
1436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1445 * Jump to the next edge, with the longest matching token.
1447 * @param block Block found in the DHT.
1448 * @param size Size of the block.
1449 * @param ctx Context of the search.
1451 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1454 regex_next_edge (const struct MeshRegexBlock *block,
1456 struct MeshRegexSearchContext *ctx)
1458 struct MeshRegexSearchContext *new_ctx;
1459 struct MeshRegexSearchInfo *info = ctx->info;
1460 struct GNUNET_DHT_GetHandle *get_h;
1464 /* Find the longest match for the current string position,
1465 * among tokens in the given block */
1466 ctx->longest_match = 0;
1467 result = GNUNET_MESH_regex_block_iterate (block, size,
1468 ®ex_edge_iterator, ctx);
1469 GNUNET_break (GNUNET_OK == result);
1471 /* Did anything match? */
1472 if (0 == ctx->longest_match)
1475 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1476 new_ctx->info = info;
1477 new_ctx->position = ctx->position + ctx->longest_match;
1478 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1480 /* Check whether we already have a DHT GET running for it */
1482 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1485 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1487 ®ex_result_iterator,
1489 // FIXME: "leaks" new_ctx? avoid keeping it around?
1490 return; // We are already looking for it
1493 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1495 /* Start search in DHT */
1496 rest = &new_ctx->info->description[new_ctx->position];
1498 GNUNET_DHT_get_start (dht_handle, /* handle */
1499 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1500 &ctx->hash, /* key to search */
1501 dht_replication_level, /* replication level */
1502 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1504 // FIXME add BLOOMFILTER to exclude filtered peers
1505 strlen(rest) + 1, /* xquery bits */
1506 // FIXME add BLOOMFILTER SIZE
1507 &dht_get_string_handler, new_ctx);
1509 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1512 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1521 * Iterator over hash map entries to cancel DHT GET requests after a
1522 * successful connect_by_string.
1524 * @param cls Closure (unused).
1525 * @param key Current key code (unused).
1526 * @param value Value in the hash map (get handle).
1527 * @return GNUNET_YES if we should continue to iterate,
1531 regex_cancel_dht_get (void *cls,
1532 const struct GNUNET_HashCode * key,
1535 struct GNUNET_DHT_GetHandle *h = value;
1537 GNUNET_DHT_get_stop (h);
1543 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1544 * search for connect_by_string.
1546 * @param cls Closure (unused).
1547 * @param key Current key code (unused).
1548 * @param value MeshRegexBlock in the hash map.
1549 * @return GNUNET_YES if we should continue to iterate,
1553 regex_free_result (void *cls,
1554 const struct GNUNET_HashCode * key,
1558 GNUNET_free (value);
1564 * Find a path to a peer that offers a regex servcie compatible
1565 * with a given string.
1567 * @param key The key of the accepting state.
1568 * @param ctx Context containing info about the string, tunnel, etc.
1571 regex_find_path (const struct GNUNET_HashCode *key,
1572 struct MeshRegexSearchContext *ctx)
1574 struct GNUNET_DHT_GetHandle *get_h;
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1577 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1578 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1579 key, /* key to search */
1580 dht_replication_level, /* replication level */
1581 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE/* |
1582 GNUNET_DHT_RO_RECORD_ROUTE*/,
1584 // FIXME add BLOOMFILTER to exclude filtered peers
1585 0, /* xquery bits */
1586 // FIXME add BLOOMFILTER SIZE
1587 &dht_get_string_accept_handler, ctx);
1588 GNUNET_break (GNUNET_OK ==
1589 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1592 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1597 * Function called if the connect attempt to a peer found via
1598 * connect_by_string times out. Try to connect to another peer, if any.
1599 * Otherwise try to reconnect to the same peer.
1601 * @param cls Closure (info about regex search).
1602 * @param tc TaskContext.
1605 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1607 struct MeshRegexSearchInfo *info = cls;
1608 struct MeshPeerInfo *peer_info;
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1613 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1614 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1622 if (0 < info->n_peers)
1624 // Select next peer, put current in that spot.
1625 id = info->peers[info->i_peer];
1626 info->peers[info->i_peer] = info->peer;
1627 info->i_peer = (info->i_peer + 1) % info->n_peers;
1631 // Try to connect to same peer again.
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1636 peer_info = peer_info_get_short(id);
1637 tunnel_add_peer (info->t, peer_info);
1639 tunnel_delete_peer (info->t, old);
1640 peer_info_connect (peer_info, info->t);
1641 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1642 ®ex_connect_timeout,
1644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1649 * Cancel an ongoing regex search in the DHT and free all resources.
1651 * @param ctx The search context.
1654 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1656 struct MeshRegexSearchInfo *info = ctx->info;
1659 GNUNET_free (info->description);
1660 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1661 ®ex_cancel_dht_get, NULL);
1662 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1663 ®ex_free_result, NULL);
1664 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1665 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1666 info->t->regex_ctx = NULL;
1667 for (i = 0; i < info->n_contexts; i++)
1669 GNUNET_free (info->contexts[i]);
1671 if (0 < info->n_contexts)
1672 GNUNET_free (info->contexts);
1673 if (0 < info->n_peers)
1674 GNUNET_free (info->peers);
1675 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1677 GNUNET_SCHEDULER_cancel(info->timeout);
1683 /******************************************************************************/
1684 /************************ PERIODIC FUNCTIONS ****************************/
1685 /******************************************************************************/
1688 * Announce iterator over for each application provided by the peer
1690 * @param cls closure
1691 * @param key current key code
1692 * @param value value in the hash map
1693 * @return GNUNET_YES if we should continue to
1698 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1700 struct PBlock block;
1701 struct MeshClient *c;
1703 block.id = my_full_id;
1704 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1705 GNUNET_assert(NULL != c);
1706 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1707 if (0 == block.type)
1712 block.type = htonl (block.type);
1714 GNUNET_break (NULL !=
1715 GNUNET_DHT_put (dht_handle, key,
1716 dht_replication_level,
1717 GNUNET_DHT_RO_RECORD_ROUTE |
1718 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1719 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1721 (const char *) &block,
1722 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1723 app_announce_time, NULL, NULL));
1730 * Function called when the DHT regex put is complete.
1732 * @param the 'struct MeshClient' for which we were PUTting
1733 * @param success GNUNET_OK if the PUT was transmitted,
1734 * GNUNET_NO on timeout,
1735 * GNUNET_SYSERR on disconnect from service
1736 * after the PUT message was transmitted
1737 * (so we don't know if it was received or not)
1740 announce_regex_done (void *cls,
1743 struct MeshClient *c = cls;
1750 * Regex callback iterator to store own service description in the DHT.
1752 * @param cls closure.
1753 * @param key hash for current state.
1754 * @param proof proof for current state.
1755 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1756 * @param num_edges number of edges leaving current state.
1757 * @param edges edges leaving current state.
1760 regex_iterator (void *cls,
1761 const struct GNUNET_HashCode *key,
1764 unsigned int num_edges,
1765 const struct GNUNET_REGEX_Edge *edges)
1767 struct MeshRegexBlock *block;
1768 struct MeshRegexEdge *block_edge;
1769 enum GNUNET_DHT_RouteOption opt;
1773 unsigned int offset;
1776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1777 " regex dht put for state %s\n",
1779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1787 if (GNUNET_YES == accepting)
1789 struct MeshRegexAccept block;
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1792 " state %s is accepting, putting own id\n",
1794 size = sizeof (block);
1796 block.id = my_full_id;
1797 GNUNET_STATISTICS_update (stats, "# regex accepting blocks stored",
1799 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes stored",
1800 sizeof (block), GNUNET_NO);
1802 GNUNET_DHT_put(dht_handle, key,
1803 dht_replication_level,
1804 opt/* | GNUNET_DHT_RO_RECORD_ROUTE*/,
1805 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1808 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: expiration time should be option */
1812 len = strlen(proof);
1813 size = sizeof (struct MeshRegexBlock) + len;
1814 block = GNUNET_malloc (size);
1817 block->n_proof = htonl (len);
1818 block->n_edges = htonl (num_edges);
1819 block->accepting = htonl (accepting);
1821 /* Store the proof at the end of the block. */
1822 aux = (char *) &block[1];
1823 memcpy (aux, proof, len);
1826 /* Store each edge in a variable length MeshEdge struct at the
1827 * very end of the MeshRegexBlock structure.
1829 for (i = 0; i < num_edges; i++)
1831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1832 " edge %s towards %s\n",
1834 GNUNET_h2s(&edges[i].destination));
1836 /* aux points at the end of the last block */
1837 len = strlen (edges[i].label);
1838 size += sizeof (struct MeshRegexEdge) + len;
1839 // Calculate offset FIXME is this ok? use size instead?
1840 offset = aux - (char *) block;
1841 block = GNUNET_realloc (block, size);
1842 aux = &((char *) block)[offset];
1843 block_edge = (struct MeshRegexEdge *) aux;
1844 block_edge->key = edges[i].destination;
1845 block_edge->n_token = htonl (len);
1846 aux = (char *) &block_edge[1];
1847 memcpy (aux, edges[i].label, len);
1851 GNUNET_DHT_put(dht_handle, key,
1852 dht_replication_level,
1854 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1856 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1859 GNUNET_STATISTICS_update (stats, "# regex blocks stored",
1861 GNUNET_STATISTICS_update (stats, "# regex block bytes stored",
1864 GNUNET_free (block);
1869 * Store the regular expression describing a local service into the DHT.
1871 * @param regex The regular expresion.
1874 regex_put (struct MeshRegexDescriptor *regex)
1876 if (NULL == regex->dfa)
1878 regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1879 strlen (regex->regex),
1880 regex->compression);
1883 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1884 GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL);
1885 DEBUG_DHT (" regex_put (%s) end\n", regex->regex);
1890 * Periodically announce what applications are provided by local clients
1893 * @param cls closure
1894 * @param tc task context
1897 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1899 struct MeshClient *c = cls;
1902 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1903 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1905 DEBUG_DHT ("Starting PUT for regex\n");
1906 for (i = 0; i < c->n_regex; i++)
1907 regex_put (&c->regexes[i]);
1908 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1911 DEBUG_DHT ("Finished PUT for regex\n");
1916 * Periodically announce what applications are provided by local clients
1919 * @param cls closure
1920 * @param tc task context
1923 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1925 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1927 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1931 DEBUG_DHT ("Starting PUT for apps\n");
1933 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1935 announce_applications_task =
1936 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1938 DEBUG_DHT ("Finished PUT for apps\n");
1943 * Periodically announce self id in the DHT
1945 * @param cls closure
1946 * @param tc task context
1949 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1951 struct PBlock block;
1953 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1955 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1959 * - Set data expiration in function of X
1960 * - Adapt X to churn
1962 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1964 block.id = my_full_id;
1965 block.type = htonl (0);
1966 GNUNET_DHT_put (dht_handle, /* DHT handle */
1967 &my_full_id.hashPubKey, /* Key to use */
1968 dht_replication_level, /* Replication level */
1969 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1970 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1971 sizeof (block), /* Size of the data */
1972 (const char *) &block, /* Data itself */
1973 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1974 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1975 NULL, /* Continuation */
1976 NULL); /* Continuation closure */
1978 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1982 /******************************************************************************/
1983 /****************** GENERAL HELPER FUNCTIONS ************************/
1984 /******************************************************************************/
1987 * Decrements the reference counter and frees all resources if needed
1989 * @param mesh_data Data Descriptor used in a multicast message.
1990 * Freed no longer needed (last message).
1993 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1995 if (0 == --(mesh_data->reference_counter))
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1998 GNUNET_free (mesh_data->data);
1999 GNUNET_free (mesh_data);
2005 * Check if client has registered with the service and has not disconnected
2007 * @param client the client to check
2009 * @return non-NULL if client exists in the global DLL
2011 static struct MeshClient *
2012 client_get (struct GNUNET_SERVER_Client *client)
2014 struct MeshClient *c;
2019 if (c->handle == client)
2028 * Checks if a given client has subscribed to certain message type
2030 * @param message_type Type of message to check
2031 * @param c Client to check
2033 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
2035 * FIXME: use of crypto_hash slows it down
2036 * The hash function alone takes 8-10us out of the ~55us for the whole
2037 * process of retransmitting the message from one local client to another.
2038 * Find faster implementation!
2041 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
2043 struct GNUNET_HashCode hc;
2045 if (NULL == c->types)
2048 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
2049 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2054 * Check whether client wants traffic from a tunnel.
2056 * @param c Client to check.
2057 * @param t Tunnel to be found.
2059 * @return GNUNET_YES if client knows tunnel.
2061 * TODO look in client hashmap
2064 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2068 for (i = 0; i < t->nclients; i++)
2069 if (t->clients[i] == c)
2076 * Check whether client has been informed about a tunnel.
2078 * @param c Client to check.
2079 * @param t Tunnel to be found.
2081 * @return GNUNET_YES if client knows tunnel.
2083 * TODO look in client hashmap
2086 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2090 for (i = 0; i < t->nignore; i++)
2091 if (t->ignore[i] == c)
2093 return client_wants_tunnel(c, t);
2098 * Marks a client as uninterested in traffic from the tunnel, updating both
2099 * client and tunnel to reflect this.
2101 * @param c Client that doesn't want traffic anymore.
2102 * @param t Tunnel which should be ignored.
2104 * FIXME when to delete an incoming tunnel?
2107 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2109 struct GNUNET_HashCode hash;
2111 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2112 GNUNET_break (GNUNET_YES ==
2113 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2115 GNUNET_break (GNUNET_YES ==
2116 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2117 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2118 tunnel_delete_active_client (t, c);
2119 GNUNET_array_append (t->ignore, t->nignore, c);
2124 * Deletes a tunnel from a client (either owner or destination). To be used on
2125 * tunnel destroy, otherwise, use client_ignore_tunnel.
2127 * @param c Client whose tunnel to delete.
2128 * @param t Tunnel which should be deleted.
2131 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2133 struct GNUNET_HashCode hash;
2137 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2138 GNUNET_assert (GNUNET_YES ==
2139 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2145 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2147 GNUNET_assert (GNUNET_YES ==
2148 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2152 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2160 * Send the message to all clients that have subscribed to its type
2162 * @param msg Pointer to the message itself
2163 * @param payload Pointer to the payload of the message.
2164 * @param t The tunnel to whose clients this message goes.
2166 * @return number of clients this message was sent to
2169 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2170 const struct GNUNET_MessageHeader *payload,
2171 struct MeshTunnel *t)
2173 struct MeshClient *c;
2174 MESH_TunnelNumber *tid;
2177 char cbuf[htons (msg->size)];
2179 type = ntohs (payload->type);
2180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2182 GNUNET_MESH_DEBUG_M2S (type));
2184 memcpy (cbuf, msg, sizeof (cbuf));
2185 switch (htons (msg->type))
2187 struct GNUNET_MESH_Unicast *uc;
2188 struct GNUNET_MESH_Multicast *mc;
2189 struct GNUNET_MESH_ToOrigin *to;
2191 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2192 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2195 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2196 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2199 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2200 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2208 for (count = 0, c = clients; c != NULL; c = c->next)
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2211 if (client_is_subscribed (type, c))
2213 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2217 *tid = htonl (t->local_tid);
2221 if (GNUNET_NO == client_knows_tunnel (c, t))
2223 /* This client doesn't know the tunnel */
2224 struct GNUNET_MESH_TunnelNotification tmsg;
2225 struct GNUNET_HashCode hash;
2227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2228 tmsg.header.size = htons (sizeof (tmsg));
2229 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2230 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2231 tmsg.tunnel_id = htonl (t->local_tid_dest);
2233 if (GNUNET_YES == t->speed_min)
2234 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2235 if (GNUNET_YES == t->nobuffer)
2236 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2237 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2238 &tmsg.header, GNUNET_NO);
2239 tunnel_add_client (t, c);
2240 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2242 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2243 c->incoming_tunnels, &hash, t,
2244 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2246 *tid = htonl (t->local_tid_dest);
2249 /* Check if the client wants to get traffic from the tunnel */
2250 if (GNUNET_NO == client_wants_tunnel(c, t))
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2254 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2255 (struct GNUNET_MessageHeader
2256 *) cbuf, GNUNET_NO);
2265 * Notify the client that owns the tunnel that a peer has connected to it
2266 * (the requested path to it has been confirmed).
2268 * @param t Tunnel whose owner to notify
2269 * @param id Short id of the peer that has connected
2272 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2274 struct GNUNET_MESH_PeerControl pc;
2276 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2277 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2278 pc.tunnel_id = htonl (t->local_tid);
2279 GNUNET_PEER_resolve (id, &pc.peer);
2280 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2286 * Notify all clients (not depending on registration status) that the incoming
2287 * tunnel is no longer valid.
2289 * @param t Tunnel that was destroyed.
2292 send_clients_tunnel_destroy (struct MeshTunnel *t)
2294 struct GNUNET_MESH_TunnelMessage msg;
2296 msg.header.size = htons (sizeof (msg));
2297 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2298 msg.tunnel_id = htonl (t->local_tid_dest);
2299 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2304 * Notify clients of tunnel disconnections, if needed.
2305 * In case the origin disconnects, the destination clients get a tunnel destroy
2306 * notification. If the last destination disconnects (only one remaining client
2307 * in tunnel), the origin gets a (local ID) peer disconnected.
2308 * Note that the function must be called BEFORE removing the client from
2311 * @param t Tunnel that was destroyed.
2312 * @param c Client that disconnected.
2315 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2321 struct GNUNET_MESH_TunnelMessage msg;
2323 msg.header.size = htons (sizeof (msg));
2324 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2325 msg.tunnel_id = htonl (t->local_tid_dest);
2326 for (i = 0; i < t->nclients; i++)
2327 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2328 &msg.header, GNUNET_NO);
2330 // FIXME when to disconnect an incoming tunnel?
2331 else if (1 == t->nclients && NULL != t->owner)
2333 struct GNUNET_MESH_PeerControl msg;
2335 msg.header.size = htons (sizeof (msg));
2336 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2337 msg.tunnel_id = htonl (t->local_tid);
2338 msg.peer = my_full_id;
2339 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2340 &msg.header, GNUNET_NO);
2346 * Iterator over all the peers to remove the oldest not-used entry.
2348 * @param cls Closure (unsued).
2349 * @param key ID of the peer.
2350 * @param value Peer_Info of the peer.
2355 peer_info_timeout (void *cls,
2356 const struct GNUNET_HashCode *key,
2363 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2364 * and insert it in the appropiate structures if the peer is not known yet.
2366 * @param peer Full identity of the peer.
2368 * @return Existing or newly created peer info.
2370 static struct MeshPeerInfo *
2371 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2373 struct MeshPeerInfo *peer_info;
2375 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2376 if (NULL == peer_info)
2379 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2380 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2382 GNUNET_CONTAINER_multihashmap_iterate (peers,
2386 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2388 peer_info->id = GNUNET_PEER_intern (peer);
2390 peer_info->last_contact = GNUNET_TIME_absolute_get();
2397 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2398 * and insert it in the appropiate structures if the peer is not known yet.
2400 * @param peer Short identity of the peer.
2402 * @return Existing or newly created peer info.
2404 static struct MeshPeerInfo *
2405 peer_info_get_short (const GNUNET_PEER_Id peer)
2407 struct GNUNET_PeerIdentity id;
2409 GNUNET_PEER_resolve (peer, &id);
2410 return peer_info_get (&id);
2415 * Iterator to remove the tunnel from the list of tunnels a peer participates
2418 * @param cls Closure (tunnel info)
2419 * @param key GNUNET_PeerIdentity of the peer (unused)
2420 * @param value PeerInfo of the peer
2422 * @return always GNUNET_YES, to keep iterating
2425 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2427 struct MeshTunnel *t = cls;
2428 struct MeshPeerInfo *peer = value;
2431 for (i = 0; i < peer->ntunnels; i++)
2434 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2437 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2438 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2447 * Core callback to write a pre-constructed data packet to core buffer
2449 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2450 * @param size Number of bytes available in buf.
2451 * @param buf Where the to write the message.
2453 * @return number of bytes written to buf
2456 send_core_data_raw (void *cls, size_t size, void *buf)
2458 struct MeshTransmissionDescriptor *info = cls;
2459 struct GNUNET_MessageHeader *msg;
2462 GNUNET_assert (NULL != info);
2463 GNUNET_assert (NULL != info->mesh_data);
2464 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2465 total_size = ntohs (msg->size);
2467 if (total_size > size)
2472 memcpy (buf, msg, total_size);
2473 data_descriptor_decrement_rc (info->mesh_data);
2480 * Sends an already built non-multicast message to a peer,
2481 * properly registrating all used resources.
2483 * @param message Message to send. Function makes a copy of it.
2484 * @param peer Short ID of the neighbor whom to send the message.
2485 * @param t Tunnel on which this message is transmitted.
2488 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2489 const struct GNUNET_PeerIdentity *peer,
2490 struct MeshTunnel *t)
2492 struct MeshTransmissionDescriptor *info;
2493 struct MeshPeerInfo *neighbor;
2494 struct MeshPeerPath *p;
2498 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2500 size = ntohs (message->size);
2501 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2502 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2503 info->mesh_data->data = GNUNET_malloc (size);
2504 memcpy (info->mesh_data->data, message, size);
2505 type = ntohs(message->type);
2508 struct GNUNET_MESH_Unicast *m;
2509 struct GNUNET_MESH_ToOrigin *to;
2511 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2512 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2513 m->ttl = htonl (ntohl (m->ttl) - 1);
2515 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2516 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2518 to->pid = htonl(t->bck_pid);
2520 info->mesh_data->data_len = size;
2521 info->mesh_data->reference_counter = 1;
2522 info->mesh_data->total_out = 1;
2523 neighbor = peer_info_get (peer);
2524 for (p = neighbor->path_head; NULL != p; p = p->next)
2534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 " %s IS NOT DIRECTLY CONNECTED\n",
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2540 for (p = neighbor->path_head; NULL != p; p = p->next)
2542 struct GNUNET_PeerIdentity debug_id;
2545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2546 " path with %u hops through:\n",
2548 for (i = 0; i < p->length; i++)
2550 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 i, GNUNET_i2s(&debug_id));
2557 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2559 " no direct connection to %s\n",
2561 GNUNET_free (info->mesh_data->data);
2562 GNUNET_free (info->mesh_data);
2566 info->peer = neighbor;
2567 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2578 * Sends a CREATE PATH message for a path to a peer, properly registrating
2579 * all used resources.
2581 * @param peer PeerInfo of the final peer for whom this path is being created.
2582 * @param p Path itself.
2583 * @param t Tunnel for which the path is created.
2586 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2587 struct MeshTunnel *t)
2589 struct GNUNET_PeerIdentity id;
2590 struct MeshPathInfo *path_info;
2591 struct MeshPeerInfo *neighbor;
2597 p = tree_get_path_to_peer (t->tree, peer->id);
2604 for (i = 0; i < p->length; i++)
2606 if (p->peers[i] == myid)
2609 if (i >= p->length - 1)
2615 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2617 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2618 path_info->path = p;
2620 neighbor = peer_info_get (&id);
2621 path_info->peer = neighbor;
2622 queue_add (path_info,
2623 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2624 sizeof (struct GNUNET_MESH_ManipulatePath) +
2625 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2632 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2634 * @param t Tunnel whose path to destroy.
2635 * @param destination Short ID of the peer to whom the path to destroy.
2638 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2640 struct MeshPeerPath *p;
2643 p = tree_get_path_to_peer (t->tree, destination);
2649 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2650 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2652 struct GNUNET_MESH_ManipulatePath *msg;
2653 struct GNUNET_PeerIdentity *pi;
2657 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2658 msg->header.size = htons (size);
2659 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2660 msg->tid = htonl (t->id.tid);
2661 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2662 for (i = 0; i < p->length; i++)
2664 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2666 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2673 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2675 * @param t Tunnel which to confirm.
2678 send_path_ack (struct MeshTunnel *t)
2680 struct MeshTransmissionDescriptor *info;
2681 struct GNUNET_PeerIdentity id;
2682 GNUNET_PEER_Id peer;
2684 peer = tree_get_predecessor (t->tree);
2685 GNUNET_PEER_resolve (peer, &id);
2686 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2687 info->origin = &t->id;
2688 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2689 GNUNET_assert (NULL != info->peer);
2692 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2693 sizeof (struct GNUNET_MESH_PathACK),
2700 * Try to establish a new connection to this peer.
2701 * Use the best path for the given tunnel.
2702 * If the peer doesn't have any path to it yet, try to get one.
2703 * If the peer already has some path, send a CREATE PATH towards it.
2705 * @param peer PeerInfo of the peer.
2706 * @param t Tunnel for which to create the path, if possible.
2709 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2711 struct MeshPeerPath *p;
2712 struct MeshPathInfo *path_info;
2714 if (NULL != peer->path_head)
2716 p = tree_get_path_to_peer (t->tree, peer->id);
2723 // FIXME always send create path to self
2726 send_create_path (peer, p, t);
2730 struct GNUNET_HashCode hash;
2733 send_client_peer_connected (t, myid);
2734 t->local_tid_dest = next_local_tid++;
2735 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2738 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2739 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2746 else if (NULL == peer->dhtget)
2748 struct GNUNET_PeerIdentity id;
2750 GNUNET_PEER_resolve (peer->id, &id);
2751 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2752 path_info->peer = peer;
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2755 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2756 peer->dhtgetcls = path_info;
2757 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2758 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2759 &id.hashPubKey, /* key to search */
2760 dht_replication_level, /* replication level */
2761 GNUNET_DHT_RO_RECORD_ROUTE |
2762 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2763 NULL, /* xquery */ // FIXME BLOOMFILTER
2764 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2765 &dht_get_id_handler, path_info);
2767 /* Otherwise, there is no path but the DHT get is already started. */
2772 * Task to delay the connection of a peer
2774 * @param cls Closure (path info with tunnel and peer to connect).
2775 * Will be free'd on exection.
2776 * @param tc TaskContext
2779 peer_info_connect_task (void *cls,
2780 const struct GNUNET_SCHEDULER_TaskContext *tc)
2782 struct MeshPathInfo *path_info = cls;
2784 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2786 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2791 peer_info_connect (path_info->peer, path_info->t);
2797 * Destroy the peer_info and free any allocated resources linked to it
2799 * @param pi The peer_info to destroy.
2801 * @return GNUNET_OK on success
2804 peer_info_destroy (struct MeshPeerInfo *pi)
2806 struct GNUNET_PeerIdentity id;
2807 struct MeshPeerPath *p;
2808 struct MeshPeerPath *nextp;
2810 GNUNET_PEER_resolve (pi->id, &id);
2811 GNUNET_PEER_change_rc (pi->id, -1);
2814 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2817 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2818 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2820 if (NULL != pi->dhtget)
2822 GNUNET_DHT_get_stop (pi->dhtget);
2823 GNUNET_free (pi->dhtgetcls);
2829 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2833 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2835 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2843 * Remove all paths that rely on a direct connection between p1 and p2
2844 * from the peer itself and notify all tunnels about it.
2846 * @param peer PeerInfo of affected peer.
2847 * @param p1 GNUNET_PEER_Id of one peer.
2848 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2851 * TODO: optimize (see below)
2854 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2857 struct MeshPeerPath *p;
2858 struct MeshPeerPath *aux;
2859 struct MeshPeerInfo *peer_d;
2861 unsigned int destroyed;
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2868 p = peer->path_head;
2872 for (i = 0; i < (p->length - 1); i++)
2874 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2875 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2877 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2888 for (i = 0; i < peer->ntunnels; i++)
2890 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2894 * Problem: one or more peers have been deleted from the tunnel tree.
2895 * We don't know who they are to try to add them again.
2896 * We need to try to find a new path for each of the disconnected peers.
2897 * Some of them might already have a path to reach them that does not
2898 * involve p1 and p2. Adding all anew might render in a better tree than
2899 * the trivial immediate fix.
2901 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2902 * its children will be reachable trough him.
2904 peer_d = peer_info_get_short (d);
2907 for (p = peer_d->path_head; NULL != p; p = p->next)
2909 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2917 /* No callback, as peer will be already disconnected and a connection
2918 * scheduled by tunnel_notify_connection_broken.
2920 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2924 peer_info_connect (peer_d, peer->tunnels[i]);
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2932 * Add the path to the peer and update the path used to reach it in case this
2935 * @param peer_info Destination peer to add the path to.
2936 * @param path New path to add. Last peer must be the peer in arg 1.
2937 * Path will be either used of freed if already known.
2938 * @param trusted Do we trust that this path is real?
2941 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2944 struct MeshPeerPath *aux;
2948 if ((NULL == peer_info) || (NULL == path))
2951 path_destroy (path);
2954 if (path->peers[path->length - 1] != peer_info->id)
2957 path_destroy (path);
2960 if (path->length <= 2 && GNUNET_NO == trusted)
2962 /* Only allow CORE to tell us about direct paths */
2963 path_destroy (path);
2966 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2967 for (l = 1; l < path->length; l++)
2969 if (path->peers[l] == myid)
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2972 for (l2 = 0; l2 < path->length - l; l2++)
2974 path->peers[l2] = path->peers[l + l2];
2979 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2984 struct GNUNET_PeerIdentity id;
2986 GNUNET_PEER_resolve (peer_info->id, &id);
2987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2988 path->length, GNUNET_i2s (&id));
2991 l = path_get_length (path);
2998 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2999 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
3001 l2 = path_get_length (aux);
3004 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
3005 peer_info->path_tail, aux, path);
3010 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
3012 path_destroy (path);
3017 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
3024 * Add the path to the origin peer and update the path used to reach it in case
3025 * this is the shortest.
3026 * The path is given in peer_info -> destination, therefore we turn the path
3027 * upside down first.
3029 * @param peer_info Peer to add the path to, being the origin of the path.
3030 * @param path New path to add after being inversed.
3031 * @param trusted Do we trust that this path is real?
3034 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
3035 struct MeshPeerPath *path, int trusted)
3038 peer_info_add_path (peer_info, path, trusted);
3043 * Function called if the connection to the peer has been stalled for a while,
3044 * possibly due to a missed ACK. Poll the peer about its ACK status.
3046 * @param cls Closure (cinfo).
3047 * @param tc TaskContext.
3050 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3052 struct MeshTunnelChildInfo *cinfo = cls;
3053 struct GNUNET_MESH_Poll msg;
3054 struct GNUNET_PeerIdentity id;
3055 struct MeshTunnel *t;
3057 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3058 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3064 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3065 msg.header.size = htons (sizeof (msg));
3066 msg.tid = htonl (t->id.tid);
3067 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3068 msg.last_ack = htonl (cinfo->fwd_ack);
3070 GNUNET_PEER_resolve (cinfo->id, &id);
3071 send_prebuilt_message (&msg.header, &id, cinfo->t);
3072 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
3074 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
3075 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3076 &tunnel_poll, cinfo);
3081 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3082 * to obtain a local peer -> destination path and interning the peer ids.
3084 * @return Newly allocated and created path
3086 static struct MeshPeerPath *
3087 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3088 unsigned int get_path_length,
3089 const struct GNUNET_PeerIdentity *put_path,
3090 unsigned int put_path_length)
3092 struct MeshPeerPath *p;
3098 GNUNET_PEER_change_rc (myid, 1);
3099 i = get_path_length;
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3101 for (i--; i >= 0; i--)
3103 id = GNUNET_PEER_intern (&get_path[i]);
3104 if (p->length > 0 && id == p->peers[p->length - 1])
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3107 GNUNET_PEER_change_rc (id, -1);
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3112 GNUNET_i2s (&get_path[i]));
3114 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3115 p->peers[p->length - 1] = id;
3118 i = put_path_length;
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3120 for (i--; i >= 0; i--)
3122 id = GNUNET_PEER_intern (&put_path[i]);
3125 /* PUT path went through us, so discard the path up until now and start
3126 * from here to get a much shorter (and loop-free) path.
3131 if (p->length > 0 && id == p->peers[p->length - 1])
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3134 GNUNET_PEER_change_rc (id, -1);
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3139 GNUNET_i2s (&put_path[i]));
3141 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3142 p->peers[p->length - 1] = id;
3146 if (get_path_length > 0)
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3148 GNUNET_i2s (&get_path[0]));
3149 if (put_path_length > 0)
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3151 GNUNET_i2s (&put_path[0]));
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3154 for (i = 0; i < p->length; i++)
3156 struct GNUNET_PeerIdentity peer_id;
3158 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3160 GNUNET_i2s (&peer_id));
3168 * Adds a path to the peer_infos of all the peers in the path
3170 * @param p Path to process.
3171 * @param confirmed Whether we know if the path works or not.
3174 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3178 /* TODO: invert and add */
3179 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3180 for (i++; i < p->length; i++)
3182 struct MeshPeerInfo *aux;
3183 struct MeshPeerPath *copy;
3185 aux = peer_info_get_short (p->peers[i]);
3186 copy = path_duplicate (p);
3187 copy->length = i + 1;
3188 peer_info_add_path (aux, copy, GNUNET_NO);
3194 * Send keepalive packets for a peer
3196 * @param cls Closure (tunnel for which to send the keepalive).
3197 * @param tc Notification context.
3199 * TODO: implement explicit multicast keepalive?
3202 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3206 * Search for a tunnel among the incoming tunnels
3208 * @param tid the local id of the tunnel
3210 * @return tunnel handler, NULL if doesn't exist
3212 static struct MeshTunnel *
3213 tunnel_get_incoming (MESH_TunnelNumber tid)
3215 struct GNUNET_HashCode hash;
3217 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3218 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3219 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3224 * Search for a tunnel among the tunnels for a client
3226 * @param c the client whose tunnels to search in
3227 * @param tid the local id of the tunnel
3229 * @return tunnel handler, NULL if doesn't exist
3231 static struct MeshTunnel *
3232 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3234 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3236 return tunnel_get_incoming (tid);
3240 struct GNUNET_HashCode hash;
3242 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3243 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3249 * Search for a tunnel by global ID using PEER_ID
3251 * @param pi owner of the tunnel
3252 * @param tid global tunnel number
3254 * @return tunnel handler, NULL if doesn't exist
3256 static struct MeshTunnel *
3257 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3259 struct MESH_TunnelID id;
3260 struct GNUNET_HashCode hash;
3265 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3266 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3271 * Search for a tunnel by global ID using full PeerIdentities
3273 * @param oid owner of the tunnel
3274 * @param tid global tunnel number
3276 * @return tunnel handler, NULL if doesn't exist
3278 static struct MeshTunnel *
3279 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3281 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3286 * Delete an active client from the tunnel.
3292 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3296 for (i = 0; i < t->nclients; i++)
3298 if (t->clients[i] == c)
3300 t->clients[i] = t->clients[t->nclients - 1];
3301 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3302 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3304 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3312 * Delete an ignored client from the tunnel.
3318 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3322 for (i = 0; i < t->nignore; i++)
3324 if (t->ignore[i] == c)
3326 t->ignore[i] = t->ignore[t->nignore - 1];
3327 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3335 * Delete a client from the tunnel. It should be only done on
3336 * client disconnection, otherwise use client_ignore_tunnel.
3342 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3344 tunnel_delete_ignored_client (t, c);
3345 tunnel_delete_active_client (t, c);
3350 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3352 * Destroys queue elements of all waiting transmissions and frees all memory
3353 * used by the struct and its elements.
3355 * @param cls Closure (tunnel info).
3356 * @param key Hash of GNUNET_PEER_Id (unused).
3357 * @param value MeshTunnelChildInfo of the child.
3359 * @return always GNUNET_YES, to keep iterating
3362 tunnel_destroy_child (void *cls,
3363 const struct GNUNET_HashCode * key,
3366 struct MeshTunnelChildInfo *cinfo = value;
3367 struct MeshTunnel *t = cls;
3368 struct MeshPeerQueue *q;
3372 for (c = 0; c < cinfo->send_buffer_n; c++)
3374 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3375 q = cinfo->send_buffer[i];
3376 cinfo->send_buffer[i] = NULL;
3378 queue_destroy (q, GNUNET_YES);
3381 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3383 GNUNET_free_non_null (cinfo->send_buffer);
3384 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
3386 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
3387 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3389 GNUNET_free (cinfo);
3395 * Callback used to notify a client owner of a tunnel that a peer has
3396 * disconnected, most likely because of a path change.
3398 * @param cls Closure (tunnel this notification is about).
3399 * @param peer_id Short ID of disconnected peer.
3402 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3404 struct MeshTunnel *t = cls;
3405 struct MeshPeerInfo *peer;
3406 struct MeshPathInfo *path_info;
3408 if (NULL != t->owner && NULL != nc)
3410 struct GNUNET_MESH_PeerControl msg;
3412 msg.header.size = htons (sizeof (msg));
3413 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3414 msg.tunnel_id = htonl (t->local_tid);
3415 GNUNET_PEER_resolve (peer_id, &msg.peer);
3416 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3417 &msg.header, GNUNET_NO);
3419 peer = peer_info_get_short (peer_id);
3420 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3421 path_info->peer = peer;
3423 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3429 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3430 * needed rescources.
3431 * If peer already exists, reevaluate shortest path and change if different.
3433 * @param t Tunnel we want to add a new peer to
3434 * @param peer PeerInfo of the peer being added
3438 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3440 struct GNUNET_PeerIdentity id;
3441 struct MeshPeerPath *best_p;
3442 struct MeshPeerPath *p;
3443 unsigned int best_cost;
3446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3447 GNUNET_PEER_resolve (peer->id, &id);
3449 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3452 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3453 GNUNET_assert (GNUNET_OK ==
3454 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3456 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3459 if (NULL != (p = peer->path_head))
3462 best_cost = tree_get_path_cost (t->tree, p);
3465 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3472 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3473 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3474 t->path_refresh_task =
3475 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3479 /* Start a DHT get */
3480 peer_info_connect (peer, t);
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3486 * Add a path to a tunnel which we don't own, just to remember the next hop.
3487 * If destination node was already in the tunnel, the first hop information
3488 * will be replaced with the new path.
3490 * @param t Tunnel we want to add a new peer to
3491 * @param p Path to add
3492 * @param own_pos Position of local node in path.
3496 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3497 unsigned int own_pos)
3499 struct GNUNET_PeerIdentity id;
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3502 GNUNET_assert (0 != own_pos);
3503 tree_add_path (t->tree, p, NULL, NULL);
3504 if (own_pos < p->length - 1)
3506 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3507 tree_update_first_hops (t->tree, myid, &id);
3509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3513 * Add a client to a tunnel, initializing all needed data structures.
3515 * @param t Tunnel to which add the client.
3516 * @param c Client which to add to the tunnel.
3519 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3521 struct MeshTunnelClientInfo clinfo;
3523 GNUNET_array_append (t->clients, t->nclients, c);
3524 clinfo.fwd_ack = t->fwd_pid + 1;
3525 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3526 clinfo.fwd_pid = t->fwd_pid;
3527 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3529 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3534 * Notifies a tunnel that a connection has broken that affects at least
3535 * some of its peers. Sends a notification towards the root of the tree.
3536 * In case the peer is the owner of the tree, notifies the client that owns
3537 * the tunnel and tries to reconnect.
3539 * @param t Tunnel affected.
3540 * @param p1 Peer that got disconnected from p2.
3541 * @param p2 Peer that got disconnected from p1.
3543 * @return Short ID of the peer disconnected (either p1 or p2).
3544 * 0 if the tunnel remained unaffected.
3546 static GNUNET_PEER_Id
3547 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3553 tree_notify_connection_broken (t->tree, p1, p2,
3554 &tunnel_notify_client_peer_disconnected,
3556 if (myid != p1 && myid != p2)
3562 if (tree_get_predecessor (t->tree) != 0)
3564 /* We are the peer still connected, notify owner of the disconnection. */
3565 struct GNUNET_MESH_PathBroken msg;
3566 struct GNUNET_PeerIdentity neighbor;
3568 msg.header.size = htons (sizeof (msg));
3569 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3570 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3571 msg.tid = htonl (t->id.tid);
3572 msg.peer1 = my_full_id;
3573 GNUNET_PEER_resolve (pid, &msg.peer2);
3574 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3575 send_prebuilt_message (&msg.header, &neighbor, t);
3583 * Send a multicast packet to a neighbor.
3585 * @param cls Closure (Info about the multicast packet)
3586 * @param neighbor_id Short ID of the neighbor to send the packet to.
3589 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3591 struct MeshData *mdata = cls;
3592 struct MeshTransmissionDescriptor *info;
3593 struct GNUNET_PeerIdentity neighbor;
3594 struct GNUNET_MessageHeader *msg;
3596 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3598 info->mesh_data = mdata;
3599 (mdata->reference_counter) ++;
3600 info->destination = neighbor_id;
3601 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3603 GNUNET_i2s (&neighbor));
3604 info->peer = peer_info_get (&neighbor);
3605 GNUNET_assert (NULL != info->peer);
3606 msg = (struct GNUNET_MessageHeader *) mdata->data;
3609 info->mesh_data->data_len,
3616 * Queue a message in a tunnel in multicast, sending a copy to each child node
3617 * down the local one in the tunnel tree.
3619 * @param t Tunnel in which to send the data.
3620 * @param msg Message to be sent.
3623 tunnel_send_multicast (struct MeshTunnel *t,
3624 const struct GNUNET_MessageHeader *msg)
3626 struct MeshData *mdata;
3628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3629 " sending a multicast packet...\n");
3631 mdata = GNUNET_malloc (sizeof (struct MeshData));
3632 mdata->data_len = ntohs (msg->size);
3634 mdata->data = GNUNET_malloc (mdata->data_len);
3635 memcpy (mdata->data, msg, mdata->data_len);
3636 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3638 struct GNUNET_MESH_Multicast *mcast;
3640 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3641 if (t->fwd_queue_n >= t->fwd_queue_max)
3644 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3646 " message from %s!\n",
3647 GNUNET_i2s(&mcast->oid));
3648 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3649 " message at %s!\n",
3650 GNUNET_i2s(&my_full_id));
3651 GNUNET_free (mdata->data);
3652 GNUNET_free (mdata);
3656 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3658 ntohl (mcast->ttl));
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3665 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3666 if (mdata->reference_counter == 0)
3668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3669 " no one to send data to\n");
3670 GNUNET_free (mdata->data);
3671 GNUNET_free (mdata);
3672 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3677 mdata->total_out = mdata->reference_counter;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3680 " sending a multicast packet done\n");
3686 * Increase the SKIP value of all peers that
3687 * have not received a unicast message.
3689 * @param cls Closure (ID of the peer that HAS received the message).
3690 * @param key ID of the neighbor.
3691 * @param value Information about the neighbor.
3693 * @return GNUNET_YES to keep iterating.
3696 tunnel_add_skip (void *cls,
3697 const struct GNUNET_HashCode * key,
3700 struct GNUNET_PeerIdentity *neighbor = cls;
3701 struct MeshTunnelChildInfo *cinfo = value;
3703 /* TODO compare only pointers? key == neighbor? */
3704 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3714 * @brief Get neighbor's Flow Control information.
3716 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3717 * descendant of the local node in a certain tunnel.
3718 * If the info is not yet there (recently created path), creates the data struct
3719 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3722 * @param t Tunnel related.
3723 * @param peer Neighbor whose Flow Control info is needed.
3725 * @return Neighbor's Flow Control info.
3727 static struct MeshTunnelChildInfo *
3728 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3729 const struct GNUNET_PeerIdentity *peer)
3731 struct MeshTunnelChildInfo *cinfo;
3733 if (NULL == t->children_fc)
3736 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3742 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3743 cinfo->id = GNUNET_PEER_intern (peer);
3744 cinfo->skip = t->fwd_pid;
3747 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3748 cinfo->fwd_ack = t->fwd_pid + delta;
3749 cinfo->bck_ack = delta;
3750 cinfo->bck_pid = -1;
3752 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3753 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3755 cinfo->send_buffer =
3756 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3758 GNUNET_assert (GNUNET_OK ==
3759 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3762 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3769 * Get the Flow Control info of a client.
3771 * @param t Tunnel on which to look.
3772 * @param c Client whose ACK to get.
3774 * @return ACK value.
3776 static struct MeshTunnelClientInfo *
3777 tunnel_get_client_fc (struct MeshTunnel *t,
3778 struct MeshClient *c)
3782 for (i = 0; i < t->nclients; i++)
3784 if (t->clients[i] != c)
3786 return &t->clients_fc[i];
3789 return NULL; // avoid compiler / coverity complaints
3794 * Iterator to get the appropiate ACK value from all children nodes.
3796 * @param cls Closue (tunnel).
3797 * @param id Id of the child node.
3800 tunnel_get_child_fwd_ack (void *cls,
3803 struct GNUNET_PeerIdentity peer_id;
3804 struct MeshTunnelChildInfo *cinfo;
3805 struct MeshTunnelChildIteratorContext *ctx = cls;
3806 struct MeshTunnel *t = ctx->t;
3809 GNUNET_PEER_resolve (id, &peer_id);
3810 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3811 ack = cinfo->fwd_ack;
3814 if (GNUNET_NO == ctx->init)
3816 ctx->max_child_ack = ack;
3817 ctx->init = GNUNET_YES;
3820 if (GNUNET_YES == t->speed_min)
3822 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3826 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3833 * Get the maximum PID allowed to transmit to any
3834 * tunnel child of the local peer, depending on the tunnel
3835 * buffering/speed settings.
3839 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3842 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3844 struct MeshTunnelChildIteratorContext ctx;
3846 ctx.max_child_ack = 0;
3848 ctx.init = GNUNET_NO;
3849 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3851 if (0 == ctx.nchildren)
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3854 " tunnel has no children, no FWD ACK\n");
3858 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3859 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3861 return (int64_t) ctx.max_child_ack;
3866 * Set the FWD ACK value of a client in a particular tunnel.
3868 * @param t Tunnel affected.
3869 * @param c Client whose ACK to set.
3870 * @param ack ACK value.
3873 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3874 struct MeshClient *c,
3879 for (i = 0; i < t->nclients; i++)
3881 if (t->clients[i] != c)
3883 t->clients_fc[i].fwd_ack = ack;
3891 * Get the highest ACK value of all clients in a particular tunnel,
3892 * according to the buffering/speed settings.
3894 * @param t Tunnel on which to look.
3896 * @return Corresponding ACK value (max uint32_t).
3897 * If no clients are suscribed, -1LL.
3900 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3905 if (0 == t->nclients)
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3908 " tunnel has no clients, no FWD ACK\n");
3912 for (ack = -1LL, i = 0; i < t->nclients; i++)
3915 (GNUNET_YES == t->speed_min &&
3916 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3917 (GNUNET_NO == t->speed_min &&
3918 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3920 ack = t->clients_fc[i].fwd_ack;
3924 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3925 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3927 return (uint32_t) ack;
3932 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3933 * mode and the status of all children nodes.
3937 * @return Maximum PID allowed.
3940 tunnel_get_fwd_ack (struct MeshTunnel *t)
3944 uint32_t buffer_free;
3948 count = t->fwd_pid - t->skip;
3949 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3950 child_ack = tunnel_get_children_fwd_ack (t);
3951 client_ack = tunnel_get_clients_fwd_ack (t);
3952 if (GNUNET_YES == t->nobuffer)
3955 if (-1LL == child_ack)
3956 child_ack = client_ack;
3957 if (-1LL == child_ack)
3960 client_ack = child_ack = ack;
3965 ack = count + buffer_free; // Overflow? OK!
3967 if (-1LL == child_ack)
3969 // Node has no children, child_ack AND core buffer are irrelevant.
3970 if (-1LL == client_ack) // No children AND no clients? Not good!
3972 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3976 return (uint32_t) client_ack;
3978 if (-1LL == client_ack)
3982 if (GNUNET_YES == t->speed_min)
3984 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3985 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3989 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3990 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3993 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3994 count, buffer_free, child_ack, client_ack, ack);
4000 * Build a local ACK message and send it to a local client.
4002 * @param t Tunnel on which to send the ACK.
4003 * @param c Client to whom send the ACK.
4004 * @param ack Value of the ACK.
4007 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
4009 struct GNUNET_MESH_LocalAck msg;
4011 msg.header.size = htons (sizeof (msg));
4012 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4013 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
4014 msg.max_pid = htonl (ack);
4015 GNUNET_SERVER_notification_context_unicast(nc,
4022 * Build an ACK message and queue it to send to the given peer.
4024 * @param t Tunnel on which to send the ACK.
4025 * @param peer Peer to whom send the ACK.
4026 * @param ack Value of the ACK.
4029 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
4031 struct GNUNET_MESH_ACK msg;
4033 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4034 msg.header.size = htons (sizeof (msg));
4035 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
4036 msg.pid = htonl (ack);
4037 msg.tid = htonl (t->id.tid);
4039 send_prebuilt_message (&msg.header, peer, t);
4044 * Notify a the owner of a tunnel about how many more
4045 * payload packages will we accept on a given tunnel.
4047 * @param t Tunnel on which to send the ACK.
4050 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
4054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4055 "Sending client FWD ACK on tunnel %X\n",
4058 ack = tunnel_get_fwd_ack (t);
4060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4061 if (t->last_fwd_ack == ack)
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
4068 t->last_fwd_ack = ack;
4069 send_local_ack (t, t->owner, ack);
4074 * Send an ACK informing the predecessor about the available buffer space.
4075 * In case there is no predecessor, inform the owning client.
4076 * If buffering is off, send only on behalf of children or self if endpoint.
4077 * If buffering is on, send when sent to children and buffer space is free.
4078 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4079 * the ACK itself goes "back" (towards root).
4081 * @param t Tunnel on which to send the ACK.
4082 * @param type Type of message that triggered the ACK transmission.
4085 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4087 struct GNUNET_PeerIdentity id;
4090 if (NULL != t->owner)
4092 tunnel_send_client_fwd_ack (t);
4095 /* Is it after unicast / multicast retransmission? */
4098 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4099 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4101 "ACK due to FWD DATA retransmission\n");
4102 if (GNUNET_YES == t->nobuffer)
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4108 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4109 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4111 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4112 t->force_ack = GNUNET_YES;
4118 /* Check if we need to transmit the ACK */
4119 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4120 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4121 GNUNET_NO == t->force_ack)
4123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4125 " t->qmax: %u, t->qn: %u\n",
4126 t->fwd_queue_max, t->fwd_queue_n);
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4128 " t->pid: %u, t->ack: %u\n",
4129 t->fwd_pid, t->last_fwd_ack);
4133 /* Ok, ACK might be necessary, what PID to ACK? */
4134 ack = tunnel_get_fwd_ack (t);
4136 /* If speed_min and not all children have ack'd, dont send yet */
4137 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4143 t->last_fwd_ack = ack;
4144 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4145 send_ack (t, &id, ack);
4147 t->force_ack = GNUNET_NO;
4152 * Iterator to send a child node a BCK ACK to allow him to send more
4155 * @param cls Closure (tunnel).
4156 * @param id Id of the child node.
4159 tunnel_send_child_bck_ack (void *cls,
4162 struct MeshTunnel *t = cls;
4163 struct MeshTunnelChildInfo *cinfo;
4164 struct GNUNET_PeerIdentity peer;
4167 GNUNET_PEER_resolve (id, &peer);
4168 cinfo = tunnel_get_neighbor_fc (t, &peer);
4169 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4171 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4174 " Not sending ACK, not needed\n");
4177 cinfo->bck_ack = ack;
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4180 " Sending BCK ACK %u (last sent: %u)\n",
4181 ack, cinfo->bck_ack);
4182 send_ack (t, &peer, ack);
4187 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4189 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4191 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4192 * - implement a size 1 buffer in each client_fc AND children_fc
4193 * to hold at least 1 message per "child".
4194 * problem: violates no buffer policy
4195 * - ack 0 and make "children" poll for transmission slots
4196 * problem: big overhead, extra latency even in low traffic
4199 * @param t Tunnel on which to send the BCK ACKs.
4202 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4205 unsigned int tunnel_delta;
4207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4209 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4212 /* Find client whom to allow to send to origin (with lowest buffer space) */
4213 for (i = 0; i < t->nclients; i++)
4215 struct MeshTunnelClientInfo *clinfo;
4218 clinfo = &t->clients_fc[i];
4219 delta = clinfo->bck_ack - clinfo->bck_pid;
4220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4221 t->clients[i]->id, delta);
4223 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4224 (GNUNET_YES == t->nobuffer && 0 == delta))
4228 ack = clinfo->bck_pid;
4229 ack += t->nobuffer ? 1 : tunnel_delta;
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4231 " sending ack to client %u: %u\n",
4232 t->clients[i]->id, ack);
4233 send_local_ack (t, t->clients[i], ack);
4234 clinfo->bck_ack = ack;
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4239 " not sending ack to client %u (td %u, d %u)\n",
4240 t->clients[i]->id, tunnel_delta, delta);
4247 * Send an ACK informing the children nodes and destination clients about
4248 * the available buffer space.
4249 * If buffering is off, send only on behalf of root (can be self).
4250 * If buffering is on, send when sent to predecessor and buffer space is free.
4251 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4252 * the ACK itself goes "forward" (towards children/clients).
4254 * @param t Tunnel on which to send the ACK.
4255 * @param type Type of message that triggered the ACK transmission.
4258 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4261 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4262 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4263 /* Is it after data to_origin retransmission? */
4266 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4267 if (GNUNET_YES == t->nobuffer)
4269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4270 " Not sending ACK, nobuffer\n");
4274 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4275 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4277 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4278 t->force_ack = GNUNET_YES;
4284 tunnel_send_clients_bck_ack (t);
4285 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4286 t->force_ack = GNUNET_NO;
4291 * @brief Re-initiate traffic to this peer if necessary.
4293 * Check if there is traffic queued towards this peer
4294 * and the core transmit handle is NULL (traffic was stalled).
4295 * If so, call core tmt rdy.
4297 * @param cls Closure (unused)
4298 * @param peer_id Short ID of peer to which initiate traffic.
4301 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4303 struct MeshPeerInfo *peer;
4304 struct GNUNET_PeerIdentity id;
4305 struct MeshPeerQueue *q;
4308 peer = peer_info_get_short(peer_id);
4309 if (NULL != peer->core_transmit)
4312 q = queue_get_next(peer);
4315 /* Might br multicast traffic already sent to this particular peer but
4316 * not to other children in this tunnel.
4317 * This way t->queue_n would be > 0 but the queue of this particular peer
4323 GNUNET_PEER_resolve (peer->id, &id);
4324 peer->core_transmit =
4325 GNUNET_CORE_notify_transmit_ready(core_handle,
4328 GNUNET_TIME_UNIT_FOREVER_REL,
4338 * @brief Allow transmission of FWD traffic on this tunnel
4340 * Check if there is traffic queued towards any children
4341 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4343 * @param t Tunnel on which to unlock FWD traffic.
4346 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4348 if (0 == t->fwd_queue_n)
4351 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4356 * @brief Allow transmission of BCK traffic on this tunnel
4358 * Check if there is traffic queued towards the root of the tree
4359 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4361 * @param t Tunnel on which to unlock BCK traffic.
4364 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4366 if (0 == t->bck_queue_n)
4369 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4374 * Send a message to all peers in this tunnel that the tunnel is no longer
4377 * @param t The tunnel whose peers to notify.
4380 tunnel_send_destroy (struct MeshTunnel *t)
4382 struct GNUNET_MESH_TunnelDestroy msg;
4384 msg.header.size = htons (sizeof (msg));
4385 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4386 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4387 msg.tid = htonl (t->id.tid);
4388 tunnel_send_multicast (t, &msg.header);
4393 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4395 * @param cls Closure (Tunnel which to cancel).
4396 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4399 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4401 struct MeshTunnel *t = cls;
4402 struct MeshPeerInfo *peer_info;
4403 struct MeshPeerQueue *pq;
4404 struct MeshPeerQueue *next;
4406 peer_info = peer_info_get_short (neighbor_id);
4407 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4410 if (pq->tunnel == t)
4412 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4413 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4414 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4416 // Should have been removed on destroy children
4419 queue_destroy (pq, GNUNET_YES);
4422 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4424 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4425 peer_info->core_transmit = NULL;
4430 * Destroy the tunnel and free any allocated resources linked to it.
4432 * @param t the tunnel to destroy
4434 * @return GNUNET_OK on success
4437 tunnel_destroy (struct MeshTunnel *t)
4439 struct MeshClient *c;
4440 struct GNUNET_HashCode hash;
4451 struct GNUNET_PeerIdentity id;
4453 GNUNET_PEER_resolve (t->id.oid, &id);
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4455 GNUNET_i2s (&id), t->id.tid);
4457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4461 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4462 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4470 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4472 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4479 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4480 for (i = 0; i < t->nclients; i++)
4484 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4490 for (i = 0; i < t->nignore; i++)
4494 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4501 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4502 GNUNET_free_non_null (t->clients);
4503 GNUNET_free_non_null (t->clients_fc);
4505 if (NULL != t->peers)
4507 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4509 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4512 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4513 &tunnel_destroy_child,
4515 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4516 t->children_fc = NULL;
4518 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4519 tree_destroy (t->tree);
4521 if (NULL != t->regex_ctx)
4522 regex_cancel_search (t->regex_ctx);
4523 if (NULL != t->dht_get_type)
4524 GNUNET_DHT_get_stop (t->dht_get_type);
4525 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4526 GNUNET_SCHEDULER_cancel (t->timeout_task);
4527 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4528 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4531 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4536 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4539 * Tunnel is empty: destroy it.
4541 * @param cls Closure (Tunnel).
4542 * @param tc TaskContext.
4545 tunnel_destroy_empty_delayed (void *cls,
4546 const struct GNUNET_SCHEDULER_TaskContext *tc)
4548 struct MeshTunnel *t = cls;
4550 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4551 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4554 if (0 != t->nclients ||
4555 0 != tree_count_children (t->tree))
4560 struct GNUNET_PeerIdentity id;
4562 GNUNET_PEER_resolve (t->id.oid, &id);
4563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4564 "executing destruction of empty tunnel %s [%X]\n",
4565 GNUNET_i2s (&id), t->id.tid);
4574 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4576 * @param t Tunnel to destroy if empty.
4579 tunnel_destroy_empty (struct MeshTunnel *t)
4581 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4583 0 != tree_count_children (t->tree))
4585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4587 t->delayed_destroy, t->nclients, 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 "scheduling destruction of empty tunnel %s [%X]\n",
4598 GNUNET_i2s (&id), t->id.tid);
4602 t->delayed_destroy =
4603 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4604 &tunnel_destroy_empty_delayed,
4610 * Create a new tunnel
4612 * @param owner Who is the owner of the tunnel (short ID).
4613 * @param tid Tunnel Number of the tunnel.
4614 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4615 * @param local Tunnel Number for the tunnel, for the client point of view.
4617 * @return A new initialized tunnel. NULL on error.
4619 static struct MeshTunnel *
4620 tunnel_new (GNUNET_PEER_Id owner,
4621 MESH_TunnelNumber tid,
4622 struct MeshClient *client,
4623 MESH_TunnelNumber local)
4625 struct MeshTunnel *t;
4626 struct GNUNET_HashCode hash;
4628 if (n_tunnels >= max_tunnels && NULL == client)
4631 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4634 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4635 t->bck_queue_max = t->fwd_queue_max;
4636 t->tree = tree_new (owner);
4638 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4639 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4640 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4641 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4642 t->local_tid = local;
4643 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4645 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4647 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4649 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4650 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4657 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4664 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4666 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4667 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4671 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4681 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4682 * that are no longer needed, as well as nodes of no longer reachable peers.
4683 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4685 * @param t Tunnel from which to remove the path.
4686 * @param peer Short id of the peer which should be removed.
4689 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4693 r = tree_del_peer (t->tree, peer, NULL, NULL);
4696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4697 "Tunnel %u [%u] has no more nodes\n",
4698 t->id.oid, t->id.tid);
4704 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4705 * client when the client disconnects. If the client is not the owner, the
4706 * owner will get notified if no more clients are in the tunnel and the client
4707 * get removed from the tunnel's list.
4709 * @param cls closure (client that is disconnecting)
4710 * @param key the hash of the local tunnel id (used to access the hashmap)
4711 * @param value the value stored at the key (tunnel to destroy)
4713 * @return GNUNET_OK, keep iterating.
4716 tunnel_destroy_iterator (void *cls,
4717 const struct GNUNET_HashCode * key,
4720 struct MeshTunnel *t = value;
4721 struct MeshClient *c = cls;
4723 send_client_tunnel_disconnect (t, c);
4726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4727 tunnel_delete_client (t, c);
4728 client_delete_tunnel (c, t);
4729 tunnel_destroy_empty (t);
4732 tunnel_send_destroy (t);
4734 t->destroy = GNUNET_YES;
4741 * Timeout function, destroys tunnel if called
4743 * @param cls Closure (tunnel to destroy).
4744 * @param tc TaskContext
4747 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4749 struct MeshTunnel *t = cls;
4750 struct GNUNET_PeerIdentity id;
4752 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4753 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4755 GNUNET_PEER_resolve(t->id.oid, &id);
4756 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4757 "Tunnel %s [%X] timed out. Destroying.\n",
4758 GNUNET_i2s(&id), t->id.tid);
4759 send_clients_tunnel_destroy (t);
4764 * Resets the tunnel timeout. Starts it if no timeout was running.
4766 * @param t Tunnel whose timeout to reset.
4768 * TODO use heap to improve efficiency of scheduler.
4771 tunnel_reset_timeout (struct MeshTunnel *t)
4773 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4774 GNUNET_SCHEDULER_cancel (t->timeout_task);
4776 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4777 (refresh_path_time, 4), &tunnel_timeout, t);
4781 /******************************************************************************/
4782 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4783 /******************************************************************************/
4786 * Function to send a create path packet to a peer.
4788 * @param cls closure
4789 * @param size number of bytes available in buf
4790 * @param buf where the callee should write the message
4791 * @return number of bytes written to buf
4794 send_core_path_create (void *cls, size_t size, void *buf)
4796 struct MeshPathInfo *info = cls;
4797 struct GNUNET_MESH_ManipulatePath *msg;
4798 struct GNUNET_PeerIdentity *peer_ptr;
4799 struct MeshTunnel *t = info->t;
4800 struct MeshPeerPath *p = info->path;
4805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4807 sizeof (struct GNUNET_MESH_ManipulatePath) +
4808 p->length * sizeof (struct GNUNET_PeerIdentity);
4810 if (size < size_needed || NULL == buf)
4815 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4816 msg->header.size = htons (size_needed);
4817 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4818 msg->tid = ntohl (t->id.tid);
4821 if (GNUNET_YES == t->speed_min)
4822 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4823 if (GNUNET_YES == t->nobuffer)
4824 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4825 msg->opt = htonl(opt);
4828 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4829 for (i = 0; i < p->length; i++)
4831 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4838 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4844 * Fill the core buffer
4846 * @param cls closure (data itself)
4847 * @param size number of bytes available in buf
4848 * @param buf where the callee should write the message
4850 * @return number of bytes written to buf
4853 send_core_data_multicast (void *cls, size_t size, void *buf)
4855 struct MeshTransmissionDescriptor *info = cls;
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4859 GNUNET_assert (NULL != info);
4860 GNUNET_assert (NULL != info->peer);
4861 total_size = info->mesh_data->data_len;
4862 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4864 if (total_size > size)
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4870 memcpy (buf, info->mesh_data->data, total_size);
4873 struct GNUNET_MESH_Multicast *mc;
4874 struct GNUNET_MessageHeader *mh;
4877 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4879 mc = (struct GNUNET_MESH_Multicast *) mh;
4880 mh = (struct GNUNET_MessageHeader *) &mc[1];
4881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4882 " multicast, payload type %s\n",
4883 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4885 " multicast, payload size %u\n", ntohs (mh->size));
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4890 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4894 data_descriptor_decrement_rc (info->mesh_data);
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4903 * Creates a path ack message in buf and frees all unused resources.
4905 * @param cls closure (MeshTransmissionDescriptor)
4906 * @param size number of bytes available in buf
4907 * @param buf where the callee should write the message
4908 * @return number of bytes written to buf
4911 send_core_path_ack (void *cls, size_t size, void *buf)
4913 struct MeshTransmissionDescriptor *info = cls;
4914 struct GNUNET_MESH_PathACK *msg = buf;
4916 GNUNET_assert (NULL != info);
4917 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4922 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4923 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4924 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4925 msg->tid = htonl (info->origin->tid);
4926 msg->peer_id = my_full_id;
4929 /* TODO add signature */
4931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4932 return sizeof (struct GNUNET_MESH_PathACK);
4937 * Free a transmission that was already queued with all resources
4938 * associated to the request.
4940 * @param queue Queue handler to cancel.
4941 * @param clear_cls Is it necessary to free associated cls?
4944 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4946 struct MeshTransmissionDescriptor *dd;
4947 struct MeshPathInfo *path_info;
4948 struct MeshTunnelChildInfo *cinfo;
4949 struct GNUNET_PeerIdentity id;
4953 if (GNUNET_YES == clear_cls)
4955 switch (queue->type)
4957 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4958 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4959 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4961 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4962 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4963 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4964 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4965 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4966 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4968 " prebuilt message\n");
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4971 GNUNET_MESH_DEBUG_M2S(queue->type));
4973 data_descriptor_decrement_rc (dd->mesh_data);
4975 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4977 path_info = queue->cls;
4978 path_destroy (path_info->path);
4982 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4983 " type %s unknown!\n",
4984 GNUNET_MESH_DEBUG_M2S(queue->type));
4986 GNUNET_free_non_null (queue->cls);
4988 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4989 queue->peer->queue_tail,
4992 /* Delete from child_fc in the appropiate tunnel */
4993 max = queue->tunnel->fwd_queue_max;
4994 GNUNET_PEER_resolve (queue->peer->id, &id);
4995 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4998 for (i = 0; i < cinfo->send_buffer_n; i++)
5001 i2 = (cinfo->send_buffer_start + i) % max;
5002 if (cinfo->send_buffer[i2] == queue)
5004 /* Found corresponding entry in the send_buffer. Move all others back. */
5009 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
5011 j2 = (cinfo->send_buffer_start + j) % max;
5012 j3 = (cinfo->send_buffer_start + j + 1) % max;
5013 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
5016 cinfo->send_buffer[j3] = NULL;
5017 cinfo->send_buffer_n--;
5022 GNUNET_free (queue);
5027 * @brief Get the next transmittable message from the queue.
5029 * This will be the head, except in the case of being a data packet
5030 * not allowed by the destination peer.
5032 * @param peer Destination peer.
5034 * @return The next viable MeshPeerQueue element to send to that peer.
5035 * NULL when there are no transmittable messages.
5037 struct MeshPeerQueue *
5038 queue_get_next (const struct MeshPeerInfo *peer)
5040 struct MeshPeerQueue *q;
5041 struct MeshTunnel *t;
5042 struct MeshTransmissionDescriptor *info;
5043 struct MeshTunnelChildInfo *cinfo;
5044 struct GNUNET_MESH_Unicast *ucast;
5045 struct GNUNET_MESH_ToOrigin *to_orig;
5046 struct GNUNET_MESH_Multicast *mcast;
5047 struct GNUNET_PeerIdentity id;
5051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
5052 for (q = peer->queue_head; NULL != q; q = q->next)
5056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5058 GNUNET_MESH_DEBUG_M2S(q->type));
5061 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5062 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
5063 pid = ntohl (ucast->pid);
5064 GNUNET_PEER_resolve (info->peer->id, &id);
5065 cinfo = tunnel_get_neighbor_fc(t, &id);
5066 ack = cinfo->fwd_ack;
5068 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5069 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
5070 pid = ntohl (to_orig->pid);
5073 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5074 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
5075 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
5077 // Not a multicast payload: multicast control traffic (destroy, etc)
5080 pid = ntohl (mcast->pid);
5081 GNUNET_PEER_resolve (info->peer->id, &id);
5082 cinfo = tunnel_get_neighbor_fc(t, &id);
5083 ack = cinfo->fwd_ack;
5086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5091 "********* ACK: %u, PID: %u\n",
5093 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
5095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5102 "********* NEXT!\n");
5105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5106 "********* nothing found\n");
5112 * Core callback to write a queued packet to core buffer
5114 * @param cls Closure (peer info).
5115 * @param size Number of bytes available in buf.
5116 * @param buf Where the to write the message.
5118 * @return number of bytes written to buf
5121 queue_send (void *cls, size_t size, void *buf)
5123 struct MeshPeerInfo *peer = cls;
5124 struct GNUNET_MessageHeader *msg;
5125 struct MeshPeerQueue *queue;
5126 struct MeshTunnel *t;
5127 struct MeshTunnelChildInfo *cinfo;
5128 struct GNUNET_PeerIdentity dst_id;
5131 peer->core_transmit = NULL;
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5135 queue = queue_get_next (peer);
5137 /* Queue has no internal mesh traffic nor sendable payload */
5140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5141 if (NULL == peer->queue_head)
5142 GNUNET_break (0); // Should've been canceled
5145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5147 GNUNET_PEER_resolve (peer->id, &dst_id);
5148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5149 "********* towards %s\n",
5150 GNUNET_i2s(&dst_id));
5151 /* Check if buffer size is enough for the message */
5152 if (queue->size > size)
5154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5155 "********* not enough room, reissue\n");
5156 peer->core_transmit =
5157 GNUNET_CORE_notify_transmit_ready (core_handle,
5160 GNUNET_TIME_UNIT_FOREVER_REL,
5167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5170 GNUNET_assert (0 < t->pending_messages);
5171 t->pending_messages--;
5172 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5176 "********* unicast: t->q (%u/%u)\n",
5177 t->fwd_queue_n, t->fwd_queue_max);
5179 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5186 switch (queue->type)
5189 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5190 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5191 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5192 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5193 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5195 "********* raw: %s\n",
5196 GNUNET_MESH_DEBUG_M2S (queue->type));
5198 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5199 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5200 data_size = send_core_data_raw (queue->cls, size, buf);
5201 msg = (struct GNUNET_MessageHeader *) buf;
5202 switch (ntohs (msg->type)) // Type of preconstructed message
5204 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5205 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5207 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5208 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5214 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5217 struct MeshTransmissionDescriptor *info = queue->cls;
5219 if ((1 == info->mesh_data->reference_counter
5220 && GNUNET_YES == t->speed_min)
5222 (info->mesh_data->total_out == info->mesh_data->reference_counter
5223 && GNUNET_NO == t->speed_min))
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5226 "********* considered sent\n");
5231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5232 "********* NOT considered sent yet\n");
5233 t->pending_messages++;
5236 data_size = send_core_data_multicast(queue->cls, size, buf);
5237 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5239 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5241 data_size = send_core_path_create (queue->cls, size, buf);
5243 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5245 data_size = send_core_path_ack (queue->cls, size, buf);
5247 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5249 data_size = send_core_data_multicast (queue->cls, size, buf);
5253 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5254 "********* type unknown: %u\n",
5258 switch (queue->type)
5260 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5261 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5262 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5263 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5264 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5268 "at pos %u (%p) != %p\n",
5269 cinfo->send_buffer_start,
5270 cinfo->send_buffer[cinfo->send_buffer_start],
5273 if (cinfo->send_buffer_n > 0)
5275 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5276 cinfo->send_buffer_n--;
5277 cinfo->send_buffer_start++;
5278 cinfo->send_buffer_start %= t->fwd_queue_max;
5289 /* Free queue, but cls was freed by send_core_* */
5290 queue_destroy (queue, GNUNET_NO);
5292 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5298 /* If more data in queue, send next */
5299 queue = queue_get_next(peer);
5302 struct GNUNET_PeerIdentity id;
5304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5305 GNUNET_PEER_resolve (peer->id, &id);
5306 peer->core_transmit =
5307 GNUNET_CORE_notify_transmit_ready(core_handle,
5310 GNUNET_TIME_UNIT_FOREVER_REL,
5318 if (NULL != peer->queue_head)
5320 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5321 "********* %s stalled\n",
5322 GNUNET_i2s(&my_full_id));
5324 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5325 // FIXME unify bck/fwd structures, bck does not have cinfo right now
5326 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5328 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
5329 &tunnel_poll, cinfo);
5333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5339 * @brief Queue and pass message to core when possible.
5341 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5342 * and accounts for it. In case the queue is full, the message is dropped and
5345 * Otherwise, message is treated as internal and allowed to go regardless of
5348 * @param cls Closure (@c type dependant). It will be used by queue_send to
5349 * build the message to be sent if not already prebuilt.
5350 * @param type Type of the message, 0 for a raw message.
5351 * @param size Size of the message.
5352 * @param dst Neighbor to send message to.
5353 * @param t Tunnel this message belongs to.
5356 queue_add (void *cls, uint16_t type, size_t size,
5357 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5359 struct MeshPeerQueue *queue;
5360 struct MeshTunnelChildInfo *cinfo;
5361 struct GNUNET_PeerIdentity id;
5367 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5368 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5370 n = &t->fwd_queue_n;
5371 max = &t->fwd_queue_max;
5373 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5375 n = &t->bck_queue_n;
5376 max = &t->bck_queue_max;
5383 GNUNET_STATISTICS_update(stats,
5384 "# messages dropped (buffer full)",
5386 return; // Drop message
5390 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5396 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5397 GNUNET_PEER_resolve (dst->id, &id);
5398 if (NULL == dst->core_transmit)
5400 dst->core_transmit =
5401 GNUNET_CORE_notify_transmit_ready (core_handle,
5404 GNUNET_TIME_UNIT_FOREVER_REL,
5410 t->pending_messages++;
5411 if (NULL == n) // Is this internal mesh traffic?
5414 // It's payload, keep track of buffer per peer.
5415 cinfo = tunnel_get_neighbor_fc(t, &id);
5416 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5417 if (NULL != cinfo->send_buffer[i])
5419 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5420 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5421 cinfo->send_buffer_start++;
5422 cinfo->send_buffer_start %= t->fwd_queue_max;
5426 cinfo->send_buffer_n++;
5428 cinfo->send_buffer[i] = queue;
5429 if (cinfo->send_buffer_n > t->fwd_queue_max)
5432 cinfo->send_buffer_n = t->fwd_queue_max;
5437 /******************************************************************************/
5438 /******************** MESH NETWORK HANDLERS **************************/
5439 /******************************************************************************/
5443 * Core handler for path creation
5445 * @param cls closure
5446 * @param message message
5447 * @param peer peer identity this notification is about
5448 * @param atsi performance data
5449 * @param atsi_count number of records in 'atsi'
5451 * @return GNUNET_OK to keep the connection open,
5452 * GNUNET_SYSERR to close it (signal serious error)
5455 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5456 const struct GNUNET_MessageHeader *message,
5457 const struct GNUNET_ATS_Information *atsi,
5458 unsigned int atsi_count)
5460 unsigned int own_pos;
5463 MESH_TunnelNumber tid;
5464 struct GNUNET_MESH_ManipulatePath *msg;
5465 struct GNUNET_PeerIdentity *pi;
5466 struct GNUNET_HashCode hash;
5467 struct MeshPeerPath *path;
5468 struct MeshPeerInfo *dest_peer_info;
5469 struct MeshPeerInfo *orig_peer_info;
5470 struct MeshTunnel *t;
5472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5473 "Received a path create msg [%s]\n",
5474 GNUNET_i2s (&my_full_id));
5475 size = ntohs (message->size);
5476 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5478 GNUNET_break_op (0);
5482 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5483 if (size % sizeof (struct GNUNET_PeerIdentity))
5485 GNUNET_break_op (0);
5488 size /= sizeof (struct GNUNET_PeerIdentity);
5491 GNUNET_break_op (0);
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5495 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5497 tid = ntohl (msg->tid);
5498 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5500 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5501 t = tunnel_get (pi, tid);
5502 if (NULL == t) // FIXME only for INCOMING tunnels?
5506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5507 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5510 // FIXME notify failure
5513 opt = ntohl (msg->opt);
5514 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5515 GNUNET_YES : GNUNET_NO;
5516 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5518 t->nobuffer = GNUNET_YES;
5519 t->last_fwd_ack = t->fwd_pid + 1;
5521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5522 " speed_min: %d, nobuffer:%d\n",
5523 t->speed_min, t->nobuffer);
5525 if (GNUNET_YES == t->nobuffer)
5527 t->bck_queue_max = 1;
5528 t->fwd_queue_max = 1;
5531 // FIXME only assign a local tid if a local client is interested (on demand)
5532 while (NULL != tunnel_get_incoming (next_local_tid))
5533 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5534 t->local_tid_dest = next_local_tid++;
5535 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5538 tunnel_reset_timeout (t);
5539 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5541 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5542 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5550 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5551 if (NULL == dest_peer_info)
5553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5554 " Creating PeerInfo for destination.\n");
5555 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5556 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5557 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5559 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5561 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5562 if (NULL == orig_peer_info)
5564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5565 " Creating PeerInfo for origin.\n");
5566 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5567 orig_peer_info->id = GNUNET_PEER_intern (pi);
5568 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5569 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5572 path = path_new (size);
5574 for (i = 0; i < size; i++)
5576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5577 GNUNET_i2s (&pi[i]));
5578 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5579 if (path->peers[i] == myid)
5582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5585 /* cannot be self, must be 'not found' */
5586 /* create path: self not found in path through self */
5587 GNUNET_break_op (0);
5588 path_destroy (path);
5592 path_add_to_peers (path, GNUNET_NO);
5593 tunnel_add_path (t, path, own_pos);
5594 if (own_pos == size - 1)
5596 /* It is for us! Send ack. */
5597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5598 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5599 if (NULL == t->peers)
5601 /* New tunnel! Notify clients on first payload message. */
5602 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5604 GNUNET_break (GNUNET_SYSERR !=
5605 GNUNET_CONTAINER_multihashmap_put (t->peers,
5606 &my_full_id.hashPubKey,
5609 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5614 struct MeshPeerPath *path2;
5616 /* It's for somebody else! Retransmit. */
5617 path2 = path_duplicate (path);
5618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5619 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5620 path2 = path_duplicate (path);
5621 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5622 send_create_path (dest_peer_info, path, t);
5629 * Core handler for path destruction
5631 * @param cls closure
5632 * @param message message
5633 * @param peer peer identity this notification is about
5634 * @param atsi performance data
5635 * @param atsi_count number of records in 'atsi'
5637 * @return GNUNET_OK to keep the connection open,
5638 * GNUNET_SYSERR to close it (signal serious error)
5641 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5642 const struct GNUNET_MessageHeader *message,
5643 const struct GNUNET_ATS_Information *atsi,
5644 unsigned int atsi_count)
5646 struct GNUNET_MESH_ManipulatePath *msg;
5647 struct GNUNET_PeerIdentity *pi;
5648 struct MeshPeerPath *path;
5649 struct MeshTunnel *t;
5650 unsigned int own_pos;
5654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5655 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5656 size = ntohs (message->size);
5657 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5659 GNUNET_break_op (0);
5663 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5664 if (size % sizeof (struct GNUNET_PeerIdentity))
5666 GNUNET_break_op (0);
5669 size /= sizeof (struct GNUNET_PeerIdentity);
5672 GNUNET_break_op (0);
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5677 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5678 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5680 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5682 t = tunnel_get (pi, ntohl (msg->tid));
5685 /* TODO notify back: we don't know this tunnel */
5686 GNUNET_break_op (0);
5689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5690 path = path_new (size);
5692 for (i = 0; i < size; i++)
5694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5695 GNUNET_i2s (&pi[i]));
5696 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5697 if (path->peers[i] == myid)
5700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5701 if (own_pos < path->length - 1)
5702 send_prebuilt_message (message, &pi[own_pos + 1], t);
5704 send_client_tunnel_disconnect(t, NULL);
5706 tunnel_delete_peer (t, path->peers[path->length - 1]);
5707 path_destroy (path);
5713 * Core handler for notifications of broken paths
5715 * @param cls closure
5716 * @param message message
5717 * @param peer peer identity this notification is about
5718 * @param atsi performance data
5719 * @param atsi_count number of records in 'atsi'
5721 * @return GNUNET_OK to keep the connection open,
5722 * GNUNET_SYSERR to close it (signal serious error)
5725 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5726 const struct GNUNET_MessageHeader *message,
5727 const struct GNUNET_ATS_Information *atsi,
5728 unsigned int atsi_count)
5730 struct GNUNET_MESH_PathBroken *msg;
5731 struct MeshTunnel *t;
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5734 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5735 msg = (struct GNUNET_MESH_PathBroken *) message;
5736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5737 GNUNET_i2s (&msg->peer1));
5738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5739 GNUNET_i2s (&msg->peer2));
5740 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5743 GNUNET_break_op (0);
5746 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5747 GNUNET_PEER_search (&msg->peer2));
5754 * Core handler for tunnel destruction
5756 * @param cls closure
5757 * @param message message
5758 * @param peer peer identity this notification is about
5759 * @param atsi performance data
5760 * @param atsi_count number of records in 'atsi'
5762 * @return GNUNET_OK to keep the connection open,
5763 * GNUNET_SYSERR to close it (signal serious error)
5766 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5767 const struct GNUNET_MessageHeader *message,
5768 const struct GNUNET_ATS_Information *atsi,
5769 unsigned int atsi_count)
5771 struct GNUNET_MESH_TunnelDestroy *msg;
5772 struct MeshTunnel *t;
5774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5775 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5776 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5778 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5779 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5782 /* Probably already got the message from another path,
5783 * destroyed the tunnel and retransmitted to children.
5786 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5789 if (t->id.oid == myid)
5791 GNUNET_break_op (0);
5794 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5796 /* Tunnel was incoming, notify clients */
5797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5798 t->local_tid, t->local_tid_dest);
5799 send_clients_tunnel_destroy (t);
5801 tunnel_send_destroy (t);
5802 t->destroy = GNUNET_YES;
5803 // TODO: add timeout to destroy the tunnel anyway
5809 * Core handler for mesh network traffic going from the origin to a peer
5811 * @param cls closure
5812 * @param peer peer identity this notification is about
5813 * @param message message
5814 * @param atsi performance data
5815 * @param atsi_count number of records in 'atsi'
5816 * @return GNUNET_OK to keep the connection open,
5817 * GNUNET_SYSERR to close it (signal serious error)
5820 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5821 const struct GNUNET_MessageHeader *message,
5822 const struct GNUNET_ATS_Information *atsi,
5823 unsigned int atsi_count)
5825 struct GNUNET_MESH_Unicast *msg;
5826 struct GNUNET_PeerIdentity *neighbor;
5827 struct MeshTunnelChildInfo *cinfo;
5828 struct MeshTunnel *t;
5829 GNUNET_PEER_Id dest_id;
5834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5837 size = ntohs (message->size);
5839 sizeof (struct GNUNET_MESH_Unicast) +
5840 sizeof (struct GNUNET_MessageHeader))
5845 msg = (struct GNUNET_MESH_Unicast *) message;
5846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5847 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5849 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5852 /* TODO notify back: we don't know this tunnel */
5853 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5854 GNUNET_break_op (0);
5857 pid = ntohl (msg->pid);
5858 if (t->fwd_pid == pid)
5860 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5861 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5862 " Already seen pid %u, DROPPING!\n", pid);
5867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5868 " pid %u not seen yet, forwarding\n", pid);
5871 t->skip += (pid - t->fwd_pid) - 1;
5874 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5876 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5877 GNUNET_break_op (0);
5878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5879 "Received PID %u, ACK %u\n",
5880 pid, t->last_fwd_ack);
5884 tunnel_reset_timeout (t);
5885 dest_id = GNUNET_PEER_search (&msg->destination);
5886 if (dest_id == myid)
5888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5889 " it's for us! sending to clients...\n");
5890 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5891 send_subscribed_clients (message, &msg[1].header, t);
5892 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5895 ttl = ntohl (msg->ttl);
5896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5899 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5900 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5901 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5905 " not for us, retransmitting...\n");
5907 neighbor = tree_get_first_hop (t->tree, dest_id);
5908 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5909 cinfo->fwd_pid = pid;
5910 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5913 if (GNUNET_YES == t->nobuffer &&
5914 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5916 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5917 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5918 GNUNET_break_op (0);
5921 send_prebuilt_message (message, neighbor, t);
5922 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5928 * Core handler for mesh network traffic going from the origin to all peers
5930 * @param cls closure
5931 * @param message message
5932 * @param peer peer identity this notification is about
5933 * @param atsi performance data
5934 * @param atsi_count number of records in 'atsi'
5935 * @return GNUNET_OK to keep the connection open,
5936 * GNUNET_SYSERR to close it (signal serious error)
5938 * TODO: Check who we got this from, to validate route.
5941 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5942 const struct GNUNET_MessageHeader *message,
5943 const struct GNUNET_ATS_Information *atsi,
5944 unsigned int atsi_count)
5946 struct GNUNET_MESH_Multicast *msg;
5947 struct MeshTunnel *t;
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5953 size = ntohs (message->size);
5954 if (sizeof (struct GNUNET_MESH_Multicast) +
5955 sizeof (struct GNUNET_MessageHeader) > size)
5957 GNUNET_break_op (0);
5960 msg = (struct GNUNET_MESH_Multicast *) message;
5961 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5965 /* TODO notify that we dont know that tunnel */
5966 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5967 GNUNET_break_op (0);
5970 pid = ntohl (msg->pid);
5971 if (t->fwd_pid == pid)
5973 /* already seen this packet, drop */
5974 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5976 " Already seen pid %u, DROPPING!\n", pid);
5977 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5983 " pid %u not seen yet, forwarding\n", pid);
5985 t->skip += (pid - t->fwd_pid) - 1;
5987 tunnel_reset_timeout (t);
5989 /* Transmit to locally interested clients */
5990 if (NULL != t->peers &&
5991 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5993 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5994 send_subscribed_clients (message, &msg[1].header, t);
5995 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5998 if (ntohl (msg->ttl) == 0)
6000 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
6001 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
6002 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6005 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
6006 tunnel_send_multicast (t, message);
6012 * Core handler for mesh network traffic toward the owner of a tunnel
6014 * @param cls closure
6015 * @param message message
6016 * @param peer peer identity this notification is about
6017 * @param atsi performance data
6018 * @param atsi_count number of records in 'atsi'
6020 * @return GNUNET_OK to keep the connection open,
6021 * GNUNET_SYSERR to close it (signal serious error)
6024 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
6025 const struct GNUNET_MessageHeader *message,
6026 const struct GNUNET_ATS_Information *atsi,
6027 unsigned int atsi_count)
6029 struct GNUNET_MESH_ToOrigin *msg;
6030 struct GNUNET_PeerIdentity id;
6031 struct MeshPeerInfo *peer_info;
6032 struct MeshTunnel *t;
6033 struct MeshTunnelChildInfo *cinfo;
6034 GNUNET_PEER_Id predecessor;
6038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
6040 size = ntohs (message->size);
6041 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
6042 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
6044 GNUNET_break_op (0);
6047 msg = (struct GNUNET_MESH_ToOrigin *) message;
6048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
6049 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
6050 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6051 pid = ntohl (msg->pid);
6055 /* TODO notify that we dont know this tunnel (whom)? */
6056 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
6057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6058 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
6059 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
6063 cinfo = tunnel_get_neighbor_fc(t, peer);
6070 if (cinfo->bck_pid == pid)
6072 /* already seen this packet, drop */
6073 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
6074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6075 " Already seen pid %u, DROPPING!\n", pid);
6076 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6081 " pid %u not seen yet, forwarding\n", pid);
6082 cinfo->bck_pid = pid;
6084 if (NULL != t->owner)
6087 struct GNUNET_MESH_ToOrigin *copy;
6089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6090 " it's for us! sending to clients...\n");
6091 /* TODO signature verification */
6092 memcpy (cbuf, message, size);
6093 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
6094 copy->tid = htonl (t->local_tid);
6096 copy->pid = htonl (t->bck_pid);
6097 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
6098 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
6099 ©->header, GNUNET_NO);
6100 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6104 " not for us, retransmitting...\n");
6106 peer_info = peer_info_get (&msg->oid);
6107 if (NULL == peer_info)
6109 /* unknown origin of tunnel */
6113 predecessor = tree_get_predecessor (t->tree);
6114 if (0 == predecessor)
6116 if (GNUNET_YES == t->destroy)
6118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6119 "to orig received on a dying tunnel %s [%X]\n",
6120 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6123 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6124 "unknown to origin at %s\n",
6125 GNUNET_i2s (&my_full_id));
6126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6130 "for tunnel %s [%X]\n",
6131 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6134 tree_debug (t->tree);
6137 GNUNET_PEER_resolve (predecessor, &id);
6138 send_prebuilt_message (message, &id, t);
6139 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6146 * Core handler for mesh network traffic point-to-point acks.
6148 * @param cls closure
6149 * @param message message
6150 * @param peer peer identity this notification is about
6151 * @param atsi performance data
6152 * @param atsi_count number of records in 'atsi'
6154 * @return GNUNET_OK to keep the connection open,
6155 * GNUNET_SYSERR to close it (signal serious error)
6158 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6159 const struct GNUNET_MessageHeader *message,
6160 const struct GNUNET_ATS_Information *atsi,
6161 unsigned int atsi_count)
6163 struct GNUNET_MESH_ACK *msg;
6164 struct MeshTunnel *t;
6167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6169 msg = (struct GNUNET_MESH_ACK *) message;
6171 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6175 /* TODO notify that we dont know this tunnel (whom)? */
6176 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6179 ack = ntohl (msg->pid);
6180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6182 /* Is this a forward or backward ACK? */
6183 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6185 struct MeshTunnelChildInfo *cinfo;
6188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6189 cinfo = tunnel_get_neighbor_fc (t, peer);
6190 cinfo->fwd_ack = ack;
6191 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6192 tunnel_unlock_fwd_queues (t);
6193 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6195 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6196 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6197 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6204 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6205 tunnel_unlock_bck_queue (t);
6212 * Core handler for mesh network traffic point-to-point ack polls.
6214 * @param cls closure
6215 * @param message message
6216 * @param peer peer identity this notification is about
6217 * @param atsi performance data
6218 * @param atsi_count number of records in 'atsi'
6220 * @return GNUNET_OK to keep the connection open,
6221 * GNUNET_SYSERR to close it (signal serious error)
6224 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6225 const struct GNUNET_MessageHeader *message,
6226 const struct GNUNET_ATS_Information *atsi,
6227 unsigned int atsi_count)
6229 struct GNUNET_MESH_Poll *msg;
6230 struct MeshTunnel *t;
6232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6235 msg = (struct GNUNET_MESH_Poll *) message;
6237 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6241 /* TODO notify that we dont know this tunnel (whom)? */
6242 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6243 GNUNET_break_op (0);
6247 /* Is this a forward or backward ACK? */
6248 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6250 struct MeshTunnelChildInfo *cinfo;
6252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6253 cinfo = tunnel_get_neighbor_fc (t, peer);
6254 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6255 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6260 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6268 * Core handler for path ACKs
6270 * @param cls closure
6271 * @param message message
6272 * @param peer peer identity this notification is about
6273 * @param atsi performance data
6274 * @param atsi_count number of records in 'atsi'
6276 * @return GNUNET_OK to keep the connection open,
6277 * GNUNET_SYSERR to close it (signal serious error)
6280 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6281 const struct GNUNET_MessageHeader *message,
6282 const struct GNUNET_ATS_Information *atsi,
6283 unsigned int atsi_count)
6285 struct GNUNET_MESH_PathACK *msg;
6286 struct GNUNET_PeerIdentity id;
6287 struct MeshPeerInfo *peer_info;
6288 struct MeshPeerPath *p;
6289 struct MeshTunnel *t;
6291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6292 GNUNET_i2s (&my_full_id));
6293 msg = (struct GNUNET_MESH_PathACK *) message;
6294 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6297 /* TODO notify that we don't know the tunnel */
6298 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6300 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6304 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6306 peer_info = peer_info_get (&msg->peer_id);
6307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6308 GNUNET_i2s (&msg->peer_id));
6309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6312 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6315 "connect_by_string completed, stopping search\n");
6316 regex_cancel_search (t->regex_ctx);
6317 t->regex_ctx = NULL;
6320 /* Add paths to peers? */
6321 p = tree_get_path_to_peer (t->tree, peer_info->id);
6324 path_add_to_peers (p, GNUNET_YES);
6332 /* Message for us? */
6333 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6336 if (NULL == t->owner)
6338 GNUNET_break_op (0);
6341 if (NULL != t->dht_get_type)
6343 GNUNET_DHT_get_stop (t->dht_get_type);
6344 t->dht_get_type = NULL;
6346 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6348 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6349 send_client_peer_connected (t, peer_info->id);
6354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6355 " not for us, retransmitting...\n");
6356 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6357 peer_info = peer_info_get (&msg->oid);
6358 if (NULL == peer_info)
6360 /* If we know the tunnel, we should DEFINITELY know the peer */
6364 send_prebuilt_message (message, &id, t);
6370 * Core handler for mesh keepalives.
6372 * @param cls closure
6373 * @param message message
6374 * @param peer peer identity this notification is about
6375 * @param atsi performance data
6376 * @param atsi_count number of records in 'atsi'
6377 * @return GNUNET_OK to keep the connection open,
6378 * GNUNET_SYSERR to close it (signal serious error)
6380 * TODO: Check who we got this from, to validate route.
6383 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6384 const struct GNUNET_MessageHeader *message,
6385 const struct GNUNET_ATS_Information *atsi,
6386 unsigned int atsi_count)
6388 struct GNUNET_MESH_TunnelKeepAlive *msg;
6389 struct MeshTunnel *t;
6391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6394 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6395 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6399 /* TODO notify that we dont know that tunnel */
6400 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6401 GNUNET_break_op (0);
6405 tunnel_reset_timeout (t);
6407 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6408 tunnel_send_multicast (t, message);
6415 * Functions to handle messages from core
6417 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6418 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6419 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6420 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6421 sizeof (struct GNUNET_MESH_PathBroken)},
6422 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6423 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6424 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6425 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6426 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6427 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6428 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6429 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6430 sizeof (struct GNUNET_MESH_ACK)},
6431 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6432 sizeof (struct GNUNET_MESH_Poll)},
6433 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6434 sizeof (struct GNUNET_MESH_PathACK)},
6440 /******************************************************************************/
6441 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6442 /******************************************************************************/
6445 * deregister_app: iterator for removing each application registered by a client
6447 * @param cls closure
6448 * @param key the hash of the application id (used to access the hashmap)
6449 * @param value the value stored at the key (client)
6451 * @return GNUNET_OK on success
6454 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6456 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6457 GNUNET_break (GNUNET_YES ==
6458 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6464 * notify_client_connection_failure: notify a client that the connection to the
6465 * requested remote peer is not possible (for instance, no route found)
6466 * Function called when the socket is ready to queue more data. "buf" will be
6467 * NULL and "size" zero if the socket was closed for writing in the meantime.
6469 * @param cls closure
6470 * @param size number of bytes available in buf
6471 * @param buf where the callee should write the message
6472 * @return number of bytes written to buf
6475 notify_client_connection_failure (void *cls, size_t size, void *buf)
6478 struct MeshPeerInfo *peer_info;
6479 struct GNUNET_MESH_PeerControl *msg;
6480 struct GNUNET_PeerIdentity id;
6482 if (0 == size && NULL == buf)
6484 // TODO retry? cancel?
6488 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6489 peer_info = (struct MeshPeerInfo *) cls;
6490 msg = (struct GNUNET_MESH_PeerControl *) buf;
6491 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6492 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6493 // msg->tunnel_id = htonl(peer_info->t->tid);
6494 GNUNET_PEER_resolve (peer_info->id, &id);
6495 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6503 * Send keepalive packets for a peer
6505 * @param cls Closure (tunnel for which to send the keepalive).
6506 * @param tc Notification context.
6509 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6511 struct MeshTunnel *t = cls;
6512 struct GNUNET_MESH_TunnelKeepAlive *msg;
6513 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6516 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6517 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6523 "sending keepalive for tunnel %d\n", t->id.tid);
6525 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6526 msg->header.size = htons (size);
6527 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6528 msg->oid = my_full_id;
6529 msg->tid = htonl (t->id.tid);
6530 tunnel_send_multicast (t, &msg->header);
6532 t->path_refresh_task =
6533 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6534 tunnel_reset_timeout(t);
6539 * Function to process paths received for a new peer addition. The recorded
6540 * paths form the initial tunnel, which can be optimized later.
6541 * Called on each result obtained for the DHT search.
6543 * @param cls closure
6544 * @param exp when will this value expire
6545 * @param key key of the result
6546 * @param get_path path of the get request
6547 * @param get_path_length lenght of get_path
6548 * @param put_path path of the put request
6549 * @param put_path_length length of the put_path
6550 * @param type type of the result
6551 * @param size number of bytes in data
6552 * @param data pointer to the result data
6554 * TODO: re-issue the request after certain time? cancel after X results?
6557 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6558 const struct GNUNET_HashCode * key,
6559 const struct GNUNET_PeerIdentity *get_path,
6560 unsigned int get_path_length,
6561 const struct GNUNET_PeerIdentity *put_path,
6562 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6563 size_t size, const void *data)
6565 struct MeshPathInfo *path_info = cls;
6566 struct MeshPeerPath *p;
6567 struct GNUNET_PeerIdentity pi;
6570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6571 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6574 p = path_build_from_dht (get_path, get_path_length, put_path,
6576 path_add_to_peers (p, GNUNET_NO);
6578 for (i = 0; i < path_info->peer->ntunnels; i++)
6580 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6581 peer_info_connect (path_info->peer, path_info->t);
6589 * Function to process paths received for a new peer addition. The recorded
6590 * paths form the initial tunnel, which can be optimized later.
6591 * Called on each result obtained for the DHT search.
6593 * @param cls closure
6594 * @param exp when will this value expire
6595 * @param key key of the result
6596 * @param get_path path of the get request
6597 * @param get_path_length lenght of get_path
6598 * @param put_path path of the put request
6599 * @param put_path_length length of the put_path
6600 * @param type type of the result
6601 * @param size number of bytes in data
6602 * @param data pointer to the result data
6605 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6606 const struct GNUNET_HashCode * key,
6607 const struct GNUNET_PeerIdentity *get_path,
6608 unsigned int get_path_length,
6609 const struct GNUNET_PeerIdentity *put_path,
6610 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6611 size_t size, const void *data)
6613 const struct PBlock *pb = data;
6614 const struct GNUNET_PeerIdentity *pi = &pb->id;
6615 struct MeshTunnel *t = cls;
6616 struct MeshPeerInfo *peer_info;
6617 struct MeshPeerPath *p;
6619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6620 if (size != sizeof (struct PBlock))
6622 GNUNET_break_op (0);
6625 if (ntohl(pb->type) != t->type)
6627 GNUNET_break_op (0);
6630 GNUNET_assert (NULL != t->owner);
6631 peer_info = peer_info_get (pi);
6632 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6634 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6636 p = path_build_from_dht (get_path, get_path_length, put_path,
6638 path_add_to_peers (p, GNUNET_NO);
6640 tunnel_add_peer (t, peer_info);
6641 peer_info_connect (peer_info, t);
6646 * Function to process DHT string to regex matching.
6647 * Called on each result obtained for the DHT search.
6649 * @param cls closure (search context)
6650 * @param exp when will this value expire
6651 * @param key key of the result
6652 * @param get_path path of the get request (not used)
6653 * @param get_path_length lenght of get_path (not used)
6654 * @param put_path path of the put request (not used)
6655 * @param put_path_length length of the put_path (not used)
6656 * @param type type of the result
6657 * @param size number of bytes in data
6658 * @param data pointer to the result data
6661 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6662 const struct GNUNET_HashCode * key,
6663 const struct GNUNET_PeerIdentity *get_path,
6664 unsigned int get_path_length,
6665 const struct GNUNET_PeerIdentity *put_path,
6666 unsigned int put_path_length,
6667 enum GNUNET_BLOCK_Type type,
6668 size_t size, const void *data)
6670 const struct MeshRegexAccept *block = data;
6671 struct MeshRegexSearchContext *ctx = cls;
6672 struct MeshRegexSearchInfo *info = ctx->info;
6673 // struct MeshPeerPath *p;
6674 struct MeshPeerInfo *peer_info;
6676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6678 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6680 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6683 peer_info = peer_info_get(&block->id);
6684 // p = path_build_from_dht (get_path, get_path_length, put_path,
6685 // put_path_length);
6686 // path_add_to_peers (p, GNUNET_NO);
6689 tunnel_add_peer (info->t, peer_info);
6690 peer_info_connect (peer_info, info->t);
6691 if (0 == info->peer)
6693 info->peer = peer_info->id;
6697 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6700 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6701 ®ex_connect_timeout,
6709 * Function to process DHT string to regex matching.
6710 * Called on each result obtained for the DHT search.
6712 * @param cls closure (search context)
6713 * @param exp when will this value expire
6714 * @param key key of the result
6715 * @param get_path path of the get request (not used)
6716 * @param get_path_length lenght of get_path (not used)
6717 * @param put_path path of the put request (not used)
6718 * @param put_path_length length of the put_path (not used)
6719 * @param type type of the result
6720 * @param size number of bytes in data
6721 * @param data pointer to the result data
6723 * TODO: re-issue the request after certain time? cancel after X results?
6726 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6727 const struct GNUNET_HashCode * key,
6728 const struct GNUNET_PeerIdentity *get_path,
6729 unsigned int get_path_length,
6730 const struct GNUNET_PeerIdentity *put_path,
6731 unsigned int put_path_length,
6732 enum GNUNET_BLOCK_Type type,
6733 size_t size, const void *data)
6735 const struct MeshRegexBlock *block = data;
6736 struct MeshRegexSearchContext *ctx = cls;
6737 struct MeshRegexSearchInfo *info = ctx->info;
6741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6742 "DHT GET STRING RETURNED RESULTS\n");
6743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6744 " key: %s\n", GNUNET_h2s (key));
6746 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6748 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6751 copy = GNUNET_malloc (size);
6752 memcpy (copy, data, size);
6753 GNUNET_break (GNUNET_OK ==
6754 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6755 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6756 len = ntohl (block->n_proof);
6758 char proof[len + 1];
6760 memcpy (proof, &block[1], len);
6762 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6764 GNUNET_break_op (0);
6768 len = strlen (info->description);
6769 if (len == ctx->position) // String processed
6771 if (GNUNET_YES == ntohl (block->accepting))
6773 regex_find_path(key, ctx);
6777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6778 // FIXME REGEX this block not successful, wait for more? start timeout?
6783 regex_next_edge (block, size, ctx);
6788 /******************************************************************************/
6789 /********************* MESH LOCAL HANDLES **************************/
6790 /******************************************************************************/
6794 * Handler for client disconnection
6796 * @param cls closure
6797 * @param client identification of the client; NULL
6798 * for the last call when the server is destroyed
6801 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6803 struct MeshClient *c;
6804 struct MeshClient *next;
6807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6817 if (c->handle != client)
6819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6825 GNUNET_SERVER_client_drop (c->handle);
6826 c->shutting_down = GNUNET_YES;
6827 GNUNET_assert (NULL != c->own_tunnels);
6828 GNUNET_assert (NULL != c->incoming_tunnels);
6829 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6830 &tunnel_destroy_iterator, c);
6831 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6832 &tunnel_destroy_iterator, c);
6833 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6834 &tunnel_destroy_iterator, c);
6835 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6836 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6837 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6839 /* deregister clients applications */
6840 if (NULL != c->apps)
6842 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6843 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6845 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6846 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6848 GNUNET_SCHEDULER_cancel (announce_applications_task);
6849 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6851 if (NULL != c->types)
6852 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6853 for (i = 0; i < c->n_regex; i++)
6855 GNUNET_free (c->regexes[i].regex);
6856 if (NULL != c->regexes[i].dfa)
6857 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6859 GNUNET_free_non_null (c->regexes);
6860 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6861 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6863 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6866 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6875 * Handler for new clients
6877 * @param cls closure
6878 * @param client identification of the client
6879 * @param message the actual message, which includes messages the client wants
6882 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6883 const struct GNUNET_MessageHeader *message)
6885 struct GNUNET_MESH_ClientConnect *cc_msg;
6886 struct MeshClient *c;
6887 GNUNET_MESH_ApplicationType *a;
6894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6895 /* Check data sanity */
6896 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6897 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6898 ntypes = ntohs (cc_msg->types);
6899 napps = ntohs (cc_msg->applications);
6901 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6904 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6908 /* Create new client structure */
6909 c = GNUNET_malloc (sizeof (struct MeshClient));
6910 c->id = next_client_id++;
6911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6913 GNUNET_SERVER_client_keep (client);
6914 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6917 GNUNET_MESH_ApplicationType at;
6918 struct GNUNET_HashCode hc;
6920 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6921 for (i = 0; i < napps; i++)
6924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6925 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6926 /* store in clients hashmap */
6927 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6928 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6929 /* store in global hashmap, for announcements */
6930 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6931 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6933 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6934 announce_applications_task =
6935 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6941 struct GNUNET_HashCode hc;
6943 t = (uint16_t *) & a[napps];
6944 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6945 for (i = 0; i < ntypes; i++)
6948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6949 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6951 /* store in clients hashmap */
6952 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6953 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6954 /* store in global hashmap */
6955 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6956 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6960 " client has %u+%u subscriptions\n", napps, ntypes);
6962 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6963 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6964 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6965 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6966 GNUNET_SERVER_notification_context_add (nc, client);
6967 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6969 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6975 * Handler for clients announcing available services by a regular expression.
6977 * @param cls closure
6978 * @param client identification of the client
6979 * @param message the actual message, which includes messages the client wants
6982 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6983 const struct GNUNET_MessageHeader *message)
6985 const struct GNUNET_MESH_RegexAnnounce *msg;
6986 struct MeshRegexDescriptor rd;
6987 struct MeshClient *c;
6992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6994 /* Sanity check for client registration */
6995 if (NULL == (c = client_get (client)))
6998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7003 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
7005 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
7006 if (NULL != c->partial_regex)
7008 regex = c->partial_regex;
7009 offset = strlen (c->partial_regex);
7010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7011 " continuation, already have %u bytes\n",
7020 regex = GNUNET_realloc (regex, offset + len + 1);
7021 memcpy (®ex[offset], &msg[1], len);
7022 regex[offset + len] = '\0';
7023 if (0 == ntohs (msg->last))
7025 c->partial_regex = regex;
7026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7027 " not ended, stored %u bytes for later\n",
7029 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7033 rd.compression = ntohs (msg->compression_characters);
7035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
7036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
7037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
7038 GNUNET_array_append (c->regexes, c->n_regex, rd);
7039 c->partial_regex = NULL;
7040 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
7042 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
7048 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
7054 * Handler for requests of new tunnels
7056 * @param cls closure
7057 * @param client identification of the client
7058 * @param message the actual message
7061 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
7062 const struct GNUNET_MessageHeader *message)
7064 struct GNUNET_MESH_TunnelMessage *t_msg;
7065 struct MeshTunnel *t;
7066 struct MeshClient *c;
7067 MESH_TunnelNumber tid;
7069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
7071 /* Sanity check for client registration */
7072 if (NULL == (c = client_get (client)))
7075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7080 /* Message sanity check */
7081 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7084 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7088 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7089 /* Sanity check for tunnel numbering */
7090 tid = ntohl (t_msg->tunnel_id);
7091 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
7094 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7097 /* Sanity check for duplicate tunnel IDs */
7098 if (NULL != tunnel_get_by_local_id (c, tid))
7101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7105 while (NULL != tunnel_get_by_pi (myid, next_tid))
7106 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7107 t = tunnel_new (myid, next_tid++, c, tid);
7110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
7112 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7115 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
7117 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
7118 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
7121 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7127 * Handler for requests of deleting tunnels
7129 * @param cls closure
7130 * @param client identification of the client
7131 * @param message the actual message
7134 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
7135 const struct GNUNET_MessageHeader *message)
7137 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7138 struct MeshClient *c;
7139 struct MeshTunnel *t;
7140 MESH_TunnelNumber tid;
7142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7143 "Got a DESTROY TUNNEL from client!\n");
7145 /* Sanity check for client registration */
7146 if (NULL == (c = client_get (client)))
7149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7154 /* Message sanity check */
7155 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7162 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7164 /* Retrieve tunnel */
7165 tid = ntohl (tunnel_msg->tunnel_id);
7166 t = tunnel_get_by_local_id(c, tid);
7169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7171 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7174 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7176 client_ignore_tunnel (c, t);
7177 tunnel_destroy_empty (t);
7178 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7181 send_client_tunnel_disconnect(t, c);
7182 client_delete_tunnel(c, t);
7184 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7186 tunnel_send_destroy (t);
7187 t->destroy = GNUNET_YES;
7188 // The tunnel will be destroyed when the last message is transmitted.
7189 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7195 * Handler for requests of seeting tunnel's speed.
7197 * @param cls Closure (unused).
7198 * @param client Identification of the client.
7199 * @param message The actual message.
7202 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7203 const struct GNUNET_MessageHeader *message)
7205 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7206 struct MeshClient *c;
7207 struct MeshTunnel *t;
7208 MESH_TunnelNumber tid;
7210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7211 "Got a SPEED request from client!\n");
7213 /* Sanity check for client registration */
7214 if (NULL == (c = client_get (client)))
7217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7223 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7225 /* Retrieve tunnel */
7226 tid = ntohl (tunnel_msg->tunnel_id);
7227 t = tunnel_get_by_local_id(c, tid);
7230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7236 switch (ntohs(message->type))
7238 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7239 t->speed_min = GNUNET_YES;
7241 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7242 t->speed_min = GNUNET_NO;
7247 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7252 * Handler for requests of seeting tunnel's buffering policy.
7254 * @param cls Closure (unused).
7255 * @param client Identification of the client.
7256 * @param message The actual message.
7259 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7260 const struct GNUNET_MessageHeader *message)
7262 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7263 struct MeshClient *c;
7264 struct MeshTunnel *t;
7265 MESH_TunnelNumber tid;
7267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7268 "Got a BUFFER request from client!\n");
7270 /* Sanity check for client registration */
7271 if (NULL == (c = client_get (client)))
7274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7279 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7281 /* Retrieve tunnel */
7282 tid = ntohl (tunnel_msg->tunnel_id);
7283 t = tunnel_get_by_local_id(c, tid);
7286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7292 switch (ntohs(message->type))
7294 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7295 t->nobuffer = GNUNET_NO;
7297 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7298 t->nobuffer = GNUNET_YES;
7304 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7309 * Handler for connection requests to new peers
7311 * @param cls closure
7312 * @param client identification of the client
7313 * @param message the actual message (PeerControl)
7316 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7317 const struct GNUNET_MessageHeader *message)
7319 struct GNUNET_MESH_PeerControl *peer_msg;
7320 struct MeshPeerInfo *peer_info;
7321 struct MeshClient *c;
7322 struct MeshTunnel *t;
7323 MESH_TunnelNumber tid;
7325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7326 /* Sanity check for client registration */
7327 if (NULL == (c = client_get (client)))
7330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7335 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7337 /* Sanity check for message size */
7338 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7341 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7345 /* Tunnel exists? */
7346 tid = ntohl (peer_msg->tunnel_id);
7347 t = tunnel_get_by_local_id (c, tid);
7351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7355 /* Does client own tunnel? */
7356 if (t->owner->handle != client)
7359 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7363 GNUNET_i2s (&peer_msg->peer));
7364 peer_info = peer_info_get (&peer_msg->peer);
7366 tunnel_add_peer (t, peer_info);
7367 peer_info_connect (peer_info, t);
7369 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7375 * Handler for disconnection requests of peers in a tunnel
7377 * @param cls closure
7378 * @param client identification of the client
7379 * @param message the actual message (PeerControl)
7382 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7383 const struct GNUNET_MessageHeader *message)
7385 struct GNUNET_MESH_PeerControl *peer_msg;
7386 struct MeshPeerInfo *peer_info;
7387 struct MeshClient *c;
7388 struct MeshTunnel *t;
7389 MESH_TunnelNumber tid;
7391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7392 /* Sanity check for client registration */
7393 if (NULL == (c = client_get (client)))
7396 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7401 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7403 /* Sanity check for message size */
7404 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7411 /* Tunnel exists? */
7412 tid = ntohl (peer_msg->tunnel_id);
7413 t = tunnel_get_by_local_id (c, tid);
7417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7422 /* Does client own tunnel? */
7423 if (t->owner->handle != client)
7426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7431 GNUNET_i2s (&peer_msg->peer));
7432 /* Is the peer in the tunnel? */
7434 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7435 if (NULL == peer_info)
7438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7442 /* Ok, delete peer from tunnel */
7443 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7444 &peer_msg->peer.hashPubKey);
7446 send_destroy_path (t, peer_info->id);
7447 tunnel_delete_peer (t, peer_info->id);
7448 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7453 * Handler for blacklist requests of peers in a tunnel
7455 * @param cls closure
7456 * @param client identification of the client
7457 * @param message the actual message (PeerControl)
7459 * FIXME implement DHT block bloomfilter
7462 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7463 const struct GNUNET_MessageHeader *message)
7465 struct GNUNET_MESH_PeerControl *peer_msg;
7466 struct MeshClient *c;
7467 struct MeshTunnel *t;
7468 MESH_TunnelNumber tid;
7470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7471 /* Sanity check for client registration */
7472 if (NULL == (c = client_get (client)))
7475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7480 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7482 /* Sanity check for message size */
7483 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7486 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7490 /* Tunnel exists? */
7491 tid = ntohl (peer_msg->tunnel_id);
7492 t = tunnel_get_by_local_id (c, tid);
7496 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7501 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7502 GNUNET_PEER_intern(&peer_msg->peer));
7507 * Handler for unblacklist requests of peers in a tunnel
7509 * @param cls closure
7510 * @param client identification of the client
7511 * @param message the actual message (PeerControl)
7514 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7515 const struct GNUNET_MessageHeader *message)
7517 struct GNUNET_MESH_PeerControl *peer_msg;
7518 struct MeshClient *c;
7519 struct MeshTunnel *t;
7520 MESH_TunnelNumber tid;
7524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7525 /* Sanity check for client registration */
7526 if (NULL == (c = client_get (client)))
7529 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7534 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7536 /* Sanity check for message size */
7537 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7544 /* Tunnel exists? */
7545 tid = ntohl (peer_msg->tunnel_id);
7546 t = tunnel_get_by_local_id (c, tid);
7550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7555 /* if peer is not known, complain */
7556 pid = GNUNET_PEER_search (&peer_msg->peer);
7563 /* search and remove from list */
7564 for (i = 0; i < t->nblacklisted; i++)
7566 if (t->blacklisted[i] == pid)
7568 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7569 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7574 /* if peer hasn't been blacklisted, complain */
7580 * Handler for connection requests to new peers by type
7582 * @param cls closure
7583 * @param client identification of the client
7584 * @param message the actual message (ConnectPeerByType)
7587 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7588 const struct GNUNET_MessageHeader *message)
7590 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7591 struct MeshClient *c;
7592 struct MeshTunnel *t;
7593 struct GNUNET_HashCode hash;
7594 MESH_TunnelNumber tid;
7596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7597 /* Sanity check for client registration */
7598 if (NULL == (c = client_get (client)))
7601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7606 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7608 /* Sanity check for message size */
7609 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7610 ntohs (connect_msg->header.size))
7613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7617 /* Tunnel exists? */
7618 tid = ntohl (connect_msg->tunnel_id);
7619 t = tunnel_get_by_local_id (c, tid);
7623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7627 /* Does client own tunnel? */
7628 if (t->owner->handle != client)
7631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7635 /* Do WE have the service? */
7636 t->type = ntohl (connect_msg->type);
7637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7638 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7639 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7642 /* Yes! Fast forward, add ourselves to the tunnel and send the
7643 * good news to the client, and alert the destination client of
7644 * an incoming tunnel.
7646 * FIXME send a path create to self, avoid code duplication
7648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7649 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7650 peer_info_get (&my_full_id),
7651 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7654 send_client_peer_connected (t, myid);
7655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7656 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7658 t->local_tid_dest = next_local_tid++;
7659 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7660 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7661 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7665 /* Ok, lets find a peer offering the service */
7666 if (NULL != t->dht_get_type)
7668 GNUNET_DHT_get_stop (t->dht_get_type);
7670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7671 GNUNET_h2s (&hash));
7673 GNUNET_DHT_get_start (dht_handle,
7674 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7676 dht_replication_level,
7677 GNUNET_DHT_RO_RECORD_ROUTE |
7678 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7680 &dht_get_type_handler, t);
7682 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7688 * Handler for connection requests to new peers by a string service description.
7690 * @param cls closure
7691 * @param client identification of the client
7692 * @param message the actual message, which includes messages the client wants
7695 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7696 const struct GNUNET_MessageHeader *message)
7698 struct GNUNET_MESH_ConnectPeerByString *msg;
7699 struct MeshRegexSearchContext *ctx;
7700 struct MeshRegexSearchInfo *info;
7701 struct GNUNET_DHT_GetHandle *get_h;
7702 struct GNUNET_HashCode key;
7703 struct MeshTunnel *t;
7704 struct MeshClient *c;
7705 MESH_TunnelNumber tid;
7710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7711 "Connect by string started\n");
7712 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7713 size = htons (message->size);
7715 /* Sanity check for client registration */
7716 if (NULL == (c = client_get (client)))
7719 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7724 /* Message size sanity check */
7725 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7732 /* Tunnel exists? */
7733 tid = ntohl (msg->tunnel_id);
7734 t = tunnel_get_by_local_id (c, tid);
7738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7742 /* Does client own tunnel? */
7743 if (t->owner->handle != client)
7746 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7751 " on tunnel %s [%u]\n",
7752 GNUNET_i2s(&my_full_id),
7755 /* Only one connect_by_string allowed at the same time! */
7756 /* FIXME: allow more, return handle at api level to cancel, document */
7757 if (NULL != t->regex_ctx)
7760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7764 /* Find string itself */
7765 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7766 string = (const char *) &msg[1];
7768 /* Initialize context */
7769 size = GNUNET_REGEX_get_first_key (string, len, &key);
7770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7771 " consumed %u bits out of %u\n", size, len);
7772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7773 " looking for %s\n", GNUNET_h2s (&key));
7775 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7777 info->description = GNUNET_strndup (string, len);
7778 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7779 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7782 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7783 ctx->position = size;
7787 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7789 /* Start search in DHT */
7790 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7791 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7792 &key, /* key to search */
7793 dht_replication_level, /* replication level */
7794 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7795 &info->description[size], /* xquery */
7796 // FIXME add BLOOMFILTER to exclude filtered peers
7797 len + 1 - size, /* xquery bits */
7798 // FIXME add BLOOMFILTER SIZE
7799 &dht_get_string_handler, ctx);
7801 GNUNET_break (GNUNET_OK ==
7802 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7805 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7807 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7813 * Handler for client traffic directed to one peer
7815 * @param cls closure
7816 * @param client identification of the client
7817 * @param message the actual message
7820 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7821 const struct GNUNET_MessageHeader *message)
7823 struct MeshClient *c;
7824 struct MeshTunnel *t;
7825 struct MeshPeerInfo *pi;
7826 struct GNUNET_MESH_Unicast *data_msg;
7827 MESH_TunnelNumber tid;
7830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7831 "Got a unicast request from a client!\n");
7833 /* Sanity check for client registration */
7834 if (NULL == (c = client_get (client)))
7837 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7842 data_msg = (struct GNUNET_MESH_Unicast *) message;
7844 /* Sanity check for message size */
7845 size = ntohs (message->size);
7846 if (sizeof (struct GNUNET_MESH_Unicast) +
7847 sizeof (struct GNUNET_MessageHeader) > size)
7850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7854 /* Tunnel exists? */
7855 tid = ntohl (data_msg->tid);
7856 t = tunnel_get_by_local_id (c, tid);
7860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7864 /* Is it a local tunnel? Then, does client own the tunnel? */
7865 if (t->owner->handle != client)
7868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7872 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7873 &data_msg->destination.hashPubKey);
7874 /* Is the selected peer in the tunnel? */
7878 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7882 /* PID should be as expected */
7883 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7886 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7887 "Unicast PID, expected %u, got %u\n",
7888 t->fwd_pid + 1, ntohl (data_msg->pid));
7889 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7893 /* Ok, everything is correct, send the message
7894 * (pretend we got it from a mesh peer)
7897 /* Work around const limitation */
7898 char buf[ntohs (message->size)] GNUNET_ALIGN;
7899 struct GNUNET_MESH_Unicast *copy;
7901 copy = (struct GNUNET_MESH_Unicast *) buf;
7902 memcpy (buf, data_msg, size);
7903 copy->oid = my_full_id;
7904 copy->tid = htonl (t->id.tid);
7905 copy->ttl = htonl (default_ttl);
7906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7907 " calling generic handler...\n");
7908 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7911 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7918 * Handler for client traffic directed to the origin
7920 * @param cls closure
7921 * @param client identification of the client
7922 * @param message the actual message
7925 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7926 const struct GNUNET_MessageHeader *message)
7928 struct GNUNET_MESH_ToOrigin *data_msg;
7929 struct MeshTunnelClientInfo *clinfo;
7930 struct MeshClient *c;
7931 struct MeshTunnel *t;
7932 MESH_TunnelNumber tid;
7935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7936 "Got a ToOrigin request from a client!\n");
7937 /* Sanity check for client registration */
7938 if (NULL == (c = client_get (client)))
7941 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7946 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7948 /* Sanity check for message size */
7949 size = ntohs (message->size);
7950 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7951 sizeof (struct GNUNET_MessageHeader) > size)
7954 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7958 /* Tunnel exists? */
7959 tid = ntohl (data_msg->tid);
7960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7961 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7967 t = tunnel_get_by_local_id (c, tid);
7970 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7971 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7977 /* It should be sent by someone who has this as incoming tunnel. */
7978 if (GNUNET_NO == client_knows_tunnel (c, t))
7981 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7985 /* PID should be as expected */
7986 clinfo = tunnel_get_client_fc (t, c);
7987 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7990 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7991 "To Origin PID, expected %u, got %u\n",
7992 clinfo->bck_pid + 1,
7993 ntohl (data_msg->pid));
7994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7999 /* Ok, everything is correct, send the message
8000 * (pretend we got it from a mesh peer)
8003 char buf[ntohs (message->size)] GNUNET_ALIGN;
8004 struct GNUNET_MESH_ToOrigin *copy;
8006 /* Work around const limitation */
8007 copy = (struct GNUNET_MESH_ToOrigin *) buf;
8008 memcpy (buf, data_msg, size);
8009 GNUNET_PEER_resolve (t->id.oid, ©->oid);
8010 copy->tid = htonl (t->id.tid);
8011 copy->ttl = htonl (default_ttl);
8012 copy->pid = htonl (t->bck_pid + 1);
8014 copy->sender = my_full_id;
8015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8016 " calling generic handler...\n");
8017 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
8019 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8026 * Handler for client traffic directed to all peers in a tunnel
8028 * @param cls closure
8029 * @param client identification of the client
8030 * @param message the actual message
8033 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
8034 const struct GNUNET_MessageHeader *message)
8036 struct MeshClient *c;
8037 struct MeshTunnel *t;
8038 struct GNUNET_MESH_Multicast *data_msg;
8039 MESH_TunnelNumber tid;
8041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8042 "Got a multicast request from a client!\n");
8044 /* Sanity check for client registration */
8045 if (NULL == (c = client_get (client)))
8048 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8053 data_msg = (struct GNUNET_MESH_Multicast *) message;
8055 /* Sanity check for message size */
8056 if (sizeof (struct GNUNET_MESH_Multicast) +
8057 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
8060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8064 /* Tunnel exists? */
8065 tid = ntohl (data_msg->tid);
8066 t = tunnel_get_by_local_id (c, tid);
8070 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8077 /* Does client own tunnel? */
8078 if (t->owner->handle != client)
8081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8085 /* PID should be as expected */
8086 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
8089 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8090 "Multicast PID, expected %u, got %u\n",
8091 t->fwd_pid + 1, ntohl (data_msg->pid));
8092 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8097 char buf[ntohs (message->size)] GNUNET_ALIGN;
8098 struct GNUNET_MESH_Multicast *copy;
8100 copy = (struct GNUNET_MESH_Multicast *) buf;
8101 memcpy (buf, message, ntohs (message->size));
8102 copy->oid = my_full_id;
8103 copy->tid = htonl (t->id.tid);
8104 copy->ttl = htonl (default_ttl);
8105 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
8106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8107 " calling generic handler...\n");
8108 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
8111 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
8117 * Handler for client's ACKs for payload traffic.
8119 * @param cls Closure (unused).
8120 * @param client Identification of the client.
8121 * @param message The actual message.
8124 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
8125 const struct GNUNET_MessageHeader *message)
8127 struct GNUNET_MESH_LocalAck *msg;
8128 struct MeshTunnel *t;
8129 struct MeshClient *c;
8130 MESH_TunnelNumber tid;
8133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
8134 /* Sanity check for client registration */
8135 if (NULL == (c = client_get (client)))
8138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8143 msg = (struct GNUNET_MESH_LocalAck *) message;
8145 /* Tunnel exists? */
8146 tid = ntohl (msg->tunnel_id);
8147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8148 t = tunnel_get_by_local_id (c, tid);
8152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8153 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8158 ack = ntohl (msg->max_pid);
8159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8161 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8162 if (NULL != t->owner && t->owner->handle == client)
8164 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8166 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8170 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8171 tunnel_set_client_fwd_ack (t, c, ack);
8172 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8175 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8182 * Iterator over all peers to send a monitoring client info about a tunnel.
8184 * @param cls Closure (message being built).
8185 * @param key Key (hashed tunnel ID, unused).
8186 * @param value Peer info.
8188 * @return GNUNET_YES, to keep iterating.
8191 monitor_peers_iterator (void *cls,
8192 const struct GNUNET_HashCode * key,
8195 struct GNUNET_MESH_LocalMonitor *msg = cls;
8196 struct GNUNET_PeerIdentity *id;
8197 struct MeshPeerInfo *info = value;
8199 id = (struct GNUNET_PeerIdentity *) &msg[1];
8200 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8204 "* sending info about peer %s [%u]\n",
8205 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8213 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8215 * @param cls Closure (client handle).
8216 * @param key Key (hashed tunnel ID, unused).
8217 * @param value Tunnel info.
8219 * @return GNUNET_YES, to keep iterating.
8222 monitor_all_tunnels_iterator (void *cls,
8223 const struct GNUNET_HashCode * key,
8226 struct GNUNET_SERVER_Client *client = cls;
8227 struct MeshTunnel *t = value;
8228 struct GNUNET_MESH_LocalMonitor *msg;
8231 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8232 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8233 npeers * sizeof (struct GNUNET_PeerIdentity));
8234 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8235 msg->tunnel_id = htonl (t->id.tid);
8236 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8237 npeers * sizeof (struct GNUNET_PeerIdentity));
8238 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8240 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8241 monitor_peers_iterator,
8244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8245 "* sending info about tunnel %s [%u] (%u peers)\n",
8246 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8248 if (msg->npeers != npeers)
8251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8252 "Get tunnels fail: size %u - iter %u\n",
8253 npeers, msg->npeers);
8256 msg->npeers = htonl (npeers);
8257 GNUNET_SERVER_notification_context_unicast (nc, client,
8258 &msg->header, GNUNET_NO);
8264 * Handler for client's MONITOR request.
8266 * @param cls Closure (unused).
8267 * @param client Identification of the client.
8268 * @param message The actual message.
8271 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8272 const struct GNUNET_MessageHeader *message)
8274 struct MeshClient *c;
8276 /* Sanity check for client registration */
8277 if (NULL == (c = client_get (client)))
8280 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8284 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8285 "Received get tunnels request from client %u\n",
8287 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8288 monitor_all_tunnels_iterator,
8290 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8291 "Get tunnels request from client %u completed\n",
8293 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8298 * Data needed to build a Monitor_Tunnel message.
8300 struct MeshMonitorTunnelContext
8303 * Partial message, including peer count.
8305 struct GNUNET_MESH_LocalMonitor *msg;
8308 * Hashmap with positions: peer->position.
8310 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8313 * Index of the parent of each peer in the message, realtive to the absolute
8314 * order in the array (can be in a previous message).
8316 uint32_t parents[1024];
8319 * Peers visited so far in the tree, aka position of the current peer.
8321 unsigned int npeers;
8324 * Client requesting the info.
8326 struct MeshClient *c;
8331 * Send a client a message about the structure of a tunnel.
8333 * @param ctx Context of the tunnel iteration, with info regarding the state
8334 * of the execution and the number of peers visited for this message.
8337 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8339 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8340 struct GNUNET_PeerIdentity *pid;
8341 unsigned int *parent;
8344 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8345 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8346 resp->header.size = htons (size);
8347 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8348 parent = (unsigned int *) &pid[resp->npeers];
8349 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8350 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8351 &resp->header, GNUNET_NO);
8355 * Iterator over a tunnel tree to build a message containing all peers
8356 * the in the tunnel, including relay nodes.
8358 * @param cls Closure (pointer to pointer of message being built).
8359 * @param peer Short ID of a peer.
8360 * @param parent Short ID of the @c peer 's parent.
8363 tunnel_tree_iterator (void *cls,
8364 GNUNET_PEER_Id peer,
8365 GNUNET_PEER_Id parent)
8367 struct MeshMonitorTunnelContext *ctx = cls;
8368 struct GNUNET_MESH_LocalMonitor *msg;
8369 struct GNUNET_PeerIdentity *pid;
8370 struct GNUNET_PeerIdentity ppid;
8373 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8374 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8375 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8376 &pid[msg->npeers].hashPubKey,
8377 (void *) (long) ctx->npeers,
8378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8379 GNUNET_PEER_resolve (parent, &ppid);
8380 ctx->parents[msg->npeers] =
8381 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8387 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8389 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8392 send_client_tunnel_info (ctx);
8399 * Handler for client's MONITOR_TUNNEL request.
8401 * @param cls Closure (unused).
8402 * @param client Identification of the client.
8403 * @param message The actual message.
8406 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8407 const struct GNUNET_MessageHeader *message)
8409 const struct GNUNET_MESH_LocalMonitor *msg;
8410 struct GNUNET_MESH_LocalMonitor *resp;
8411 struct MeshMonitorTunnelContext ctx;
8412 struct MeshClient *c;
8413 struct MeshTunnel *t;
8415 /* Sanity check for client registration */
8416 if (NULL == (c = client_get (client)))
8419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8423 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8424 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8425 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8428 ntohl (msg->tunnel_id));
8429 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8432 /* We don't know the tunnel */
8433 struct GNUNET_MESH_LocalMonitor warn;
8436 warn.npeers = htonl (UINT_MAX);
8437 GNUNET_SERVER_notification_context_unicast (nc, client,
8440 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8444 /* Initialize context */
8445 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8449 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8453 /* Collect and send information */
8454 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8455 send_client_tunnel_info (&ctx);
8458 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8461 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8462 "Monitor tunnel request from client %u completed\n",
8464 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8469 * Functions to handle messages from clients
8471 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8472 {&handle_local_new_client, NULL,
8473 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8474 {&handle_local_announce_regex, NULL,
8475 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8476 {&handle_local_tunnel_create, NULL,
8477 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8478 sizeof (struct GNUNET_MESH_TunnelMessage)},
8479 {&handle_local_tunnel_destroy, NULL,
8480 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8481 sizeof (struct GNUNET_MESH_TunnelMessage)},
8482 {&handle_local_tunnel_speed, NULL,
8483 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8484 sizeof (struct GNUNET_MESH_TunnelMessage)},
8485 {&handle_local_tunnel_speed, NULL,
8486 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8487 sizeof (struct GNUNET_MESH_TunnelMessage)},
8488 {&handle_local_tunnel_buffer, NULL,
8489 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8490 sizeof (struct GNUNET_MESH_TunnelMessage)},
8491 {&handle_local_tunnel_buffer, NULL,
8492 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8493 sizeof (struct GNUNET_MESH_TunnelMessage)},
8494 {&handle_local_connect_add, NULL,
8495 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8496 sizeof (struct GNUNET_MESH_PeerControl)},
8497 {&handle_local_connect_del, NULL,
8498 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8499 sizeof (struct GNUNET_MESH_PeerControl)},
8500 {&handle_local_blacklist, NULL,
8501 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8502 sizeof (struct GNUNET_MESH_PeerControl)},
8503 {&handle_local_unblacklist, NULL,
8504 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8505 sizeof (struct GNUNET_MESH_PeerControl)},
8506 {&handle_local_connect_by_type, NULL,
8507 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8508 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8509 {&handle_local_connect_by_string, NULL,
8510 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8511 {&handle_local_unicast, NULL,
8512 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8513 {&handle_local_to_origin, NULL,
8514 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8515 {&handle_local_multicast, NULL,
8516 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8517 {&handle_local_ack, NULL,
8518 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8519 sizeof (struct GNUNET_MESH_LocalAck)},
8520 {&handle_local_get_tunnels, NULL,
8521 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8522 sizeof (struct GNUNET_MessageHeader)},
8523 {&handle_local_show_tunnel, NULL,
8524 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8525 sizeof (struct GNUNET_MESH_LocalMonitor)},
8531 * To be called on core init/fail.
8533 * @param cls service closure
8534 * @param server handle to the server for this service
8535 * @param identity the public identity of this peer
8538 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8539 const struct GNUNET_PeerIdentity *identity)
8542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8543 core_handle = server;
8544 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8547 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8550 GNUNET_i2s (identity));
8551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8553 GNUNET_i2s (&my_full_id));
8554 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8556 GNUNET_abort(); // Try harder
8563 * Method called whenever a given peer connects.
8565 * @param cls closure
8566 * @param peer peer identity this notification is about
8567 * @param atsi performance data for the connection
8568 * @param atsi_count number of records in 'atsi'
8571 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8572 const struct GNUNET_ATS_Information *atsi,
8573 unsigned int atsi_count)
8575 struct MeshPeerInfo *peer_info;
8576 struct MeshPeerPath *path;
8578 DEBUG_CONN ("Peer connected\n");
8579 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8580 peer_info = peer_info_get (peer);
8581 if (myid == peer_info->id)
8583 DEBUG_CONN (" (self)\n");
8588 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8590 path = path_new (2);
8591 path->peers[0] = myid;
8592 path->peers[1] = peer_info->id;
8593 GNUNET_PEER_change_rc (myid, 1);
8594 GNUNET_PEER_change_rc (peer_info->id, 1);
8595 peer_info_add_path (peer_info, path, GNUNET_YES);
8596 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8602 * Method called whenever a peer disconnects.
8604 * @param cls closure
8605 * @param peer peer identity this notification is about
8608 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8610 struct MeshPeerInfo *pi;
8611 struct MeshPeerQueue *q;
8612 struct MeshPeerQueue *n;
8614 DEBUG_CONN ("Peer disconnected\n");
8615 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8625 /* TODO try to reroute this traffic instead */
8626 queue_destroy(q, GNUNET_YES);
8629 if (NULL != pi->core_transmit)
8631 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8632 pi->core_transmit = NULL;
8634 peer_info_remove_path (pi, pi->id, myid);
8637 DEBUG_CONN (" (self)\n");
8639 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8644 /******************************************************************************/
8645 /************************ MAIN FUNCTIONS ****************************/
8646 /******************************************************************************/
8649 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8651 * @param cls closure
8652 * @param key current key code
8653 * @param value value in the hash map
8654 * @return GNUNET_YES if we should continue to iterate,
8658 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8660 struct MeshTunnel *t = value;
8667 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8669 * @param cls closure
8670 * @param key current key code
8671 * @param value value in the hash map
8672 * @return GNUNET_YES if we should continue to iterate,
8676 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8678 struct MeshPeerInfo *p = value;
8679 struct MeshPeerQueue *q;
8680 struct MeshPeerQueue *n;
8688 queue_destroy(q, GNUNET_YES);
8692 peer_info_destroy (p);
8698 * Task run during shutdown.
8704 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8708 if (core_handle != NULL)
8710 GNUNET_CORE_disconnect (core_handle);
8715 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8718 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8719 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8720 if (dht_handle != NULL)
8722 GNUNET_DHT_disconnect (dht_handle);
8727 GNUNET_SERVER_notification_context_destroy (nc);
8730 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8732 GNUNET_SCHEDULER_cancel (announce_id_task);
8733 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8735 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8737 GNUNET_SCHEDULER_cancel (announce_applications_task);
8738 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8745 * Callback for hostkey read/generation
8747 * @param cls Closure (Configuration handle).
8748 * @param pk the private key
8749 * @param emsg error message
8752 key_generation_cb (void *cls,
8753 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8756 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8757 struct MeshPeerInfo *peer;
8758 struct MeshPeerPath *p;
8763 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8764 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8766 GNUNET_SCHEDULER_shutdown ();
8769 my_private_key = pk;
8770 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8771 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8772 &my_full_id.hashPubKey);
8773 myid = GNUNET_PEER_intern (&my_full_id);
8774 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8775 "Mesh for peer [%s] starting\n",
8776 GNUNET_i2s(&my_full_id));
8778 // transport_handle = GNUNET_TRANSPORT_connect(c,
8785 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8786 NULL, /* Closure passed to MESH functions */
8787 &core_init, /* Call core_init once connected */
8788 &core_connect, /* Handle connects */
8789 &core_disconnect, /* remove peers on disconnects */
8790 NULL, /* Don't notify about all incoming messages */
8791 GNUNET_NO, /* For header only in notification */
8792 NULL, /* Don't notify about all outbound messages */
8793 GNUNET_NO, /* For header-only out notification */
8794 core_handlers); /* Register these handlers */
8796 if (core_handle == NULL)
8799 GNUNET_SCHEDULER_shutdown ();
8804 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8807 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8808 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8809 GNUNET_SERVER_disconnect_notify (server_handle,
8810 &handle_local_client_disconnect, NULL);
8814 clients_tail = NULL;
8817 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8818 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8820 /* Create a peer_info for the local peer */
8821 peer = peer_info_get (&my_full_id);
8824 GNUNET_PEER_change_rc (myid, 1);
8825 peer_info_add_path (peer, p, GNUNET_YES);
8826 GNUNET_SERVER_resume (server_handle);
8827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8832 * Process mesh requests.
8834 * @param cls closure
8835 * @param server the initialized server
8836 * @param c configuration to use
8839 run (void *cls, struct GNUNET_SERVER_Handle *server,
8840 const struct GNUNET_CONFIGURATION_Handle *c)
8844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8845 server_handle = server;
8848 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8853 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8855 GNUNET_SCHEDULER_shutdown ();
8860 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8861 &refresh_path_time))
8863 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8865 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8866 "mesh", "refresh path time");
8867 GNUNET_SCHEDULER_shutdown ();
8872 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8873 &app_announce_time))
8875 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8877 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8878 "mesh", "app announce time");
8879 GNUNET_SCHEDULER_shutdown ();
8882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8883 "APP_ANNOUNCE_TIME %llu ms\n",
8884 app_announce_time.rel_value);
8886 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8891 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8892 "mesh", "id announce time");
8893 GNUNET_SCHEDULER_shutdown ();
8898 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8903 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8904 "mesh", "connect timeout");
8905 GNUNET_SCHEDULER_shutdown ();
8910 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8915 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8916 "mesh", "max msgs queue");
8917 GNUNET_SCHEDULER_shutdown ();
8922 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8927 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8928 "mesh", "max tunnels");
8929 GNUNET_SCHEDULER_shutdown ();
8934 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8937 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8939 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8940 "mesh", "default ttl", 64);
8945 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8948 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8949 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8950 "mesh", "max peers", 1000);
8955 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8956 &dht_replication_level))
8958 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8960 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8961 "mesh", "dht replication level", 3);
8962 dht_replication_level = 3;
8965 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8966 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8967 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8968 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8969 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8971 dht_handle = GNUNET_DHT_connect (c, 64);
8972 if (NULL == dht_handle)
8976 stats = GNUNET_STATISTICS_create ("mesh", c);
8978 GNUNET_SERVER_suspend (server_handle);
8979 /* Scheduled the task to clean up when shutdown is called */
8980 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8982 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
8985 GNUNET_free (keyfile);
8990 * The main function for the mesh service.
8992 * @param argc number of arguments from the command line
8993 * @param argv command line arguments
8994 * @return 0 ok, 1 on error
8997 main (int argc, char *const *argv)
9002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
9003 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
9005 ret = (GNUNET_OK == r) ? 0 : 1;
9006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
9010 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
9011 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
9012 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);