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
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING GNUNET_YES
64 #if MESH_DEBUG_CONNECTION
65 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_CONN(...)
71 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
73 #define DEBUG_DHT(...)
80 struct timespec __mesh_start;
81 struct timespec __mesh_end;
82 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
83 #define INTERVAL_END \
85 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
86 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
87 if (__diff < 0) __diff += 1000000000;\
91 #define INTERVAL_SHOW GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
93 #define INTERVAL_START
98 /******************************************************************************/
99 /************************ DATA STRUCTURES ****************************/
100 /******************************************************************************/
102 /** FWD declaration */
108 * Struct representing a piece of data being sent to other peers
112 /** Tunnel it belongs to. */
113 struct MeshTunnel *t;
115 /** In case of a multicast, task to allow a client to send more data if
116 * some neighbor is too slow. */
117 GNUNET_SCHEDULER_TaskIdentifier *task;
119 /** How many remaining neighbors we need to send this to. */
120 unsigned int *reference_counter;
122 /** Size of the data. */
131 * Struct containing info about a queued transmission to this peer
138 struct MeshPeerQueue *next;
143 struct MeshPeerQueue *prev;
146 * Peer this transmission is directed to.
148 struct MeshPeerInfo *peer;
151 * Tunnel this message belongs to.
153 struct MeshTunnel *tunnel;
156 * Pointer to info stucture used as cls.
166 * Size of the message
173 * Struct containing all info possibly needed to build a package when called
176 struct MeshTransmissionDescriptor
178 /** ID of the tunnel this packet travels in */
179 struct MESH_TunnelID *origin;
181 /** Who was this message being sent to */
182 struct MeshPeerInfo *peer;
184 /** Ultimate destination of the packet */
185 GNUNET_PEER_Id destination;
187 /** Data descriptor */
188 struct MeshData* mesh_data;
193 * Struct containing all information regarding a given peer
203 * Last time we heard from this peer
205 struct GNUNET_TIME_Absolute last_contact;
208 * Number of attempts to reconnect so far
210 int n_reconnect_attempts;
213 * Paths to reach the peer, ordered by ascending hop count
215 struct MeshPeerPath *path_head;
218 * Paths to reach the peer, ordered by ascending hop count
220 struct MeshPeerPath *path_tail;
223 * Handle to stop the DHT search for a path to this peer
225 struct GNUNET_DHT_GetHandle *dhtget;
228 * Closure given to the DHT GET
230 struct MeshPathInfo *dhtgetcls;
233 * Array of tunnels this peer participates in
234 * (most probably a small amount, therefore not a hashmap)
235 * When the path to the peer changes, notify these tunnels to let them
236 * re-adjust their path trees.
238 struct MeshTunnel **tunnels;
241 * Number of tunnels this peers participates in
243 unsigned int ntunnels;
246 * Transmission queue to core DLL head
248 struct MeshPeerQueue *queue_head;
251 * Transmission queue to core DLL tail
253 struct MeshPeerQueue *queue_tail;
256 * How many messages are in the queue to this peer.
258 unsigned int queue_n;
261 * Handle to for queued transmissions
263 struct GNUNET_CORE_TransmitHandle *core_transmit;
268 * Globally unique tunnel identification (owner + number)
269 * DO NOT USE OVER THE NETWORK
274 * Node that owns the tunnel
279 * Tunnel number to differentiate all the tunnels owned by the node oid
280 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
282 MESH_TunnelNumber tid;
287 * Struct containing all information regarding a tunnel
288 * For an intermediate node the improtant info used will be:
289 * - id Tunnel unique identification
290 * - paths[0] To know where to send it next
291 * - metainfo: ready, speeds, accounting
298 struct MESH_TunnelID id;
301 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
303 MESH_TunnelNumber local_tid;
306 * Local tunnel number for local destination clients (incoming number)
307 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
310 MESH_TunnelNumber local_tid_dest;
313 * Is the speed on the tunnel limited to the slowest peer?
318 * Is the tunnel bufferless (minimum latency)?
323 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
328 * Packet ID of the last bck packet sent (unique counter per hop).
333 * SKIP value for this tunnel.
338 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
339 * Contains the Flow Control info: FWD ACK value received,
340 * last BCK ACK sent, PID and SKIP values.
342 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
345 * Last ACK sent towards the origin (for traffic towards leaf node).
347 uint32_t last_fwd_ack;
350 * BCK ACK value received from the hop towards the owner of the tunnel,
351 * (previous node / owner): up to what message PID can we sent back to him.
356 * How many messages are in the forward queue (towards leaves).
358 unsigned int fwd_queue_n;
361 * How many messages do we accept in the forward queue.
363 unsigned int fwd_queue_max;
366 * How many messages are in the backward queue (towards origin).
368 unsigned int bck_queue_n;
371 * How many messages do we accept in the backward queue.
373 unsigned int bck_queue_max;
376 * Last time the tunnel was used
378 struct GNUNET_TIME_Absolute timestamp;
381 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
382 * containing peers added by id or by type, not intermediate peers.
384 struct GNUNET_CONTAINER_MultiHashMap *peers;
387 * Number of peers that are connected and potentially ready to receive data
389 unsigned int peers_ready;
392 * Number of peers that have been added to the tunnel
394 unsigned int peers_total;
397 * Client owner of the tunnel, if any
399 struct MeshClient *owner;
402 * Clients that have been informed about and want to stay in the tunnel.
404 struct MeshClient **clients;
407 * Flow control info for each client.
409 struct MeshTunnelClientInfo *clients_fc;
412 * Number of elements in clients/clients_fc
414 unsigned int nclients;
417 * Clients that have been informed but requested to leave the tunnel.
419 struct MeshClient **ignore;
422 * Number of elements in clients
424 unsigned int nignore;
429 GNUNET_PEER_Id *blacklisted;
432 * Number of elements in blacklisted
434 unsigned int nblacklisted;
437 * Bloomfilter (for peer identities) to stop circular routes
439 char bloomfilter[MESH_BLOOM_SIZE];
444 struct MeshTunnelTree *tree;
447 * Application type we are looking for in this tunnel
449 GNUNET_MESH_ApplicationType type;
452 * Used to search peers offering a service
454 struct GNUNET_DHT_GetHandle *dht_get_type;
457 * Initial context of the regex search for a connect_by_string
459 struct MeshRegexSearchContext *regex_ctx;
462 * Task to keep the used paths alive
464 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
467 * Task to destroy the tunnel after timeout
469 * FIXME: merge the two? a tunnel will have either
470 * a path refresh OR a timeout, never both!
472 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
475 * Flag to signal the destruction of the tunnel.
476 * If this is set GNUNET_YES the tunnel will be destroyed
477 * when the queue is empty.
484 * Info about a child node in a tunnel, needed to perform flow control.
486 struct MeshTunnelChildInfo
489 * ID of the child node.
504 * Maximum PID allowed (FWD ACK received).
509 * Last ACK sent to that child (BCK ACK).
516 * Info about a leaf client of a tunnel, needed to perform flow control.
518 struct MeshTunnelClientInfo
521 * PID of the last packet sent to the client (FWD).
526 * PID of the last packet received from the client (BCK).
531 * Maximum PID allowed (FWD ACK received).
536 * Last ACK sent to that child (BCK ACK).
544 * Info collected during iteration of child nodes in order to get the ACK value
547 struct MeshTunnelChildIteratorContext
550 * Tunnel whose info is being collected.
552 struct MeshTunnel *t;
555 * Is this context initialized? Is the value in max_child_ack valid?
560 * Maximum child ACK so far.
562 uint32_t max_child_ack;
565 * Number of children nodes
567 unsigned int nchildren;
572 * Info needed to work with tunnel paths and peers
579 struct MeshTunnel *t;
582 * Neighbouring peer to whom we send the packet to
584 struct MeshPeerInfo *peer;
589 struct MeshPeerPath *path;
594 * Struct containing information about a client of the service
601 struct MeshClient *next;
606 struct MeshClient *prev;
609 * Tunnels that belong to this client, indexed by local id
611 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
614 * Tunnels this client has accepted, indexed by incoming local id
616 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
619 * Tunnels this client has rejected, indexed by incoming local id
621 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
623 * Handle to communicate with the client
625 struct GNUNET_SERVER_Client *handle;
628 * Applications that this client has claimed to provide
630 struct GNUNET_CONTAINER_MultiHashMap *apps;
633 * Messages that this client has declared interest in
635 struct GNUNET_CONTAINER_MultiHashMap *types;
638 * Whether the client is active or shutting down (don't send confirmations
639 * to a client that is shutting down.
644 * ID of the client, mainly for debug messages
649 * Regular expressions describing the services offered by this client.
651 char **regexes; // FIXME add timeout? API to remove a regex?
654 * Number of regular expressions in regexes.
656 unsigned int n_regex;
659 * Task to refresh all regular expresions in the DHT.
661 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
667 * Struct to keep information of searches of services described by a regex
668 * using a user-provided string service description.
670 struct MeshRegexSearchInfo
673 * Which tunnel is this for
675 struct MeshTunnel *t;
678 * User provided description of the searched service.
683 * Part of the description already consumed by the search.
690 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
693 * Results from running DHT GETs.
695 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
698 * Contexts, for each running DHT GET. Free all on end of search.
700 struct MeshRegexSearchContext **contexts;
703 * Number of contexts (branches/steps in search).
705 unsigned int n_contexts;
708 * Peer that is connecting via connect_by_string. When connected, free ctx.
713 * Other peers that are found but not yet being connected to.
715 GNUNET_PEER_Id *peers;
718 * Number of elements in peers.
720 unsigned int n_peers;
723 * Next peer to try to connect to.
728 * Timeout for a connect attempt.
729 * When reached, try to connect to a different peer, if any. If not,
730 * try the same peer again.
732 GNUNET_SCHEDULER_TaskIdentifier timeout;
737 * Struct to keep state of running searches that have consumed a part of
740 struct MeshRegexSearchContext
743 * Part of the description already consumed by
744 * this particular search branch.
749 * Information about the search.
751 struct MeshRegexSearchInfo *info;
755 /******************************************************************************/
756 /************************ DEBUG FUNCTIONS ****************************/
757 /******************************************************************************/
761 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
762 * @param cls string to print
763 * @param success GNUNET_OK if the PUT was transmitted,
764 * GNUNET_NO on timeout,
765 * GNUNET_SYSERR on disconnect from service
766 * after the PUT message was transmitted
767 * (so we don't know if it was received or not)
772 mesh_debug (void *cls, int success)
776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
782 /******************************************************************************/
783 /*********************** GLOBAL VARIABLES ****************************/
784 /******************************************************************************/
787 * Configuration parameters
789 static struct GNUNET_TIME_Relative refresh_path_time;
790 static struct GNUNET_TIME_Relative app_announce_time;
791 static struct GNUNET_TIME_Relative id_announce_time;
792 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
793 static struct GNUNET_TIME_Relative connect_timeout;
794 static long long unsigned int default_ttl;
795 static long long unsigned int dht_replication_level;
796 static long long unsigned int max_tunnels;
797 static long long unsigned int max_msgs_queue;
800 * DLL with all the clients, head.
802 static struct MeshClient *clients;
805 * DLL with all the clients, tail.
807 static struct MeshClient *clients_tail;
810 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
812 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
815 * Number of tunnels known.
817 static unsigned long long n_tunnels;
820 * Tunnels incoming, indexed by MESH_TunnelNumber
821 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
823 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
826 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
828 static struct GNUNET_CONTAINER_MultiHashMap *peers;
831 * Handle to communicate with transport
833 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
836 * Handle to communicate with core.
838 static struct GNUNET_CORE_Handle *core_handle;
843 static struct GNUNET_DHT_Handle *dht_handle;
848 static struct GNUNET_SERVER_Handle *server_handle;
851 * Handle to the statistics service.
853 static struct GNUNET_STATISTICS_Handle *stats;
856 * Notification context, to send messages to local clients.
858 static struct GNUNET_SERVER_NotificationContext *nc;
861 * Local peer own ID (memory efficient handle).
863 static GNUNET_PEER_Id myid;
866 * Local peer own ID (full value).
868 static struct GNUNET_PeerIdentity my_full_id;
873 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
878 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
881 * Tunnel ID for the next created tunnel (global tunnel number).
883 static MESH_TunnelNumber next_tid;
886 * Tunnel ID for the next incoming tunnel (local tunnel number).
888 static MESH_TunnelNumber next_local_tid;
891 * All application types provided by this peer.
893 static struct GNUNET_CONTAINER_MultiHashMap *applications;
896 * All message types clients of this peer are interested in.
898 static struct GNUNET_CONTAINER_MultiHashMap *types;
901 * Task to periodically announce provided applications.
903 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
906 * Task to periodically announce itself in the network.
908 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
911 * Next ID to assign to a client.
913 unsigned int next_client_id;
916 /******************************************************************************/
917 /*********************** DECLARATIONS **************************/
918 /******************************************************************************/
920 /* FIXME move declarations here */
923 * Function to process paths received for a new peer addition. The recorded
924 * paths form the initial tunnel, which can be optimized later.
925 * Called on each result obtained for the DHT search.
928 * @param exp when will this value expire
929 * @param key key of the result
930 * @param type type of the result
931 * @param size number of bytes in data
932 * @param data pointer to the result data
935 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
936 const struct GNUNET_HashCode * key,
937 const struct GNUNET_PeerIdentity *get_path,
938 unsigned int get_path_length,
939 const struct GNUNET_PeerIdentity *put_path,
940 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
941 size_t size, const void *data);
945 * Function to process DHT string to regex matching.
946 * Called on each result obtained for the DHT search.
948 * @param cls closure (search context)
949 * @param exp when will this value expire
950 * @param key key of the result
951 * @param get_path path of the get request (not used)
952 * @param get_path_length lenght of get_path (not used)
953 * @param put_path path of the put request (not used)
954 * @param put_path_length length of the put_path (not used)
955 * @param type type of the result
956 * @param size number of bytes in data
957 * @param data pointer to the result data
959 * TODO: re-issue the request after certain time? cancel after X results?
962 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
963 const struct GNUNET_HashCode * key,
964 const struct GNUNET_PeerIdentity *get_path,
965 unsigned int get_path_length,
966 const struct GNUNET_PeerIdentity *put_path,
967 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
968 size_t size, const void *data);
972 * Function to process DHT string to regex matching.
973 * Called on each result obtained for the DHT search.
975 * @param cls closure (search context)
976 * @param exp when will this value expire
977 * @param key key of the result
978 * @param get_path path of the get request (not used)
979 * @param get_path_length lenght of get_path (not used)
980 * @param put_path path of the put request (not used)
981 * @param put_path_length length of the put_path (not used)
982 * @param type type of the result
983 * @param size number of bytes in data
984 * @param data pointer to the result data
987 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
988 const struct GNUNET_HashCode * key,
989 const struct GNUNET_PeerIdentity *get_path,
990 unsigned int get_path_length,
991 const struct GNUNET_PeerIdentity *put_path,
992 unsigned int put_path_length,
993 enum GNUNET_BLOCK_Type type,
994 size_t size, const void *data);
998 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
999 * and insert it in the appropiate structures if the peer is not known yet.
1001 * @param peer Short identity of the peer.
1003 * @return Existing or newly created peer info.
1005 static struct MeshPeerInfo *
1006 peer_info_get_short (const GNUNET_PEER_Id peer);
1010 * Try to establish a new connection to this peer.
1011 * Use the best path for the given tunnel.
1012 * If the peer doesn't have any path to it yet, try to get one.
1013 * If the peer already has some path, send a CREATE PATH towards it.
1015 * @param peer PeerInfo of the peer.
1016 * @param t Tunnel for which to create the path, if possible.
1019 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1023 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1024 * needed rescources.
1025 * If peer already exists, reevaluate shortest path and change if different.
1027 * @param t Tunnel we want to add a new peer to
1028 * @param peer PeerInfo of the peer being added
1032 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1036 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1037 * that are no longer needed, as well as nodes of no longer reachable peers.
1038 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1040 * @param t Tunnel from which to remove the path.
1041 * @param peer Short id of the peer which should be removed.
1044 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1048 * Search for a tunnel by global ID using full PeerIdentities.
1050 * @param oid owner of the tunnel.
1051 * @param tid global tunnel number.
1053 * @return tunnel handler, NULL if doesn't exist.
1055 static struct MeshTunnel *
1056 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1060 * Delete an active client from the tunnel.
1066 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1069 * Notify a tunnel that a connection has broken that affects at least
1070 * some of its peers.
1072 * @param t Tunnel affected.
1073 * @param p1 Peer that got disconnected from p2.
1074 * @param p2 Peer that got disconnected from p1.
1076 * @return Short ID of the peer disconnected (either p1 or p2).
1077 * 0 if the tunnel remained unaffected.
1079 static GNUNET_PEER_Id
1080 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1085 * Get the current ack value for a tunnel, for data going from root to leaves,
1086 * taking in account the tunnel mode and the status of all children and clients.
1090 * @return Maximum PID allowed.
1093 tunnel_get_fwd_ack (struct MeshTunnel *t);
1097 * Add a client to a tunnel, initializing all needed data structures.
1099 * @param t Tunnel to which add the client.
1100 * @param c Client which to add to the tunnel.
1103 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1107 * Iterator over edges in a regex block retrieved from the DHT.
1109 * @param cls Closure.
1110 * @param token Token that follows to next state.
1111 * @param len Lenght of token.
1112 * @param key Hash of next state.
1114 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1117 regex_edge_iterator (void *cls,
1120 const struct GNUNET_HashCode *key);
1124 * Find a path to a peer that offers a regex servcie compatible
1125 * with a given string.
1127 * @param key The key of the accepting state.
1128 * @param ctx Context containing info about the string, tunnel, etc.
1131 regex_find_path (const struct GNUNET_HashCode *key,
1132 struct MeshRegexSearchContext *ctx);
1136 * Queue and pass message to core when possible.
1138 * @param cls Closure (type dependant).
1139 * @param type Type of the message, 0 for a raw message.
1140 * @param size Size of the message.
1141 * @param dst Neighbor to send message to.
1142 * @param t Tunnel this message belongs to.
1145 queue_add (void *cls, uint16_t type, size_t size,
1146 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1149 * Free a transmission that was already queued with all resources
1150 * associated to the request.
1152 * @param queue Queue handler to cancel.
1153 * @param clear_cls Is it necessary to free associated cls?
1156 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1158 /******************************************************************************/
1159 /************************ ITERATORS ****************************/
1160 /******************************************************************************/
1163 * Iterator over found existing mesh regex blocks that match an ongoing search.
1165 * @param cls closure
1166 * @param key current key code
1167 * @param value value in the hash map
1168 * @return GNUNET_YES if we should continue to iterate,
1172 regex_result_iterator (void *cls,
1173 const struct GNUNET_HashCode * key,
1176 struct MeshRegexBlock *block = value;
1177 struct MeshRegexSearchContext *ctx = cls;
1179 if (GNUNET_YES == ntohl(block->accepting) &&
1180 ctx->position == strlen (ctx->info->description))
1182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1183 regex_find_path (key, ctx);
1184 return GNUNET_YES; // We found an accept state!
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1189 ctx->position, strlen(ctx->info->description),
1190 ntohl(block->accepting));
1193 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1194 ®ex_edge_iterator, ctx);
1201 * Iterator over edges in a regex block retrieved from the DHT.
1203 * @param cls Closure (context of the search).
1204 * @param token Token that follows to next state.
1205 * @param len Lenght of token.
1206 * @param key Hash of next state.
1208 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1211 regex_edge_iterator (void *cls,
1214 const struct GNUNET_HashCode *key)
1216 struct MeshRegexSearchContext *ctx = cls;
1217 struct MeshRegexSearchContext *new_ctx;
1218 struct MeshRegexSearchInfo *info = ctx->info;
1219 struct GNUNET_DHT_GetHandle *get_h;
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1226 current = &info->description[ctx->position];
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1228 current_len = strlen (info->description) - ctx->position;
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1233 if (len > current_len)
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1236 return GNUNET_YES; // Token too long, wont match
1238 if (0 != strncmp (current, token, len))
1240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1241 return GNUNET_YES; // Token doesn't match
1243 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1244 new_ctx->info = info;
1245 new_ctx->position = ctx->position + len;
1246 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1248 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1251 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1252 ®ex_result_iterator,
1254 return GNUNET_YES; // We are already looking for it
1256 /* Start search in DHT */
1258 GNUNET_DHT_get_start (dht_handle, /* handle */
1259 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1260 key, /* key to search */
1261 dht_replication_level, /* replication level */
1262 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1263 NULL, /* xquery */ // FIXME BLOOMFILTER
1264 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1265 &dht_get_string_handler, new_ctx);
1267 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1268 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1278 * Iterator over hash map entries to cancel DHT GET requests after a
1279 * successful connect_by_string.
1281 * @param cls Closure (unused).
1282 * @param key Current key code (unused).
1283 * @param value Value in the hash map (get handle).
1284 * @return GNUNET_YES if we should continue to iterate,
1288 regex_cancel_dht_get (void *cls,
1289 const struct GNUNET_HashCode * key,
1292 struct GNUNET_DHT_GetHandle *h = value;
1294 GNUNET_DHT_get_stop (h);
1300 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1301 * search for connect_by_string.
1303 * @param cls Closure (unused).
1304 * @param key Current key code (unused).
1305 * @param value MeshRegexBlock in the hash map.
1306 * @return GNUNET_YES if we should continue to iterate,
1310 regex_free_result (void *cls,
1311 const struct GNUNET_HashCode * key,
1315 GNUNET_free (value);
1321 * Regex callback iterator to store own service description in the DHT.
1323 * @param cls closure.
1324 * @param key hash for current state.
1325 * @param proof proof for current state.
1326 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1327 * @param num_edges number of edges leaving current state.
1328 * @param edges edges leaving current state.
1331 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1332 int accepting, unsigned int num_edges,
1333 const struct GNUNET_REGEX_Edge *edges)
1335 struct MeshRegexBlock *block;
1336 struct MeshRegexEdge *block_edge;
1337 enum GNUNET_DHT_RouteOption opt;
1341 unsigned int offset;
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1345 " regex dht put for state %s\n",
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1355 if (GNUNET_YES == accepting)
1357 struct MeshRegexAccept block;
1359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1360 " state %s is accepting, putting own id\n",
1362 size = sizeof (block);
1364 block.id = my_full_id;
1366 GNUNET_DHT_put(dht_handle, key,
1367 dht_replication_level,
1368 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1369 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1372 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1377 len = strlen(proof);
1378 size = sizeof (struct MeshRegexBlock) + len;
1379 block = GNUNET_malloc (size);
1382 block->n_proof = htonl (len);
1383 block->n_edges = htonl (num_edges);
1384 block->accepting = htonl (accepting);
1386 /* Store the proof at the end of the block. */
1387 aux = (char *) &block[1];
1388 memcpy (aux, proof, len);
1391 /* Store each edge in a variable length MeshEdge struct at the
1392 * very end of the MeshRegexBlock structure.
1394 for (i = 0; i < num_edges; i++)
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 " edge %s towards %s\n",
1399 GNUNET_h2s(&edges[i].destination));
1401 /* aux points at the end of the last block */
1402 len = strlen (edges[i].label);
1403 size += sizeof (struct MeshRegexEdge) + len;
1404 // Calculate offset FIXME is this ok? use size instead?
1405 offset = aux - (char *) block;
1406 block = GNUNET_realloc (block, size);
1407 aux = &((char *) block)[offset];
1408 block_edge = (struct MeshRegexEdge *) aux;
1409 block_edge->key = edges[i].destination;
1410 block_edge->n_token = htonl (len);
1411 aux = (char *) &block_edge[1];
1412 memcpy (aux, edges[i].label, len);
1416 GNUNET_DHT_put(dht_handle, key,
1417 dht_replication_level,
1419 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1421 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1425 GNUNET_free (block);
1430 * Store the regular expression describing a local service into the DHT.
1432 * @param regex The regular expresion.
1435 regex_put (const char *regex)
1437 struct GNUNET_REGEX_Automaton *dfa;
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1440 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1441 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1442 GNUNET_REGEX_automaton_destroy (dfa);
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1448 * Find a path to a peer that offers a regex servcie compatible
1449 * with a given string.
1451 * @param key The key of the accepting state.
1452 * @param ctx Context containing info about the string, tunnel, etc.
1455 regex_find_path (const struct GNUNET_HashCode *key,
1456 struct MeshRegexSearchContext *ctx)
1458 struct GNUNET_DHT_GetHandle *get_h;
1460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1461 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1462 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1463 key, /* key to search */
1464 dht_replication_level, /* replication level */
1465 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1466 GNUNET_DHT_RO_RECORD_ROUTE,
1467 NULL, /* xquery */ // FIXME BLOOMFILTER
1468 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1469 &dht_get_string_accept_handler, ctx);
1470 GNUNET_break (GNUNET_OK ==
1471 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1474 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1479 * Function called if the connect attempt to a peer found via
1480 * connect_by_string times out. Try to connect to another peer, if any.
1481 * Otherwise try to reconnect to the same peer.
1483 * @param cls Closure (info about regex search).
1484 * @param tc TaskContext.
1487 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1489 struct MeshRegexSearchInfo *info = cls;
1490 struct MeshPeerInfo *peer_info;
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1495 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1496 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1504 if (0 < info->n_peers)
1506 // Select next peer, put current in that spot.
1507 id = info->peers[info->i_peer];
1508 info->peers[info->i_peer] = info->peer;
1509 info->i_peer = (info->i_peer + 1) % info->n_peers;
1513 // Try to connect to same peer again.
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1518 peer_info = peer_info_get_short(id);
1519 tunnel_add_peer (info->t, peer_info);
1521 tunnel_delete_peer (info->t, old);
1522 peer_info_connect (peer_info, info->t);
1523 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1524 ®ex_connect_timeout,
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1531 * Cancel an ongoing regex search in the DHT and free all resources.
1533 * @param ctx The search context.
1536 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1538 struct MeshRegexSearchInfo *info = ctx->info;
1541 GNUNET_free (info->description);
1542 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1543 ®ex_cancel_dht_get, NULL);
1544 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1545 ®ex_free_result, NULL);
1546 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1547 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1548 info->t->regex_ctx = NULL;
1549 for (i = 0; i < info->n_contexts; i++)
1551 GNUNET_free (info->contexts[i]);
1553 if (0 < info->n_contexts)
1554 GNUNET_free (info->contexts);
1555 if (0 < info->n_peers)
1556 GNUNET_free (info->peers);
1557 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1559 GNUNET_SCHEDULER_cancel(info->timeout);
1565 /******************************************************************************/
1566 /************************ PERIODIC FUNCTIONS ****************************/
1567 /******************************************************************************/
1570 * Announce iterator over for each application provided by the peer
1572 * @param cls closure
1573 * @param key current key code
1574 * @param value value in the hash map
1575 * @return GNUNET_YES if we should continue to
1580 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1582 struct PBlock block;
1583 struct MeshClient *c;
1585 block.id = my_full_id;
1586 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1587 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1588 if (0 == block.type)
1593 block.type = htonl (block.type);
1595 GNUNET_break (NULL !=
1596 GNUNET_DHT_put (dht_handle, key,
1597 dht_replication_level,
1598 GNUNET_DHT_RO_RECORD_ROUTE |
1599 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1600 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1602 (const char *) &block,
1603 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1605 app_announce_time, NULL, NULL));
1611 * Periodically announce what applications are provided by local clients
1614 * @param cls closure
1615 * @param tc task context
1618 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1620 struct MeshClient *c = cls;
1623 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1625 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1629 DEBUG_DHT ("Starting PUT for regex\n");
1631 for (i = 0; i < c->n_regex; i++)
1633 regex_put (c->regexes[i]);
1635 c->regex_announce_task =
1636 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1637 DEBUG_DHT ("Finished PUT for regex\n");
1644 * Periodically announce what applications are provided by local clients
1647 * @param cls closure
1648 * @param tc task context
1651 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1653 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1655 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1659 DEBUG_DHT ("Starting PUT for apps\n");
1661 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1663 announce_applications_task =
1664 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1666 DEBUG_DHT ("Finished PUT for apps\n");
1673 * Periodically announce self id in the DHT
1675 * @param cls closure
1676 * @param tc task context
1679 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1681 struct PBlock block;
1683 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1685 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1689 * - Set data expiration in function of X
1690 * - Adapt X to churn
1692 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1694 block.id = my_full_id;
1695 block.type = htonl (0);
1696 GNUNET_DHT_put (dht_handle, /* DHT handle */
1697 &my_full_id.hashPubKey, /* Key to use */
1698 dht_replication_level, /* Replication level */
1699 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1700 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1701 sizeof (block), /* Size of the data */
1702 (const char *) &block, /* Data itself */
1703 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1704 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1705 NULL, /* Continuation */
1706 NULL); /* Continuation closure */
1708 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1712 /******************************************************************************/
1713 /****************** GENERAL HELPER FUNCTIONS ************************/
1714 /******************************************************************************/
1717 * Decrements the reference counter and frees all resources if needed
1719 * @param mesh_data Data Descriptor used in a multicast message.
1720 * Freed no longer needed (last message).
1723 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1725 /* Make sure it's a multicast packet */
1726 GNUNET_assert (NULL != mesh_data->reference_counter);
1728 if (0 == --(*(mesh_data->reference_counter)))
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1731 if (NULL != mesh_data->task)
1733 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1735 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1737 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1739 GNUNET_free (mesh_data->task);
1741 GNUNET_free (mesh_data->reference_counter);
1742 GNUNET_free (mesh_data->data);
1743 GNUNET_free (mesh_data);
1749 * Check if client has registered with the service and has not disconnected
1751 * @param client the client to check
1753 * @return non-NULL if client exists in the global DLL
1755 static struct MeshClient *
1756 client_get (struct GNUNET_SERVER_Client *client)
1758 struct MeshClient *c;
1763 if (c->handle == client)
1772 * Checks if a given client has subscribed to certain message type
1774 * @param message_type Type of message to check
1775 * @param c Client to check
1777 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1779 * FIXME: use of crypto_hash slows it down
1780 * The hash function alone takes 8-10us out of the ~55us for the whole
1781 * process of retransmitting the message from one local client to another.
1782 * Find faster implementation!
1785 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1787 struct GNUNET_HashCode hc;
1789 if (NULL == c->types)
1792 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1793 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1798 * Allow a client to send more data after transmitting a multicast message
1799 * which some neighbor has not yet accepted altough a reasonable time has
1802 * @param cls Closure (DataDescriptor containing the task identifier)
1803 * @param tc Task Context
1805 * FIXME reference counter cshould be just int
1808 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1810 struct MeshData *mdata = cls;
1812 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1814 GNUNET_assert (NULL != mdata->reference_counter);
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1816 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1817 *(mdata->reference_counter));
1818 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1819 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1824 * Check whether client wants traffic from a tunnel.
1826 * @param c Client to check.
1827 * @param t Tunnel to be found.
1829 * @return GNUNET_YES if client knows tunnel.
1831 * TODO look in client hashmap
1834 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1838 for (i = 0; i < t->nclients; i++)
1839 if (t->clients[i] == c)
1846 * Check whether client has been informed about a tunnel.
1848 * @param c Client to check.
1849 * @param t Tunnel to be found.
1851 * @return GNUNET_YES if client knows tunnel.
1853 * TODO look in client hashmap
1856 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1860 for (i = 0; i < t->nignore; i++)
1861 if (t->ignore[i] == c)
1863 return client_wants_tunnel(c, t);
1868 * Marks a client as uninterested in traffic from the tunnel, updating both
1869 * client and tunnel to reflect this.
1871 * @param c Client that doesn't want traffic anymore.
1872 * @param t Tunnel which should be ignored.
1874 * FIXME when to delete an incoming tunnel?
1877 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1879 struct GNUNET_HashCode hash;
1881 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1882 GNUNET_break (GNUNET_YES ==
1883 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1885 GNUNET_break (GNUNET_YES ==
1886 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1887 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1888 tunnel_delete_active_client (t, c);
1889 GNUNET_array_append (t->ignore, t->nignore, c);
1894 * Deletes a tunnel from a client (either owner or destination). To be used on
1895 * tunnel destroy, otherwise, use client_ignore_tunnel.
1897 * @param c Client whose tunnel to delete.
1898 * @param t Tunnel which should be deleted.
1901 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1903 struct GNUNET_HashCode hash;
1907 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1908 GNUNET_assert (GNUNET_YES ==
1909 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1915 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1917 GNUNET_assert (GNUNET_YES ==
1918 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1922 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1930 * Send the message to all clients that have subscribed to its type
1932 * @param msg Pointer to the message itself
1933 * @param payload Pointer to the payload of the message.
1934 * @param t The tunnel to whose clients this message goes.
1936 * @return number of clients this message was sent to
1939 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1940 const struct GNUNET_MessageHeader *payload,
1941 struct MeshTunnel *t)
1943 struct MeshClient *c;
1944 MESH_TunnelNumber *tid;
1947 char cbuf[htons (msg->size)];
1949 type = ntohs (payload->type);
1950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1952 GNUNET_MESH_DEBUG_M2S (type));
1954 memcpy (cbuf, msg, sizeof (cbuf));
1955 switch (htons (msg->type))
1957 struct GNUNET_MESH_Unicast *uc;
1958 struct GNUNET_MESH_Multicast *mc;
1959 struct GNUNET_MESH_ToOrigin *to;
1961 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1962 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1965 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1966 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1969 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1970 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1978 for (count = 0, c = clients; c != NULL; c = c->next)
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1981 if (client_is_subscribed (type, c))
1983 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1987 *tid = htonl (t->local_tid);
1991 if (GNUNET_NO == client_knows_tunnel (c, t))
1993 /* This client doesn't know the tunnel */
1994 struct GNUNET_MESH_TunnelNotification tmsg;
1995 struct GNUNET_HashCode hash;
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1998 tmsg.header.size = htons (sizeof (tmsg));
1999 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2000 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2001 tmsg.tunnel_id = htonl (t->local_tid_dest);
2002 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2003 &tmsg.header, GNUNET_NO);
2004 tunnel_add_client (t, c);
2005 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2007 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2008 c->incoming_tunnels, &hash, t,
2009 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2011 *tid = htonl (t->local_tid_dest);
2014 /* Check if the client wants to get traffic from the tunnel */
2015 if (GNUNET_NO == client_wants_tunnel(c, t))
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2019 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2020 (struct GNUNET_MessageHeader
2021 *) cbuf, GNUNET_NO);
2030 * Notify the client that owns the tunnel that a peer has connected to it
2031 * (the requested path to it has been confirmed).
2033 * @param t Tunnel whose owner to notify
2034 * @param id Short id of the peer that has connected
2037 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2039 struct GNUNET_MESH_PeerControl pc;
2041 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2042 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2043 pc.tunnel_id = htonl (t->local_tid);
2044 GNUNET_PEER_resolve (id, &pc.peer);
2045 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2051 * Notify all clients (not depending on registration status) that the incoming
2052 * tunnel is no longer valid.
2054 * @param t Tunnel that was destroyed.
2057 send_clients_tunnel_destroy (struct MeshTunnel *t)
2059 struct GNUNET_MESH_TunnelMessage msg;
2061 msg.header.size = htons (sizeof (msg));
2062 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2063 msg.tunnel_id = htonl (t->local_tid_dest);
2064 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2069 * Notify clients of tunnel disconnections, if needed.
2070 * In case the origin disconnects, the destination clients get a tunnel destroy
2071 * notification. If the last destination disconnects (only one remaining client
2072 * in tunnel), the origin gets a (local ID) peer disconnected.
2073 * Note that the function must be called BEFORE removing the client from
2076 * @param t Tunnel that was destroyed.
2077 * @param c Client that disconnected.
2080 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2086 struct GNUNET_MESH_TunnelMessage msg;
2088 msg.header.size = htons (sizeof (msg));
2089 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2090 msg.tunnel_id = htonl (t->local_tid_dest);
2091 for (i = 0; i < t->nclients; i++)
2092 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2093 &msg.header, GNUNET_NO);
2095 // FIXME when to disconnect an incoming tunnel?
2096 else if (1 == t->nclients && NULL != t->owner)
2098 struct GNUNET_MESH_PeerControl msg;
2100 msg.header.size = htons (sizeof (msg));
2101 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2102 msg.tunnel_id = htonl (t->local_tid);
2103 msg.peer = my_full_id;
2104 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2105 &msg.header, GNUNET_NO);
2111 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2112 * and insert it in the appropiate structures if the peer is not known yet.
2114 * @param peer Full identity of the peer.
2116 * @return Existing or newly created peer info.
2118 static struct MeshPeerInfo *
2119 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2121 struct MeshPeerInfo *peer_info;
2123 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2124 if (NULL == peer_info)
2127 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2128 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2129 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2130 peer_info->id = GNUNET_PEER_intern (peer);
2138 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2139 * and insert it in the appropiate structures if the peer is not known yet.
2141 * @param peer Short identity of the peer.
2143 * @return Existing or newly created peer info.
2145 static struct MeshPeerInfo *
2146 peer_info_get_short (const GNUNET_PEER_Id peer)
2148 struct GNUNET_PeerIdentity id;
2150 GNUNET_PEER_resolve (peer, &id);
2151 return peer_info_get (&id);
2156 * Iterator to remove the tunnel from the list of tunnels a peer participates
2159 * @param cls Closure (tunnel info)
2160 * @param key GNUNET_PeerIdentity of the peer (unused)
2161 * @param value PeerInfo of the peer
2163 * @return always GNUNET_YES, to keep iterating
2166 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2168 struct MeshTunnel *t = cls;
2169 struct MeshPeerInfo *peer = value;
2172 for (i = 0; i < peer->ntunnels; i++)
2175 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2178 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2179 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2188 * Core callback to write a pre-constructed data packet to core buffer
2190 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2191 * @param size Number of bytes available in buf.
2192 * @param buf Where the to write the message.
2194 * @return number of bytes written to buf
2197 send_core_data_raw (void *cls, size_t size, void *buf)
2199 struct MeshTransmissionDescriptor *info = cls;
2200 struct GNUNET_MessageHeader *msg;
2203 GNUNET_assert (NULL != info);
2204 GNUNET_assert (NULL != info->mesh_data);
2205 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2206 total_size = ntohs (msg->size);
2208 if (total_size > size)
2213 memcpy (buf, msg, total_size);
2214 data_descriptor_decrement_rc (info->mesh_data);
2221 * Sends an already built non-multicast message to a peer,
2222 * properly registrating all used resources.
2224 * @param message Message to send. Function makes a copy of it.
2225 * @param peer Short ID of the neighbor whom to send the message.
2226 * @param t Tunnel on which this message is transmitted.
2229 send_message (const struct GNUNET_MessageHeader *message,
2230 const struct GNUNET_PeerIdentity *peer,
2231 struct MeshTunnel *t)
2233 struct MeshTransmissionDescriptor *info;
2234 struct MeshPeerInfo *neighbor;
2235 struct MeshPeerPath *p;
2239 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2241 size = ntohs (message->size);
2242 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2243 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2244 info->mesh_data->data = GNUNET_malloc (size);
2245 memcpy (info->mesh_data->data, message, size);
2246 type = ntohs(message->type);
2247 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2249 struct GNUNET_MESH_Unicast *m;
2251 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2252 m->ttl = htonl (ntohl (m->ttl) - 1);
2254 info->mesh_data->data_len = size;
2255 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2256 *info->mesh_data->reference_counter = 1;
2257 neighbor = peer_info_get (peer);
2258 for (p = neighbor->path_head; NULL != p; p = p->next)
2268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2269 " %s IS NOT DIRECTLY CONNECTED\n",
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2274 for (p = neighbor->path_head; NULL != p; p = p->next)
2276 struct GNUNET_PeerIdentity debug_id;
2279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2280 " path with %u hops through:\n",
2282 for (i = 0; i < p->length; i++)
2284 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2287 i, GNUNET_i2s(&debug_id));
2291 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2292 GNUNET_free (info->mesh_data->data);
2293 GNUNET_free (info->mesh_data);
2297 info->peer = neighbor;
2298 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2301 * TODO: in this case we only need the service to retransmit
2302 * the message down the path. If we pass the real type to queue_add,
2303 * queue_send will try to build the message from scratch. This can
2304 * probably be done by some other way instead of deleteing the type
2318 * Sends a CREATE PATH message for a path to a peer, properly registrating
2319 * all used resources.
2321 * @param peer PeerInfo of the final peer for whom this path is being created.
2322 * @param p Path itself.
2323 * @param t Tunnel for which the path is created.
2326 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2327 struct MeshTunnel *t)
2329 struct GNUNET_PeerIdentity id;
2330 struct MeshPathInfo *path_info;
2331 struct MeshPeerInfo *neighbor;
2337 p = tree_get_path_to_peer (t->tree, peer->id);
2344 for (i = 0; i < p->length; i++)
2346 if (p->peers[i] == myid)
2349 if (i >= p->length - 1)
2355 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2357 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2358 path_info->path = p;
2360 neighbor = peer_info_get (&id);
2361 path_info->peer = neighbor;
2362 queue_add (path_info,
2363 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2364 sizeof (struct GNUNET_MESH_ManipulatePath) +
2365 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2372 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2374 * @param t Tunnel whose path to destroy.
2375 * @param destination Short ID of the peer to whom the path to destroy.
2378 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2380 struct MeshPeerPath *p;
2383 p = tree_get_path_to_peer (t->tree, destination);
2389 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2390 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2392 struct GNUNET_MESH_ManipulatePath *msg;
2393 struct GNUNET_PeerIdentity *pi;
2397 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2398 msg->header.size = htons (size);
2399 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2400 msg->tid = htonl (t->id.tid);
2401 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2402 for (i = 0; i < p->length; i++)
2404 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2406 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2413 * Try to establish a new connection to this peer.
2414 * Use the best path for the given tunnel.
2415 * If the peer doesn't have any path to it yet, try to get one.
2416 * If the peer already has some path, send a CREATE PATH towards it.
2418 * @param peer PeerInfo of the peer.
2419 * @param t Tunnel for which to create the path, if possible.
2422 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2424 struct MeshPeerPath *p;
2425 struct MeshPathInfo *path_info;
2427 if (NULL != peer->path_head)
2429 p = tree_get_path_to_peer (t->tree, peer->id);
2436 // FIXME always send create path to self
2439 send_create_path (peer, p, t);
2443 struct GNUNET_HashCode hash;
2446 send_client_peer_connected (t, myid);
2447 t->local_tid_dest = next_local_tid++;
2448 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2451 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2452 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2459 else if (NULL == peer->dhtget)
2461 struct GNUNET_PeerIdentity id;
2463 GNUNET_PEER_resolve (peer->id, &id);
2464 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2465 path_info->peer = peer;
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2469 peer->dhtgetcls = path_info;
2470 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2471 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2472 &id.hashPubKey, /* key to search */
2473 dht_replication_level, /* replication level */
2474 GNUNET_DHT_RO_RECORD_ROUTE |
2475 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2476 NULL, /* xquery */ // FIXME BLOOMFILTER
2477 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2478 &dht_get_id_handler, path_info);
2480 /* Otherwise, there is no path but the DHT get is already started. */
2485 * Task to delay the connection of a peer
2487 * @param cls Closure (path info with tunnel and peer to connect).
2488 * Will be free'd on exection.
2489 * @param tc TaskContext
2492 peer_info_connect_task (void *cls,
2493 const struct GNUNET_SCHEDULER_TaskContext *tc)
2495 struct MeshPathInfo *path_info = cls;
2497 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2502 peer_info_connect (path_info->peer, path_info->t);
2508 * Destroy the peer_info and free any allocated resources linked to it
2510 * @param pi The peer_info to destroy.
2512 * @return GNUNET_OK on success
2515 peer_info_destroy (struct MeshPeerInfo *pi)
2517 struct GNUNET_PeerIdentity id;
2518 struct MeshPeerPath *p;
2519 struct MeshPeerPath *nextp;
2521 GNUNET_PEER_resolve (pi->id, &id);
2522 GNUNET_PEER_change_rc (pi->id, -1);
2525 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2529 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2531 if (NULL != pi->dhtget)
2533 GNUNET_DHT_get_stop (pi->dhtget);
2534 GNUNET_free (pi->dhtgetcls);
2540 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2550 * Remove all paths that rely on a direct connection between p1 and p2
2551 * from the peer itself and notify all tunnels about it.
2553 * @param peer PeerInfo of affected peer.
2554 * @param p1 GNUNET_PEER_Id of one peer.
2555 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2558 * TODO: optimize (see below)
2561 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2564 struct MeshPeerPath *p;
2565 struct MeshPeerPath *aux;
2566 struct MeshPeerInfo *peer_d;
2568 unsigned int destroyed;
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2575 p = peer->path_head;
2579 for (i = 0; i < (p->length - 1); i++)
2581 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2582 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2584 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2595 for (i = 0; i < peer->ntunnels; i++)
2597 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2601 * Problem: one or more peers have been deleted from the tunnel tree.
2602 * We don't know who they are to try to add them again.
2603 * We need to try to find a new path for each of the disconnected peers.
2604 * Some of them might already have a path to reach them that does not
2605 * involve p1 and p2. Adding all anew might render in a better tree than
2606 * the trivial immediate fix.
2608 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2609 * its children will be reachable trough him.
2611 peer_d = peer_info_get_short (d);
2614 for (p = peer_d->path_head; NULL != p; p = p->next)
2616 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2624 /* No callback, as peer will be already disconnected and a connection
2625 * scheduled by tunnel_notify_connection_broken.
2627 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2631 peer_info_connect (peer_d, peer->tunnels[i]);
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2639 * Add the path to the peer and update the path used to reach it in case this
2642 * @param peer_info Destination peer to add the path to.
2643 * @param path New path to add. Last peer must be the peer in arg 1.
2644 * Path will be either used of freed if already known.
2645 * @param trusted Do we trust that this path is real?
2648 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2651 struct MeshPeerPath *aux;
2655 if ((NULL == peer_info) || (NULL == path))
2658 path_destroy (path);
2661 if (path->peers[path->length - 1] != peer_info->id)
2664 path_destroy (path);
2667 if (path->length <= 2 && GNUNET_NO == trusted)
2669 /* Only allow CORE to tell us about direct paths */
2670 path_destroy (path);
2673 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2674 for (l = 1; l < path->length; l++)
2676 if (path->peers[l] == myid)
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2679 for (l2 = 0; l2 < path->length - l; l2++)
2681 path->peers[l2] = path->peers[l + l2];
2686 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2691 struct GNUNET_PeerIdentity id;
2693 GNUNET_PEER_resolve (peer_info->id, &id);
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2695 path->length, GNUNET_i2s (&id));
2698 l = path_get_length (path);
2705 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2706 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2708 l2 = path_get_length (aux);
2711 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2712 peer_info->path_tail, aux, path);
2717 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2719 path_destroy (path);
2724 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2731 * Add the path to the origin peer and update the path used to reach it in case
2732 * this is the shortest.
2733 * The path is given in peer_info -> destination, therefore we turn the path
2734 * upside down first.
2736 * @param peer_info Peer to add the path to, being the origin of the path.
2737 * @param path New path to add after being inversed.
2738 * @param trusted Do we trust that this path is real?
2741 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2742 struct MeshPeerPath *path, int trusted)
2745 peer_info_add_path (peer_info, path, trusted);
2750 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2751 * to obtain a local peer -> destination path and interning the peer ids.
2753 * @return Newly allocated and created path
2755 static struct MeshPeerPath *
2756 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2757 unsigned int get_path_length,
2758 const struct GNUNET_PeerIdentity *put_path,
2759 unsigned int put_path_length)
2761 struct MeshPeerPath *p;
2767 GNUNET_PEER_change_rc (myid, 1);
2768 i = get_path_length;
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2770 for (i--; i >= 0; i--)
2772 id = GNUNET_PEER_intern (&get_path[i]);
2773 if (p->length > 0 && id == p->peers[p->length - 1])
2775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2776 GNUNET_PEER_change_rc (id, -1);
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2781 GNUNET_i2s (&get_path[i]));
2783 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2784 p->peers[p->length - 1] = id;
2787 i = put_path_length;
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2789 for (i--; i >= 0; i--)
2791 id = GNUNET_PEER_intern (&put_path[i]);
2794 /* PUT path went through us, so discard the path up until now and start
2795 * from here to get a much shorter (and loop-free) path.
2800 if (p->length > 0 && id == p->peers[p->length - 1])
2802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2803 GNUNET_PEER_change_rc (id, -1);
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2808 GNUNET_i2s (&put_path[i]));
2810 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2811 p->peers[p->length - 1] = id;
2815 if (get_path_length > 0)
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2817 GNUNET_i2s (&get_path[0]));
2818 if (put_path_length > 0)
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2820 GNUNET_i2s (&put_path[0]));
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2823 for (i = 0; i < p->length; i++)
2825 struct GNUNET_PeerIdentity peer_id;
2827 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2829 GNUNET_i2s (&peer_id));
2837 * Adds a path to the peer_infos of all the peers in the path
2839 * @param p Path to process.
2840 * @param confirmed Whether we know if the path works or not.
2843 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2847 /* TODO: invert and add */
2848 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2849 for (i++; i < p->length; i++)
2851 struct MeshPeerInfo *aux;
2852 struct MeshPeerPath *copy;
2854 aux = peer_info_get_short (p->peers[i]);
2855 copy = path_duplicate (p);
2856 copy->length = i + 1;
2857 peer_info_add_path (aux, copy, GNUNET_NO);
2863 * Send keepalive packets for a peer
2865 * @param cls Closure (tunnel for which to send the keepalive).
2866 * @param tc Notification context.
2868 * TODO: implement explicit multicast keepalive?
2871 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2875 * Search for a tunnel among the incoming tunnels
2877 * @param tid the local id of the tunnel
2879 * @return tunnel handler, NULL if doesn't exist
2881 static struct MeshTunnel *
2882 tunnel_get_incoming (MESH_TunnelNumber tid)
2884 struct GNUNET_HashCode hash;
2886 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2887 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2888 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2893 * Search for a tunnel among the tunnels for a client
2895 * @param c the client whose tunnels to search in
2896 * @param tid the local id of the tunnel
2898 * @return tunnel handler, NULL if doesn't exist
2900 static struct MeshTunnel *
2901 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2903 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2905 return tunnel_get_incoming (tid);
2909 struct GNUNET_HashCode hash;
2911 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2912 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2918 * Search for a tunnel by global ID using PEER_ID
2920 * @param pi owner of the tunnel
2921 * @param tid global tunnel number
2923 * @return tunnel handler, NULL if doesn't exist
2925 static struct MeshTunnel *
2926 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2928 struct MESH_TunnelID id;
2929 struct GNUNET_HashCode hash;
2934 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2935 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2940 * Search for a tunnel by global ID using full PeerIdentities
2942 * @param oid owner of the tunnel
2943 * @param tid global tunnel number
2945 * @return tunnel handler, NULL if doesn't exist
2947 static struct MeshTunnel *
2948 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2950 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2955 * Delete an active client from the tunnel.
2961 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2965 for (i = 0; i < t->nclients; i++)
2967 if (t->clients[i] == c)
2969 t->clients[i] = t->clients[t->nclients - 1];
2970 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2971 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2973 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2981 * Delete an ignored client from the tunnel.
2987 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2991 for (i = 0; i < t->nignore; i++)
2993 if (t->ignore[i] == c)
2995 t->ignore[i] = t->ignore[t->nignore - 1];
2996 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3004 * Delete a client from the tunnel. It should be only done on
3005 * client disconnection, otherwise use client_ignore_tunnel.
3011 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3013 tunnel_delete_ignored_client (t, c);
3014 tunnel_delete_active_client (t, c);
3019 * Iterator to free MeshTunnelChildInfo of tunnel children.
3021 * @param cls Closure (tunnel info).
3022 * @param key Hash of GNUNET_PEER_Id (unused).
3023 * @param value MeshTunnelChildInfo of the child.
3025 * @return always GNUNET_YES, to keep iterating
3028 tunnel_destroy_child (void *cls,
3029 const struct GNUNET_HashCode * key,
3032 GNUNET_free (value);
3038 * Callback used to notify a client owner of a tunnel that a peer has
3039 * disconnected, most likely because of a path change.
3041 * @param cls Closure (tunnel this notification is about).
3042 * @param peer_id Short ID of disconnected peer.
3045 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3047 struct MeshTunnel *t = cls;
3048 struct MeshPeerInfo *peer;
3049 struct MeshPathInfo *path_info;
3051 if (NULL != t->owner && NULL != nc)
3053 struct GNUNET_MESH_PeerControl msg;
3055 msg.header.size = htons (sizeof (msg));
3056 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3057 msg.tunnel_id = htonl (t->local_tid);
3058 GNUNET_PEER_resolve (peer_id, &msg.peer);
3059 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3060 &msg.header, GNUNET_NO);
3062 peer = peer_info_get_short (peer_id);
3063 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3064 path_info->peer = peer;
3066 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3071 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3072 * needed rescources.
3073 * If peer already exists, reevaluate shortest path and change if different.
3075 * @param t Tunnel we want to add a new peer to
3076 * @param peer PeerInfo of the peer being added
3080 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3082 struct GNUNET_PeerIdentity id;
3083 struct MeshPeerPath *best_p;
3084 struct MeshPeerPath *p;
3085 unsigned int best_cost;
3088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3089 GNUNET_PEER_resolve (peer->id, &id);
3091 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3094 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3095 GNUNET_assert (GNUNET_OK ==
3096 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3098 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3101 if (NULL != (p = peer->path_head))
3104 best_cost = tree_get_path_cost (t->tree, p);
3107 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3114 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3115 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3116 t->path_refresh_task =
3117 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3121 /* Start a DHT get */
3122 peer_info_connect (peer, t);
3124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3128 * Add a path to a tunnel which we don't own, just to remember the next hop.
3129 * If destination node was already in the tunnel, the first hop information
3130 * will be replaced with the new path.
3132 * @param t Tunnel we want to add a new peer to
3133 * @param p Path to add
3134 * @param own_pos Position of local node in path.
3138 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3139 unsigned int own_pos)
3141 struct GNUNET_PeerIdentity id;
3143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3144 GNUNET_assert (0 != own_pos);
3145 tree_add_path (t->tree, p, NULL, NULL);
3146 if (own_pos < p->length - 1)
3148 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3149 tree_update_first_hops (t->tree, myid, &id);
3151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3155 * Add a client to a tunnel, initializing all needed data structures.
3157 * @param t Tunnel to which add the client.
3158 * @param c Client which to add to the tunnel.
3161 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3163 struct MeshTunnelClientInfo clinfo;
3165 GNUNET_array_append (t->clients, t->nclients, c);
3166 clinfo.fwd_ack = t->fwd_pid + 1;
3167 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3168 clinfo.fwd_pid = t->fwd_pid;
3169 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3171 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3176 * Notifies a tunnel that a connection has broken that affects at least
3177 * some of its peers. Sends a notification towards the root of the tree.
3178 * In case the peer is the owner of the tree, notifies the client that owns
3179 * the tunnel and tries to reconnect.
3181 * @param t Tunnel affected.
3182 * @param p1 Peer that got disconnected from p2.
3183 * @param p2 Peer that got disconnected from p1.
3185 * @return Short ID of the peer disconnected (either p1 or p2).
3186 * 0 if the tunnel remained unaffected.
3188 static GNUNET_PEER_Id
3189 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3195 tree_notify_connection_broken (t->tree, p1, p2,
3196 &tunnel_notify_client_peer_disconnected,
3198 if (myid != p1 && myid != p2)
3204 if (tree_get_predecessor (t->tree) != 0)
3206 /* We are the peer still connected, notify owner of the disconnection. */
3207 struct GNUNET_MESH_PathBroken msg;
3208 struct GNUNET_PeerIdentity neighbor;
3210 msg.header.size = htons (sizeof (msg));
3211 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3212 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3213 msg.tid = htonl (t->id.tid);
3214 msg.peer1 = my_full_id;
3215 GNUNET_PEER_resolve (pid, &msg.peer2);
3216 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3217 send_message (&msg.header, &neighbor, t);
3225 * Send a multicast packet to a neighbor.
3227 * @param cls Closure (Info about the multicast packet)
3228 * @param neighbor_id Short ID of the neighbor to send the packet to.
3231 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3233 struct MeshData *mdata = cls;
3234 struct MeshTransmissionDescriptor *info;
3235 struct GNUNET_PeerIdentity neighbor;
3237 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3239 info->mesh_data = mdata;
3240 (*(mdata->reference_counter)) ++;
3241 info->destination = neighbor_id;
3242 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3244 GNUNET_i2s (&neighbor));
3245 info->peer = peer_info_get (&neighbor);
3246 GNUNET_assert (NULL != info->peer);
3248 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3249 info->mesh_data->data_len,
3256 * Send a message in a tunnel in multicast, sending a copy to each child node
3257 * down the local one in the tunnel tree.
3259 * @param t Tunnel in which to send the data.
3260 * @param msg Message to be sent.
3261 * @param internal Has the service generated this message?
3264 tunnel_send_multicast (struct MeshTunnel *t,
3265 const struct GNUNET_MessageHeader *msg,
3268 struct MeshData *mdata;
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3271 " sending a multicast packet...\n");
3272 mdata = GNUNET_malloc (sizeof (struct MeshData));
3273 mdata->data_len = ntohs (msg->size);
3274 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3276 mdata->data = GNUNET_malloc (mdata->data_len);
3277 memcpy (mdata->data, msg, mdata->data_len);
3278 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3280 struct GNUNET_MESH_Multicast *mcast;
3282 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3283 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3285 ntohl (mcast->ttl));
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3291 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3292 && GNUNET_NO == internal)
3294 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3296 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3302 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3303 if (*(mdata->reference_counter) == 0)
3305 GNUNET_free (mdata->data);
3306 GNUNET_free (mdata->reference_counter);
3307 if (NULL != mdata->task)
3309 GNUNET_SCHEDULER_cancel(*(mdata->task));
3310 GNUNET_free (mdata->task);
3311 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3313 // FIXME change order?
3314 GNUNET_free (mdata);
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3317 " sending a multicast packet done\n");
3323 * Increase the SKIP value of all peers that
3324 * have not received a unicast message.
3326 * @param cls Closure (ID of the peer that HAS received the message).
3327 * @param key ID of the neighbor.
3328 * @param value Information about the neighbor.
3330 * @return GNUNET_YES to keep iterating.
3333 tunnel_add_skip (void *cls,
3334 const struct GNUNET_HashCode * key,
3337 struct GNUNET_PeerIdentity *neighbor = cls;
3338 struct MeshTunnelChildInfo *cinfo = value;
3340 /* TODO compare only pointers? key == neighbor? */
3341 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3351 * @brief Get neighbor's Flow Control information.
3353 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3354 * descendant of the local node in a certain tunnel.
3355 * If the info is not yet there (recently created path), creates the data struct
3356 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3359 * @param t Tunnel related.
3360 * @param peer Neighbor whose Flow Control info is needed.
3362 * @return Neighbor's Flow Control info.
3364 static struct MeshTunnelChildInfo *
3365 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3366 const struct GNUNET_PeerIdentity *peer)
3368 struct MeshTunnelChildInfo *cinfo;
3369 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3375 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3376 cinfo->id = GNUNET_PEER_intern (peer);
3377 cinfo->skip = t->fwd_pid;
3379 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3380 cinfo->fwd_ack = t->fwd_pid + delta;
3381 cinfo->bck_ack = delta;
3383 GNUNET_assert (GNUNET_OK ==
3384 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3387 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3394 * Get the Flow Control info of a client.
3396 * @param t Tunnel on which to look.
3397 * @param c Client whose ACK to get.
3399 * @return ACK value.
3401 static struct MeshTunnelClientInfo *
3402 tunnel_get_client_fc (struct MeshTunnel *t,
3403 struct MeshClient *c)
3407 for (i = 0; i < t->nclients; i++)
3409 if (t->clients[i] != c)
3411 return &t->clients_fc[i];
3414 return NULL; // avoid compiler / coverity complaints
3419 * Iterator to get the appropiate ACK value from all children nodes.
3421 * @param cls Closue (tunnel).
3422 * @param id Id of the child node.
3425 tunnel_get_child_fwd_ack (void *cls,
3428 struct GNUNET_PeerIdentity peer_id;
3429 struct MeshTunnelChildInfo *cinfo;
3430 struct MeshTunnelChildIteratorContext *ctx = cls;
3431 struct MeshTunnel *t = ctx->t;
3434 GNUNET_PEER_resolve (id, &peer_id);
3435 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3436 ack = cinfo->fwd_ack;
3438 if (GNUNET_NO == ctx->init)
3440 ctx->max_child_ack = ack;
3441 ctx->init = GNUNET_YES;
3444 if (GNUNET_YES == t->speed_min)
3446 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3450 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3457 * Get the maximum PID allowed to transmit to any
3458 * tunnel child of the local peer, depending on the tunnel
3459 * buffering/speed settings.
3463 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3466 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3468 struct MeshTunnelChildIteratorContext ctx;
3470 ctx.max_child_ack = 0;
3472 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3474 if (0 == ctx.nchildren)
3477 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3478 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3480 return (int64_t) ctx.max_child_ack;
3485 * Set the FWD ACK value of a client in a particular tunnel.
3487 * @param t Tunnel affected.
3488 * @param c Client whose ACK to set.
3489 * @param ack ACK value.
3492 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3493 struct MeshClient *c,
3498 for (i = 0; i < t->nclients; i++)
3500 if (t->clients[i] != c)
3502 t->clients_fc[i].fwd_ack = ack;
3510 * Get the highest ACK value of all clients in a particular tunnel,
3511 * according to the buffering/speed settings.
3513 * @param t Tunnel on which to look.
3515 * @return Corresponding ACK value (max uint32_t).
3516 * If no clients are suscribed, -1.
3519 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3524 if (0 == t->nclients)
3527 for (ack = -1, i = 0; i < t->nclients; i++)
3530 (GNUNET_YES == t->speed_min &&
3531 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3532 (GNUNET_NO == t->speed_min &&
3533 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3535 ack = t->clients_fc[i].fwd_ack;
3539 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3540 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3542 return (uint32_t) ack;
3547 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3548 * mode and the status of all children nodes.
3552 * @return Maximum PID allowed.
3555 tunnel_get_fwd_ack (struct MeshTunnel *t)
3558 uint32_t buffer_free;
3563 count = t->fwd_pid - t->skip;
3564 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3565 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3566 child_ack = tunnel_get_children_fwd_ack (t);
3567 client_ack = tunnel_get_clients_fwd_ack (t);
3568 if (-1 == child_ack)
3570 // Node has no children, child_ack AND core buffer are irrelevant.
3571 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
3572 return (uint32_t) client_ack;
3575 if (GNUNET_YES == t->speed_min)
3577 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3578 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3582 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3583 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3585 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3586 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3588 count, buffer_free, child_ack, client_ack, ack);
3594 * Build a local ACK message and send it to a local client.
3596 * @param t Tunnel on which to send the ACK.
3597 * @param c Client to whom send the ACK.
3598 * @param ack Value of the ACK.
3601 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3603 struct GNUNET_MESH_LocalAck msg;
3605 msg.header.size = htons (sizeof (msg));
3606 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3607 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3608 msg.max_pid = htonl (ack);
3609 GNUNET_SERVER_notification_context_unicast(nc,
3616 * Build an ACK message and queue it to send to the given peer.
3618 * @param t Tunnel on which to send the ACK.
3619 * @param peer Peer to whom send the ACK.
3620 * @param ack Value of the ACK.
3623 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3625 struct GNUNET_MESH_ACK msg;
3627 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3628 msg.header.size = htons (sizeof (msg));
3629 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3630 msg.pid = htonl (ack);
3631 msg.tid = htonl (t->id.tid);
3633 send_message (&msg.header, peer, t);
3638 * Notify a the owner of a tunnel about how many more
3639 * payload packages will we accept on a given tunnel.
3641 * @param t Tunnel on which to send the ACK.
3644 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3649 "Sending client FWD ACK on tunnel %X\n",
3652 ack = tunnel_get_fwd_ack (t);
3654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3655 if (t->last_fwd_ack == ack)
3658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3659 t->last_fwd_ack = ack;
3660 send_local_ack (t, t->owner, ack);
3665 * Send an ACK informing the predecessor about the available buffer space.
3666 * In case there is no predecessor, inform the owning client.
3667 * If buffering is off, send only on behalf of children or self if endpoint.
3668 * If buffering is on, send when sent to children and buffer space is free.
3669 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3670 * the ACK itself goes "back" (towards root).
3672 * @param t Tunnel on which to send the ACK.
3673 * @param type Type of message that triggered the ACK transmission.
3676 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3678 struct GNUNET_PeerIdentity id;
3681 if (NULL != t->owner)
3683 tunnel_send_client_fwd_ack (t);
3686 /* Is it after unicast / multicast retransmission? */
3689 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3690 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3692 "ACK due to FWD DATA retransmission\n");
3693 if (GNUNET_YES == t->nobuffer)
3695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3698 if (t->fwd_queue_max > t->fwd_queue_n * 2)
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3704 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3705 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3711 /* Ok, ACK might be necessary, what PID to ACK? */
3712 ack = tunnel_get_fwd_ack (t);
3714 /* If speed_min and not all children have ack'd, dont send yet */
3715 if (ack == t->last_fwd_ack)
3717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3721 t->last_fwd_ack = ack;
3722 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3723 send_ack (t, &id, ack);
3728 * Iterator to send a child node a BCK ACK to allow him to send more
3731 * @param cls Closure (tunnel).
3732 * @param id Id of the child node.
3735 tunnel_send_child_bck_ack (void *cls,
3738 struct MeshTunnel *t = cls;
3739 struct MeshTunnelChildInfo *cinfo;
3740 struct GNUNET_PeerIdentity peer;
3742 GNUNET_PEER_resolve (id, &peer);
3743 cinfo = tunnel_get_neighbor_fc (t, &peer);
3745 if (cinfo->bck_ack != cinfo->pid &&
3746 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3750 send_ack (t, &peer, cinfo->bck_ack);
3755 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3757 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3759 * @param t Tunnel on which to send the BCK ACKs.
3762 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3765 unsigned int tunnel_delta;
3767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3769 tunnel_delta = t->bck_ack - t->bck_pid;
3770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3772 /* Find client whom to allow to send to origin (with lowest buffer space) */
3773 for (i = 0; i < t->nclients; i++)
3775 struct MeshTunnelClientInfo *clinfo;
3778 clinfo = &t->clients_fc[i];
3779 delta = clinfo->bck_ack - clinfo->bck_pid;
3780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3781 t->clients[i]->id, delta);
3783 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3784 (GNUNET_YES == t->nobuffer && 0 == delta))
3788 ack = clinfo->bck_pid;
3789 ack += t->nobuffer ? 1 : tunnel_delta;
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3791 " sending ack to client %u: %u\n",
3792 t->clients[i]->id, ack);
3793 send_local_ack (t, t->clients[i], ack);
3794 clinfo->bck_ack = ack;
3801 * Send an ACK informing the children nodes and destination clients about
3802 * the available buffer space.
3803 * If buffering is off, send only on behalf of root (can be self).
3804 * If buffering is on, send when sent to predecessor and buffer space is free.
3805 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3806 * the ACK itself goes "forward" (towards children/clients).
3808 * @param t Tunnel on which to send the ACK.
3809 * @param type Type of message that triggered the ACK transmission.
3812 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3815 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3816 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3817 /* Is it after data to_origin retransmission? */
3820 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3821 if (GNUNET_YES == t->nobuffer)
3823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3824 " Not sending ACK, nobuffer\n");
3828 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3829 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3835 tunnel_send_clients_bck_ack (t);
3836 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
3841 * Send a message to all peers in this tunnel that the tunnel is no longer
3844 * @param t The tunnel whose peers to notify.
3847 tunnel_send_destroy (struct MeshTunnel *t)
3849 struct GNUNET_MESH_TunnelDestroy msg;
3851 msg.header.size = htons (sizeof (msg));
3852 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3853 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3854 msg.tid = htonl (t->id.tid);
3855 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3860 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3862 * @param cls Closure (Tunnel which to cancel).
3863 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3866 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3868 struct MeshTunnel *t = cls;
3869 struct MeshPeerInfo *peer_info;
3870 struct MeshPeerQueue *pq;
3871 struct MeshPeerQueue *next;
3873 peer_info = peer_info_get_short (neighbor_id);
3874 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3877 if (pq->tunnel == t)
3879 queue_destroy (pq, GNUNET_YES);
3882 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3884 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3885 peer_info->core_transmit = NULL;
3890 * Destroy the tunnel and free any allocated resources linked to it.
3892 * @param t the tunnel to destroy
3894 * @return GNUNET_OK on success
3897 tunnel_destroy (struct MeshTunnel *t)
3899 struct MeshClient *c;
3900 struct GNUNET_HashCode hash;
3907 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3913 struct GNUNET_PeerIdentity id;
3915 GNUNET_PEER_resolve (t->id.oid, &id);
3916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3917 GNUNET_i2s (&id), t->id.tid);
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3923 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3924 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3929 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3932 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3936 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3937 for (i = 0; i < t->nclients; i++)
3941 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3946 for (i = 0; i < t->nignore; i++)
3950 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3955 if (t->nclients > 0)
3958 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3962 GNUNET_free (t->clients);
3964 if (NULL != t->peers)
3966 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3968 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3971 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3972 &tunnel_destroy_child,
3974 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3976 tree_destroy (t->tree);
3978 if (NULL != t->regex_ctx)
3979 regex_cancel_search (t->regex_ctx);
3980 if (NULL != t->dht_get_type)
3981 GNUNET_DHT_get_stop (t->dht_get_type);
3982 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3983 GNUNET_SCHEDULER_cancel (t->timeout_task);
3984 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3985 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3988 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3989 GNUNET_assert (0 <= n_tunnels);
3996 * Create a new tunnel
3998 * @param owner Who is the owner of the tunnel (short ID).
3999 * @param tid Tunnel Number of the tunnel.
4000 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4001 * @param local Tunnel Number for the tunnel, for the client point of view.
4003 * @return A new initialized tunnel. NULL on error.
4005 static struct MeshTunnel *
4006 tunnel_new (GNUNET_PEER_Id owner,
4007 MESH_TunnelNumber tid,
4008 struct MeshClient *client,
4009 MESH_TunnelNumber local)
4011 struct MeshTunnel *t;
4012 struct GNUNET_HashCode hash;
4014 if (n_tunnels >= max_tunnels && NULL == client)
4017 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4020 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4021 t->bck_queue_max = t->fwd_queue_max;
4022 t->tree = tree_new (owner);
4024 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4025 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4026 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4027 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4028 t->local_tid = local;
4029 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4031 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4033 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4035 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4036 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4043 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4050 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4052 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4053 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4057 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4067 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4068 * that are no longer needed, as well as nodes of no longer reachable peers.
4069 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4071 * @param t Tunnel from which to remove the path.
4072 * @param peer Short id of the peer which should be removed.
4075 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4077 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4083 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4084 * client when the client disconnects. If the client is not the owner, the
4085 * owner will get notified if no more clients are in the tunnel and the client
4086 * get removed from the tunnel's list.
4088 * @param cls closure (client that is disconnecting)
4089 * @param key the hash of the local tunnel id (used to access the hashmap)
4090 * @param value the value stored at the key (tunnel to destroy)
4092 * @return GNUNET_OK on success
4095 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4097 struct MeshTunnel *t = value;
4098 struct MeshClient *c = cls;
4101 send_client_tunnel_disconnect(t, c);
4104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4105 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4106 tunnel_delete_client(t, c);
4107 client_delete_tunnel(c, t);
4110 tunnel_send_destroy(t);
4111 r = tunnel_destroy (t);
4117 * Timeout function, destroys tunnel if called
4119 * @param cls Closure (tunnel to destroy).
4120 * @param tc TaskContext
4123 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4125 struct MeshTunnel *t = cls;
4127 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4129 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4134 * Resets the tunnel timeout. Starts it if no timeout was running.
4136 * @param t Tunnel whose timeout to reset.
4138 * TODO use heap to improve efficiency of scheduler.
4141 tunnel_reset_timeout (struct MeshTunnel *t)
4143 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4144 GNUNET_SCHEDULER_cancel (t->timeout_task);
4146 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4147 (refresh_path_time, 4), &tunnel_timeout, t);
4151 /******************************************************************************/
4152 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4153 /******************************************************************************/
4156 * Function to send a create path packet to a peer.
4158 * @param cls closure
4159 * @param size number of bytes available in buf
4160 * @param buf where the callee should write the message
4161 * @return number of bytes written to buf
4164 send_core_path_create (void *cls, size_t size, void *buf)
4166 struct MeshPathInfo *info = cls;
4167 struct GNUNET_MESH_ManipulatePath *msg;
4168 struct GNUNET_PeerIdentity *peer_ptr;
4169 struct MeshTunnel *t = info->t;
4170 struct MeshPeerPath *p = info->path;
4175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4177 sizeof (struct GNUNET_MESH_ManipulatePath) +
4178 p->length * sizeof (struct GNUNET_PeerIdentity);
4180 if (size < size_needed || NULL == buf)
4185 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4186 msg->header.size = htons (size_needed);
4187 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4188 msg->tid = ntohl (t->id.tid);
4190 if (GNUNET_YES == t->speed_min)
4191 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4192 if (GNUNET_YES == t->nobuffer)
4193 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4194 msg->opt = htonl(opt);
4196 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4197 for (i = 0; i < p->length; i++)
4199 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4206 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4212 * Fill the core buffer
4214 * @param cls closure (data itself)
4215 * @param size number of bytes available in buf
4216 * @param buf where the callee should write the message
4218 * @return number of bytes written to buf
4221 send_core_data_multicast (void *cls, size_t size, void *buf)
4223 struct MeshTransmissionDescriptor *info = cls;
4226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4227 GNUNET_assert (NULL != info);
4228 GNUNET_assert (NULL != info->peer);
4229 total_size = info->mesh_data->data_len;
4230 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4232 if (total_size > size)
4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4238 memcpy (buf, info->mesh_data->data, total_size);
4241 struct GNUNET_MESH_Multicast *mc;
4242 struct GNUNET_MessageHeader *mh;
4245 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4247 mc = (struct GNUNET_MESH_Multicast *) mh;
4248 mh = (struct GNUNET_MessageHeader *) &mc[1];
4249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4250 " multicast, payload type %s\n",
4251 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4253 " multicast, payload size %u\n", ntohs (mh->size));
4257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4258 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4262 data_descriptor_decrement_rc (info->mesh_data);
4263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4271 * Creates a path ack message in buf and frees all unused resources.
4273 * @param cls closure (MeshTransmissionDescriptor)
4274 * @param size number of bytes available in buf
4275 * @param buf where the callee should write the message
4276 * @return number of bytes written to buf
4279 send_core_path_ack (void *cls, size_t size, void *buf)
4281 struct MeshTransmissionDescriptor *info = cls;
4282 struct GNUNET_MESH_PathACK *msg = buf;
4284 GNUNET_assert (NULL != info);
4285 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4290 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4291 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4292 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4293 msg->tid = htonl (info->origin->tid);
4294 msg->peer_id = my_full_id;
4297 /* TODO add signature */
4299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4300 return sizeof (struct GNUNET_MESH_PathACK);
4305 * Free a transmission that was already queued with all resources
4306 * associated to the request.
4308 * @param queue Queue handler to cancel.
4309 * @param clear_cls Is it necessary to free associated cls?
4312 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4314 struct MeshTransmissionDescriptor *dd;
4315 struct MeshPathInfo *path_info;
4317 if (GNUNET_YES == clear_cls)
4319 switch (queue->type)
4321 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4322 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4323 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4326 data_descriptor_decrement_rc (dd->mesh_data);
4328 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4330 path_info = queue->cls;
4331 path_destroy (path_info->path);
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4337 GNUNET_free_non_null (queue->cls);
4339 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4340 queue->peer->queue_tail,
4342 GNUNET_free (queue);
4347 * Core callback to write a queued packet to core buffer
4349 * @param cls Closure (peer info).
4350 * @param size Number of bytes available in buf.
4351 * @param buf Where the to write the message.
4353 * @return number of bytes written to buf
4356 queue_send (void *cls, size_t size, void *buf)
4358 struct MeshPeerInfo *peer = cls;
4359 struct GNUNET_MessageHeader *msg;
4360 struct MeshPeerQueue *queue;
4361 struct MeshTunnel *t;
4364 peer->core_transmit = NULL;
4365 queue = peer->queue_head;
4367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4369 /* If queue is empty, send should have been cancelled */
4375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4377 /* Check if buffer size is enough for the message */
4378 if (queue->size > size)
4380 struct GNUNET_PeerIdentity id;
4382 GNUNET_PEER_resolve (peer->id, &id);
4383 peer->core_transmit =
4384 GNUNET_CORE_notify_transmit_ready(core_handle,
4387 GNUNET_TIME_UNIT_FOREVER_REL,
4394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4397 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast: %u\n");
4402 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4409 switch (queue->type)
4412 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4413 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4414 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4416 "********* raw: %u\n",
4419 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4420 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4421 data_size = send_core_data_raw (queue->cls, size, buf);
4422 msg = (struct GNUNET_MessageHeader *) buf;
4423 switch (ntohs (msg->type)) // Type of preconstructed message
4425 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4426 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4428 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4429 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4435 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4437 data_size = send_core_data_multicast(queue->cls, size, buf);
4438 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4440 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4442 data_size = send_core_path_create(queue->cls, size, buf);
4444 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4446 data_size = send_core_path_ack(queue->cls, size, buf);
4450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4451 "********* type unknown: %u\n",
4456 /* Free queue, but cls was freed by send_core_* */
4457 queue_destroy (queue, GNUNET_NO);
4459 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4465 /* If more data in queue, send next */
4466 if (NULL != peer->queue_head)
4468 struct GNUNET_PeerIdentity id;
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4471 GNUNET_PEER_resolve (peer->id, &id);
4472 peer->core_transmit =
4473 GNUNET_CORE_notify_transmit_ready(core_handle,
4476 GNUNET_TIME_UNIT_FOREVER_REL,
4478 peer->queue_head->size,
4482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4488 * Queue and pass message to core when possible.
4490 * @param cls Closure (type dependant).
4491 * @param type Type of the message, 0 for a raw message.
4492 * @param size Size of the message.
4493 * @param dst Neighbor to send message to.
4494 * @param t Tunnel this message belongs to.
4497 queue_add (void *cls, uint16_t type, size_t size,
4498 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4500 struct MeshPeerQueue *queue;
4505 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4506 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4508 n = &t->fwd_queue_n;
4509 max = &t->fwd_queue_max;
4511 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4513 n = &t->bck_queue_n;
4514 max = &t->bck_queue_max;
4519 if (NULL == t->owner)
4520 GNUNET_break_op(0); // TODO: kill connection?
4523 return; // Drop message
4527 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4533 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4534 if (NULL == dst->core_transmit)
4536 struct GNUNET_PeerIdentity id;
4538 GNUNET_PEER_resolve (dst->id, &id);
4539 dst->core_transmit =
4540 GNUNET_CORE_notify_transmit_ready(core_handle,
4543 GNUNET_TIME_UNIT_FOREVER_REL,
4552 /******************************************************************************/
4553 /******************** MESH NETWORK HANDLERS **************************/
4554 /******************************************************************************/
4558 * Core handler for path creation
4560 * @param cls closure
4561 * @param message message
4562 * @param peer peer identity this notification is about
4563 * @param atsi performance data
4564 * @param atsi_count number of records in 'atsi'
4566 * @return GNUNET_OK to keep the connection open,
4567 * GNUNET_SYSERR to close it (signal serious error)
4570 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4571 const struct GNUNET_MessageHeader *message,
4572 const struct GNUNET_ATS_Information *atsi,
4573 unsigned int atsi_count)
4575 unsigned int own_pos;
4578 MESH_TunnelNumber tid;
4579 struct GNUNET_MESH_ManipulatePath *msg;
4580 struct GNUNET_PeerIdentity *pi;
4581 struct GNUNET_HashCode hash;
4582 struct MeshPeerPath *path;
4583 struct MeshPeerInfo *dest_peer_info;
4584 struct MeshPeerInfo *orig_peer_info;
4585 struct MeshTunnel *t;
4587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4588 "Received a path create msg [%s]\n",
4589 GNUNET_i2s (&my_full_id));
4590 size = ntohs (message->size);
4591 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4593 GNUNET_break_op (0);
4597 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4598 if (size % sizeof (struct GNUNET_PeerIdentity))
4600 GNUNET_break_op (0);
4603 size /= sizeof (struct GNUNET_PeerIdentity);
4606 GNUNET_break_op (0);
4609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4610 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4612 tid = ntohl (msg->tid);
4613 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4615 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4616 t = tunnel_get (pi, tid);
4617 if (NULL == t) // FIXME only for INCOMING tunnels?
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4622 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4625 // FIXME notify failure
4628 opt = ntohl (msg->opt);
4629 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4630 GNUNET_YES : GNUNET_NO;
4631 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4632 GNUNET_YES : GNUNET_NO;
4634 if (GNUNET_YES == t->nobuffer)
4636 t->bck_queue_max = 1;
4637 t->fwd_queue_max = 1;
4640 // FIXME only assign a local tid if a local client is interested (on demand)
4641 while (NULL != tunnel_get_incoming (next_local_tid))
4642 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4643 t->local_tid_dest = next_local_tid++;
4644 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4647 tunnel_reset_timeout (t);
4648 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4650 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4651 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4659 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4660 if (NULL == dest_peer_info)
4662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4663 " Creating PeerInfo for destination.\n");
4664 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4665 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4666 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4670 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4671 if (NULL == orig_peer_info)
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4674 " Creating PeerInfo for origin.\n");
4675 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4676 orig_peer_info->id = GNUNET_PEER_intern (pi);
4677 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4678 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4681 path = path_new (size);
4683 for (i = 0; i < size; i++)
4685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4686 GNUNET_i2s (&pi[i]));
4687 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4688 if (path->peers[i] == myid)
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4694 /* cannot be self, must be 'not found' */
4695 /* create path: self not found in path through self */
4696 GNUNET_break_op (0);
4697 path_destroy (path);
4701 path_add_to_peers (path, GNUNET_NO);
4702 tunnel_add_path (t, path, own_pos);
4703 if (own_pos == size - 1)
4705 /* It is for us! Send ack. */
4706 struct MeshTransmissionDescriptor *info;
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4709 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4710 if (NULL == t->peers)
4712 /* New tunnel! Notify clients on data. */
4713 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4715 GNUNET_break (GNUNET_SYSERR !=
4716 GNUNET_CONTAINER_multihashmap_put (t->peers,
4717 &my_full_id.hashPubKey,
4720 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4721 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4722 info->origin = &t->id;
4723 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4724 GNUNET_assert (NULL != info->peer);
4726 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4727 sizeof (struct GNUNET_MESH_PathACK),
4733 struct MeshPeerPath *path2;
4735 /* It's for somebody else! Retransmit. */
4736 path2 = path_duplicate (path);
4737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4738 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4739 path2 = path_duplicate (path);
4740 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4741 send_create_path (dest_peer_info, path, t);
4748 * Core handler for path destruction
4750 * @param cls closure
4751 * @param message message
4752 * @param peer peer identity this notification is about
4753 * @param atsi performance data
4754 * @param atsi_count number of records in 'atsi'
4756 * @return GNUNET_OK to keep the connection open,
4757 * GNUNET_SYSERR to close it (signal serious error)
4760 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4761 const struct GNUNET_MessageHeader *message,
4762 const struct GNUNET_ATS_Information *atsi,
4763 unsigned int atsi_count)
4765 struct GNUNET_MESH_ManipulatePath *msg;
4766 struct GNUNET_PeerIdentity *pi;
4767 struct MeshPeerPath *path;
4768 struct MeshTunnel *t;
4769 unsigned int own_pos;
4773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4774 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4775 size = ntohs (message->size);
4776 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4778 GNUNET_break_op (0);
4782 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4783 if (size % sizeof (struct GNUNET_PeerIdentity))
4785 GNUNET_break_op (0);
4788 size /= sizeof (struct GNUNET_PeerIdentity);
4791 GNUNET_break_op (0);
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4796 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4797 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4799 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4801 t = tunnel_get (pi, ntohl (msg->tid));
4804 /* TODO notify back: we don't know this tunnel */
4805 GNUNET_break_op (0);
4808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4809 path = path_new (size);
4811 for (i = 0; i < size; i++)
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4814 GNUNET_i2s (&pi[i]));
4815 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4816 if (path->peers[i] == myid)
4819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4820 if (own_pos < path->length - 1)
4821 send_message (message, &pi[own_pos + 1], t);
4823 send_client_tunnel_disconnect(t, NULL);
4825 tunnel_delete_peer (t, path->peers[path->length - 1]);
4826 path_destroy (path);
4832 * Core handler for notifications of broken paths
4834 * @param cls closure
4835 * @param message message
4836 * @param peer peer identity this notification is about
4837 * @param atsi performance data
4838 * @param atsi_count number of records in 'atsi'
4840 * @return GNUNET_OK to keep the connection open,
4841 * GNUNET_SYSERR to close it (signal serious error)
4844 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4845 const struct GNUNET_MessageHeader *message,
4846 const struct GNUNET_ATS_Information *atsi,
4847 unsigned int atsi_count)
4849 struct GNUNET_MESH_PathBroken *msg;
4850 struct MeshTunnel *t;
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4853 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4854 msg = (struct GNUNET_MESH_PathBroken *) message;
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4856 GNUNET_i2s (&msg->peer1));
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4858 GNUNET_i2s (&msg->peer2));
4859 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4862 GNUNET_break_op (0);
4865 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4866 GNUNET_PEER_search (&msg->peer2));
4873 * Core handler for tunnel destruction
4875 * @param cls closure
4876 * @param message message
4877 * @param peer peer identity this notification is about
4878 * @param atsi performance data
4879 * @param atsi_count number of records in 'atsi'
4881 * @return GNUNET_OK to keep the connection open,
4882 * GNUNET_SYSERR to close it (signal serious error)
4885 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4886 const struct GNUNET_MessageHeader *message,
4887 const struct GNUNET_ATS_Information *atsi,
4888 unsigned int atsi_count)
4890 struct GNUNET_MESH_TunnelDestroy *msg;
4891 struct MeshTunnel *t;
4893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4894 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4895 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4897 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4898 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4901 /* Probably already got the message from another path,
4902 * destroyed the tunnel and retransmitted to children.
4905 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4908 if (t->id.oid == myid)
4910 GNUNET_break_op (0);
4913 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4915 /* Tunnel was incoming, notify clients */
4916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4917 t->local_tid, t->local_tid_dest);
4918 send_clients_tunnel_destroy (t);
4920 tunnel_send_destroy (t);
4921 t->destroy = GNUNET_YES;
4922 // TODO: add timeout to destroy the tunnel anyway
4928 * Core handler for mesh network traffic going from the origin to a peer
4930 * @param cls closure
4931 * @param peer peer identity this notification is about
4932 * @param message message
4933 * @param atsi performance data
4934 * @param atsi_count number of records in 'atsi'
4935 * @return GNUNET_OK to keep the connection open,
4936 * GNUNET_SYSERR to close it (signal serious error)
4939 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4940 const struct GNUNET_MessageHeader *message,
4941 const struct GNUNET_ATS_Information *atsi,
4942 unsigned int atsi_count)
4944 struct GNUNET_MESH_Unicast *msg;
4945 struct GNUNET_PeerIdentity *neighbor;
4946 struct MeshTunnelChildInfo *cinfo;
4947 struct MeshTunnel *t;
4948 GNUNET_PEER_Id dest_id;
4953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4956 size = ntohs (message->size);
4958 sizeof (struct GNUNET_MESH_Unicast) +
4959 sizeof (struct GNUNET_MessageHeader))
4964 msg = (struct GNUNET_MESH_Unicast *) message;
4965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4966 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4968 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4971 /* TODO notify back: we don't know this tunnel */
4972 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4973 GNUNET_break_op (0);
4976 pid = ntohl (msg->pid);
4977 if (t->fwd_pid == pid)
4979 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4980 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4981 " Already seen pid %u, DROPPING!\n", pid);
4986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4987 " pid %u not seen yet, forwarding\n", pid);
4990 t->skip += (pid - t->fwd_pid) - 1;
4993 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4995 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4996 GNUNET_break_op (0);
5000 tunnel_reset_timeout (t);
5001 dest_id = GNUNET_PEER_search (&msg->destination);
5002 if (dest_id == myid)
5004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5005 " it's for us! sending to clients...\n");
5006 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5007 send_subscribed_clients (message, &msg[1].header, t);
5008 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5011 ttl = ntohl (msg->ttl);
5012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5015 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5016 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5017 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5021 " not for us, retransmitting...\n");
5023 neighbor = tree_get_first_hop (t->tree, dest_id);
5024 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5026 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5029 if (GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5031 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5032 GNUNET_break_op (0);
5035 send_message (message, neighbor, t);
5036 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5042 * Core handler for mesh network traffic going from the origin to all peers
5044 * @param cls closure
5045 * @param message message
5046 * @param peer peer identity this notification is about
5047 * @param atsi performance data
5048 * @param atsi_count number of records in 'atsi'
5049 * @return GNUNET_OK to keep the connection open,
5050 * GNUNET_SYSERR to close it (signal serious error)
5052 * TODO: Check who we got this from, to validate route.
5055 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5056 const struct GNUNET_MessageHeader *message,
5057 const struct GNUNET_ATS_Information *atsi,
5058 unsigned int atsi_count)
5060 struct GNUNET_MESH_Multicast *msg;
5061 struct MeshTunnel *t;
5065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5067 size = ntohs (message->size);
5068 if (sizeof (struct GNUNET_MESH_Multicast) +
5069 sizeof (struct GNUNET_MessageHeader) > size)
5071 GNUNET_break_op (0);
5074 msg = (struct GNUNET_MESH_Multicast *) message;
5075 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5079 /* TODO notify that we dont know that tunnel */
5080 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5081 GNUNET_break_op (0);
5084 pid = ntohl (msg->pid);
5085 if (t->fwd_pid == pid)
5087 /* already seen this packet, drop */
5088 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5090 " Already seen pid %u, DROPPING!\n", pid);
5091 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5097 " pid %u not seen yet, forwarding\n", pid);
5099 t->skip += (pid - t->fwd_pid) - 1;
5101 tunnel_reset_timeout (t);
5103 /* Transmit to locally interested clients */
5104 if (NULL != t->peers &&
5105 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5107 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5108 send_subscribed_clients (message, &msg[1].header, t);
5109 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5112 if (ntohl (msg->ttl) == 0)
5114 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5115 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5116 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5119 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5120 tunnel_send_multicast (t, message, GNUNET_NO);
5126 * Core handler for mesh network traffic toward the owner of a tunnel
5128 * @param cls closure
5129 * @param message message
5130 * @param peer peer identity this notification is about
5131 * @param atsi performance data
5132 * @param atsi_count number of records in 'atsi'
5134 * @return GNUNET_OK to keep the connection open,
5135 * GNUNET_SYSERR to close it (signal serious error)
5138 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5139 const struct GNUNET_MessageHeader *message,
5140 const struct GNUNET_ATS_Information *atsi,
5141 unsigned int atsi_count)
5143 struct GNUNET_MESH_ToOrigin *msg;
5144 struct GNUNET_PeerIdentity id;
5145 struct MeshPeerInfo *peer_info;
5146 struct MeshTunnel *t;
5150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5152 size = ntohs (message->size);
5153 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5154 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5156 GNUNET_break_op (0);
5159 msg = (struct GNUNET_MESH_ToOrigin *) message;
5160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5161 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5162 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5166 /* TODO notify that we dont know this tunnel (whom)? */
5167 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5168 GNUNET_break_op (0);
5172 if (NULL != t->owner)
5175 struct GNUNET_MESH_ToOrigin *copy;
5177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5178 " it's for us! sending to clients...\n");
5179 /* TODO signature verification */
5180 memcpy (cbuf, message, size);
5181 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5182 copy->tid = htonl (t->local_tid);
5183 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5184 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5185 ©->header, GNUNET_NO);
5186 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5190 " not for us, retransmitting...\n");
5192 peer_info = peer_info_get (&msg->oid);
5193 if (NULL == peer_info)
5195 /* unknown origin of tunnel */
5199 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5200 send_message (message, &id, t);
5201 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5208 * Core handler for mesh network traffic point-to-point acks.
5210 * @param cls closure
5211 * @param message message
5212 * @param peer peer identity this notification is about
5213 * @param atsi performance data
5214 * @param atsi_count number of records in 'atsi'
5216 * @return GNUNET_OK to keep the connection open,
5217 * GNUNET_SYSERR to close it (signal serious error)
5220 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5221 const struct GNUNET_MessageHeader *message,
5222 const struct GNUNET_ATS_Information *atsi,
5223 unsigned int atsi_count)
5225 struct GNUNET_MESH_ACK *msg;
5226 struct MeshTunnel *t;
5229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5231 msg = (struct GNUNET_MESH_ACK *) message;
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5233 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5234 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5238 /* TODO notify that we dont know this tunnel (whom)? */
5239 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5240 GNUNET_break_op (0);
5243 ack = ntohl (msg->pid);
5245 /* Is this a forward or backward ACK? */
5246 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5248 struct MeshTunnelChildInfo *cinfo;
5250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5251 cinfo = tunnel_get_neighbor_fc (t, peer);
5252 cinfo->fwd_ack = ack;
5253 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5259 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5261 // FIXME fc Unlock queues?
5267 * Core handler for path ACKs
5269 * @param cls closure
5270 * @param message message
5271 * @param peer peer identity this notification is about
5272 * @param atsi performance data
5273 * @param atsi_count number of records in 'atsi'
5275 * @return GNUNET_OK to keep the connection open,
5276 * GNUNET_SYSERR to close it (signal serious error)
5279 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5280 const struct GNUNET_MessageHeader *message,
5281 const struct GNUNET_ATS_Information *atsi,
5282 unsigned int atsi_count)
5284 struct GNUNET_MESH_PathACK *msg;
5285 struct GNUNET_PeerIdentity id;
5286 struct MeshPeerInfo *peer_info;
5287 struct MeshPeerPath *p;
5288 struct MeshTunnel *t;
5290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5291 GNUNET_i2s (&my_full_id));
5292 msg = (struct GNUNET_MESH_PathACK *) message;
5293 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5296 /* TODO notify that we don't know the tunnel */
5297 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5299 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5303 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5305 peer_info = peer_info_get (&msg->peer_id);
5306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5307 GNUNET_i2s (&msg->peer_id));
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5311 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5314 "connect_by_string completed, stopping search\n");
5315 regex_cancel_search (t->regex_ctx);
5316 t->regex_ctx = NULL;
5319 /* Add paths to peers? */
5320 p = tree_get_path_to_peer (t->tree, peer_info->id);
5323 path_add_to_peers (p, GNUNET_YES);
5331 /* Message for us? */
5332 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5335 if (NULL == t->owner)
5337 GNUNET_break_op (0);
5340 if (NULL != t->dht_get_type)
5342 GNUNET_DHT_get_stop (t->dht_get_type);
5343 t->dht_get_type = NULL;
5345 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5347 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5348 send_client_peer_connected (t, peer_info->id);
5353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5354 " not for us, retransmitting...\n");
5355 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5356 peer_info = peer_info_get (&msg->oid);
5357 if (NULL == peer_info)
5359 /* If we know the tunnel, we should DEFINITELY know the peer */
5363 send_message (message, &id, t);
5369 * Functions to handle messages from core
5371 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5372 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5373 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5374 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5375 sizeof (struct GNUNET_MESH_PathBroken)},
5376 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5377 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5378 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5379 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5380 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5381 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5382 sizeof (struct GNUNET_MESH_ACK)},
5383 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5384 sizeof (struct GNUNET_MESH_PathACK)},
5390 /******************************************************************************/
5391 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5392 /******************************************************************************/
5395 * deregister_app: iterator for removing each application registered by a client
5397 * @param cls closure
5398 * @param key the hash of the application id (used to access the hashmap)
5399 * @param value the value stored at the key (client)
5401 * @return GNUNET_OK on success
5404 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5406 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5407 GNUNET_break (GNUNET_YES ==
5408 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5414 * notify_client_connection_failure: notify a client that the connection to the
5415 * requested remote peer is not possible (for instance, no route found)
5416 * Function called when the socket is ready to queue more data. "buf" will be
5417 * NULL and "size" zero if the socket was closed for writing in the meantime.
5419 * @param cls closure
5420 * @param size number of bytes available in buf
5421 * @param buf where the callee should write the message
5422 * @return number of bytes written to buf
5425 notify_client_connection_failure (void *cls, size_t size, void *buf)
5428 struct MeshPeerInfo *peer_info;
5429 struct GNUNET_MESH_PeerControl *msg;
5430 struct GNUNET_PeerIdentity id;
5432 if (0 == size && NULL == buf)
5434 // TODO retry? cancel?
5438 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5439 peer_info = (struct MeshPeerInfo *) cls;
5440 msg = (struct GNUNET_MESH_PeerControl *) buf;
5441 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5442 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5443 // msg->tunnel_id = htonl(peer_info->t->tid);
5444 GNUNET_PEER_resolve (peer_info->id, &id);
5445 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5453 * Send keepalive packets for a peer
5455 * @param cls Closure (tunnel for which to send the keepalive).
5456 * @param tc Notification context.
5458 * TODO: implement explicit multicast keepalive?
5461 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5463 struct MeshTunnel *t = cls;
5464 struct GNUNET_MessageHeader *payload;
5465 struct GNUNET_MESH_Multicast *msg;
5467 sizeof (struct GNUNET_MESH_Multicast) +
5468 sizeof (struct GNUNET_MessageHeader);
5471 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5475 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5478 "sending keepalive for tunnel %d\n", t->id.tid);
5480 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5481 msg->header.size = htons (size);
5482 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5483 msg->oid = my_full_id;
5484 msg->tid = htonl (t->id.tid);
5485 msg->ttl = htonl (default_ttl);
5486 msg->pid = htonl (t->fwd_pid + 1);
5488 payload = (struct GNUNET_MessageHeader *) &msg[1];
5489 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5490 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5491 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5493 t->path_refresh_task =
5494 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5500 * Function to process paths received for a new peer addition. The recorded
5501 * paths form the initial tunnel, which can be optimized later.
5502 * Called on each result obtained for the DHT search.
5504 * @param cls closure
5505 * @param exp when will this value expire
5506 * @param key key of the result
5507 * @param get_path path of the get request
5508 * @param get_path_length lenght of get_path
5509 * @param put_path path of the put request
5510 * @param put_path_length length of the put_path
5511 * @param type type of the result
5512 * @param size number of bytes in data
5513 * @param data pointer to the result data
5515 * TODO: re-issue the request after certain time? cancel after X results?
5518 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5519 const struct GNUNET_HashCode * key,
5520 const struct GNUNET_PeerIdentity *get_path,
5521 unsigned int get_path_length,
5522 const struct GNUNET_PeerIdentity *put_path,
5523 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5524 size_t size, const void *data)
5526 struct MeshPathInfo *path_info = cls;
5527 struct MeshPeerPath *p;
5528 struct GNUNET_PeerIdentity pi;
5531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5532 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5535 p = path_build_from_dht (get_path, get_path_length, put_path,
5537 path_add_to_peers (p, GNUNET_NO);
5539 for (i = 0; i < path_info->peer->ntunnels; i++)
5541 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5542 peer_info_connect (path_info->peer, path_info->t);
5550 * Function to process paths received for a new peer addition. The recorded
5551 * paths form the initial tunnel, which can be optimized later.
5552 * Called on each result obtained for the DHT search.
5554 * @param cls closure
5555 * @param exp when will this value expire
5556 * @param key key of the result
5557 * @param get_path path of the get request
5558 * @param get_path_length lenght of get_path
5559 * @param put_path path of the put request
5560 * @param put_path_length length of the put_path
5561 * @param type type of the result
5562 * @param size number of bytes in data
5563 * @param data pointer to the result data
5566 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5567 const struct GNUNET_HashCode * key,
5568 const struct GNUNET_PeerIdentity *get_path,
5569 unsigned int get_path_length,
5570 const struct GNUNET_PeerIdentity *put_path,
5571 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5572 size_t size, const void *data)
5574 const struct PBlock *pb = data;
5575 const struct GNUNET_PeerIdentity *pi = &pb->id;
5576 struct MeshTunnel *t = cls;
5577 struct MeshPeerInfo *peer_info;
5578 struct MeshPeerPath *p;
5580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5581 if (size != sizeof (struct PBlock))
5583 GNUNET_break_op (0);
5586 if (ntohl(pb->type) != t->type)
5588 GNUNET_break_op (0);
5591 GNUNET_assert (NULL != t->owner);
5592 peer_info = peer_info_get (pi);
5593 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5595 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5597 p = path_build_from_dht (get_path, get_path_length, put_path,
5599 path_add_to_peers (p, GNUNET_NO);
5601 tunnel_add_peer (t, peer_info);
5602 peer_info_connect (peer_info, t);
5607 * Function to process DHT string to regex matching.
5608 * Called on each result obtained for the DHT search.
5610 * @param cls closure (search context)
5611 * @param exp when will this value expire
5612 * @param key key of the result
5613 * @param get_path path of the get request (not used)
5614 * @param get_path_length lenght of get_path (not used)
5615 * @param put_path path of the put request (not used)
5616 * @param put_path_length length of the put_path (not used)
5617 * @param type type of the result
5618 * @param size number of bytes in data
5619 * @param data pointer to the result data
5622 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5623 const struct GNUNET_HashCode * key,
5624 const struct GNUNET_PeerIdentity *get_path,
5625 unsigned int get_path_length,
5626 const struct GNUNET_PeerIdentity *put_path,
5627 unsigned int put_path_length,
5628 enum GNUNET_BLOCK_Type type,
5629 size_t size, const void *data)
5631 const struct MeshRegexAccept *block = data;
5632 struct MeshRegexSearchContext *ctx = cls;
5633 struct MeshRegexSearchInfo *info = ctx->info;
5634 struct MeshPeerPath *p;
5635 struct MeshPeerInfo *peer_info;
5637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5640 peer_info = peer_info_get(&block->id);
5641 p = path_build_from_dht (get_path, get_path_length, put_path,
5643 path_add_to_peers (p, GNUNET_NO);
5646 tunnel_add_peer (info->t, peer_info);
5647 peer_info_connect (peer_info, info->t);
5648 if (0 == info->peer)
5650 info->peer = peer_info->id;
5654 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5657 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5658 ®ex_connect_timeout,
5666 * Function to process DHT string to regex matching.
5667 * Called on each result obtained for the DHT search.
5669 * @param cls closure (search context)
5670 * @param exp when will this value expire
5671 * @param key key of the result
5672 * @param get_path path of the get request (not used)
5673 * @param get_path_length lenght of get_path (not used)
5674 * @param put_path path of the put request (not used)
5675 * @param put_path_length length of the put_path (not used)
5676 * @param type type of the result
5677 * @param size number of bytes in data
5678 * @param data pointer to the result data
5680 * TODO: re-issue the request after certain time? cancel after X results?
5683 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5684 const struct GNUNET_HashCode * key,
5685 const struct GNUNET_PeerIdentity *get_path,
5686 unsigned int get_path_length,
5687 const struct GNUNET_PeerIdentity *put_path,
5688 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5689 size_t size, const void *data)
5691 const struct MeshRegexBlock *block = data;
5692 struct MeshRegexSearchContext *ctx = cls;
5693 struct MeshRegexSearchInfo *info = ctx->info;
5697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5698 "DHT GET STRING RETURNED RESULTS\n");
5699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5700 " key: %s\n", GNUNET_h2s (key));
5702 copy = GNUNET_malloc (size);
5703 memcpy (copy, data, size);
5704 GNUNET_break (GNUNET_OK ==
5705 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5706 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5707 len = ntohl (block->n_proof);
5709 char proof[len + 1];
5711 memcpy (proof, &block[1], len);
5713 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5715 GNUNET_break_op (0);
5719 len = strlen (info->description);
5720 if (len == ctx->position) // String processed
5722 if (GNUNET_YES == ntohl (block->accepting))
5724 regex_find_path(key, ctx);
5728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5729 // FIXME REGEX this block not successful, wait for more? start timeout?
5733 GNUNET_break (GNUNET_OK ==
5734 GNUNET_MESH_regex_block_iterate (block, size,
5735 ®ex_edge_iterator, ctx));
5739 /******************************************************************************/
5740 /********************* MESH LOCAL HANDLES **************************/
5741 /******************************************************************************/
5745 * Handler for client disconnection
5747 * @param cls closure
5748 * @param client identification of the client; NULL
5749 * for the last call when the server is destroyed
5752 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5754 struct MeshClient *c;
5755 struct MeshClient *next;
5758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5767 if (c->handle != client)
5769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5775 GNUNET_SERVER_client_drop (c->handle);
5776 c->shutting_down = GNUNET_YES;
5777 GNUNET_assert (NULL != c->own_tunnels);
5778 GNUNET_assert (NULL != c->incoming_tunnels);
5779 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5780 &tunnel_destroy_iterator, c);
5781 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5782 &tunnel_destroy_iterator, c);
5783 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5784 &tunnel_destroy_iterator, c);
5785 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5786 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5787 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5789 /* deregister clients applications */
5790 if (NULL != c->apps)
5792 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5793 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5795 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5796 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5798 GNUNET_SCHEDULER_cancel (announce_applications_task);
5799 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5801 if (NULL != c->types)
5802 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5803 for (i = 0; i < c->n_regex; i++)
5805 GNUNET_free (c->regexes[i]);
5807 GNUNET_free_non_null (c->regexes);
5808 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5809 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5811 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5814 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5823 * Handler for new clients
5825 * @param cls closure
5826 * @param client identification of the client
5827 * @param message the actual message, which includes messages the client wants
5830 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5831 const struct GNUNET_MessageHeader *message)
5833 struct GNUNET_MESH_ClientConnect *cc_msg;
5834 struct MeshClient *c;
5835 GNUNET_MESH_ApplicationType *a;
5842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5843 /* Check data sanity */
5844 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5845 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5846 ntypes = ntohs (cc_msg->types);
5847 napps = ntohs (cc_msg->applications);
5849 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5856 /* Create new client structure */
5857 c = GNUNET_malloc (sizeof (struct MeshClient));
5858 c->id = next_client_id++;
5859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5861 GNUNET_SERVER_client_keep (client);
5862 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5865 GNUNET_MESH_ApplicationType at;
5866 struct GNUNET_HashCode hc;
5868 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5869 for (i = 0; i < napps; i++)
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5873 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5874 /* store in clients hashmap */
5875 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5876 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5877 /* store in global hashmap, for announcements */
5878 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5881 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5882 announce_applications_task =
5883 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5889 struct GNUNET_HashCode hc;
5891 t = (uint16_t *) & a[napps];
5892 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5893 for (i = 0; i < ntypes; i++)
5896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5897 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5899 /* store in clients hashmap */
5900 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5901 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5902 /* store in global hashmap */
5903 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5904 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5908 " client has %u+%u subscriptions\n", napps, ntypes);
5910 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5911 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5912 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5913 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5914 GNUNET_SERVER_notification_context_add (nc, client);
5915 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5917 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5923 * Handler for clients announcing available services by a regular expression.
5925 * @param cls closure
5926 * @param client identification of the client
5927 * @param message the actual message, which includes messages the client wants
5930 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5931 const struct GNUNET_MessageHeader *message)
5933 struct MeshClient *c;
5937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5939 /* Sanity check for client registration */
5940 if (NULL == (c = client_get (client)))
5943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5948 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5949 regex = GNUNET_malloc (len + 1);
5950 memcpy (regex, &message[1], len);
5952 GNUNET_array_append (c->regexes, c->n_regex, regex);
5953 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5955 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5961 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5967 * Handler for requests of new tunnels
5969 * @param cls closure
5970 * @param client identification of the client
5971 * @param message the actual message
5974 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5975 const struct GNUNET_MessageHeader *message)
5977 struct GNUNET_MESH_TunnelMessage *t_msg;
5978 struct MeshTunnel *t;
5979 struct MeshClient *c;
5980 MESH_TunnelNumber tid;
5982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5984 /* Sanity check for client registration */
5985 if (NULL == (c = client_get (client)))
5988 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5993 /* Message sanity check */
5994 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5997 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6001 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6002 /* Sanity check for tunnel numbering */
6003 tid = ntohl (t_msg->tunnel_id);
6004 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6010 /* Sanity check for duplicate tunnel IDs */
6011 if (NULL != tunnel_get_by_local_id (c, tid))
6014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6018 while (NULL != tunnel_get_by_pi (myid, next_tid))
6019 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6020 t = tunnel_new (myid, next_tid++, c, tid);
6023 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6028 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6030 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6031 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6034 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6040 * Handler for requests of deleting tunnels
6042 * @param cls closure
6043 * @param client identification of the client
6044 * @param message the actual message
6047 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6048 const struct GNUNET_MessageHeader *message)
6050 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6051 struct MeshClient *c;
6052 struct MeshTunnel *t;
6053 MESH_TunnelNumber tid;
6055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6056 "Got a DESTROY TUNNEL from client!\n");
6058 /* Sanity check for client registration */
6059 if (NULL == (c = client_get (client)))
6062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6067 /* Message sanity check */
6068 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6075 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6077 /* Retrieve tunnel */
6078 tid = ntohl (tunnel_msg->tunnel_id);
6079 t = tunnel_get_by_local_id(c, tid);
6082 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6084 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6087 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6089 client_ignore_tunnel (c, t);
6091 // TODO: when to destroy incoming tunnel?
6092 if (t->nclients == 0)
6094 GNUNET_assert (GNUNET_YES ==
6095 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6097 GNUNET_assert (GNUNET_YES ==
6098 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6099 &my_full_id.hashPubKey,
6103 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6106 send_client_tunnel_disconnect(t, c);
6107 client_delete_tunnel(c, t);
6109 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6111 tunnel_send_destroy (t);
6112 t->destroy = GNUNET_YES;
6113 // The tunnel will be destroyed when the last message is transmitted.
6114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6120 * Handler for requests of seeting tunnel's speed.
6122 * @param cls Closure (unused).
6123 * @param client Identification of the client.
6124 * @param message The actual message.
6127 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6128 const struct GNUNET_MessageHeader *message)
6130 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6131 struct MeshClient *c;
6132 struct MeshTunnel *t;
6133 MESH_TunnelNumber tid;
6135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6136 "Got a SPEED request from client!\n");
6138 /* Sanity check for client registration */
6139 if (NULL == (c = client_get (client)))
6142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6148 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6150 /* Retrieve tunnel */
6151 tid = ntohl (tunnel_msg->tunnel_id);
6152 t = tunnel_get_by_local_id(c, tid);
6155 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6161 switch (ntohs(message->type))
6163 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6164 t->speed_min = GNUNET_YES;
6166 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6167 t->speed_min = GNUNET_NO;
6172 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6177 * Handler for requests of seeting tunnel's buffering policy.
6179 * @param cls Closure (unused).
6180 * @param client Identification of the client.
6181 * @param message The actual message.
6184 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6185 const struct GNUNET_MessageHeader *message)
6187 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6188 struct MeshClient *c;
6189 struct MeshTunnel *t;
6190 MESH_TunnelNumber tid;
6192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6193 "Got a BUFFER request from client!\n");
6195 /* Sanity check for client registration */
6196 if (NULL == (c = client_get (client)))
6199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6204 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6206 /* Retrieve tunnel */
6207 tid = ntohl (tunnel_msg->tunnel_id);
6208 t = tunnel_get_by_local_id(c, tid);
6211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6217 switch (ntohs(message->type))
6219 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6220 t->nobuffer = GNUNET_NO;
6222 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6223 t->nobuffer = GNUNET_YES;
6229 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6234 * Handler for connection requests to new peers
6236 * @param cls closure
6237 * @param client identification of the client
6238 * @param message the actual message (PeerControl)
6241 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6242 const struct GNUNET_MessageHeader *message)
6244 struct GNUNET_MESH_PeerControl *peer_msg;
6245 struct MeshPeerInfo *peer_info;
6246 struct MeshClient *c;
6247 struct MeshTunnel *t;
6248 MESH_TunnelNumber tid;
6250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6251 /* Sanity check for client registration */
6252 if (NULL == (c = client_get (client)))
6255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6260 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6262 /* Sanity check for message size */
6263 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6270 /* Tunnel exists? */
6271 tid = ntohl (peer_msg->tunnel_id);
6272 t = tunnel_get_by_local_id (c, tid);
6276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6280 /* Does client own tunnel? */
6281 if (t->owner->handle != client)
6284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6288 GNUNET_i2s (&peer_msg->peer));
6289 peer_info = peer_info_get (&peer_msg->peer);
6291 tunnel_add_peer (t, peer_info);
6292 peer_info_connect (peer_info, t);
6294 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6300 * Handler for disconnection requests of peers in a tunnel
6302 * @param cls closure
6303 * @param client identification of the client
6304 * @param message the actual message (PeerControl)
6307 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6308 const struct GNUNET_MessageHeader *message)
6310 struct GNUNET_MESH_PeerControl *peer_msg;
6311 struct MeshPeerInfo *peer_info;
6312 struct MeshClient *c;
6313 struct MeshTunnel *t;
6314 MESH_TunnelNumber tid;
6316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6317 /* Sanity check for client registration */
6318 if (NULL == (c = client_get (client)))
6321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6326 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6328 /* Sanity check for message size */
6329 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6336 /* Tunnel exists? */
6337 tid = ntohl (peer_msg->tunnel_id);
6338 t = tunnel_get_by_local_id (c, tid);
6342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6347 /* Does client own tunnel? */
6348 if (t->owner->handle != client)
6351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6356 GNUNET_i2s (&peer_msg->peer));
6357 /* Is the peer in the tunnel? */
6359 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6360 if (NULL == peer_info)
6363 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6367 /* Ok, delete peer from tunnel */
6368 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6369 &peer_msg->peer.hashPubKey);
6371 send_destroy_path (t, peer_info->id);
6372 tunnel_delete_peer (t, peer_info->id);
6373 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6378 * Handler for blacklist requests of peers in a tunnel
6380 * @param cls closure
6381 * @param client identification of the client
6382 * @param message the actual message (PeerControl)
6384 * FIXME implement DHT block bloomfilter
6387 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6388 const struct GNUNET_MessageHeader *message)
6390 struct GNUNET_MESH_PeerControl *peer_msg;
6391 struct MeshClient *c;
6392 struct MeshTunnel *t;
6393 MESH_TunnelNumber tid;
6395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6396 /* Sanity check for client registration */
6397 if (NULL == (c = client_get (client)))
6400 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6405 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6407 /* Sanity check for message size */
6408 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6415 /* Tunnel exists? */
6416 tid = ntohl (peer_msg->tunnel_id);
6417 t = tunnel_get_by_local_id (c, tid);
6421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6426 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6427 GNUNET_PEER_intern(&peer_msg->peer));
6432 * Handler for unblacklist requests of peers in a tunnel
6434 * @param cls closure
6435 * @param client identification of the client
6436 * @param message the actual message (PeerControl)
6439 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6440 const struct GNUNET_MessageHeader *message)
6442 struct GNUNET_MESH_PeerControl *peer_msg;
6443 struct MeshClient *c;
6444 struct MeshTunnel *t;
6445 MESH_TunnelNumber tid;
6449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6450 /* Sanity check for client registration */
6451 if (NULL == (c = client_get (client)))
6454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6459 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6461 /* Sanity check for message size */
6462 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6469 /* Tunnel exists? */
6470 tid = ntohl (peer_msg->tunnel_id);
6471 t = tunnel_get_by_local_id (c, tid);
6475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6480 /* if peer is not known, complain */
6481 pid = GNUNET_PEER_search (&peer_msg->peer);
6488 /* search and remove from list */
6489 for (i = 0; i < t->nblacklisted; i++)
6491 if (t->blacklisted[i] == pid)
6493 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6494 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6499 /* if peer hasn't been blacklisted, complain */
6505 * Handler for connection requests to new peers by type
6507 * @param cls closure
6508 * @param client identification of the client
6509 * @param message the actual message (ConnectPeerByType)
6512 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6513 const struct GNUNET_MessageHeader *message)
6515 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6516 struct MeshClient *c;
6517 struct MeshTunnel *t;
6518 struct GNUNET_HashCode hash;
6519 MESH_TunnelNumber tid;
6521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6522 /* Sanity check for client registration */
6523 if (NULL == (c = client_get (client)))
6526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6531 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6533 /* Sanity check for message size */
6534 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6535 ntohs (connect_msg->header.size))
6538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6542 /* Tunnel exists? */
6543 tid = ntohl (connect_msg->tunnel_id);
6544 t = tunnel_get_by_local_id (c, tid);
6548 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6552 /* Does client own tunnel? */
6553 if (t->owner->handle != client)
6556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6560 /* Do WE have the service? */
6561 t->type = ntohl (connect_msg->type);
6562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6563 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6564 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6567 /* Yes! Fast forward, add ourselves to the tunnel and send the
6568 * good news to the client, and alert the destination client of
6569 * an incoming tunnel.
6571 * FIXME send a path create to self, avoid code duplication
6573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6574 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6575 peer_info_get (&my_full_id),
6576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6579 send_client_peer_connected (t, myid);
6580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6581 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6583 t->local_tid_dest = next_local_tid++;
6584 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6585 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6586 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6590 /* Ok, lets find a peer offering the service */
6591 if (NULL != t->dht_get_type)
6593 GNUNET_DHT_get_stop (t->dht_get_type);
6595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6596 GNUNET_h2s (&hash));
6598 GNUNET_DHT_get_start (dht_handle,
6599 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6601 dht_replication_level,
6602 GNUNET_DHT_RO_RECORD_ROUTE |
6603 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6605 &dht_get_type_handler, t);
6607 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6613 * Handler for connection requests to new peers by a string service description.
6615 * @param cls closure
6616 * @param client identification of the client
6617 * @param message the actual message, which includes messages the client wants
6620 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6621 const struct GNUNET_MessageHeader *message)
6623 struct GNUNET_MESH_ConnectPeerByString *msg;
6624 struct MeshRegexSearchContext *ctx;
6625 struct MeshRegexSearchInfo *info;
6626 struct GNUNET_DHT_GetHandle *get_h;
6627 struct GNUNET_HashCode key;
6628 struct MeshTunnel *t;
6629 struct MeshClient *c;
6630 MESH_TunnelNumber tid;
6634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6635 "Connect by string started\n");
6636 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6637 size = htons (message->size);
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 /* Message size sanity check */
6649 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6652 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6656 /* Tunnel exists? */
6657 tid = ntohl (msg->tunnel_id);
6658 t = tunnel_get_by_local_id (c, tid);
6662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6666 /* Does client own tunnel? */
6667 if (t->owner->handle != client)
6670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6675 " on tunnel %s [%u]\n",
6676 GNUNET_i2s(&my_full_id),
6679 /* Only one connect_by_string allowed at the same time! */
6680 /* FIXME: allow more, return handle at api level to cancel, document */
6681 if (NULL != t->regex_ctx)
6684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6688 /* Find string itself */
6689 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6690 string = (const char *) &msg[1];
6692 /* Initialize context */
6693 size = GNUNET_REGEX_get_first_key(string, len, &key);
6694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6695 " consumed %u bits out of %u\n", size, len);
6696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6697 " looking for %s\n", GNUNET_h2s (&key));
6699 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6701 info->description = GNUNET_malloc (len + 1);
6702 memcpy (info->description, string, len);
6703 info->description[len] = '\0';
6704 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6705 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6708 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6709 ctx->position = size;
6713 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6715 /* Start search in DHT */
6716 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6717 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6718 &key, /* key to search */
6719 dht_replication_level, /* replication level */
6720 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6721 NULL, /* xquery */ // FIXME BLOOMFILTER
6722 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6723 &dht_get_string_handler, ctx);
6725 GNUNET_break (GNUNET_OK ==
6726 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6731 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6737 * Handler for client traffic directed to one peer
6739 * @param cls closure
6740 * @param client identification of the client
6741 * @param message the actual message
6744 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6745 const struct GNUNET_MessageHeader *message)
6747 struct MeshClient *c;
6748 struct MeshTunnel *t;
6749 struct MeshPeerInfo *pi;
6750 struct GNUNET_MESH_Unicast *data_msg;
6751 MESH_TunnelNumber tid;
6754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6755 "Got a unicast request from a client!\n");
6757 /* Sanity check for client registration */
6758 if (NULL == (c = client_get (client)))
6761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6766 data_msg = (struct GNUNET_MESH_Unicast *) message;
6768 /* Sanity check for message size */
6769 size = ntohs (message->size);
6770 if (sizeof (struct GNUNET_MESH_Unicast) +
6771 sizeof (struct GNUNET_MessageHeader) > size)
6774 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6778 /* Tunnel exists? */
6779 tid = ntohl (data_msg->tid);
6780 t = tunnel_get_by_local_id (c, tid);
6784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6788 /* Is it a local tunnel? Then, does client own the tunnel? */
6789 if (t->owner->handle != client)
6792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6796 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6797 &data_msg->destination.hashPubKey);
6798 /* Is the selected peer in the tunnel? */
6802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6806 /* PID should be as expected */
6807 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6810 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6811 "Unicast PID, expected %u, got %u\n",
6812 t->fwd_pid + 1, ntohl (data_msg->pid));
6813 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6817 /* Ok, everything is correct, send the message
6818 * (pretend we got it from a mesh peer)
6821 /* Work around const limitation */
6822 char buf[ntohs (message->size)] GNUNET_ALIGN;
6823 struct GNUNET_MESH_Unicast *copy;
6825 copy = (struct GNUNET_MESH_Unicast *) buf;
6826 memcpy (buf, data_msg, size);
6827 copy->oid = my_full_id;
6828 copy->tid = htonl (t->id.tid);
6829 copy->ttl = htonl (default_ttl);
6830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6831 " calling generic handler...\n");
6832 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6835 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6842 * Handler for client traffic directed to the origin
6844 * @param cls closure
6845 * @param client identification of the client
6846 * @param message the actual message
6849 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6850 const struct GNUNET_MessageHeader *message)
6852 struct GNUNET_MESH_ToOrigin *data_msg;
6853 struct MeshTunnelClientInfo *clinfo;
6854 struct MeshClient *c;
6855 struct MeshTunnel *t;
6856 MESH_TunnelNumber tid;
6859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6860 "Got a ToOrigin request from a client!\n");
6861 /* Sanity check for client registration */
6862 if (NULL == (c = client_get (client)))
6865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6870 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6872 /* Sanity check for message size */
6873 size = ntohs (message->size);
6874 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6875 sizeof (struct GNUNET_MessageHeader) > size)
6878 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6882 /* Tunnel exists? */
6883 tid = ntohl (data_msg->tid);
6884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6885 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6891 t = tunnel_get_by_local_id (c, tid);
6895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6899 /* It should be sent by someone who has this as incoming tunnel. */
6900 if (GNUNET_NO == client_knows_tunnel (c, t))
6903 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6907 /* PID should be as expected */
6908 clinfo = tunnel_get_client_fc (t, c);
6909 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6912 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6913 "To Origin PID, expected %u, got %u\n",
6914 clinfo->bck_pid + 1,
6915 ntohl (data_msg->pid));
6916 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6920 /* Ok, everything is correct, send the message
6921 * (pretend we got it from a mesh peer)
6925 char buf[ntohs (message->size)] GNUNET_ALIGN;
6926 struct GNUNET_MESH_ToOrigin *copy;
6928 /* Work around const limitation */
6929 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6930 memcpy (buf, data_msg, size);
6931 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6932 copy->tid = htonl (t->id.tid);
6933 copy->ttl = htonl (default_ttl);
6934 if (ntohl (copy->pid) != (t->bck_pid + 1))
6937 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6938 "To Origin PID, expected %u, got %u\n",
6944 copy->sender = my_full_id;
6945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6946 " calling generic handler...\n");
6947 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6949 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6956 * Handler for client traffic directed to all peers in a tunnel
6958 * @param cls closure
6959 * @param client identification of the client
6960 * @param message the actual message
6963 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6964 const struct GNUNET_MessageHeader *message)
6966 struct MeshClient *c;
6967 struct MeshTunnel *t;
6968 struct GNUNET_MESH_Multicast *data_msg;
6969 MESH_TunnelNumber tid;
6971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6972 "Got a multicast request from a client!\n");
6974 /* Sanity check for client registration */
6975 if (NULL == (c = client_get (client)))
6978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6983 data_msg = (struct GNUNET_MESH_Multicast *) message;
6985 /* Sanity check for message size */
6986 if (sizeof (struct GNUNET_MESH_Multicast) +
6987 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6990 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6994 /* Tunnel exists? */
6995 tid = ntohl (data_msg->tid);
6996 t = tunnel_get_by_local_id (c, tid);
7000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7004 /* Does client own tunnel? */
7005 if (t->owner->handle != client)
7008 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7012 /* PID should be as expected */
7013 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7016 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7017 "Multicast PID, expected %u, got %u\n",
7018 t->fwd_pid + 1, ntohl (data_msg->pid));
7019 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7024 char buf[ntohs (message->size)] GNUNET_ALIGN;
7025 struct GNUNET_MESH_Multicast *copy;
7027 copy = (struct GNUNET_MESH_Multicast *) buf;
7028 memcpy (buf, message, ntohs (message->size));
7029 copy->oid = my_full_id;
7030 copy->tid = htonl (t->id.tid);
7031 copy->ttl = htonl (default_ttl);
7032 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7034 " calling generic handler...\n");
7035 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7038 /* receive done gets called when last copy is sent to a neighbor */
7044 * Handler for client's ACKs for payload traffic.
7046 * @param cls Closure (unused).
7047 * @param client Identification of the client.
7048 * @param message The actual message.
7051 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7052 const struct GNUNET_MessageHeader *message)
7054 struct GNUNET_MESH_LocalAck *msg;
7055 struct MeshTunnel *t;
7056 struct MeshClient *c;
7057 MESH_TunnelNumber tid;
7060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7061 /* Sanity check for client registration */
7062 if (NULL == (c = client_get (client)))
7065 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7070 msg = (struct GNUNET_MESH_LocalAck *) message;
7072 /* Tunnel exists? */
7073 tid = ntohl (msg->tunnel_id);
7074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7075 t = tunnel_get_by_local_id (c, tid);
7078 GNUNET_break (0); // FIXME fc
7079 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7083 ack = ntohl (msg->max_pid);
7084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7086 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7087 if (NULL != t->owner && t->owner->handle == client)
7089 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7091 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7095 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7096 tunnel_set_client_fwd_ack (t, c, ack);
7097 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7100 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7106 * Functions to handle messages from clients
7108 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7109 {&handle_local_new_client, NULL,
7110 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7111 {&handle_local_announce_regex, NULL,
7112 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7113 {&handle_local_tunnel_create, NULL,
7114 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7115 sizeof (struct GNUNET_MESH_TunnelMessage)},
7116 {&handle_local_tunnel_destroy, NULL,
7117 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7118 sizeof (struct GNUNET_MESH_TunnelMessage)},
7119 {&handle_local_tunnel_speed, NULL,
7120 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7121 sizeof (struct GNUNET_MESH_TunnelMessage)},
7122 {&handle_local_tunnel_speed, NULL,
7123 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7124 sizeof (struct GNUNET_MESH_TunnelMessage)},
7125 {&handle_local_tunnel_buffer, NULL,
7126 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7127 sizeof (struct GNUNET_MESH_TunnelMessage)},
7128 {&handle_local_tunnel_buffer, NULL,
7129 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7130 sizeof (struct GNUNET_MESH_TunnelMessage)},
7131 {&handle_local_connect_add, NULL,
7132 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7133 sizeof (struct GNUNET_MESH_PeerControl)},
7134 {&handle_local_connect_del, NULL,
7135 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7136 sizeof (struct GNUNET_MESH_PeerControl)},
7137 {&handle_local_blacklist, NULL,
7138 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7139 sizeof (struct GNUNET_MESH_PeerControl)},
7140 {&handle_local_unblacklist, NULL,
7141 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7142 sizeof (struct GNUNET_MESH_PeerControl)},
7143 {&handle_local_connect_by_type, NULL,
7144 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7145 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7146 {&handle_local_connect_by_string, NULL,
7147 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7148 {&handle_local_unicast, NULL,
7149 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7150 {&handle_local_to_origin, NULL,
7151 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7152 {&handle_local_multicast, NULL,
7153 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7154 {&handle_local_ack, NULL,
7155 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7156 sizeof (struct GNUNET_MESH_LocalAck)},
7162 * To be called on core init/fail.
7164 * @param cls service closure
7165 * @param server handle to the server for this service
7166 * @param identity the public identity of this peer
7169 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7170 const struct GNUNET_PeerIdentity *identity)
7173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7174 core_handle = server;
7175 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7179 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7181 GNUNET_abort(); // Try harder
7188 * Method called whenever a given peer connects.
7190 * @param cls closure
7191 * @param peer peer identity this notification is about
7192 * @param atsi performance data for the connection
7193 * @param atsi_count number of records in 'atsi'
7196 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7197 const struct GNUNET_ATS_Information *atsi,
7198 unsigned int atsi_count)
7200 struct MeshPeerInfo *peer_info;
7201 struct MeshPeerPath *path;
7203 DEBUG_CONN ("Peer connected\n");
7204 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7205 peer_info = peer_info_get (peer);
7206 if (myid == peer_info->id)
7208 DEBUG_CONN (" (self)\n");
7213 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7215 path = path_new (2);
7216 path->peers[0] = myid;
7217 path->peers[1] = peer_info->id;
7218 GNUNET_PEER_change_rc (myid, 1);
7219 GNUNET_PEER_change_rc (peer_info->id, 1);
7220 peer_info_add_path (peer_info, path, GNUNET_YES);
7221 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7227 * Method called whenever a peer disconnects.
7229 * @param cls closure
7230 * @param peer peer identity this notification is about
7233 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7235 struct MeshPeerInfo *pi;
7236 struct MeshPeerQueue *q;
7237 struct MeshPeerQueue *n;
7239 DEBUG_CONN ("Peer disconnected\n");
7240 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7252 /* try to reroute this traffic instead */
7253 queue_destroy(q, GNUNET_YES);
7257 peer_info_remove_path (pi, pi->id, myid);
7260 DEBUG_CONN (" (self)\n");
7262 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7267 /******************************************************************************/
7268 /************************ MAIN FUNCTIONS ****************************/
7269 /******************************************************************************/
7272 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7274 * @param cls closure
7275 * @param key current key code
7276 * @param value value in the hash map
7277 * @return GNUNET_YES if we should continue to iterate,
7281 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7283 struct MeshTunnel *t = value;
7290 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7292 * @param cls closure
7293 * @param key current key code
7294 * @param value value in the hash map
7295 * @return GNUNET_YES if we should continue to iterate,
7299 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7301 struct MeshPeerInfo *p = value;
7302 struct MeshPeerQueue *q;
7303 struct MeshPeerQueue *n;
7311 queue_destroy(q, GNUNET_YES);
7315 peer_info_destroy (p);
7320 * Task run during shutdown.
7326 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7330 if (core_handle != NULL)
7332 GNUNET_CORE_disconnect (core_handle);
7335 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7336 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7337 if (dht_handle != NULL)
7339 GNUNET_DHT_disconnect (dht_handle);
7344 GNUNET_SERVER_notification_context_destroy (nc);
7347 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7349 GNUNET_SCHEDULER_cancel (announce_id_task);
7350 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7356 * Process mesh requests.
7358 * @param cls closure
7359 * @param server the initialized server
7360 * @param c configuration to use
7363 run (void *cls, struct GNUNET_SERVER_Handle *server,
7364 const struct GNUNET_CONFIGURATION_Handle *c)
7366 struct MeshPeerInfo *peer;
7367 struct MeshPeerPath *p;
7370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7371 server_handle = server;
7372 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7373 NULL, /* Closure passed to MESH functions */
7374 &core_init, /* Call core_init once connected */
7375 &core_connect, /* Handle connects */
7376 &core_disconnect, /* remove peers on disconnects */
7377 NULL, /* Don't notify about all incoming messages */
7378 GNUNET_NO, /* For header only in notification */
7379 NULL, /* Don't notify about all outbound messages */
7380 GNUNET_NO, /* For header-only out notification */
7381 core_handlers); /* Register these handlers */
7383 if (core_handle == NULL)
7386 GNUNET_SCHEDULER_shutdown ();
7391 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7396 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7398 GNUNET_SCHEDULER_shutdown ();
7403 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7404 &refresh_path_time))
7406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7408 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7409 "refresh path time");
7410 GNUNET_SCHEDULER_shutdown ();
7415 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7416 &app_announce_time))
7418 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7420 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7421 "app announce time");
7422 GNUNET_SCHEDULER_shutdown ();
7427 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7432 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7433 "id announce time");
7434 GNUNET_SCHEDULER_shutdown ();
7439 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7440 &unacknowledged_wait_time))
7442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7444 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7445 "unacknowledged wait time");
7446 GNUNET_SCHEDULER_shutdown ();
7451 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7456 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7458 GNUNET_SCHEDULER_shutdown ();
7463 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7468 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7470 GNUNET_SCHEDULER_shutdown ();
7475 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7480 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7482 GNUNET_SCHEDULER_shutdown ();
7487 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7490 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7492 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7498 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7499 &dht_replication_level))
7501 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7503 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7504 "dht replication level", 10);
7505 dht_replication_level = 10;
7509 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7510 GNUNET_free (keyfile);
7511 if (my_private_key == NULL)
7513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7514 _("Mesh service could not access hostkey. Exiting.\n"));
7515 GNUNET_SCHEDULER_shutdown ();
7518 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7519 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7520 &my_full_id.hashPubKey);
7521 myid = GNUNET_PEER_intern (&my_full_id);
7523 // transport_handle = GNUNET_TRANSPORT_connect(c,
7530 dht_handle = GNUNET_DHT_connect (c, 64);
7531 if (dht_handle == NULL)
7536 stats = GNUNET_STATISTICS_create ("mesh", c);
7540 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7542 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7543 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7544 peers = GNUNET_CONTAINER_multihashmap_create (32);
7545 applications = GNUNET_CONTAINER_multihashmap_create (32);
7546 types = GNUNET_CONTAINER_multihashmap_create (32);
7548 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7549 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7550 GNUNET_SERVER_disconnect_notify (server_handle,
7551 &handle_local_client_disconnect, NULL);
7555 clients_tail = NULL;
7558 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7559 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7561 /* Create a peer_info for the local peer */
7562 peer = peer_info_get (&my_full_id);
7565 GNUNET_PEER_change_rc (myid, 1);
7566 peer_info_add_path (peer, p, GNUNET_YES);
7568 /* Scheduled the task to clean up when shutdown is called */
7569 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7576 * The main function for the mesh service.
7578 * @param argc number of arguments from the command line
7579 * @param argv command line arguments
7580 * @return 0 ok, 1 on error
7583 main (int argc, char *const *argv)
7587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7590 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");