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 if (NULL == t->owner || GNUNET_YES == t->destroy)
2279 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2280 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2281 pc.tunnel_id = htonl (t->local_tid);
2282 GNUNET_PEER_resolve (id, &pc.peer);
2283 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2289 * Notify all clients (not depending on registration status) that the incoming
2290 * tunnel is no longer valid.
2292 * @param t Tunnel that was destroyed.
2295 send_clients_tunnel_destroy (struct MeshTunnel *t)
2297 struct GNUNET_MESH_TunnelMessage msg;
2299 msg.header.size = htons (sizeof (msg));
2300 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2301 msg.tunnel_id = htonl (t->local_tid_dest);
2302 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2307 * Notify clients of tunnel disconnections, if needed.
2308 * In case the origin disconnects, the destination clients get a tunnel destroy
2309 * notification. If the last destination disconnects (only one remaining client
2310 * in tunnel), the origin gets a (local ID) peer disconnected.
2311 * Note that the function must be called BEFORE removing the client from
2314 * @param t Tunnel that was destroyed.
2315 * @param c Client that disconnected.
2318 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2324 struct GNUNET_MESH_TunnelMessage msg;
2326 msg.header.size = htons (sizeof (msg));
2327 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2328 msg.tunnel_id = htonl (t->local_tid_dest);
2329 for (i = 0; i < t->nclients; i++)
2330 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2331 &msg.header, GNUNET_NO);
2333 // FIXME when to disconnect an incoming tunnel?
2334 else if (1 == t->nclients && NULL != t->owner)
2336 struct GNUNET_MESH_PeerControl msg;
2338 msg.header.size = htons (sizeof (msg));
2339 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2340 msg.tunnel_id = htonl (t->local_tid);
2341 msg.peer = my_full_id;
2342 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2343 &msg.header, GNUNET_NO);
2349 * Iterator over all the peers to remove the oldest not-used entry.
2351 * @param cls Closure (unsued).
2352 * @param key ID of the peer.
2353 * @param value Peer_Info of the peer.
2358 peer_info_timeout (void *cls,
2359 const struct GNUNET_HashCode *key,
2366 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2367 * and insert it in the appropiate structures if the peer is not known yet.
2369 * @param peer Full identity of the peer.
2371 * @return Existing or newly created peer info.
2373 static struct MeshPeerInfo *
2374 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2376 struct MeshPeerInfo *peer_info;
2378 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2379 if (NULL == peer_info)
2382 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2383 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2385 GNUNET_CONTAINER_multihashmap_iterate (peers,
2389 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2390 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2391 peer_info->id = GNUNET_PEER_intern (peer);
2393 peer_info->last_contact = GNUNET_TIME_absolute_get();
2400 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2401 * and insert it in the appropiate structures if the peer is not known yet.
2403 * @param peer Short identity of the peer.
2405 * @return Existing or newly created peer info.
2407 static struct MeshPeerInfo *
2408 peer_info_get_short (const GNUNET_PEER_Id peer)
2410 struct GNUNET_PeerIdentity id;
2412 GNUNET_PEER_resolve (peer, &id);
2413 return peer_info_get (&id);
2418 * Iterator to remove the tunnel from the list of tunnels a peer participates
2421 * @param cls Closure (tunnel info)
2422 * @param key GNUNET_PeerIdentity of the peer (unused)
2423 * @param value PeerInfo of the peer
2425 * @return always GNUNET_YES, to keep iterating
2428 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2430 struct MeshTunnel *t = cls;
2431 struct MeshPeerInfo *peer = value;
2434 for (i = 0; i < peer->ntunnels; i++)
2437 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2440 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2441 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2450 * Core callback to write a pre-constructed data packet to core buffer
2452 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2453 * @param size Number of bytes available in buf.
2454 * @param buf Where the to write the message.
2456 * @return number of bytes written to buf
2459 send_core_data_raw (void *cls, size_t size, void *buf)
2461 struct MeshTransmissionDescriptor *info = cls;
2462 struct GNUNET_MessageHeader *msg;
2465 GNUNET_assert (NULL != info);
2466 GNUNET_assert (NULL != info->mesh_data);
2467 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2468 total_size = ntohs (msg->size);
2470 if (total_size > size)
2475 memcpy (buf, msg, total_size);
2476 data_descriptor_decrement_rc (info->mesh_data);
2483 * Sends an already built non-multicast message to a peer,
2484 * properly registrating all used resources.
2486 * @param message Message to send. Function makes a copy of it.
2487 * @param peer Short ID of the neighbor whom to send the message.
2488 * @param t Tunnel on which this message is transmitted.
2491 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2492 const struct GNUNET_PeerIdentity *peer,
2493 struct MeshTunnel *t)
2495 struct MeshTransmissionDescriptor *info;
2496 struct MeshPeerInfo *neighbor;
2497 struct MeshPeerPath *p;
2501 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2503 size = ntohs (message->size);
2504 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2505 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2506 info->mesh_data->data = GNUNET_malloc (size);
2507 memcpy (info->mesh_data->data, message, size);
2508 type = ntohs(message->type);
2511 struct GNUNET_MESH_Unicast *m;
2512 struct GNUNET_MESH_ToOrigin *to;
2514 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2515 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2516 m->ttl = htonl (ntohl (m->ttl) - 1);
2518 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2519 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2521 to->pid = htonl(t->bck_pid);
2523 info->mesh_data->data_len = size;
2524 info->mesh_data->reference_counter = 1;
2525 info->mesh_data->total_out = 1;
2526 neighbor = peer_info_get (peer);
2527 for (p = neighbor->path_head; NULL != p; p = p->next)
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2538 " %s IS NOT DIRECTLY CONNECTED\n",
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543 for (p = neighbor->path_head; NULL != p; p = p->next)
2545 struct GNUNET_PeerIdentity debug_id;
2548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2549 " path with %u hops through:\n",
2551 for (i = 0; i < p->length; i++)
2553 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2556 i, GNUNET_i2s(&debug_id));
2560 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2562 " no direct connection to %s\n",
2564 GNUNET_free (info->mesh_data->data);
2565 GNUNET_free (info->mesh_data);
2569 info->peer = neighbor;
2570 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2581 * Sends a CREATE PATH message for a path to a peer, properly registrating
2582 * all used resources.
2584 * @param peer PeerInfo of the final peer for whom this path is being created.
2585 * @param p Path itself.
2586 * @param t Tunnel for which the path is created.
2589 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2590 struct MeshTunnel *t)
2592 struct GNUNET_PeerIdentity id;
2593 struct MeshPathInfo *path_info;
2594 struct MeshPeerInfo *neighbor;
2600 p = tree_get_path_to_peer (t->tree, peer->id);
2607 for (i = 0; i < p->length; i++)
2609 if (p->peers[i] == myid)
2612 if (i >= p->length - 1)
2618 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2620 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2621 path_info->path = p;
2623 neighbor = peer_info_get (&id);
2624 path_info->peer = neighbor;
2625 queue_add (path_info,
2626 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2627 sizeof (struct GNUNET_MESH_ManipulatePath) +
2628 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2635 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2637 * @param t Tunnel whose path to destroy.
2638 * @param destination Short ID of the peer to whom the path to destroy.
2641 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2643 struct MeshPeerPath *p;
2646 p = tree_get_path_to_peer (t->tree, destination);
2652 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2653 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2655 struct GNUNET_MESH_ManipulatePath *msg;
2656 struct GNUNET_PeerIdentity *pi;
2660 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2661 msg->header.size = htons (size);
2662 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2663 msg->tid = htonl (t->id.tid);
2664 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2665 for (i = 0; i < p->length; i++)
2667 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2669 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2676 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2678 * @param t Tunnel which to confirm.
2681 send_path_ack (struct MeshTunnel *t)
2683 struct MeshTransmissionDescriptor *info;
2684 struct GNUNET_PeerIdentity id;
2685 GNUNET_PEER_Id peer;
2687 peer = tree_get_predecessor (t->tree);
2688 GNUNET_PEER_resolve (peer, &id);
2689 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2690 info->origin = &t->id;
2691 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2692 GNUNET_assert (NULL != info->peer);
2695 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2696 sizeof (struct GNUNET_MESH_PathACK),
2703 * Try to establish a new connection to this peer.
2704 * Use the best path for the given tunnel.
2705 * If the peer doesn't have any path to it yet, try to get one.
2706 * If the peer already has some path, send a CREATE PATH towards it.
2708 * @param peer PeerInfo of the peer.
2709 * @param t Tunnel for which to create the path, if possible.
2712 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2714 struct MeshPeerPath *p;
2715 struct MeshPathInfo *path_info;
2717 if (NULL != peer->path_head)
2719 p = tree_get_path_to_peer (t->tree, peer->id);
2726 // FIXME always send create path to self
2729 send_create_path (peer, p, t);
2733 struct GNUNET_HashCode hash;
2736 send_client_peer_connected (t, myid);
2737 t->local_tid_dest = next_local_tid++;
2738 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2741 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2742 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2749 else if (NULL == peer->dhtget)
2751 struct GNUNET_PeerIdentity id;
2753 GNUNET_PEER_resolve (peer->id, &id);
2754 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2755 path_info->peer = peer;
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2758 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2759 peer->dhtgetcls = path_info;
2760 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2761 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2762 &id.hashPubKey, /* key to search */
2763 dht_replication_level, /* replication level */
2764 GNUNET_DHT_RO_RECORD_ROUTE |
2765 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2766 NULL, /* xquery */ // FIXME BLOOMFILTER
2767 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2768 &dht_get_id_handler, path_info);
2770 /* Otherwise, there is no path but the DHT get is already started. */
2775 * Task to delay the connection of a peer
2777 * @param cls Closure (path info with tunnel and peer to connect).
2778 * Will be free'd on exection.
2779 * @param tc TaskContext
2782 peer_info_connect_task (void *cls,
2783 const struct GNUNET_SCHEDULER_TaskContext *tc)
2785 struct MeshPathInfo *path_info = cls;
2787 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2789 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2794 peer_info_connect (path_info->peer, path_info->t);
2800 * Destroy the peer_info and free any allocated resources linked to it
2802 * @param pi The peer_info to destroy.
2804 * @return GNUNET_OK on success
2807 peer_info_destroy (struct MeshPeerInfo *pi)
2809 struct GNUNET_PeerIdentity id;
2810 struct MeshPeerPath *p;
2811 struct MeshPeerPath *nextp;
2813 GNUNET_PEER_resolve (pi->id, &id);
2814 GNUNET_PEER_change_rc (pi->id, -1);
2817 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2820 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2821 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2823 if (NULL != pi->dhtget)
2825 GNUNET_DHT_get_stop (pi->dhtget);
2826 GNUNET_free (pi->dhtgetcls);
2832 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2836 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2838 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2846 * Remove all paths that rely on a direct connection between p1 and p2
2847 * from the peer itself and notify all tunnels about it.
2849 * @param peer PeerInfo of affected peer.
2850 * @param p1 GNUNET_PEER_Id of one peer.
2851 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2854 * TODO: optimize (see below)
2857 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2860 struct MeshPeerPath *p;
2861 struct MeshPeerPath *aux;
2862 struct MeshPeerInfo *peer_d;
2864 unsigned int destroyed;
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2871 p = peer->path_head;
2875 for (i = 0; i < (p->length - 1); i++)
2877 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2878 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2880 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2891 for (i = 0; i < peer->ntunnels; i++)
2893 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2897 * Problem: one or more peers have been deleted from the tunnel tree.
2898 * We don't know who they are to try to add them again.
2899 * We need to try to find a new path for each of the disconnected peers.
2900 * Some of them might already have a path to reach them that does not
2901 * involve p1 and p2. Adding all anew might render in a better tree than
2902 * the trivial immediate fix.
2904 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2905 * its children will be reachable trough him.
2907 peer_d = peer_info_get_short (d);
2910 for (p = peer_d->path_head; NULL != p; p = p->next)
2912 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2920 /* No callback, as peer will be already disconnected and a connection
2921 * scheduled by tunnel_notify_connection_broken.
2923 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2927 peer_info_connect (peer_d, peer->tunnels[i]);
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2935 * Add the path to the peer and update the path used to reach it in case this
2938 * @param peer_info Destination peer to add the path to.
2939 * @param path New path to add. Last peer must be the peer in arg 1.
2940 * Path will be either used of freed if already known.
2941 * @param trusted Do we trust that this path is real?
2944 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2947 struct MeshPeerPath *aux;
2951 if ((NULL == peer_info) || (NULL == path))
2954 path_destroy (path);
2957 if (path->peers[path->length - 1] != peer_info->id)
2960 path_destroy (path);
2963 if (path->length <= 2 && GNUNET_NO == trusted)
2965 /* Only allow CORE to tell us about direct paths */
2966 path_destroy (path);
2969 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2970 for (l = 1; l < path->length; l++)
2972 if (path->peers[l] == myid)
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2975 for (l2 = 0; l2 < path->length - l; l2++)
2977 path->peers[l2] = path->peers[l + l2];
2982 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2987 struct GNUNET_PeerIdentity id;
2989 GNUNET_PEER_resolve (peer_info->id, &id);
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2991 path->length, GNUNET_i2s (&id));
2994 l = path_get_length (path);
3001 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
3002 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
3004 l2 = path_get_length (aux);
3007 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
3008 peer_info->path_tail, aux, path);
3013 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
3015 path_destroy (path);
3020 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
3027 * Add the path to the origin peer and update the path used to reach it in case
3028 * this is the shortest.
3029 * The path is given in peer_info -> destination, therefore we turn the path
3030 * upside down first.
3032 * @param peer_info Peer to add the path to, being the origin of the path.
3033 * @param path New path to add after being inversed.
3034 * @param trusted Do we trust that this path is real?
3037 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
3038 struct MeshPeerPath *path, int trusted)
3041 peer_info_add_path (peer_info, path, trusted);
3046 * Function called if the connection to the peer has been stalled for a while,
3047 * possibly due to a missed ACK. Poll the peer about its ACK status.
3049 * @param cls Closure (cinfo).
3050 * @param tc TaskContext.
3053 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3055 struct MeshTunnelChildInfo *cinfo = cls;
3056 struct GNUNET_MESH_Poll msg;
3057 struct GNUNET_PeerIdentity id;
3058 struct MeshTunnel *t;
3060 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3061 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3067 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3068 msg.header.size = htons (sizeof (msg));
3069 msg.tid = htonl (t->id.tid);
3070 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3071 msg.last_ack = htonl (cinfo->fwd_ack);
3073 GNUNET_PEER_resolve (cinfo->id, &id);
3074 send_prebuilt_message (&msg.header, &id, cinfo->t);
3075 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
3077 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
3078 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3079 &tunnel_poll, cinfo);
3084 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3085 * to obtain a local peer -> destination path and interning the peer ids.
3087 * @return Newly allocated and created path
3089 static struct MeshPeerPath *
3090 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3091 unsigned int get_path_length,
3092 const struct GNUNET_PeerIdentity *put_path,
3093 unsigned int put_path_length)
3095 struct MeshPeerPath *p;
3101 GNUNET_PEER_change_rc (myid, 1);
3102 i = get_path_length;
3103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3104 for (i--; i >= 0; i--)
3106 id = GNUNET_PEER_intern (&get_path[i]);
3107 if (p->length > 0 && id == p->peers[p->length - 1])
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3110 GNUNET_PEER_change_rc (id, -1);
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3115 GNUNET_i2s (&get_path[i]));
3117 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3118 p->peers[p->length - 1] = id;
3121 i = put_path_length;
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3123 for (i--; i >= 0; i--)
3125 id = GNUNET_PEER_intern (&put_path[i]);
3128 /* PUT path went through us, so discard the path up until now and start
3129 * from here to get a much shorter (and loop-free) path.
3134 if (p->length > 0 && id == p->peers[p->length - 1])
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3137 GNUNET_PEER_change_rc (id, -1);
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3142 GNUNET_i2s (&put_path[i]));
3144 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3145 p->peers[p->length - 1] = id;
3149 if (get_path_length > 0)
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3151 GNUNET_i2s (&get_path[0]));
3152 if (put_path_length > 0)
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3154 GNUNET_i2s (&put_path[0]));
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3157 for (i = 0; i < p->length; i++)
3159 struct GNUNET_PeerIdentity peer_id;
3161 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3163 GNUNET_i2s (&peer_id));
3171 * Adds a path to the peer_infos of all the peers in the path
3173 * @param p Path to process.
3174 * @param confirmed Whether we know if the path works or not.
3177 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3181 /* TODO: invert and add */
3182 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3183 for (i++; i < p->length; i++)
3185 struct MeshPeerInfo *aux;
3186 struct MeshPeerPath *copy;
3188 aux = peer_info_get_short (p->peers[i]);
3189 copy = path_duplicate (p);
3190 copy->length = i + 1;
3191 peer_info_add_path (aux, copy, GNUNET_NO);
3197 * Send keepalive packets for a peer
3199 * @param cls Closure (tunnel for which to send the keepalive).
3200 * @param tc Notification context.
3202 * TODO: implement explicit multicast keepalive?
3205 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3209 * Search for a tunnel among the incoming tunnels
3211 * @param tid the local id of the tunnel
3213 * @return tunnel handler, NULL if doesn't exist
3215 static struct MeshTunnel *
3216 tunnel_get_incoming (MESH_TunnelNumber tid)
3218 struct GNUNET_HashCode hash;
3220 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3221 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3222 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3227 * Search for a tunnel among the tunnels for a client
3229 * @param c the client whose tunnels to search in
3230 * @param tid the local id of the tunnel
3232 * @return tunnel handler, NULL if doesn't exist
3234 static struct MeshTunnel *
3235 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3237 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3239 return tunnel_get_incoming (tid);
3243 struct GNUNET_HashCode hash;
3245 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3246 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3252 * Search for a tunnel by global ID using PEER_ID
3254 * @param pi owner of the tunnel
3255 * @param tid global tunnel number
3257 * @return tunnel handler, NULL if doesn't exist
3259 static struct MeshTunnel *
3260 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3262 struct MESH_TunnelID id;
3263 struct GNUNET_HashCode hash;
3268 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3269 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3274 * Search for a tunnel by global ID using full PeerIdentities
3276 * @param oid owner of the tunnel
3277 * @param tid global tunnel number
3279 * @return tunnel handler, NULL if doesn't exist
3281 static struct MeshTunnel *
3282 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3284 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3289 * Delete an active client from the tunnel.
3295 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3299 for (i = 0; i < t->nclients; i++)
3301 if (t->clients[i] == c)
3303 t->clients[i] = t->clients[t->nclients - 1];
3304 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3305 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3307 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3315 * Delete an ignored client from the tunnel.
3321 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3325 for (i = 0; i < t->nignore; i++)
3327 if (t->ignore[i] == c)
3329 t->ignore[i] = t->ignore[t->nignore - 1];
3330 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3338 * Delete a client from the tunnel. It should be only done on
3339 * client disconnection, otherwise use client_ignore_tunnel.
3345 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3347 tunnel_delete_ignored_client (t, c);
3348 tunnel_delete_active_client (t, c);
3353 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3355 * Destroys queue elements of all waiting transmissions and frees all memory
3356 * used by the struct and its elements.
3358 * @param cls Closure (tunnel info).
3359 * @param key Hash of GNUNET_PEER_Id (unused).
3360 * @param value MeshTunnelChildInfo of the child.
3362 * @return always GNUNET_YES, to keep iterating
3365 tunnel_destroy_child (void *cls,
3366 const struct GNUNET_HashCode * key,
3369 struct MeshTunnelChildInfo *cinfo = value;
3370 struct MeshTunnel *t = cls;
3371 struct MeshPeerQueue *q;
3375 for (c = 0; c < cinfo->send_buffer_n; c++)
3377 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3378 q = cinfo->send_buffer[i];
3379 cinfo->send_buffer[i] = NULL;
3381 queue_destroy (q, GNUNET_YES);
3384 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3386 GNUNET_free_non_null (cinfo->send_buffer);
3387 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
3389 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
3390 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3392 GNUNET_free (cinfo);
3398 * Callback used to notify a client owner of a tunnel that a peer has
3399 * disconnected, most likely because of a path change.
3401 * @param cls Closure (tunnel this notification is about).
3402 * @param peer_id Short ID of disconnected peer.
3405 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3407 struct MeshTunnel *t = cls;
3408 struct MeshPeerInfo *peer;
3409 struct MeshPathInfo *path_info;
3411 if (NULL != t->owner && NULL != nc)
3413 struct GNUNET_MESH_PeerControl msg;
3415 msg.header.size = htons (sizeof (msg));
3416 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3417 msg.tunnel_id = htonl (t->local_tid);
3418 GNUNET_PEER_resolve (peer_id, &msg.peer);
3419 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3420 &msg.header, GNUNET_NO);
3422 peer = peer_info_get_short (peer_id);
3423 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3424 path_info->peer = peer;
3426 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3432 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3433 * needed rescources.
3434 * If peer already exists, reevaluate shortest path and change if different.
3436 * @param t Tunnel we want to add a new peer to
3437 * @param peer PeerInfo of the peer being added
3441 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3443 struct GNUNET_PeerIdentity id;
3444 struct MeshPeerPath *best_p;
3445 struct MeshPeerPath *p;
3446 unsigned int best_cost;
3449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3450 GNUNET_PEER_resolve (peer->id, &id);
3452 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3455 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3456 GNUNET_assert (GNUNET_OK ==
3457 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3459 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3462 if (NULL != (p = peer->path_head))
3465 best_cost = tree_get_path_cost (t->tree, p);
3468 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3475 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3476 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3477 t->path_refresh_task =
3478 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3482 /* Start a DHT get */
3483 peer_info_connect (peer, t);
3485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3489 * Add a path to a tunnel which we don't own, just to remember the next hop.
3490 * If destination node was already in the tunnel, the first hop information
3491 * will be replaced with the new path.
3493 * @param t Tunnel we want to add a new peer to
3494 * @param p Path to add
3495 * @param own_pos Position of local node in path.
3499 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3500 unsigned int own_pos)
3502 struct GNUNET_PeerIdentity id;
3504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3505 GNUNET_assert (0 != own_pos);
3506 tree_add_path (t->tree, p, NULL, NULL);
3507 if (own_pos < p->length - 1)
3509 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3510 tree_update_first_hops (t->tree, myid, &id);
3512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3516 * Add a client to a tunnel, initializing all needed data structures.
3518 * @param t Tunnel to which add the client.
3519 * @param c Client which to add to the tunnel.
3522 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3524 struct MeshTunnelClientInfo clinfo;
3526 GNUNET_array_append (t->clients, t->nclients, c);
3527 clinfo.fwd_ack = t->fwd_pid + 1;
3528 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3529 clinfo.fwd_pid = t->fwd_pid;
3530 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3532 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3537 * Notifies a tunnel that a connection has broken that affects at least
3538 * some of its peers. Sends a notification towards the root of the tree.
3539 * In case the peer is the owner of the tree, notifies the client that owns
3540 * the tunnel and tries to reconnect.
3542 * @param t Tunnel affected.
3543 * @param p1 Peer that got disconnected from p2.
3544 * @param p2 Peer that got disconnected from p1.
3546 * @return Short ID of the peer disconnected (either p1 or p2).
3547 * 0 if the tunnel remained unaffected.
3549 static GNUNET_PEER_Id
3550 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3556 tree_notify_connection_broken (t->tree, p1, p2,
3557 &tunnel_notify_client_peer_disconnected,
3559 if (myid != p1 && myid != p2)
3565 if (tree_get_predecessor (t->tree) != 0)
3567 /* We are the peer still connected, notify owner of the disconnection. */
3568 struct GNUNET_MESH_PathBroken msg;
3569 struct GNUNET_PeerIdentity neighbor;
3571 msg.header.size = htons (sizeof (msg));
3572 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3573 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3574 msg.tid = htonl (t->id.tid);
3575 msg.peer1 = my_full_id;
3576 GNUNET_PEER_resolve (pid, &msg.peer2);
3577 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3578 send_prebuilt_message (&msg.header, &neighbor, t);
3586 * Send a multicast packet to a neighbor.
3588 * @param cls Closure (Info about the multicast packet)
3589 * @param neighbor_id Short ID of the neighbor to send the packet to.
3592 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3594 struct MeshData *mdata = cls;
3595 struct MeshTransmissionDescriptor *info;
3596 struct GNUNET_PeerIdentity neighbor;
3597 struct GNUNET_MessageHeader *msg;
3599 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3601 info->mesh_data = mdata;
3602 (mdata->reference_counter) ++;
3603 info->destination = neighbor_id;
3604 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3606 GNUNET_i2s (&neighbor));
3607 info->peer = peer_info_get (&neighbor);
3608 GNUNET_assert (NULL != info->peer);
3609 msg = (struct GNUNET_MessageHeader *) mdata->data;
3612 info->mesh_data->data_len,
3619 * Queue a message in a tunnel in multicast, sending a copy to each child node
3620 * down the local one in the tunnel tree.
3622 * @param t Tunnel in which to send the data.
3623 * @param msg Message to be sent.
3626 tunnel_send_multicast (struct MeshTunnel *t,
3627 const struct GNUNET_MessageHeader *msg)
3629 struct MeshData *mdata;
3631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3632 " sending a multicast packet...\n");
3634 mdata = GNUNET_malloc (sizeof (struct MeshData));
3635 mdata->data_len = ntohs (msg->size);
3637 mdata->data = GNUNET_malloc (mdata->data_len);
3638 memcpy (mdata->data, msg, mdata->data_len);
3639 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3641 struct GNUNET_MESH_Multicast *mcast;
3643 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3644 if (t->fwd_queue_n >= t->fwd_queue_max)
3647 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3648 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3649 " message from %s!\n",
3650 GNUNET_i2s(&mcast->oid));
3651 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3652 " message at %s!\n",
3653 GNUNET_i2s(&my_full_id));
3654 GNUNET_free (mdata->data);
3655 GNUNET_free (mdata);
3659 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3661 ntohl (mcast->ttl));
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3668 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3669 if (mdata->reference_counter == 0)
3671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3672 " no one to send data to\n");
3673 GNUNET_free (mdata->data);
3674 GNUNET_free (mdata);
3675 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3680 mdata->total_out = mdata->reference_counter;
3682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3683 " sending a multicast packet done\n");
3689 * Increase the SKIP value of all peers that
3690 * have not received a unicast message.
3692 * @param cls Closure (ID of the peer that HAS received the message).
3693 * @param key ID of the neighbor.
3694 * @param value Information about the neighbor.
3696 * @return GNUNET_YES to keep iterating.
3699 tunnel_add_skip (void *cls,
3700 const struct GNUNET_HashCode * key,
3703 struct GNUNET_PeerIdentity *neighbor = cls;
3704 struct MeshTunnelChildInfo *cinfo = value;
3706 /* TODO compare only pointers? key == neighbor? */
3707 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3717 * @brief Get neighbor's Flow Control information.
3719 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3720 * descendant of the local node in a certain tunnel.
3721 * If the info is not yet there (recently created path), creates the data struct
3722 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3725 * @param t Tunnel related.
3726 * @param peer Neighbor whose Flow Control info is needed.
3728 * @return Neighbor's Flow Control info.
3730 static struct MeshTunnelChildInfo *
3731 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3732 const struct GNUNET_PeerIdentity *peer)
3734 struct MeshTunnelChildInfo *cinfo;
3736 if (NULL == t->children_fc)
3739 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3745 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3746 cinfo->id = GNUNET_PEER_intern (peer);
3747 cinfo->skip = t->fwd_pid;
3750 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3751 cinfo->fwd_ack = t->fwd_pid + delta;
3752 cinfo->bck_ack = delta;
3753 cinfo->bck_pid = -1;
3755 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3756 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3758 cinfo->send_buffer =
3759 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3761 GNUNET_assert (GNUNET_OK ==
3762 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3765 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3772 * Get the Flow Control info of a client.
3774 * @param t Tunnel on which to look.
3775 * @param c Client whose ACK to get.
3777 * @return ACK value.
3779 static struct MeshTunnelClientInfo *
3780 tunnel_get_client_fc (struct MeshTunnel *t,
3781 struct MeshClient *c)
3785 for (i = 0; i < t->nclients; i++)
3787 if (t->clients[i] != c)
3789 return &t->clients_fc[i];
3792 return NULL; // avoid compiler / coverity complaints
3797 * Iterator to get the appropiate ACK value from all children nodes.
3799 * @param cls Closue (tunnel).
3800 * @param id Id of the child node.
3803 tunnel_get_child_fwd_ack (void *cls,
3806 struct GNUNET_PeerIdentity peer_id;
3807 struct MeshTunnelChildInfo *cinfo;
3808 struct MeshTunnelChildIteratorContext *ctx = cls;
3809 struct MeshTunnel *t = ctx->t;
3812 GNUNET_PEER_resolve (id, &peer_id);
3813 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3814 ack = cinfo->fwd_ack;
3817 if (GNUNET_NO == ctx->init)
3819 ctx->max_child_ack = ack;
3820 ctx->init = GNUNET_YES;
3823 if (GNUNET_YES == t->speed_min)
3825 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3829 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3836 * Get the maximum PID allowed to transmit to any
3837 * tunnel child of the local peer, depending on the tunnel
3838 * buffering/speed settings.
3842 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3845 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3847 struct MeshTunnelChildIteratorContext ctx;
3849 ctx.max_child_ack = 0;
3851 ctx.init = GNUNET_NO;
3852 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3854 if (0 == ctx.nchildren)
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3857 " tunnel has no children, no FWD ACK\n");
3861 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3862 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3864 return (int64_t) ctx.max_child_ack;
3869 * Set the FWD ACK value of a client in a particular tunnel.
3871 * @param t Tunnel affected.
3872 * @param c Client whose ACK to set.
3873 * @param ack ACK value.
3876 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3877 struct MeshClient *c,
3882 for (i = 0; i < t->nclients; i++)
3884 if (t->clients[i] != c)
3886 t->clients_fc[i].fwd_ack = ack;
3894 * Get the highest ACK value of all clients in a particular tunnel,
3895 * according to the buffering/speed settings.
3897 * @param t Tunnel on which to look.
3899 * @return Corresponding ACK value (max uint32_t).
3900 * If no clients are suscribed, -1LL.
3903 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3908 if (0 == t->nclients)
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3911 " tunnel has no clients, no FWD ACK\n");
3915 for (ack = -1LL, i = 0; i < t->nclients; i++)
3918 (GNUNET_YES == t->speed_min &&
3919 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3920 (GNUNET_NO == t->speed_min &&
3921 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3923 ack = t->clients_fc[i].fwd_ack;
3927 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3928 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3930 return (uint32_t) ack;
3935 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3936 * mode and the status of all children nodes.
3940 * @return Maximum PID allowed.
3943 tunnel_get_fwd_ack (struct MeshTunnel *t)
3947 uint32_t buffer_free;
3951 count = t->fwd_pid - t->skip;
3952 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3953 child_ack = tunnel_get_children_fwd_ack (t);
3954 client_ack = tunnel_get_clients_fwd_ack (t);
3955 if (GNUNET_YES == t->nobuffer)
3958 if (-1LL == child_ack)
3959 child_ack = client_ack;
3960 if (-1LL == child_ack)
3963 client_ack = child_ack = ack;
3968 ack = count + buffer_free; // Overflow? OK!
3970 if (-1LL == child_ack)
3972 // Node has no children, child_ack AND core buffer are irrelevant.
3973 if (-1LL == client_ack) // No children AND no clients? Not good!
3975 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3979 return (uint32_t) client_ack;
3981 if (-1LL == client_ack)
3985 if (GNUNET_YES == t->speed_min)
3987 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3988 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3992 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3993 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3996 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3997 count, buffer_free, child_ack, client_ack, ack);
4003 * Build a local ACK message and send it to a local client.
4005 * @param t Tunnel on which to send the ACK.
4006 * @param c Client to whom send the ACK.
4007 * @param ack Value of the ACK.
4010 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
4012 struct GNUNET_MESH_LocalAck msg;
4014 msg.header.size = htons (sizeof (msg));
4015 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4016 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
4017 msg.max_pid = htonl (ack);
4018 GNUNET_SERVER_notification_context_unicast(nc,
4025 * Build an ACK message and queue it to send to the given peer.
4027 * @param t Tunnel on which to send the ACK.
4028 * @param peer Peer to whom send the ACK.
4029 * @param ack Value of the ACK.
4032 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
4034 struct GNUNET_MESH_ACK msg;
4036 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4037 msg.header.size = htons (sizeof (msg));
4038 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
4039 msg.pid = htonl (ack);
4040 msg.tid = htonl (t->id.tid);
4042 send_prebuilt_message (&msg.header, peer, t);
4047 * Notify a the owner of a tunnel about how many more
4048 * payload packages will we accept on a given tunnel.
4050 * @param t Tunnel on which to send the ACK.
4053 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4058 "Sending client FWD ACK on tunnel %X\n",
4061 ack = tunnel_get_fwd_ack (t);
4063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4064 if (t->last_fwd_ack == ack)
4066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
4070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
4071 t->last_fwd_ack = ack;
4072 send_local_ack (t, t->owner, ack);
4077 * Send an ACK informing the predecessor about the available buffer space.
4078 * In case there is no predecessor, inform the owning client.
4079 * If buffering is off, send only on behalf of children or self if endpoint.
4080 * If buffering is on, send when sent to children and buffer space is free.
4081 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4082 * the ACK itself goes "back" (towards root).
4084 * @param t Tunnel on which to send the ACK.
4085 * @param type Type of message that triggered the ACK transmission.
4088 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4090 struct GNUNET_PeerIdentity id;
4093 if (NULL != t->owner)
4095 tunnel_send_client_fwd_ack (t);
4098 /* Is it after unicast / multicast retransmission? */
4101 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4102 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4104 "ACK due to FWD DATA retransmission\n");
4105 if (GNUNET_YES == t->nobuffer)
4107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4111 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4112 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4114 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4115 t->force_ack = GNUNET_YES;
4121 /* Check if we need to transmit the ACK */
4122 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4123 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4124 GNUNET_NO == t->force_ack)
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4128 " t->qmax: %u, t->qn: %u\n",
4129 t->fwd_queue_max, t->fwd_queue_n);
4130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4131 " t->pid: %u, t->ack: %u\n",
4132 t->fwd_pid, t->last_fwd_ack);
4136 /* Ok, ACK might be necessary, what PID to ACK? */
4137 ack = tunnel_get_fwd_ack (t);
4139 /* If speed_min and not all children have ack'd, dont send yet */
4140 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4146 t->last_fwd_ack = ack;
4147 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4148 send_ack (t, &id, ack);
4150 t->force_ack = GNUNET_NO;
4155 * Iterator to send a child node a BCK ACK to allow him to send more
4158 * @param cls Closure (tunnel).
4159 * @param id Id of the child node.
4162 tunnel_send_child_bck_ack (void *cls,
4165 struct MeshTunnel *t = cls;
4166 struct MeshTunnelChildInfo *cinfo;
4167 struct GNUNET_PeerIdentity peer;
4170 GNUNET_PEER_resolve (id, &peer);
4171 cinfo = tunnel_get_neighbor_fc (t, &peer);
4172 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4174 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4177 " Not sending ACK, not needed\n");
4180 cinfo->bck_ack = ack;
4182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4183 " Sending BCK ACK %u (last sent: %u)\n",
4184 ack, cinfo->bck_ack);
4185 send_ack (t, &peer, ack);
4190 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4192 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4194 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4195 * - implement a size 1 buffer in each client_fc AND children_fc
4196 * to hold at least 1 message per "child".
4197 * problem: violates no buffer policy
4198 * - ack 0 and make "children" poll for transmission slots
4199 * problem: big overhead, extra latency even in low traffic
4202 * @param t Tunnel on which to send the BCK ACKs.
4205 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4208 unsigned int tunnel_delta;
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4212 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4215 /* Find client whom to allow to send to origin (with lowest buffer space) */
4216 for (i = 0; i < t->nclients; i++)
4218 struct MeshTunnelClientInfo *clinfo;
4221 clinfo = &t->clients_fc[i];
4222 delta = clinfo->bck_ack - clinfo->bck_pid;
4223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4224 t->clients[i]->id, delta);
4226 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4227 (GNUNET_YES == t->nobuffer && 0 == delta))
4231 ack = clinfo->bck_pid;
4232 ack += t->nobuffer ? 1 : tunnel_delta;
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4234 " sending ack to client %u: %u\n",
4235 t->clients[i]->id, ack);
4236 send_local_ack (t, t->clients[i], ack);
4237 clinfo->bck_ack = ack;
4241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4242 " not sending ack to client %u (td %u, d %u)\n",
4243 t->clients[i]->id, tunnel_delta, delta);
4250 * Send an ACK informing the children nodes and destination clients about
4251 * the available buffer space.
4252 * If buffering is off, send only on behalf of root (can be self).
4253 * If buffering is on, send when sent to predecessor and buffer space is free.
4254 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4255 * the ACK itself goes "forward" (towards children/clients).
4257 * @param t Tunnel on which to send the ACK.
4258 * @param type Type of message that triggered the ACK transmission.
4261 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4264 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4265 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4266 /* Is it after data to_origin retransmission? */
4269 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4270 if (GNUNET_YES == t->nobuffer)
4272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4273 " Not sending ACK, nobuffer\n");
4277 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4278 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4280 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4281 t->force_ack = GNUNET_YES;
4287 tunnel_send_clients_bck_ack (t);
4288 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4289 t->force_ack = GNUNET_NO;
4294 * @brief Re-initiate traffic to this peer if necessary.
4296 * Check if there is traffic queued towards this peer
4297 * and the core transmit handle is NULL (traffic was stalled).
4298 * If so, call core tmt rdy.
4300 * @param cls Closure (unused)
4301 * @param peer_id Short ID of peer to which initiate traffic.
4304 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4306 struct MeshPeerInfo *peer;
4307 struct GNUNET_PeerIdentity id;
4308 struct MeshPeerQueue *q;
4311 peer = peer_info_get_short(peer_id);
4312 if (NULL != peer->core_transmit)
4315 q = queue_get_next(peer);
4318 /* Might br multicast traffic already sent to this particular peer but
4319 * not to other children in this tunnel.
4320 * This way t->queue_n would be > 0 but the queue of this particular peer
4326 GNUNET_PEER_resolve (peer->id, &id);
4327 peer->core_transmit =
4328 GNUNET_CORE_notify_transmit_ready(core_handle,
4331 GNUNET_TIME_UNIT_FOREVER_REL,
4341 * @brief Allow transmission of FWD traffic on this tunnel
4343 * Check if there is traffic queued towards any children
4344 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4346 * @param t Tunnel on which to unlock FWD traffic.
4349 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4351 if (0 == t->fwd_queue_n)
4354 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4359 * @brief Allow transmission of BCK traffic on this tunnel
4361 * Check if there is traffic queued towards the root of the tree
4362 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4364 * @param t Tunnel on which to unlock BCK traffic.
4367 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4369 if (0 == t->bck_queue_n)
4372 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4377 * Send a message to all peers in this tunnel that the tunnel is no longer
4380 * @param t The tunnel whose peers to notify.
4381 * @param send_back Do we need to notify our parent node?
4384 tunnel_send_destroy (struct MeshTunnel *t, int send_back)
4386 struct GNUNET_MESH_TunnelDestroy msg;
4387 struct GNUNET_PeerIdentity id;
4388 GNUNET_PEER_Id parent;
4390 if (tree_count_children(t->tree) > 0)
4392 msg.header.size = htons (sizeof (msg));
4393 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4394 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4395 msg.tid = htonl (t->id.tid);
4396 tunnel_send_multicast (t, &msg.header);
4398 parent = tree_get_predecessor(t->tree);
4399 if (GNUNET_NO == send_back || 0 == parent)
4402 msg.header.size = htons (sizeof (msg));
4403 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4404 GNUNET_PEER_resolve (parent, &id);
4405 send_prebuilt_message(&msg.header, &id, t);
4410 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4412 * @param cls Closure (Tunnel which to cancel).
4413 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4416 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4418 struct MeshTunnel *t = cls;
4419 struct MeshPeerInfo *peer_info;
4420 struct MeshPeerQueue *pq;
4421 struct MeshPeerQueue *next;
4423 peer_info = peer_info_get_short (neighbor_id);
4424 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4427 if (pq->tunnel == t)
4429 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4430 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4431 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4433 // Should have been removed on destroy children
4436 queue_destroy (pq, GNUNET_YES);
4439 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4441 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4442 peer_info->core_transmit = NULL;
4447 * Destroy the tunnel and free any allocated resources linked to it.
4449 * @param t the tunnel to destroy
4451 * @return GNUNET_OK on success
4454 tunnel_destroy (struct MeshTunnel *t)
4456 struct MeshClient *c;
4457 struct GNUNET_HashCode hash;
4468 struct GNUNET_PeerIdentity id;
4470 GNUNET_PEER_resolve (t->id.oid, &id);
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4472 GNUNET_i2s (&id), t->id.tid);
4474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4478 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4479 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4487 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4489 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4496 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4497 for (i = 0; i < t->nclients; i++)
4501 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4507 for (i = 0; i < t->nignore; i++)
4511 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4518 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
4519 GNUNET_free_non_null (t->clients);
4520 GNUNET_free_non_null (t->clients_fc);
4522 if (NULL != t->peers)
4524 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4526 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4529 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4530 &tunnel_destroy_child,
4532 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4533 t->children_fc = NULL;
4535 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4536 tree_destroy (t->tree);
4538 if (NULL != t->regex_ctx)
4539 regex_cancel_search (t->regex_ctx);
4540 if (NULL != t->dht_get_type)
4541 GNUNET_DHT_get_stop (t->dht_get_type);
4542 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4543 GNUNET_SCHEDULER_cancel (t->timeout_task);
4544 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4545 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4548 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4553 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
4556 * Tunnel is empty: destroy it.
4558 * @param cls Closure (Tunnel).
4559 * @param tc TaskContext.
4562 tunnel_destroy_empty_delayed (void *cls,
4563 const struct GNUNET_SCHEDULER_TaskContext *tc)
4565 struct MeshTunnel *t = cls;
4567 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
4568 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4571 if (0 != t->nclients ||
4572 0 != tree_count_children (t->tree))
4577 struct GNUNET_PeerIdentity id;
4579 GNUNET_PEER_resolve (t->id.oid, &id);
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4581 "executing destruction of empty tunnel %s [%X]\n",
4582 GNUNET_i2s (&id), t->id.tid);
4586 tunnel_send_destroy (t, GNUNET_YES);
4592 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
4594 * @param t Tunnel to destroy if empty.
4597 tunnel_destroy_empty (struct MeshTunnel *t)
4599 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
4601 0 != tree_count_children (t->tree))
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4605 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
4611 struct GNUNET_PeerIdentity id;
4613 GNUNET_PEER_resolve (t->id.oid, &id);
4614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4615 "scheduling destruction of empty tunnel %s [%X]\n",
4616 GNUNET_i2s (&id), t->id.tid);
4620 t->delayed_destroy =
4621 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
4622 &tunnel_destroy_empty_delayed,
4628 * Create a new tunnel
4630 * @param owner Who is the owner of the tunnel (short ID).
4631 * @param tid Tunnel Number of the tunnel.
4632 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4633 * @param local Tunnel Number for the tunnel, for the client point of view.
4635 * @return A new initialized tunnel. NULL on error.
4637 static struct MeshTunnel *
4638 tunnel_new (GNUNET_PEER_Id owner,
4639 MESH_TunnelNumber tid,
4640 struct MeshClient *client,
4641 MESH_TunnelNumber local)
4643 struct MeshTunnel *t;
4644 struct GNUNET_HashCode hash;
4646 if (n_tunnels >= max_tunnels && NULL == client)
4649 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4652 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4653 t->bck_queue_max = t->fwd_queue_max;
4654 t->tree = tree_new (owner);
4656 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4657 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4658 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4659 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4660 t->local_tid = local;
4661 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4663 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4665 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4667 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4675 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4682 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4684 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4689 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4699 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4700 * that are no longer needed, as well as nodes of no longer reachable peers.
4701 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4703 * @param t Tunnel from which to remove the path.
4704 * @param peer Short id of the peer which should be removed.
4707 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4711 r = tree_del_peer (t->tree, peer, NULL, NULL);
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4715 "Tunnel %u [%u] has no more nodes\n",
4716 t->id.oid, t->id.tid);
4722 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4723 * client when the client disconnects. If the client is not the owner, the
4724 * owner will get notified if no more clients are in the tunnel and the client
4725 * get removed from the tunnel's list.
4727 * @param cls closure (client that is disconnecting)
4728 * @param key the hash of the local tunnel id (used to access the hashmap)
4729 * @param value the value stored at the key (tunnel to destroy)
4731 * @return GNUNET_OK, keep iterating.
4734 tunnel_destroy_iterator (void *cls,
4735 const struct GNUNET_HashCode * key,
4738 struct MeshTunnel *t = value;
4739 struct MeshClient *c = cls;
4741 send_client_tunnel_disconnect (t, c);
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
4745 tunnel_delete_client (t, c);
4746 client_delete_tunnel (c, t);
4747 tunnel_destroy_empty (t);
4750 tunnel_send_destroy (t, GNUNET_YES);
4752 t->destroy = GNUNET_YES;
4759 * Timeout function, destroys tunnel if called
4761 * @param cls Closure (tunnel to destroy).
4762 * @param tc TaskContext
4765 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4767 struct MeshTunnel *t = cls;
4768 struct GNUNET_PeerIdentity id;
4770 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4771 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4773 GNUNET_PEER_resolve(t->id.oid, &id);
4774 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4775 "Tunnel %s [%X] timed out. Destroying.\n",
4776 GNUNET_i2s(&id), t->id.tid);
4777 send_clients_tunnel_destroy (t);
4782 * Resets the tunnel timeout. Starts it if no timeout was running.
4784 * @param t Tunnel whose timeout to reset.
4786 * TODO use heap to improve efficiency of scheduler.
4789 tunnel_reset_timeout (struct MeshTunnel *t)
4791 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4792 GNUNET_SCHEDULER_cancel (t->timeout_task);
4794 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4795 (refresh_path_time, 4), &tunnel_timeout, t);
4799 /******************************************************************************/
4800 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4801 /******************************************************************************/
4804 * Function to send a create path packet to a peer.
4806 * @param cls closure
4807 * @param size number of bytes available in buf
4808 * @param buf where the callee should write the message
4809 * @return number of bytes written to buf
4812 send_core_path_create (void *cls, size_t size, void *buf)
4814 struct MeshPathInfo *info = cls;
4815 struct GNUNET_MESH_ManipulatePath *msg;
4816 struct GNUNET_PeerIdentity *peer_ptr;
4817 struct MeshTunnel *t = info->t;
4818 struct MeshPeerPath *p = info->path;
4823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4825 sizeof (struct GNUNET_MESH_ManipulatePath) +
4826 p->length * sizeof (struct GNUNET_PeerIdentity);
4828 if (size < size_needed || NULL == buf)
4833 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4834 msg->header.size = htons (size_needed);
4835 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4836 msg->tid = ntohl (t->id.tid);
4839 if (GNUNET_YES == t->speed_min)
4840 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4841 if (GNUNET_YES == t->nobuffer)
4842 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4843 msg->opt = htonl(opt);
4846 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4847 for (i = 0; i < p->length; i++)
4849 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4856 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4862 * Fill the core buffer
4864 * @param cls closure (data itself)
4865 * @param size number of bytes available in buf
4866 * @param buf where the callee should write the message
4868 * @return number of bytes written to buf
4871 send_core_data_multicast (void *cls, size_t size, void *buf)
4873 struct MeshTransmissionDescriptor *info = cls;
4876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4877 GNUNET_assert (NULL != info);
4878 GNUNET_assert (NULL != info->peer);
4879 total_size = info->mesh_data->data_len;
4880 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4882 if (total_size > size)
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4888 memcpy (buf, info->mesh_data->data, total_size);
4891 struct GNUNET_MESH_Multicast *mc;
4892 struct GNUNET_MessageHeader *mh;
4895 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4897 mc = (struct GNUNET_MESH_Multicast *) mh;
4898 mh = (struct GNUNET_MessageHeader *) &mc[1];
4899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4900 " multicast, payload type %s\n",
4901 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4903 " multicast, payload size %u\n", ntohs (mh->size));
4907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4908 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4912 data_descriptor_decrement_rc (info->mesh_data);
4913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4921 * Creates a path ack message in buf and frees all unused resources.
4923 * @param cls closure (MeshTransmissionDescriptor)
4924 * @param size number of bytes available in buf
4925 * @param buf where the callee should write the message
4926 * @return number of bytes written to buf
4929 send_core_path_ack (void *cls, size_t size, void *buf)
4931 struct MeshTransmissionDescriptor *info = cls;
4932 struct GNUNET_MESH_PathACK *msg = buf;
4934 GNUNET_assert (NULL != info);
4935 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4940 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4941 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4942 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4943 msg->tid = htonl (info->origin->tid);
4944 msg->peer_id = my_full_id;
4947 /* TODO add signature */
4949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4950 return sizeof (struct GNUNET_MESH_PathACK);
4955 * Free a transmission that was already queued with all resources
4956 * associated to the request.
4958 * @param queue Queue handler to cancel.
4959 * @param clear_cls Is it necessary to free associated cls?
4962 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4964 struct MeshTransmissionDescriptor *dd;
4965 struct MeshPathInfo *path_info;
4966 struct MeshTunnelChildInfo *cinfo;
4967 struct GNUNET_PeerIdentity id;
4971 if (GNUNET_YES == clear_cls)
4973 switch (queue->type)
4975 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4976 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4977 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4979 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4980 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4981 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4982 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4983 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4984 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4986 " prebuilt message\n");
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4989 GNUNET_MESH_DEBUG_M2S(queue->type));
4991 data_descriptor_decrement_rc (dd->mesh_data);
4993 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4995 path_info = queue->cls;
4996 path_destroy (path_info->path);
5000 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5001 " type %s unknown!\n",
5002 GNUNET_MESH_DEBUG_M2S(queue->type));
5004 GNUNET_free_non_null (queue->cls);
5006 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
5007 queue->peer->queue_tail,
5010 /* Delete from child_fc in the appropiate tunnel */
5011 max = queue->tunnel->fwd_queue_max;
5012 GNUNET_PEER_resolve (queue->peer->id, &id);
5013 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
5016 for (i = 0; i < cinfo->send_buffer_n; i++)
5019 i2 = (cinfo->send_buffer_start + i) % max;
5020 if (cinfo->send_buffer[i2] == queue)
5022 /* Found corresponding entry in the send_buffer. Move all others back. */
5027 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
5029 j2 = (cinfo->send_buffer_start + j) % max;
5030 j3 = (cinfo->send_buffer_start + j + 1) % max;
5031 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
5034 cinfo->send_buffer[j3] = NULL;
5035 cinfo->send_buffer_n--;
5040 GNUNET_free (queue);
5045 * @brief Get the next transmittable message from the queue.
5047 * This will be the head, except in the case of being a data packet
5048 * not allowed by the destination peer.
5050 * @param peer Destination peer.
5052 * @return The next viable MeshPeerQueue element to send to that peer.
5053 * NULL when there are no transmittable messages.
5055 struct MeshPeerQueue *
5056 queue_get_next (const struct MeshPeerInfo *peer)
5058 struct MeshPeerQueue *q;
5059 struct MeshTunnel *t;
5060 struct MeshTransmissionDescriptor *info;
5061 struct MeshTunnelChildInfo *cinfo;
5062 struct GNUNET_MESH_Unicast *ucast;
5063 struct GNUNET_MESH_ToOrigin *to_orig;
5064 struct GNUNET_MESH_Multicast *mcast;
5065 struct GNUNET_PeerIdentity id;
5069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
5070 for (q = peer->queue_head; NULL != q; q = q->next)
5074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5076 GNUNET_MESH_DEBUG_M2S(q->type));
5079 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5080 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
5081 pid = ntohl (ucast->pid);
5082 GNUNET_PEER_resolve (info->peer->id, &id);
5083 cinfo = tunnel_get_neighbor_fc(t, &id);
5084 ack = cinfo->fwd_ack;
5086 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5087 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
5088 pid = ntohl (to_orig->pid);
5091 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5092 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
5093 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
5095 // Not a multicast payload: multicast control traffic (destroy, etc)
5098 pid = ntohl (mcast->pid);
5099 GNUNET_PEER_resolve (info->peer->id, &id);
5100 cinfo = tunnel_get_neighbor_fc(t, &id);
5101 ack = cinfo->fwd_ack;
5104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5109 "********* ACK: %u, PID: %u\n",
5111 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
5113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5120 "********* NEXT!\n");
5123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5124 "********* nothing found\n");
5130 * Core callback to write a queued packet to core buffer
5132 * @param cls Closure (peer info).
5133 * @param size Number of bytes available in buf.
5134 * @param buf Where the to write the message.
5136 * @return number of bytes written to buf
5139 queue_send (void *cls, size_t size, void *buf)
5141 struct MeshPeerInfo *peer = cls;
5142 struct GNUNET_MessageHeader *msg;
5143 struct MeshPeerQueue *queue;
5144 struct MeshTunnel *t;
5145 struct MeshTunnelChildInfo *cinfo;
5146 struct GNUNET_PeerIdentity dst_id;
5149 peer->core_transmit = NULL;
5152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5153 queue = queue_get_next (peer);
5155 /* Queue has no internal mesh traffic nor sendable payload */
5158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5159 if (NULL == peer->queue_head)
5160 GNUNET_break (0); // Should've been canceled
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5165 GNUNET_PEER_resolve (peer->id, &dst_id);
5166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5167 "********* towards %s\n",
5168 GNUNET_i2s(&dst_id));
5169 /* Check if buffer size is enough for the message */
5170 if (queue->size > size)
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5173 "********* not enough room, reissue\n");
5174 peer->core_transmit =
5175 GNUNET_CORE_notify_transmit_ready (core_handle,
5178 GNUNET_TIME_UNIT_FOREVER_REL,
5185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5188 GNUNET_assert (0 < t->pending_messages);
5189 t->pending_messages--;
5190 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5194 "********* unicast: t->q (%u/%u)\n",
5195 t->fwd_queue_n, t->fwd_queue_max);
5197 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5204 switch (queue->type)
5207 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5208 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5209 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5210 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5211 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5213 "********* raw: %s\n",
5214 GNUNET_MESH_DEBUG_M2S (queue->type));
5216 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5217 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5218 data_size = send_core_data_raw (queue->cls, size, buf);
5219 msg = (struct GNUNET_MessageHeader *) buf;
5220 switch (ntohs (msg->type)) // Type of preconstructed message
5222 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5223 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5225 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5226 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5232 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5235 struct MeshTransmissionDescriptor *info = queue->cls;
5237 if ((1 == info->mesh_data->reference_counter
5238 && GNUNET_YES == t->speed_min)
5240 (info->mesh_data->total_out == info->mesh_data->reference_counter
5241 && GNUNET_NO == t->speed_min))
5243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5244 "********* considered sent\n");
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5250 "********* NOT considered sent yet\n");
5251 t->pending_messages++;
5254 data_size = send_core_data_multicast(queue->cls, size, buf);
5255 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5257 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5259 data_size = send_core_path_create (queue->cls, size, buf);
5261 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5263 data_size = send_core_path_ack (queue->cls, size, buf);
5265 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5267 data_size = send_core_data_multicast (queue->cls, size, buf);
5271 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5272 "********* type unknown: %u\n",
5276 switch (queue->type)
5278 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5279 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5280 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5281 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5282 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5286 "at pos %u (%p) != %p\n",
5287 cinfo->send_buffer_start,
5288 cinfo->send_buffer[cinfo->send_buffer_start],
5291 if (cinfo->send_buffer_n > 0)
5293 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5294 cinfo->send_buffer_n--;
5295 cinfo->send_buffer_start++;
5296 cinfo->send_buffer_start %= t->fwd_queue_max;
5307 /* Free queue, but cls was freed by send_core_* */
5308 queue_destroy (queue, GNUNET_NO);
5310 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5316 /* If more data in queue, send next */
5317 queue = queue_get_next(peer);
5320 struct GNUNET_PeerIdentity id;
5322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5323 GNUNET_PEER_resolve (peer->id, &id);
5324 peer->core_transmit =
5325 GNUNET_CORE_notify_transmit_ready(core_handle,
5328 GNUNET_TIME_UNIT_FOREVER_REL,
5336 if (NULL != peer->queue_head)
5338 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5339 "********* %s stalled\n",
5340 GNUNET_i2s(&my_full_id));
5342 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5343 // FIXME unify bck/fwd structures, bck does not have cinfo right now
5344 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5346 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
5347 &tunnel_poll, cinfo);
5351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5357 * @brief Queue and pass message to core when possible.
5359 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5360 * and accounts for it. In case the queue is full, the message is dropped and
5363 * Otherwise, message is treated as internal and allowed to go regardless of
5366 * @param cls Closure (@c type dependant). It will be used by queue_send to
5367 * build the message to be sent if not already prebuilt.
5368 * @param type Type of the message, 0 for a raw message.
5369 * @param size Size of the message.
5370 * @param dst Neighbor to send message to.
5371 * @param t Tunnel this message belongs to.
5374 queue_add (void *cls, uint16_t type, size_t size,
5375 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5377 struct MeshPeerQueue *queue;
5378 struct MeshTunnelChildInfo *cinfo;
5379 struct GNUNET_PeerIdentity id;
5385 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5386 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5388 n = &t->fwd_queue_n;
5389 max = &t->fwd_queue_max;
5391 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5393 n = &t->bck_queue_n;
5394 max = &t->bck_queue_max;
5401 GNUNET_STATISTICS_update(stats,
5402 "# messages dropped (buffer full)",
5404 return; // Drop message
5408 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5414 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5415 GNUNET_PEER_resolve (dst->id, &id);
5416 if (NULL == dst->core_transmit)
5418 dst->core_transmit =
5419 GNUNET_CORE_notify_transmit_ready (core_handle,
5422 GNUNET_TIME_UNIT_FOREVER_REL,
5428 t->pending_messages++;
5429 if (NULL == n) // Is this internal mesh traffic?
5432 // It's payload, keep track of buffer per peer.
5433 cinfo = tunnel_get_neighbor_fc(t, &id);
5434 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5435 if (NULL != cinfo->send_buffer[i])
5437 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5438 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5439 cinfo->send_buffer_start++;
5440 cinfo->send_buffer_start %= t->fwd_queue_max;
5444 cinfo->send_buffer_n++;
5446 cinfo->send_buffer[i] = queue;
5447 if (cinfo->send_buffer_n > t->fwd_queue_max)
5450 cinfo->send_buffer_n = t->fwd_queue_max;
5455 /******************************************************************************/
5456 /******************** MESH NETWORK HANDLERS **************************/
5457 /******************************************************************************/
5461 * Core handler for path creation
5463 * @param cls closure
5464 * @param message message
5465 * @param peer peer identity this notification is about
5466 * @param atsi performance data
5467 * @param atsi_count number of records in 'atsi'
5469 * @return GNUNET_OK to keep the connection open,
5470 * GNUNET_SYSERR to close it (signal serious error)
5473 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5474 const struct GNUNET_MessageHeader *message,
5475 const struct GNUNET_ATS_Information *atsi,
5476 unsigned int atsi_count)
5478 unsigned int own_pos;
5481 MESH_TunnelNumber tid;
5482 struct GNUNET_MESH_ManipulatePath *msg;
5483 struct GNUNET_PeerIdentity *pi;
5484 struct GNUNET_HashCode hash;
5485 struct MeshPeerPath *path;
5486 struct MeshPeerInfo *dest_peer_info;
5487 struct MeshPeerInfo *orig_peer_info;
5488 struct MeshTunnel *t;
5490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5491 "Received a path create msg [%s]\n",
5492 GNUNET_i2s (&my_full_id));
5493 size = ntohs (message->size);
5494 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5496 GNUNET_break_op (0);
5500 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5501 if (size % sizeof (struct GNUNET_PeerIdentity))
5503 GNUNET_break_op (0);
5506 size /= sizeof (struct GNUNET_PeerIdentity);
5509 GNUNET_break_op (0);
5512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5513 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5515 tid = ntohl (msg->tid);
5516 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5518 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5519 t = tunnel_get (pi, tid);
5520 if (NULL == t) // FIXME only for INCOMING tunnels?
5524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5525 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5528 // FIXME notify failure
5531 opt = ntohl (msg->opt);
5532 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5533 GNUNET_YES : GNUNET_NO;
5534 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5536 t->nobuffer = GNUNET_YES;
5537 t->last_fwd_ack = t->fwd_pid + 1;
5539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5540 " speed_min: %d, nobuffer:%d\n",
5541 t->speed_min, t->nobuffer);
5543 if (GNUNET_YES == t->nobuffer)
5545 t->bck_queue_max = 1;
5546 t->fwd_queue_max = 1;
5549 // FIXME only assign a local tid if a local client is interested (on demand)
5550 while (NULL != tunnel_get_incoming (next_local_tid))
5551 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5552 t->local_tid_dest = next_local_tid++;
5553 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5556 tunnel_reset_timeout (t);
5557 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5559 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5560 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5568 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5569 if (NULL == dest_peer_info)
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5572 " Creating PeerInfo for destination.\n");
5573 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5574 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5575 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5577 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5579 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5580 if (NULL == orig_peer_info)
5582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5583 " Creating PeerInfo for origin.\n");
5584 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5585 orig_peer_info->id = GNUNET_PEER_intern (pi);
5586 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5590 path = path_new (size);
5592 for (i = 0; i < size; i++)
5594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5595 GNUNET_i2s (&pi[i]));
5596 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5597 if (path->peers[i] == myid)
5600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5603 /* cannot be self, must be 'not found' */
5604 /* create path: self not found in path through self */
5605 GNUNET_break_op (0);
5606 path_destroy (path);
5610 path_add_to_peers (path, GNUNET_NO);
5611 tunnel_add_path (t, path, own_pos);
5612 if (own_pos == size - 1)
5614 /* It is for us! Send ack. */
5615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5616 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5617 if (NULL == t->peers)
5619 /* New tunnel! Notify clients on first payload message. */
5620 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5622 GNUNET_break (GNUNET_SYSERR !=
5623 GNUNET_CONTAINER_multihashmap_put (t->peers,
5624 &my_full_id.hashPubKey,
5627 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5632 struct MeshPeerPath *path2;
5634 /* It's for somebody else! Retransmit. */
5635 path2 = path_duplicate (path);
5636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5637 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5638 path2 = path_duplicate (path);
5639 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5640 send_create_path (dest_peer_info, path, t);
5647 * Core handler for path destruction
5649 * @param cls closure
5650 * @param message message
5651 * @param peer peer identity this notification is about
5652 * @param atsi performance data
5653 * @param atsi_count number of records in 'atsi'
5655 * @return GNUNET_OK to keep the connection open,
5656 * GNUNET_SYSERR to close it (signal serious error)
5659 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5660 const struct GNUNET_MessageHeader *message,
5661 const struct GNUNET_ATS_Information *atsi,
5662 unsigned int atsi_count)
5664 struct GNUNET_MESH_ManipulatePath *msg;
5665 struct GNUNET_PeerIdentity *pi;
5666 struct MeshPeerPath *path;
5667 struct MeshTunnel *t;
5668 unsigned int own_pos;
5672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5673 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5674 size = ntohs (message->size);
5675 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5677 GNUNET_break_op (0);
5681 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5682 if (size % sizeof (struct GNUNET_PeerIdentity))
5684 GNUNET_break_op (0);
5687 size /= sizeof (struct GNUNET_PeerIdentity);
5690 GNUNET_break_op (0);
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5695 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5696 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5698 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5700 t = tunnel_get (pi, ntohl (msg->tid));
5703 /* TODO notify back: we don't know this tunnel */
5704 GNUNET_break_op (0);
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5708 path = path_new (size);
5710 for (i = 0; i < size; i++)
5712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5713 GNUNET_i2s (&pi[i]));
5714 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5715 if (path->peers[i] == myid)
5718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5719 if (own_pos < path->length - 1)
5720 send_prebuilt_message (message, &pi[own_pos + 1], t);
5722 send_client_tunnel_disconnect(t, NULL);
5724 tunnel_delete_peer (t, path->peers[path->length - 1]);
5725 path_destroy (path);
5731 * Core handler for notifications of broken paths
5733 * @param cls closure
5734 * @param message message
5735 * @param peer peer identity this notification is about
5736 * @param atsi performance data
5737 * @param atsi_count number of records in 'atsi'
5739 * @return GNUNET_OK to keep the connection open,
5740 * GNUNET_SYSERR to close it (signal serious error)
5743 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5744 const struct GNUNET_MessageHeader *message,
5745 const struct GNUNET_ATS_Information *atsi,
5746 unsigned int atsi_count)
5748 struct GNUNET_MESH_PathBroken *msg;
5749 struct MeshTunnel *t;
5751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5752 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5753 msg = (struct GNUNET_MESH_PathBroken *) message;
5754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5755 GNUNET_i2s (&msg->peer1));
5756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5757 GNUNET_i2s (&msg->peer2));
5758 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5761 GNUNET_break_op (0);
5764 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5765 GNUNET_PEER_search (&msg->peer2));
5772 * Core handler for tunnel destruction
5774 * @param cls closure
5775 * @param message message
5776 * @param peer peer identity this notification is about
5777 * @param atsi performance data
5778 * @param atsi_count number of records in 'atsi'
5780 * @return GNUNET_OK to keep the connection open,
5781 * GNUNET_SYSERR to close it (signal serious error)
5784 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5785 const struct GNUNET_MessageHeader *message,
5786 const struct GNUNET_ATS_Information *atsi,
5787 unsigned int atsi_count)
5789 struct GNUNET_MESH_TunnelDestroy *msg;
5790 struct MeshTunnel *t;
5792 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5794 "Got a TUNNEL DESTROY packet from %s\n",
5796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5797 " for tunnel %s [%u]\n",
5798 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5799 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5802 /* Probably already got the message from another path,
5803 * destroyed the tunnel and retransmitted to children.
5806 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
5810 if (t->id.oid == myid)
5812 GNUNET_break_op (0);
5815 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5817 /* Tunnel was incoming, notify clients */
5818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5819 t->local_tid, t->local_tid_dest);
5820 send_clients_tunnel_destroy (t);
5822 tunnel_send_destroy (t, GNUNET_YES);
5823 t->destroy = GNUNET_YES;
5824 // TODO: add timeout to destroy the tunnel anyway
5830 * Core handler for mesh network traffic going from the origin to a peer
5832 * @param cls closure
5833 * @param peer peer identity this notification is about
5834 * @param message message
5835 * @param atsi performance data
5836 * @param atsi_count number of records in 'atsi'
5837 * @return GNUNET_OK to keep the connection open,
5838 * GNUNET_SYSERR to close it (signal serious error)
5841 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5842 const struct GNUNET_MessageHeader *message,
5843 const struct GNUNET_ATS_Information *atsi,
5844 unsigned int atsi_count)
5846 struct GNUNET_MESH_Unicast *msg;
5847 struct GNUNET_PeerIdentity *neighbor;
5848 struct MeshTunnelChildInfo *cinfo;
5849 struct MeshTunnel *t;
5850 GNUNET_PEER_Id dest_id;
5855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5858 size = ntohs (message->size);
5860 sizeof (struct GNUNET_MESH_Unicast) +
5861 sizeof (struct GNUNET_MessageHeader))
5866 msg = (struct GNUNET_MESH_Unicast *) message;
5867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5868 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5870 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5873 /* TODO notify back: we don't know this tunnel */
5874 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5875 GNUNET_break_op (0);
5878 pid = ntohl (msg->pid);
5879 if (t->fwd_pid == pid)
5881 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5882 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5883 " Already seen pid %u, DROPPING!\n", pid);
5888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5889 " pid %u not seen yet, forwarding\n", pid);
5892 t->skip += (pid - t->fwd_pid) - 1;
5895 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5897 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5898 GNUNET_break_op (0);
5899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5900 "Received PID %u, ACK %u\n",
5901 pid, t->last_fwd_ack);
5905 tunnel_reset_timeout (t);
5906 dest_id = GNUNET_PEER_search (&msg->destination);
5907 if (dest_id == myid)
5909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5910 " it's for us! sending to clients...\n");
5911 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5912 send_subscribed_clients (message, &msg[1].header, t);
5913 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5916 ttl = ntohl (msg->ttl);
5917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5920 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5921 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5922 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5926 " not for us, retransmitting...\n");
5928 neighbor = tree_get_first_hop (t->tree, dest_id);
5929 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5930 cinfo->fwd_pid = pid;
5931 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5934 if (GNUNET_YES == t->nobuffer &&
5935 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5937 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5938 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5939 GNUNET_break_op (0);
5942 send_prebuilt_message (message, neighbor, t);
5943 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5949 * Core handler for mesh network traffic going from the origin to all peers
5951 * @param cls closure
5952 * @param message message
5953 * @param peer peer identity this notification is about
5954 * @param atsi performance data
5955 * @param atsi_count number of records in 'atsi'
5956 * @return GNUNET_OK to keep the connection open,
5957 * GNUNET_SYSERR to close it (signal serious error)
5959 * TODO: Check who we got this from, to validate route.
5962 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5963 const struct GNUNET_MessageHeader *message,
5964 const struct GNUNET_ATS_Information *atsi,
5965 unsigned int atsi_count)
5967 struct GNUNET_MESH_Multicast *msg;
5968 struct MeshTunnel *t;
5972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5974 size = ntohs (message->size);
5975 if (sizeof (struct GNUNET_MESH_Multicast) +
5976 sizeof (struct GNUNET_MessageHeader) > size)
5978 GNUNET_break_op (0);
5981 msg = (struct GNUNET_MESH_Multicast *) message;
5982 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5986 /* TODO notify that we dont know that tunnel */
5987 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5988 GNUNET_break_op (0);
5991 pid = ntohl (msg->pid);
5992 if (t->fwd_pid == pid)
5994 /* already seen this packet, drop */
5995 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5997 " Already seen pid %u, DROPPING!\n", pid);
5998 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6004 " pid %u not seen yet, forwarding\n", pid);
6006 t->skip += (pid - t->fwd_pid) - 1;
6008 tunnel_reset_timeout (t);
6010 /* Transmit to locally interested clients */
6011 if (NULL != t->peers &&
6012 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
6014 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
6015 send_subscribed_clients (message, &msg[1].header, t);
6016 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
6018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
6019 if (ntohl (msg->ttl) == 0)
6021 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
6022 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
6023 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6026 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
6027 tunnel_send_multicast (t, message);
6033 * Core handler for mesh network traffic toward the owner of a tunnel
6035 * @param cls closure
6036 * @param message message
6037 * @param peer peer identity this notification is about
6038 * @param atsi performance data
6039 * @param atsi_count number of records in 'atsi'
6041 * @return GNUNET_OK to keep the connection open,
6042 * GNUNET_SYSERR to close it (signal serious error)
6045 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
6046 const struct GNUNET_MessageHeader *message,
6047 const struct GNUNET_ATS_Information *atsi,
6048 unsigned int atsi_count)
6050 struct GNUNET_MESH_ToOrigin *msg;
6051 struct GNUNET_PeerIdentity id;
6052 struct MeshPeerInfo *peer_info;
6053 struct MeshTunnel *t;
6054 struct MeshTunnelChildInfo *cinfo;
6055 GNUNET_PEER_Id predecessor;
6059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
6061 size = ntohs (message->size);
6062 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
6063 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
6065 GNUNET_break_op (0);
6068 msg = (struct GNUNET_MESH_ToOrigin *) message;
6069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
6070 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
6071 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6072 pid = ntohl (msg->pid);
6076 /* TODO notify that we dont know this tunnel (whom)? */
6077 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
6078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6079 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
6080 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
6084 cinfo = tunnel_get_neighbor_fc(t, peer);
6091 if (cinfo->bck_pid == pid)
6093 /* already seen this packet, drop */
6094 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
6095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6096 " Already seen pid %u, DROPPING!\n", pid);
6097 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6102 " pid %u not seen yet, forwarding\n", pid);
6103 cinfo->bck_pid = pid;
6105 if (NULL != t->owner)
6108 struct GNUNET_MESH_ToOrigin *copy;
6110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6111 " it's for us! sending to clients...\n");
6112 /* TODO signature verification */
6113 memcpy (cbuf, message, size);
6114 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
6115 copy->tid = htonl (t->local_tid);
6117 copy->pid = htonl (t->bck_pid);
6118 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
6119 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
6120 ©->header, GNUNET_NO);
6121 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6125 " not for us, retransmitting...\n");
6127 peer_info = peer_info_get (&msg->oid);
6128 if (NULL == peer_info)
6130 /* unknown origin of tunnel */
6134 predecessor = tree_get_predecessor (t->tree);
6135 if (0 == predecessor)
6137 if (GNUNET_YES == t->destroy)
6139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6140 "to orig received on a dying tunnel %s [%X]\n",
6141 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6145 "unknown to origin at %s\n",
6146 GNUNET_i2s (&my_full_id));
6147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6151 "for tunnel %s [%X]\n",
6152 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6155 tree_debug (t->tree);
6158 GNUNET_PEER_resolve (predecessor, &id);
6159 send_prebuilt_message (message, &id, t);
6160 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6167 * Core handler for mesh network traffic point-to-point acks.
6169 * @param cls closure
6170 * @param message message
6171 * @param peer peer identity this notification is about
6172 * @param atsi performance data
6173 * @param atsi_count number of records in 'atsi'
6175 * @return GNUNET_OK to keep the connection open,
6176 * GNUNET_SYSERR to close it (signal serious error)
6179 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6180 const struct GNUNET_MessageHeader *message,
6181 const struct GNUNET_ATS_Information *atsi,
6182 unsigned int atsi_count)
6184 struct GNUNET_MESH_ACK *msg;
6185 struct MeshTunnel *t;
6188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6190 msg = (struct GNUNET_MESH_ACK *) message;
6192 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6196 /* TODO notify that we dont know this tunnel (whom)? */
6197 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6200 ack = ntohl (msg->pid);
6201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6203 /* Is this a forward or backward ACK? */
6204 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6206 struct MeshTunnelChildInfo *cinfo;
6209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6210 cinfo = tunnel_get_neighbor_fc (t, peer);
6211 cinfo->fwd_ack = ack;
6212 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6213 tunnel_unlock_fwd_queues (t);
6214 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6216 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6217 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6218 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6225 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6226 tunnel_unlock_bck_queue (t);
6233 * Core handler for mesh network traffic point-to-point ack polls.
6235 * @param cls closure
6236 * @param message message
6237 * @param peer peer identity this notification is about
6238 * @param atsi performance data
6239 * @param atsi_count number of records in 'atsi'
6241 * @return GNUNET_OK to keep the connection open,
6242 * GNUNET_SYSERR to close it (signal serious error)
6245 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6246 const struct GNUNET_MessageHeader *message,
6247 const struct GNUNET_ATS_Information *atsi,
6248 unsigned int atsi_count)
6250 struct GNUNET_MESH_Poll *msg;
6251 struct MeshTunnel *t;
6253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6256 msg = (struct GNUNET_MESH_Poll *) message;
6258 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6262 /* TODO notify that we dont know this tunnel (whom)? */
6263 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6264 GNUNET_break_op (0);
6268 /* Is this a forward or backward ACK? */
6269 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6271 struct MeshTunnelChildInfo *cinfo;
6273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6274 cinfo = tunnel_get_neighbor_fc (t, peer);
6275 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6276 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6281 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6289 * Core handler for path ACKs
6291 * @param cls closure
6292 * @param message message
6293 * @param peer peer identity this notification is about
6294 * @param atsi performance data
6295 * @param atsi_count number of records in 'atsi'
6297 * @return GNUNET_OK to keep the connection open,
6298 * GNUNET_SYSERR to close it (signal serious error)
6301 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6302 const struct GNUNET_MessageHeader *message,
6303 const struct GNUNET_ATS_Information *atsi,
6304 unsigned int atsi_count)
6306 struct GNUNET_MESH_PathACK *msg;
6307 struct GNUNET_PeerIdentity id;
6308 struct MeshPeerInfo *peer_info;
6309 struct MeshPeerPath *p;
6310 struct MeshTunnel *t;
6312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6313 GNUNET_i2s (&my_full_id));
6314 msg = (struct GNUNET_MESH_PathACK *) message;
6315 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6318 /* TODO notify that we don't know the tunnel */
6319 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6321 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6325 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6327 peer_info = peer_info_get (&msg->peer_id);
6328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6329 GNUNET_i2s (&msg->peer_id));
6330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6333 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6336 "connect_by_string completed, stopping search\n");
6337 regex_cancel_search (t->regex_ctx);
6338 t->regex_ctx = NULL;
6341 /* Add paths to peers? */
6342 p = tree_get_path_to_peer (t->tree, peer_info->id);
6345 path_add_to_peers (p, GNUNET_YES);
6353 /* Message for us? */
6354 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6357 if (NULL == t->owner)
6359 GNUNET_break_op (0);
6362 if (NULL != t->dht_get_type)
6364 GNUNET_DHT_get_stop (t->dht_get_type);
6365 t->dht_get_type = NULL;
6367 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6369 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6370 send_client_peer_connected (t, peer_info->id);
6375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6376 " not for us, retransmitting...\n");
6377 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6378 peer_info = peer_info_get (&msg->oid);
6379 if (NULL == peer_info)
6381 /* If we know the tunnel, we should DEFINITELY know the peer */
6385 send_prebuilt_message (message, &id, t);
6391 * Core handler for mesh keepalives.
6393 * @param cls closure
6394 * @param message message
6395 * @param peer peer identity this notification is about
6396 * @param atsi performance data
6397 * @param atsi_count number of records in 'atsi'
6398 * @return GNUNET_OK to keep the connection open,
6399 * GNUNET_SYSERR to close it (signal serious error)
6401 * TODO: Check who we got this from, to validate route.
6404 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6405 const struct GNUNET_MessageHeader *message,
6406 const struct GNUNET_ATS_Information *atsi,
6407 unsigned int atsi_count)
6409 struct GNUNET_MESH_TunnelKeepAlive *msg;
6410 struct MeshTunnel *t;
6412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6415 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6416 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6420 /* TODO notify that we dont know that tunnel */
6421 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
6426 tunnel_reset_timeout (t);
6428 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6429 tunnel_send_multicast (t, message);
6436 * Functions to handle messages from core
6438 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6439 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6440 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6441 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6442 sizeof (struct GNUNET_MESH_PathBroken)},
6443 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6444 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6445 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6446 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6447 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6448 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6449 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6450 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6451 sizeof (struct GNUNET_MESH_ACK)},
6452 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6453 sizeof (struct GNUNET_MESH_Poll)},
6454 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6455 sizeof (struct GNUNET_MESH_PathACK)},
6461 /******************************************************************************/
6462 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6463 /******************************************************************************/
6466 * deregister_app: iterator for removing each application registered by a client
6468 * @param cls closure
6469 * @param key the hash of the application id (used to access the hashmap)
6470 * @param value the value stored at the key (client)
6472 * @return GNUNET_OK on success
6475 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6477 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6478 GNUNET_break (GNUNET_YES ==
6479 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6485 * notify_client_connection_failure: notify a client that the connection to the
6486 * requested remote peer is not possible (for instance, no route found)
6487 * Function called when the socket is ready to queue more data. "buf" will be
6488 * NULL and "size" zero if the socket was closed for writing in the meantime.
6490 * @param cls closure
6491 * @param size number of bytes available in buf
6492 * @param buf where the callee should write the message
6493 * @return number of bytes written to buf
6496 notify_client_connection_failure (void *cls, size_t size, void *buf)
6499 struct MeshPeerInfo *peer_info;
6500 struct GNUNET_MESH_PeerControl *msg;
6501 struct GNUNET_PeerIdentity id;
6503 if (0 == size && NULL == buf)
6505 // TODO retry? cancel?
6509 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6510 peer_info = (struct MeshPeerInfo *) cls;
6511 msg = (struct GNUNET_MESH_PeerControl *) buf;
6512 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6513 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6514 // msg->tunnel_id = htonl(peer_info->t->tid);
6515 GNUNET_PEER_resolve (peer_info->id, &id);
6516 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6524 * Send keepalive packets for a peer
6526 * @param cls Closure (tunnel for which to send the keepalive).
6527 * @param tc Notification context.
6530 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6532 struct MeshTunnel *t = cls;
6533 struct GNUNET_MESH_TunnelKeepAlive *msg;
6534 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6537 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6538 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6544 "sending keepalive for tunnel %d\n", t->id.tid);
6546 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6547 msg->header.size = htons (size);
6548 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6549 msg->oid = my_full_id;
6550 msg->tid = htonl (t->id.tid);
6551 tunnel_send_multicast (t, &msg->header);
6553 t->path_refresh_task =
6554 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6555 tunnel_reset_timeout(t);
6560 * Function to process paths received for a new peer addition. The recorded
6561 * paths form the initial tunnel, which can be optimized later.
6562 * Called on each result obtained for the DHT search.
6564 * @param cls closure
6565 * @param exp when will this value expire
6566 * @param key key of the result
6567 * @param get_path path of the get request
6568 * @param get_path_length lenght of get_path
6569 * @param put_path path of the put request
6570 * @param put_path_length length of the put_path
6571 * @param type type of the result
6572 * @param size number of bytes in data
6573 * @param data pointer to the result data
6575 * TODO: re-issue the request after certain time? cancel after X results?
6578 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6579 const struct GNUNET_HashCode * key,
6580 const struct GNUNET_PeerIdentity *get_path,
6581 unsigned int get_path_length,
6582 const struct GNUNET_PeerIdentity *put_path,
6583 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6584 size_t size, const void *data)
6586 struct MeshPathInfo *path_info = cls;
6587 struct MeshPeerPath *p;
6588 struct GNUNET_PeerIdentity pi;
6591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6592 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6595 p = path_build_from_dht (get_path, get_path_length, put_path,
6597 path_add_to_peers (p, GNUNET_NO);
6599 for (i = 0; i < path_info->peer->ntunnels; i++)
6601 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6602 peer_info_connect (path_info->peer, path_info->t);
6610 * Function to process paths received for a new peer addition. The recorded
6611 * paths form the initial tunnel, which can be optimized later.
6612 * Called on each result obtained for the DHT search.
6614 * @param cls closure
6615 * @param exp when will this value expire
6616 * @param key key of the result
6617 * @param get_path path of the get request
6618 * @param get_path_length lenght of get_path
6619 * @param put_path path of the put request
6620 * @param put_path_length length of the put_path
6621 * @param type type of the result
6622 * @param size number of bytes in data
6623 * @param data pointer to the result data
6626 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6627 const struct GNUNET_HashCode * key,
6628 const struct GNUNET_PeerIdentity *get_path,
6629 unsigned int get_path_length,
6630 const struct GNUNET_PeerIdentity *put_path,
6631 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6632 size_t size, const void *data)
6634 const struct PBlock *pb = data;
6635 const struct GNUNET_PeerIdentity *pi = &pb->id;
6636 struct MeshTunnel *t = cls;
6637 struct MeshPeerInfo *peer_info;
6638 struct MeshPeerPath *p;
6640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6641 if (size != sizeof (struct PBlock))
6643 GNUNET_break_op (0);
6646 if (ntohl(pb->type) != t->type)
6648 GNUNET_break_op (0);
6651 GNUNET_assert (NULL != t->owner);
6652 peer_info = peer_info_get (pi);
6653 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6655 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6657 p = path_build_from_dht (get_path, get_path_length, put_path,
6659 path_add_to_peers (p, GNUNET_NO);
6661 tunnel_add_peer (t, peer_info);
6662 peer_info_connect (peer_info, t);
6667 * Function to process DHT string to regex matching.
6668 * Called on each result obtained for the DHT search.
6670 * @param cls closure (search context)
6671 * @param exp when will this value expire
6672 * @param key key of the result
6673 * @param get_path path of the get request (not used)
6674 * @param get_path_length lenght of get_path (not used)
6675 * @param put_path path of the put request (not used)
6676 * @param put_path_length length of the put_path (not used)
6677 * @param type type of the result
6678 * @param size number of bytes in data
6679 * @param data pointer to the result data
6682 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6683 const struct GNUNET_HashCode * key,
6684 const struct GNUNET_PeerIdentity *get_path,
6685 unsigned int get_path_length,
6686 const struct GNUNET_PeerIdentity *put_path,
6687 unsigned int put_path_length,
6688 enum GNUNET_BLOCK_Type type,
6689 size_t size, const void *data)
6691 const struct MeshRegexAccept *block = data;
6692 struct MeshRegexSearchContext *ctx = cls;
6693 struct MeshRegexSearchInfo *info = ctx->info;
6694 // struct MeshPeerPath *p;
6695 struct MeshPeerInfo *peer_info;
6697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6699 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6701 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6704 peer_info = peer_info_get(&block->id);
6705 // p = path_build_from_dht (get_path, get_path_length, put_path,
6706 // put_path_length);
6707 // path_add_to_peers (p, GNUNET_NO);
6710 tunnel_add_peer (info->t, peer_info);
6711 peer_info_connect (peer_info, info->t);
6712 if (0 == info->peer)
6714 info->peer = peer_info->id;
6718 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6721 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
6724 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6725 ®ex_connect_timeout,
6733 * Function to process DHT string to regex matching.
6734 * Called on each result obtained for the DHT search.
6736 * @param cls closure (search context)
6737 * @param exp when will this value expire
6738 * @param key key of the result
6739 * @param get_path path of the get request (not used)
6740 * @param get_path_length lenght of get_path (not used)
6741 * @param put_path path of the put request (not used)
6742 * @param put_path_length length of the put_path (not used)
6743 * @param type type of the result
6744 * @param size number of bytes in data
6745 * @param data pointer to the result data
6747 * TODO: re-issue the request after certain time? cancel after X results?
6750 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6751 const struct GNUNET_HashCode * key,
6752 const struct GNUNET_PeerIdentity *get_path,
6753 unsigned int get_path_length,
6754 const struct GNUNET_PeerIdentity *put_path,
6755 unsigned int put_path_length,
6756 enum GNUNET_BLOCK_Type type,
6757 size_t size, const void *data)
6759 const struct MeshRegexBlock *block = data;
6760 struct MeshRegexSearchContext *ctx = cls;
6761 struct MeshRegexSearchInfo *info = ctx->info;
6765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6766 "DHT GET STRING RETURNED RESULTS\n");
6767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6768 " key: %s\n", GNUNET_h2s (key));
6770 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6772 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6775 copy = GNUNET_malloc (size);
6776 memcpy (copy, data, size);
6777 GNUNET_break (GNUNET_OK ==
6778 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6779 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6780 len = ntohl (block->n_proof);
6782 char proof[len + 1];
6784 memcpy (proof, &block[1], len);
6786 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6788 GNUNET_break_op (0);
6792 len = strlen (info->description);
6793 if (len == ctx->position) // String processed
6795 if (GNUNET_YES == ntohl (block->accepting))
6797 regex_find_path(key, ctx);
6801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6802 // FIXME REGEX this block not successful, wait for more? start timeout?
6807 regex_next_edge (block, size, ctx);
6812 /******************************************************************************/
6813 /********************* MESH LOCAL HANDLES **************************/
6814 /******************************************************************************/
6818 * Handler for client disconnection
6820 * @param cls closure
6821 * @param client identification of the client; NULL
6822 * for the last call when the server is destroyed
6825 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6827 struct MeshClient *c;
6828 struct MeshClient *next;
6831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6841 if (c->handle != client)
6843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6849 GNUNET_SERVER_client_drop (c->handle);
6850 c->shutting_down = GNUNET_YES;
6851 GNUNET_assert (NULL != c->own_tunnels);
6852 GNUNET_assert (NULL != c->incoming_tunnels);
6853 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6854 &tunnel_destroy_iterator, c);
6855 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6856 &tunnel_destroy_iterator, c);
6857 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6858 &tunnel_destroy_iterator, c);
6859 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6860 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6861 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6863 /* deregister clients applications */
6864 if (NULL != c->apps)
6866 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6867 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6869 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6870 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6872 GNUNET_SCHEDULER_cancel (announce_applications_task);
6873 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6875 if (NULL != c->types)
6876 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6877 for (i = 0; i < c->n_regex; i++)
6879 GNUNET_free (c->regexes[i].regex);
6880 if (NULL != c->regexes[i].dfa)
6881 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6883 GNUNET_free_non_null (c->regexes);
6884 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6885 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6887 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6890 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6899 * Handler for new clients
6901 * @param cls closure
6902 * @param client identification of the client
6903 * @param message the actual message, which includes messages the client wants
6906 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6907 const struct GNUNET_MessageHeader *message)
6909 struct GNUNET_MESH_ClientConnect *cc_msg;
6910 struct MeshClient *c;
6911 GNUNET_MESH_ApplicationType *a;
6918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6919 /* Check data sanity */
6920 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6921 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6922 ntypes = ntohs (cc_msg->types);
6923 napps = ntohs (cc_msg->applications);
6925 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6928 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6932 /* Create new client structure */
6933 c = GNUNET_malloc (sizeof (struct MeshClient));
6934 c->id = next_client_id++;
6935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6937 GNUNET_SERVER_client_keep (client);
6938 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6941 GNUNET_MESH_ApplicationType at;
6942 struct GNUNET_HashCode hc;
6944 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6945 for (i = 0; i < napps; i++)
6948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6949 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6950 /* store in clients hashmap */
6951 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6952 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6953 /* store in global hashmap, for announcements */
6954 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6955 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6957 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6958 announce_applications_task =
6959 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6965 struct GNUNET_HashCode hc;
6967 t = (uint16_t *) & a[napps];
6968 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6969 for (i = 0; i < ntypes; i++)
6972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6973 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6975 /* store in clients hashmap */
6976 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6977 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6978 /* store in global hashmap */
6979 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6980 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6984 " client has %u+%u subscriptions\n", napps, ntypes);
6986 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6987 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6988 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6989 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6990 GNUNET_SERVER_notification_context_add (nc, client);
6991 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6993 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6999 * Handler for clients announcing available services by a regular expression.
7001 * @param cls closure
7002 * @param client identification of the client
7003 * @param message the actual message, which includes messages the client wants
7006 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
7007 const struct GNUNET_MessageHeader *message)
7009 const struct GNUNET_MESH_RegexAnnounce *msg;
7010 struct MeshRegexDescriptor rd;
7011 struct MeshClient *c;
7016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
7018 /* Sanity check for client registration */
7019 if (NULL == (c = client_get (client)))
7022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7027 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
7029 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
7030 if (NULL != c->partial_regex)
7032 regex = c->partial_regex;
7033 offset = strlen (c->partial_regex);
7034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7035 " continuation, already have %u bytes\n",
7044 regex = GNUNET_realloc (regex, offset + len + 1);
7045 memcpy (®ex[offset], &msg[1], len);
7046 regex[offset + len] = '\0';
7047 if (0 == ntohs (msg->last))
7049 c->partial_regex = regex;
7050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7051 " not ended, stored %u bytes for later\n",
7053 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7057 rd.compression = ntohs (msg->compression_characters);
7059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
7060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
7061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
7062 GNUNET_array_append (c->regexes, c->n_regex, rd);
7063 c->partial_regex = NULL;
7064 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
7066 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
7072 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
7078 * Handler for requests of new tunnels
7080 * @param cls closure
7081 * @param client identification of the client
7082 * @param message the actual message
7085 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
7086 const struct GNUNET_MessageHeader *message)
7088 struct GNUNET_MESH_TunnelMessage *t_msg;
7089 struct MeshTunnel *t;
7090 struct MeshClient *c;
7091 MESH_TunnelNumber tid;
7093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
7095 /* Sanity check for client registration */
7096 if (NULL == (c = client_get (client)))
7099 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7104 /* Message sanity check */
7105 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7112 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7113 /* Sanity check for tunnel numbering */
7114 tid = ntohl (t_msg->tunnel_id);
7115 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
7118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7121 /* Sanity check for duplicate tunnel IDs */
7122 if (NULL != tunnel_get_by_local_id (c, tid))
7125 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7129 while (NULL != tunnel_get_by_pi (myid, next_tid))
7130 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7131 t = tunnel_new (myid, next_tid++, c, tid);
7134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
7136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7139 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
7141 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
7142 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
7145 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7151 * Handler for requests of deleting tunnels
7153 * @param cls closure
7154 * @param client identification of the client
7155 * @param message the actual message
7158 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
7159 const struct GNUNET_MessageHeader *message)
7161 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7162 struct MeshClient *c;
7163 struct MeshTunnel *t;
7164 MESH_TunnelNumber tid;
7166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7167 "Got a DESTROY TUNNEL from client!\n");
7169 /* Sanity check for client registration */
7170 if (NULL == (c = client_get (client)))
7173 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7178 /* Message sanity check */
7179 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7182 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7186 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7188 /* Retrieve tunnel */
7189 tid = ntohl (tunnel_msg->tunnel_id);
7190 t = tunnel_get_by_local_id(c, tid);
7193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7198 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7200 client_ignore_tunnel (c, t);
7201 tunnel_destroy_empty (t);
7202 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7205 send_client_tunnel_disconnect(t, c);
7206 client_delete_tunnel(c, t);
7208 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7210 tunnel_send_destroy (t, GNUNET_YES);
7211 t->destroy = GNUNET_YES;
7212 // The tunnel will be destroyed when the last message is transmitted.
7213 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7219 * Handler for requests of seeting tunnel's speed.
7221 * @param cls Closure (unused).
7222 * @param client Identification of the client.
7223 * @param message The actual message.
7226 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7227 const struct GNUNET_MessageHeader *message)
7229 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7230 struct MeshClient *c;
7231 struct MeshTunnel *t;
7232 MESH_TunnelNumber tid;
7234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7235 "Got a SPEED request from client!\n");
7237 /* Sanity check for client registration */
7238 if (NULL == (c = client_get (client)))
7241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7247 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7249 /* Retrieve tunnel */
7250 tid = ntohl (tunnel_msg->tunnel_id);
7251 t = tunnel_get_by_local_id(c, tid);
7254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7260 switch (ntohs(message->type))
7262 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7263 t->speed_min = GNUNET_YES;
7265 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7266 t->speed_min = GNUNET_NO;
7271 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7276 * Handler for requests of seeting tunnel's buffering policy.
7278 * @param cls Closure (unused).
7279 * @param client Identification of the client.
7280 * @param message The actual message.
7283 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7284 const struct GNUNET_MessageHeader *message)
7286 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7287 struct MeshClient *c;
7288 struct MeshTunnel *t;
7289 MESH_TunnelNumber tid;
7291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7292 "Got a BUFFER request from client!\n");
7294 /* Sanity check for client registration */
7295 if (NULL == (c = client_get (client)))
7298 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7303 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7305 /* Retrieve tunnel */
7306 tid = ntohl (tunnel_msg->tunnel_id);
7307 t = tunnel_get_by_local_id(c, tid);
7310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7312 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7316 switch (ntohs(message->type))
7318 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7319 t->nobuffer = GNUNET_NO;
7321 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7322 t->nobuffer = GNUNET_YES;
7328 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7333 * Handler for connection requests to new peers
7335 * @param cls closure
7336 * @param client identification of the client
7337 * @param message the actual message (PeerControl)
7340 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7341 const struct GNUNET_MessageHeader *message)
7343 struct GNUNET_MESH_PeerControl *peer_msg;
7344 struct MeshPeerInfo *peer_info;
7345 struct MeshClient *c;
7346 struct MeshTunnel *t;
7347 MESH_TunnelNumber tid;
7349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7350 /* Sanity check for client registration */
7351 if (NULL == (c = client_get (client)))
7354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7359 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7361 /* Sanity check for message size */
7362 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7369 /* Tunnel exists? */
7370 tid = ntohl (peer_msg->tunnel_id);
7371 t = tunnel_get_by_local_id (c, tid);
7375 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7379 /* Does client own tunnel? */
7380 if (t->owner->handle != client)
7383 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7387 GNUNET_i2s (&peer_msg->peer));
7388 peer_info = peer_info_get (&peer_msg->peer);
7390 tunnel_add_peer (t, peer_info);
7391 peer_info_connect (peer_info, t);
7393 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7399 * Handler for disconnection requests of peers in a tunnel
7401 * @param cls closure
7402 * @param client identification of the client
7403 * @param message the actual message (PeerControl)
7406 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7407 const struct GNUNET_MessageHeader *message)
7409 struct GNUNET_MESH_PeerControl *peer_msg;
7410 struct MeshPeerInfo *peer_info;
7411 struct MeshClient *c;
7412 struct MeshTunnel *t;
7413 MESH_TunnelNumber tid;
7415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7416 /* Sanity check for client registration */
7417 if (NULL == (c = client_get (client)))
7420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7425 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7427 /* Sanity check for message size */
7428 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7435 /* Tunnel exists? */
7436 tid = ntohl (peer_msg->tunnel_id);
7437 t = tunnel_get_by_local_id (c, tid);
7441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7446 /* Does client own tunnel? */
7447 if (t->owner->handle != client)
7450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7455 GNUNET_i2s (&peer_msg->peer));
7456 /* Is the peer in the tunnel? */
7458 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7459 if (NULL == peer_info)
7462 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7466 /* Ok, delete peer from tunnel */
7467 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7468 &peer_msg->peer.hashPubKey);
7470 send_destroy_path (t, peer_info->id);
7471 tunnel_delete_peer (t, peer_info->id);
7472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7477 * Handler for blacklist requests of peers in a tunnel
7479 * @param cls closure
7480 * @param client identification of the client
7481 * @param message the actual message (PeerControl)
7483 * FIXME implement DHT block bloomfilter
7486 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7487 const struct GNUNET_MessageHeader *message)
7489 struct GNUNET_MESH_PeerControl *peer_msg;
7490 struct MeshClient *c;
7491 struct MeshTunnel *t;
7492 MESH_TunnelNumber tid;
7494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7495 /* Sanity check for client registration */
7496 if (NULL == (c = client_get (client)))
7499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7504 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7506 /* Sanity check for message size */
7507 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7514 /* Tunnel exists? */
7515 tid = ntohl (peer_msg->tunnel_id);
7516 t = tunnel_get_by_local_id (c, tid);
7520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7525 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7526 GNUNET_PEER_intern(&peer_msg->peer));
7531 * Handler for unblacklist requests of peers in a tunnel
7533 * @param cls closure
7534 * @param client identification of the client
7535 * @param message the actual message (PeerControl)
7538 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7539 const struct GNUNET_MessageHeader *message)
7541 struct GNUNET_MESH_PeerControl *peer_msg;
7542 struct MeshClient *c;
7543 struct MeshTunnel *t;
7544 MESH_TunnelNumber tid;
7548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7549 /* Sanity check for client registration */
7550 if (NULL == (c = client_get (client)))
7553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7558 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7560 /* Sanity check for message size */
7561 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7568 /* Tunnel exists? */
7569 tid = ntohl (peer_msg->tunnel_id);
7570 t = tunnel_get_by_local_id (c, tid);
7574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7579 /* if peer is not known, complain */
7580 pid = GNUNET_PEER_search (&peer_msg->peer);
7587 /* search and remove from list */
7588 for (i = 0; i < t->nblacklisted; i++)
7590 if (t->blacklisted[i] == pid)
7592 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7593 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7598 /* if peer hasn't been blacklisted, complain */
7604 * Handler for connection requests to new peers by type
7606 * @param cls closure
7607 * @param client identification of the client
7608 * @param message the actual message (ConnectPeerByType)
7611 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7612 const struct GNUNET_MessageHeader *message)
7614 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7615 struct MeshClient *c;
7616 struct MeshTunnel *t;
7617 struct GNUNET_HashCode hash;
7618 MESH_TunnelNumber tid;
7620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7621 /* Sanity check for client registration */
7622 if (NULL == (c = client_get (client)))
7625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7630 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7632 /* Sanity check for message size */
7633 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7634 ntohs (connect_msg->header.size))
7637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7641 /* Tunnel exists? */
7642 tid = ntohl (connect_msg->tunnel_id);
7643 t = tunnel_get_by_local_id (c, tid);
7647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7651 /* Does client own tunnel? */
7652 if (t->owner->handle != client)
7655 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7659 /* Do WE have the service? */
7660 t->type = ntohl (connect_msg->type);
7661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7662 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7663 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7666 /* Yes! Fast forward, add ourselves to the tunnel and send the
7667 * good news to the client, and alert the destination client of
7668 * an incoming tunnel.
7670 * FIXME send a path create to self, avoid code duplication
7672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7673 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7674 peer_info_get (&my_full_id),
7675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7678 send_client_peer_connected (t, myid);
7679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7680 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7682 t->local_tid_dest = next_local_tid++;
7683 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7684 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7689 /* Ok, lets find a peer offering the service */
7690 if (NULL != t->dht_get_type)
7692 GNUNET_DHT_get_stop (t->dht_get_type);
7694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7695 GNUNET_h2s (&hash));
7697 GNUNET_DHT_get_start (dht_handle,
7698 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7700 dht_replication_level,
7701 GNUNET_DHT_RO_RECORD_ROUTE |
7702 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7704 &dht_get_type_handler, t);
7706 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7712 * Handler for connection requests to new peers by a string service description.
7714 * @param cls closure
7715 * @param client identification of the client
7716 * @param message the actual message, which includes messages the client wants
7719 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7720 const struct GNUNET_MessageHeader *message)
7722 struct GNUNET_MESH_ConnectPeerByString *msg;
7723 struct MeshRegexSearchContext *ctx;
7724 struct MeshRegexSearchInfo *info;
7725 struct GNUNET_DHT_GetHandle *get_h;
7726 struct GNUNET_HashCode key;
7727 struct MeshTunnel *t;
7728 struct MeshClient *c;
7729 MESH_TunnelNumber tid;
7734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7735 "Connect by string started\n");
7736 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7737 size = htons (message->size);
7739 /* Sanity check for client registration */
7740 if (NULL == (c = client_get (client)))
7743 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7748 /* Message size sanity check */
7749 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7756 /* Tunnel exists? */
7757 tid = ntohl (msg->tunnel_id);
7758 t = tunnel_get_by_local_id (c, tid);
7762 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7766 /* Does client own tunnel? */
7767 if (t->owner->handle != client)
7770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7775 " on tunnel %s [%u]\n",
7776 GNUNET_i2s(&my_full_id),
7779 /* Only one connect_by_string allowed at the same time! */
7780 /* FIXME: allow more, return handle at api level to cancel, document */
7781 if (NULL != t->regex_ctx)
7784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7788 /* Find string itself */
7789 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7790 string = (const char *) &msg[1];
7792 /* Initialize context */
7793 size = GNUNET_REGEX_get_first_key (string, len, &key);
7794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7795 " consumed %u bits out of %u\n", size, len);
7796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7797 " looking for %s\n", GNUNET_h2s (&key));
7799 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7801 info->description = GNUNET_strndup (string, len);
7802 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7803 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7806 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7807 ctx->position = size;
7811 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7813 /* Start search in DHT */
7814 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7815 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7816 &key, /* key to search */
7817 dht_replication_level, /* replication level */
7818 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7819 &info->description[size], /* xquery */
7820 // FIXME add BLOOMFILTER to exclude filtered peers
7821 len + 1 - size, /* xquery bits */
7822 // FIXME add BLOOMFILTER SIZE
7823 &dht_get_string_handler, ctx);
7825 GNUNET_break (GNUNET_OK ==
7826 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7829 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7831 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7837 * Handler for client traffic directed to one peer
7839 * @param cls closure
7840 * @param client identification of the client
7841 * @param message the actual message
7844 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7845 const struct GNUNET_MessageHeader *message)
7847 struct MeshClient *c;
7848 struct MeshTunnel *t;
7849 struct MeshPeerInfo *pi;
7850 struct GNUNET_MESH_Unicast *data_msg;
7851 MESH_TunnelNumber tid;
7854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7855 "Got a unicast request from a client!\n");
7857 /* Sanity check for client registration */
7858 if (NULL == (c = client_get (client)))
7861 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7866 data_msg = (struct GNUNET_MESH_Unicast *) message;
7868 /* Sanity check for message size */
7869 size = ntohs (message->size);
7870 if (sizeof (struct GNUNET_MESH_Unicast) +
7871 sizeof (struct GNUNET_MessageHeader) > size)
7874 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7878 /* Tunnel exists? */
7879 tid = ntohl (data_msg->tid);
7880 t = tunnel_get_by_local_id (c, tid);
7884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7888 /* Is it a local tunnel? Then, does client own the tunnel? */
7889 if (t->owner->handle != client)
7892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7896 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7897 &data_msg->destination.hashPubKey);
7898 /* Is the selected peer in the tunnel? */
7902 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7906 /* PID should be as expected */
7907 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7910 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7911 "Unicast PID, expected %u, got %u\n",
7912 t->fwd_pid + 1, ntohl (data_msg->pid));
7913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7917 /* Ok, everything is correct, send the message
7918 * (pretend we got it from a mesh peer)
7921 /* Work around const limitation */
7922 char buf[ntohs (message->size)] GNUNET_ALIGN;
7923 struct GNUNET_MESH_Unicast *copy;
7925 copy = (struct GNUNET_MESH_Unicast *) buf;
7926 memcpy (buf, data_msg, size);
7927 copy->oid = my_full_id;
7928 copy->tid = htonl (t->id.tid);
7929 copy->ttl = htonl (default_ttl);
7930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7931 " calling generic handler...\n");
7932 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7935 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7942 * Handler for client traffic directed to the origin
7944 * @param cls closure
7945 * @param client identification of the client
7946 * @param message the actual message
7949 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7950 const struct GNUNET_MessageHeader *message)
7952 struct GNUNET_MESH_ToOrigin *data_msg;
7953 struct MeshTunnelClientInfo *clinfo;
7954 struct MeshClient *c;
7955 struct MeshTunnel *t;
7956 MESH_TunnelNumber tid;
7959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7960 "Got a ToOrigin request from a client!\n");
7961 /* Sanity check for client registration */
7962 if (NULL == (c = client_get (client)))
7965 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7970 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7972 /* Sanity check for message size */
7973 size = ntohs (message->size);
7974 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7975 sizeof (struct GNUNET_MessageHeader) > size)
7978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7982 /* Tunnel exists? */
7983 tid = ntohl (data_msg->tid);
7984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7985 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7991 t = tunnel_get_by_local_id (c, tid);
7994 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7997 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8001 /* It should be sent by someone who has this as incoming tunnel. */
8002 if (GNUNET_NO == client_knows_tunnel (c, t))
8005 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8009 /* PID should be as expected */
8010 clinfo = tunnel_get_client_fc (t, c);
8011 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
8014 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8015 "To Origin PID, expected %u, got %u\n",
8016 clinfo->bck_pid + 1,
8017 ntohl (data_msg->pid));
8018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8023 /* Ok, everything is correct, send the message
8024 * (pretend we got it from a mesh peer)
8027 char buf[ntohs (message->size)] GNUNET_ALIGN;
8028 struct GNUNET_MESH_ToOrigin *copy;
8030 /* Work around const limitation */
8031 copy = (struct GNUNET_MESH_ToOrigin *) buf;
8032 memcpy (buf, data_msg, size);
8033 GNUNET_PEER_resolve (t->id.oid, ©->oid);
8034 copy->tid = htonl (t->id.tid);
8035 copy->ttl = htonl (default_ttl);
8036 copy->pid = htonl (t->bck_pid + 1);
8038 copy->sender = my_full_id;
8039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8040 " calling generic handler...\n");
8041 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
8043 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8050 * Handler for client traffic directed to all peers in a tunnel
8052 * @param cls closure
8053 * @param client identification of the client
8054 * @param message the actual message
8057 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
8058 const struct GNUNET_MessageHeader *message)
8060 struct MeshClient *c;
8061 struct MeshTunnel *t;
8062 struct GNUNET_MESH_Multicast *data_msg;
8063 MESH_TunnelNumber tid;
8065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8066 "Got a multicast request from a client!\n");
8068 /* Sanity check for client registration */
8069 if (NULL == (c = client_get (client)))
8072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8077 data_msg = (struct GNUNET_MESH_Multicast *) message;
8079 /* Sanity check for message size */
8080 if (sizeof (struct GNUNET_MESH_Multicast) +
8081 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
8084 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8088 /* Tunnel exists? */
8089 tid = ntohl (data_msg->tid);
8090 t = tunnel_get_by_local_id (c, tid);
8094 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8095 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8101 /* Does client own tunnel? */
8102 if (t->owner->handle != client)
8105 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8109 /* PID should be as expected */
8110 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
8113 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8114 "Multicast PID, expected %u, got %u\n",
8115 t->fwd_pid + 1, ntohl (data_msg->pid));
8116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8121 char buf[ntohs (message->size)] GNUNET_ALIGN;
8122 struct GNUNET_MESH_Multicast *copy;
8124 copy = (struct GNUNET_MESH_Multicast *) buf;
8125 memcpy (buf, message, ntohs (message->size));
8126 copy->oid = my_full_id;
8127 copy->tid = htonl (t->id.tid);
8128 copy->ttl = htonl (default_ttl);
8129 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
8130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8131 " calling generic handler...\n");
8132 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
8135 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
8141 * Handler for client's ACKs for payload traffic.
8143 * @param cls Closure (unused).
8144 * @param client Identification of the client.
8145 * @param message The actual message.
8148 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
8149 const struct GNUNET_MessageHeader *message)
8151 struct GNUNET_MESH_LocalAck *msg;
8152 struct MeshTunnel *t;
8153 struct MeshClient *c;
8154 MESH_TunnelNumber tid;
8157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
8158 /* Sanity check for client registration */
8159 if (NULL == (c = client_get (client)))
8162 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8167 msg = (struct GNUNET_MESH_LocalAck *) message;
8169 /* Tunnel exists? */
8170 tid = ntohl (msg->tunnel_id);
8171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8172 t = tunnel_get_by_local_id (c, tid);
8176 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8177 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8182 ack = ntohl (msg->max_pid);
8183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8185 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8186 if (NULL != t->owner && t->owner->handle == client)
8188 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8190 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8194 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8195 tunnel_set_client_fwd_ack (t, c, ack);
8196 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8199 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8206 * Iterator over all peers to send a monitoring client info about a tunnel.
8208 * @param cls Closure (message being built).
8209 * @param key Key (hashed tunnel ID, unused).
8210 * @param value Peer info.
8212 * @return GNUNET_YES, to keep iterating.
8215 monitor_peers_iterator (void *cls,
8216 const struct GNUNET_HashCode * key,
8219 struct GNUNET_MESH_LocalMonitor *msg = cls;
8220 struct GNUNET_PeerIdentity *id;
8221 struct MeshPeerInfo *info = value;
8223 id = (struct GNUNET_PeerIdentity *) &msg[1];
8224 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8227 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8228 "* sending info about peer %s [%u]\n",
8229 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8237 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8239 * @param cls Closure (client handle).
8240 * @param key Key (hashed tunnel ID, unused).
8241 * @param value Tunnel info.
8243 * @return GNUNET_YES, to keep iterating.
8246 monitor_all_tunnels_iterator (void *cls,
8247 const struct GNUNET_HashCode * key,
8250 struct GNUNET_SERVER_Client *client = cls;
8251 struct MeshTunnel *t = value;
8252 struct GNUNET_MESH_LocalMonitor *msg;
8255 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8256 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8257 npeers * sizeof (struct GNUNET_PeerIdentity));
8258 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8259 msg->tunnel_id = htonl (t->id.tid);
8260 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8261 npeers * sizeof (struct GNUNET_PeerIdentity));
8262 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8264 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8265 monitor_peers_iterator,
8268 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8269 "* sending info about tunnel %s [%u] (%u peers)\n",
8270 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8272 if (msg->npeers != npeers)
8275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8276 "Get tunnels fail: size %u - iter %u\n",
8277 npeers, msg->npeers);
8280 msg->npeers = htonl (npeers);
8281 GNUNET_SERVER_notification_context_unicast (nc, client,
8282 &msg->header, GNUNET_NO);
8288 * Handler for client's MONITOR request.
8290 * @param cls Closure (unused).
8291 * @param client Identification of the client.
8292 * @param message The actual message.
8295 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8296 const struct GNUNET_MessageHeader *message)
8298 struct MeshClient *c;
8300 /* Sanity check for client registration */
8301 if (NULL == (c = client_get (client)))
8304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8308 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8309 "Received get tunnels request from client %u\n",
8311 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8312 monitor_all_tunnels_iterator,
8314 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8315 "Get tunnels request from client %u completed\n",
8317 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8322 * Data needed to build a Monitor_Tunnel message.
8324 struct MeshMonitorTunnelContext
8327 * Partial message, including peer count.
8329 struct GNUNET_MESH_LocalMonitor *msg;
8332 * Hashmap with positions: peer->position.
8334 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8337 * Index of the parent of each peer in the message, realtive to the absolute
8338 * order in the array (can be in a previous message).
8340 uint32_t parents[1024];
8343 * Peers visited so far in the tree, aka position of the current peer.
8345 unsigned int npeers;
8348 * Client requesting the info.
8350 struct MeshClient *c;
8355 * Send a client a message about the structure of a tunnel.
8357 * @param ctx Context of the tunnel iteration, with info regarding the state
8358 * of the execution and the number of peers visited for this message.
8361 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8363 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8364 struct GNUNET_PeerIdentity *pid;
8365 unsigned int *parent;
8368 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8369 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8370 resp->header.size = htons (size);
8371 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8372 parent = (unsigned int *) &pid[resp->npeers];
8373 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8374 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8375 &resp->header, GNUNET_NO);
8379 * Iterator over a tunnel tree to build a message containing all peers
8380 * the in the tunnel, including relay nodes.
8382 * @param cls Closure (pointer to pointer of message being built).
8383 * @param peer Short ID of a peer.
8384 * @param parent Short ID of the @c peer 's parent.
8387 tunnel_tree_iterator (void *cls,
8388 GNUNET_PEER_Id peer,
8389 GNUNET_PEER_Id parent)
8391 struct MeshMonitorTunnelContext *ctx = cls;
8392 struct GNUNET_MESH_LocalMonitor *msg;
8393 struct GNUNET_PeerIdentity *pid;
8394 struct GNUNET_PeerIdentity ppid;
8397 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8398 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8399 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8400 &pid[msg->npeers].hashPubKey,
8401 (void *) (long) ctx->npeers,
8402 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8403 GNUNET_PEER_resolve (parent, &ppid);
8404 ctx->parents[msg->npeers] =
8405 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8411 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8413 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8416 send_client_tunnel_info (ctx);
8423 * Handler for client's MONITOR_TUNNEL request.
8425 * @param cls Closure (unused).
8426 * @param client Identification of the client.
8427 * @param message The actual message.
8430 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8431 const struct GNUNET_MessageHeader *message)
8433 const struct GNUNET_MESH_LocalMonitor *msg;
8434 struct GNUNET_MESH_LocalMonitor *resp;
8435 struct MeshMonitorTunnelContext ctx;
8436 struct MeshClient *c;
8437 struct MeshTunnel *t;
8439 /* Sanity check for client registration */
8440 if (NULL == (c = client_get (client)))
8443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8447 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8448 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8449 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8452 ntohl (msg->tunnel_id));
8453 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8456 /* We don't know the tunnel */
8457 struct GNUNET_MESH_LocalMonitor warn;
8460 warn.npeers = htonl (UINT_MAX);
8461 GNUNET_SERVER_notification_context_unicast (nc, client,
8464 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8468 /* Initialize context */
8469 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8473 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8477 /* Collect and send information */
8478 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8479 send_client_tunnel_info (&ctx);
8482 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8485 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8486 "Monitor tunnel request from client %u completed\n",
8488 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8493 * Functions to handle messages from clients
8495 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8496 {&handle_local_new_client, NULL,
8497 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8498 {&handle_local_announce_regex, NULL,
8499 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8500 {&handle_local_tunnel_create, NULL,
8501 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8502 sizeof (struct GNUNET_MESH_TunnelMessage)},
8503 {&handle_local_tunnel_destroy, NULL,
8504 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8505 sizeof (struct GNUNET_MESH_TunnelMessage)},
8506 {&handle_local_tunnel_speed, NULL,
8507 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8508 sizeof (struct GNUNET_MESH_TunnelMessage)},
8509 {&handle_local_tunnel_speed, NULL,
8510 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8511 sizeof (struct GNUNET_MESH_TunnelMessage)},
8512 {&handle_local_tunnel_buffer, NULL,
8513 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8514 sizeof (struct GNUNET_MESH_TunnelMessage)},
8515 {&handle_local_tunnel_buffer, NULL,
8516 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8517 sizeof (struct GNUNET_MESH_TunnelMessage)},
8518 {&handle_local_connect_add, NULL,
8519 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8520 sizeof (struct GNUNET_MESH_PeerControl)},
8521 {&handle_local_connect_del, NULL,
8522 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8523 sizeof (struct GNUNET_MESH_PeerControl)},
8524 {&handle_local_blacklist, NULL,
8525 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8526 sizeof (struct GNUNET_MESH_PeerControl)},
8527 {&handle_local_unblacklist, NULL,
8528 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8529 sizeof (struct GNUNET_MESH_PeerControl)},
8530 {&handle_local_connect_by_type, NULL,
8531 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8532 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8533 {&handle_local_connect_by_string, NULL,
8534 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8535 {&handle_local_unicast, NULL,
8536 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8537 {&handle_local_to_origin, NULL,
8538 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8539 {&handle_local_multicast, NULL,
8540 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8541 {&handle_local_ack, NULL,
8542 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8543 sizeof (struct GNUNET_MESH_LocalAck)},
8544 {&handle_local_get_tunnels, NULL,
8545 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8546 sizeof (struct GNUNET_MessageHeader)},
8547 {&handle_local_show_tunnel, NULL,
8548 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8549 sizeof (struct GNUNET_MESH_LocalMonitor)},
8555 * To be called on core init/fail.
8557 * @param cls service closure
8558 * @param server handle to the server for this service
8559 * @param identity the public identity of this peer
8562 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8563 const struct GNUNET_PeerIdentity *identity)
8566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8567 core_handle = server;
8568 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8574 GNUNET_i2s (identity));
8575 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8577 GNUNET_i2s (&my_full_id));
8578 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8580 GNUNET_abort(); // Try harder
8587 * Method called whenever a given peer connects.
8589 * @param cls closure
8590 * @param peer peer identity this notification is about
8591 * @param atsi performance data for the connection
8592 * @param atsi_count number of records in 'atsi'
8595 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8596 const struct GNUNET_ATS_Information *atsi,
8597 unsigned int atsi_count)
8599 struct MeshPeerInfo *peer_info;
8600 struct MeshPeerPath *path;
8602 DEBUG_CONN ("Peer connected\n");
8603 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8604 peer_info = peer_info_get (peer);
8605 if (myid == peer_info->id)
8607 DEBUG_CONN (" (self)\n");
8612 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8614 path = path_new (2);
8615 path->peers[0] = myid;
8616 path->peers[1] = peer_info->id;
8617 GNUNET_PEER_change_rc (myid, 1);
8618 GNUNET_PEER_change_rc (peer_info->id, 1);
8619 peer_info_add_path (peer_info, path, GNUNET_YES);
8620 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8626 * Method called whenever a peer disconnects.
8628 * @param cls closure
8629 * @param peer peer identity this notification is about
8632 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8634 struct MeshPeerInfo *pi;
8635 struct MeshPeerQueue *q;
8636 struct MeshPeerQueue *n;
8638 DEBUG_CONN ("Peer disconnected\n");
8639 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8649 /* TODO try to reroute this traffic instead */
8650 queue_destroy(q, GNUNET_YES);
8653 if (NULL != pi->core_transmit)
8655 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8656 pi->core_transmit = NULL;
8658 peer_info_remove_path (pi, pi->id, myid);
8661 DEBUG_CONN (" (self)\n");
8663 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8668 /******************************************************************************/
8669 /************************ MAIN FUNCTIONS ****************************/
8670 /******************************************************************************/
8673 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8675 * @param cls closure
8676 * @param key current key code
8677 * @param value value in the hash map
8678 * @return GNUNET_YES if we should continue to iterate,
8682 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8684 struct MeshTunnel *t = value;
8691 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8693 * @param cls closure
8694 * @param key current key code
8695 * @param value value in the hash map
8696 * @return GNUNET_YES if we should continue to iterate,
8700 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8702 struct MeshPeerInfo *p = value;
8703 struct MeshPeerQueue *q;
8704 struct MeshPeerQueue *n;
8712 queue_destroy(q, GNUNET_YES);
8716 peer_info_destroy (p);
8722 * Task run during shutdown.
8728 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8732 if (core_handle != NULL)
8734 GNUNET_CORE_disconnect (core_handle);
8739 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8742 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8743 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8744 if (dht_handle != NULL)
8746 GNUNET_DHT_disconnect (dht_handle);
8751 GNUNET_SERVER_notification_context_destroy (nc);
8754 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8756 GNUNET_SCHEDULER_cancel (announce_id_task);
8757 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8759 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8761 GNUNET_SCHEDULER_cancel (announce_applications_task);
8762 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8769 * Callback for hostkey read/generation
8771 * @param cls Closure (Configuration handle).
8772 * @param pk the private key
8773 * @param emsg error message
8776 key_generation_cb (void *cls,
8777 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8780 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8781 struct MeshPeerInfo *peer;
8782 struct MeshPeerPath *p;
8787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8788 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8790 GNUNET_SCHEDULER_shutdown ();
8793 my_private_key = pk;
8794 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8795 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8796 &my_full_id.hashPubKey);
8797 myid = GNUNET_PEER_intern (&my_full_id);
8798 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8799 "Mesh for peer [%s] starting\n",
8800 GNUNET_i2s(&my_full_id));
8802 // transport_handle = GNUNET_TRANSPORT_connect(c,
8809 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8810 NULL, /* Closure passed to MESH functions */
8811 &core_init, /* Call core_init once connected */
8812 &core_connect, /* Handle connects */
8813 &core_disconnect, /* remove peers on disconnects */
8814 NULL, /* Don't notify about all incoming messages */
8815 GNUNET_NO, /* For header only in notification */
8816 NULL, /* Don't notify about all outbound messages */
8817 GNUNET_NO, /* For header-only out notification */
8818 core_handlers); /* Register these handlers */
8820 if (core_handle == NULL)
8823 GNUNET_SCHEDULER_shutdown ();
8828 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8831 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8832 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8833 GNUNET_SERVER_disconnect_notify (server_handle,
8834 &handle_local_client_disconnect, NULL);
8838 clients_tail = NULL;
8841 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8842 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8844 /* Create a peer_info for the local peer */
8845 peer = peer_info_get (&my_full_id);
8848 GNUNET_PEER_change_rc (myid, 1);
8849 peer_info_add_path (peer, p, GNUNET_YES);
8850 GNUNET_SERVER_resume (server_handle);
8851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8856 * Process mesh requests.
8858 * @param cls closure
8859 * @param server the initialized server
8860 * @param c configuration to use
8863 run (void *cls, struct GNUNET_SERVER_Handle *server,
8864 const struct GNUNET_CONFIGURATION_Handle *c)
8868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8869 server_handle = server;
8872 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8875 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8877 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8879 GNUNET_SCHEDULER_shutdown ();
8884 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8885 &refresh_path_time))
8887 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8889 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8890 "mesh", "refresh path time");
8891 GNUNET_SCHEDULER_shutdown ();
8896 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8897 &app_announce_time))
8899 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8901 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8902 "mesh", "app announce time");
8903 GNUNET_SCHEDULER_shutdown ();
8906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8907 "APP_ANNOUNCE_TIME %llu ms\n",
8908 app_announce_time.rel_value);
8910 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8915 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8916 "mesh", "id announce time");
8917 GNUNET_SCHEDULER_shutdown ();
8922 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8927 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8928 "mesh", "connect timeout");
8929 GNUNET_SCHEDULER_shutdown ();
8934 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8939 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8940 "mesh", "max msgs queue");
8941 GNUNET_SCHEDULER_shutdown ();
8946 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8949 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8951 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8952 "mesh", "max tunnels");
8953 GNUNET_SCHEDULER_shutdown ();
8958 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8961 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8963 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8964 "mesh", "default ttl", 64);
8969 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8972 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8973 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8974 "mesh", "max peers", 1000);
8979 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8980 &dht_replication_level))
8982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8984 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8985 "mesh", "dht replication level", 3);
8986 dht_replication_level = 3;
8989 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8990 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8991 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8992 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8993 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8995 dht_handle = GNUNET_DHT_connect (c, 64);
8996 if (NULL == dht_handle)
9000 stats = GNUNET_STATISTICS_create ("mesh", c);
9002 GNUNET_SERVER_suspend (server_handle);
9003 /* Scheduled the task to clean up when shutdown is called */
9004 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
9006 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
9009 GNUNET_free (keyfile);
9014 * The main function for the mesh service.
9016 * @param argc number of arguments from the command line
9017 * @param argv command line arguments
9018 * @return 0 ok, 1 on error
9021 main (int argc, char *const *argv)
9026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
9027 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
9029 ret = (GNUNET_OK == r) ? 0 : 1;
9030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
9034 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
9035 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
9036 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);