2 This file is part of GNUnet.
3 (C) 2001 - 2011 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?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
42 * - relay corking down to core
43 * - set ttl relative to tree depth
44 * - Add data ACK count in path ACK
45 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
51 #include "mesh_protocol.h"
52 #include "mesh_tunnel_tree.h"
53 #include "block_mesh.h"
54 #include "mesh_block_lib.h"
55 #include "gnunet_dht_service.h"
56 #include "gnunet_statistics_service.h"
57 #include "gnunet_regex_lib.h"
59 #define MESH_BLOOM_SIZE 128
61 #define MESH_DEBUG_DHT GNUNET_NO
62 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_YES
65 #if MESH_DEBUG_CONNECTION
66 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
68 #define DEBUG_CONN(...)
72 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
74 #define DEBUG_DHT(...)
81 struct timespec __mesh_start;
82 struct timespec __mesh_end;
83 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
84 #define INTERVAL_END \
86 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
87 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
88 if (__diff < 0) __diff += 1000000000;\
92 #define INTERVAL_SHOW \
94 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
96 #define INTERVAL_START
101 /******************************************************************************/
102 /************************ DATA STRUCTURES ****************************/
103 /******************************************************************************/
105 /** FWD declaration */
111 * Struct representing a piece of data being sent to other peers
115 /** Tunnel it belongs to. */
116 struct MeshTunnel *t;
118 /** In case of a multicast, task to allow a client to send more data if
119 * some neighbor is too slow. */
120 GNUNET_SCHEDULER_TaskIdentifier *task;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int *reference_counter;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct containing all info possibly needed to build a package when called
179 struct MeshTransmissionDescriptor
181 /** ID of the tunnel this packet travels in */
182 struct MESH_TunnelID *origin;
184 /** Who was this message being sent to */
185 struct MeshPeerInfo *peer;
187 /** Ultimate destination of the packet */
188 GNUNET_PEER_Id destination;
190 /** Data descriptor */
191 struct MeshData* mesh_data;
196 * Struct containing all information regarding a given peer
206 * Last time we heard from this peer
208 struct GNUNET_TIME_Absolute last_contact;
211 * Number of attempts to reconnect so far
213 int n_reconnect_attempts;
216 * Paths to reach the peer, ordered by ascending hop count
218 struct MeshPeerPath *path_head;
221 * Paths to reach the peer, ordered by ascending hop count
223 struct MeshPeerPath *path_tail;
226 * Handle to stop the DHT search for a path to this peer
228 struct GNUNET_DHT_GetHandle *dhtget;
231 * Closure given to the DHT GET
233 struct MeshPathInfo *dhtgetcls;
236 * Array of tunnels this peer participates in
237 * (most probably a small amount, therefore not a hashmap)
238 * When the path to the peer changes, notify these tunnels to let them
239 * re-adjust their path trees.
241 struct MeshTunnel **tunnels;
244 * Number of tunnels this peers participates in
246 unsigned int ntunnels;
249 * Transmission queue to core DLL head
251 struct MeshPeerQueue *queue_head;
254 * Transmission queue to core DLL tail
256 struct MeshPeerQueue *queue_tail;
259 * How many messages are in the queue to this peer.
261 unsigned int queue_n;
264 * Handle to for queued transmissions
266 struct GNUNET_CORE_TransmitHandle *core_transmit;
271 * Globally unique tunnel identification (owner + number)
272 * DO NOT USE OVER THE NETWORK
277 * Node that owns the tunnel
282 * Tunnel number to differentiate all the tunnels owned by the node oid
283 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
285 MESH_TunnelNumber tid;
290 * Struct containing all information regarding a tunnel
291 * For an intermediate node the improtant info used will be:
292 * - id Tunnel unique identification
293 * - paths[0] To know where to send it next
294 * - metainfo: ready, speeds, accounting
301 struct MESH_TunnelID id;
304 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
306 MESH_TunnelNumber local_tid;
309 * Local tunnel number for local destination clients (incoming number)
310 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
313 MESH_TunnelNumber local_tid_dest;
316 * Is the speed on the tunnel limited to the slowest peer?
321 * Is the tunnel bufferless (minimum latency)?
326 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
331 * Packet ID of the last bck packet sent (unique counter per hop).
336 * SKIP value for this tunnel.
341 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
342 * Contains the Flow Control info: FWD ACK value received,
343 * last BCK ACK sent, PID and SKIP values.
345 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
348 * Last ACK sent towards the origin (for traffic towards leaf node).
350 uint32_t last_fwd_ack;
353 * BCK ACK value received from the hop towards the owner of the tunnel,
354 * (previous node / owner): up to what message PID can we sent back to him.
359 * How many messages are in the forward queue (towards leaves).
361 unsigned int fwd_queue_n;
364 * How many messages do we accept in the forward queue.
366 unsigned int fwd_queue_max;
369 * How many messages are in the backward queue (towards origin).
371 unsigned int bck_queue_n;
374 * How many messages do we accept in the backward queue.
376 unsigned int bck_queue_max;
379 * Last time the tunnel was used
381 struct GNUNET_TIME_Absolute timestamp;
384 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
385 * containing peers added by id or by type, not intermediate peers.
387 struct GNUNET_CONTAINER_MultiHashMap *peers;
390 * Number of peers that are connected and potentially ready to receive data
392 unsigned int peers_ready;
395 * Number of peers that have been added to the tunnel
397 unsigned int peers_total;
400 * Client owner of the tunnel, if any
402 struct MeshClient *owner;
405 * Clients that have been informed about and want to stay in the tunnel.
407 struct MeshClient **clients;
410 * Flow control info for each client.
412 struct MeshTunnelClientInfo *clients_fc;
415 * Number of elements in clients/clients_fc
417 unsigned int nclients;
420 * Clients that have been informed but requested to leave the tunnel.
422 struct MeshClient **ignore;
425 * Number of elements in clients
427 unsigned int nignore;
432 GNUNET_PEER_Id *blacklisted;
435 * Number of elements in blacklisted
437 unsigned int nblacklisted;
440 * Bloomfilter (for peer identities) to stop circular routes
442 char bloomfilter[MESH_BLOOM_SIZE];
447 struct MeshTunnelTree *tree;
450 * Application type we are looking for in this tunnel
452 GNUNET_MESH_ApplicationType type;
455 * Used to search peers offering a service
457 struct GNUNET_DHT_GetHandle *dht_get_type;
460 * Initial context of the regex search for a connect_by_string
462 struct MeshRegexSearchContext *regex_ctx;
465 * Task to keep the used paths alive
467 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
470 * Task to destroy the tunnel after timeout
472 * FIXME: merge the two? a tunnel will have either
473 * a path refresh OR a timeout, never both!
475 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
478 * Flag to signal the destruction of the tunnel.
479 * If this is set GNUNET_YES the tunnel will be destroyed
480 * when the queue is empty.
487 * Info about a child node in a tunnel, needed to perform flow control.
489 struct MeshTunnelChildInfo
492 * ID of the child node.
507 * Maximum PID allowed (FWD ACK received).
512 * Last ACK sent to that child (BCK ACK).
519 * Info about a leaf client of a tunnel, needed to perform flow control.
521 struct MeshTunnelClientInfo
524 * PID of the last packet sent to the client (FWD).
529 * PID of the last packet received from the client (BCK).
534 * Maximum PID allowed (FWD ACK received).
539 * Last ACK sent to that child (BCK ACK).
547 * Info collected during iteration of child nodes in order to get the ACK value
550 struct MeshTunnelChildIteratorContext
553 * Tunnel whose info is being collected.
555 struct MeshTunnel *t;
558 * Is this context initialized? Is the value in max_child_ack valid?
563 * Maximum child ACK so far.
565 uint32_t max_child_ack;
568 * Number of children nodes
570 unsigned int nchildren;
575 * Info needed to work with tunnel paths and peers
582 struct MeshTunnel *t;
585 * Neighbouring peer to whom we send the packet to
587 struct MeshPeerInfo *peer;
592 struct MeshPeerPath *path;
597 * Struct containing information about a client of the service
604 struct MeshClient *next;
609 struct MeshClient *prev;
612 * Tunnels that belong to this client, indexed by local id
614 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
617 * Tunnels this client has accepted, indexed by incoming local id
619 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
622 * Tunnels this client has rejected, indexed by incoming local id
624 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
626 * Handle to communicate with the client
628 struct GNUNET_SERVER_Client *handle;
631 * Applications that this client has claimed to provide
633 struct GNUNET_CONTAINER_MultiHashMap *apps;
636 * Messages that this client has declared interest in
638 struct GNUNET_CONTAINER_MultiHashMap *types;
641 * Whether the client is active or shutting down (don't send confirmations
642 * to a client that is shutting down.
647 * ID of the client, mainly for debug messages
652 * Regular expressions describing the services offered by this client.
654 char **regexes; // FIXME add timeout? API to remove a regex?
657 * Number of regular expressions in regexes.
659 unsigned int n_regex;
662 * Task to refresh all regular expresions in the DHT.
664 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
670 * Struct to keep information of searches of services described by a regex
671 * using a user-provided string service description.
673 struct MeshRegexSearchInfo
676 * Which tunnel is this for
678 struct MeshTunnel *t;
681 * User provided description of the searched service.
686 * Part of the description already consumed by the search.
693 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
696 * Results from running DHT GETs.
698 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
701 * Contexts, for each running DHT GET. Free all on end of search.
703 struct MeshRegexSearchContext **contexts;
706 * Number of contexts (branches/steps in search).
708 unsigned int n_contexts;
711 * Peer that is connecting via connect_by_string. When connected, free ctx.
716 * Other peers that are found but not yet being connected to.
718 GNUNET_PEER_Id *peers;
721 * Number of elements in peers.
723 unsigned int n_peers;
726 * Next peer to try to connect to.
731 * Timeout for a connect attempt.
732 * When reached, try to connect to a different peer, if any. If not,
733 * try the same peer again.
735 GNUNET_SCHEDULER_TaskIdentifier timeout;
740 * Struct to keep state of running searches that have consumed a part of
743 struct MeshRegexSearchContext
746 * Part of the description already consumed by
747 * this particular search branch.
752 * Information about the search.
754 struct MeshRegexSearchInfo *info;
758 /******************************************************************************/
759 /************************ DEBUG FUNCTIONS ****************************/
760 /******************************************************************************/
764 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
765 * @param cls string to print
766 * @param success GNUNET_OK if the PUT was transmitted,
767 * GNUNET_NO on timeout,
768 * GNUNET_SYSERR on disconnect from service
769 * after the PUT message was transmitted
770 * (so we don't know if it was received or not)
775 mesh_debug (void *cls, int success)
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
785 /******************************************************************************/
786 /*********************** GLOBAL VARIABLES ****************************/
787 /******************************************************************************/
790 * Configuration parameters
792 static struct GNUNET_TIME_Relative refresh_path_time;
793 static struct GNUNET_TIME_Relative app_announce_time;
794 static struct GNUNET_TIME_Relative id_announce_time;
795 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
796 static struct GNUNET_TIME_Relative connect_timeout;
797 static long long unsigned int default_ttl;
798 static long long unsigned int dht_replication_level;
799 static long long unsigned int max_tunnels;
800 static long long unsigned int max_msgs_queue;
803 * DLL with all the clients, head.
805 static struct MeshClient *clients;
808 * DLL with all the clients, tail.
810 static struct MeshClient *clients_tail;
813 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
815 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
818 * Number of tunnels known.
820 static unsigned long long n_tunnels;
823 * Tunnels incoming, indexed by MESH_TunnelNumber
824 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
826 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
829 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
831 static struct GNUNET_CONTAINER_MultiHashMap *peers;
834 * Handle to communicate with transport
836 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
839 * Handle to communicate with core.
841 static struct GNUNET_CORE_Handle *core_handle;
846 static struct GNUNET_DHT_Handle *dht_handle;
851 static struct GNUNET_SERVER_Handle *server_handle;
854 * Handle to the statistics service.
856 static struct GNUNET_STATISTICS_Handle *stats;
859 * Notification context, to send messages to local clients.
861 static struct GNUNET_SERVER_NotificationContext *nc;
864 * Local peer own ID (memory efficient handle).
866 static GNUNET_PEER_Id myid;
869 * Local peer own ID (full value).
871 static struct GNUNET_PeerIdentity my_full_id;
876 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
881 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
884 * Tunnel ID for the next created tunnel (global tunnel number).
886 static MESH_TunnelNumber next_tid;
889 * Tunnel ID for the next incoming tunnel (local tunnel number).
891 static MESH_TunnelNumber next_local_tid;
894 * All application types provided by this peer.
896 static struct GNUNET_CONTAINER_MultiHashMap *applications;
899 * All message types clients of this peer are interested in.
901 static struct GNUNET_CONTAINER_MultiHashMap *types;
904 * Task to periodically announce provided applications.
906 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
909 * Task to periodically announce itself in the network.
911 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
914 * Next ID to assign to a client.
916 unsigned int next_client_id;
919 /******************************************************************************/
920 /*********************** DECLARATIONS **************************/
921 /******************************************************************************/
923 /* FIXME move declarations here */
926 * Function to process paths received for a new peer addition. The recorded
927 * paths form the initial tunnel, which can be optimized later.
928 * Called on each result obtained for the DHT search.
931 * @param exp when will this value expire
932 * @param key key of the result
933 * @param type type of the result
934 * @param size number of bytes in data
935 * @param data pointer to the result data
938 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
939 const struct GNUNET_HashCode * key,
940 const struct GNUNET_PeerIdentity *get_path,
941 unsigned int get_path_length,
942 const struct GNUNET_PeerIdentity *put_path,
943 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
944 size_t size, const void *data);
948 * Function to process DHT string to regex matching.
949 * Called on each result obtained for the DHT search.
951 * @param cls closure (search context)
952 * @param exp when will this value expire
953 * @param key key of the result
954 * @param get_path path of the get request (not used)
955 * @param get_path_length lenght of get_path (not used)
956 * @param put_path path of the put request (not used)
957 * @param put_path_length length of the put_path (not used)
958 * @param type type of the result
959 * @param size number of bytes in data
960 * @param data pointer to the result data
962 * TODO: re-issue the request after certain time? cancel after X results?
965 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
966 const struct GNUNET_HashCode * key,
967 const struct GNUNET_PeerIdentity *get_path,
968 unsigned int get_path_length,
969 const struct GNUNET_PeerIdentity *put_path,
970 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
971 size_t size, const void *data);
975 * Function to process DHT string to regex matching.
976 * Called on each result obtained for the DHT search.
978 * @param cls closure (search context)
979 * @param exp when will this value expire
980 * @param key key of the result
981 * @param get_path path of the get request (not used)
982 * @param get_path_length lenght of get_path (not used)
983 * @param put_path path of the put request (not used)
984 * @param put_path_length length of the put_path (not used)
985 * @param type type of the result
986 * @param size number of bytes in data
987 * @param data pointer to the result data
990 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
991 const struct GNUNET_HashCode * key,
992 const struct GNUNET_PeerIdentity *get_path,
993 unsigned int get_path_length,
994 const struct GNUNET_PeerIdentity *put_path,
995 unsigned int put_path_length,
996 enum GNUNET_BLOCK_Type type,
997 size_t size, const void *data);
1001 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1002 * and insert it in the appropiate structures if the peer is not known yet.
1004 * @param peer Short identity of the peer.
1006 * @return Existing or newly created peer info.
1008 static struct MeshPeerInfo *
1009 peer_info_get_short (const GNUNET_PEER_Id peer);
1013 * Try to establish a new connection to this peer.
1014 * Use the best path for the given tunnel.
1015 * If the peer doesn't have any path to it yet, try to get one.
1016 * If the peer already has some path, send a CREATE PATH towards it.
1018 * @param peer PeerInfo of the peer.
1019 * @param t Tunnel for which to create the path, if possible.
1022 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1026 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1027 * needed rescources.
1028 * If peer already exists, reevaluate shortest path and change if different.
1030 * @param t Tunnel we want to add a new peer to
1031 * @param peer PeerInfo of the peer being added
1035 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1039 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1040 * that are no longer needed, as well as nodes of no longer reachable peers.
1041 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1043 * @param t Tunnel from which to remove the path.
1044 * @param peer Short id of the peer which should be removed.
1047 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1051 * Search for a tunnel by global ID using full PeerIdentities.
1053 * @param oid owner of the tunnel.
1054 * @param tid global tunnel number.
1056 * @return tunnel handler, NULL if doesn't exist.
1058 static struct MeshTunnel *
1059 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1063 * Delete an active client from the tunnel.
1069 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1072 * Notify a tunnel that a connection has broken that affects at least
1073 * some of its peers.
1075 * @param t Tunnel affected.
1076 * @param p1 Peer that got disconnected from p2.
1077 * @param p2 Peer that got disconnected from p1.
1079 * @return Short ID of the peer disconnected (either p1 or p2).
1080 * 0 if the tunnel remained unaffected.
1082 static GNUNET_PEER_Id
1083 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1088 * Get the current ack value for a tunnel, for data going from root to leaves,
1089 * taking in account the tunnel mode and the status of all children and clients.
1093 * @return Maximum PID allowed.
1096 tunnel_get_fwd_ack (struct MeshTunnel *t);
1100 * Add a client to a tunnel, initializing all needed data structures.
1102 * @param t Tunnel to which add the client.
1103 * @param c Client which to add to the tunnel.
1106 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1110 * Iterator over edges in a regex block retrieved from the DHT.
1112 * @param cls Closure.
1113 * @param token Token that follows to next state.
1114 * @param len Lenght of token.
1115 * @param key Hash of next state.
1117 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1120 regex_edge_iterator (void *cls,
1123 const struct GNUNET_HashCode *key);
1127 * Find a path to a peer that offers a regex servcie compatible
1128 * with a given string.
1130 * @param key The key of the accepting state.
1131 * @param ctx Context containing info about the string, tunnel, etc.
1134 regex_find_path (const struct GNUNET_HashCode *key,
1135 struct MeshRegexSearchContext *ctx);
1139 * Queue and pass message to core when possible.
1141 * @param cls Closure (type dependant).
1142 * @param type Type of the message, 0 for a raw message.
1143 * @param size Size of the message.
1144 * @param dst Neighbor to send message to.
1145 * @param t Tunnel this message belongs to.
1148 queue_add (void *cls, uint16_t type, size_t size,
1149 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1152 * Free a transmission that was already queued with all resources
1153 * associated to the request.
1155 * @param queue Queue handler to cancel.
1156 * @param clear_cls Is it necessary to free associated cls?
1159 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1161 /******************************************************************************/
1162 /************************ ITERATORS ****************************/
1163 /******************************************************************************/
1166 * Iterator over found existing mesh regex blocks that match an ongoing search.
1168 * @param cls closure
1169 * @param key current key code
1170 * @param value value in the hash map
1171 * @return GNUNET_YES if we should continue to iterate,
1175 regex_result_iterator (void *cls,
1176 const struct GNUNET_HashCode * key,
1179 struct MeshRegexBlock *block = value;
1180 struct MeshRegexSearchContext *ctx = cls;
1182 if (GNUNET_YES == ntohl(block->accepting) &&
1183 ctx->position == strlen (ctx->info->description))
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1186 regex_find_path (key, ctx);
1187 return GNUNET_YES; // We found an accept state!
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1192 ctx->position, strlen(ctx->info->description),
1193 ntohl(block->accepting));
1196 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1197 ®ex_edge_iterator, ctx);
1204 * Iterator over edges in a regex block retrieved from the DHT.
1206 * @param cls Closure (context of the search).
1207 * @param token Token that follows to next state.
1208 * @param len Lenght of token.
1209 * @param key Hash of next state.
1211 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1214 regex_edge_iterator (void *cls,
1217 const struct GNUNET_HashCode *key)
1219 struct MeshRegexSearchContext *ctx = cls;
1220 struct MeshRegexSearchContext *new_ctx;
1221 struct MeshRegexSearchInfo *info = ctx->info;
1222 struct GNUNET_DHT_GetHandle *get_h;
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1229 current = &info->description[ctx->position];
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1231 current_len = strlen (info->description) - ctx->position;
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1236 if (len > current_len)
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1239 return GNUNET_YES; // Token too long, wont match
1241 if (0 != strncmp (current, token, len))
1243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1244 return GNUNET_YES; // Token doesn't match
1246 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1247 new_ctx->info = info;
1248 new_ctx->position = ctx->position + len;
1249 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1251 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1254 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1255 ®ex_result_iterator,
1257 return GNUNET_YES; // We are already looking for it
1259 /* Start search in DHT */
1261 GNUNET_DHT_get_start (dht_handle, /* handle */
1262 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1263 key, /* key to search */
1264 dht_replication_level, /* replication level */
1265 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1266 NULL, /* xquery */ // FIXME BLOOMFILTER
1267 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1268 &dht_get_string_handler, new_ctx);
1270 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1271 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1281 * Iterator over hash map entries to cancel DHT GET requests after a
1282 * successful connect_by_string.
1284 * @param cls Closure (unused).
1285 * @param key Current key code (unused).
1286 * @param value Value in the hash map (get handle).
1287 * @return GNUNET_YES if we should continue to iterate,
1291 regex_cancel_dht_get (void *cls,
1292 const struct GNUNET_HashCode * key,
1295 struct GNUNET_DHT_GetHandle *h = value;
1297 GNUNET_DHT_get_stop (h);
1303 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1304 * search for connect_by_string.
1306 * @param cls Closure (unused).
1307 * @param key Current key code (unused).
1308 * @param value MeshRegexBlock in the hash map.
1309 * @return GNUNET_YES if we should continue to iterate,
1313 regex_free_result (void *cls,
1314 const struct GNUNET_HashCode * key,
1318 GNUNET_free (value);
1324 * Regex callback iterator to store own service description in the DHT.
1326 * @param cls closure.
1327 * @param key hash for current state.
1328 * @param proof proof for current state.
1329 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1330 * @param num_edges number of edges leaving current state.
1331 * @param edges edges leaving current state.
1334 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1335 int accepting, unsigned int num_edges,
1336 const struct GNUNET_REGEX_Edge *edges)
1338 struct MeshRegexBlock *block;
1339 struct MeshRegexEdge *block_edge;
1340 enum GNUNET_DHT_RouteOption opt;
1344 unsigned int offset;
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 " regex dht put for state %s\n",
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1358 if (GNUNET_YES == accepting)
1360 struct MeshRegexAccept block;
1362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1363 " state %s is accepting, putting own id\n",
1365 size = sizeof (block);
1367 block.id = my_full_id;
1369 GNUNET_DHT_put(dht_handle, key,
1370 dht_replication_level,
1371 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1372 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1375 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1380 len = strlen(proof);
1381 size = sizeof (struct MeshRegexBlock) + len;
1382 block = GNUNET_malloc (size);
1385 block->n_proof = htonl (len);
1386 block->n_edges = htonl (num_edges);
1387 block->accepting = htonl (accepting);
1389 /* Store the proof at the end of the block. */
1390 aux = (char *) &block[1];
1391 memcpy (aux, proof, len);
1394 /* Store each edge in a variable length MeshEdge struct at the
1395 * very end of the MeshRegexBlock structure.
1397 for (i = 0; i < num_edges; i++)
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 " edge %s towards %s\n",
1402 GNUNET_h2s(&edges[i].destination));
1404 /* aux points at the end of the last block */
1405 len = strlen (edges[i].label);
1406 size += sizeof (struct MeshRegexEdge) + len;
1407 // Calculate offset FIXME is this ok? use size instead?
1408 offset = aux - (char *) block;
1409 block = GNUNET_realloc (block, size);
1410 aux = &((char *) block)[offset];
1411 block_edge = (struct MeshRegexEdge *) aux;
1412 block_edge->key = edges[i].destination;
1413 block_edge->n_token = htonl (len);
1414 aux = (char *) &block_edge[1];
1415 memcpy (aux, edges[i].label, len);
1419 GNUNET_DHT_put(dht_handle, key,
1420 dht_replication_level,
1422 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1424 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1428 GNUNET_free (block);
1433 * Store the regular expression describing a local service into the DHT.
1435 * @param regex The regular expresion.
1438 regex_put (const char *regex)
1440 struct GNUNET_REGEX_Automaton *dfa;
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1443 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1444 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1445 GNUNET_REGEX_automaton_destroy (dfa);
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1451 * Find a path to a peer that offers a regex servcie compatible
1452 * with a given string.
1454 * @param key The key of the accepting state.
1455 * @param ctx Context containing info about the string, tunnel, etc.
1458 regex_find_path (const struct GNUNET_HashCode *key,
1459 struct MeshRegexSearchContext *ctx)
1461 struct GNUNET_DHT_GetHandle *get_h;
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1464 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1465 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1466 key, /* key to search */
1467 dht_replication_level, /* replication level */
1468 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1469 GNUNET_DHT_RO_RECORD_ROUTE,
1470 NULL, /* xquery */ // FIXME BLOOMFILTER
1471 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1472 &dht_get_string_accept_handler, ctx);
1473 GNUNET_break (GNUNET_OK ==
1474 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1477 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1482 * Function called if the connect attempt to a peer found via
1483 * connect_by_string times out. Try to connect to another peer, if any.
1484 * Otherwise try to reconnect to the same peer.
1486 * @param cls Closure (info about regex search).
1487 * @param tc TaskContext.
1490 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1492 struct MeshRegexSearchInfo *info = cls;
1493 struct MeshPeerInfo *peer_info;
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1498 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1499 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1507 if (0 < info->n_peers)
1509 // Select next peer, put current in that spot.
1510 id = info->peers[info->i_peer];
1511 info->peers[info->i_peer] = info->peer;
1512 info->i_peer = (info->i_peer + 1) % info->n_peers;
1516 // Try to connect to same peer again.
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1521 peer_info = peer_info_get_short(id);
1522 tunnel_add_peer (info->t, peer_info);
1524 tunnel_delete_peer (info->t, old);
1525 peer_info_connect (peer_info, info->t);
1526 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1527 ®ex_connect_timeout,
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1534 * Cancel an ongoing regex search in the DHT and free all resources.
1536 * @param ctx The search context.
1539 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1541 struct MeshRegexSearchInfo *info = ctx->info;
1544 GNUNET_free (info->description);
1545 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1546 ®ex_cancel_dht_get, NULL);
1547 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1548 ®ex_free_result, NULL);
1549 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1550 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1551 info->t->regex_ctx = NULL;
1552 for (i = 0; i < info->n_contexts; i++)
1554 GNUNET_free (info->contexts[i]);
1556 if (0 < info->n_contexts)
1557 GNUNET_free (info->contexts);
1558 if (0 < info->n_peers)
1559 GNUNET_free (info->peers);
1560 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1562 GNUNET_SCHEDULER_cancel(info->timeout);
1568 /******************************************************************************/
1569 /************************ PERIODIC FUNCTIONS ****************************/
1570 /******************************************************************************/
1573 * Announce iterator over for each application provided by the peer
1575 * @param cls closure
1576 * @param key current key code
1577 * @param value value in the hash map
1578 * @return GNUNET_YES if we should continue to
1583 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1585 struct PBlock block;
1586 struct MeshClient *c;
1588 block.id = my_full_id;
1589 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1590 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1591 if (0 == block.type)
1596 block.type = htonl (block.type);
1598 GNUNET_break (NULL !=
1599 GNUNET_DHT_put (dht_handle, key,
1600 dht_replication_level,
1601 GNUNET_DHT_RO_RECORD_ROUTE |
1602 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1603 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1605 (const char *) &block,
1606 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1608 app_announce_time, NULL, NULL));
1614 * Periodically announce what applications are provided by local clients
1617 * @param cls closure
1618 * @param tc task context
1621 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1623 struct MeshClient *c = cls;
1626 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1628 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1632 DEBUG_DHT ("Starting PUT for regex\n");
1634 for (i = 0; i < c->n_regex; i++)
1636 regex_put (c->regexes[i]);
1638 c->regex_announce_task =
1639 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1640 DEBUG_DHT ("Finished PUT for regex\n");
1647 * Periodically announce what applications are provided by local clients
1650 * @param cls closure
1651 * @param tc task context
1654 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1656 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1658 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1662 DEBUG_DHT ("Starting PUT for apps\n");
1664 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1666 announce_applications_task =
1667 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1669 DEBUG_DHT ("Finished PUT for apps\n");
1676 * Periodically announce self id in the DHT
1678 * @param cls closure
1679 * @param tc task context
1682 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1684 struct PBlock block;
1686 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1688 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1692 * - Set data expiration in function of X
1693 * - Adapt X to churn
1695 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1697 block.id = my_full_id;
1698 block.type = htonl (0);
1699 GNUNET_DHT_put (dht_handle, /* DHT handle */
1700 &my_full_id.hashPubKey, /* Key to use */
1701 dht_replication_level, /* Replication level */
1702 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1703 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1704 sizeof (block), /* Size of the data */
1705 (const char *) &block, /* Data itself */
1706 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1707 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1708 NULL, /* Continuation */
1709 NULL); /* Continuation closure */
1711 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1715 /******************************************************************************/
1716 /****************** GENERAL HELPER FUNCTIONS ************************/
1717 /******************************************************************************/
1720 * Decrements the reference counter and frees all resources if needed
1722 * @param mesh_data Data Descriptor used in a multicast message.
1723 * Freed no longer needed (last message).
1726 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1728 /* Make sure it's a multicast packet */
1729 GNUNET_assert (NULL != mesh_data->reference_counter);
1731 if (0 == --(*(mesh_data->reference_counter)))
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1734 if (NULL != mesh_data->task)
1736 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1738 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1740 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1742 GNUNET_free (mesh_data->task);
1744 GNUNET_free (mesh_data->reference_counter);
1745 GNUNET_free (mesh_data->data);
1746 GNUNET_free (mesh_data);
1752 * Check if client has registered with the service and has not disconnected
1754 * @param client the client to check
1756 * @return non-NULL if client exists in the global DLL
1758 static struct MeshClient *
1759 client_get (struct GNUNET_SERVER_Client *client)
1761 struct MeshClient *c;
1766 if (c->handle == client)
1775 * Checks if a given client has subscribed to certain message type
1777 * @param message_type Type of message to check
1778 * @param c Client to check
1780 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1782 * FIXME: use of crypto_hash slows it down
1783 * The hash function alone takes 8-10us out of the ~55us for the whole
1784 * process of retransmitting the message from one local client to another.
1785 * Find faster implementation!
1788 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1790 struct GNUNET_HashCode hc;
1792 if (NULL == c->types)
1795 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1796 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1801 * Allow a client to send more data after transmitting a multicast message
1802 * which some neighbor has not yet accepted altough a reasonable time has
1805 * @param cls Closure (DataDescriptor containing the task identifier)
1806 * @param tc Task Context
1808 * FIXME reference counter cshould be just int
1811 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1813 struct MeshData *mdata = cls;
1815 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1817 GNUNET_assert (NULL != mdata->reference_counter);
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1820 *(mdata->reference_counter));
1821 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1822 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1827 * Check whether client wants traffic from a tunnel.
1829 * @param c Client to check.
1830 * @param t Tunnel to be found.
1832 * @return GNUNET_YES if client knows tunnel.
1834 * TODO look in client hashmap
1837 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1841 for (i = 0; i < t->nclients; i++)
1842 if (t->clients[i] == c)
1849 * Check whether client has been informed about a tunnel.
1851 * @param c Client to check.
1852 * @param t Tunnel to be found.
1854 * @return GNUNET_YES if client knows tunnel.
1856 * TODO look in client hashmap
1859 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1863 for (i = 0; i < t->nignore; i++)
1864 if (t->ignore[i] == c)
1866 return client_wants_tunnel(c, t);
1871 * Marks a client as uninterested in traffic from the tunnel, updating both
1872 * client and tunnel to reflect this.
1874 * @param c Client that doesn't want traffic anymore.
1875 * @param t Tunnel which should be ignored.
1877 * FIXME when to delete an incoming tunnel?
1880 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1882 struct GNUNET_HashCode hash;
1884 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1885 GNUNET_break (GNUNET_YES ==
1886 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1888 GNUNET_break (GNUNET_YES ==
1889 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1890 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1891 tunnel_delete_active_client (t, c);
1892 GNUNET_array_append (t->ignore, t->nignore, c);
1897 * Deletes a tunnel from a client (either owner or destination). To be used on
1898 * tunnel destroy, otherwise, use client_ignore_tunnel.
1900 * @param c Client whose tunnel to delete.
1901 * @param t Tunnel which should be deleted.
1904 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1906 struct GNUNET_HashCode hash;
1910 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1911 GNUNET_assert (GNUNET_YES ==
1912 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1918 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1920 GNUNET_assert (GNUNET_YES ==
1921 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1925 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1933 * Send the message to all clients that have subscribed to its type
1935 * @param msg Pointer to the message itself
1936 * @param payload Pointer to the payload of the message.
1937 * @param t The tunnel to whose clients this message goes.
1939 * @return number of clients this message was sent to
1942 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1943 const struct GNUNET_MessageHeader *payload,
1944 struct MeshTunnel *t)
1946 struct MeshClient *c;
1947 MESH_TunnelNumber *tid;
1950 char cbuf[htons (msg->size)];
1952 type = ntohs (payload->type);
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1955 GNUNET_MESH_DEBUG_M2S (type));
1957 memcpy (cbuf, msg, sizeof (cbuf));
1958 switch (htons (msg->type))
1960 struct GNUNET_MESH_Unicast *uc;
1961 struct GNUNET_MESH_Multicast *mc;
1962 struct GNUNET_MESH_ToOrigin *to;
1964 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1965 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1968 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1969 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1972 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1973 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1981 for (count = 0, c = clients; c != NULL; c = c->next)
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1984 if (client_is_subscribed (type, c))
1986 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1990 *tid = htonl (t->local_tid);
1994 if (GNUNET_NO == client_knows_tunnel (c, t))
1996 /* This client doesn't know the tunnel */
1997 struct GNUNET_MESH_TunnelNotification tmsg;
1998 struct GNUNET_HashCode hash;
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2001 tmsg.header.size = htons (sizeof (tmsg));
2002 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2003 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2004 tmsg.tunnel_id = htonl (t->local_tid_dest);
2005 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2006 &tmsg.header, GNUNET_NO);
2007 tunnel_add_client (t, c);
2008 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2010 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2011 c->incoming_tunnels, &hash, t,
2012 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2014 *tid = htonl (t->local_tid_dest);
2017 /* Check if the client wants to get traffic from the tunnel */
2018 if (GNUNET_NO == client_wants_tunnel(c, t))
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2022 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2023 (struct GNUNET_MessageHeader
2024 *) cbuf, GNUNET_NO);
2033 * Notify the client that owns the tunnel that a peer has connected to it
2034 * (the requested path to it has been confirmed).
2036 * @param t Tunnel whose owner to notify
2037 * @param id Short id of the peer that has connected
2040 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2042 struct GNUNET_MESH_PeerControl pc;
2044 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2045 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2046 pc.tunnel_id = htonl (t->local_tid);
2047 GNUNET_PEER_resolve (id, &pc.peer);
2048 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2054 * Notify all clients (not depending on registration status) that the incoming
2055 * tunnel is no longer valid.
2057 * @param t Tunnel that was destroyed.
2060 send_clients_tunnel_destroy (struct MeshTunnel *t)
2062 struct GNUNET_MESH_TunnelMessage msg;
2064 msg.header.size = htons (sizeof (msg));
2065 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2066 msg.tunnel_id = htonl (t->local_tid_dest);
2067 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2072 * Notify clients of tunnel disconnections, if needed.
2073 * In case the origin disconnects, the destination clients get a tunnel destroy
2074 * notification. If the last destination disconnects (only one remaining client
2075 * in tunnel), the origin gets a (local ID) peer disconnected.
2076 * Note that the function must be called BEFORE removing the client from
2079 * @param t Tunnel that was destroyed.
2080 * @param c Client that disconnected.
2083 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2089 struct GNUNET_MESH_TunnelMessage msg;
2091 msg.header.size = htons (sizeof (msg));
2092 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2093 msg.tunnel_id = htonl (t->local_tid_dest);
2094 for (i = 0; i < t->nclients; i++)
2095 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2096 &msg.header, GNUNET_NO);
2098 // FIXME when to disconnect an incoming tunnel?
2099 else if (1 == t->nclients && NULL != t->owner)
2101 struct GNUNET_MESH_PeerControl msg;
2103 msg.header.size = htons (sizeof (msg));
2104 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2105 msg.tunnel_id = htonl (t->local_tid);
2106 msg.peer = my_full_id;
2107 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2108 &msg.header, GNUNET_NO);
2114 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2115 * and insert it in the appropiate structures if the peer is not known yet.
2117 * @param peer Full identity of the peer.
2119 * @return Existing or newly created peer info.
2121 static struct MeshPeerInfo *
2122 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2124 struct MeshPeerInfo *peer_info;
2126 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2127 if (NULL == peer_info)
2130 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2131 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2132 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2133 peer_info->id = GNUNET_PEER_intern (peer);
2141 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2142 * and insert it in the appropiate structures if the peer is not known yet.
2144 * @param peer Short identity of the peer.
2146 * @return Existing or newly created peer info.
2148 static struct MeshPeerInfo *
2149 peer_info_get_short (const GNUNET_PEER_Id peer)
2151 struct GNUNET_PeerIdentity id;
2153 GNUNET_PEER_resolve (peer, &id);
2154 return peer_info_get (&id);
2159 * Iterator to remove the tunnel from the list of tunnels a peer participates
2162 * @param cls Closure (tunnel info)
2163 * @param key GNUNET_PeerIdentity of the peer (unused)
2164 * @param value PeerInfo of the peer
2166 * @return always GNUNET_YES, to keep iterating
2169 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2171 struct MeshTunnel *t = cls;
2172 struct MeshPeerInfo *peer = value;
2175 for (i = 0; i < peer->ntunnels; i++)
2178 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2181 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2182 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2191 * Core callback to write a pre-constructed data packet to core buffer
2193 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2194 * @param size Number of bytes available in buf.
2195 * @param buf Where the to write the message.
2197 * @return number of bytes written to buf
2200 send_core_data_raw (void *cls, size_t size, void *buf)
2202 struct MeshTransmissionDescriptor *info = cls;
2203 struct GNUNET_MessageHeader *msg;
2206 GNUNET_assert (NULL != info);
2207 GNUNET_assert (NULL != info->mesh_data);
2208 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2209 total_size = ntohs (msg->size);
2211 if (total_size > size)
2216 memcpy (buf, msg, total_size);
2217 data_descriptor_decrement_rc (info->mesh_data);
2224 * Sends an already built non-multicast message to a peer,
2225 * properly registrating all used resources.
2227 * @param message Message to send. Function makes a copy of it.
2228 * @param peer Short ID of the neighbor whom to send the message.
2229 * @param t Tunnel on which this message is transmitted.
2232 send_message (const struct GNUNET_MessageHeader *message,
2233 const struct GNUNET_PeerIdentity *peer,
2234 struct MeshTunnel *t)
2236 struct MeshTransmissionDescriptor *info;
2237 struct MeshPeerInfo *neighbor;
2238 struct MeshPeerPath *p;
2242 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2244 size = ntohs (message->size);
2245 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2246 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2247 info->mesh_data->data = GNUNET_malloc (size);
2248 memcpy (info->mesh_data->data, message, size);
2249 type = ntohs(message->type);
2250 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2252 struct GNUNET_MESH_Unicast *m;
2254 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2255 m->ttl = htonl (ntohl (m->ttl) - 1);
2257 info->mesh_data->data_len = size;
2258 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2259 *info->mesh_data->reference_counter = 1;
2260 neighbor = peer_info_get (peer);
2261 for (p = neighbor->path_head; NULL != p; p = p->next)
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 " %s IS NOT DIRECTLY CONNECTED\n",
2274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2277 for (p = neighbor->path_head; NULL != p; p = p->next)
2279 struct GNUNET_PeerIdentity debug_id;
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2283 " path with %u hops through:\n",
2285 for (i = 0; i < p->length; i++)
2287 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2290 i, GNUNET_i2s(&debug_id));
2294 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2295 GNUNET_free (info->mesh_data->data);
2296 GNUNET_free (info->mesh_data);
2300 info->peer = neighbor;
2301 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2304 * TODO: in this case we only need the service to retransmit
2305 * the message down the path. If we pass the real type to queue_add,
2306 * queue_send will try to build the message from scratch. This can
2307 * probably be done by some other way instead of deleteing the type
2321 * Sends a CREATE PATH message for a path to a peer, properly registrating
2322 * all used resources.
2324 * @param peer PeerInfo of the final peer for whom this path is being created.
2325 * @param p Path itself.
2326 * @param t Tunnel for which the path is created.
2329 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2330 struct MeshTunnel *t)
2332 struct GNUNET_PeerIdentity id;
2333 struct MeshPathInfo *path_info;
2334 struct MeshPeerInfo *neighbor;
2340 p = tree_get_path_to_peer (t->tree, peer->id);
2347 for (i = 0; i < p->length; i++)
2349 if (p->peers[i] == myid)
2352 if (i >= p->length - 1)
2358 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2360 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2361 path_info->path = p;
2363 neighbor = peer_info_get (&id);
2364 path_info->peer = neighbor;
2365 queue_add (path_info,
2366 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2367 sizeof (struct GNUNET_MESH_ManipulatePath) +
2368 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2375 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2377 * @param t Tunnel whose path to destroy.
2378 * @param destination Short ID of the peer to whom the path to destroy.
2381 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2383 struct MeshPeerPath *p;
2386 p = tree_get_path_to_peer (t->tree, destination);
2392 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2393 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2395 struct GNUNET_MESH_ManipulatePath *msg;
2396 struct GNUNET_PeerIdentity *pi;
2400 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2401 msg->header.size = htons (size);
2402 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2403 msg->tid = htonl (t->id.tid);
2404 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2405 for (i = 0; i < p->length; i++)
2407 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2409 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2416 * Try to establish a new connection to this peer.
2417 * Use the best path for the given tunnel.
2418 * If the peer doesn't have any path to it yet, try to get one.
2419 * If the peer already has some path, send a CREATE PATH towards it.
2421 * @param peer PeerInfo of the peer.
2422 * @param t Tunnel for which to create the path, if possible.
2425 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2427 struct MeshPeerPath *p;
2428 struct MeshPathInfo *path_info;
2430 if (NULL != peer->path_head)
2432 p = tree_get_path_to_peer (t->tree, peer->id);
2439 // FIXME always send create path to self
2442 send_create_path (peer, p, t);
2446 struct GNUNET_HashCode hash;
2449 send_client_peer_connected (t, myid);
2450 t->local_tid_dest = next_local_tid++;
2451 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2454 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2462 else if (NULL == peer->dhtget)
2464 struct GNUNET_PeerIdentity id;
2466 GNUNET_PEER_resolve (peer->id, &id);
2467 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2468 path_info->peer = peer;
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2472 peer->dhtgetcls = path_info;
2473 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2474 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2475 &id.hashPubKey, /* key to search */
2476 dht_replication_level, /* replication level */
2477 GNUNET_DHT_RO_RECORD_ROUTE |
2478 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2479 NULL, /* xquery */ // FIXME BLOOMFILTER
2480 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2481 &dht_get_id_handler, path_info);
2483 /* Otherwise, there is no path but the DHT get is already started. */
2488 * Task to delay the connection of a peer
2490 * @param cls Closure (path info with tunnel and peer to connect).
2491 * Will be free'd on exection.
2492 * @param tc TaskContext
2495 peer_info_connect_task (void *cls,
2496 const struct GNUNET_SCHEDULER_TaskContext *tc)
2498 struct MeshPathInfo *path_info = cls;
2500 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2505 peer_info_connect (path_info->peer, path_info->t);
2511 * Destroy the peer_info and free any allocated resources linked to it
2513 * @param pi The peer_info to destroy.
2515 * @return GNUNET_OK on success
2518 peer_info_destroy (struct MeshPeerInfo *pi)
2520 struct GNUNET_PeerIdentity id;
2521 struct MeshPeerPath *p;
2522 struct MeshPeerPath *nextp;
2524 GNUNET_PEER_resolve (pi->id, &id);
2525 GNUNET_PEER_change_rc (pi->id, -1);
2528 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2532 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2534 if (NULL != pi->dhtget)
2536 GNUNET_DHT_get_stop (pi->dhtget);
2537 GNUNET_free (pi->dhtgetcls);
2543 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2553 * Remove all paths that rely on a direct connection between p1 and p2
2554 * from the peer itself and notify all tunnels about it.
2556 * @param peer PeerInfo of affected peer.
2557 * @param p1 GNUNET_PEER_Id of one peer.
2558 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2561 * TODO: optimize (see below)
2564 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2567 struct MeshPeerPath *p;
2568 struct MeshPeerPath *aux;
2569 struct MeshPeerInfo *peer_d;
2571 unsigned int destroyed;
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2578 p = peer->path_head;
2582 for (i = 0; i < (p->length - 1); i++)
2584 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2585 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2587 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2598 for (i = 0; i < peer->ntunnels; i++)
2600 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2604 * Problem: one or more peers have been deleted from the tunnel tree.
2605 * We don't know who they are to try to add them again.
2606 * We need to try to find a new path for each of the disconnected peers.
2607 * Some of them might already have a path to reach them that does not
2608 * involve p1 and p2. Adding all anew might render in a better tree than
2609 * the trivial immediate fix.
2611 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2612 * its children will be reachable trough him.
2614 peer_d = peer_info_get_short (d);
2617 for (p = peer_d->path_head; NULL != p; p = p->next)
2619 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2627 /* No callback, as peer will be already disconnected and a connection
2628 * scheduled by tunnel_notify_connection_broken.
2630 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2634 peer_info_connect (peer_d, peer->tunnels[i]);
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2642 * Add the path to the peer and update the path used to reach it in case this
2645 * @param peer_info Destination peer to add the path to.
2646 * @param path New path to add. Last peer must be the peer in arg 1.
2647 * Path will be either used of freed if already known.
2648 * @param trusted Do we trust that this path is real?
2651 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2654 struct MeshPeerPath *aux;
2658 if ((NULL == peer_info) || (NULL == path))
2661 path_destroy (path);
2664 if (path->peers[path->length - 1] != peer_info->id)
2667 path_destroy (path);
2670 if (path->length <= 2 && GNUNET_NO == trusted)
2672 /* Only allow CORE to tell us about direct paths */
2673 path_destroy (path);
2676 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2677 for (l = 1; l < path->length; l++)
2679 if (path->peers[l] == myid)
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2682 for (l2 = 0; l2 < path->length - l; l2++)
2684 path->peers[l2] = path->peers[l + l2];
2689 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2694 struct GNUNET_PeerIdentity id;
2696 GNUNET_PEER_resolve (peer_info->id, &id);
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2698 path->length, GNUNET_i2s (&id));
2701 l = path_get_length (path);
2708 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2709 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2711 l2 = path_get_length (aux);
2714 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2715 peer_info->path_tail, aux, path);
2720 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2722 path_destroy (path);
2727 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2734 * Add the path to the origin peer and update the path used to reach it in case
2735 * this is the shortest.
2736 * The path is given in peer_info -> destination, therefore we turn the path
2737 * upside down first.
2739 * @param peer_info Peer to add the path to, being the origin of the path.
2740 * @param path New path to add after being inversed.
2741 * @param trusted Do we trust that this path is real?
2744 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2745 struct MeshPeerPath *path, int trusted)
2748 peer_info_add_path (peer_info, path, trusted);
2753 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2754 * to obtain a local peer -> destination path and interning the peer ids.
2756 * @return Newly allocated and created path
2758 static struct MeshPeerPath *
2759 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2760 unsigned int get_path_length,
2761 const struct GNUNET_PeerIdentity *put_path,
2762 unsigned int put_path_length)
2764 struct MeshPeerPath *p;
2770 GNUNET_PEER_change_rc (myid, 1);
2771 i = get_path_length;
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2773 for (i--; i >= 0; i--)
2775 id = GNUNET_PEER_intern (&get_path[i]);
2776 if (p->length > 0 && id == p->peers[p->length - 1])
2778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2779 GNUNET_PEER_change_rc (id, -1);
2783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2784 GNUNET_i2s (&get_path[i]));
2786 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2787 p->peers[p->length - 1] = id;
2790 i = put_path_length;
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2792 for (i--; i >= 0; i--)
2794 id = GNUNET_PEER_intern (&put_path[i]);
2797 /* PUT path went through us, so discard the path up until now and start
2798 * from here to get a much shorter (and loop-free) path.
2803 if (p->length > 0 && id == p->peers[p->length - 1])
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2806 GNUNET_PEER_change_rc (id, -1);
2810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2811 GNUNET_i2s (&put_path[i]));
2813 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2814 p->peers[p->length - 1] = id;
2818 if (get_path_length > 0)
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2820 GNUNET_i2s (&get_path[0]));
2821 if (put_path_length > 0)
2822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2823 GNUNET_i2s (&put_path[0]));
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2826 for (i = 0; i < p->length; i++)
2828 struct GNUNET_PeerIdentity peer_id;
2830 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2832 GNUNET_i2s (&peer_id));
2840 * Adds a path to the peer_infos of all the peers in the path
2842 * @param p Path to process.
2843 * @param confirmed Whether we know if the path works or not.
2846 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2850 /* TODO: invert and add */
2851 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2852 for (i++; i < p->length; i++)
2854 struct MeshPeerInfo *aux;
2855 struct MeshPeerPath *copy;
2857 aux = peer_info_get_short (p->peers[i]);
2858 copy = path_duplicate (p);
2859 copy->length = i + 1;
2860 peer_info_add_path (aux, copy, GNUNET_NO);
2866 * Send keepalive packets for a peer
2868 * @param cls Closure (tunnel for which to send the keepalive).
2869 * @param tc Notification context.
2871 * TODO: implement explicit multicast keepalive?
2874 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2878 * Search for a tunnel among the incoming tunnels
2880 * @param tid the local id of the tunnel
2882 * @return tunnel handler, NULL if doesn't exist
2884 static struct MeshTunnel *
2885 tunnel_get_incoming (MESH_TunnelNumber tid)
2887 struct GNUNET_HashCode hash;
2889 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2890 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2891 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2896 * Search for a tunnel among the tunnels for a client
2898 * @param c the client whose tunnels to search in
2899 * @param tid the local id of the tunnel
2901 * @return tunnel handler, NULL if doesn't exist
2903 static struct MeshTunnel *
2904 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2906 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2908 return tunnel_get_incoming (tid);
2912 struct GNUNET_HashCode hash;
2914 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2915 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2921 * Search for a tunnel by global ID using PEER_ID
2923 * @param pi owner of the tunnel
2924 * @param tid global tunnel number
2926 * @return tunnel handler, NULL if doesn't exist
2928 static struct MeshTunnel *
2929 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2931 struct MESH_TunnelID id;
2932 struct GNUNET_HashCode hash;
2937 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2938 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2943 * Search for a tunnel by global ID using full PeerIdentities
2945 * @param oid owner of the tunnel
2946 * @param tid global tunnel number
2948 * @return tunnel handler, NULL if doesn't exist
2950 static struct MeshTunnel *
2951 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2953 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2958 * Delete an active client from the tunnel.
2964 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2968 for (i = 0; i < t->nclients; i++)
2970 if (t->clients[i] == c)
2972 t->clients[i] = t->clients[t->nclients - 1];
2973 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2974 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2976 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2984 * Delete an ignored client from the tunnel.
2990 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2994 for (i = 0; i < t->nignore; i++)
2996 if (t->ignore[i] == c)
2998 t->ignore[i] = t->ignore[t->nignore - 1];
2999 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3007 * Delete a client from the tunnel. It should be only done on
3008 * client disconnection, otherwise use client_ignore_tunnel.
3014 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3016 tunnel_delete_ignored_client (t, c);
3017 tunnel_delete_active_client (t, c);
3022 * Iterator to free MeshTunnelChildInfo of tunnel children.
3024 * @param cls Closure (tunnel info).
3025 * @param key Hash of GNUNET_PEER_Id (unused).
3026 * @param value MeshTunnelChildInfo of the child.
3028 * @return always GNUNET_YES, to keep iterating
3031 tunnel_destroy_child (void *cls,
3032 const struct GNUNET_HashCode * key,
3035 GNUNET_free (value);
3041 * Callback used to notify a client owner of a tunnel that a peer has
3042 * disconnected, most likely because of a path change.
3044 * @param cls Closure (tunnel this notification is about).
3045 * @param peer_id Short ID of disconnected peer.
3048 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3050 struct MeshTunnel *t = cls;
3051 struct MeshPeerInfo *peer;
3052 struct MeshPathInfo *path_info;
3054 if (NULL != t->owner && NULL != nc)
3056 struct GNUNET_MESH_PeerControl msg;
3058 msg.header.size = htons (sizeof (msg));
3059 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3060 msg.tunnel_id = htonl (t->local_tid);
3061 GNUNET_PEER_resolve (peer_id, &msg.peer);
3062 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3063 &msg.header, GNUNET_NO);
3065 peer = peer_info_get_short (peer_id);
3066 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3067 path_info->peer = peer;
3069 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3074 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3075 * needed rescources.
3076 * If peer already exists, reevaluate shortest path and change if different.
3078 * @param t Tunnel we want to add a new peer to
3079 * @param peer PeerInfo of the peer being added
3083 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3085 struct GNUNET_PeerIdentity id;
3086 struct MeshPeerPath *best_p;
3087 struct MeshPeerPath *p;
3088 unsigned int best_cost;
3091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3092 GNUNET_PEER_resolve (peer->id, &id);
3094 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3097 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3098 GNUNET_assert (GNUNET_OK ==
3099 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3101 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3104 if (NULL != (p = peer->path_head))
3107 best_cost = tree_get_path_cost (t->tree, p);
3110 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3117 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3118 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3119 t->path_refresh_task =
3120 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3124 /* Start a DHT get */
3125 peer_info_connect (peer, t);
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3131 * Add a path to a tunnel which we don't own, just to remember the next hop.
3132 * If destination node was already in the tunnel, the first hop information
3133 * will be replaced with the new path.
3135 * @param t Tunnel we want to add a new peer to
3136 * @param p Path to add
3137 * @param own_pos Position of local node in path.
3141 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3142 unsigned int own_pos)
3144 struct GNUNET_PeerIdentity id;
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3147 GNUNET_assert (0 != own_pos);
3148 tree_add_path (t->tree, p, NULL, NULL);
3149 if (own_pos < p->length - 1)
3151 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3152 tree_update_first_hops (t->tree, myid, &id);
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3158 * Add a client to a tunnel, initializing all needed data structures.
3160 * @param t Tunnel to which add the client.
3161 * @param c Client which to add to the tunnel.
3164 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3166 struct MeshTunnelClientInfo clinfo;
3168 GNUNET_array_append (t->clients, t->nclients, c);
3169 clinfo.fwd_ack = t->fwd_pid + 1;
3170 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3171 clinfo.fwd_pid = t->fwd_pid;
3172 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3174 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3179 * Notifies a tunnel that a connection has broken that affects at least
3180 * some of its peers. Sends a notification towards the root of the tree.
3181 * In case the peer is the owner of the tree, notifies the client that owns
3182 * the tunnel and tries to reconnect.
3184 * @param t Tunnel affected.
3185 * @param p1 Peer that got disconnected from p2.
3186 * @param p2 Peer that got disconnected from p1.
3188 * @return Short ID of the peer disconnected (either p1 or p2).
3189 * 0 if the tunnel remained unaffected.
3191 static GNUNET_PEER_Id
3192 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3198 tree_notify_connection_broken (t->tree, p1, p2,
3199 &tunnel_notify_client_peer_disconnected,
3201 if (myid != p1 && myid != p2)
3207 if (tree_get_predecessor (t->tree) != 0)
3209 /* We are the peer still connected, notify owner of the disconnection. */
3210 struct GNUNET_MESH_PathBroken msg;
3211 struct GNUNET_PeerIdentity neighbor;
3213 msg.header.size = htons (sizeof (msg));
3214 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3215 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3216 msg.tid = htonl (t->id.tid);
3217 msg.peer1 = my_full_id;
3218 GNUNET_PEER_resolve (pid, &msg.peer2);
3219 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3220 send_message (&msg.header, &neighbor, t);
3228 * Send a multicast packet to a neighbor.
3230 * @param cls Closure (Info about the multicast packet)
3231 * @param neighbor_id Short ID of the neighbor to send the packet to.
3234 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3236 struct MeshData *mdata = cls;
3237 struct MeshTransmissionDescriptor *info;
3238 struct GNUNET_PeerIdentity neighbor;
3240 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3242 info->mesh_data = mdata;
3243 (*(mdata->reference_counter)) ++;
3244 info->destination = neighbor_id;
3245 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3247 GNUNET_i2s (&neighbor));
3248 info->peer = peer_info_get (&neighbor);
3249 GNUNET_assert (NULL != info->peer);
3251 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3252 info->mesh_data->data_len,
3259 * Queue a message in a tunnel in multicast, sending a copy to each child node
3260 * down the local one in the tunnel tree.
3262 * @param t Tunnel in which to send the data.
3263 * @param msg Message to be sent.
3264 * @param internal Has the service generated this message?
3267 tunnel_send_multicast (struct MeshTunnel *t,
3268 const struct GNUNET_MessageHeader *msg,
3271 struct MeshData *mdata;
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3274 " sending a multicast packet...\n");
3276 mdata = GNUNET_malloc (sizeof (struct MeshData));
3277 mdata->data_len = ntohs (msg->size);
3278 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3280 mdata->data = GNUNET_malloc (mdata->data_len);
3281 memcpy (mdata->data, msg, mdata->data_len);
3282 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3284 struct GNUNET_MESH_Multicast *mcast;
3286 if (t->fwd_queue_n >= t->fwd_queue_max)
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue full!\n");
3290 GNUNET_free (mdata->data);
3291 GNUNET_free (mdata->reference_counter);
3292 GNUNET_free (mdata);
3296 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3297 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3299 ntohl (mcast->ttl));
3303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3305 if (NULL != t->owner &&
3306 GNUNET_YES != t->owner->shutting_down &&
3307 GNUNET_NO == internal)
3309 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3311 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3317 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3318 if (*(mdata->reference_counter) == 0)
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3322 " no one to send data to\n");
3323 GNUNET_free (mdata->data);
3324 GNUNET_free (mdata->reference_counter);
3325 if (NULL != mdata->task)
3327 GNUNET_SCHEDULER_cancel(*(mdata->task));
3328 GNUNET_free (mdata->task);
3329 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3331 GNUNET_free (mdata);
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3335 " sending a multicast packet done\n");
3341 * Increase the SKIP value of all peers that
3342 * have not received a unicast message.
3344 * @param cls Closure (ID of the peer that HAS received the message).
3345 * @param key ID of the neighbor.
3346 * @param value Information about the neighbor.
3348 * @return GNUNET_YES to keep iterating.
3351 tunnel_add_skip (void *cls,
3352 const struct GNUNET_HashCode * key,
3355 struct GNUNET_PeerIdentity *neighbor = cls;
3356 struct MeshTunnelChildInfo *cinfo = value;
3358 /* TODO compare only pointers? key == neighbor? */
3359 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3369 * @brief Get neighbor's Flow Control information.
3371 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3372 * descendant of the local node in a certain tunnel.
3373 * If the info is not yet there (recently created path), creates the data struct
3374 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3377 * @param t Tunnel related.
3378 * @param peer Neighbor whose Flow Control info is needed.
3380 * @return Neighbor's Flow Control info.
3382 static struct MeshTunnelChildInfo *
3383 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3384 const struct GNUNET_PeerIdentity *peer)
3386 struct MeshTunnelChildInfo *cinfo;
3387 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3393 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3394 cinfo->id = GNUNET_PEER_intern (peer);
3395 cinfo->skip = t->fwd_pid;
3397 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3398 cinfo->fwd_ack = t->fwd_pid + delta;
3399 cinfo->bck_ack = delta;
3401 GNUNET_assert (GNUNET_OK ==
3402 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3405 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3412 * Get the Flow Control info of a client.
3414 * @param t Tunnel on which to look.
3415 * @param c Client whose ACK to get.
3417 * @return ACK value.
3419 static struct MeshTunnelClientInfo *
3420 tunnel_get_client_fc (struct MeshTunnel *t,
3421 struct MeshClient *c)
3425 for (i = 0; i < t->nclients; i++)
3427 if (t->clients[i] != c)
3429 return &t->clients_fc[i];
3432 return NULL; // avoid compiler / coverity complaints
3437 * Iterator to get the appropiate ACK value from all children nodes.
3439 * @param cls Closue (tunnel).
3440 * @param id Id of the child node.
3443 tunnel_get_child_fwd_ack (void *cls,
3446 struct GNUNET_PeerIdentity peer_id;
3447 struct MeshTunnelChildInfo *cinfo;
3448 struct MeshTunnelChildIteratorContext *ctx = cls;
3449 struct MeshTunnel *t = ctx->t;
3452 GNUNET_PEER_resolve (id, &peer_id);
3453 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3454 ack = cinfo->fwd_ack;
3457 if (GNUNET_NO == ctx->init)
3459 ctx->max_child_ack = ack;
3460 ctx->init = GNUNET_YES;
3463 if (GNUNET_YES == t->speed_min)
3465 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3469 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3476 * Get the maximum PID allowed to transmit to any
3477 * tunnel child of the local peer, depending on the tunnel
3478 * buffering/speed settings.
3482 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3485 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3487 struct MeshTunnelChildIteratorContext ctx;
3489 ctx.max_child_ack = 0;
3491 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3493 if (0 == ctx.nchildren)
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3496 " tunnel has no children, no FWD ACK\n");
3500 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3501 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3503 return (int64_t) ctx.max_child_ack;
3508 * Set the FWD ACK value of a client in a particular tunnel.
3510 * @param t Tunnel affected.
3511 * @param c Client whose ACK to set.
3512 * @param ack ACK value.
3515 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3516 struct MeshClient *c,
3521 for (i = 0; i < t->nclients; i++)
3523 if (t->clients[i] != c)
3525 t->clients_fc[i].fwd_ack = ack;
3533 * Get the highest ACK value of all clients in a particular tunnel,
3534 * according to the buffering/speed settings.
3536 * @param t Tunnel on which to look.
3538 * @return Corresponding ACK value (max uint32_t).
3539 * If no clients are suscribed, -1.
3542 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3547 if (0 == t->nclients)
3549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3550 " tunnel has no clients, no FWD ACK\n");
3554 for (ack = -1, i = 0; i < t->nclients; i++)
3557 (GNUNET_YES == t->speed_min &&
3558 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3559 (GNUNET_NO == t->speed_min &&
3560 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3562 ack = t->clients_fc[i].fwd_ack;
3566 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3567 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3569 return (uint32_t) ack;
3574 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3575 * mode and the status of all children nodes.
3579 * @return Maximum PID allowed.
3582 tunnel_get_fwd_ack (struct MeshTunnel *t)
3585 uint32_t buffer_free;
3590 count = t->fwd_pid - t->skip;
3591 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3592 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3593 child_ack = tunnel_get_children_fwd_ack (t);
3594 client_ack = tunnel_get_clients_fwd_ack (t);
3595 if (-1 == child_ack)
3597 // Node has no children, child_ack AND core buffer are irrelevant.
3598 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
3599 return (uint32_t) client_ack;
3602 if (GNUNET_YES == t->speed_min)
3604 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3605 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3609 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3610 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3612 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3613 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3615 count, buffer_free, child_ack, client_ack, ack);
3621 * Build a local ACK message and send it to a local client.
3623 * @param t Tunnel on which to send the ACK.
3624 * @param c Client to whom send the ACK.
3625 * @param ack Value of the ACK.
3628 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3630 struct GNUNET_MESH_LocalAck msg;
3632 msg.header.size = htons (sizeof (msg));
3633 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3634 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3635 msg.max_pid = htonl (ack);
3636 GNUNET_SERVER_notification_context_unicast(nc,
3643 * Build an ACK message and queue it to send to the given peer.
3645 * @param t Tunnel on which to send the ACK.
3646 * @param peer Peer to whom send the ACK.
3647 * @param ack Value of the ACK.
3650 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3652 struct GNUNET_MESH_ACK msg;
3654 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3655 msg.header.size = htons (sizeof (msg));
3656 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3657 msg.pid = htonl (ack);
3658 msg.tid = htonl (t->id.tid);
3660 send_message (&msg.header, peer, t);
3665 * Notify a the owner of a tunnel about how many more
3666 * payload packages will we accept on a given tunnel.
3668 * @param t Tunnel on which to send the ACK.
3671 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3676 "Sending client FWD ACK on tunnel %X\n",
3679 ack = tunnel_get_fwd_ack (t);
3681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3682 if (t->last_fwd_ack == ack)
3685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3686 t->last_fwd_ack = ack;
3687 send_local_ack (t, t->owner, ack);
3692 * Send an ACK informing the predecessor about the available buffer space.
3693 * In case there is no predecessor, inform the owning client.
3694 * If buffering is off, send only on behalf of children or self if endpoint.
3695 * If buffering is on, send when sent to children and buffer space is free.
3696 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3697 * the ACK itself goes "back" (towards root).
3699 * @param t Tunnel on which to send the ACK.
3700 * @param type Type of message that triggered the ACK transmission.
3703 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3705 struct GNUNET_PeerIdentity id;
3708 if (NULL != t->owner)
3710 tunnel_send_client_fwd_ack (t);
3713 /* Is it after unicast / multicast retransmission? */
3716 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3717 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3719 "ACK due to FWD DATA retransmission\n");
3720 if (GNUNET_YES == t->nobuffer)
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3725 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3726 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3730 " t->qmax: %u, t->qn: %u\n",
3731 t->fwd_queue_max, t->fwd_queue_n);
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3733 " t->pid: %u, t->ack: %u\n",
3734 t->fwd_pid, t->last_fwd_ack);
3738 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3739 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3745 /* Ok, ACK might be necessary, what PID to ACK? */
3746 ack = tunnel_get_fwd_ack (t);
3748 /* If speed_min and not all children have ack'd, dont send yet */
3749 if (ack == t->last_fwd_ack)
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3755 t->last_fwd_ack = ack;
3756 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3757 send_ack (t, &id, ack);
3762 * Iterator to send a child node a BCK ACK to allow him to send more
3765 * @param cls Closure (tunnel).
3766 * @param id Id of the child node.
3769 tunnel_send_child_bck_ack (void *cls,
3772 struct MeshTunnel *t = cls;
3773 struct MeshTunnelChildInfo *cinfo;
3774 struct GNUNET_PeerIdentity peer;
3776 GNUNET_PEER_resolve (id, &peer);
3777 cinfo = tunnel_get_neighbor_fc (t, &peer);
3779 if (cinfo->bck_ack != cinfo->pid &&
3780 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3784 send_ack (t, &peer, cinfo->bck_ack);
3789 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3791 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3793 * @param t Tunnel on which to send the BCK ACKs.
3796 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3799 unsigned int tunnel_delta;
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3803 tunnel_delta = t->bck_ack - t->bck_pid;
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3806 /* Find client whom to allow to send to origin (with lowest buffer space) */
3807 for (i = 0; i < t->nclients; i++)
3809 struct MeshTunnelClientInfo *clinfo;
3812 clinfo = &t->clients_fc[i];
3813 delta = clinfo->bck_ack - clinfo->bck_pid;
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3815 t->clients[i]->id, delta);
3817 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3818 (GNUNET_YES == t->nobuffer && 0 == delta))
3822 ack = clinfo->bck_pid;
3823 ack += t->nobuffer ? 1 : tunnel_delta;
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3825 " sending ack to client %u: %u\n",
3826 t->clients[i]->id, ack);
3827 send_local_ack (t, t->clients[i], ack);
3828 clinfo->bck_ack = ack;
3835 * Send an ACK informing the children nodes and destination clients about
3836 * the available buffer space.
3837 * If buffering is off, send only on behalf of root (can be self).
3838 * If buffering is on, send when sent to predecessor and buffer space is free.
3839 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3840 * the ACK itself goes "forward" (towards children/clients).
3842 * @param t Tunnel on which to send the ACK.
3843 * @param type Type of message that triggered the ACK transmission.
3846 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3849 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3850 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3851 /* Is it after data to_origin retransmission? */
3854 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3855 if (GNUNET_YES == t->nobuffer)
3857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3858 " Not sending ACK, nobuffer\n");
3862 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3863 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3869 tunnel_send_clients_bck_ack (t);
3870 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3875 * Send a message to all peers in this tunnel that the tunnel is no longer
3878 * @param t The tunnel whose peers to notify.
3881 tunnel_send_destroy (struct MeshTunnel *t)
3883 struct GNUNET_MESH_TunnelDestroy msg;
3885 msg.header.size = htons (sizeof (msg));
3886 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3887 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3888 msg.tid = htonl (t->id.tid);
3889 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3894 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3896 * @param cls Closure (Tunnel which to cancel).
3897 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3900 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3902 struct MeshTunnel *t = cls;
3903 struct MeshPeerInfo *peer_info;
3904 struct MeshPeerQueue *pq;
3905 struct MeshPeerQueue *next;
3907 peer_info = peer_info_get_short (neighbor_id);
3908 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3911 if (pq->tunnel == t)
3913 queue_destroy (pq, GNUNET_YES);
3916 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3918 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3919 peer_info->core_transmit = NULL;
3924 * Destroy the tunnel and free any allocated resources linked to it.
3926 * @param t the tunnel to destroy
3928 * @return GNUNET_OK on success
3931 tunnel_destroy (struct MeshTunnel *t)
3933 struct MeshClient *c;
3934 struct GNUNET_HashCode hash;
3941 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3947 struct GNUNET_PeerIdentity id;
3949 GNUNET_PEER_resolve (t->id.oid, &id);
3950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3951 GNUNET_i2s (&id), t->id.tid);
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3957 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3958 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3963 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3966 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3970 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3971 for (i = 0; i < t->nclients; i++)
3975 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3980 for (i = 0; i < t->nignore; i++)
3984 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3989 if (t->nclients > 0)
3992 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3996 GNUNET_free (t->clients);
3998 if (NULL != t->peers)
4000 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4002 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4005 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4006 &tunnel_destroy_child,
4008 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4010 tree_destroy (t->tree);
4012 if (NULL != t->regex_ctx)
4013 regex_cancel_search (t->regex_ctx);
4014 if (NULL != t->dht_get_type)
4015 GNUNET_DHT_get_stop (t->dht_get_type);
4016 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4017 GNUNET_SCHEDULER_cancel (t->timeout_task);
4018 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4019 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4022 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4023 GNUNET_assert (0 <= n_tunnels);
4030 * Create a new tunnel
4032 * @param owner Who is the owner of the tunnel (short ID).
4033 * @param tid Tunnel Number of the tunnel.
4034 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4035 * @param local Tunnel Number for the tunnel, for the client point of view.
4037 * @return A new initialized tunnel. NULL on error.
4039 static struct MeshTunnel *
4040 tunnel_new (GNUNET_PEER_Id owner,
4041 MESH_TunnelNumber tid,
4042 struct MeshClient *client,
4043 MESH_TunnelNumber local)
4045 struct MeshTunnel *t;
4046 struct GNUNET_HashCode hash;
4048 if (n_tunnels >= max_tunnels && NULL == client)
4051 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4054 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4055 t->bck_queue_max = t->fwd_queue_max;
4056 t->tree = tree_new (owner);
4058 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4059 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4060 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4061 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4062 t->local_tid = local;
4063 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4065 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4067 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4069 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4070 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4077 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4084 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4086 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4087 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4091 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4101 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4102 * that are no longer needed, as well as nodes of no longer reachable peers.
4103 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4105 * @param t Tunnel from which to remove the path.
4106 * @param peer Short id of the peer which should be removed.
4109 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4111 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4117 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4118 * client when the client disconnects. If the client is not the owner, the
4119 * owner will get notified if no more clients are in the tunnel and the client
4120 * get removed from the tunnel's list.
4122 * @param cls closure (client that is disconnecting)
4123 * @param key the hash of the local tunnel id (used to access the hashmap)
4124 * @param value the value stored at the key (tunnel to destroy)
4126 * @return GNUNET_OK on success
4129 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4131 struct MeshTunnel *t = value;
4132 struct MeshClient *c = cls;
4135 send_client_tunnel_disconnect(t, c);
4138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4139 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4140 tunnel_delete_client(t, c);
4141 client_delete_tunnel(c, t);
4144 tunnel_send_destroy(t);
4145 r = tunnel_destroy (t);
4151 * Timeout function, destroys tunnel if called
4153 * @param cls Closure (tunnel to destroy).
4154 * @param tc TaskContext
4157 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4159 struct MeshTunnel *t = cls;
4161 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4163 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4168 * Resets the tunnel timeout. Starts it if no timeout was running.
4170 * @param t Tunnel whose timeout to reset.
4172 * TODO use heap to improve efficiency of scheduler.
4175 tunnel_reset_timeout (struct MeshTunnel *t)
4177 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4178 GNUNET_SCHEDULER_cancel (t->timeout_task);
4180 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4181 (refresh_path_time, 4), &tunnel_timeout, t);
4185 /******************************************************************************/
4186 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4187 /******************************************************************************/
4190 * Function to send a create path packet to a peer.
4192 * @param cls closure
4193 * @param size number of bytes available in buf
4194 * @param buf where the callee should write the message
4195 * @return number of bytes written to buf
4198 send_core_path_create (void *cls, size_t size, void *buf)
4200 struct MeshPathInfo *info = cls;
4201 struct GNUNET_MESH_ManipulatePath *msg;
4202 struct GNUNET_PeerIdentity *peer_ptr;
4203 struct MeshTunnel *t = info->t;
4204 struct MeshPeerPath *p = info->path;
4209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4211 sizeof (struct GNUNET_MESH_ManipulatePath) +
4212 p->length * sizeof (struct GNUNET_PeerIdentity);
4214 if (size < size_needed || NULL == buf)
4219 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4220 msg->header.size = htons (size_needed);
4221 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4222 msg->tid = ntohl (t->id.tid);
4224 if (GNUNET_YES == t->speed_min)
4225 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4226 if (GNUNET_YES == t->nobuffer)
4227 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4228 msg->opt = htonl(opt);
4230 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4231 for (i = 0; i < p->length; i++)
4233 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4240 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4246 * Fill the core buffer
4248 * @param cls closure (data itself)
4249 * @param size number of bytes available in buf
4250 * @param buf where the callee should write the message
4252 * @return number of bytes written to buf
4255 send_core_data_multicast (void *cls, size_t size, void *buf)
4257 struct MeshTransmissionDescriptor *info = cls;
4260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4261 GNUNET_assert (NULL != info);
4262 GNUNET_assert (NULL != info->peer);
4263 total_size = info->mesh_data->data_len;
4264 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4266 if (total_size > size)
4271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4272 memcpy (buf, info->mesh_data->data, total_size);
4275 struct GNUNET_MESH_Multicast *mc;
4276 struct GNUNET_MessageHeader *mh;
4279 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4281 mc = (struct GNUNET_MESH_Multicast *) mh;
4282 mh = (struct GNUNET_MessageHeader *) &mc[1];
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4284 " multicast, payload type %s\n",
4285 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4287 " multicast, payload size %u\n", ntohs (mh->size));
4291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4292 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4296 data_descriptor_decrement_rc (info->mesh_data);
4297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4305 * Creates a path ack message in buf and frees all unused resources.
4307 * @param cls closure (MeshTransmissionDescriptor)
4308 * @param size number of bytes available in buf
4309 * @param buf where the callee should write the message
4310 * @return number of bytes written to buf
4313 send_core_path_ack (void *cls, size_t size, void *buf)
4315 struct MeshTransmissionDescriptor *info = cls;
4316 struct GNUNET_MESH_PathACK *msg = buf;
4318 GNUNET_assert (NULL != info);
4319 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4324 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4325 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4326 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4327 msg->tid = htonl (info->origin->tid);
4328 msg->peer_id = my_full_id;
4331 /* TODO add signature */
4333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4334 return sizeof (struct GNUNET_MESH_PathACK);
4339 * Free a transmission that was already queued with all resources
4340 * associated to the request.
4342 * @param queue Queue handler to cancel.
4343 * @param clear_cls Is it necessary to free associated cls?
4346 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4348 struct MeshTransmissionDescriptor *dd;
4349 struct MeshPathInfo *path_info;
4351 if (GNUNET_YES == clear_cls)
4353 switch (queue->type)
4355 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4356 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4357 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4360 data_descriptor_decrement_rc (dd->mesh_data);
4362 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4364 path_info = queue->cls;
4365 path_destroy (path_info->path);
4369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4371 GNUNET_free_non_null (queue->cls);
4373 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4374 queue->peer->queue_tail,
4376 GNUNET_free (queue);
4381 * @brief Get the next transmittable message from the queue.
4383 * This will be the head, expect in the case of being a data packet
4384 * not allowed by the destination peer.
4386 * @param peer Destination peer.
4388 * @return The next viable MeshPeerQueue element to send to that peer.
4389 * NULL when there are no transmittable messages.
4391 struct MeshPeerQueue *
4392 queue_get_next (const struct MeshPeerInfo *peer)
4394 struct MeshPeerQueue *q;
4395 struct MeshTunnel *t;
4396 struct MeshTransmissionDescriptor *info;
4397 struct MeshTunnelChildInfo *cinfo;
4398 struct GNUNET_MESH_Unicast *ucast;
4399 struct GNUNET_MESH_ToOrigin *to_orig;
4400 struct GNUNET_MESH_Multicast *mcast;
4401 struct GNUNET_PeerIdentity id;
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4406 for (q = peer->queue_head; NULL != q; q = q->next)
4410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4412 GNUNET_MESH_DEBUG_M2S(q->type));
4415 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4416 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4417 pid = ntohl (ucast->pid);
4418 GNUNET_PEER_resolve (info->peer->id, &id);
4419 cinfo = tunnel_get_neighbor_fc(t, &id);
4420 ack = cinfo->fwd_ack;
4422 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4423 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4424 pid = ntohl (to_orig->pid);
4427 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4428 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4429 pid = ntohl (mcast->pid);
4430 GNUNET_PEER_resolve (info->peer->id, &id);
4431 cinfo = tunnel_get_neighbor_fc(t, &id);
4432 ack = cinfo->fwd_ack;
4435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4440 "********* ACK: %u, PID: %u\n",
4442 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4451 "********* NEXT!\n");
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4455 "********* nothing found\n");
4461 * Core callback to write a queued packet to core buffer
4463 * @param cls Closure (peer info).
4464 * @param size Number of bytes available in buf.
4465 * @param buf Where the to write the message.
4467 * @return number of bytes written to buf
4470 queue_send (void *cls, size_t size, void *buf)
4472 struct MeshPeerInfo *peer = cls;
4473 struct GNUNET_MessageHeader *msg;
4474 struct MeshPeerQueue *queue;
4475 struct MeshTunnel *t;
4478 peer->core_transmit = NULL;
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4481 queue = queue_get_next(peer);
4483 /* Queue has no internal mesh traffic not sendable payload */
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4487 if (NULL == peer->queue_head)
4488 GNUNET_break(0); // Should've been canceled
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4493 /* Check if buffer size is enough for the message */
4494 if (queue->size > size)
4496 struct GNUNET_PeerIdentity id;
4498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4499 "********* not enough room, reissue\n");
4500 GNUNET_PEER_resolve (peer->id, &id);
4501 peer->core_transmit =
4502 GNUNET_CORE_notify_transmit_ready(core_handle,
4505 GNUNET_TIME_UNIT_FOREVER_REL,
4512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4515 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4519 "********* unicast: t->q (%u/%u)\n",
4520 t->fwd_queue_n, t->fwd_queue_max);
4522 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4529 switch (queue->type)
4532 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4533 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4534 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4536 "********* raw: %s\n",
4537 GNUNET_MESH_DEBUG_M2S (queue->type));
4539 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4540 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4541 data_size = send_core_data_raw (queue->cls, size, buf);
4542 msg = (struct GNUNET_MessageHeader *) buf;
4543 switch (ntohs (msg->type)) // Type of preconstructed message
4545 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4546 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4548 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4549 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4555 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4557 data_size = send_core_data_multicast(queue->cls, size, buf);
4558 // t->fwd_queue_n--; FIXME fc
4559 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4561 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4563 data_size = send_core_path_create(queue->cls, size, buf);
4565 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4567 data_size = send_core_path_ack(queue->cls, size, buf);
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4572 "********* type unknown: %u\n",
4577 /* Free queue, but cls was freed by send_core_* */
4578 queue_destroy (queue, GNUNET_NO);
4580 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4586 /* If more data in queue, send next */
4587 if (NULL != peer->queue_head)
4589 struct GNUNET_PeerIdentity id;
4591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4592 GNUNET_PEER_resolve (peer->id, &id);
4593 peer->core_transmit =
4594 GNUNET_CORE_notify_transmit_ready(core_handle,
4597 GNUNET_TIME_UNIT_FOREVER_REL,
4599 peer->queue_head->size,
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4609 * Queue and pass message to core when possible.
4611 * @param cls Closure (type dependant).
4612 * @param type Type of the message, 0 for a raw message.
4613 * @param size Size of the message.
4614 * @param dst Neighbor to send message to.
4615 * @param t Tunnel this message belongs to.
4618 queue_add (void *cls, uint16_t type, size_t size,
4619 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4621 struct MeshPeerQueue *queue;
4626 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4627 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4629 n = &t->fwd_queue_n;
4630 max = &t->fwd_queue_max;
4632 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4634 n = &t->bck_queue_n;
4635 max = &t->bck_queue_max;
4640 if (NULL == t->owner)
4641 GNUNET_break_op(0); // TODO: kill connection?
4644 return; // Drop message
4648 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4654 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4655 if (NULL == dst->core_transmit)
4657 struct GNUNET_PeerIdentity id;
4659 GNUNET_PEER_resolve (dst->id, &id);
4660 dst->core_transmit =
4661 GNUNET_CORE_notify_transmit_ready(core_handle,
4664 GNUNET_TIME_UNIT_FOREVER_REL,
4673 /******************************************************************************/
4674 /******************** MESH NETWORK HANDLERS **************************/
4675 /******************************************************************************/
4679 * Core handler for path creation
4681 * @param cls closure
4682 * @param message message
4683 * @param peer peer identity this notification is about
4684 * @param atsi performance data
4685 * @param atsi_count number of records in 'atsi'
4687 * @return GNUNET_OK to keep the connection open,
4688 * GNUNET_SYSERR to close it (signal serious error)
4691 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4692 const struct GNUNET_MessageHeader *message,
4693 const struct GNUNET_ATS_Information *atsi,
4694 unsigned int atsi_count)
4696 unsigned int own_pos;
4699 MESH_TunnelNumber tid;
4700 struct GNUNET_MESH_ManipulatePath *msg;
4701 struct GNUNET_PeerIdentity *pi;
4702 struct GNUNET_HashCode hash;
4703 struct MeshPeerPath *path;
4704 struct MeshPeerInfo *dest_peer_info;
4705 struct MeshPeerInfo *orig_peer_info;
4706 struct MeshTunnel *t;
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4709 "Received a path create msg [%s]\n",
4710 GNUNET_i2s (&my_full_id));
4711 size = ntohs (message->size);
4712 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4714 GNUNET_break_op (0);
4718 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4719 if (size % sizeof (struct GNUNET_PeerIdentity))
4721 GNUNET_break_op (0);
4724 size /= sizeof (struct GNUNET_PeerIdentity);
4727 GNUNET_break_op (0);
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4731 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4733 tid = ntohl (msg->tid);
4734 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4736 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4737 t = tunnel_get (pi, tid);
4738 if (NULL == t) // FIXME only for INCOMING tunnels?
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4743 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4746 // FIXME notify failure
4749 opt = ntohl (msg->opt);
4750 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4751 GNUNET_YES : GNUNET_NO;
4752 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4753 GNUNET_YES : GNUNET_NO;
4755 if (GNUNET_YES == t->nobuffer)
4757 t->bck_queue_max = 1;
4758 t->fwd_queue_max = 1;
4761 // FIXME only assign a local tid if a local client is interested (on demand)
4762 while (NULL != tunnel_get_incoming (next_local_tid))
4763 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4764 t->local_tid_dest = next_local_tid++;
4765 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4768 tunnel_reset_timeout (t);
4769 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4771 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4772 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4780 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4781 if (NULL == dest_peer_info)
4783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4784 " Creating PeerInfo for destination.\n");
4785 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4786 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4787 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4789 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4791 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4792 if (NULL == orig_peer_info)
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4795 " Creating PeerInfo for origin.\n");
4796 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4797 orig_peer_info->id = GNUNET_PEER_intern (pi);
4798 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4799 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4802 path = path_new (size);
4804 for (i = 0; i < size; i++)
4806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4807 GNUNET_i2s (&pi[i]));
4808 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4809 if (path->peers[i] == myid)
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4815 /* cannot be self, must be 'not found' */
4816 /* create path: self not found in path through self */
4817 GNUNET_break_op (0);
4818 path_destroy (path);
4822 path_add_to_peers (path, GNUNET_NO);
4823 tunnel_add_path (t, path, own_pos);
4824 if (own_pos == size - 1)
4826 /* It is for us! Send ack. */
4827 struct MeshTransmissionDescriptor *info;
4829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4830 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4831 if (NULL == t->peers)
4833 /* New tunnel! Notify clients on data. */
4834 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4836 GNUNET_break (GNUNET_SYSERR !=
4837 GNUNET_CONTAINER_multihashmap_put (t->peers,
4838 &my_full_id.hashPubKey,
4841 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4842 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4843 info->origin = &t->id;
4844 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4845 GNUNET_assert (NULL != info->peer);
4847 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4848 sizeof (struct GNUNET_MESH_PathACK),
4854 struct MeshPeerPath *path2;
4856 /* It's for somebody else! Retransmit. */
4857 path2 = path_duplicate (path);
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4859 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4860 path2 = path_duplicate (path);
4861 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4862 send_create_path (dest_peer_info, path, t);
4869 * Core handler for path destruction
4871 * @param cls closure
4872 * @param message message
4873 * @param peer peer identity this notification is about
4874 * @param atsi performance data
4875 * @param atsi_count number of records in 'atsi'
4877 * @return GNUNET_OK to keep the connection open,
4878 * GNUNET_SYSERR to close it (signal serious error)
4881 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4882 const struct GNUNET_MessageHeader *message,
4883 const struct GNUNET_ATS_Information *atsi,
4884 unsigned int atsi_count)
4886 struct GNUNET_MESH_ManipulatePath *msg;
4887 struct GNUNET_PeerIdentity *pi;
4888 struct MeshPeerPath *path;
4889 struct MeshTunnel *t;
4890 unsigned int own_pos;
4894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4895 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4896 size = ntohs (message->size);
4897 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4899 GNUNET_break_op (0);
4903 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4904 if (size % sizeof (struct GNUNET_PeerIdentity))
4906 GNUNET_break_op (0);
4909 size /= sizeof (struct GNUNET_PeerIdentity);
4912 GNUNET_break_op (0);
4915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4917 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4918 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4920 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4922 t = tunnel_get (pi, ntohl (msg->tid));
4925 /* TODO notify back: we don't know this tunnel */
4926 GNUNET_break_op (0);
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4930 path = path_new (size);
4932 for (i = 0; i < size; i++)
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4935 GNUNET_i2s (&pi[i]));
4936 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4937 if (path->peers[i] == myid)
4940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4941 if (own_pos < path->length - 1)
4942 send_message (message, &pi[own_pos + 1], t);
4944 send_client_tunnel_disconnect(t, NULL);
4946 tunnel_delete_peer (t, path->peers[path->length - 1]);
4947 path_destroy (path);
4953 * Core handler for notifications of broken paths
4955 * @param cls closure
4956 * @param message message
4957 * @param peer peer identity this notification is about
4958 * @param atsi performance data
4959 * @param atsi_count number of records in 'atsi'
4961 * @return GNUNET_OK to keep the connection open,
4962 * GNUNET_SYSERR to close it (signal serious error)
4965 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4966 const struct GNUNET_MessageHeader *message,
4967 const struct GNUNET_ATS_Information *atsi,
4968 unsigned int atsi_count)
4970 struct GNUNET_MESH_PathBroken *msg;
4971 struct MeshTunnel *t;
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4974 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4975 msg = (struct GNUNET_MESH_PathBroken *) message;
4976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4977 GNUNET_i2s (&msg->peer1));
4978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4979 GNUNET_i2s (&msg->peer2));
4980 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4983 GNUNET_break_op (0);
4986 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4987 GNUNET_PEER_search (&msg->peer2));
4994 * Core handler for tunnel destruction
4996 * @param cls closure
4997 * @param message message
4998 * @param peer peer identity this notification is about
4999 * @param atsi performance data
5000 * @param atsi_count number of records in 'atsi'
5002 * @return GNUNET_OK to keep the connection open,
5003 * GNUNET_SYSERR to close it (signal serious error)
5006 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5007 const struct GNUNET_MessageHeader *message,
5008 const struct GNUNET_ATS_Information *atsi,
5009 unsigned int atsi_count)
5011 struct GNUNET_MESH_TunnelDestroy *msg;
5012 struct MeshTunnel *t;
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5015 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5016 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5018 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5019 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5022 /* Probably already got the message from another path,
5023 * destroyed the tunnel and retransmitted to children.
5026 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5029 if (t->id.oid == myid)
5031 GNUNET_break_op (0);
5034 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5036 /* Tunnel was incoming, notify clients */
5037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5038 t->local_tid, t->local_tid_dest);
5039 send_clients_tunnel_destroy (t);
5041 tunnel_send_destroy (t);
5042 t->destroy = GNUNET_YES;
5043 // TODO: add timeout to destroy the tunnel anyway
5049 * Core handler for mesh network traffic going from the origin to a peer
5051 * @param cls closure
5052 * @param peer peer identity this notification is about
5053 * @param message message
5054 * @param atsi performance data
5055 * @param atsi_count number of records in 'atsi'
5056 * @return GNUNET_OK to keep the connection open,
5057 * GNUNET_SYSERR to close it (signal serious error)
5060 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5061 const struct GNUNET_MessageHeader *message,
5062 const struct GNUNET_ATS_Information *atsi,
5063 unsigned int atsi_count)
5065 struct GNUNET_MESH_Unicast *msg;
5066 struct GNUNET_PeerIdentity *neighbor;
5067 struct MeshTunnelChildInfo *cinfo;
5068 struct MeshTunnel *t;
5069 GNUNET_PEER_Id dest_id;
5074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5077 size = ntohs (message->size);
5079 sizeof (struct GNUNET_MESH_Unicast) +
5080 sizeof (struct GNUNET_MessageHeader))
5085 msg = (struct GNUNET_MESH_Unicast *) message;
5086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5087 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5089 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5092 /* TODO notify back: we don't know this tunnel */
5093 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5094 GNUNET_break_op (0);
5097 pid = ntohl (msg->pid);
5098 if (t->fwd_pid == pid)
5100 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5102 " Already seen pid %u, DROPPING!\n", pid);
5107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5108 " pid %u not seen yet, forwarding\n", pid);
5111 t->skip += (pid - t->fwd_pid) - 1;
5114 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5116 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5117 GNUNET_break_op (0);
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5119 "Received PID %u, ACK %u\n",
5120 pid, t->last_fwd_ack);
5124 tunnel_reset_timeout (t);
5125 dest_id = GNUNET_PEER_search (&msg->destination);
5126 if (dest_id == myid)
5128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5129 " it's for us! sending to clients...\n");
5130 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5131 send_subscribed_clients (message, &msg[1].header, t);
5132 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5135 ttl = ntohl (msg->ttl);
5136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5139 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5141 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5145 " not for us, retransmitting...\n");
5147 neighbor = tree_get_first_hop (t->tree, dest_id);
5148 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5150 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5153 if (GNUNET_YES == t->nobuffer &&
5154 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5156 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5157 GNUNET_break_op (0);
5160 send_message (message, neighbor, t);
5161 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5167 * Core handler for mesh network traffic going from the origin to all peers
5169 * @param cls closure
5170 * @param message message
5171 * @param peer peer identity this notification is about
5172 * @param atsi performance data
5173 * @param atsi_count number of records in 'atsi'
5174 * @return GNUNET_OK to keep the connection open,
5175 * GNUNET_SYSERR to close it (signal serious error)
5177 * TODO: Check who we got this from, to validate route.
5180 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5181 const struct GNUNET_MessageHeader *message,
5182 const struct GNUNET_ATS_Information *atsi,
5183 unsigned int atsi_count)
5185 struct GNUNET_MESH_Multicast *msg;
5186 struct MeshTunnel *t;
5190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5192 size = ntohs (message->size);
5193 if (sizeof (struct GNUNET_MESH_Multicast) +
5194 sizeof (struct GNUNET_MessageHeader) > size)
5196 GNUNET_break_op (0);
5199 msg = (struct GNUNET_MESH_Multicast *) message;
5200 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5204 /* TODO notify that we dont know that tunnel */
5205 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5206 GNUNET_break_op (0);
5209 pid = ntohl (msg->pid);
5210 if (t->fwd_pid == pid)
5212 /* already seen this packet, drop */
5213 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5215 " Already seen pid %u, DROPPING!\n", pid);
5216 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5222 " pid %u not seen yet, forwarding\n", pid);
5224 t->skip += (pid - t->fwd_pid) - 1;
5226 tunnel_reset_timeout (t);
5228 /* Transmit to locally interested clients */
5229 if (NULL != t->peers &&
5230 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5232 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5233 send_subscribed_clients (message, &msg[1].header, t);
5234 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5237 if (ntohl (msg->ttl) == 0)
5239 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5240 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5241 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5244 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5245 tunnel_send_multicast (t, message, GNUNET_NO);
5251 * Core handler for mesh network traffic toward the owner of a tunnel
5253 * @param cls closure
5254 * @param message message
5255 * @param peer peer identity this notification is about
5256 * @param atsi performance data
5257 * @param atsi_count number of records in 'atsi'
5259 * @return GNUNET_OK to keep the connection open,
5260 * GNUNET_SYSERR to close it (signal serious error)
5263 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5264 const struct GNUNET_MessageHeader *message,
5265 const struct GNUNET_ATS_Information *atsi,
5266 unsigned int atsi_count)
5268 struct GNUNET_MESH_ToOrigin *msg;
5269 struct GNUNET_PeerIdentity id;
5270 struct MeshPeerInfo *peer_info;
5271 struct MeshTunnel *t;
5275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5277 size = ntohs (message->size);
5278 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5279 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5281 GNUNET_break_op (0);
5284 msg = (struct GNUNET_MESH_ToOrigin *) message;
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5286 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5287 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5291 /* TODO notify that we dont know this tunnel (whom)? */
5292 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5293 GNUNET_break_op (0);
5297 if (NULL != t->owner)
5300 struct GNUNET_MESH_ToOrigin *copy;
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5303 " it's for us! sending to clients...\n");
5304 /* TODO signature verification */
5305 memcpy (cbuf, message, size);
5306 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5307 copy->tid = htonl (t->local_tid);
5308 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5309 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5310 ©->header, GNUNET_NO);
5311 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5315 " not for us, retransmitting...\n");
5317 peer_info = peer_info_get (&msg->oid);
5318 if (NULL == peer_info)
5320 /* unknown origin of tunnel */
5324 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5325 send_message (message, &id, t);
5326 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5333 * Core handler for mesh network traffic point-to-point acks.
5335 * @param cls closure
5336 * @param message message
5337 * @param peer peer identity this notification is about
5338 * @param atsi performance data
5339 * @param atsi_count number of records in 'atsi'
5341 * @return GNUNET_OK to keep the connection open,
5342 * GNUNET_SYSERR to close it (signal serious error)
5345 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5346 const struct GNUNET_MessageHeader *message,
5347 const struct GNUNET_ATS_Information *atsi,
5348 unsigned int atsi_count)
5350 struct GNUNET_MESH_ACK *msg;
5351 struct MeshTunnel *t;
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5356 msg = (struct GNUNET_MESH_ACK *) message;
5357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5358 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5359 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5363 /* TODO notify that we dont know this tunnel (whom)? */
5364 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5365 GNUNET_break_op (0);
5368 ack = ntohl (msg->pid);
5370 /* Is this a forward or backward ACK? */
5371 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5373 struct MeshTunnelChildInfo *cinfo;
5375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5376 cinfo = tunnel_get_neighbor_fc (t, peer);
5377 cinfo->fwd_ack = ack;
5378 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5384 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5386 // FIXME fc Unlock queues?
5392 * Core handler for path ACKs
5394 * @param cls closure
5395 * @param message message
5396 * @param peer peer identity this notification is about
5397 * @param atsi performance data
5398 * @param atsi_count number of records in 'atsi'
5400 * @return GNUNET_OK to keep the connection open,
5401 * GNUNET_SYSERR to close it (signal serious error)
5404 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5405 const struct GNUNET_MessageHeader *message,
5406 const struct GNUNET_ATS_Information *atsi,
5407 unsigned int atsi_count)
5409 struct GNUNET_MESH_PathACK *msg;
5410 struct GNUNET_PeerIdentity id;
5411 struct MeshPeerInfo *peer_info;
5412 struct MeshPeerPath *p;
5413 struct MeshTunnel *t;
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5416 GNUNET_i2s (&my_full_id));
5417 msg = (struct GNUNET_MESH_PathACK *) message;
5418 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5421 /* TODO notify that we don't know the tunnel */
5422 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5424 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5428 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5430 peer_info = peer_info_get (&msg->peer_id);
5431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5432 GNUNET_i2s (&msg->peer_id));
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5436 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5439 "connect_by_string completed, stopping search\n");
5440 regex_cancel_search (t->regex_ctx);
5441 t->regex_ctx = NULL;
5444 /* Add paths to peers? */
5445 p = tree_get_path_to_peer (t->tree, peer_info->id);
5448 path_add_to_peers (p, GNUNET_YES);
5456 /* Message for us? */
5457 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5460 if (NULL == t->owner)
5462 GNUNET_break_op (0);
5465 if (NULL != t->dht_get_type)
5467 GNUNET_DHT_get_stop (t->dht_get_type);
5468 t->dht_get_type = NULL;
5470 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5472 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5473 send_client_peer_connected (t, peer_info->id);
5478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5479 " not for us, retransmitting...\n");
5480 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5481 peer_info = peer_info_get (&msg->oid);
5482 if (NULL == peer_info)
5484 /* If we know the tunnel, we should DEFINITELY know the peer */
5488 send_message (message, &id, t);
5494 * Functions to handle messages from core
5496 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5497 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5498 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5499 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5500 sizeof (struct GNUNET_MESH_PathBroken)},
5501 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5502 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5503 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5504 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5505 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5506 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5507 sizeof (struct GNUNET_MESH_ACK)},
5508 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5509 sizeof (struct GNUNET_MESH_PathACK)},
5515 /******************************************************************************/
5516 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5517 /******************************************************************************/
5520 * deregister_app: iterator for removing each application registered by a client
5522 * @param cls closure
5523 * @param key the hash of the application id (used to access the hashmap)
5524 * @param value the value stored at the key (client)
5526 * @return GNUNET_OK on success
5529 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5531 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5532 GNUNET_break (GNUNET_YES ==
5533 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5539 * notify_client_connection_failure: notify a client that the connection to the
5540 * requested remote peer is not possible (for instance, no route found)
5541 * Function called when the socket is ready to queue more data. "buf" will be
5542 * NULL and "size" zero if the socket was closed for writing in the meantime.
5544 * @param cls closure
5545 * @param size number of bytes available in buf
5546 * @param buf where the callee should write the message
5547 * @return number of bytes written to buf
5550 notify_client_connection_failure (void *cls, size_t size, void *buf)
5553 struct MeshPeerInfo *peer_info;
5554 struct GNUNET_MESH_PeerControl *msg;
5555 struct GNUNET_PeerIdentity id;
5557 if (0 == size && NULL == buf)
5559 // TODO retry? cancel?
5563 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5564 peer_info = (struct MeshPeerInfo *) cls;
5565 msg = (struct GNUNET_MESH_PeerControl *) buf;
5566 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5567 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5568 // msg->tunnel_id = htonl(peer_info->t->tid);
5569 GNUNET_PEER_resolve (peer_info->id, &id);
5570 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5578 * Send keepalive packets for a peer
5580 * @param cls Closure (tunnel for which to send the keepalive).
5581 * @param tc Notification context.
5583 * TODO: implement explicit multicast keepalive?
5586 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5588 struct MeshTunnel *t = cls;
5589 struct GNUNET_MessageHeader *payload;
5590 struct GNUNET_MESH_Multicast *msg;
5592 sizeof (struct GNUNET_MESH_Multicast) +
5593 sizeof (struct GNUNET_MessageHeader);
5596 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5600 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5603 "sending keepalive for tunnel %d\n", t->id.tid);
5605 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5606 msg->header.size = htons (size);
5607 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5608 // FIXME: change type to != MULTICAST
5609 msg->oid = my_full_id;
5610 msg->tid = htonl (t->id.tid);
5611 msg->ttl = htonl (default_ttl);
5612 msg->pid = htonl (t->fwd_pid + 1);
5614 payload = (struct GNUNET_MessageHeader *) &msg[1];
5615 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5616 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5617 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5619 t->path_refresh_task =
5620 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5626 * Function to process paths received for a new peer addition. The recorded
5627 * paths form the initial tunnel, which can be optimized later.
5628 * Called on each result obtained for the DHT search.
5630 * @param cls closure
5631 * @param exp when will this value expire
5632 * @param key key of the result
5633 * @param get_path path of the get request
5634 * @param get_path_length lenght of get_path
5635 * @param put_path path of the put request
5636 * @param put_path_length length of the put_path
5637 * @param type type of the result
5638 * @param size number of bytes in data
5639 * @param data pointer to the result data
5641 * TODO: re-issue the request after certain time? cancel after X results?
5644 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5645 const struct GNUNET_HashCode * key,
5646 const struct GNUNET_PeerIdentity *get_path,
5647 unsigned int get_path_length,
5648 const struct GNUNET_PeerIdentity *put_path,
5649 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5650 size_t size, const void *data)
5652 struct MeshPathInfo *path_info = cls;
5653 struct MeshPeerPath *p;
5654 struct GNUNET_PeerIdentity pi;
5657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5658 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5661 p = path_build_from_dht (get_path, get_path_length, put_path,
5663 path_add_to_peers (p, GNUNET_NO);
5665 for (i = 0; i < path_info->peer->ntunnels; i++)
5667 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5668 peer_info_connect (path_info->peer, path_info->t);
5676 * Function to process paths received for a new peer addition. The recorded
5677 * paths form the initial tunnel, which can be optimized later.
5678 * Called on each result obtained for the DHT search.
5680 * @param cls closure
5681 * @param exp when will this value expire
5682 * @param key key of the result
5683 * @param get_path path of the get request
5684 * @param get_path_length lenght of get_path
5685 * @param put_path path of the put request
5686 * @param put_path_length length of the put_path
5687 * @param type type of the result
5688 * @param size number of bytes in data
5689 * @param data pointer to the result data
5692 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5693 const struct GNUNET_HashCode * key,
5694 const struct GNUNET_PeerIdentity *get_path,
5695 unsigned int get_path_length,
5696 const struct GNUNET_PeerIdentity *put_path,
5697 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5698 size_t size, const void *data)
5700 const struct PBlock *pb = data;
5701 const struct GNUNET_PeerIdentity *pi = &pb->id;
5702 struct MeshTunnel *t = cls;
5703 struct MeshPeerInfo *peer_info;
5704 struct MeshPeerPath *p;
5706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5707 if (size != sizeof (struct PBlock))
5709 GNUNET_break_op (0);
5712 if (ntohl(pb->type) != t->type)
5714 GNUNET_break_op (0);
5717 GNUNET_assert (NULL != t->owner);
5718 peer_info = peer_info_get (pi);
5719 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5721 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5723 p = path_build_from_dht (get_path, get_path_length, put_path,
5725 path_add_to_peers (p, GNUNET_NO);
5727 tunnel_add_peer (t, peer_info);
5728 peer_info_connect (peer_info, t);
5733 * Function to process DHT string to regex matching.
5734 * Called on each result obtained for the DHT search.
5736 * @param cls closure (search context)
5737 * @param exp when will this value expire
5738 * @param key key of the result
5739 * @param get_path path of the get request (not used)
5740 * @param get_path_length lenght of get_path (not used)
5741 * @param put_path path of the put request (not used)
5742 * @param put_path_length length of the put_path (not used)
5743 * @param type type of the result
5744 * @param size number of bytes in data
5745 * @param data pointer to the result data
5748 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5749 const struct GNUNET_HashCode * key,
5750 const struct GNUNET_PeerIdentity *get_path,
5751 unsigned int get_path_length,
5752 const struct GNUNET_PeerIdentity *put_path,
5753 unsigned int put_path_length,
5754 enum GNUNET_BLOCK_Type type,
5755 size_t size, const void *data)
5757 const struct MeshRegexAccept *block = data;
5758 struct MeshRegexSearchContext *ctx = cls;
5759 struct MeshRegexSearchInfo *info = ctx->info;
5760 struct MeshPeerPath *p;
5761 struct MeshPeerInfo *peer_info;
5763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5766 peer_info = peer_info_get(&block->id);
5767 p = path_build_from_dht (get_path, get_path_length, put_path,
5769 path_add_to_peers (p, GNUNET_NO);
5772 tunnel_add_peer (info->t, peer_info);
5773 peer_info_connect (peer_info, info->t);
5774 if (0 == info->peer)
5776 info->peer = peer_info->id;
5780 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5783 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5784 ®ex_connect_timeout,
5792 * Function to process DHT string to regex matching.
5793 * Called on each result obtained for the DHT search.
5795 * @param cls closure (search context)
5796 * @param exp when will this value expire
5797 * @param key key of the result
5798 * @param get_path path of the get request (not used)
5799 * @param get_path_length lenght of get_path (not used)
5800 * @param put_path path of the put request (not used)
5801 * @param put_path_length length of the put_path (not used)
5802 * @param type type of the result
5803 * @param size number of bytes in data
5804 * @param data pointer to the result data
5806 * TODO: re-issue the request after certain time? cancel after X results?
5809 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5810 const struct GNUNET_HashCode * key,
5811 const struct GNUNET_PeerIdentity *get_path,
5812 unsigned int get_path_length,
5813 const struct GNUNET_PeerIdentity *put_path,
5814 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5815 size_t size, const void *data)
5817 const struct MeshRegexBlock *block = data;
5818 struct MeshRegexSearchContext *ctx = cls;
5819 struct MeshRegexSearchInfo *info = ctx->info;
5823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5824 "DHT GET STRING RETURNED RESULTS\n");
5825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5826 " key: %s\n", GNUNET_h2s (key));
5828 copy = GNUNET_malloc (size);
5829 memcpy (copy, data, size);
5830 GNUNET_break (GNUNET_OK ==
5831 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5833 len = ntohl (block->n_proof);
5835 char proof[len + 1];
5837 memcpy (proof, &block[1], len);
5839 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5841 GNUNET_break_op (0);
5845 len = strlen (info->description);
5846 if (len == ctx->position) // String processed
5848 if (GNUNET_YES == ntohl (block->accepting))
5850 regex_find_path(key, ctx);
5854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5855 // FIXME REGEX this block not successful, wait for more? start timeout?
5859 GNUNET_break (GNUNET_OK ==
5860 GNUNET_MESH_regex_block_iterate (block, size,
5861 ®ex_edge_iterator, ctx));
5865 /******************************************************************************/
5866 /********************* MESH LOCAL HANDLES **************************/
5867 /******************************************************************************/
5871 * Handler for client disconnection
5873 * @param cls closure
5874 * @param client identification of the client; NULL
5875 * for the last call when the server is destroyed
5878 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5880 struct MeshClient *c;
5881 struct MeshClient *next;
5884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5893 if (c->handle != client)
5895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5901 GNUNET_SERVER_client_drop (c->handle);
5902 c->shutting_down = GNUNET_YES;
5903 GNUNET_assert (NULL != c->own_tunnels);
5904 GNUNET_assert (NULL != c->incoming_tunnels);
5905 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5906 &tunnel_destroy_iterator, c);
5907 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5908 &tunnel_destroy_iterator, c);
5909 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5910 &tunnel_destroy_iterator, c);
5911 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5912 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5913 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5915 /* deregister clients applications */
5916 if (NULL != c->apps)
5918 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5919 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5921 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5922 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5924 GNUNET_SCHEDULER_cancel (announce_applications_task);
5925 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5927 if (NULL != c->types)
5928 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5929 for (i = 0; i < c->n_regex; i++)
5931 GNUNET_free (c->regexes[i]);
5933 GNUNET_free_non_null (c->regexes);
5934 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5935 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5937 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5940 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5949 * Handler for new clients
5951 * @param cls closure
5952 * @param client identification of the client
5953 * @param message the actual message, which includes messages the client wants
5956 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5957 const struct GNUNET_MessageHeader *message)
5959 struct GNUNET_MESH_ClientConnect *cc_msg;
5960 struct MeshClient *c;
5961 GNUNET_MESH_ApplicationType *a;
5968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5969 /* Check data sanity */
5970 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5971 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5972 ntypes = ntohs (cc_msg->types);
5973 napps = ntohs (cc_msg->applications);
5975 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5982 /* Create new client structure */
5983 c = GNUNET_malloc (sizeof (struct MeshClient));
5984 c->id = next_client_id++;
5985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5987 GNUNET_SERVER_client_keep (client);
5988 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5991 GNUNET_MESH_ApplicationType at;
5992 struct GNUNET_HashCode hc;
5994 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5995 for (i = 0; i < napps; i++)
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5999 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6000 /* store in clients hashmap */
6001 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6002 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6003 /* store in global hashmap, for announcements */
6004 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6005 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6007 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6008 announce_applications_task =
6009 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6015 struct GNUNET_HashCode hc;
6017 t = (uint16_t *) & a[napps];
6018 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6019 for (i = 0; i < ntypes; i++)
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6023 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6025 /* store in clients hashmap */
6026 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6027 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6028 /* store in global hashmap */
6029 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6030 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6034 " client has %u+%u subscriptions\n", napps, ntypes);
6036 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6037 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6038 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6039 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6040 GNUNET_SERVER_notification_context_add (nc, client);
6041 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6043 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6049 * Handler for clients announcing available services by a regular expression.
6051 * @param cls closure
6052 * @param client identification of the client
6053 * @param message the actual message, which includes messages the client wants
6056 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6057 const struct GNUNET_MessageHeader *message)
6059 struct MeshClient *c;
6063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6065 /* Sanity check for client registration */
6066 if (NULL == (c = client_get (client)))
6069 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6074 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6075 regex = GNUNET_malloc (len + 1);
6076 memcpy (regex, &message[1], len);
6078 GNUNET_array_append (c->regexes, c->n_regex, regex);
6079 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6081 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6087 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6093 * Handler for requests of new tunnels
6095 * @param cls closure
6096 * @param client identification of the client
6097 * @param message the actual message
6100 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6101 const struct GNUNET_MessageHeader *message)
6103 struct GNUNET_MESH_TunnelMessage *t_msg;
6104 struct MeshTunnel *t;
6105 struct MeshClient *c;
6106 MESH_TunnelNumber tid;
6108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6110 /* Sanity check for client registration */
6111 if (NULL == (c = client_get (client)))
6114 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6119 /* Message sanity check */
6120 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6123 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6127 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6128 /* Sanity check for tunnel numbering */
6129 tid = ntohl (t_msg->tunnel_id);
6130 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6136 /* Sanity check for duplicate tunnel IDs */
6137 if (NULL != tunnel_get_by_local_id (c, tid))
6140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6144 while (NULL != tunnel_get_by_pi (myid, next_tid))
6145 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6146 t = tunnel_new (myid, next_tid++, c, tid);
6149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6154 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6156 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6157 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6166 * Handler for requests of deleting tunnels
6168 * @param cls closure
6169 * @param client identification of the client
6170 * @param message the actual message
6173 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6174 const struct GNUNET_MessageHeader *message)
6176 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6177 struct MeshClient *c;
6178 struct MeshTunnel *t;
6179 MESH_TunnelNumber tid;
6181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6182 "Got a DESTROY TUNNEL from client!\n");
6184 /* Sanity check for client registration */
6185 if (NULL == (c = client_get (client)))
6188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6193 /* Message sanity check */
6194 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6201 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6203 /* Retrieve tunnel */
6204 tid = ntohl (tunnel_msg->tunnel_id);
6205 t = tunnel_get_by_local_id(c, tid);
6208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6213 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6215 client_ignore_tunnel (c, t);
6217 // TODO: when to destroy incoming tunnel?
6218 if (t->nclients == 0)
6220 GNUNET_assert (GNUNET_YES ==
6221 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6223 GNUNET_assert (GNUNET_YES ==
6224 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6225 &my_full_id.hashPubKey,
6229 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6232 send_client_tunnel_disconnect(t, c);
6233 client_delete_tunnel(c, t);
6235 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6237 tunnel_send_destroy (t);
6238 t->destroy = GNUNET_YES;
6239 // The tunnel will be destroyed when the last message is transmitted.
6240 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6246 * Handler for requests of seeting tunnel's speed.
6248 * @param cls Closure (unused).
6249 * @param client Identification of the client.
6250 * @param message The actual message.
6253 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6254 const struct GNUNET_MessageHeader *message)
6256 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6257 struct MeshClient *c;
6258 struct MeshTunnel *t;
6259 MESH_TunnelNumber tid;
6261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6262 "Got a SPEED request from client!\n");
6264 /* Sanity check for client registration */
6265 if (NULL == (c = client_get (client)))
6268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6274 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6276 /* Retrieve tunnel */
6277 tid = ntohl (tunnel_msg->tunnel_id);
6278 t = tunnel_get_by_local_id(c, tid);
6281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6287 switch (ntohs(message->type))
6289 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6290 t->speed_min = GNUNET_YES;
6292 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6293 t->speed_min = GNUNET_NO;
6298 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6303 * Handler for requests of seeting tunnel's buffering policy.
6305 * @param cls Closure (unused).
6306 * @param client Identification of the client.
6307 * @param message The actual message.
6310 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6311 const struct GNUNET_MessageHeader *message)
6313 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6314 struct MeshClient *c;
6315 struct MeshTunnel *t;
6316 MESH_TunnelNumber tid;
6318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6319 "Got a BUFFER request from client!\n");
6321 /* Sanity check for client registration */
6322 if (NULL == (c = client_get (client)))
6325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6330 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6332 /* Retrieve tunnel */
6333 tid = ntohl (tunnel_msg->tunnel_id);
6334 t = tunnel_get_by_local_id(c, tid);
6337 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6339 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6343 switch (ntohs(message->type))
6345 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6346 t->nobuffer = GNUNET_NO;
6348 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6349 t->nobuffer = GNUNET_YES;
6355 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6360 * Handler for connection requests to new peers
6362 * @param cls closure
6363 * @param client identification of the client
6364 * @param message the actual message (PeerControl)
6367 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6368 const struct GNUNET_MessageHeader *message)
6370 struct GNUNET_MESH_PeerControl *peer_msg;
6371 struct MeshPeerInfo *peer_info;
6372 struct MeshClient *c;
6373 struct MeshTunnel *t;
6374 MESH_TunnelNumber tid;
6376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6377 /* Sanity check for client registration */
6378 if (NULL == (c = client_get (client)))
6381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6386 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6388 /* Sanity check for message size */
6389 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6392 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6396 /* Tunnel exists? */
6397 tid = ntohl (peer_msg->tunnel_id);
6398 t = tunnel_get_by_local_id (c, tid);
6402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6406 /* Does client own tunnel? */
6407 if (t->owner->handle != client)
6410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6414 GNUNET_i2s (&peer_msg->peer));
6415 peer_info = peer_info_get (&peer_msg->peer);
6417 tunnel_add_peer (t, peer_info);
6418 peer_info_connect (peer_info, t);
6420 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6426 * Handler for disconnection requests of peers in a tunnel
6428 * @param cls closure
6429 * @param client identification of the client
6430 * @param message the actual message (PeerControl)
6433 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6434 const struct GNUNET_MessageHeader *message)
6436 struct GNUNET_MESH_PeerControl *peer_msg;
6437 struct MeshPeerInfo *peer_info;
6438 struct MeshClient *c;
6439 struct MeshTunnel *t;
6440 MESH_TunnelNumber tid;
6442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6443 /* Sanity check for client registration */
6444 if (NULL == (c = client_get (client)))
6447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6452 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6454 /* Sanity check for message size */
6455 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6458 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6462 /* Tunnel exists? */
6463 tid = ntohl (peer_msg->tunnel_id);
6464 t = tunnel_get_by_local_id (c, tid);
6468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6473 /* Does client own tunnel? */
6474 if (t->owner->handle != client)
6477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6482 GNUNET_i2s (&peer_msg->peer));
6483 /* Is the peer in the tunnel? */
6485 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6486 if (NULL == peer_info)
6489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6493 /* Ok, delete peer from tunnel */
6494 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6495 &peer_msg->peer.hashPubKey);
6497 send_destroy_path (t, peer_info->id);
6498 tunnel_delete_peer (t, peer_info->id);
6499 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6504 * Handler for blacklist requests of peers in a tunnel
6506 * @param cls closure
6507 * @param client identification of the client
6508 * @param message the actual message (PeerControl)
6510 * FIXME implement DHT block bloomfilter
6513 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6514 const struct GNUNET_MessageHeader *message)
6516 struct GNUNET_MESH_PeerControl *peer_msg;
6517 struct MeshClient *c;
6518 struct MeshTunnel *t;
6519 MESH_TunnelNumber tid;
6521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6522 /* Sanity check for client registration */
6523 if (NULL == (c = client_get (client)))
6526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6531 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6533 /* Sanity check for message size */
6534 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6537 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6541 /* Tunnel exists? */
6542 tid = ntohl (peer_msg->tunnel_id);
6543 t = tunnel_get_by_local_id (c, tid);
6547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6552 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6553 GNUNET_PEER_intern(&peer_msg->peer));
6558 * Handler for unblacklist requests of peers in a tunnel
6560 * @param cls closure
6561 * @param client identification of the client
6562 * @param message the actual message (PeerControl)
6565 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6566 const struct GNUNET_MessageHeader *message)
6568 struct GNUNET_MESH_PeerControl *peer_msg;
6569 struct MeshClient *c;
6570 struct MeshTunnel *t;
6571 MESH_TunnelNumber tid;
6575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6576 /* Sanity check for client registration */
6577 if (NULL == (c = client_get (client)))
6580 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6585 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6587 /* Sanity check for message size */
6588 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6591 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6595 /* Tunnel exists? */
6596 tid = ntohl (peer_msg->tunnel_id);
6597 t = tunnel_get_by_local_id (c, tid);
6601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6606 /* if peer is not known, complain */
6607 pid = GNUNET_PEER_search (&peer_msg->peer);
6614 /* search and remove from list */
6615 for (i = 0; i < t->nblacklisted; i++)
6617 if (t->blacklisted[i] == pid)
6619 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6620 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6625 /* if peer hasn't been blacklisted, complain */
6631 * Handler for connection requests to new peers by type
6633 * @param cls closure
6634 * @param client identification of the client
6635 * @param message the actual message (ConnectPeerByType)
6638 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6639 const struct GNUNET_MessageHeader *message)
6641 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6642 struct MeshClient *c;
6643 struct MeshTunnel *t;
6644 struct GNUNET_HashCode hash;
6645 MESH_TunnelNumber tid;
6647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6648 /* Sanity check for client registration */
6649 if (NULL == (c = client_get (client)))
6652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6657 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6659 /* Sanity check for message size */
6660 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6661 ntohs (connect_msg->header.size))
6664 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6668 /* Tunnel exists? */
6669 tid = ntohl (connect_msg->tunnel_id);
6670 t = tunnel_get_by_local_id (c, tid);
6674 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6678 /* Does client own tunnel? */
6679 if (t->owner->handle != client)
6682 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6686 /* Do WE have the service? */
6687 t->type = ntohl (connect_msg->type);
6688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6689 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6690 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6693 /* Yes! Fast forward, add ourselves to the tunnel and send the
6694 * good news to the client, and alert the destination client of
6695 * an incoming tunnel.
6697 * FIXME send a path create to self, avoid code duplication
6699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6700 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6701 peer_info_get (&my_full_id),
6702 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6705 send_client_peer_connected (t, myid);
6706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6707 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6709 t->local_tid_dest = next_local_tid++;
6710 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6711 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6712 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6716 /* Ok, lets find a peer offering the service */
6717 if (NULL != t->dht_get_type)
6719 GNUNET_DHT_get_stop (t->dht_get_type);
6721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6722 GNUNET_h2s (&hash));
6724 GNUNET_DHT_get_start (dht_handle,
6725 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6727 dht_replication_level,
6728 GNUNET_DHT_RO_RECORD_ROUTE |
6729 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6731 &dht_get_type_handler, t);
6733 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6739 * Handler for connection requests to new peers by a string service description.
6741 * @param cls closure
6742 * @param client identification of the client
6743 * @param message the actual message, which includes messages the client wants
6746 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6747 const struct GNUNET_MessageHeader *message)
6749 struct GNUNET_MESH_ConnectPeerByString *msg;
6750 struct MeshRegexSearchContext *ctx;
6751 struct MeshRegexSearchInfo *info;
6752 struct GNUNET_DHT_GetHandle *get_h;
6753 struct GNUNET_HashCode key;
6754 struct MeshTunnel *t;
6755 struct MeshClient *c;
6756 MESH_TunnelNumber tid;
6760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6761 "Connect by string started\n");
6762 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6763 size = htons (message->size);
6765 /* Sanity check for client registration */
6766 if (NULL == (c = client_get (client)))
6769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6774 /* Message size sanity check */
6775 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6782 /* Tunnel exists? */
6783 tid = ntohl (msg->tunnel_id);
6784 t = tunnel_get_by_local_id (c, tid);
6788 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6792 /* Does client own tunnel? */
6793 if (t->owner->handle != client)
6796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6801 " on tunnel %s [%u]\n",
6802 GNUNET_i2s(&my_full_id),
6805 /* Only one connect_by_string allowed at the same time! */
6806 /* FIXME: allow more, return handle at api level to cancel, document */
6807 if (NULL != t->regex_ctx)
6810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6814 /* Find string itself */
6815 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6816 string = (const char *) &msg[1];
6818 /* Initialize context */
6819 size = GNUNET_REGEX_get_first_key(string, len, &key);
6820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6821 " consumed %u bits out of %u\n", size, len);
6822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6823 " looking for %s\n", GNUNET_h2s (&key));
6825 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6827 info->description = GNUNET_malloc (len + 1);
6828 memcpy (info->description, string, len);
6829 info->description[len] = '\0';
6830 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6831 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6834 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6835 ctx->position = size;
6839 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6841 /* Start search in DHT */
6842 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6843 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6844 &key, /* key to search */
6845 dht_replication_level, /* replication level */
6846 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6847 NULL, /* xquery */ // FIXME BLOOMFILTER
6848 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6849 &dht_get_string_handler, ctx);
6851 GNUNET_break (GNUNET_OK ==
6852 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6855 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6857 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6863 * Handler for client traffic directed to one peer
6865 * @param cls closure
6866 * @param client identification of the client
6867 * @param message the actual message
6870 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6871 const struct GNUNET_MessageHeader *message)
6873 struct MeshClient *c;
6874 struct MeshTunnel *t;
6875 struct MeshPeerInfo *pi;
6876 struct GNUNET_MESH_Unicast *data_msg;
6877 MESH_TunnelNumber tid;
6880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6881 "Got a unicast request from a client!\n");
6883 /* Sanity check for client registration */
6884 if (NULL == (c = client_get (client)))
6887 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6892 data_msg = (struct GNUNET_MESH_Unicast *) message;
6894 /* Sanity check for message size */
6895 size = ntohs (message->size);
6896 if (sizeof (struct GNUNET_MESH_Unicast) +
6897 sizeof (struct GNUNET_MessageHeader) > size)
6900 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6904 /* Tunnel exists? */
6905 tid = ntohl (data_msg->tid);
6906 t = tunnel_get_by_local_id (c, tid);
6910 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6914 /* Is it a local tunnel? Then, does client own the tunnel? */
6915 if (t->owner->handle != client)
6918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6922 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6923 &data_msg->destination.hashPubKey);
6924 /* Is the selected peer in the tunnel? */
6928 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6932 /* PID should be as expected */
6933 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6936 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6937 "Unicast PID, expected %u, got %u\n",
6938 t->fwd_pid + 1, ntohl (data_msg->pid));
6939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6943 /* Ok, everything is correct, send the message
6944 * (pretend we got it from a mesh peer)
6947 /* Work around const limitation */
6948 char buf[ntohs (message->size)] GNUNET_ALIGN;
6949 struct GNUNET_MESH_Unicast *copy;
6951 copy = (struct GNUNET_MESH_Unicast *) buf;
6952 memcpy (buf, data_msg, size);
6953 copy->oid = my_full_id;
6954 copy->tid = htonl (t->id.tid);
6955 copy->ttl = htonl (default_ttl);
6956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6957 " calling generic handler...\n");
6958 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6961 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6968 * Handler for client traffic directed to the origin
6970 * @param cls closure
6971 * @param client identification of the client
6972 * @param message the actual message
6975 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6976 const struct GNUNET_MessageHeader *message)
6978 struct GNUNET_MESH_ToOrigin *data_msg;
6979 struct MeshTunnelClientInfo *clinfo;
6980 struct MeshClient *c;
6981 struct MeshTunnel *t;
6982 MESH_TunnelNumber tid;
6985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6986 "Got a ToOrigin request from a client!\n");
6987 /* Sanity check for client registration */
6988 if (NULL == (c = client_get (client)))
6991 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6996 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6998 /* Sanity check for message size */
6999 size = ntohs (message->size);
7000 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7001 sizeof (struct GNUNET_MessageHeader) > size)
7004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7008 /* Tunnel exists? */
7009 tid = ntohl (data_msg->tid);
7010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7011 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7017 t = tunnel_get_by_local_id (c, tid);
7020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7021 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7023 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is owner)\n");
7025 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is leaf)\n");
7027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7031 /* It should be sent by someone who has this as incoming tunnel. */
7032 if (GNUNET_NO == client_knows_tunnel (c, t))
7035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7039 /* PID should be as expected */
7040 clinfo = tunnel_get_client_fc (t, c);
7041 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7044 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7045 "To Origin PID, expected %u, got %u\n",
7046 clinfo->bck_pid + 1,
7047 ntohl (data_msg->pid));
7048 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7052 /* Ok, everything is correct, send the message
7053 * (pretend we got it from a mesh peer)
7057 char buf[ntohs (message->size)] GNUNET_ALIGN;
7058 struct GNUNET_MESH_ToOrigin *copy;
7060 /* Work around const limitation */
7061 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7062 memcpy (buf, data_msg, size);
7063 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7064 copy->tid = htonl (t->id.tid);
7065 copy->ttl = htonl (default_ttl);
7066 if (ntohl (copy->pid) != (t->bck_pid + 1))
7069 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7070 "To Origin PID, expected %u, got %u\n",
7076 copy->sender = my_full_id;
7077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7078 " calling generic handler...\n");
7079 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7081 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7088 * Handler for client traffic directed to all peers in a tunnel
7090 * @param cls closure
7091 * @param client identification of the client
7092 * @param message the actual message
7095 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7096 const struct GNUNET_MessageHeader *message)
7098 struct MeshClient *c;
7099 struct MeshTunnel *t;
7100 struct GNUNET_MESH_Multicast *data_msg;
7101 MESH_TunnelNumber tid;
7103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7104 "Got a multicast request from a client!\n");
7106 /* Sanity check for client registration */
7107 if (NULL == (c = client_get (client)))
7110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7115 data_msg = (struct GNUNET_MESH_Multicast *) message;
7117 /* Sanity check for message size */
7118 if (sizeof (struct GNUNET_MESH_Multicast) +
7119 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7122 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7126 /* Tunnel exists? */
7127 tid = ntohl (data_msg->tid);
7128 t = tunnel_get_by_local_id (c, tid);
7132 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7135 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is owner)\n");
7137 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is leaf)\n"); GNUNET_break (0);
7138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7142 /* Does client own tunnel? */
7143 if (t->owner->handle != client)
7146 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7150 /* PID should be as expected */
7151 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7155 "Multicast PID, expected %u, got %u\n",
7156 t->fwd_pid + 1, ntohl (data_msg->pid));
7157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7162 char buf[ntohs (message->size)] GNUNET_ALIGN;
7163 struct GNUNET_MESH_Multicast *copy;
7165 copy = (struct GNUNET_MESH_Multicast *) buf;
7166 memcpy (buf, message, ntohs (message->size));
7167 copy->oid = my_full_id;
7168 copy->tid = htonl (t->id.tid);
7169 copy->ttl = htonl (default_ttl);
7170 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7172 " calling generic handler...\n");
7173 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7176 /* receive done gets called when last copy is sent to a neighbor */
7182 * Handler for client's ACKs for payload traffic.
7184 * @param cls Closure (unused).
7185 * @param client Identification of the client.
7186 * @param message The actual message.
7189 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7190 const struct GNUNET_MessageHeader *message)
7192 struct GNUNET_MESH_LocalAck *msg;
7193 struct MeshTunnel *t;
7194 struct MeshClient *c;
7195 MESH_TunnelNumber tid;
7198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7199 /* Sanity check for client registration */
7200 if (NULL == (c = client_get (client)))
7203 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7208 msg = (struct GNUNET_MESH_LocalAck *) message;
7210 /* Tunnel exists? */
7211 tid = ntohl (msg->tunnel_id);
7212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7213 t = tunnel_get_by_local_id (c, tid);
7216 GNUNET_break (0); // FIXME fc
7217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7220 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is owner)\n");
7222 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is leaf)\n");
7223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7227 ack = ntohl (msg->max_pid);
7228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7230 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7231 if (NULL != t->owner && t->owner->handle == client)
7233 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7235 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7239 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7240 tunnel_set_client_fwd_ack (t, c, ack);
7241 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7244 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7251 * Functions to handle messages from clients
7253 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7254 {&handle_local_new_client, NULL,
7255 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7256 {&handle_local_announce_regex, NULL,
7257 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7258 {&handle_local_tunnel_create, NULL,
7259 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7260 sizeof (struct GNUNET_MESH_TunnelMessage)},
7261 {&handle_local_tunnel_destroy, NULL,
7262 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7263 sizeof (struct GNUNET_MESH_TunnelMessage)},
7264 {&handle_local_tunnel_speed, NULL,
7265 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7266 sizeof (struct GNUNET_MESH_TunnelMessage)},
7267 {&handle_local_tunnel_speed, NULL,
7268 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7269 sizeof (struct GNUNET_MESH_TunnelMessage)},
7270 {&handle_local_tunnel_buffer, NULL,
7271 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7272 sizeof (struct GNUNET_MESH_TunnelMessage)},
7273 {&handle_local_tunnel_buffer, NULL,
7274 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7275 sizeof (struct GNUNET_MESH_TunnelMessage)},
7276 {&handle_local_connect_add, NULL,
7277 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7278 sizeof (struct GNUNET_MESH_PeerControl)},
7279 {&handle_local_connect_del, NULL,
7280 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7281 sizeof (struct GNUNET_MESH_PeerControl)},
7282 {&handle_local_blacklist, NULL,
7283 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7284 sizeof (struct GNUNET_MESH_PeerControl)},
7285 {&handle_local_unblacklist, NULL,
7286 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7287 sizeof (struct GNUNET_MESH_PeerControl)},
7288 {&handle_local_connect_by_type, NULL,
7289 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7290 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7291 {&handle_local_connect_by_string, NULL,
7292 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7293 {&handle_local_unicast, NULL,
7294 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7295 {&handle_local_to_origin, NULL,
7296 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7297 {&handle_local_multicast, NULL,
7298 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7299 {&handle_local_ack, NULL,
7300 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7301 sizeof (struct GNUNET_MESH_LocalAck)},
7307 * To be called on core init/fail.
7309 * @param cls service closure
7310 * @param server handle to the server for this service
7311 * @param identity the public identity of this peer
7314 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7315 const struct GNUNET_PeerIdentity *identity)
7318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7319 core_handle = server;
7320 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7324 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7326 GNUNET_abort(); // Try harder
7333 * Method called whenever a given peer connects.
7335 * @param cls closure
7336 * @param peer peer identity this notification is about
7337 * @param atsi performance data for the connection
7338 * @param atsi_count number of records in 'atsi'
7341 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7342 const struct GNUNET_ATS_Information *atsi,
7343 unsigned int atsi_count)
7345 struct MeshPeerInfo *peer_info;
7346 struct MeshPeerPath *path;
7348 DEBUG_CONN ("Peer connected\n");
7349 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7350 peer_info = peer_info_get (peer);
7351 if (myid == peer_info->id)
7353 DEBUG_CONN (" (self)\n");
7358 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7360 path = path_new (2);
7361 path->peers[0] = myid;
7362 path->peers[1] = peer_info->id;
7363 GNUNET_PEER_change_rc (myid, 1);
7364 GNUNET_PEER_change_rc (peer_info->id, 1);
7365 peer_info_add_path (peer_info, path, GNUNET_YES);
7366 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7372 * Method called whenever a peer disconnects.
7374 * @param cls closure
7375 * @param peer peer identity this notification is about
7378 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7380 struct MeshPeerInfo *pi;
7381 struct MeshPeerQueue *q;
7382 struct MeshPeerQueue *n;
7384 DEBUG_CONN ("Peer disconnected\n");
7385 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7397 /* try to reroute this traffic instead */
7398 queue_destroy(q, GNUNET_YES);
7402 peer_info_remove_path (pi, pi->id, myid);
7405 DEBUG_CONN (" (self)\n");
7407 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7412 /******************************************************************************/
7413 /************************ MAIN FUNCTIONS ****************************/
7414 /******************************************************************************/
7417 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7419 * @param cls closure
7420 * @param key current key code
7421 * @param value value in the hash map
7422 * @return GNUNET_YES if we should continue to iterate,
7426 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7428 struct MeshTunnel *t = value;
7435 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7437 * @param cls closure
7438 * @param key current key code
7439 * @param value value in the hash map
7440 * @return GNUNET_YES if we should continue to iterate,
7444 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7446 struct MeshPeerInfo *p = value;
7447 struct MeshPeerQueue *q;
7448 struct MeshPeerQueue *n;
7456 queue_destroy(q, GNUNET_YES);
7460 peer_info_destroy (p);
7465 * Task run during shutdown.
7471 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7475 if (core_handle != NULL)
7477 GNUNET_CORE_disconnect (core_handle);
7480 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7481 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7482 if (dht_handle != NULL)
7484 GNUNET_DHT_disconnect (dht_handle);
7489 GNUNET_SERVER_notification_context_destroy (nc);
7492 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7494 GNUNET_SCHEDULER_cancel (announce_id_task);
7495 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7501 * Process mesh requests.
7503 * @param cls closure
7504 * @param server the initialized server
7505 * @param c configuration to use
7508 run (void *cls, struct GNUNET_SERVER_Handle *server,
7509 const struct GNUNET_CONFIGURATION_Handle *c)
7511 struct MeshPeerInfo *peer;
7512 struct MeshPeerPath *p;
7515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7516 server_handle = server;
7517 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7518 NULL, /* Closure passed to MESH functions */
7519 &core_init, /* Call core_init once connected */
7520 &core_connect, /* Handle connects */
7521 &core_disconnect, /* remove peers on disconnects */
7522 NULL, /* Don't notify about all incoming messages */
7523 GNUNET_NO, /* For header only in notification */
7524 NULL, /* Don't notify about all outbound messages */
7525 GNUNET_NO, /* For header-only out notification */
7526 core_handlers); /* Register these handlers */
7528 if (core_handle == NULL)
7531 GNUNET_SCHEDULER_shutdown ();
7536 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7541 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7543 GNUNET_SCHEDULER_shutdown ();
7548 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7549 &refresh_path_time))
7551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7553 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7554 "refresh path time");
7555 GNUNET_SCHEDULER_shutdown ();
7560 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7561 &app_announce_time))
7563 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7565 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7566 "app announce time");
7567 GNUNET_SCHEDULER_shutdown ();
7572 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7575 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7577 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7578 "id announce time");
7579 GNUNET_SCHEDULER_shutdown ();
7584 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7585 &unacknowledged_wait_time))
7587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7589 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7590 "unacknowledged wait time");
7591 GNUNET_SCHEDULER_shutdown ();
7596 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7601 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7603 GNUNET_SCHEDULER_shutdown ();
7608 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7613 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7615 GNUNET_SCHEDULER_shutdown ();
7620 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7625 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7627 GNUNET_SCHEDULER_shutdown ();
7632 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7635 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7637 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7643 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7644 &dht_replication_level))
7646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7648 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7649 "dht replication level", 10);
7650 dht_replication_level = 10;
7654 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7655 GNUNET_free (keyfile);
7656 if (my_private_key == NULL)
7658 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7659 _("Mesh service could not access hostkey. Exiting.\n"));
7660 GNUNET_SCHEDULER_shutdown ();
7663 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7664 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7665 &my_full_id.hashPubKey);
7666 myid = GNUNET_PEER_intern (&my_full_id);
7667 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7668 "Mesh for peer [%s] starting\n",
7669 GNUNET_i2s(&my_full_id));
7671 // transport_handle = GNUNET_TRANSPORT_connect(c,
7678 dht_handle = GNUNET_DHT_connect (c, 64);
7679 if (dht_handle == NULL)
7684 stats = GNUNET_STATISTICS_create ("mesh", c);
7688 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7690 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7691 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7692 peers = GNUNET_CONTAINER_multihashmap_create (32);
7693 applications = GNUNET_CONTAINER_multihashmap_create (32);
7694 types = GNUNET_CONTAINER_multihashmap_create (32);
7696 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7697 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7698 GNUNET_SERVER_disconnect_notify (server_handle,
7699 &handle_local_client_disconnect, NULL);
7703 clients_tail = NULL;
7706 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7707 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7709 /* Create a peer_info for the local peer */
7710 peer = peer_info_get (&my_full_id);
7713 GNUNET_PEER_change_rc (myid, 1);
7714 peer_info_add_path (peer, p, GNUNET_YES);
7716 /* Scheduled the task to clean up when shutdown is called */
7717 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7724 * The main function for the mesh service.
7726 * @param argc number of arguments from the command line
7727 * @param argv command line arguments
7728 * @return 0 ok, 1 on error
7731 main (int argc, char *const *argv)
7735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7738 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");