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);
1153 * Free a transmission that was already queued with all resources
1154 * associated to the request.
1156 * @param queue Queue handler to cancel.
1157 * @param clear_cls Is it necessary to free associated cls?
1160 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1164 * @brief Get the next transmittable message from the queue.
1166 * This will be the head, except in the case of being a data packet
1167 * not allowed by the destination peer.
1169 * @param peer Destination peer.
1171 * @return The next viable MeshPeerQueue element to send to that peer.
1172 * NULL when there are no transmittable messages.
1174 struct MeshPeerQueue *
1175 queue_get_next (const struct MeshPeerInfo *peer);
1179 * Core callback to write a queued packet to core buffer
1181 * @param cls Closure (peer info).
1182 * @param size Number of bytes available in buf.
1183 * @param buf Where the to write the message.
1185 * @return number of bytes written to buf
1188 queue_send (void *cls, size_t size, void *buf);
1190 /******************************************************************************/
1191 /************************ ITERATORS ****************************/
1192 /******************************************************************************/
1195 * Iterator over found existing mesh regex blocks that match an ongoing search.
1197 * @param cls closure
1198 * @param key current key code
1199 * @param value value in the hash map
1200 * @return GNUNET_YES if we should continue to iterate,
1204 regex_result_iterator (void *cls,
1205 const struct GNUNET_HashCode * key,
1208 struct MeshRegexBlock *block = value;
1209 struct MeshRegexSearchContext *ctx = cls;
1211 if (GNUNET_YES == ntohl(block->accepting) &&
1212 ctx->position == strlen (ctx->info->description))
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1215 regex_find_path (key, ctx);
1216 return GNUNET_YES; // We found an accept state!
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1221 ctx->position, strlen(ctx->info->description),
1222 ntohl(block->accepting));
1225 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1226 ®ex_edge_iterator, ctx);
1233 * Iterator over edges in a regex block retrieved from the DHT.
1235 * @param cls Closure (context of the search).
1236 * @param token Token that follows to next state.
1237 * @param len Lenght of token.
1238 * @param key Hash of next state.
1240 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1243 regex_edge_iterator (void *cls,
1246 const struct GNUNET_HashCode *key)
1248 struct MeshRegexSearchContext *ctx = cls;
1249 struct MeshRegexSearchContext *new_ctx;
1250 struct MeshRegexSearchInfo *info = ctx->info;
1251 struct GNUNET_DHT_GetHandle *get_h;
1255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1258 current = &info->description[ctx->position];
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1260 current_len = strlen (info->description) - ctx->position;
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1265 if (len > current_len)
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1268 return GNUNET_YES; // Token too long, wont match
1270 if (0 != strncmp (current, token, len))
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1273 return GNUNET_YES; // Token doesn't match
1275 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1276 new_ctx->info = info;
1277 new_ctx->position = ctx->position + len;
1278 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1280 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1283 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1284 ®ex_result_iterator,
1286 return GNUNET_YES; // We are already looking for it
1288 /* Start search in DHT */
1290 GNUNET_DHT_get_start (dht_handle, /* handle */
1291 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1292 key, /* key to search */
1293 dht_replication_level, /* replication level */
1294 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1295 NULL, /* xquery */ // FIXME BLOOMFILTER
1296 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1297 &dht_get_string_handler, new_ctx);
1299 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1310 * Iterator over hash map entries to cancel DHT GET requests after a
1311 * successful connect_by_string.
1313 * @param cls Closure (unused).
1314 * @param key Current key code (unused).
1315 * @param value Value in the hash map (get handle).
1316 * @return GNUNET_YES if we should continue to iterate,
1320 regex_cancel_dht_get (void *cls,
1321 const struct GNUNET_HashCode * key,
1324 struct GNUNET_DHT_GetHandle *h = value;
1326 GNUNET_DHT_get_stop (h);
1332 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1333 * search for connect_by_string.
1335 * @param cls Closure (unused).
1336 * @param key Current key code (unused).
1337 * @param value MeshRegexBlock in the hash map.
1338 * @return GNUNET_YES if we should continue to iterate,
1342 regex_free_result (void *cls,
1343 const struct GNUNET_HashCode * key,
1347 GNUNET_free (value);
1353 * Regex callback iterator to store own service description in the DHT.
1355 * @param cls closure.
1356 * @param key hash for current state.
1357 * @param proof proof for current state.
1358 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1359 * @param num_edges number of edges leaving current state.
1360 * @param edges edges leaving current state.
1363 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1364 int accepting, unsigned int num_edges,
1365 const struct GNUNET_REGEX_Edge *edges)
1367 struct MeshRegexBlock *block;
1368 struct MeshRegexEdge *block_edge;
1369 enum GNUNET_DHT_RouteOption opt;
1373 unsigned int offset;
1376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377 " regex dht put for state %s\n",
1379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1387 if (GNUNET_YES == accepting)
1389 struct MeshRegexAccept block;
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 " state %s is accepting, putting own id\n",
1394 size = sizeof (block);
1396 block.id = my_full_id;
1398 GNUNET_DHT_put(dht_handle, key,
1399 dht_replication_level,
1400 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1401 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1404 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1409 len = strlen(proof);
1410 size = sizeof (struct MeshRegexBlock) + len;
1411 block = GNUNET_malloc (size);
1414 block->n_proof = htonl (len);
1415 block->n_edges = htonl (num_edges);
1416 block->accepting = htonl (accepting);
1418 /* Store the proof at the end of the block. */
1419 aux = (char *) &block[1];
1420 memcpy (aux, proof, len);
1423 /* Store each edge in a variable length MeshEdge struct at the
1424 * very end of the MeshRegexBlock structure.
1426 for (i = 0; i < num_edges; i++)
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 " edge %s towards %s\n",
1431 GNUNET_h2s(&edges[i].destination));
1433 /* aux points at the end of the last block */
1434 len = strlen (edges[i].label);
1435 size += sizeof (struct MeshRegexEdge) + len;
1436 // Calculate offset FIXME is this ok? use size instead?
1437 offset = aux - (char *) block;
1438 block = GNUNET_realloc (block, size);
1439 aux = &((char *) block)[offset];
1440 block_edge = (struct MeshRegexEdge *) aux;
1441 block_edge->key = edges[i].destination;
1442 block_edge->n_token = htonl (len);
1443 aux = (char *) &block_edge[1];
1444 memcpy (aux, edges[i].label, len);
1448 GNUNET_DHT_put(dht_handle, key,
1449 dht_replication_level,
1451 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1453 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1457 GNUNET_free (block);
1462 * Store the regular expression describing a local service into the DHT.
1464 * @param regex The regular expresion.
1467 regex_put (const char *regex)
1469 struct GNUNET_REGEX_Automaton *dfa;
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1472 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1473 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1474 GNUNET_REGEX_automaton_destroy (dfa);
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1480 * Find a path to a peer that offers a regex servcie compatible
1481 * with a given string.
1483 * @param key The key of the accepting state.
1484 * @param ctx Context containing info about the string, tunnel, etc.
1487 regex_find_path (const struct GNUNET_HashCode *key,
1488 struct MeshRegexSearchContext *ctx)
1490 struct GNUNET_DHT_GetHandle *get_h;
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1493 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1494 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1495 key, /* key to search */
1496 dht_replication_level, /* replication level */
1497 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1498 GNUNET_DHT_RO_RECORD_ROUTE,
1499 NULL, /* xquery */ // FIXME BLOOMFILTER
1500 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1501 &dht_get_string_accept_handler, ctx);
1502 GNUNET_break (GNUNET_OK ==
1503 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1506 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1511 * Function called if the connect attempt to a peer found via
1512 * connect_by_string times out. Try to connect to another peer, if any.
1513 * Otherwise try to reconnect to the same peer.
1515 * @param cls Closure (info about regex search).
1516 * @param tc TaskContext.
1519 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1521 struct MeshRegexSearchInfo *info = cls;
1522 struct MeshPeerInfo *peer_info;
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1527 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1528 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1536 if (0 < info->n_peers)
1538 // Select next peer, put current in that spot.
1539 id = info->peers[info->i_peer];
1540 info->peers[info->i_peer] = info->peer;
1541 info->i_peer = (info->i_peer + 1) % info->n_peers;
1545 // Try to connect to same peer again.
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1550 peer_info = peer_info_get_short(id);
1551 tunnel_add_peer (info->t, peer_info);
1553 tunnel_delete_peer (info->t, old);
1554 peer_info_connect (peer_info, info->t);
1555 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1556 ®ex_connect_timeout,
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1563 * Cancel an ongoing regex search in the DHT and free all resources.
1565 * @param ctx The search context.
1568 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1570 struct MeshRegexSearchInfo *info = ctx->info;
1573 GNUNET_free (info->description);
1574 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1575 ®ex_cancel_dht_get, NULL);
1576 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1577 ®ex_free_result, NULL);
1578 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1579 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1580 info->t->regex_ctx = NULL;
1581 for (i = 0; i < info->n_contexts; i++)
1583 GNUNET_free (info->contexts[i]);
1585 if (0 < info->n_contexts)
1586 GNUNET_free (info->contexts);
1587 if (0 < info->n_peers)
1588 GNUNET_free (info->peers);
1589 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1591 GNUNET_SCHEDULER_cancel(info->timeout);
1597 /******************************************************************************/
1598 /************************ PERIODIC FUNCTIONS ****************************/
1599 /******************************************************************************/
1602 * Announce iterator over for each application provided by the peer
1604 * @param cls closure
1605 * @param key current key code
1606 * @param value value in the hash map
1607 * @return GNUNET_YES if we should continue to
1612 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1614 struct PBlock block;
1615 struct MeshClient *c;
1617 block.id = my_full_id;
1618 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1619 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1620 if (0 == block.type)
1625 block.type = htonl (block.type);
1627 GNUNET_break (NULL !=
1628 GNUNET_DHT_put (dht_handle, key,
1629 dht_replication_level,
1630 GNUNET_DHT_RO_RECORD_ROUTE |
1631 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1632 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1634 (const char *) &block,
1635 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1637 app_announce_time, NULL, NULL));
1643 * Periodically announce what applications are provided by local clients
1646 * @param cls closure
1647 * @param tc task context
1650 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1652 struct MeshClient *c = cls;
1655 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1657 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1661 DEBUG_DHT ("Starting PUT for regex\n");
1663 for (i = 0; i < c->n_regex; i++)
1665 regex_put (c->regexes[i]);
1667 c->regex_announce_task =
1668 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1669 DEBUG_DHT ("Finished PUT for regex\n");
1676 * Periodically announce what applications are provided by local clients
1679 * @param cls closure
1680 * @param tc task context
1683 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1685 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1687 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1691 DEBUG_DHT ("Starting PUT for apps\n");
1693 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1695 announce_applications_task =
1696 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1698 DEBUG_DHT ("Finished PUT for apps\n");
1705 * Periodically announce self id in the DHT
1707 * @param cls closure
1708 * @param tc task context
1711 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1713 struct PBlock block;
1715 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1717 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1721 * - Set data expiration in function of X
1722 * - Adapt X to churn
1724 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1726 block.id = my_full_id;
1727 block.type = htonl (0);
1728 GNUNET_DHT_put (dht_handle, /* DHT handle */
1729 &my_full_id.hashPubKey, /* Key to use */
1730 dht_replication_level, /* Replication level */
1731 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1732 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1733 sizeof (block), /* Size of the data */
1734 (const char *) &block, /* Data itself */
1735 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1736 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1737 NULL, /* Continuation */
1738 NULL); /* Continuation closure */
1740 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1744 /******************************************************************************/
1745 /****************** GENERAL HELPER FUNCTIONS ************************/
1746 /******************************************************************************/
1749 * Decrements the reference counter and frees all resources if needed
1751 * @param mesh_data Data Descriptor used in a multicast message.
1752 * Freed no longer needed (last message).
1755 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1757 /* Make sure it's a multicast packet */
1758 GNUNET_assert (NULL != mesh_data->reference_counter);
1760 if (0 == --(*(mesh_data->reference_counter)))
1762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1763 if (NULL != mesh_data->task)
1765 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1767 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1769 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1771 GNUNET_free (mesh_data->task);
1773 GNUNET_free (mesh_data->reference_counter);
1774 GNUNET_free (mesh_data->data);
1775 GNUNET_free (mesh_data);
1781 * Check if client has registered with the service and has not disconnected
1783 * @param client the client to check
1785 * @return non-NULL if client exists in the global DLL
1787 static struct MeshClient *
1788 client_get (struct GNUNET_SERVER_Client *client)
1790 struct MeshClient *c;
1795 if (c->handle == client)
1804 * Checks if a given client has subscribed to certain message type
1806 * @param message_type Type of message to check
1807 * @param c Client to check
1809 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1811 * FIXME: use of crypto_hash slows it down
1812 * The hash function alone takes 8-10us out of the ~55us for the whole
1813 * process of retransmitting the message from one local client to another.
1814 * Find faster implementation!
1817 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1819 struct GNUNET_HashCode hc;
1821 if (NULL == c->types)
1824 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1825 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1830 * Allow a client to send more data after transmitting a multicast message
1831 * which some neighbor has not yet accepted altough a reasonable time has
1834 * @param cls Closure (DataDescriptor containing the task identifier)
1835 * @param tc Task Context
1837 * FIXME reference counter cshould be just int
1840 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1842 struct MeshData *mdata = cls;
1844 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1846 GNUNET_assert (NULL != mdata->reference_counter);
1847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1848 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1849 *(mdata->reference_counter));
1850 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1851 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1856 * Check whether client wants traffic from a tunnel.
1858 * @param c Client to check.
1859 * @param t Tunnel to be found.
1861 * @return GNUNET_YES if client knows tunnel.
1863 * TODO look in client hashmap
1866 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1870 for (i = 0; i < t->nclients; i++)
1871 if (t->clients[i] == c)
1878 * Check whether client has been informed about a tunnel.
1880 * @param c Client to check.
1881 * @param t Tunnel to be found.
1883 * @return GNUNET_YES if client knows tunnel.
1885 * TODO look in client hashmap
1888 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1892 for (i = 0; i < t->nignore; i++)
1893 if (t->ignore[i] == c)
1895 return client_wants_tunnel(c, t);
1900 * Marks a client as uninterested in traffic from the tunnel, updating both
1901 * client and tunnel to reflect this.
1903 * @param c Client that doesn't want traffic anymore.
1904 * @param t Tunnel which should be ignored.
1906 * FIXME when to delete an incoming tunnel?
1909 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1911 struct GNUNET_HashCode hash;
1913 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1914 GNUNET_break (GNUNET_YES ==
1915 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1917 GNUNET_break (GNUNET_YES ==
1918 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1919 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1920 tunnel_delete_active_client (t, c);
1921 GNUNET_array_append (t->ignore, t->nignore, c);
1926 * Deletes a tunnel from a client (either owner or destination). To be used on
1927 * tunnel destroy, otherwise, use client_ignore_tunnel.
1929 * @param c Client whose tunnel to delete.
1930 * @param t Tunnel which should be deleted.
1933 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1935 struct GNUNET_HashCode hash;
1939 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1940 GNUNET_assert (GNUNET_YES ==
1941 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1947 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1949 GNUNET_assert (GNUNET_YES ==
1950 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1954 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1962 * Send the message to all clients that have subscribed to its type
1964 * @param msg Pointer to the message itself
1965 * @param payload Pointer to the payload of the message.
1966 * @param t The tunnel to whose clients this message goes.
1968 * @return number of clients this message was sent to
1971 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1972 const struct GNUNET_MessageHeader *payload,
1973 struct MeshTunnel *t)
1975 struct MeshClient *c;
1976 MESH_TunnelNumber *tid;
1979 char cbuf[htons (msg->size)];
1981 type = ntohs (payload->type);
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1984 GNUNET_MESH_DEBUG_M2S (type));
1986 memcpy (cbuf, msg, sizeof (cbuf));
1987 switch (htons (msg->type))
1989 struct GNUNET_MESH_Unicast *uc;
1990 struct GNUNET_MESH_Multicast *mc;
1991 struct GNUNET_MESH_ToOrigin *to;
1993 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1994 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1997 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1998 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2001 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2002 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2010 for (count = 0, c = clients; c != NULL; c = c->next)
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2013 if (client_is_subscribed (type, c))
2015 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2019 *tid = htonl (t->local_tid);
2023 if (GNUNET_NO == client_knows_tunnel (c, t))
2025 /* This client doesn't know the tunnel */
2026 struct GNUNET_MESH_TunnelNotification tmsg;
2027 struct GNUNET_HashCode hash;
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2030 tmsg.header.size = htons (sizeof (tmsg));
2031 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2032 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2033 tmsg.tunnel_id = htonl (t->local_tid_dest);
2034 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2035 &tmsg.header, GNUNET_NO);
2036 tunnel_add_client (t, c);
2037 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2039 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2040 c->incoming_tunnels, &hash, t,
2041 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2043 *tid = htonl (t->local_tid_dest);
2046 /* Check if the client wants to get traffic from the tunnel */
2047 if (GNUNET_NO == client_wants_tunnel(c, t))
2050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2051 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2052 (struct GNUNET_MessageHeader
2053 *) cbuf, GNUNET_NO);
2062 * Notify the client that owns the tunnel that a peer has connected to it
2063 * (the requested path to it has been confirmed).
2065 * @param t Tunnel whose owner to notify
2066 * @param id Short id of the peer that has connected
2069 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2071 struct GNUNET_MESH_PeerControl pc;
2073 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2074 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2075 pc.tunnel_id = htonl (t->local_tid);
2076 GNUNET_PEER_resolve (id, &pc.peer);
2077 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2083 * Notify all clients (not depending on registration status) that the incoming
2084 * tunnel is no longer valid.
2086 * @param t Tunnel that was destroyed.
2089 send_clients_tunnel_destroy (struct MeshTunnel *t)
2091 struct GNUNET_MESH_TunnelMessage msg;
2093 msg.header.size = htons (sizeof (msg));
2094 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2095 msg.tunnel_id = htonl (t->local_tid_dest);
2096 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2101 * Notify clients of tunnel disconnections, if needed.
2102 * In case the origin disconnects, the destination clients get a tunnel destroy
2103 * notification. If the last destination disconnects (only one remaining client
2104 * in tunnel), the origin gets a (local ID) peer disconnected.
2105 * Note that the function must be called BEFORE removing the client from
2108 * @param t Tunnel that was destroyed.
2109 * @param c Client that disconnected.
2112 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2118 struct GNUNET_MESH_TunnelMessage msg;
2120 msg.header.size = htons (sizeof (msg));
2121 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2122 msg.tunnel_id = htonl (t->local_tid_dest);
2123 for (i = 0; i < t->nclients; i++)
2124 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2125 &msg.header, GNUNET_NO);
2127 // FIXME when to disconnect an incoming tunnel?
2128 else if (1 == t->nclients && NULL != t->owner)
2130 struct GNUNET_MESH_PeerControl msg;
2132 msg.header.size = htons (sizeof (msg));
2133 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2134 msg.tunnel_id = htonl (t->local_tid);
2135 msg.peer = my_full_id;
2136 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2137 &msg.header, GNUNET_NO);
2143 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2144 * and insert it in the appropiate structures if the peer is not known yet.
2146 * @param peer Full identity of the peer.
2148 * @return Existing or newly created peer info.
2150 static struct MeshPeerInfo *
2151 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2153 struct MeshPeerInfo *peer_info;
2155 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2156 if (NULL == peer_info)
2159 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2160 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2161 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2162 peer_info->id = GNUNET_PEER_intern (peer);
2170 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2171 * and insert it in the appropiate structures if the peer is not known yet.
2173 * @param peer Short identity of the peer.
2175 * @return Existing or newly created peer info.
2177 static struct MeshPeerInfo *
2178 peer_info_get_short (const GNUNET_PEER_Id peer)
2180 struct GNUNET_PeerIdentity id;
2182 GNUNET_PEER_resolve (peer, &id);
2183 return peer_info_get (&id);
2188 * Iterator to remove the tunnel from the list of tunnels a peer participates
2191 * @param cls Closure (tunnel info)
2192 * @param key GNUNET_PeerIdentity of the peer (unused)
2193 * @param value PeerInfo of the peer
2195 * @return always GNUNET_YES, to keep iterating
2198 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2200 struct MeshTunnel *t = cls;
2201 struct MeshPeerInfo *peer = value;
2204 for (i = 0; i < peer->ntunnels; i++)
2207 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2210 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2211 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2220 * Core callback to write a pre-constructed data packet to core buffer
2222 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2223 * @param size Number of bytes available in buf.
2224 * @param buf Where the to write the message.
2226 * @return number of bytes written to buf
2229 send_core_data_raw (void *cls, size_t size, void *buf)
2231 struct MeshTransmissionDescriptor *info = cls;
2232 struct GNUNET_MessageHeader *msg;
2235 GNUNET_assert (NULL != info);
2236 GNUNET_assert (NULL != info->mesh_data);
2237 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2238 total_size = ntohs (msg->size);
2240 if (total_size > size)
2245 memcpy (buf, msg, total_size);
2246 data_descriptor_decrement_rc (info->mesh_data);
2253 * Sends an already built non-multicast message to a peer,
2254 * properly registrating all used resources.
2256 * @param message Message to send. Function makes a copy of it.
2257 * @param peer Short ID of the neighbor whom to send the message.
2258 * @param t Tunnel on which this message is transmitted.
2261 send_message (const struct GNUNET_MessageHeader *message,
2262 const struct GNUNET_PeerIdentity *peer,
2263 struct MeshTunnel *t)
2265 struct MeshTransmissionDescriptor *info;
2266 struct MeshPeerInfo *neighbor;
2267 struct MeshPeerPath *p;
2271 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2273 size = ntohs (message->size);
2274 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2275 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2276 info->mesh_data->data = GNUNET_malloc (size);
2277 memcpy (info->mesh_data->data, message, size);
2278 type = ntohs(message->type);
2279 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2281 struct GNUNET_MESH_Unicast *m;
2283 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2284 m->ttl = htonl (ntohl (m->ttl) - 1);
2286 info->mesh_data->data_len = size;
2287 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2288 *info->mesh_data->reference_counter = 1;
2289 neighbor = peer_info_get (peer);
2290 for (p = neighbor->path_head; NULL != p; p = p->next)
2300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2301 " %s IS NOT DIRECTLY CONNECTED\n",
2303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 for (p = neighbor->path_head; NULL != p; p = p->next)
2308 struct GNUNET_PeerIdentity debug_id;
2311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2312 " path with %u hops through:\n",
2314 for (i = 0; i < p->length; i++)
2316 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 i, GNUNET_i2s(&debug_id));
2323 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2324 GNUNET_free (info->mesh_data->data);
2325 GNUNET_free (info->mesh_data);
2329 info->peer = neighbor;
2330 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2333 * TODO: in this case we only need the service to retransmit
2334 * the message down the path. If we pass the real type to queue_add,
2335 * queue_send will try to build the message from scratch. This can
2336 * probably be done by some other way instead of deleteing the type
2350 * Sends a CREATE PATH message for a path to a peer, properly registrating
2351 * all used resources.
2353 * @param peer PeerInfo of the final peer for whom this path is being created.
2354 * @param p Path itself.
2355 * @param t Tunnel for which the path is created.
2358 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2359 struct MeshTunnel *t)
2361 struct GNUNET_PeerIdentity id;
2362 struct MeshPathInfo *path_info;
2363 struct MeshPeerInfo *neighbor;
2369 p = tree_get_path_to_peer (t->tree, peer->id);
2376 for (i = 0; i < p->length; i++)
2378 if (p->peers[i] == myid)
2381 if (i >= p->length - 1)
2387 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2389 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2390 path_info->path = p;
2392 neighbor = peer_info_get (&id);
2393 path_info->peer = neighbor;
2394 queue_add (path_info,
2395 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2396 sizeof (struct GNUNET_MESH_ManipulatePath) +
2397 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2404 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2406 * @param t Tunnel whose path to destroy.
2407 * @param destination Short ID of the peer to whom the path to destroy.
2410 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2412 struct MeshPeerPath *p;
2415 p = tree_get_path_to_peer (t->tree, destination);
2421 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2422 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2424 struct GNUNET_MESH_ManipulatePath *msg;
2425 struct GNUNET_PeerIdentity *pi;
2429 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2430 msg->header.size = htons (size);
2431 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2432 msg->tid = htonl (t->id.tid);
2433 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2434 for (i = 0; i < p->length; i++)
2436 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2438 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2445 * Try to establish a new connection to this peer.
2446 * Use the best path for the given tunnel.
2447 * If the peer doesn't have any path to it yet, try to get one.
2448 * If the peer already has some path, send a CREATE PATH towards it.
2450 * @param peer PeerInfo of the peer.
2451 * @param t Tunnel for which to create the path, if possible.
2454 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2456 struct MeshPeerPath *p;
2457 struct MeshPathInfo *path_info;
2459 if (NULL != peer->path_head)
2461 p = tree_get_path_to_peer (t->tree, peer->id);
2468 // FIXME always send create path to self
2471 send_create_path (peer, p, t);
2475 struct GNUNET_HashCode hash;
2478 send_client_peer_connected (t, myid);
2479 t->local_tid_dest = next_local_tid++;
2480 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2483 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2484 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2491 else if (NULL == peer->dhtget)
2493 struct GNUNET_PeerIdentity id;
2495 GNUNET_PEER_resolve (peer->id, &id);
2496 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2497 path_info->peer = peer;
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2501 peer->dhtgetcls = path_info;
2502 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2503 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2504 &id.hashPubKey, /* key to search */
2505 dht_replication_level, /* replication level */
2506 GNUNET_DHT_RO_RECORD_ROUTE |
2507 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2508 NULL, /* xquery */ // FIXME BLOOMFILTER
2509 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2510 &dht_get_id_handler, path_info);
2512 /* Otherwise, there is no path but the DHT get is already started. */
2517 * Task to delay the connection of a peer
2519 * @param cls Closure (path info with tunnel and peer to connect).
2520 * Will be free'd on exection.
2521 * @param tc TaskContext
2524 peer_info_connect_task (void *cls,
2525 const struct GNUNET_SCHEDULER_TaskContext *tc)
2527 struct MeshPathInfo *path_info = cls;
2529 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2534 peer_info_connect (path_info->peer, path_info->t);
2540 * Destroy the peer_info and free any allocated resources linked to it
2542 * @param pi The peer_info to destroy.
2544 * @return GNUNET_OK on success
2547 peer_info_destroy (struct MeshPeerInfo *pi)
2549 struct GNUNET_PeerIdentity id;
2550 struct MeshPeerPath *p;
2551 struct MeshPeerPath *nextp;
2553 GNUNET_PEER_resolve (pi->id, &id);
2554 GNUNET_PEER_change_rc (pi->id, -1);
2557 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2560 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2561 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2563 if (NULL != pi->dhtget)
2565 GNUNET_DHT_get_stop (pi->dhtget);
2566 GNUNET_free (pi->dhtgetcls);
2572 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2582 * Remove all paths that rely on a direct connection between p1 and p2
2583 * from the peer itself and notify all tunnels about it.
2585 * @param peer PeerInfo of affected peer.
2586 * @param p1 GNUNET_PEER_Id of one peer.
2587 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2590 * TODO: optimize (see below)
2593 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2596 struct MeshPeerPath *p;
2597 struct MeshPeerPath *aux;
2598 struct MeshPeerInfo *peer_d;
2600 unsigned int destroyed;
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2607 p = peer->path_head;
2611 for (i = 0; i < (p->length - 1); i++)
2613 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2614 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2616 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2627 for (i = 0; i < peer->ntunnels; i++)
2629 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2633 * Problem: one or more peers have been deleted from the tunnel tree.
2634 * We don't know who they are to try to add them again.
2635 * We need to try to find a new path for each of the disconnected peers.
2636 * Some of them might already have a path to reach them that does not
2637 * involve p1 and p2. Adding all anew might render in a better tree than
2638 * the trivial immediate fix.
2640 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2641 * its children will be reachable trough him.
2643 peer_d = peer_info_get_short (d);
2646 for (p = peer_d->path_head; NULL != p; p = p->next)
2648 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2656 /* No callback, as peer will be already disconnected and a connection
2657 * scheduled by tunnel_notify_connection_broken.
2659 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2663 peer_info_connect (peer_d, peer->tunnels[i]);
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2671 * Add the path to the peer and update the path used to reach it in case this
2674 * @param peer_info Destination peer to add the path to.
2675 * @param path New path to add. Last peer must be the peer in arg 1.
2676 * Path will be either used of freed if already known.
2677 * @param trusted Do we trust that this path is real?
2680 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2683 struct MeshPeerPath *aux;
2687 if ((NULL == peer_info) || (NULL == path))
2690 path_destroy (path);
2693 if (path->peers[path->length - 1] != peer_info->id)
2696 path_destroy (path);
2699 if (path->length <= 2 && GNUNET_NO == trusted)
2701 /* Only allow CORE to tell us about direct paths */
2702 path_destroy (path);
2705 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2706 for (l = 1; l < path->length; l++)
2708 if (path->peers[l] == myid)
2710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2711 for (l2 = 0; l2 < path->length - l; l2++)
2713 path->peers[l2] = path->peers[l + l2];
2718 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2723 struct GNUNET_PeerIdentity id;
2725 GNUNET_PEER_resolve (peer_info->id, &id);
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2727 path->length, GNUNET_i2s (&id));
2730 l = path_get_length (path);
2737 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2738 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2740 l2 = path_get_length (aux);
2743 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2744 peer_info->path_tail, aux, path);
2749 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2751 path_destroy (path);
2756 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2763 * Add the path to the origin peer and update the path used to reach it in case
2764 * this is the shortest.
2765 * The path is given in peer_info -> destination, therefore we turn the path
2766 * upside down first.
2768 * @param peer_info Peer to add the path to, being the origin of the path.
2769 * @param path New path to add after being inversed.
2770 * @param trusted Do we trust that this path is real?
2773 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2774 struct MeshPeerPath *path, int trusted)
2777 peer_info_add_path (peer_info, path, trusted);
2782 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2783 * to obtain a local peer -> destination path and interning the peer ids.
2785 * @return Newly allocated and created path
2787 static struct MeshPeerPath *
2788 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2789 unsigned int get_path_length,
2790 const struct GNUNET_PeerIdentity *put_path,
2791 unsigned int put_path_length)
2793 struct MeshPeerPath *p;
2799 GNUNET_PEER_change_rc (myid, 1);
2800 i = get_path_length;
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2802 for (i--; i >= 0; i--)
2804 id = GNUNET_PEER_intern (&get_path[i]);
2805 if (p->length > 0 && id == p->peers[p->length - 1])
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2808 GNUNET_PEER_change_rc (id, -1);
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2813 GNUNET_i2s (&get_path[i]));
2815 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2816 p->peers[p->length - 1] = id;
2819 i = put_path_length;
2820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2821 for (i--; i >= 0; i--)
2823 id = GNUNET_PEER_intern (&put_path[i]);
2826 /* PUT path went through us, so discard the path up until now and start
2827 * from here to get a much shorter (and loop-free) path.
2832 if (p->length > 0 && id == p->peers[p->length - 1])
2834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2835 GNUNET_PEER_change_rc (id, -1);
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2840 GNUNET_i2s (&put_path[i]));
2842 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2843 p->peers[p->length - 1] = id;
2847 if (get_path_length > 0)
2848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2849 GNUNET_i2s (&get_path[0]));
2850 if (put_path_length > 0)
2851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2852 GNUNET_i2s (&put_path[0]));
2853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2855 for (i = 0; i < p->length; i++)
2857 struct GNUNET_PeerIdentity peer_id;
2859 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2861 GNUNET_i2s (&peer_id));
2869 * Adds a path to the peer_infos of all the peers in the path
2871 * @param p Path to process.
2872 * @param confirmed Whether we know if the path works or not.
2875 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2879 /* TODO: invert and add */
2880 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2881 for (i++; i < p->length; i++)
2883 struct MeshPeerInfo *aux;
2884 struct MeshPeerPath *copy;
2886 aux = peer_info_get_short (p->peers[i]);
2887 copy = path_duplicate (p);
2888 copy->length = i + 1;
2889 peer_info_add_path (aux, copy, GNUNET_NO);
2895 * Send keepalive packets for a peer
2897 * @param cls Closure (tunnel for which to send the keepalive).
2898 * @param tc Notification context.
2900 * TODO: implement explicit multicast keepalive?
2903 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2907 * Search for a tunnel among the incoming tunnels
2909 * @param tid the local id of the tunnel
2911 * @return tunnel handler, NULL if doesn't exist
2913 static struct MeshTunnel *
2914 tunnel_get_incoming (MESH_TunnelNumber tid)
2916 struct GNUNET_HashCode hash;
2918 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2919 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2920 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2925 * Search for a tunnel among the tunnels for a client
2927 * @param c the client whose tunnels to search in
2928 * @param tid the local id of the tunnel
2930 * @return tunnel handler, NULL if doesn't exist
2932 static struct MeshTunnel *
2933 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2935 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2937 return tunnel_get_incoming (tid);
2941 struct GNUNET_HashCode hash;
2943 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2944 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2950 * Search for a tunnel by global ID using PEER_ID
2952 * @param pi owner of the tunnel
2953 * @param tid global tunnel number
2955 * @return tunnel handler, NULL if doesn't exist
2957 static struct MeshTunnel *
2958 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2960 struct MESH_TunnelID id;
2961 struct GNUNET_HashCode hash;
2966 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2967 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2972 * Search for a tunnel by global ID using full PeerIdentities
2974 * @param oid owner of the tunnel
2975 * @param tid global tunnel number
2977 * @return tunnel handler, NULL if doesn't exist
2979 static struct MeshTunnel *
2980 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2982 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2987 * Delete an active client from the tunnel.
2993 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2997 for (i = 0; i < t->nclients; i++)
2999 if (t->clients[i] == c)
3001 t->clients[i] = t->clients[t->nclients - 1];
3002 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3003 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3005 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3013 * Delete an ignored client from the tunnel.
3019 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3023 for (i = 0; i < t->nignore; i++)
3025 if (t->ignore[i] == c)
3027 t->ignore[i] = t->ignore[t->nignore - 1];
3028 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3036 * Delete a client from the tunnel. It should be only done on
3037 * client disconnection, otherwise use client_ignore_tunnel.
3043 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3045 tunnel_delete_ignored_client (t, c);
3046 tunnel_delete_active_client (t, c);
3051 * Iterator to free MeshTunnelChildInfo of tunnel children.
3053 * @param cls Closure (tunnel info).
3054 * @param key Hash of GNUNET_PEER_Id (unused).
3055 * @param value MeshTunnelChildInfo of the child.
3057 * @return always GNUNET_YES, to keep iterating
3060 tunnel_destroy_child (void *cls,
3061 const struct GNUNET_HashCode * key,
3064 GNUNET_free (value);
3070 * Callback used to notify a client owner of a tunnel that a peer has
3071 * disconnected, most likely because of a path change.
3073 * @param cls Closure (tunnel this notification is about).
3074 * @param peer_id Short ID of disconnected peer.
3077 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3079 struct MeshTunnel *t = cls;
3080 struct MeshPeerInfo *peer;
3081 struct MeshPathInfo *path_info;
3083 if (NULL != t->owner && NULL != nc)
3085 struct GNUNET_MESH_PeerControl msg;
3087 msg.header.size = htons (sizeof (msg));
3088 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3089 msg.tunnel_id = htonl (t->local_tid);
3090 GNUNET_PEER_resolve (peer_id, &msg.peer);
3091 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3092 &msg.header, GNUNET_NO);
3094 peer = peer_info_get_short (peer_id);
3095 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3096 path_info->peer = peer;
3098 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3103 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3104 * needed rescources.
3105 * If peer already exists, reevaluate shortest path and change if different.
3107 * @param t Tunnel we want to add a new peer to
3108 * @param peer PeerInfo of the peer being added
3112 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3114 struct GNUNET_PeerIdentity id;
3115 struct MeshPeerPath *best_p;
3116 struct MeshPeerPath *p;
3117 unsigned int best_cost;
3120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3121 GNUNET_PEER_resolve (peer->id, &id);
3123 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3126 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3127 GNUNET_assert (GNUNET_OK ==
3128 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3130 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3133 if (NULL != (p = peer->path_head))
3136 best_cost = tree_get_path_cost (t->tree, p);
3139 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3146 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3147 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3148 t->path_refresh_task =
3149 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3153 /* Start a DHT get */
3154 peer_info_connect (peer, t);
3156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3160 * Add a path to a tunnel which we don't own, just to remember the next hop.
3161 * If destination node was already in the tunnel, the first hop information
3162 * will be replaced with the new path.
3164 * @param t Tunnel we want to add a new peer to
3165 * @param p Path to add
3166 * @param own_pos Position of local node in path.
3170 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3171 unsigned int own_pos)
3173 struct GNUNET_PeerIdentity id;
3175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3176 GNUNET_assert (0 != own_pos);
3177 tree_add_path (t->tree, p, NULL, NULL);
3178 if (own_pos < p->length - 1)
3180 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3181 tree_update_first_hops (t->tree, myid, &id);
3183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3187 * Add a client to a tunnel, initializing all needed data structures.
3189 * @param t Tunnel to which add the client.
3190 * @param c Client which to add to the tunnel.
3193 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3195 struct MeshTunnelClientInfo clinfo;
3197 GNUNET_array_append (t->clients, t->nclients, c);
3198 clinfo.fwd_ack = t->fwd_pid + 1;
3199 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3200 clinfo.fwd_pid = t->fwd_pid;
3201 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3203 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3208 * Notifies a tunnel that a connection has broken that affects at least
3209 * some of its peers. Sends a notification towards the root of the tree.
3210 * In case the peer is the owner of the tree, notifies the client that owns
3211 * the tunnel and tries to reconnect.
3213 * @param t Tunnel affected.
3214 * @param p1 Peer that got disconnected from p2.
3215 * @param p2 Peer that got disconnected from p1.
3217 * @return Short ID of the peer disconnected (either p1 or p2).
3218 * 0 if the tunnel remained unaffected.
3220 static GNUNET_PEER_Id
3221 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3227 tree_notify_connection_broken (t->tree, p1, p2,
3228 &tunnel_notify_client_peer_disconnected,
3230 if (myid != p1 && myid != p2)
3236 if (tree_get_predecessor (t->tree) != 0)
3238 /* We are the peer still connected, notify owner of the disconnection. */
3239 struct GNUNET_MESH_PathBroken msg;
3240 struct GNUNET_PeerIdentity neighbor;
3242 msg.header.size = htons (sizeof (msg));
3243 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3244 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3245 msg.tid = htonl (t->id.tid);
3246 msg.peer1 = my_full_id;
3247 GNUNET_PEER_resolve (pid, &msg.peer2);
3248 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3249 send_message (&msg.header, &neighbor, t);
3257 * Send a multicast packet to a neighbor.
3259 * @param cls Closure (Info about the multicast packet)
3260 * @param neighbor_id Short ID of the neighbor to send the packet to.
3263 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3265 struct MeshData *mdata = cls;
3266 struct MeshTransmissionDescriptor *info;
3267 struct GNUNET_PeerIdentity neighbor;
3269 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3271 info->mesh_data = mdata;
3272 (*(mdata->reference_counter)) ++;
3273 info->destination = neighbor_id;
3274 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3276 GNUNET_i2s (&neighbor));
3277 info->peer = peer_info_get (&neighbor);
3278 GNUNET_assert (NULL != info->peer);
3280 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3281 info->mesh_data->data_len,
3288 * Queue a message in a tunnel in multicast, sending a copy to each child node
3289 * down the local one in the tunnel tree.
3291 * @param t Tunnel in which to send the data.
3292 * @param msg Message to be sent.
3293 * @param internal Has the service generated this message?
3296 tunnel_send_multicast (struct MeshTunnel *t,
3297 const struct GNUNET_MessageHeader *msg,
3300 struct MeshData *mdata;
3302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3303 " sending a multicast packet...\n");
3305 mdata = GNUNET_malloc (sizeof (struct MeshData));
3306 mdata->data_len = ntohs (msg->size);
3307 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3309 mdata->data = GNUNET_malloc (mdata->data_len);
3310 memcpy (mdata->data, msg, mdata->data_len);
3311 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3313 struct GNUNET_MESH_Multicast *mcast;
3315 if (t->fwd_queue_n >= t->fwd_queue_max)
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue full!\n");
3319 GNUNET_free (mdata->data);
3320 GNUNET_free (mdata->reference_counter);
3321 GNUNET_free (mdata);
3325 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3326 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3328 ntohl (mcast->ttl));
3332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3334 if (NULL != t->owner &&
3335 GNUNET_YES != t->owner->shutting_down &&
3336 GNUNET_NO == internal)
3338 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3340 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3346 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3347 if (*(mdata->reference_counter) == 0)
3349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3350 " no one to send data to\n");
3351 GNUNET_free (mdata->data);
3352 GNUNET_free (mdata->reference_counter);
3353 if (NULL != mdata->task)
3355 GNUNET_SCHEDULER_cancel(*(mdata->task));
3356 GNUNET_free (mdata->task);
3357 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3359 GNUNET_free (mdata);
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3363 " sending a multicast packet done\n");
3369 * Increase the SKIP value of all peers that
3370 * have not received a unicast message.
3372 * @param cls Closure (ID of the peer that HAS received the message).
3373 * @param key ID of the neighbor.
3374 * @param value Information about the neighbor.
3376 * @return GNUNET_YES to keep iterating.
3379 tunnel_add_skip (void *cls,
3380 const struct GNUNET_HashCode * key,
3383 struct GNUNET_PeerIdentity *neighbor = cls;
3384 struct MeshTunnelChildInfo *cinfo = value;
3386 /* TODO compare only pointers? key == neighbor? */
3387 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3397 * @brief Get neighbor's Flow Control information.
3399 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3400 * descendant of the local node in a certain tunnel.
3401 * If the info is not yet there (recently created path), creates the data struct
3402 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3405 * @param t Tunnel related.
3406 * @param peer Neighbor whose Flow Control info is needed.
3408 * @return Neighbor's Flow Control info.
3410 static struct MeshTunnelChildInfo *
3411 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3412 const struct GNUNET_PeerIdentity *peer)
3414 struct MeshTunnelChildInfo *cinfo;
3415 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3421 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3422 cinfo->id = GNUNET_PEER_intern (peer);
3423 cinfo->skip = t->fwd_pid;
3425 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3426 cinfo->fwd_ack = t->fwd_pid + delta;
3427 cinfo->bck_ack = delta;
3429 GNUNET_assert (GNUNET_OK ==
3430 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3433 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3440 * Get the Flow Control info of a client.
3442 * @param t Tunnel on which to look.
3443 * @param c Client whose ACK to get.
3445 * @return ACK value.
3447 static struct MeshTunnelClientInfo *
3448 tunnel_get_client_fc (struct MeshTunnel *t,
3449 struct MeshClient *c)
3453 for (i = 0; i < t->nclients; i++)
3455 if (t->clients[i] != c)
3457 return &t->clients_fc[i];
3460 return NULL; // avoid compiler / coverity complaints
3465 * Iterator to get the appropiate ACK value from all children nodes.
3467 * @param cls Closue (tunnel).
3468 * @param id Id of the child node.
3471 tunnel_get_child_fwd_ack (void *cls,
3474 struct GNUNET_PeerIdentity peer_id;
3475 struct MeshTunnelChildInfo *cinfo;
3476 struct MeshTunnelChildIteratorContext *ctx = cls;
3477 struct MeshTunnel *t = ctx->t;
3480 GNUNET_PEER_resolve (id, &peer_id);
3481 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3482 ack = cinfo->fwd_ack;
3485 if (GNUNET_NO == ctx->init)
3487 ctx->max_child_ack = ack;
3488 ctx->init = GNUNET_YES;
3491 if (GNUNET_YES == t->speed_min)
3493 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3497 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3504 * Get the maximum PID allowed to transmit to any
3505 * tunnel child of the local peer, depending on the tunnel
3506 * buffering/speed settings.
3510 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3513 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3515 struct MeshTunnelChildIteratorContext ctx;
3517 ctx.max_child_ack = 0;
3519 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3521 if (0 == ctx.nchildren)
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3524 " tunnel has no children, no FWD ACK\n");
3528 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3529 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3531 return (int64_t) ctx.max_child_ack;
3536 * Set the FWD ACK value of a client in a particular tunnel.
3538 * @param t Tunnel affected.
3539 * @param c Client whose ACK to set.
3540 * @param ack ACK value.
3543 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3544 struct MeshClient *c,
3549 for (i = 0; i < t->nclients; i++)
3551 if (t->clients[i] != c)
3553 t->clients_fc[i].fwd_ack = ack;
3561 * Get the highest ACK value of all clients in a particular tunnel,
3562 * according to the buffering/speed settings.
3564 * @param t Tunnel on which to look.
3566 * @return Corresponding ACK value (max uint32_t).
3567 * If no clients are suscribed, -1.
3570 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3575 if (0 == t->nclients)
3577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3578 " tunnel has no clients, no FWD ACK\n");
3582 for (ack = -1, i = 0; i < t->nclients; i++)
3585 (GNUNET_YES == t->speed_min &&
3586 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3587 (GNUNET_NO == t->speed_min &&
3588 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3590 ack = t->clients_fc[i].fwd_ack;
3594 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3595 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3597 return (uint32_t) ack;
3602 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3603 * mode and the status of all children nodes.
3607 * @return Maximum PID allowed.
3610 tunnel_get_fwd_ack (struct MeshTunnel *t)
3613 uint32_t buffer_free;
3618 count = t->fwd_pid - t->skip;
3619 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3620 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3621 child_ack = tunnel_get_children_fwd_ack (t);
3622 client_ack = tunnel_get_clients_fwd_ack (t);
3623 if (-1 == child_ack)
3625 // Node has no children, child_ack AND core buffer are irrelevant.
3626 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
3627 return (uint32_t) client_ack;
3630 if (GNUNET_YES == t->speed_min)
3632 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3633 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3637 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3638 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3640 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3641 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3643 count, buffer_free, child_ack, client_ack, ack);
3649 * Build a local ACK message and send it to a local client.
3651 * @param t Tunnel on which to send the ACK.
3652 * @param c Client to whom send the ACK.
3653 * @param ack Value of the ACK.
3656 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3658 struct GNUNET_MESH_LocalAck msg;
3660 msg.header.size = htons (sizeof (msg));
3661 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3662 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3663 msg.max_pid = htonl (ack);
3664 GNUNET_SERVER_notification_context_unicast(nc,
3671 * Build an ACK message and queue it to send to the given peer.
3673 * @param t Tunnel on which to send the ACK.
3674 * @param peer Peer to whom send the ACK.
3675 * @param ack Value of the ACK.
3678 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3680 struct GNUNET_MESH_ACK msg;
3682 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3683 msg.header.size = htons (sizeof (msg));
3684 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3685 msg.pid = htonl (ack);
3686 msg.tid = htonl (t->id.tid);
3688 send_message (&msg.header, peer, t);
3693 * Notify a the owner of a tunnel about how many more
3694 * payload packages will we accept on a given tunnel.
3696 * @param t Tunnel on which to send the ACK.
3699 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3704 "Sending client FWD ACK on tunnel %X\n",
3707 ack = tunnel_get_fwd_ack (t);
3709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3710 if (t->last_fwd_ack == ack)
3713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3714 t->last_fwd_ack = ack;
3715 send_local_ack (t, t->owner, ack);
3720 * Send an ACK informing the predecessor about the available buffer space.
3721 * In case there is no predecessor, inform the owning client.
3722 * If buffering is off, send only on behalf of children or self if endpoint.
3723 * If buffering is on, send when sent to children and buffer space is free.
3724 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3725 * the ACK itself goes "back" (towards root).
3727 * @param t Tunnel on which to send the ACK.
3728 * @param type Type of message that triggered the ACK transmission.
3731 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3733 struct GNUNET_PeerIdentity id;
3736 if (NULL != t->owner)
3738 tunnel_send_client_fwd_ack (t);
3741 /* Is it after unicast / multicast retransmission? */
3744 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3745 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3747 "ACK due to FWD DATA retransmission\n");
3748 if (GNUNET_YES == t->nobuffer)
3750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3753 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3754 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3758 " t->qmax: %u, t->qn: %u\n",
3759 t->fwd_queue_max, t->fwd_queue_n);
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3761 " t->pid: %u, t->ack: %u\n",
3762 t->fwd_pid, t->last_fwd_ack);
3766 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3767 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3773 /* Ok, ACK might be necessary, what PID to ACK? */
3774 ack = tunnel_get_fwd_ack (t);
3776 /* If speed_min and not all children have ack'd, dont send yet */
3777 if (ack == t->last_fwd_ack)
3779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3783 t->last_fwd_ack = ack;
3784 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3785 send_ack (t, &id, ack);
3790 * Iterator to send a child node a BCK ACK to allow him to send more
3793 * @param cls Closure (tunnel).
3794 * @param id Id of the child node.
3797 tunnel_send_child_bck_ack (void *cls,
3800 struct MeshTunnel *t = cls;
3801 struct MeshTunnelChildInfo *cinfo;
3802 struct GNUNET_PeerIdentity peer;
3804 GNUNET_PEER_resolve (id, &peer);
3805 cinfo = tunnel_get_neighbor_fc (t, &peer);
3807 if (cinfo->bck_ack != cinfo->pid &&
3808 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3812 send_ack (t, &peer, cinfo->bck_ack);
3817 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3819 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3821 * @param t Tunnel on which to send the BCK ACKs.
3824 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3827 unsigned int tunnel_delta;
3829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3831 tunnel_delta = t->bck_ack - t->bck_pid;
3832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3834 /* Find client whom to allow to send to origin (with lowest buffer space) */
3835 for (i = 0; i < t->nclients; i++)
3837 struct MeshTunnelClientInfo *clinfo;
3840 clinfo = &t->clients_fc[i];
3841 delta = clinfo->bck_ack - clinfo->bck_pid;
3842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3843 t->clients[i]->id, delta);
3845 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3846 (GNUNET_YES == t->nobuffer && 0 == delta))
3850 ack = clinfo->bck_pid;
3851 ack += t->nobuffer ? 1 : tunnel_delta;
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3853 " sending ack to client %u: %u\n",
3854 t->clients[i]->id, ack);
3855 send_local_ack (t, t->clients[i], ack);
3856 clinfo->bck_ack = ack;
3863 * Send an ACK informing the children nodes and destination clients about
3864 * the available buffer space.
3865 * If buffering is off, send only on behalf of root (can be self).
3866 * If buffering is on, send when sent to predecessor and buffer space is free.
3867 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3868 * the ACK itself goes "forward" (towards children/clients).
3870 * @param t Tunnel on which to send the ACK.
3871 * @param type Type of message that triggered the ACK transmission.
3874 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3877 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3878 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3879 /* Is it after data to_origin retransmission? */
3882 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3883 if (GNUNET_YES == t->nobuffer)
3885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3886 " Not sending ACK, nobuffer\n");
3890 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3891 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3897 tunnel_send_clients_bck_ack (t);
3898 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3903 * @brief Re-initiate traffic to this peer if necessary.
3905 * Check if there is traffic queued towards this peer
3906 * and the core transmit handle is NULL (traffic was stalled).
3907 * If so, call core tmt rdy.
3909 * @param cls Closure (unused)
3910 * @param peer_id Short ID of peer to which initiate traffic.
3913 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3915 struct MeshPeerInfo *peer;
3916 struct GNUNET_PeerIdentity id;
3917 struct MeshPeerQueue *q;
3920 peer = peer_info_get_short(peer_id);
3921 if (NULL != peer->core_transmit)
3924 q = queue_get_next(peer);
3927 /* Might br multicast traffic already sent to this particular peer but
3928 * not to other children in this tunnel.
3929 * This way t->queue_n would be > 0 but the queue of this particular peer
3935 GNUNET_PEER_resolve (peer->id, &id);
3936 peer->core_transmit =
3937 GNUNET_CORE_notify_transmit_ready(core_handle,
3940 GNUNET_TIME_UNIT_FOREVER_REL,
3950 * @brief Allow transmission of FWD traffic on this tunnel
3952 * Check if there is traffic queued towards any children
3953 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3955 * @param t Tunnel on which to unlock FWD traffic.
3958 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3960 if (0 == t->fwd_queue_n)
3963 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3968 * @brief Allow transmission of BCK traffic on this tunnel
3970 * Check if there is traffic queued towards the root of the tree
3971 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3973 * @param t Tunnel on which to unlock BCK traffic.
3976 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3978 if (0 == t->bck_queue_n)
3981 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3986 * Send a message to all peers in this tunnel that the tunnel is no longer
3989 * @param t The tunnel whose peers to notify.
3992 tunnel_send_destroy (struct MeshTunnel *t)
3994 struct GNUNET_MESH_TunnelDestroy msg;
3996 msg.header.size = htons (sizeof (msg));
3997 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3998 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3999 msg.tid = htonl (t->id.tid);
4000 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4005 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4007 * @param cls Closure (Tunnel which to cancel).
4008 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4011 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4013 struct MeshTunnel *t = cls;
4014 struct MeshPeerInfo *peer_info;
4015 struct MeshPeerQueue *pq;
4016 struct MeshPeerQueue *next;
4018 peer_info = peer_info_get_short (neighbor_id);
4019 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4022 if (pq->tunnel == t)
4024 queue_destroy (pq, GNUNET_YES);
4027 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4029 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4030 peer_info->core_transmit = NULL;
4035 * Destroy the tunnel and free any allocated resources linked to it.
4037 * @param t the tunnel to destroy
4039 * @return GNUNET_OK on success
4042 tunnel_destroy (struct MeshTunnel *t)
4044 struct MeshClient *c;
4045 struct GNUNET_HashCode hash;
4052 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4058 struct GNUNET_PeerIdentity id;
4060 GNUNET_PEER_resolve (t->id.oid, &id);
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4062 GNUNET_i2s (&id), t->id.tid);
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4068 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4069 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4074 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4077 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4081 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4082 for (i = 0; i < t->nclients; i++)
4086 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4091 for (i = 0; i < t->nignore; i++)
4095 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4100 if (t->nclients > 0)
4103 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4107 GNUNET_free (t->clients);
4109 if (NULL != t->peers)
4111 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4113 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4116 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4117 &tunnel_destroy_child,
4119 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4121 tree_destroy (t->tree);
4123 if (NULL != t->regex_ctx)
4124 regex_cancel_search (t->regex_ctx);
4125 if (NULL != t->dht_get_type)
4126 GNUNET_DHT_get_stop (t->dht_get_type);
4127 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4128 GNUNET_SCHEDULER_cancel (t->timeout_task);
4129 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4130 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4133 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4134 GNUNET_assert (0 <= n_tunnels);
4141 * Create a new tunnel
4143 * @param owner Who is the owner of the tunnel (short ID).
4144 * @param tid Tunnel Number of the tunnel.
4145 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4146 * @param local Tunnel Number for the tunnel, for the client point of view.
4148 * @return A new initialized tunnel. NULL on error.
4150 static struct MeshTunnel *
4151 tunnel_new (GNUNET_PEER_Id owner,
4152 MESH_TunnelNumber tid,
4153 struct MeshClient *client,
4154 MESH_TunnelNumber local)
4156 struct MeshTunnel *t;
4157 struct GNUNET_HashCode hash;
4159 if (n_tunnels >= max_tunnels && NULL == client)
4162 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4165 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4166 t->bck_queue_max = t->fwd_queue_max;
4167 t->tree = tree_new (owner);
4169 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4170 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4171 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4172 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4173 t->local_tid = local;
4174 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4176 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4178 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4180 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4188 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4195 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4197 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4198 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4202 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4212 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4213 * that are no longer needed, as well as nodes of no longer reachable peers.
4214 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4216 * @param t Tunnel from which to remove the path.
4217 * @param peer Short id of the peer which should be removed.
4220 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4222 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4228 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4229 * client when the client disconnects. If the client is not the owner, the
4230 * owner will get notified if no more clients are in the tunnel and the client
4231 * get removed from the tunnel's list.
4233 * @param cls closure (client that is disconnecting)
4234 * @param key the hash of the local tunnel id (used to access the hashmap)
4235 * @param value the value stored at the key (tunnel to destroy)
4237 * @return GNUNET_OK on success
4240 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4242 struct MeshTunnel *t = value;
4243 struct MeshClient *c = cls;
4246 send_client_tunnel_disconnect(t, c);
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4250 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4251 tunnel_delete_client(t, c);
4252 client_delete_tunnel(c, t);
4255 tunnel_send_destroy(t);
4256 r = tunnel_destroy (t);
4262 * Timeout function, destroys tunnel if called
4264 * @param cls Closure (tunnel to destroy).
4265 * @param tc TaskContext
4268 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4270 struct MeshTunnel *t = cls;
4272 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4274 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4279 * Resets the tunnel timeout. Starts it if no timeout was running.
4281 * @param t Tunnel whose timeout to reset.
4283 * TODO use heap to improve efficiency of scheduler.
4286 tunnel_reset_timeout (struct MeshTunnel *t)
4288 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4289 GNUNET_SCHEDULER_cancel (t->timeout_task);
4291 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4292 (refresh_path_time, 4), &tunnel_timeout, t);
4296 /******************************************************************************/
4297 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4298 /******************************************************************************/
4301 * Function to send a create path packet to a peer.
4303 * @param cls closure
4304 * @param size number of bytes available in buf
4305 * @param buf where the callee should write the message
4306 * @return number of bytes written to buf
4309 send_core_path_create (void *cls, size_t size, void *buf)
4311 struct MeshPathInfo *info = cls;
4312 struct GNUNET_MESH_ManipulatePath *msg;
4313 struct GNUNET_PeerIdentity *peer_ptr;
4314 struct MeshTunnel *t = info->t;
4315 struct MeshPeerPath *p = info->path;
4320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4322 sizeof (struct GNUNET_MESH_ManipulatePath) +
4323 p->length * sizeof (struct GNUNET_PeerIdentity);
4325 if (size < size_needed || NULL == buf)
4330 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4331 msg->header.size = htons (size_needed);
4332 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4333 msg->tid = ntohl (t->id.tid);
4335 if (GNUNET_YES == t->speed_min)
4336 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4337 if (GNUNET_YES == t->nobuffer)
4338 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4339 msg->opt = htonl(opt);
4341 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4342 for (i = 0; i < p->length; i++)
4344 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4351 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4357 * Fill the core buffer
4359 * @param cls closure (data itself)
4360 * @param size number of bytes available in buf
4361 * @param buf where the callee should write the message
4363 * @return number of bytes written to buf
4366 send_core_data_multicast (void *cls, size_t size, void *buf)
4368 struct MeshTransmissionDescriptor *info = cls;
4371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4372 GNUNET_assert (NULL != info);
4373 GNUNET_assert (NULL != info->peer);
4374 total_size = info->mesh_data->data_len;
4375 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4377 if (total_size > size)
4382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4383 memcpy (buf, info->mesh_data->data, total_size);
4386 struct GNUNET_MESH_Multicast *mc;
4387 struct GNUNET_MessageHeader *mh;
4390 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4392 mc = (struct GNUNET_MESH_Multicast *) mh;
4393 mh = (struct GNUNET_MessageHeader *) &mc[1];
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4395 " multicast, payload type %s\n",
4396 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4398 " multicast, payload size %u\n", ntohs (mh->size));
4402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4403 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4407 data_descriptor_decrement_rc (info->mesh_data);
4408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4416 * Creates a path ack message in buf and frees all unused resources.
4418 * @param cls closure (MeshTransmissionDescriptor)
4419 * @param size number of bytes available in buf
4420 * @param buf where the callee should write the message
4421 * @return number of bytes written to buf
4424 send_core_path_ack (void *cls, size_t size, void *buf)
4426 struct MeshTransmissionDescriptor *info = cls;
4427 struct GNUNET_MESH_PathACK *msg = buf;
4429 GNUNET_assert (NULL != info);
4430 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4435 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4436 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4437 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4438 msg->tid = htonl (info->origin->tid);
4439 msg->peer_id = my_full_id;
4442 /* TODO add signature */
4444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4445 return sizeof (struct GNUNET_MESH_PathACK);
4450 * Free a transmission that was already queued with all resources
4451 * associated to the request.
4453 * @param queue Queue handler to cancel.
4454 * @param clear_cls Is it necessary to free associated cls?
4457 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4459 struct MeshTransmissionDescriptor *dd;
4460 struct MeshPathInfo *path_info;
4462 if (GNUNET_YES == clear_cls)
4464 switch (queue->type)
4466 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4467 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4468 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4471 data_descriptor_decrement_rc (dd->mesh_data);
4473 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4475 path_info = queue->cls;
4476 path_destroy (path_info->path);
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4482 GNUNET_free_non_null (queue->cls);
4484 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4485 queue->peer->queue_tail,
4487 GNUNET_free (queue);
4492 * @brief Get the next transmittable message from the queue.
4494 * This will be the head, except in the case of being a data packet
4495 * not allowed by the destination peer.
4497 * @param peer Destination peer.
4499 * @return The next viable MeshPeerQueue element to send to that peer.
4500 * NULL when there are no transmittable messages.
4502 struct MeshPeerQueue *
4503 queue_get_next (const struct MeshPeerInfo *peer)
4505 struct MeshPeerQueue *q;
4506 struct MeshTunnel *t;
4507 struct MeshTransmissionDescriptor *info;
4508 struct MeshTunnelChildInfo *cinfo;
4509 struct GNUNET_MESH_Unicast *ucast;
4510 struct GNUNET_MESH_ToOrigin *to_orig;
4511 struct GNUNET_MESH_Multicast *mcast;
4512 struct GNUNET_PeerIdentity id;
4516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4517 for (q = peer->queue_head; NULL != q; q = q->next)
4521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4523 GNUNET_MESH_DEBUG_M2S(q->type));
4526 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4527 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4528 pid = ntohl (ucast->pid);
4529 GNUNET_PEER_resolve (info->peer->id, &id);
4530 cinfo = tunnel_get_neighbor_fc(t, &id);
4531 ack = cinfo->fwd_ack;
4533 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4534 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4535 pid = ntohl (to_orig->pid);
4538 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4539 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4540 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4542 // Not a multicast payload: multicast control traffic (destroy, etc)
4545 pid = ntohl (mcast->pid);
4546 GNUNET_PEER_resolve (info->peer->id, &id);
4547 cinfo = tunnel_get_neighbor_fc(t, &id);
4548 ack = cinfo->fwd_ack;
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4556 "********* ACK: %u, PID: %u\n",
4558 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4567 "********* NEXT!\n");
4570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4571 "********* nothing found\n");
4577 * Core callback to write a queued packet to core buffer
4579 * @param cls Closure (peer info).
4580 * @param size Number of bytes available in buf.
4581 * @param buf Where the to write the message.
4583 * @return number of bytes written to buf
4586 queue_send (void *cls, size_t size, void *buf)
4588 struct MeshPeerInfo *peer = cls;
4589 struct GNUNET_MessageHeader *msg;
4590 struct MeshPeerQueue *queue;
4591 struct MeshTunnel *t;
4594 peer->core_transmit = NULL;
4596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4597 queue = queue_get_next(peer);
4599 /* Queue has no internal mesh traffic not sendable payload */
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4603 if (NULL == peer->queue_head)
4604 GNUNET_break(0); // Should've been canceled
4607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4609 /* Check if buffer size is enough for the message */
4610 if (queue->size > size)
4612 struct GNUNET_PeerIdentity id;
4614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4615 "********* not enough room, reissue\n");
4616 GNUNET_PEER_resolve (peer->id, &id);
4617 peer->core_transmit =
4618 GNUNET_CORE_notify_transmit_ready(core_handle,
4621 GNUNET_TIME_UNIT_FOREVER_REL,
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4631 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4635 "********* unicast: t->q (%u/%u)\n",
4636 t->fwd_queue_n, t->fwd_queue_max);
4638 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4645 switch (queue->type)
4648 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4649 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4650 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4652 "********* raw: %s\n",
4653 GNUNET_MESH_DEBUG_M2S (queue->type));
4655 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4656 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4657 data_size = send_core_data_raw (queue->cls, size, buf);
4658 msg = (struct GNUNET_MessageHeader *) buf;
4659 switch (ntohs (msg->type)) // Type of preconstructed message
4661 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4662 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4664 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4665 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4671 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4673 data_size = send_core_data_multicast(queue->cls, size, buf);
4674 // FIXME fc substract when? depending on the tunnel conf.
4675 // t->fwd_queue_n--;
4676 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4678 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4680 data_size = send_core_path_create(queue->cls, size, buf);
4682 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4684 data_size = send_core_path_ack(queue->cls, size, buf);
4688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4689 "********* type unknown: %u\n",
4694 /* Free queue, but cls was freed by send_core_* */
4695 queue_destroy (queue, GNUNET_NO);
4697 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4703 /* If more data in queue, send next */
4704 if (NULL != peer->queue_head)
4706 struct GNUNET_PeerIdentity id;
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4709 GNUNET_PEER_resolve (peer->id, &id);
4710 peer->core_transmit =
4711 GNUNET_CORE_notify_transmit_ready(core_handle,
4714 GNUNET_TIME_UNIT_FOREVER_REL,
4716 peer->queue_head->size,
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4726 * Queue and pass message to core when possible.
4728 * @param cls Closure (type dependant).
4729 * @param type Type of the message, 0 for a raw message.
4730 * @param size Size of the message.
4731 * @param dst Neighbor to send message to.
4732 * @param t Tunnel this message belongs to.
4735 queue_add (void *cls, uint16_t type, size_t size,
4736 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4738 struct MeshPeerQueue *queue;
4743 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4744 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4746 n = &t->fwd_queue_n;
4747 max = &t->fwd_queue_max;
4749 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4751 n = &t->bck_queue_n;
4752 max = &t->bck_queue_max;
4757 if (NULL == t->owner)
4758 GNUNET_break_op(0); // TODO: kill connection?
4761 return; // Drop message
4765 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4771 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4772 if (NULL == dst->core_transmit)
4774 struct GNUNET_PeerIdentity id;
4776 GNUNET_PEER_resolve (dst->id, &id);
4777 dst->core_transmit =
4778 GNUNET_CORE_notify_transmit_ready(core_handle,
4781 GNUNET_TIME_UNIT_FOREVER_REL,
4790 /******************************************************************************/
4791 /******************** MESH NETWORK HANDLERS **************************/
4792 /******************************************************************************/
4796 * Core handler for path creation
4798 * @param cls closure
4799 * @param message message
4800 * @param peer peer identity this notification is about
4801 * @param atsi performance data
4802 * @param atsi_count number of records in 'atsi'
4804 * @return GNUNET_OK to keep the connection open,
4805 * GNUNET_SYSERR to close it (signal serious error)
4808 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4809 const struct GNUNET_MessageHeader *message,
4810 const struct GNUNET_ATS_Information *atsi,
4811 unsigned int atsi_count)
4813 unsigned int own_pos;
4816 MESH_TunnelNumber tid;
4817 struct GNUNET_MESH_ManipulatePath *msg;
4818 struct GNUNET_PeerIdentity *pi;
4819 struct GNUNET_HashCode hash;
4820 struct MeshPeerPath *path;
4821 struct MeshPeerInfo *dest_peer_info;
4822 struct MeshPeerInfo *orig_peer_info;
4823 struct MeshTunnel *t;
4825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4826 "Received a path create msg [%s]\n",
4827 GNUNET_i2s (&my_full_id));
4828 size = ntohs (message->size);
4829 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4831 GNUNET_break_op (0);
4835 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4836 if (size % sizeof (struct GNUNET_PeerIdentity))
4838 GNUNET_break_op (0);
4841 size /= sizeof (struct GNUNET_PeerIdentity);
4844 GNUNET_break_op (0);
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4848 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4850 tid = ntohl (msg->tid);
4851 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4853 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4854 t = tunnel_get (pi, tid);
4855 if (NULL == t) // FIXME only for INCOMING tunnels?
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4860 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4863 // FIXME notify failure
4866 opt = ntohl (msg->opt);
4867 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4868 GNUNET_YES : GNUNET_NO;
4869 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4870 GNUNET_YES : GNUNET_NO;
4872 if (GNUNET_YES == t->nobuffer)
4874 t->bck_queue_max = 1;
4875 t->fwd_queue_max = 1;
4878 // FIXME only assign a local tid if a local client is interested (on demand)
4879 while (NULL != tunnel_get_incoming (next_local_tid))
4880 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4881 t->local_tid_dest = next_local_tid++;
4882 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4885 tunnel_reset_timeout (t);
4886 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4888 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4889 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4897 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4898 if (NULL == dest_peer_info)
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4901 " Creating PeerInfo for destination.\n");
4902 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4903 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4904 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4906 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4908 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4909 if (NULL == orig_peer_info)
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4912 " Creating PeerInfo for origin.\n");
4913 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4914 orig_peer_info->id = GNUNET_PEER_intern (pi);
4915 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4919 path = path_new (size);
4921 for (i = 0; i < size; i++)
4923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4924 GNUNET_i2s (&pi[i]));
4925 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4926 if (path->peers[i] == myid)
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4932 /* cannot be self, must be 'not found' */
4933 /* create path: self not found in path through self */
4934 GNUNET_break_op (0);
4935 path_destroy (path);
4939 path_add_to_peers (path, GNUNET_NO);
4940 tunnel_add_path (t, path, own_pos);
4941 if (own_pos == size - 1)
4943 /* It is for us! Send ack. */
4944 struct MeshTransmissionDescriptor *info;
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4947 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4948 if (NULL == t->peers)
4950 /* New tunnel! Notify clients on data. */
4951 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4953 GNUNET_break (GNUNET_SYSERR !=
4954 GNUNET_CONTAINER_multihashmap_put (t->peers,
4955 &my_full_id.hashPubKey,
4958 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4959 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4960 info->origin = &t->id;
4961 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4962 GNUNET_assert (NULL != info->peer);
4964 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4965 sizeof (struct GNUNET_MESH_PathACK),
4971 struct MeshPeerPath *path2;
4973 /* It's for somebody else! Retransmit. */
4974 path2 = path_duplicate (path);
4975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4976 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4977 path2 = path_duplicate (path);
4978 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4979 send_create_path (dest_peer_info, path, t);
4986 * Core handler for path destruction
4988 * @param cls closure
4989 * @param message message
4990 * @param peer peer identity this notification is about
4991 * @param atsi performance data
4992 * @param atsi_count number of records in 'atsi'
4994 * @return GNUNET_OK to keep the connection open,
4995 * GNUNET_SYSERR to close it (signal serious error)
4998 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4999 const struct GNUNET_MessageHeader *message,
5000 const struct GNUNET_ATS_Information *atsi,
5001 unsigned int atsi_count)
5003 struct GNUNET_MESH_ManipulatePath *msg;
5004 struct GNUNET_PeerIdentity *pi;
5005 struct MeshPeerPath *path;
5006 struct MeshTunnel *t;
5007 unsigned int own_pos;
5011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5012 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5013 size = ntohs (message->size);
5014 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5016 GNUNET_break_op (0);
5020 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5021 if (size % sizeof (struct GNUNET_PeerIdentity))
5023 GNUNET_break_op (0);
5026 size /= sizeof (struct GNUNET_PeerIdentity);
5029 GNUNET_break_op (0);
5032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5034 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5035 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5037 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5039 t = tunnel_get (pi, ntohl (msg->tid));
5042 /* TODO notify back: we don't know this tunnel */
5043 GNUNET_break_op (0);
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5047 path = path_new (size);
5049 for (i = 0; i < size; i++)
5051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5052 GNUNET_i2s (&pi[i]));
5053 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5054 if (path->peers[i] == myid)
5057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5058 if (own_pos < path->length - 1)
5059 send_message (message, &pi[own_pos + 1], t);
5061 send_client_tunnel_disconnect(t, NULL);
5063 tunnel_delete_peer (t, path->peers[path->length - 1]);
5064 path_destroy (path);
5070 * Core handler for notifications of broken paths
5072 * @param cls closure
5073 * @param message message
5074 * @param peer peer identity this notification is about
5075 * @param atsi performance data
5076 * @param atsi_count number of records in 'atsi'
5078 * @return GNUNET_OK to keep the connection open,
5079 * GNUNET_SYSERR to close it (signal serious error)
5082 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5083 const struct GNUNET_MessageHeader *message,
5084 const struct GNUNET_ATS_Information *atsi,
5085 unsigned int atsi_count)
5087 struct GNUNET_MESH_PathBroken *msg;
5088 struct MeshTunnel *t;
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5091 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5092 msg = (struct GNUNET_MESH_PathBroken *) message;
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5094 GNUNET_i2s (&msg->peer1));
5095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5096 GNUNET_i2s (&msg->peer2));
5097 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5100 GNUNET_break_op (0);
5103 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5104 GNUNET_PEER_search (&msg->peer2));
5111 * Core handler for tunnel destruction
5113 * @param cls closure
5114 * @param message message
5115 * @param peer peer identity this notification is about
5116 * @param atsi performance data
5117 * @param atsi_count number of records in 'atsi'
5119 * @return GNUNET_OK to keep the connection open,
5120 * GNUNET_SYSERR to close it (signal serious error)
5123 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5124 const struct GNUNET_MessageHeader *message,
5125 const struct GNUNET_ATS_Information *atsi,
5126 unsigned int atsi_count)
5128 struct GNUNET_MESH_TunnelDestroy *msg;
5129 struct MeshTunnel *t;
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5132 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5133 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5135 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5136 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5139 /* Probably already got the message from another path,
5140 * destroyed the tunnel and retransmitted to children.
5143 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5146 if (t->id.oid == myid)
5148 GNUNET_break_op (0);
5151 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5153 /* Tunnel was incoming, notify clients */
5154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5155 t->local_tid, t->local_tid_dest);
5156 send_clients_tunnel_destroy (t);
5158 tunnel_send_destroy (t);
5159 t->destroy = GNUNET_YES;
5160 // TODO: add timeout to destroy the tunnel anyway
5166 * Core handler for mesh network traffic going from the origin to a peer
5168 * @param cls closure
5169 * @param peer peer identity this notification is about
5170 * @param message message
5171 * @param atsi performance data
5172 * @param atsi_count number of records in 'atsi'
5173 * @return GNUNET_OK to keep the connection open,
5174 * GNUNET_SYSERR to close it (signal serious error)
5177 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5178 const struct GNUNET_MessageHeader *message,
5179 const struct GNUNET_ATS_Information *atsi,
5180 unsigned int atsi_count)
5182 struct GNUNET_MESH_Unicast *msg;
5183 struct GNUNET_PeerIdentity *neighbor;
5184 struct MeshTunnelChildInfo *cinfo;
5185 struct MeshTunnel *t;
5186 GNUNET_PEER_Id dest_id;
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5194 size = ntohs (message->size);
5196 sizeof (struct GNUNET_MESH_Unicast) +
5197 sizeof (struct GNUNET_MessageHeader))
5202 msg = (struct GNUNET_MESH_Unicast *) message;
5203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5204 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5206 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5209 /* TODO notify back: we don't know this tunnel */
5210 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5211 GNUNET_break_op (0);
5214 pid = ntohl (msg->pid);
5215 if (t->fwd_pid == pid)
5217 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5219 " Already seen pid %u, DROPPING!\n", pid);
5224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5225 " pid %u not seen yet, forwarding\n", pid);
5228 t->skip += (pid - t->fwd_pid) - 1;
5231 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5233 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5234 GNUNET_break_op (0);
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5236 "Received PID %u, ACK %u\n",
5237 pid, t->last_fwd_ack);
5241 tunnel_reset_timeout (t);
5242 dest_id = GNUNET_PEER_search (&msg->destination);
5243 if (dest_id == myid)
5245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5246 " it's for us! sending to clients...\n");
5247 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5248 send_subscribed_clients (message, &msg[1].header, t);
5249 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5252 ttl = ntohl (msg->ttl);
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5256 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5257 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5258 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5262 " not for us, retransmitting...\n");
5264 neighbor = tree_get_first_hop (t->tree, dest_id);
5265 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5267 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5270 if (GNUNET_YES == t->nobuffer &&
5271 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5273 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5274 GNUNET_break_op (0);
5277 send_message (message, neighbor, t);
5278 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5284 * Core handler for mesh network traffic going from the origin to all peers
5286 * @param cls closure
5287 * @param message message
5288 * @param peer peer identity this notification is about
5289 * @param atsi performance data
5290 * @param atsi_count number of records in 'atsi'
5291 * @return GNUNET_OK to keep the connection open,
5292 * GNUNET_SYSERR to close it (signal serious error)
5294 * TODO: Check who we got this from, to validate route.
5297 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5298 const struct GNUNET_MessageHeader *message,
5299 const struct GNUNET_ATS_Information *atsi,
5300 unsigned int atsi_count)
5302 struct GNUNET_MESH_Multicast *msg;
5303 struct MeshTunnel *t;
5307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5309 size = ntohs (message->size);
5310 if (sizeof (struct GNUNET_MESH_Multicast) +
5311 sizeof (struct GNUNET_MessageHeader) > size)
5313 GNUNET_break_op (0);
5316 msg = (struct GNUNET_MESH_Multicast *) message;
5317 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5321 /* TODO notify that we dont know that tunnel */
5322 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5323 GNUNET_break_op (0);
5326 pid = ntohl (msg->pid);
5327 if (t->fwd_pid == pid)
5329 /* already seen this packet, drop */
5330 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5332 " Already seen pid %u, DROPPING!\n", pid);
5333 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5339 " pid %u not seen yet, forwarding\n", pid);
5341 t->skip += (pid - t->fwd_pid) - 1;
5343 tunnel_reset_timeout (t);
5345 /* Transmit to locally interested clients */
5346 if (NULL != t->peers &&
5347 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5349 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5350 send_subscribed_clients (message, &msg[1].header, t);
5351 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5354 if (ntohl (msg->ttl) == 0)
5356 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5358 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5361 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5362 tunnel_send_multicast (t, message, GNUNET_NO);
5368 * Core handler for mesh network traffic toward the owner of a tunnel
5370 * @param cls closure
5371 * @param message message
5372 * @param peer peer identity this notification is about
5373 * @param atsi performance data
5374 * @param atsi_count number of records in 'atsi'
5376 * @return GNUNET_OK to keep the connection open,
5377 * GNUNET_SYSERR to close it (signal serious error)
5380 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5381 const struct GNUNET_MessageHeader *message,
5382 const struct GNUNET_ATS_Information *atsi,
5383 unsigned int atsi_count)
5385 struct GNUNET_MESH_ToOrigin *msg;
5386 struct GNUNET_PeerIdentity id;
5387 struct MeshPeerInfo *peer_info;
5388 struct MeshTunnel *t;
5392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5394 size = ntohs (message->size);
5395 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5396 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5398 GNUNET_break_op (0);
5401 msg = (struct GNUNET_MESH_ToOrigin *) message;
5402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5403 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5404 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5408 /* TODO notify that we dont know this tunnel (whom)? */
5409 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5410 GNUNET_break_op (0);
5414 if (NULL != t->owner)
5417 struct GNUNET_MESH_ToOrigin *copy;
5419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5420 " it's for us! sending to clients...\n");
5421 /* TODO signature verification */
5422 memcpy (cbuf, message, size);
5423 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5424 copy->tid = htonl (t->local_tid);
5425 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5426 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5427 ©->header, GNUNET_NO);
5428 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5432 " not for us, retransmitting...\n");
5434 peer_info = peer_info_get (&msg->oid);
5435 if (NULL == peer_info)
5437 /* unknown origin of tunnel */
5441 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5442 send_message (message, &id, t);
5443 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5450 * Core handler for mesh network traffic point-to-point acks.
5452 * @param cls closure
5453 * @param message message
5454 * @param peer peer identity this notification is about
5455 * @param atsi performance data
5456 * @param atsi_count number of records in 'atsi'
5458 * @return GNUNET_OK to keep the connection open,
5459 * GNUNET_SYSERR to close it (signal serious error)
5462 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5463 const struct GNUNET_MessageHeader *message,
5464 const struct GNUNET_ATS_Information *atsi,
5465 unsigned int atsi_count)
5467 struct GNUNET_MESH_ACK *msg;
5468 struct MeshTunnel *t;
5471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5473 msg = (struct GNUNET_MESH_ACK *) message;
5475 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5479 /* TODO notify that we dont know this tunnel (whom)? */
5480 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5481 GNUNET_break_op (0);
5484 ack = ntohl (msg->pid);
5486 /* Is this a forward or backward ACK? */
5487 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5489 struct MeshTunnelChildInfo *cinfo;
5491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5492 cinfo = tunnel_get_neighbor_fc (t, peer);
5493 cinfo->fwd_ack = ack;
5494 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5495 tunnel_unlock_fwd_queues (t);
5499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5501 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5502 tunnel_unlock_bck_queue (t);
5509 * Core handler for path ACKs
5511 * @param cls closure
5512 * @param message message
5513 * @param peer peer identity this notification is about
5514 * @param atsi performance data
5515 * @param atsi_count number of records in 'atsi'
5517 * @return GNUNET_OK to keep the connection open,
5518 * GNUNET_SYSERR to close it (signal serious error)
5521 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5522 const struct GNUNET_MessageHeader *message,
5523 const struct GNUNET_ATS_Information *atsi,
5524 unsigned int atsi_count)
5526 struct GNUNET_MESH_PathACK *msg;
5527 struct GNUNET_PeerIdentity id;
5528 struct MeshPeerInfo *peer_info;
5529 struct MeshPeerPath *p;
5530 struct MeshTunnel *t;
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5533 GNUNET_i2s (&my_full_id));
5534 msg = (struct GNUNET_MESH_PathACK *) message;
5535 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5538 /* TODO notify that we don't know the tunnel */
5539 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5541 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5545 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5547 peer_info = peer_info_get (&msg->peer_id);
5548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5549 GNUNET_i2s (&msg->peer_id));
5550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5553 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5556 "connect_by_string completed, stopping search\n");
5557 regex_cancel_search (t->regex_ctx);
5558 t->regex_ctx = NULL;
5561 /* Add paths to peers? */
5562 p = tree_get_path_to_peer (t->tree, peer_info->id);
5565 path_add_to_peers (p, GNUNET_YES);
5573 /* Message for us? */
5574 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5577 if (NULL == t->owner)
5579 GNUNET_break_op (0);
5582 if (NULL != t->dht_get_type)
5584 GNUNET_DHT_get_stop (t->dht_get_type);
5585 t->dht_get_type = NULL;
5587 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5589 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5590 send_client_peer_connected (t, peer_info->id);
5595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5596 " not for us, retransmitting...\n");
5597 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5598 peer_info = peer_info_get (&msg->oid);
5599 if (NULL == peer_info)
5601 /* If we know the tunnel, we should DEFINITELY know the peer */
5605 send_message (message, &id, t);
5611 * Functions to handle messages from core
5613 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5614 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5615 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5616 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5617 sizeof (struct GNUNET_MESH_PathBroken)},
5618 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5619 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5620 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5621 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5622 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5623 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5624 sizeof (struct GNUNET_MESH_ACK)},
5625 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5626 sizeof (struct GNUNET_MESH_PathACK)},
5632 /******************************************************************************/
5633 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5634 /******************************************************************************/
5637 * deregister_app: iterator for removing each application registered by a client
5639 * @param cls closure
5640 * @param key the hash of the application id (used to access the hashmap)
5641 * @param value the value stored at the key (client)
5643 * @return GNUNET_OK on success
5646 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5648 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5649 GNUNET_break (GNUNET_YES ==
5650 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5656 * notify_client_connection_failure: notify a client that the connection to the
5657 * requested remote peer is not possible (for instance, no route found)
5658 * Function called when the socket is ready to queue more data. "buf" will be
5659 * NULL and "size" zero if the socket was closed for writing in the meantime.
5661 * @param cls closure
5662 * @param size number of bytes available in buf
5663 * @param buf where the callee should write the message
5664 * @return number of bytes written to buf
5667 notify_client_connection_failure (void *cls, size_t size, void *buf)
5670 struct MeshPeerInfo *peer_info;
5671 struct GNUNET_MESH_PeerControl *msg;
5672 struct GNUNET_PeerIdentity id;
5674 if (0 == size && NULL == buf)
5676 // TODO retry? cancel?
5680 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5681 peer_info = (struct MeshPeerInfo *) cls;
5682 msg = (struct GNUNET_MESH_PeerControl *) buf;
5683 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5684 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5685 // msg->tunnel_id = htonl(peer_info->t->tid);
5686 GNUNET_PEER_resolve (peer_info->id, &id);
5687 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5695 * Send keepalive packets for a peer
5697 * @param cls Closure (tunnel for which to send the keepalive).
5698 * @param tc Notification context.
5700 * TODO: implement explicit multicast keepalive?
5703 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5705 struct MeshTunnel *t = cls;
5706 struct GNUNET_MessageHeader *payload;
5707 struct GNUNET_MESH_Multicast *msg;
5709 sizeof (struct GNUNET_MESH_Multicast) +
5710 sizeof (struct GNUNET_MessageHeader);
5713 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5717 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5720 "sending keepalive for tunnel %d\n", t->id.tid);
5722 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5723 msg->header.size = htons (size);
5724 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5725 // FIXME: change type to != MULTICAST
5726 msg->oid = my_full_id;
5727 msg->tid = htonl (t->id.tid);
5728 msg->ttl = htonl (default_ttl);
5729 msg->pid = htonl (t->fwd_pid + 1);
5731 payload = (struct GNUNET_MessageHeader *) &msg[1];
5732 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5733 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5734 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5736 t->path_refresh_task =
5737 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5743 * Function to process paths received for a new peer addition. The recorded
5744 * paths form the initial tunnel, which can be optimized later.
5745 * Called on each result obtained for the DHT search.
5747 * @param cls closure
5748 * @param exp when will this value expire
5749 * @param key key of the result
5750 * @param get_path path of the get request
5751 * @param get_path_length lenght of get_path
5752 * @param put_path path of the put request
5753 * @param put_path_length length of the put_path
5754 * @param type type of the result
5755 * @param size number of bytes in data
5756 * @param data pointer to the result data
5758 * TODO: re-issue the request after certain time? cancel after X results?
5761 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5762 const struct GNUNET_HashCode * key,
5763 const struct GNUNET_PeerIdentity *get_path,
5764 unsigned int get_path_length,
5765 const struct GNUNET_PeerIdentity *put_path,
5766 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5767 size_t size, const void *data)
5769 struct MeshPathInfo *path_info = cls;
5770 struct MeshPeerPath *p;
5771 struct GNUNET_PeerIdentity pi;
5774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5775 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5778 p = path_build_from_dht (get_path, get_path_length, put_path,
5780 path_add_to_peers (p, GNUNET_NO);
5782 for (i = 0; i < path_info->peer->ntunnels; i++)
5784 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5785 peer_info_connect (path_info->peer, path_info->t);
5793 * Function to process paths received for a new peer addition. The recorded
5794 * paths form the initial tunnel, which can be optimized later.
5795 * Called on each result obtained for the DHT search.
5797 * @param cls closure
5798 * @param exp when will this value expire
5799 * @param key key of the result
5800 * @param get_path path of the get request
5801 * @param get_path_length lenght of get_path
5802 * @param put_path path of the put request
5803 * @param put_path_length length of the put_path
5804 * @param type type of the result
5805 * @param size number of bytes in data
5806 * @param data pointer to the result data
5809 dht_get_type_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 PBlock *pb = data;
5818 const struct GNUNET_PeerIdentity *pi = &pb->id;
5819 struct MeshTunnel *t = cls;
5820 struct MeshPeerInfo *peer_info;
5821 struct MeshPeerPath *p;
5823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5824 if (size != sizeof (struct PBlock))
5826 GNUNET_break_op (0);
5829 if (ntohl(pb->type) != t->type)
5831 GNUNET_break_op (0);
5834 GNUNET_assert (NULL != t->owner);
5835 peer_info = peer_info_get (pi);
5836 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5838 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5840 p = path_build_from_dht (get_path, get_path_length, put_path,
5842 path_add_to_peers (p, GNUNET_NO);
5844 tunnel_add_peer (t, peer_info);
5845 peer_info_connect (peer_info, t);
5850 * Function to process DHT string to regex matching.
5851 * Called on each result obtained for the DHT search.
5853 * @param cls closure (search context)
5854 * @param exp when will this value expire
5855 * @param key key of the result
5856 * @param get_path path of the get request (not used)
5857 * @param get_path_length lenght of get_path (not used)
5858 * @param put_path path of the put request (not used)
5859 * @param put_path_length length of the put_path (not used)
5860 * @param type type of the result
5861 * @param size number of bytes in data
5862 * @param data pointer to the result data
5865 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5866 const struct GNUNET_HashCode * key,
5867 const struct GNUNET_PeerIdentity *get_path,
5868 unsigned int get_path_length,
5869 const struct GNUNET_PeerIdentity *put_path,
5870 unsigned int put_path_length,
5871 enum GNUNET_BLOCK_Type type,
5872 size_t size, const void *data)
5874 const struct MeshRegexAccept *block = data;
5875 struct MeshRegexSearchContext *ctx = cls;
5876 struct MeshRegexSearchInfo *info = ctx->info;
5877 struct MeshPeerPath *p;
5878 struct MeshPeerInfo *peer_info;
5880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5883 peer_info = peer_info_get(&block->id);
5884 p = path_build_from_dht (get_path, get_path_length, put_path,
5886 path_add_to_peers (p, GNUNET_NO);
5889 tunnel_add_peer (info->t, peer_info);
5890 peer_info_connect (peer_info, info->t);
5891 if (0 == info->peer)
5893 info->peer = peer_info->id;
5897 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5900 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5901 ®ex_connect_timeout,
5909 * Function to process DHT string to regex matching.
5910 * Called on each result obtained for the DHT search.
5912 * @param cls closure (search context)
5913 * @param exp when will this value expire
5914 * @param key key of the result
5915 * @param get_path path of the get request (not used)
5916 * @param get_path_length lenght of get_path (not used)
5917 * @param put_path path of the put request (not used)
5918 * @param put_path_length length of the put_path (not used)
5919 * @param type type of the result
5920 * @param size number of bytes in data
5921 * @param data pointer to the result data
5923 * TODO: re-issue the request after certain time? cancel after X results?
5926 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5927 const struct GNUNET_HashCode * key,
5928 const struct GNUNET_PeerIdentity *get_path,
5929 unsigned int get_path_length,
5930 const struct GNUNET_PeerIdentity *put_path,
5931 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5932 size_t size, const void *data)
5934 const struct MeshRegexBlock *block = data;
5935 struct MeshRegexSearchContext *ctx = cls;
5936 struct MeshRegexSearchInfo *info = ctx->info;
5940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5941 "DHT GET STRING RETURNED RESULTS\n");
5942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5943 " key: %s\n", GNUNET_h2s (key));
5945 copy = GNUNET_malloc (size);
5946 memcpy (copy, data, size);
5947 GNUNET_break (GNUNET_OK ==
5948 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5949 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5950 len = ntohl (block->n_proof);
5952 char proof[len + 1];
5954 memcpy (proof, &block[1], len);
5956 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5958 GNUNET_break_op (0);
5962 len = strlen (info->description);
5963 if (len == ctx->position) // String processed
5965 if (GNUNET_YES == ntohl (block->accepting))
5967 regex_find_path(key, ctx);
5971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5972 // FIXME REGEX this block not successful, wait for more? start timeout?
5976 GNUNET_break (GNUNET_OK ==
5977 GNUNET_MESH_regex_block_iterate (block, size,
5978 ®ex_edge_iterator, ctx));
5982 /******************************************************************************/
5983 /********************* MESH LOCAL HANDLES **************************/
5984 /******************************************************************************/
5988 * Handler for client disconnection
5990 * @param cls closure
5991 * @param client identification of the client; NULL
5992 * for the last call when the server is destroyed
5995 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5997 struct MeshClient *c;
5998 struct MeshClient *next;
6001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6010 if (c->handle != client)
6012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6018 GNUNET_SERVER_client_drop (c->handle);
6019 c->shutting_down = GNUNET_YES;
6020 GNUNET_assert (NULL != c->own_tunnels);
6021 GNUNET_assert (NULL != c->incoming_tunnels);
6022 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6023 &tunnel_destroy_iterator, c);
6024 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6025 &tunnel_destroy_iterator, c);
6026 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6027 &tunnel_destroy_iterator, c);
6028 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6029 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6030 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6032 /* deregister clients applications */
6033 if (NULL != c->apps)
6035 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6036 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6038 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6039 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6041 GNUNET_SCHEDULER_cancel (announce_applications_task);
6042 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6044 if (NULL != c->types)
6045 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6046 for (i = 0; i < c->n_regex; i++)
6048 GNUNET_free (c->regexes[i]);
6050 GNUNET_free_non_null (c->regexes);
6051 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6052 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6054 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6057 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6066 * Handler for new clients
6068 * @param cls closure
6069 * @param client identification of the client
6070 * @param message the actual message, which includes messages the client wants
6073 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6074 const struct GNUNET_MessageHeader *message)
6076 struct GNUNET_MESH_ClientConnect *cc_msg;
6077 struct MeshClient *c;
6078 GNUNET_MESH_ApplicationType *a;
6085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6086 /* Check data sanity */
6087 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6088 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6089 ntypes = ntohs (cc_msg->types);
6090 napps = ntohs (cc_msg->applications);
6092 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6095 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6099 /* Create new client structure */
6100 c = GNUNET_malloc (sizeof (struct MeshClient));
6101 c->id = next_client_id++;
6102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6104 GNUNET_SERVER_client_keep (client);
6105 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6108 GNUNET_MESH_ApplicationType at;
6109 struct GNUNET_HashCode hc;
6111 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6112 for (i = 0; i < napps; i++)
6115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6116 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6117 /* store in clients hashmap */
6118 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6119 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6120 /* store in global hashmap, for announcements */
6121 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6122 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6124 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6125 announce_applications_task =
6126 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6132 struct GNUNET_HashCode hc;
6134 t = (uint16_t *) & a[napps];
6135 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6136 for (i = 0; i < ntypes; i++)
6139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6140 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6142 /* store in clients hashmap */
6143 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6144 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6145 /* store in global hashmap */
6146 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6147 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6151 " client has %u+%u subscriptions\n", napps, ntypes);
6153 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6154 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6155 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6156 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6157 GNUNET_SERVER_notification_context_add (nc, client);
6158 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6160 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6166 * Handler for clients announcing available services by a regular expression.
6168 * @param cls closure
6169 * @param client identification of the client
6170 * @param message the actual message, which includes messages the client wants
6173 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6174 const struct GNUNET_MessageHeader *message)
6176 struct MeshClient *c;
6180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6182 /* Sanity check for client registration */
6183 if (NULL == (c = client_get (client)))
6186 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6191 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6192 regex = GNUNET_malloc (len + 1);
6193 memcpy (regex, &message[1], len);
6195 GNUNET_array_append (c->regexes, c->n_regex, regex);
6196 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6198 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6204 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6210 * Handler for requests of new tunnels
6212 * @param cls closure
6213 * @param client identification of the client
6214 * @param message the actual message
6217 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6218 const struct GNUNET_MessageHeader *message)
6220 struct GNUNET_MESH_TunnelMessage *t_msg;
6221 struct MeshTunnel *t;
6222 struct MeshClient *c;
6223 MESH_TunnelNumber tid;
6225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6227 /* Sanity check for client registration */
6228 if (NULL == (c = client_get (client)))
6231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6236 /* Message sanity check */
6237 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6240 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6244 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6245 /* Sanity check for tunnel numbering */
6246 tid = ntohl (t_msg->tunnel_id);
6247 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6250 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6253 /* Sanity check for duplicate tunnel IDs */
6254 if (NULL != tunnel_get_by_local_id (c, tid))
6257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6261 while (NULL != tunnel_get_by_pi (myid, next_tid))
6262 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6263 t = tunnel_new (myid, next_tid++, c, tid);
6266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6268 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6271 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6273 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6274 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6277 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6283 * Handler for requests of deleting tunnels
6285 * @param cls closure
6286 * @param client identification of the client
6287 * @param message the actual message
6290 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6291 const struct GNUNET_MessageHeader *message)
6293 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6294 struct MeshClient *c;
6295 struct MeshTunnel *t;
6296 MESH_TunnelNumber tid;
6298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6299 "Got a DESTROY TUNNEL from client!\n");
6301 /* Sanity check for client registration */
6302 if (NULL == (c = client_get (client)))
6305 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6310 /* Message sanity check */
6311 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6318 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6320 /* Retrieve tunnel */
6321 tid = ntohl (tunnel_msg->tunnel_id);
6322 t = tunnel_get_by_local_id(c, tid);
6325 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6327 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6330 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6332 client_ignore_tunnel (c, t);
6334 // TODO: when to destroy incoming tunnel?
6335 if (t->nclients == 0)
6337 GNUNET_assert (GNUNET_YES ==
6338 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6340 GNUNET_assert (GNUNET_YES ==
6341 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6342 &my_full_id.hashPubKey,
6346 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6349 send_client_tunnel_disconnect(t, c);
6350 client_delete_tunnel(c, t);
6352 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6354 tunnel_send_destroy (t);
6355 t->destroy = GNUNET_YES;
6356 // The tunnel will be destroyed when the last message is transmitted.
6357 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6363 * Handler for requests of seeting tunnel's speed.
6365 * @param cls Closure (unused).
6366 * @param client Identification of the client.
6367 * @param message The actual message.
6370 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6371 const struct GNUNET_MessageHeader *message)
6373 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6374 struct MeshClient *c;
6375 struct MeshTunnel *t;
6376 MESH_TunnelNumber tid;
6378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6379 "Got a SPEED request from client!\n");
6381 /* Sanity check for client registration */
6382 if (NULL == (c = client_get (client)))
6385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6391 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6393 /* Retrieve tunnel */
6394 tid = ntohl (tunnel_msg->tunnel_id);
6395 t = tunnel_get_by_local_id(c, tid);
6398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6400 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6404 switch (ntohs(message->type))
6406 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6407 t->speed_min = GNUNET_YES;
6409 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6410 t->speed_min = GNUNET_NO;
6415 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6420 * Handler for requests of seeting tunnel's buffering policy.
6422 * @param cls Closure (unused).
6423 * @param client Identification of the client.
6424 * @param message The actual message.
6427 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6428 const struct GNUNET_MessageHeader *message)
6430 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6431 struct MeshClient *c;
6432 struct MeshTunnel *t;
6433 MESH_TunnelNumber tid;
6435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6436 "Got a BUFFER request from client!\n");
6438 /* Sanity check for client registration */
6439 if (NULL == (c = client_get (client)))
6442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6447 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6449 /* Retrieve tunnel */
6450 tid = ntohl (tunnel_msg->tunnel_id);
6451 t = tunnel_get_by_local_id(c, tid);
6454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6460 switch (ntohs(message->type))
6462 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6463 t->nobuffer = GNUNET_NO;
6465 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6466 t->nobuffer = GNUNET_YES;
6472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6477 * Handler for connection requests to new peers
6479 * @param cls closure
6480 * @param client identification of the client
6481 * @param message the actual message (PeerControl)
6484 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6485 const struct GNUNET_MessageHeader *message)
6487 struct GNUNET_MESH_PeerControl *peer_msg;
6488 struct MeshPeerInfo *peer_info;
6489 struct MeshClient *c;
6490 struct MeshTunnel *t;
6491 MESH_TunnelNumber tid;
6493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6494 /* Sanity check for client registration */
6495 if (NULL == (c = client_get (client)))
6498 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6503 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6505 /* Sanity check for message size */
6506 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6513 /* Tunnel exists? */
6514 tid = ntohl (peer_msg->tunnel_id);
6515 t = tunnel_get_by_local_id (c, tid);
6519 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6523 /* Does client own tunnel? */
6524 if (t->owner->handle != client)
6527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6531 GNUNET_i2s (&peer_msg->peer));
6532 peer_info = peer_info_get (&peer_msg->peer);
6534 tunnel_add_peer (t, peer_info);
6535 peer_info_connect (peer_info, t);
6537 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6543 * Handler for disconnection requests of peers in a tunnel
6545 * @param cls closure
6546 * @param client identification of the client
6547 * @param message the actual message (PeerControl)
6550 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6551 const struct GNUNET_MessageHeader *message)
6553 struct GNUNET_MESH_PeerControl *peer_msg;
6554 struct MeshPeerInfo *peer_info;
6555 struct MeshClient *c;
6556 struct MeshTunnel *t;
6557 MESH_TunnelNumber tid;
6559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6560 /* Sanity check for client registration */
6561 if (NULL == (c = client_get (client)))
6564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6569 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6571 /* Sanity check for message size */
6572 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6579 /* Tunnel exists? */
6580 tid = ntohl (peer_msg->tunnel_id);
6581 t = tunnel_get_by_local_id (c, tid);
6585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6590 /* Does client own tunnel? */
6591 if (t->owner->handle != client)
6594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6599 GNUNET_i2s (&peer_msg->peer));
6600 /* Is the peer in the tunnel? */
6602 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6603 if (NULL == peer_info)
6606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6610 /* Ok, delete peer from tunnel */
6611 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6612 &peer_msg->peer.hashPubKey);
6614 send_destroy_path (t, peer_info->id);
6615 tunnel_delete_peer (t, peer_info->id);
6616 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6621 * Handler for blacklist requests of peers in a tunnel
6623 * @param cls closure
6624 * @param client identification of the client
6625 * @param message the actual message (PeerControl)
6627 * FIXME implement DHT block bloomfilter
6630 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6631 const struct GNUNET_MessageHeader *message)
6633 struct GNUNET_MESH_PeerControl *peer_msg;
6634 struct MeshClient *c;
6635 struct MeshTunnel *t;
6636 MESH_TunnelNumber tid;
6638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6639 /* Sanity check for client registration */
6640 if (NULL == (c = client_get (client)))
6643 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6648 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6650 /* Sanity check for message size */
6651 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6654 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6658 /* Tunnel exists? */
6659 tid = ntohl (peer_msg->tunnel_id);
6660 t = tunnel_get_by_local_id (c, tid);
6664 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6669 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6670 GNUNET_PEER_intern(&peer_msg->peer));
6675 * Handler for unblacklist requests of peers in a tunnel
6677 * @param cls closure
6678 * @param client identification of the client
6679 * @param message the actual message (PeerControl)
6682 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6683 const struct GNUNET_MessageHeader *message)
6685 struct GNUNET_MESH_PeerControl *peer_msg;
6686 struct MeshClient *c;
6687 struct MeshTunnel *t;
6688 MESH_TunnelNumber tid;
6692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6693 /* Sanity check for client registration */
6694 if (NULL == (c = client_get (client)))
6697 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6702 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6704 /* Sanity check for message size */
6705 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6712 /* Tunnel exists? */
6713 tid = ntohl (peer_msg->tunnel_id);
6714 t = tunnel_get_by_local_id (c, tid);
6718 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6723 /* if peer is not known, complain */
6724 pid = GNUNET_PEER_search (&peer_msg->peer);
6731 /* search and remove from list */
6732 for (i = 0; i < t->nblacklisted; i++)
6734 if (t->blacklisted[i] == pid)
6736 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6737 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6742 /* if peer hasn't been blacklisted, complain */
6748 * Handler for connection requests to new peers by type
6750 * @param cls closure
6751 * @param client identification of the client
6752 * @param message the actual message (ConnectPeerByType)
6755 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6756 const struct GNUNET_MessageHeader *message)
6758 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6759 struct MeshClient *c;
6760 struct MeshTunnel *t;
6761 struct GNUNET_HashCode hash;
6762 MESH_TunnelNumber tid;
6764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
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 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6776 /* Sanity check for message size */
6777 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6778 ntohs (connect_msg->header.size))
6781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6785 /* Tunnel exists? */
6786 tid = ntohl (connect_msg->tunnel_id);
6787 t = tunnel_get_by_local_id (c, tid);
6791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6795 /* Does client own tunnel? */
6796 if (t->owner->handle != client)
6799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6803 /* Do WE have the service? */
6804 t->type = ntohl (connect_msg->type);
6805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6806 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6807 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6810 /* Yes! Fast forward, add ourselves to the tunnel and send the
6811 * good news to the client, and alert the destination client of
6812 * an incoming tunnel.
6814 * FIXME send a path create to self, avoid code duplication
6816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6817 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6818 peer_info_get (&my_full_id),
6819 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6822 send_client_peer_connected (t, myid);
6823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6824 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6826 t->local_tid_dest = next_local_tid++;
6827 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6828 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6829 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6833 /* Ok, lets find a peer offering the service */
6834 if (NULL != t->dht_get_type)
6836 GNUNET_DHT_get_stop (t->dht_get_type);
6838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6839 GNUNET_h2s (&hash));
6841 GNUNET_DHT_get_start (dht_handle,
6842 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6844 dht_replication_level,
6845 GNUNET_DHT_RO_RECORD_ROUTE |
6846 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6848 &dht_get_type_handler, t);
6850 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6856 * Handler for connection requests to new peers by a string service description.
6858 * @param cls closure
6859 * @param client identification of the client
6860 * @param message the actual message, which includes messages the client wants
6863 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6864 const struct GNUNET_MessageHeader *message)
6866 struct GNUNET_MESH_ConnectPeerByString *msg;
6867 struct MeshRegexSearchContext *ctx;
6868 struct MeshRegexSearchInfo *info;
6869 struct GNUNET_DHT_GetHandle *get_h;
6870 struct GNUNET_HashCode key;
6871 struct MeshTunnel *t;
6872 struct MeshClient *c;
6873 MESH_TunnelNumber tid;
6877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6878 "Connect by string started\n");
6879 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6880 size = htons (message->size);
6882 /* Sanity check for client registration */
6883 if (NULL == (c = client_get (client)))
6886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6891 /* Message size sanity check */
6892 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6899 /* Tunnel exists? */
6900 tid = ntohl (msg->tunnel_id);
6901 t = tunnel_get_by_local_id (c, tid);
6905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6909 /* Does client own tunnel? */
6910 if (t->owner->handle != client)
6913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6918 " on tunnel %s [%u]\n",
6919 GNUNET_i2s(&my_full_id),
6922 /* Only one connect_by_string allowed at the same time! */
6923 /* FIXME: allow more, return handle at api level to cancel, document */
6924 if (NULL != t->regex_ctx)
6927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6931 /* Find string itself */
6932 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6933 string = (const char *) &msg[1];
6935 /* Initialize context */
6936 size = GNUNET_REGEX_get_first_key(string, len, &key);
6937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6938 " consumed %u bits out of %u\n", size, len);
6939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6940 " looking for %s\n", GNUNET_h2s (&key));
6942 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6944 info->description = GNUNET_malloc (len + 1);
6945 memcpy (info->description, string, len);
6946 info->description[len] = '\0';
6947 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6948 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6951 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6952 ctx->position = size;
6956 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6958 /* Start search in DHT */
6959 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6960 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6961 &key, /* key to search */
6962 dht_replication_level, /* replication level */
6963 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6964 NULL, /* xquery */ // FIXME BLOOMFILTER
6965 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6966 &dht_get_string_handler, ctx);
6968 GNUNET_break (GNUNET_OK ==
6969 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6972 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6974 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6980 * Handler for client traffic directed to one peer
6982 * @param cls closure
6983 * @param client identification of the client
6984 * @param message the actual message
6987 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6988 const struct GNUNET_MessageHeader *message)
6990 struct MeshClient *c;
6991 struct MeshTunnel *t;
6992 struct MeshPeerInfo *pi;
6993 struct GNUNET_MESH_Unicast *data_msg;
6994 MESH_TunnelNumber tid;
6997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6998 "Got a unicast request from a client!\n");
7000 /* Sanity check for client registration */
7001 if (NULL == (c = client_get (client)))
7004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7009 data_msg = (struct GNUNET_MESH_Unicast *) message;
7011 /* Sanity check for message size */
7012 size = ntohs (message->size);
7013 if (sizeof (struct GNUNET_MESH_Unicast) +
7014 sizeof (struct GNUNET_MessageHeader) > size)
7017 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7021 /* Tunnel exists? */
7022 tid = ntohl (data_msg->tid);
7023 t = tunnel_get_by_local_id (c, tid);
7027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7031 /* Is it a local tunnel? Then, does client own the tunnel? */
7032 if (t->owner->handle != client)
7035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7039 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7040 &data_msg->destination.hashPubKey);
7041 /* Is the selected peer in the tunnel? */
7045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7049 /* PID should be as expected */
7050 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7053 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7054 "Unicast PID, expected %u, got %u\n",
7055 t->fwd_pid + 1, ntohl (data_msg->pid));
7056 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7060 /* Ok, everything is correct, send the message
7061 * (pretend we got it from a mesh peer)
7064 /* Work around const limitation */
7065 char buf[ntohs (message->size)] GNUNET_ALIGN;
7066 struct GNUNET_MESH_Unicast *copy;
7068 copy = (struct GNUNET_MESH_Unicast *) buf;
7069 memcpy (buf, data_msg, size);
7070 copy->oid = my_full_id;
7071 copy->tid = htonl (t->id.tid);
7072 copy->ttl = htonl (default_ttl);
7073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7074 " calling generic handler...\n");
7075 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7078 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7085 * Handler for client traffic directed to the origin
7087 * @param cls closure
7088 * @param client identification of the client
7089 * @param message the actual message
7092 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7093 const struct GNUNET_MessageHeader *message)
7095 struct GNUNET_MESH_ToOrigin *data_msg;
7096 struct MeshTunnelClientInfo *clinfo;
7097 struct MeshClient *c;
7098 struct MeshTunnel *t;
7099 MESH_TunnelNumber tid;
7102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7103 "Got a ToOrigin request from a client!\n");
7104 /* Sanity check for client registration */
7105 if (NULL == (c = client_get (client)))
7108 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7113 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7115 /* Sanity check for message size */
7116 size = ntohs (message->size);
7117 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7118 sizeof (struct GNUNET_MessageHeader) > size)
7121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7125 /* Tunnel exists? */
7126 tid = ntohl (data_msg->tid);
7127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7128 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7134 t = tunnel_get_by_local_id (c, tid);
7137 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7138 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7144 /* It should be sent by someone who has this as incoming tunnel. */
7145 if (GNUNET_NO == client_knows_tunnel (c, t))
7148 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7152 /* PID should be as expected */
7153 clinfo = tunnel_get_client_fc (t, c);
7154 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7158 "To Origin PID, expected %u, got %u\n",
7159 clinfo->bck_pid + 1,
7160 ntohl (data_msg->pid));
7161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7165 /* Ok, everything is correct, send the message
7166 * (pretend we got it from a mesh peer)
7170 char buf[ntohs (message->size)] GNUNET_ALIGN;
7171 struct GNUNET_MESH_ToOrigin *copy;
7173 /* Work around const limitation */
7174 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7175 memcpy (buf, data_msg, size);
7176 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7177 copy->tid = htonl (t->id.tid);
7178 copy->ttl = htonl (default_ttl);
7179 if (ntohl (copy->pid) != (t->bck_pid + 1))
7182 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7183 "To Origin PID, expected %u, got %u\n",
7189 copy->sender = my_full_id;
7190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7191 " calling generic handler...\n");
7192 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7194 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7201 * Handler for client traffic directed to all peers in a tunnel
7203 * @param cls closure
7204 * @param client identification of the client
7205 * @param message the actual message
7208 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7209 const struct GNUNET_MessageHeader *message)
7211 struct MeshClient *c;
7212 struct MeshTunnel *t;
7213 struct GNUNET_MESH_Multicast *data_msg;
7214 MESH_TunnelNumber tid;
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7217 "Got a multicast request from a client!\n");
7219 /* Sanity check for client registration */
7220 if (NULL == (c = client_get (client)))
7223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7228 data_msg = (struct GNUNET_MESH_Multicast *) message;
7230 /* Sanity check for message size */
7231 if (sizeof (struct GNUNET_MESH_Multicast) +
7232 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7239 /* Tunnel exists? */
7240 tid = ntohl (data_msg->tid);
7241 t = tunnel_get_by_local_id (c, tid);
7245 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7246 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7252 /* Does client own tunnel? */
7253 if (t->owner->handle != client)
7256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7260 /* PID should be as expected */
7261 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7265 "Multicast PID, expected %u, got %u\n",
7266 t->fwd_pid + 1, ntohl (data_msg->pid));
7267 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7272 char buf[ntohs (message->size)] GNUNET_ALIGN;
7273 struct GNUNET_MESH_Multicast *copy;
7275 copy = (struct GNUNET_MESH_Multicast *) buf;
7276 memcpy (buf, message, ntohs (message->size));
7277 copy->oid = my_full_id;
7278 copy->tid = htonl (t->id.tid);
7279 copy->ttl = htonl (default_ttl);
7280 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7282 " calling generic handler...\n");
7283 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7286 /* receive done gets called when last copy is sent to a neighbor */
7292 * Handler for client's ACKs for payload traffic.
7294 * @param cls Closure (unused).
7295 * @param client Identification of the client.
7296 * @param message The actual message.
7299 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7300 const struct GNUNET_MessageHeader *message)
7302 struct GNUNET_MESH_LocalAck *msg;
7303 struct MeshTunnel *t;
7304 struct MeshClient *c;
7305 MESH_TunnelNumber tid;
7308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7309 /* Sanity check for client registration */
7310 if (NULL == (c = client_get (client)))
7313 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7318 msg = (struct GNUNET_MESH_LocalAck *) message;
7320 /* Tunnel exists? */
7321 tid = ntohl (msg->tunnel_id);
7322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7323 t = tunnel_get_by_local_id (c, tid);
7326 GNUNET_break (0); // FIXME fc
7327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7333 ack = ntohl (msg->max_pid);
7334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7336 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7337 if (NULL != t->owner && t->owner->handle == client)
7339 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7341 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7345 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7346 tunnel_set_client_fwd_ack (t, c, ack);
7347 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7350 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7357 * Functions to handle messages from clients
7359 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7360 {&handle_local_new_client, NULL,
7361 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7362 {&handle_local_announce_regex, NULL,
7363 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7364 {&handle_local_tunnel_create, NULL,
7365 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7366 sizeof (struct GNUNET_MESH_TunnelMessage)},
7367 {&handle_local_tunnel_destroy, NULL,
7368 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7369 sizeof (struct GNUNET_MESH_TunnelMessage)},
7370 {&handle_local_tunnel_speed, NULL,
7371 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7372 sizeof (struct GNUNET_MESH_TunnelMessage)},
7373 {&handle_local_tunnel_speed, NULL,
7374 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7375 sizeof (struct GNUNET_MESH_TunnelMessage)},
7376 {&handle_local_tunnel_buffer, NULL,
7377 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7378 sizeof (struct GNUNET_MESH_TunnelMessage)},
7379 {&handle_local_tunnel_buffer, NULL,
7380 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7381 sizeof (struct GNUNET_MESH_TunnelMessage)},
7382 {&handle_local_connect_add, NULL,
7383 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7384 sizeof (struct GNUNET_MESH_PeerControl)},
7385 {&handle_local_connect_del, NULL,
7386 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7387 sizeof (struct GNUNET_MESH_PeerControl)},
7388 {&handle_local_blacklist, NULL,
7389 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7390 sizeof (struct GNUNET_MESH_PeerControl)},
7391 {&handle_local_unblacklist, NULL,
7392 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7393 sizeof (struct GNUNET_MESH_PeerControl)},
7394 {&handle_local_connect_by_type, NULL,
7395 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7396 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7397 {&handle_local_connect_by_string, NULL,
7398 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7399 {&handle_local_unicast, NULL,
7400 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7401 {&handle_local_to_origin, NULL,
7402 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7403 {&handle_local_multicast, NULL,
7404 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7405 {&handle_local_ack, NULL,
7406 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7407 sizeof (struct GNUNET_MESH_LocalAck)},
7413 * To be called on core init/fail.
7415 * @param cls service closure
7416 * @param server handle to the server for this service
7417 * @param identity the public identity of this peer
7420 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7421 const struct GNUNET_PeerIdentity *identity)
7424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7425 core_handle = server;
7426 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7430 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7432 GNUNET_abort(); // Try harder
7439 * Method called whenever a given peer connects.
7441 * @param cls closure
7442 * @param peer peer identity this notification is about
7443 * @param atsi performance data for the connection
7444 * @param atsi_count number of records in 'atsi'
7447 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7448 const struct GNUNET_ATS_Information *atsi,
7449 unsigned int atsi_count)
7451 struct MeshPeerInfo *peer_info;
7452 struct MeshPeerPath *path;
7454 DEBUG_CONN ("Peer connected\n");
7455 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7456 peer_info = peer_info_get (peer);
7457 if (myid == peer_info->id)
7459 DEBUG_CONN (" (self)\n");
7464 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7466 path = path_new (2);
7467 path->peers[0] = myid;
7468 path->peers[1] = peer_info->id;
7469 GNUNET_PEER_change_rc (myid, 1);
7470 GNUNET_PEER_change_rc (peer_info->id, 1);
7471 peer_info_add_path (peer_info, path, GNUNET_YES);
7472 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7478 * Method called whenever a peer disconnects.
7480 * @param cls closure
7481 * @param peer peer identity this notification is about
7484 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7486 struct MeshPeerInfo *pi;
7487 struct MeshPeerQueue *q;
7488 struct MeshPeerQueue *n;
7490 DEBUG_CONN ("Peer disconnected\n");
7491 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7503 /* try to reroute this traffic instead */
7504 queue_destroy(q, GNUNET_YES);
7508 peer_info_remove_path (pi, pi->id, myid);
7511 DEBUG_CONN (" (self)\n");
7513 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7518 /******************************************************************************/
7519 /************************ MAIN FUNCTIONS ****************************/
7520 /******************************************************************************/
7523 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7525 * @param cls closure
7526 * @param key current key code
7527 * @param value value in the hash map
7528 * @return GNUNET_YES if we should continue to iterate,
7532 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7534 struct MeshTunnel *t = value;
7541 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7543 * @param cls closure
7544 * @param key current key code
7545 * @param value value in the hash map
7546 * @return GNUNET_YES if we should continue to iterate,
7550 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7552 struct MeshPeerInfo *p = value;
7553 struct MeshPeerQueue *q;
7554 struct MeshPeerQueue *n;
7562 queue_destroy(q, GNUNET_YES);
7566 peer_info_destroy (p);
7571 * Task run during shutdown.
7577 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7581 if (core_handle != NULL)
7583 GNUNET_CORE_disconnect (core_handle);
7586 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7587 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7588 if (dht_handle != NULL)
7590 GNUNET_DHT_disconnect (dht_handle);
7595 GNUNET_SERVER_notification_context_destroy (nc);
7598 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7600 GNUNET_SCHEDULER_cancel (announce_id_task);
7601 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7607 * Process mesh requests.
7609 * @param cls closure
7610 * @param server the initialized server
7611 * @param c configuration to use
7614 run (void *cls, struct GNUNET_SERVER_Handle *server,
7615 const struct GNUNET_CONFIGURATION_Handle *c)
7617 struct MeshPeerInfo *peer;
7618 struct MeshPeerPath *p;
7621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7622 server_handle = server;
7623 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7624 NULL, /* Closure passed to MESH functions */
7625 &core_init, /* Call core_init once connected */
7626 &core_connect, /* Handle connects */
7627 &core_disconnect, /* remove peers on disconnects */
7628 NULL, /* Don't notify about all incoming messages */
7629 GNUNET_NO, /* For header only in notification */
7630 NULL, /* Don't notify about all outbound messages */
7631 GNUNET_NO, /* For header-only out notification */
7632 core_handlers); /* Register these handlers */
7634 if (core_handle == NULL)
7637 GNUNET_SCHEDULER_shutdown ();
7642 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7645 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7647 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7649 GNUNET_SCHEDULER_shutdown ();
7654 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7655 &refresh_path_time))
7657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7659 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7660 "refresh path time");
7661 GNUNET_SCHEDULER_shutdown ();
7666 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7667 &app_announce_time))
7669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7671 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7672 "app announce time");
7673 GNUNET_SCHEDULER_shutdown ();
7678 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7681 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7683 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7684 "id announce time");
7685 GNUNET_SCHEDULER_shutdown ();
7690 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7691 &unacknowledged_wait_time))
7693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7695 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7696 "unacknowledged wait time");
7697 GNUNET_SCHEDULER_shutdown ();
7702 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7705 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7707 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7709 GNUNET_SCHEDULER_shutdown ();
7714 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7717 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7719 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7721 GNUNET_SCHEDULER_shutdown ();
7726 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7729 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7731 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7733 GNUNET_SCHEDULER_shutdown ();
7738 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7741 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7743 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7749 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7750 &dht_replication_level))
7752 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7754 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7755 "dht replication level", 10);
7756 dht_replication_level = 10;
7760 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7761 GNUNET_free (keyfile);
7762 if (my_private_key == NULL)
7764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7765 _("Mesh service could not access hostkey. Exiting.\n"));
7766 GNUNET_SCHEDULER_shutdown ();
7769 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7770 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7771 &my_full_id.hashPubKey);
7772 myid = GNUNET_PEER_intern (&my_full_id);
7773 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7774 "Mesh for peer [%s] starting\n",
7775 GNUNET_i2s(&my_full_id));
7777 // transport_handle = GNUNET_TRANSPORT_connect(c,
7784 dht_handle = GNUNET_DHT_connect (c, 64);
7785 if (dht_handle == NULL)
7790 stats = GNUNET_STATISTICS_create ("mesh", c);
7794 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7796 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7797 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7798 peers = GNUNET_CONTAINER_multihashmap_create (32);
7799 applications = GNUNET_CONTAINER_multihashmap_create (32);
7800 types = GNUNET_CONTAINER_multihashmap_create (32);
7802 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7803 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7804 GNUNET_SERVER_disconnect_notify (server_handle,
7805 &handle_local_client_disconnect, NULL);
7809 clients_tail = NULL;
7812 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7813 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7815 /* Create a peer_info for the local peer */
7816 peer = peer_info_get (&my_full_id);
7819 GNUNET_PEER_change_rc (myid, 1);
7820 peer_info_add_path (peer, p, GNUNET_YES);
7822 /* Scheduled the task to clean up when shutdown is called */
7823 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7830 * The main function for the mesh service.
7832 * @param argc number of arguments from the command line
7833 * @param argv command line arguments
7834 * @return 0 ok, 1 on error
7837 main (int argc, char *const *argv)
7841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7844 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");