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 #if MESH_DEBUG_CONNECTION
65 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_CONN(...)
71 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
73 #define DEBUG_DHT(...)
80 struct timespec __mesh_start;
81 struct timespec __mesh_end;
82 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
83 #define INTERVAL_END \
85 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
86 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
87 if (__diff < 0) __diff += 1000000000;\
91 #define INTERVAL_SHOW \
93 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
95 #define INTERVAL_START
100 /******************************************************************************/
101 /************************ DATA STRUCTURES ****************************/
102 /******************************************************************************/
104 /** FWD declaration */
110 * Struct representing a piece of data being sent to other peers
114 /** Tunnel it belongs to. */
115 struct MeshTunnel *t;
117 /** How many remaining neighbors still hav't got it. */
118 unsigned int reference_counter;
120 /** How many remaining neighbors we need to send this to. */
121 unsigned int total_out;
123 /** Size of the data. */
132 * Struct containing info about a queued transmission to this peer
139 struct MeshPeerQueue *next;
144 struct MeshPeerQueue *prev;
147 * Peer this transmission is directed to.
149 struct MeshPeerInfo *peer;
152 * Tunnel this message belongs to.
154 struct MeshTunnel *tunnel;
157 * Pointer to info stucture used as cls.
167 * Size of the message
174 * Struct to store regex information announced by clients.
176 struct MeshRegexDescriptor
179 * Regular expression itself.
184 * How many characters per edge can we squeeze?
186 uint16_t compression;
190 * Struct containing all info possibly needed to build a package when called
193 struct MeshTransmissionDescriptor
195 /** ID of the tunnel this packet travels in */
196 struct MESH_TunnelID *origin;
198 /** Who was this message being sent to */
199 struct MeshPeerInfo *peer;
201 /** Ultimate destination of the packet */
202 GNUNET_PEER_Id destination;
204 /** Data descriptor */
205 struct MeshData* mesh_data;
210 * Struct containing all information regarding a given peer
220 * Last time we heard from this peer
222 struct GNUNET_TIME_Absolute last_contact;
225 * Task handler for delayed connect task;
227 GNUNET_SCHEDULER_TaskIdentifier connect_task;
230 * Number of attempts to reconnect so far
232 int n_reconnect_attempts;
235 * Paths to reach the peer, ordered by ascending hop count
237 struct MeshPeerPath *path_head;
240 * Paths to reach the peer, ordered by ascending hop count
242 struct MeshPeerPath *path_tail;
245 * Handle to stop the DHT search for a path to this peer
247 struct GNUNET_DHT_GetHandle *dhtget;
250 * Closure given to the DHT GET
252 struct MeshPathInfo *dhtgetcls;
255 * Array of tunnels this peer participates in
256 * (most probably a small amount, therefore not a hashmap)
257 * When the path to the peer changes, notify these tunnels to let them
258 * re-adjust their path trees.
260 struct MeshTunnel **tunnels;
263 * Number of tunnels this peers participates in
265 unsigned int ntunnels;
268 * Transmission queue to core DLL head
270 struct MeshPeerQueue *queue_head;
273 * Transmission queue to core DLL tail
275 struct MeshPeerQueue *queue_tail;
278 * How many messages are in the queue to this peer.
280 unsigned int queue_n;
283 * Handle to for queued transmissions
285 struct GNUNET_CORE_TransmitHandle *core_transmit;
290 * Globally unique tunnel identification (owner + number)
291 * DO NOT USE OVER THE NETWORK
296 * Node that owns the tunnel
301 * Tunnel number to differentiate all the tunnels owned by the node oid
302 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
304 MESH_TunnelNumber tid;
309 * Struct containing all information regarding a tunnel
310 * For an intermediate node the improtant info used will be:
311 * - id Tunnel unique identification
312 * - paths[0] To know where to send it next
313 * - metainfo: ready, speeds, accounting
320 struct MESH_TunnelID id;
323 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
325 MESH_TunnelNumber local_tid;
328 * Local tunnel number for local destination clients (incoming number)
329 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
332 MESH_TunnelNumber local_tid_dest;
335 * Is the speed on the tunnel limited to the slowest peer?
340 * Is the tunnel bufferless (minimum latency)?
345 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
350 * Packet ID of the last bck packet sent (unique counter per hop).
355 * SKIP value for this tunnel.
360 * MeshTunnelFlowControlInfo of all children, indexed by GNUNET_PEER_Id.
361 * Contains the Flow Control info: FWD ACK value received,
362 * last BCK ACK sent, PID and SKIP values.
364 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
367 * Last ACK sent towards the origin (for traffic towards leaf node).
369 uint32_t last_fwd_ack;
372 * BCK ACK value received from the hop towards the owner of the tunnel,
373 * (previous node / owner): up to what message PID can we sent back to him.
378 * How many messages are in the forward queue (towards leaves).
380 unsigned int fwd_queue_n;
383 * How many messages do we accept in the forward queue.
385 unsigned int fwd_queue_max;
388 * How many messages are in the backward queue (towards origin).
390 unsigned int bck_queue_n;
393 * How many messages do we accept in the backward queue.
395 unsigned int bck_queue_max;
398 * Task to poll peer in case of a stall.
400 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
403 * Last time the tunnel was used
405 struct GNUNET_TIME_Absolute timestamp;
408 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
409 * containing peers added by id or by type, not intermediate peers.
411 struct GNUNET_CONTAINER_MultiHashMap *peers;
414 * Number of peers that are connected and potentially ready to receive data
416 unsigned int peers_ready;
419 * Number of peers that have been added to the tunnel
421 unsigned int peers_total;
424 * Client owner of the tunnel, if any
426 struct MeshClient *owner;
429 * Clients that have been informed about and want to stay in the tunnel.
431 struct MeshClient **clients;
434 * Flow control info for each client.
436 struct MeshTunnelFlowControlInfo *clients_fc;
439 * Number of elements in clients/clients_fc
441 unsigned int nclients;
444 * Clients that have been informed but requested to leave the tunnel.
446 struct MeshClient **ignore;
449 * Number of elements in clients
451 unsigned int nignore;
456 GNUNET_PEER_Id *blacklisted;
459 * Number of elements in blacklisted
461 unsigned int nblacklisted;
464 * Bloomfilter (for peer identities) to stop circular routes
466 char bloomfilter[MESH_BLOOM_SIZE];
471 struct MeshTunnelTree *tree;
474 * Application type we are looking for in this tunnel
476 GNUNET_MESH_ApplicationType type;
479 * Used to search peers offering a service
481 struct GNUNET_DHT_GetHandle *dht_get_type;
484 * Initial context of the regex search for a connect_by_string
486 struct MeshRegexSearchContext *regex_ctx;
489 * Task to keep the used paths alive
491 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
494 * Task to destroy the tunnel after timeout
496 * FIXME: merge the two? a tunnel will have either
497 * a path refresh OR a timeout, never both!
499 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
502 * Flag to signal the destruction of the tunnel.
503 * If this is set GNUNET_YES the tunnel will be destroyed
504 * when the queue is empty.
511 * Flow control info about a node in a tunnel. The node can be a local client
514 struct MeshTunnelFlowControlInfo
517 * Peer info of the node, NULL if local client.
519 struct MeshPeerInfo *peer;
522 * Client info of the node, NULL if remote peer.
524 struct MeshClient *client;
542 * Maximum PID they allowed us to send (FWD ACK received).
547 * Maximum PID we allowed them to send (BCK ACK sent).
552 * Circular buffer pointing to MeshPeerQueue elements for all
553 * payload traffic going to this node.
554 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
556 struct MeshPeerQueue **send_buffer;
559 * Index of the oldest element in the send_buffer.
561 unsigned int send_buffer_start;
564 * How many elements are already in the buffer.
566 unsigned int send_buffer_n;
569 * Tunnel this info is about
571 struct MeshTunnel *t;
574 * Task to poll peer in case of a stall.
576 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
581 * Info collected during iteration of child nodes in order to get the ACK value
584 struct MeshTunnelChildIteratorContext
587 * Tunnel whose info is being collected.
589 struct MeshTunnel *t;
592 * Is this context initialized? Is the value in max_child_ack valid?
597 * Maximum child ACK so far.
599 uint32_t max_child_ack;
602 * Number of children nodes
604 unsigned int nchildren;
609 * Info needed to work with tunnel paths and peers
616 struct MeshTunnel *t;
619 * Neighbouring peer to whom we send the packet to
621 struct MeshPeerInfo *peer;
626 struct MeshPeerPath *path;
631 * Struct containing information about a client of the service
638 struct MeshClient *next;
643 struct MeshClient *prev;
646 * Tunnels that belong to this client, indexed by local id
648 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
651 * Tunnels this client has accepted, indexed by incoming local id
653 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
656 * Tunnels this client has rejected, indexed by incoming local id
658 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
660 * Handle to communicate with the client
662 struct GNUNET_SERVER_Client *handle;
665 * Applications that this client has claimed to provide
667 struct GNUNET_CONTAINER_MultiHashMap *apps;
670 * Messages that this client has declared interest in
672 struct GNUNET_CONTAINER_MultiHashMap *types;
675 * Whether the client is active or shutting down (don't send confirmations
676 * to a client that is shutting down.
681 * ID of the client, mainly for debug messages
686 * Regular expressions describing the services offered by this client.
688 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
691 * Number of regular expressions in regexes.
693 unsigned int n_regex;
696 * Task to refresh all regular expresions in the DHT.
698 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
704 * Struct to keep information of searches of services described by a regex
705 * using a user-provided string service description.
707 struct MeshRegexSearchInfo
710 * Which tunnel is this for
712 struct MeshTunnel *t;
715 * User provided description of the searched service.
720 * Part of the description already consumed by the search.
727 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
730 * Results from running DHT GETs.
732 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
735 * Contexts, for each running DHT GET. Free all on end of search.
737 struct MeshRegexSearchContext **contexts;
740 * Number of contexts (branches/steps in search).
742 unsigned int n_contexts;
745 * Peer that is connecting via connect_by_string. When connected, free ctx.
750 * Other peers that are found but not yet being connected to.
752 GNUNET_PEER_Id *peers;
755 * Number of elements in peers.
757 unsigned int n_peers;
760 * Next peer to try to connect to.
765 * Timeout for a connect attempt.
766 * When reached, try to connect to a different peer, if any. If not,
767 * try the same peer again.
769 GNUNET_SCHEDULER_TaskIdentifier timeout;
774 * Struct to keep state of running searches that have consumed a part of
777 struct MeshRegexSearchContext
780 * Part of the description already consumed by
781 * this particular search branch.
786 * Information about the search.
788 struct MeshRegexSearchInfo *info;
791 * We just want to look for one edge, the longer the better.
794 unsigned int longest_match;
797 * Destination hash of the longest match.
799 struct GNUNET_HashCode hash;
802 /******************************************************************************/
803 /************************ DEBUG FUNCTIONS ****************************/
804 /******************************************************************************/
808 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
809 * @param cls string to print
810 * @param success GNUNET_OK if the PUT was transmitted,
811 * GNUNET_NO on timeout,
812 * GNUNET_SYSERR on disconnect from service
813 * after the PUT message was transmitted
814 * (so we don't know if it was received or not)
819 mesh_debug (void *cls, int success)
823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
827 unsigned int debug_fwd_ack;
828 unsigned int debug_bck_ack;
832 /******************************************************************************/
833 /*********************** GLOBAL VARIABLES ****************************/
834 /******************************************************************************/
836 /************************** Configuration parameters **************************/
839 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
841 static struct GNUNET_TIME_Relative refresh_path_time;
844 * How often to PUT local application numbers in the DHT.
846 static struct GNUNET_TIME_Relative app_announce_time;
849 * How often to PUT own ID in the DHT.
851 static struct GNUNET_TIME_Relative id_announce_time;
854 * Maximum time allowed to connect to a peer found by string.
856 static struct GNUNET_TIME_Relative connect_timeout;
859 * Default TTL for payload packets.
861 static unsigned long long default_ttl;
864 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
866 static unsigned long long dht_replication_level;
869 * How many tunnels are we willing to maintain.
870 * Local tunnels are always allowed, even if there are more tunnels than max.
872 static unsigned long long max_tunnels;
875 * How many messages *in total* are we willing to queue, divided by number of
876 * tunnels to get tunnel queue size.
878 static unsigned long long max_msgs_queue;
881 /*************************** Static global variables **************************/
884 * Hostkey generation context
886 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
889 * DLL with all the clients, head.
891 static struct MeshClient *clients;
894 * DLL with all the clients, tail.
896 static struct MeshClient *clients_tail;
899 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
901 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
904 * Number of tunnels known.
906 static unsigned long long n_tunnels;
909 * Tunnels incoming, indexed by MESH_TunnelNumber
910 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
912 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
915 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
917 static struct GNUNET_CONTAINER_MultiHashMap *peers;
920 * Handle to communicate with transport
922 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
925 * Handle to communicate with core.
927 static struct GNUNET_CORE_Handle *core_handle;
932 static struct GNUNET_DHT_Handle *dht_handle;
937 static struct GNUNET_SERVER_Handle *server_handle;
940 * Handle to the statistics service.
942 static struct GNUNET_STATISTICS_Handle *stats;
945 * Notification context, to send messages to local clients.
947 static struct GNUNET_SERVER_NotificationContext *nc;
950 * Local peer own ID (memory efficient handle).
952 static GNUNET_PEER_Id myid;
955 * Local peer own ID (full value).
957 static struct GNUNET_PeerIdentity my_full_id;
962 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
967 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
970 * Tunnel ID for the next created tunnel (global tunnel number).
972 static MESH_TunnelNumber next_tid;
975 * Tunnel ID for the next incoming tunnel (local tunnel number).
977 static MESH_TunnelNumber next_local_tid;
980 * All application types provided by this peer.
982 static struct GNUNET_CONTAINER_MultiHashMap *applications;
985 * All message types clients of this peer are interested in.
987 static struct GNUNET_CONTAINER_MultiHashMap *types;
990 * Task to periodically announce provided applications.
992 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
995 * Task to periodically announce itself in the network.
997 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1000 * Next ID to assign to a client.
1002 unsigned int next_client_id;
1005 /******************************************************************************/
1006 /*********************** DECLARATIONS **************************/
1007 /******************************************************************************/
1010 * Function to process paths received for a new peer addition. The recorded
1011 * paths form the initial tunnel, which can be optimized later.
1012 * Called on each result obtained for the DHT search.
1014 * @param cls closure
1015 * @param exp when will this value expire
1016 * @param key key of the result
1017 * @param type type of the result
1018 * @param size number of bytes in data
1019 * @param data pointer to the result data
1022 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1023 const struct GNUNET_HashCode * key,
1024 const struct GNUNET_PeerIdentity *get_path,
1025 unsigned int get_path_length,
1026 const struct GNUNET_PeerIdentity *put_path,
1027 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1028 size_t size, const void *data);
1032 * Function to process DHT string to regex matching.
1033 * Called on each result obtained for the DHT search.
1035 * @param cls closure (search context)
1036 * @param exp when will this value expire
1037 * @param key key of the result
1038 * @param get_path path of the get request (not used)
1039 * @param get_path_length lenght of get_path (not used)
1040 * @param put_path path of the put request (not used)
1041 * @param put_path_length length of the put_path (not used)
1042 * @param type type of the result
1043 * @param size number of bytes in data
1044 * @param data pointer to the result data
1046 * TODO: re-issue the request after certain time? cancel after X results?
1049 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1050 const struct GNUNET_HashCode * key,
1051 const struct GNUNET_PeerIdentity *get_path,
1052 unsigned int get_path_length,
1053 const struct GNUNET_PeerIdentity *put_path,
1054 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1055 size_t size, const void *data);
1059 * Function to process DHT string to regex matching.
1060 * Called on each result obtained for the DHT search.
1062 * @param cls closure (search context)
1063 * @param exp when will this value expire
1064 * @param key key of the result
1065 * @param get_path path of the get request (not used)
1066 * @param get_path_length lenght of get_path (not used)
1067 * @param put_path path of the put request (not used)
1068 * @param put_path_length length of the put_path (not used)
1069 * @param type type of the result
1070 * @param size number of bytes in data
1071 * @param data pointer to the result data
1074 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1075 const struct GNUNET_HashCode * key,
1076 const struct GNUNET_PeerIdentity *get_path,
1077 unsigned int get_path_length,
1078 const struct GNUNET_PeerIdentity *put_path,
1079 unsigned int put_path_length,
1080 enum GNUNET_BLOCK_Type type,
1081 size_t size, const void *data);
1085 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1086 * and insert it in the appropiate structures if the peer is not known yet.
1088 * @param peer Short identity of the peer.
1090 * @return Existing or newly created peer info.
1092 static struct MeshPeerInfo *
1093 peer_info_get_short (const GNUNET_PEER_Id peer);
1097 * Try to establish a new connection to this peer.
1098 * Use the best path for the given tunnel.
1099 * If the peer doesn't have any path to it yet, try to get one.
1100 * If the peer already has some path, send a CREATE PATH towards it.
1102 * @param peer PeerInfo of the peer.
1103 * @param t Tunnel for which to create the path, if possible.
1106 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1110 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1111 * needed rescources.
1112 * If peer already exists, reevaluate shortest path and change if different.
1114 * @param t Tunnel we want to add a new peer to
1115 * @param peer PeerInfo of the peer being added
1119 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1123 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1124 * that are no longer needed, as well as nodes of no longer reachable peers.
1125 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1127 * @param t Tunnel from which to remove the path.
1128 * @param peer Short id of the peer which should be removed.
1131 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1135 * Search for a tunnel by global ID using full PeerIdentities.
1137 * @param oid owner of the tunnel.
1138 * @param tid global tunnel number.
1140 * @return tunnel handler, NULL if doesn't exist.
1142 static struct MeshTunnel *
1143 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1147 * Delete an active client from the tunnel.
1153 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1156 * Notify a tunnel that a connection has broken that affects at least
1157 * some of its peers.
1159 * @param t Tunnel affected.
1160 * @param p1 Peer that got disconnected from p2.
1161 * @param p2 Peer that got disconnected from p1.
1163 * @return Short ID of the peer disconnected (either p1 or p2).
1164 * 0 if the tunnel remained unaffected.
1166 static GNUNET_PEER_Id
1167 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1172 * Get the current ack value for a tunnel, for data going from root to leaves,
1173 * taking in account the tunnel mode and the status of all children and clients.
1177 * @return Maximum PID allowed.
1180 tunnel_get_fwd_ack (struct MeshTunnel *t);
1184 * Add a client to a tunnel, initializing all needed data structures.
1186 * @param t Tunnel to which add the client.
1187 * @param c Client which to add to the tunnel.
1190 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1194 * Jump to the next edge, with the longest matching token.
1196 * @param block Block found in the DHT.
1197 * @param size Size of the block.
1198 * @param ctx Context of the search.
1200 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1203 regex_next_edge (const struct MeshRegexBlock *block,
1205 struct MeshRegexSearchContext *ctx);
1209 * Find a path to a peer that offers a regex servcie compatible
1210 * with a given string.
1212 * @param key The key of the accepting state.
1213 * @param ctx Context containing info about the string, tunnel, etc.
1216 regex_find_path (const struct GNUNET_HashCode *key,
1217 struct MeshRegexSearchContext *ctx);
1221 * @brief Queue and pass message to core when possible.
1223 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1224 * and accounts for it. In case the queue is full, the message is dropped and
1227 * Otherwise, message is treated as internal and allowed to go regardless of
1230 * @param cls Closure (@c type dependant). It will be used by queue_send to
1231 * build the message to be sent if not already prebuilt.
1232 * @param type Type of the message, 0 for a raw message.
1233 * @param size Size of the message.
1234 * @param dst Neighbor to send message to.
1235 * @param t Tunnel this message belongs to.
1238 queue_add (void *cls, uint16_t type, size_t size,
1239 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1243 * Free a transmission that was already queued with all resources
1244 * associated to the request.
1246 * @param queue Queue handler to cancel.
1247 * @param clear_cls Is it necessary to free associated cls?
1250 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1254 * @brief Get the next transmittable message from the queue.
1256 * This will be the head, except in the case of being a data packet
1257 * not allowed by the destination peer.
1259 * @param peer Destination peer.
1261 * @return The next viable MeshPeerQueue element to send to that peer.
1262 * NULL when there are no transmittable messages.
1264 struct MeshPeerQueue *
1265 queue_get_next (const struct MeshPeerInfo *peer);
1269 * Core callback to write a queued packet to core buffer
1271 * @param cls Closure (peer info).
1272 * @param size Number of bytes available in buf.
1273 * @param buf Where the to write the message.
1275 * @return number of bytes written to buf
1278 queue_send (void *cls, size_t size, void *buf);
1280 /******************************************************************************/
1281 /************************ ITERATORS ****************************/
1282 /******************************************************************************/
1285 * Iterator over found existing mesh regex blocks that match an ongoing search.
1287 * @param cls closure
1288 * @param key current key code
1289 * @param value value in the hash map
1290 * @return GNUNET_YES if we should continue to iterate,
1294 regex_result_iterator (void *cls,
1295 const struct GNUNET_HashCode * key,
1298 struct MeshRegexBlock *block = value;
1299 struct MeshRegexSearchContext *ctx = cls;
1301 if (GNUNET_YES == ntohl(block->accepting) &&
1302 ctx->position == strlen (ctx->info->description))
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1305 regex_find_path (key, ctx);
1306 return GNUNET_YES; // We found an accept state!
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1311 ctx->position, strlen(ctx->info->description),
1312 ntohl(block->accepting));
1315 regex_next_edge(block, SIZE_MAX, ctx);
1317 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1324 * Iterator over edges in a regex block retrieved from the DHT.
1326 * @param cls Closure (context of the search).
1327 * @param token Token that follows to next state.
1328 * @param len Lenght of token.
1329 * @param key Hash of next state.
1331 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1334 regex_edge_iterator (void *cls,
1337 const struct GNUNET_HashCode *key)
1339 struct MeshRegexSearchContext *ctx = cls;
1340 struct MeshRegexSearchInfo *info = ctx->info;
1344 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1349 current = &info->description[ctx->position];
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1351 current_len = strlen (info->description) - ctx->position;
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1356 if (len > current_len)
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1359 return GNUNET_YES; // Token too long, wont match
1361 if (0 != strncmp (current, token, len))
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1364 return GNUNET_YES; // Token doesn't match
1367 if (len > ctx->longest_match)
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1370 ctx->longest_match = len;
1375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1384 * Jump to the next edge, with the longest matching token.
1386 * @param block Block found in the DHT.
1387 * @param size Size of the block.
1388 * @param ctx Context of the search.
1390 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1393 regex_next_edge (const struct MeshRegexBlock *block,
1395 struct MeshRegexSearchContext *ctx)
1397 struct MeshRegexSearchContext *new_ctx;
1398 struct MeshRegexSearchInfo *info = ctx->info;
1399 struct GNUNET_DHT_GetHandle *get_h;
1403 /* Find the longest match for the current string position,
1404 * among tokens in the given block */
1405 ctx->longest_match = 0;
1406 result = GNUNET_MESH_regex_block_iterate (block, size,
1407 ®ex_edge_iterator, ctx);
1408 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1410 /* Did anything match? */
1411 if (0 == ctx->longest_match)
1414 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1415 new_ctx->info = info;
1416 new_ctx->position = ctx->position + ctx->longest_match;
1417 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1419 /* Check whether we already have a DHT GET running for it */
1421 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1424 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1426 ®ex_result_iterator,
1428 return; // We are already looking for it
1431 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1433 /* Start search in DHT */
1435 GNUNET_DHT_get_start (dht_handle, /* handle */
1436 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1437 &ctx->hash, /* key to search */
1438 dht_replication_level, /* replication level */
1439 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1440 NULL, /* xquery */ // FIXME BLOOMFILTER
1441 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1442 &dht_get_string_handler, new_ctx);
1444 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1447 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1456 * Iterator over hash map entries to cancel DHT GET requests after a
1457 * successful connect_by_string.
1459 * @param cls Closure (unused).
1460 * @param key Current key code (unused).
1461 * @param value Value in the hash map (get handle).
1462 * @return GNUNET_YES if we should continue to iterate,
1466 regex_cancel_dht_get (void *cls,
1467 const struct GNUNET_HashCode * key,
1470 struct GNUNET_DHT_GetHandle *h = value;
1472 GNUNET_DHT_get_stop (h);
1478 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1479 * search for connect_by_string.
1481 * @param cls Closure (unused).
1482 * @param key Current key code (unused).
1483 * @param value MeshRegexBlock in the hash map.
1484 * @return GNUNET_YES if we should continue to iterate,
1488 regex_free_result (void *cls,
1489 const struct GNUNET_HashCode * key,
1493 GNUNET_free (value);
1499 * Regex callback iterator to store own service description in the DHT.
1501 * @param cls closure.
1502 * @param key hash for current state.
1503 * @param proof proof for current state.
1504 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1505 * @param num_edges number of edges leaving current state.
1506 * @param edges edges leaving current state.
1509 regex_iterator (void *cls,
1510 const struct GNUNET_HashCode *key,
1513 unsigned int num_edges,
1514 const struct GNUNET_REGEX_Edge *edges)
1516 struct MeshRegexBlock *block;
1517 struct MeshRegexEdge *block_edge;
1518 enum GNUNET_DHT_RouteOption opt;
1522 unsigned int offset;
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 " regex dht put for state %s\n",
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1535 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1536 if (GNUNET_YES == accepting)
1538 struct MeshRegexAccept block;
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 " state %s is accepting, putting own id\n",
1543 size = sizeof (block);
1545 block.id = my_full_id;
1547 GNUNET_DHT_put(dht_handle, key,
1548 dht_replication_level,
1549 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1550 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1553 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1558 len = strlen(proof);
1559 size = sizeof (struct MeshRegexBlock) + len;
1560 block = GNUNET_malloc (size);
1563 block->n_proof = htonl (len);
1564 block->n_edges = htonl (num_edges);
1565 block->accepting = htonl (accepting);
1567 /* Store the proof at the end of the block. */
1568 aux = (char *) &block[1];
1569 memcpy (aux, proof, len);
1572 /* Store each edge in a variable length MeshEdge struct at the
1573 * very end of the MeshRegexBlock structure.
1575 for (i = 0; i < num_edges; i++)
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 " edge %s towards %s\n",
1580 GNUNET_h2s(&edges[i].destination));
1582 /* aux points at the end of the last block */
1583 len = strlen (edges[i].label);
1584 size += sizeof (struct MeshRegexEdge) + len;
1585 // Calculate offset FIXME is this ok? use size instead?
1586 offset = aux - (char *) block;
1587 block = GNUNET_realloc (block, size);
1588 aux = &((char *) block)[offset];
1589 block_edge = (struct MeshRegexEdge *) aux;
1590 block_edge->key = edges[i].destination;
1591 block_edge->n_token = htonl (len);
1592 aux = (char *) &block_edge[1];
1593 memcpy (aux, edges[i].label, len);
1597 GNUNET_DHT_put(dht_handle, key,
1598 dht_replication_level,
1600 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1602 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1606 GNUNET_free (block);
1611 * Store the regular expression describing a local service into the DHT.
1613 * @param regex The regular expresion.
1616 regex_put (const struct MeshRegexDescriptor *regex)
1618 struct GNUNET_REGEX_Automaton *dfa;
1620 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1621 dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1622 strlen(regex->regex),
1623 regex->compression);
1624 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1625 GNUNET_REGEX_automaton_destroy (dfa);
1626 DEBUG_DHT (" regex_put (%s) end\n", regex);
1631 * Find a path to a peer that offers a regex servcie compatible
1632 * with a given string.
1634 * @param key The key of the accepting state.
1635 * @param ctx Context containing info about the string, tunnel, etc.
1638 regex_find_path (const struct GNUNET_HashCode *key,
1639 struct MeshRegexSearchContext *ctx)
1641 struct GNUNET_DHT_GetHandle *get_h;
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1644 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1645 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1646 key, /* key to search */
1647 dht_replication_level, /* replication level */
1648 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1649 GNUNET_DHT_RO_RECORD_ROUTE,
1650 NULL, /* xquery */ // FIXME BLOOMFILTER
1651 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1652 &dht_get_string_accept_handler, ctx);
1653 GNUNET_break (GNUNET_OK ==
1654 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1657 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1662 * Function called if the connect attempt to a peer found via
1663 * connect_by_string times out. Try to connect to another peer, if any.
1664 * Otherwise try to reconnect to the same peer.
1666 * @param cls Closure (info about regex search).
1667 * @param tc TaskContext.
1670 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1672 struct MeshRegexSearchInfo *info = cls;
1673 struct MeshPeerInfo *peer_info;
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1678 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1679 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1687 if (0 < info->n_peers)
1689 // Select next peer, put current in that spot.
1690 id = info->peers[info->i_peer];
1691 info->peers[info->i_peer] = info->peer;
1692 info->i_peer = (info->i_peer + 1) % info->n_peers;
1696 // Try to connect to same peer again.
1699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1701 peer_info = peer_info_get_short(id);
1702 tunnel_add_peer (info->t, peer_info);
1704 tunnel_delete_peer (info->t, old);
1705 peer_info_connect (peer_info, info->t);
1706 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1707 ®ex_connect_timeout,
1709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1714 * Cancel an ongoing regex search in the DHT and free all resources.
1716 * @param ctx The search context.
1719 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1721 struct MeshRegexSearchInfo *info = ctx->info;
1724 GNUNET_free (info->description);
1725 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1726 ®ex_cancel_dht_get, NULL);
1727 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1728 ®ex_free_result, NULL);
1729 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1730 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1731 info->t->regex_ctx = NULL;
1732 for (i = 0; i < info->n_contexts; i++)
1734 GNUNET_free (info->contexts[i]);
1736 if (0 < info->n_contexts)
1737 GNUNET_free (info->contexts);
1738 if (0 < info->n_peers)
1739 GNUNET_free (info->peers);
1740 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1742 GNUNET_SCHEDULER_cancel(info->timeout);
1748 /******************************************************************************/
1749 /************************ PERIODIC FUNCTIONS ****************************/
1750 /******************************************************************************/
1753 * Announce iterator over for each application provided by the peer
1755 * @param cls closure
1756 * @param key current key code
1757 * @param value value in the hash map
1758 * @return GNUNET_YES if we should continue to
1763 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1765 struct PBlock block;
1766 struct MeshClient *c;
1768 block.id = my_full_id;
1769 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1770 GNUNET_assert(NULL != c);
1771 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1772 if (0 == block.type)
1777 block.type = htonl (block.type);
1779 GNUNET_break (NULL !=
1780 GNUNET_DHT_put (dht_handle, key,
1781 dht_replication_level,
1782 GNUNET_DHT_RO_RECORD_ROUTE |
1783 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1784 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1786 (const char *) &block,
1787 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1789 app_announce_time, NULL, NULL));
1795 * Periodically announce what applications are provided by local clients
1798 * @param cls closure
1799 * @param tc task context
1802 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1804 struct MeshClient *c = cls;
1807 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1808 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1813 DEBUG_DHT ("Starting PUT for regex\n");
1815 for (i = 0; i < c->n_regex; i++)
1817 regex_put (&c->regexes[i]);
1819 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1822 DEBUG_DHT ("Finished PUT for regex\n");
1829 * Periodically announce what applications are provided by local clients
1832 * @param cls closure
1833 * @param tc task context
1836 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1838 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1840 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1844 DEBUG_DHT ("Starting PUT for apps\n");
1846 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1848 announce_applications_task =
1849 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1851 DEBUG_DHT ("Finished PUT for apps\n");
1858 * Periodically announce self id in the DHT
1860 * @param cls closure
1861 * @param tc task context
1864 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1866 struct PBlock block;
1868 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1870 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1874 * - Set data expiration in function of X
1875 * - Adapt X to churn
1877 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1879 block.id = my_full_id;
1880 block.type = htonl (0);
1881 GNUNET_DHT_put (dht_handle, /* DHT handle */
1882 &my_full_id.hashPubKey, /* Key to use */
1883 dht_replication_level, /* Replication level */
1884 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1885 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1886 sizeof (block), /* Size of the data */
1887 (const char *) &block, /* Data itself */
1888 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1889 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1890 NULL, /* Continuation */
1891 NULL); /* Continuation closure */
1893 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1897 /******************************************************************************/
1898 /****************** GENERAL HELPER FUNCTIONS ************************/
1899 /******************************************************************************/
1902 * Decrements the reference counter and frees all resources if needed
1904 * @param mesh_data Data Descriptor used in a multicast message.
1905 * Freed no longer needed (last message).
1908 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1910 if (0 == --(mesh_data->reference_counter))
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1913 GNUNET_free (mesh_data->data);
1914 GNUNET_free (mesh_data);
1920 * Check if client has registered with the service and has not disconnected
1922 * @param client the client to check
1924 * @return non-NULL if client exists in the global DLL
1926 static struct MeshClient *
1927 client_get (struct GNUNET_SERVER_Client *client)
1929 struct MeshClient *c;
1934 if (c->handle == client)
1943 * Checks if a given client has subscribed to certain message type
1945 * @param message_type Type of message to check
1946 * @param c Client to check
1948 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1950 * FIXME: use of crypto_hash slows it down
1951 * The hash function alone takes 8-10us out of the ~55us for the whole
1952 * process of retransmitting the message from one local client to another.
1953 * Find faster implementation!
1956 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1958 struct GNUNET_HashCode hc;
1960 if (NULL == c->types)
1963 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1964 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1969 * Check whether client wants traffic from a tunnel.
1971 * @param c Client to check.
1972 * @param t Tunnel to be found.
1974 * @return GNUNET_YES if client knows tunnel.
1976 * TODO look in client hashmap
1979 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1983 for (i = 0; i < t->nclients; i++)
1984 if (t->clients[i] == c)
1991 * Check whether client has been informed about a tunnel.
1993 * @param c Client to check.
1994 * @param t Tunnel to be found.
1996 * @return GNUNET_YES if client knows tunnel.
1998 * TODO look in client hashmap
2001 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2005 for (i = 0; i < t->nignore; i++)
2006 if (t->ignore[i] == c)
2008 return client_wants_tunnel(c, t);
2013 * Marks a client as uninterested in traffic from the tunnel, updating both
2014 * client and tunnel to reflect this.
2016 * @param c Client that doesn't want traffic anymore.
2017 * @param t Tunnel which should be ignored.
2019 * FIXME when to delete an incoming tunnel?
2022 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2024 struct GNUNET_HashCode hash;
2026 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2027 GNUNET_break (GNUNET_YES ==
2028 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2030 GNUNET_break (GNUNET_YES ==
2031 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2032 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2033 tunnel_delete_active_client (t, c);
2034 GNUNET_array_append (t->ignore, t->nignore, c);
2039 * Deletes a tunnel from a client (either owner or destination). To be used on
2040 * tunnel destroy, otherwise, use client_ignore_tunnel.
2042 * @param c Client whose tunnel to delete.
2043 * @param t Tunnel which should be deleted.
2046 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2048 struct GNUNET_HashCode hash;
2052 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2053 GNUNET_assert (GNUNET_YES ==
2054 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2060 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2062 GNUNET_assert (GNUNET_YES ==
2063 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2067 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2075 * Send the message to all clients that have subscribed to its type
2077 * @param msg Pointer to the message itself
2078 * @param payload Pointer to the payload of the message.
2079 * @param t The tunnel to whose clients this message goes.
2081 * @return number of clients this message was sent to
2084 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2085 const struct GNUNET_MessageHeader *payload,
2086 struct MeshTunnel *t)
2088 struct MeshClient *c;
2089 MESH_TunnelNumber *tid;
2092 char cbuf[htons (msg->size)];
2094 type = ntohs (payload->type);
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2097 GNUNET_MESH_DEBUG_M2S (type));
2099 memcpy (cbuf, msg, sizeof (cbuf));
2100 switch (htons (msg->type))
2102 struct GNUNET_MESH_Unicast *uc;
2103 struct GNUNET_MESH_Multicast *mc;
2104 struct GNUNET_MESH_ToOrigin *to;
2106 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2107 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2110 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2111 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2114 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2115 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2123 for (count = 0, c = clients; c != NULL; c = c->next)
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2126 if (client_is_subscribed (type, c))
2128 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2132 *tid = htonl (t->local_tid);
2136 if (GNUNET_NO == client_knows_tunnel (c, t))
2138 /* This client doesn't know the tunnel */
2139 struct GNUNET_MESH_TunnelNotification tmsg;
2140 struct GNUNET_HashCode hash;
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2143 tmsg.header.size = htons (sizeof (tmsg));
2144 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2145 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2146 tmsg.tunnel_id = htonl (t->local_tid_dest);
2148 if (GNUNET_YES == t->speed_min)
2149 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2150 if (GNUNET_YES == t->nobuffer)
2151 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2152 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2153 &tmsg.header, GNUNET_NO);
2154 tunnel_add_client (t, c);
2155 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2157 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2158 c->incoming_tunnels, &hash, t,
2159 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2161 *tid = htonl (t->local_tid_dest);
2164 /* Check if the client wants to get traffic from the tunnel */
2165 if (GNUNET_NO == client_wants_tunnel(c, t))
2168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2169 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2170 (struct GNUNET_MessageHeader
2171 *) cbuf, GNUNET_NO);
2180 * Notify the client that owns the tunnel that a peer has connected to it
2181 * (the requested path to it has been confirmed).
2183 * @param t Tunnel whose owner to notify
2184 * @param id Short id of the peer that has connected
2187 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2189 struct GNUNET_MESH_PeerControl pc;
2191 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2192 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2193 pc.tunnel_id = htonl (t->local_tid);
2194 GNUNET_PEER_resolve (id, &pc.peer);
2195 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2201 * Notify all clients (not depending on registration status) that the incoming
2202 * tunnel is no longer valid.
2204 * @param t Tunnel that was destroyed.
2207 send_clients_tunnel_destroy (struct MeshTunnel *t)
2209 struct GNUNET_MESH_TunnelMessage msg;
2211 msg.header.size = htons (sizeof (msg));
2212 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2213 msg.tunnel_id = htonl (t->local_tid_dest);
2214 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2219 * Notify clients of tunnel disconnections, if needed.
2220 * In case the origin disconnects, the destination clients get a tunnel destroy
2221 * notification. If the last destination disconnects (only one remaining client
2222 * in tunnel), the origin gets a (local ID) peer disconnected.
2223 * Note that the function must be called BEFORE removing the client from
2226 * @param t Tunnel that was destroyed.
2227 * @param c Client that disconnected.
2230 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2236 struct GNUNET_MESH_TunnelMessage msg;
2238 msg.header.size = htons (sizeof (msg));
2239 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2240 msg.tunnel_id = htonl (t->local_tid_dest);
2241 for (i = 0; i < t->nclients; i++)
2242 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2243 &msg.header, GNUNET_NO);
2245 // FIXME when to disconnect an incoming tunnel?
2246 else if (1 == t->nclients && NULL != t->owner)
2248 struct GNUNET_MESH_PeerControl msg;
2250 msg.header.size = htons (sizeof (msg));
2251 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2252 msg.tunnel_id = htonl (t->local_tid);
2253 msg.peer = my_full_id;
2254 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2255 &msg.header, GNUNET_NO);
2261 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2262 * and insert it in the appropiate structures if the peer is not known yet.
2264 * @param peer Full identity of the peer.
2266 * @return Existing or newly created peer info.
2268 static struct MeshPeerInfo *
2269 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2271 struct MeshPeerInfo *peer_info;
2273 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2274 if (NULL == peer_info)
2277 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2278 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2279 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2280 peer_info->id = GNUNET_PEER_intern (peer);
2288 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2289 * and insert it in the appropiate structures if the peer is not known yet.
2291 * @param peer Short identity of the peer.
2293 * @return Existing or newly created peer info.
2295 static struct MeshPeerInfo *
2296 peer_info_get_short (const GNUNET_PEER_Id peer)
2298 struct GNUNET_PeerIdentity id;
2300 GNUNET_PEER_resolve (peer, &id);
2301 return peer_info_get (&id);
2306 * Iterator to remove the tunnel from the list of tunnels a peer participates
2309 * @param cls Closure (tunnel info)
2310 * @param key GNUNET_PeerIdentity of the peer (unused)
2311 * @param value PeerInfo of the peer
2313 * @return always GNUNET_YES, to keep iterating
2316 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2318 struct MeshTunnel *t = cls;
2319 struct MeshPeerInfo *peer = value;
2322 for (i = 0; i < peer->ntunnels; i++)
2325 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2328 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2329 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2338 * Core callback to write a pre-constructed data packet to core buffer
2340 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2341 * @param size Number of bytes available in buf.
2342 * @param buf Where the to write the message.
2344 * @return number of bytes written to buf
2347 send_core_data_raw (void *cls, size_t size, void *buf)
2349 struct MeshTransmissionDescriptor *info = cls;
2350 struct GNUNET_MessageHeader *msg;
2353 GNUNET_assert (NULL != info);
2354 GNUNET_assert (NULL != info->mesh_data);
2355 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2356 total_size = ntohs (msg->size);
2358 if (total_size > size)
2363 memcpy (buf, msg, total_size);
2364 data_descriptor_decrement_rc (info->mesh_data);
2371 * Sends an already built non-multicast message to a peer,
2372 * properly registrating all used resources.
2374 * @param message Message to send. Function makes a copy of it.
2375 * @param peer Short ID of the neighbor whom to send the message.
2376 * @param t Tunnel on which this message is transmitted.
2379 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2380 const struct GNUNET_PeerIdentity *peer,
2381 struct MeshTunnel *t)
2383 struct MeshTransmissionDescriptor *info;
2384 struct MeshPeerInfo *neighbor;
2385 struct MeshPeerPath *p;
2389 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2391 size = ntohs (message->size);
2392 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2393 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2394 info->mesh_data->data = GNUNET_malloc (size);
2395 memcpy (info->mesh_data->data, message, size);
2396 type = ntohs(message->type);
2399 struct GNUNET_MESH_Unicast *m;
2400 struct GNUNET_MESH_ToOrigin *to;
2402 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2403 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2404 m->ttl = htonl (ntohl (m->ttl) - 1);
2406 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2407 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2409 to->pid = htonl(t->bck_pid);
2411 info->mesh_data->data_len = size;
2412 info->mesh_data->reference_counter = 1;
2413 info->mesh_data->total_out = 1;
2414 neighbor = peer_info_get (peer);
2415 for (p = neighbor->path_head; NULL != p; p = p->next)
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 " %s IS NOT DIRECTLY CONNECTED\n",
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2431 for (p = neighbor->path_head; NULL != p; p = p->next)
2433 struct GNUNET_PeerIdentity debug_id;
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2437 " path with %u hops through:\n",
2439 for (i = 0; i < p->length; i++)
2441 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 i, GNUNET_i2s(&debug_id));
2448 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2449 GNUNET_free (info->mesh_data->data);
2450 GNUNET_free (info->mesh_data);
2454 info->peer = neighbor;
2455 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2466 * Sends a CREATE PATH message for a path to a peer, properly registrating
2467 * all used resources.
2469 * @param peer PeerInfo of the final peer for whom this path is being created.
2470 * @param p Path itself.
2471 * @param t Tunnel for which the path is created.
2474 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2475 struct MeshTunnel *t)
2477 struct GNUNET_PeerIdentity id;
2478 struct MeshPathInfo *path_info;
2479 struct MeshPeerInfo *neighbor;
2485 p = tree_get_path_to_peer (t->tree, peer->id);
2492 for (i = 0; i < p->length; i++)
2494 if (p->peers[i] == myid)
2497 if (i >= p->length - 1)
2503 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2505 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2506 path_info->path = p;
2508 neighbor = peer_info_get (&id);
2509 path_info->peer = neighbor;
2510 queue_add (path_info,
2511 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2512 sizeof (struct GNUNET_MESH_ManipulatePath) +
2513 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2520 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2522 * @param t Tunnel whose path to destroy.
2523 * @param destination Short ID of the peer to whom the path to destroy.
2526 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2528 struct MeshPeerPath *p;
2531 p = tree_get_path_to_peer (t->tree, destination);
2537 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2538 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2540 struct GNUNET_MESH_ManipulatePath *msg;
2541 struct GNUNET_PeerIdentity *pi;
2545 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2546 msg->header.size = htons (size);
2547 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2548 msg->tid = htonl (t->id.tid);
2549 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2550 for (i = 0; i < p->length; i++)
2552 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2554 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2561 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2563 * @param t Tunnel which to confirm.
2566 send_path_ack (struct MeshTunnel *t)
2568 struct MeshTransmissionDescriptor *info;
2569 struct GNUNET_PeerIdentity id;
2570 GNUNET_PEER_Id peer;
2572 peer = tree_get_predecessor (t->tree);
2573 GNUNET_PEER_resolve (peer, &id);
2574 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2575 info->origin = &t->id;
2576 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2577 GNUNET_assert (NULL != info->peer);
2580 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2581 sizeof (struct GNUNET_MESH_PathACK),
2588 * Try to establish a new connection to this peer.
2589 * Use the best path for the given tunnel.
2590 * If the peer doesn't have any path to it yet, try to get one.
2591 * If the peer already has some path, send a CREATE PATH towards it.
2593 * @param peer PeerInfo of the peer.
2594 * @param t Tunnel for which to create the path, if possible.
2597 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2599 struct MeshPeerPath *p;
2600 struct MeshPathInfo *path_info;
2602 if (NULL != peer->path_head)
2604 p = tree_get_path_to_peer (t->tree, peer->id);
2611 // FIXME always send create path to self
2614 send_create_path (peer, p, t);
2618 struct GNUNET_HashCode hash;
2621 send_client_peer_connected (t, myid);
2622 t->local_tid_dest = next_local_tid++;
2623 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2626 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2627 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2634 else if (NULL == peer->dhtget)
2636 struct GNUNET_PeerIdentity id;
2638 GNUNET_PEER_resolve (peer->id, &id);
2639 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2640 path_info->peer = peer;
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2643 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2644 peer->dhtgetcls = path_info;
2645 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2646 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2647 &id.hashPubKey, /* key to search */
2648 dht_replication_level, /* replication level */
2649 GNUNET_DHT_RO_RECORD_ROUTE |
2650 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2651 NULL, /* xquery */ // FIXME BLOOMFILTER
2652 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2653 &dht_get_id_handler, path_info);
2655 /* Otherwise, there is no path but the DHT get is already started. */
2660 * Task to delay the connection of a peer
2662 * @param cls Closure (path info with tunnel and peer to connect).
2663 * Will be free'd on exection.
2664 * @param tc TaskContext
2667 peer_info_connect_task (void *cls,
2668 const struct GNUNET_SCHEDULER_TaskContext *tc)
2670 struct MeshPathInfo *path_info = cls;
2672 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2674 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2679 peer_info_connect (path_info->peer, path_info->t);
2685 * Destroy the peer_info and free any allocated resources linked to it
2687 * @param pi The peer_info to destroy.
2689 * @return GNUNET_OK on success
2692 peer_info_destroy (struct MeshPeerInfo *pi)
2694 struct GNUNET_PeerIdentity id;
2695 struct MeshPeerPath *p;
2696 struct MeshPeerPath *nextp;
2698 GNUNET_PEER_resolve (pi->id, &id);
2699 GNUNET_PEER_change_rc (pi->id, -1);
2702 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2705 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2706 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2708 if (NULL != pi->dhtget)
2710 GNUNET_DHT_get_stop (pi->dhtget);
2711 GNUNET_free (pi->dhtgetcls);
2717 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2721 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2723 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2731 * Remove all paths that rely on a direct connection between p1 and p2
2732 * from the peer itself and notify all tunnels about it.
2734 * @param peer PeerInfo of affected peer.
2735 * @param p1 GNUNET_PEER_Id of one peer.
2736 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2739 * TODO: optimize (see below)
2742 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2745 struct MeshPeerPath *p;
2746 struct MeshPeerPath *aux;
2747 struct MeshPeerInfo *peer_d;
2749 unsigned int destroyed;
2754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2756 p = peer->path_head;
2760 for (i = 0; i < (p->length - 1); i++)
2762 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2763 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2765 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2776 for (i = 0; i < peer->ntunnels; i++)
2778 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2782 * Problem: one or more peers have been deleted from the tunnel tree.
2783 * We don't know who they are to try to add them again.
2784 * We need to try to find a new path for each of the disconnected peers.
2785 * Some of them might already have a path to reach them that does not
2786 * involve p1 and p2. Adding all anew might render in a better tree than
2787 * the trivial immediate fix.
2789 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2790 * its children will be reachable trough him.
2792 peer_d = peer_info_get_short (d);
2795 for (p = peer_d->path_head; NULL != p; p = p->next)
2797 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2805 /* No callback, as peer will be already disconnected and a connection
2806 * scheduled by tunnel_notify_connection_broken.
2808 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2812 peer_info_connect (peer_d, peer->tunnels[i]);
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2820 * Add the path to the peer and update the path used to reach it in case this
2823 * @param peer_info Destination peer to add the path to.
2824 * @param path New path to add. Last peer must be the peer in arg 1.
2825 * Path will be either used of freed if already known.
2826 * @param trusted Do we trust that this path is real?
2829 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2832 struct MeshPeerPath *aux;
2836 if ((NULL == peer_info) || (NULL == path))
2839 path_destroy (path);
2842 if (path->peers[path->length - 1] != peer_info->id)
2845 path_destroy (path);
2848 if (path->length <= 2 && GNUNET_NO == trusted)
2850 /* Only allow CORE to tell us about direct paths */
2851 path_destroy (path);
2854 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2855 for (l = 1; l < path->length; l++)
2857 if (path->peers[l] == myid)
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2860 for (l2 = 0; l2 < path->length - l; l2++)
2862 path->peers[l2] = path->peers[l + l2];
2867 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2872 struct GNUNET_PeerIdentity id;
2874 GNUNET_PEER_resolve (peer_info->id, &id);
2875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2876 path->length, GNUNET_i2s (&id));
2879 l = path_get_length (path);
2886 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2887 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2889 l2 = path_get_length (aux);
2892 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2893 peer_info->path_tail, aux, path);
2898 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2900 path_destroy (path);
2905 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2912 * Add the path to the origin peer and update the path used to reach it in case
2913 * this is the shortest.
2914 * The path is given in peer_info -> destination, therefore we turn the path
2915 * upside down first.
2917 * @param peer_info Peer to add the path to, being the origin of the path.
2918 * @param path New path to add after being inversed.
2919 * @param trusted Do we trust that this path is real?
2922 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2923 struct MeshPeerPath *path, int trusted)
2926 peer_info_add_path (peer_info, path, trusted);
2931 * Function called if the connection to the peer has been stalled for a while,
2932 * possibly due to a missed ACK. Poll the peer about its ACK status.
2934 * @param cls Closure (MeshPeerInfo of stalled peer).
2935 * @param tc TaskContext.
2938 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2940 struct MeshTunnelFlowControlInfo *fcinfo = cls;
2941 struct GNUNET_MESH_Poll msg;
2942 struct GNUNET_PeerIdentity id;
2943 struct MeshTunnel *t;
2945 // FIXME fc include code to poll clients
2946 if (NULL == fcinfo->peer)
2948 fcinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2949 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2955 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2956 msg.header.size = htons (sizeof (msg));
2957 msg.tid = htonl (t->id.tid);
2958 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2959 msg.last_ack = htonl (fcinfo->fwd_ack);
2961 GNUNET_PEER_resolve (fcinfo->peer->id, &id);
2962 send_prebuilt_message (&msg.header, &id, fcinfo->t);
2963 fcinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2964 &tunnel_poll, fcinfo);
2970 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2971 * to obtain a local peer -> destination path and interning the peer ids.
2973 * @return Newly allocated and created path
2975 static struct MeshPeerPath *
2976 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2977 unsigned int get_path_length,
2978 const struct GNUNET_PeerIdentity *put_path,
2979 unsigned int put_path_length)
2981 struct MeshPeerPath *p;
2987 GNUNET_PEER_change_rc (myid, 1);
2988 i = get_path_length;
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2990 for (i--; i >= 0; i--)
2992 id = GNUNET_PEER_intern (&get_path[i]);
2993 if (p->length > 0 && id == p->peers[p->length - 1])
2995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2996 GNUNET_PEER_change_rc (id, -1);
3000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3001 GNUNET_i2s (&get_path[i]));
3003 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3004 p->peers[p->length - 1] = id;
3007 i = put_path_length;
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3009 for (i--; i >= 0; i--)
3011 id = GNUNET_PEER_intern (&put_path[i]);
3014 /* PUT path went through us, so discard the path up until now and start
3015 * from here to get a much shorter (and loop-free) path.
3020 if (p->length > 0 && id == p->peers[p->length - 1])
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3023 GNUNET_PEER_change_rc (id, -1);
3027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3028 GNUNET_i2s (&put_path[i]));
3030 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3031 p->peers[p->length - 1] = id;
3035 if (get_path_length > 0)
3036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3037 GNUNET_i2s (&get_path[0]));
3038 if (put_path_length > 0)
3039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3040 GNUNET_i2s (&put_path[0]));
3041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3043 for (i = 0; i < p->length; i++)
3045 struct GNUNET_PeerIdentity peer_id;
3047 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3049 GNUNET_i2s (&peer_id));
3057 * Adds a path to the peer_infos of all the peers in the path
3059 * @param p Path to process.
3060 * @param confirmed Whether we know if the path works or not.
3063 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3067 /* TODO: invert and add */
3068 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3069 for (i++; i < p->length; i++)
3071 struct MeshPeerInfo *aux;
3072 struct MeshPeerPath *copy;
3074 aux = peer_info_get_short (p->peers[i]);
3075 copy = path_duplicate (p);
3076 copy->length = i + 1;
3077 peer_info_add_path (aux, copy, GNUNET_NO);
3083 * Send keepalive packets for a peer
3085 * @param cls Closure (tunnel for which to send the keepalive).
3086 * @param tc Notification context.
3088 * TODO: implement explicit multicast keepalive?
3091 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3095 * Search for a tunnel among the incoming tunnels
3097 * @param tid the local id of the tunnel
3099 * @return tunnel handler, NULL if doesn't exist
3101 static struct MeshTunnel *
3102 tunnel_get_incoming (MESH_TunnelNumber tid)
3104 struct GNUNET_HashCode hash;
3106 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3107 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3108 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3113 * Search for a tunnel among the tunnels for a client
3115 * @param c the client whose tunnels to search in
3116 * @param tid the local id of the tunnel
3118 * @return tunnel handler, NULL if doesn't exist
3120 static struct MeshTunnel *
3121 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3123 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3125 return tunnel_get_incoming (tid);
3129 struct GNUNET_HashCode hash;
3131 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3132 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3138 * Search for a tunnel by global ID using PEER_ID
3140 * @param pi owner of the tunnel
3141 * @param tid global tunnel number
3143 * @return tunnel handler, NULL if doesn't exist
3145 static struct MeshTunnel *
3146 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3148 struct MESH_TunnelID id;
3149 struct GNUNET_HashCode hash;
3154 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3155 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3160 * Search for a tunnel by global ID using full PeerIdentities
3162 * @param oid owner of the tunnel
3163 * @param tid global tunnel number
3165 * @return tunnel handler, NULL if doesn't exist
3167 static struct MeshTunnel *
3168 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3170 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3175 * Delete an active client from the tunnel.
3181 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3185 for (i = 0; i < t->nclients; i++)
3187 if (t->clients[i] == c)
3189 t->clients[i] = t->clients[t->nclients - 1];
3190 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3191 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3193 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3201 * Delete an ignored client from the tunnel.
3207 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3211 for (i = 0; i < t->nignore; i++)
3213 if (t->ignore[i] == c)
3215 t->ignore[i] = t->ignore[t->nignore - 1];
3216 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3224 * Delete a client from the tunnel. It should be only done on
3225 * client disconnection, otherwise use client_ignore_tunnel.
3231 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3233 tunnel_delete_ignored_client (t, c);
3234 tunnel_delete_active_client (t, c);
3239 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3241 * Destroys queue elements of all waiting transmissions and frees all memory
3242 * used by the struct and its elements.
3244 * @param cls Closure (tunnel info).
3245 * @param key Hash of GNUNET_PEER_Id (unused).
3246 * @param value MeshTunnelChildInfo of the child.
3248 * @return always GNUNET_YES, to keep iterating
3251 tunnel_destroy_child (void *cls,
3252 const struct GNUNET_HashCode * key,
3255 struct MeshTunnelFlowControlInfo *cinfo = value;
3256 struct MeshTunnel *t = cls;
3257 struct MeshPeerQueue *q;
3261 for (c = 0; c < cinfo->send_buffer_n; c++)
3263 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3264 q = cinfo->send_buffer[i];
3265 cinfo->send_buffer[i] = NULL;
3267 queue_destroy (q, GNUNET_YES);
3270 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3272 GNUNET_free_non_null (cinfo->send_buffer);
3273 GNUNET_free (cinfo);
3279 * Callback used to notify a client owner of a tunnel that a peer has
3280 * disconnected, most likely because of a path change.
3282 * @param cls Closure (tunnel this notification is about).
3283 * @param peer_id Short ID of disconnected peer.
3286 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3288 struct MeshTunnel *t = cls;
3289 struct MeshPeerInfo *peer;
3290 struct MeshPathInfo *path_info;
3292 if (NULL != t->owner && NULL != nc)
3294 struct GNUNET_MESH_PeerControl msg;
3296 msg.header.size = htons (sizeof (msg));
3297 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3298 msg.tunnel_id = htonl (t->local_tid);
3299 GNUNET_PEER_resolve (peer_id, &msg.peer);
3300 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3301 &msg.header, GNUNET_NO);
3303 peer = peer_info_get_short (peer_id);
3304 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3305 path_info->peer = peer;
3307 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3313 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3314 * needed rescources.
3315 * If peer already exists, reevaluate shortest path and change if different.
3317 * @param t Tunnel we want to add a new peer to
3318 * @param peer PeerInfo of the peer being added
3322 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3324 struct GNUNET_PeerIdentity id;
3325 struct MeshPeerPath *best_p;
3326 struct MeshPeerPath *p;
3327 unsigned int best_cost;
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3331 GNUNET_PEER_resolve (peer->id, &id);
3333 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3336 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3337 GNUNET_assert (GNUNET_OK ==
3338 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3340 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3343 if (NULL != (p = peer->path_head))
3346 best_cost = tree_get_path_cost (t->tree, p);
3349 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3356 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3357 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3358 t->path_refresh_task =
3359 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3363 /* Start a DHT get */
3364 peer_info_connect (peer, t);
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3370 * Add a path to a tunnel which we don't own, just to remember the next hop.
3371 * If destination node was already in the tunnel, the first hop information
3372 * will be replaced with the new path.
3374 * @param t Tunnel we want to add a new peer to
3375 * @param p Path to add
3376 * @param own_pos Position of local node in path.
3380 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3381 unsigned int own_pos)
3383 struct GNUNET_PeerIdentity id;
3385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3386 GNUNET_assert (0 != own_pos);
3387 tree_add_path (t->tree, p, NULL, NULL);
3388 if (own_pos < p->length - 1)
3390 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3391 tree_update_first_hops (t->tree, myid, &id);
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3397 * Add a client to a tunnel, initializing all needed data structures.
3399 * @param t Tunnel to which add the client.
3400 * @param c Client which to add to the tunnel.
3403 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3405 struct MeshTunnelFlowControlInfo fcinfo;
3407 GNUNET_array_append (t->clients, t->nclients, c);
3409 fcinfo.fwd_ack = t->fwd_pid + 1;
3410 fcinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3411 fcinfo.fwd_pid = t->fwd_pid;
3412 fcinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3413 fcinfo.fc_poll = GNUNET_SCHEDULER_NO_TASK;
3414 // FIXME fc buffering is done by context_notify. Confirm this is OK.
3417 GNUNET_array_append (t->clients_fc, t->nclients, fcinfo);
3422 * Notifies a tunnel that a connection has broken that affects at least
3423 * some of its peers. Sends a notification towards the root of the tree.
3424 * In case the peer is the owner of the tree, notifies the client that owns
3425 * the tunnel and tries to reconnect.
3427 * @param t Tunnel affected.
3428 * @param p1 Peer that got disconnected from p2.
3429 * @param p2 Peer that got disconnected from p1.
3431 * @return Short ID of the peer disconnected (either p1 or p2).
3432 * 0 if the tunnel remained unaffected.
3434 static GNUNET_PEER_Id
3435 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3441 tree_notify_connection_broken (t->tree, p1, p2,
3442 &tunnel_notify_client_peer_disconnected,
3444 if (myid != p1 && myid != p2)
3450 if (tree_get_predecessor (t->tree) != 0)
3452 /* We are the peer still connected, notify owner of the disconnection. */
3453 struct GNUNET_MESH_PathBroken msg;
3454 struct GNUNET_PeerIdentity neighbor;
3456 msg.header.size = htons (sizeof (msg));
3457 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3458 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3459 msg.tid = htonl (t->id.tid);
3460 msg.peer1 = my_full_id;
3461 GNUNET_PEER_resolve (pid, &msg.peer2);
3462 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3463 send_prebuilt_message (&msg.header, &neighbor, t);
3471 * Send a multicast packet to a neighbor.
3473 * @param cls Closure (Info about the multicast packet)
3474 * @param neighbor_id Short ID of the neighbor to send the packet to.
3477 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3479 struct MeshData *mdata = cls;
3480 struct MeshTransmissionDescriptor *info;
3481 struct GNUNET_PeerIdentity neighbor;
3482 struct GNUNET_MessageHeader *msg;
3484 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3486 info->mesh_data = mdata;
3487 (mdata->reference_counter) ++;
3488 info->destination = neighbor_id;
3489 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3491 GNUNET_i2s (&neighbor));
3492 info->peer = peer_info_get (&neighbor);
3493 GNUNET_assert (NULL != info->peer);
3494 msg = (struct GNUNET_MessageHeader *) mdata->data;
3497 info->mesh_data->data_len,
3504 * Queue a message in a tunnel in multicast, sending a copy to each child node
3505 * down the local one in the tunnel tree.
3507 * @param t Tunnel in which to send the data.
3508 * @param msg Message to be sent.
3511 tunnel_send_multicast (struct MeshTunnel *t,
3512 const struct GNUNET_MessageHeader *msg)
3514 struct MeshData *mdata;
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3517 " sending a multicast packet...\n");
3519 mdata = GNUNET_malloc (sizeof (struct MeshData));
3520 mdata->data_len = ntohs (msg->size);
3522 mdata->data = GNUNET_malloc (mdata->data_len);
3523 memcpy (mdata->data, msg, mdata->data_len);
3524 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3526 struct GNUNET_MESH_Multicast *mcast;
3528 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3529 if (t->fwd_queue_n >= t->fwd_queue_max)
3532 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3534 " message from %s!\n",
3535 GNUNET_i2s(&mcast->oid));
3536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3537 " message at %s!\n",
3538 GNUNET_i2s(&my_full_id));
3539 GNUNET_free (mdata->data);
3540 GNUNET_free (mdata);
3544 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3546 ntohl (mcast->ttl));
3550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3553 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3554 if (mdata->reference_counter == 0)
3556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3557 " no one to send data to\n");
3558 GNUNET_free (mdata->data);
3559 GNUNET_free (mdata);
3564 mdata->total_out = mdata->reference_counter;
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 " sending a multicast packet done\n");
3573 * Increase the SKIP value of all peers that
3574 * have not received a unicast message.
3576 * @param cls Closure (ID of the peer that HAS received the message).
3577 * @param key ID of the neighbor.
3578 * @param value FLow control information about the neighbor.
3580 * @return GNUNET_YES to keep iterating.
3583 tunnel_add_skip (void *cls,
3584 const struct GNUNET_HashCode * key,
3587 struct GNUNET_PeerIdentity *neighbor = cls;
3588 struct MeshTunnelFlowControlInfo *fcinfo = value;
3590 /* TODO compare only pointers? key == neighbor? */
3591 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3601 * @brief Get neighbor's Flow Control information.
3603 * Retrieves the MeshTunnelFlowControlInfo containing Flow Control data about
3604 * a direct descendant of the local node in a certain tunnel.
3605 * If the info is not yet there (recently created path), creates the data struct
3606 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3609 * @param t Tunnel related.
3610 * @param peer Neighbor whose Flow Control info is needed.
3612 * @return Neighbor's Flow Control info.
3614 static struct MeshTunnelFlowControlInfo *
3615 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3616 const struct GNUNET_PeerIdentity *peer)
3618 struct MeshTunnelFlowControlInfo *fcinfo;
3620 if (NULL == t->children_fc)
3623 fcinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3629 fcinfo = GNUNET_malloc (sizeof (struct MeshTunnelFlowControlInfo));
3630 fcinfo->peer = peer_info_get(peer);
3631 fcinfo->skip = t->fwd_pid;
3634 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3635 fcinfo->fwd_ack = t->fwd_pid + delta;
3636 fcinfo->bck_ack = delta;
3637 fcinfo->bck_pid = -1;
3639 fcinfo->send_buffer =
3640 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3642 GNUNET_assert (GNUNET_OK ==
3643 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3646 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3653 * Get the Flow Control info of a client.
3655 * @param t Tunnel on which to look.
3656 * @param c Client whose ACK to get.
3658 * @return ACK value.
3660 static struct MeshTunnelFlowControlInfo *
3661 tunnel_get_client_fc (struct MeshTunnel *t,
3662 struct MeshClient *c)
3666 for (i = 0; i < t->nclients; i++)
3668 if (t->clients[i] != c)
3670 return &t->clients_fc[i];
3673 return NULL; // won't get here. Just to avoid compiler / coverity complaints.
3678 * Iterator to get the appropiate ACK value from all children nodes.
3680 * @param cls Closue (tunnel).
3681 * @param id Id of the child node.
3684 tunnel_get_child_fwd_ack (void *cls,
3687 struct GNUNET_PeerIdentity peer_id;
3688 struct MeshTunnelFlowControlInfo *fcinfo;
3689 struct MeshTunnelChildIteratorContext *ctx = cls;
3690 struct MeshTunnel *t = ctx->t;
3693 GNUNET_PEER_resolve (id, &peer_id);
3694 fcinfo = tunnel_get_neighbor_fc (t, &peer_id);
3695 ack = fcinfo->fwd_ack;
3698 if (GNUNET_NO == ctx->init)
3700 ctx->max_child_ack = ack;
3701 ctx->init = GNUNET_YES;
3704 if (GNUNET_YES == t->speed_min)
3706 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3710 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3716 * Get the maximum PID allowed to transmit to any
3717 * tunnel child of the local peer, depending on the tunnel
3718 * buffering/speed settings.
3722 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3725 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3727 struct MeshTunnelChildIteratorContext ctx;
3729 ctx.max_child_ack = 0;
3731 ctx.init = GNUNET_NO;
3732 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3734 if (0 == ctx.nchildren)
3736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3737 " tunnel has no children, no FWD ACK\n");
3741 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3742 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3744 return (int64_t) ctx.max_child_ack;
3749 * Set the FWD ACK value of a client in a particular tunnel.
3751 * @param t Tunnel affected.
3752 * @param c Client whose ACK to set.
3753 * @param ack ACK value.
3756 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3757 struct MeshClient *c,
3762 for (i = 0; i < t->nclients; i++)
3764 if (t->clients[i] != c)
3766 t->clients_fc[i].fwd_ack = ack;
3774 * Get the highest ACK value of all clients in a particular tunnel,
3775 * according to the buffering/speed settings.
3777 * @param t Tunnel on which to look.
3779 * @return Corresponding ACK value (max uint32_t).
3780 * If no clients are suscribed, -1LL.
3783 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3788 if (0 == t->nclients)
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3791 " tunnel has no clients, no FWD ACK\n");
3795 for (ack = -1LL, i = 0; i < t->nclients; i++)
3798 (GNUNET_YES == t->speed_min &&
3799 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3800 (GNUNET_NO == t->speed_min &&
3801 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3803 ack = t->clients_fc[i].fwd_ack;
3807 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3808 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3810 return (uint32_t) ack;
3815 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3816 * mode and the status of all children nodes.
3820 * @return Maximum PID allowed.
3823 tunnel_get_fwd_ack (struct MeshTunnel *t)
3827 uint32_t buffer_free;
3831 count = t->fwd_pid - t->skip;
3832 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3833 child_ack = tunnel_get_children_fwd_ack (t);
3834 client_ack = tunnel_get_clients_fwd_ack (t);
3835 if (GNUNET_YES == t->nobuffer)
3838 if (-1LL == child_ack)
3839 child_ack = client_ack;
3840 if (-1LL == child_ack)
3843 client_ack = child_ack = ack;
3848 ack = count + buffer_free; // Overflow? OK!
3850 if (-1LL == child_ack)
3852 // Node has no children, child_ack AND core buffer are irrelevant.
3853 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3854 return (uint32_t) client_ack;
3856 if (-1LL == client_ack)
3860 if (GNUNET_YES == t->speed_min)
3862 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3863 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3867 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3868 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3871 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3872 count, buffer_free, child_ack, client_ack, ack);
3878 * Build a local ACK message and send it to a local client.
3880 * @param t Tunnel on which to send the ACK.
3881 * @param c Client to whom send the ACK.
3882 * @param ack Value of the ACK.
3885 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3887 struct GNUNET_MESH_LocalAck msg;
3889 msg.header.size = htons (sizeof (msg));
3890 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3891 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3892 msg.max_pid = htonl (ack);
3893 GNUNET_SERVER_notification_context_unicast(nc,
3900 * Build an ACK message and queue it to send to the given peer.
3902 * @param t Tunnel on which to send the ACK.
3903 * @param peer Peer to whom send the ACK.
3904 * @param ack Value of the ACK.
3907 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3909 struct GNUNET_MESH_ACK msg;
3911 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3912 msg.header.size = htons (sizeof (msg));
3913 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3914 msg.pid = htonl (ack);
3915 msg.tid = htonl (t->id.tid);
3917 send_prebuilt_message (&msg.header, peer, t);
3922 * Notify a the owner of a tunnel about how many more
3923 * payload packages will we accept on a given tunnel.
3925 * @param t Tunnel on which to send the ACK.
3928 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3933 "Sending client FWD ACK on tunnel %X\n",
3936 ack = tunnel_get_fwd_ack (t);
3938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3939 if (t->last_fwd_ack == ack)
3941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3946 t->last_fwd_ack = ack;
3947 send_local_ack (t, t->owner, ack);
3952 * Send an ACK informing the predecessor about the available buffer space.
3953 * In case there is no predecessor, inform the owning client.
3954 * If buffering is off, send only on behalf of children or self if endpoint.
3955 * If buffering is on, send when sent to children and buffer space is free.
3956 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3957 * the ACK itself goes "back" (towards root).
3959 * @param t Tunnel on which to send the ACK.
3960 * @param type Type of message that triggered the ACK transmission.
3963 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3965 struct GNUNET_PeerIdentity id;
3968 if (NULL != t->owner)
3970 tunnel_send_client_fwd_ack (t);
3973 /* Is it after unicast / multicast retransmission? */
3976 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3977 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3979 "ACK due to FWD DATA retransmission\n");
3980 if (GNUNET_YES == t->nobuffer)
3982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3986 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3987 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3993 /* Check if we need no retransmit the ACK */
3994 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3995 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3999 " t->qmax: %u, t->qn: %u\n",
4000 t->fwd_queue_max, t->fwd_queue_n);
4001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4002 " t->pid: %u, t->ack: %u\n",
4003 t->fwd_pid, t->last_fwd_ack);
4007 /* Ok, ACK might be necessary, what PID to ACK? */
4008 ack = tunnel_get_fwd_ack (t);
4010 /* If speed_min and not all children have ack'd, dont send yet */
4011 if (ack == t->last_fwd_ack)
4013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4017 t->last_fwd_ack = ack;
4018 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4019 send_ack (t, &id, ack);
4025 * Iterator to send a child node a BCK ACK to allow him to send more
4028 * @param cls Closure (tunnel).
4029 * @param id Id of the child node.
4032 tunnel_send_child_bck_ack (void *cls,
4035 struct MeshTunnel *t = cls;
4036 struct MeshTunnelFlowControlInfo *fcinfo;
4037 struct GNUNET_PeerIdentity peer;
4040 GNUNET_PEER_resolve (id, &peer);
4041 fcinfo = tunnel_get_neighbor_fc (t, &peer);
4042 ack = fcinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4045 if (fcinfo->bck_ack == ack)
4047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4048 " Not sending ACK, not needed\n");
4051 fcinfo->bck_ack = ack;
4053 fcinfo->bck_ack = fcinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4055 " Sending BCK ACK %u (last sent: %u)\n",
4056 ack, fcinfo->bck_ack);
4057 send_ack (t, &peer, ack);
4062 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4064 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4066 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4067 * - implement a size 1 buffer in each client_fc AND children_fc
4068 * to hold at least 1 message per "child".
4069 * problem: violates no buffer policy
4070 * - ack 0 and make "children" poll for transmission slots
4071 * problem: big overhead, extra latency even in low traffic
4074 * @param t Tunnel on which to send the BCK ACKs.
4077 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4080 unsigned int tunnel_delta;
4082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4084 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4087 /* Find client whom to allow to send to origin (with lowest buffer space) */
4088 for (i = 0; i < t->nclients; i++)
4090 struct MeshTunnelFlowControlInfo *fcinfo;
4093 fcinfo = &t->clients_fc[i];
4094 delta = fcinfo->bck_ack - fcinfo->bck_pid;
4095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4096 t->clients[i]->id, delta);
4098 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4099 (GNUNET_YES == t->nobuffer && 0 == delta))
4103 ack = fcinfo->bck_pid;
4104 ack += t->nobuffer ? 1 : tunnel_delta;
4105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4106 " sending ack to client %u: %u\n",
4107 t->clients[i]->id, ack);
4108 send_local_ack (t, t->clients[i], ack);
4109 fcinfo->bck_ack = ack;
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4114 " not sending ack to client %u (td %u, d %u)\n",
4115 t->clients[i]->id, tunnel_delta, delta);
4122 * Send an ACK informing the children nodes and destination clients about
4123 * the available buffer space.
4124 * If buffering is off, send only on behalf of root (can be self).
4125 * If buffering is on, send when sent to predecessor and buffer space is free.
4126 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4127 * the ACK itself goes "forward" (towards children/clients).
4129 * @param t Tunnel on which to send the ACK.
4130 * @param type Type of message that triggered the ACK transmission.
4133 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4136 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4137 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4138 /* Is it after data to_origin retransmission? */
4141 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4142 if (GNUNET_YES == t->nobuffer)
4144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4145 " Not sending ACK, nobuffer\n");
4149 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4150 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4151 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4157 tunnel_send_clients_bck_ack (t);
4158 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4163 * @brief Re-initiate traffic to this peer if necessary.
4165 * Check if there is traffic queued towards this peer
4166 * and the core transmit handle is NULL (traffic was stalled).
4167 * If so, call core tmt rdy.
4169 * @param cls Closure (unused)
4170 * @param peer_id Short ID of peer to which initiate traffic.
4173 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4175 struct MeshPeerInfo *peer;
4176 struct GNUNET_PeerIdentity id;
4177 struct MeshPeerQueue *q;
4180 peer = peer_info_get_short(peer_id);
4181 if (NULL != peer->core_transmit)
4184 q = queue_get_next(peer);
4187 /* Might br multicast traffic already sent to this particular peer but
4188 * not to other children in this tunnel.
4189 * This way t->queue_n would be > 0 but the queue of this particular peer
4195 GNUNET_PEER_resolve (peer->id, &id);
4196 peer->core_transmit =
4197 GNUNET_CORE_notify_transmit_ready(core_handle,
4200 GNUNET_TIME_UNIT_FOREVER_REL,
4210 * @brief Allow transmission of FWD traffic on this tunnel
4212 * Check if there is traffic queued towards any children
4213 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4215 * @param t Tunnel on which to unlock FWD traffic.
4218 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4220 if (0 == t->fwd_queue_n)
4223 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4228 * @brief Allow transmission of BCK traffic on this tunnel
4230 * Check if there is traffic queued towards the root of the tree
4231 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4233 * @param t Tunnel on which to unlock BCK traffic.
4236 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4238 if (0 == t->bck_queue_n)
4241 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4246 * Send a message to all peers in this tunnel that the tunnel is no longer
4249 * @param t The tunnel whose peers to notify.
4252 tunnel_send_destroy (struct MeshTunnel *t)
4254 struct GNUNET_MESH_TunnelDestroy msg;
4256 msg.header.size = htons (sizeof (msg));
4257 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4258 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4259 msg.tid = htonl (t->id.tid);
4260 tunnel_send_multicast (t, &msg.header);
4265 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4267 * @param cls Closure (Tunnel which to cancel).
4268 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4271 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4273 struct MeshTunnel *t = cls;
4274 struct MeshPeerInfo *peer_info;
4275 struct MeshPeerQueue *pq;
4276 struct MeshPeerQueue *next;
4278 peer_info = peer_info_get_short (neighbor_id);
4279 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4282 if (pq->tunnel == t)
4284 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4285 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4286 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4288 // Should have been removed on destroy children
4291 queue_destroy (pq, GNUNET_YES);
4294 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4296 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4297 peer_info->core_transmit = NULL;
4302 * Destroy the tunnel and free any allocated resources linked to it.
4304 * @param t the tunnel to destroy
4306 * @return GNUNET_OK on success
4309 tunnel_destroy (struct MeshTunnel *t)
4311 struct MeshClient *c;
4312 struct GNUNET_HashCode hash;
4323 struct GNUNET_PeerIdentity id;
4325 GNUNET_PEER_resolve (t->id.oid, &id);
4326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4327 GNUNET_i2s (&id), t->id.tid);
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4333 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4334 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4342 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4344 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4351 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4352 for (i = 0; i < t->nclients; i++)
4356 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4362 for (i = 0; i < t->nignore; i++)
4366 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4373 if (t->nclients > 0)
4376 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4381 GNUNET_free (t->clients);
4382 GNUNET_free (t->clients_fc);
4385 if (NULL != t->peers)
4387 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4389 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4392 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4393 &tunnel_destroy_child,
4395 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4396 t->children_fc = NULL;
4398 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4399 tree_destroy (t->tree);
4401 if (NULL != t->regex_ctx)
4402 regex_cancel_search (t->regex_ctx);
4403 if (NULL != t->dht_get_type)
4404 GNUNET_DHT_get_stop (t->dht_get_type);
4405 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4406 GNUNET_SCHEDULER_cancel (t->timeout_task);
4407 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4408 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4411 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4418 * Create a new tunnel
4420 * @param owner Who is the owner of the tunnel (short ID).
4421 * @param tid Tunnel Number of the tunnel.
4422 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4423 * @param local Tunnel Number for the tunnel, for the client point of view.
4425 * @return A new initialized tunnel. NULL on error.
4427 static struct MeshTunnel *
4428 tunnel_new (GNUNET_PEER_Id owner,
4429 MESH_TunnelNumber tid,
4430 struct MeshClient *client,
4431 MESH_TunnelNumber local)
4433 struct MeshTunnel *t;
4434 struct GNUNET_HashCode hash;
4436 if (n_tunnels >= max_tunnels && NULL == client)
4439 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4442 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4443 t->bck_queue_max = t->fwd_queue_max;
4444 t->tree = tree_new (owner);
4446 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4447 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4448 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4449 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4450 t->local_tid = local;
4451 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4453 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4455 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4457 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4458 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4465 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4472 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4474 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4475 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4479 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4489 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4490 * that are no longer needed, as well as nodes of no longer reachable peers.
4491 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4493 * @param t Tunnel from which to remove the path.
4494 * @param peer Short id of the peer which should be removed.
4497 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4499 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4505 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4506 * client when the client disconnects. If the client is not the owner, the
4507 * owner will get notified if no more clients are in the tunnel and the client
4508 * get removed from the tunnel's list.
4510 * @param cls closure (client that is disconnecting)
4511 * @param key the hash of the local tunnel id (used to access the hashmap)
4512 * @param value the value stored at the key (tunnel to destroy)
4514 * @return GNUNET_OK, keep iterating.
4517 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4519 struct MeshTunnel *t = value;
4520 struct MeshClient *c = cls;
4522 send_client_tunnel_disconnect(t, c);
4525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4526 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4527 tunnel_delete_client(t, c);
4528 client_delete_tunnel(c, t);
4531 tunnel_send_destroy(t);
4533 t->destroy = GNUNET_YES;
4540 * Timeout function, destroys tunnel if called
4542 * @param cls Closure (tunnel to destroy).
4543 * @param tc TaskContext
4546 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4548 struct MeshTunnel *t = cls;
4549 struct GNUNET_PeerIdentity id;
4551 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4552 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4554 GNUNET_PEER_resolve(t->id.oid, &id);
4555 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4556 "Tunnel %s [%X] timed out. Destroying.\n",
4557 GNUNET_i2s(&id), t->id.tid);
4558 send_clients_tunnel_destroy (t);
4563 * Resets the tunnel timeout. Starts it if no timeout was running.
4565 * @param t Tunnel whose timeout to reset.
4567 * TODO use heap to improve efficiency of scheduler.
4570 tunnel_reset_timeout (struct MeshTunnel *t)
4572 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4573 GNUNET_SCHEDULER_cancel (t->timeout_task);
4575 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4576 (refresh_path_time, 4), &tunnel_timeout, t);
4580 /******************************************************************************/
4581 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4582 /******************************************************************************/
4585 * Function to send a create path packet to a peer.
4587 * @param cls closure
4588 * @param size number of bytes available in buf
4589 * @param buf where the callee should write the message
4590 * @return number of bytes written to buf
4593 send_core_path_create (void *cls, size_t size, void *buf)
4595 struct MeshPathInfo *info = cls;
4596 struct GNUNET_MESH_ManipulatePath *msg;
4597 struct GNUNET_PeerIdentity *peer_ptr;
4598 struct MeshTunnel *t = info->t;
4599 struct MeshPeerPath *p = info->path;
4604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4606 sizeof (struct GNUNET_MESH_ManipulatePath) +
4607 p->length * sizeof (struct GNUNET_PeerIdentity);
4609 if (size < size_needed || NULL == buf)
4614 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4615 msg->header.size = htons (size_needed);
4616 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4617 msg->tid = ntohl (t->id.tid);
4620 if (GNUNET_YES == t->speed_min)
4621 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4622 if (GNUNET_YES == t->nobuffer)
4623 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4624 msg->opt = htonl(opt);
4627 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4628 for (i = 0; i < p->length; i++)
4630 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4637 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4643 * Fill the core buffer
4645 * @param cls closure (data itself)
4646 * @param size number of bytes available in buf
4647 * @param buf where the callee should write the message
4649 * @return number of bytes written to buf
4652 send_core_data_multicast (void *cls, size_t size, void *buf)
4654 struct MeshTransmissionDescriptor *info = cls;
4657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4658 GNUNET_assert (NULL != info);
4659 GNUNET_assert (NULL != info->peer);
4660 total_size = info->mesh_data->data_len;
4661 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4663 if (total_size > size)
4668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4669 memcpy (buf, info->mesh_data->data, total_size);
4672 struct GNUNET_MESH_Multicast *mc;
4673 struct GNUNET_MessageHeader *mh;
4676 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4678 mc = (struct GNUNET_MESH_Multicast *) mh;
4679 mh = (struct GNUNET_MessageHeader *) &mc[1];
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4681 " multicast, payload type %s\n",
4682 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4684 " multicast, payload size %u\n", ntohs (mh->size));
4688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4689 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4693 data_descriptor_decrement_rc (info->mesh_data);
4694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4702 * Creates a path ack message in buf and frees all unused resources.
4704 * @param cls closure (MeshTransmissionDescriptor)
4705 * @param size number of bytes available in buf
4706 * @param buf where the callee should write the message
4707 * @return number of bytes written to buf
4710 send_core_path_ack (void *cls, size_t size, void *buf)
4712 struct MeshTransmissionDescriptor *info = cls;
4713 struct GNUNET_MESH_PathACK *msg = buf;
4715 GNUNET_assert (NULL != info);
4716 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4721 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4722 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4723 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4724 msg->tid = htonl (info->origin->tid);
4725 msg->peer_id = my_full_id;
4728 /* TODO add signature */
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4731 return sizeof (struct GNUNET_MESH_PathACK);
4736 * Free a transmission that was already queued with all resources
4737 * associated to the request.
4739 * @param queue Queue handler to cancel.
4740 * @param clear_cls Is it necessary to free associated cls?
4743 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4745 struct MeshTransmissionDescriptor *dd;
4746 struct MeshPathInfo *path_info;
4747 struct MeshTunnelFlowControlInfo *fcinfo;
4748 struct GNUNET_PeerIdentity id;
4752 if (GNUNET_YES == clear_cls)
4754 switch (queue->type)
4756 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4757 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4758 GNUNET_assert (GNUNET_YES == queue->tunnel->destroy);
4759 /* FIXME: don't cancel, send and destroy tunnel in queue_send */
4761 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4762 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4763 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4764 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4765 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4767 " prebuilt message\n");
4768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4770 GNUNET_MESH_DEBUG_M2S(queue->type));
4772 data_descriptor_decrement_rc (dd->mesh_data);
4774 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4776 path_info = queue->cls;
4777 path_destroy (path_info->path);
4781 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4782 " type %s unknown!\n",
4783 GNUNET_MESH_DEBUG_M2S(queue->type));
4785 GNUNET_free_non_null (queue->cls);
4787 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4788 queue->peer->queue_tail,
4791 /* Delete from child_fc in the appropiate tunnel */
4792 max = queue->tunnel->fwd_queue_max;
4793 GNUNET_PEER_resolve (queue->peer->id, &id);
4794 fcinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4797 GNUNET_assert (NULL != fcinfo->peer);
4798 for (i = 0; i < fcinfo->send_buffer_n; i++)
4801 i2 = (fcinfo->send_buffer_start + i) % max;
4802 if (fcinfo->send_buffer[i2] == queue)
4804 /* Found corresponding entry in the send_buffer. Move all others back. */
4809 for (j = i, j2 = 0, j3 = 0; j < fcinfo->send_buffer_n - 1; j++)
4811 j2 = (fcinfo->send_buffer_start + j) % max;
4812 j3 = (fcinfo->send_buffer_start + j + 1) % max;
4813 fcinfo->send_buffer[j2] = fcinfo->send_buffer[j3];
4816 fcinfo->send_buffer[j3] = NULL;
4817 fcinfo->send_buffer_n--;
4822 GNUNET_free (queue);
4827 * @brief Get the next transmittable message from the queue.
4829 * This will be the head, except in the case of being a data packet
4830 * not allowed by the destination peer.
4832 * @param peer Destination peer.
4834 * @return The next viable MeshPeerQueue element to send to that peer.
4835 * NULL when there are no transmittable messages.
4837 struct MeshPeerQueue *
4838 queue_get_next (const struct MeshPeerInfo *peer)
4840 struct MeshPeerQueue *q;
4841 struct MeshTunnel *t;
4842 struct MeshTransmissionDescriptor *info;
4843 struct MeshTunnelFlowControlInfo *fcinfo;
4844 struct GNUNET_MESH_Unicast *ucast;
4845 struct GNUNET_MESH_ToOrigin *to_orig;
4846 struct GNUNET_MESH_Multicast *mcast;
4847 struct GNUNET_PeerIdentity id;
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4852 for (q = peer->queue_head; NULL != q; q = q->next)
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4858 GNUNET_MESH_DEBUG_M2S(q->type));
4861 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4862 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4863 pid = ntohl (ucast->pid);
4864 GNUNET_PEER_resolve (info->peer->id, &id);
4865 fcinfo = tunnel_get_neighbor_fc (t, &id);
4866 ack = fcinfo->fwd_ack;
4868 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4869 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4870 pid = ntohl (to_orig->pid);
4873 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4874 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4875 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4877 // Not a multicast payload: multicast control traffic (destroy, etc)
4880 pid = ntohl (mcast->pid);
4881 GNUNET_PEER_resolve (info->peer->id, &id);
4882 fcinfo = tunnel_get_neighbor_fc (t, &id);
4883 ack = fcinfo->fwd_ack;
4886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4891 "********* ACK: %u, PID: %u\n",
4893 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4902 "********* NEXT!\n");
4905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4906 "********* nothing found\n");
4912 * Core callback to write a queued packet to core buffer
4914 * @param cls Closure (peer info).
4915 * @param size Number of bytes available in buf.
4916 * @param buf Where the to write the message.
4918 * @return number of bytes written to buf
4921 queue_send (void *cls, size_t size, void *buf)
4923 struct MeshPeerInfo *peer = cls;
4924 struct GNUNET_MessageHeader *msg;
4925 struct MeshPeerQueue *queue;
4926 struct MeshTunnel *t;
4927 struct MeshTunnelFlowControlInfo *fcinfo;
4928 struct GNUNET_PeerIdentity dst_id;
4931 peer->core_transmit = NULL;
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4935 queue = queue_get_next (peer);
4937 /* Queue has no internal mesh traffic nor sendable payload */
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4941 if (NULL == peer->queue_head)
4942 GNUNET_break (0); // Should've been canceled
4945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4947 GNUNET_PEER_resolve (peer->id, &dst_id);
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4949 "********* towards %s\n",
4950 GNUNET_i2s(&dst_id));
4951 /* Check if buffer size is enough for the message */
4952 if (queue->size > size)
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4955 "********* not enough room, reissue\n");
4956 peer->core_transmit =
4957 GNUNET_CORE_notify_transmit_ready (core_handle,
4960 GNUNET_TIME_UNIT_FOREVER_REL,
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4970 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4974 "********* unicast: t->q (%u/%u)\n",
4975 t->fwd_queue_n, t->fwd_queue_max);
4977 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4984 switch (queue->type)
4987 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4988 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4989 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4990 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4991 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4993 "********* raw: %s\n",
4994 GNUNET_MESH_DEBUG_M2S (queue->type));
4996 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4997 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4998 data_size = send_core_data_raw (queue->cls, size, buf);
4999 msg = (struct GNUNET_MessageHeader *) buf;
5000 switch (ntohs (msg->type)) // Type of preconstructed message
5002 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5003 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5005 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5006 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5012 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5015 struct MeshTransmissionDescriptor *info = queue->cls;
5017 if ((1 == info->mesh_data->reference_counter
5018 && GNUNET_YES == t->speed_min)
5020 (info->mesh_data->total_out == info->mesh_data->reference_counter
5021 && GNUNET_NO == t->speed_min))
5023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5024 "********* considered sent\n");
5028 data_size = send_core_data_multicast(queue->cls, size, buf);
5029 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5031 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5033 data_size = send_core_path_create (queue->cls, size, buf);
5035 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5037 data_size = send_core_path_ack (queue->cls, size, buf);
5039 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5041 data_size = send_core_data_multicast (queue->cls, size, buf);
5045 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5046 "********* type unknown: %u\n",
5050 switch (queue->type)
5052 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5053 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5054 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5055 fcinfo = tunnel_get_neighbor_fc (t, &dst_id);
5056 if (fcinfo->send_buffer[fcinfo->send_buffer_start] != queue)
5059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5060 "at pos %u (%p) != %p\n",
5061 fcinfo->send_buffer_start,
5062 fcinfo->send_buffer[fcinfo->send_buffer_start],
5065 if (fcinfo->send_buffer_n > 0)
5067 fcinfo->send_buffer[fcinfo->send_buffer_start] = NULL;
5068 fcinfo->send_buffer_n--;
5069 fcinfo->send_buffer_start++;
5070 fcinfo->send_buffer_start %= t->fwd_queue_max;
5081 /* Free queue, but cls was freed by send_core_* */
5082 queue_destroy (queue, GNUNET_NO);
5084 if (GNUNET_YES == t->destroy)
5086 // FIXME fc tunnel destroy all pending traffic? wait for it?
5087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5091 /* If more data in queue, send next */
5092 queue = queue_get_next(peer);
5095 struct GNUNET_PeerIdentity id;
5097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5098 GNUNET_PEER_resolve (peer->id, &id);
5099 peer->core_transmit =
5100 GNUNET_CORE_notify_transmit_ready(core_handle,
5103 GNUNET_TIME_UNIT_FOREVER_REL,
5111 if (NULL != peer->queue_head)
5113 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5114 "********* %s stalled\n",
5115 GNUNET_i2s(&my_full_id));
5117 fcinfo = tunnel_get_neighbor_fc (t, &dst_id);
5118 fcinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5119 &tunnel_poll, fcinfo);
5122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5128 * @brief Queue and pass message to core when possible.
5130 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5131 * and accounts for it. In case the queue is full, the message is dropped and
5134 * Otherwise, message is treated as internal and allowed to go regardless of
5137 * @param cls Closure (@c type dependant). It will be used by queue_send to
5138 * build the message to be sent if not already prebuilt.
5139 * @param type Type of the message, 0 for a raw message.
5140 * @param size Size of the message.
5141 * @param dst Neighbor to send message to.
5142 * @param t Tunnel this message belongs to.
5145 queue_add (void *cls, uint16_t type, size_t size,
5146 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5148 struct MeshPeerQueue *queue;
5149 struct MeshTunnelFlowControlInfo *fcinfo;
5150 struct GNUNET_PeerIdentity id;
5156 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5157 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5159 n = &t->fwd_queue_n;
5160 max = &t->fwd_queue_max;
5162 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5164 n = &t->bck_queue_n;
5165 max = &t->bck_queue_max;
5171 struct MeshTransmissionDescriptor *td = cls;
5172 struct GNUNET_MESH_ToOrigin *to;
5174 to = td->mesh_data->data;
5176 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5177 "bck pid %u, bck ack %u, msg pid %u\n",
5178 t->bck_pid, t->bck_ack, ntohl(to->pid));
5179 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5181 return; // Drop message
5185 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5191 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5192 GNUNET_PEER_resolve (dst->id, &id);
5193 if (NULL == dst->core_transmit)
5195 dst->core_transmit =
5196 GNUNET_CORE_notify_transmit_ready (core_handle,
5199 GNUNET_TIME_UNIT_FOREVER_REL,
5205 if (NULL == n) // Is this internal mesh traffic?
5208 // It's payload, keep track of buffer per peer.
5209 fcinfo = tunnel_get_neighbor_fc (t, &id);
5210 i = (fcinfo->send_buffer_start + fcinfo->send_buffer_n) % t->fwd_queue_max;
5211 if (NULL != fcinfo->send_buffer[i])
5213 GNUNET_break (fcinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5214 queue_destroy (fcinfo->send_buffer[fcinfo->send_buffer_start], GNUNET_YES);
5215 fcinfo->send_buffer_start++;
5216 fcinfo->send_buffer_start %= t->fwd_queue_max;
5220 fcinfo->send_buffer_n++;
5222 fcinfo->send_buffer[i] = queue;
5223 if (fcinfo->send_buffer_n > t->fwd_queue_max)
5226 fcinfo->send_buffer_n = t->fwd_queue_max;
5231 /******************************************************************************/
5232 /******************** MESH NETWORK HANDLERS **************************/
5233 /******************************************************************************/
5237 * Core handler for path creation
5239 * @param cls closure
5240 * @param message message
5241 * @param peer peer identity this notification is about
5242 * @param atsi performance data
5243 * @param atsi_count number of records in 'atsi'
5245 * @return GNUNET_OK to keep the connection open,
5246 * GNUNET_SYSERR to close it (signal serious error)
5249 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5250 const struct GNUNET_MessageHeader *message,
5251 const struct GNUNET_ATS_Information *atsi,
5252 unsigned int atsi_count)
5254 unsigned int own_pos;
5257 MESH_TunnelNumber tid;
5258 struct GNUNET_MESH_ManipulatePath *msg;
5259 struct GNUNET_PeerIdentity *pi;
5260 struct GNUNET_HashCode hash;
5261 struct MeshPeerPath *path;
5262 struct MeshPeerInfo *dest_peer_info;
5263 struct MeshPeerInfo *orig_peer_info;
5264 struct MeshTunnel *t;
5266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5267 "Received a path create msg [%s]\n",
5268 GNUNET_i2s (&my_full_id));
5269 size = ntohs (message->size);
5270 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5272 GNUNET_break_op (0);
5276 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5277 if (size % sizeof (struct GNUNET_PeerIdentity))
5279 GNUNET_break_op (0);
5282 size /= sizeof (struct GNUNET_PeerIdentity);
5285 GNUNET_break_op (0);
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5289 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5291 tid = ntohl (msg->tid);
5292 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5294 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5295 t = tunnel_get (pi, tid);
5296 if (NULL == t) // FIXME only for INCOMING tunnels?
5300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5301 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5304 // FIXME notify failure
5307 opt = ntohl (msg->opt);
5308 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5309 GNUNET_YES : GNUNET_NO;
5310 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5312 t->nobuffer = GNUNET_YES;
5313 t->last_fwd_ack = t->fwd_pid + 1;
5315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5316 " speed_min: %d, nobuffer:%d\n",
5317 t->speed_min, t->nobuffer);
5319 if (GNUNET_YES == t->nobuffer)
5321 t->bck_queue_max = 1;
5322 t->fwd_queue_max = 1;
5325 // FIXME only assign a local tid if a local client is interested (on demand)
5326 while (NULL != tunnel_get_incoming (next_local_tid))
5327 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5328 t->local_tid_dest = next_local_tid++;
5329 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5332 tunnel_reset_timeout (t);
5333 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5335 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5336 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5344 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5345 if (NULL == dest_peer_info)
5347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5348 " Creating PeerInfo for destination.\n");
5349 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5350 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5351 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5355 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5356 if (NULL == orig_peer_info)
5358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5359 " Creating PeerInfo for origin.\n");
5360 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5361 orig_peer_info->id = GNUNET_PEER_intern (pi);
5362 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5366 path = path_new (size);
5368 for (i = 0; i < size; i++)
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5371 GNUNET_i2s (&pi[i]));
5372 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5373 if (path->peers[i] == myid)
5376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5379 /* cannot be self, must be 'not found' */
5380 /* create path: self not found in path through self */
5381 GNUNET_break_op (0);
5382 path_destroy (path);
5386 path_add_to_peers (path, GNUNET_NO);
5387 tunnel_add_path (t, path, own_pos);
5388 if (own_pos == size - 1)
5390 /* It is for us! Send ack. */
5391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5392 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5393 if (NULL == t->peers)
5395 /* New tunnel! Notify clients on first payload message. */
5396 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5398 GNUNET_break (GNUNET_SYSERR !=
5399 GNUNET_CONTAINER_multihashmap_put (t->peers,
5400 &my_full_id.hashPubKey,
5403 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5408 struct MeshPeerPath *path2;
5410 /* It's for somebody else! Retransmit. */
5411 path2 = path_duplicate (path);
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5413 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5414 path2 = path_duplicate (path);
5415 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5416 send_create_path (dest_peer_info, path, t);
5423 * Core handler for path destruction
5425 * @param cls closure
5426 * @param message message
5427 * @param peer peer identity this notification is about
5428 * @param atsi performance data
5429 * @param atsi_count number of records in 'atsi'
5431 * @return GNUNET_OK to keep the connection open,
5432 * GNUNET_SYSERR to close it (signal serious error)
5435 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5436 const struct GNUNET_MessageHeader *message,
5437 const struct GNUNET_ATS_Information *atsi,
5438 unsigned int atsi_count)
5440 struct GNUNET_MESH_ManipulatePath *msg;
5441 struct GNUNET_PeerIdentity *pi;
5442 struct MeshPeerPath *path;
5443 struct MeshTunnel *t;
5444 unsigned int own_pos;
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5449 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5450 size = ntohs (message->size);
5451 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5453 GNUNET_break_op (0);
5457 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5458 if (size % sizeof (struct GNUNET_PeerIdentity))
5460 GNUNET_break_op (0);
5463 size /= sizeof (struct GNUNET_PeerIdentity);
5466 GNUNET_break_op (0);
5469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5471 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5472 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5474 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5476 t = tunnel_get (pi, ntohl (msg->tid));
5479 /* TODO notify back: we don't know this tunnel */
5480 GNUNET_break_op (0);
5483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5484 path = path_new (size);
5486 for (i = 0; i < size; i++)
5488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5489 GNUNET_i2s (&pi[i]));
5490 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5491 if (path->peers[i] == myid)
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5495 if (own_pos < path->length - 1)
5496 send_prebuilt_message (message, &pi[own_pos + 1], t);
5498 send_client_tunnel_disconnect(t, NULL);
5500 tunnel_delete_peer (t, path->peers[path->length - 1]);
5501 path_destroy (path);
5507 * Core handler for notifications of broken paths
5509 * @param cls closure
5510 * @param message message
5511 * @param peer peer identity this notification is about
5512 * @param atsi performance data
5513 * @param atsi_count number of records in 'atsi'
5515 * @return GNUNET_OK to keep the connection open,
5516 * GNUNET_SYSERR to close it (signal serious error)
5519 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5520 const struct GNUNET_MessageHeader *message,
5521 const struct GNUNET_ATS_Information *atsi,
5522 unsigned int atsi_count)
5524 struct GNUNET_MESH_PathBroken *msg;
5525 struct MeshTunnel *t;
5527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5528 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5529 msg = (struct GNUNET_MESH_PathBroken *) message;
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5531 GNUNET_i2s (&msg->peer1));
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5533 GNUNET_i2s (&msg->peer2));
5534 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5537 GNUNET_break_op (0);
5540 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5541 GNUNET_PEER_search (&msg->peer2));
5548 * Core handler for tunnel destruction
5550 * @param cls closure
5551 * @param message message
5552 * @param peer peer identity this notification is about
5553 * @param atsi performance data
5554 * @param atsi_count number of records in 'atsi'
5556 * @return GNUNET_OK to keep the connection open,
5557 * GNUNET_SYSERR to close it (signal serious error)
5560 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5561 const struct GNUNET_MessageHeader *message,
5562 const struct GNUNET_ATS_Information *atsi,
5563 unsigned int atsi_count)
5565 struct GNUNET_MESH_TunnelDestroy *msg;
5566 struct MeshTunnel *t;
5568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5569 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5570 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5572 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5573 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5576 /* Probably already got the message from another path,
5577 * destroyed the tunnel and retransmitted to children.
5580 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5583 if (t->id.oid == myid)
5585 GNUNET_break_op (0);
5588 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5590 /* Tunnel was incoming, notify clients */
5591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5592 t->local_tid, t->local_tid_dest);
5593 send_clients_tunnel_destroy (t);
5595 tunnel_send_destroy (t);
5596 t->destroy = GNUNET_YES;
5597 // TODO: add timeout to destroy the tunnel anyway
5603 * Core handler for mesh network traffic going from the origin to a peer
5605 * @param cls closure
5606 * @param peer peer identity this notification is about
5607 * @param message message
5608 * @param atsi performance data
5609 * @param atsi_count number of records in 'atsi'
5610 * @return GNUNET_OK to keep the connection open,
5611 * GNUNET_SYSERR to close it (signal serious error)
5614 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5615 const struct GNUNET_MessageHeader *message,
5616 const struct GNUNET_ATS_Information *atsi,
5617 unsigned int atsi_count)
5619 struct GNUNET_MESH_Unicast *msg;
5620 struct GNUNET_PeerIdentity *neighbor;
5621 struct MeshTunnelFlowControlInfo *fcinfo;
5622 struct MeshTunnel *t;
5623 GNUNET_PEER_Id dest_id;
5628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5631 size = ntohs (message->size);
5633 sizeof (struct GNUNET_MESH_Unicast) +
5634 sizeof (struct GNUNET_MessageHeader))
5639 msg = (struct GNUNET_MESH_Unicast *) message;
5640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5641 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5643 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5646 /* TODO notify back: we don't know this tunnel */
5647 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5648 GNUNET_break_op (0);
5651 pid = ntohl (msg->pid);
5652 if (t->fwd_pid == pid)
5654 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5655 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5656 " Already seen pid %u, DROPPING!\n", pid);
5661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5662 " pid %u not seen yet, forwarding\n", pid);
5665 t->skip += (pid - t->fwd_pid) - 1;
5668 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5670 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5671 GNUNET_break_op (0);
5672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5673 "Received PID %u, ACK %u\n",
5674 pid, t->last_fwd_ack);
5678 tunnel_reset_timeout (t);
5679 dest_id = GNUNET_PEER_search (&msg->destination);
5680 if (dest_id == myid)
5682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5683 " it's for us! sending to clients...\n");
5684 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5685 send_subscribed_clients (message, &msg[1].header, t);
5686 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5689 ttl = ntohl (msg->ttl);
5690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5693 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5694 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5695 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5699 " not for us, retransmitting...\n");
5701 neighbor = tree_get_first_hop (t->tree, dest_id);
5702 fcinfo = tunnel_get_neighbor_fc (t, neighbor);
5703 fcinfo->fwd_pid = pid;
5704 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5707 if (GNUNET_YES == t->nobuffer &&
5708 GNUNET_YES == GMC_is_pid_bigger (pid, fcinfo->fwd_ack))
5710 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5711 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, fcinfo->fwd_ack);
5712 GNUNET_break_op (0);
5715 send_prebuilt_message (message, neighbor, t);
5716 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5722 * Core handler for mesh network traffic going from the origin to all peers
5724 * @param cls closure
5725 * @param message message
5726 * @param peer peer identity this notification is about
5727 * @param atsi performance data
5728 * @param atsi_count number of records in 'atsi'
5729 * @return GNUNET_OK to keep the connection open,
5730 * GNUNET_SYSERR to close it (signal serious error)
5732 * TODO: Check who we got this from, to validate route.
5735 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5736 const struct GNUNET_MessageHeader *message,
5737 const struct GNUNET_ATS_Information *atsi,
5738 unsigned int atsi_count)
5740 struct GNUNET_MESH_Multicast *msg;
5741 struct MeshTunnel *t;
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5747 size = ntohs (message->size);
5748 if (sizeof (struct GNUNET_MESH_Multicast) +
5749 sizeof (struct GNUNET_MessageHeader) > size)
5751 GNUNET_break_op (0);
5754 msg = (struct GNUNET_MESH_Multicast *) message;
5755 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5759 /* TODO notify that we dont know that tunnel */
5760 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5761 GNUNET_break_op (0);
5764 pid = ntohl (msg->pid);
5765 if (t->fwd_pid == pid)
5767 /* already seen this packet, drop */
5768 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5770 " Already seen pid %u, DROPPING!\n", pid);
5771 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5777 " pid %u not seen yet, forwarding\n", pid);
5779 t->skip += (pid - t->fwd_pid) - 1;
5781 tunnel_reset_timeout (t);
5783 /* Transmit to locally interested clients */
5784 if (NULL != t->peers &&
5785 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5787 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5788 send_subscribed_clients (message, &msg[1].header, t);
5789 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5792 if (ntohl (msg->ttl) == 0)
5794 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5795 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5796 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5799 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5800 tunnel_send_multicast (t, message);
5806 * Core handler for mesh network traffic toward the owner of a tunnel
5808 * @param cls closure
5809 * @param message message
5810 * @param peer peer identity this notification is about
5811 * @param atsi performance data
5812 * @param atsi_count number of records in 'atsi'
5814 * @return GNUNET_OK to keep the connection open,
5815 * GNUNET_SYSERR to close it (signal serious error)
5818 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5819 const struct GNUNET_MessageHeader *message,
5820 const struct GNUNET_ATS_Information *atsi,
5821 unsigned int atsi_count)
5823 struct GNUNET_MESH_ToOrigin *msg;
5824 struct GNUNET_PeerIdentity id;
5825 struct MeshPeerInfo *peer_info;
5826 struct MeshTunnel *t;
5827 struct MeshTunnelFlowControlInfo *fcinfo;
5831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5833 size = ntohs (message->size);
5834 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5835 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5837 GNUNET_break_op (0);
5840 msg = (struct GNUNET_MESH_ToOrigin *) message;
5841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5842 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5843 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5844 pid = ntohl (msg->pid);
5848 /* TODO notify that we dont know this tunnel (whom)? */
5849 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5850 GNUNET_break_op (0);
5851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5852 "Received to_origin with PID %u on unknown tunnel\n",
5857 fcinfo = tunnel_get_neighbor_fc (t, peer);
5864 if (fcinfo->bck_pid == pid)
5866 /* already seen this packet, drop */
5867 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5869 " Already seen pid %u, DROPPING!\n", pid);
5870 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5875 " pid %u not seen yet, forwarding\n", pid);
5876 fcinfo->bck_pid = pid;
5878 if (NULL != t->owner)
5881 struct GNUNET_MESH_ToOrigin *copy;
5883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5884 " it's for us! sending to clients...\n");
5885 /* TODO signature verification */
5886 memcpy (cbuf, message, size);
5887 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5888 copy->tid = htonl (t->local_tid);
5890 copy->pid = htonl (t->bck_pid);
5891 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5892 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5893 ©->header, GNUNET_NO);
5894 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5898 " not for us, retransmitting...\n");
5900 peer_info = peer_info_get (&msg->oid);
5901 if (NULL == peer_info)
5903 /* unknown origin of tunnel */
5907 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5908 send_prebuilt_message (message, &id, t);
5909 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5916 * Core handler for mesh network traffic point-to-point acks.
5918 * @param cls closure
5919 * @param message message
5920 * @param peer peer identity this notification is about
5921 * @param atsi performance data
5922 * @param atsi_count number of records in 'atsi'
5924 * @return GNUNET_OK to keep the connection open,
5925 * GNUNET_SYSERR to close it (signal serious error)
5928 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5929 const struct GNUNET_MessageHeader *message,
5930 const struct GNUNET_ATS_Information *atsi,
5931 unsigned int atsi_count)
5933 struct GNUNET_MESH_ACK *msg;
5934 struct MeshTunnel *t;
5937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5939 msg = (struct GNUNET_MESH_ACK *) message;
5941 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5945 /* TODO notify that we dont know this tunnel (whom)? */
5946 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5947 GNUNET_break_op (0);
5950 ack = ntohl (msg->pid);
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5953 /* Is this a forward or backward ACK? */
5954 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5956 struct MeshTunnelFlowControlInfo *fcinfo;
5959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5960 fcinfo = tunnel_get_neighbor_fc (t, peer);
5961 fcinfo->fwd_ack = ack;
5962 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5963 tunnel_unlock_fwd_queues (t);
5967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5969 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5970 tunnel_unlock_bck_queue (t);
5977 * Core handler for mesh network traffic point-to-point ack polls.
5979 * @param cls closure
5980 * @param message message
5981 * @param peer peer identity this notification is about
5982 * @param atsi performance data
5983 * @param atsi_count number of records in 'atsi'
5985 * @return GNUNET_OK to keep the connection open,
5986 * GNUNET_SYSERR to close it (signal serious error)
5989 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5990 const struct GNUNET_MessageHeader *message,
5991 const struct GNUNET_ATS_Information *atsi,
5992 unsigned int atsi_count)
5994 struct GNUNET_MESH_Poll *msg;
5995 struct MeshTunnel *t;
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6000 msg = (struct GNUNET_MESH_Poll *) message;
6002 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6006 /* TODO notify that we dont know this tunnel (whom)? */
6007 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6008 GNUNET_break_op (0);
6012 /* Is this a forward or backward ACK? */
6013 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6015 struct MeshTunnelFlowControlInfo *fcinfo;
6017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6018 fcinfo = tunnel_get_neighbor_fc (t, peer);
6019 fcinfo->bck_ack = fcinfo->fwd_pid; // mark as ready to send
6020 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6025 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6033 * Core handler for path ACKs
6035 * @param cls closure
6036 * @param message message
6037 * @param peer peer identity this notification is about
6038 * @param atsi performance data
6039 * @param atsi_count number of records in 'atsi'
6041 * @return GNUNET_OK to keep the connection open,
6042 * GNUNET_SYSERR to close it (signal serious error)
6045 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6046 const struct GNUNET_MessageHeader *message,
6047 const struct GNUNET_ATS_Information *atsi,
6048 unsigned int atsi_count)
6050 struct GNUNET_MESH_PathACK *msg;
6051 struct GNUNET_PeerIdentity id;
6052 struct MeshPeerInfo *peer_info;
6053 struct MeshPeerPath *p;
6054 struct MeshTunnel *t;
6056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6057 GNUNET_i2s (&my_full_id));
6058 msg = (struct GNUNET_MESH_PathACK *) message;
6059 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6062 /* TODO notify that we don't know the tunnel */
6063 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6065 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6069 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6071 peer_info = peer_info_get (&msg->peer_id);
6072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6073 GNUNET_i2s (&msg->peer_id));
6074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6077 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6080 "connect_by_string completed, stopping search\n");
6081 regex_cancel_search (t->regex_ctx);
6082 t->regex_ctx = NULL;
6085 /* Add paths to peers? */
6086 p = tree_get_path_to_peer (t->tree, peer_info->id);
6089 path_add_to_peers (p, GNUNET_YES);
6097 /* Message for us? */
6098 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6101 if (NULL == t->owner)
6103 GNUNET_break_op (0);
6106 if (NULL != t->dht_get_type)
6108 GNUNET_DHT_get_stop (t->dht_get_type);
6109 t->dht_get_type = NULL;
6111 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6113 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6114 send_client_peer_connected (t, peer_info->id);
6119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6120 " not for us, retransmitting...\n");
6121 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6122 peer_info = peer_info_get (&msg->oid);
6123 if (NULL == peer_info)
6125 /* If we know the tunnel, we should DEFINITELY know the peer */
6129 send_prebuilt_message (message, &id, t);
6135 * Core handler for mesh keepalives.
6137 * @param cls closure
6138 * @param message message
6139 * @param peer peer identity this notification is about
6140 * @param atsi performance data
6141 * @param atsi_count number of records in 'atsi'
6142 * @return GNUNET_OK to keep the connection open,
6143 * GNUNET_SYSERR to close it (signal serious error)
6145 * TODO: Check who we got this from, to validate route.
6148 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6149 const struct GNUNET_MessageHeader *message,
6150 const struct GNUNET_ATS_Information *atsi,
6151 unsigned int atsi_count)
6153 struct GNUNET_MESH_TunnelKeepAlive *msg;
6154 struct MeshTunnel *t;
6156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6159 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6160 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6164 /* TODO notify that we dont know that tunnel */
6165 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6166 GNUNET_break_op (0);
6170 tunnel_reset_timeout (t);
6172 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6173 tunnel_send_multicast (t, message);
6180 * Functions to handle messages from core
6182 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6183 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6184 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6185 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6186 sizeof (struct GNUNET_MESH_PathBroken)},
6187 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6188 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6189 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6190 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6191 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6192 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6193 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6194 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6195 sizeof (struct GNUNET_MESH_ACK)},
6196 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6197 sizeof (struct GNUNET_MESH_Poll)},
6198 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6199 sizeof (struct GNUNET_MESH_PathACK)},
6205 /******************************************************************************/
6206 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6207 /******************************************************************************/
6210 * deregister_app: iterator for removing each application registered by a client
6212 * @param cls closure
6213 * @param key the hash of the application id (used to access the hashmap)
6214 * @param value the value stored at the key (client)
6216 * @return GNUNET_OK on success
6219 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6221 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6222 GNUNET_break (GNUNET_YES ==
6223 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6229 * notify_client_connection_failure: notify a client that the connection to the
6230 * requested remote peer is not possible (for instance, no route found)
6231 * Function called when the socket is ready to queue more data. "buf" will be
6232 * NULL and "size" zero if the socket was closed for writing in the meantime.
6234 * @param cls closure
6235 * @param size number of bytes available in buf
6236 * @param buf where the callee should write the message
6237 * @return number of bytes written to buf
6240 notify_client_connection_failure (void *cls, size_t size, void *buf)
6243 struct MeshPeerInfo *peer_info;
6244 struct GNUNET_MESH_PeerControl *msg;
6245 struct GNUNET_PeerIdentity id;
6247 if (0 == size && NULL == buf)
6249 // TODO retry? cancel?
6253 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6254 peer_info = (struct MeshPeerInfo *) cls;
6255 msg = (struct GNUNET_MESH_PeerControl *) buf;
6256 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6257 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6258 // msg->tunnel_id = htonl(peer_info->t->tid);
6259 GNUNET_PEER_resolve (peer_info->id, &id);
6260 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6268 * Send keepalive packets for a peer
6270 * @param cls Closure (tunnel for which to send the keepalive).
6271 * @param tc Notification context.
6274 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6276 struct MeshTunnel *t = cls;
6277 struct GNUNET_MESH_TunnelKeepAlive *msg;
6278 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6281 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6282 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6288 "sending keepalive for tunnel %d\n", t->id.tid);
6290 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6291 msg->header.size = htons (size);
6292 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6293 msg->oid = my_full_id;
6294 msg->tid = htonl (t->id.tid);
6295 tunnel_send_multicast (t, &msg->header);
6297 t->path_refresh_task =
6298 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6299 tunnel_reset_timeout(t);
6304 * Function to process paths received for a new peer addition. The recorded
6305 * paths form the initial tunnel, which can be optimized later.
6306 * Called on each result obtained for the DHT search.
6308 * @param cls closure
6309 * @param exp when will this value expire
6310 * @param key key of the result
6311 * @param get_path path of the get request
6312 * @param get_path_length lenght of get_path
6313 * @param put_path path of the put request
6314 * @param put_path_length length of the put_path
6315 * @param type type of the result
6316 * @param size number of bytes in data
6317 * @param data pointer to the result data
6319 * TODO: re-issue the request after certain time? cancel after X results?
6322 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6323 const struct GNUNET_HashCode * key,
6324 const struct GNUNET_PeerIdentity *get_path,
6325 unsigned int get_path_length,
6326 const struct GNUNET_PeerIdentity *put_path,
6327 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6328 size_t size, const void *data)
6330 struct MeshPathInfo *path_info = cls;
6331 struct MeshPeerPath *p;
6332 struct GNUNET_PeerIdentity pi;
6335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6336 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6339 p = path_build_from_dht (get_path, get_path_length, put_path,
6341 path_add_to_peers (p, GNUNET_NO);
6343 for (i = 0; i < path_info->peer->ntunnels; i++)
6345 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6346 peer_info_connect (path_info->peer, path_info->t);
6354 * Function to process paths received for a new peer addition. The recorded
6355 * paths form the initial tunnel, which can be optimized later.
6356 * Called on each result obtained for the DHT search.
6358 * @param cls closure
6359 * @param exp when will this value expire
6360 * @param key key of the result
6361 * @param get_path path of the get request
6362 * @param get_path_length lenght of get_path
6363 * @param put_path path of the put request
6364 * @param put_path_length length of the put_path
6365 * @param type type of the result
6366 * @param size number of bytes in data
6367 * @param data pointer to the result data
6370 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6371 const struct GNUNET_HashCode * key,
6372 const struct GNUNET_PeerIdentity *get_path,
6373 unsigned int get_path_length,
6374 const struct GNUNET_PeerIdentity *put_path,
6375 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6376 size_t size, const void *data)
6378 const struct PBlock *pb = data;
6379 const struct GNUNET_PeerIdentity *pi = &pb->id;
6380 struct MeshTunnel *t = cls;
6381 struct MeshPeerInfo *peer_info;
6382 struct MeshPeerPath *p;
6384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6385 if (size != sizeof (struct PBlock))
6387 GNUNET_break_op (0);
6390 if (ntohl(pb->type) != t->type)
6392 GNUNET_break_op (0);
6395 GNUNET_assert (NULL != t->owner);
6396 peer_info = peer_info_get (pi);
6397 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6399 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6401 p = path_build_from_dht (get_path, get_path_length, put_path,
6403 path_add_to_peers (p, GNUNET_NO);
6405 tunnel_add_peer (t, peer_info);
6406 peer_info_connect (peer_info, t);
6411 * Function to process DHT string to regex matching.
6412 * Called on each result obtained for the DHT search.
6414 * @param cls closure (search context)
6415 * @param exp when will this value expire
6416 * @param key key of the result
6417 * @param get_path path of the get request (not used)
6418 * @param get_path_length lenght of get_path (not used)
6419 * @param put_path path of the put request (not used)
6420 * @param put_path_length length of the put_path (not used)
6421 * @param type type of the result
6422 * @param size number of bytes in data
6423 * @param data pointer to the result data
6426 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6427 const struct GNUNET_HashCode * key,
6428 const struct GNUNET_PeerIdentity *get_path,
6429 unsigned int get_path_length,
6430 const struct GNUNET_PeerIdentity *put_path,
6431 unsigned int put_path_length,
6432 enum GNUNET_BLOCK_Type type,
6433 size_t size, const void *data)
6435 const struct MeshRegexAccept *block = data;
6436 struct MeshRegexSearchContext *ctx = cls;
6437 struct MeshRegexSearchInfo *info = ctx->info;
6438 struct MeshPeerPath *p;
6439 struct MeshPeerInfo *peer_info;
6441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6444 peer_info = peer_info_get(&block->id);
6445 p = path_build_from_dht (get_path, get_path_length, put_path,
6447 path_add_to_peers (p, GNUNET_NO);
6450 tunnel_add_peer (info->t, peer_info);
6451 peer_info_connect (peer_info, info->t);
6452 if (0 == info->peer)
6454 info->peer = peer_info->id;
6458 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6461 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6462 ®ex_connect_timeout,
6470 * Function to process DHT string to regex matching.
6471 * Called on each result obtained for the DHT search.
6473 * @param cls closure (search context)
6474 * @param exp when will this value expire
6475 * @param key key of the result
6476 * @param get_path path of the get request (not used)
6477 * @param get_path_length lenght of get_path (not used)
6478 * @param put_path path of the put request (not used)
6479 * @param put_path_length length of the put_path (not used)
6480 * @param type type of the result
6481 * @param size number of bytes in data
6482 * @param data pointer to the result data
6484 * TODO: re-issue the request after certain time? cancel after X results?
6487 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6488 const struct GNUNET_HashCode * key,
6489 const struct GNUNET_PeerIdentity *get_path,
6490 unsigned int get_path_length,
6491 const struct GNUNET_PeerIdentity *put_path,
6492 unsigned int put_path_length,
6493 enum GNUNET_BLOCK_Type type,
6494 size_t size, const void *data)
6496 const struct MeshRegexBlock *block = data;
6497 struct MeshRegexSearchContext *ctx = cls;
6498 struct MeshRegexSearchInfo *info = ctx->info;
6502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6503 "DHT GET STRING RETURNED RESULTS\n");
6504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6505 " key: %s\n", GNUNET_h2s (key));
6507 copy = GNUNET_malloc (size);
6508 memcpy (copy, data, size);
6509 GNUNET_break (GNUNET_OK ==
6510 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6511 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6512 len = ntohl (block->n_proof);
6514 char proof[len + 1];
6516 memcpy (proof, &block[1], len);
6518 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6520 GNUNET_break_op (0);
6524 len = strlen (info->description);
6525 if (len == ctx->position) // String processed
6527 if (GNUNET_YES == ntohl (block->accepting))
6529 regex_find_path(key, ctx);
6533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6534 // FIXME REGEX this block not successful, wait for more? start timeout?
6539 regex_next_edge (block, size, ctx);
6544 /******************************************************************************/
6545 /********************* MESH LOCAL HANDLES **************************/
6546 /******************************************************************************/
6550 * Handler for client disconnection
6552 * @param cls closure
6553 * @param client identification of the client; NULL
6554 * for the last call when the server is destroyed
6557 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6559 struct MeshClient *c;
6560 struct MeshClient *next;
6563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6572 if (c->handle != client)
6574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6580 GNUNET_SERVER_client_drop (c->handle);
6581 c->shutting_down = GNUNET_YES;
6582 GNUNET_assert (NULL != c->own_tunnels);
6583 GNUNET_assert (NULL != c->incoming_tunnels);
6584 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6585 &tunnel_destroy_iterator, c);
6586 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6587 &tunnel_destroy_iterator, c);
6588 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6589 &tunnel_destroy_iterator, c);
6590 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6591 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6592 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6594 /* deregister clients applications */
6595 if (NULL != c->apps)
6597 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6598 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6600 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6601 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6603 GNUNET_SCHEDULER_cancel (announce_applications_task);
6604 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6606 if (NULL != c->types)
6607 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6608 for (i = 0; i < c->n_regex; i++)
6610 GNUNET_free (c->regexes[i].regex);
6612 GNUNET_free_non_null (c->regexes);
6613 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6614 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6616 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6619 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6628 * Handler for new clients
6630 * @param cls closure
6631 * @param client identification of the client
6632 * @param message the actual message, which includes messages the client wants
6635 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6636 const struct GNUNET_MessageHeader *message)
6638 struct GNUNET_MESH_ClientConnect *cc_msg;
6639 struct MeshClient *c;
6640 GNUNET_MESH_ApplicationType *a;
6647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6648 /* Check data sanity */
6649 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6650 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6651 ntypes = ntohs (cc_msg->types);
6652 napps = ntohs (cc_msg->applications);
6654 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6661 /* Create new client structure */
6662 c = GNUNET_malloc (sizeof (struct MeshClient));
6663 c->id = next_client_id++;
6664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6666 GNUNET_SERVER_client_keep (client);
6667 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6670 GNUNET_MESH_ApplicationType at;
6671 struct GNUNET_HashCode hc;
6673 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6674 for (i = 0; i < napps; i++)
6677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6678 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6679 /* store in clients hashmap */
6680 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6681 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6682 /* store in global hashmap, for announcements */
6683 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6686 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6687 announce_applications_task =
6688 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6694 struct GNUNET_HashCode hc;
6696 t = (uint16_t *) & a[napps];
6697 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6698 for (i = 0; i < ntypes; i++)
6701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6702 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6704 /* store in clients hashmap */
6705 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6706 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6707 /* store in global hashmap */
6708 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6709 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6713 " client has %u+%u subscriptions\n", napps, ntypes);
6715 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6716 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6717 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6718 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6719 GNUNET_SERVER_notification_context_add (nc, client);
6720 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6722 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6728 * Handler for clients announcing available services by a regular expression.
6730 * @param cls closure
6731 * @param client identification of the client
6732 * @param message the actual message, which includes messages the client wants
6735 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6736 const struct GNUNET_MessageHeader *message)
6738 struct GNUNET_MESH_RegexAnnounce *msg;
6739 struct MeshRegexDescriptor rd;
6740 struct MeshClient *c;
6744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6746 /* Sanity check for client registration */
6747 if (NULL == (c = client_get (client)))
6750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6755 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6756 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6757 regex = GNUNET_malloc (len + 1);
6758 memcpy (regex, &msg[1], len);
6761 rd.compression = ntohs (msg->compression_characters);
6762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6765 GNUNET_array_append (c->regexes, c->n_regex, rd);
6766 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6768 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6774 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6780 * Handler for requests of new tunnels
6782 * @param cls closure
6783 * @param client identification of the client
6784 * @param message the actual message
6787 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6788 const struct GNUNET_MessageHeader *message)
6790 struct GNUNET_MESH_TunnelMessage *t_msg;
6791 struct MeshTunnel *t;
6792 struct MeshClient *c;
6793 MESH_TunnelNumber tid;
6795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6797 /* Sanity check for client registration */
6798 if (NULL == (c = client_get (client)))
6801 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6806 /* Message sanity check */
6807 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6814 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6815 /* Sanity check for tunnel numbering */
6816 tid = ntohl (t_msg->tunnel_id);
6817 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6823 /* Sanity check for duplicate tunnel IDs */
6824 if (NULL != tunnel_get_by_local_id (c, tid))
6827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6831 while (NULL != tunnel_get_by_pi (myid, next_tid))
6832 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6833 t = tunnel_new (myid, next_tid++, c, tid);
6836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6838 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6841 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6843 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6844 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6847 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6853 * Handler for requests of deleting tunnels
6855 * @param cls closure
6856 * @param client identification of the client
6857 * @param message the actual message
6860 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6861 const struct GNUNET_MessageHeader *message)
6863 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6864 struct MeshClient *c;
6865 struct MeshTunnel *t;
6866 MESH_TunnelNumber tid;
6868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6869 "Got a DESTROY TUNNEL from client!\n");
6871 /* Sanity check for client registration */
6872 if (NULL == (c = client_get (client)))
6875 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6880 /* Message sanity check */
6881 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6888 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6890 /* Retrieve tunnel */
6891 tid = ntohl (tunnel_msg->tunnel_id);
6892 t = tunnel_get_by_local_id(c, tid);
6895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6897 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6900 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6902 client_ignore_tunnel (c, t);
6904 // TODO: when to destroy incoming tunnel?
6905 if (t->nclients == 0)
6907 GNUNET_assert (GNUNET_YES ==
6908 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6910 GNUNET_assert (GNUNET_YES ==
6911 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6912 &my_full_id.hashPubKey,
6916 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6919 send_client_tunnel_disconnect(t, c);
6920 client_delete_tunnel(c, t);
6922 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6924 tunnel_send_destroy (t);
6925 t->destroy = GNUNET_YES;
6926 // The tunnel will be destroyed when the last message is transmitted.
6927 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6933 * Handler for requests of seeting tunnel's speed.
6935 * @param cls Closure (unused).
6936 * @param client Identification of the client.
6937 * @param message The actual message.
6940 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6941 const struct GNUNET_MessageHeader *message)
6943 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6944 struct MeshClient *c;
6945 struct MeshTunnel *t;
6946 MESH_TunnelNumber tid;
6948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6949 "Got a SPEED request from client!\n");
6951 /* Sanity check for client registration */
6952 if (NULL == (c = client_get (client)))
6955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6961 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6963 /* Retrieve tunnel */
6964 tid = ntohl (tunnel_msg->tunnel_id);
6965 t = tunnel_get_by_local_id(c, tid);
6968 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6974 switch (ntohs(message->type))
6976 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6977 t->speed_min = GNUNET_YES;
6979 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6980 t->speed_min = GNUNET_NO;
6985 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6990 * Handler for requests of seeting tunnel's buffering policy.
6992 * @param cls Closure (unused).
6993 * @param client Identification of the client.
6994 * @param message The actual message.
6997 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6998 const struct GNUNET_MessageHeader *message)
7000 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7001 struct MeshClient *c;
7002 struct MeshTunnel *t;
7003 MESH_TunnelNumber tid;
7005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7006 "Got a BUFFER request from client!\n");
7008 /* Sanity check for client registration */
7009 if (NULL == (c = client_get (client)))
7012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7017 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7019 /* Retrieve tunnel */
7020 tid = ntohl (tunnel_msg->tunnel_id);
7021 t = tunnel_get_by_local_id(c, tid);
7024 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7026 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7030 switch (ntohs(message->type))
7032 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7033 t->nobuffer = GNUNET_NO;
7035 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7036 t->nobuffer = GNUNET_YES;
7042 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7047 * Handler for connection requests to new peers
7049 * @param cls closure
7050 * @param client identification of the client
7051 * @param message the actual message (PeerControl)
7054 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7055 const struct GNUNET_MessageHeader *message)
7057 struct GNUNET_MESH_PeerControl *peer_msg;
7058 struct MeshPeerInfo *peer_info;
7059 struct MeshClient *c;
7060 struct MeshTunnel *t;
7061 MESH_TunnelNumber tid;
7063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7064 /* Sanity check for client registration */
7065 if (NULL == (c = client_get (client)))
7068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7073 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7075 /* Sanity check for message size */
7076 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7079 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7083 /* Tunnel exists? */
7084 tid = ntohl (peer_msg->tunnel_id);
7085 t = tunnel_get_by_local_id (c, tid);
7089 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7093 /* Does client own tunnel? */
7094 if (t->owner->handle != client)
7097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7101 GNUNET_i2s (&peer_msg->peer));
7102 peer_info = peer_info_get (&peer_msg->peer);
7104 tunnel_add_peer (t, peer_info);
7105 peer_info_connect (peer_info, t);
7107 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7113 * Handler for disconnection requests of peers in a tunnel
7115 * @param cls closure
7116 * @param client identification of the client
7117 * @param message the actual message (PeerControl)
7120 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7121 const struct GNUNET_MessageHeader *message)
7123 struct GNUNET_MESH_PeerControl *peer_msg;
7124 struct MeshPeerInfo *peer_info;
7125 struct MeshClient *c;
7126 struct MeshTunnel *t;
7127 MESH_TunnelNumber tid;
7129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7130 /* Sanity check for client registration */
7131 if (NULL == (c = client_get (client)))
7134 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7139 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7141 /* Sanity check for message size */
7142 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7145 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7149 /* Tunnel exists? */
7150 tid = ntohl (peer_msg->tunnel_id);
7151 t = tunnel_get_by_local_id (c, tid);
7155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7160 /* Does client own tunnel? */
7161 if (t->owner->handle != client)
7164 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7169 GNUNET_i2s (&peer_msg->peer));
7170 /* Is the peer in the tunnel? */
7172 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7173 if (NULL == peer_info)
7176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7180 /* Ok, delete peer from tunnel */
7181 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7182 &peer_msg->peer.hashPubKey);
7184 send_destroy_path (t, peer_info->id);
7185 tunnel_delete_peer (t, peer_info->id);
7186 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7191 * Handler for blacklist requests of peers in a tunnel
7193 * @param cls closure
7194 * @param client identification of the client
7195 * @param message the actual message (PeerControl)
7197 * FIXME implement DHT block bloomfilter
7200 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7201 const struct GNUNET_MessageHeader *message)
7203 struct GNUNET_MESH_PeerControl *peer_msg;
7204 struct MeshClient *c;
7205 struct MeshTunnel *t;
7206 MESH_TunnelNumber tid;
7208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7209 /* Sanity check for client registration */
7210 if (NULL == (c = client_get (client)))
7213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7218 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7220 /* Sanity check for message size */
7221 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7228 /* Tunnel exists? */
7229 tid = ntohl (peer_msg->tunnel_id);
7230 t = tunnel_get_by_local_id (c, tid);
7234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7239 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7240 GNUNET_PEER_intern(&peer_msg->peer));
7245 * Handler for unblacklist requests of peers in a tunnel
7247 * @param cls closure
7248 * @param client identification of the client
7249 * @param message the actual message (PeerControl)
7252 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7253 const struct GNUNET_MessageHeader *message)
7255 struct GNUNET_MESH_PeerControl *peer_msg;
7256 struct MeshClient *c;
7257 struct MeshTunnel *t;
7258 MESH_TunnelNumber tid;
7262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7263 /* Sanity check for client registration */
7264 if (NULL == (c = client_get (client)))
7267 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7272 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7274 /* Sanity check for message size */
7275 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7282 /* Tunnel exists? */
7283 tid = ntohl (peer_msg->tunnel_id);
7284 t = tunnel_get_by_local_id (c, tid);
7288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7293 /* if peer is not known, complain */
7294 pid = GNUNET_PEER_search (&peer_msg->peer);
7301 /* search and remove from list */
7302 for (i = 0; i < t->nblacklisted; i++)
7304 if (t->blacklisted[i] == pid)
7306 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7307 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7312 /* if peer hasn't been blacklisted, complain */
7318 * Handler for connection requests to new peers by type
7320 * @param cls closure
7321 * @param client identification of the client
7322 * @param message the actual message (ConnectPeerByType)
7325 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7326 const struct GNUNET_MessageHeader *message)
7328 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7329 struct MeshClient *c;
7330 struct MeshTunnel *t;
7331 struct GNUNET_HashCode hash;
7332 MESH_TunnelNumber tid;
7334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7335 /* Sanity check for client registration */
7336 if (NULL == (c = client_get (client)))
7339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7344 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7346 /* Sanity check for message size */
7347 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7348 ntohs (connect_msg->header.size))
7351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7355 /* Tunnel exists? */
7356 tid = ntohl (connect_msg->tunnel_id);
7357 t = tunnel_get_by_local_id (c, tid);
7361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7365 /* Does client own tunnel? */
7366 if (t->owner->handle != client)
7369 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7373 /* Do WE have the service? */
7374 t->type = ntohl (connect_msg->type);
7375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7376 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7377 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7380 /* Yes! Fast forward, add ourselves to the tunnel and send the
7381 * good news to the client, and alert the destination client of
7382 * an incoming tunnel.
7384 * FIXME send a path create to self, avoid code duplication
7386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7387 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7388 peer_info_get (&my_full_id),
7389 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7392 send_client_peer_connected (t, myid);
7393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7394 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7396 t->local_tid_dest = next_local_tid++;
7397 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7398 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7399 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7403 /* Ok, lets find a peer offering the service */
7404 if (NULL != t->dht_get_type)
7406 GNUNET_DHT_get_stop (t->dht_get_type);
7408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7409 GNUNET_h2s (&hash));
7411 GNUNET_DHT_get_start (dht_handle,
7412 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7414 dht_replication_level,
7415 GNUNET_DHT_RO_RECORD_ROUTE |
7416 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7418 &dht_get_type_handler, t);
7420 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7426 * Handler for connection requests to new peers by a string service description.
7428 * @param cls closure
7429 * @param client identification of the client
7430 * @param message the actual message, which includes messages the client wants
7433 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7434 const struct GNUNET_MessageHeader *message)
7436 struct GNUNET_MESH_ConnectPeerByString *msg;
7437 struct MeshRegexSearchContext *ctx;
7438 struct MeshRegexSearchInfo *info;
7439 struct GNUNET_DHT_GetHandle *get_h;
7440 struct GNUNET_HashCode key;
7441 struct MeshTunnel *t;
7442 struct MeshClient *c;
7443 MESH_TunnelNumber tid;
7447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7448 "Connect by string started\n");
7449 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7450 size = htons (message->size);
7452 /* Sanity check for client registration */
7453 if (NULL == (c = client_get (client)))
7456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7461 /* Message size sanity check */
7462 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7469 /* Tunnel exists? */
7470 tid = ntohl (msg->tunnel_id);
7471 t = tunnel_get_by_local_id (c, tid);
7475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7479 /* Does client own tunnel? */
7480 if (t->owner->handle != client)
7483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7488 " on tunnel %s [%u]\n",
7489 GNUNET_i2s(&my_full_id),
7492 /* Only one connect_by_string allowed at the same time! */
7493 /* FIXME: allow more, return handle at api level to cancel, document */
7494 if (NULL != t->regex_ctx)
7497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7501 /* Find string itself */
7502 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7503 string = (const char *) &msg[1];
7505 /* Initialize context */
7506 size = GNUNET_REGEX_get_first_key(string, len, &key);
7507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7508 " consumed %u bits out of %u\n", size, len);
7509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7510 " looking for %s\n", GNUNET_h2s (&key));
7512 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7514 info->description = GNUNET_malloc (len + 1);
7515 memcpy (info->description, string, len);
7516 info->description[len] = '\0';
7517 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7518 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7521 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7522 ctx->position = size;
7526 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7528 /* Start search in DHT */
7529 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7530 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7531 &key, /* key to search */
7532 dht_replication_level, /* replication level */
7533 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7534 NULL, /* xquery */ // FIXME BLOOMFILTER
7535 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7536 &dht_get_string_handler, ctx);
7538 GNUNET_break (GNUNET_OK ==
7539 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7542 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7544 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7550 * Handler for client traffic directed to one peer
7552 * @param cls closure
7553 * @param client identification of the client
7554 * @param message the actual message
7557 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7558 const struct GNUNET_MessageHeader *message)
7560 struct MeshClient *c;
7561 struct MeshTunnel *t;
7562 struct MeshPeerInfo *pi;
7563 struct GNUNET_MESH_Unicast *data_msg;
7564 MESH_TunnelNumber tid;
7567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7568 "Got a unicast request from a client!\n");
7570 /* Sanity check for client registration */
7571 if (NULL == (c = client_get (client)))
7574 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7579 data_msg = (struct GNUNET_MESH_Unicast *) message;
7581 /* Sanity check for message size */
7582 size = ntohs (message->size);
7583 if (sizeof (struct GNUNET_MESH_Unicast) +
7584 sizeof (struct GNUNET_MessageHeader) > size)
7587 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7591 /* Tunnel exists? */
7592 tid = ntohl (data_msg->tid);
7593 t = tunnel_get_by_local_id (c, tid);
7597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7601 /* Is it a local tunnel? Then, does client own the tunnel? */
7602 if (t->owner->handle != client)
7605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7609 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7610 &data_msg->destination.hashPubKey);
7611 /* Is the selected peer in the tunnel? */
7615 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7619 /* PID should be as expected */
7620 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7624 "Unicast PID, expected %u, got %u\n",
7625 t->fwd_pid + 1, ntohl (data_msg->pid));
7626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7630 /* Ok, everything is correct, send the message
7631 * (pretend we got it from a mesh peer)
7634 /* Work around const limitation */
7635 char buf[ntohs (message->size)] GNUNET_ALIGN;
7636 struct GNUNET_MESH_Unicast *copy;
7638 copy = (struct GNUNET_MESH_Unicast *) buf;
7639 memcpy (buf, data_msg, size);
7640 copy->oid = my_full_id;
7641 copy->tid = htonl (t->id.tid);
7642 copy->ttl = htonl (default_ttl);
7643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7644 " calling generic handler...\n");
7645 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7648 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7655 * Handler for client traffic directed to the origin
7657 * @param cls closure
7658 * @param client identification of the client
7659 * @param message the actual message
7662 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7663 const struct GNUNET_MessageHeader *message)
7665 struct GNUNET_MESH_ToOrigin *data_msg;
7666 struct MeshTunnelFlowControlInfo *fcinfo;
7667 struct MeshClient *c;
7668 struct MeshTunnel *t;
7669 MESH_TunnelNumber tid;
7672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7673 "Got a ToOrigin request from a client!\n");
7674 /* Sanity check for client registration */
7675 if (NULL == (c = client_get (client)))
7678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7683 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7685 /* Sanity check for message size */
7686 size = ntohs (message->size);
7687 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7688 sizeof (struct GNUNET_MessageHeader) > size)
7691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7695 /* Tunnel exists? */
7696 tid = ntohl (data_msg->tid);
7697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7698 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7704 t = tunnel_get_by_local_id (c, tid);
7707 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7708 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7710 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7714 /* It should be sent by someone who has this as incoming tunnel. */
7715 if (GNUNET_NO == client_knows_tunnel (c, t))
7718 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7722 /* PID should be as expected */
7723 fcinfo = tunnel_get_client_fc (t, c);
7724 if (ntohl (data_msg->pid) != fcinfo->bck_pid + 1)
7727 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7728 "To Origin PID, expected %u, got %u\n",
7729 fcinfo->bck_pid + 1,
7730 ntohl (data_msg->pid));
7731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7736 /* Ok, everything is correct, send the message
7737 * (pretend we got it from a mesh peer)
7740 char buf[ntohs (message->size)] GNUNET_ALIGN;
7741 struct GNUNET_MESH_ToOrigin *copy;
7743 /* Work around const limitation */
7744 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7745 memcpy (buf, data_msg, size);
7746 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7747 copy->tid = htonl (t->id.tid);
7748 copy->ttl = htonl (default_ttl);
7749 copy->pid = htonl (t->bck_pid + 1);
7751 copy->sender = my_full_id;
7752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7753 " calling generic handler...\n");
7754 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7756 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7763 * Handler for client traffic directed to all peers in a tunnel
7765 * @param cls closure
7766 * @param client identification of the client
7767 * @param message the actual message
7770 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7771 const struct GNUNET_MessageHeader *message)
7773 struct MeshClient *c;
7774 struct MeshTunnel *t;
7775 struct GNUNET_MESH_Multicast *data_msg;
7776 MESH_TunnelNumber tid;
7778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7779 "Got a multicast request from a client!\n");
7781 /* Sanity check for client registration */
7782 if (NULL == (c = client_get (client)))
7785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7790 data_msg = (struct GNUNET_MESH_Multicast *) message;
7792 /* Sanity check for message size */
7793 if (sizeof (struct GNUNET_MESH_Multicast) +
7794 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7801 /* Tunnel exists? */
7802 tid = ntohl (data_msg->tid);
7803 t = tunnel_get_by_local_id (c, tid);
7807 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7814 /* Does client own tunnel? */
7815 if (t->owner->handle != client)
7818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7822 /* PID should be as expected */
7823 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7827 "Multicast PID, expected %u, got %u\n",
7828 t->fwd_pid + 1, ntohl (data_msg->pid));
7829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7834 char buf[ntohs (message->size)] GNUNET_ALIGN;
7835 struct GNUNET_MESH_Multicast *copy;
7837 copy = (struct GNUNET_MESH_Multicast *) buf;
7838 memcpy (buf, message, ntohs (message->size));
7839 copy->oid = my_full_id;
7840 copy->tid = htonl (t->id.tid);
7841 copy->ttl = htonl (default_ttl);
7842 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7844 " calling generic handler...\n");
7845 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7848 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7854 * Handler for client's ACKs for payload traffic.
7856 * @param cls Closure (unused).
7857 * @param client Identification of the client.
7858 * @param message The actual message.
7861 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7862 const struct GNUNET_MessageHeader *message)
7864 struct GNUNET_MESH_LocalAck *msg;
7865 struct MeshTunnel *t;
7866 struct MeshClient *c;
7867 MESH_TunnelNumber tid;
7870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7871 /* Sanity check for client registration */
7872 if (NULL == (c = client_get (client)))
7875 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7880 msg = (struct GNUNET_MESH_LocalAck *) message;
7882 /* Tunnel exists? */
7883 tid = ntohl (msg->tunnel_id);
7884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7885 t = tunnel_get_by_local_id (c, tid);
7889 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7895 ack = ntohl (msg->max_pid);
7896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7898 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7899 if (NULL != t->owner && t->owner->handle == client)
7901 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7903 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7907 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7908 tunnel_set_client_fwd_ack (t, c, ack);
7909 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7912 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7919 * Functions to handle messages from clients
7921 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7922 {&handle_local_new_client, NULL,
7923 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7924 {&handle_local_announce_regex, NULL,
7925 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7926 {&handle_local_tunnel_create, NULL,
7927 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7928 sizeof (struct GNUNET_MESH_TunnelMessage)},
7929 {&handle_local_tunnel_destroy, NULL,
7930 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7931 sizeof (struct GNUNET_MESH_TunnelMessage)},
7932 {&handle_local_tunnel_speed, NULL,
7933 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7934 sizeof (struct GNUNET_MESH_TunnelMessage)},
7935 {&handle_local_tunnel_speed, NULL,
7936 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7937 sizeof (struct GNUNET_MESH_TunnelMessage)},
7938 {&handle_local_tunnel_buffer, NULL,
7939 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7940 sizeof (struct GNUNET_MESH_TunnelMessage)},
7941 {&handle_local_tunnel_buffer, NULL,
7942 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7943 sizeof (struct GNUNET_MESH_TunnelMessage)},
7944 {&handle_local_connect_add, NULL,
7945 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7946 sizeof (struct GNUNET_MESH_PeerControl)},
7947 {&handle_local_connect_del, NULL,
7948 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7949 sizeof (struct GNUNET_MESH_PeerControl)},
7950 {&handle_local_blacklist, NULL,
7951 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7952 sizeof (struct GNUNET_MESH_PeerControl)},
7953 {&handle_local_unblacklist, NULL,
7954 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7955 sizeof (struct GNUNET_MESH_PeerControl)},
7956 {&handle_local_connect_by_type, NULL,
7957 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7958 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7959 {&handle_local_connect_by_string, NULL,
7960 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7961 {&handle_local_unicast, NULL,
7962 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7963 {&handle_local_to_origin, NULL,
7964 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7965 {&handle_local_multicast, NULL,
7966 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7967 {&handle_local_ack, NULL,
7968 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7969 sizeof (struct GNUNET_MESH_LocalAck)},
7975 * To be called on core init/fail.
7977 * @param cls service closure
7978 * @param server handle to the server for this service
7979 * @param identity the public identity of this peer
7982 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7983 const struct GNUNET_PeerIdentity *identity)
7986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7987 core_handle = server;
7988 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7991 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7992 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7994 GNUNET_abort(); // Try harder
8001 * Method called whenever a given peer connects.
8003 * @param cls closure
8004 * @param peer peer identity this notification is about
8005 * @param atsi performance data for the connection
8006 * @param atsi_count number of records in 'atsi'
8009 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8010 const struct GNUNET_ATS_Information *atsi,
8011 unsigned int atsi_count)
8013 struct MeshPeerInfo *peer_info;
8014 struct MeshPeerPath *path;
8016 DEBUG_CONN ("Peer connected\n");
8017 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8018 peer_info = peer_info_get (peer);
8019 if (myid == peer_info->id)
8021 DEBUG_CONN (" (self)\n");
8026 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8028 path = path_new (2);
8029 path->peers[0] = myid;
8030 path->peers[1] = peer_info->id;
8031 GNUNET_PEER_change_rc (myid, 1);
8032 GNUNET_PEER_change_rc (peer_info->id, 1);
8033 peer_info_add_path (peer_info, path, GNUNET_YES);
8034 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8040 * Method called whenever a peer disconnects.
8042 * @param cls closure
8043 * @param peer peer identity this notification is about
8046 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8048 struct MeshPeerInfo *pi;
8049 struct MeshPeerQueue *q;
8050 struct MeshPeerQueue *n;
8052 DEBUG_CONN ("Peer disconnected\n");
8053 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8063 /* TODO try to reroute this traffic instead */
8064 queue_destroy(q, GNUNET_YES);
8067 if (NULL != pi->core_transmit)
8069 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8070 pi->core_transmit = NULL;
8072 peer_info_remove_path (pi, pi->id, myid);
8075 DEBUG_CONN (" (self)\n");
8077 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8082 /******************************************************************************/
8083 /************************ MAIN FUNCTIONS ****************************/
8084 /******************************************************************************/
8087 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8089 * @param cls closure
8090 * @param key current key code
8091 * @param value value in the hash map
8092 * @return GNUNET_YES if we should continue to iterate,
8096 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8098 struct MeshTunnel *t = value;
8105 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8107 * @param cls closure
8108 * @param key current key code
8109 * @param value value in the hash map
8110 * @return GNUNET_YES if we should continue to iterate,
8114 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8116 struct MeshPeerInfo *p = value;
8117 struct MeshPeerQueue *q;
8118 struct MeshPeerQueue *n;
8126 queue_destroy(q, GNUNET_YES);
8130 peer_info_destroy (p);
8136 * Task run during shutdown.
8142 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8146 if (core_handle != NULL)
8148 GNUNET_CORE_disconnect (core_handle);
8153 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8156 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8157 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8158 if (dht_handle != NULL)
8160 GNUNET_DHT_disconnect (dht_handle);
8165 GNUNET_SERVER_notification_context_destroy (nc);
8168 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8170 GNUNET_SCHEDULER_cancel (announce_id_task);
8171 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8178 * Callback for hostkey read/generation
8181 * @param pk the private key
8182 * @param emsg error message
8185 key_generation_cb (void *cls,
8186 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8189 struct MeshPeerInfo *peer;
8190 struct MeshPeerPath *p;
8195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8196 _("Mesh service could not access hostkey. Exiting.\n"));
8197 GNUNET_SCHEDULER_shutdown ();
8200 my_private_key = pk;
8201 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8202 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8203 &my_full_id.hashPubKey);
8204 myid = GNUNET_PEER_intern (&my_full_id);
8205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8206 "Mesh for peer [%s] starting\n",
8207 GNUNET_i2s(&my_full_id));
8209 // transport_handle = GNUNET_TRANSPORT_connect(c,
8219 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8222 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8223 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8224 GNUNET_SERVER_disconnect_notify (server_handle,
8225 &handle_local_client_disconnect, NULL);
8229 clients_tail = NULL;
8232 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8233 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8235 /* Create a peer_info for the local peer */
8236 peer = peer_info_get (&my_full_id);
8239 GNUNET_PEER_change_rc (myid, 1);
8240 peer_info_add_path (peer, p, GNUNET_YES);
8241 GNUNET_SERVER_resume (server_handle);
8242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8247 * Process mesh requests.
8249 * @param cls closure
8250 * @param server the initialized server
8251 * @param c configuration to use
8254 run (void *cls, struct GNUNET_SERVER_Handle *server,
8255 const struct GNUNET_CONFIGURATION_Handle *c)
8259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8260 server_handle = server;
8261 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8262 NULL, /* Closure passed to MESH functions */
8263 &core_init, /* Call core_init once connected */
8264 &core_connect, /* Handle connects */
8265 &core_disconnect, /* remove peers on disconnects */
8266 NULL, /* Don't notify about all incoming messages */
8267 GNUNET_NO, /* For header only in notification */
8268 NULL, /* Don't notify about all outbound messages */
8269 GNUNET_NO, /* For header-only out notification */
8270 core_handlers); /* Register these handlers */
8272 if (core_handle == NULL)
8275 GNUNET_SCHEDULER_shutdown ();
8280 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8285 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8287 GNUNET_SCHEDULER_shutdown ();
8292 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8293 &refresh_path_time))
8295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8297 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8298 "mesh", "refresh path time");
8299 GNUNET_SCHEDULER_shutdown ();
8304 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8305 &app_announce_time))
8307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8309 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8310 "mesh", "app announce time");
8311 GNUNET_SCHEDULER_shutdown ();
8314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8317 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8322 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8323 "mesh", "id announce time");
8324 GNUNET_SCHEDULER_shutdown ();
8332 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8333 &unacknowledged_wait_time))
8335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8337 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8338 "mesh", "unacknowledged wait time");
8339 GNUNET_SCHEDULER_shutdown ();
8344 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8347 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8349 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8350 "mesh", "connect timeout");
8351 GNUNET_SCHEDULER_shutdown ();
8356 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8361 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8362 "mesh", "max msgs queue");
8363 GNUNET_SCHEDULER_shutdown ();
8368 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8373 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8374 "mesh", "max tunnels");
8375 GNUNET_SCHEDULER_shutdown ();
8380 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8385 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8386 "mesh", "default ttl", 64);
8391 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8392 &dht_replication_level))
8394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8396 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8397 "mesh", "dht replication level", 10);
8398 dht_replication_level = 10;
8401 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8402 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8403 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8404 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8405 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8407 dht_handle = GNUNET_DHT_connect (c, 64);
8408 if (NULL == dht_handle)
8412 stats = GNUNET_STATISTICS_create ("mesh", c);
8414 GNUNET_SERVER_suspend (server_handle);
8415 /* Scheduled the task to clean up when shutdown is called */
8416 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8418 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8419 GNUNET_free (keyfile);
8424 * The main function for the mesh service.
8426 * @param argc number of arguments from the command line
8427 * @param argv command line arguments
8428 * @return 0 ok, 1 on error
8431 main (int argc, char *const *argv)
8436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8437 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8439 ret = (GNUNET_OK == r) ? 0 : 1;
8440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8444 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8445 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8446 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);