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_POLL_TIME GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
82 struct timespec __mesh_start;
83 struct timespec __mesh_end;
84 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
85 #define INTERVAL_END \
87 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
88 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
89 if (__diff < 0) __diff += 1000000000;\
93 #define INTERVAL_SHOW \
95 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
97 #define INTERVAL_START
102 /******************************************************************************/
103 /************************ DATA STRUCTURES ****************************/
104 /******************************************************************************/
106 /** FWD declaration */
112 * Struct representing a piece of data being sent to other peers
116 /** Tunnel it belongs to. */
117 struct MeshTunnel *t;
119 /** How many remaining neighbors still hav't got it. */
120 unsigned int reference_counter;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int total_out;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct to store regex information announced by clients.
178 struct MeshRegexDescriptor
181 * Regular expression itself.
185 struct GNUNET_REGEX_Automaton *dfa;
188 * How many characters per edge can we squeeze?
190 uint16_t compression;
194 * Struct containing all info possibly needed to build a package when called
197 struct MeshTransmissionDescriptor
199 /** ID of the tunnel this packet travels in */
200 struct MESH_TunnelID *origin;
202 /** Who was this message being sent to */
203 struct MeshPeerInfo *peer;
205 /** Ultimate destination of the packet */
206 GNUNET_PEER_Id destination;
208 /** Data descriptor */
209 struct MeshData* mesh_data;
214 * Struct containing all information regarding a given peer
224 * Last time we heard from this peer
226 struct GNUNET_TIME_Absolute last_contact;
229 * Task handler for delayed connect task;
231 GNUNET_SCHEDULER_TaskIdentifier connect_task;
234 * Number of attempts to reconnect so far
236 int n_reconnect_attempts;
239 * Paths to reach the peer, ordered by ascending hop count
241 struct MeshPeerPath *path_head;
244 * Paths to reach the peer, ordered by ascending hop count
246 struct MeshPeerPath *path_tail;
249 * Handle to stop the DHT search for a path to this peer
251 struct GNUNET_DHT_GetHandle *dhtget;
254 * Closure given to the DHT GET
256 struct MeshPathInfo *dhtgetcls;
259 * Array of tunnels this peer participates in
260 * (most probably a small amount, therefore not a hashmap)
261 * When the path to the peer changes, notify these tunnels to let them
262 * re-adjust their path trees.
264 struct MeshTunnel **tunnels;
267 * Number of tunnels this peers participates in
269 unsigned int ntunnels;
272 * Transmission queue to core DLL head
274 struct MeshPeerQueue *queue_head;
277 * Transmission queue to core DLL tail
279 struct MeshPeerQueue *queue_tail;
282 * How many messages are in the queue to this peer.
284 unsigned int queue_n;
287 * Handle to for queued transmissions
289 struct GNUNET_CORE_TransmitHandle *core_transmit;
294 * Globally unique tunnel identification (owner + number)
295 * DO NOT USE OVER THE NETWORK
300 * Node that owns the tunnel
305 * Tunnel number to differentiate all the tunnels owned by the node oid
306 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
308 MESH_TunnelNumber tid;
313 * Struct containing all information regarding a tunnel
314 * For an intermediate node the improtant info used will be:
315 * - id Tunnel unique identification
316 * - paths[0] To know where to send it next
317 * - metainfo: ready, speeds, accounting
324 struct MESH_TunnelID id;
327 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
329 MESH_TunnelNumber local_tid;
332 * Local tunnel number for local destination clients (incoming number)
333 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
336 MESH_TunnelNumber local_tid_dest;
339 * Is the speed on the tunnel limited to the slowest peer?
344 * Is the tunnel bufferless (minimum latency)?
349 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
354 * Packet ID of the last bck packet sent (unique counter per hop).
359 * SKIP value for this tunnel.
364 * Force sending ACK? Flag to allow duplicate ACK on POLL.
369 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
370 * Contains the Flow Control info: FWD ACK value received,
371 * last BCK ACK sent, PID and SKIP values.
373 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
376 * Last ACK sent towards the origin (for traffic towards leaf node).
378 uint32_t last_fwd_ack;
381 * BCK ACK value received from the hop towards the owner of the tunnel,
382 * (previous node / owner): up to what message PID can we sent back to him.
387 * How many messages are in the forward queue (towards leaves).
389 unsigned int fwd_queue_n;
392 * How many messages do we accept in the forward queue.
394 unsigned int fwd_queue_max;
397 * How many messages are in the backward queue (towards origin).
399 unsigned int bck_queue_n;
402 * How many messages do we accept in the backward queue.
404 unsigned int bck_queue_max;
407 * Task to poll peer in case of a stall.
409 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
412 * Last time the tunnel was used
414 struct GNUNET_TIME_Absolute timestamp;
417 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
418 * containing peers added by id or by type, not intermediate peers.
420 struct GNUNET_CONTAINER_MultiHashMap *peers;
423 * Number of peers that are connected and potentially ready to receive data
425 unsigned int peers_ready;
428 * Number of peers that have been added to the tunnel
430 unsigned int peers_total;
433 * Client owner of the tunnel, if any
435 struct MeshClient *owner;
438 * Clients that have been informed about and want to stay in the tunnel.
440 struct MeshClient **clients;
443 * Flow control info for each client.
445 struct MeshTunnelClientInfo *clients_fc;
448 * Number of elements in clients/clients_fc
450 unsigned int nclients;
453 * Clients that have been informed but requested to leave the tunnel.
455 struct MeshClient **ignore;
458 * Number of elements in clients
460 unsigned int nignore;
465 GNUNET_PEER_Id *blacklisted;
468 * Number of elements in blacklisted
470 unsigned int nblacklisted;
473 * Bloomfilter (for peer identities) to stop circular routes
475 char bloomfilter[MESH_BLOOM_SIZE];
480 struct MeshTunnelTree *tree;
483 * Application type we are looking for in this tunnel
485 GNUNET_MESH_ApplicationType type;
488 * Used to search peers offering a service
490 struct GNUNET_DHT_GetHandle *dht_get_type;
493 * Initial context of the regex search for a connect_by_string
495 struct MeshRegexSearchContext *regex_ctx;
498 * Task to keep the used paths alive
500 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
503 * Task to destroy the tunnel after timeout
505 * FIXME: merge the two? a tunnel will have either
506 * a path refresh OR a timeout, never both!
508 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
511 * Flag to signal the destruction of the tunnel.
512 * If this is set GNUNET_YES the tunnel will be destroyed
513 * when the queue is empty.
518 * Total messages pending for this tunnels, payload or not.
520 unsigned int pending_messages;
525 * Info about a child node in a tunnel, needed to perform flow control.
527 struct MeshTunnelChildInfo
530 * ID of the child node.
550 * Maximum PID allowed (FWD ACK received).
555 * Last ACK sent to that child (BCK ACK).
560 * Circular buffer pointing to MeshPeerQueue elements for all
561 * payload traffic going to this child.
562 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
564 struct MeshPeerQueue **send_buffer;
567 * Index of the oldest element in the send_buffer.
569 unsigned int send_buffer_start;
572 * How many elements are already in the buffer.
574 unsigned int send_buffer_n;
577 * Tunnel this info is about
579 struct MeshTunnel *t;
582 * Task to poll peer in case of a stall.
584 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
589 * Info about a leaf client of a tunnel, needed to perform flow control.
591 struct MeshTunnelClientInfo
594 * PID of the last packet sent to the client (FWD).
599 * PID of the last packet received from the client (BCK).
604 * Maximum PID allowed (FWD ACK received).
609 * Last ACK sent to that child (BCK ACK).
617 * Info collected during iteration of child nodes in order to get the ACK value
620 struct MeshTunnelChildIteratorContext
623 * Tunnel whose info is being collected.
625 struct MeshTunnel *t;
628 * Is this context initialized? Is the value in max_child_ack valid?
633 * Maximum child ACK so far.
635 uint32_t max_child_ack;
638 * Number of children nodes
640 unsigned int nchildren;
645 * Info needed to work with tunnel paths and peers
652 struct MeshTunnel *t;
655 * Neighbouring peer to whom we send the packet to
657 struct MeshPeerInfo *peer;
662 struct MeshPeerPath *path;
667 * Struct containing information about a client of the service
674 struct MeshClient *next;
679 struct MeshClient *prev;
682 * Tunnels that belong to this client, indexed by local id
684 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
687 * Tunnels this client has accepted, indexed by incoming local id
689 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
692 * Tunnels this client has rejected, indexed by incoming local id
694 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
696 * Handle to communicate with the client
698 struct GNUNET_SERVER_Client *handle;
701 * Applications that this client has claimed to provide
703 struct GNUNET_CONTAINER_MultiHashMap *apps;
706 * Messages that this client has declared interest in
708 struct GNUNET_CONTAINER_MultiHashMap *types;
711 * Whether the client is active or shutting down (don't send confirmations
712 * to a client that is shutting down.
717 * ID of the client, mainly for debug messages
722 * Regular expressions describing the services offered by this client.
724 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
727 * Number of regular expressions in regexes.
729 unsigned int n_regex;
732 * Task to refresh all regular expresions in the DHT.
734 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
740 * Struct to keep information of searches of services described by a regex
741 * using a user-provided string service description.
743 struct MeshRegexSearchInfo
746 * Which tunnel is this for
748 struct MeshTunnel *t;
751 * User provided description of the searched service.
756 * Part of the description already consumed by the search.
763 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
766 * Results from running DHT GETs.
768 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
771 * Contexts, for each running DHT GET. Free all on end of search.
773 struct MeshRegexSearchContext **contexts;
776 * Number of contexts (branches/steps in search).
778 unsigned int n_contexts;
781 * Peer that is connecting via connect_by_string. When connected, free ctx.
786 * Other peers that are found but not yet being connected to.
788 GNUNET_PEER_Id *peers;
791 * Number of elements in peers.
793 unsigned int n_peers;
796 * Next peer to try to connect to.
801 * Timeout for a connect attempt.
802 * When reached, try to connect to a different peer, if any. If not,
803 * try the same peer again.
805 GNUNET_SCHEDULER_TaskIdentifier timeout;
810 * Struct to keep state of running searches that have consumed a part of
813 struct MeshRegexSearchContext
816 * Part of the description already consumed by
817 * this particular search branch.
822 * Information about the search.
824 struct MeshRegexSearchInfo *info;
827 * We just want to look for one edge, the longer the better.
830 unsigned int longest_match;
833 * Destination hash of the longest match.
835 struct GNUNET_HashCode hash;
838 /******************************************************************************/
839 /************************ DEBUG FUNCTIONS ****************************/
840 /******************************************************************************/
844 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
845 * @param cls string to print
846 * @param success GNUNET_OK if the PUT was transmitted,
847 * GNUNET_NO on timeout,
848 * GNUNET_SYSERR on disconnect from service
849 * after the PUT message was transmitted
850 * (so we don't know if it was received or not)
855 mesh_debug (void *cls, int success)
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
863 unsigned int debug_fwd_ack;
864 unsigned int debug_bck_ack;
868 /******************************************************************************/
869 /*********************** GLOBAL VARIABLES ****************************/
870 /******************************************************************************/
872 /************************** Configuration parameters **************************/
875 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
877 static struct GNUNET_TIME_Relative refresh_path_time;
880 * How often to PUT local application numbers in the DHT.
882 static struct GNUNET_TIME_Relative app_announce_time;
885 * How often to PUT own ID in the DHT.
887 static struct GNUNET_TIME_Relative id_announce_time;
890 * Maximum time allowed to connect to a peer found by string.
892 static struct GNUNET_TIME_Relative connect_timeout;
895 * Default TTL for payload packets.
897 static unsigned long long default_ttl;
900 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
902 static unsigned long long dht_replication_level;
905 * How many tunnels are we willing to maintain.
906 * Local tunnels are always allowed, even if there are more tunnels than max.
908 static unsigned long long max_tunnels;
911 * How many messages *in total* are we willing to queue, divided by number of
912 * tunnels to get tunnel queue size.
914 static unsigned long long max_msgs_queue;
917 /*************************** Static global variables **************************/
920 * Hostkey generation context
922 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
925 * DLL with all the clients, head.
927 static struct MeshClient *clients;
930 * DLL with all the clients, tail.
932 static struct MeshClient *clients_tail;
935 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
937 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
940 * Number of tunnels known.
942 static unsigned long long n_tunnels;
945 * Tunnels incoming, indexed by MESH_TunnelNumber
946 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
948 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
951 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
953 static struct GNUNET_CONTAINER_MultiHashMap *peers;
956 * Handle to communicate with transport
958 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
961 * Handle to communicate with core.
963 static struct GNUNET_CORE_Handle *core_handle;
968 static struct GNUNET_DHT_Handle *dht_handle;
973 static struct GNUNET_SERVER_Handle *server_handle;
976 * Handle to the statistics service.
978 static struct GNUNET_STATISTICS_Handle *stats;
981 * Notification context, to send messages to local clients.
983 static struct GNUNET_SERVER_NotificationContext *nc;
986 * Local peer own ID (memory efficient handle).
988 static GNUNET_PEER_Id myid;
991 * Local peer own ID (full value).
993 static struct GNUNET_PeerIdentity my_full_id;
998 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1003 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1006 * Tunnel ID for the next created tunnel (global tunnel number).
1008 static MESH_TunnelNumber next_tid;
1011 * Tunnel ID for the next incoming tunnel (local tunnel number).
1013 static MESH_TunnelNumber next_local_tid;
1016 * All application types provided by this peer.
1018 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1021 * All message types clients of this peer are interested in.
1023 static struct GNUNET_CONTAINER_MultiHashMap *types;
1026 * Task to periodically announce provided applications.
1028 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1031 * Task to periodically announce itself in the network.
1033 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1036 * Next ID to assign to a client.
1038 unsigned int next_client_id;
1041 /******************************************************************************/
1042 /*********************** DECLARATIONS **************************/
1043 /******************************************************************************/
1046 * Function to process paths received for a new peer addition. The recorded
1047 * paths form the initial tunnel, which can be optimized later.
1048 * Called on each result obtained for the DHT search.
1050 * @param cls closure
1051 * @param exp when will this value expire
1052 * @param key key of the result
1053 * @param type type of the result
1054 * @param size number of bytes in data
1055 * @param data pointer to the result data
1058 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1059 const struct GNUNET_HashCode * key,
1060 const struct GNUNET_PeerIdentity *get_path,
1061 unsigned int get_path_length,
1062 const struct GNUNET_PeerIdentity *put_path,
1063 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1064 size_t size, const void *data);
1068 * Function to process DHT string to regex matching.
1069 * Called on each result obtained for the DHT search.
1071 * @param cls closure (search context)
1072 * @param exp when will this value expire
1073 * @param key key of the result
1074 * @param get_path path of the get request (not used)
1075 * @param get_path_length lenght of get_path (not used)
1076 * @param put_path path of the put request (not used)
1077 * @param put_path_length length of the put_path (not used)
1078 * @param type type of the result
1079 * @param size number of bytes in data
1080 * @param data pointer to the result data
1082 * TODO: re-issue the request after certain time? cancel after X results?
1085 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1086 const struct GNUNET_HashCode * key,
1087 const struct GNUNET_PeerIdentity *get_path,
1088 unsigned int get_path_length,
1089 const struct GNUNET_PeerIdentity *put_path,
1090 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1091 size_t size, const void *data);
1095 * Function to process DHT string to regex matching.
1096 * Called on each result obtained for the DHT search.
1098 * @param cls closure (search context)
1099 * @param exp when will this value expire
1100 * @param key key of the result
1101 * @param get_path path of the get request (not used)
1102 * @param get_path_length lenght of get_path (not used)
1103 * @param put_path path of the put request (not used)
1104 * @param put_path_length length of the put_path (not used)
1105 * @param type type of the result
1106 * @param size number of bytes in data
1107 * @param data pointer to the result data
1110 dht_get_string_accept_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,
1116 enum GNUNET_BLOCK_Type type,
1117 size_t size, const void *data);
1121 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1122 * and insert it in the appropiate structures if the peer is not known yet.
1124 * @param peer Short identity of the peer.
1126 * @return Existing or newly created peer info.
1128 static struct MeshPeerInfo *
1129 peer_info_get_short (const GNUNET_PEER_Id peer);
1133 * Try to establish a new connection to this peer.
1134 * Use the best path for the given tunnel.
1135 * If the peer doesn't have any path to it yet, try to get one.
1136 * If the peer already has some path, send a CREATE PATH towards it.
1138 * @param peer PeerInfo of the peer.
1139 * @param t Tunnel for which to create the path, if possible.
1142 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1146 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1147 * needed rescources.
1148 * If peer already exists, reevaluate shortest path and change if different.
1150 * @param t Tunnel we want to add a new peer to
1151 * @param peer PeerInfo of the peer being added
1155 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1159 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1160 * that are no longer needed, as well as nodes of no longer reachable peers.
1161 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1163 * @param t Tunnel from which to remove the path.
1164 * @param peer Short id of the peer which should be removed.
1167 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1171 * Search for a tunnel by global ID using full PeerIdentities.
1173 * @param oid owner of the tunnel.
1174 * @param tid global tunnel number.
1176 * @return tunnel handler, NULL if doesn't exist.
1178 static struct MeshTunnel *
1179 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1183 * Delete an active client from the tunnel.
1189 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1192 * Notify a tunnel that a connection has broken that affects at least
1193 * some of its peers.
1195 * @param t Tunnel affected.
1196 * @param p1 Peer that got disconnected from p2.
1197 * @param p2 Peer that got disconnected from p1.
1199 * @return Short ID of the peer disconnected (either p1 or p2).
1200 * 0 if the tunnel remained unaffected.
1202 static GNUNET_PEER_Id
1203 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1208 * Get the current ack value for a tunnel, for data going from root to leaves,
1209 * taking in account the tunnel mode and the status of all children and clients.
1213 * @return Maximum PID allowed.
1216 tunnel_get_fwd_ack (struct MeshTunnel *t);
1220 * Add a client to a tunnel, initializing all needed data structures.
1222 * @param t Tunnel to which add the client.
1223 * @param c Client which to add to the tunnel.
1226 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1230 * Jump to the next edge, with the longest matching token.
1232 * @param block Block found in the DHT.
1233 * @param size Size of the block.
1234 * @param ctx Context of the search.
1236 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1239 regex_next_edge (const struct MeshRegexBlock *block,
1241 struct MeshRegexSearchContext *ctx);
1245 * Find a path to a peer that offers a regex servcie compatible
1246 * with a given string.
1248 * @param key The key of the accepting state.
1249 * @param ctx Context containing info about the string, tunnel, etc.
1252 regex_find_path (const struct GNUNET_HashCode *key,
1253 struct MeshRegexSearchContext *ctx);
1257 * @brief Queue and pass message to core when possible.
1259 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1260 * and accounts for it. In case the queue is full, the message is dropped and
1263 * Otherwise, message is treated as internal and allowed to go regardless of
1266 * @param cls Closure (@c type dependant). It will be used by queue_send to
1267 * build the message to be sent if not already prebuilt.
1268 * @param type Type of the message, 0 for a raw message.
1269 * @param size Size of the message.
1270 * @param dst Neighbor to send message to.
1271 * @param t Tunnel this message belongs to.
1274 queue_add (void *cls, uint16_t type, size_t size,
1275 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1279 * Free a transmission that was already queued with all resources
1280 * associated to the request.
1282 * @param queue Queue handler to cancel.
1283 * @param clear_cls Is it necessary to free associated cls?
1286 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1290 * @brief Get the next transmittable message from the queue.
1292 * This will be the head, except in the case of being a data packet
1293 * not allowed by the destination peer.
1295 * @param peer Destination peer.
1297 * @return The next viable MeshPeerQueue element to send to that peer.
1298 * NULL when there are no transmittable messages.
1300 struct MeshPeerQueue *
1301 queue_get_next (const struct MeshPeerInfo *peer);
1305 * Core callback to write a queued packet to core buffer
1307 * @param cls Closure (peer info).
1308 * @param size Number of bytes available in buf.
1309 * @param buf Where the to write the message.
1311 * @return number of bytes written to buf
1314 queue_send (void *cls, size_t size, void *buf);
1316 /******************************************************************************/
1317 /************************ ITERATORS ****************************/
1318 /******************************************************************************/
1321 * Iterator over found existing mesh regex blocks that match an ongoing search.
1323 * @param cls closure
1324 * @param key current key code
1325 * @param value value in the hash map
1326 * @return GNUNET_YES if we should continue to iterate,
1330 regex_result_iterator (void *cls,
1331 const struct GNUNET_HashCode * key,
1334 struct MeshRegexBlock *block = value;
1335 struct MeshRegexSearchContext *ctx = cls;
1337 if (GNUNET_YES == ntohl(block->accepting) &&
1338 ctx->position == strlen (ctx->info->description))
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1341 regex_find_path (key, ctx);
1342 return GNUNET_YES; // We found an accept state!
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1347 ctx->position, strlen(ctx->info->description),
1348 ntohl(block->accepting));
1351 regex_next_edge(block, SIZE_MAX, ctx);
1353 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1360 * Iterator over edges in a regex block retrieved from the DHT.
1362 * @param cls Closure (context of the search).
1363 * @param token Token that follows to next state.
1364 * @param len Lenght of token.
1365 * @param key Hash of next state.
1367 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1370 regex_edge_iterator (void *cls,
1373 const struct GNUNET_HashCode *key)
1375 struct MeshRegexSearchContext *ctx = cls;
1376 struct MeshRegexSearchInfo *info = ctx->info;
1380 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1385 current = &info->description[ctx->position];
1386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1387 current_len = strlen (info->description) - ctx->position;
1388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1392 if (len > current_len)
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1395 return GNUNET_YES; // Token too long, wont match
1397 if (0 != strncmp (current, token, len))
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1400 return GNUNET_YES; // Token doesn't match
1403 if (len > ctx->longest_match)
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1406 ctx->longest_match = len;
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1420 * Jump to the next edge, with the longest matching token.
1422 * @param block Block found in the DHT.
1423 * @param size Size of the block.
1424 * @param ctx Context of the search.
1426 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1429 regex_next_edge (const struct MeshRegexBlock *block,
1431 struct MeshRegexSearchContext *ctx)
1433 struct MeshRegexSearchContext *new_ctx;
1434 struct MeshRegexSearchInfo *info = ctx->info;
1435 struct GNUNET_DHT_GetHandle *get_h;
1439 /* Find the longest match for the current string position,
1440 * among tokens in the given block */
1441 ctx->longest_match = 0;
1442 result = GNUNET_MESH_regex_block_iterate (block, size,
1443 ®ex_edge_iterator, ctx);
1444 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1446 /* Did anything match? */
1447 if (0 == ctx->longest_match)
1450 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1451 new_ctx->info = info;
1452 new_ctx->position = ctx->position + ctx->longest_match;
1453 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1455 /* Check whether we already have a DHT GET running for it */
1457 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1460 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1462 ®ex_result_iterator,
1464 // FIXME: "leaks" new_ctx? avoid keeping it around?
1465 return; // We are already looking for it
1468 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1470 /* Start search in DHT */
1471 rest = &new_ctx->info->description[new_ctx->position];
1473 GNUNET_DHT_get_start (dht_handle, /* handle */
1474 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1475 &ctx->hash, /* key to search */
1476 dht_replication_level, /* replication level */
1477 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1479 // FIXME add BLOOMFILTER to exclude filtered peers
1480 strlen(rest) + 1, /* xquery bits */
1481 // FIXME add BLOOMFILTER SIZE
1482 &dht_get_string_handler, new_ctx);
1484 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1487 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1496 * Iterator over hash map entries to cancel DHT GET requests after a
1497 * successful connect_by_string.
1499 * @param cls Closure (unused).
1500 * @param key Current key code (unused).
1501 * @param value Value in the hash map (get handle).
1502 * @return GNUNET_YES if we should continue to iterate,
1506 regex_cancel_dht_get (void *cls,
1507 const struct GNUNET_HashCode * key,
1510 struct GNUNET_DHT_GetHandle *h = value;
1512 GNUNET_DHT_get_stop (h);
1518 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1519 * search for connect_by_string.
1521 * @param cls Closure (unused).
1522 * @param key Current key code (unused).
1523 * @param value MeshRegexBlock in the hash map.
1524 * @return GNUNET_YES if we should continue to iterate,
1528 regex_free_result (void *cls,
1529 const struct GNUNET_HashCode * key,
1533 GNUNET_free (value);
1539 * Regex callback iterator to store own service description in the DHT.
1541 * @param cls closure.
1542 * @param key hash for current state.
1543 * @param proof proof for current state.
1544 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1545 * @param num_edges number of edges leaving current state.
1546 * @param edges edges leaving current state.
1549 regex_iterator (void *cls,
1550 const struct GNUNET_HashCode *key,
1553 unsigned int num_edges,
1554 const struct GNUNET_REGEX_Edge *edges)
1556 struct MeshRegexBlock *block;
1557 struct MeshRegexEdge *block_edge;
1558 enum GNUNET_DHT_RouteOption opt;
1562 unsigned int offset;
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566 " regex dht put for state %s\n",
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1576 if (GNUNET_YES == accepting)
1578 struct MeshRegexAccept block;
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1581 " state %s is accepting, putting own id\n",
1583 size = sizeof (block);
1585 block.id = my_full_id;
1587 GNUNET_DHT_put(dht_handle, key,
1588 dht_replication_level,
1589 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1590 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1593 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1598 len = strlen(proof);
1599 size = sizeof (struct MeshRegexBlock) + len;
1600 block = GNUNET_malloc (size);
1603 block->n_proof = htonl (len);
1604 block->n_edges = htonl (num_edges);
1605 block->accepting = htonl (accepting);
1607 /* Store the proof at the end of the block. */
1608 aux = (char *) &block[1];
1609 memcpy (aux, proof, len);
1612 /* Store each edge in a variable length MeshEdge struct at the
1613 * very end of the MeshRegexBlock structure.
1615 for (i = 0; i < num_edges; i++)
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 " edge %s towards %s\n",
1620 GNUNET_h2s(&edges[i].destination));
1622 /* aux points at the end of the last block */
1623 len = strlen (edges[i].label);
1624 size += sizeof (struct MeshRegexEdge) + len;
1625 // Calculate offset FIXME is this ok? use size instead?
1626 offset = aux - (char *) block;
1627 block = GNUNET_realloc (block, size);
1628 aux = &((char *) block)[offset];
1629 block_edge = (struct MeshRegexEdge *) aux;
1630 block_edge->key = edges[i].destination;
1631 block_edge->n_token = htonl (len);
1632 aux = (char *) &block_edge[1];
1633 memcpy (aux, edges[i].label, len);
1637 GNUNET_DHT_put(dht_handle, key,
1638 dht_replication_level,
1640 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1642 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1646 GNUNET_free (block);
1651 * Store the regular expression describing a local service into the DHT.
1653 * @param regex The regular expresion.
1656 regex_put (struct MeshRegexDescriptor *regex)
1658 if (NULL == regex->dfa)
1660 regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1661 strlen (regex->regex),
1662 regex->compression);
1665 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1666 GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL);
1667 DEBUG_DHT (" regex_put (%s) end\n", regex);
1671 * Find a path to a peer that offers a regex servcie compatible
1672 * with a given string.
1674 * @param key The key of the accepting state.
1675 * @param ctx Context containing info about the string, tunnel, etc.
1678 regex_find_path (const struct GNUNET_HashCode *key,
1679 struct MeshRegexSearchContext *ctx)
1681 struct GNUNET_DHT_GetHandle *get_h;
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1684 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1685 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1686 key, /* key to search */
1687 dht_replication_level, /* replication level */
1688 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1689 GNUNET_DHT_RO_RECORD_ROUTE,
1691 // FIXME add BLOOMFILTER to exclude filtered peers
1692 0, /* xquery bits */
1693 // FIXME add BLOOMFILTER SIZE
1694 &dht_get_string_accept_handler, ctx);
1695 GNUNET_break (GNUNET_OK ==
1696 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1699 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1704 * Function called if the connect attempt to a peer found via
1705 * connect_by_string times out. Try to connect to another peer, if any.
1706 * Otherwise try to reconnect to the same peer.
1708 * @param cls Closure (info about regex search).
1709 * @param tc TaskContext.
1712 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1714 struct MeshRegexSearchInfo *info = cls;
1715 struct MeshPeerInfo *peer_info;
1719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1720 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1721 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1729 if (0 < info->n_peers)
1731 // Select next peer, put current in that spot.
1732 id = info->peers[info->i_peer];
1733 info->peers[info->i_peer] = info->peer;
1734 info->i_peer = (info->i_peer + 1) % info->n_peers;
1738 // Try to connect to same peer again.
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1743 peer_info = peer_info_get_short(id);
1744 tunnel_add_peer (info->t, peer_info);
1746 tunnel_delete_peer (info->t, old);
1747 peer_info_connect (peer_info, info->t);
1748 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1749 ®ex_connect_timeout,
1751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1756 * Cancel an ongoing regex search in the DHT and free all resources.
1758 * @param ctx The search context.
1761 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1763 struct MeshRegexSearchInfo *info = ctx->info;
1766 GNUNET_free (info->description);
1767 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1768 ®ex_cancel_dht_get, NULL);
1769 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1770 ®ex_free_result, NULL);
1771 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1772 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1773 info->t->regex_ctx = NULL;
1774 for (i = 0; i < info->n_contexts; i++)
1776 GNUNET_free (info->contexts[i]);
1778 if (0 < info->n_contexts)
1779 GNUNET_free (info->contexts);
1780 if (0 < info->n_peers)
1781 GNUNET_free (info->peers);
1782 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1784 GNUNET_SCHEDULER_cancel(info->timeout);
1790 /******************************************************************************/
1791 /************************ PERIODIC FUNCTIONS ****************************/
1792 /******************************************************************************/
1795 * Announce iterator over for each application provided by the peer
1797 * @param cls closure
1798 * @param key current key code
1799 * @param value value in the hash map
1800 * @return GNUNET_YES if we should continue to
1805 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1807 struct PBlock block;
1808 struct MeshClient *c;
1810 block.id = my_full_id;
1811 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1812 GNUNET_assert(NULL != c);
1813 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1814 if (0 == block.type)
1819 block.type = htonl (block.type);
1821 GNUNET_break (NULL !=
1822 GNUNET_DHT_put (dht_handle, key,
1823 dht_replication_level,
1824 GNUNET_DHT_RO_RECORD_ROUTE |
1825 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1826 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1828 (const char *) &block,
1829 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1831 app_announce_time, NULL, NULL));
1837 * Periodically announce what applications are provided by local clients
1840 * @param cls closure
1841 * @param tc task context
1844 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1846 struct MeshClient *c = cls;
1849 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1850 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1855 DEBUG_DHT ("Starting PUT for regex\n");
1857 for (i = 0; i < c->n_regex; i++)
1859 regex_put (&c->regexes[i]);
1861 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1864 DEBUG_DHT ("Finished PUT for regex\n");
1871 * Periodically announce what applications are provided by local clients
1874 * @param cls closure
1875 * @param tc task context
1878 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1880 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1882 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1886 DEBUG_DHT ("Starting PUT for apps\n");
1888 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1890 announce_applications_task =
1891 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1893 DEBUG_DHT ("Finished PUT for apps\n");
1900 * Periodically announce self id in the DHT
1902 * @param cls closure
1903 * @param tc task context
1906 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1908 struct PBlock block;
1910 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1912 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1916 * - Set data expiration in function of X
1917 * - Adapt X to churn
1919 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1921 block.id = my_full_id;
1922 block.type = htonl (0);
1923 GNUNET_DHT_put (dht_handle, /* DHT handle */
1924 &my_full_id.hashPubKey, /* Key to use */
1925 dht_replication_level, /* Replication level */
1926 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1927 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1928 sizeof (block), /* Size of the data */
1929 (const char *) &block, /* Data itself */
1930 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1931 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1932 NULL, /* Continuation */
1933 NULL); /* Continuation closure */
1935 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1939 /******************************************************************************/
1940 /****************** GENERAL HELPER FUNCTIONS ************************/
1941 /******************************************************************************/
1944 * Decrements the reference counter and frees all resources if needed
1946 * @param mesh_data Data Descriptor used in a multicast message.
1947 * Freed no longer needed (last message).
1950 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1952 if (0 == --(mesh_data->reference_counter))
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1955 GNUNET_free (mesh_data->data);
1956 GNUNET_free (mesh_data);
1962 * Check if client has registered with the service and has not disconnected
1964 * @param client the client to check
1966 * @return non-NULL if client exists in the global DLL
1968 static struct MeshClient *
1969 client_get (struct GNUNET_SERVER_Client *client)
1971 struct MeshClient *c;
1976 if (c->handle == client)
1985 * Checks if a given client has subscribed to certain message type
1987 * @param message_type Type of message to check
1988 * @param c Client to check
1990 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1992 * FIXME: use of crypto_hash slows it down
1993 * The hash function alone takes 8-10us out of the ~55us for the whole
1994 * process of retransmitting the message from one local client to another.
1995 * Find faster implementation!
1998 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
2000 struct GNUNET_HashCode hc;
2002 if (NULL == c->types)
2005 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
2006 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2011 * Check whether client wants traffic from a tunnel.
2013 * @param c Client to check.
2014 * @param t Tunnel to be found.
2016 * @return GNUNET_YES if client knows tunnel.
2018 * TODO look in client hashmap
2021 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2025 for (i = 0; i < t->nclients; i++)
2026 if (t->clients[i] == c)
2033 * Check whether client has been informed about a tunnel.
2035 * @param c Client to check.
2036 * @param t Tunnel to be found.
2038 * @return GNUNET_YES if client knows tunnel.
2040 * TODO look in client hashmap
2043 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2047 for (i = 0; i < t->nignore; i++)
2048 if (t->ignore[i] == c)
2050 return client_wants_tunnel(c, t);
2055 * Marks a client as uninterested in traffic from the tunnel, updating both
2056 * client and tunnel to reflect this.
2058 * @param c Client that doesn't want traffic anymore.
2059 * @param t Tunnel which should be ignored.
2061 * FIXME when to delete an incoming tunnel?
2064 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2066 struct GNUNET_HashCode hash;
2068 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2069 GNUNET_break (GNUNET_YES ==
2070 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2072 GNUNET_break (GNUNET_YES ==
2073 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2074 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2075 tunnel_delete_active_client (t, c);
2076 GNUNET_array_append (t->ignore, t->nignore, c);
2081 * Deletes a tunnel from a client (either owner or destination). To be used on
2082 * tunnel destroy, otherwise, use client_ignore_tunnel.
2084 * @param c Client whose tunnel to delete.
2085 * @param t Tunnel which should be deleted.
2088 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2090 struct GNUNET_HashCode hash;
2094 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2095 GNUNET_assert (GNUNET_YES ==
2096 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2102 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2104 GNUNET_assert (GNUNET_YES ==
2105 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2109 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2117 * Send the message to all clients that have subscribed to its type
2119 * @param msg Pointer to the message itself
2120 * @param payload Pointer to the payload of the message.
2121 * @param t The tunnel to whose clients this message goes.
2123 * @return number of clients this message was sent to
2126 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2127 const struct GNUNET_MessageHeader *payload,
2128 struct MeshTunnel *t)
2130 struct MeshClient *c;
2131 MESH_TunnelNumber *tid;
2134 char cbuf[htons (msg->size)];
2136 type = ntohs (payload->type);
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2139 GNUNET_MESH_DEBUG_M2S (type));
2141 memcpy (cbuf, msg, sizeof (cbuf));
2142 switch (htons (msg->type))
2144 struct GNUNET_MESH_Unicast *uc;
2145 struct GNUNET_MESH_Multicast *mc;
2146 struct GNUNET_MESH_ToOrigin *to;
2148 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2149 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2152 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2153 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2156 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2157 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2165 for (count = 0, c = clients; c != NULL; c = c->next)
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2168 if (client_is_subscribed (type, c))
2170 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2174 *tid = htonl (t->local_tid);
2178 if (GNUNET_NO == client_knows_tunnel (c, t))
2180 /* This client doesn't know the tunnel */
2181 struct GNUNET_MESH_TunnelNotification tmsg;
2182 struct GNUNET_HashCode hash;
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2185 tmsg.header.size = htons (sizeof (tmsg));
2186 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2187 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2188 tmsg.tunnel_id = htonl (t->local_tid_dest);
2190 if (GNUNET_YES == t->speed_min)
2191 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2192 if (GNUNET_YES == t->nobuffer)
2193 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2194 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2195 &tmsg.header, GNUNET_NO);
2196 tunnel_add_client (t, c);
2197 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2199 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2200 c->incoming_tunnels, &hash, t,
2201 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2203 *tid = htonl (t->local_tid_dest);
2206 /* Check if the client wants to get traffic from the tunnel */
2207 if (GNUNET_NO == client_wants_tunnel(c, t))
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2211 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2212 (struct GNUNET_MessageHeader
2213 *) cbuf, GNUNET_NO);
2222 * Notify the client that owns the tunnel that a peer has connected to it
2223 * (the requested path to it has been confirmed).
2225 * @param t Tunnel whose owner to notify
2226 * @param id Short id of the peer that has connected
2229 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2231 struct GNUNET_MESH_PeerControl pc;
2233 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2234 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2235 pc.tunnel_id = htonl (t->local_tid);
2236 GNUNET_PEER_resolve (id, &pc.peer);
2237 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2243 * Notify all clients (not depending on registration status) that the incoming
2244 * tunnel is no longer valid.
2246 * @param t Tunnel that was destroyed.
2249 send_clients_tunnel_destroy (struct MeshTunnel *t)
2251 struct GNUNET_MESH_TunnelMessage msg;
2253 msg.header.size = htons (sizeof (msg));
2254 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2255 msg.tunnel_id = htonl (t->local_tid_dest);
2256 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2261 * Notify clients of tunnel disconnections, if needed.
2262 * In case the origin disconnects, the destination clients get a tunnel destroy
2263 * notification. If the last destination disconnects (only one remaining client
2264 * in tunnel), the origin gets a (local ID) peer disconnected.
2265 * Note that the function must be called BEFORE removing the client from
2268 * @param t Tunnel that was destroyed.
2269 * @param c Client that disconnected.
2272 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2278 struct GNUNET_MESH_TunnelMessage msg;
2280 msg.header.size = htons (sizeof (msg));
2281 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2282 msg.tunnel_id = htonl (t->local_tid_dest);
2283 for (i = 0; i < t->nclients; i++)
2284 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2285 &msg.header, GNUNET_NO);
2287 // FIXME when to disconnect an incoming tunnel?
2288 else if (1 == t->nclients && NULL != t->owner)
2290 struct GNUNET_MESH_PeerControl msg;
2292 msg.header.size = htons (sizeof (msg));
2293 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2294 msg.tunnel_id = htonl (t->local_tid);
2295 msg.peer = my_full_id;
2296 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2297 &msg.header, GNUNET_NO);
2303 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2304 * and insert it in the appropiate structures if the peer is not known yet.
2306 * @param peer Full identity of the peer.
2308 * @return Existing or newly created peer info.
2310 static struct MeshPeerInfo *
2311 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2313 struct MeshPeerInfo *peer_info;
2315 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2316 if (NULL == peer_info)
2319 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2320 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2321 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2322 peer_info->id = GNUNET_PEER_intern (peer);
2330 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2331 * and insert it in the appropiate structures if the peer is not known yet.
2333 * @param peer Short identity of the peer.
2335 * @return Existing or newly created peer info.
2337 static struct MeshPeerInfo *
2338 peer_info_get_short (const GNUNET_PEER_Id peer)
2340 struct GNUNET_PeerIdentity id;
2342 GNUNET_PEER_resolve (peer, &id);
2343 return peer_info_get (&id);
2348 * Iterator to remove the tunnel from the list of tunnels a peer participates
2351 * @param cls Closure (tunnel info)
2352 * @param key GNUNET_PeerIdentity of the peer (unused)
2353 * @param value PeerInfo of the peer
2355 * @return always GNUNET_YES, to keep iterating
2358 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2360 struct MeshTunnel *t = cls;
2361 struct MeshPeerInfo *peer = value;
2364 for (i = 0; i < peer->ntunnels; i++)
2367 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2370 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2371 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2380 * Core callback to write a pre-constructed data packet to core buffer
2382 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2383 * @param size Number of bytes available in buf.
2384 * @param buf Where the to write the message.
2386 * @return number of bytes written to buf
2389 send_core_data_raw (void *cls, size_t size, void *buf)
2391 struct MeshTransmissionDescriptor *info = cls;
2392 struct GNUNET_MessageHeader *msg;
2395 GNUNET_assert (NULL != info);
2396 GNUNET_assert (NULL != info->mesh_data);
2397 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2398 total_size = ntohs (msg->size);
2400 if (total_size > size)
2405 memcpy (buf, msg, total_size);
2406 data_descriptor_decrement_rc (info->mesh_data);
2413 * Sends an already built non-multicast message to a peer,
2414 * properly registrating all used resources.
2416 * @param message Message to send. Function makes a copy of it.
2417 * @param peer Short ID of the neighbor whom to send the message.
2418 * @param t Tunnel on which this message is transmitted.
2421 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2422 const struct GNUNET_PeerIdentity *peer,
2423 struct MeshTunnel *t)
2425 struct MeshTransmissionDescriptor *info;
2426 struct MeshPeerInfo *neighbor;
2427 struct MeshPeerPath *p;
2431 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2433 size = ntohs (message->size);
2434 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2435 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2436 info->mesh_data->data = GNUNET_malloc (size);
2437 memcpy (info->mesh_data->data, message, size);
2438 type = ntohs(message->type);
2441 struct GNUNET_MESH_Unicast *m;
2442 struct GNUNET_MESH_ToOrigin *to;
2444 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2445 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2446 m->ttl = htonl (ntohl (m->ttl) - 1);
2448 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2449 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2451 to->pid = htonl(t->bck_pid);
2453 info->mesh_data->data_len = size;
2454 info->mesh_data->reference_counter = 1;
2455 info->mesh_data->total_out = 1;
2456 neighbor = peer_info_get (peer);
2457 for (p = neighbor->path_head; NULL != p; p = p->next)
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468 " %s IS NOT DIRECTLY CONNECTED\n",
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2473 for (p = neighbor->path_head; NULL != p; p = p->next)
2475 struct GNUNET_PeerIdentity debug_id;
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 " path with %u hops through:\n",
2481 for (i = 0; i < p->length; i++)
2483 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2486 i, GNUNET_i2s(&debug_id));
2490 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2491 GNUNET_free (info->mesh_data->data);
2492 GNUNET_free (info->mesh_data);
2496 info->peer = neighbor;
2497 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2508 * Sends a CREATE PATH message for a path to a peer, properly registrating
2509 * all used resources.
2511 * @param peer PeerInfo of the final peer for whom this path is being created.
2512 * @param p Path itself.
2513 * @param t Tunnel for which the path is created.
2516 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2517 struct MeshTunnel *t)
2519 struct GNUNET_PeerIdentity id;
2520 struct MeshPathInfo *path_info;
2521 struct MeshPeerInfo *neighbor;
2527 p = tree_get_path_to_peer (t->tree, peer->id);
2534 for (i = 0; i < p->length; i++)
2536 if (p->peers[i] == myid)
2539 if (i >= p->length - 1)
2545 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2547 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2548 path_info->path = p;
2550 neighbor = peer_info_get (&id);
2551 path_info->peer = neighbor;
2552 queue_add (path_info,
2553 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2554 sizeof (struct GNUNET_MESH_ManipulatePath) +
2555 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2562 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2564 * @param t Tunnel whose path to destroy.
2565 * @param destination Short ID of the peer to whom the path to destroy.
2568 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2570 struct MeshPeerPath *p;
2573 p = tree_get_path_to_peer (t->tree, destination);
2579 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2580 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2582 struct GNUNET_MESH_ManipulatePath *msg;
2583 struct GNUNET_PeerIdentity *pi;
2587 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2588 msg->header.size = htons (size);
2589 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2590 msg->tid = htonl (t->id.tid);
2591 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2592 for (i = 0; i < p->length; i++)
2594 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2596 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2603 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2605 * @param t Tunnel which to confirm.
2608 send_path_ack (struct MeshTunnel *t)
2610 struct MeshTransmissionDescriptor *info;
2611 struct GNUNET_PeerIdentity id;
2612 GNUNET_PEER_Id peer;
2614 peer = tree_get_predecessor (t->tree);
2615 GNUNET_PEER_resolve (peer, &id);
2616 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2617 info->origin = &t->id;
2618 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2619 GNUNET_assert (NULL != info->peer);
2622 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2623 sizeof (struct GNUNET_MESH_PathACK),
2630 * Try to establish a new connection to this peer.
2631 * Use the best path for the given tunnel.
2632 * If the peer doesn't have any path to it yet, try to get one.
2633 * If the peer already has some path, send a CREATE PATH towards it.
2635 * @param peer PeerInfo of the peer.
2636 * @param t Tunnel for which to create the path, if possible.
2639 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2641 struct MeshPeerPath *p;
2642 struct MeshPathInfo *path_info;
2644 if (NULL != peer->path_head)
2646 p = tree_get_path_to_peer (t->tree, peer->id);
2653 // FIXME always send create path to self
2656 send_create_path (peer, p, t);
2660 struct GNUNET_HashCode hash;
2663 send_client_peer_connected (t, myid);
2664 t->local_tid_dest = next_local_tid++;
2665 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2668 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2669 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2676 else if (NULL == peer->dhtget)
2678 struct GNUNET_PeerIdentity id;
2680 GNUNET_PEER_resolve (peer->id, &id);
2681 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2682 path_info->peer = peer;
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2686 peer->dhtgetcls = path_info;
2687 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2688 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2689 &id.hashPubKey, /* key to search */
2690 dht_replication_level, /* replication level */
2691 GNUNET_DHT_RO_RECORD_ROUTE |
2692 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2693 NULL, /* xquery */ // FIXME BLOOMFILTER
2694 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2695 &dht_get_id_handler, path_info);
2697 /* Otherwise, there is no path but the DHT get is already started. */
2702 * Task to delay the connection of a peer
2704 * @param cls Closure (path info with tunnel and peer to connect).
2705 * Will be free'd on exection.
2706 * @param tc TaskContext
2709 peer_info_connect_task (void *cls,
2710 const struct GNUNET_SCHEDULER_TaskContext *tc)
2712 struct MeshPathInfo *path_info = cls;
2714 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2716 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2721 peer_info_connect (path_info->peer, path_info->t);
2727 * Destroy the peer_info and free any allocated resources linked to it
2729 * @param pi The peer_info to destroy.
2731 * @return GNUNET_OK on success
2734 peer_info_destroy (struct MeshPeerInfo *pi)
2736 struct GNUNET_PeerIdentity id;
2737 struct MeshPeerPath *p;
2738 struct MeshPeerPath *nextp;
2740 GNUNET_PEER_resolve (pi->id, &id);
2741 GNUNET_PEER_change_rc (pi->id, -1);
2744 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2747 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2748 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2750 if (NULL != pi->dhtget)
2752 GNUNET_DHT_get_stop (pi->dhtget);
2753 GNUNET_free (pi->dhtgetcls);
2759 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2763 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2765 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2773 * Remove all paths that rely on a direct connection between p1 and p2
2774 * from the peer itself and notify all tunnels about it.
2776 * @param peer PeerInfo of affected peer.
2777 * @param p1 GNUNET_PEER_Id of one peer.
2778 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2781 * TODO: optimize (see below)
2784 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2787 struct MeshPeerPath *p;
2788 struct MeshPeerPath *aux;
2789 struct MeshPeerInfo *peer_d;
2791 unsigned int destroyed;
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2798 p = peer->path_head;
2802 for (i = 0; i < (p->length - 1); i++)
2804 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2805 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2807 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2818 for (i = 0; i < peer->ntunnels; i++)
2820 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2824 * Problem: one or more peers have been deleted from the tunnel tree.
2825 * We don't know who they are to try to add them again.
2826 * We need to try to find a new path for each of the disconnected peers.
2827 * Some of them might already have a path to reach them that does not
2828 * involve p1 and p2. Adding all anew might render in a better tree than
2829 * the trivial immediate fix.
2831 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2832 * its children will be reachable trough him.
2834 peer_d = peer_info_get_short (d);
2837 for (p = peer_d->path_head; NULL != p; p = p->next)
2839 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2847 /* No callback, as peer will be already disconnected and a connection
2848 * scheduled by tunnel_notify_connection_broken.
2850 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2854 peer_info_connect (peer_d, peer->tunnels[i]);
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2862 * Add the path to the peer and update the path used to reach it in case this
2865 * @param peer_info Destination peer to add the path to.
2866 * @param path New path to add. Last peer must be the peer in arg 1.
2867 * Path will be either used of freed if already known.
2868 * @param trusted Do we trust that this path is real?
2871 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2874 struct MeshPeerPath *aux;
2878 if ((NULL == peer_info) || (NULL == path))
2881 path_destroy (path);
2884 if (path->peers[path->length - 1] != peer_info->id)
2887 path_destroy (path);
2890 if (path->length <= 2 && GNUNET_NO == trusted)
2892 /* Only allow CORE to tell us about direct paths */
2893 path_destroy (path);
2896 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2897 for (l = 1; l < path->length; l++)
2899 if (path->peers[l] == myid)
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2902 for (l2 = 0; l2 < path->length - l; l2++)
2904 path->peers[l2] = path->peers[l + l2];
2909 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2914 struct GNUNET_PeerIdentity id;
2916 GNUNET_PEER_resolve (peer_info->id, &id);
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2918 path->length, GNUNET_i2s (&id));
2921 l = path_get_length (path);
2928 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2929 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2931 l2 = path_get_length (aux);
2934 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2935 peer_info->path_tail, aux, path);
2940 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2942 path_destroy (path);
2947 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2954 * Add the path to the origin peer and update the path used to reach it in case
2955 * this is the shortest.
2956 * The path is given in peer_info -> destination, therefore we turn the path
2957 * upside down first.
2959 * @param peer_info Peer to add the path to, being the origin of the path.
2960 * @param path New path to add after being inversed.
2961 * @param trusted Do we trust that this path is real?
2964 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2965 struct MeshPeerPath *path, int trusted)
2968 peer_info_add_path (peer_info, path, trusted);
2973 * Function called if the connection to the peer has been stalled for a while,
2974 * possibly due to a missed ACK. Poll the peer about its ACK status.
2976 * @param cls Closure (info about regex search).
2977 * @param tc TaskContext.
2980 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2982 struct MeshTunnelChildInfo *cinfo = cls;
2983 struct GNUNET_MESH_Poll msg;
2984 struct GNUNET_PeerIdentity id;
2985 struct MeshTunnel *t;
2987 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2988 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2994 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2995 msg.header.size = htons (sizeof (msg));
2996 msg.tid = htonl (t->id.tid);
2997 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2998 msg.last_ack = htonl (cinfo->fwd_ack);
3000 GNUNET_PEER_resolve (cinfo->id, &id);
3001 send_prebuilt_message (&msg.header, &id, cinfo->t);
3002 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
3003 &tunnel_poll, cinfo);
3008 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3009 * to obtain a local peer -> destination path and interning the peer ids.
3011 * @return Newly allocated and created path
3013 static struct MeshPeerPath *
3014 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3015 unsigned int get_path_length,
3016 const struct GNUNET_PeerIdentity *put_path,
3017 unsigned int put_path_length)
3019 struct MeshPeerPath *p;
3025 GNUNET_PEER_change_rc (myid, 1);
3026 i = get_path_length;
3027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3028 for (i--; i >= 0; i--)
3030 id = GNUNET_PEER_intern (&get_path[i]);
3031 if (p->length > 0 && id == p->peers[p->length - 1])
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3034 GNUNET_PEER_change_rc (id, -1);
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3039 GNUNET_i2s (&get_path[i]));
3041 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3042 p->peers[p->length - 1] = id;
3045 i = put_path_length;
3046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3047 for (i--; i >= 0; i--)
3049 id = GNUNET_PEER_intern (&put_path[i]);
3052 /* PUT path went through us, so discard the path up until now and start
3053 * from here to get a much shorter (and loop-free) path.
3058 if (p->length > 0 && id == p->peers[p->length - 1])
3060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3061 GNUNET_PEER_change_rc (id, -1);
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3066 GNUNET_i2s (&put_path[i]));
3068 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3069 p->peers[p->length - 1] = id;
3073 if (get_path_length > 0)
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3075 GNUNET_i2s (&get_path[0]));
3076 if (put_path_length > 0)
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3078 GNUNET_i2s (&put_path[0]));
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3081 for (i = 0; i < p->length; i++)
3083 struct GNUNET_PeerIdentity peer_id;
3085 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3087 GNUNET_i2s (&peer_id));
3095 * Adds a path to the peer_infos of all the peers in the path
3097 * @param p Path to process.
3098 * @param confirmed Whether we know if the path works or not.
3101 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3105 /* TODO: invert and add */
3106 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3107 for (i++; i < p->length; i++)
3109 struct MeshPeerInfo *aux;
3110 struct MeshPeerPath *copy;
3112 aux = peer_info_get_short (p->peers[i]);
3113 copy = path_duplicate (p);
3114 copy->length = i + 1;
3115 peer_info_add_path (aux, copy, GNUNET_NO);
3121 * Send keepalive packets for a peer
3123 * @param cls Closure (tunnel for which to send the keepalive).
3124 * @param tc Notification context.
3126 * TODO: implement explicit multicast keepalive?
3129 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3133 * Search for a tunnel among the incoming tunnels
3135 * @param tid the local id of the tunnel
3137 * @return tunnel handler, NULL if doesn't exist
3139 static struct MeshTunnel *
3140 tunnel_get_incoming (MESH_TunnelNumber tid)
3142 struct GNUNET_HashCode hash;
3144 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3145 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3146 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3151 * Search for a tunnel among the tunnels for a client
3153 * @param c the client whose tunnels to search in
3154 * @param tid the local id of the tunnel
3156 * @return tunnel handler, NULL if doesn't exist
3158 static struct MeshTunnel *
3159 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3161 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3163 return tunnel_get_incoming (tid);
3167 struct GNUNET_HashCode hash;
3169 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3170 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3176 * Search for a tunnel by global ID using PEER_ID
3178 * @param pi owner of the tunnel
3179 * @param tid global tunnel number
3181 * @return tunnel handler, NULL if doesn't exist
3183 static struct MeshTunnel *
3184 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3186 struct MESH_TunnelID id;
3187 struct GNUNET_HashCode hash;
3192 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3193 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3198 * Search for a tunnel by global ID using full PeerIdentities
3200 * @param oid owner of the tunnel
3201 * @param tid global tunnel number
3203 * @return tunnel handler, NULL if doesn't exist
3205 static struct MeshTunnel *
3206 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3208 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3213 * Delete an active client from the tunnel.
3219 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3223 for (i = 0; i < t->nclients; i++)
3225 if (t->clients[i] == c)
3227 t->clients[i] = t->clients[t->nclients - 1];
3228 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3229 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3231 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3239 * Delete an ignored client from the tunnel.
3245 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3249 for (i = 0; i < t->nignore; i++)
3251 if (t->ignore[i] == c)
3253 t->ignore[i] = t->ignore[t->nignore - 1];
3254 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3262 * Delete a client from the tunnel. It should be only done on
3263 * client disconnection, otherwise use client_ignore_tunnel.
3269 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3271 tunnel_delete_ignored_client (t, c);
3272 tunnel_delete_active_client (t, c);
3277 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3279 * Destroys queue elements of all waiting transmissions and frees all memory
3280 * used by the struct and its elements.
3282 * @param cls Closure (tunnel info).
3283 * @param key Hash of GNUNET_PEER_Id (unused).
3284 * @param value MeshTunnelChildInfo of the child.
3286 * @return always GNUNET_YES, to keep iterating
3289 tunnel_destroy_child (void *cls,
3290 const struct GNUNET_HashCode * key,
3293 struct MeshTunnelChildInfo *cinfo = value;
3294 struct MeshTunnel *t = cls;
3295 struct MeshPeerQueue *q;
3299 for (c = 0; c < cinfo->send_buffer_n; c++)
3301 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3302 q = cinfo->send_buffer[i];
3303 cinfo->send_buffer[i] = NULL;
3305 queue_destroy (q, GNUNET_YES);
3308 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3310 GNUNET_free_non_null (cinfo->send_buffer);
3311 GNUNET_free (cinfo);
3317 * Callback used to notify a client owner of a tunnel that a peer has
3318 * disconnected, most likely because of a path change.
3320 * @param cls Closure (tunnel this notification is about).
3321 * @param peer_id Short ID of disconnected peer.
3324 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3326 struct MeshTunnel *t = cls;
3327 struct MeshPeerInfo *peer;
3328 struct MeshPathInfo *path_info;
3330 if (NULL != t->owner && NULL != nc)
3332 struct GNUNET_MESH_PeerControl msg;
3334 msg.header.size = htons (sizeof (msg));
3335 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3336 msg.tunnel_id = htonl (t->local_tid);
3337 GNUNET_PEER_resolve (peer_id, &msg.peer);
3338 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3339 &msg.header, GNUNET_NO);
3341 peer = peer_info_get_short (peer_id);
3342 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3343 path_info->peer = peer;
3345 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3351 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3352 * needed rescources.
3353 * If peer already exists, reevaluate shortest path and change if different.
3355 * @param t Tunnel we want to add a new peer to
3356 * @param peer PeerInfo of the peer being added
3360 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3362 struct GNUNET_PeerIdentity id;
3363 struct MeshPeerPath *best_p;
3364 struct MeshPeerPath *p;
3365 unsigned int best_cost;
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3369 GNUNET_PEER_resolve (peer->id, &id);
3371 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3374 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3375 GNUNET_assert (GNUNET_OK ==
3376 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3378 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3381 if (NULL != (p = peer->path_head))
3384 best_cost = tree_get_path_cost (t->tree, p);
3387 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3394 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3395 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3396 t->path_refresh_task =
3397 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3401 /* Start a DHT get */
3402 peer_info_connect (peer, t);
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3408 * Add a path to a tunnel which we don't own, just to remember the next hop.
3409 * If destination node was already in the tunnel, the first hop information
3410 * will be replaced with the new path.
3412 * @param t Tunnel we want to add a new peer to
3413 * @param p Path to add
3414 * @param own_pos Position of local node in path.
3418 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3419 unsigned int own_pos)
3421 struct GNUNET_PeerIdentity id;
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3424 GNUNET_assert (0 != own_pos);
3425 tree_add_path (t->tree, p, NULL, NULL);
3426 if (own_pos < p->length - 1)
3428 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3429 tree_update_first_hops (t->tree, myid, &id);
3431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3435 * Add a client to a tunnel, initializing all needed data structures.
3437 * @param t Tunnel to which add the client.
3438 * @param c Client which to add to the tunnel.
3441 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3443 struct MeshTunnelClientInfo clinfo;
3445 GNUNET_array_append (t->clients, t->nclients, c);
3446 clinfo.fwd_ack = t->fwd_pid + 1;
3447 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3448 clinfo.fwd_pid = t->fwd_pid;
3449 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3451 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3456 * Notifies a tunnel that a connection has broken that affects at least
3457 * some of its peers. Sends a notification towards the root of the tree.
3458 * In case the peer is the owner of the tree, notifies the client that owns
3459 * the tunnel and tries to reconnect.
3461 * @param t Tunnel affected.
3462 * @param p1 Peer that got disconnected from p2.
3463 * @param p2 Peer that got disconnected from p1.
3465 * @return Short ID of the peer disconnected (either p1 or p2).
3466 * 0 if the tunnel remained unaffected.
3468 static GNUNET_PEER_Id
3469 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3475 tree_notify_connection_broken (t->tree, p1, p2,
3476 &tunnel_notify_client_peer_disconnected,
3478 if (myid != p1 && myid != p2)
3484 if (tree_get_predecessor (t->tree) != 0)
3486 /* We are the peer still connected, notify owner of the disconnection. */
3487 struct GNUNET_MESH_PathBroken msg;
3488 struct GNUNET_PeerIdentity neighbor;
3490 msg.header.size = htons (sizeof (msg));
3491 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3492 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3493 msg.tid = htonl (t->id.tid);
3494 msg.peer1 = my_full_id;
3495 GNUNET_PEER_resolve (pid, &msg.peer2);
3496 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3497 send_prebuilt_message (&msg.header, &neighbor, t);
3505 * Send a multicast packet to a neighbor.
3507 * @param cls Closure (Info about the multicast packet)
3508 * @param neighbor_id Short ID of the neighbor to send the packet to.
3511 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3513 struct MeshData *mdata = cls;
3514 struct MeshTransmissionDescriptor *info;
3515 struct GNUNET_PeerIdentity neighbor;
3516 struct GNUNET_MessageHeader *msg;
3518 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3520 info->mesh_data = mdata;
3521 (mdata->reference_counter) ++;
3522 info->destination = neighbor_id;
3523 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3525 GNUNET_i2s (&neighbor));
3526 info->peer = peer_info_get (&neighbor);
3527 GNUNET_assert (NULL != info->peer);
3528 msg = (struct GNUNET_MessageHeader *) mdata->data;
3531 info->mesh_data->data_len,
3538 * Queue a message in a tunnel in multicast, sending a copy to each child node
3539 * down the local one in the tunnel tree.
3541 * @param t Tunnel in which to send the data.
3542 * @param msg Message to be sent.
3545 tunnel_send_multicast (struct MeshTunnel *t,
3546 const struct GNUNET_MessageHeader *msg)
3548 struct MeshData *mdata;
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3551 " sending a multicast packet...\n");
3553 mdata = GNUNET_malloc (sizeof (struct MeshData));
3554 mdata->data_len = ntohs (msg->size);
3556 mdata->data = GNUNET_malloc (mdata->data_len);
3557 memcpy (mdata->data, msg, mdata->data_len);
3558 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3560 struct GNUNET_MESH_Multicast *mcast;
3562 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3563 if (t->fwd_queue_n >= t->fwd_queue_max)
3566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3567 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3568 " message from %s!\n",
3569 GNUNET_i2s(&mcast->oid));
3570 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3571 " message at %s!\n",
3572 GNUNET_i2s(&my_full_id));
3573 GNUNET_free (mdata->data);
3574 GNUNET_free (mdata);
3578 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3580 ntohl (mcast->ttl));
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3587 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3588 if (mdata->reference_counter == 0)
3590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3591 " no one to send data to\n");
3592 GNUNET_free (mdata->data);
3593 GNUNET_free (mdata);
3594 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3599 mdata->total_out = mdata->reference_counter;
3601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3602 " sending a multicast packet done\n");
3608 * Increase the SKIP value of all peers that
3609 * have not received a unicast message.
3611 * @param cls Closure (ID of the peer that HAS received the message).
3612 * @param key ID of the neighbor.
3613 * @param value Information about the neighbor.
3615 * @return GNUNET_YES to keep iterating.
3618 tunnel_add_skip (void *cls,
3619 const struct GNUNET_HashCode * key,
3622 struct GNUNET_PeerIdentity *neighbor = cls;
3623 struct MeshTunnelChildInfo *cinfo = value;
3625 /* TODO compare only pointers? key == neighbor? */
3626 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3636 * @brief Get neighbor's Flow Control information.
3638 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3639 * descendant of the local node in a certain tunnel.
3640 * If the info is not yet there (recently created path), creates the data struct
3641 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3644 * @param t Tunnel related.
3645 * @param peer Neighbor whose Flow Control info is needed.
3647 * @return Neighbor's Flow Control info.
3649 static struct MeshTunnelChildInfo *
3650 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3651 const struct GNUNET_PeerIdentity *peer)
3653 struct MeshTunnelChildInfo *cinfo;
3655 if (NULL == t->children_fc)
3658 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3664 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3665 cinfo->id = GNUNET_PEER_intern (peer);
3666 cinfo->skip = t->fwd_pid;
3669 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3670 cinfo->fwd_ack = t->fwd_pid + delta;
3671 cinfo->bck_ack = delta;
3672 cinfo->bck_pid = -1;
3674 cinfo->send_buffer =
3675 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3677 GNUNET_assert (GNUNET_OK ==
3678 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3681 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3688 * Get the Flow Control info of a client.
3690 * @param t Tunnel on which to look.
3691 * @param c Client whose ACK to get.
3693 * @return ACK value.
3695 static struct MeshTunnelClientInfo *
3696 tunnel_get_client_fc (struct MeshTunnel *t,
3697 struct MeshClient *c)
3701 for (i = 0; i < t->nclients; i++)
3703 if (t->clients[i] != c)
3705 return &t->clients_fc[i];
3708 return NULL; // avoid compiler / coverity complaints
3713 * Iterator to get the appropiate ACK value from all children nodes.
3715 * @param cls Closue (tunnel).
3716 * @param id Id of the child node.
3719 tunnel_get_child_fwd_ack (void *cls,
3722 struct GNUNET_PeerIdentity peer_id;
3723 struct MeshTunnelChildInfo *cinfo;
3724 struct MeshTunnelChildIteratorContext *ctx = cls;
3725 struct MeshTunnel *t = ctx->t;
3728 GNUNET_PEER_resolve (id, &peer_id);
3729 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3730 ack = cinfo->fwd_ack;
3733 if (GNUNET_NO == ctx->init)
3735 ctx->max_child_ack = ack;
3736 ctx->init = GNUNET_YES;
3739 if (GNUNET_YES == t->speed_min)
3741 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3745 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3752 * Get the maximum PID allowed to transmit to any
3753 * tunnel child of the local peer, depending on the tunnel
3754 * buffering/speed settings.
3758 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3761 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3763 struct MeshTunnelChildIteratorContext ctx;
3765 ctx.max_child_ack = 0;
3767 ctx.init = GNUNET_NO;
3768 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3770 if (0 == ctx.nchildren)
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3773 " tunnel has no children, no FWD ACK\n");
3777 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3778 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3780 return (int64_t) ctx.max_child_ack;
3785 * Set the FWD ACK value of a client in a particular tunnel.
3787 * @param t Tunnel affected.
3788 * @param c Client whose ACK to set.
3789 * @param ack ACK value.
3792 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3793 struct MeshClient *c,
3798 for (i = 0; i < t->nclients; i++)
3800 if (t->clients[i] != c)
3802 t->clients_fc[i].fwd_ack = ack;
3810 * Get the highest ACK value of all clients in a particular tunnel,
3811 * according to the buffering/speed settings.
3813 * @param t Tunnel on which to look.
3815 * @return Corresponding ACK value (max uint32_t).
3816 * If no clients are suscribed, -1LL.
3819 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3824 if (0 == t->nclients)
3826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3827 " tunnel has no clients, no FWD ACK\n");
3831 for (ack = -1LL, i = 0; i < t->nclients; i++)
3834 (GNUNET_YES == t->speed_min &&
3835 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3836 (GNUNET_NO == t->speed_min &&
3837 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3839 ack = t->clients_fc[i].fwd_ack;
3843 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3844 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3846 return (uint32_t) ack;
3851 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3852 * mode and the status of all children nodes.
3856 * @return Maximum PID allowed.
3859 tunnel_get_fwd_ack (struct MeshTunnel *t)
3863 uint32_t buffer_free;
3867 count = t->fwd_pid - t->skip;
3868 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3869 child_ack = tunnel_get_children_fwd_ack (t);
3870 client_ack = tunnel_get_clients_fwd_ack (t);
3871 if (GNUNET_YES == t->nobuffer)
3874 if (-1LL == child_ack)
3875 child_ack = client_ack;
3876 if (-1LL == child_ack)
3879 client_ack = child_ack = ack;
3884 ack = count + buffer_free; // Overflow? OK!
3886 if (-1LL == child_ack)
3888 // Node has no children, child_ack AND core buffer are irrelevant.
3889 if (-1LL == client_ack) // No children AND no clients? Not good!
3891 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3895 return (uint32_t) client_ack;
3897 if (-1LL == client_ack)
3901 if (GNUNET_YES == t->speed_min)
3903 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3904 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3908 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3909 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3912 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3913 count, buffer_free, child_ack, client_ack, ack);
3919 * Build a local ACK message and send it to a local client.
3921 * @param t Tunnel on which to send the ACK.
3922 * @param c Client to whom send the ACK.
3923 * @param ack Value of the ACK.
3926 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3928 struct GNUNET_MESH_LocalAck msg;
3930 msg.header.size = htons (sizeof (msg));
3931 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3932 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3933 msg.max_pid = htonl (ack);
3934 GNUNET_SERVER_notification_context_unicast(nc,
3941 * Build an ACK message and queue it to send to the given peer.
3943 * @param t Tunnel on which to send the ACK.
3944 * @param peer Peer to whom send the ACK.
3945 * @param ack Value of the ACK.
3948 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3950 struct GNUNET_MESH_ACK msg;
3952 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3953 msg.header.size = htons (sizeof (msg));
3954 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3955 msg.pid = htonl (ack);
3956 msg.tid = htonl (t->id.tid);
3958 send_prebuilt_message (&msg.header, peer, t);
3963 * Notify a the owner of a tunnel about how many more
3964 * payload packages will we accept on a given tunnel.
3966 * @param t Tunnel on which to send the ACK.
3969 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3974 "Sending client FWD ACK on tunnel %X\n",
3977 ack = tunnel_get_fwd_ack (t);
3979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3980 if (t->last_fwd_ack == ack)
3982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3987 t->last_fwd_ack = ack;
3988 send_local_ack (t, t->owner, ack);
3993 * Send an ACK informing the predecessor about the available buffer space.
3994 * In case there is no predecessor, inform the owning client.
3995 * If buffering is off, send only on behalf of children or self if endpoint.
3996 * If buffering is on, send when sent to children and buffer space is free.
3997 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3998 * the ACK itself goes "back" (towards root).
4000 * @param t Tunnel on which to send the ACK.
4001 * @param type Type of message that triggered the ACK transmission.
4004 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4006 struct GNUNET_PeerIdentity id;
4009 if (NULL != t->owner)
4011 tunnel_send_client_fwd_ack (t);
4014 /* Is it after unicast / multicast retransmission? */
4017 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4018 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4020 "ACK due to FWD DATA retransmission\n");
4021 if (GNUNET_YES == t->nobuffer)
4023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4027 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4028 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4030 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4031 t->force_ack = GNUNET_YES;
4037 /* Check if we need to transmit the ACK */
4038 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4039 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4040 GNUNET_NO == t->force_ack)
4042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4044 " t->qmax: %u, t->qn: %u\n",
4045 t->fwd_queue_max, t->fwd_queue_n);
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4047 " t->pid: %u, t->ack: %u\n",
4048 t->fwd_pid, t->last_fwd_ack);
4052 /* Ok, ACK might be necessary, what PID to ACK? */
4053 ack = tunnel_get_fwd_ack (t);
4055 /* If speed_min and not all children have ack'd, dont send yet */
4056 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4062 t->last_fwd_ack = ack;
4063 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4064 send_ack (t, &id, ack);
4066 t->force_ack = GNUNET_NO;
4071 * Iterator to send a child node a BCK ACK to allow him to send more
4074 * @param cls Closure (tunnel).
4075 * @param id Id of the child node.
4078 tunnel_send_child_bck_ack (void *cls,
4081 struct MeshTunnel *t = cls;
4082 struct MeshTunnelChildInfo *cinfo;
4083 struct GNUNET_PeerIdentity peer;
4086 GNUNET_PEER_resolve (id, &peer);
4087 cinfo = tunnel_get_neighbor_fc (t, &peer);
4088 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4090 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4093 " Not sending ACK, not needed\n");
4096 cinfo->bck_ack = ack;
4098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4099 " Sending BCK ACK %u (last sent: %u)\n",
4100 ack, cinfo->bck_ack);
4101 send_ack (t, &peer, ack);
4106 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4108 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4110 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4111 * - implement a size 1 buffer in each client_fc AND children_fc
4112 * to hold at least 1 message per "child".
4113 * problem: violates no buffer policy
4114 * - ack 0 and make "children" poll for transmission slots
4115 * problem: big overhead, extra latency even in low traffic
4118 * @param t Tunnel on which to send the BCK ACKs.
4121 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4124 unsigned int tunnel_delta;
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4128 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4131 /* Find client whom to allow to send to origin (with lowest buffer space) */
4132 for (i = 0; i < t->nclients; i++)
4134 struct MeshTunnelClientInfo *clinfo;
4137 clinfo = &t->clients_fc[i];
4138 delta = clinfo->bck_ack - clinfo->bck_pid;
4139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4140 t->clients[i]->id, delta);
4142 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4143 (GNUNET_YES == t->nobuffer && 0 == delta))
4147 ack = clinfo->bck_pid;
4148 ack += t->nobuffer ? 1 : tunnel_delta;
4149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4150 " sending ack to client %u: %u\n",
4151 t->clients[i]->id, ack);
4152 send_local_ack (t, t->clients[i], ack);
4153 clinfo->bck_ack = ack;
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4158 " not sending ack to client %u (td %u, d %u)\n",
4159 t->clients[i]->id, tunnel_delta, delta);
4166 * Send an ACK informing the children nodes and destination clients about
4167 * the available buffer space.
4168 * If buffering is off, send only on behalf of root (can be self).
4169 * If buffering is on, send when sent to predecessor and buffer space is free.
4170 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4171 * the ACK itself goes "forward" (towards children/clients).
4173 * @param t Tunnel on which to send the ACK.
4174 * @param type Type of message that triggered the ACK transmission.
4177 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4180 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4181 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4182 /* Is it after data to_origin retransmission? */
4185 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4186 if (GNUNET_YES == t->nobuffer)
4188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4189 " Not sending ACK, nobuffer\n");
4193 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4194 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4196 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4197 t->force_ack = GNUNET_YES;
4203 tunnel_send_clients_bck_ack (t);
4204 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4205 t->force_ack = GNUNET_NO;
4210 * @brief Re-initiate traffic to this peer if necessary.
4212 * Check if there is traffic queued towards this peer
4213 * and the core transmit handle is NULL (traffic was stalled).
4214 * If so, call core tmt rdy.
4216 * @param cls Closure (unused)
4217 * @param peer_id Short ID of peer to which initiate traffic.
4220 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4222 struct MeshPeerInfo *peer;
4223 struct GNUNET_PeerIdentity id;
4224 struct MeshPeerQueue *q;
4227 peer = peer_info_get_short(peer_id);
4228 if (NULL != peer->core_transmit)
4231 q = queue_get_next(peer);
4234 /* Might br multicast traffic already sent to this particular peer but
4235 * not to other children in this tunnel.
4236 * This way t->queue_n would be > 0 but the queue of this particular peer
4242 GNUNET_PEER_resolve (peer->id, &id);
4243 peer->core_transmit =
4244 GNUNET_CORE_notify_transmit_ready(core_handle,
4247 GNUNET_TIME_UNIT_FOREVER_REL,
4257 * @brief Allow transmission of FWD traffic on this tunnel
4259 * Check if there is traffic queued towards any children
4260 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4262 * @param t Tunnel on which to unlock FWD traffic.
4265 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4267 if (0 == t->fwd_queue_n)
4270 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4275 * @brief Allow transmission of BCK traffic on this tunnel
4277 * Check if there is traffic queued towards the root of the tree
4278 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4280 * @param t Tunnel on which to unlock BCK traffic.
4283 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4285 if (0 == t->bck_queue_n)
4288 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4293 * Send a message to all peers in this tunnel that the tunnel is no longer
4296 * @param t The tunnel whose peers to notify.
4299 tunnel_send_destroy (struct MeshTunnel *t)
4301 struct GNUNET_MESH_TunnelDestroy msg;
4303 msg.header.size = htons (sizeof (msg));
4304 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4305 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4306 msg.tid = htonl (t->id.tid);
4307 tunnel_send_multicast (t, &msg.header);
4312 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4314 * @param cls Closure (Tunnel which to cancel).
4315 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4318 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4320 struct MeshTunnel *t = cls;
4321 struct MeshPeerInfo *peer_info;
4322 struct MeshPeerQueue *pq;
4323 struct MeshPeerQueue *next;
4325 peer_info = peer_info_get_short (neighbor_id);
4326 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4329 if (pq->tunnel == t)
4331 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4332 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4333 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4335 // Should have been removed on destroy children
4338 queue_destroy (pq, GNUNET_YES);
4341 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4343 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4344 peer_info->core_transmit = NULL;
4349 * Destroy the tunnel and free any allocated resources linked to it.
4351 * @param t the tunnel to destroy
4353 * @return GNUNET_OK on success
4356 tunnel_destroy (struct MeshTunnel *t)
4358 struct MeshClient *c;
4359 struct GNUNET_HashCode hash;
4370 struct GNUNET_PeerIdentity id;
4372 GNUNET_PEER_resolve (t->id.oid, &id);
4373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4374 GNUNET_i2s (&id), t->id.tid);
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4380 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4381 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4389 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4391 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4398 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4399 for (i = 0; i < t->nclients; i++)
4403 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4409 for (i = 0; i < t->nignore; i++)
4413 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4420 if (t->nclients > 0)
4423 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4428 GNUNET_free (t->clients);
4429 GNUNET_free (t->clients_fc);
4432 if (NULL != t->peers)
4434 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4436 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4439 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4440 &tunnel_destroy_child,
4442 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4443 t->children_fc = NULL;
4445 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4446 tree_destroy (t->tree);
4448 if (NULL != t->regex_ctx)
4449 regex_cancel_search (t->regex_ctx);
4450 if (NULL != t->dht_get_type)
4451 GNUNET_DHT_get_stop (t->dht_get_type);
4452 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4453 GNUNET_SCHEDULER_cancel (t->timeout_task);
4454 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4455 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4458 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4465 * Create a new tunnel
4467 * @param owner Who is the owner of the tunnel (short ID).
4468 * @param tid Tunnel Number of the tunnel.
4469 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4470 * @param local Tunnel Number for the tunnel, for the client point of view.
4472 * @return A new initialized tunnel. NULL on error.
4474 static struct MeshTunnel *
4475 tunnel_new (GNUNET_PEER_Id owner,
4476 MESH_TunnelNumber tid,
4477 struct MeshClient *client,
4478 MESH_TunnelNumber local)
4480 struct MeshTunnel *t;
4481 struct GNUNET_HashCode hash;
4483 if (n_tunnels >= max_tunnels && NULL == client)
4486 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4489 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4490 t->bck_queue_max = t->fwd_queue_max;
4491 t->tree = tree_new (owner);
4493 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4494 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4495 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4496 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4497 t->local_tid = local;
4498 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4500 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4502 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4504 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4512 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4519 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4521 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4522 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4526 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4536 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4537 * that are no longer needed, as well as nodes of no longer reachable peers.
4538 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4540 * @param t Tunnel from which to remove the path.
4541 * @param peer Short id of the peer which should be removed.
4544 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4548 r = tree_del_peer (t->tree, peer, NULL, NULL);
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4552 "Tunnel %u [%u] has no more nodes\n",
4553 t->id.oid, t->id.tid);
4559 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4560 * client when the client disconnects. If the client is not the owner, the
4561 * owner will get notified if no more clients are in the tunnel and the client
4562 * get removed from the tunnel's list.
4564 * @param cls closure (client that is disconnecting)
4565 * @param key the hash of the local tunnel id (used to access the hashmap)
4566 * @param value the value stored at the key (tunnel to destroy)
4568 * @return GNUNET_OK, keep iterating.
4571 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4573 struct MeshTunnel *t = value;
4574 struct MeshClient *c = cls;
4576 send_client_tunnel_disconnect(t, c);
4579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4580 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4581 tunnel_delete_client(t, c);
4582 client_delete_tunnel(c, t);
4585 tunnel_send_destroy(t);
4587 t->destroy = GNUNET_YES;
4594 * Timeout function, destroys tunnel if called
4596 * @param cls Closure (tunnel to destroy).
4597 * @param tc TaskContext
4600 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4602 struct MeshTunnel *t = cls;
4603 struct GNUNET_PeerIdentity id;
4605 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4606 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4608 GNUNET_PEER_resolve(t->id.oid, &id);
4609 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4610 "Tunnel %s [%X] timed out. Destroying.\n",
4611 GNUNET_i2s(&id), t->id.tid);
4612 send_clients_tunnel_destroy (t);
4617 * Resets the tunnel timeout. Starts it if no timeout was running.
4619 * @param t Tunnel whose timeout to reset.
4621 * TODO use heap to improve efficiency of scheduler.
4624 tunnel_reset_timeout (struct MeshTunnel *t)
4626 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4627 GNUNET_SCHEDULER_cancel (t->timeout_task);
4629 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4630 (refresh_path_time, 4), &tunnel_timeout, t);
4634 /******************************************************************************/
4635 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4636 /******************************************************************************/
4639 * Function to send a create path packet to a peer.
4641 * @param cls closure
4642 * @param size number of bytes available in buf
4643 * @param buf where the callee should write the message
4644 * @return number of bytes written to buf
4647 send_core_path_create (void *cls, size_t size, void *buf)
4649 struct MeshPathInfo *info = cls;
4650 struct GNUNET_MESH_ManipulatePath *msg;
4651 struct GNUNET_PeerIdentity *peer_ptr;
4652 struct MeshTunnel *t = info->t;
4653 struct MeshPeerPath *p = info->path;
4658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4660 sizeof (struct GNUNET_MESH_ManipulatePath) +
4661 p->length * sizeof (struct GNUNET_PeerIdentity);
4663 if (size < size_needed || NULL == buf)
4668 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4669 msg->header.size = htons (size_needed);
4670 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4671 msg->tid = ntohl (t->id.tid);
4674 if (GNUNET_YES == t->speed_min)
4675 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4676 if (GNUNET_YES == t->nobuffer)
4677 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4678 msg->opt = htonl(opt);
4681 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4682 for (i = 0; i < p->length; i++)
4684 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4691 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4697 * Fill the core buffer
4699 * @param cls closure (data itself)
4700 * @param size number of bytes available in buf
4701 * @param buf where the callee should write the message
4703 * @return number of bytes written to buf
4706 send_core_data_multicast (void *cls, size_t size, void *buf)
4708 struct MeshTransmissionDescriptor *info = cls;
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4712 GNUNET_assert (NULL != info);
4713 GNUNET_assert (NULL != info->peer);
4714 total_size = info->mesh_data->data_len;
4715 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4717 if (total_size > size)
4722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4723 memcpy (buf, info->mesh_data->data, total_size);
4726 struct GNUNET_MESH_Multicast *mc;
4727 struct GNUNET_MessageHeader *mh;
4730 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4732 mc = (struct GNUNET_MESH_Multicast *) mh;
4733 mh = (struct GNUNET_MessageHeader *) &mc[1];
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4735 " multicast, payload type %s\n",
4736 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4738 " multicast, payload size %u\n", ntohs (mh->size));
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4743 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4747 data_descriptor_decrement_rc (info->mesh_data);
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4756 * Creates a path ack message in buf and frees all unused resources.
4758 * @param cls closure (MeshTransmissionDescriptor)
4759 * @param size number of bytes available in buf
4760 * @param buf where the callee should write the message
4761 * @return number of bytes written to buf
4764 send_core_path_ack (void *cls, size_t size, void *buf)
4766 struct MeshTransmissionDescriptor *info = cls;
4767 struct GNUNET_MESH_PathACK *msg = buf;
4769 GNUNET_assert (NULL != info);
4770 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4775 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4776 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4777 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4778 msg->tid = htonl (info->origin->tid);
4779 msg->peer_id = my_full_id;
4782 /* TODO add signature */
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4785 return sizeof (struct GNUNET_MESH_PathACK);
4790 * Free a transmission that was already queued with all resources
4791 * associated to the request.
4793 * @param queue Queue handler to cancel.
4794 * @param clear_cls Is it necessary to free associated cls?
4797 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4799 struct MeshTransmissionDescriptor *dd;
4800 struct MeshPathInfo *path_info;
4801 struct MeshTunnelChildInfo *cinfo;
4802 struct GNUNET_PeerIdentity id;
4806 if (GNUNET_YES == clear_cls)
4808 switch (queue->type)
4810 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4811 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4812 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4814 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4815 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4816 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4817 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4818 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4819 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4821 " prebuilt message\n");
4822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4824 GNUNET_MESH_DEBUG_M2S(queue->type));
4826 data_descriptor_decrement_rc (dd->mesh_data);
4828 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4830 path_info = queue->cls;
4831 path_destroy (path_info->path);
4835 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4836 " type %s unknown!\n",
4837 GNUNET_MESH_DEBUG_M2S(queue->type));
4839 GNUNET_free_non_null (queue->cls);
4841 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4842 queue->peer->queue_tail,
4845 /* Delete from child_fc in the appropiate tunnel */
4846 max = queue->tunnel->fwd_queue_max;
4847 GNUNET_PEER_resolve (queue->peer->id, &id);
4848 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4851 for (i = 0; i < cinfo->send_buffer_n; i++)
4854 i2 = (cinfo->send_buffer_start + i) % max;
4855 if (cinfo->send_buffer[i2] == queue)
4857 /* Found corresponding entry in the send_buffer. Move all others back. */
4862 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4864 j2 = (cinfo->send_buffer_start + j) % max;
4865 j3 = (cinfo->send_buffer_start + j + 1) % max;
4866 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4869 cinfo->send_buffer[j3] = NULL;
4870 cinfo->send_buffer_n--;
4875 GNUNET_free (queue);
4880 * @brief Get the next transmittable message from the queue.
4882 * This will be the head, except in the case of being a data packet
4883 * not allowed by the destination peer.
4885 * @param peer Destination peer.
4887 * @return The next viable MeshPeerQueue element to send to that peer.
4888 * NULL when there are no transmittable messages.
4890 struct MeshPeerQueue *
4891 queue_get_next (const struct MeshPeerInfo *peer)
4893 struct MeshPeerQueue *q;
4894 struct MeshTunnel *t;
4895 struct MeshTransmissionDescriptor *info;
4896 struct MeshTunnelChildInfo *cinfo;
4897 struct GNUNET_MESH_Unicast *ucast;
4898 struct GNUNET_MESH_ToOrigin *to_orig;
4899 struct GNUNET_MESH_Multicast *mcast;
4900 struct GNUNET_PeerIdentity id;
4904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4905 for (q = peer->queue_head; NULL != q; q = q->next)
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4911 GNUNET_MESH_DEBUG_M2S(q->type));
4914 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4915 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4916 pid = ntohl (ucast->pid);
4917 GNUNET_PEER_resolve (info->peer->id, &id);
4918 cinfo = tunnel_get_neighbor_fc(t, &id);
4919 ack = cinfo->fwd_ack;
4921 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4922 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4923 pid = ntohl (to_orig->pid);
4926 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4927 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4928 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4930 // Not a multicast payload: multicast control traffic (destroy, etc)
4933 pid = ntohl (mcast->pid);
4934 GNUNET_PEER_resolve (info->peer->id, &id);
4935 cinfo = tunnel_get_neighbor_fc(t, &id);
4936 ack = cinfo->fwd_ack;
4939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4944 "********* ACK: %u, PID: %u\n",
4946 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4955 "********* NEXT!\n");
4958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4959 "********* nothing found\n");
4965 * Core callback to write a queued packet to core buffer
4967 * @param cls Closure (peer info).
4968 * @param size Number of bytes available in buf.
4969 * @param buf Where the to write the message.
4971 * @return number of bytes written to buf
4974 queue_send (void *cls, size_t size, void *buf)
4976 struct MeshPeerInfo *peer = cls;
4977 struct GNUNET_MessageHeader *msg;
4978 struct MeshPeerQueue *queue;
4979 struct MeshTunnel *t;
4980 struct MeshTunnelChildInfo *cinfo;
4981 struct GNUNET_PeerIdentity dst_id;
4984 peer->core_transmit = NULL;
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4988 queue = queue_get_next (peer);
4990 /* Queue has no internal mesh traffic nor sendable payload */
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4994 if (NULL == peer->queue_head)
4995 GNUNET_break (0); // Should've been canceled
4998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5000 GNUNET_PEER_resolve (peer->id, &dst_id);
5001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5002 "********* towards %s\n",
5003 GNUNET_i2s(&dst_id));
5004 /* Check if buffer size is enough for the message */
5005 if (queue->size > size)
5007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5008 "********* not enough room, reissue\n");
5009 peer->core_transmit =
5010 GNUNET_CORE_notify_transmit_ready (core_handle,
5013 GNUNET_TIME_UNIT_FOREVER_REL,
5020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5023 GNUNET_assert (0 < t->pending_messages);
5024 t->pending_messages--;
5025 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5029 "********* unicast: t->q (%u/%u)\n",
5030 t->fwd_queue_n, t->fwd_queue_max);
5032 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5039 switch (queue->type)
5042 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5043 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5044 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5045 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5046 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5048 "********* raw: %s\n",
5049 GNUNET_MESH_DEBUG_M2S (queue->type));
5051 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5052 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5053 data_size = send_core_data_raw (queue->cls, size, buf);
5054 msg = (struct GNUNET_MessageHeader *) buf;
5055 switch (ntohs (msg->type)) // Type of preconstructed message
5057 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5058 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5060 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5061 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5067 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5070 struct MeshTransmissionDescriptor *info = queue->cls;
5072 if ((1 == info->mesh_data->reference_counter
5073 && GNUNET_YES == t->speed_min)
5075 (info->mesh_data->total_out == info->mesh_data->reference_counter
5076 && GNUNET_NO == t->speed_min))
5078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5079 "********* considered sent\n");
5084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5085 "********* NOT considered sent yet\n");
5086 t->pending_messages++;
5089 data_size = send_core_data_multicast(queue->cls, size, buf);
5090 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5092 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5094 data_size = send_core_path_create (queue->cls, size, buf);
5096 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5098 data_size = send_core_path_ack (queue->cls, size, buf);
5100 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5102 data_size = send_core_data_multicast (queue->cls, size, buf);
5106 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5107 "********* type unknown: %u\n",
5111 switch (queue->type)
5113 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5114 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5115 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5116 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5117 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5120 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5121 "at pos %u (%p) != %p\n",
5122 cinfo->send_buffer_start,
5123 cinfo->send_buffer[cinfo->send_buffer_start],
5126 if (cinfo->send_buffer_n > 0)
5128 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5129 cinfo->send_buffer_n--;
5130 cinfo->send_buffer_start++;
5131 cinfo->send_buffer_start %= t->fwd_queue_max;
5142 /* Free queue, but cls was freed by send_core_* */
5143 queue_destroy (queue, GNUNET_NO);
5145 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5151 /* If more data in queue, send next */
5152 queue = queue_get_next(peer);
5155 struct GNUNET_PeerIdentity id;
5157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5158 GNUNET_PEER_resolve (peer->id, &id);
5159 peer->core_transmit =
5160 GNUNET_CORE_notify_transmit_ready(core_handle,
5163 GNUNET_TIME_UNIT_FOREVER_REL,
5171 if (NULL != peer->queue_head)
5173 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5174 "********* %s stalled\n",
5175 GNUNET_i2s(&my_full_id));
5177 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5178 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(MESH_POLL_TIME,
5179 &tunnel_poll, cinfo);
5182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5188 * @brief Queue and pass message to core when possible.
5190 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5191 * and accounts for it. In case the queue is full, the message is dropped and
5194 * Otherwise, message is treated as internal and allowed to go regardless of
5197 * @param cls Closure (@c type dependant). It will be used by queue_send to
5198 * build the message to be sent if not already prebuilt.
5199 * @param type Type of the message, 0 for a raw message.
5200 * @param size Size of the message.
5201 * @param dst Neighbor to send message to.
5202 * @param t Tunnel this message belongs to.
5205 queue_add (void *cls, uint16_t type, size_t size,
5206 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5208 struct MeshPeerQueue *queue;
5209 struct MeshTunnelChildInfo *cinfo;
5210 struct GNUNET_PeerIdentity id;
5216 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5217 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5219 n = &t->fwd_queue_n;
5220 max = &t->fwd_queue_max;
5222 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5224 n = &t->bck_queue_n;
5225 max = &t->bck_queue_max;
5232 GNUNET_STATISTICS_update(stats,
5233 "# messages dropped (buffer full)",
5235 return; // Drop message
5239 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5245 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5246 GNUNET_PEER_resolve (dst->id, &id);
5247 if (NULL == dst->core_transmit)
5249 dst->core_transmit =
5250 GNUNET_CORE_notify_transmit_ready (core_handle,
5253 GNUNET_TIME_UNIT_FOREVER_REL,
5259 t->pending_messages++;
5260 if (NULL == n) // Is this internal mesh traffic?
5263 // It's payload, keep track of buffer per peer.
5264 cinfo = tunnel_get_neighbor_fc(t, &id);
5265 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5266 if (NULL != cinfo->send_buffer[i])
5268 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5269 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5270 cinfo->send_buffer_start++;
5271 cinfo->send_buffer_start %= t->fwd_queue_max;
5275 cinfo->send_buffer_n++;
5277 cinfo->send_buffer[i] = queue;
5278 if (cinfo->send_buffer_n > t->fwd_queue_max)
5281 cinfo->send_buffer_n = t->fwd_queue_max;
5286 /******************************************************************************/
5287 /******************** MESH NETWORK HANDLERS **************************/
5288 /******************************************************************************/
5292 * Core handler for path creation
5294 * @param cls closure
5295 * @param message message
5296 * @param peer peer identity this notification is about
5297 * @param atsi performance data
5298 * @param atsi_count number of records in 'atsi'
5300 * @return GNUNET_OK to keep the connection open,
5301 * GNUNET_SYSERR to close it (signal serious error)
5304 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5305 const struct GNUNET_MessageHeader *message,
5306 const struct GNUNET_ATS_Information *atsi,
5307 unsigned int atsi_count)
5309 unsigned int own_pos;
5312 MESH_TunnelNumber tid;
5313 struct GNUNET_MESH_ManipulatePath *msg;
5314 struct GNUNET_PeerIdentity *pi;
5315 struct GNUNET_HashCode hash;
5316 struct MeshPeerPath *path;
5317 struct MeshPeerInfo *dest_peer_info;
5318 struct MeshPeerInfo *orig_peer_info;
5319 struct MeshTunnel *t;
5321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5322 "Received a path create msg [%s]\n",
5323 GNUNET_i2s (&my_full_id));
5324 size = ntohs (message->size);
5325 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5327 GNUNET_break_op (0);
5331 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5332 if (size % sizeof (struct GNUNET_PeerIdentity))
5334 GNUNET_break_op (0);
5337 size /= sizeof (struct GNUNET_PeerIdentity);
5340 GNUNET_break_op (0);
5343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5344 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5346 tid = ntohl (msg->tid);
5347 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5349 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5350 t = tunnel_get (pi, tid);
5351 if (NULL == t) // FIXME only for INCOMING tunnels?
5355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5356 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5359 // FIXME notify failure
5362 opt = ntohl (msg->opt);
5363 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5364 GNUNET_YES : GNUNET_NO;
5365 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5367 t->nobuffer = GNUNET_YES;
5368 t->last_fwd_ack = t->fwd_pid + 1;
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5371 " speed_min: %d, nobuffer:%d\n",
5372 t->speed_min, t->nobuffer);
5374 if (GNUNET_YES == t->nobuffer)
5376 t->bck_queue_max = 1;
5377 t->fwd_queue_max = 1;
5380 // FIXME only assign a local tid if a local client is interested (on demand)
5381 while (NULL != tunnel_get_incoming (next_local_tid))
5382 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5383 t->local_tid_dest = next_local_tid++;
5384 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5387 tunnel_reset_timeout (t);
5388 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5390 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5391 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5399 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5400 if (NULL == dest_peer_info)
5402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5403 " Creating PeerInfo for destination.\n");
5404 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5405 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5406 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5408 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5410 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5411 if (NULL == orig_peer_info)
5413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5414 " Creating PeerInfo for origin.\n");
5415 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5416 orig_peer_info->id = GNUNET_PEER_intern (pi);
5417 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5418 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5421 path = path_new (size);
5423 for (i = 0; i < size; i++)
5425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5426 GNUNET_i2s (&pi[i]));
5427 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5428 if (path->peers[i] == myid)
5431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5434 /* cannot be self, must be 'not found' */
5435 /* create path: self not found in path through self */
5436 GNUNET_break_op (0);
5437 path_destroy (path);
5441 path_add_to_peers (path, GNUNET_NO);
5442 tunnel_add_path (t, path, own_pos);
5443 if (own_pos == size - 1)
5445 /* It is for us! Send ack. */
5446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5447 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5448 if (NULL == t->peers)
5450 /* New tunnel! Notify clients on first payload message. */
5451 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5453 GNUNET_break (GNUNET_SYSERR !=
5454 GNUNET_CONTAINER_multihashmap_put (t->peers,
5455 &my_full_id.hashPubKey,
5458 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5463 struct MeshPeerPath *path2;
5465 /* It's for somebody else! Retransmit. */
5466 path2 = path_duplicate (path);
5467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5468 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5469 path2 = path_duplicate (path);
5470 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5471 send_create_path (dest_peer_info, path, t);
5478 * Core handler for path destruction
5480 * @param cls closure
5481 * @param message message
5482 * @param peer peer identity this notification is about
5483 * @param atsi performance data
5484 * @param atsi_count number of records in 'atsi'
5486 * @return GNUNET_OK to keep the connection open,
5487 * GNUNET_SYSERR to close it (signal serious error)
5490 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5491 const struct GNUNET_MessageHeader *message,
5492 const struct GNUNET_ATS_Information *atsi,
5493 unsigned int atsi_count)
5495 struct GNUNET_MESH_ManipulatePath *msg;
5496 struct GNUNET_PeerIdentity *pi;
5497 struct MeshPeerPath *path;
5498 struct MeshTunnel *t;
5499 unsigned int own_pos;
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5504 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5505 size = ntohs (message->size);
5506 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5508 GNUNET_break_op (0);
5512 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5513 if (size % sizeof (struct GNUNET_PeerIdentity))
5515 GNUNET_break_op (0);
5518 size /= sizeof (struct GNUNET_PeerIdentity);
5521 GNUNET_break_op (0);
5524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5526 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5527 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5529 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5531 t = tunnel_get (pi, ntohl (msg->tid));
5534 /* TODO notify back: we don't know this tunnel */
5535 GNUNET_break_op (0);
5538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5539 path = path_new (size);
5541 for (i = 0; i < size; i++)
5543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5544 GNUNET_i2s (&pi[i]));
5545 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5546 if (path->peers[i] == myid)
5549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5550 if (own_pos < path->length - 1)
5551 send_prebuilt_message (message, &pi[own_pos + 1], t);
5553 send_client_tunnel_disconnect(t, NULL);
5555 tunnel_delete_peer (t, path->peers[path->length - 1]);
5556 path_destroy (path);
5562 * Core handler for notifications of broken paths
5564 * @param cls closure
5565 * @param message message
5566 * @param peer peer identity this notification is about
5567 * @param atsi performance data
5568 * @param atsi_count number of records in 'atsi'
5570 * @return GNUNET_OK to keep the connection open,
5571 * GNUNET_SYSERR to close it (signal serious error)
5574 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5575 const struct GNUNET_MessageHeader *message,
5576 const struct GNUNET_ATS_Information *atsi,
5577 unsigned int atsi_count)
5579 struct GNUNET_MESH_PathBroken *msg;
5580 struct MeshTunnel *t;
5582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5583 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5584 msg = (struct GNUNET_MESH_PathBroken *) message;
5585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5586 GNUNET_i2s (&msg->peer1));
5587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5588 GNUNET_i2s (&msg->peer2));
5589 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5592 GNUNET_break_op (0);
5595 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5596 GNUNET_PEER_search (&msg->peer2));
5603 * Core handler for tunnel destruction
5605 * @param cls closure
5606 * @param message message
5607 * @param peer peer identity this notification is about
5608 * @param atsi performance data
5609 * @param atsi_count number of records in 'atsi'
5611 * @return GNUNET_OK to keep the connection open,
5612 * GNUNET_SYSERR to close it (signal serious error)
5615 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5616 const struct GNUNET_MessageHeader *message,
5617 const struct GNUNET_ATS_Information *atsi,
5618 unsigned int atsi_count)
5620 struct GNUNET_MESH_TunnelDestroy *msg;
5621 struct MeshTunnel *t;
5623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5624 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5625 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5627 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5628 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5631 /* Probably already got the message from another path,
5632 * destroyed the tunnel and retransmitted to children.
5635 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5638 if (t->id.oid == myid)
5640 GNUNET_break_op (0);
5643 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5645 /* Tunnel was incoming, notify clients */
5646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5647 t->local_tid, t->local_tid_dest);
5648 send_clients_tunnel_destroy (t);
5650 tunnel_send_destroy (t);
5651 t->destroy = GNUNET_YES;
5652 // TODO: add timeout to destroy the tunnel anyway
5658 * Core handler for mesh network traffic going from the origin to a peer
5660 * @param cls closure
5661 * @param peer peer identity this notification is about
5662 * @param message message
5663 * @param atsi performance data
5664 * @param atsi_count number of records in 'atsi'
5665 * @return GNUNET_OK to keep the connection open,
5666 * GNUNET_SYSERR to close it (signal serious error)
5669 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5670 const struct GNUNET_MessageHeader *message,
5671 const struct GNUNET_ATS_Information *atsi,
5672 unsigned int atsi_count)
5674 struct GNUNET_MESH_Unicast *msg;
5675 struct GNUNET_PeerIdentity *neighbor;
5676 struct MeshTunnelChildInfo *cinfo;
5677 struct MeshTunnel *t;
5678 GNUNET_PEER_Id dest_id;
5683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5686 size = ntohs (message->size);
5688 sizeof (struct GNUNET_MESH_Unicast) +
5689 sizeof (struct GNUNET_MessageHeader))
5694 msg = (struct GNUNET_MESH_Unicast *) message;
5695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5696 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5698 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5701 /* TODO notify back: we don't know this tunnel */
5702 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5703 GNUNET_break_op (0);
5706 pid = ntohl (msg->pid);
5707 if (t->fwd_pid == pid)
5709 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5710 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5711 " Already seen pid %u, DROPPING!\n", pid);
5716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5717 " pid %u not seen yet, forwarding\n", pid);
5720 t->skip += (pid - t->fwd_pid) - 1;
5723 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5725 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5726 GNUNET_break_op (0);
5727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5728 "Received PID %u, ACK %u\n",
5729 pid, t->last_fwd_ack);
5733 tunnel_reset_timeout (t);
5734 dest_id = GNUNET_PEER_search (&msg->destination);
5735 if (dest_id == myid)
5737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5738 " it's for us! sending to clients...\n");
5739 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5740 send_subscribed_clients (message, &msg[1].header, t);
5741 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5744 ttl = ntohl (msg->ttl);
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5748 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5750 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5754 " not for us, retransmitting...\n");
5756 neighbor = tree_get_first_hop (t->tree, dest_id);
5757 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5758 cinfo->fwd_pid = pid;
5759 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5762 if (GNUNET_YES == t->nobuffer &&
5763 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5765 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5766 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5767 GNUNET_break_op (0);
5770 send_prebuilt_message (message, neighbor, t);
5771 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5777 * Core handler for mesh network traffic going from the origin to all peers
5779 * @param cls closure
5780 * @param message message
5781 * @param peer peer identity this notification is about
5782 * @param atsi performance data
5783 * @param atsi_count number of records in 'atsi'
5784 * @return GNUNET_OK to keep the connection open,
5785 * GNUNET_SYSERR to close it (signal serious error)
5787 * TODO: Check who we got this from, to validate route.
5790 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5791 const struct GNUNET_MessageHeader *message,
5792 const struct GNUNET_ATS_Information *atsi,
5793 unsigned int atsi_count)
5795 struct GNUNET_MESH_Multicast *msg;
5796 struct MeshTunnel *t;
5800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5802 size = ntohs (message->size);
5803 if (sizeof (struct GNUNET_MESH_Multicast) +
5804 sizeof (struct GNUNET_MessageHeader) > size)
5806 GNUNET_break_op (0);
5809 msg = (struct GNUNET_MESH_Multicast *) message;
5810 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5814 /* TODO notify that we dont know that tunnel */
5815 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5816 GNUNET_break_op (0);
5819 pid = ntohl (msg->pid);
5820 if (t->fwd_pid == pid)
5822 /* already seen this packet, drop */
5823 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5825 " Already seen pid %u, DROPPING!\n", pid);
5826 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5832 " pid %u not seen yet, forwarding\n", pid);
5834 t->skip += (pid - t->fwd_pid) - 1;
5836 tunnel_reset_timeout (t);
5838 /* Transmit to locally interested clients */
5839 if (NULL != t->peers &&
5840 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5842 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5843 send_subscribed_clients (message, &msg[1].header, t);
5844 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5847 if (ntohl (msg->ttl) == 0)
5849 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5850 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5851 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5854 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5855 tunnel_send_multicast (t, message);
5861 * Core handler for mesh network traffic toward the owner of a tunnel
5863 * @param cls closure
5864 * @param message message
5865 * @param peer peer identity this notification is about
5866 * @param atsi performance data
5867 * @param atsi_count number of records in 'atsi'
5869 * @return GNUNET_OK to keep the connection open,
5870 * GNUNET_SYSERR to close it (signal serious error)
5873 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5874 const struct GNUNET_MessageHeader *message,
5875 const struct GNUNET_ATS_Information *atsi,
5876 unsigned int atsi_count)
5878 struct GNUNET_MESH_ToOrigin *msg;
5879 struct GNUNET_PeerIdentity id;
5880 struct MeshPeerInfo *peer_info;
5881 struct MeshTunnel *t;
5882 struct MeshTunnelChildInfo *cinfo;
5886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5888 size = ntohs (message->size);
5889 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5890 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5892 GNUNET_break_op (0);
5895 msg = (struct GNUNET_MESH_ToOrigin *) message;
5896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5897 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5898 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5899 pid = ntohl (msg->pid);
5903 /* TODO notify that we dont know this tunnel (whom)? */
5904 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5905 GNUNET_break_op (0);
5906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5907 "Received to_origin with PID %u on unknown tunnel\n",
5912 cinfo = tunnel_get_neighbor_fc(t, peer);
5919 if (cinfo->bck_pid == pid)
5921 /* already seen this packet, drop */
5922 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5924 " Already seen pid %u, DROPPING!\n", pid);
5925 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5930 " pid %u not seen yet, forwarding\n", pid);
5931 cinfo->bck_pid = pid;
5933 if (NULL != t->owner)
5936 struct GNUNET_MESH_ToOrigin *copy;
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5939 " it's for us! sending to clients...\n");
5940 /* TODO signature verification */
5941 memcpy (cbuf, message, size);
5942 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5943 copy->tid = htonl (t->local_tid);
5945 copy->pid = htonl (t->bck_pid);
5946 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5947 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5948 ©->header, GNUNET_NO);
5949 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5953 " not for us, retransmitting...\n");
5955 peer_info = peer_info_get (&msg->oid);
5956 if (NULL == peer_info)
5958 /* unknown origin of tunnel */
5962 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5963 send_prebuilt_message (message, &id, t);
5964 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5971 * Core handler for mesh network traffic point-to-point acks.
5973 * @param cls closure
5974 * @param message message
5975 * @param peer peer identity this notification is about
5976 * @param atsi performance data
5977 * @param atsi_count number of records in 'atsi'
5979 * @return GNUNET_OK to keep the connection open,
5980 * GNUNET_SYSERR to close it (signal serious error)
5983 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5984 const struct GNUNET_MessageHeader *message,
5985 const struct GNUNET_ATS_Information *atsi,
5986 unsigned int atsi_count)
5988 struct GNUNET_MESH_ACK *msg;
5989 struct MeshTunnel *t;
5992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5994 msg = (struct GNUNET_MESH_ACK *) message;
5996 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6000 /* TODO notify that we dont know this tunnel (whom)? */
6001 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6004 ack = ntohl (msg->pid);
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6007 /* Is this a forward or backward ACK? */
6008 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6010 struct MeshTunnelChildInfo *cinfo;
6013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6014 cinfo = tunnel_get_neighbor_fc (t, peer);
6015 cinfo->fwd_ack = ack;
6016 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6017 tunnel_unlock_fwd_queues (t);
6021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6023 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6024 tunnel_unlock_bck_queue (t);
6031 * Core handler for mesh network traffic point-to-point ack polls.
6033 * @param cls closure
6034 * @param message message
6035 * @param peer peer identity this notification is about
6036 * @param atsi performance data
6037 * @param atsi_count number of records in 'atsi'
6039 * @return GNUNET_OK to keep the connection open,
6040 * GNUNET_SYSERR to close it (signal serious error)
6043 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6044 const struct GNUNET_MessageHeader *message,
6045 const struct GNUNET_ATS_Information *atsi,
6046 unsigned int atsi_count)
6048 struct GNUNET_MESH_Poll *msg;
6049 struct MeshTunnel *t;
6051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6054 msg = (struct GNUNET_MESH_Poll *) message;
6056 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6060 /* TODO notify that we dont know this tunnel (whom)? */
6061 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6062 GNUNET_break_op (0);
6066 /* Is this a forward or backward ACK? */
6067 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6069 struct MeshTunnelChildInfo *cinfo;
6071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6072 cinfo = tunnel_get_neighbor_fc (t, peer);
6073 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6074 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6079 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6087 * Core handler for path ACKs
6089 * @param cls closure
6090 * @param message message
6091 * @param peer peer identity this notification is about
6092 * @param atsi performance data
6093 * @param atsi_count number of records in 'atsi'
6095 * @return GNUNET_OK to keep the connection open,
6096 * GNUNET_SYSERR to close it (signal serious error)
6099 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6100 const struct GNUNET_MessageHeader *message,
6101 const struct GNUNET_ATS_Information *atsi,
6102 unsigned int atsi_count)
6104 struct GNUNET_MESH_PathACK *msg;
6105 struct GNUNET_PeerIdentity id;
6106 struct MeshPeerInfo *peer_info;
6107 struct MeshPeerPath *p;
6108 struct MeshTunnel *t;
6110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6111 GNUNET_i2s (&my_full_id));
6112 msg = (struct GNUNET_MESH_PathACK *) message;
6113 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6116 /* TODO notify that we don't know the tunnel */
6117 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6119 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6123 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6125 peer_info = peer_info_get (&msg->peer_id);
6126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6127 GNUNET_i2s (&msg->peer_id));
6128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6131 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6134 "connect_by_string completed, stopping search\n");
6135 regex_cancel_search (t->regex_ctx);
6136 t->regex_ctx = NULL;
6139 /* Add paths to peers? */
6140 p = tree_get_path_to_peer (t->tree, peer_info->id);
6143 path_add_to_peers (p, GNUNET_YES);
6151 /* Message for us? */
6152 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6155 if (NULL == t->owner)
6157 GNUNET_break_op (0);
6160 if (NULL != t->dht_get_type)
6162 GNUNET_DHT_get_stop (t->dht_get_type);
6163 t->dht_get_type = NULL;
6165 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6167 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6168 send_client_peer_connected (t, peer_info->id);
6173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6174 " not for us, retransmitting...\n");
6175 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6176 peer_info = peer_info_get (&msg->oid);
6177 if (NULL == peer_info)
6179 /* If we know the tunnel, we should DEFINITELY know the peer */
6183 send_prebuilt_message (message, &id, t);
6189 * Core handler for mesh keepalives.
6191 * @param cls closure
6192 * @param message message
6193 * @param peer peer identity this notification is about
6194 * @param atsi performance data
6195 * @param atsi_count number of records in 'atsi'
6196 * @return GNUNET_OK to keep the connection open,
6197 * GNUNET_SYSERR to close it (signal serious error)
6199 * TODO: Check who we got this from, to validate route.
6202 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6203 const struct GNUNET_MessageHeader *message,
6204 const struct GNUNET_ATS_Information *atsi,
6205 unsigned int atsi_count)
6207 struct GNUNET_MESH_TunnelKeepAlive *msg;
6208 struct MeshTunnel *t;
6210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6213 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6214 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6218 /* TODO notify that we dont know that tunnel */
6219 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6220 GNUNET_break_op (0);
6224 tunnel_reset_timeout (t);
6226 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6227 tunnel_send_multicast (t, message);
6234 * Functions to handle messages from core
6236 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6237 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6238 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6239 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6240 sizeof (struct GNUNET_MESH_PathBroken)},
6241 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6242 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6243 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6244 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6245 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6246 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6247 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6248 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6249 sizeof (struct GNUNET_MESH_ACK)},
6250 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6251 sizeof (struct GNUNET_MESH_Poll)},
6252 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6253 sizeof (struct GNUNET_MESH_PathACK)},
6259 /******************************************************************************/
6260 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6261 /******************************************************************************/
6264 * deregister_app: iterator for removing each application registered by a client
6266 * @param cls closure
6267 * @param key the hash of the application id (used to access the hashmap)
6268 * @param value the value stored at the key (client)
6270 * @return GNUNET_OK on success
6273 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6275 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6276 GNUNET_break (GNUNET_YES ==
6277 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6283 * notify_client_connection_failure: notify a client that the connection to the
6284 * requested remote peer is not possible (for instance, no route found)
6285 * Function called when the socket is ready to queue more data. "buf" will be
6286 * NULL and "size" zero if the socket was closed for writing in the meantime.
6288 * @param cls closure
6289 * @param size number of bytes available in buf
6290 * @param buf where the callee should write the message
6291 * @return number of bytes written to buf
6294 notify_client_connection_failure (void *cls, size_t size, void *buf)
6297 struct MeshPeerInfo *peer_info;
6298 struct GNUNET_MESH_PeerControl *msg;
6299 struct GNUNET_PeerIdentity id;
6301 if (0 == size && NULL == buf)
6303 // TODO retry? cancel?
6307 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6308 peer_info = (struct MeshPeerInfo *) cls;
6309 msg = (struct GNUNET_MESH_PeerControl *) buf;
6310 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6311 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6312 // msg->tunnel_id = htonl(peer_info->t->tid);
6313 GNUNET_PEER_resolve (peer_info->id, &id);
6314 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6322 * Send keepalive packets for a peer
6324 * @param cls Closure (tunnel for which to send the keepalive).
6325 * @param tc Notification context.
6328 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6330 struct MeshTunnel *t = cls;
6331 struct GNUNET_MESH_TunnelKeepAlive *msg;
6332 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6335 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6336 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6342 "sending keepalive for tunnel %d\n", t->id.tid);
6344 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6345 msg->header.size = htons (size);
6346 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6347 msg->oid = my_full_id;
6348 msg->tid = htonl (t->id.tid);
6349 tunnel_send_multicast (t, &msg->header);
6351 t->path_refresh_task =
6352 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6353 tunnel_reset_timeout(t);
6358 * Function to process paths received for a new peer addition. The recorded
6359 * paths form the initial tunnel, which can be optimized later.
6360 * Called on each result obtained for the DHT search.
6362 * @param cls closure
6363 * @param exp when will this value expire
6364 * @param key key of the result
6365 * @param get_path path of the get request
6366 * @param get_path_length lenght of get_path
6367 * @param put_path path of the put request
6368 * @param put_path_length length of the put_path
6369 * @param type type of the result
6370 * @param size number of bytes in data
6371 * @param data pointer to the result data
6373 * TODO: re-issue the request after certain time? cancel after X results?
6376 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6377 const struct GNUNET_HashCode * key,
6378 const struct GNUNET_PeerIdentity *get_path,
6379 unsigned int get_path_length,
6380 const struct GNUNET_PeerIdentity *put_path,
6381 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6382 size_t size, const void *data)
6384 struct MeshPathInfo *path_info = cls;
6385 struct MeshPeerPath *p;
6386 struct GNUNET_PeerIdentity pi;
6389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6390 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6393 p = path_build_from_dht (get_path, get_path_length, put_path,
6395 path_add_to_peers (p, GNUNET_NO);
6397 for (i = 0; i < path_info->peer->ntunnels; i++)
6399 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6400 peer_info_connect (path_info->peer, path_info->t);
6408 * Function to process paths received for a new peer addition. The recorded
6409 * paths form the initial tunnel, which can be optimized later.
6410 * Called on each result obtained for the DHT search.
6412 * @param cls closure
6413 * @param exp when will this value expire
6414 * @param key key of the result
6415 * @param get_path path of the get request
6416 * @param get_path_length lenght of get_path
6417 * @param put_path path of the put request
6418 * @param put_path_length length of the put_path
6419 * @param type type of the result
6420 * @param size number of bytes in data
6421 * @param data pointer to the result data
6424 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6425 const struct GNUNET_HashCode * key,
6426 const struct GNUNET_PeerIdentity *get_path,
6427 unsigned int get_path_length,
6428 const struct GNUNET_PeerIdentity *put_path,
6429 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6430 size_t size, const void *data)
6432 const struct PBlock *pb = data;
6433 const struct GNUNET_PeerIdentity *pi = &pb->id;
6434 struct MeshTunnel *t = cls;
6435 struct MeshPeerInfo *peer_info;
6436 struct MeshPeerPath *p;
6438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6439 if (size != sizeof (struct PBlock))
6441 GNUNET_break_op (0);
6444 if (ntohl(pb->type) != t->type)
6446 GNUNET_break_op (0);
6449 GNUNET_assert (NULL != t->owner);
6450 peer_info = peer_info_get (pi);
6451 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6453 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6455 p = path_build_from_dht (get_path, get_path_length, put_path,
6457 path_add_to_peers (p, GNUNET_NO);
6459 tunnel_add_peer (t, peer_info);
6460 peer_info_connect (peer_info, t);
6465 * Function to process DHT string to regex matching.
6466 * Called on each result obtained for the DHT search.
6468 * @param cls closure (search context)
6469 * @param exp when will this value expire
6470 * @param key key of the result
6471 * @param get_path path of the get request (not used)
6472 * @param get_path_length lenght of get_path (not used)
6473 * @param put_path path of the put request (not used)
6474 * @param put_path_length length of the put_path (not used)
6475 * @param type type of the result
6476 * @param size number of bytes in data
6477 * @param data pointer to the result data
6480 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6481 const struct GNUNET_HashCode * key,
6482 const struct GNUNET_PeerIdentity *get_path,
6483 unsigned int get_path_length,
6484 const struct GNUNET_PeerIdentity *put_path,
6485 unsigned int put_path_length,
6486 enum GNUNET_BLOCK_Type type,
6487 size_t size, const void *data)
6489 const struct MeshRegexAccept *block = data;
6490 struct MeshRegexSearchContext *ctx = cls;
6491 struct MeshRegexSearchInfo *info = ctx->info;
6492 struct MeshPeerPath *p;
6493 struct MeshPeerInfo *peer_info;
6495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6498 peer_info = peer_info_get(&block->id);
6499 p = path_build_from_dht (get_path, get_path_length, put_path,
6501 path_add_to_peers (p, GNUNET_NO);
6504 tunnel_add_peer (info->t, peer_info);
6505 peer_info_connect (peer_info, info->t);
6506 if (0 == info->peer)
6508 info->peer = peer_info->id;
6512 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6515 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6516 ®ex_connect_timeout,
6524 * Function to process DHT string to regex matching.
6525 * Called on each result obtained for the DHT search.
6527 * @param cls closure (search context)
6528 * @param exp when will this value expire
6529 * @param key key of the result
6530 * @param get_path path of the get request (not used)
6531 * @param get_path_length lenght of get_path (not used)
6532 * @param put_path path of the put request (not used)
6533 * @param put_path_length length of the put_path (not used)
6534 * @param type type of the result
6535 * @param size number of bytes in data
6536 * @param data pointer to the result data
6538 * TODO: re-issue the request after certain time? cancel after X results?
6541 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6542 const struct GNUNET_HashCode * key,
6543 const struct GNUNET_PeerIdentity *get_path,
6544 unsigned int get_path_length,
6545 const struct GNUNET_PeerIdentity *put_path,
6546 unsigned int put_path_length,
6547 enum GNUNET_BLOCK_Type type,
6548 size_t size, const void *data)
6550 const struct MeshRegexBlock *block = data;
6551 struct MeshRegexSearchContext *ctx = cls;
6552 struct MeshRegexSearchInfo *info = ctx->info;
6556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6557 "DHT GET STRING RETURNED RESULTS\n");
6558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6559 " key: %s\n", GNUNET_h2s (key));
6561 copy = GNUNET_malloc (size);
6562 memcpy (copy, data, size);
6563 GNUNET_break (GNUNET_OK ==
6564 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6565 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6566 len = ntohl (block->n_proof);
6568 char proof[len + 1];
6570 memcpy (proof, &block[1], len);
6572 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6574 GNUNET_break_op (0);
6578 len = strlen (info->description);
6579 if (len == ctx->position) // String processed
6581 if (GNUNET_YES == ntohl (block->accepting))
6583 regex_find_path(key, ctx);
6587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6588 // FIXME REGEX this block not successful, wait for more? start timeout?
6593 regex_next_edge (block, size, ctx);
6598 /******************************************************************************/
6599 /********************* MESH LOCAL HANDLES **************************/
6600 /******************************************************************************/
6604 * Handler for client disconnection
6606 * @param cls closure
6607 * @param client identification of the client; NULL
6608 * for the last call when the server is destroyed
6611 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6613 struct MeshClient *c;
6614 struct MeshClient *next;
6617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6624 // return; uncomment for regex_profiler
6628 if (c->handle != client)
6630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6636 GNUNET_SERVER_client_drop (c->handle);
6637 c->shutting_down = GNUNET_YES;
6638 GNUNET_assert (NULL != c->own_tunnels);
6639 GNUNET_assert (NULL != c->incoming_tunnels);
6640 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6641 &tunnel_destroy_iterator, c);
6642 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6643 &tunnel_destroy_iterator, c);
6644 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6645 &tunnel_destroy_iterator, c);
6646 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6647 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6648 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6650 /* deregister clients applications */
6651 if (NULL != c->apps)
6653 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6654 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6656 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6657 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6659 GNUNET_SCHEDULER_cancel (announce_applications_task);
6660 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6662 if (NULL != c->types)
6663 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6664 for (i = 0; i < c->n_regex; i++)
6666 GNUNET_free (c->regexes[i].regex);
6667 if (NULL != c->regexes[i].dfa)
6668 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6670 GNUNET_free_non_null (c->regexes);
6671 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6672 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6674 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6677 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6686 * Handler for new clients
6688 * @param cls closure
6689 * @param client identification of the client
6690 * @param message the actual message, which includes messages the client wants
6693 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6694 const struct GNUNET_MessageHeader *message)
6696 struct GNUNET_MESH_ClientConnect *cc_msg;
6697 struct MeshClient *c;
6698 GNUNET_MESH_ApplicationType *a;
6705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6706 /* Check data sanity */
6707 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6708 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6709 ntypes = ntohs (cc_msg->types);
6710 napps = ntohs (cc_msg->applications);
6712 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6715 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6719 /* Create new client structure */
6720 c = GNUNET_malloc (sizeof (struct MeshClient));
6721 c->id = next_client_id++;
6722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6724 GNUNET_SERVER_client_keep (client);
6725 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6728 GNUNET_MESH_ApplicationType at;
6729 struct GNUNET_HashCode hc;
6731 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6732 for (i = 0; i < napps; i++)
6735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6736 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6737 /* store in clients hashmap */
6738 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6739 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6740 /* store in global hashmap, for announcements */
6741 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6742 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6744 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6745 announce_applications_task =
6746 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6752 struct GNUNET_HashCode hc;
6754 t = (uint16_t *) & a[napps];
6755 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6756 for (i = 0; i < ntypes; i++)
6759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6760 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6762 /* store in clients hashmap */
6763 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6764 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6765 /* store in global hashmap */
6766 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6767 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6771 " client has %u+%u subscriptions\n", napps, ntypes);
6773 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6774 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6775 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6776 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6777 GNUNET_SERVER_notification_context_add (nc, client);
6778 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6780 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6786 * Handler for clients announcing available services by a regular expression.
6788 * @param cls closure
6789 * @param client identification of the client
6790 * @param message the actual message, which includes messages the client wants
6793 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6794 const struct GNUNET_MessageHeader *message)
6796 struct GNUNET_MESH_RegexAnnounce *msg;
6797 struct MeshRegexDescriptor rd;
6798 struct MeshClient *c;
6802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6804 /* Sanity check for client registration */
6805 if (NULL == (c = client_get (client)))
6808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6813 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6814 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6815 regex = GNUNET_malloc (len + 1);
6816 memcpy (regex, &msg[1], len);
6819 rd.compression = ntohs (msg->compression_characters);
6821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6824 GNUNET_array_append (c->regexes, c->n_regex, rd);
6825 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6827 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6833 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6839 * Handler for requests of new tunnels
6841 * @param cls closure
6842 * @param client identification of the client
6843 * @param message the actual message
6846 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6847 const struct GNUNET_MessageHeader *message)
6849 struct GNUNET_MESH_TunnelMessage *t_msg;
6850 struct MeshTunnel *t;
6851 struct MeshClient *c;
6852 MESH_TunnelNumber tid;
6854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6856 /* Sanity check for client registration */
6857 if (NULL == (c = client_get (client)))
6860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6865 /* Message sanity check */
6866 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6873 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6874 /* Sanity check for tunnel numbering */
6875 tid = ntohl (t_msg->tunnel_id);
6876 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6882 /* Sanity check for duplicate tunnel IDs */
6883 if (NULL != tunnel_get_by_local_id (c, tid))
6886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6890 while (NULL != tunnel_get_by_pi (myid, next_tid))
6891 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6892 t = tunnel_new (myid, next_tid++, c, tid);
6895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6897 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6900 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6902 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6903 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6906 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6912 * Handler for requests of deleting tunnels
6914 * @param cls closure
6915 * @param client identification of the client
6916 * @param message the actual message
6919 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6920 const struct GNUNET_MessageHeader *message)
6922 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6923 struct MeshClient *c;
6924 struct MeshTunnel *t;
6925 MESH_TunnelNumber tid;
6927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6928 "Got a DESTROY TUNNEL from client!\n");
6930 /* Sanity check for client registration */
6931 if (NULL == (c = client_get (client)))
6934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6939 /* Message sanity check */
6940 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6947 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6949 /* Retrieve tunnel */
6950 tid = ntohl (tunnel_msg->tunnel_id);
6951 t = tunnel_get_by_local_id(c, tid);
6954 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6959 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6961 client_ignore_tunnel (c, t);
6963 // TODO: when to destroy incoming tunnel?
6964 if (t->nclients == 0)
6966 GNUNET_assert (GNUNET_YES ==
6967 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6969 GNUNET_assert (GNUNET_YES ==
6970 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6971 &my_full_id.hashPubKey,
6975 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6978 send_client_tunnel_disconnect(t, c);
6979 client_delete_tunnel(c, t);
6981 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6983 tunnel_send_destroy (t);
6984 t->destroy = GNUNET_YES;
6985 // The tunnel will be destroyed when the last message is transmitted.
6986 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6992 * Handler for requests of seeting tunnel's speed.
6994 * @param cls Closure (unused).
6995 * @param client Identification of the client.
6996 * @param message The actual message.
6999 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7000 const struct GNUNET_MessageHeader *message)
7002 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7003 struct MeshClient *c;
7004 struct MeshTunnel *t;
7005 MESH_TunnelNumber tid;
7007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7008 "Got a SPEED request from client!\n");
7010 /* Sanity check for client registration */
7011 if (NULL == (c = client_get (client)))
7014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7020 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7022 /* Retrieve tunnel */
7023 tid = ntohl (tunnel_msg->tunnel_id);
7024 t = tunnel_get_by_local_id(c, tid);
7027 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7033 switch (ntohs(message->type))
7035 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7036 t->speed_min = GNUNET_YES;
7038 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7039 t->speed_min = GNUNET_NO;
7044 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7049 * Handler for requests of seeting tunnel's buffering policy.
7051 * @param cls Closure (unused).
7052 * @param client Identification of the client.
7053 * @param message The actual message.
7056 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7057 const struct GNUNET_MessageHeader *message)
7059 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7060 struct MeshClient *c;
7061 struct MeshTunnel *t;
7062 MESH_TunnelNumber tid;
7064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7065 "Got a BUFFER request from client!\n");
7067 /* Sanity check for client registration */
7068 if (NULL == (c = client_get (client)))
7071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7076 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7078 /* Retrieve tunnel */
7079 tid = ntohl (tunnel_msg->tunnel_id);
7080 t = tunnel_get_by_local_id(c, tid);
7083 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7089 switch (ntohs(message->type))
7091 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7092 t->nobuffer = GNUNET_NO;
7094 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7095 t->nobuffer = GNUNET_YES;
7101 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7106 * Handler for connection requests to new peers
7108 * @param cls closure
7109 * @param client identification of the client
7110 * @param message the actual message (PeerControl)
7113 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7114 const struct GNUNET_MessageHeader *message)
7116 struct GNUNET_MESH_PeerControl *peer_msg;
7117 struct MeshPeerInfo *peer_info;
7118 struct MeshClient *c;
7119 struct MeshTunnel *t;
7120 MESH_TunnelNumber tid;
7122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7123 /* Sanity check for client registration */
7124 if (NULL == (c = client_get (client)))
7127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7132 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7134 /* Sanity check for message size */
7135 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7142 /* Tunnel exists? */
7143 tid = ntohl (peer_msg->tunnel_id);
7144 t = tunnel_get_by_local_id (c, tid);
7148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7152 /* Does client own tunnel? */
7153 if (t->owner->handle != client)
7156 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7160 GNUNET_i2s (&peer_msg->peer));
7161 peer_info = peer_info_get (&peer_msg->peer);
7163 tunnel_add_peer (t, peer_info);
7164 peer_info_connect (peer_info, t);
7166 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7172 * Handler for disconnection requests of peers in a tunnel
7174 * @param cls closure
7175 * @param client identification of the client
7176 * @param message the actual message (PeerControl)
7179 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7180 const struct GNUNET_MessageHeader *message)
7182 struct GNUNET_MESH_PeerControl *peer_msg;
7183 struct MeshPeerInfo *peer_info;
7184 struct MeshClient *c;
7185 struct MeshTunnel *t;
7186 MESH_TunnelNumber tid;
7188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7189 /* Sanity check for client registration */
7190 if (NULL == (c = client_get (client)))
7193 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7198 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7200 /* Sanity check for message size */
7201 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7204 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7208 /* Tunnel exists? */
7209 tid = ntohl (peer_msg->tunnel_id);
7210 t = tunnel_get_by_local_id (c, tid);
7214 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7219 /* Does client own tunnel? */
7220 if (t->owner->handle != client)
7223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7228 GNUNET_i2s (&peer_msg->peer));
7229 /* Is the peer in the tunnel? */
7231 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7232 if (NULL == peer_info)
7235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7239 /* Ok, delete peer from tunnel */
7240 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7241 &peer_msg->peer.hashPubKey);
7243 send_destroy_path (t, peer_info->id);
7244 tunnel_delete_peer (t, peer_info->id);
7245 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7250 * Handler for blacklist requests of peers in a tunnel
7252 * @param cls closure
7253 * @param client identification of the client
7254 * @param message the actual message (PeerControl)
7256 * FIXME implement DHT block bloomfilter
7259 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7260 const struct GNUNET_MessageHeader *message)
7262 struct GNUNET_MESH_PeerControl *peer_msg;
7263 struct MeshClient *c;
7264 struct MeshTunnel *t;
7265 MESH_TunnelNumber tid;
7267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7268 /* Sanity check for client registration */
7269 if (NULL == (c = client_get (client)))
7272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7277 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7279 /* Sanity check for message size */
7280 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7287 /* Tunnel exists? */
7288 tid = ntohl (peer_msg->tunnel_id);
7289 t = tunnel_get_by_local_id (c, tid);
7293 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7298 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7299 GNUNET_PEER_intern(&peer_msg->peer));
7304 * Handler for unblacklist requests of peers in a tunnel
7306 * @param cls closure
7307 * @param client identification of the client
7308 * @param message the actual message (PeerControl)
7311 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7312 const struct GNUNET_MessageHeader *message)
7314 struct GNUNET_MESH_PeerControl *peer_msg;
7315 struct MeshClient *c;
7316 struct MeshTunnel *t;
7317 MESH_TunnelNumber tid;
7321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7322 /* Sanity check for client registration */
7323 if (NULL == (c = client_get (client)))
7326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7331 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7333 /* Sanity check for message size */
7334 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7341 /* Tunnel exists? */
7342 tid = ntohl (peer_msg->tunnel_id);
7343 t = tunnel_get_by_local_id (c, tid);
7347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7352 /* if peer is not known, complain */
7353 pid = GNUNET_PEER_search (&peer_msg->peer);
7360 /* search and remove from list */
7361 for (i = 0; i < t->nblacklisted; i++)
7363 if (t->blacklisted[i] == pid)
7365 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7366 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7371 /* if peer hasn't been blacklisted, complain */
7377 * Handler for connection requests to new peers by type
7379 * @param cls closure
7380 * @param client identification of the client
7381 * @param message the actual message (ConnectPeerByType)
7384 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7385 const struct GNUNET_MessageHeader *message)
7387 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7388 struct MeshClient *c;
7389 struct MeshTunnel *t;
7390 struct GNUNET_HashCode hash;
7391 MESH_TunnelNumber tid;
7393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7394 /* Sanity check for client registration */
7395 if (NULL == (c = client_get (client)))
7398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7403 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7405 /* Sanity check for message size */
7406 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7407 ntohs (connect_msg->header.size))
7410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7414 /* Tunnel exists? */
7415 tid = ntohl (connect_msg->tunnel_id);
7416 t = tunnel_get_by_local_id (c, tid);
7420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7424 /* Does client own tunnel? */
7425 if (t->owner->handle != client)
7428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7432 /* Do WE have the service? */
7433 t->type = ntohl (connect_msg->type);
7434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7435 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7436 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7439 /* Yes! Fast forward, add ourselves to the tunnel and send the
7440 * good news to the client, and alert the destination client of
7441 * an incoming tunnel.
7443 * FIXME send a path create to self, avoid code duplication
7445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7446 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7447 peer_info_get (&my_full_id),
7448 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7451 send_client_peer_connected (t, myid);
7452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7453 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7455 t->local_tid_dest = next_local_tid++;
7456 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7457 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7458 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7462 /* Ok, lets find a peer offering the service */
7463 if (NULL != t->dht_get_type)
7465 GNUNET_DHT_get_stop (t->dht_get_type);
7467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7468 GNUNET_h2s (&hash));
7470 GNUNET_DHT_get_start (dht_handle,
7471 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7473 dht_replication_level,
7474 GNUNET_DHT_RO_RECORD_ROUTE |
7475 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7477 &dht_get_type_handler, t);
7479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7485 * Handler for connection requests to new peers by a string service description.
7487 * @param cls closure
7488 * @param client identification of the client
7489 * @param message the actual message, which includes messages the client wants
7492 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7493 const struct GNUNET_MessageHeader *message)
7495 struct GNUNET_MESH_ConnectPeerByString *msg;
7496 struct MeshRegexSearchContext *ctx;
7497 struct MeshRegexSearchInfo *info;
7498 struct GNUNET_DHT_GetHandle *get_h;
7499 struct GNUNET_HashCode key;
7500 struct MeshTunnel *t;
7501 struct MeshClient *c;
7502 MESH_TunnelNumber tid;
7507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7508 "Connect by string started\n");
7509 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7510 size = htons (message->size);
7512 /* Sanity check for client registration */
7513 if (NULL == (c = client_get (client)))
7516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7521 /* Message size sanity check */
7522 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7529 /* Tunnel exists? */
7530 tid = ntohl (msg->tunnel_id);
7531 t = tunnel_get_by_local_id (c, tid);
7535 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7539 /* Does client own tunnel? */
7540 if (t->owner->handle != client)
7543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7548 " on tunnel %s [%u]\n",
7549 GNUNET_i2s(&my_full_id),
7552 /* Only one connect_by_string allowed at the same time! */
7553 /* FIXME: allow more, return handle at api level to cancel, document */
7554 if (NULL != t->regex_ctx)
7557 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7561 /* Find string itself */
7562 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7563 string = (const char *) &msg[1];
7565 /* Initialize context */
7566 size = GNUNET_REGEX_get_first_key (string, len, &key);
7567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7568 " consumed %u bits out of %u\n", size, len);
7569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7570 " looking for %s\n", GNUNET_h2s (&key));
7572 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7574 info->description = GNUNET_strndup (string, len);
7575 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7576 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7579 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7580 ctx->position = size;
7584 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7586 /* Start search in DHT */
7587 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7588 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7589 &key, /* key to search */
7590 dht_replication_level, /* replication level */
7591 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7592 &info->description[size], /* xquery */
7593 // FIXME add BLOOMFILTER to exclude filtered peers
7594 len + 1 - size, /* xquery bits */
7595 // FIXME add BLOOMFILTER SIZE
7596 &dht_get_string_handler, ctx);
7598 GNUNET_break (GNUNET_OK ==
7599 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7602 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7604 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7610 * Handler for client traffic directed to one peer
7612 * @param cls closure
7613 * @param client identification of the client
7614 * @param message the actual message
7617 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7618 const struct GNUNET_MessageHeader *message)
7620 struct MeshClient *c;
7621 struct MeshTunnel *t;
7622 struct MeshPeerInfo *pi;
7623 struct GNUNET_MESH_Unicast *data_msg;
7624 MESH_TunnelNumber tid;
7627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7628 "Got a unicast request from a client!\n");
7630 /* Sanity check for client registration */
7631 if (NULL == (c = client_get (client)))
7634 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7639 data_msg = (struct GNUNET_MESH_Unicast *) message;
7641 /* Sanity check for message size */
7642 size = ntohs (message->size);
7643 if (sizeof (struct GNUNET_MESH_Unicast) +
7644 sizeof (struct GNUNET_MessageHeader) > size)
7647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7651 /* Tunnel exists? */
7652 tid = ntohl (data_msg->tid);
7653 t = tunnel_get_by_local_id (c, tid);
7657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7661 /* Is it a local tunnel? Then, does client own the tunnel? */
7662 if (t->owner->handle != client)
7665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7669 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7670 &data_msg->destination.hashPubKey);
7671 /* Is the selected peer in the tunnel? */
7675 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7679 /* PID should be as expected */
7680 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7683 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7684 "Unicast PID, expected %u, got %u\n",
7685 t->fwd_pid + 1, ntohl (data_msg->pid));
7686 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7690 /* Ok, everything is correct, send the message
7691 * (pretend we got it from a mesh peer)
7694 /* Work around const limitation */
7695 char buf[ntohs (message->size)] GNUNET_ALIGN;
7696 struct GNUNET_MESH_Unicast *copy;
7698 copy = (struct GNUNET_MESH_Unicast *) buf;
7699 memcpy (buf, data_msg, size);
7700 copy->oid = my_full_id;
7701 copy->tid = htonl (t->id.tid);
7702 copy->ttl = htonl (default_ttl);
7703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7704 " calling generic handler...\n");
7705 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7708 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7715 * Handler for client traffic directed to the origin
7717 * @param cls closure
7718 * @param client identification of the client
7719 * @param message the actual message
7722 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7723 const struct GNUNET_MessageHeader *message)
7725 struct GNUNET_MESH_ToOrigin *data_msg;
7726 struct MeshTunnelClientInfo *clinfo;
7727 struct MeshClient *c;
7728 struct MeshTunnel *t;
7729 MESH_TunnelNumber tid;
7732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7733 "Got a ToOrigin request from a client!\n");
7734 /* Sanity check for client registration */
7735 if (NULL == (c = client_get (client)))
7738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7743 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7745 /* Sanity check for message size */
7746 size = ntohs (message->size);
7747 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7748 sizeof (struct GNUNET_MessageHeader) > size)
7751 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7755 /* Tunnel exists? */
7756 tid = ntohl (data_msg->tid);
7757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7758 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7764 t = tunnel_get_by_local_id (c, tid);
7767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7774 /* It should be sent by someone who has this as incoming tunnel. */
7775 if (GNUNET_NO == client_knows_tunnel (c, t))
7778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7782 /* PID should be as expected */
7783 clinfo = tunnel_get_client_fc (t, c);
7784 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7787 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7788 "To Origin PID, expected %u, got %u\n",
7789 clinfo->bck_pid + 1,
7790 ntohl (data_msg->pid));
7791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7796 /* Ok, everything is correct, send the message
7797 * (pretend we got it from a mesh peer)
7800 char buf[ntohs (message->size)] GNUNET_ALIGN;
7801 struct GNUNET_MESH_ToOrigin *copy;
7803 /* Work around const limitation */
7804 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7805 memcpy (buf, data_msg, size);
7806 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7807 copy->tid = htonl (t->id.tid);
7808 copy->ttl = htonl (default_ttl);
7809 copy->pid = htonl (t->bck_pid + 1);
7811 copy->sender = my_full_id;
7812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7813 " calling generic handler...\n");
7814 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7816 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7823 * Handler for client traffic directed to all peers in a tunnel
7825 * @param cls closure
7826 * @param client identification of the client
7827 * @param message the actual message
7830 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7831 const struct GNUNET_MessageHeader *message)
7833 struct MeshClient *c;
7834 struct MeshTunnel *t;
7835 struct GNUNET_MESH_Multicast *data_msg;
7836 MESH_TunnelNumber tid;
7838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7839 "Got a multicast request from a client!\n");
7841 /* Sanity check for client registration */
7842 if (NULL == (c = client_get (client)))
7845 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7850 data_msg = (struct GNUNET_MESH_Multicast *) message;
7852 /* Sanity check for message size */
7853 if (sizeof (struct GNUNET_MESH_Multicast) +
7854 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7861 /* Tunnel exists? */
7862 tid = ntohl (data_msg->tid);
7863 t = tunnel_get_by_local_id (c, tid);
7867 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7868 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7870 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7874 /* Does client own tunnel? */
7875 if (t->owner->handle != client)
7878 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7882 /* PID should be as expected */
7883 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7886 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7887 "Multicast PID, expected %u, got %u\n",
7888 t->fwd_pid + 1, ntohl (data_msg->pid));
7889 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7894 char buf[ntohs (message->size)] GNUNET_ALIGN;
7895 struct GNUNET_MESH_Multicast *copy;
7897 copy = (struct GNUNET_MESH_Multicast *) buf;
7898 memcpy (buf, message, ntohs (message->size));
7899 copy->oid = my_full_id;
7900 copy->tid = htonl (t->id.tid);
7901 copy->ttl = htonl (default_ttl);
7902 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7904 " calling generic handler...\n");
7905 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7908 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7914 * Handler for client's ACKs for payload traffic.
7916 * @param cls Closure (unused).
7917 * @param client Identification of the client.
7918 * @param message The actual message.
7921 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7922 const struct GNUNET_MessageHeader *message)
7924 struct GNUNET_MESH_LocalAck *msg;
7925 struct MeshTunnel *t;
7926 struct MeshClient *c;
7927 MESH_TunnelNumber tid;
7930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7931 /* Sanity check for client registration */
7932 if (NULL == (c = client_get (client)))
7935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7940 msg = (struct GNUNET_MESH_LocalAck *) message;
7942 /* Tunnel exists? */
7943 tid = ntohl (msg->tunnel_id);
7944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7945 t = tunnel_get_by_local_id (c, tid);
7949 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7955 ack = ntohl (msg->max_pid);
7956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7958 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7959 if (NULL != t->owner && t->owner->handle == client)
7961 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7963 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7967 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7968 tunnel_set_client_fwd_ack (t, c, ack);
7969 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7972 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7979 * Functions to handle messages from clients
7981 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7982 {&handle_local_new_client, NULL,
7983 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7984 {&handle_local_announce_regex, NULL,
7985 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7986 {&handle_local_tunnel_create, NULL,
7987 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7988 sizeof (struct GNUNET_MESH_TunnelMessage)},
7989 {&handle_local_tunnel_destroy, NULL,
7990 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7991 sizeof (struct GNUNET_MESH_TunnelMessage)},
7992 {&handle_local_tunnel_speed, NULL,
7993 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7994 sizeof (struct GNUNET_MESH_TunnelMessage)},
7995 {&handle_local_tunnel_speed, NULL,
7996 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7997 sizeof (struct GNUNET_MESH_TunnelMessage)},
7998 {&handle_local_tunnel_buffer, NULL,
7999 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8000 sizeof (struct GNUNET_MESH_TunnelMessage)},
8001 {&handle_local_tunnel_buffer, NULL,
8002 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8003 sizeof (struct GNUNET_MESH_TunnelMessage)},
8004 {&handle_local_connect_add, NULL,
8005 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8006 sizeof (struct GNUNET_MESH_PeerControl)},
8007 {&handle_local_connect_del, NULL,
8008 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8009 sizeof (struct GNUNET_MESH_PeerControl)},
8010 {&handle_local_blacklist, NULL,
8011 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8012 sizeof (struct GNUNET_MESH_PeerControl)},
8013 {&handle_local_unblacklist, NULL,
8014 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8015 sizeof (struct GNUNET_MESH_PeerControl)},
8016 {&handle_local_connect_by_type, NULL,
8017 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8018 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8019 {&handle_local_connect_by_string, NULL,
8020 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8021 {&handle_local_unicast, NULL,
8022 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8023 {&handle_local_to_origin, NULL,
8024 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8025 {&handle_local_multicast, NULL,
8026 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8027 {&handle_local_ack, NULL,
8028 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8029 sizeof (struct GNUNET_MESH_LocalAck)},
8035 * To be called on core init/fail.
8037 * @param cls service closure
8038 * @param server handle to the server for this service
8039 * @param identity the public identity of this peer
8042 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8043 const struct GNUNET_PeerIdentity *identity)
8046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8047 core_handle = server;
8048 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8051 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8052 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8054 GNUNET_abort(); // Try harder
8061 * Method called whenever a given peer connects.
8063 * @param cls closure
8064 * @param peer peer identity this notification is about
8065 * @param atsi performance data for the connection
8066 * @param atsi_count number of records in 'atsi'
8069 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8070 const struct GNUNET_ATS_Information *atsi,
8071 unsigned int atsi_count)
8073 struct MeshPeerInfo *peer_info;
8074 struct MeshPeerPath *path;
8076 DEBUG_CONN ("Peer connected\n");
8077 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8078 peer_info = peer_info_get (peer);
8079 if (myid == peer_info->id)
8081 DEBUG_CONN (" (self)\n");
8086 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8088 path = path_new (2);
8089 path->peers[0] = myid;
8090 path->peers[1] = peer_info->id;
8091 GNUNET_PEER_change_rc (myid, 1);
8092 GNUNET_PEER_change_rc (peer_info->id, 1);
8093 peer_info_add_path (peer_info, path, GNUNET_YES);
8094 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8100 * Method called whenever a peer disconnects.
8102 * @param cls closure
8103 * @param peer peer identity this notification is about
8106 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8108 struct MeshPeerInfo *pi;
8109 struct MeshPeerQueue *q;
8110 struct MeshPeerQueue *n;
8112 DEBUG_CONN ("Peer disconnected\n");
8113 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8123 /* TODO try to reroute this traffic instead */
8124 queue_destroy(q, GNUNET_YES);
8127 if (NULL != pi->core_transmit)
8129 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8130 pi->core_transmit = NULL;
8132 peer_info_remove_path (pi, pi->id, myid);
8135 DEBUG_CONN (" (self)\n");
8137 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8142 /******************************************************************************/
8143 /************************ MAIN FUNCTIONS ****************************/
8144 /******************************************************************************/
8147 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8149 * @param cls closure
8150 * @param key current key code
8151 * @param value value in the hash map
8152 * @return GNUNET_YES if we should continue to iterate,
8156 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8158 struct MeshTunnel *t = value;
8165 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8167 * @param cls closure
8168 * @param key current key code
8169 * @param value value in the hash map
8170 * @return GNUNET_YES if we should continue to iterate,
8174 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8176 struct MeshPeerInfo *p = value;
8177 struct MeshPeerQueue *q;
8178 struct MeshPeerQueue *n;
8186 queue_destroy(q, GNUNET_YES);
8190 peer_info_destroy (p);
8196 * Task run during shutdown.
8202 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8206 if (core_handle != NULL)
8208 GNUNET_CORE_disconnect (core_handle);
8213 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8216 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8217 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8218 if (dht_handle != NULL)
8220 GNUNET_DHT_disconnect (dht_handle);
8225 GNUNET_SERVER_notification_context_destroy (nc);
8228 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8230 GNUNET_SCHEDULER_cancel (announce_id_task);
8231 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8238 * Callback for hostkey read/generation
8241 * @param pk the private key
8242 * @param emsg error message
8245 key_generation_cb (void *cls,
8246 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8249 struct MeshPeerInfo *peer;
8250 struct MeshPeerPath *p;
8255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8256 _("Mesh service could not access hostkey. Exiting.\n"));
8257 GNUNET_SCHEDULER_shutdown ();
8260 my_private_key = pk;
8261 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8262 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8263 &my_full_id.hashPubKey);
8264 myid = GNUNET_PEER_intern (&my_full_id);
8265 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8266 "Mesh for peer [%s] starting\n",
8267 GNUNET_i2s(&my_full_id));
8269 // transport_handle = GNUNET_TRANSPORT_connect(c,
8279 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8282 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8283 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8284 GNUNET_SERVER_disconnect_notify (server_handle,
8285 &handle_local_client_disconnect, NULL);
8289 clients_tail = NULL;
8292 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8293 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8295 /* Create a peer_info for the local peer */
8296 peer = peer_info_get (&my_full_id);
8299 GNUNET_PEER_change_rc (myid, 1);
8300 peer_info_add_path (peer, p, GNUNET_YES);
8301 GNUNET_SERVER_resume (server_handle);
8302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8307 * Process mesh requests.
8309 * @param cls closure
8310 * @param server the initialized server
8311 * @param c configuration to use
8314 run (void *cls, struct GNUNET_SERVER_Handle *server,
8315 const struct GNUNET_CONFIGURATION_Handle *c)
8319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8320 server_handle = server;
8321 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8322 NULL, /* Closure passed to MESH functions */
8323 &core_init, /* Call core_init once connected */
8324 &core_connect, /* Handle connects */
8325 &core_disconnect, /* remove peers on disconnects */
8326 NULL, /* Don't notify about all incoming messages */
8327 GNUNET_NO, /* For header only in notification */
8328 NULL, /* Don't notify about all outbound messages */
8329 GNUNET_NO, /* For header-only out notification */
8330 core_handlers); /* Register these handlers */
8332 if (core_handle == NULL)
8335 GNUNET_SCHEDULER_shutdown ();
8340 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8345 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8347 GNUNET_SCHEDULER_shutdown ();
8352 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8353 &refresh_path_time))
8355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8357 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8358 "mesh", "refresh path time");
8359 GNUNET_SCHEDULER_shutdown ();
8364 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8365 &app_announce_time))
8367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8369 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8370 "mesh", "app announce time");
8371 GNUNET_SCHEDULER_shutdown ();
8374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8377 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8382 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8383 "mesh", "id announce time");
8384 GNUNET_SCHEDULER_shutdown ();
8392 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8397 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8398 "mesh", "connect timeout");
8399 GNUNET_SCHEDULER_shutdown ();
8404 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8409 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8410 "mesh", "max msgs queue");
8411 GNUNET_SCHEDULER_shutdown ();
8416 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8421 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8422 "mesh", "max tunnels");
8423 GNUNET_SCHEDULER_shutdown ();
8428 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8433 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8434 "mesh", "default ttl", 64);
8439 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8440 &dht_replication_level))
8442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8444 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8445 "mesh", "dht replication level", 10);
8446 dht_replication_level = 10;
8449 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8450 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8451 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8452 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8453 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8455 dht_handle = GNUNET_DHT_connect (c, 64);
8456 if (NULL == dht_handle)
8460 stats = GNUNET_STATISTICS_create ("mesh", c);
8462 GNUNET_SERVER_suspend (server_handle);
8463 /* Scheduled the task to clean up when shutdown is called */
8464 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8466 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8467 GNUNET_free (keyfile);
8472 * The main function for the mesh service.
8474 * @param argc number of arguments from the command line
8475 * @param argv command line arguments
8476 * @return 0 ok, 1 on error
8479 main (int argc, char *const *argv)
8484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8485 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8487 ret = (GNUNET_OK == r) ? 0 : 1;
8488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8492 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8493 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8494 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);