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 __LINUX__ && 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 \
93 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
95 #define INTERVAL_START
100 /******************************************************************************/
101 /************************ DATA STRUCTURES ****************************/
102 /******************************************************************************/
104 /** FWD declaration */
110 * Struct representing a piece of data being sent to other peers
114 /** Tunnel it belongs to. */
115 struct MeshTunnel *t;
117 /** In case of a multicast, task to allow a client to send more data if
118 * some neighbor is too slow. */
119 GNUNET_SCHEDULER_TaskIdentifier *task;
121 /** How many remaining neighbors we need to send this to. */
122 unsigned int *reference_counter;
124 /** Size of the data. */
133 * Struct containing info about a queued transmission to this peer
140 struct MeshPeerQueue *next;
145 struct MeshPeerQueue *prev;
148 * Peer this transmission is directed to.
150 struct MeshPeerInfo *peer;
153 * Tunnel this message belongs to.
155 struct MeshTunnel *tunnel;
158 * Pointer to info stucture used as cls.
168 * Size of the message
175 * Struct containing all info possibly needed to build a package when called
178 struct MeshTransmissionDescriptor
180 /** ID of the tunnel this packet travels in */
181 struct MESH_TunnelID *origin;
183 /** Who was this message being sent to */
184 struct MeshPeerInfo *peer;
186 /** Ultimate destination of the packet */
187 GNUNET_PEER_Id destination;
189 /** Data descriptor */
190 struct MeshData* mesh_data;
195 * Struct containing all information regarding a given peer
205 * Last time we heard from this peer
207 struct GNUNET_TIME_Absolute last_contact;
210 * Number of attempts to reconnect so far
212 int n_reconnect_attempts;
215 * Paths to reach the peer, ordered by ascending hop count
217 struct MeshPeerPath *path_head;
220 * Paths to reach the peer, ordered by ascending hop count
222 struct MeshPeerPath *path_tail;
225 * Handle to stop the DHT search for a path to this peer
227 struct GNUNET_DHT_GetHandle *dhtget;
230 * Closure given to the DHT GET
232 struct MeshPathInfo *dhtgetcls;
235 * Array of tunnels this peer participates in
236 * (most probably a small amount, therefore not a hashmap)
237 * When the path to the peer changes, notify these tunnels to let them
238 * re-adjust their path trees.
240 struct MeshTunnel **tunnels;
243 * Number of tunnels this peers participates in
245 unsigned int ntunnels;
248 * Transmission queue to core DLL head
250 struct MeshPeerQueue *queue_head;
253 * Transmission queue to core DLL tail
255 struct MeshPeerQueue *queue_tail;
258 * How many messages are in the queue to this peer.
260 unsigned int queue_n;
263 * Handle to for queued transmissions
265 struct GNUNET_CORE_TransmitHandle *core_transmit;
270 * Globally unique tunnel identification (owner + number)
271 * DO NOT USE OVER THE NETWORK
276 * Node that owns the tunnel
281 * Tunnel number to differentiate all the tunnels owned by the node oid
282 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
284 MESH_TunnelNumber tid;
289 * Struct containing all information regarding a tunnel
290 * For an intermediate node the improtant info used will be:
291 * - id Tunnel unique identification
292 * - paths[0] To know where to send it next
293 * - metainfo: ready, speeds, accounting
300 struct MESH_TunnelID id;
303 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
305 MESH_TunnelNumber local_tid;
308 * Local tunnel number for local destination clients (incoming number)
309 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
312 MESH_TunnelNumber local_tid_dest;
315 * Is the speed on the tunnel limited to the slowest peer?
320 * Is the tunnel bufferless (minimum latency)?
325 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
330 * Packet ID of the last bck packet sent (unique counter per hop).
335 * SKIP value for this tunnel.
340 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
341 * Contains the Flow Control info: FWD ACK value received,
342 * last BCK ACK sent, PID and SKIP values.
344 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
347 * Last ACK sent towards the origin (for traffic towards leaf node).
349 uint32_t last_fwd_ack;
352 * BCK ACK value received from the hop towards the owner of the tunnel,
353 * (previous node / owner): up to what message PID can we sent back to him.
358 * How many messages are in the forward queue (towards leaves).
360 unsigned int fwd_queue_n;
363 * How many messages do we accept in the forward queue.
365 unsigned int fwd_queue_max;
368 * How many messages are in the backward queue (towards origin).
370 unsigned int bck_queue_n;
373 * How many messages do we accept in the backward queue.
375 unsigned int bck_queue_max;
378 * Last time the tunnel was used
380 struct GNUNET_TIME_Absolute timestamp;
383 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
384 * containing peers added by id or by type, not intermediate peers.
386 struct GNUNET_CONTAINER_MultiHashMap *peers;
389 * Number of peers that are connected and potentially ready to receive data
391 unsigned int peers_ready;
394 * Number of peers that have been added to the tunnel
396 unsigned int peers_total;
399 * Client owner of the tunnel, if any
401 struct MeshClient *owner;
404 * Clients that have been informed about and want to stay in the tunnel.
406 struct MeshClient **clients;
409 * Flow control info for each client.
411 struct MeshTunnelClientInfo *clients_fc;
414 * Number of elements in clients/clients_fc
416 unsigned int nclients;
419 * Clients that have been informed but requested to leave the tunnel.
421 struct MeshClient **ignore;
424 * Number of elements in clients
426 unsigned int nignore;
431 GNUNET_PEER_Id *blacklisted;
434 * Number of elements in blacklisted
436 unsigned int nblacklisted;
439 * Bloomfilter (for peer identities) to stop circular routes
441 char bloomfilter[MESH_BLOOM_SIZE];
446 struct MeshTunnelTree *tree;
449 * Application type we are looking for in this tunnel
451 GNUNET_MESH_ApplicationType type;
454 * Used to search peers offering a service
456 struct GNUNET_DHT_GetHandle *dht_get_type;
459 * Initial context of the regex search for a connect_by_string
461 struct MeshRegexSearchContext *regex_ctx;
464 * Task to keep the used paths alive
466 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
469 * Task to destroy the tunnel after timeout
471 * FIXME: merge the two? a tunnel will have either
472 * a path refresh OR a timeout, never both!
474 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
477 * Flag to signal the destruction of the tunnel.
478 * If this is set GNUNET_YES the tunnel will be destroyed
479 * when the queue is empty.
486 * Info about a child node in a tunnel, needed to perform flow control.
488 struct MeshTunnelChildInfo
491 * ID of the child node.
506 * Maximum PID allowed (FWD ACK received).
511 * Last ACK sent to that child (BCK ACK).
518 * Info about a leaf client of a tunnel, needed to perform flow control.
520 struct MeshTunnelClientInfo
523 * PID of the last packet sent to the client (FWD).
528 * PID of the last packet received from the client (BCK).
533 * Maximum PID allowed (FWD ACK received).
538 * Last ACK sent to that child (BCK ACK).
546 * Info collected during iteration of child nodes in order to get the ACK value
549 struct MeshTunnelChildIteratorContext
552 * Tunnel whose info is being collected.
554 struct MeshTunnel *t;
557 * Is this context initialized? Is the value in max_child_ack valid?
562 * Maximum child ACK so far.
564 uint32_t max_child_ack;
567 * Number of children nodes
569 unsigned int nchildren;
574 * Info needed to work with tunnel paths and peers
581 struct MeshTunnel *t;
584 * Neighbouring peer to whom we send the packet to
586 struct MeshPeerInfo *peer;
591 struct MeshPeerPath *path;
596 * Struct containing information about a client of the service
603 struct MeshClient *next;
608 struct MeshClient *prev;
611 * Tunnels that belong to this client, indexed by local id
613 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
616 * Tunnels this client has accepted, indexed by incoming local id
618 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
621 * Tunnels this client has rejected, indexed by incoming local id
623 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
625 * Handle to communicate with the client
627 struct GNUNET_SERVER_Client *handle;
630 * Applications that this client has claimed to provide
632 struct GNUNET_CONTAINER_MultiHashMap *apps;
635 * Messages that this client has declared interest in
637 struct GNUNET_CONTAINER_MultiHashMap *types;
640 * Whether the client is active or shutting down (don't send confirmations
641 * to a client that is shutting down.
646 * ID of the client, mainly for debug messages
651 * Regular expressions describing the services offered by this client.
653 char **regexes; // FIXME add timeout? API to remove a regex?
656 * Number of regular expressions in regexes.
658 unsigned int n_regex;
661 * Task to refresh all regular expresions in the DHT.
663 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
669 * Struct to keep information of searches of services described by a regex
670 * using a user-provided string service description.
672 struct MeshRegexSearchInfo
675 * Which tunnel is this for
677 struct MeshTunnel *t;
680 * User provided description of the searched service.
685 * Part of the description already consumed by the search.
692 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
695 * Results from running DHT GETs.
697 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
700 * Contexts, for each running DHT GET. Free all on end of search.
702 struct MeshRegexSearchContext **contexts;
705 * Number of contexts (branches/steps in search).
707 unsigned int n_contexts;
710 * Peer that is connecting via connect_by_string. When connected, free ctx.
715 * Other peers that are found but not yet being connected to.
717 GNUNET_PEER_Id *peers;
720 * Number of elements in peers.
722 unsigned int n_peers;
725 * Next peer to try to connect to.
730 * Timeout for a connect attempt.
731 * When reached, try to connect to a different peer, if any. If not,
732 * try the same peer again.
734 GNUNET_SCHEDULER_TaskIdentifier timeout;
739 * Struct to keep state of running searches that have consumed a part of
742 struct MeshRegexSearchContext
745 * Part of the description already consumed by
746 * this particular search branch.
751 * Information about the search.
753 struct MeshRegexSearchInfo *info;
757 /******************************************************************************/
758 /************************ DEBUG FUNCTIONS ****************************/
759 /******************************************************************************/
763 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
764 * @param cls string to print
765 * @param success GNUNET_OK if the PUT was transmitted,
766 * GNUNET_NO on timeout,
767 * GNUNET_SYSERR on disconnect from service
768 * after the PUT message was transmitted
769 * (so we don't know if it was received or not)
774 mesh_debug (void *cls, int success)
778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
784 /******************************************************************************/
785 /*********************** GLOBAL VARIABLES ****************************/
786 /******************************************************************************/
789 * Configuration parameters
791 static struct GNUNET_TIME_Relative refresh_path_time;
792 static struct GNUNET_TIME_Relative app_announce_time;
793 static struct GNUNET_TIME_Relative id_announce_time;
794 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
795 static struct GNUNET_TIME_Relative connect_timeout;
796 static long long unsigned int default_ttl;
797 static long long unsigned int dht_replication_level;
798 static long long unsigned int max_tunnels;
799 static long long unsigned int max_msgs_queue;
802 * DLL with all the clients, head.
804 static struct MeshClient *clients;
807 * DLL with all the clients, tail.
809 static struct MeshClient *clients_tail;
812 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
814 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
817 * Number of tunnels known.
819 static unsigned long long n_tunnels;
822 * Tunnels incoming, indexed by MESH_TunnelNumber
823 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
825 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
828 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
830 static struct GNUNET_CONTAINER_MultiHashMap *peers;
833 * Handle to communicate with transport
835 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
838 * Handle to communicate with core.
840 static struct GNUNET_CORE_Handle *core_handle;
845 static struct GNUNET_DHT_Handle *dht_handle;
850 static struct GNUNET_SERVER_Handle *server_handle;
853 * Handle to the statistics service.
855 static struct GNUNET_STATISTICS_Handle *stats;
858 * Notification context, to send messages to local clients.
860 static struct GNUNET_SERVER_NotificationContext *nc;
863 * Local peer own ID (memory efficient handle).
865 static GNUNET_PEER_Id myid;
868 * Local peer own ID (full value).
870 static struct GNUNET_PeerIdentity my_full_id;
875 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
880 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
883 * Tunnel ID for the next created tunnel (global tunnel number).
885 static MESH_TunnelNumber next_tid;
888 * Tunnel ID for the next incoming tunnel (local tunnel number).
890 static MESH_TunnelNumber next_local_tid;
893 * All application types provided by this peer.
895 static struct GNUNET_CONTAINER_MultiHashMap *applications;
898 * All message types clients of this peer are interested in.
900 static struct GNUNET_CONTAINER_MultiHashMap *types;
903 * Task to periodically announce provided applications.
905 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
908 * Task to periodically announce itself in the network.
910 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
913 * Next ID to assign to a client.
915 unsigned int next_client_id;
918 /******************************************************************************/
919 /*********************** DECLARATIONS **************************/
920 /******************************************************************************/
922 /* FIXME move declarations here */
925 * Function to process paths received for a new peer addition. The recorded
926 * paths form the initial tunnel, which can be optimized later.
927 * Called on each result obtained for the DHT search.
930 * @param exp when will this value expire
931 * @param key key of the result
932 * @param type type of the result
933 * @param size number of bytes in data
934 * @param data pointer to the result data
937 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
938 const struct GNUNET_HashCode * key,
939 const struct GNUNET_PeerIdentity *get_path,
940 unsigned int get_path_length,
941 const struct GNUNET_PeerIdentity *put_path,
942 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
943 size_t size, const void *data);
947 * Function to process DHT string to regex matching.
948 * Called on each result obtained for the DHT search.
950 * @param cls closure (search context)
951 * @param exp when will this value expire
952 * @param key key of the result
953 * @param get_path path of the get request (not used)
954 * @param get_path_length lenght of get_path (not used)
955 * @param put_path path of the put request (not used)
956 * @param put_path_length length of the put_path (not used)
957 * @param type type of the result
958 * @param size number of bytes in data
959 * @param data pointer to the result data
961 * TODO: re-issue the request after certain time? cancel after X results?
964 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
965 const struct GNUNET_HashCode * key,
966 const struct GNUNET_PeerIdentity *get_path,
967 unsigned int get_path_length,
968 const struct GNUNET_PeerIdentity *put_path,
969 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
970 size_t size, const void *data);
974 * Function to process DHT string to regex matching.
975 * Called on each result obtained for the DHT search.
977 * @param cls closure (search context)
978 * @param exp when will this value expire
979 * @param key key of the result
980 * @param get_path path of the get request (not used)
981 * @param get_path_length lenght of get_path (not used)
982 * @param put_path path of the put request (not used)
983 * @param put_path_length length of the put_path (not used)
984 * @param type type of the result
985 * @param size number of bytes in data
986 * @param data pointer to the result data
989 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
990 const struct GNUNET_HashCode * key,
991 const struct GNUNET_PeerIdentity *get_path,
992 unsigned int get_path_length,
993 const struct GNUNET_PeerIdentity *put_path,
994 unsigned int put_path_length,
995 enum GNUNET_BLOCK_Type type,
996 size_t size, const void *data);
1000 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1001 * and insert it in the appropiate structures if the peer is not known yet.
1003 * @param peer Short identity of the peer.
1005 * @return Existing or newly created peer info.
1007 static struct MeshPeerInfo *
1008 peer_info_get_short (const GNUNET_PEER_Id peer);
1012 * Try to establish a new connection to this peer.
1013 * Use the best path for the given tunnel.
1014 * If the peer doesn't have any path to it yet, try to get one.
1015 * If the peer already has some path, send a CREATE PATH towards it.
1017 * @param peer PeerInfo of the peer.
1018 * @param t Tunnel for which to create the path, if possible.
1021 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1025 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1026 * needed rescources.
1027 * If peer already exists, reevaluate shortest path and change if different.
1029 * @param t Tunnel we want to add a new peer to
1030 * @param peer PeerInfo of the peer being added
1034 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1038 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1039 * that are no longer needed, as well as nodes of no longer reachable peers.
1040 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1042 * @param t Tunnel from which to remove the path.
1043 * @param peer Short id of the peer which should be removed.
1046 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1050 * Search for a tunnel by global ID using full PeerIdentities.
1052 * @param oid owner of the tunnel.
1053 * @param tid global tunnel number.
1055 * @return tunnel handler, NULL if doesn't exist.
1057 static struct MeshTunnel *
1058 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1062 * Delete an active client from the tunnel.
1068 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1071 * Notify a tunnel that a connection has broken that affects at least
1072 * some of its peers.
1074 * @param t Tunnel affected.
1075 * @param p1 Peer that got disconnected from p2.
1076 * @param p2 Peer that got disconnected from p1.
1078 * @return Short ID of the peer disconnected (either p1 or p2).
1079 * 0 if the tunnel remained unaffected.
1081 static GNUNET_PEER_Id
1082 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1087 * Get the current ack value for a tunnel, for data going from root to leaves,
1088 * taking in account the tunnel mode and the status of all children and clients.
1092 * @return Maximum PID allowed.
1095 tunnel_get_fwd_ack (struct MeshTunnel *t);
1099 * Add a client to a tunnel, initializing all needed data structures.
1101 * @param t Tunnel to which add the client.
1102 * @param c Client which to add to the tunnel.
1105 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1109 * Iterator over edges in a regex block retrieved from the DHT.
1111 * @param cls Closure.
1112 * @param token Token that follows to next state.
1113 * @param len Lenght of token.
1114 * @param key Hash of next state.
1116 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1119 regex_edge_iterator (void *cls,
1122 const struct GNUNET_HashCode *key);
1126 * Find a path to a peer that offers a regex servcie compatible
1127 * with a given string.
1129 * @param key The key of the accepting state.
1130 * @param ctx Context containing info about the string, tunnel, etc.
1133 regex_find_path (const struct GNUNET_HashCode *key,
1134 struct MeshRegexSearchContext *ctx);
1138 * Queue and pass message to core when possible.
1140 * @param cls Closure (type dependant).
1141 * @param type Type of the message, 0 for a raw message.
1142 * @param size Size of the message.
1143 * @param dst Neighbor to send message to.
1144 * @param t Tunnel this message belongs to.
1147 queue_add (void *cls, uint16_t type, size_t size,
1148 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1151 * Free a transmission that was already queued with all resources
1152 * associated to the request.
1154 * @param queue Queue handler to cancel.
1155 * @param clear_cls Is it necessary to free associated cls?
1158 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1160 /******************************************************************************/
1161 /************************ ITERATORS ****************************/
1162 /******************************************************************************/
1165 * Iterator over found existing mesh regex blocks that match an ongoing search.
1167 * @param cls closure
1168 * @param key current key code
1169 * @param value value in the hash map
1170 * @return GNUNET_YES if we should continue to iterate,
1174 regex_result_iterator (void *cls,
1175 const struct GNUNET_HashCode * key,
1178 struct MeshRegexBlock *block = value;
1179 struct MeshRegexSearchContext *ctx = cls;
1181 if (GNUNET_YES == ntohl(block->accepting) &&
1182 ctx->position == strlen (ctx->info->description))
1184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1185 regex_find_path (key, ctx);
1186 return GNUNET_YES; // We found an accept state!
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1191 ctx->position, strlen(ctx->info->description),
1192 ntohl(block->accepting));
1195 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1196 ®ex_edge_iterator, ctx);
1203 * Iterator over edges in a regex block retrieved from the DHT.
1205 * @param cls Closure (context of the search).
1206 * @param token Token that follows to next state.
1207 * @param len Lenght of token.
1208 * @param key Hash of next state.
1210 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1213 regex_edge_iterator (void *cls,
1216 const struct GNUNET_HashCode *key)
1218 struct MeshRegexSearchContext *ctx = cls;
1219 struct MeshRegexSearchContext *new_ctx;
1220 struct MeshRegexSearchInfo *info = ctx->info;
1221 struct GNUNET_DHT_GetHandle *get_h;
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1228 current = &info->description[ctx->position];
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1230 current_len = strlen (info->description) - ctx->position;
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1235 if (len > current_len)
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1238 return GNUNET_YES; // Token too long, wont match
1240 if (0 != strncmp (current, token, len))
1242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1243 return GNUNET_YES; // Token doesn't match
1245 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1246 new_ctx->info = info;
1247 new_ctx->position = ctx->position + len;
1248 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1250 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1253 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1254 ®ex_result_iterator,
1256 return GNUNET_YES; // We are already looking for it
1258 /* Start search in DHT */
1260 GNUNET_DHT_get_start (dht_handle, /* handle */
1261 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1262 key, /* key to search */
1263 dht_replication_level, /* replication level */
1264 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1265 NULL, /* xquery */ // FIXME BLOOMFILTER
1266 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1267 &dht_get_string_handler, new_ctx);
1269 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1270 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1280 * Iterator over hash map entries to cancel DHT GET requests after a
1281 * successful connect_by_string.
1283 * @param cls Closure (unused).
1284 * @param key Current key code (unused).
1285 * @param value Value in the hash map (get handle).
1286 * @return GNUNET_YES if we should continue to iterate,
1290 regex_cancel_dht_get (void *cls,
1291 const struct GNUNET_HashCode * key,
1294 struct GNUNET_DHT_GetHandle *h = value;
1296 GNUNET_DHT_get_stop (h);
1302 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1303 * search for connect_by_string.
1305 * @param cls Closure (unused).
1306 * @param key Current key code (unused).
1307 * @param value MeshRegexBlock in the hash map.
1308 * @return GNUNET_YES if we should continue to iterate,
1312 regex_free_result (void *cls,
1313 const struct GNUNET_HashCode * key,
1317 GNUNET_free (value);
1323 * Regex callback iterator to store own service description in the DHT.
1325 * @param cls closure.
1326 * @param key hash for current state.
1327 * @param proof proof for current state.
1328 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1329 * @param num_edges number of edges leaving current state.
1330 * @param edges edges leaving current state.
1333 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1334 int accepting, unsigned int num_edges,
1335 const struct GNUNET_REGEX_Edge *edges)
1337 struct MeshRegexBlock *block;
1338 struct MeshRegexEdge *block_edge;
1339 enum GNUNET_DHT_RouteOption opt;
1343 unsigned int offset;
1346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1347 " regex dht put for state %s\n",
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1356 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1357 if (GNUNET_YES == accepting)
1359 struct MeshRegexAccept block;
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 " state %s is accepting, putting own id\n",
1364 size = sizeof (block);
1366 block.id = my_full_id;
1368 GNUNET_DHT_put(dht_handle, key,
1369 dht_replication_level,
1370 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1371 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1374 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1379 len = strlen(proof);
1380 size = sizeof (struct MeshRegexBlock) + len;
1381 block = GNUNET_malloc (size);
1384 block->n_proof = htonl (len);
1385 block->n_edges = htonl (num_edges);
1386 block->accepting = htonl (accepting);
1388 /* Store the proof at the end of the block. */
1389 aux = (char *) &block[1];
1390 memcpy (aux, proof, len);
1393 /* Store each edge in a variable length MeshEdge struct at the
1394 * very end of the MeshRegexBlock structure.
1396 for (i = 0; i < num_edges; i++)
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 " edge %s towards %s\n",
1401 GNUNET_h2s(&edges[i].destination));
1403 /* aux points at the end of the last block */
1404 len = strlen (edges[i].label);
1405 size += sizeof (struct MeshRegexEdge) + len;
1406 // Calculate offset FIXME is this ok? use size instead?
1407 offset = aux - (char *) block;
1408 block = GNUNET_realloc (block, size);
1409 aux = &((char *) block)[offset];
1410 block_edge = (struct MeshRegexEdge *) aux;
1411 block_edge->key = edges[i].destination;
1412 block_edge->n_token = htonl (len);
1413 aux = (char *) &block_edge[1];
1414 memcpy (aux, edges[i].label, len);
1418 GNUNET_DHT_put(dht_handle, key,
1419 dht_replication_level,
1421 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1423 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1427 GNUNET_free (block);
1432 * Store the regular expression describing a local service into the DHT.
1434 * @param regex The regular expresion.
1437 regex_put (const char *regex)
1439 struct GNUNET_REGEX_Automaton *dfa;
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1442 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1443 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1444 GNUNET_REGEX_automaton_destroy (dfa);
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1450 * Find a path to a peer that offers a regex servcie compatible
1451 * with a given string.
1453 * @param key The key of the accepting state.
1454 * @param ctx Context containing info about the string, tunnel, etc.
1457 regex_find_path (const struct GNUNET_HashCode *key,
1458 struct MeshRegexSearchContext *ctx)
1460 struct GNUNET_DHT_GetHandle *get_h;
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1463 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1464 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1465 key, /* key to search */
1466 dht_replication_level, /* replication level */
1467 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1468 GNUNET_DHT_RO_RECORD_ROUTE,
1469 NULL, /* xquery */ // FIXME BLOOMFILTER
1470 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1471 &dht_get_string_accept_handler, ctx);
1472 GNUNET_break (GNUNET_OK ==
1473 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1481 * Function called if the connect attempt to a peer found via
1482 * connect_by_string times out. Try to connect to another peer, if any.
1483 * Otherwise try to reconnect to the same peer.
1485 * @param cls Closure (info about regex search).
1486 * @param tc TaskContext.
1489 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1491 struct MeshRegexSearchInfo *info = cls;
1492 struct MeshPeerInfo *peer_info;
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1497 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1498 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1506 if (0 < info->n_peers)
1508 // Select next peer, put current in that spot.
1509 id = info->peers[info->i_peer];
1510 info->peers[info->i_peer] = info->peer;
1511 info->i_peer = (info->i_peer + 1) % info->n_peers;
1515 // Try to connect to same peer again.
1518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1520 peer_info = peer_info_get_short(id);
1521 tunnel_add_peer (info->t, peer_info);
1523 tunnel_delete_peer (info->t, old);
1524 peer_info_connect (peer_info, info->t);
1525 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1526 ®ex_connect_timeout,
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1533 * Cancel an ongoing regex search in the DHT and free all resources.
1535 * @param ctx The search context.
1538 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1540 struct MeshRegexSearchInfo *info = ctx->info;
1543 GNUNET_free (info->description);
1544 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1545 ®ex_cancel_dht_get, NULL);
1546 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1547 ®ex_free_result, NULL);
1548 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1549 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1550 info->t->regex_ctx = NULL;
1551 for (i = 0; i < info->n_contexts; i++)
1553 GNUNET_free (info->contexts[i]);
1555 if (0 < info->n_contexts)
1556 GNUNET_free (info->contexts);
1557 if (0 < info->n_peers)
1558 GNUNET_free (info->peers);
1559 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1561 GNUNET_SCHEDULER_cancel(info->timeout);
1567 /******************************************************************************/
1568 /************************ PERIODIC FUNCTIONS ****************************/
1569 /******************************************************************************/
1572 * Announce iterator over for each application provided by the peer
1574 * @param cls closure
1575 * @param key current key code
1576 * @param value value in the hash map
1577 * @return GNUNET_YES if we should continue to
1582 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1584 struct PBlock block;
1585 struct MeshClient *c;
1587 block.id = my_full_id;
1588 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1589 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1590 if (0 == block.type)
1595 block.type = htonl (block.type);
1597 GNUNET_break (NULL !=
1598 GNUNET_DHT_put (dht_handle, key,
1599 dht_replication_level,
1600 GNUNET_DHT_RO_RECORD_ROUTE |
1601 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1602 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1604 (const char *) &block,
1605 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1607 app_announce_time, NULL, NULL));
1613 * Periodically announce what applications are provided by local clients
1616 * @param cls closure
1617 * @param tc task context
1620 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1622 struct MeshClient *c = cls;
1625 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1627 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1631 DEBUG_DHT ("Starting PUT for regex\n");
1633 for (i = 0; i < c->n_regex; i++)
1635 regex_put (c->regexes[i]);
1637 c->regex_announce_task =
1638 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1639 DEBUG_DHT ("Finished PUT for regex\n");
1646 * Periodically announce what applications are provided by local clients
1649 * @param cls closure
1650 * @param tc task context
1653 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1655 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1657 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1661 DEBUG_DHT ("Starting PUT for apps\n");
1663 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1665 announce_applications_task =
1666 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1668 DEBUG_DHT ("Finished PUT for apps\n");
1675 * Periodically announce self id in the DHT
1677 * @param cls closure
1678 * @param tc task context
1681 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1683 struct PBlock block;
1685 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1687 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1691 * - Set data expiration in function of X
1692 * - Adapt X to churn
1694 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1696 block.id = my_full_id;
1697 block.type = htonl (0);
1698 GNUNET_DHT_put (dht_handle, /* DHT handle */
1699 &my_full_id.hashPubKey, /* Key to use */
1700 dht_replication_level, /* Replication level */
1701 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1702 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1703 sizeof (block), /* Size of the data */
1704 (const char *) &block, /* Data itself */
1705 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1706 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1707 NULL, /* Continuation */
1708 NULL); /* Continuation closure */
1710 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1714 /******************************************************************************/
1715 /****************** GENERAL HELPER FUNCTIONS ************************/
1716 /******************************************************************************/
1719 * Decrements the reference counter and frees all resources if needed
1721 * @param mesh_data Data Descriptor used in a multicast message.
1722 * Freed no longer needed (last message).
1725 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1727 /* Make sure it's a multicast packet */
1728 GNUNET_assert (NULL != mesh_data->reference_counter);
1730 if (0 == --(*(mesh_data->reference_counter)))
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1733 if (NULL != mesh_data->task)
1735 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1737 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1739 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1741 GNUNET_free (mesh_data->task);
1743 GNUNET_free (mesh_data->reference_counter);
1744 GNUNET_free (mesh_data->data);
1745 GNUNET_free (mesh_data);
1751 * Check if client has registered with the service and has not disconnected
1753 * @param client the client to check
1755 * @return non-NULL if client exists in the global DLL
1757 static struct MeshClient *
1758 client_get (struct GNUNET_SERVER_Client *client)
1760 struct MeshClient *c;
1765 if (c->handle == client)
1774 * Checks if a given client has subscribed to certain message type
1776 * @param message_type Type of message to check
1777 * @param c Client to check
1779 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1781 * FIXME: use of crypto_hash slows it down
1782 * The hash function alone takes 8-10us out of the ~55us for the whole
1783 * process of retransmitting the message from one local client to another.
1784 * Find faster implementation!
1787 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1789 struct GNUNET_HashCode hc;
1791 if (NULL == c->types)
1794 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1795 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1800 * Allow a client to send more data after transmitting a multicast message
1801 * which some neighbor has not yet accepted altough a reasonable time has
1804 * @param cls Closure (DataDescriptor containing the task identifier)
1805 * @param tc Task Context
1807 * FIXME reference counter cshould be just int
1810 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1812 struct MeshData *mdata = cls;
1814 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1816 GNUNET_assert (NULL != mdata->reference_counter);
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1819 *(mdata->reference_counter));
1820 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1821 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1826 * Check whether client wants traffic from a tunnel.
1828 * @param c Client to check.
1829 * @param t Tunnel to be found.
1831 * @return GNUNET_YES if client knows tunnel.
1833 * TODO look in client hashmap
1836 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1840 for (i = 0; i < t->nclients; i++)
1841 if (t->clients[i] == c)
1848 * Check whether client has been informed about a tunnel.
1850 * @param c Client to check.
1851 * @param t Tunnel to be found.
1853 * @return GNUNET_YES if client knows tunnel.
1855 * TODO look in client hashmap
1858 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1862 for (i = 0; i < t->nignore; i++)
1863 if (t->ignore[i] == c)
1865 return client_wants_tunnel(c, t);
1870 * Marks a client as uninterested in traffic from the tunnel, updating both
1871 * client and tunnel to reflect this.
1873 * @param c Client that doesn't want traffic anymore.
1874 * @param t Tunnel which should be ignored.
1876 * FIXME when to delete an incoming tunnel?
1879 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1881 struct GNUNET_HashCode hash;
1883 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1884 GNUNET_break (GNUNET_YES ==
1885 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1887 GNUNET_break (GNUNET_YES ==
1888 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1889 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1890 tunnel_delete_active_client (t, c);
1891 GNUNET_array_append (t->ignore, t->nignore, c);
1896 * Deletes a tunnel from a client (either owner or destination). To be used on
1897 * tunnel destroy, otherwise, use client_ignore_tunnel.
1899 * @param c Client whose tunnel to delete.
1900 * @param t Tunnel which should be deleted.
1903 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1905 struct GNUNET_HashCode hash;
1909 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1910 GNUNET_assert (GNUNET_YES ==
1911 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1917 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1919 GNUNET_assert (GNUNET_YES ==
1920 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1924 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1932 * Send the message to all clients that have subscribed to its type
1934 * @param msg Pointer to the message itself
1935 * @param payload Pointer to the payload of the message.
1936 * @param t The tunnel to whose clients this message goes.
1938 * @return number of clients this message was sent to
1941 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1942 const struct GNUNET_MessageHeader *payload,
1943 struct MeshTunnel *t)
1945 struct MeshClient *c;
1946 MESH_TunnelNumber *tid;
1949 char cbuf[htons (msg->size)];
1951 type = ntohs (payload->type);
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1954 GNUNET_MESH_DEBUG_M2S (type));
1956 memcpy (cbuf, msg, sizeof (cbuf));
1957 switch (htons (msg->type))
1959 struct GNUNET_MESH_Unicast *uc;
1960 struct GNUNET_MESH_Multicast *mc;
1961 struct GNUNET_MESH_ToOrigin *to;
1963 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1964 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1967 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1968 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1971 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1972 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1980 for (count = 0, c = clients; c != NULL; c = c->next)
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1983 if (client_is_subscribed (type, c))
1985 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1989 *tid = htonl (t->local_tid);
1993 if (GNUNET_NO == client_knows_tunnel (c, t))
1995 /* This client doesn't know the tunnel */
1996 struct GNUNET_MESH_TunnelNotification tmsg;
1997 struct GNUNET_HashCode hash;
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2000 tmsg.header.size = htons (sizeof (tmsg));
2001 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2002 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2003 tmsg.tunnel_id = htonl (t->local_tid_dest);
2004 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2005 &tmsg.header, GNUNET_NO);
2006 tunnel_add_client (t, c);
2007 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2009 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2010 c->incoming_tunnels, &hash, t,
2011 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2013 *tid = htonl (t->local_tid_dest);
2016 /* Check if the client wants to get traffic from the tunnel */
2017 if (GNUNET_NO == client_wants_tunnel(c, t))
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2021 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2022 (struct GNUNET_MessageHeader
2023 *) cbuf, GNUNET_NO);
2032 * Notify the client that owns the tunnel that a peer has connected to it
2033 * (the requested path to it has been confirmed).
2035 * @param t Tunnel whose owner to notify
2036 * @param id Short id of the peer that has connected
2039 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2041 struct GNUNET_MESH_PeerControl pc;
2043 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2044 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2045 pc.tunnel_id = htonl (t->local_tid);
2046 GNUNET_PEER_resolve (id, &pc.peer);
2047 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2053 * Notify all clients (not depending on registration status) that the incoming
2054 * tunnel is no longer valid.
2056 * @param t Tunnel that was destroyed.
2059 send_clients_tunnel_destroy (struct MeshTunnel *t)
2061 struct GNUNET_MESH_TunnelMessage msg;
2063 msg.header.size = htons (sizeof (msg));
2064 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2065 msg.tunnel_id = htonl (t->local_tid_dest);
2066 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2071 * Notify clients of tunnel disconnections, if needed.
2072 * In case the origin disconnects, the destination clients get a tunnel destroy
2073 * notification. If the last destination disconnects (only one remaining client
2074 * in tunnel), the origin gets a (local ID) peer disconnected.
2075 * Note that the function must be called BEFORE removing the client from
2078 * @param t Tunnel that was destroyed.
2079 * @param c Client that disconnected.
2082 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2088 struct GNUNET_MESH_TunnelMessage msg;
2090 msg.header.size = htons (sizeof (msg));
2091 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2092 msg.tunnel_id = htonl (t->local_tid_dest);
2093 for (i = 0; i < t->nclients; i++)
2094 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2095 &msg.header, GNUNET_NO);
2097 // FIXME when to disconnect an incoming tunnel?
2098 else if (1 == t->nclients && NULL != t->owner)
2100 struct GNUNET_MESH_PeerControl msg;
2102 msg.header.size = htons (sizeof (msg));
2103 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2104 msg.tunnel_id = htonl (t->local_tid);
2105 msg.peer = my_full_id;
2106 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2107 &msg.header, GNUNET_NO);
2113 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2114 * and insert it in the appropiate structures if the peer is not known yet.
2116 * @param peer Full identity of the peer.
2118 * @return Existing or newly created peer info.
2120 static struct MeshPeerInfo *
2121 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2123 struct MeshPeerInfo *peer_info;
2125 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2126 if (NULL == peer_info)
2129 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2130 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2131 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2132 peer_info->id = GNUNET_PEER_intern (peer);
2140 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2141 * and insert it in the appropiate structures if the peer is not known yet.
2143 * @param peer Short identity of the peer.
2145 * @return Existing or newly created peer info.
2147 static struct MeshPeerInfo *
2148 peer_info_get_short (const GNUNET_PEER_Id peer)
2150 struct GNUNET_PeerIdentity id;
2152 GNUNET_PEER_resolve (peer, &id);
2153 return peer_info_get (&id);
2158 * Iterator to remove the tunnel from the list of tunnels a peer participates
2161 * @param cls Closure (tunnel info)
2162 * @param key GNUNET_PeerIdentity of the peer (unused)
2163 * @param value PeerInfo of the peer
2165 * @return always GNUNET_YES, to keep iterating
2168 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2170 struct MeshTunnel *t = cls;
2171 struct MeshPeerInfo *peer = value;
2174 for (i = 0; i < peer->ntunnels; i++)
2177 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2180 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2181 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2190 * Core callback to write a pre-constructed data packet to core buffer
2192 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2193 * @param size Number of bytes available in buf.
2194 * @param buf Where the to write the message.
2196 * @return number of bytes written to buf
2199 send_core_data_raw (void *cls, size_t size, void *buf)
2201 struct MeshTransmissionDescriptor *info = cls;
2202 struct GNUNET_MessageHeader *msg;
2205 GNUNET_assert (NULL != info);
2206 GNUNET_assert (NULL != info->mesh_data);
2207 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2208 total_size = ntohs (msg->size);
2210 if (total_size > size)
2215 memcpy (buf, msg, total_size);
2216 data_descriptor_decrement_rc (info->mesh_data);
2223 * Sends an already built non-multicast message to a peer,
2224 * properly registrating all used resources.
2226 * @param message Message to send. Function makes a copy of it.
2227 * @param peer Short ID of the neighbor whom to send the message.
2228 * @param t Tunnel on which this message is transmitted.
2231 send_message (const struct GNUNET_MessageHeader *message,
2232 const struct GNUNET_PeerIdentity *peer,
2233 struct MeshTunnel *t)
2235 struct MeshTransmissionDescriptor *info;
2236 struct MeshPeerInfo *neighbor;
2237 struct MeshPeerPath *p;
2241 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2243 size = ntohs (message->size);
2244 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2245 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2246 info->mesh_data->data = GNUNET_malloc (size);
2247 memcpy (info->mesh_data->data, message, size);
2248 type = ntohs(message->type);
2249 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2251 struct GNUNET_MESH_Unicast *m;
2253 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2254 m->ttl = htonl (ntohl (m->ttl) - 1);
2256 info->mesh_data->data_len = size;
2257 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2258 *info->mesh_data->reference_counter = 1;
2259 neighbor = peer_info_get (peer);
2260 for (p = neighbor->path_head; NULL != p; p = p->next)
2270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2271 " %s IS NOT DIRECTLY CONNECTED\n",
2273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2276 for (p = neighbor->path_head; NULL != p; p = p->next)
2278 struct GNUNET_PeerIdentity debug_id;
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 " path with %u hops through:\n",
2284 for (i = 0; i < p->length; i++)
2286 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 i, GNUNET_i2s(&debug_id));
2293 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2294 GNUNET_free (info->mesh_data->data);
2295 GNUNET_free (info->mesh_data);
2299 info->peer = neighbor;
2300 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2303 * TODO: in this case we only need the service to retransmit
2304 * the message down the path. If we pass the real type to queue_add,
2305 * queue_send will try to build the message from scratch. This can
2306 * probably be done by some other way instead of deleteing the type
2320 * Sends a CREATE PATH message for a path to a peer, properly registrating
2321 * all used resources.
2323 * @param peer PeerInfo of the final peer for whom this path is being created.
2324 * @param p Path itself.
2325 * @param t Tunnel for which the path is created.
2328 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2329 struct MeshTunnel *t)
2331 struct GNUNET_PeerIdentity id;
2332 struct MeshPathInfo *path_info;
2333 struct MeshPeerInfo *neighbor;
2339 p = tree_get_path_to_peer (t->tree, peer->id);
2346 for (i = 0; i < p->length; i++)
2348 if (p->peers[i] == myid)
2351 if (i >= p->length - 1)
2357 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2359 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2360 path_info->path = p;
2362 neighbor = peer_info_get (&id);
2363 path_info->peer = neighbor;
2364 queue_add (path_info,
2365 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2366 sizeof (struct GNUNET_MESH_ManipulatePath) +
2367 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2374 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2376 * @param t Tunnel whose path to destroy.
2377 * @param destination Short ID of the peer to whom the path to destroy.
2380 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2382 struct MeshPeerPath *p;
2385 p = tree_get_path_to_peer (t->tree, destination);
2391 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2392 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2394 struct GNUNET_MESH_ManipulatePath *msg;
2395 struct GNUNET_PeerIdentity *pi;
2399 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2400 msg->header.size = htons (size);
2401 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2402 msg->tid = htonl (t->id.tid);
2403 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2404 for (i = 0; i < p->length; i++)
2406 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2408 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2415 * Try to establish a new connection to this peer.
2416 * Use the best path for the given tunnel.
2417 * If the peer doesn't have any path to it yet, try to get one.
2418 * If the peer already has some path, send a CREATE PATH towards it.
2420 * @param peer PeerInfo of the peer.
2421 * @param t Tunnel for which to create the path, if possible.
2424 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2426 struct MeshPeerPath *p;
2427 struct MeshPathInfo *path_info;
2429 if (NULL != peer->path_head)
2431 p = tree_get_path_to_peer (t->tree, peer->id);
2438 // FIXME always send create path to self
2441 send_create_path (peer, p, t);
2445 struct GNUNET_HashCode hash;
2448 send_client_peer_connected (t, myid);
2449 t->local_tid_dest = next_local_tid++;
2450 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2453 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2454 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2461 else if (NULL == peer->dhtget)
2463 struct GNUNET_PeerIdentity id;
2465 GNUNET_PEER_resolve (peer->id, &id);
2466 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2467 path_info->peer = peer;
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2471 peer->dhtgetcls = path_info;
2472 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2473 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2474 &id.hashPubKey, /* key to search */
2475 dht_replication_level, /* replication level */
2476 GNUNET_DHT_RO_RECORD_ROUTE |
2477 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2478 NULL, /* xquery */ // FIXME BLOOMFILTER
2479 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2480 &dht_get_id_handler, path_info);
2482 /* Otherwise, there is no path but the DHT get is already started. */
2487 * Task to delay the connection of a peer
2489 * @param cls Closure (path info with tunnel and peer to connect).
2490 * Will be free'd on exection.
2491 * @param tc TaskContext
2494 peer_info_connect_task (void *cls,
2495 const struct GNUNET_SCHEDULER_TaskContext *tc)
2497 struct MeshPathInfo *path_info = cls;
2499 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2504 peer_info_connect (path_info->peer, path_info->t);
2510 * Destroy the peer_info and free any allocated resources linked to it
2512 * @param pi The peer_info to destroy.
2514 * @return GNUNET_OK on success
2517 peer_info_destroy (struct MeshPeerInfo *pi)
2519 struct GNUNET_PeerIdentity id;
2520 struct MeshPeerPath *p;
2521 struct MeshPeerPath *nextp;
2523 GNUNET_PEER_resolve (pi->id, &id);
2524 GNUNET_PEER_change_rc (pi->id, -1);
2527 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2530 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2531 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2533 if (NULL != pi->dhtget)
2535 GNUNET_DHT_get_stop (pi->dhtget);
2536 GNUNET_free (pi->dhtgetcls);
2542 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2552 * Remove all paths that rely on a direct connection between p1 and p2
2553 * from the peer itself and notify all tunnels about it.
2555 * @param peer PeerInfo of affected peer.
2556 * @param p1 GNUNET_PEER_Id of one peer.
2557 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2560 * TODO: optimize (see below)
2563 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2566 struct MeshPeerPath *p;
2567 struct MeshPeerPath *aux;
2568 struct MeshPeerInfo *peer_d;
2570 unsigned int destroyed;
2575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2577 p = peer->path_head;
2581 for (i = 0; i < (p->length - 1); i++)
2583 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2584 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2586 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2597 for (i = 0; i < peer->ntunnels; i++)
2599 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2603 * Problem: one or more peers have been deleted from the tunnel tree.
2604 * We don't know who they are to try to add them again.
2605 * We need to try to find a new path for each of the disconnected peers.
2606 * Some of them might already have a path to reach them that does not
2607 * involve p1 and p2. Adding all anew might render in a better tree than
2608 * the trivial immediate fix.
2610 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2611 * its children will be reachable trough him.
2613 peer_d = peer_info_get_short (d);
2616 for (p = peer_d->path_head; NULL != p; p = p->next)
2618 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2626 /* No callback, as peer will be already disconnected and a connection
2627 * scheduled by tunnel_notify_connection_broken.
2629 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2633 peer_info_connect (peer_d, peer->tunnels[i]);
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2641 * Add the path to the peer and update the path used to reach it in case this
2644 * @param peer_info Destination peer to add the path to.
2645 * @param path New path to add. Last peer must be the peer in arg 1.
2646 * Path will be either used of freed if already known.
2647 * @param trusted Do we trust that this path is real?
2650 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2653 struct MeshPeerPath *aux;
2657 if ((NULL == peer_info) || (NULL == path))
2660 path_destroy (path);
2663 if (path->peers[path->length - 1] != peer_info->id)
2666 path_destroy (path);
2669 if (path->length <= 2 && GNUNET_NO == trusted)
2671 /* Only allow CORE to tell us about direct paths */
2672 path_destroy (path);
2675 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2676 for (l = 1; l < path->length; l++)
2678 if (path->peers[l] == myid)
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2681 for (l2 = 0; l2 < path->length - l; l2++)
2683 path->peers[l2] = path->peers[l + l2];
2688 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2693 struct GNUNET_PeerIdentity id;
2695 GNUNET_PEER_resolve (peer_info->id, &id);
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2697 path->length, GNUNET_i2s (&id));
2700 l = path_get_length (path);
2707 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2708 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2710 l2 = path_get_length (aux);
2713 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2714 peer_info->path_tail, aux, path);
2719 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2721 path_destroy (path);
2726 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2733 * Add the path to the origin peer and update the path used to reach it in case
2734 * this is the shortest.
2735 * The path is given in peer_info -> destination, therefore we turn the path
2736 * upside down first.
2738 * @param peer_info Peer to add the path to, being the origin of the path.
2739 * @param path New path to add after being inversed.
2740 * @param trusted Do we trust that this path is real?
2743 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2744 struct MeshPeerPath *path, int trusted)
2747 peer_info_add_path (peer_info, path, trusted);
2752 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2753 * to obtain a local peer -> destination path and interning the peer ids.
2755 * @return Newly allocated and created path
2757 static struct MeshPeerPath *
2758 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2759 unsigned int get_path_length,
2760 const struct GNUNET_PeerIdentity *put_path,
2761 unsigned int put_path_length)
2763 struct MeshPeerPath *p;
2769 GNUNET_PEER_change_rc (myid, 1);
2770 i = get_path_length;
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2772 for (i--; i >= 0; i--)
2774 id = GNUNET_PEER_intern (&get_path[i]);
2775 if (p->length > 0 && id == p->peers[p->length - 1])
2777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2778 GNUNET_PEER_change_rc (id, -1);
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2783 GNUNET_i2s (&get_path[i]));
2785 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2786 p->peers[p->length - 1] = id;
2789 i = put_path_length;
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2791 for (i--; i >= 0; i--)
2793 id = GNUNET_PEER_intern (&put_path[i]);
2796 /* PUT path went through us, so discard the path up until now and start
2797 * from here to get a much shorter (and loop-free) path.
2802 if (p->length > 0 && id == p->peers[p->length - 1])
2804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2805 GNUNET_PEER_change_rc (id, -1);
2809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2810 GNUNET_i2s (&put_path[i]));
2812 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2813 p->peers[p->length - 1] = id;
2817 if (get_path_length > 0)
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2819 GNUNET_i2s (&get_path[0]));
2820 if (put_path_length > 0)
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2822 GNUNET_i2s (&put_path[0]));
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2825 for (i = 0; i < p->length; i++)
2827 struct GNUNET_PeerIdentity peer_id;
2829 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2831 GNUNET_i2s (&peer_id));
2839 * Adds a path to the peer_infos of all the peers in the path
2841 * @param p Path to process.
2842 * @param confirmed Whether we know if the path works or not.
2845 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2849 /* TODO: invert and add */
2850 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2851 for (i++; i < p->length; i++)
2853 struct MeshPeerInfo *aux;
2854 struct MeshPeerPath *copy;
2856 aux = peer_info_get_short (p->peers[i]);
2857 copy = path_duplicate (p);
2858 copy->length = i + 1;
2859 peer_info_add_path (aux, copy, GNUNET_NO);
2865 * Send keepalive packets for a peer
2867 * @param cls Closure (tunnel for which to send the keepalive).
2868 * @param tc Notification context.
2870 * TODO: implement explicit multicast keepalive?
2873 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2877 * Search for a tunnel among the incoming tunnels
2879 * @param tid the local id of the tunnel
2881 * @return tunnel handler, NULL if doesn't exist
2883 static struct MeshTunnel *
2884 tunnel_get_incoming (MESH_TunnelNumber tid)
2886 struct GNUNET_HashCode hash;
2888 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2889 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2890 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2895 * Search for a tunnel among the tunnels for a client
2897 * @param c the client whose tunnels to search in
2898 * @param tid the local id of the tunnel
2900 * @return tunnel handler, NULL if doesn't exist
2902 static struct MeshTunnel *
2903 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2905 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2907 return tunnel_get_incoming (tid);
2911 struct GNUNET_HashCode hash;
2913 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2914 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2920 * Search for a tunnel by global ID using PEER_ID
2922 * @param pi owner of the tunnel
2923 * @param tid global tunnel number
2925 * @return tunnel handler, NULL if doesn't exist
2927 static struct MeshTunnel *
2928 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2930 struct MESH_TunnelID id;
2931 struct GNUNET_HashCode hash;
2936 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2937 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2942 * Search for a tunnel by global ID using full PeerIdentities
2944 * @param oid owner of the tunnel
2945 * @param tid global tunnel number
2947 * @return tunnel handler, NULL if doesn't exist
2949 static struct MeshTunnel *
2950 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2952 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2957 * Delete an active client from the tunnel.
2963 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2967 for (i = 0; i < t->nclients; i++)
2969 if (t->clients[i] == c)
2971 t->clients[i] = t->clients[t->nclients - 1];
2972 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2973 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2975 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2983 * Delete an ignored client from the tunnel.
2989 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2993 for (i = 0; i < t->nignore; i++)
2995 if (t->ignore[i] == c)
2997 t->ignore[i] = t->ignore[t->nignore - 1];
2998 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3006 * Delete a client from the tunnel. It should be only done on
3007 * client disconnection, otherwise use client_ignore_tunnel.
3013 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3015 tunnel_delete_ignored_client (t, c);
3016 tunnel_delete_active_client (t, c);
3021 * Iterator to free MeshTunnelChildInfo of tunnel children.
3023 * @param cls Closure (tunnel info).
3024 * @param key Hash of GNUNET_PEER_Id (unused).
3025 * @param value MeshTunnelChildInfo of the child.
3027 * @return always GNUNET_YES, to keep iterating
3030 tunnel_destroy_child (void *cls,
3031 const struct GNUNET_HashCode * key,
3034 GNUNET_free (value);
3040 * Callback used to notify a client owner of a tunnel that a peer has
3041 * disconnected, most likely because of a path change.
3043 * @param cls Closure (tunnel this notification is about).
3044 * @param peer_id Short ID of disconnected peer.
3047 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3049 struct MeshTunnel *t = cls;
3050 struct MeshPeerInfo *peer;
3051 struct MeshPathInfo *path_info;
3053 if (NULL != t->owner && NULL != nc)
3055 struct GNUNET_MESH_PeerControl msg;
3057 msg.header.size = htons (sizeof (msg));
3058 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3059 msg.tunnel_id = htonl (t->local_tid);
3060 GNUNET_PEER_resolve (peer_id, &msg.peer);
3061 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3062 &msg.header, GNUNET_NO);
3064 peer = peer_info_get_short (peer_id);
3065 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3066 path_info->peer = peer;
3068 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3073 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3074 * needed rescources.
3075 * If peer already exists, reevaluate shortest path and change if different.
3077 * @param t Tunnel we want to add a new peer to
3078 * @param peer PeerInfo of the peer being added
3082 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3084 struct GNUNET_PeerIdentity id;
3085 struct MeshPeerPath *best_p;
3086 struct MeshPeerPath *p;
3087 unsigned int best_cost;
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3091 GNUNET_PEER_resolve (peer->id, &id);
3093 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3096 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3097 GNUNET_assert (GNUNET_OK ==
3098 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3100 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3103 if (NULL != (p = peer->path_head))
3106 best_cost = tree_get_path_cost (t->tree, p);
3109 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3116 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3117 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3118 t->path_refresh_task =
3119 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3123 /* Start a DHT get */
3124 peer_info_connect (peer, t);
3126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3130 * Add a path to a tunnel which we don't own, just to remember the next hop.
3131 * If destination node was already in the tunnel, the first hop information
3132 * will be replaced with the new path.
3134 * @param t Tunnel we want to add a new peer to
3135 * @param p Path to add
3136 * @param own_pos Position of local node in path.
3140 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3141 unsigned int own_pos)
3143 struct GNUNET_PeerIdentity id;
3145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3146 GNUNET_assert (0 != own_pos);
3147 tree_add_path (t->tree, p, NULL, NULL);
3148 if (own_pos < p->length - 1)
3150 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3151 tree_update_first_hops (t->tree, myid, &id);
3153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3157 * Add a client to a tunnel, initializing all needed data structures.
3159 * @param t Tunnel to which add the client.
3160 * @param c Client which to add to the tunnel.
3163 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3165 struct MeshTunnelClientInfo clinfo;
3167 GNUNET_array_append (t->clients, t->nclients, c);
3168 clinfo.fwd_ack = t->fwd_pid + 1;
3169 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3170 clinfo.fwd_pid = t->fwd_pid;
3171 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3173 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3178 * Notifies a tunnel that a connection has broken that affects at least
3179 * some of its peers. Sends a notification towards the root of the tree.
3180 * In case the peer is the owner of the tree, notifies the client that owns
3181 * the tunnel and tries to reconnect.
3183 * @param t Tunnel affected.
3184 * @param p1 Peer that got disconnected from p2.
3185 * @param p2 Peer that got disconnected from p1.
3187 * @return Short ID of the peer disconnected (either p1 or p2).
3188 * 0 if the tunnel remained unaffected.
3190 static GNUNET_PEER_Id
3191 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3197 tree_notify_connection_broken (t->tree, p1, p2,
3198 &tunnel_notify_client_peer_disconnected,
3200 if (myid != p1 && myid != p2)
3206 if (tree_get_predecessor (t->tree) != 0)
3208 /* We are the peer still connected, notify owner of the disconnection. */
3209 struct GNUNET_MESH_PathBroken msg;
3210 struct GNUNET_PeerIdentity neighbor;
3212 msg.header.size = htons (sizeof (msg));
3213 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3214 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3215 msg.tid = htonl (t->id.tid);
3216 msg.peer1 = my_full_id;
3217 GNUNET_PEER_resolve (pid, &msg.peer2);
3218 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3219 send_message (&msg.header, &neighbor, t);
3227 * Send a multicast packet to a neighbor.
3229 * @param cls Closure (Info about the multicast packet)
3230 * @param neighbor_id Short ID of the neighbor to send the packet to.
3233 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3235 struct MeshData *mdata = cls;
3236 struct MeshTransmissionDescriptor *info;
3237 struct GNUNET_PeerIdentity neighbor;
3239 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3241 info->mesh_data = mdata;
3242 (*(mdata->reference_counter)) ++;
3243 info->destination = neighbor_id;
3244 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3246 GNUNET_i2s (&neighbor));
3247 info->peer = peer_info_get (&neighbor);
3248 GNUNET_assert (NULL != info->peer);
3250 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3251 info->mesh_data->data_len,
3258 * Send a message in a tunnel in multicast, sending a copy to each child node
3259 * down the local one in the tunnel tree.
3261 * @param t Tunnel in which to send the data.
3262 * @param msg Message to be sent.
3263 * @param internal Has the service generated this message?
3266 tunnel_send_multicast (struct MeshTunnel *t,
3267 const struct GNUNET_MessageHeader *msg,
3270 struct MeshData *mdata;
3272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3273 " sending a multicast packet...\n");
3274 mdata = GNUNET_malloc (sizeof (struct MeshData));
3275 mdata->data_len = ntohs (msg->size);
3276 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3278 mdata->data = GNUNET_malloc (mdata->data_len);
3279 memcpy (mdata->data, msg, mdata->data_len);
3280 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3282 struct GNUNET_MESH_Multicast *mcast;
3284 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3285 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3287 ntohl (mcast->ttl));
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3293 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3294 && GNUNET_NO == internal)
3296 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3298 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3304 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3305 if (*(mdata->reference_counter) == 0)
3307 GNUNET_free (mdata->data);
3308 GNUNET_free (mdata->reference_counter);
3309 if (NULL != mdata->task)
3311 GNUNET_SCHEDULER_cancel(*(mdata->task));
3312 GNUNET_free (mdata->task);
3313 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3315 // FIXME change order?
3316 GNUNET_free (mdata);
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3319 " sending a multicast packet done\n");
3325 * Increase the SKIP value of all peers that
3326 * have not received a unicast message.
3328 * @param cls Closure (ID of the peer that HAS received the message).
3329 * @param key ID of the neighbor.
3330 * @param value Information about the neighbor.
3332 * @return GNUNET_YES to keep iterating.
3335 tunnel_add_skip (void *cls,
3336 const struct GNUNET_HashCode * key,
3339 struct GNUNET_PeerIdentity *neighbor = cls;
3340 struct MeshTunnelChildInfo *cinfo = value;
3342 /* TODO compare only pointers? key == neighbor? */
3343 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3353 * @brief Get neighbor's Flow Control information.
3355 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3356 * descendant of the local node in a certain tunnel.
3357 * If the info is not yet there (recently created path), creates the data struct
3358 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3361 * @param t Tunnel related.
3362 * @param peer Neighbor whose Flow Control info is needed.
3364 * @return Neighbor's Flow Control info.
3366 static struct MeshTunnelChildInfo *
3367 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3368 const struct GNUNET_PeerIdentity *peer)
3370 struct MeshTunnelChildInfo *cinfo;
3371 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3377 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3378 cinfo->id = GNUNET_PEER_intern (peer);
3379 cinfo->skip = t->fwd_pid;
3381 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3382 cinfo->fwd_ack = t->fwd_pid + delta;
3383 cinfo->bck_ack = delta;
3385 GNUNET_assert (GNUNET_OK ==
3386 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3389 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3396 * Get the Flow Control info of a client.
3398 * @param t Tunnel on which to look.
3399 * @param c Client whose ACK to get.
3401 * @return ACK value.
3403 static struct MeshTunnelClientInfo *
3404 tunnel_get_client_fc (struct MeshTunnel *t,
3405 struct MeshClient *c)
3409 for (i = 0; i < t->nclients; i++)
3411 if (t->clients[i] != c)
3413 return &t->clients_fc[i];
3416 return NULL; // avoid compiler / coverity complaints
3421 * Iterator to get the appropiate ACK value from all children nodes.
3423 * @param cls Closue (tunnel).
3424 * @param id Id of the child node.
3427 tunnel_get_child_fwd_ack (void *cls,
3430 struct GNUNET_PeerIdentity peer_id;
3431 struct MeshTunnelChildInfo *cinfo;
3432 struct MeshTunnelChildIteratorContext *ctx = cls;
3433 struct MeshTunnel *t = ctx->t;
3436 GNUNET_PEER_resolve (id, &peer_id);
3437 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3438 ack = cinfo->fwd_ack;
3441 if (GNUNET_NO == ctx->init)
3443 ctx->max_child_ack = ack;
3444 ctx->init = GNUNET_YES;
3447 if (GNUNET_YES == t->speed_min)
3449 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3453 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3460 * Get the maximum PID allowed to transmit to any
3461 * tunnel child of the local peer, depending on the tunnel
3462 * buffering/speed settings.
3466 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3469 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3471 struct MeshTunnelChildIteratorContext ctx;
3473 ctx.max_child_ack = 0;
3475 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3477 if (0 == ctx.nchildren)
3479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3480 " tunnel has no children, no FWD ACK\n");
3484 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3485 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3487 return (int64_t) ctx.max_child_ack;
3492 * Set the FWD ACK value of a client in a particular tunnel.
3494 * @param t Tunnel affected.
3495 * @param c Client whose ACK to set.
3496 * @param ack ACK value.
3499 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3500 struct MeshClient *c,
3505 for (i = 0; i < t->nclients; i++)
3507 if (t->clients[i] != c)
3509 t->clients_fc[i].fwd_ack = ack;
3517 * Get the highest ACK value of all clients in a particular tunnel,
3518 * according to the buffering/speed settings.
3520 * @param t Tunnel on which to look.
3522 * @return Corresponding ACK value (max uint32_t).
3523 * If no clients are suscribed, -1.
3526 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3531 if (0 == t->nclients)
3533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3534 " tunnel has no clients, no FWD ACK\n");
3538 for (ack = -1, i = 0; i < t->nclients; i++)
3541 (GNUNET_YES == t->speed_min &&
3542 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3543 (GNUNET_NO == t->speed_min &&
3544 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3546 ack = t->clients_fc[i].fwd_ack;
3550 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3551 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3553 return (uint32_t) ack;
3558 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3559 * mode and the status of all children nodes.
3563 * @return Maximum PID allowed.
3566 tunnel_get_fwd_ack (struct MeshTunnel *t)
3569 uint32_t buffer_free;
3574 count = t->fwd_pid - t->skip;
3575 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3576 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3577 child_ack = tunnel_get_children_fwd_ack (t);
3578 client_ack = tunnel_get_clients_fwd_ack (t);
3579 if (-1 == child_ack)
3581 // Node has no children, child_ack AND core buffer are irrelevant.
3582 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
3583 return (uint32_t) client_ack;
3586 if (GNUNET_YES == t->speed_min)
3588 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3589 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3593 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3594 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3596 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3597 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3599 count, buffer_free, child_ack, client_ack, ack);
3605 * Build a local ACK message and send it to a local client.
3607 * @param t Tunnel on which to send the ACK.
3608 * @param c Client to whom send the ACK.
3609 * @param ack Value of the ACK.
3612 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3614 struct GNUNET_MESH_LocalAck msg;
3616 msg.header.size = htons (sizeof (msg));
3617 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3618 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3619 msg.max_pid = htonl (ack);
3620 GNUNET_SERVER_notification_context_unicast(nc,
3627 * Build an ACK message and queue it to send to the given peer.
3629 * @param t Tunnel on which to send the ACK.
3630 * @param peer Peer to whom send the ACK.
3631 * @param ack Value of the ACK.
3634 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3636 struct GNUNET_MESH_ACK msg;
3638 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3639 msg.header.size = htons (sizeof (msg));
3640 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3641 msg.pid = htonl (ack);
3642 msg.tid = htonl (t->id.tid);
3644 send_message (&msg.header, peer, t);
3649 * Notify a the owner of a tunnel about how many more
3650 * payload packages will we accept on a given tunnel.
3652 * @param t Tunnel on which to send the ACK.
3655 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3660 "Sending client FWD ACK on tunnel %X\n",
3663 ack = tunnel_get_fwd_ack (t);
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3666 if (t->last_fwd_ack == ack)
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3670 t->last_fwd_ack = ack;
3671 send_local_ack (t, t->owner, ack);
3676 * Send an ACK informing the predecessor about the available buffer space.
3677 * In case there is no predecessor, inform the owning client.
3678 * If buffering is off, send only on behalf of children or self if endpoint.
3679 * If buffering is on, send when sent to children and buffer space is free.
3680 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3681 * the ACK itself goes "back" (towards root).
3683 * @param t Tunnel on which to send the ACK.
3684 * @param type Type of message that triggered the ACK transmission.
3687 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3689 struct GNUNET_PeerIdentity id;
3692 if (NULL != t->owner)
3694 tunnel_send_client_fwd_ack (t);
3697 /* Is it after unicast / multicast retransmission? */
3700 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3701 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3703 "ACK due to FWD DATA retransmission\n");
3704 if (GNUNET_YES == t->nobuffer)
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3709 if (t->fwd_queue_max > t->fwd_queue_n * 2)
3711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3715 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3716 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3722 /* Ok, ACK might be necessary, what PID to ACK? */
3723 ack = tunnel_get_fwd_ack (t);
3725 /* If speed_min and not all children have ack'd, dont send yet */
3726 if (ack == t->last_fwd_ack)
3728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3732 t->last_fwd_ack = ack;
3733 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3734 send_ack (t, &id, ack);
3739 * Iterator to send a child node a BCK ACK to allow him to send more
3742 * @param cls Closure (tunnel).
3743 * @param id Id of the child node.
3746 tunnel_send_child_bck_ack (void *cls,
3749 struct MeshTunnel *t = cls;
3750 struct MeshTunnelChildInfo *cinfo;
3751 struct GNUNET_PeerIdentity peer;
3753 GNUNET_PEER_resolve (id, &peer);
3754 cinfo = tunnel_get_neighbor_fc (t, &peer);
3756 if (cinfo->bck_ack != cinfo->pid &&
3757 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3761 send_ack (t, &peer, cinfo->bck_ack);
3766 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3768 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3770 * @param t Tunnel on which to send the BCK ACKs.
3773 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3776 unsigned int tunnel_delta;
3778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3780 tunnel_delta = t->bck_ack - t->bck_pid;
3781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3783 /* Find client whom to allow to send to origin (with lowest buffer space) */
3784 for (i = 0; i < t->nclients; i++)
3786 struct MeshTunnelClientInfo *clinfo;
3789 clinfo = &t->clients_fc[i];
3790 delta = clinfo->bck_ack - clinfo->bck_pid;
3791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3792 t->clients[i]->id, delta);
3794 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3795 (GNUNET_YES == t->nobuffer && 0 == delta))
3799 ack = clinfo->bck_pid;
3800 ack += t->nobuffer ? 1 : tunnel_delta;
3801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3802 " sending ack to client %u: %u\n",
3803 t->clients[i]->id, ack);
3804 send_local_ack (t, t->clients[i], ack);
3805 clinfo->bck_ack = ack;
3812 * Send an ACK informing the children nodes and destination clients about
3813 * the available buffer space.
3814 * If buffering is off, send only on behalf of root (can be self).
3815 * If buffering is on, send when sent to predecessor and buffer space is free.
3816 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3817 * the ACK itself goes "forward" (towards children/clients).
3819 * @param t Tunnel on which to send the ACK.
3820 * @param type Type of message that triggered the ACK transmission.
3823 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3826 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3827 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3828 /* Is it after data to_origin retransmission? */
3831 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3832 if (GNUNET_YES == t->nobuffer)
3834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3835 " Not sending ACK, nobuffer\n");
3839 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3840 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3846 tunnel_send_clients_bck_ack (t);
3847 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3852 * Send a message to all peers in this tunnel that the tunnel is no longer
3855 * @param t The tunnel whose peers to notify.
3858 tunnel_send_destroy (struct MeshTunnel *t)
3860 struct GNUNET_MESH_TunnelDestroy msg;
3862 msg.header.size = htons (sizeof (msg));
3863 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3864 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3865 msg.tid = htonl (t->id.tid);
3866 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3871 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3873 * @param cls Closure (Tunnel which to cancel).
3874 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3877 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3879 struct MeshTunnel *t = cls;
3880 struct MeshPeerInfo *peer_info;
3881 struct MeshPeerQueue *pq;
3882 struct MeshPeerQueue *next;
3884 peer_info = peer_info_get_short (neighbor_id);
3885 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3888 if (pq->tunnel == t)
3890 queue_destroy (pq, GNUNET_YES);
3893 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3895 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3896 peer_info->core_transmit = NULL;
3901 * Destroy the tunnel and free any allocated resources linked to it.
3903 * @param t the tunnel to destroy
3905 * @return GNUNET_OK on success
3908 tunnel_destroy (struct MeshTunnel *t)
3910 struct MeshClient *c;
3911 struct GNUNET_HashCode hash;
3918 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3924 struct GNUNET_PeerIdentity id;
3926 GNUNET_PEER_resolve (t->id.oid, &id);
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3928 GNUNET_i2s (&id), t->id.tid);
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3934 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3935 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3940 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3943 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3947 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3948 for (i = 0; i < t->nclients; i++)
3952 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3957 for (i = 0; i < t->nignore; i++)
3961 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3966 if (t->nclients > 0)
3969 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3973 GNUNET_free (t->clients);
3975 if (NULL != t->peers)
3977 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3979 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3982 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3983 &tunnel_destroy_child,
3985 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3987 tree_destroy (t->tree);
3989 if (NULL != t->regex_ctx)
3990 regex_cancel_search (t->regex_ctx);
3991 if (NULL != t->dht_get_type)
3992 GNUNET_DHT_get_stop (t->dht_get_type);
3993 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3994 GNUNET_SCHEDULER_cancel (t->timeout_task);
3995 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3996 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3999 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4000 GNUNET_assert (0 <= n_tunnels);
4007 * Create a new tunnel
4009 * @param owner Who is the owner of the tunnel (short ID).
4010 * @param tid Tunnel Number of the tunnel.
4011 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4012 * @param local Tunnel Number for the tunnel, for the client point of view.
4014 * @return A new initialized tunnel. NULL on error.
4016 static struct MeshTunnel *
4017 tunnel_new (GNUNET_PEER_Id owner,
4018 MESH_TunnelNumber tid,
4019 struct MeshClient *client,
4020 MESH_TunnelNumber local)
4022 struct MeshTunnel *t;
4023 struct GNUNET_HashCode hash;
4025 if (n_tunnels >= max_tunnels && NULL == client)
4028 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4031 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4032 t->bck_queue_max = t->fwd_queue_max;
4033 t->tree = tree_new (owner);
4035 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4036 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4037 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4038 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4039 t->local_tid = local;
4040 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4042 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4044 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4046 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4047 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4054 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4061 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4063 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4064 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4068 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4078 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4079 * that are no longer needed, as well as nodes of no longer reachable peers.
4080 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4082 * @param t Tunnel from which to remove the path.
4083 * @param peer Short id of the peer which should be removed.
4086 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4088 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4094 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4095 * client when the client disconnects. If the client is not the owner, the
4096 * owner will get notified if no more clients are in the tunnel and the client
4097 * get removed from the tunnel's list.
4099 * @param cls closure (client that is disconnecting)
4100 * @param key the hash of the local tunnel id (used to access the hashmap)
4101 * @param value the value stored at the key (tunnel to destroy)
4103 * @return GNUNET_OK on success
4106 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4108 struct MeshTunnel *t = value;
4109 struct MeshClient *c = cls;
4112 send_client_tunnel_disconnect(t, c);
4115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4116 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4117 tunnel_delete_client(t, c);
4118 client_delete_tunnel(c, t);
4121 tunnel_send_destroy(t);
4122 r = tunnel_destroy (t);
4128 * Timeout function, destroys tunnel if called
4130 * @param cls Closure (tunnel to destroy).
4131 * @param tc TaskContext
4134 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4136 struct MeshTunnel *t = cls;
4138 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4140 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4145 * Resets the tunnel timeout. Starts it if no timeout was running.
4147 * @param t Tunnel whose timeout to reset.
4149 * TODO use heap to improve efficiency of scheduler.
4152 tunnel_reset_timeout (struct MeshTunnel *t)
4154 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4155 GNUNET_SCHEDULER_cancel (t->timeout_task);
4157 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4158 (refresh_path_time, 4), &tunnel_timeout, t);
4162 /******************************************************************************/
4163 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4164 /******************************************************************************/
4167 * Function to send a create path packet to a peer.
4169 * @param cls closure
4170 * @param size number of bytes available in buf
4171 * @param buf where the callee should write the message
4172 * @return number of bytes written to buf
4175 send_core_path_create (void *cls, size_t size, void *buf)
4177 struct MeshPathInfo *info = cls;
4178 struct GNUNET_MESH_ManipulatePath *msg;
4179 struct GNUNET_PeerIdentity *peer_ptr;
4180 struct MeshTunnel *t = info->t;
4181 struct MeshPeerPath *p = info->path;
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4188 sizeof (struct GNUNET_MESH_ManipulatePath) +
4189 p->length * sizeof (struct GNUNET_PeerIdentity);
4191 if (size < size_needed || NULL == buf)
4196 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4197 msg->header.size = htons (size_needed);
4198 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4199 msg->tid = ntohl (t->id.tid);
4201 if (GNUNET_YES == t->speed_min)
4202 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4203 if (GNUNET_YES == t->nobuffer)
4204 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4205 msg->opt = htonl(opt);
4207 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4208 for (i = 0; i < p->length; i++)
4210 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4217 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4223 * Fill the core buffer
4225 * @param cls closure (data itself)
4226 * @param size number of bytes available in buf
4227 * @param buf where the callee should write the message
4229 * @return number of bytes written to buf
4232 send_core_data_multicast (void *cls, size_t size, void *buf)
4234 struct MeshTransmissionDescriptor *info = cls;
4237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4238 GNUNET_assert (NULL != info);
4239 GNUNET_assert (NULL != info->peer);
4240 total_size = info->mesh_data->data_len;
4241 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4243 if (total_size > size)
4248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4249 memcpy (buf, info->mesh_data->data, total_size);
4252 struct GNUNET_MESH_Multicast *mc;
4253 struct GNUNET_MessageHeader *mh;
4256 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4258 mc = (struct GNUNET_MESH_Multicast *) mh;
4259 mh = (struct GNUNET_MessageHeader *) &mc[1];
4260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4261 " multicast, payload type %s\n",
4262 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4264 " multicast, payload size %u\n", ntohs (mh->size));
4268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4269 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4273 data_descriptor_decrement_rc (info->mesh_data);
4274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4282 * Creates a path ack message in buf and frees all unused resources.
4284 * @param cls closure (MeshTransmissionDescriptor)
4285 * @param size number of bytes available in buf
4286 * @param buf where the callee should write the message
4287 * @return number of bytes written to buf
4290 send_core_path_ack (void *cls, size_t size, void *buf)
4292 struct MeshTransmissionDescriptor *info = cls;
4293 struct GNUNET_MESH_PathACK *msg = buf;
4295 GNUNET_assert (NULL != info);
4296 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4301 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4302 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4303 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4304 msg->tid = htonl (info->origin->tid);
4305 msg->peer_id = my_full_id;
4308 /* TODO add signature */
4310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4311 return sizeof (struct GNUNET_MESH_PathACK);
4316 * Free a transmission that was already queued with all resources
4317 * associated to the request.
4319 * @param queue Queue handler to cancel.
4320 * @param clear_cls Is it necessary to free associated cls?
4323 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4325 struct MeshTransmissionDescriptor *dd;
4326 struct MeshPathInfo *path_info;
4328 if (GNUNET_YES == clear_cls)
4330 switch (queue->type)
4332 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4333 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4334 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4337 data_descriptor_decrement_rc (dd->mesh_data);
4339 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4341 path_info = queue->cls;
4342 path_destroy (path_info->path);
4346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4348 GNUNET_free_non_null (queue->cls);
4350 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4351 queue->peer->queue_tail,
4353 GNUNET_free (queue);
4358 * @brief Get the next transmittable message from the queue.
4360 * This will be the head, expect in the case of being a data packet
4361 * not allowed by the destination peer.
4363 * @param peer Destination peer.
4365 * @return The next viable MeshPeerQueue element to send to that peer.
4366 * NULL when there are no transmittable messages.
4368 struct MeshPeerQueue *
4369 queue_get_next (static struct MeshPeerInfo *peer)
4371 struct MeshPeerQueue *q;
4372 struct MeshTunnel *t;
4373 struct MeshTransmissionDescriptor *info;
4375 for (q = peer->queue_head; NULL != q; q = q->next)
4380 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4383 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4385 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4397 * Core callback to write a queued packet to core buffer
4399 * @param cls Closure (peer info).
4400 * @param size Number of bytes available in buf.
4401 * @param buf Where the to write the message.
4403 * @return number of bytes written to buf
4406 queue_send (void *cls, size_t size, void *buf)
4408 struct MeshPeerInfo *peer = cls;
4409 struct GNUNET_MessageHeader *msg;
4410 struct MeshPeerQueue *queue;
4411 struct MeshTunnel *t;
4414 peer->core_transmit = NULL;
4415 queue = peer->queue_head;
4417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4419 /* If queue is empty, send should have been cancelled */
4425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4427 /* Check if buffer size is enough for the message */
4428 if (queue->size > size)
4430 struct GNUNET_PeerIdentity id;
4432 GNUNET_PEER_resolve (peer->id, &id);
4433 peer->core_transmit =
4434 GNUNET_CORE_notify_transmit_ready(core_handle,
4437 GNUNET_TIME_UNIT_FOREVER_REL,
4444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4447 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4451 "********* unicast: %u\n",
4454 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4461 switch (queue->type)
4464 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4465 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4466 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4468 "********* raw: %u\n",
4471 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4472 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4473 data_size = send_core_data_raw (queue->cls, size, buf);
4474 msg = (struct GNUNET_MessageHeader *) buf;
4475 switch (ntohs (msg->type)) // Type of preconstructed message
4477 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4478 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4480 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4481 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4487 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4489 data_size = send_core_data_multicast(queue->cls, size, buf);
4490 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4492 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4494 data_size = send_core_path_create(queue->cls, size, buf);
4496 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4498 data_size = send_core_path_ack(queue->cls, size, buf);
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4503 "********* type unknown: %u\n",
4508 /* Free queue, but cls was freed by send_core_* */
4509 queue_destroy (queue, GNUNET_NO);
4511 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4517 /* If more data in queue, send next */
4518 if (NULL != peer->queue_head)
4520 struct GNUNET_PeerIdentity id;
4522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4523 GNUNET_PEER_resolve (peer->id, &id);
4524 peer->core_transmit =
4525 GNUNET_CORE_notify_transmit_ready(core_handle,
4528 GNUNET_TIME_UNIT_FOREVER_REL,
4530 peer->queue_head->size,
4534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4540 * Queue and pass message to core when possible.
4542 * @param cls Closure (type dependant).
4543 * @param type Type of the message, 0 for a raw message.
4544 * @param size Size of the message.
4545 * @param dst Neighbor to send message to.
4546 * @param t Tunnel this message belongs to.
4549 queue_add (void *cls, uint16_t type, size_t size,
4550 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4552 struct MeshPeerQueue *queue;
4557 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4558 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4560 n = &t->fwd_queue_n;
4561 max = &t->fwd_queue_max;
4563 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4565 n = &t->bck_queue_n;
4566 max = &t->bck_queue_max;
4571 if (NULL == t->owner)
4572 GNUNET_break_op(0); // TODO: kill connection?
4575 return; // Drop message
4579 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4585 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4586 if (NULL == dst->core_transmit)
4588 struct GNUNET_PeerIdentity id;
4590 GNUNET_PEER_resolve (dst->id, &id);
4591 dst->core_transmit =
4592 GNUNET_CORE_notify_transmit_ready(core_handle,
4595 GNUNET_TIME_UNIT_FOREVER_REL,
4604 /******************************************************************************/
4605 /******************** MESH NETWORK HANDLERS **************************/
4606 /******************************************************************************/
4610 * Core handler for path creation
4612 * @param cls closure
4613 * @param message message
4614 * @param peer peer identity this notification is about
4615 * @param atsi performance data
4616 * @param atsi_count number of records in 'atsi'
4618 * @return GNUNET_OK to keep the connection open,
4619 * GNUNET_SYSERR to close it (signal serious error)
4622 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4623 const struct GNUNET_MessageHeader *message,
4624 const struct GNUNET_ATS_Information *atsi,
4625 unsigned int atsi_count)
4627 unsigned int own_pos;
4630 MESH_TunnelNumber tid;
4631 struct GNUNET_MESH_ManipulatePath *msg;
4632 struct GNUNET_PeerIdentity *pi;
4633 struct GNUNET_HashCode hash;
4634 struct MeshPeerPath *path;
4635 struct MeshPeerInfo *dest_peer_info;
4636 struct MeshPeerInfo *orig_peer_info;
4637 struct MeshTunnel *t;
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4640 "Received a path create msg [%s]\n",
4641 GNUNET_i2s (&my_full_id));
4642 size = ntohs (message->size);
4643 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4645 GNUNET_break_op (0);
4649 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4650 if (size % sizeof (struct GNUNET_PeerIdentity))
4652 GNUNET_break_op (0);
4655 size /= sizeof (struct GNUNET_PeerIdentity);
4658 GNUNET_break_op (0);
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4662 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4664 tid = ntohl (msg->tid);
4665 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4667 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4668 t = tunnel_get (pi, tid);
4669 if (NULL == t) // FIXME only for INCOMING tunnels?
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4674 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4677 // FIXME notify failure
4680 opt = ntohl (msg->opt);
4681 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4682 GNUNET_YES : GNUNET_NO;
4683 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4684 GNUNET_YES : GNUNET_NO;
4686 if (GNUNET_YES == t->nobuffer)
4688 t->bck_queue_max = 1;
4689 t->fwd_queue_max = 1;
4692 // FIXME only assign a local tid if a local client is interested (on demand)
4693 while (NULL != tunnel_get_incoming (next_local_tid))
4694 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4695 t->local_tid_dest = next_local_tid++;
4696 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4699 tunnel_reset_timeout (t);
4700 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4702 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4703 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4711 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4712 if (NULL == dest_peer_info)
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4715 " Creating PeerInfo for destination.\n");
4716 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4717 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4718 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4720 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4722 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4723 if (NULL == orig_peer_info)
4725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4726 " Creating PeerInfo for origin.\n");
4727 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4728 orig_peer_info->id = GNUNET_PEER_intern (pi);
4729 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4730 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4733 path = path_new (size);
4735 for (i = 0; i < size; i++)
4737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4738 GNUNET_i2s (&pi[i]));
4739 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4740 if (path->peers[i] == myid)
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4746 /* cannot be self, must be 'not found' */
4747 /* create path: self not found in path through self */
4748 GNUNET_break_op (0);
4749 path_destroy (path);
4753 path_add_to_peers (path, GNUNET_NO);
4754 tunnel_add_path (t, path, own_pos);
4755 if (own_pos == size - 1)
4757 /* It is for us! Send ack. */
4758 struct MeshTransmissionDescriptor *info;
4760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4761 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4762 if (NULL == t->peers)
4764 /* New tunnel! Notify clients on data. */
4765 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4767 GNUNET_break (GNUNET_SYSERR !=
4768 GNUNET_CONTAINER_multihashmap_put (t->peers,
4769 &my_full_id.hashPubKey,
4772 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4773 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4774 info->origin = &t->id;
4775 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4776 GNUNET_assert (NULL != info->peer);
4778 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4779 sizeof (struct GNUNET_MESH_PathACK),
4785 struct MeshPeerPath *path2;
4787 /* It's for somebody else! Retransmit. */
4788 path2 = path_duplicate (path);
4789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4790 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4791 path2 = path_duplicate (path);
4792 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4793 send_create_path (dest_peer_info, path, t);
4800 * Core handler for path destruction
4802 * @param cls closure
4803 * @param message message
4804 * @param peer peer identity this notification is about
4805 * @param atsi performance data
4806 * @param atsi_count number of records in 'atsi'
4808 * @return GNUNET_OK to keep the connection open,
4809 * GNUNET_SYSERR to close it (signal serious error)
4812 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4813 const struct GNUNET_MessageHeader *message,
4814 const struct GNUNET_ATS_Information *atsi,
4815 unsigned int atsi_count)
4817 struct GNUNET_MESH_ManipulatePath *msg;
4818 struct GNUNET_PeerIdentity *pi;
4819 struct MeshPeerPath *path;
4820 struct MeshTunnel *t;
4821 unsigned int own_pos;
4825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4826 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4827 size = ntohs (message->size);
4828 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4830 GNUNET_break_op (0);
4834 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4835 if (size % sizeof (struct GNUNET_PeerIdentity))
4837 GNUNET_break_op (0);
4840 size /= sizeof (struct GNUNET_PeerIdentity);
4843 GNUNET_break_op (0);
4846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4848 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4849 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4851 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4853 t = tunnel_get (pi, ntohl (msg->tid));
4856 /* TODO notify back: we don't know this tunnel */
4857 GNUNET_break_op (0);
4860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4861 path = path_new (size);
4863 for (i = 0; i < size; i++)
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4866 GNUNET_i2s (&pi[i]));
4867 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4868 if (path->peers[i] == myid)
4871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4872 if (own_pos < path->length - 1)
4873 send_message (message, &pi[own_pos + 1], t);
4875 send_client_tunnel_disconnect(t, NULL);
4877 tunnel_delete_peer (t, path->peers[path->length - 1]);
4878 path_destroy (path);
4884 * Core handler for notifications of broken paths
4886 * @param cls closure
4887 * @param message message
4888 * @param peer peer identity this notification is about
4889 * @param atsi performance data
4890 * @param atsi_count number of records in 'atsi'
4892 * @return GNUNET_OK to keep the connection open,
4893 * GNUNET_SYSERR to close it (signal serious error)
4896 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4897 const struct GNUNET_MessageHeader *message,
4898 const struct GNUNET_ATS_Information *atsi,
4899 unsigned int atsi_count)
4901 struct GNUNET_MESH_PathBroken *msg;
4902 struct MeshTunnel *t;
4904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4905 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4906 msg = (struct GNUNET_MESH_PathBroken *) message;
4907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4908 GNUNET_i2s (&msg->peer1));
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4910 GNUNET_i2s (&msg->peer2));
4911 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4914 GNUNET_break_op (0);
4917 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4918 GNUNET_PEER_search (&msg->peer2));
4925 * Core handler for tunnel destruction
4927 * @param cls closure
4928 * @param message message
4929 * @param peer peer identity this notification is about
4930 * @param atsi performance data
4931 * @param atsi_count number of records in 'atsi'
4933 * @return GNUNET_OK to keep the connection open,
4934 * GNUNET_SYSERR to close it (signal serious error)
4937 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4938 const struct GNUNET_MessageHeader *message,
4939 const struct GNUNET_ATS_Information *atsi,
4940 unsigned int atsi_count)
4942 struct GNUNET_MESH_TunnelDestroy *msg;
4943 struct MeshTunnel *t;
4945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4946 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4947 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4949 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4950 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4953 /* Probably already got the message from another path,
4954 * destroyed the tunnel and retransmitted to children.
4957 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4960 if (t->id.oid == myid)
4962 GNUNET_break_op (0);
4965 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4967 /* Tunnel was incoming, notify clients */
4968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4969 t->local_tid, t->local_tid_dest);
4970 send_clients_tunnel_destroy (t);
4972 tunnel_send_destroy (t);
4973 t->destroy = GNUNET_YES;
4974 // TODO: add timeout to destroy the tunnel anyway
4980 * Core handler for mesh network traffic going from the origin to a peer
4982 * @param cls closure
4983 * @param peer peer identity this notification is about
4984 * @param message message
4985 * @param atsi performance data
4986 * @param atsi_count number of records in 'atsi'
4987 * @return GNUNET_OK to keep the connection open,
4988 * GNUNET_SYSERR to close it (signal serious error)
4991 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4992 const struct GNUNET_MessageHeader *message,
4993 const struct GNUNET_ATS_Information *atsi,
4994 unsigned int atsi_count)
4996 struct GNUNET_MESH_Unicast *msg;
4997 struct GNUNET_PeerIdentity *neighbor;
4998 struct MeshTunnelChildInfo *cinfo;
4999 struct MeshTunnel *t;
5000 GNUNET_PEER_Id dest_id;
5005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5008 size = ntohs (message->size);
5010 sizeof (struct GNUNET_MESH_Unicast) +
5011 sizeof (struct GNUNET_MessageHeader))
5016 msg = (struct GNUNET_MESH_Unicast *) message;
5017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5018 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5020 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5023 /* TODO notify back: we don't know this tunnel */
5024 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5025 GNUNET_break_op (0);
5028 pid = ntohl (msg->pid);
5029 if (t->fwd_pid == pid)
5031 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5032 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5033 " Already seen pid %u, DROPPING!\n", pid);
5038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5039 " pid %u not seen yet, forwarding\n", pid);
5042 t->skip += (pid - t->fwd_pid) - 1;
5045 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5047 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5048 GNUNET_break_op (0);
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5050 "Received PID %u, ACK %u\n",
5051 pid, t->last_fwd_ack);
5055 tunnel_reset_timeout (t);
5056 dest_id = GNUNET_PEER_search (&msg->destination);
5057 if (dest_id == myid)
5059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5060 " it's for us! sending to clients...\n");
5061 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5062 send_subscribed_clients (message, &msg[1].header, t);
5063 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5066 ttl = ntohl (msg->ttl);
5067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5070 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5072 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5076 " not for us, retransmitting...\n");
5078 neighbor = tree_get_first_hop (t->tree, dest_id);
5079 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5081 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5084 if (GNUNET_YES == t->nobuffer &&
5085 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5087 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5088 GNUNET_break_op (0);
5091 send_message (message, neighbor, t);
5092 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5098 * Core handler for mesh network traffic going from the origin to all peers
5100 * @param cls closure
5101 * @param message message
5102 * @param peer peer identity this notification is about
5103 * @param atsi performance data
5104 * @param atsi_count number of records in 'atsi'
5105 * @return GNUNET_OK to keep the connection open,
5106 * GNUNET_SYSERR to close it (signal serious error)
5108 * TODO: Check who we got this from, to validate route.
5111 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5112 const struct GNUNET_MessageHeader *message,
5113 const struct GNUNET_ATS_Information *atsi,
5114 unsigned int atsi_count)
5116 struct GNUNET_MESH_Multicast *msg;
5117 struct MeshTunnel *t;
5121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5123 size = ntohs (message->size);
5124 if (sizeof (struct GNUNET_MESH_Multicast) +
5125 sizeof (struct GNUNET_MessageHeader) > size)
5127 GNUNET_break_op (0);
5130 msg = (struct GNUNET_MESH_Multicast *) message;
5131 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5135 /* TODO notify that we dont know that tunnel */
5136 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5137 GNUNET_break_op (0);
5140 pid = ntohl (msg->pid);
5141 if (t->fwd_pid == pid)
5143 /* already seen this packet, drop */
5144 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5146 " Already seen pid %u, DROPPING!\n", pid);
5147 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5153 " pid %u not seen yet, forwarding\n", pid);
5155 t->skip += (pid - t->fwd_pid) - 1;
5157 tunnel_reset_timeout (t);
5159 /* Transmit to locally interested clients */
5160 if (NULL != t->peers &&
5161 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5163 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5164 send_subscribed_clients (message, &msg[1].header, t);
5165 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5168 if (ntohl (msg->ttl) == 0)
5170 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5171 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5172 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5175 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5176 tunnel_send_multicast (t, message, GNUNET_NO);
5182 * Core handler for mesh network traffic toward the owner of a tunnel
5184 * @param cls closure
5185 * @param message message
5186 * @param peer peer identity this notification is about
5187 * @param atsi performance data
5188 * @param atsi_count number of records in 'atsi'
5190 * @return GNUNET_OK to keep the connection open,
5191 * GNUNET_SYSERR to close it (signal serious error)
5194 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5195 const struct GNUNET_MessageHeader *message,
5196 const struct GNUNET_ATS_Information *atsi,
5197 unsigned int atsi_count)
5199 struct GNUNET_MESH_ToOrigin *msg;
5200 struct GNUNET_PeerIdentity id;
5201 struct MeshPeerInfo *peer_info;
5202 struct MeshTunnel *t;
5206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5208 size = ntohs (message->size);
5209 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5210 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5212 GNUNET_break_op (0);
5215 msg = (struct GNUNET_MESH_ToOrigin *) message;
5216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5217 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5218 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5222 /* TODO notify that we dont know this tunnel (whom)? */
5223 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5224 GNUNET_break_op (0);
5228 if (NULL != t->owner)
5231 struct GNUNET_MESH_ToOrigin *copy;
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5234 " it's for us! sending to clients...\n");
5235 /* TODO signature verification */
5236 memcpy (cbuf, message, size);
5237 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5238 copy->tid = htonl (t->local_tid);
5239 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5240 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5241 ©->header, GNUNET_NO);
5242 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5246 " not for us, retransmitting...\n");
5248 peer_info = peer_info_get (&msg->oid);
5249 if (NULL == peer_info)
5251 /* unknown origin of tunnel */
5255 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5256 send_message (message, &id, t);
5257 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5264 * Core handler for mesh network traffic point-to-point acks.
5266 * @param cls closure
5267 * @param message message
5268 * @param peer peer identity this notification is about
5269 * @param atsi performance data
5270 * @param atsi_count number of records in 'atsi'
5272 * @return GNUNET_OK to keep the connection open,
5273 * GNUNET_SYSERR to close it (signal serious error)
5276 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5277 const struct GNUNET_MessageHeader *message,
5278 const struct GNUNET_ATS_Information *atsi,
5279 unsigned int atsi_count)
5281 struct GNUNET_MESH_ACK *msg;
5282 struct MeshTunnel *t;
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5287 msg = (struct GNUNET_MESH_ACK *) message;
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5289 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5290 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5294 /* TODO notify that we dont know this tunnel (whom)? */
5295 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5296 GNUNET_break_op (0);
5299 ack = ntohl (msg->pid);
5301 /* Is this a forward or backward ACK? */
5302 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5304 struct MeshTunnelChildInfo *cinfo;
5306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5307 cinfo = tunnel_get_neighbor_fc (t, peer);
5308 cinfo->fwd_ack = ack;
5309 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5315 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5317 // FIXME fc Unlock queues?
5323 * Core handler for path ACKs
5325 * @param cls closure
5326 * @param message message
5327 * @param peer peer identity this notification is about
5328 * @param atsi performance data
5329 * @param atsi_count number of records in 'atsi'
5331 * @return GNUNET_OK to keep the connection open,
5332 * GNUNET_SYSERR to close it (signal serious error)
5335 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5336 const struct GNUNET_MessageHeader *message,
5337 const struct GNUNET_ATS_Information *atsi,
5338 unsigned int atsi_count)
5340 struct GNUNET_MESH_PathACK *msg;
5341 struct GNUNET_PeerIdentity id;
5342 struct MeshPeerInfo *peer_info;
5343 struct MeshPeerPath *p;
5344 struct MeshTunnel *t;
5346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5347 GNUNET_i2s (&my_full_id));
5348 msg = (struct GNUNET_MESH_PathACK *) message;
5349 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5352 /* TODO notify that we don't know the tunnel */
5353 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5355 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5359 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5361 peer_info = peer_info_get (&msg->peer_id);
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5363 GNUNET_i2s (&msg->peer_id));
5364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5367 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5370 "connect_by_string completed, stopping search\n");
5371 regex_cancel_search (t->regex_ctx);
5372 t->regex_ctx = NULL;
5375 /* Add paths to peers? */
5376 p = tree_get_path_to_peer (t->tree, peer_info->id);
5379 path_add_to_peers (p, GNUNET_YES);
5387 /* Message for us? */
5388 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5391 if (NULL == t->owner)
5393 GNUNET_break_op (0);
5396 if (NULL != t->dht_get_type)
5398 GNUNET_DHT_get_stop (t->dht_get_type);
5399 t->dht_get_type = NULL;
5401 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5403 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5404 send_client_peer_connected (t, peer_info->id);
5409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5410 " not for us, retransmitting...\n");
5411 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5412 peer_info = peer_info_get (&msg->oid);
5413 if (NULL == peer_info)
5415 /* If we know the tunnel, we should DEFINITELY know the peer */
5419 send_message (message, &id, t);
5425 * Functions to handle messages from core
5427 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5428 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5429 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5430 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5431 sizeof (struct GNUNET_MESH_PathBroken)},
5432 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5433 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5434 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5435 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5436 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5437 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5438 sizeof (struct GNUNET_MESH_ACK)},
5439 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5440 sizeof (struct GNUNET_MESH_PathACK)},
5446 /******************************************************************************/
5447 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5448 /******************************************************************************/
5451 * deregister_app: iterator for removing each application registered by a client
5453 * @param cls closure
5454 * @param key the hash of the application id (used to access the hashmap)
5455 * @param value the value stored at the key (client)
5457 * @return GNUNET_OK on success
5460 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5462 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5463 GNUNET_break (GNUNET_YES ==
5464 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5470 * notify_client_connection_failure: notify a client that the connection to the
5471 * requested remote peer is not possible (for instance, no route found)
5472 * Function called when the socket is ready to queue more data. "buf" will be
5473 * NULL and "size" zero if the socket was closed for writing in the meantime.
5475 * @param cls closure
5476 * @param size number of bytes available in buf
5477 * @param buf where the callee should write the message
5478 * @return number of bytes written to buf
5481 notify_client_connection_failure (void *cls, size_t size, void *buf)
5484 struct MeshPeerInfo *peer_info;
5485 struct GNUNET_MESH_PeerControl *msg;
5486 struct GNUNET_PeerIdentity id;
5488 if (0 == size && NULL == buf)
5490 // TODO retry? cancel?
5494 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5495 peer_info = (struct MeshPeerInfo *) cls;
5496 msg = (struct GNUNET_MESH_PeerControl *) buf;
5497 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5498 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5499 // msg->tunnel_id = htonl(peer_info->t->tid);
5500 GNUNET_PEER_resolve (peer_info->id, &id);
5501 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5509 * Send keepalive packets for a peer
5511 * @param cls Closure (tunnel for which to send the keepalive).
5512 * @param tc Notification context.
5514 * TODO: implement explicit multicast keepalive?
5517 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5519 struct MeshTunnel *t = cls;
5520 struct GNUNET_MessageHeader *payload;
5521 struct GNUNET_MESH_Multicast *msg;
5523 sizeof (struct GNUNET_MESH_Multicast) +
5524 sizeof (struct GNUNET_MessageHeader);
5527 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5531 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5534 "sending keepalive for tunnel %d\n", t->id.tid);
5536 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5537 msg->header.size = htons (size);
5538 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5539 msg->oid = my_full_id;
5540 msg->tid = htonl (t->id.tid);
5541 msg->ttl = htonl (default_ttl);
5542 msg->pid = htonl (t->fwd_pid + 1);
5544 payload = (struct GNUNET_MessageHeader *) &msg[1];
5545 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5546 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5547 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5549 t->path_refresh_task =
5550 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5556 * Function to process paths received for a new peer addition. The recorded
5557 * paths form the initial tunnel, which can be optimized later.
5558 * Called on each result obtained for the DHT search.
5560 * @param cls closure
5561 * @param exp when will this value expire
5562 * @param key key of the result
5563 * @param get_path path of the get request
5564 * @param get_path_length lenght of get_path
5565 * @param put_path path of the put request
5566 * @param put_path_length length of the put_path
5567 * @param type type of the result
5568 * @param size number of bytes in data
5569 * @param data pointer to the result data
5571 * TODO: re-issue the request after certain time? cancel after X results?
5574 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5575 const struct GNUNET_HashCode * key,
5576 const struct GNUNET_PeerIdentity *get_path,
5577 unsigned int get_path_length,
5578 const struct GNUNET_PeerIdentity *put_path,
5579 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5580 size_t size, const void *data)
5582 struct MeshPathInfo *path_info = cls;
5583 struct MeshPeerPath *p;
5584 struct GNUNET_PeerIdentity pi;
5587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5588 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5591 p = path_build_from_dht (get_path, get_path_length, put_path,
5593 path_add_to_peers (p, GNUNET_NO);
5595 for (i = 0; i < path_info->peer->ntunnels; i++)
5597 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5598 peer_info_connect (path_info->peer, path_info->t);
5606 * Function to process paths received for a new peer addition. The recorded
5607 * paths form the initial tunnel, which can be optimized later.
5608 * Called on each result obtained for the DHT search.
5610 * @param cls closure
5611 * @param exp when will this value expire
5612 * @param key key of the result
5613 * @param get_path path of the get request
5614 * @param get_path_length lenght of get_path
5615 * @param put_path path of the put request
5616 * @param put_path_length length of the put_path
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_type_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, enum GNUNET_BLOCK_Type type,
5628 size_t size, const void *data)
5630 const struct PBlock *pb = data;
5631 const struct GNUNET_PeerIdentity *pi = &pb->id;
5632 struct MeshTunnel *t = cls;
5633 struct MeshPeerInfo *peer_info;
5634 struct MeshPeerPath *p;
5636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5637 if (size != sizeof (struct PBlock))
5639 GNUNET_break_op (0);
5642 if (ntohl(pb->type) != t->type)
5644 GNUNET_break_op (0);
5647 GNUNET_assert (NULL != t->owner);
5648 peer_info = peer_info_get (pi);
5649 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5651 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5653 p = path_build_from_dht (get_path, get_path_length, put_path,
5655 path_add_to_peers (p, GNUNET_NO);
5657 tunnel_add_peer (t, peer_info);
5658 peer_info_connect (peer_info, t);
5663 * Function to process DHT string to regex matching.
5664 * Called on each result obtained for the DHT search.
5666 * @param cls closure (search context)
5667 * @param exp when will this value expire
5668 * @param key key of the result
5669 * @param get_path path of the get request (not used)
5670 * @param get_path_length lenght of get_path (not used)
5671 * @param put_path path of the put request (not used)
5672 * @param put_path_length length of the put_path (not used)
5673 * @param type type of the result
5674 * @param size number of bytes in data
5675 * @param data pointer to the result data
5678 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5679 const struct GNUNET_HashCode * key,
5680 const struct GNUNET_PeerIdentity *get_path,
5681 unsigned int get_path_length,
5682 const struct GNUNET_PeerIdentity *put_path,
5683 unsigned int put_path_length,
5684 enum GNUNET_BLOCK_Type type,
5685 size_t size, const void *data)
5687 const struct MeshRegexAccept *block = data;
5688 struct MeshRegexSearchContext *ctx = cls;
5689 struct MeshRegexSearchInfo *info = ctx->info;
5690 struct MeshPeerPath *p;
5691 struct MeshPeerInfo *peer_info;
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5696 peer_info = peer_info_get(&block->id);
5697 p = path_build_from_dht (get_path, get_path_length, put_path,
5699 path_add_to_peers (p, GNUNET_NO);
5702 tunnel_add_peer (info->t, peer_info);
5703 peer_info_connect (peer_info, info->t);
5704 if (0 == info->peer)
5706 info->peer = peer_info->id;
5710 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5713 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5714 ®ex_connect_timeout,
5722 * Function to process DHT string to regex matching.
5723 * Called on each result obtained for the DHT search.
5725 * @param cls closure (search context)
5726 * @param exp when will this value expire
5727 * @param key key of the result
5728 * @param get_path path of the get request (not used)
5729 * @param get_path_length lenght of get_path (not used)
5730 * @param put_path path of the put request (not used)
5731 * @param put_path_length length of the put_path (not used)
5732 * @param type type of the result
5733 * @param size number of bytes in data
5734 * @param data pointer to the result data
5736 * TODO: re-issue the request after certain time? cancel after X results?
5739 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5740 const struct GNUNET_HashCode * key,
5741 const struct GNUNET_PeerIdentity *get_path,
5742 unsigned int get_path_length,
5743 const struct GNUNET_PeerIdentity *put_path,
5744 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5745 size_t size, const void *data)
5747 const struct MeshRegexBlock *block = data;
5748 struct MeshRegexSearchContext *ctx = cls;
5749 struct MeshRegexSearchInfo *info = ctx->info;
5753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5754 "DHT GET STRING RETURNED RESULTS\n");
5755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5756 " key: %s\n", GNUNET_h2s (key));
5758 copy = GNUNET_malloc (size);
5759 memcpy (copy, data, size);
5760 GNUNET_break (GNUNET_OK ==
5761 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5762 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5763 len = ntohl (block->n_proof);
5765 char proof[len + 1];
5767 memcpy (proof, &block[1], len);
5769 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5771 GNUNET_break_op (0);
5775 len = strlen (info->description);
5776 if (len == ctx->position) // String processed
5778 if (GNUNET_YES == ntohl (block->accepting))
5780 regex_find_path(key, ctx);
5784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5785 // FIXME REGEX this block not successful, wait for more? start timeout?
5789 GNUNET_break (GNUNET_OK ==
5790 GNUNET_MESH_regex_block_iterate (block, size,
5791 ®ex_edge_iterator, ctx));
5795 /******************************************************************************/
5796 /********************* MESH LOCAL HANDLES **************************/
5797 /******************************************************************************/
5801 * Handler for client disconnection
5803 * @param cls closure
5804 * @param client identification of the client; NULL
5805 * for the last call when the server is destroyed
5808 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5810 struct MeshClient *c;
5811 struct MeshClient *next;
5814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5823 if (c->handle != client)
5825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5831 GNUNET_SERVER_client_drop (c->handle);
5832 c->shutting_down = GNUNET_YES;
5833 GNUNET_assert (NULL != c->own_tunnels);
5834 GNUNET_assert (NULL != c->incoming_tunnels);
5835 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5836 &tunnel_destroy_iterator, c);
5837 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5838 &tunnel_destroy_iterator, c);
5839 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5840 &tunnel_destroy_iterator, c);
5841 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5842 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5843 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5845 /* deregister clients applications */
5846 if (NULL != c->apps)
5848 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5849 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5851 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5852 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5854 GNUNET_SCHEDULER_cancel (announce_applications_task);
5855 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5857 if (NULL != c->types)
5858 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5859 for (i = 0; i < c->n_regex; i++)
5861 GNUNET_free (c->regexes[i]);
5863 GNUNET_free_non_null (c->regexes);
5864 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5865 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5867 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5870 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5879 * Handler for new clients
5881 * @param cls closure
5882 * @param client identification of the client
5883 * @param message the actual message, which includes messages the client wants
5886 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5887 const struct GNUNET_MessageHeader *message)
5889 struct GNUNET_MESH_ClientConnect *cc_msg;
5890 struct MeshClient *c;
5891 GNUNET_MESH_ApplicationType *a;
5898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5899 /* Check data sanity */
5900 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5901 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5902 ntypes = ntohs (cc_msg->types);
5903 napps = ntohs (cc_msg->applications);
5905 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5908 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5912 /* Create new client structure */
5913 c = GNUNET_malloc (sizeof (struct MeshClient));
5914 c->id = next_client_id++;
5915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5917 GNUNET_SERVER_client_keep (client);
5918 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5921 GNUNET_MESH_ApplicationType at;
5922 struct GNUNET_HashCode hc;
5924 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5925 for (i = 0; i < napps; i++)
5928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5929 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5930 /* store in clients hashmap */
5931 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5932 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5933 /* store in global hashmap, for announcements */
5934 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5935 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5937 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5938 announce_applications_task =
5939 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5945 struct GNUNET_HashCode hc;
5947 t = (uint16_t *) & a[napps];
5948 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5949 for (i = 0; i < ntypes; i++)
5952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5953 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5955 /* store in clients hashmap */
5956 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5957 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5958 /* store in global hashmap */
5959 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5960 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5964 " client has %u+%u subscriptions\n", napps, ntypes);
5966 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5967 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5968 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5969 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5970 GNUNET_SERVER_notification_context_add (nc, client);
5971 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5973 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5979 * Handler for clients announcing available services by a regular expression.
5981 * @param cls closure
5982 * @param client identification of the client
5983 * @param message the actual message, which includes messages the client wants
5986 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5987 const struct GNUNET_MessageHeader *message)
5989 struct MeshClient *c;
5993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5995 /* Sanity check for client registration */
5996 if (NULL == (c = client_get (client)))
5999 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6004 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6005 regex = GNUNET_malloc (len + 1);
6006 memcpy (regex, &message[1], len);
6008 GNUNET_array_append (c->regexes, c->n_regex, regex);
6009 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6011 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6017 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6023 * Handler for requests of new tunnels
6025 * @param cls closure
6026 * @param client identification of the client
6027 * @param message the actual message
6030 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6031 const struct GNUNET_MessageHeader *message)
6033 struct GNUNET_MESH_TunnelMessage *t_msg;
6034 struct MeshTunnel *t;
6035 struct MeshClient *c;
6036 MESH_TunnelNumber tid;
6038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6040 /* Sanity check for client registration */
6041 if (NULL == (c = client_get (client)))
6044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6049 /* Message sanity check */
6050 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6057 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6058 /* Sanity check for tunnel numbering */
6059 tid = ntohl (t_msg->tunnel_id);
6060 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6066 /* Sanity check for duplicate tunnel IDs */
6067 if (NULL != tunnel_get_by_local_id (c, tid))
6070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6074 while (NULL != tunnel_get_by_pi (myid, next_tid))
6075 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6076 t = tunnel_new (myid, next_tid++, c, tid);
6079 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6081 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6084 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6086 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6087 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6096 * Handler for requests of deleting tunnels
6098 * @param cls closure
6099 * @param client identification of the client
6100 * @param message the actual message
6103 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6104 const struct GNUNET_MessageHeader *message)
6106 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6107 struct MeshClient *c;
6108 struct MeshTunnel *t;
6109 MESH_TunnelNumber tid;
6111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6112 "Got a DESTROY TUNNEL from client!\n");
6114 /* Sanity check for client registration */
6115 if (NULL == (c = client_get (client)))
6118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6123 /* Message sanity check */
6124 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6131 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6133 /* Retrieve tunnel */
6134 tid = ntohl (tunnel_msg->tunnel_id);
6135 t = tunnel_get_by_local_id(c, tid);
6138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6143 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6145 client_ignore_tunnel (c, t);
6147 // TODO: when to destroy incoming tunnel?
6148 if (t->nclients == 0)
6150 GNUNET_assert (GNUNET_YES ==
6151 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6153 GNUNET_assert (GNUNET_YES ==
6154 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6155 &my_full_id.hashPubKey,
6159 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6162 send_client_tunnel_disconnect(t, c);
6163 client_delete_tunnel(c, t);
6165 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6167 tunnel_send_destroy (t);
6168 t->destroy = GNUNET_YES;
6169 // The tunnel will be destroyed when the last message is transmitted.
6170 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6176 * Handler for requests of seeting tunnel's speed.
6178 * @param cls Closure (unused).
6179 * @param client Identification of the client.
6180 * @param message The actual message.
6183 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6184 const struct GNUNET_MessageHeader *message)
6186 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6187 struct MeshClient *c;
6188 struct MeshTunnel *t;
6189 MESH_TunnelNumber tid;
6191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6192 "Got a SPEED request from client!\n");
6194 /* Sanity check for client registration */
6195 if (NULL == (c = client_get (client)))
6198 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_WARNING, " 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_MIN:
6220 t->speed_min = GNUNET_YES;
6222 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6223 t->speed_min = GNUNET_NO;
6228 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6233 * Handler for requests of seeting tunnel's buffering policy.
6235 * @param cls Closure (unused).
6236 * @param client Identification of the client.
6237 * @param message The actual message.
6240 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6241 const struct GNUNET_MessageHeader *message)
6243 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6244 struct MeshClient *c;
6245 struct MeshTunnel *t;
6246 MESH_TunnelNumber tid;
6248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6249 "Got a BUFFER request from client!\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 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6262 /* Retrieve tunnel */
6263 tid = ntohl (tunnel_msg->tunnel_id);
6264 t = tunnel_get_by_local_id(c, tid);
6267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6273 switch (ntohs(message->type))
6275 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6276 t->nobuffer = GNUNET_NO;
6278 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6279 t->nobuffer = GNUNET_YES;
6285 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6290 * Handler for connection requests to new peers
6292 * @param cls closure
6293 * @param client identification of the client
6294 * @param message the actual message (PeerControl)
6297 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6298 const struct GNUNET_MessageHeader *message)
6300 struct GNUNET_MESH_PeerControl *peer_msg;
6301 struct MeshPeerInfo *peer_info;
6302 struct MeshClient *c;
6303 struct MeshTunnel *t;
6304 MESH_TunnelNumber tid;
6306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6307 /* Sanity check for client registration */
6308 if (NULL == (c = client_get (client)))
6311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6316 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6318 /* Sanity check for message size */
6319 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6326 /* Tunnel exists? */
6327 tid = ntohl (peer_msg->tunnel_id);
6328 t = tunnel_get_by_local_id (c, tid);
6332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6336 /* Does client own tunnel? */
6337 if (t->owner->handle != client)
6340 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6344 GNUNET_i2s (&peer_msg->peer));
6345 peer_info = peer_info_get (&peer_msg->peer);
6347 tunnel_add_peer (t, peer_info);
6348 peer_info_connect (peer_info, t);
6350 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6356 * Handler for disconnection requests of peers in a tunnel
6358 * @param cls closure
6359 * @param client identification of the client
6360 * @param message the actual message (PeerControl)
6363 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6364 const struct GNUNET_MessageHeader *message)
6366 struct GNUNET_MESH_PeerControl *peer_msg;
6367 struct MeshPeerInfo *peer_info;
6368 struct MeshClient *c;
6369 struct MeshTunnel *t;
6370 MESH_TunnelNumber tid;
6372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6373 /* Sanity check for client registration */
6374 if (NULL == (c = client_get (client)))
6377 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6382 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6384 /* Sanity check for message size */
6385 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6388 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6392 /* Tunnel exists? */
6393 tid = ntohl (peer_msg->tunnel_id);
6394 t = tunnel_get_by_local_id (c, tid);
6398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6403 /* Does client own tunnel? */
6404 if (t->owner->handle != client)
6407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6412 GNUNET_i2s (&peer_msg->peer));
6413 /* Is the peer in the tunnel? */
6415 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6416 if (NULL == peer_info)
6419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6423 /* Ok, delete peer from tunnel */
6424 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6425 &peer_msg->peer.hashPubKey);
6427 send_destroy_path (t, peer_info->id);
6428 tunnel_delete_peer (t, peer_info->id);
6429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6434 * Handler for blacklist requests of peers in a tunnel
6436 * @param cls closure
6437 * @param client identification of the client
6438 * @param message the actual message (PeerControl)
6440 * FIXME implement DHT block bloomfilter
6443 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6444 const struct GNUNET_MessageHeader *message)
6446 struct GNUNET_MESH_PeerControl *peer_msg;
6447 struct MeshClient *c;
6448 struct MeshTunnel *t;
6449 MESH_TunnelNumber tid;
6451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6452 /* Sanity check for client registration */
6453 if (NULL == (c = client_get (client)))
6456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6461 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6463 /* Sanity check for message size */
6464 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6471 /* Tunnel exists? */
6472 tid = ntohl (peer_msg->tunnel_id);
6473 t = tunnel_get_by_local_id (c, tid);
6477 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6482 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6483 GNUNET_PEER_intern(&peer_msg->peer));
6488 * Handler for unblacklist requests of peers in a tunnel
6490 * @param cls closure
6491 * @param client identification of the client
6492 * @param message the actual message (PeerControl)
6495 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6496 const struct GNUNET_MessageHeader *message)
6498 struct GNUNET_MESH_PeerControl *peer_msg;
6499 struct MeshClient *c;
6500 struct MeshTunnel *t;
6501 MESH_TunnelNumber tid;
6505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6506 /* Sanity check for client registration */
6507 if (NULL == (c = client_get (client)))
6510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6515 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6517 /* Sanity check for message size */
6518 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6521 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6525 /* Tunnel exists? */
6526 tid = ntohl (peer_msg->tunnel_id);
6527 t = tunnel_get_by_local_id (c, tid);
6531 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6536 /* if peer is not known, complain */
6537 pid = GNUNET_PEER_search (&peer_msg->peer);
6544 /* search and remove from list */
6545 for (i = 0; i < t->nblacklisted; i++)
6547 if (t->blacklisted[i] == pid)
6549 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6550 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6555 /* if peer hasn't been blacklisted, complain */
6561 * Handler for connection requests to new peers by type
6563 * @param cls closure
6564 * @param client identification of the client
6565 * @param message the actual message (ConnectPeerByType)
6568 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6569 const struct GNUNET_MessageHeader *message)
6571 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6572 struct MeshClient *c;
6573 struct MeshTunnel *t;
6574 struct GNUNET_HashCode hash;
6575 MESH_TunnelNumber tid;
6577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6578 /* Sanity check for client registration */
6579 if (NULL == (c = client_get (client)))
6582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6587 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6589 /* Sanity check for message size */
6590 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6591 ntohs (connect_msg->header.size))
6594 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6598 /* Tunnel exists? */
6599 tid = ntohl (connect_msg->tunnel_id);
6600 t = tunnel_get_by_local_id (c, tid);
6604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6608 /* Does client own tunnel? */
6609 if (t->owner->handle != client)
6612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6616 /* Do WE have the service? */
6617 t->type = ntohl (connect_msg->type);
6618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6619 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6620 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6623 /* Yes! Fast forward, add ourselves to the tunnel and send the
6624 * good news to the client, and alert the destination client of
6625 * an incoming tunnel.
6627 * FIXME send a path create to self, avoid code duplication
6629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6630 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6631 peer_info_get (&my_full_id),
6632 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6635 send_client_peer_connected (t, myid);
6636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6637 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6639 t->local_tid_dest = next_local_tid++;
6640 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6641 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6642 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6646 /* Ok, lets find a peer offering the service */
6647 if (NULL != t->dht_get_type)
6649 GNUNET_DHT_get_stop (t->dht_get_type);
6651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6652 GNUNET_h2s (&hash));
6654 GNUNET_DHT_get_start (dht_handle,
6655 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6657 dht_replication_level,
6658 GNUNET_DHT_RO_RECORD_ROUTE |
6659 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6661 &dht_get_type_handler, t);
6663 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6669 * Handler for connection requests to new peers by a string service description.
6671 * @param cls closure
6672 * @param client identification of the client
6673 * @param message the actual message, which includes messages the client wants
6676 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6677 const struct GNUNET_MessageHeader *message)
6679 struct GNUNET_MESH_ConnectPeerByString *msg;
6680 struct MeshRegexSearchContext *ctx;
6681 struct MeshRegexSearchInfo *info;
6682 struct GNUNET_DHT_GetHandle *get_h;
6683 struct GNUNET_HashCode key;
6684 struct MeshTunnel *t;
6685 struct MeshClient *c;
6686 MESH_TunnelNumber tid;
6690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6691 "Connect by string started\n");
6692 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6693 size = htons (message->size);
6695 /* Sanity check for client registration */
6696 if (NULL == (c = client_get (client)))
6699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6704 /* Message size sanity check */
6705 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6712 /* Tunnel exists? */
6713 tid = ntohl (msg->tunnel_id);
6714 t = tunnel_get_by_local_id (c, tid);
6718 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6722 /* Does client own tunnel? */
6723 if (t->owner->handle != client)
6726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6731 " on tunnel %s [%u]\n",
6732 GNUNET_i2s(&my_full_id),
6735 /* Only one connect_by_string allowed at the same time! */
6736 /* FIXME: allow more, return handle at api level to cancel, document */
6737 if (NULL != t->regex_ctx)
6740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6744 /* Find string itself */
6745 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6746 string = (const char *) &msg[1];
6748 /* Initialize context */
6749 size = GNUNET_REGEX_get_first_key(string, len, &key);
6750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6751 " consumed %u bits out of %u\n", size, len);
6752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6753 " looking for %s\n", GNUNET_h2s (&key));
6755 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6757 info->description = GNUNET_malloc (len + 1);
6758 memcpy (info->description, string, len);
6759 info->description[len] = '\0';
6760 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6761 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6764 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6765 ctx->position = size;
6769 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6771 /* Start search in DHT */
6772 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6773 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6774 &key, /* key to search */
6775 dht_replication_level, /* replication level */
6776 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6777 NULL, /* xquery */ // FIXME BLOOMFILTER
6778 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6779 &dht_get_string_handler, ctx);
6781 GNUNET_break (GNUNET_OK ==
6782 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6785 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6787 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6793 * Handler for client traffic directed to one peer
6795 * @param cls closure
6796 * @param client identification of the client
6797 * @param message the actual message
6800 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6801 const struct GNUNET_MessageHeader *message)
6803 struct MeshClient *c;
6804 struct MeshTunnel *t;
6805 struct MeshPeerInfo *pi;
6806 struct GNUNET_MESH_Unicast *data_msg;
6807 MESH_TunnelNumber tid;
6810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6811 "Got a unicast request from a client!\n");
6813 /* Sanity check for client registration */
6814 if (NULL == (c = client_get (client)))
6817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6822 data_msg = (struct GNUNET_MESH_Unicast *) message;
6824 /* Sanity check for message size */
6825 size = ntohs (message->size);
6826 if (sizeof (struct GNUNET_MESH_Unicast) +
6827 sizeof (struct GNUNET_MessageHeader) > size)
6830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6834 /* Tunnel exists? */
6835 tid = ntohl (data_msg->tid);
6836 t = tunnel_get_by_local_id (c, tid);
6840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6844 /* Is it a local tunnel? Then, does client own the tunnel? */
6845 if (t->owner->handle != client)
6848 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6852 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6853 &data_msg->destination.hashPubKey);
6854 /* Is the selected peer in the tunnel? */
6858 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6862 /* PID should be as expected */
6863 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6866 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6867 "Unicast PID, expected %u, got %u\n",
6868 t->fwd_pid + 1, ntohl (data_msg->pid));
6869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6873 /* Ok, everything is correct, send the message
6874 * (pretend we got it from a mesh peer)
6877 /* Work around const limitation */
6878 char buf[ntohs (message->size)] GNUNET_ALIGN;
6879 struct GNUNET_MESH_Unicast *copy;
6881 copy = (struct GNUNET_MESH_Unicast *) buf;
6882 memcpy (buf, data_msg, size);
6883 copy->oid = my_full_id;
6884 copy->tid = htonl (t->id.tid);
6885 copy->ttl = htonl (default_ttl);
6886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6887 " calling generic handler...\n");
6888 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6891 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6898 * Handler for client traffic directed to the origin
6900 * @param cls closure
6901 * @param client identification of the client
6902 * @param message the actual message
6905 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6906 const struct GNUNET_MessageHeader *message)
6908 struct GNUNET_MESH_ToOrigin *data_msg;
6909 struct MeshTunnelClientInfo *clinfo;
6910 struct MeshClient *c;
6911 struct MeshTunnel *t;
6912 MESH_TunnelNumber tid;
6915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6916 "Got a ToOrigin request from a client!\n");
6917 /* Sanity check for client registration */
6918 if (NULL == (c = client_get (client)))
6921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6926 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6928 /* Sanity check for message size */
6929 size = ntohs (message->size);
6930 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6931 sizeof (struct GNUNET_MessageHeader) > size)
6934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6938 /* Tunnel exists? */
6939 tid = ntohl (data_msg->tid);
6940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6941 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6947 t = tunnel_get_by_local_id (c, tid);
6950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
6951 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6953 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is owner)\n");
6955 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is leaf)\n");
6957 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6961 /* It should be sent by someone who has this as incoming tunnel. */
6962 if (GNUNET_NO == client_knows_tunnel (c, t))
6965 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6969 /* PID should be as expected */
6970 clinfo = tunnel_get_client_fc (t, c);
6971 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6974 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6975 "To Origin PID, expected %u, got %u\n",
6976 clinfo->bck_pid + 1,
6977 ntohl (data_msg->pid));
6978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6982 /* Ok, everything is correct, send the message
6983 * (pretend we got it from a mesh peer)
6987 char buf[ntohs (message->size)] GNUNET_ALIGN;
6988 struct GNUNET_MESH_ToOrigin *copy;
6990 /* Work around const limitation */
6991 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6992 memcpy (buf, data_msg, size);
6993 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6994 copy->tid = htonl (t->id.tid);
6995 copy->ttl = htonl (default_ttl);
6996 if (ntohl (copy->pid) != (t->bck_pid + 1))
6999 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7000 "To Origin PID, expected %u, got %u\n",
7006 copy->sender = my_full_id;
7007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7008 " calling generic handler...\n");
7009 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7011 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7018 * Handler for client traffic directed to all peers in a tunnel
7020 * @param cls closure
7021 * @param client identification of the client
7022 * @param message the actual message
7025 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7026 const struct GNUNET_MessageHeader *message)
7028 struct MeshClient *c;
7029 struct MeshTunnel *t;
7030 struct GNUNET_MESH_Multicast *data_msg;
7031 MESH_TunnelNumber tid;
7033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7034 "Got a multicast request from a client!\n");
7036 /* Sanity check for client registration */
7037 if (NULL == (c = client_get (client)))
7040 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7045 data_msg = (struct GNUNET_MESH_Multicast *) message;
7047 /* Sanity check for message size */
7048 if (sizeof (struct GNUNET_MESH_Multicast) +
7049 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7056 /* Tunnel exists? */
7057 tid = ntohl (data_msg->tid);
7058 t = tunnel_get_by_local_id (c, tid);
7062 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7063 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7065 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is owner)\n");
7067 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is leaf)\n"); GNUNET_break (0);
7068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7072 /* Does client own tunnel? */
7073 if (t->owner->handle != client)
7076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7080 /* PID should be as expected */
7081 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7084 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7085 "Multicast PID, expected %u, got %u\n",
7086 t->fwd_pid + 1, ntohl (data_msg->pid));
7087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7092 char buf[ntohs (message->size)] GNUNET_ALIGN;
7093 struct GNUNET_MESH_Multicast *copy;
7095 copy = (struct GNUNET_MESH_Multicast *) buf;
7096 memcpy (buf, message, ntohs (message->size));
7097 copy->oid = my_full_id;
7098 copy->tid = htonl (t->id.tid);
7099 copy->ttl = htonl (default_ttl);
7100 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7102 " calling generic handler...\n");
7103 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7106 /* receive done gets called when last copy is sent to a neighbor */
7112 * Handler for client's ACKs for payload traffic.
7114 * @param cls Closure (unused).
7115 * @param client Identification of the client.
7116 * @param message The actual message.
7119 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7120 const struct GNUNET_MessageHeader *message)
7122 struct GNUNET_MESH_LocalAck *msg;
7123 struct MeshTunnel *t;
7124 struct MeshClient *c;
7125 MESH_TunnelNumber tid;
7128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7129 /* Sanity check for client registration */
7130 if (NULL == (c = client_get (client)))
7133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7138 msg = (struct GNUNET_MESH_LocalAck *) message;
7140 /* Tunnel exists? */
7141 tid = ntohl (msg->tunnel_id);
7142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7143 t = tunnel_get_by_local_id (c, tid);
7146 GNUNET_break (0); // FIXME fc
7147 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7150 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is owner)\n");
7152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " (client is leaf)\n");
7153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7157 ack = ntohl (msg->max_pid);
7158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7160 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7161 if (NULL != t->owner && t->owner->handle == client)
7163 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7165 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7169 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7170 tunnel_set_client_fwd_ack (t, c, ack);
7171 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7174 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7181 * Functions to handle messages from clients
7183 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7184 {&handle_local_new_client, NULL,
7185 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7186 {&handle_local_announce_regex, NULL,
7187 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7188 {&handle_local_tunnel_create, NULL,
7189 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7190 sizeof (struct GNUNET_MESH_TunnelMessage)},
7191 {&handle_local_tunnel_destroy, NULL,
7192 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7193 sizeof (struct GNUNET_MESH_TunnelMessage)},
7194 {&handle_local_tunnel_speed, NULL,
7195 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7196 sizeof (struct GNUNET_MESH_TunnelMessage)},
7197 {&handle_local_tunnel_speed, NULL,
7198 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7199 sizeof (struct GNUNET_MESH_TunnelMessage)},
7200 {&handle_local_tunnel_buffer, NULL,
7201 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7202 sizeof (struct GNUNET_MESH_TunnelMessage)},
7203 {&handle_local_tunnel_buffer, NULL,
7204 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7205 sizeof (struct GNUNET_MESH_TunnelMessage)},
7206 {&handle_local_connect_add, NULL,
7207 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7208 sizeof (struct GNUNET_MESH_PeerControl)},
7209 {&handle_local_connect_del, NULL,
7210 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7211 sizeof (struct GNUNET_MESH_PeerControl)},
7212 {&handle_local_blacklist, NULL,
7213 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7214 sizeof (struct GNUNET_MESH_PeerControl)},
7215 {&handle_local_unblacklist, NULL,
7216 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7217 sizeof (struct GNUNET_MESH_PeerControl)},
7218 {&handle_local_connect_by_type, NULL,
7219 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7220 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7221 {&handle_local_connect_by_string, NULL,
7222 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7223 {&handle_local_unicast, NULL,
7224 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7225 {&handle_local_to_origin, NULL,
7226 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7227 {&handle_local_multicast, NULL,
7228 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7229 {&handle_local_ack, NULL,
7230 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7231 sizeof (struct GNUNET_MESH_LocalAck)},
7237 * To be called on core init/fail.
7239 * @param cls service closure
7240 * @param server handle to the server for this service
7241 * @param identity the public identity of this peer
7244 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7245 const struct GNUNET_PeerIdentity *identity)
7248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7249 core_handle = server;
7250 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7254 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7256 GNUNET_abort(); // Try harder
7263 * Method called whenever a given peer connects.
7265 * @param cls closure
7266 * @param peer peer identity this notification is about
7267 * @param atsi performance data for the connection
7268 * @param atsi_count number of records in 'atsi'
7271 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7272 const struct GNUNET_ATS_Information *atsi,
7273 unsigned int atsi_count)
7275 struct MeshPeerInfo *peer_info;
7276 struct MeshPeerPath *path;
7278 DEBUG_CONN ("Peer connected\n");
7279 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7280 peer_info = peer_info_get (peer);
7281 if (myid == peer_info->id)
7283 DEBUG_CONN (" (self)\n");
7288 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7290 path = path_new (2);
7291 path->peers[0] = myid;
7292 path->peers[1] = peer_info->id;
7293 GNUNET_PEER_change_rc (myid, 1);
7294 GNUNET_PEER_change_rc (peer_info->id, 1);
7295 peer_info_add_path (peer_info, path, GNUNET_YES);
7296 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7302 * Method called whenever a peer disconnects.
7304 * @param cls closure
7305 * @param peer peer identity this notification is about
7308 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7310 struct MeshPeerInfo *pi;
7311 struct MeshPeerQueue *q;
7312 struct MeshPeerQueue *n;
7314 DEBUG_CONN ("Peer disconnected\n");
7315 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7327 /* try to reroute this traffic instead */
7328 queue_destroy(q, GNUNET_YES);
7332 peer_info_remove_path (pi, pi->id, myid);
7335 DEBUG_CONN (" (self)\n");
7337 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7342 /******************************************************************************/
7343 /************************ MAIN FUNCTIONS ****************************/
7344 /******************************************************************************/
7347 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7349 * @param cls closure
7350 * @param key current key code
7351 * @param value value in the hash map
7352 * @return GNUNET_YES if we should continue to iterate,
7356 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7358 struct MeshTunnel *t = value;
7365 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7367 * @param cls closure
7368 * @param key current key code
7369 * @param value value in the hash map
7370 * @return GNUNET_YES if we should continue to iterate,
7374 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7376 struct MeshPeerInfo *p = value;
7377 struct MeshPeerQueue *q;
7378 struct MeshPeerQueue *n;
7386 queue_destroy(q, GNUNET_YES);
7390 peer_info_destroy (p);
7395 * Task run during shutdown.
7401 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7405 if (core_handle != NULL)
7407 GNUNET_CORE_disconnect (core_handle);
7410 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7411 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7412 if (dht_handle != NULL)
7414 GNUNET_DHT_disconnect (dht_handle);
7419 GNUNET_SERVER_notification_context_destroy (nc);
7422 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7424 GNUNET_SCHEDULER_cancel (announce_id_task);
7425 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7431 * Process mesh requests.
7433 * @param cls closure
7434 * @param server the initialized server
7435 * @param c configuration to use
7438 run (void *cls, struct GNUNET_SERVER_Handle *server,
7439 const struct GNUNET_CONFIGURATION_Handle *c)
7441 struct MeshPeerInfo *peer;
7442 struct MeshPeerPath *p;
7445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7446 server_handle = server;
7447 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7448 NULL, /* Closure passed to MESH functions */
7449 &core_init, /* Call core_init once connected */
7450 &core_connect, /* Handle connects */
7451 &core_disconnect, /* remove peers on disconnects */
7452 NULL, /* Don't notify about all incoming messages */
7453 GNUNET_NO, /* For header only in notification */
7454 NULL, /* Don't notify about all outbound messages */
7455 GNUNET_NO, /* For header-only out notification */
7456 core_handlers); /* Register these handlers */
7458 if (core_handle == NULL)
7461 GNUNET_SCHEDULER_shutdown ();
7466 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7471 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7473 GNUNET_SCHEDULER_shutdown ();
7478 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7479 &refresh_path_time))
7481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7483 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7484 "refresh path time");
7485 GNUNET_SCHEDULER_shutdown ();
7490 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7491 &app_announce_time))
7493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7495 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7496 "app announce time");
7497 GNUNET_SCHEDULER_shutdown ();
7502 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7505 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7507 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7508 "id announce time");
7509 GNUNET_SCHEDULER_shutdown ();
7514 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7515 &unacknowledged_wait_time))
7517 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7519 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7520 "unacknowledged wait time");
7521 GNUNET_SCHEDULER_shutdown ();
7526 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7531 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7533 GNUNET_SCHEDULER_shutdown ();
7538 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7543 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7545 GNUNET_SCHEDULER_shutdown ();
7550 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7555 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7557 GNUNET_SCHEDULER_shutdown ();
7562 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7567 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7573 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7574 &dht_replication_level))
7576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7578 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7579 "dht replication level", 10);
7580 dht_replication_level = 10;
7584 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7585 GNUNET_free (keyfile);
7586 if (my_private_key == NULL)
7588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7589 _("Mesh service could not access hostkey. Exiting.\n"));
7590 GNUNET_SCHEDULER_shutdown ();
7593 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7594 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7595 &my_full_id.hashPubKey);
7596 myid = GNUNET_PEER_intern (&my_full_id);
7598 // transport_handle = GNUNET_TRANSPORT_connect(c,
7605 dht_handle = GNUNET_DHT_connect (c, 64);
7606 if (dht_handle == NULL)
7611 stats = GNUNET_STATISTICS_create ("mesh", c);
7615 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7617 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7618 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7619 peers = GNUNET_CONTAINER_multihashmap_create (32);
7620 applications = GNUNET_CONTAINER_multihashmap_create (32);
7621 types = GNUNET_CONTAINER_multihashmap_create (32);
7623 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7624 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7625 GNUNET_SERVER_disconnect_notify (server_handle,
7626 &handle_local_client_disconnect, NULL);
7630 clients_tail = NULL;
7633 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7634 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7636 /* Create a peer_info for the local peer */
7637 peer = peer_info_get (&my_full_id);
7640 GNUNET_PEER_change_rc (myid, 1);
7641 peer_info_add_path (peer, p, GNUNET_YES);
7643 /* Scheduled the task to clean up when shutdown is called */
7644 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7651 * The main function for the mesh service.
7653 * @param argc number of arguments from the command line
7654 * @param argv command line arguments
7655 * @return 0 ok, 1 on error
7658 main (int argc, char *const *argv)
7662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7665 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");