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_YES
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #define INITIAL_WINDOW_SIZE 2
64 #define ACK_THRESHOLD INITIAL_WINDOW_SIZE / 2
66 #if MESH_DEBUG_CONNECTION
67 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
69 #define DEBUG_CONN(...)
73 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
75 #define DEBUG_DHT(...)
78 /******************************************************************************/
79 /************************ DATA STRUCTURES ****************************/
80 /******************************************************************************/
82 /** FWD declaration */
88 * Struct representing a piece of data being sent to other peers
92 /** Tunnel it belongs to. */
95 /** In case of a multicast, task to allow a client to send more data if
96 * some neighbor is too slow. */
97 GNUNET_SCHEDULER_TaskIdentifier *task;
99 /** How many remaining neighbors we need to send this to. */
100 unsigned int *reference_counter;
102 /** Size of the data. */
111 * Struct containing info about a queued transmission to this peer
118 struct MeshPeerQueue *next;
123 struct MeshPeerQueue *prev;
126 * Peer this transmission is directed to.
128 struct MeshPeerInfo *peer;
131 * Tunnel this message belongs to.
133 struct MeshTunnel *tunnel;
136 * Pointer to info stucture used as cls.
146 * Size of the message
153 * Struct containing all info possibly needed to build a package when called
156 struct MeshTransmissionDescriptor
158 /** ID of the tunnel this packet travels in */
159 struct MESH_TunnelID *origin;
161 /** Who was this message being sent to */
162 struct MeshPeerInfo *peer;
164 /** Ultimate destination of the packet */
165 GNUNET_PEER_Id destination;
167 /** Data descriptor */
168 struct MeshData* mesh_data;
173 * Struct containing all information regarding a given peer
183 * Last time we heard from this peer
185 struct GNUNET_TIME_Absolute last_contact;
188 * Number of attempts to reconnect so far
190 int n_reconnect_attempts;
193 * Paths to reach the peer, ordered by ascending hop count
195 struct MeshPeerPath *path_head;
198 * Paths to reach the peer, ordered by ascending hop count
200 struct MeshPeerPath *path_tail;
203 * Handle to stop the DHT search for a path to this peer
205 struct GNUNET_DHT_GetHandle *dhtget;
208 * Closure given to the DHT GET
210 struct MeshPathInfo *dhtgetcls;
213 * Array of tunnels this peer participates in
214 * (most probably a small amount, therefore not a hashmap)
215 * When the path to the peer changes, notify these tunnels to let them
216 * re-adjust their path trees.
218 struct MeshTunnel **tunnels;
221 * Number of tunnels this peers participates in
223 unsigned int ntunnels;
226 * Transmission queue to core DLL head
228 struct MeshPeerQueue *queue_head;
231 * Transmission queue to core DLL tail
233 struct MeshPeerQueue *queue_tail;
236 * How many messages are in the queue to this peer.
238 unsigned int queue_n;
241 * Handle to for queued transmissions
243 struct GNUNET_CORE_TransmitHandle *core_transmit;
248 * Globally unique tunnel identification (owner + number)
249 * DO NOT USE OVER THE NETWORK
254 * Node that owns the tunnel
259 * Tunnel number to differentiate all the tunnels owned by the node oid
260 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
262 MESH_TunnelNumber tid;
267 * Struct containing all information regarding a tunnel
268 * For an intermediate node the improtant info used will be:
269 * - id Tunnel unique identification
270 * - paths[0] To know where to send it next
271 * - metainfo: ready, speeds, accounting
278 struct MESH_TunnelID id;
281 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
283 MESH_TunnelNumber local_tid;
286 * Local tunnel number for local destination clients (incoming number)
287 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
290 MESH_TunnelNumber local_tid_dest;
293 * Is the speed on the tunnel limited to the slowest peer?
298 * Is the tunnel bufferless (minimum latency)?
303 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
308 * Packet ID of the last bck packet sent (unique counter per hop).
313 * SKIP value for this tunnel.
318 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
319 * Contains the Flow Control info: FWD ACK value received,
320 * last BCK ACK sent, PID and SKIP values.
322 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
325 * Last ACK sent towards the origin (for traffic towards leaf node).
327 uint32_t last_fwd_ack;
330 * BCK ACK value received from the hop towards the owner of the tunnel,
331 * (previous node / owner): up to what message PID can we sent back to him.
336 * How many messages are in the forward queue (towards leaves).
338 unsigned int fwd_queue_n;
341 * How many messages do we accept in the forward queue.
343 unsigned int fwd_queue_max;
346 * How many messages are in the backward queue (towards origin).
348 unsigned int bck_queue_n;
351 * How many messages do we accept in the backward queue.
353 unsigned int bck_queue_max;
356 * Last time the tunnel was used
358 struct GNUNET_TIME_Absolute timestamp;
361 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
362 * containing peers added by id or by type, not intermediate peers.
364 struct GNUNET_CONTAINER_MultiHashMap *peers;
367 * Number of peers that are connected and potentially ready to receive data
369 unsigned int peers_ready;
372 * Number of peers that have been added to the tunnel
374 unsigned int peers_total;
377 * Client owner of the tunnel, if any
379 struct MeshClient *owner;
382 * Clients that have been informed about and want to stay in the tunnel.
384 struct MeshClient **clients;
387 * Flow control info for each client.
389 struct MeshTunnelClientInfo *clients_fc;
392 * Number of elements in clients/clients_fc
394 unsigned int nclients;
397 * Clients that have been informed but requested to leave the tunnel.
399 struct MeshClient **ignore;
402 * Number of elements in clients
404 unsigned int nignore;
409 GNUNET_PEER_Id *blacklisted;
412 * Number of elements in blacklisted
414 unsigned int nblacklisted;
417 * Bloomfilter (for peer identities) to stop circular routes
419 char bloomfilter[MESH_BLOOM_SIZE];
424 struct MeshTunnelTree *tree;
427 * Application type we are looking for in this tunnel
429 GNUNET_MESH_ApplicationType type;
432 * Used to search peers offering a service
434 struct GNUNET_DHT_GetHandle *dht_get_type;
437 * Initial context of the regex search for a connect_by_string
439 struct MeshRegexSearchContext *regex_ctx;
442 * Task to keep the used paths alive
444 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
447 * Task to destroy the tunnel after timeout
449 * FIXME: merge the two? a tunnel will have either
450 * a path refresh OR a timeout, never both!
452 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
455 * Flag to signal the destruction of the tunnel.
456 * If this is set GNUNET_YES the tunnel will be destroyed
457 * when the queue is empty.
464 * Info about a child node in a tunnel, needed to perform flow control.
466 struct MeshTunnelChildInfo
469 * ID of the child node.
484 * Maximum PID allowed (FWD ACK received).
489 * Last ACK sent to that child (BCK ACK).
496 * Info about a leaf client of a tunnel, needed to perform flow control.
498 struct MeshTunnelClientInfo
506 * Maximum PID allowed (FWD ACK received).
511 * Last ACK sent to that child (BCK ACK).
519 * Info collected during iteration of child nodes in order to get the ACK value
522 struct MeshTunnelChildIteratorContext
525 * Tunnel whose info is being collected.
527 struct MeshTunnel *t;
530 * Is this context initialized? Is the value in max_child_ack valid?
535 * Maximum child ACK so far.
537 uint32_t max_child_ack;
540 * Number of children nodes
542 unsigned int nchildren;
547 * Info needed to work with tunnel paths and peers
554 struct MeshTunnel *t;
557 * Neighbouring peer to whom we send the packet to
559 struct MeshPeerInfo *peer;
564 struct MeshPeerPath *path;
569 * Struct containing information about a client of the service
576 struct MeshClient *next;
581 struct MeshClient *prev;
584 * Tunnels that belong to this client, indexed by local id
586 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
589 * Tunnels this client has accepted, indexed by incoming local id
591 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
594 * Tunnels this client has rejected, indexed by incoming local id
596 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
598 * Handle to communicate with the client
600 struct GNUNET_SERVER_Client *handle;
603 * Applications that this client has claimed to provide
605 struct GNUNET_CONTAINER_MultiHashMap *apps;
608 * Messages that this client has declared interest in
610 struct GNUNET_CONTAINER_MultiHashMap *types;
613 * Whether the client is active or shutting down (don't send confirmations
614 * to a client that is shutting down.
619 * ID of the client, mainly for debug messages
624 * Regular expressions describing the services offered by this client.
626 char **regexes; // FIXME add timeout? API to remove a regex?
629 * Number of regular expressions in regexes.
631 unsigned int n_regex;
634 * Task to refresh all regular expresions in the DHT.
636 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
642 * Struct to keep information of searches of services described by a regex
643 * using a user-provided string service description.
645 struct MeshRegexSearchInfo
648 * Which tunnel is this for
650 struct MeshTunnel *t;
653 * User provided description of the searched service.
658 * Part of the description already consumed by the search.
665 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
668 * Results from running DHT GETs.
670 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
673 * Contexts, for each running DHT GET. Free all on end of search.
675 struct MeshRegexSearchContext **contexts;
678 * Number of contexts (branches/steps in search).
680 unsigned int n_contexts;
683 * Peer that is connecting via connect_by_string. When connected, free ctx.
688 * Other peers that are found but not yet being connected to.
690 GNUNET_PEER_Id *peers;
693 * Number of elements in peers.
695 unsigned int n_peers;
698 * Next peer to try to connect to.
703 * Timeout for a connect attempt.
704 * When reached, try to connect to a different peer, if any. If not,
705 * try the same peer again.
707 GNUNET_SCHEDULER_TaskIdentifier timeout;
712 * Struct to keep state of running searches that have consumed a part of
715 struct MeshRegexSearchContext
718 * Part of the description already consumed by
719 * this particular search branch.
724 * Information about the search.
726 struct MeshRegexSearchInfo *info;
730 /******************************************************************************/
731 /************************ DEBUG FUNCTIONS ****************************/
732 /******************************************************************************/
736 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
737 * @param cls string to print
738 * @param success GNUNET_OK if the PUT was transmitted,
739 * GNUNET_NO on timeout,
740 * GNUNET_SYSERR on disconnect from service
741 * after the PUT message was transmitted
742 * (so we don't know if it was received or not)
747 mesh_debug (void *cls, int success)
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
757 /******************************************************************************/
758 /*********************** GLOBAL VARIABLES ****************************/
759 /******************************************************************************/
763 * Configuration parameters
765 static struct GNUNET_TIME_Relative refresh_path_time;
766 static struct GNUNET_TIME_Relative app_announce_time;
767 static struct GNUNET_TIME_Relative id_announce_time;
768 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
769 static struct GNUNET_TIME_Relative connect_timeout;
770 static long long unsigned int default_ttl;
771 static long long unsigned int dht_replication_level;
772 static long long unsigned int max_tunnels;
773 static long long unsigned int max_msgs_queue;
776 * DLL with all the clients, head.
778 static struct MeshClient *clients;
781 * DLL with all the clients, tail.
783 static struct MeshClient *clients_tail;
786 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
788 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
791 * Number of tunnels known.
793 static unsigned long long n_tunnels;
796 * Tunnels incoming, indexed by MESH_TunnelNumber
797 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
799 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
802 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
804 static struct GNUNET_CONTAINER_MultiHashMap *peers;
807 * Handle to communicate with transport
809 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
812 * Handle to communicate with core.
814 static struct GNUNET_CORE_Handle *core_handle;
819 static struct GNUNET_DHT_Handle *dht_handle;
824 static struct GNUNET_SERVER_Handle *server_handle;
827 * Handle to the statistics service.
829 static struct GNUNET_STATISTICS_Handle *stats;
832 * Notification context, to send messages to local clients.
834 static struct GNUNET_SERVER_NotificationContext *nc;
837 * Local peer own ID (memory efficient handle).
839 static GNUNET_PEER_Id myid;
842 * Local peer own ID (full value).
844 static struct GNUNET_PeerIdentity my_full_id;
849 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
854 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
857 * Tunnel ID for the next created tunnel (global tunnel number).
859 static MESH_TunnelNumber next_tid;
862 * Tunnel ID for the next incoming tunnel (local tunnel number).
864 static MESH_TunnelNumber next_local_tid;
867 * All application types provided by this peer.
869 static struct GNUNET_CONTAINER_MultiHashMap *applications;
872 * All message types clients of this peer are interested in.
874 static struct GNUNET_CONTAINER_MultiHashMap *types;
877 * Task to periodically announce provided applications.
879 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
882 * Task to periodically announce itself in the network.
884 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
887 * Next ID to assign to a client.
889 unsigned int next_client_id;
892 /******************************************************************************/
893 /*********************** DECLARATIONS **************************/
894 /******************************************************************************/
896 /* FIXME move declarations here */
899 * Function to process paths received for a new peer addition. The recorded
900 * paths form the initial tunnel, which can be optimized later.
901 * Called on each result obtained for the DHT search.
904 * @param exp when will this value expire
905 * @param key key of the result
906 * @param type type of the result
907 * @param size number of bytes in data
908 * @param data pointer to the result data
911 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
912 const struct GNUNET_HashCode * key,
913 const struct GNUNET_PeerIdentity *get_path,
914 unsigned int get_path_length,
915 const struct GNUNET_PeerIdentity *put_path,
916 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
917 size_t size, const void *data);
921 * Function to process DHT string to regex matching.
922 * Called on each result obtained for the DHT search.
924 * @param cls closure (search context)
925 * @param exp when will this value expire
926 * @param key key of the result
927 * @param get_path path of the get request (not used)
928 * @param get_path_length lenght of get_path (not used)
929 * @param put_path path of the put request (not used)
930 * @param put_path_length length of the put_path (not used)
931 * @param type type of the result
932 * @param size number of bytes in data
933 * @param data pointer to the result data
935 * TODO: re-issue the request after certain time? cancel after X results?
938 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
939 const struct GNUNET_HashCode * key,
940 const struct GNUNET_PeerIdentity *get_path,
941 unsigned int get_path_length,
942 const struct GNUNET_PeerIdentity *put_path,
943 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
944 size_t size, const void *data);
948 * Function to process DHT string to regex matching.
949 * Called on each result obtained for the DHT search.
951 * @param cls closure (search context)
952 * @param exp when will this value expire
953 * @param key key of the result
954 * @param get_path path of the get request (not used)
955 * @param get_path_length lenght of get_path (not used)
956 * @param put_path path of the put request (not used)
957 * @param put_path_length length of the put_path (not used)
958 * @param type type of the result
959 * @param size number of bytes in data
960 * @param data pointer to the result data
963 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
964 const struct GNUNET_HashCode * key,
965 const struct GNUNET_PeerIdentity *get_path,
966 unsigned int get_path_length,
967 const struct GNUNET_PeerIdentity *put_path,
968 unsigned int put_path_length,
969 enum GNUNET_BLOCK_Type type,
970 size_t size, const void *data);
974 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
975 * and insert it in the appropiate structures if the peer is not known yet.
977 * @param peer Short identity of the peer.
979 * @return Existing or newly created peer info.
981 static struct MeshPeerInfo *
982 peer_info_get_short (const GNUNET_PEER_Id peer);
986 * Try to establish a new connection to this peer.
987 * Use the best path for the given tunnel.
988 * If the peer doesn't have any path to it yet, try to get one.
989 * If the peer already has some path, send a CREATE PATH towards it.
991 * @param peer PeerInfo of the peer.
992 * @param t Tunnel for which to create the path, if possible.
995 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
999 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1000 * needed rescources.
1001 * If peer already exists, reevaluate shortest path and change if different.
1003 * @param t Tunnel we want to add a new peer to
1004 * @param peer PeerInfo of the peer being added
1008 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1012 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1013 * that are no longer needed, as well as nodes of no longer reachable peers.
1014 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1016 * @param t Tunnel from which to remove the path.
1017 * @param peer Short id of the peer which should be removed.
1020 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1024 * Search for a tunnel by global ID using full PeerIdentities.
1026 * @param oid owner of the tunnel.
1027 * @param tid global tunnel number.
1029 * @return tunnel handler, NULL if doesn't exist.
1031 static struct MeshTunnel *
1032 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1036 * Delete an active client from the tunnel.
1042 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1045 * Notify a tunnel that a connection has broken that affects at least
1046 * some of its peers.
1048 * @param t Tunnel affected.
1049 * @param p1 Peer that got disconnected from p2.
1050 * @param p2 Peer that got disconnected from p1.
1052 * @return Short ID of the peer disconnected (either p1 or p2).
1053 * 0 if the tunnel remained unaffected.
1055 static GNUNET_PEER_Id
1056 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1061 * Get the current ack value for a tunnel, for data going from root to leaves,
1062 * taking in account the tunnel mode and the status of all children and clients.
1066 * @return Maximum PID allowed.
1069 tunnel_get_fwd_ack (struct MeshTunnel *t);
1072 * Get the current ack value for a tunnel, for data going from leaves to root,
1073 * taking in account the tunnel mode and the status of all children and clients.
1077 * @return Maximum PID allowed.
1080 tunnel_get_bck_ack (struct MeshTunnel *t);
1083 * Add a client to a tunnel, initializing all needed data structures.
1085 * @param t Tunnel to which add the client.
1086 * @param c Client which to add to the tunnel.
1089 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1093 * Iterator over edges in a regex block retrieved from the DHT.
1095 * @param cls Closure.
1096 * @param token Token that follows to next state.
1097 * @param len Lenght of token.
1098 * @param key Hash of next state.
1100 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1103 regex_edge_iterator (void *cls,
1106 const struct GNUNET_HashCode *key);
1110 * Find a path to a peer that offers a regex servcie compatible
1111 * with a given string.
1113 * @param key The key of the accepting state.
1114 * @param ctx Context containing info about the string, tunnel, etc.
1117 regex_find_path (const struct GNUNET_HashCode *key,
1118 struct MeshRegexSearchContext *ctx);
1122 * Queue and pass message to core when possible.
1124 * @param cls Closure (type dependant).
1125 * @param type Type of the message, 0 for a raw message.
1126 * @param size Size of the message.
1127 * @param dst Neighbor to send message to.
1128 * @param t Tunnel this message belongs to.
1131 queue_add (void *cls, uint16_t type, size_t size,
1132 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1135 * Free a transmission that was already queued with all resources
1136 * associated to the request.
1138 * @param queue Queue handler to cancel.
1139 * @param clear_cls Is it necessary to free associated cls?
1142 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1144 /******************************************************************************/
1145 /************************ ITERATORS ****************************/
1146 /******************************************************************************/
1149 * Iterator over found existing mesh regex blocks that match an ongoing search.
1151 * @param cls closure
1152 * @param key current key code
1153 * @param value value in the hash map
1154 * @return GNUNET_YES if we should continue to iterate,
1158 regex_result_iterator (void *cls,
1159 const struct GNUNET_HashCode * key,
1162 struct MeshRegexBlock *block = value;
1163 struct MeshRegexSearchContext *ctx = cls;
1165 if (GNUNET_YES == ntohl(block->accepting) &&
1166 ctx->position == strlen (ctx->info->description))
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1169 regex_find_path (key, ctx);
1170 return GNUNET_YES; // We found an accept state!
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1175 ctx->position, strlen(ctx->info->description),
1176 ntohl(block->accepting));
1179 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1180 ®ex_edge_iterator, ctx);
1187 * Iterator over edges in a regex block retrieved from the DHT.
1189 * @param cls Closure (context of the search).
1190 * @param token Token that follows to next state.
1191 * @param len Lenght of token.
1192 * @param key Hash of next state.
1194 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1197 regex_edge_iterator (void *cls,
1200 const struct GNUNET_HashCode *key)
1202 struct MeshRegexSearchContext *ctx = cls;
1203 struct MeshRegexSearchContext *new_ctx;
1204 struct MeshRegexSearchInfo *info = ctx->info;
1205 struct GNUNET_DHT_GetHandle *get_h;
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1212 current = &info->description[ctx->position];
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1214 current_len = strlen (info->description) - ctx->position;
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1219 if (len > current_len)
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1222 return GNUNET_YES; // Token too long, wont match
1224 if (0 != strncmp (current, token, len))
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1227 return GNUNET_YES; // Token doesn't match
1229 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1230 new_ctx->info = info;
1231 new_ctx->position = ctx->position + len;
1232 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1234 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1237 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1238 ®ex_result_iterator,
1240 return GNUNET_YES; // We are already looking for it
1242 /* Start search in DHT */
1244 GNUNET_DHT_get_start (dht_handle, /* handle */
1245 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1246 key, /* key to search */
1247 dht_replication_level, /* replication level */
1248 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1249 NULL, /* xquery */ // FIXME BLOOMFILTER
1250 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1251 &dht_get_string_handler, new_ctx);
1253 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1254 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1264 * Iterator over hash map entries to cancel DHT GET requests after a
1265 * successful connect_by_string.
1267 * @param cls Closure (unused).
1268 * @param key Current key code (unused).
1269 * @param value Value in the hash map (get handle).
1270 * @return GNUNET_YES if we should continue to iterate,
1274 regex_cancel_dht_get (void *cls,
1275 const struct GNUNET_HashCode * key,
1278 struct GNUNET_DHT_GetHandle *h = value;
1280 GNUNET_DHT_get_stop (h);
1286 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1287 * search for connect_by_string.
1289 * @param cls Closure (unused).
1290 * @param key Current key code (unused).
1291 * @param value MeshRegexBlock in the hash map.
1292 * @return GNUNET_YES if we should continue to iterate,
1296 regex_free_result (void *cls,
1297 const struct GNUNET_HashCode * key,
1301 GNUNET_free (value);
1307 * Regex callback iterator to store own service description in the DHT.
1309 * @param cls closure.
1310 * @param key hash for current state.
1311 * @param proof proof for current state.
1312 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1313 * @param num_edges number of edges leaving current state.
1314 * @param edges edges leaving current state.
1317 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1318 int accepting, unsigned int num_edges,
1319 const struct GNUNET_REGEX_Edge *edges)
1321 struct MeshRegexBlock *block;
1322 struct MeshRegexEdge *block_edge;
1323 enum GNUNET_DHT_RouteOption opt;
1327 unsigned int offset;
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 " regex dht put for state %s\n",
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1340 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1341 if (GNUNET_YES == accepting)
1343 struct MeshRegexAccept block;
1345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1346 " state %s is accepting, putting own id\n",
1348 size = sizeof (block);
1350 block.id = my_full_id;
1352 GNUNET_DHT_put(dht_handle, key,
1353 dht_replication_level,
1354 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1355 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1358 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1363 len = strlen(proof);
1364 size = sizeof (struct MeshRegexBlock) + len;
1365 block = GNUNET_malloc (size);
1368 block->n_proof = htonl (len);
1369 block->n_edges = htonl (num_edges);
1370 block->accepting = htonl (accepting);
1372 /* Store the proof at the end of the block. */
1373 aux = (char *) &block[1];
1374 memcpy (aux, proof, len);
1377 /* Store each edge in a variable length MeshEdge struct at the
1378 * very end of the MeshRegexBlock structure.
1380 for (i = 0; i < num_edges; i++)
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 " edge %s towards %s\n",
1385 GNUNET_h2s(&edges[i].destination));
1387 /* aux points at the end of the last block */
1388 len = strlen (edges[i].label);
1389 size += sizeof (struct MeshRegexEdge) + len;
1390 // Calculate offset FIXME is this ok? use size instead?
1391 offset = aux - (char *) block;
1392 block = GNUNET_realloc (block, size);
1393 aux = &((char *) block)[offset];
1394 block_edge = (struct MeshRegexEdge *) aux;
1395 block_edge->key = edges[i].destination;
1396 block_edge->n_token = htonl (len);
1397 aux = (char *) &block_edge[1];
1398 memcpy (aux, edges[i].label, len);
1402 GNUNET_DHT_put(dht_handle, key,
1403 dht_replication_level,
1405 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1407 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1411 GNUNET_free (block);
1416 * Store the regular expression describing a local service into the DHT.
1418 * @param regex The regular expresion.
1421 regex_put (const char *regex)
1423 struct GNUNET_REGEX_Automaton *dfa;
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1426 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1427 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1428 GNUNET_REGEX_automaton_destroy (dfa);
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1434 * Find a path to a peer that offers a regex servcie compatible
1435 * with a given string.
1437 * @param key The key of the accepting state.
1438 * @param ctx Context containing info about the string, tunnel, etc.
1441 regex_find_path (const struct GNUNET_HashCode *key,
1442 struct MeshRegexSearchContext *ctx)
1444 struct GNUNET_DHT_GetHandle *get_h;
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1447 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1448 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1449 key, /* key to search */
1450 dht_replication_level, /* replication level */
1451 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1452 GNUNET_DHT_RO_RECORD_ROUTE,
1453 NULL, /* xquery */ // FIXME BLOOMFILTER
1454 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1455 &dht_get_string_accept_handler, ctx);
1456 GNUNET_break (GNUNET_OK ==
1457 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1460 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1465 * Function called if the connect attempt to a peer found via
1466 * connect_by_string times out. Try to connect to another peer, if any.
1467 * Otherwise try to reconnect to the same peer.
1469 * @param cls Closure (info about regex search).
1470 * @param tc TaskContext.
1473 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1475 struct MeshRegexSearchInfo *info = cls;
1476 struct MeshPeerInfo *peer_info;
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1481 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1482 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1490 if (0 < info->n_peers)
1492 // Select next peer, put current in that spot.
1493 id = info->peers[info->i_peer];
1494 info->peers[info->i_peer] = info->peer;
1495 info->i_peer = (info->i_peer + 1) % info->n_peers;
1499 // Try to connect to same peer again.
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1504 peer_info = peer_info_get_short(id);
1505 tunnel_add_peer (info->t, peer_info);
1507 tunnel_delete_peer (info->t, old);
1508 peer_info_connect (peer_info, info->t);
1509 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1510 ®ex_connect_timeout,
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1517 * Cancel an ongoing regex search in the DHT and free all resources.
1519 * @param ctx The search context.
1522 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1524 struct MeshRegexSearchInfo *info = ctx->info;
1527 GNUNET_free (info->description);
1528 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1529 ®ex_cancel_dht_get, NULL);
1530 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1531 ®ex_free_result, NULL);
1532 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1533 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1534 info->t->regex_ctx = NULL;
1535 for (i = 0; i < info->n_contexts; i++)
1537 GNUNET_free (info->contexts[i]);
1539 if (0 < info->n_contexts)
1540 GNUNET_free (info->contexts);
1541 if (0 < info->n_peers)
1542 GNUNET_free (info->peers);
1543 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1545 GNUNET_SCHEDULER_cancel(info->timeout);
1551 /******************************************************************************/
1552 /************************ PERIODIC FUNCTIONS ****************************/
1553 /******************************************************************************/
1556 * Announce iterator over for each application provided by the peer
1558 * @param cls closure
1559 * @param key current key code
1560 * @param value value in the hash map
1561 * @return GNUNET_YES if we should continue to
1566 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1568 struct PBlock block;
1569 struct MeshClient *c;
1571 block.id = my_full_id;
1572 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1573 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1574 if (0 == block.type)
1579 block.type = htonl (block.type);
1581 GNUNET_break (NULL !=
1582 GNUNET_DHT_put (dht_handle, key,
1583 dht_replication_level,
1584 GNUNET_DHT_RO_RECORD_ROUTE |
1585 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1586 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1588 (const char *) &block,
1589 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1591 app_announce_time, NULL, NULL));
1597 * Periodically announce what applications are provided by local clients
1600 * @param cls closure
1601 * @param tc task context
1604 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1606 struct MeshClient *c = cls;
1609 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1611 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1615 DEBUG_DHT ("Starting PUT for regex\n");
1617 for (i = 0; i < c->n_regex; i++)
1619 regex_put (c->regexes[i]);
1621 c->regex_announce_task =
1622 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1623 DEBUG_DHT ("Finished PUT for regex\n");
1630 * Periodically announce what applications are provided by local clients
1633 * @param cls closure
1634 * @param tc task context
1637 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1639 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1641 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1645 DEBUG_DHT ("Starting PUT for apps\n");
1647 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1649 announce_applications_task =
1650 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1652 DEBUG_DHT ("Finished PUT for apps\n");
1659 * Periodically announce self id in the DHT
1661 * @param cls closure
1662 * @param tc task context
1665 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1667 struct PBlock block;
1669 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1671 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1675 * - Set data expiration in function of X
1676 * - Adapt X to churn
1678 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1680 block.id = my_full_id;
1681 block.type = htonl (0);
1682 GNUNET_DHT_put (dht_handle, /* DHT handle */
1683 &my_full_id.hashPubKey, /* Key to use */
1684 dht_replication_level, /* Replication level */
1685 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1686 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1687 sizeof (block), /* Size of the data */
1688 (const char *) &block, /* Data itself */
1689 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1690 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1691 NULL, /* Continuation */
1692 NULL); /* Continuation closure */
1694 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1698 /******************************************************************************/
1699 /****************** GENERAL HELPER FUNCTIONS ************************/
1700 /******************************************************************************/
1703 * Check if one pid is bigger than other, accounting for overflow.
1705 * @param bigger Argument that should be bigger.
1706 * @param smaller Argument that should be smaller.
1708 * @return True if bigger (arg1) has a higher value than smaller (arg 2).
1711 is_pid_bigger (uint32_t bigger, uint32_t smaller)
1713 return (GNUNET_YES == PID_OVERFLOW(smaller, bigger) ||
1714 (bigger > smaller && GNUNET_NO == PID_OVERFLOW(bigger, smaller)));
1718 * Get the higher ACK value out of two values, taking in account overflow.
1720 * @param a First ACK value.
1721 * @param b Second ACK value.
1723 * @return Highest ACK value from the two.
1726 max_pid (uint32_t a, uint32_t b)
1728 if (is_pid_bigger(a, b))
1735 * Get the lower ACK value out of two values, taking in account overflow.
1737 * @param a First ACK value.
1738 * @param b Second ACK value.
1740 * @return Lowest ACK value from the two.
1743 min_pid (uint32_t a, uint32_t b)
1745 if (is_pid_bigger(a, b))
1752 * Decrements the reference counter and frees all resources if needed
1754 * @param mesh_data Data Descriptor used in a multicast message.
1755 * Freed no longer needed (last message).
1758 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1760 /* Make sure it's a multicast packet */
1761 GNUNET_assert (NULL != mesh_data->reference_counter);
1763 if (0 == --(*(mesh_data->reference_counter)))
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1766 if (NULL != mesh_data->task)
1768 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1770 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1772 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1774 GNUNET_free (mesh_data->task);
1776 GNUNET_free (mesh_data->reference_counter);
1777 GNUNET_free (mesh_data->data);
1778 GNUNET_free (mesh_data);
1784 * Check if client has registered with the service and has not disconnected
1786 * @param client the client to check
1788 * @return non-NULL if client exists in the global DLL
1790 static struct MeshClient *
1791 client_get (struct GNUNET_SERVER_Client *client)
1793 struct MeshClient *c;
1798 if (c->handle == client)
1807 * Checks if a given client has subscribed to certain message type
1809 * @param message_type Type of message to check
1810 * @param c Client to check
1812 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1815 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1817 struct GNUNET_HashCode hc;
1819 if (NULL == c->types)
1821 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1822 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1827 * Allow a client to send more data after transmitting a multicast message
1828 * which some neighbor has not yet accepted altough a reasonable time has
1831 * @param cls Closure (DataDescriptor containing the task identifier)
1832 * @param tc Task Context
1834 * FIXME reference counter cshould be just int
1837 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1839 struct MeshData *mdata = cls;
1841 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1843 GNUNET_assert (NULL != mdata->reference_counter);
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1846 *(mdata->reference_counter));
1847 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1848 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1853 * Check whether client wants traffic from a tunnel.
1855 * @param c Client to check.
1856 * @param t Tunnel to be found.
1858 * @return GNUNET_YES if client knows tunnel.
1860 * TODO look in client hashmap
1863 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1867 for (i = 0; i < t->nclients; i++)
1868 if (t->clients[i] == c)
1875 * Check whether client has been informed about a tunnel.
1877 * @param c Client to check.
1878 * @param t Tunnel to be found.
1880 * @return GNUNET_YES if client knows tunnel.
1882 * TODO look in client hashmap
1885 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1889 for (i = 0; i < t->nignore; i++)
1890 if (t->ignore[i] == c)
1892 return client_wants_tunnel(c, t);
1897 * Marks a client as uninterested in traffic from the tunnel, updating both
1898 * client and tunnel to reflect this.
1900 * @param c Client that doesn't want traffic anymore.
1901 * @param t Tunnel which should be ignored.
1903 * FIXME when to delete an incoming tunnel?
1906 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1908 struct GNUNET_HashCode hash;
1910 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1911 GNUNET_break (GNUNET_YES ==
1912 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1914 GNUNET_break (GNUNET_YES ==
1915 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1916 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1917 tunnel_delete_active_client (t, c);
1918 GNUNET_array_append (t->ignore, t->nignore, c);
1923 * Deletes a tunnel from a client (either owner or destination). To be used on
1924 * tunnel destroy, otherwise, use client_ignore_tunnel.
1926 * @param c Client whose tunnel to delete.
1927 * @param t Tunnel which should be deleted.
1930 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1932 struct GNUNET_HashCode hash;
1936 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1937 GNUNET_assert (GNUNET_YES ==
1938 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1944 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1946 GNUNET_assert (GNUNET_YES ==
1947 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1951 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1959 * Send the message to all clients that have subscribed to its type
1961 * @param msg Pointer to the message itself
1962 * @param payload Pointer to the payload of the message.
1963 * @return number of clients this message was sent to
1966 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1967 const struct GNUNET_MessageHeader *payload)
1969 struct GNUNET_PeerIdentity *oid;
1970 struct MeshClient *c;
1971 struct MeshTunnel *t;
1972 MESH_TunnelNumber *tid;
1975 char cbuf[htons (msg->size)];
1977 type = ntohs (payload->type);
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1981 memcpy (cbuf, msg, sizeof (cbuf));
1982 switch (htons (msg->type))
1984 struct GNUNET_MESH_Unicast *uc;
1985 struct GNUNET_MESH_Multicast *mc;
1986 struct GNUNET_MESH_ToOrigin *to;
1988 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1989 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1993 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1994 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1998 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1999 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2007 t = tunnel_get (oid, ntohl (*tid));
2014 for (count = 0, c = clients; c != NULL; c = c->next)
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2017 if (client_is_subscribed (type, c))
2019 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2023 *tid = htonl (t->local_tid);
2027 if (GNUNET_NO == client_knows_tunnel (c, t))
2029 /* This client doesn't know the tunnel */
2030 struct GNUNET_MESH_TunnelNotification tmsg;
2031 struct GNUNET_HashCode hash;
2033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2034 tmsg.header.size = htons (sizeof (tmsg));
2035 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2036 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2037 tmsg.tunnel_id = htonl (t->local_tid_dest);
2038 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2039 &tmsg.header, GNUNET_NO);
2040 tunnel_add_client (t, c);
2041 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2043 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2044 c->incoming_tunnels, &hash, t,
2045 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2047 *tid = htonl (t->local_tid_dest);
2050 /* Check if the client wants to get traffic from the tunnel */
2051 if (GNUNET_NO == client_wants_tunnel(c, t))
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2055 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2056 (struct GNUNET_MessageHeader
2057 *) cbuf, GNUNET_NO);
2065 * Notify the client that owns the tunnel that a peer has connected to it
2066 * (the requested path to it has been confirmed).
2068 * @param t Tunnel whose owner to notify
2069 * @param id Short id of the peer that has connected
2072 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2074 struct GNUNET_MESH_PeerControl pc;
2076 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2077 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2078 pc.tunnel_id = htonl (t->local_tid);
2079 GNUNET_PEER_resolve (id, &pc.peer);
2080 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2086 * Notify a the client of a tunnel about how many more
2087 * payload packages will we accept on a given tunnel,
2088 * distinguishing between root and leaf clients.
2090 * @param c Client whom to send the ACK.
2091 * @param t Tunnel on which to send the ACK.
2094 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
2096 struct GNUNET_MESH_LocalAck msg;
2097 MESH_TunnelNumber tid;
2100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2101 "Sending client ACK on tunnel %X\n",
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (owner, FWD ACK)\n");
2111 ack = tunnel_get_fwd_ack (t);
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (leaf, BCK ACK)\n");
2117 ack = tunnel_get_bck_ack (t);
2118 tid = t->local_tid_dest;
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2122 if (t->last_fwd_ack == ack)
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2126 t->last_fwd_ack = ack;
2127 msg.header.size = htons (sizeof (msg));
2128 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2129 msg.tunnel_id = htonl (tid);
2130 msg.max_pid = htonl (ack);
2132 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2133 &msg.header, GNUNET_NO);
2138 * Notify all clients (not depending on registration status) that the incoming
2139 * tunnel is no longer valid.
2141 * @param t Tunnel that was destroyed.
2144 send_clients_tunnel_destroy (struct MeshTunnel *t)
2146 struct GNUNET_MESH_TunnelMessage msg;
2148 msg.header.size = htons (sizeof (msg));
2149 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2150 msg.tunnel_id = htonl (t->local_tid_dest);
2151 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2156 * Notify clients of tunnel disconnections, if needed.
2157 * In case the origin disconnects, the destination clients get a tunnel destroy
2158 * notification. If the last destination disconnects (only one remaining client
2159 * in tunnel), the origin gets a (local ID) peer disconnected.
2160 * Note that the function must be called BEFORE removing the client from
2163 * @param t Tunnel that was destroyed.
2164 * @param c Client that disconnected.
2167 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2173 struct GNUNET_MESH_TunnelMessage msg;
2175 msg.header.size = htons (sizeof (msg));
2176 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2177 msg.tunnel_id = htonl (t->local_tid_dest);
2178 for (i = 0; i < t->nclients; i++)
2179 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2180 &msg.header, GNUNET_NO);
2182 // FIXME when to disconnect an incoming tunnel?
2183 else if (1 == t->nclients && NULL != t->owner)
2185 struct GNUNET_MESH_PeerControl msg;
2187 msg.header.size = htons (sizeof (msg));
2188 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2189 msg.tunnel_id = htonl (t->local_tid);
2190 msg.peer = my_full_id;
2191 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2192 &msg.header, GNUNET_NO);
2198 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2199 * and insert it in the appropiate structures if the peer is not known yet.
2201 * @param peer Full identity of the peer.
2203 * @return Existing or newly created peer info.
2205 static struct MeshPeerInfo *
2206 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2208 struct MeshPeerInfo *peer_info;
2210 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2211 if (NULL == peer_info)
2214 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2215 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2216 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2217 peer_info->id = GNUNET_PEER_intern (peer);
2225 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2226 * and insert it in the appropiate structures if the peer is not known yet.
2228 * @param peer Short identity of the peer.
2230 * @return Existing or newly created peer info.
2232 static struct MeshPeerInfo *
2233 peer_info_get_short (const GNUNET_PEER_Id peer)
2235 struct GNUNET_PeerIdentity id;
2237 GNUNET_PEER_resolve (peer, &id);
2238 return peer_info_get (&id);
2243 * Iterator to remove the tunnel from the list of tunnels a peer participates
2246 * @param cls Closure (tunnel info)
2247 * @param key GNUNET_PeerIdentity of the peer (unused)
2248 * @param value PeerInfo of the peer
2250 * @return always GNUNET_YES, to keep iterating
2253 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2255 struct MeshTunnel *t = cls;
2256 struct MeshPeerInfo *peer = value;
2259 for (i = 0; i < peer->ntunnels; i++)
2262 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2265 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2266 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2275 * Core callback to write a pre-constructed data packet to core buffer
2277 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2278 * @param size Number of bytes available in buf.
2279 * @param buf Where the to write the message.
2281 * @return number of bytes written to buf
2284 send_core_data_raw (void *cls, size_t size, void *buf)
2286 struct MeshTransmissionDescriptor *info = cls;
2287 struct GNUNET_MessageHeader *msg;
2290 GNUNET_assert (NULL != info);
2291 GNUNET_assert (NULL != info->mesh_data);
2292 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2293 total_size = ntohs (msg->size);
2295 if (total_size > size)
2300 memcpy (buf, msg, total_size);
2301 data_descriptor_decrement_rc (info->mesh_data);
2308 * Sends an already built non-multicast message to a peer,
2309 * properly registrating all used resources.
2311 * @param message Message to send. Function makes a copy of it.
2312 * @param peer Short ID of the neighbor whom to send the message.
2313 * @param t Tunnel on which this message is transmitted.
2316 send_message (const struct GNUNET_MessageHeader *message,
2317 const struct GNUNET_PeerIdentity *peer,
2318 struct MeshTunnel *t)
2320 struct MeshTransmissionDescriptor *info;
2321 struct MeshPeerInfo *neighbor;
2322 struct MeshPeerPath *p;
2325 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2327 size = ntohs (message->size);
2328 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2329 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2330 info->mesh_data->data = GNUNET_malloc (size);
2331 memcpy (info->mesh_data->data, message, size);
2332 if (ntohs(message->type) == GNUNET_MESSAGE_TYPE_MESH_UNICAST)
2334 struct GNUNET_MESH_Unicast *m;
2336 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2337 m->ttl = htonl (ntohl (m->ttl) - 1);
2339 info->mesh_data->data_len = size;
2340 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2341 *info->mesh_data->reference_counter = 1;
2342 neighbor = peer_info_get (peer);
2343 for (p = neighbor->path_head; NULL != p; p = p->next)
2352 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2353 GNUNET_free (info->mesh_data->data);
2354 GNUNET_free (info->mesh_data);
2358 info->peer = neighbor;
2368 * Sends a CREATE PATH message for a path to a peer, properly registrating
2369 * all used resources.
2371 * @param peer PeerInfo of the final peer for whom this path is being created.
2372 * @param p Path itself.
2373 * @param t Tunnel for which the path is created.
2376 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2377 struct MeshTunnel *t)
2379 struct GNUNET_PeerIdentity id;
2380 struct MeshPathInfo *path_info;
2381 struct MeshPeerInfo *neighbor;
2387 p = tree_get_path_to_peer (t->tree, peer->id);
2394 for (i = 0; i < p->length; i++)
2396 if (p->peers[i] == myid)
2399 if (i >= p->length - 1)
2405 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2407 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2408 path_info->path = p;
2410 neighbor = peer_info_get (&id);
2411 path_info->peer = neighbor;
2412 queue_add (path_info,
2413 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2414 sizeof (struct GNUNET_MESH_ManipulatePath) +
2415 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2422 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2424 * @param t Tunnel whose path to destroy.
2425 * @param destination Short ID of the peer to whom the path to destroy.
2428 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2430 struct MeshPeerPath *p;
2433 p = tree_get_path_to_peer (t->tree, destination);
2439 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2440 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2442 struct GNUNET_MESH_ManipulatePath *msg;
2443 struct GNUNET_PeerIdentity *pi;
2447 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2448 msg->header.size = htons (size);
2449 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2450 msg->tid = htonl (t->id.tid);
2451 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2452 for (i = 0; i < p->length; i++)
2454 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2456 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2463 * Try to establish a new connection to this peer.
2464 * Use the best path for the given tunnel.
2465 * If the peer doesn't have any path to it yet, try to get one.
2466 * If the peer already has some path, send a CREATE PATH towards it.
2468 * @param peer PeerInfo of the peer.
2469 * @param t Tunnel for which to create the path, if possible.
2472 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2474 struct MeshPeerPath *p;
2475 struct MeshPathInfo *path_info;
2477 if (NULL != peer->path_head)
2479 p = tree_get_path_to_peer (t->tree, peer->id);
2486 // FIXME always send create path to self
2489 send_create_path (peer, p, t);
2493 struct GNUNET_HashCode hash;
2496 send_client_peer_connected (t, myid);
2497 t->local_tid_dest = next_local_tid++;
2498 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2501 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2502 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2509 else if (NULL == peer->dhtget)
2511 struct GNUNET_PeerIdentity id;
2513 GNUNET_PEER_resolve (peer->id, &id);
2514 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2515 path_info->peer = peer;
2517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2518 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2519 peer->dhtgetcls = path_info;
2520 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2521 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2522 &id.hashPubKey, /* key to search */
2523 dht_replication_level, /* replication level */
2524 GNUNET_DHT_RO_RECORD_ROUTE |
2525 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2526 NULL, /* xquery */ // FIXME BLOOMFILTER
2527 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2528 &dht_get_id_handler, path_info);
2530 /* Otherwise, there is no path but the DHT get is already started. */
2535 * Task to delay the connection of a peer
2537 * @param cls Closure (path info with tunnel and peer to connect).
2538 * Will be free'd on exection.
2539 * @param tc TaskContext
2542 peer_info_connect_task (void *cls,
2543 const struct GNUNET_SCHEDULER_TaskContext *tc)
2545 struct MeshPathInfo *path_info = cls;
2547 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2552 peer_info_connect (path_info->peer, path_info->t);
2558 * Destroy the peer_info and free any allocated resources linked to it
2560 * @param pi The peer_info to destroy.
2562 * @return GNUNET_OK on success
2565 peer_info_destroy (struct MeshPeerInfo *pi)
2567 struct GNUNET_PeerIdentity id;
2568 struct MeshPeerPath *p;
2569 struct MeshPeerPath *nextp;
2571 GNUNET_PEER_resolve (pi->id, &id);
2572 GNUNET_PEER_change_rc (pi->id, -1);
2575 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2578 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2579 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2581 if (NULL != pi->dhtget)
2583 GNUNET_DHT_get_stop (pi->dhtget);
2584 GNUNET_free (pi->dhtgetcls);
2590 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2600 * Remove all paths that rely on a direct connection between p1 and p2
2601 * from the peer itself and notify all tunnels about it.
2603 * @param peer PeerInfo of affected peer.
2604 * @param p1 GNUNET_PEER_Id of one peer.
2605 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2608 * TODO: optimize (see below)
2611 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2614 struct MeshPeerPath *p;
2615 struct MeshPeerPath *aux;
2616 struct MeshPeerInfo *peer_d;
2618 unsigned int destroyed;
2623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2625 p = peer->path_head;
2629 for (i = 0; i < (p->length - 1); i++)
2631 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2632 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2634 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2645 for (i = 0; i < peer->ntunnels; i++)
2647 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2651 * Problem: one or more peers have been deleted from the tunnel tree.
2652 * We don't know who they are to try to add them again.
2653 * We need to try to find a new path for each of the disconnected peers.
2654 * Some of them might already have a path to reach them that does not
2655 * involve p1 and p2. Adding all anew might render in a better tree than
2656 * the trivial immediate fix.
2658 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2659 * its children will be reachable trough him.
2661 peer_d = peer_info_get_short (d);
2664 for (p = peer_d->path_head; NULL != p; p = p->next)
2666 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2674 /* No callback, as peer will be already disconnected and a connection
2675 * scheduled by tunnel_notify_connection_broken.
2677 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2681 peer_info_connect (peer_d, peer->tunnels[i]);
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2689 * Add the path to the peer and update the path used to reach it in case this
2692 * @param peer_info Destination peer to add the path to.
2693 * @param path New path to add. Last peer must be the peer in arg 1.
2694 * Path will be either used of freed if already known.
2695 * @param trusted Do we trust that this path is real?
2698 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2701 struct MeshPeerPath *aux;
2705 if ((NULL == peer_info) || (NULL == path))
2708 path_destroy (path);
2711 if (path->peers[path->length - 1] != peer_info->id)
2714 path_destroy (path);
2717 if (path->length <= 2 && GNUNET_NO == trusted)
2719 /* Only allow CORE to tell us about direct paths */
2720 path_destroy (path);
2723 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2724 for (l = 1; l < path->length; l++)
2726 if (path->peers[l] == myid)
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2729 for (l2 = 0; l2 < path->length - l; l2++)
2731 path->peers[l2] = path->peers[l + l2];
2736 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2741 struct GNUNET_PeerIdentity id;
2743 GNUNET_PEER_resolve (peer_info->id, &id);
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2745 path->length, GNUNET_i2s (&id));
2748 l = path_get_length (path);
2755 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2756 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2758 l2 = path_get_length (aux);
2761 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2762 peer_info->path_tail, aux, path);
2767 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2769 path_destroy (path);
2774 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2781 * Add the path to the origin peer and update the path used to reach it in case
2782 * this is the shortest.
2783 * The path is given in peer_info -> destination, therefore we turn the path
2784 * upside down first.
2786 * @param peer_info Peer to add the path to, being the origin of the path.
2787 * @param path New path to add after being inversed.
2788 * @param trusted Do we trust that this path is real?
2791 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2792 struct MeshPeerPath *path, int trusted)
2795 peer_info_add_path (peer_info, path, trusted);
2800 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2801 * to obtain a local peer -> destination path and interning the peer ids.
2803 * @return Newly allocated and created path
2805 static struct MeshPeerPath *
2806 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2807 unsigned int get_path_length,
2808 const struct GNUNET_PeerIdentity *put_path,
2809 unsigned int put_path_length)
2811 struct MeshPeerPath *p;
2817 GNUNET_PEER_change_rc (myid, 1);
2818 i = get_path_length;
2819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2820 for (i--; i >= 0; i--)
2822 id = GNUNET_PEER_intern (&get_path[i]);
2823 if (p->length > 0 && id == p->peers[p->length - 1])
2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2826 GNUNET_PEER_change_rc (id, -1);
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2831 GNUNET_i2s (&get_path[i]));
2833 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2834 p->peers[p->length - 1] = id;
2837 i = put_path_length;
2838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2839 for (i--; i >= 0; i--)
2841 id = GNUNET_PEER_intern (&put_path[i]);
2844 /* PUT path went through us, so discard the path up until now and start
2845 * from here to get a much shorter (and loop-free) path.
2850 if (p->length > 0 && id == p->peers[p->length - 1])
2852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2853 GNUNET_PEER_change_rc (id, -1);
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2858 GNUNET_i2s (&put_path[i]));
2860 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2861 p->peers[p->length - 1] = id;
2865 if (get_path_length > 0)
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2867 GNUNET_i2s (&get_path[0]));
2868 if (put_path_length > 0)
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2870 GNUNET_i2s (&put_path[0]));
2871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2873 for (i = 0; i < p->length; i++)
2875 struct GNUNET_PeerIdentity peer_id;
2877 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2879 GNUNET_i2s (&peer_id));
2887 * Adds a path to the peer_infos of all the peers in the path
2889 * @param p Path to process.
2890 * @param confirmed Whether we know if the path works or not.
2893 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2897 /* TODO: invert and add */
2898 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2899 for (i++; i < p->length; i++)
2901 struct MeshPeerInfo *aux;
2902 struct MeshPeerPath *copy;
2904 aux = peer_info_get_short (p->peers[i]);
2905 copy = path_duplicate (p);
2906 copy->length = i + 1;
2907 peer_info_add_path (aux, copy, GNUNET_NO);
2913 * Send keepalive packets for a peer
2915 * @param cls Closure (tunnel for which to send the keepalive).
2916 * @param tc Notification context.
2918 * TODO: implement explicit multicast keepalive?
2921 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2925 * Search for a tunnel among the incoming tunnels
2927 * @param tid the local id of the tunnel
2929 * @return tunnel handler, NULL if doesn't exist
2931 static struct MeshTunnel *
2932 tunnel_get_incoming (MESH_TunnelNumber tid)
2934 struct GNUNET_HashCode hash;
2936 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2937 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2938 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2943 * Search for a tunnel among the tunnels for a client
2945 * @param c the client whose tunnels to search in
2946 * @param tid the local id of the tunnel
2948 * @return tunnel handler, NULL if doesn't exist
2950 static struct MeshTunnel *
2951 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2953 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2955 return tunnel_get_incoming (tid);
2959 struct GNUNET_HashCode hash;
2961 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2962 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2968 * Search for a tunnel by global ID using PEER_ID
2970 * @param pi owner of the tunnel
2971 * @param tid global tunnel number
2973 * @return tunnel handler, NULL if doesn't exist
2975 static struct MeshTunnel *
2976 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2978 struct MESH_TunnelID id;
2979 struct GNUNET_HashCode hash;
2984 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2985 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2990 * Search for a tunnel by global ID using full PeerIdentities
2992 * @param oid owner of the tunnel
2993 * @param tid global tunnel number
2995 * @return tunnel handler, NULL if doesn't exist
2997 static struct MeshTunnel *
2998 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3000 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3005 * Delete an active client from the tunnel.
3011 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3015 for (i = 0; i < t->nclients; i++)
3017 if (t->clients[i] == c)
3019 t->clients[i] = t->clients[t->nclients - 1];
3020 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3021 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3023 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3031 * Delete an ignored client from the tunnel.
3037 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3041 for (i = 0; i < t->nignore; i++)
3043 if (t->ignore[i] == c)
3045 t->ignore[i] = t->ignore[t->nignore - 1];
3046 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3054 * Delete a client from the tunnel. It should be only done on
3055 * client disconnection, otherwise use client_ignore_tunnel.
3061 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3063 tunnel_delete_ignored_client (t, c);
3064 tunnel_delete_active_client (t, c);
3069 * Iterator to free MeshTunnelChildInfo of tunnel children.
3071 * @param cls Closure (tunnel info).
3072 * @param key Hash of GNUNET_PEER_Id (unused).
3073 * @param value MeshTunnelChildInfo of the child.
3075 * @return always GNUNET_YES, to keep iterating
3078 tunnel_destroy_child (void *cls,
3079 const struct GNUNET_HashCode * key,
3082 GNUNET_free (value);
3088 * Callback used to notify a client owner of a tunnel that a peer has
3089 * disconnected, most likely because of a path change.
3091 * @param cls Closure (tunnel this notification is about).
3092 * @param peer_id Short ID of disconnected peer.
3095 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3097 struct MeshTunnel *t = cls;
3098 struct MeshPeerInfo *peer;
3099 struct MeshPathInfo *path_info;
3101 if (NULL != t->owner && NULL != nc)
3103 struct GNUNET_MESH_PeerControl msg;
3105 msg.header.size = htons (sizeof (msg));
3106 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3107 msg.tunnel_id = htonl (t->local_tid);
3108 GNUNET_PEER_resolve (peer_id, &msg.peer);
3109 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3110 &msg.header, GNUNET_NO);
3112 peer = peer_info_get_short (peer_id);
3113 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3114 path_info->peer = peer;
3116 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3121 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3122 * needed rescources.
3123 * If peer already exists, reevaluate shortest path and change if different.
3125 * @param t Tunnel we want to add a new peer to
3126 * @param peer PeerInfo of the peer being added
3130 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3132 struct GNUNET_PeerIdentity id;
3133 struct MeshPeerPath *best_p;
3134 struct MeshPeerPath *p;
3135 unsigned int best_cost;
3138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3139 GNUNET_PEER_resolve (peer->id, &id);
3141 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3144 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3145 GNUNET_assert (GNUNET_OK ==
3146 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3148 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3151 if (NULL != (p = peer->path_head))
3154 best_cost = tree_get_path_cost (t->tree, p);
3157 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3164 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3165 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3166 t->path_refresh_task =
3167 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3171 /* Start a DHT get */
3172 peer_info_connect (peer, t);
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3178 * Add a path to a tunnel which we don't own, just to remember the next hop.
3179 * If destination node was already in the tunnel, the first hop information
3180 * will be replaced with the new path.
3182 * @param t Tunnel we want to add a new peer to
3183 * @param p Path to add
3184 * @param own_pos Position of local node in path.
3188 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3189 unsigned int own_pos)
3191 struct GNUNET_PeerIdentity id;
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3194 GNUNET_assert (0 != own_pos);
3195 tree_add_path (t->tree, p, NULL, NULL);
3196 if (own_pos < p->length - 1)
3198 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3199 tree_update_first_hops (t->tree, myid, &id);
3201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3205 * Add a client to a tunnel, initializing all needed data structures.
3207 * @param t Tunnel to which add the client.
3208 * @param c Client which to add to the tunnel.
3211 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3213 struct MeshTunnelClientInfo clinfo;
3215 GNUNET_array_append (t->clients, t->nclients, c);
3217 clinfo.fwd_ack = t->fwd_pid + 1;
3218 clinfo.bck_ack = t->bck_ack + 1; // FIXME fc review
3219 clinfo.pid = t->fwd_pid;
3220 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3225 * Notifies a tunnel that a connection has broken that affects at least
3226 * some of its peers. Sends a notification towards the root of the tree.
3227 * In case the peer is the owner of the tree, notifies the client that owns
3228 * the tunnel and tries to reconnect.
3230 * @param t Tunnel affected.
3231 * @param p1 Peer that got disconnected from p2.
3232 * @param p2 Peer that got disconnected from p1.
3234 * @return Short ID of the peer disconnected (either p1 or p2).
3235 * 0 if the tunnel remained unaffected.
3237 static GNUNET_PEER_Id
3238 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3244 tree_notify_connection_broken (t->tree, p1, p2,
3245 &tunnel_notify_client_peer_disconnected,
3247 if (myid != p1 && myid != p2)
3253 if (tree_get_predecessor (t->tree) != 0)
3255 /* We are the peer still connected, notify owner of the disconnection. */
3256 struct GNUNET_MESH_PathBroken msg;
3257 struct GNUNET_PeerIdentity neighbor;
3259 msg.header.size = htons (sizeof (msg));
3260 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3261 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3262 msg.tid = htonl (t->id.tid);
3263 msg.peer1 = my_full_id;
3264 GNUNET_PEER_resolve (pid, &msg.peer2);
3265 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3266 send_message (&msg.header, &neighbor, t);
3274 * Send a multicast packet to a neighbor.
3276 * @param cls Closure (Info about the multicast packet)
3277 * @param neighbor_id Short ID of the neighbor to send the packet to.
3280 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3282 struct MeshData *mdata = cls;
3283 struct MeshTransmissionDescriptor *info;
3284 struct GNUNET_PeerIdentity neighbor;
3286 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3288 info->mesh_data = mdata;
3289 (*(mdata->reference_counter)) ++;
3290 info->destination = neighbor_id;
3291 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3293 GNUNET_i2s (&neighbor));
3294 info->peer = peer_info_get (&neighbor);
3295 GNUNET_assert (NULL != info->peer);
3297 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3298 info->mesh_data->data_len,
3305 * Send a message in a tunnel in multicast, sending a copy to each child node
3306 * down the local one in the tunnel tree.
3308 * @param t Tunnel in which to send the data.
3309 * @param msg Message to be sent.
3310 * @param internal Has the service generated this message?
3313 tunnel_send_multicast (struct MeshTunnel *t,
3314 const struct GNUNET_MessageHeader *msg,
3317 struct MeshData *mdata;
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 " sending a multicast packet...\n");
3321 mdata = GNUNET_malloc (sizeof (struct MeshData));
3322 mdata->data_len = ntohs (msg->size);
3323 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3325 mdata->data = GNUNET_malloc (mdata->data_len);
3326 memcpy (mdata->data, msg, mdata->data_len);
3327 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3329 struct GNUNET_MESH_Multicast *mcast;
3331 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3332 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3334 ntohl (mcast->ttl));
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3340 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3341 && GNUNET_NO == internal)
3343 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3345 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3351 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3352 if (*(mdata->reference_counter) == 0)
3354 GNUNET_free (mdata->data);
3355 GNUNET_free (mdata->reference_counter);
3356 if (NULL != mdata->task)
3358 GNUNET_SCHEDULER_cancel(*(mdata->task));
3359 GNUNET_free (mdata->task);
3360 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3362 // FIXME change order?
3363 GNUNET_free (mdata);
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3366 " sending a multicast packet done\n");
3372 * Increase the SKIP value of all peers that
3373 * have not received a unicast message.
3375 * @param cls Closure (ID of the peer that HAS received the message).
3376 * @param key ID of the neighbor.
3377 * @param value Information about the neighbor.
3379 * @return GNUNET_YES to keep iterating.
3382 tunnel_add_skip (void *cls,
3383 const struct GNUNET_HashCode * key,
3386 struct GNUNET_PeerIdentity *neighbor = cls;
3387 struct MeshTunnelChildInfo *cinfo = value;
3389 /* TODO compare only pointers? key == neighbor? */
3390 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3400 * @brief Get neighbor's Flow Control information.
3402 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3403 * descendant of the local node in a certain tunnel.
3404 * If the info is not yet there (recently created path), creates the data struct
3405 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3408 * @param t Tunnel related.
3409 * @param peer Neighbor whose Flow Control info is needed.
3411 * @return Neighbor's Flow Control info.
3413 static struct MeshTunnelChildInfo *
3414 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3415 const struct GNUNET_PeerIdentity *peer)
3417 struct MeshTunnelChildInfo *cinfo;
3418 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3422 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3423 cinfo->id = GNUNET_PEER_intern (peer);
3424 cinfo->skip = t->fwd_pid;
3425 cinfo->fwd_ack = t->fwd_pid + t->fwd_queue_max - t->fwd_queue_n; // FIXME review
3427 GNUNET_assert (GNUNET_OK ==
3428 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3431 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3438 * Iterator to get the appropiate ACK value from all children nodes.
3440 * @param cls Closue (tunnel).
3441 * @param id Id of the child node.
3444 tunnel_get_child_fwd_ack (void *cls,
3447 struct GNUNET_PeerIdentity peer_id;
3448 struct MeshTunnelChildInfo *cinfo;
3449 struct MeshTunnelChildIteratorContext *ctx = cls;
3450 struct MeshTunnel *t = ctx->t;
3453 GNUNET_PEER_resolve (id, &peer_id);
3454 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3455 ack = cinfo->fwd_ack;
3457 if (GNUNET_NO == ctx->init)
3459 ctx->max_child_ack = ack;
3460 ctx->init = GNUNET_YES;
3463 if (GNUNET_YES == t->speed_min)
3465 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3469 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3476 * Get the maximum PID allowed to transmit to any
3477 * tunnel child of the local peer, depending on the tunnel
3478 * buffering/speed settings.
3482 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3485 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3487 struct MeshTunnelChildIteratorContext ctx;
3489 ctx.max_child_ack = 0;
3491 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3493 if (0 == ctx.nchildren)
3496 if (GNUNET_YES == t->nobuffer && is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3497 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3499 return (int64_t) ctx.max_child_ack;
3504 * Set the FWD ACK value of a client in a particular tunnel.
3506 * @param t Tunnel affected.
3507 * @param c Client whose ACK to set.
3508 * @param ack ACK value.
3511 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3512 struct MeshClient *c,
3517 for (i = 0; i < t->nclients; i++)
3519 if (t->clients[i] != c)
3521 t->clients_fc[i].fwd_ack = ack;
3529 * Get the ACK value of a client in a particular tunnel.
3531 * @param t Tunnel on which to look.
3532 * @param c Client whose ACK to get.
3534 * @return ACK value.
3537 tunnel_get_client_fwd_ack (struct MeshTunnel *t,
3538 struct MeshClient *c)
3542 for (i = 0; i < t->nclients; i++)
3544 if (t->clients[i] != c)
3546 return t->clients_fc[i].fwd_ack;
3554 * Get the highest ACK value of all clients in a particular tunnel,
3555 * according to the buffering/speed settings.
3557 * @param t Tunnel on which to look.
3559 * @return Corresponding ACK value (max uint32_t).
3560 * If no clients are suscribed, -1.
3563 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3568 if (0 == t->nclients)
3571 for (ack = -1, i = 0; i < t->nclients; i++)
3574 (GNUNET_YES == t->speed_min &&
3575 GNUNET_YES == is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3576 (GNUNET_NO == t->speed_min &&
3577 GNUNET_YES == is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3579 ack = t->clients_fc[i].fwd_ack;
3583 if (GNUNET_YES == t->nobuffer && is_pid_bigger(ack, t->fwd_pid))
3584 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3586 return (uint32_t) ack;
3591 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3592 * mode and the status of all children nodes.
3596 * @return Maximum PID allowed.
3599 tunnel_get_fwd_ack (struct MeshTunnel *t)
3602 uint32_t buffer_free;
3607 count = t->fwd_pid - t->skip;
3608 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3609 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3610 child_ack = tunnel_get_children_fwd_ack (t);
3611 client_ack = tunnel_get_clients_fwd_ack (t);
3612 if (-1 == child_ack)
3614 // Node has no children, child_ack AND core buffer are irrelevant.
3615 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good!
3616 return (uint32_t) client_ack;
3619 if (GNUNET_YES == t->speed_min)
3621 ack = min_pid ((uint32_t) child_ack, ack);
3622 ack = min_pid ((uint32_t) client_ack, ack);
3626 ack = max_pid ((uint32_t) child_ack, ack);
3627 ack = max_pid ((uint32_t) client_ack, ack);
3629 if (GNUNET_YES == t->nobuffer && is_pid_bigger(ack, t->fwd_pid))
3630 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3632 count, buffer_free, child_ack, client_ack, ack);
3637 * Get the current bck ack value for a tunnel, taking in account the tunnel
3638 * mode and the status of all children nodes.
3642 * @return Maximum PID allowed.
3645 tunnel_get_bck_ack (struct MeshTunnel *t)
3649 if (GNUNET_YES == t->nobuffer)
3651 if (t->bck_ack > t->bck_pid)
3653 return t->bck_pid + 1;
3660 ack = t->bck_pid + t->bck_queue_max - t->bck_queue_n;
3667 send_local_ack (struct MeshClient *c, struct MeshTunnel *t, uint32_t ack)
3669 struct GNUNET_MESH_LocalAck msg;
3671 msg.header.size = htons (sizeof (msg));
3672 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3673 msg.tunnel_id = htonl (t->local_tid_dest);
3674 msg.max_pid = htonl (ack);
3675 GNUNET_SERVER_notification_context_unicast(nc,
3682 * Build an ACK message and send it to the given peer.
3685 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3687 struct GNUNET_MESH_ACK msg;
3689 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3690 msg.header.size = htons (sizeof (msg));
3691 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3692 msg.pid = htonl (ack);
3693 msg.tid = htonl (t->id.tid);
3695 send_message (&msg.header, peer, t);
3700 * Send an ACK informing the predecessor about the available buffer space.
3701 * In case there is no predecessor, inform the owning client.
3702 * If buffering is off, send only on behalf of children or self if endpoint.
3703 * If buffering is on, send when sent to children and buffer space is free.
3704 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3705 * the ACK itself goes "back" (towards root).
3707 * @param t Tunnel on which to send the ACK.
3708 * @param type Type of message that triggered the ACK transmission.
3711 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3713 struct GNUNET_PeerIdentity id;
3716 if (NULL != t->owner)
3718 send_client_tunnel_ack (t->owner, t);
3721 /* Is it after unicast / multicast retransmission? */
3724 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3725 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3727 "ACK due to FWD DATA retransmission\n");
3728 if (GNUNET_YES == t->nobuffer)
3730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3733 if (t->fwd_queue_max > t->fwd_queue_n * 2)
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3739 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3740 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3746 /* Ok, ACK might be necessary, what PID to ACK? */
3747 ack = tunnel_get_fwd_ack (t);
3749 /* If speed_min and not all children have ack'd, dont send yet */
3750 if (ack == t->last_fwd_ack)
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3756 t->last_fwd_ack = ack;
3757 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3758 send_ack (t, &id, ack);
3763 * Iterator to send a child node a BCK ACK to allow him to send more
3766 * @param cls Closure (tunnel).
3767 * @param id Id of the child node.
3770 tunnel_send_child_bck_ack (void *cls,
3773 struct MeshTunnel *t = cls;
3774 struct MeshTunnelChildInfo *cinfo;
3775 struct GNUNET_PeerIdentity peer;
3777 GNUNET_PEER_resolve (id, &peer);
3778 cinfo = tunnel_get_neighbor_fc (t, &peer);
3780 if (cinfo->bck_ack != cinfo->pid &&
3781 GNUNET_NO == is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3785 send_ack (t, &peer, cinfo->bck_ack);
3790 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3792 * Iterates over all clients and sends BCK ACKs to the ones that need
3794 * @param t Tunnel on which to send the BCK ACKs.
3797 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3801 /* Find client whom to allow to send to origin (with lowest buffer space) */
3802 for (i = 0; i < t->nclients; i++)
3804 struct MeshTunnelClientInfo *clinfo;
3807 clinfo = &t->clients_fc[i];
3808 delta = clinfo->bck_ack - clinfo->pid;
3810 if ((GNUNET_NO == t->nobuffer && ACK_THRESHOLD > delta) ||
3811 (GNUNET_YES == t->nobuffer && 0 == delta))
3816 ack += t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3817 send_local_ack(t->clients[i], t, ack);
3818 clinfo->bck_ack = ack;
3825 * Send an ACK informing the children nodes and destination clients about
3826 * the available buffer space.
3827 * If buffering is off, send only on behalf of root (can be self).
3828 * If buffering is on, send when sent to predecessor and buffer space is free.
3829 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3830 * the ACK itself goes "forward" (towards children/clients).
3832 * @param t Tunnel on which to send the ACK.
3833 * @param type Type of message that triggered the ACK transmission.
3836 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3838 if (NULL != t->owner)
3840 send_client_tunnel_ack (t->owner, t);
3843 /* Is it after data to_origin retransmission? */
3846 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 "ACK due to BCK DATA retransmission\n");
3849 if (GNUNET_YES == t->nobuffer)
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3854 if (t->bck_queue_max > t->bck_queue_n * 2)
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3860 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3861 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3867 tunnel_send_clients_bck_ack (t);
3868 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
3873 * Send a message to all peers in this tunnel that the tunnel is no longer
3876 * @param t The tunnel whose peers to notify.
3879 tunnel_send_destroy (struct MeshTunnel *t)
3881 struct GNUNET_MESH_TunnelDestroy msg;
3883 msg.header.size = htons (sizeof (msg));
3884 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3885 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3886 msg.tid = htonl (t->id.tid);
3887 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3892 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3894 * @param cls Closure (Tunnel which to cancel).
3895 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3898 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3900 struct MeshTunnel *t = cls;
3901 struct MeshPeerInfo *peer_info;
3902 struct MeshPeerQueue *pq;
3903 struct MeshPeerQueue *next;
3905 peer_info = peer_info_get_short (neighbor_id);
3906 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3909 if (pq->tunnel == t)
3911 queue_destroy (pq, GNUNET_YES);
3914 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3916 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3917 peer_info->core_transmit = NULL;
3922 * Destroy the tunnel and free any allocated resources linked to it.
3924 * @param t the tunnel to destroy
3926 * @return GNUNET_OK on success
3929 tunnel_destroy (struct MeshTunnel *t)
3931 struct MeshClient *c;
3932 struct GNUNET_HashCode hash;
3939 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3945 struct GNUNET_PeerIdentity id;
3947 GNUNET_PEER_resolve (t->id.oid, &id);
3948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3949 GNUNET_i2s (&id), t->id.tid);
3951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3955 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3956 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3961 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3964 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3968 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3969 for (i = 0; i < t->nclients; i++)
3973 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3978 for (i = 0; i < t->nignore; i++)
3982 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3987 if (t->nclients > 0)
3990 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3994 GNUNET_free (t->clients);
3996 if (NULL != t->peers)
3998 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4000 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4003 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4004 &tunnel_destroy_child,
4006 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4008 tree_destroy (t->tree);
4010 if (NULL != t->regex_ctx)
4011 regex_cancel_search (t->regex_ctx);
4012 if (NULL != t->dht_get_type)
4013 GNUNET_DHT_get_stop (t->dht_get_type);
4014 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4015 GNUNET_SCHEDULER_cancel (t->timeout_task);
4016 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4017 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4020 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4021 GNUNET_assert (0 <= n_tunnels);
4028 * Create a new tunnel
4030 * @param owner Who is the owner of the tunnel (short ID).
4031 * @param tid Tunnel Number of the tunnel.
4032 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4033 * @param local Tunnel Number for the tunnel, for the client point of view.
4035 * @return A new initialized tunnel. NULL on error.
4037 static struct MeshTunnel *
4038 tunnel_new (GNUNET_PEER_Id owner,
4039 MESH_TunnelNumber tid,
4040 struct MeshClient *client,
4041 MESH_TunnelNumber local)
4043 struct MeshTunnel *t;
4044 struct GNUNET_HashCode hash;
4046 if (n_tunnels >= max_tunnels && NULL == client)
4049 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4052 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4053 t->bck_queue_max = t->fwd_queue_max;
4054 t->tree = tree_new (owner);
4056 t->bck_ack = INITIAL_WINDOW_SIZE;
4057 t->last_fwd_ack = INITIAL_WINDOW_SIZE;
4058 t->local_tid = local;
4059 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4061 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4063 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4065 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4073 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4080 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4082 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4083 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4087 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4097 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4098 * that are no longer needed, as well as nodes of no longer reachable peers.
4099 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4101 * @param t Tunnel from which to remove the path.
4102 * @param peer Short id of the peer which should be removed.
4105 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4107 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4113 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4114 * client when the client disconnects. If the client is not the owner, the
4115 * owner will get notified if no more clients are in the tunnel and the client
4116 * get removed from the tunnel's list.
4118 * @param cls closure (client that is disconnecting)
4119 * @param key the hash of the local tunnel id (used to access the hashmap)
4120 * @param value the value stored at the key (tunnel to destroy)
4122 * @return GNUNET_OK on success
4125 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4127 struct MeshTunnel *t = value;
4128 struct MeshClient *c = cls;
4131 send_client_tunnel_disconnect(t, c);
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4135 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4136 tunnel_delete_client(t, c);
4137 client_delete_tunnel(c, t);
4140 tunnel_send_destroy(t);
4141 r = tunnel_destroy (t);
4147 * Timeout function, destroys tunnel if called
4149 * @param cls Closure (tunnel to destroy).
4150 * @param tc TaskContext
4153 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4155 struct MeshTunnel *t = cls;
4157 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4159 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4164 * Resets the tunnel timeout. Starts it if no timeout was running.
4166 * @param t Tunnel whose timeout to reset.
4168 * TODO use heap to improve efficiency of scheduler.
4171 tunnel_reset_timeout (struct MeshTunnel *t)
4173 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4174 GNUNET_SCHEDULER_cancel (t->timeout_task);
4176 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4177 (refresh_path_time, 4), &tunnel_timeout, t);
4181 /******************************************************************************/
4182 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4183 /******************************************************************************/
4186 * Function to send a create path packet to a peer.
4188 * @param cls closure
4189 * @param size number of bytes available in buf
4190 * @param buf where the callee should write the message
4191 * @return number of bytes written to buf
4194 send_core_path_create (void *cls, size_t size, void *buf)
4196 struct MeshPathInfo *info = cls;
4197 struct GNUNET_MESH_ManipulatePath *msg;
4198 struct GNUNET_PeerIdentity *peer_ptr;
4199 struct MeshTunnel *t = info->t;
4200 struct MeshPeerPath *p = info->path;
4205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4207 sizeof (struct GNUNET_MESH_ManipulatePath) +
4208 p->length * sizeof (struct GNUNET_PeerIdentity);
4210 if (size < size_needed || NULL == buf)
4215 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4216 msg->header.size = htons (size_needed);
4217 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4218 msg->tid = ntohl (t->id.tid);
4220 if (GNUNET_YES == t->speed_min)
4221 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4222 if (GNUNET_YES == t->nobuffer)
4223 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4224 msg->opt = htonl(opt);
4226 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4227 for (i = 0; i < p->length; i++)
4229 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4236 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4242 * Fill the core buffer
4244 * @param cls closure (data itself)
4245 * @param size number of bytes available in buf
4246 * @param buf where the callee should write the message
4248 * @return number of bytes written to buf
4251 send_core_data_multicast (void *cls, size_t size, void *buf)
4253 struct MeshTransmissionDescriptor *info = cls;
4256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4257 GNUNET_assert (NULL != info);
4258 GNUNET_assert (NULL != info->peer);
4259 total_size = info->mesh_data->data_len;
4260 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4262 if (total_size > size)
4267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4268 memcpy (buf, info->mesh_data->data, total_size);
4271 struct GNUNET_MESH_Multicast *mc;
4272 struct GNUNET_MessageHeader *mh;
4275 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4277 mc = (struct GNUNET_MESH_Multicast *) mh;
4278 mh = (struct GNUNET_MessageHeader *) &mc[1];
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4280 " multicast, payload type %u\n", ntohs (mh->type));
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4282 " multicast, payload size %u\n", ntohs (mh->size));
4286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
4291 data_descriptor_decrement_rc (info->mesh_data);
4292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4300 * Creates a path ack message in buf and frees all unused resources.
4302 * @param cls closure (MeshTransmissionDescriptor)
4303 * @param size number of bytes available in buf
4304 * @param buf where the callee should write the message
4305 * @return number of bytes written to buf
4308 send_core_path_ack (void *cls, size_t size, void *buf)
4310 struct MeshTransmissionDescriptor *info = cls;
4311 struct GNUNET_MESH_PathACK *msg = buf;
4313 GNUNET_assert (NULL != info);
4314 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4319 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4320 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4321 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4322 msg->tid = htonl (info->origin->tid);
4323 msg->peer_id = my_full_id;
4326 /* TODO add signature */
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4329 return sizeof (struct GNUNET_MESH_PathACK);
4334 * Free a transmission that was already queued with all resources
4335 * associated to the request.
4337 * @param queue Queue handler to cancel.
4338 * @param clear_cls Is it necessary to free associated cls?
4341 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4343 struct MeshTransmissionDescriptor *dd;
4344 struct MeshPathInfo *path_info;
4346 if (GNUNET_YES == clear_cls)
4348 switch (queue->type)
4350 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4351 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4352 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4355 data_descriptor_decrement_rc (dd->mesh_data);
4357 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4359 path_info = queue->cls;
4360 path_destroy (path_info->path);
4364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4366 GNUNET_free_non_null (queue->cls);
4368 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4369 queue->peer->queue_tail,
4371 GNUNET_free (queue);
4376 * Core callback to write a queued packet to core buffer
4378 * @param cls Closure (peer info).
4379 * @param size Number of bytes available in buf.
4380 * @param buf Where the to write the message.
4382 * @return number of bytes written to buf
4385 queue_send (void *cls, size_t size, void *buf)
4387 struct MeshPeerInfo *peer = cls;
4388 struct GNUNET_MessageHeader *msg;
4389 struct MeshPeerQueue *queue;
4390 struct MeshTunnel *t;
4393 peer->core_transmit = NULL;
4394 queue = peer->queue_head;
4396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4398 /* If queue is empty, send should have been cancelled */
4404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4406 /* Check if buffer size is enough for the message */
4407 if (queue->size > size)
4409 struct GNUNET_PeerIdentity id;
4411 GNUNET_PEER_resolve (peer->id, &id);
4412 peer->core_transmit =
4413 GNUNET_CORE_notify_transmit_ready(core_handle,
4416 GNUNET_TIME_UNIT_FOREVER_REL,
4423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4429 switch (queue->type)
4431 case 0: // RAW data (preconstructed message, retransmission, etc.)
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* raw\n");
4433 data_size = send_core_data_raw (queue->cls, size, buf);
4434 msg = (struct GNUNET_MessageHeader *) buf;
4435 switch (ntohs (msg->type)) // Type of preconstructed message
4437 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4438 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4440 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4441 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4447 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4449 data_size = send_core_data_multicast(queue->cls, size, buf);
4450 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4452 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4454 data_size = send_core_path_create(queue->cls, size, buf);
4456 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4458 data_size = send_core_path_ack(queue->cls, size, buf);
4462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* type unknown\n");
4466 /* Free queue, but cls was freed by send_core_* */
4467 queue_destroy (queue, GNUNET_NO);
4469 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4475 /* If more data in queue, send next */
4476 if (NULL != peer->queue_head)
4478 struct GNUNET_PeerIdentity id;
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4481 GNUNET_PEER_resolve (peer->id, &id);
4482 peer->core_transmit =
4483 GNUNET_CORE_notify_transmit_ready(core_handle,
4486 GNUNET_TIME_UNIT_FOREVER_REL,
4488 peer->queue_head->size,
4492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4498 * Queue and pass message to core when possible.
4500 * @param cls Closure (type dependant).
4501 * @param type Type of the message, 0 for a raw message.
4502 * @param size Size of the message.
4503 * @param dst Neighbor to send message to.
4504 * @param t Tunnel this message belongs to.
4507 queue_add (void *cls, uint16_t type, size_t size,
4508 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4510 struct MeshPeerQueue *queue;
4514 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4515 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4517 n = &t->fwd_queue_n;
4518 max = &t->fwd_queue_max;
4522 n = &t->bck_queue_n;
4523 max = &t->bck_queue_max;
4527 if (NULL == t->owner)
4528 GNUNET_break_op(0); // TODO: kill connection?
4531 return; // Drop message
4534 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4540 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4541 if (NULL == dst->core_transmit)
4543 struct GNUNET_PeerIdentity id;
4545 GNUNET_PEER_resolve (dst->id, &id);
4546 dst->core_transmit =
4547 GNUNET_CORE_notify_transmit_ready(core_handle,
4550 GNUNET_TIME_UNIT_FOREVER_REL,
4559 /******************************************************************************/
4560 /******************** MESH NETWORK HANDLERS **************************/
4561 /******************************************************************************/
4565 * Core handler for path creation
4567 * @param cls closure
4568 * @param message message
4569 * @param peer peer identity this notification is about
4570 * @param atsi performance data
4571 * @param atsi_count number of records in 'atsi'
4573 * @return GNUNET_OK to keep the connection open,
4574 * GNUNET_SYSERR to close it (signal serious error)
4577 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4578 const struct GNUNET_MessageHeader *message,
4579 const struct GNUNET_ATS_Information *atsi,
4580 unsigned int atsi_count)
4582 unsigned int own_pos;
4585 MESH_TunnelNumber tid;
4586 struct GNUNET_MESH_ManipulatePath *msg;
4587 struct GNUNET_PeerIdentity *pi;
4588 struct GNUNET_HashCode hash;
4589 struct MeshPeerPath *path;
4590 struct MeshPeerInfo *dest_peer_info;
4591 struct MeshPeerInfo *orig_peer_info;
4592 struct MeshTunnel *t;
4594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4595 "Received a path create msg [%s]\n",
4596 GNUNET_i2s (&my_full_id));
4597 size = ntohs (message->size);
4598 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4600 GNUNET_break_op (0);
4604 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4605 if (size % sizeof (struct GNUNET_PeerIdentity))
4607 GNUNET_break_op (0);
4610 size /= sizeof (struct GNUNET_PeerIdentity);
4613 GNUNET_break_op (0);
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4617 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4619 tid = ntohl (msg->tid);
4620 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4622 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4623 t = tunnel_get (pi, tid);
4624 if (NULL == t) // FIXME only for INCOMING tunnels?
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4629 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4632 // FIXME notify failure
4635 opt = ntohl (msg->opt);
4636 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4637 GNUNET_YES : GNUNET_NO;
4638 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4639 GNUNET_YES : GNUNET_NO;
4641 if (GNUNET_YES == t->nobuffer)
4643 t->bck_queue_max = 1;
4644 t->fwd_queue_max = 1;
4647 // FIXME only assign a local tid if a local client is interested (on demand)
4648 while (NULL != tunnel_get_incoming (next_local_tid))
4649 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4650 t->local_tid_dest = next_local_tid++;
4651 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4654 tunnel_reset_timeout (t);
4655 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4657 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4658 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4666 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4667 if (NULL == dest_peer_info)
4669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4670 " Creating PeerInfo for destination.\n");
4671 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4672 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4673 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4675 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4677 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4678 if (NULL == orig_peer_info)
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4681 " Creating PeerInfo for origin.\n");
4682 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4683 orig_peer_info->id = GNUNET_PEER_intern (pi);
4684 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4688 path = path_new (size);
4690 for (i = 0; i < size; i++)
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4693 GNUNET_i2s (&pi[i]));
4694 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4695 if (path->peers[i] == myid)
4698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4701 /* cannot be self, must be 'not found' */
4702 /* create path: self not found in path through self */
4703 GNUNET_break_op (0);
4704 path_destroy (path);
4708 path_add_to_peers (path, GNUNET_NO);
4709 tunnel_add_path (t, path, own_pos);
4710 if (own_pos == size - 1)
4712 /* It is for us! Send ack. */
4713 struct MeshTransmissionDescriptor *info;
4715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4716 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4717 if (NULL == t->peers)
4719 /* New tunnel! Notify clients on data. */
4720 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4722 GNUNET_break (GNUNET_SYSERR !=
4723 GNUNET_CONTAINER_multihashmap_put (t->peers,
4724 &my_full_id.hashPubKey,
4727 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4728 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4729 info->origin = &t->id;
4730 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4731 GNUNET_assert (NULL != info->peer);
4733 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4734 sizeof (struct GNUNET_MESH_PathACK),
4740 struct MeshPeerPath *path2;
4742 /* It's for somebody else! Retransmit. */
4743 path2 = path_duplicate (path);
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4745 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4746 path2 = path_duplicate (path);
4747 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4748 send_create_path (dest_peer_info, path, t);
4755 * Core handler for path destruction
4757 * @param cls closure
4758 * @param message message
4759 * @param peer peer identity this notification is about
4760 * @param atsi performance data
4761 * @param atsi_count number of records in 'atsi'
4763 * @return GNUNET_OK to keep the connection open,
4764 * GNUNET_SYSERR to close it (signal serious error)
4767 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4768 const struct GNUNET_MessageHeader *message,
4769 const struct GNUNET_ATS_Information *atsi,
4770 unsigned int atsi_count)
4772 struct GNUNET_MESH_ManipulatePath *msg;
4773 struct GNUNET_PeerIdentity *pi;
4774 struct MeshPeerPath *path;
4775 struct MeshTunnel *t;
4776 unsigned int own_pos;
4780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4781 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4782 size = ntohs (message->size);
4783 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4785 GNUNET_break_op (0);
4789 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4790 if (size % sizeof (struct GNUNET_PeerIdentity))
4792 GNUNET_break_op (0);
4795 size /= sizeof (struct GNUNET_PeerIdentity);
4798 GNUNET_break_op (0);
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4803 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4804 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4806 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4808 t = tunnel_get (pi, ntohl (msg->tid));
4811 /* TODO notify back: we don't know this tunnel */
4812 GNUNET_break_op (0);
4815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4816 path = path_new (size);
4818 for (i = 0; i < size; i++)
4820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4821 GNUNET_i2s (&pi[i]));
4822 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4823 if (path->peers[i] == myid)
4826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4827 if (own_pos < path->length - 1)
4828 send_message (message, &pi[own_pos + 1], t);
4830 send_client_tunnel_disconnect(t, NULL);
4832 tunnel_delete_peer (t, path->peers[path->length - 1]);
4833 path_destroy (path);
4839 * Core handler for notifications of broken paths
4841 * @param cls closure
4842 * @param message message
4843 * @param peer peer identity this notification is about
4844 * @param atsi performance data
4845 * @param atsi_count number of records in 'atsi'
4847 * @return GNUNET_OK to keep the connection open,
4848 * GNUNET_SYSERR to close it (signal serious error)
4851 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4852 const struct GNUNET_MessageHeader *message,
4853 const struct GNUNET_ATS_Information *atsi,
4854 unsigned int atsi_count)
4856 struct GNUNET_MESH_PathBroken *msg;
4857 struct MeshTunnel *t;
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4860 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4861 msg = (struct GNUNET_MESH_PathBroken *) message;
4862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4863 GNUNET_i2s (&msg->peer1));
4864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4865 GNUNET_i2s (&msg->peer2));
4866 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4869 GNUNET_break_op (0);
4872 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4873 GNUNET_PEER_search (&msg->peer2));
4880 * Core handler for tunnel destruction
4882 * @param cls closure
4883 * @param message message
4884 * @param peer peer identity this notification is about
4885 * @param atsi performance data
4886 * @param atsi_count number of records in 'atsi'
4888 * @return GNUNET_OK to keep the connection open,
4889 * GNUNET_SYSERR to close it (signal serious error)
4892 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4893 const struct GNUNET_MessageHeader *message,
4894 const struct GNUNET_ATS_Information *atsi,
4895 unsigned int atsi_count)
4897 struct GNUNET_MESH_TunnelDestroy *msg;
4898 struct MeshTunnel *t;
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4901 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4902 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4904 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4905 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4908 /* Probably already got the message from another path,
4909 * destroyed the tunnel and retransmitted to children.
4912 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4915 if (t->id.oid == myid)
4917 GNUNET_break_op (0);
4920 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4922 /* Tunnel was incoming, notify clients */
4923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4924 t->local_tid, t->local_tid_dest);
4925 send_clients_tunnel_destroy (t);
4927 tunnel_send_destroy (t);
4928 t->destroy = GNUNET_YES;
4929 // TODO: add timeout to destroy the tunnel anyway
4935 * Core handler for mesh network traffic going from the origin to a peer
4937 * @param cls closure
4938 * @param peer peer identity this notification is about
4939 * @param message message
4940 * @param atsi performance data
4941 * @param atsi_count number of records in 'atsi'
4942 * @return GNUNET_OK to keep the connection open,
4943 * GNUNET_SYSERR to close it (signal serious error)
4946 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4947 const struct GNUNET_MessageHeader *message,
4948 const struct GNUNET_ATS_Information *atsi,
4949 unsigned int atsi_count)
4951 struct GNUNET_MESH_Unicast *msg;
4952 struct GNUNET_PeerIdentity *neighbor;
4953 struct MeshTunnelChildInfo *cinfo;
4954 struct MeshTunnel *t;
4955 GNUNET_PEER_Id dest_id;
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4963 size = ntohs (message->size);
4965 sizeof (struct GNUNET_MESH_Unicast) +
4966 sizeof (struct GNUNET_MessageHeader))
4971 msg = (struct GNUNET_MESH_Unicast *) message;
4972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
4973 ntohs (msg[1].header.type));
4975 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4978 /* TODO notify back: we don't know this tunnel */
4979 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4980 GNUNET_break_op (0);
4983 pid = ntohl (msg->pid);
4984 if (t->fwd_pid == pid)
4986 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4987 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4988 " Already seen pid %u, DROPPING!\n", pid);
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4994 " pid %u not seen yet, forwarding\n", pid);
4996 t->skip += (pid - t->fwd_pid) - 1;
4998 if (is_pid_bigger (pid, t->last_fwd_ack))
5000 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5001 GNUNET_break_op (0);
5004 tunnel_reset_timeout (t);
5005 dest_id = GNUNET_PEER_search (&msg->destination);
5006 if (dest_id == myid)
5008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5009 " it's for us! sending to clients...\n");
5010 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5011 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
5012 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5015 ttl = ntohl (msg->ttl);
5016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5019 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5020 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5021 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5025 " not for us, retransmitting...\n");
5027 neighbor = tree_get_first_hop (t->tree, dest_id);
5028 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5030 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5033 if (is_pid_bigger (pid, cinfo->fwd_ack))
5035 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5036 GNUNET_break_op (0);
5039 send_message (message, neighbor, t);
5040 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5047 * Core handler for mesh network traffic going from the origin to all peers
5049 * @param cls closure
5050 * @param message message
5051 * @param peer peer identity this notification is about
5052 * @param atsi performance data
5053 * @param atsi_count number of records in 'atsi'
5054 * @return GNUNET_OK to keep the connection open,
5055 * GNUNET_SYSERR to close it (signal serious error)
5057 * TODO: Check who we got this from, to validate route.
5060 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5061 const struct GNUNET_MessageHeader *message,
5062 const struct GNUNET_ATS_Information *atsi,
5063 unsigned int atsi_count)
5065 struct GNUNET_MESH_Multicast *msg;
5066 struct MeshTunnel *t;
5070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5072 size = ntohs (message->size);
5073 if (sizeof (struct GNUNET_MESH_Multicast) +
5074 sizeof (struct GNUNET_MessageHeader) > size)
5076 GNUNET_break_op (0);
5079 msg = (struct GNUNET_MESH_Multicast *) message;
5080 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5084 /* TODO notify that we dont know that tunnel */
5085 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5086 GNUNET_break_op (0);
5089 pid = ntohl (msg->pid);
5090 if (t->fwd_pid == pid)
5092 /* already seen this packet, drop */
5093 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5095 " Already seen pid %u, DROPPING!\n", pid);
5096 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5102 " pid %u not seen yet, forwarding\n", pid);
5104 t->skip += (pid - t->fwd_pid) - 1;
5106 tunnel_reset_timeout (t);
5108 /* Transmit to locally interested clients */
5109 if (NULL != t->peers &&
5110 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5112 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5113 send_subscribed_clients (message, &msg[1].header);
5114 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5117 if (ntohl (msg->ttl) == 0)
5119 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5121 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5124 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5125 tunnel_send_multicast (t, message, GNUNET_NO);
5131 * Core handler for mesh network traffic toward the owner of a tunnel
5133 * @param cls closure
5134 * @param message message
5135 * @param peer peer identity this notification is about
5136 * @param atsi performance data
5137 * @param atsi_count number of records in 'atsi'
5139 * @return GNUNET_OK to keep the connection open,
5140 * GNUNET_SYSERR to close it (signal serious error)
5143 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5144 const struct GNUNET_MessageHeader *message,
5145 const struct GNUNET_ATS_Information *atsi,
5146 unsigned int atsi_count)
5148 struct GNUNET_MESH_ToOrigin *msg;
5149 struct GNUNET_PeerIdentity id;
5150 struct MeshPeerInfo *peer_info;
5151 struct MeshTunnel *t;
5154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5156 size = ntohs (message->size);
5157 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5158 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5160 GNUNET_break_op (0);
5163 msg = (struct GNUNET_MESH_ToOrigin *) message;
5164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5165 ntohs (msg[1].header.type));
5166 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5170 /* TODO notify that we dont know this tunnel (whom)? */
5171 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5172 GNUNET_break_op (0);
5176 if (NULL != t->owner)
5179 struct GNUNET_MESH_ToOrigin *copy;
5181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5182 " it's for us! sending to clients...\n");
5183 if (NULL == t->owner)
5185 /* got data packet for ownerless tunnel */
5186 GNUNET_STATISTICS_update (stats, "# data on ownerless tunnel",
5188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " no clients!\n");
5189 GNUNET_break_op (0);
5192 /* TODO signature verification */
5193 memcpy (cbuf, message, size);
5194 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5195 copy->tid = htonl (t->local_tid);
5196 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5197 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5198 ©->header, GNUNET_NO);
5199 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5203 " not for us, retransmitting...\n");
5205 peer_info = peer_info_get (&msg->oid);
5206 if (NULL == peer_info)
5208 /* unknown origin of tunnel */
5212 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5213 send_message (message, &id, t);
5214 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5221 * Core handler for mesh network traffic point-to-point acks.
5223 * @param cls closure
5224 * @param message message
5225 * @param peer peer identity this notification is about
5226 * @param atsi performance data
5227 * @param atsi_count number of records in 'atsi'
5229 * @return GNUNET_OK to keep the connection open,
5230 * GNUNET_SYSERR to close it (signal serious error)
5233 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5234 const struct GNUNET_MessageHeader *message,
5235 const struct GNUNET_ATS_Information *atsi,
5236 unsigned int atsi_count)
5238 struct GNUNET_MESH_ACK *msg;
5239 struct MeshTunnel *t;
5242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5244 msg = (struct GNUNET_MESH_ACK *) message;
5245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5246 ntohs (msg[1].header.type));
5247 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5251 /* TODO notify that we dont know this tunnel (whom)? */
5252 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5253 GNUNET_break_op (0);
5256 ack = ntohl (msg->pid);
5258 /* Is this a forward or backward ACK? */
5259 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5261 struct MeshTunnelChildInfo *cinfo;
5263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5264 cinfo = tunnel_get_neighbor_fc (t, peer);
5265 cinfo->fwd_ack = ack;
5266 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5272 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5274 // FIXME fc Unlock queues?
5280 * Core handler for path ACKs
5282 * @param cls closure
5283 * @param message message
5284 * @param peer peer identity this notification is about
5285 * @param atsi performance data
5286 * @param atsi_count number of records in 'atsi'
5288 * @return GNUNET_OK to keep the connection open,
5289 * GNUNET_SYSERR to close it (signal serious error)
5292 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5293 const struct GNUNET_MessageHeader *message,
5294 const struct GNUNET_ATS_Information *atsi,
5295 unsigned int atsi_count)
5297 struct GNUNET_MESH_PathACK *msg;
5298 struct GNUNET_PeerIdentity id;
5299 struct MeshPeerInfo *peer_info;
5300 struct MeshPeerPath *p;
5301 struct MeshTunnel *t;
5303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5304 GNUNET_i2s (&my_full_id));
5305 msg = (struct GNUNET_MESH_PathACK *) message;
5306 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5309 /* TODO notify that we don't know the tunnel */
5310 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5312 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5316 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5318 peer_info = peer_info_get (&msg->peer_id);
5319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5320 GNUNET_i2s (&msg->peer_id));
5321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5324 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5327 "connect_by_string completed, stopping search\n");
5328 regex_cancel_search (t->regex_ctx);
5329 t->regex_ctx = NULL;
5332 /* Add paths to peers? */
5333 p = tree_get_path_to_peer (t->tree, peer_info->id);
5336 path_add_to_peers (p, GNUNET_YES);
5344 /* Message for us? */
5345 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5348 if (NULL == t->owner)
5350 GNUNET_break_op (0);
5353 if (NULL != t->dht_get_type)
5355 GNUNET_DHT_get_stop (t->dht_get_type);
5356 t->dht_get_type = NULL;
5358 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5360 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5361 send_client_peer_connected (t, peer_info->id);
5366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5367 " not for us, retransmitting...\n");
5368 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5369 peer_info = peer_info_get (&msg->oid);
5370 if (NULL == peer_info)
5372 /* If we know the tunnel, we should DEFINITELY know the peer */
5376 send_message (message, &id, t);
5382 * Functions to handle messages from core
5384 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5385 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5386 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5387 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5388 sizeof (struct GNUNET_MESH_PathBroken)},
5389 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5390 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5391 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5392 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5393 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5394 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5395 sizeof (struct GNUNET_MESH_ACK)},
5396 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5397 sizeof (struct GNUNET_MESH_PathACK)},
5403 /******************************************************************************/
5404 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5405 /******************************************************************************/
5408 * deregister_app: iterator for removing each application registered by a client
5410 * @param cls closure
5411 * @param key the hash of the application id (used to access the hashmap)
5412 * @param value the value stored at the key (client)
5414 * @return GNUNET_OK on success
5417 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5419 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5420 GNUNET_break (GNUNET_YES ==
5421 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5427 * notify_client_connection_failure: notify a client that the connection to the
5428 * requested remote peer is not possible (for instance, no route found)
5429 * Function called when the socket is ready to queue more data. "buf" will be
5430 * NULL and "size" zero if the socket was closed for writing in the meantime.
5432 * @param cls closure
5433 * @param size number of bytes available in buf
5434 * @param buf where the callee should write the message
5435 * @return number of bytes written to buf
5438 notify_client_connection_failure (void *cls, size_t size, void *buf)
5441 struct MeshPeerInfo *peer_info;
5442 struct GNUNET_MESH_PeerControl *msg;
5443 struct GNUNET_PeerIdentity id;
5445 if (0 == size && NULL == buf)
5447 // TODO retry? cancel?
5451 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5452 peer_info = (struct MeshPeerInfo *) cls;
5453 msg = (struct GNUNET_MESH_PeerControl *) buf;
5454 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5455 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5456 // msg->tunnel_id = htonl(peer_info->t->tid);
5457 GNUNET_PEER_resolve (peer_info->id, &id);
5458 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5466 * Send keepalive packets for a peer
5468 * @param cls Closure (tunnel for which to send the keepalive).
5469 * @param tc Notification context.
5471 * TODO: implement explicit multicast keepalive?
5474 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5476 struct MeshTunnel *t = cls;
5477 struct GNUNET_MessageHeader *payload;
5478 struct GNUNET_MESH_Multicast *msg;
5480 sizeof (struct GNUNET_MESH_Multicast) +
5481 sizeof (struct GNUNET_MessageHeader);
5484 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5488 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5491 "sending keepalive for tunnel %d\n", t->id.tid);
5493 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5494 msg->header.size = htons (size);
5495 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5496 msg->oid = my_full_id;
5497 msg->tid = htonl (t->id.tid);
5498 msg->ttl = htonl (default_ttl);
5499 msg->pid = htonl (t->fwd_pid + 1);
5501 payload = (struct GNUNET_MessageHeader *) &msg[1];
5502 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5503 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5504 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5506 t->path_refresh_task =
5507 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5513 * Function to process paths received for a new peer addition. The recorded
5514 * paths form the initial tunnel, which can be optimized later.
5515 * Called on each result obtained for the DHT search.
5517 * @param cls closure
5518 * @param exp when will this value expire
5519 * @param key key of the result
5520 * @param get_path path of the get request
5521 * @param get_path_length lenght of get_path
5522 * @param put_path path of the put request
5523 * @param put_path_length length of the put_path
5524 * @param type type of the result
5525 * @param size number of bytes in data
5526 * @param data pointer to the result data
5528 * TODO: re-issue the request after certain time? cancel after X results?
5531 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5532 const struct GNUNET_HashCode * key,
5533 const struct GNUNET_PeerIdentity *get_path,
5534 unsigned int get_path_length,
5535 const struct GNUNET_PeerIdentity *put_path,
5536 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5537 size_t size, const void *data)
5539 struct MeshPathInfo *path_info = cls;
5540 struct MeshPeerPath *p;
5541 struct GNUNET_PeerIdentity pi;
5544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5545 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5548 p = path_build_from_dht (get_path, get_path_length, put_path,
5550 path_add_to_peers (p, GNUNET_NO);
5552 for (i = 0; i < path_info->peer->ntunnels; i++)
5554 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5555 peer_info_connect (path_info->peer, path_info->t);
5563 * Function to process paths received for a new peer addition. The recorded
5564 * paths form the initial tunnel, which can be optimized later.
5565 * Called on each result obtained for the DHT search.
5567 * @param cls closure
5568 * @param exp when will this value expire
5569 * @param key key of the result
5570 * @param get_path path of the get request
5571 * @param get_path_length lenght of get_path
5572 * @param put_path path of the put request
5573 * @param put_path_length length of the put_path
5574 * @param type type of the result
5575 * @param size number of bytes in data
5576 * @param data pointer to the result data
5579 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5580 const struct GNUNET_HashCode * key,
5581 const struct GNUNET_PeerIdentity *get_path,
5582 unsigned int get_path_length,
5583 const struct GNUNET_PeerIdentity *put_path,
5584 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5585 size_t size, const void *data)
5587 const struct PBlock *pb = data;
5588 const struct GNUNET_PeerIdentity *pi = &pb->id;
5589 struct MeshTunnel *t = cls;
5590 struct MeshPeerInfo *peer_info;
5591 struct MeshPeerPath *p;
5593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5594 if (size != sizeof (struct PBlock))
5596 GNUNET_break_op (0);
5599 if (ntohl(pb->type) != t->type)
5601 GNUNET_break_op (0);
5604 GNUNET_assert (NULL != t->owner);
5605 peer_info = peer_info_get (pi);
5606 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5608 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5610 p = path_build_from_dht (get_path, get_path_length, put_path,
5612 path_add_to_peers (p, GNUNET_NO);
5614 tunnel_add_peer (t, peer_info);
5615 peer_info_connect (peer_info, t);
5620 * Function to process DHT string to regex matching.
5621 * Called on each result obtained for the DHT search.
5623 * @param cls closure (search context)
5624 * @param exp when will this value expire
5625 * @param key key of the result
5626 * @param get_path path of the get request (not used)
5627 * @param get_path_length lenght of get_path (not used)
5628 * @param put_path path of the put request (not used)
5629 * @param put_path_length length of the put_path (not used)
5630 * @param type type of the result
5631 * @param size number of bytes in data
5632 * @param data pointer to the result data
5635 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5636 const struct GNUNET_HashCode * key,
5637 const struct GNUNET_PeerIdentity *get_path,
5638 unsigned int get_path_length,
5639 const struct GNUNET_PeerIdentity *put_path,
5640 unsigned int put_path_length,
5641 enum GNUNET_BLOCK_Type type,
5642 size_t size, const void *data)
5644 const struct MeshRegexAccept *block = data;
5645 struct MeshRegexSearchContext *ctx = cls;
5646 struct MeshRegexSearchInfo *info = ctx->info;
5647 struct MeshPeerPath *p;
5648 struct MeshPeerInfo *peer_info;
5650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5653 peer_info = peer_info_get(&block->id);
5654 p = path_build_from_dht (get_path, get_path_length, put_path,
5656 path_add_to_peers (p, GNUNET_NO);
5659 tunnel_add_peer (info->t, peer_info);
5660 peer_info_connect (peer_info, info->t);
5661 if (0 == info->peer)
5663 info->peer = peer_info->id;
5667 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5670 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5671 ®ex_connect_timeout,
5679 * Function to process DHT string to regex matching.
5680 * Called on each result obtained for the DHT search.
5682 * @param cls closure (search context)
5683 * @param exp when will this value expire
5684 * @param key key of the result
5685 * @param get_path path of the get request (not used)
5686 * @param get_path_length lenght of get_path (not used)
5687 * @param put_path path of the put request (not used)
5688 * @param put_path_length length of the put_path (not used)
5689 * @param type type of the result
5690 * @param size number of bytes in data
5691 * @param data pointer to the result data
5693 * TODO: re-issue the request after certain time? cancel after X results?
5696 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5697 const struct GNUNET_HashCode * key,
5698 const struct GNUNET_PeerIdentity *get_path,
5699 unsigned int get_path_length,
5700 const struct GNUNET_PeerIdentity *put_path,
5701 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5702 size_t size, const void *data)
5704 const struct MeshRegexBlock *block = data;
5705 struct MeshRegexSearchContext *ctx = cls;
5706 struct MeshRegexSearchInfo *info = ctx->info;
5710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5711 "DHT GET STRING RETURNED RESULTS\n");
5712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5713 " key: %s\n", GNUNET_h2s (key));
5715 copy = GNUNET_malloc (size);
5716 memcpy (copy, data, size);
5717 GNUNET_break (GNUNET_OK ==
5718 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5719 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5720 len = ntohl (block->n_proof);
5722 char proof[len + 1];
5724 memcpy (proof, &block[1], len);
5726 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5728 GNUNET_break_op (0);
5732 len = strlen (info->description);
5733 if (len == ctx->position) // String processed
5735 if (GNUNET_YES == ntohl (block->accepting))
5737 regex_find_path(key, ctx);
5741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5742 // FIXME REGEX this block not successful, wait for more? start timeout?
5746 GNUNET_break (GNUNET_OK ==
5747 GNUNET_MESH_regex_block_iterate (block, size,
5748 ®ex_edge_iterator, ctx));
5752 /******************************************************************************/
5753 /********************* MESH LOCAL HANDLES **************************/
5754 /******************************************************************************/
5758 * Handler for client disconnection
5760 * @param cls closure
5761 * @param client identification of the client; NULL
5762 * for the last call when the server is destroyed
5765 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5767 struct MeshClient *c;
5768 struct MeshClient *next;
5771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5780 if (c->handle != client)
5782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5788 GNUNET_SERVER_client_drop (c->handle);
5789 c->shutting_down = GNUNET_YES;
5790 GNUNET_assert (NULL != c->own_tunnels);
5791 GNUNET_assert (NULL != c->incoming_tunnels);
5792 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5793 &tunnel_destroy_iterator, c);
5794 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5795 &tunnel_destroy_iterator, c);
5796 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5797 &tunnel_destroy_iterator, c);
5798 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5799 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5800 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5802 /* deregister clients applications */
5803 if (NULL != c->apps)
5805 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5806 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5808 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5809 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5811 GNUNET_SCHEDULER_cancel (announce_applications_task);
5812 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5814 if (NULL != c->types)
5815 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5816 for (i = 0; i < c->n_regex; i++)
5818 GNUNET_free (c->regexes[i]);
5820 GNUNET_free_non_null (c->regexes);
5821 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5822 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5824 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5827 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5836 * Handler for new clients
5838 * @param cls closure
5839 * @param client identification of the client
5840 * @param message the actual message, which includes messages the client wants
5843 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5844 const struct GNUNET_MessageHeader *message)
5846 struct GNUNET_MESH_ClientConnect *cc_msg;
5847 struct MeshClient *c;
5848 GNUNET_MESH_ApplicationType *a;
5855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5856 /* Check data sanity */
5857 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5858 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5859 ntypes = ntohs (cc_msg->types);
5860 napps = ntohs (cc_msg->applications);
5862 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5869 /* Create new client structure */
5870 c = GNUNET_malloc (sizeof (struct MeshClient));
5871 c->id = next_client_id++;
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5874 GNUNET_SERVER_client_keep (client);
5875 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5878 GNUNET_MESH_ApplicationType at;
5879 struct GNUNET_HashCode hc;
5881 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5882 for (i = 0; i < napps; i++)
5885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5886 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5887 /* store in clients hashmap */
5888 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5889 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5890 /* store in global hashmap, for announcements */
5891 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5892 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5894 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5895 announce_applications_task =
5896 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5902 struct GNUNET_HashCode hc;
5904 t = (uint16_t *) & a[napps];
5905 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5906 for (i = 0; i < ntypes; i++)
5909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5910 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5912 /* store in clients hashmap */
5913 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5914 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5915 /* store in global hashmap */
5916 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5917 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5921 " client has %u+%u subscriptions\n", napps, ntypes);
5923 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5924 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5925 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5926 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5927 GNUNET_SERVER_notification_context_add (nc, client);
5928 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5930 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5936 * Handler for clients announcing available services by a regular expression.
5938 * @param cls closure
5939 * @param client identification of the client
5940 * @param message the actual message, which includes messages the client wants
5943 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5944 const struct GNUNET_MessageHeader *message)
5946 struct MeshClient *c;
5950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5952 /* Sanity check for client registration */
5953 if (NULL == (c = client_get (client)))
5956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5961 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5962 regex = GNUNET_malloc (len + 1);
5963 memcpy (regex, &message[1], len);
5965 GNUNET_array_append (c->regexes, c->n_regex, regex);
5966 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5968 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5974 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5980 * Handler for requests of new tunnels
5982 * @param cls closure
5983 * @param client identification of the client
5984 * @param message the actual message
5987 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5988 const struct GNUNET_MessageHeader *message)
5990 struct GNUNET_MESH_TunnelMessage *t_msg;
5991 struct MeshTunnel *t;
5992 struct MeshClient *c;
5993 MESH_TunnelNumber tid;
5995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5997 /* Sanity check for client registration */
5998 if (NULL == (c = client_get (client)))
6001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6006 /* Message sanity check */
6007 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6010 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6014 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6015 /* Sanity check for tunnel numbering */
6016 tid = ntohl (t_msg->tunnel_id);
6017 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6023 /* Sanity check for duplicate tunnel IDs */
6024 if (NULL != tunnel_get_by_local_id (c, tid))
6027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6031 while (NULL != tunnel_get_by_pi (myid, next_tid))
6032 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6033 t = tunnel_new (myid, next_tid++, c, tid);
6036 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6038 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6041 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6043 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6044 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6047 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6053 * Handler for requests of deleting tunnels
6055 * @param cls closure
6056 * @param client identification of the client
6057 * @param message the actual message
6060 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6061 const struct GNUNET_MessageHeader *message)
6063 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6064 struct MeshClient *c;
6065 struct MeshTunnel *t;
6066 MESH_TunnelNumber tid;
6068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6069 "Got a DESTROY TUNNEL from client!\n");
6071 /* Sanity check for client registration */
6072 if (NULL == (c = client_get (client)))
6075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6078 /* Message sanity check */
6079 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6082 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6086 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6088 /* Retrieve tunnel */
6089 tid = ntohl (tunnel_msg->tunnel_id);
6090 t = tunnel_get_by_local_id(c, tid);
6093 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6095 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6098 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6100 client_ignore_tunnel (c, t);
6102 // TODO: when to destroy incoming tunnel?
6103 if (t->nclients == 0)
6105 GNUNET_assert (GNUNET_YES ==
6106 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6108 GNUNET_assert (GNUNET_YES ==
6109 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6110 &my_full_id.hashPubKey,
6114 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6117 send_client_tunnel_disconnect(t, c);
6118 client_delete_tunnel(c, t);
6120 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6122 tunnel_send_destroy (t);
6123 t->destroy = GNUNET_YES;
6124 // The tunnel will be destroyed when the last message is transmitted.
6125 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6131 * Handler for requests of seeting tunnel's speed.
6133 * @param cls Closure (unused).
6134 * @param client Identification of the client.
6135 * @param message The actual message.
6138 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6139 const struct GNUNET_MessageHeader *message)
6141 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6142 struct MeshClient *c;
6143 struct MeshTunnel *t;
6144 MESH_TunnelNumber tid;
6146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6147 "Got a SPEED request from client!\n");
6149 /* Sanity check for client registration */
6150 if (NULL == (c = client_get (client)))
6153 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6158 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6160 /* Retrieve tunnel */
6161 tid = ntohl (tunnel_msg->tunnel_id);
6162 t = tunnel_get_by_local_id(c, tid);
6165 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6171 switch (ntohs(message->type))
6173 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6174 t->speed_min = GNUNET_YES;
6176 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6177 t->speed_min = GNUNET_NO;
6182 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6187 * Handler for requests of seeting tunnel's buffering policy.
6189 * @param cls Closure (unused).
6190 * @param client Identification of the client.
6191 * @param message The actual message.
6194 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6195 const struct GNUNET_MessageHeader *message)
6197 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6198 struct MeshClient *c;
6199 struct MeshTunnel *t;
6200 MESH_TunnelNumber tid;
6202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6203 "Got a BUFFER request from client!\n");
6205 /* Sanity check for client registration */
6206 if (NULL == (c = client_get (client)))
6209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6214 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6216 /* Retrieve tunnel */
6217 tid = ntohl (tunnel_msg->tunnel_id);
6218 t = tunnel_get_by_local_id(c, tid);
6221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6223 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6227 switch (ntohs(message->type))
6229 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6230 t->nobuffer = GNUNET_NO;
6232 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6233 t->nobuffer = GNUNET_YES;
6239 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6244 * Handler for connection requests to new peers
6246 * @param cls closure
6247 * @param client identification of the client
6248 * @param message the actual message (PeerControl)
6251 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6252 const struct GNUNET_MessageHeader *message)
6254 struct GNUNET_MESH_PeerControl *peer_msg;
6255 struct MeshPeerInfo *peer_info;
6256 struct MeshClient *c;
6257 struct MeshTunnel *t;
6258 MESH_TunnelNumber tid;
6260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6261 /* Sanity check for client registration */
6262 if (NULL == (c = client_get (client)))
6265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6269 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6270 /* Sanity check for message size */
6271 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6274 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6278 /* Tunnel exists? */
6279 tid = ntohl (peer_msg->tunnel_id);
6280 t = tunnel_get_by_local_id (c, tid);
6284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6288 /* Does client own tunnel? */
6289 if (t->owner->handle != client)
6292 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6296 GNUNET_i2s (&peer_msg->peer));
6297 peer_info = peer_info_get (&peer_msg->peer);
6299 tunnel_add_peer (t, peer_info);
6300 peer_info_connect (peer_info, t);
6302 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6308 * Handler for disconnection requests of peers in a tunnel
6310 * @param cls closure
6311 * @param client identification of the client
6312 * @param message the actual message (PeerControl)
6315 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6316 const struct GNUNET_MessageHeader *message)
6318 struct GNUNET_MESH_PeerControl *peer_msg;
6319 struct MeshPeerInfo *peer_info;
6320 struct MeshClient *c;
6321 struct MeshTunnel *t;
6322 MESH_TunnelNumber tid;
6324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6325 /* Sanity check for client registration */
6326 if (NULL == (c = client_get (client)))
6329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6332 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6333 /* Sanity check for message size */
6334 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6341 /* Tunnel exists? */
6342 tid = ntohl (peer_msg->tunnel_id);
6343 t = tunnel_get_by_local_id (c, tid);
6347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6352 /* Does client own tunnel? */
6353 if (t->owner->handle != client)
6356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6361 GNUNET_i2s (&peer_msg->peer));
6362 /* Is the peer in the tunnel? */
6364 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6365 if (NULL == peer_info)
6368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6372 /* Ok, delete peer from tunnel */
6373 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6374 &peer_msg->peer.hashPubKey);
6376 send_destroy_path (t, peer_info->id);
6377 tunnel_delete_peer (t, peer_info->id);
6378 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6383 * Handler for blacklist requests of peers in a tunnel
6385 * @param cls closure
6386 * @param client identification of the client
6387 * @param message the actual message (PeerControl)
6390 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6391 const struct GNUNET_MessageHeader *message)
6393 struct GNUNET_MESH_PeerControl *peer_msg;
6394 struct MeshClient *c;
6395 struct MeshTunnel *t;
6396 MESH_TunnelNumber tid;
6398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6399 /* Sanity check for client registration */
6400 if (NULL == (c = client_get (client)))
6403 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6406 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6408 /* Sanity check for message size */
6409 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6412 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6416 /* Tunnel exists? */
6417 tid = ntohl (peer_msg->tunnel_id);
6418 t = tunnel_get_by_local_id (c, tid);
6422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6427 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6428 GNUNET_PEER_intern(&peer_msg->peer));
6433 * Handler for unblacklist requests of peers in a tunnel
6435 * @param cls closure
6436 * @param client identification of the client
6437 * @param message the actual message (PeerControl)
6440 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6441 const struct GNUNET_MessageHeader *message)
6443 struct GNUNET_MESH_PeerControl *peer_msg;
6444 struct MeshClient *c;
6445 struct MeshTunnel *t;
6446 MESH_TunnelNumber tid;
6450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6451 /* Sanity check for client registration */
6452 if (NULL == (c = client_get (client)))
6455 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6458 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6460 /* Sanity check for message size */
6461 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6464 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6468 /* Tunnel exists? */
6469 tid = ntohl (peer_msg->tunnel_id);
6470 t = tunnel_get_by_local_id (c, tid);
6474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6479 /* if peer is not known, complain */
6480 pid = GNUNET_PEER_search (&peer_msg->peer);
6487 /* search and remove from list */
6488 for (i = 0; i < t->nblacklisted; i++)
6490 if (t->blacklisted[i] == pid)
6492 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6493 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6498 /* if peer hasn't been blacklisted, complain */
6504 * Handler for connection requests to new peers by type
6506 * @param cls closure
6507 * @param client identification of the client
6508 * @param message the actual message (ConnectPeerByType)
6511 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6512 const struct GNUNET_MessageHeader *message)
6514 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6515 struct MeshClient *c;
6516 struct MeshTunnel *t;
6517 struct GNUNET_HashCode hash;
6518 MESH_TunnelNumber tid;
6520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6521 /* Sanity check for client registration */
6522 if (NULL == (c = client_get (client)))
6525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6529 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6530 /* Sanity check for message size */
6531 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6532 ntohs (connect_msg->header.size))
6535 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6539 /* Tunnel exists? */
6540 tid = ntohl (connect_msg->tunnel_id);
6541 t = tunnel_get_by_local_id (c, tid);
6545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6549 /* Does client own tunnel? */
6550 if (t->owner->handle != client)
6553 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6557 /* Do WE have the service? */
6558 t->type = ntohl (connect_msg->type);
6559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6560 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6561 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6564 /* Yes! Fast forward, add ourselves to the tunnel and send the
6565 * good news to the client, and alert the destination client of
6566 * an incoming tunnel.
6568 * FIXME send a path create to self, avoid code duplication
6570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6571 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6572 peer_info_get (&my_full_id),
6573 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6576 send_client_peer_connected (t, myid);
6577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6578 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6580 t->local_tid_dest = next_local_tid++;
6581 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6582 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6583 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6587 /* Ok, lets find a peer offering the service */
6588 if (NULL != t->dht_get_type)
6590 GNUNET_DHT_get_stop (t->dht_get_type);
6592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6593 GNUNET_h2s (&hash));
6595 GNUNET_DHT_get_start (dht_handle,
6596 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6598 dht_replication_level,
6599 GNUNET_DHT_RO_RECORD_ROUTE |
6600 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6602 &dht_get_type_handler, t);
6604 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6610 * Handler for connection requests to new peers by a string service description.
6612 * @param cls closure
6613 * @param client identification of the client
6614 * @param message the actual message, which includes messages the client wants
6617 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6618 const struct GNUNET_MessageHeader *message)
6620 struct GNUNET_MESH_ConnectPeerByString *msg;
6621 struct MeshRegexSearchContext *ctx;
6622 struct MeshRegexSearchInfo *info;
6623 struct GNUNET_DHT_GetHandle *get_h;
6624 struct GNUNET_HashCode key;
6625 struct MeshTunnel *t;
6626 struct MeshClient *c;
6627 MESH_TunnelNumber tid;
6631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6632 "Connect by string started\n");
6633 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6634 size = htons (message->size);
6636 /* Sanity check for client registration */
6637 if (NULL == (c = client_get (client)))
6640 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6644 /* Message size sanity check */
6645 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6652 /* Tunnel exists? */
6653 tid = ntohl (msg->tunnel_id);
6654 t = tunnel_get_by_local_id (c, tid);
6658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6662 /* Does client own tunnel? */
6663 if (t->owner->handle != client)
6666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6671 " on tunnel %s [%u]\n",
6672 GNUNET_i2s(&my_full_id),
6675 /* Only one connect_by_string allowed at the same time! */
6676 /* FIXME: allow more, return handle at api level to cancel, document */
6677 if (NULL != t->regex_ctx)
6680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6684 /* Find string itself */
6685 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6686 string = (const char *) &msg[1];
6688 /* Initialize context */
6689 size = GNUNET_REGEX_get_first_key(string, len, &key);
6690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6691 " consumed %u bits out of %u\n", size, len);
6692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6693 " looking for %s\n", GNUNET_h2s (&key));
6695 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6697 info->description = GNUNET_malloc (len + 1);
6698 memcpy (info->description, string, len);
6699 info->description[len] = '\0';
6700 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6701 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6704 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6705 ctx->position = size;
6709 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6711 /* Start search in DHT */
6712 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6713 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6714 &key, /* key to search */
6715 dht_replication_level, /* replication level */
6716 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6717 NULL, /* xquery */ // FIXME BLOOMFILTER
6718 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6719 &dht_get_string_handler, ctx);
6721 GNUNET_break (GNUNET_OK ==
6722 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6725 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6727 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6733 * Handler for client traffic directed to one peer
6735 * @param cls closure
6736 * @param client identification of the client
6737 * @param message the actual message
6740 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6741 const struct GNUNET_MessageHeader *message)
6743 struct MeshClient *c;
6744 struct MeshTunnel *t;
6745 struct MeshPeerInfo *pi;
6746 struct GNUNET_MESH_Unicast *data_msg;
6747 MESH_TunnelNumber tid;
6750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6751 "Got a unicast request from a client!\n");
6753 /* Sanity check for client registration */
6754 if (NULL == (c = client_get (client)))
6757 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6760 data_msg = (struct GNUNET_MESH_Unicast *) message;
6761 /* Sanity check for message size */
6762 size = ntohs (message->size);
6763 if (sizeof (struct GNUNET_MESH_Unicast) +
6764 sizeof (struct GNUNET_MessageHeader) > size)
6767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6771 /* Tunnel exists? */
6772 tid = ntohl (data_msg->tid);
6773 t = tunnel_get_by_local_id (c, tid);
6777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6781 /* Is it a local tunnel? Then, does client own the tunnel? */
6782 if (t->owner->handle != client)
6785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6789 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6790 &data_msg->destination.hashPubKey);
6791 /* Is the selected peer in the tunnel? */
6795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6799 /* Ok, everything is correct, send the message
6800 * (pretend we got it from a mesh peer)
6803 char buf[ntohs (message->size)] GNUNET_ALIGN;
6804 struct GNUNET_MESH_Unicast *copy;
6806 /* Work around const limitation */
6807 copy = (struct GNUNET_MESH_Unicast *) buf;
6808 memcpy (buf, data_msg, size);
6809 copy->oid = my_full_id;
6810 copy->tid = htonl (t->id.tid);
6811 copy->ttl = htonl (default_ttl);
6812 copy->pid = htonl (t->fwd_pid + 1);
6813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6814 " calling generic handler...\n");
6815 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6818 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6824 * Handler for client traffic directed to the origin
6826 * @param cls closure
6827 * @param client identification of the client
6828 * @param message the actual message
6831 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6832 const struct GNUNET_MessageHeader *message)
6834 struct GNUNET_MESH_ToOrigin *data_msg;
6835 struct GNUNET_PeerIdentity id;
6836 struct MeshClient *c;
6837 struct MeshTunnel *t;
6838 MESH_TunnelNumber tid;
6841 /* Sanity check for client registration */
6842 if (NULL == (c = client_get (client)))
6845 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6848 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6849 /* Sanity check for message size */
6850 size = ntohs (message->size);
6851 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6852 sizeof (struct GNUNET_MessageHeader) > size)
6855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6859 /* Tunnel exists? */
6860 tid = ntohl (data_msg->tid);
6861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6862 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6863 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6869 t = tunnel_get_by_local_id (c, tid);
6873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6877 /* It should be sent by someone who has this as incoming tunnel. */
6878 if (-1 == client_knows_tunnel (c, t))
6881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6884 GNUNET_PEER_resolve (t->id.oid, &id);
6886 /* Ok, everything is correct, send the message
6887 * (pretend we got it from a mesh peer)
6890 char buf[ntohs (message->size)] GNUNET_ALIGN;
6891 struct GNUNET_MESH_ToOrigin *copy;
6893 /* Work around const limitation */
6894 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6895 memcpy (buf, data_msg, size);
6897 copy->tid = htonl (t->id.tid);
6898 copy->sender = my_full_id;
6899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6900 " calling generic handler...\n");
6901 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6903 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6909 * Handler for client traffic directed to all peers in a tunnel
6911 * @param cls closure
6912 * @param client identification of the client
6913 * @param message the actual message
6916 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6917 const struct GNUNET_MessageHeader *message)
6919 struct MeshClient *c;
6920 struct MeshTunnel *t;
6921 struct GNUNET_MESH_Multicast *data_msg;
6922 MESH_TunnelNumber tid;
6924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6925 "Got a multicast request from a client!\n");
6927 /* Sanity check for client registration */
6928 if (NULL == (c = client_get (client)))
6931 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6934 data_msg = (struct GNUNET_MESH_Multicast *) message;
6935 /* Sanity check for message size */
6936 if (sizeof (struct GNUNET_MESH_Multicast) +
6937 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6940 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6944 /* Tunnel exists? */
6945 tid = ntohl (data_msg->tid);
6946 t = tunnel_get_by_local_id (c, tid);
6950 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6954 /* Does client own tunnel? */
6955 if (t->owner->handle != client)
6958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6963 char buf[ntohs (message->size)] GNUNET_ALIGN;
6964 struct GNUNET_MESH_Multicast *copy;
6966 copy = (struct GNUNET_MESH_Multicast *) buf;
6967 memcpy (buf, message, ntohs (message->size));
6968 copy->oid = my_full_id;
6969 copy->tid = htonl (t->id.tid);
6970 copy->ttl = htonl (default_ttl);
6971 copy->pid = htonl (t->fwd_pid + 1);
6972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6973 " calling generic handler...\n");
6974 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
6977 /* receive done gets called when last copy is sent to a neighbor */
6983 * Handler for client's ACKs for payload traffic.
6985 * @param cls Closure (unused).
6986 * @param client Identification of the client.
6987 * @param message The actual message.
6990 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6991 const struct GNUNET_MessageHeader *message)
6993 struct GNUNET_MESH_LocalAck *msg;
6994 struct MeshTunnel *t;
6995 struct MeshClient *c;
6996 MESH_TunnelNumber tid;
6998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6999 /* Sanity check for client registration */
7000 if (NULL == (c = client_get (client)))
7003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7007 msg = (struct GNUNET_MESH_LocalAck *) message;
7008 /* Tunnel exists? */
7009 tid = ntohl (msg->tunnel_id);
7010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7011 t = tunnel_get_by_local_id (c, tid);
7015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7019 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7020 if (NULL != t->owner && t->owner->handle == client)
7022 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7023 t->bck_ack = ntohl(msg->max_pid);
7024 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7028 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7029 tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
7030 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7039 * Functions to handle messages from clients
7041 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7042 {&handle_local_new_client, NULL,
7043 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7044 {&handle_local_announce_regex, NULL,
7045 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7046 {&handle_local_tunnel_create, NULL,
7047 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7048 sizeof (struct GNUNET_MESH_TunnelMessage)},
7049 {&handle_local_tunnel_destroy, NULL,
7050 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7051 sizeof (struct GNUNET_MESH_TunnelMessage)},
7052 {&handle_local_tunnel_speed, NULL,
7053 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7054 sizeof (struct GNUNET_MESH_TunnelMessage)},
7055 {&handle_local_tunnel_speed, NULL,
7056 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7057 sizeof (struct GNUNET_MESH_TunnelMessage)},
7058 {&handle_local_tunnel_buffer, NULL,
7059 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7060 sizeof (struct GNUNET_MESH_TunnelMessage)},
7061 {&handle_local_tunnel_buffer, NULL,
7062 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7063 sizeof (struct GNUNET_MESH_TunnelMessage)},
7064 {&handle_local_connect_add, NULL,
7065 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7066 sizeof (struct GNUNET_MESH_PeerControl)},
7067 {&handle_local_connect_del, NULL,
7068 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7069 sizeof (struct GNUNET_MESH_PeerControl)},
7070 {&handle_local_blacklist, NULL,
7071 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7072 sizeof (struct GNUNET_MESH_PeerControl)},
7073 {&handle_local_unblacklist, NULL,
7074 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7075 sizeof (struct GNUNET_MESH_PeerControl)},
7076 {&handle_local_connect_by_type, NULL,
7077 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7078 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7079 {&handle_local_connect_by_string, NULL,
7080 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7081 {&handle_local_unicast, NULL,
7082 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7083 {&handle_local_to_origin, NULL,
7084 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7085 {&handle_local_multicast, NULL,
7086 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7087 {&handle_local_ack, NULL,
7088 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7089 sizeof (struct GNUNET_MESH_LocalAck)},
7095 * To be called on core init/fail.
7097 * @param cls service closure
7098 * @param server handle to the server for this service
7099 * @param identity the public identity of this peer
7102 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7103 const struct GNUNET_PeerIdentity *identity)
7105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7106 core_handle = server;
7107 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7111 GNUNET_SCHEDULER_shutdown ();
7118 * Method called whenever a given peer connects.
7120 * @param cls closure
7121 * @param peer peer identity this notification is about
7122 * @param atsi performance data for the connection
7123 * @param atsi_count number of records in 'atsi'
7126 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7127 const struct GNUNET_ATS_Information *atsi,
7128 unsigned int atsi_count)
7130 struct MeshPeerInfo *peer_info;
7131 struct MeshPeerPath *path;
7133 DEBUG_CONN ("Peer connected\n");
7134 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7135 peer_info = peer_info_get (peer);
7136 if (myid == peer_info->id)
7138 DEBUG_CONN (" (self)\n");
7143 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7145 path = path_new (2);
7146 path->peers[0] = myid;
7147 path->peers[1] = peer_info->id;
7148 GNUNET_PEER_change_rc (myid, 1);
7149 GNUNET_PEER_change_rc (peer_info->id, 1);
7150 peer_info_add_path (peer_info, path, GNUNET_YES);
7151 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7157 * Method called whenever a peer disconnects.
7159 * @param cls closure
7160 * @param peer peer identity this notification is about
7163 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7165 struct MeshPeerInfo *pi;
7166 struct MeshPeerQueue *q;
7167 struct MeshPeerQueue *n;
7169 DEBUG_CONN ("Peer disconnected\n");
7170 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7182 /* try to reroute this traffic instead */
7183 queue_destroy(q, GNUNET_YES);
7187 peer_info_remove_path (pi, pi->id, myid);
7190 DEBUG_CONN (" (self)\n");
7192 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7197 /******************************************************************************/
7198 /************************ MAIN FUNCTIONS ****************************/
7199 /******************************************************************************/
7202 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7204 * @param cls closure
7205 * @param key current key code
7206 * @param value value in the hash map
7207 * @return GNUNET_YES if we should continue to iterate,
7211 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7213 struct MeshTunnel *t = value;
7220 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7222 * @param cls closure
7223 * @param key current key code
7224 * @param value value in the hash map
7225 * @return GNUNET_YES if we should continue to iterate,
7229 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7231 struct MeshPeerInfo *p = value;
7232 struct MeshPeerQueue *q;
7233 struct MeshPeerQueue *n;
7241 queue_destroy(q, GNUNET_YES);
7245 peer_info_destroy (p);
7250 * Task run during shutdown.
7256 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7260 if (core_handle != NULL)
7262 GNUNET_CORE_disconnect (core_handle);
7265 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7266 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7267 if (dht_handle != NULL)
7269 GNUNET_DHT_disconnect (dht_handle);
7274 GNUNET_SERVER_notification_context_destroy (nc);
7277 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7279 GNUNET_SCHEDULER_cancel (announce_id_task);
7280 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7286 * Process mesh requests.
7288 * @param cls closure
7289 * @param server the initialized server
7290 * @param c configuration to use
7293 run (void *cls, struct GNUNET_SERVER_Handle *server,
7294 const struct GNUNET_CONFIGURATION_Handle *c)
7296 struct MeshPeerInfo *peer;
7297 struct MeshPeerPath *p;
7300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7301 server_handle = server;
7302 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7303 NULL, /* Closure passed to MESH functions */
7304 &core_init, /* Call core_init once connected */
7305 &core_connect, /* Handle connects */
7306 &core_disconnect, /* remove peers on disconnects */
7307 NULL, /* Don't notify about all incoming messages */
7308 GNUNET_NO, /* For header only in notification */
7309 NULL, /* Don't notify about all outbound messages */
7310 GNUNET_NO, /* For header-only out notification */
7311 core_handlers); /* Register these handlers */
7313 if (core_handle == NULL)
7316 GNUNET_SCHEDULER_shutdown ();
7321 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7326 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7328 GNUNET_SCHEDULER_shutdown ();
7333 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7334 &refresh_path_time))
7336 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7338 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7339 "refresh path time");
7340 GNUNET_SCHEDULER_shutdown ();
7345 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7346 &app_announce_time))
7348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7350 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7351 "app announce time");
7352 GNUNET_SCHEDULER_shutdown ();
7357 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7362 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7363 "id announce time");
7364 GNUNET_SCHEDULER_shutdown ();
7369 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7370 &unacknowledged_wait_time))
7372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7374 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7375 "unacknowledged wait time");
7376 GNUNET_SCHEDULER_shutdown ();
7381 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7386 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7388 GNUNET_SCHEDULER_shutdown ();
7393 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7398 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7400 GNUNET_SCHEDULER_shutdown ();
7405 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7410 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7412 GNUNET_SCHEDULER_shutdown ();
7417 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7420 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7422 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7428 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7429 &dht_replication_level))
7431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7433 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7434 "dht replication level", 10);
7435 dht_replication_level = 10;
7439 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7440 GNUNET_free (keyfile);
7441 if (my_private_key == NULL)
7443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7444 _("Mesh service could not access hostkey. Exiting.\n"));
7445 GNUNET_SCHEDULER_shutdown ();
7448 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7449 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7450 &my_full_id.hashPubKey);
7451 myid = GNUNET_PEER_intern (&my_full_id);
7453 // transport_handle = GNUNET_TRANSPORT_connect(c,
7460 dht_handle = GNUNET_DHT_connect (c, 64);
7461 if (dht_handle == NULL)
7466 stats = GNUNET_STATISTICS_create ("mesh", c);
7470 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7472 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7473 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7474 peers = GNUNET_CONTAINER_multihashmap_create (32);
7475 applications = GNUNET_CONTAINER_multihashmap_create (32);
7476 types = GNUNET_CONTAINER_multihashmap_create (32);
7478 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7479 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7480 GNUNET_SERVER_disconnect_notify (server_handle,
7481 &handle_local_client_disconnect, NULL);
7485 clients_tail = NULL;
7488 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7489 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7491 /* Create a peer_info for the local peer */
7492 peer = peer_info_get (&my_full_id);
7495 GNUNET_PEER_change_rc (myid, 1);
7496 peer_info_add_path (peer, p, GNUNET_YES);
7498 /* Scheduled the task to clean up when shutdown is called */
7499 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7506 * The main function for the mesh service.
7508 * @param argc number of arguments from the command line
7509 * @param argv command line arguments
7510 * @return 0 ok, 1 on error
7513 main (int argc, char *const *argv)
7517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7520 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");