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
501 * PID of the last packet sent to the client (FWD).
506 * PID of the last packet received from the client (BCK).
511 * Maximum PID allowed (FWD ACK received).
516 * Last ACK sent to that child (BCK ACK).
524 * Info collected during iteration of child nodes in order to get the ACK value
527 struct MeshTunnelChildIteratorContext
530 * Tunnel whose info is being collected.
532 struct MeshTunnel *t;
535 * Is this context initialized? Is the value in max_child_ack valid?
540 * Maximum child ACK so far.
542 uint32_t max_child_ack;
545 * Number of children nodes
547 unsigned int nchildren;
552 * Info needed to work with tunnel paths and peers
559 struct MeshTunnel *t;
562 * Neighbouring peer to whom we send the packet to
564 struct MeshPeerInfo *peer;
569 struct MeshPeerPath *path;
574 * Struct containing information about a client of the service
581 struct MeshClient *next;
586 struct MeshClient *prev;
589 * Tunnels that belong to this client, indexed by local id
591 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
594 * Tunnels this client has accepted, indexed by incoming local id
596 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
599 * Tunnels this client has rejected, indexed by incoming local id
601 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
603 * Handle to communicate with the client
605 struct GNUNET_SERVER_Client *handle;
608 * Applications that this client has claimed to provide
610 struct GNUNET_CONTAINER_MultiHashMap *apps;
613 * Messages that this client has declared interest in
615 struct GNUNET_CONTAINER_MultiHashMap *types;
618 * Whether the client is active or shutting down (don't send confirmations
619 * to a client that is shutting down.
624 * ID of the client, mainly for debug messages
629 * Regular expressions describing the services offered by this client.
631 char **regexes; // FIXME add timeout? API to remove a regex?
634 * Number of regular expressions in regexes.
636 unsigned int n_regex;
639 * Task to refresh all regular expresions in the DHT.
641 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
647 * Struct to keep information of searches of services described by a regex
648 * using a user-provided string service description.
650 struct MeshRegexSearchInfo
653 * Which tunnel is this for
655 struct MeshTunnel *t;
658 * User provided description of the searched service.
663 * Part of the description already consumed by the search.
670 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
673 * Results from running DHT GETs.
675 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
678 * Contexts, for each running DHT GET. Free all on end of search.
680 struct MeshRegexSearchContext **contexts;
683 * Number of contexts (branches/steps in search).
685 unsigned int n_contexts;
688 * Peer that is connecting via connect_by_string. When connected, free ctx.
693 * Other peers that are found but not yet being connected to.
695 GNUNET_PEER_Id *peers;
698 * Number of elements in peers.
700 unsigned int n_peers;
703 * Next peer to try to connect to.
708 * Timeout for a connect attempt.
709 * When reached, try to connect to a different peer, if any. If not,
710 * try the same peer again.
712 GNUNET_SCHEDULER_TaskIdentifier timeout;
717 * Struct to keep state of running searches that have consumed a part of
720 struct MeshRegexSearchContext
723 * Part of the description already consumed by
724 * this particular search branch.
729 * Information about the search.
731 struct MeshRegexSearchInfo *info;
735 /******************************************************************************/
736 /************************ DEBUG FUNCTIONS ****************************/
737 /******************************************************************************/
741 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
742 * @param cls string to print
743 * @param success GNUNET_OK if the PUT was transmitted,
744 * GNUNET_NO on timeout,
745 * GNUNET_SYSERR on disconnect from service
746 * after the PUT message was transmitted
747 * (so we don't know if it was received or not)
752 mesh_debug (void *cls, int success)
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
762 /******************************************************************************/
763 /*********************** GLOBAL VARIABLES ****************************/
764 /******************************************************************************/
768 * Configuration parameters
770 static struct GNUNET_TIME_Relative refresh_path_time;
771 static struct GNUNET_TIME_Relative app_announce_time;
772 static struct GNUNET_TIME_Relative id_announce_time;
773 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
774 static struct GNUNET_TIME_Relative connect_timeout;
775 static long long unsigned int default_ttl;
776 static long long unsigned int dht_replication_level;
777 static long long unsigned int max_tunnels;
778 static long long unsigned int max_msgs_queue;
781 * DLL with all the clients, head.
783 static struct MeshClient *clients;
786 * DLL with all the clients, tail.
788 static struct MeshClient *clients_tail;
791 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
793 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
796 * Number of tunnels known.
798 static unsigned long long n_tunnels;
801 * Tunnels incoming, indexed by MESH_TunnelNumber
802 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
804 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
807 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
809 static struct GNUNET_CONTAINER_MultiHashMap *peers;
812 * Handle to communicate with transport
814 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
817 * Handle to communicate with core.
819 static struct GNUNET_CORE_Handle *core_handle;
824 static struct GNUNET_DHT_Handle *dht_handle;
829 static struct GNUNET_SERVER_Handle *server_handle;
832 * Handle to the statistics service.
834 static struct GNUNET_STATISTICS_Handle *stats;
837 * Notification context, to send messages to local clients.
839 static struct GNUNET_SERVER_NotificationContext *nc;
842 * Local peer own ID (memory efficient handle).
844 static GNUNET_PEER_Id myid;
847 * Local peer own ID (full value).
849 static struct GNUNET_PeerIdentity my_full_id;
854 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
859 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
862 * Tunnel ID for the next created tunnel (global tunnel number).
864 static MESH_TunnelNumber next_tid;
867 * Tunnel ID for the next incoming tunnel (local tunnel number).
869 static MESH_TunnelNumber next_local_tid;
872 * All application types provided by this peer.
874 static struct GNUNET_CONTAINER_MultiHashMap *applications;
877 * All message types clients of this peer are interested in.
879 static struct GNUNET_CONTAINER_MultiHashMap *types;
882 * Task to periodically announce provided applications.
884 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
887 * Task to periodically announce itself in the network.
889 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
892 * Next ID to assign to a client.
894 unsigned int next_client_id;
897 /******************************************************************************/
898 /*********************** DECLARATIONS **************************/
899 /******************************************************************************/
901 /* FIXME move declarations here */
904 * Function to process paths received for a new peer addition. The recorded
905 * paths form the initial tunnel, which can be optimized later.
906 * Called on each result obtained for the DHT search.
909 * @param exp when will this value expire
910 * @param key key of the result
911 * @param type type of the result
912 * @param size number of bytes in data
913 * @param data pointer to the result data
916 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
917 const struct GNUNET_HashCode * key,
918 const struct GNUNET_PeerIdentity *get_path,
919 unsigned int get_path_length,
920 const struct GNUNET_PeerIdentity *put_path,
921 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
922 size_t size, const void *data);
926 * Function to process DHT string to regex matching.
927 * Called on each result obtained for the DHT search.
929 * @param cls closure (search context)
930 * @param exp when will this value expire
931 * @param key key of the result
932 * @param get_path path of the get request (not used)
933 * @param get_path_length lenght of get_path (not used)
934 * @param put_path path of the put request (not used)
935 * @param put_path_length length of the put_path (not used)
936 * @param type type of the result
937 * @param size number of bytes in data
938 * @param data pointer to the result data
940 * TODO: re-issue the request after certain time? cancel after X results?
943 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
944 const struct GNUNET_HashCode * key,
945 const struct GNUNET_PeerIdentity *get_path,
946 unsigned int get_path_length,
947 const struct GNUNET_PeerIdentity *put_path,
948 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
949 size_t size, const void *data);
953 * Function to process DHT string to regex matching.
954 * Called on each result obtained for the DHT search.
956 * @param cls closure (search context)
957 * @param exp when will this value expire
958 * @param key key of the result
959 * @param get_path path of the get request (not used)
960 * @param get_path_length lenght of get_path (not used)
961 * @param put_path path of the put request (not used)
962 * @param put_path_length length of the put_path (not used)
963 * @param type type of the result
964 * @param size number of bytes in data
965 * @param data pointer to the result data
968 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
969 const struct GNUNET_HashCode * key,
970 const struct GNUNET_PeerIdentity *get_path,
971 unsigned int get_path_length,
972 const struct GNUNET_PeerIdentity *put_path,
973 unsigned int put_path_length,
974 enum GNUNET_BLOCK_Type type,
975 size_t size, const void *data);
979 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
980 * and insert it in the appropiate structures if the peer is not known yet.
982 * @param peer Short identity of the peer.
984 * @return Existing or newly created peer info.
986 static struct MeshPeerInfo *
987 peer_info_get_short (const GNUNET_PEER_Id peer);
991 * Try to establish a new connection to this peer.
992 * Use the best path for the given tunnel.
993 * If the peer doesn't have any path to it yet, try to get one.
994 * If the peer already has some path, send a CREATE PATH towards it.
996 * @param peer PeerInfo of the peer.
997 * @param t Tunnel for which to create the path, if possible.
1000 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1004 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1005 * needed rescources.
1006 * If peer already exists, reevaluate shortest path and change if different.
1008 * @param t Tunnel we want to add a new peer to
1009 * @param peer PeerInfo of the peer being added
1013 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1017 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1018 * that are no longer needed, as well as nodes of no longer reachable peers.
1019 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1021 * @param t Tunnel from which to remove the path.
1022 * @param peer Short id of the peer which should be removed.
1025 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1029 * Search for a tunnel by global ID using full PeerIdentities.
1031 * @param oid owner of the tunnel.
1032 * @param tid global tunnel number.
1034 * @return tunnel handler, NULL if doesn't exist.
1036 static struct MeshTunnel *
1037 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1041 * Delete an active client from the tunnel.
1047 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1050 * Notify a tunnel that a connection has broken that affects at least
1051 * some of its peers.
1053 * @param t Tunnel affected.
1054 * @param p1 Peer that got disconnected from p2.
1055 * @param p2 Peer that got disconnected from p1.
1057 * @return Short ID of the peer disconnected (either p1 or p2).
1058 * 0 if the tunnel remained unaffected.
1060 static GNUNET_PEER_Id
1061 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1066 * Get the current ack value for a tunnel, for data going from root to leaves,
1067 * taking in account the tunnel mode and the status of all children and clients.
1071 * @return Maximum PID allowed.
1074 tunnel_get_fwd_ack (struct MeshTunnel *t);
1077 * Get the current ack value for a tunnel, for data going from leaves to root,
1078 * taking in account the tunnel mode and the status of all children and clients.
1082 * @return Maximum PID allowed.
1085 tunnel_get_bck_ack (struct MeshTunnel *t);
1088 * Add a client to a tunnel, initializing all needed data structures.
1090 * @param t Tunnel to which add the client.
1091 * @param c Client which to add to the tunnel.
1094 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1098 * Iterator over edges in a regex block retrieved from the DHT.
1100 * @param cls Closure.
1101 * @param token Token that follows to next state.
1102 * @param len Lenght of token.
1103 * @param key Hash of next state.
1105 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1108 regex_edge_iterator (void *cls,
1111 const struct GNUNET_HashCode *key);
1115 * Find a path to a peer that offers a regex servcie compatible
1116 * with a given string.
1118 * @param key The key of the accepting state.
1119 * @param ctx Context containing info about the string, tunnel, etc.
1122 regex_find_path (const struct GNUNET_HashCode *key,
1123 struct MeshRegexSearchContext *ctx);
1127 * Queue and pass message to core when possible.
1129 * @param cls Closure (type dependant).
1130 * @param type Type of the message, 0 for a raw message.
1131 * @param size Size of the message.
1132 * @param dst Neighbor to send message to.
1133 * @param t Tunnel this message belongs to.
1136 queue_add (void *cls, uint16_t type, size_t size,
1137 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1140 * Free a transmission that was already queued with all resources
1141 * associated to the request.
1143 * @param queue Queue handler to cancel.
1144 * @param clear_cls Is it necessary to free associated cls?
1147 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1149 /******************************************************************************/
1150 /************************ ITERATORS ****************************/
1151 /******************************************************************************/
1154 * Iterator over found existing mesh regex blocks that match an ongoing search.
1156 * @param cls closure
1157 * @param key current key code
1158 * @param value value in the hash map
1159 * @return GNUNET_YES if we should continue to iterate,
1163 regex_result_iterator (void *cls,
1164 const struct GNUNET_HashCode * key,
1167 struct MeshRegexBlock *block = value;
1168 struct MeshRegexSearchContext *ctx = cls;
1170 if (GNUNET_YES == ntohl(block->accepting) &&
1171 ctx->position == strlen (ctx->info->description))
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1174 regex_find_path (key, ctx);
1175 return GNUNET_YES; // We found an accept state!
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1180 ctx->position, strlen(ctx->info->description),
1181 ntohl(block->accepting));
1184 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1185 ®ex_edge_iterator, ctx);
1192 * Iterator over edges in a regex block retrieved from the DHT.
1194 * @param cls Closure (context of the search).
1195 * @param token Token that follows to next state.
1196 * @param len Lenght of token.
1197 * @param key Hash of next state.
1199 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1202 regex_edge_iterator (void *cls,
1205 const struct GNUNET_HashCode *key)
1207 struct MeshRegexSearchContext *ctx = cls;
1208 struct MeshRegexSearchContext *new_ctx;
1209 struct MeshRegexSearchInfo *info = ctx->info;
1210 struct GNUNET_DHT_GetHandle *get_h;
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1217 current = &info->description[ctx->position];
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1219 current_len = strlen (info->description) - ctx->position;
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1224 if (len > current_len)
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1227 return GNUNET_YES; // Token too long, wont match
1229 if (0 != strncmp (current, token, len))
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1232 return GNUNET_YES; // Token doesn't match
1234 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1235 new_ctx->info = info;
1236 new_ctx->position = ctx->position + len;
1237 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1239 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1242 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1243 ®ex_result_iterator,
1245 return GNUNET_YES; // We are already looking for it
1247 /* Start search in DHT */
1249 GNUNET_DHT_get_start (dht_handle, /* handle */
1250 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1251 key, /* key to search */
1252 dht_replication_level, /* replication level */
1253 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1254 NULL, /* xquery */ // FIXME BLOOMFILTER
1255 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1256 &dht_get_string_handler, new_ctx);
1258 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1259 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1269 * Iterator over hash map entries to cancel DHT GET requests after a
1270 * successful connect_by_string.
1272 * @param cls Closure (unused).
1273 * @param key Current key code (unused).
1274 * @param value Value in the hash map (get handle).
1275 * @return GNUNET_YES if we should continue to iterate,
1279 regex_cancel_dht_get (void *cls,
1280 const struct GNUNET_HashCode * key,
1283 struct GNUNET_DHT_GetHandle *h = value;
1285 GNUNET_DHT_get_stop (h);
1291 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1292 * search for connect_by_string.
1294 * @param cls Closure (unused).
1295 * @param key Current key code (unused).
1296 * @param value MeshRegexBlock in the hash map.
1297 * @return GNUNET_YES if we should continue to iterate,
1301 regex_free_result (void *cls,
1302 const struct GNUNET_HashCode * key,
1306 GNUNET_free (value);
1312 * Regex callback iterator to store own service description in the DHT.
1314 * @param cls closure.
1315 * @param key hash for current state.
1316 * @param proof proof for current state.
1317 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1318 * @param num_edges number of edges leaving current state.
1319 * @param edges edges leaving current state.
1322 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1323 int accepting, unsigned int num_edges,
1324 const struct GNUNET_REGEX_Edge *edges)
1326 struct MeshRegexBlock *block;
1327 struct MeshRegexEdge *block_edge;
1328 enum GNUNET_DHT_RouteOption opt;
1332 unsigned int offset;
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 " regex dht put for state %s\n",
1338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1345 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1346 if (GNUNET_YES == accepting)
1348 struct MeshRegexAccept block;
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 " state %s is accepting, putting own id\n",
1353 size = sizeof (block);
1355 block.id = my_full_id;
1357 GNUNET_DHT_put(dht_handle, key,
1358 dht_replication_level,
1359 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1360 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1363 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1368 len = strlen(proof);
1369 size = sizeof (struct MeshRegexBlock) + len;
1370 block = GNUNET_malloc (size);
1373 block->n_proof = htonl (len);
1374 block->n_edges = htonl (num_edges);
1375 block->accepting = htonl (accepting);
1377 /* Store the proof at the end of the block. */
1378 aux = (char *) &block[1];
1379 memcpy (aux, proof, len);
1382 /* Store each edge in a variable length MeshEdge struct at the
1383 * very end of the MeshRegexBlock structure.
1385 for (i = 0; i < num_edges; i++)
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 " edge %s towards %s\n",
1390 GNUNET_h2s(&edges[i].destination));
1392 /* aux points at the end of the last block */
1393 len = strlen (edges[i].label);
1394 size += sizeof (struct MeshRegexEdge) + len;
1395 // Calculate offset FIXME is this ok? use size instead?
1396 offset = aux - (char *) block;
1397 block = GNUNET_realloc (block, size);
1398 aux = &((char *) block)[offset];
1399 block_edge = (struct MeshRegexEdge *) aux;
1400 block_edge->key = edges[i].destination;
1401 block_edge->n_token = htonl (len);
1402 aux = (char *) &block_edge[1];
1403 memcpy (aux, edges[i].label, len);
1407 GNUNET_DHT_put(dht_handle, key,
1408 dht_replication_level,
1410 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1412 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1416 GNUNET_free (block);
1421 * Store the regular expression describing a local service into the DHT.
1423 * @param regex The regular expresion.
1426 regex_put (const char *regex)
1428 struct GNUNET_REGEX_Automaton *dfa;
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1431 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1432 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1433 GNUNET_REGEX_automaton_destroy (dfa);
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1439 * Find a path to a peer that offers a regex servcie compatible
1440 * with a given string.
1442 * @param key The key of the accepting state.
1443 * @param ctx Context containing info about the string, tunnel, etc.
1446 regex_find_path (const struct GNUNET_HashCode *key,
1447 struct MeshRegexSearchContext *ctx)
1449 struct GNUNET_DHT_GetHandle *get_h;
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1452 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1453 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1454 key, /* key to search */
1455 dht_replication_level, /* replication level */
1456 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1457 GNUNET_DHT_RO_RECORD_ROUTE,
1458 NULL, /* xquery */ // FIXME BLOOMFILTER
1459 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1460 &dht_get_string_accept_handler, ctx);
1461 GNUNET_break (GNUNET_OK ==
1462 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1465 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1470 * Function called if the connect attempt to a peer found via
1471 * connect_by_string times out. Try to connect to another peer, if any.
1472 * Otherwise try to reconnect to the same peer.
1474 * @param cls Closure (info about regex search).
1475 * @param tc TaskContext.
1478 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1480 struct MeshRegexSearchInfo *info = cls;
1481 struct MeshPeerInfo *peer_info;
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1486 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1487 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1495 if (0 < info->n_peers)
1497 // Select next peer, put current in that spot.
1498 id = info->peers[info->i_peer];
1499 info->peers[info->i_peer] = info->peer;
1500 info->i_peer = (info->i_peer + 1) % info->n_peers;
1504 // Try to connect to same peer again.
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1509 peer_info = peer_info_get_short(id);
1510 tunnel_add_peer (info->t, peer_info);
1512 tunnel_delete_peer (info->t, old);
1513 peer_info_connect (peer_info, info->t);
1514 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1515 ®ex_connect_timeout,
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1522 * Cancel an ongoing regex search in the DHT and free all resources.
1524 * @param ctx The search context.
1527 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1529 struct MeshRegexSearchInfo *info = ctx->info;
1532 GNUNET_free (info->description);
1533 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1534 ®ex_cancel_dht_get, NULL);
1535 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1536 ®ex_free_result, NULL);
1537 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1538 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1539 info->t->regex_ctx = NULL;
1540 for (i = 0; i < info->n_contexts; i++)
1542 GNUNET_free (info->contexts[i]);
1544 if (0 < info->n_contexts)
1545 GNUNET_free (info->contexts);
1546 if (0 < info->n_peers)
1547 GNUNET_free (info->peers);
1548 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1550 GNUNET_SCHEDULER_cancel(info->timeout);
1556 /******************************************************************************/
1557 /************************ PERIODIC FUNCTIONS ****************************/
1558 /******************************************************************************/
1561 * Announce iterator over for each application provided by the peer
1563 * @param cls closure
1564 * @param key current key code
1565 * @param value value in the hash map
1566 * @return GNUNET_YES if we should continue to
1571 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1573 struct PBlock block;
1574 struct MeshClient *c;
1576 block.id = my_full_id;
1577 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1578 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1579 if (0 == block.type)
1584 block.type = htonl (block.type);
1586 GNUNET_break (NULL !=
1587 GNUNET_DHT_put (dht_handle, key,
1588 dht_replication_level,
1589 GNUNET_DHT_RO_RECORD_ROUTE |
1590 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1591 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1593 (const char *) &block,
1594 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1596 app_announce_time, NULL, NULL));
1602 * Periodically announce what applications are provided by local clients
1605 * @param cls closure
1606 * @param tc task context
1609 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1611 struct MeshClient *c = cls;
1614 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1616 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1620 DEBUG_DHT ("Starting PUT for regex\n");
1622 for (i = 0; i < c->n_regex; i++)
1624 regex_put (c->regexes[i]);
1626 c->regex_announce_task =
1627 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1628 DEBUG_DHT ("Finished PUT for regex\n");
1635 * Periodically announce what applications are provided by local clients
1638 * @param cls closure
1639 * @param tc task context
1642 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1644 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1646 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1650 DEBUG_DHT ("Starting PUT for apps\n");
1652 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1654 announce_applications_task =
1655 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1657 DEBUG_DHT ("Finished PUT for apps\n");
1664 * Periodically announce self id in the DHT
1666 * @param cls closure
1667 * @param tc task context
1670 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1672 struct PBlock block;
1674 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1676 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1680 * - Set data expiration in function of X
1681 * - Adapt X to churn
1683 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1685 block.id = my_full_id;
1686 block.type = htonl (0);
1687 GNUNET_DHT_put (dht_handle, /* DHT handle */
1688 &my_full_id.hashPubKey, /* Key to use */
1689 dht_replication_level, /* Replication level */
1690 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1691 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1692 sizeof (block), /* Size of the data */
1693 (const char *) &block, /* Data itself */
1694 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1695 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1696 NULL, /* Continuation */
1697 NULL); /* Continuation closure */
1699 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1703 /******************************************************************************/
1704 /****************** GENERAL HELPER FUNCTIONS ************************/
1705 /******************************************************************************/
1708 * Decrements the reference counter and frees all resources if needed
1710 * @param mesh_data Data Descriptor used in a multicast message.
1711 * Freed no longer needed (last message).
1714 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1716 /* Make sure it's a multicast packet */
1717 GNUNET_assert (NULL != mesh_data->reference_counter);
1719 if (0 == --(*(mesh_data->reference_counter)))
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1722 if (NULL != mesh_data->task)
1724 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1726 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1728 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1730 GNUNET_free (mesh_data->task);
1732 GNUNET_free (mesh_data->reference_counter);
1733 GNUNET_free (mesh_data->data);
1734 GNUNET_free (mesh_data);
1740 * Check if client has registered with the service and has not disconnected
1742 * @param client the client to check
1744 * @return non-NULL if client exists in the global DLL
1746 static struct MeshClient *
1747 client_get (struct GNUNET_SERVER_Client *client)
1749 struct MeshClient *c;
1754 if (c->handle == client)
1763 * Checks if a given client has subscribed to certain message type
1765 * @param message_type Type of message to check
1766 * @param c Client to check
1768 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1771 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1773 struct GNUNET_HashCode hc;
1775 if (NULL == c->types)
1777 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1778 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1783 * Allow a client to send more data after transmitting a multicast message
1784 * which some neighbor has not yet accepted altough a reasonable time has
1787 * @param cls Closure (DataDescriptor containing the task identifier)
1788 * @param tc Task Context
1790 * FIXME reference counter cshould be just int
1793 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1795 struct MeshData *mdata = cls;
1797 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1799 GNUNET_assert (NULL != mdata->reference_counter);
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1801 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1802 *(mdata->reference_counter));
1803 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1804 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1809 * Check whether client wants traffic from a tunnel.
1811 * @param c Client to check.
1812 * @param t Tunnel to be found.
1814 * @return GNUNET_YES if client knows tunnel.
1816 * TODO look in client hashmap
1819 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1823 for (i = 0; i < t->nclients; i++)
1824 if (t->clients[i] == c)
1831 * Check whether client has been informed about a tunnel.
1833 * @param c Client to check.
1834 * @param t Tunnel to be found.
1836 * @return GNUNET_YES if client knows tunnel.
1838 * TODO look in client hashmap
1841 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1845 for (i = 0; i < t->nignore; i++)
1846 if (t->ignore[i] == c)
1848 return client_wants_tunnel(c, t);
1853 * Marks a client as uninterested in traffic from the tunnel, updating both
1854 * client and tunnel to reflect this.
1856 * @param c Client that doesn't want traffic anymore.
1857 * @param t Tunnel which should be ignored.
1859 * FIXME when to delete an incoming tunnel?
1862 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1864 struct GNUNET_HashCode hash;
1866 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1867 GNUNET_break (GNUNET_YES ==
1868 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1870 GNUNET_break (GNUNET_YES ==
1871 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1872 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1873 tunnel_delete_active_client (t, c);
1874 GNUNET_array_append (t->ignore, t->nignore, c);
1879 * Deletes a tunnel from a client (either owner or destination). To be used on
1880 * tunnel destroy, otherwise, use client_ignore_tunnel.
1882 * @param c Client whose tunnel to delete.
1883 * @param t Tunnel which should be deleted.
1886 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1888 struct GNUNET_HashCode hash;
1892 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1893 GNUNET_assert (GNUNET_YES ==
1894 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1900 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1902 GNUNET_assert (GNUNET_YES ==
1903 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1907 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1915 * Send the message to all clients that have subscribed to its type
1917 * @param msg Pointer to the message itself
1918 * @param payload Pointer to the payload of the message.
1919 * @return number of clients this message was sent to
1922 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1923 const struct GNUNET_MessageHeader *payload)
1925 struct GNUNET_PeerIdentity *oid;
1926 struct MeshClient *c;
1927 struct MeshTunnel *t;
1928 MESH_TunnelNumber *tid;
1931 char cbuf[htons (msg->size)];
1933 type = ntohs (payload->type);
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1936 GNUNET_MESH_DEBUG_M2S (type));
1938 memcpy (cbuf, msg, sizeof (cbuf));
1939 switch (htons (msg->type))
1941 struct GNUNET_MESH_Unicast *uc;
1942 struct GNUNET_MESH_Multicast *mc;
1943 struct GNUNET_MESH_ToOrigin *to;
1945 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1946 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1950 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1951 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1955 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1956 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1964 t = tunnel_get (oid, ntohl (*tid));
1971 for (count = 0, c = clients; c != NULL; c = c->next)
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1974 if (client_is_subscribed (type, c))
1976 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1980 *tid = htonl (t->local_tid);
1984 if (GNUNET_NO == client_knows_tunnel (c, t))
1986 /* This client doesn't know the tunnel */
1987 struct GNUNET_MESH_TunnelNotification tmsg;
1988 struct GNUNET_HashCode hash;
1990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1991 tmsg.header.size = htons (sizeof (tmsg));
1992 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1993 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1994 tmsg.tunnel_id = htonl (t->local_tid_dest);
1995 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1996 &tmsg.header, GNUNET_NO);
1997 tunnel_add_client (t, c);
1998 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2000 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2001 c->incoming_tunnels, &hash, t,
2002 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2004 *tid = htonl (t->local_tid_dest);
2007 /* Check if the client wants to get traffic from the tunnel */
2008 if (GNUNET_NO == client_wants_tunnel(c, t))
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2012 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2013 (struct GNUNET_MessageHeader
2014 *) cbuf, GNUNET_NO);
2022 * Notify the client that owns the tunnel that a peer has connected to it
2023 * (the requested path to it has been confirmed).
2025 * @param t Tunnel whose owner to notify
2026 * @param id Short id of the peer that has connected
2029 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2031 struct GNUNET_MESH_PeerControl pc;
2033 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2034 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2035 pc.tunnel_id = htonl (t->local_tid);
2036 GNUNET_PEER_resolve (id, &pc.peer);
2037 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2043 * Notify a client of a tunnel about how many more
2044 * payload packages will we accept on a given tunnel,
2045 * distinguishing between root and leaf clients.
2047 * @param c Client whom to send the ACK.
2048 * @param t Tunnel on which to send the ACK.
2051 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
2053 MESH_TunnelNumber tid;
2056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2057 "Sending client ACK on tunnel %X\n",
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (owner, FWD ACK)\n");
2067 ack = tunnel_get_fwd_ack (t);
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (leaf, BCK ACK)\n");
2073 ack = tunnel_get_bck_ack (t);
2074 tid = t->local_tid_dest;
2077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2078 if (t->last_fwd_ack == ack)
2081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2082 t->last_fwd_ack = ack;
2083 send_local_ack (t, c, ack);
2088 * Notify all clients (not depending on registration status) that the incoming
2089 * tunnel is no longer valid.
2091 * @param t Tunnel that was destroyed.
2094 send_clients_tunnel_destroy (struct MeshTunnel *t)
2096 struct GNUNET_MESH_TunnelMessage msg;
2098 msg.header.size = htons (sizeof (msg));
2099 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2100 msg.tunnel_id = htonl (t->local_tid_dest);
2101 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2106 * Notify clients of tunnel disconnections, if needed.
2107 * In case the origin disconnects, the destination clients get a tunnel destroy
2108 * notification. If the last destination disconnects (only one remaining client
2109 * in tunnel), the origin gets a (local ID) peer disconnected.
2110 * Note that the function must be called BEFORE removing the client from
2113 * @param t Tunnel that was destroyed.
2114 * @param c Client that disconnected.
2117 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2123 struct GNUNET_MESH_TunnelMessage msg;
2125 msg.header.size = htons (sizeof (msg));
2126 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2127 msg.tunnel_id = htonl (t->local_tid_dest);
2128 for (i = 0; i < t->nclients; i++)
2129 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2130 &msg.header, GNUNET_NO);
2132 // FIXME when to disconnect an incoming tunnel?
2133 else if (1 == t->nclients && NULL != t->owner)
2135 struct GNUNET_MESH_PeerControl msg;
2137 msg.header.size = htons (sizeof (msg));
2138 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2139 msg.tunnel_id = htonl (t->local_tid);
2140 msg.peer = my_full_id;
2141 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2142 &msg.header, GNUNET_NO);
2148 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2149 * and insert it in the appropiate structures if the peer is not known yet.
2151 * @param peer Full identity of the peer.
2153 * @return Existing or newly created peer info.
2155 static struct MeshPeerInfo *
2156 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2158 struct MeshPeerInfo *peer_info;
2160 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2161 if (NULL == peer_info)
2164 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2165 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2167 peer_info->id = GNUNET_PEER_intern (peer);
2175 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2176 * and insert it in the appropiate structures if the peer is not known yet.
2178 * @param peer Short identity of the peer.
2180 * @return Existing or newly created peer info.
2182 static struct MeshPeerInfo *
2183 peer_info_get_short (const GNUNET_PEER_Id peer)
2185 struct GNUNET_PeerIdentity id;
2187 GNUNET_PEER_resolve (peer, &id);
2188 return peer_info_get (&id);
2193 * Iterator to remove the tunnel from the list of tunnels a peer participates
2196 * @param cls Closure (tunnel info)
2197 * @param key GNUNET_PeerIdentity of the peer (unused)
2198 * @param value PeerInfo of the peer
2200 * @return always GNUNET_YES, to keep iterating
2203 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2205 struct MeshTunnel *t = cls;
2206 struct MeshPeerInfo *peer = value;
2209 for (i = 0; i < peer->ntunnels; i++)
2212 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2215 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2216 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2225 * Core callback to write a pre-constructed data packet to core buffer
2227 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2228 * @param size Number of bytes available in buf.
2229 * @param buf Where the to write the message.
2231 * @return number of bytes written to buf
2234 send_core_data_raw (void *cls, size_t size, void *buf)
2236 struct MeshTransmissionDescriptor *info = cls;
2237 struct GNUNET_MessageHeader *msg;
2240 GNUNET_assert (NULL != info);
2241 GNUNET_assert (NULL != info->mesh_data);
2242 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2243 total_size = ntohs (msg->size);
2245 if (total_size > size)
2250 memcpy (buf, msg, total_size);
2251 data_descriptor_decrement_rc (info->mesh_data);
2258 * Sends an already built non-multicast message to a peer,
2259 * properly registrating all used resources.
2261 * @param message Message to send. Function makes a copy of it.
2262 * @param peer Short ID of the neighbor whom to send the message.
2263 * @param t Tunnel on which this message is transmitted.
2266 send_message (const struct GNUNET_MessageHeader *message,
2267 const struct GNUNET_PeerIdentity *peer,
2268 struct MeshTunnel *t)
2270 struct MeshTransmissionDescriptor *info;
2271 struct MeshPeerInfo *neighbor;
2272 struct MeshPeerPath *p;
2276 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2278 size = ntohs (message->size);
2279 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2280 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2281 info->mesh_data->data = GNUNET_malloc (size);
2282 memcpy (info->mesh_data->data, message, size);
2283 type = ntohs(message->type);
2284 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2286 struct GNUNET_MESH_Unicast *m;
2288 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2289 m->ttl = htonl (ntohl (m->ttl) - 1);
2291 info->mesh_data->data_len = size;
2292 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2293 *info->mesh_data->reference_counter = 1;
2294 neighbor = peer_info_get (peer);
2295 for (p = neighbor->path_head; NULL != p; p = p->next)
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 " %s IS NOT DIRECTLY CONNECTED\n",
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2311 for (p = neighbor->path_head; NULL != p; p = p->next)
2313 struct GNUNET_PeerIdentity debug_id;
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2317 " path with %u hops through:\n",
2319 for (i = 0; i < p->length; i++)
2321 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2324 i, GNUNET_i2s(&debug_id));
2328 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2329 GNUNET_free (info->mesh_data->data);
2330 GNUNET_free (info->mesh_data);
2334 info->peer = neighbor;
2335 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2338 * TODO: in this case we only need the service to retransmit
2339 * the message down the path. If we pass the real type to queue_add,
2340 * queue_send will try to build the message from scratch. This can
2341 * probably be done by some other way instead of deleteing the type
2355 * Sends a CREATE PATH message for a path to a peer, properly registrating
2356 * all used resources.
2358 * @param peer PeerInfo of the final peer for whom this path is being created.
2359 * @param p Path itself.
2360 * @param t Tunnel for which the path is created.
2363 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2364 struct MeshTunnel *t)
2366 struct GNUNET_PeerIdentity id;
2367 struct MeshPathInfo *path_info;
2368 struct MeshPeerInfo *neighbor;
2374 p = tree_get_path_to_peer (t->tree, peer->id);
2381 for (i = 0; i < p->length; i++)
2383 if (p->peers[i] == myid)
2386 if (i >= p->length - 1)
2392 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2394 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2395 path_info->path = p;
2397 neighbor = peer_info_get (&id);
2398 path_info->peer = neighbor;
2399 queue_add (path_info,
2400 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2401 sizeof (struct GNUNET_MESH_ManipulatePath) +
2402 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2409 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2411 * @param t Tunnel whose path to destroy.
2412 * @param destination Short ID of the peer to whom the path to destroy.
2415 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2417 struct MeshPeerPath *p;
2420 p = tree_get_path_to_peer (t->tree, destination);
2426 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2427 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2429 struct GNUNET_MESH_ManipulatePath *msg;
2430 struct GNUNET_PeerIdentity *pi;
2434 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2435 msg->header.size = htons (size);
2436 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2437 msg->tid = htonl (t->id.tid);
2438 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2439 for (i = 0; i < p->length; i++)
2441 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2443 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2450 * Try to establish a new connection to this peer.
2451 * Use the best path for the given tunnel.
2452 * If the peer doesn't have any path to it yet, try to get one.
2453 * If the peer already has some path, send a CREATE PATH towards it.
2455 * @param peer PeerInfo of the peer.
2456 * @param t Tunnel for which to create the path, if possible.
2459 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2461 struct MeshPeerPath *p;
2462 struct MeshPathInfo *path_info;
2464 if (NULL != peer->path_head)
2466 p = tree_get_path_to_peer (t->tree, peer->id);
2473 // FIXME always send create path to self
2476 send_create_path (peer, p, t);
2480 struct GNUNET_HashCode hash;
2483 send_client_peer_connected (t, myid);
2484 t->local_tid_dest = next_local_tid++;
2485 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2488 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2496 else if (NULL == peer->dhtget)
2498 struct GNUNET_PeerIdentity id;
2500 GNUNET_PEER_resolve (peer->id, &id);
2501 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2502 path_info->peer = peer;
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2506 peer->dhtgetcls = path_info;
2507 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2508 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2509 &id.hashPubKey, /* key to search */
2510 dht_replication_level, /* replication level */
2511 GNUNET_DHT_RO_RECORD_ROUTE |
2512 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2513 NULL, /* xquery */ // FIXME BLOOMFILTER
2514 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2515 &dht_get_id_handler, path_info);
2517 /* Otherwise, there is no path but the DHT get is already started. */
2522 * Task to delay the connection of a peer
2524 * @param cls Closure (path info with tunnel and peer to connect).
2525 * Will be free'd on exection.
2526 * @param tc TaskContext
2529 peer_info_connect_task (void *cls,
2530 const struct GNUNET_SCHEDULER_TaskContext *tc)
2532 struct MeshPathInfo *path_info = cls;
2534 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2539 peer_info_connect (path_info->peer, path_info->t);
2545 * Destroy the peer_info and free any allocated resources linked to it
2547 * @param pi The peer_info to destroy.
2549 * @return GNUNET_OK on success
2552 peer_info_destroy (struct MeshPeerInfo *pi)
2554 struct GNUNET_PeerIdentity id;
2555 struct MeshPeerPath *p;
2556 struct MeshPeerPath *nextp;
2558 GNUNET_PEER_resolve (pi->id, &id);
2559 GNUNET_PEER_change_rc (pi->id, -1);
2562 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2566 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2568 if (NULL != pi->dhtget)
2570 GNUNET_DHT_get_stop (pi->dhtget);
2571 GNUNET_free (pi->dhtgetcls);
2577 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2587 * Remove all paths that rely on a direct connection between p1 and p2
2588 * from the peer itself and notify all tunnels about it.
2590 * @param peer PeerInfo of affected peer.
2591 * @param p1 GNUNET_PEER_Id of one peer.
2592 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2595 * TODO: optimize (see below)
2598 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2601 struct MeshPeerPath *p;
2602 struct MeshPeerPath *aux;
2603 struct MeshPeerInfo *peer_d;
2605 unsigned int destroyed;
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2612 p = peer->path_head;
2616 for (i = 0; i < (p->length - 1); i++)
2618 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2619 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2621 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2632 for (i = 0; i < peer->ntunnels; i++)
2634 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2638 * Problem: one or more peers have been deleted from the tunnel tree.
2639 * We don't know who they are to try to add them again.
2640 * We need to try to find a new path for each of the disconnected peers.
2641 * Some of them might already have a path to reach them that does not
2642 * involve p1 and p2. Adding all anew might render in a better tree than
2643 * the trivial immediate fix.
2645 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2646 * its children will be reachable trough him.
2648 peer_d = peer_info_get_short (d);
2651 for (p = peer_d->path_head; NULL != p; p = p->next)
2653 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2661 /* No callback, as peer will be already disconnected and a connection
2662 * scheduled by tunnel_notify_connection_broken.
2664 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2668 peer_info_connect (peer_d, peer->tunnels[i]);
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2676 * Add the path to the peer and update the path used to reach it in case this
2679 * @param peer_info Destination peer to add the path to.
2680 * @param path New path to add. Last peer must be the peer in arg 1.
2681 * Path will be either used of freed if already known.
2682 * @param trusted Do we trust that this path is real?
2685 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2688 struct MeshPeerPath *aux;
2692 if ((NULL == peer_info) || (NULL == path))
2695 path_destroy (path);
2698 if (path->peers[path->length - 1] != peer_info->id)
2701 path_destroy (path);
2704 if (path->length <= 2 && GNUNET_NO == trusted)
2706 /* Only allow CORE to tell us about direct paths */
2707 path_destroy (path);
2710 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2711 for (l = 1; l < path->length; l++)
2713 if (path->peers[l] == myid)
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2716 for (l2 = 0; l2 < path->length - l; l2++)
2718 path->peers[l2] = path->peers[l + l2];
2723 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2728 struct GNUNET_PeerIdentity id;
2730 GNUNET_PEER_resolve (peer_info->id, &id);
2731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2732 path->length, GNUNET_i2s (&id));
2735 l = path_get_length (path);
2742 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2743 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2745 l2 = path_get_length (aux);
2748 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2749 peer_info->path_tail, aux, path);
2754 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2756 path_destroy (path);
2761 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2768 * Add the path to the origin peer and update the path used to reach it in case
2769 * this is the shortest.
2770 * The path is given in peer_info -> destination, therefore we turn the path
2771 * upside down first.
2773 * @param peer_info Peer to add the path to, being the origin of the path.
2774 * @param path New path to add after being inversed.
2775 * @param trusted Do we trust that this path is real?
2778 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2779 struct MeshPeerPath *path, int trusted)
2782 peer_info_add_path (peer_info, path, trusted);
2787 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2788 * to obtain a local peer -> destination path and interning the peer ids.
2790 * @return Newly allocated and created path
2792 static struct MeshPeerPath *
2793 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2794 unsigned int get_path_length,
2795 const struct GNUNET_PeerIdentity *put_path,
2796 unsigned int put_path_length)
2798 struct MeshPeerPath *p;
2804 GNUNET_PEER_change_rc (myid, 1);
2805 i = get_path_length;
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2807 for (i--; i >= 0; i--)
2809 id = GNUNET_PEER_intern (&get_path[i]);
2810 if (p->length > 0 && id == p->peers[p->length - 1])
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2813 GNUNET_PEER_change_rc (id, -1);
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2818 GNUNET_i2s (&get_path[i]));
2820 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2821 p->peers[p->length - 1] = id;
2824 i = put_path_length;
2825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2826 for (i--; i >= 0; i--)
2828 id = GNUNET_PEER_intern (&put_path[i]);
2831 /* PUT path went through us, so discard the path up until now and start
2832 * from here to get a much shorter (and loop-free) path.
2837 if (p->length > 0 && id == p->peers[p->length - 1])
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2840 GNUNET_PEER_change_rc (id, -1);
2844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2845 GNUNET_i2s (&put_path[i]));
2847 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2848 p->peers[p->length - 1] = id;
2852 if (get_path_length > 0)
2853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2854 GNUNET_i2s (&get_path[0]));
2855 if (put_path_length > 0)
2856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2857 GNUNET_i2s (&put_path[0]));
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2860 for (i = 0; i < p->length; i++)
2862 struct GNUNET_PeerIdentity peer_id;
2864 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2866 GNUNET_i2s (&peer_id));
2874 * Adds a path to the peer_infos of all the peers in the path
2876 * @param p Path to process.
2877 * @param confirmed Whether we know if the path works or not.
2880 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2884 /* TODO: invert and add */
2885 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2886 for (i++; i < p->length; i++)
2888 struct MeshPeerInfo *aux;
2889 struct MeshPeerPath *copy;
2891 aux = peer_info_get_short (p->peers[i]);
2892 copy = path_duplicate (p);
2893 copy->length = i + 1;
2894 peer_info_add_path (aux, copy, GNUNET_NO);
2900 * Send keepalive packets for a peer
2902 * @param cls Closure (tunnel for which to send the keepalive).
2903 * @param tc Notification context.
2905 * TODO: implement explicit multicast keepalive?
2908 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2912 * Search for a tunnel among the incoming tunnels
2914 * @param tid the local id of the tunnel
2916 * @return tunnel handler, NULL if doesn't exist
2918 static struct MeshTunnel *
2919 tunnel_get_incoming (MESH_TunnelNumber tid)
2921 struct GNUNET_HashCode hash;
2923 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2924 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2925 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2930 * Search for a tunnel among the tunnels for a client
2932 * @param c the client whose tunnels to search in
2933 * @param tid the local id of the tunnel
2935 * @return tunnel handler, NULL if doesn't exist
2937 static struct MeshTunnel *
2938 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2940 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2942 return tunnel_get_incoming (tid);
2946 struct GNUNET_HashCode hash;
2948 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2949 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2955 * Search for a tunnel by global ID using PEER_ID
2957 * @param pi owner of the tunnel
2958 * @param tid global tunnel number
2960 * @return tunnel handler, NULL if doesn't exist
2962 static struct MeshTunnel *
2963 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2965 struct MESH_TunnelID id;
2966 struct GNUNET_HashCode hash;
2971 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2972 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2977 * Search for a tunnel by global ID using full PeerIdentities
2979 * @param oid owner of the tunnel
2980 * @param tid global tunnel number
2982 * @return tunnel handler, NULL if doesn't exist
2984 static struct MeshTunnel *
2985 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2987 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2992 * Delete an active client from the tunnel.
2998 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3002 for (i = 0; i < t->nclients; i++)
3004 if (t->clients[i] == c)
3006 t->clients[i] = t->clients[t->nclients - 1];
3007 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3008 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3010 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3018 * Delete an ignored client from the tunnel.
3024 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3028 for (i = 0; i < t->nignore; i++)
3030 if (t->ignore[i] == c)
3032 t->ignore[i] = t->ignore[t->nignore - 1];
3033 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3041 * Delete a client from the tunnel. It should be only done on
3042 * client disconnection, otherwise use client_ignore_tunnel.
3048 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3050 tunnel_delete_ignored_client (t, c);
3051 tunnel_delete_active_client (t, c);
3056 * Iterator to free MeshTunnelChildInfo of tunnel children.
3058 * @param cls Closure (tunnel info).
3059 * @param key Hash of GNUNET_PEER_Id (unused).
3060 * @param value MeshTunnelChildInfo of the child.
3062 * @return always GNUNET_YES, to keep iterating
3065 tunnel_destroy_child (void *cls,
3066 const struct GNUNET_HashCode * key,
3069 GNUNET_free (value);
3075 * Callback used to notify a client owner of a tunnel that a peer has
3076 * disconnected, most likely because of a path change.
3078 * @param cls Closure (tunnel this notification is about).
3079 * @param peer_id Short ID of disconnected peer.
3082 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3084 struct MeshTunnel *t = cls;
3085 struct MeshPeerInfo *peer;
3086 struct MeshPathInfo *path_info;
3088 if (NULL != t->owner && NULL != nc)
3090 struct GNUNET_MESH_PeerControl msg;
3092 msg.header.size = htons (sizeof (msg));
3093 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3094 msg.tunnel_id = htonl (t->local_tid);
3095 GNUNET_PEER_resolve (peer_id, &msg.peer);
3096 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3097 &msg.header, GNUNET_NO);
3099 peer = peer_info_get_short (peer_id);
3100 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3101 path_info->peer = peer;
3103 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3108 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3109 * needed rescources.
3110 * If peer already exists, reevaluate shortest path and change if different.
3112 * @param t Tunnel we want to add a new peer to
3113 * @param peer PeerInfo of the peer being added
3117 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3119 struct GNUNET_PeerIdentity id;
3120 struct MeshPeerPath *best_p;
3121 struct MeshPeerPath *p;
3122 unsigned int best_cost;
3125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3126 GNUNET_PEER_resolve (peer->id, &id);
3128 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3131 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3132 GNUNET_assert (GNUNET_OK ==
3133 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3135 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3138 if (NULL != (p = peer->path_head))
3141 best_cost = tree_get_path_cost (t->tree, p);
3144 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3151 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3152 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3153 t->path_refresh_task =
3154 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3158 /* Start a DHT get */
3159 peer_info_connect (peer, t);
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3165 * Add a path to a tunnel which we don't own, just to remember the next hop.
3166 * If destination node was already in the tunnel, the first hop information
3167 * will be replaced with the new path.
3169 * @param t Tunnel we want to add a new peer to
3170 * @param p Path to add
3171 * @param own_pos Position of local node in path.
3175 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3176 unsigned int own_pos)
3178 struct GNUNET_PeerIdentity id;
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3181 GNUNET_assert (0 != own_pos);
3182 tree_add_path (t->tree, p, NULL, NULL);
3183 if (own_pos < p->length - 1)
3185 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3186 tree_update_first_hops (t->tree, myid, &id);
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3192 * Add a client to a tunnel, initializing all needed data structures.
3194 * @param t Tunnel to which add the client.
3195 * @param c Client which to add to the tunnel.
3198 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3200 struct MeshTunnelClientInfo clinfo;
3202 GNUNET_array_append (t->clients, t->nclients, c);
3203 clinfo.fwd_ack = t->fwd_pid + 1;
3204 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3205 clinfo.fwd_pid = t->fwd_pid;
3206 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3208 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3213 * Notifies a tunnel that a connection has broken that affects at least
3214 * some of its peers. Sends a notification towards the root of the tree.
3215 * In case the peer is the owner of the tree, notifies the client that owns
3216 * the tunnel and tries to reconnect.
3218 * @param t Tunnel affected.
3219 * @param p1 Peer that got disconnected from p2.
3220 * @param p2 Peer that got disconnected from p1.
3222 * @return Short ID of the peer disconnected (either p1 or p2).
3223 * 0 if the tunnel remained unaffected.
3225 static GNUNET_PEER_Id
3226 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3232 tree_notify_connection_broken (t->tree, p1, p2,
3233 &tunnel_notify_client_peer_disconnected,
3235 if (myid != p1 && myid != p2)
3241 if (tree_get_predecessor (t->tree) != 0)
3243 /* We are the peer still connected, notify owner of the disconnection. */
3244 struct GNUNET_MESH_PathBroken msg;
3245 struct GNUNET_PeerIdentity neighbor;
3247 msg.header.size = htons (sizeof (msg));
3248 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3249 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3250 msg.tid = htonl (t->id.tid);
3251 msg.peer1 = my_full_id;
3252 GNUNET_PEER_resolve (pid, &msg.peer2);
3253 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3254 send_message (&msg.header, &neighbor, t);
3262 * Send a multicast packet to a neighbor.
3264 * @param cls Closure (Info about the multicast packet)
3265 * @param neighbor_id Short ID of the neighbor to send the packet to.
3268 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3270 struct MeshData *mdata = cls;
3271 struct MeshTransmissionDescriptor *info;
3272 struct GNUNET_PeerIdentity neighbor;
3274 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3276 info->mesh_data = mdata;
3277 (*(mdata->reference_counter)) ++;
3278 info->destination = neighbor_id;
3279 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3281 GNUNET_i2s (&neighbor));
3282 info->peer = peer_info_get (&neighbor);
3283 GNUNET_assert (NULL != info->peer);
3285 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3286 info->mesh_data->data_len,
3293 * Send a message in a tunnel in multicast, sending a copy to each child node
3294 * down the local one in the tunnel tree.
3296 * @param t Tunnel in which to send the data.
3297 * @param msg Message to be sent.
3298 * @param internal Has the service generated this message?
3301 tunnel_send_multicast (struct MeshTunnel *t,
3302 const struct GNUNET_MessageHeader *msg,
3305 struct MeshData *mdata;
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 " sending a multicast packet...\n");
3309 mdata = GNUNET_malloc (sizeof (struct MeshData));
3310 mdata->data_len = ntohs (msg->size);
3311 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3313 mdata->data = GNUNET_malloc (mdata->data_len);
3314 memcpy (mdata->data, msg, mdata->data_len);
3315 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3317 struct GNUNET_MESH_Multicast *mcast;
3319 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3320 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3322 ntohl (mcast->ttl));
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3328 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3329 && GNUNET_NO == internal)
3331 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3333 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3339 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3340 if (*(mdata->reference_counter) == 0)
3342 GNUNET_free (mdata->data);
3343 GNUNET_free (mdata->reference_counter);
3344 if (NULL != mdata->task)
3346 GNUNET_SCHEDULER_cancel(*(mdata->task));
3347 GNUNET_free (mdata->task);
3348 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3350 // FIXME change order?
3351 GNUNET_free (mdata);
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3354 " sending a multicast packet done\n");
3360 * Increase the SKIP value of all peers that
3361 * have not received a unicast message.
3363 * @param cls Closure (ID of the peer that HAS received the message).
3364 * @param key ID of the neighbor.
3365 * @param value Information about the neighbor.
3367 * @return GNUNET_YES to keep iterating.
3370 tunnel_add_skip (void *cls,
3371 const struct GNUNET_HashCode * key,
3374 struct GNUNET_PeerIdentity *neighbor = cls;
3375 struct MeshTunnelChildInfo *cinfo = value;
3377 /* TODO compare only pointers? key == neighbor? */
3378 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3388 * @brief Get neighbor's Flow Control information.
3390 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3391 * descendant of the local node in a certain tunnel.
3392 * If the info is not yet there (recently created path), creates the data struct
3393 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3396 * @param t Tunnel related.
3397 * @param peer Neighbor whose Flow Control info is needed.
3399 * @return Neighbor's Flow Control info.
3401 static struct MeshTunnelChildInfo *
3402 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3403 const struct GNUNET_PeerIdentity *peer)
3405 struct MeshTunnelChildInfo *cinfo;
3406 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3412 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3413 cinfo->id = GNUNET_PEER_intern (peer);
3414 cinfo->skip = t->fwd_pid;
3416 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3417 cinfo->fwd_ack = t->fwd_pid + delta;
3418 cinfo->bck_ack = delta;
3420 GNUNET_assert (GNUNET_OK ==
3421 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3431 * Get the Flow Control info of a client.
3433 * @param t Tunnel on which to look.
3434 * @param c Client whose ACK to get.
3436 * @return ACK value.
3438 static struct MeshTunnelClientInfo *
3439 tunnel_get_client_fc (struct MeshTunnel *t,
3440 struct MeshClient *c)
3444 for (i = 0; i < t->nclients; i++)
3446 if (t->clients[i] != c)
3448 return &t->clients_fc[i];
3451 return NULL; // avoid compiler / coverity complaints
3456 * Iterator to get the appropiate ACK value from all children nodes.
3458 * @param cls Closue (tunnel).
3459 * @param id Id of the child node.
3462 tunnel_get_child_fwd_ack (void *cls,
3465 struct GNUNET_PeerIdentity peer_id;
3466 struct MeshTunnelChildInfo *cinfo;
3467 struct MeshTunnelChildIteratorContext *ctx = cls;
3468 struct MeshTunnel *t = ctx->t;
3471 GNUNET_PEER_resolve (id, &peer_id);
3472 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3473 ack = cinfo->fwd_ack;
3475 if (GNUNET_NO == ctx->init)
3477 ctx->max_child_ack = ack;
3478 ctx->init = GNUNET_YES;
3481 if (GNUNET_YES == t->speed_min)
3483 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3487 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3494 * Get the maximum PID allowed to transmit to any
3495 * tunnel child of the local peer, depending on the tunnel
3496 * buffering/speed settings.
3500 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3503 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3505 struct MeshTunnelChildIteratorContext ctx;
3507 ctx.max_child_ack = 0;
3509 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3511 if (0 == ctx.nchildren)
3514 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3515 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3517 return (int64_t) ctx.max_child_ack;
3522 * Set the FWD ACK value of a client in a particular tunnel.
3524 * @param t Tunnel affected.
3525 * @param c Client whose ACK to set.
3526 * @param ack ACK value.
3529 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3530 struct MeshClient *c,
3535 for (i = 0; i < t->nclients; i++)
3537 if (t->clients[i] != c)
3539 t->clients_fc[i].fwd_ack = ack;
3547 * Get the highest ACK value of all clients in a particular tunnel,
3548 * according to the buffering/speed settings.
3550 * @param t Tunnel on which to look.
3552 * @return Corresponding ACK value (max uint32_t).
3553 * If no clients are suscribed, -1.
3556 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3561 if (0 == t->nclients)
3564 for (ack = -1, i = 0; i < t->nclients; i++)
3567 (GNUNET_YES == t->speed_min &&
3568 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3569 (GNUNET_NO == t->speed_min &&
3570 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3572 ack = t->clients_fc[i].fwd_ack;
3576 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3577 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3579 return (uint32_t) ack;
3584 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3585 * mode and the status of all children nodes.
3589 * @return Maximum PID allowed.
3592 tunnel_get_fwd_ack (struct MeshTunnel *t)
3595 uint32_t buffer_free;
3600 count = t->fwd_pid - t->skip;
3601 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3602 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3603 child_ack = tunnel_get_children_fwd_ack (t);
3604 client_ack = tunnel_get_clients_fwd_ack (t);
3605 if (-1 == child_ack)
3607 // Node has no children, child_ack AND core buffer are irrelevant.
3608 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
3609 return (uint32_t) client_ack;
3612 if (GNUNET_YES == t->speed_min)
3614 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3615 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3619 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3620 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3622 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3623 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3625 count, buffer_free, child_ack, client_ack, ack);
3630 * Get the current bck ack value for a tunnel, taking in account the tunnel
3631 * mode and the status of all children nodes.
3635 * @return Maximum PID allowed.
3638 tunnel_get_bck_ack (struct MeshTunnel *t)
3642 if (GNUNET_YES == t->nobuffer)
3644 if (t->bck_ack > t->bck_pid)
3646 return t->bck_pid + 1;
3653 ack = t->bck_pid + t->bck_queue_max - t->bck_queue_n;
3661 * Build a local ACK message and send it to a local client.
3663 * @param t Tunnel on which to send the ACK.
3664 * @param c Client to whom send the ACK.
3665 * @param ack Value of the ACK.
3668 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3670 struct GNUNET_MESH_LocalAck msg;
3672 msg.header.size = htons (sizeof (msg));
3673 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3674 msg.tunnel_id = htonl (t->local_tid_dest);
3675 msg.max_pid = htonl (ack);
3676 GNUNET_SERVER_notification_context_unicast(nc,
3683 * Build an ACK message and queue it to send to the given peer.
3685 * @param t Tunnel on which to send the ACK.
3686 * @param peer Peer to whom send the ACK.
3687 * @param ack Value of the ACK.
3690 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3692 struct GNUNET_MESH_ACK msg;
3694 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3695 msg.header.size = htons (sizeof (msg));
3696 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3697 msg.pid = htonl (ack);
3698 msg.tid = htonl (t->id.tid);
3700 send_message (&msg.header, peer, t);
3705 * Send an ACK informing the predecessor about the available buffer space.
3706 * In case there is no predecessor, inform the owning client.
3707 * If buffering is off, send only on behalf of children or self if endpoint.
3708 * If buffering is on, send when sent to children and buffer space is free.
3709 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3710 * the ACK itself goes "back" (towards root).
3712 * @param t Tunnel on which to send the ACK.
3713 * @param type Type of message that triggered the ACK transmission.
3716 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3718 struct GNUNET_PeerIdentity id;
3721 if (NULL != t->owner)
3723 send_client_tunnel_ack (t->owner, t);
3726 /* Is it after unicast / multicast retransmission? */
3729 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3730 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3732 "ACK due to FWD DATA retransmission\n");
3733 if (GNUNET_YES == t->nobuffer)
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3738 if (t->fwd_queue_max > t->fwd_queue_n * 2)
3740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3744 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3745 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3751 /* Ok, ACK might be necessary, what PID to ACK? */
3752 ack = tunnel_get_fwd_ack (t);
3754 /* If speed_min and not all children have ack'd, dont send yet */
3755 if (ack == t->last_fwd_ack)
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3761 t->last_fwd_ack = ack;
3762 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3763 send_ack (t, &id, ack);
3768 * Iterator to send a child node a BCK ACK to allow him to send more
3771 * @param cls Closure (tunnel).
3772 * @param id Id of the child node.
3775 tunnel_send_child_bck_ack (void *cls,
3778 struct MeshTunnel *t = cls;
3779 struct MeshTunnelChildInfo *cinfo;
3780 struct GNUNET_PeerIdentity peer;
3782 GNUNET_PEER_resolve (id, &peer);
3783 cinfo = tunnel_get_neighbor_fc (t, &peer);
3785 if (cinfo->bck_ack != cinfo->pid &&
3786 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3790 send_ack (t, &peer, cinfo->bck_ack);
3795 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3797 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3799 * @param t Tunnel on which to send the BCK ACKs.
3802 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3806 /* Find client whom to allow to send to origin (with lowest buffer space) */
3807 for (i = 0; i < t->nclients; i++)
3809 struct MeshTunnelClientInfo *clinfo;
3812 clinfo = &t->clients_fc[i];
3813 delta = clinfo->bck_ack - clinfo->bck_pid;
3815 if ((GNUNET_NO == t->nobuffer && ACK_THRESHOLD > delta) ||
3816 (GNUNET_YES == t->nobuffer && 0 == delta))
3820 ack = clinfo->bck_pid;
3821 ack += t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3823 " sending ack to client %u: %u\n",
3824 t->clients[i]->id, ack);
3825 send_local_ack (t, t->clients[i], ack);
3826 clinfo->bck_ack = ack;
3833 * Send an ACK informing the children nodes and destination clients about
3834 * the available buffer space.
3835 * If buffering is off, send only on behalf of root (can be self).
3836 * If buffering is on, send when sent to predecessor and buffer space is free.
3837 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3838 * the ACK itself goes "forward" (towards children/clients).
3840 * @param t Tunnel on which to send the ACK.
3841 * @param type Type of message that triggered the ACK transmission.
3844 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3847 "Sending BCK ACK on tunnel %u [%u]\n",
3848 t->id.oid, t->id.tid);
3849 /* Is it after data to_origin retransmission? */
3852 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3854 "ACK due to BCK DATA retransmission\n");
3855 if (GNUNET_YES == t->nobuffer)
3857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3860 if (t->bck_queue_max > t->bck_queue_n * 2)
3862 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3866 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3867 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3874 tunnel_send_clients_bck_ack (t);
3875 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
3880 * Send a message to all peers in this tunnel that the tunnel is no longer
3883 * @param t The tunnel whose peers to notify.
3886 tunnel_send_destroy (struct MeshTunnel *t)
3888 struct GNUNET_MESH_TunnelDestroy msg;
3890 msg.header.size = htons (sizeof (msg));
3891 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3892 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3893 msg.tid = htonl (t->id.tid);
3894 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3899 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3901 * @param cls Closure (Tunnel which to cancel).
3902 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3905 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3907 struct MeshTunnel *t = cls;
3908 struct MeshPeerInfo *peer_info;
3909 struct MeshPeerQueue *pq;
3910 struct MeshPeerQueue *next;
3912 peer_info = peer_info_get_short (neighbor_id);
3913 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3916 if (pq->tunnel == t)
3918 queue_destroy (pq, GNUNET_YES);
3921 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3923 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3924 peer_info->core_transmit = NULL;
3929 * Destroy the tunnel and free any allocated resources linked to it.
3931 * @param t the tunnel to destroy
3933 * @return GNUNET_OK on success
3936 tunnel_destroy (struct MeshTunnel *t)
3938 struct MeshClient *c;
3939 struct GNUNET_HashCode hash;
3946 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3952 struct GNUNET_PeerIdentity id;
3954 GNUNET_PEER_resolve (t->id.oid, &id);
3955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3956 GNUNET_i2s (&id), t->id.tid);
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3962 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3963 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3968 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3971 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3975 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3976 for (i = 0; i < t->nclients; i++)
3980 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3985 for (i = 0; i < t->nignore; i++)
3989 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3994 if (t->nclients > 0)
3997 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4001 GNUNET_free (t->clients);
4003 if (NULL != t->peers)
4005 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4007 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4010 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4011 &tunnel_destroy_child,
4013 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4015 tree_destroy (t->tree);
4017 if (NULL != t->regex_ctx)
4018 regex_cancel_search (t->regex_ctx);
4019 if (NULL != t->dht_get_type)
4020 GNUNET_DHT_get_stop (t->dht_get_type);
4021 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4022 GNUNET_SCHEDULER_cancel (t->timeout_task);
4023 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4024 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4027 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4028 GNUNET_assert (0 <= n_tunnels);
4035 * Create a new tunnel
4037 * @param owner Who is the owner of the tunnel (short ID).
4038 * @param tid Tunnel Number of the tunnel.
4039 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4040 * @param local Tunnel Number for the tunnel, for the client point of view.
4042 * @return A new initialized tunnel. NULL on error.
4044 static struct MeshTunnel *
4045 tunnel_new (GNUNET_PEER_Id owner,
4046 MESH_TunnelNumber tid,
4047 struct MeshClient *client,
4048 MESH_TunnelNumber local)
4050 struct MeshTunnel *t;
4051 struct GNUNET_HashCode hash;
4053 if (n_tunnels >= max_tunnels && NULL == client)
4056 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4059 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4060 t->bck_queue_max = t->fwd_queue_max;
4061 t->tree = tree_new (owner);
4063 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4064 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4065 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4066 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4067 t->local_tid = local;
4068 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4070 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4072 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4074 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4075 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4082 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4089 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4091 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4092 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4096 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4106 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4107 * that are no longer needed, as well as nodes of no longer reachable peers.
4108 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4110 * @param t Tunnel from which to remove the path.
4111 * @param peer Short id of the peer which should be removed.
4114 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4116 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4122 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4123 * client when the client disconnects. If the client is not the owner, the
4124 * owner will get notified if no more clients are in the tunnel and the client
4125 * get removed from the tunnel's list.
4127 * @param cls closure (client that is disconnecting)
4128 * @param key the hash of the local tunnel id (used to access the hashmap)
4129 * @param value the value stored at the key (tunnel to destroy)
4131 * @return GNUNET_OK on success
4134 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4136 struct MeshTunnel *t = value;
4137 struct MeshClient *c = cls;
4140 send_client_tunnel_disconnect(t, c);
4143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4144 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4145 tunnel_delete_client(t, c);
4146 client_delete_tunnel(c, t);
4149 tunnel_send_destroy(t);
4150 r = tunnel_destroy (t);
4156 * Timeout function, destroys tunnel if called
4158 * @param cls Closure (tunnel to destroy).
4159 * @param tc TaskContext
4162 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4164 struct MeshTunnel *t = cls;
4166 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4168 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4173 * Resets the tunnel timeout. Starts it if no timeout was running.
4175 * @param t Tunnel whose timeout to reset.
4177 * TODO use heap to improve efficiency of scheduler.
4180 tunnel_reset_timeout (struct MeshTunnel *t)
4182 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4183 GNUNET_SCHEDULER_cancel (t->timeout_task);
4185 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4186 (refresh_path_time, 4), &tunnel_timeout, t);
4190 /******************************************************************************/
4191 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4192 /******************************************************************************/
4195 * Function to send a create path packet to a peer.
4197 * @param cls closure
4198 * @param size number of bytes available in buf
4199 * @param buf where the callee should write the message
4200 * @return number of bytes written to buf
4203 send_core_path_create (void *cls, size_t size, void *buf)
4205 struct MeshPathInfo *info = cls;
4206 struct GNUNET_MESH_ManipulatePath *msg;
4207 struct GNUNET_PeerIdentity *peer_ptr;
4208 struct MeshTunnel *t = info->t;
4209 struct MeshPeerPath *p = info->path;
4214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4216 sizeof (struct GNUNET_MESH_ManipulatePath) +
4217 p->length * sizeof (struct GNUNET_PeerIdentity);
4219 if (size < size_needed || NULL == buf)
4224 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4225 msg->header.size = htons (size_needed);
4226 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4227 msg->tid = ntohl (t->id.tid);
4229 if (GNUNET_YES == t->speed_min)
4230 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4231 if (GNUNET_YES == t->nobuffer)
4232 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4233 msg->opt = htonl(opt);
4235 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4236 for (i = 0; i < p->length; i++)
4238 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4245 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4251 * Fill the core buffer
4253 * @param cls closure (data itself)
4254 * @param size number of bytes available in buf
4255 * @param buf where the callee should write the message
4257 * @return number of bytes written to buf
4260 send_core_data_multicast (void *cls, size_t size, void *buf)
4262 struct MeshTransmissionDescriptor *info = cls;
4265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4266 GNUNET_assert (NULL != info);
4267 GNUNET_assert (NULL != info->peer);
4268 total_size = info->mesh_data->data_len;
4269 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4271 if (total_size > size)
4276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4277 memcpy (buf, info->mesh_data->data, total_size);
4280 struct GNUNET_MESH_Multicast *mc;
4281 struct GNUNET_MessageHeader *mh;
4284 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4286 mc = (struct GNUNET_MESH_Multicast *) mh;
4287 mh = (struct GNUNET_MessageHeader *) &mc[1];
4288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4289 " multicast, payload type %s\n",
4290 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4292 " multicast, payload size %u\n", ntohs (mh->size));
4296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4297 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4301 data_descriptor_decrement_rc (info->mesh_data);
4302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4310 * Creates a path ack message in buf and frees all unused resources.
4312 * @param cls closure (MeshTransmissionDescriptor)
4313 * @param size number of bytes available in buf
4314 * @param buf where the callee should write the message
4315 * @return number of bytes written to buf
4318 send_core_path_ack (void *cls, size_t size, void *buf)
4320 struct MeshTransmissionDescriptor *info = cls;
4321 struct GNUNET_MESH_PathACK *msg = buf;
4323 GNUNET_assert (NULL != info);
4324 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4329 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4330 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4331 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4332 msg->tid = htonl (info->origin->tid);
4333 msg->peer_id = my_full_id;
4336 /* TODO add signature */
4338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4339 return sizeof (struct GNUNET_MESH_PathACK);
4344 * Free a transmission that was already queued with all resources
4345 * associated to the request.
4347 * @param queue Queue handler to cancel.
4348 * @param clear_cls Is it necessary to free associated cls?
4351 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4353 struct MeshTransmissionDescriptor *dd;
4354 struct MeshPathInfo *path_info;
4356 if (GNUNET_YES == clear_cls)
4358 switch (queue->type)
4360 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4361 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4362 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4365 data_descriptor_decrement_rc (dd->mesh_data);
4367 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4369 path_info = queue->cls;
4370 path_destroy (path_info->path);
4374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4376 GNUNET_free_non_null (queue->cls);
4378 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4379 queue->peer->queue_tail,
4381 GNUNET_free (queue);
4386 * Core callback to write a queued packet to core buffer
4388 * @param cls Closure (peer info).
4389 * @param size Number of bytes available in buf.
4390 * @param buf Where the to write the message.
4392 * @return number of bytes written to buf
4395 queue_send (void *cls, size_t size, void *buf)
4397 struct MeshPeerInfo *peer = cls;
4398 struct GNUNET_MessageHeader *msg;
4399 struct MeshPeerQueue *queue;
4400 struct MeshTunnel *t;
4403 peer->core_transmit = NULL;
4404 queue = peer->queue_head;
4406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4408 /* If queue is empty, send should have been cancelled */
4414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4416 /* Check if buffer size is enough for the message */
4417 if (queue->size > size)
4419 struct GNUNET_PeerIdentity id;
4421 GNUNET_PEER_resolve (peer->id, &id);
4422 peer->core_transmit =
4423 GNUNET_CORE_notify_transmit_ready(core_handle,
4426 GNUNET_TIME_UNIT_FOREVER_REL,
4433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4436 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast: %u\n");
4441 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4448 switch (queue->type)
4451 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4452 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4453 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4455 "********* raw: %u\n",
4458 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4459 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4460 data_size = send_core_data_raw (queue->cls, size, buf);
4461 msg = (struct GNUNET_MessageHeader *) buf;
4462 switch (ntohs (msg->type)) // Type of preconstructed message
4464 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4465 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4467 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4468 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4474 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4476 data_size = send_core_data_multicast(queue->cls, size, buf);
4477 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4479 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4481 data_size = send_core_path_create(queue->cls, size, buf);
4483 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4485 data_size = send_core_path_ack(queue->cls, size, buf);
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4490 "********* type unknown: %u\n",
4495 /* Free queue, but cls was freed by send_core_* */
4496 queue_destroy (queue, GNUNET_NO);
4498 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4504 /* If more data in queue, send next */
4505 if (NULL != peer->queue_head)
4507 struct GNUNET_PeerIdentity id;
4509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4510 GNUNET_PEER_resolve (peer->id, &id);
4511 peer->core_transmit =
4512 GNUNET_CORE_notify_transmit_ready(core_handle,
4515 GNUNET_TIME_UNIT_FOREVER_REL,
4517 peer->queue_head->size,
4521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4527 * Queue and pass message to core when possible.
4529 * @param cls Closure (type dependant).
4530 * @param type Type of the message, 0 for a raw message.
4531 * @param size Size of the message.
4532 * @param dst Neighbor to send message to.
4533 * @param t Tunnel this message belongs to.
4536 queue_add (void *cls, uint16_t type, size_t size,
4537 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4539 struct MeshPeerQueue *queue;
4544 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4545 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4547 n = &t->fwd_queue_n;
4548 max = &t->fwd_queue_max;
4550 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4552 n = &t->bck_queue_n;
4553 max = &t->bck_queue_max;
4558 if (NULL == t->owner)
4559 GNUNET_break_op(0); // TODO: kill connection?
4562 return; // Drop message
4566 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4572 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4573 if (NULL == dst->core_transmit)
4575 struct GNUNET_PeerIdentity id;
4577 GNUNET_PEER_resolve (dst->id, &id);
4578 dst->core_transmit =
4579 GNUNET_CORE_notify_transmit_ready(core_handle,
4582 GNUNET_TIME_UNIT_FOREVER_REL,
4591 /******************************************************************************/
4592 /******************** MESH NETWORK HANDLERS **************************/
4593 /******************************************************************************/
4597 * Core handler for path creation
4599 * @param cls closure
4600 * @param message message
4601 * @param peer peer identity this notification is about
4602 * @param atsi performance data
4603 * @param atsi_count number of records in 'atsi'
4605 * @return GNUNET_OK to keep the connection open,
4606 * GNUNET_SYSERR to close it (signal serious error)
4609 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4610 const struct GNUNET_MessageHeader *message,
4611 const struct GNUNET_ATS_Information *atsi,
4612 unsigned int atsi_count)
4614 unsigned int own_pos;
4617 MESH_TunnelNumber tid;
4618 struct GNUNET_MESH_ManipulatePath *msg;
4619 struct GNUNET_PeerIdentity *pi;
4620 struct GNUNET_HashCode hash;
4621 struct MeshPeerPath *path;
4622 struct MeshPeerInfo *dest_peer_info;
4623 struct MeshPeerInfo *orig_peer_info;
4624 struct MeshTunnel *t;
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 "Received a path create msg [%s]\n",
4628 GNUNET_i2s (&my_full_id));
4629 size = ntohs (message->size);
4630 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4632 GNUNET_break_op (0);
4636 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4637 if (size % sizeof (struct GNUNET_PeerIdentity))
4639 GNUNET_break_op (0);
4642 size /= sizeof (struct GNUNET_PeerIdentity);
4645 GNUNET_break_op (0);
4648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4649 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4651 tid = ntohl (msg->tid);
4652 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4654 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4655 t = tunnel_get (pi, tid);
4656 if (NULL == t) // FIXME only for INCOMING tunnels?
4660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4661 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4664 // FIXME notify failure
4667 opt = ntohl (msg->opt);
4668 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4669 GNUNET_YES : GNUNET_NO;
4670 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4671 GNUNET_YES : GNUNET_NO;
4673 if (GNUNET_YES == t->nobuffer)
4675 t->bck_queue_max = 1;
4676 t->fwd_queue_max = 1;
4679 // FIXME only assign a local tid if a local client is interested (on demand)
4680 while (NULL != tunnel_get_incoming (next_local_tid))
4681 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4682 t->local_tid_dest = next_local_tid++;
4683 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4686 tunnel_reset_timeout (t);
4687 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4689 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4690 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4698 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4699 if (NULL == dest_peer_info)
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4702 " Creating PeerInfo for destination.\n");
4703 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4704 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4705 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4707 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4709 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4710 if (NULL == orig_peer_info)
4712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4713 " Creating PeerInfo for origin.\n");
4714 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4715 orig_peer_info->id = GNUNET_PEER_intern (pi);
4716 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4717 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4720 path = path_new (size);
4722 for (i = 0; i < size; i++)
4724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4725 GNUNET_i2s (&pi[i]));
4726 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4727 if (path->peers[i] == myid)
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4733 /* cannot be self, must be 'not found' */
4734 /* create path: self not found in path through self */
4735 GNUNET_break_op (0);
4736 path_destroy (path);
4740 path_add_to_peers (path, GNUNET_NO);
4741 tunnel_add_path (t, path, own_pos);
4742 if (own_pos == size - 1)
4744 /* It is for us! Send ack. */
4745 struct MeshTransmissionDescriptor *info;
4747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4748 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4749 if (NULL == t->peers)
4751 /* New tunnel! Notify clients on data. */
4752 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4754 GNUNET_break (GNUNET_SYSERR !=
4755 GNUNET_CONTAINER_multihashmap_put (t->peers,
4756 &my_full_id.hashPubKey,
4759 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4760 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4761 info->origin = &t->id;
4762 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4763 GNUNET_assert (NULL != info->peer);
4765 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4766 sizeof (struct GNUNET_MESH_PathACK),
4772 struct MeshPeerPath *path2;
4774 /* It's for somebody else! Retransmit. */
4775 path2 = path_duplicate (path);
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4777 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4778 path2 = path_duplicate (path);
4779 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4780 send_create_path (dest_peer_info, path, t);
4787 * Core handler for path destruction
4789 * @param cls closure
4790 * @param message message
4791 * @param peer peer identity this notification is about
4792 * @param atsi performance data
4793 * @param atsi_count number of records in 'atsi'
4795 * @return GNUNET_OK to keep the connection open,
4796 * GNUNET_SYSERR to close it (signal serious error)
4799 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4800 const struct GNUNET_MessageHeader *message,
4801 const struct GNUNET_ATS_Information *atsi,
4802 unsigned int atsi_count)
4804 struct GNUNET_MESH_ManipulatePath *msg;
4805 struct GNUNET_PeerIdentity *pi;
4806 struct MeshPeerPath *path;
4807 struct MeshTunnel *t;
4808 unsigned int own_pos;
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4813 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4814 size = ntohs (message->size);
4815 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4817 GNUNET_break_op (0);
4821 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4822 if (size % sizeof (struct GNUNET_PeerIdentity))
4824 GNUNET_break_op (0);
4827 size /= sizeof (struct GNUNET_PeerIdentity);
4830 GNUNET_break_op (0);
4833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4835 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4836 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4838 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4840 t = tunnel_get (pi, ntohl (msg->tid));
4843 /* TODO notify back: we don't know this tunnel */
4844 GNUNET_break_op (0);
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4848 path = path_new (size);
4850 for (i = 0; i < size; i++)
4852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4853 GNUNET_i2s (&pi[i]));
4854 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4855 if (path->peers[i] == myid)
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4859 if (own_pos < path->length - 1)
4860 send_message (message, &pi[own_pos + 1], t);
4862 send_client_tunnel_disconnect(t, NULL);
4864 tunnel_delete_peer (t, path->peers[path->length - 1]);
4865 path_destroy (path);
4871 * Core handler for notifications of broken paths
4873 * @param cls closure
4874 * @param message message
4875 * @param peer peer identity this notification is about
4876 * @param atsi performance data
4877 * @param atsi_count number of records in 'atsi'
4879 * @return GNUNET_OK to keep the connection open,
4880 * GNUNET_SYSERR to close it (signal serious error)
4883 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4884 const struct GNUNET_MessageHeader *message,
4885 const struct GNUNET_ATS_Information *atsi,
4886 unsigned int atsi_count)
4888 struct GNUNET_MESH_PathBroken *msg;
4889 struct MeshTunnel *t;
4891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4892 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4893 msg = (struct GNUNET_MESH_PathBroken *) message;
4894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4895 GNUNET_i2s (&msg->peer1));
4896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4897 GNUNET_i2s (&msg->peer2));
4898 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4901 GNUNET_break_op (0);
4904 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4905 GNUNET_PEER_search (&msg->peer2));
4912 * Core handler for tunnel destruction
4914 * @param cls closure
4915 * @param message message
4916 * @param peer peer identity this notification is about
4917 * @param atsi performance data
4918 * @param atsi_count number of records in 'atsi'
4920 * @return GNUNET_OK to keep the connection open,
4921 * GNUNET_SYSERR to close it (signal serious error)
4924 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4925 const struct GNUNET_MessageHeader *message,
4926 const struct GNUNET_ATS_Information *atsi,
4927 unsigned int atsi_count)
4929 struct GNUNET_MESH_TunnelDestroy *msg;
4930 struct MeshTunnel *t;
4932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4933 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4934 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4936 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4937 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4940 /* Probably already got the message from another path,
4941 * destroyed the tunnel and retransmitted to children.
4944 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4947 if (t->id.oid == myid)
4949 GNUNET_break_op (0);
4952 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4954 /* Tunnel was incoming, notify clients */
4955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4956 t->local_tid, t->local_tid_dest);
4957 send_clients_tunnel_destroy (t);
4959 tunnel_send_destroy (t);
4960 t->destroy = GNUNET_YES;
4961 // TODO: add timeout to destroy the tunnel anyway
4967 * Core handler for mesh network traffic going from the origin to a peer
4969 * @param cls closure
4970 * @param peer peer identity this notification is about
4971 * @param message message
4972 * @param atsi performance data
4973 * @param atsi_count number of records in 'atsi'
4974 * @return GNUNET_OK to keep the connection open,
4975 * GNUNET_SYSERR to close it (signal serious error)
4978 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4979 const struct GNUNET_MessageHeader *message,
4980 const struct GNUNET_ATS_Information *atsi,
4981 unsigned int atsi_count)
4983 struct GNUNET_MESH_Unicast *msg;
4984 struct GNUNET_PeerIdentity *neighbor;
4985 struct MeshTunnelChildInfo *cinfo;
4986 struct MeshTunnel *t;
4987 GNUNET_PEER_Id dest_id;
4992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4995 size = ntohs (message->size);
4997 sizeof (struct GNUNET_MESH_Unicast) +
4998 sizeof (struct GNUNET_MessageHeader))
5003 msg = (struct GNUNET_MESH_Unicast *) message;
5004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5005 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5007 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5010 /* TODO notify back: we don't know this tunnel */
5011 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5012 GNUNET_break_op (0);
5015 pid = ntohl (msg->pid);
5016 if (t->fwd_pid == pid)
5018 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5019 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5020 " Already seen pid %u, DROPPING!\n", pid);
5025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5026 " pid %u not seen yet, forwarding\n", pid);
5028 t->skip += (pid - t->fwd_pid) - 1;
5030 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5032 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5033 GNUNET_break_op (0);
5036 tunnel_reset_timeout (t);
5037 dest_id = GNUNET_PEER_search (&msg->destination);
5038 if (dest_id == myid)
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5041 " it's for us! sending to clients...\n");
5042 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5043 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
5044 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5047 ttl = ntohl (msg->ttl);
5048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5051 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5052 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5053 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5057 " not for us, retransmitting...\n");
5059 neighbor = tree_get_first_hop (t->tree, dest_id);
5060 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5062 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5065 if (GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5067 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5068 GNUNET_break_op (0);
5071 send_message (message, neighbor, t);
5072 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5079 * Core handler for mesh network traffic going from the origin to all peers
5081 * @param cls closure
5082 * @param message message
5083 * @param peer peer identity this notification is about
5084 * @param atsi performance data
5085 * @param atsi_count number of records in 'atsi'
5086 * @return GNUNET_OK to keep the connection open,
5087 * GNUNET_SYSERR to close it (signal serious error)
5089 * TODO: Check who we got this from, to validate route.
5092 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5093 const struct GNUNET_MessageHeader *message,
5094 const struct GNUNET_ATS_Information *atsi,
5095 unsigned int atsi_count)
5097 struct GNUNET_MESH_Multicast *msg;
5098 struct MeshTunnel *t;
5102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5104 size = ntohs (message->size);
5105 if (sizeof (struct GNUNET_MESH_Multicast) +
5106 sizeof (struct GNUNET_MessageHeader) > size)
5108 GNUNET_break_op (0);
5111 msg = (struct GNUNET_MESH_Multicast *) message;
5112 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5116 /* TODO notify that we dont know that tunnel */
5117 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5118 GNUNET_break_op (0);
5121 pid = ntohl (msg->pid);
5122 if (t->fwd_pid == pid)
5124 /* already seen this packet, drop */
5125 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5127 " Already seen pid %u, DROPPING!\n", pid);
5128 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5134 " pid %u not seen yet, forwarding\n", pid);
5136 t->skip += (pid - t->fwd_pid) - 1;
5138 tunnel_reset_timeout (t);
5140 /* Transmit to locally interested clients */
5141 if (NULL != t->peers &&
5142 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5144 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5145 send_subscribed_clients (message, &msg[1].header);
5146 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5149 if (ntohl (msg->ttl) == 0)
5151 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5152 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5153 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5156 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5157 tunnel_send_multicast (t, message, GNUNET_NO);
5163 * Core handler for mesh network traffic toward the owner of a tunnel
5165 * @param cls closure
5166 * @param message message
5167 * @param peer peer identity this notification is about
5168 * @param atsi performance data
5169 * @param atsi_count number of records in 'atsi'
5171 * @return GNUNET_OK to keep the connection open,
5172 * GNUNET_SYSERR to close it (signal serious error)
5175 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5176 const struct GNUNET_MessageHeader *message,
5177 const struct GNUNET_ATS_Information *atsi,
5178 unsigned int atsi_count)
5180 struct GNUNET_MESH_ToOrigin *msg;
5181 struct GNUNET_PeerIdentity id;
5182 struct MeshPeerInfo *peer_info;
5183 struct MeshTunnel *t;
5186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5188 size = ntohs (message->size);
5189 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5190 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5192 GNUNET_break_op (0);
5195 msg = (struct GNUNET_MESH_ToOrigin *) message;
5196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5197 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5198 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5202 /* TODO notify that we dont know this tunnel (whom)? */
5203 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5204 GNUNET_break_op (0);
5208 if (NULL != t->owner)
5211 struct GNUNET_MESH_ToOrigin *copy;
5213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5214 " it's for us! sending to clients...\n");
5215 /* TODO signature verification */
5216 memcpy (cbuf, message, size);
5217 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5218 copy->tid = htonl (t->local_tid);
5219 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5220 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5221 ©->header, GNUNET_NO);
5222 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5226 " not for us, retransmitting...\n");
5228 peer_info = peer_info_get (&msg->oid);
5229 if (NULL == peer_info)
5231 /* unknown origin of tunnel */
5235 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5236 send_message (message, &id, t);
5237 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5244 * Core handler for mesh network traffic point-to-point acks.
5246 * @param cls closure
5247 * @param message message
5248 * @param peer peer identity this notification is about
5249 * @param atsi performance data
5250 * @param atsi_count number of records in 'atsi'
5252 * @return GNUNET_OK to keep the connection open,
5253 * GNUNET_SYSERR to close it (signal serious error)
5256 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5257 const struct GNUNET_MessageHeader *message,
5258 const struct GNUNET_ATS_Information *atsi,
5259 unsigned int atsi_count)
5261 struct GNUNET_MESH_ACK *msg;
5262 struct MeshTunnel *t;
5265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5267 msg = (struct GNUNET_MESH_ACK *) message;
5268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5269 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5270 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5274 /* TODO notify that we dont know this tunnel (whom)? */
5275 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5276 GNUNET_break_op (0);
5279 ack = ntohl (msg->pid);
5281 /* Is this a forward or backward ACK? */
5282 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5284 struct MeshTunnelChildInfo *cinfo;
5286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5287 cinfo = tunnel_get_neighbor_fc (t, peer);
5288 cinfo->fwd_ack = ack;
5289 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5295 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5297 // FIXME fc Unlock queues?
5303 * Core handler for path ACKs
5305 * @param cls closure
5306 * @param message message
5307 * @param peer peer identity this notification is about
5308 * @param atsi performance data
5309 * @param atsi_count number of records in 'atsi'
5311 * @return GNUNET_OK to keep the connection open,
5312 * GNUNET_SYSERR to close it (signal serious error)
5315 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5316 const struct GNUNET_MessageHeader *message,
5317 const struct GNUNET_ATS_Information *atsi,
5318 unsigned int atsi_count)
5320 struct GNUNET_MESH_PathACK *msg;
5321 struct GNUNET_PeerIdentity id;
5322 struct MeshPeerInfo *peer_info;
5323 struct MeshPeerPath *p;
5324 struct MeshTunnel *t;
5326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5327 GNUNET_i2s (&my_full_id));
5328 msg = (struct GNUNET_MESH_PathACK *) message;
5329 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5332 /* TODO notify that we don't know the tunnel */
5333 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5335 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5339 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5341 peer_info = peer_info_get (&msg->peer_id);
5342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5343 GNUNET_i2s (&msg->peer_id));
5344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5347 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5350 "connect_by_string completed, stopping search\n");
5351 regex_cancel_search (t->regex_ctx);
5352 t->regex_ctx = NULL;
5355 /* Add paths to peers? */
5356 p = tree_get_path_to_peer (t->tree, peer_info->id);
5359 path_add_to_peers (p, GNUNET_YES);
5367 /* Message for us? */
5368 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5371 if (NULL == t->owner)
5373 GNUNET_break_op (0);
5376 if (NULL != t->dht_get_type)
5378 GNUNET_DHT_get_stop (t->dht_get_type);
5379 t->dht_get_type = NULL;
5381 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5383 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5384 send_client_peer_connected (t, peer_info->id);
5389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5390 " not for us, retransmitting...\n");
5391 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5392 peer_info = peer_info_get (&msg->oid);
5393 if (NULL == peer_info)
5395 /* If we know the tunnel, we should DEFINITELY know the peer */
5399 send_message (message, &id, t);
5405 * Functions to handle messages from core
5407 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5408 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5409 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5410 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5411 sizeof (struct GNUNET_MESH_PathBroken)},
5412 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5413 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5414 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5415 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5416 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5417 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5418 sizeof (struct GNUNET_MESH_ACK)},
5419 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5420 sizeof (struct GNUNET_MESH_PathACK)},
5426 /******************************************************************************/
5427 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5428 /******************************************************************************/
5431 * deregister_app: iterator for removing each application registered by a client
5433 * @param cls closure
5434 * @param key the hash of the application id (used to access the hashmap)
5435 * @param value the value stored at the key (client)
5437 * @return GNUNET_OK on success
5440 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5442 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5443 GNUNET_break (GNUNET_YES ==
5444 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5450 * notify_client_connection_failure: notify a client that the connection to the
5451 * requested remote peer is not possible (for instance, no route found)
5452 * Function called when the socket is ready to queue more data. "buf" will be
5453 * NULL and "size" zero if the socket was closed for writing in the meantime.
5455 * @param cls closure
5456 * @param size number of bytes available in buf
5457 * @param buf where the callee should write the message
5458 * @return number of bytes written to buf
5461 notify_client_connection_failure (void *cls, size_t size, void *buf)
5464 struct MeshPeerInfo *peer_info;
5465 struct GNUNET_MESH_PeerControl *msg;
5466 struct GNUNET_PeerIdentity id;
5468 if (0 == size && NULL == buf)
5470 // TODO retry? cancel?
5474 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5475 peer_info = (struct MeshPeerInfo *) cls;
5476 msg = (struct GNUNET_MESH_PeerControl *) buf;
5477 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5478 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5479 // msg->tunnel_id = htonl(peer_info->t->tid);
5480 GNUNET_PEER_resolve (peer_info->id, &id);
5481 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5489 * Send keepalive packets for a peer
5491 * @param cls Closure (tunnel for which to send the keepalive).
5492 * @param tc Notification context.
5494 * TODO: implement explicit multicast keepalive?
5497 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5499 struct MeshTunnel *t = cls;
5500 struct GNUNET_MessageHeader *payload;
5501 struct GNUNET_MESH_Multicast *msg;
5503 sizeof (struct GNUNET_MESH_Multicast) +
5504 sizeof (struct GNUNET_MessageHeader);
5507 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5511 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5514 "sending keepalive for tunnel %d\n", t->id.tid);
5516 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5517 msg->header.size = htons (size);
5518 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5519 msg->oid = my_full_id;
5520 msg->tid = htonl (t->id.tid);
5521 msg->ttl = htonl (default_ttl);
5522 msg->pid = htonl (t->fwd_pid + 1);
5524 payload = (struct GNUNET_MessageHeader *) &msg[1];
5525 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5526 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5527 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5529 t->path_refresh_task =
5530 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5536 * Function to process paths received for a new peer addition. The recorded
5537 * paths form the initial tunnel, which can be optimized later.
5538 * Called on each result obtained for the DHT search.
5540 * @param cls closure
5541 * @param exp when will this value expire
5542 * @param key key of the result
5543 * @param get_path path of the get request
5544 * @param get_path_length lenght of get_path
5545 * @param put_path path of the put request
5546 * @param put_path_length length of the put_path
5547 * @param type type of the result
5548 * @param size number of bytes in data
5549 * @param data pointer to the result data
5551 * TODO: re-issue the request after certain time? cancel after X results?
5554 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5555 const struct GNUNET_HashCode * key,
5556 const struct GNUNET_PeerIdentity *get_path,
5557 unsigned int get_path_length,
5558 const struct GNUNET_PeerIdentity *put_path,
5559 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5560 size_t size, const void *data)
5562 struct MeshPathInfo *path_info = cls;
5563 struct MeshPeerPath *p;
5564 struct GNUNET_PeerIdentity pi;
5567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5568 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5571 p = path_build_from_dht (get_path, get_path_length, put_path,
5573 path_add_to_peers (p, GNUNET_NO);
5575 for (i = 0; i < path_info->peer->ntunnels; i++)
5577 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5578 peer_info_connect (path_info->peer, path_info->t);
5586 * Function to process paths received for a new peer addition. The recorded
5587 * paths form the initial tunnel, which can be optimized later.
5588 * Called on each result obtained for the DHT search.
5590 * @param cls closure
5591 * @param exp when will this value expire
5592 * @param key key of the result
5593 * @param get_path path of the get request
5594 * @param get_path_length lenght of get_path
5595 * @param put_path path of the put request
5596 * @param put_path_length length of the put_path
5597 * @param type type of the result
5598 * @param size number of bytes in data
5599 * @param data pointer to the result data
5602 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5603 const struct GNUNET_HashCode * key,
5604 const struct GNUNET_PeerIdentity *get_path,
5605 unsigned int get_path_length,
5606 const struct GNUNET_PeerIdentity *put_path,
5607 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5608 size_t size, const void *data)
5610 const struct PBlock *pb = data;
5611 const struct GNUNET_PeerIdentity *pi = &pb->id;
5612 struct MeshTunnel *t = cls;
5613 struct MeshPeerInfo *peer_info;
5614 struct MeshPeerPath *p;
5616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5617 if (size != sizeof (struct PBlock))
5619 GNUNET_break_op (0);
5622 if (ntohl(pb->type) != t->type)
5624 GNUNET_break_op (0);
5627 GNUNET_assert (NULL != t->owner);
5628 peer_info = peer_info_get (pi);
5629 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5631 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5633 p = path_build_from_dht (get_path, get_path_length, put_path,
5635 path_add_to_peers (p, GNUNET_NO);
5637 tunnel_add_peer (t, peer_info);
5638 peer_info_connect (peer_info, t);
5643 * Function to process DHT string to regex matching.
5644 * Called on each result obtained for the DHT search.
5646 * @param cls closure (search context)
5647 * @param exp when will this value expire
5648 * @param key key of the result
5649 * @param get_path path of the get request (not used)
5650 * @param get_path_length lenght of get_path (not used)
5651 * @param put_path path of the put request (not used)
5652 * @param put_path_length length of the put_path (not used)
5653 * @param type type of the result
5654 * @param size number of bytes in data
5655 * @param data pointer to the result data
5658 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5659 const struct GNUNET_HashCode * key,
5660 const struct GNUNET_PeerIdentity *get_path,
5661 unsigned int get_path_length,
5662 const struct GNUNET_PeerIdentity *put_path,
5663 unsigned int put_path_length,
5664 enum GNUNET_BLOCK_Type type,
5665 size_t size, const void *data)
5667 const struct MeshRegexAccept *block = data;
5668 struct MeshRegexSearchContext *ctx = cls;
5669 struct MeshRegexSearchInfo *info = ctx->info;
5670 struct MeshPeerPath *p;
5671 struct MeshPeerInfo *peer_info;
5673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5676 peer_info = peer_info_get(&block->id);
5677 p = path_build_from_dht (get_path, get_path_length, put_path,
5679 path_add_to_peers (p, GNUNET_NO);
5682 tunnel_add_peer (info->t, peer_info);
5683 peer_info_connect (peer_info, info->t);
5684 if (0 == info->peer)
5686 info->peer = peer_info->id;
5690 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5693 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5694 ®ex_connect_timeout,
5702 * Function to process DHT string to regex matching.
5703 * Called on each result obtained for the DHT search.
5705 * @param cls closure (search context)
5706 * @param exp when will this value expire
5707 * @param key key of the result
5708 * @param get_path path of the get request (not used)
5709 * @param get_path_length lenght of get_path (not used)
5710 * @param put_path path of the put request (not used)
5711 * @param put_path_length length of the put_path (not used)
5712 * @param type type of the result
5713 * @param size number of bytes in data
5714 * @param data pointer to the result data
5716 * TODO: re-issue the request after certain time? cancel after X results?
5719 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5720 const struct GNUNET_HashCode * key,
5721 const struct GNUNET_PeerIdentity *get_path,
5722 unsigned int get_path_length,
5723 const struct GNUNET_PeerIdentity *put_path,
5724 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5725 size_t size, const void *data)
5727 const struct MeshRegexBlock *block = data;
5728 struct MeshRegexSearchContext *ctx = cls;
5729 struct MeshRegexSearchInfo *info = ctx->info;
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5734 "DHT GET STRING RETURNED RESULTS\n");
5735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5736 " key: %s\n", GNUNET_h2s (key));
5738 copy = GNUNET_malloc (size);
5739 memcpy (copy, data, size);
5740 GNUNET_break (GNUNET_OK ==
5741 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5742 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5743 len = ntohl (block->n_proof);
5745 char proof[len + 1];
5747 memcpy (proof, &block[1], len);
5749 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5751 GNUNET_break_op (0);
5755 len = strlen (info->description);
5756 if (len == ctx->position) // String processed
5758 if (GNUNET_YES == ntohl (block->accepting))
5760 regex_find_path(key, ctx);
5764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5765 // FIXME REGEX this block not successful, wait for more? start timeout?
5769 GNUNET_break (GNUNET_OK ==
5770 GNUNET_MESH_regex_block_iterate (block, size,
5771 ®ex_edge_iterator, ctx));
5775 /******************************************************************************/
5776 /********************* MESH LOCAL HANDLES **************************/
5777 /******************************************************************************/
5781 * Handler for client disconnection
5783 * @param cls closure
5784 * @param client identification of the client; NULL
5785 * for the last call when the server is destroyed
5788 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5790 struct MeshClient *c;
5791 struct MeshClient *next;
5794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5803 if (c->handle != client)
5805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5811 GNUNET_SERVER_client_drop (c->handle);
5812 c->shutting_down = GNUNET_YES;
5813 GNUNET_assert (NULL != c->own_tunnels);
5814 GNUNET_assert (NULL != c->incoming_tunnels);
5815 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5816 &tunnel_destroy_iterator, c);
5817 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5818 &tunnel_destroy_iterator, c);
5819 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5820 &tunnel_destroy_iterator, c);
5821 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5822 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5823 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5825 /* deregister clients applications */
5826 if (NULL != c->apps)
5828 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5829 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5831 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5832 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5834 GNUNET_SCHEDULER_cancel (announce_applications_task);
5835 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5837 if (NULL != c->types)
5838 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5839 for (i = 0; i < c->n_regex; i++)
5841 GNUNET_free (c->regexes[i]);
5843 GNUNET_free_non_null (c->regexes);
5844 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5845 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5847 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5850 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5859 * Handler for new clients
5861 * @param cls closure
5862 * @param client identification of the client
5863 * @param message the actual message, which includes messages the client wants
5866 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5867 const struct GNUNET_MessageHeader *message)
5869 struct GNUNET_MESH_ClientConnect *cc_msg;
5870 struct MeshClient *c;
5871 GNUNET_MESH_ApplicationType *a;
5878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5879 /* Check data sanity */
5880 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5881 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5882 ntypes = ntohs (cc_msg->types);
5883 napps = ntohs (cc_msg->applications);
5885 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5892 /* Create new client structure */
5893 c = GNUNET_malloc (sizeof (struct MeshClient));
5894 c->id = next_client_id++;
5895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5897 GNUNET_SERVER_client_keep (client);
5898 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5901 GNUNET_MESH_ApplicationType at;
5902 struct GNUNET_HashCode hc;
5904 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5905 for (i = 0; i < napps; i++)
5908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5909 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5910 /* store in clients hashmap */
5911 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5912 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5913 /* store in global hashmap, for announcements */
5914 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5915 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5917 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5918 announce_applications_task =
5919 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5925 struct GNUNET_HashCode hc;
5927 t = (uint16_t *) & a[napps];
5928 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5929 for (i = 0; i < ntypes; i++)
5932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5933 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5935 /* store in clients hashmap */
5936 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5937 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5938 /* store in global hashmap */
5939 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5940 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5944 " client has %u+%u subscriptions\n", napps, ntypes);
5946 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5947 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5948 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5949 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5950 GNUNET_SERVER_notification_context_add (nc, client);
5951 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5953 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5959 * Handler for clients announcing available services by a regular expression.
5961 * @param cls closure
5962 * @param client identification of the client
5963 * @param message the actual message, which includes messages the client wants
5966 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5967 const struct GNUNET_MessageHeader *message)
5969 struct MeshClient *c;
5973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5975 /* Sanity check for client registration */
5976 if (NULL == (c = client_get (client)))
5979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5984 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5985 regex = GNUNET_malloc (len + 1);
5986 memcpy (regex, &message[1], len);
5988 GNUNET_array_append (c->regexes, c->n_regex, regex);
5989 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5991 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5997 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6003 * Handler for requests of new tunnels
6005 * @param cls closure
6006 * @param client identification of the client
6007 * @param message the actual message
6010 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6011 const struct GNUNET_MessageHeader *message)
6013 struct GNUNET_MESH_TunnelMessage *t_msg;
6014 struct MeshTunnel *t;
6015 struct MeshClient *c;
6016 MESH_TunnelNumber tid;
6018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6020 /* Sanity check for client registration */
6021 if (NULL == (c = client_get (client)))
6024 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6029 /* Message sanity check */
6030 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6037 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6038 /* Sanity check for tunnel numbering */
6039 tid = ntohl (t_msg->tunnel_id);
6040 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6046 /* Sanity check for duplicate tunnel IDs */
6047 if (NULL != tunnel_get_by_local_id (c, tid))
6050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6054 while (NULL != tunnel_get_by_pi (myid, next_tid))
6055 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6056 t = tunnel_new (myid, next_tid++, c, tid);
6059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6064 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6066 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6067 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6070 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6076 * Handler for requests of deleting tunnels
6078 * @param cls closure
6079 * @param client identification of the client
6080 * @param message the actual message
6083 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6084 const struct GNUNET_MessageHeader *message)
6086 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6087 struct MeshClient *c;
6088 struct MeshTunnel *t;
6089 MESH_TunnelNumber tid;
6091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6092 "Got a DESTROY TUNNEL from client!\n");
6094 /* Sanity check for client registration */
6095 if (NULL == (c = client_get (client)))
6098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6101 /* Message sanity check */
6102 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6105 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6109 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6111 /* Retrieve tunnel */
6112 tid = ntohl (tunnel_msg->tunnel_id);
6113 t = tunnel_get_by_local_id(c, tid);
6116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6121 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6123 client_ignore_tunnel (c, t);
6125 // TODO: when to destroy incoming tunnel?
6126 if (t->nclients == 0)
6128 GNUNET_assert (GNUNET_YES ==
6129 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6131 GNUNET_assert (GNUNET_YES ==
6132 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6133 &my_full_id.hashPubKey,
6137 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6140 send_client_tunnel_disconnect(t, c);
6141 client_delete_tunnel(c, t);
6143 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6145 tunnel_send_destroy (t);
6146 t->destroy = GNUNET_YES;
6147 // The tunnel will be destroyed when the last message is transmitted.
6148 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6154 * Handler for requests of seeting tunnel's speed.
6156 * @param cls Closure (unused).
6157 * @param client Identification of the client.
6158 * @param message The actual message.
6161 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6162 const struct GNUNET_MessageHeader *message)
6164 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6165 struct MeshClient *c;
6166 struct MeshTunnel *t;
6167 MESH_TunnelNumber tid;
6169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6170 "Got a SPEED request from client!\n");
6172 /* Sanity check for client registration */
6173 if (NULL == (c = client_get (client)))
6176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6181 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6183 /* Retrieve tunnel */
6184 tid = ntohl (tunnel_msg->tunnel_id);
6185 t = tunnel_get_by_local_id(c, tid);
6188 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6190 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6194 switch (ntohs(message->type))
6196 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6197 t->speed_min = GNUNET_YES;
6199 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6200 t->speed_min = GNUNET_NO;
6205 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6210 * Handler for requests of seeting tunnel's buffering policy.
6212 * @param cls Closure (unused).
6213 * @param client Identification of the client.
6214 * @param message The actual message.
6217 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6218 const struct GNUNET_MessageHeader *message)
6220 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6221 struct MeshClient *c;
6222 struct MeshTunnel *t;
6223 MESH_TunnelNumber tid;
6225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6226 "Got a BUFFER request from client!\n");
6228 /* Sanity check for client registration */
6229 if (NULL == (c = client_get (client)))
6232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6237 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6239 /* Retrieve tunnel */
6240 tid = ntohl (tunnel_msg->tunnel_id);
6241 t = tunnel_get_by_local_id(c, tid);
6244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6246 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6250 switch (ntohs(message->type))
6252 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6253 t->nobuffer = GNUNET_NO;
6255 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6256 t->nobuffer = GNUNET_YES;
6262 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6267 * Handler for connection requests to new peers
6269 * @param cls closure
6270 * @param client identification of the client
6271 * @param message the actual message (PeerControl)
6274 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6275 const struct GNUNET_MessageHeader *message)
6277 struct GNUNET_MESH_PeerControl *peer_msg;
6278 struct MeshPeerInfo *peer_info;
6279 struct MeshClient *c;
6280 struct MeshTunnel *t;
6281 MESH_TunnelNumber tid;
6283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6284 /* Sanity check for client registration */
6285 if (NULL == (c = client_get (client)))
6288 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6292 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6293 /* Sanity check for message size */
6294 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6297 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6301 /* Tunnel exists? */
6302 tid = ntohl (peer_msg->tunnel_id);
6303 t = tunnel_get_by_local_id (c, tid);
6307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6311 /* Does client own tunnel? */
6312 if (t->owner->handle != client)
6315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6319 GNUNET_i2s (&peer_msg->peer));
6320 peer_info = peer_info_get (&peer_msg->peer);
6322 tunnel_add_peer (t, peer_info);
6323 peer_info_connect (peer_info, t);
6325 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6331 * Handler for disconnection requests of peers in a tunnel
6333 * @param cls closure
6334 * @param client identification of the client
6335 * @param message the actual message (PeerControl)
6338 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6339 const struct GNUNET_MessageHeader *message)
6341 struct GNUNET_MESH_PeerControl *peer_msg;
6342 struct MeshPeerInfo *peer_info;
6343 struct MeshClient *c;
6344 struct MeshTunnel *t;
6345 MESH_TunnelNumber tid;
6347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6348 /* Sanity check for client registration */
6349 if (NULL == (c = client_get (client)))
6352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6355 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6356 /* Sanity check for message size */
6357 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6360 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6364 /* Tunnel exists? */
6365 tid = ntohl (peer_msg->tunnel_id);
6366 t = tunnel_get_by_local_id (c, tid);
6370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6375 /* Does client own tunnel? */
6376 if (t->owner->handle != client)
6379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6384 GNUNET_i2s (&peer_msg->peer));
6385 /* Is the peer in the tunnel? */
6387 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6388 if (NULL == peer_info)
6391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6395 /* Ok, delete peer from tunnel */
6396 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6397 &peer_msg->peer.hashPubKey);
6399 send_destroy_path (t, peer_info->id);
6400 tunnel_delete_peer (t, peer_info->id);
6401 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6406 * Handler for blacklist requests of peers in a tunnel
6408 * @param cls closure
6409 * @param client identification of the client
6410 * @param message the actual message (PeerControl)
6413 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6414 const struct GNUNET_MessageHeader *message)
6416 struct GNUNET_MESH_PeerControl *peer_msg;
6417 struct MeshClient *c;
6418 struct MeshTunnel *t;
6419 MESH_TunnelNumber tid;
6421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6422 /* Sanity check for client registration */
6423 if (NULL == (c = client_get (client)))
6426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6429 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6431 /* Sanity check for message size */
6432 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6439 /* Tunnel exists? */
6440 tid = ntohl (peer_msg->tunnel_id);
6441 t = tunnel_get_by_local_id (c, tid);
6445 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6450 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6451 GNUNET_PEER_intern(&peer_msg->peer));
6456 * Handler for unblacklist requests of peers in a tunnel
6458 * @param cls closure
6459 * @param client identification of the client
6460 * @param message the actual message (PeerControl)
6463 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6464 const struct GNUNET_MessageHeader *message)
6466 struct GNUNET_MESH_PeerControl *peer_msg;
6467 struct MeshClient *c;
6468 struct MeshTunnel *t;
6469 MESH_TunnelNumber tid;
6473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6474 /* Sanity check for client registration */
6475 if (NULL == (c = client_get (client)))
6478 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6481 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6483 /* Sanity check for message size */
6484 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6491 /* Tunnel exists? */
6492 tid = ntohl (peer_msg->tunnel_id);
6493 t = tunnel_get_by_local_id (c, tid);
6497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6502 /* if peer is not known, complain */
6503 pid = GNUNET_PEER_search (&peer_msg->peer);
6510 /* search and remove from list */
6511 for (i = 0; i < t->nblacklisted; i++)
6513 if (t->blacklisted[i] == pid)
6515 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6516 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6521 /* if peer hasn't been blacklisted, complain */
6527 * Handler for connection requests to new peers by type
6529 * @param cls closure
6530 * @param client identification of the client
6531 * @param message the actual message (ConnectPeerByType)
6534 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6535 const struct GNUNET_MessageHeader *message)
6537 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6538 struct MeshClient *c;
6539 struct MeshTunnel *t;
6540 struct GNUNET_HashCode hash;
6541 MESH_TunnelNumber tid;
6543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6544 /* Sanity check for client registration */
6545 if (NULL == (c = client_get (client)))
6548 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6552 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6553 /* Sanity check for message size */
6554 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6555 ntohs (connect_msg->header.size))
6558 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6562 /* Tunnel exists? */
6563 tid = ntohl (connect_msg->tunnel_id);
6564 t = tunnel_get_by_local_id (c, tid);
6568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6572 /* Does client own tunnel? */
6573 if (t->owner->handle != client)
6576 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6580 /* Do WE have the service? */
6581 t->type = ntohl (connect_msg->type);
6582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6583 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6584 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6587 /* Yes! Fast forward, add ourselves to the tunnel and send the
6588 * good news to the client, and alert the destination client of
6589 * an incoming tunnel.
6591 * FIXME send a path create to self, avoid code duplication
6593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6594 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6595 peer_info_get (&my_full_id),
6596 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6599 send_client_peer_connected (t, myid);
6600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6601 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6603 t->local_tid_dest = next_local_tid++;
6604 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6605 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6606 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6610 /* Ok, lets find a peer offering the service */
6611 if (NULL != t->dht_get_type)
6613 GNUNET_DHT_get_stop (t->dht_get_type);
6615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6616 GNUNET_h2s (&hash));
6618 GNUNET_DHT_get_start (dht_handle,
6619 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6621 dht_replication_level,
6622 GNUNET_DHT_RO_RECORD_ROUTE |
6623 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6625 &dht_get_type_handler, t);
6627 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6633 * Handler for connection requests to new peers by a string service description.
6635 * @param cls closure
6636 * @param client identification of the client
6637 * @param message the actual message, which includes messages the client wants
6640 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6641 const struct GNUNET_MessageHeader *message)
6643 struct GNUNET_MESH_ConnectPeerByString *msg;
6644 struct MeshRegexSearchContext *ctx;
6645 struct MeshRegexSearchInfo *info;
6646 struct GNUNET_DHT_GetHandle *get_h;
6647 struct GNUNET_HashCode key;
6648 struct MeshTunnel *t;
6649 struct MeshClient *c;
6650 MESH_TunnelNumber tid;
6654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6655 "Connect by string started\n");
6656 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6657 size = htons (message->size);
6659 /* Sanity check for client registration */
6660 if (NULL == (c = client_get (client)))
6663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6667 /* Message size sanity check */
6668 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6675 /* Tunnel exists? */
6676 tid = ntohl (msg->tunnel_id);
6677 t = tunnel_get_by_local_id (c, tid);
6681 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6685 /* Does client own tunnel? */
6686 if (t->owner->handle != client)
6689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6694 " on tunnel %s [%u]\n",
6695 GNUNET_i2s(&my_full_id),
6698 /* Only one connect_by_string allowed at the same time! */
6699 /* FIXME: allow more, return handle at api level to cancel, document */
6700 if (NULL != t->regex_ctx)
6703 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6707 /* Find string itself */
6708 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6709 string = (const char *) &msg[1];
6711 /* Initialize context */
6712 size = GNUNET_REGEX_get_first_key(string, len, &key);
6713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6714 " consumed %u bits out of %u\n", size, len);
6715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6716 " looking for %s\n", GNUNET_h2s (&key));
6718 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6720 info->description = GNUNET_malloc (len + 1);
6721 memcpy (info->description, string, len);
6722 info->description[len] = '\0';
6723 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6724 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6727 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6728 ctx->position = size;
6732 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6734 /* Start search in DHT */
6735 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6736 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6737 &key, /* key to search */
6738 dht_replication_level, /* replication level */
6739 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6740 NULL, /* xquery */ // FIXME BLOOMFILTER
6741 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6742 &dht_get_string_handler, ctx);
6744 GNUNET_break (GNUNET_OK ==
6745 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6748 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6750 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6756 * Handler for client traffic directed to one peer
6758 * @param cls closure
6759 * @param client identification of the client
6760 * @param message the actual message
6763 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6764 const struct GNUNET_MessageHeader *message)
6766 struct MeshClient *c;
6767 struct MeshTunnel *t;
6768 struct MeshPeerInfo *pi;
6769 struct GNUNET_MESH_Unicast *data_msg;
6770 MESH_TunnelNumber tid;
6773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6774 "Got a unicast request from a client!\n");
6776 /* Sanity check for client registration */
6777 if (NULL == (c = client_get (client)))
6780 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6783 data_msg = (struct GNUNET_MESH_Unicast *) message;
6784 /* Sanity check for message size */
6785 size = ntohs (message->size);
6786 if (sizeof (struct GNUNET_MESH_Unicast) +
6787 sizeof (struct GNUNET_MessageHeader) > size)
6790 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6794 /* Tunnel exists? */
6795 tid = ntohl (data_msg->tid);
6796 t = tunnel_get_by_local_id (c, tid);
6800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6804 /* Is it a local tunnel? Then, does client own the tunnel? */
6805 if (t->owner->handle != client)
6808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6812 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6813 &data_msg->destination.hashPubKey);
6814 /* Is the selected peer in the tunnel? */
6818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6822 /* PID should be as expected */
6823 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6827 "Unicast PID, expected %u, got %u\n",
6828 t->fwd_pid + 1, ntohl (data_msg->pid));
6829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6833 /* Ok, everything is correct, send the message
6834 * (pretend we got it from a mesh peer)
6837 /* Work around const limitation */
6838 char buf[ntohs (message->size)] GNUNET_ALIGN;
6839 struct GNUNET_MESH_Unicast *copy;
6841 copy = (struct GNUNET_MESH_Unicast *) buf;
6842 memcpy (buf, data_msg, size);
6843 copy->oid = my_full_id;
6844 copy->tid = htonl (t->id.tid);
6845 copy->ttl = htonl (default_ttl);
6846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6847 " calling generic handler...\n");
6848 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6851 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6857 * Handler for client traffic directed to the origin
6859 * @param cls closure
6860 * @param client identification of the client
6861 * @param message the actual message
6864 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6865 const struct GNUNET_MessageHeader *message)
6867 struct GNUNET_MESH_ToOrigin *data_msg;
6868 struct MeshTunnelClientInfo *clinfo;
6869 struct MeshClient *c;
6870 struct MeshTunnel *t;
6871 MESH_TunnelNumber tid;
6874 /* Sanity check for client registration */
6875 if (NULL == (c = client_get (client)))
6878 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6881 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6882 /* Sanity check for message size */
6883 size = ntohs (message->size);
6884 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6885 sizeof (struct GNUNET_MessageHeader) > size)
6888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6892 /* Tunnel exists? */
6893 tid = ntohl (data_msg->tid);
6894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6895 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6896 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6899 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6902 t = tunnel_get_by_local_id (c, tid);
6906 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6910 /* It should be sent by someone who has this as incoming tunnel. */
6911 if (GNUNET_NO == client_knows_tunnel (c, t))
6914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6918 /* PID should be as expected */
6919 clinfo = tunnel_get_client_fc (t, c);
6920 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6923 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6924 "To Origin PID, expected %u, got %u\n",
6925 clinfo->bck_pid + 1, ntohl (data_msg->pid));
6926 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6930 /* Ok, everything is correct, send the message
6931 * (pretend we got it from a mesh peer)
6934 char buf[ntohs (message->size)] GNUNET_ALIGN;
6935 struct GNUNET_MESH_ToOrigin *copy;
6937 /* Work around const limitation */
6938 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6939 memcpy (buf, data_msg, size);
6940 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6941 copy->tid = htonl (t->id.tid);
6942 copy->ttl = htonl (default_ttl);
6943 GNUNET_assert (ntohl (copy->pid) == (t->bck_pid + 1));
6944 copy->sender = my_full_id;
6945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6946 " calling generic handler...\n");
6947 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6949 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6955 * Handler for client traffic directed to all peers in a tunnel
6957 * @param cls closure
6958 * @param client identification of the client
6959 * @param message the actual message
6962 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6963 const struct GNUNET_MessageHeader *message)
6965 struct MeshClient *c;
6966 struct MeshTunnel *t;
6967 struct GNUNET_MESH_Multicast *data_msg;
6968 MESH_TunnelNumber tid;
6970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6971 "Got a multicast request from a client!\n");
6973 /* Sanity check for client registration */
6974 if (NULL == (c = client_get (client)))
6977 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6980 data_msg = (struct GNUNET_MESH_Multicast *) message;
6981 /* Sanity check for message size */
6982 if (sizeof (struct GNUNET_MESH_Multicast) +
6983 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6986 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6990 /* Tunnel exists? */
6991 tid = ntohl (data_msg->tid);
6992 t = tunnel_get_by_local_id (c, tid);
6996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7000 /* Does client own tunnel? */
7001 if (t->owner->handle != client)
7004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7008 /* PID should be as expected */
7009 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7012 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7013 "Multicast PID, expected %u, got %u\n",
7014 t->fwd_pid + 1, ntohl (data_msg->pid));
7015 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7020 char buf[ntohs (message->size)] GNUNET_ALIGN;
7021 struct GNUNET_MESH_Multicast *copy;
7023 copy = (struct GNUNET_MESH_Multicast *) buf;
7024 memcpy (buf, message, ntohs (message->size));
7025 copy->oid = my_full_id;
7026 copy->tid = htonl (t->id.tid);
7027 copy->ttl = htonl (default_ttl);
7028 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7030 " calling generic handler...\n");
7031 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7034 /* receive done gets called when last copy is sent to a neighbor */
7040 * Handler for client's ACKs for payload traffic.
7042 * @param cls Closure (unused).
7043 * @param client Identification of the client.
7044 * @param message The actual message.
7047 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7048 const struct GNUNET_MessageHeader *message)
7050 struct GNUNET_MESH_LocalAck *msg;
7051 struct MeshTunnel *t;
7052 struct MeshClient *c;
7053 MESH_TunnelNumber tid;
7055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7056 /* Sanity check for client registration */
7057 if (NULL == (c = client_get (client)))
7060 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7064 msg = (struct GNUNET_MESH_LocalAck *) message;
7065 /* Tunnel exists? */
7066 tid = ntohl (msg->tunnel_id);
7067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7068 t = tunnel_get_by_local_id (c, tid);
7071 GNUNET_break (0); // FIXME fc
7072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7076 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7077 if (NULL != t->owner && t->owner->handle == client)
7079 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7080 t->bck_ack = ntohl(msg->max_pid);
7081 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7085 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7086 tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
7087 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7090 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7096 * Functions to handle messages from clients
7098 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7099 {&handle_local_new_client, NULL,
7100 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7101 {&handle_local_announce_regex, NULL,
7102 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7103 {&handle_local_tunnel_create, NULL,
7104 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7105 sizeof (struct GNUNET_MESH_TunnelMessage)},
7106 {&handle_local_tunnel_destroy, NULL,
7107 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7108 sizeof (struct GNUNET_MESH_TunnelMessage)},
7109 {&handle_local_tunnel_speed, NULL,
7110 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7111 sizeof (struct GNUNET_MESH_TunnelMessage)},
7112 {&handle_local_tunnel_speed, NULL,
7113 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7114 sizeof (struct GNUNET_MESH_TunnelMessage)},
7115 {&handle_local_tunnel_buffer, NULL,
7116 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7117 sizeof (struct GNUNET_MESH_TunnelMessage)},
7118 {&handle_local_tunnel_buffer, NULL,
7119 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7120 sizeof (struct GNUNET_MESH_TunnelMessage)},
7121 {&handle_local_connect_add, NULL,
7122 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7123 sizeof (struct GNUNET_MESH_PeerControl)},
7124 {&handle_local_connect_del, NULL,
7125 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7126 sizeof (struct GNUNET_MESH_PeerControl)},
7127 {&handle_local_blacklist, NULL,
7128 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7129 sizeof (struct GNUNET_MESH_PeerControl)},
7130 {&handle_local_unblacklist, NULL,
7131 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7132 sizeof (struct GNUNET_MESH_PeerControl)},
7133 {&handle_local_connect_by_type, NULL,
7134 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7135 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7136 {&handle_local_connect_by_string, NULL,
7137 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7138 {&handle_local_unicast, NULL,
7139 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7140 {&handle_local_to_origin, NULL,
7141 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7142 {&handle_local_multicast, NULL,
7143 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7144 {&handle_local_ack, NULL,
7145 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7146 sizeof (struct GNUNET_MESH_LocalAck)},
7152 * To be called on core init/fail.
7154 * @param cls service closure
7155 * @param server handle to the server for this service
7156 * @param identity the public identity of this peer
7159 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7160 const struct GNUNET_PeerIdentity *identity)
7163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7164 core_handle = server;
7165 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7168 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7169 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7171 GNUNET_abort(); // Try harder
7178 * Method called whenever a given peer connects.
7180 * @param cls closure
7181 * @param peer peer identity this notification is about
7182 * @param atsi performance data for the connection
7183 * @param atsi_count number of records in 'atsi'
7186 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7187 const struct GNUNET_ATS_Information *atsi,
7188 unsigned int atsi_count)
7190 struct MeshPeerInfo *peer_info;
7191 struct MeshPeerPath *path;
7193 DEBUG_CONN ("Peer connected\n");
7194 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7195 peer_info = peer_info_get (peer);
7196 if (myid == peer_info->id)
7198 DEBUG_CONN (" (self)\n");
7203 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7205 path = path_new (2);
7206 path->peers[0] = myid;
7207 path->peers[1] = peer_info->id;
7208 GNUNET_PEER_change_rc (myid, 1);
7209 GNUNET_PEER_change_rc (peer_info->id, 1);
7210 peer_info_add_path (peer_info, path, GNUNET_YES);
7211 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7217 * Method called whenever a peer disconnects.
7219 * @param cls closure
7220 * @param peer peer identity this notification is about
7223 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7225 struct MeshPeerInfo *pi;
7226 struct MeshPeerQueue *q;
7227 struct MeshPeerQueue *n;
7229 DEBUG_CONN ("Peer disconnected\n");
7230 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7242 /* try to reroute this traffic instead */
7243 queue_destroy(q, GNUNET_YES);
7247 peer_info_remove_path (pi, pi->id, myid);
7250 DEBUG_CONN (" (self)\n");
7252 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7257 /******************************************************************************/
7258 /************************ MAIN FUNCTIONS ****************************/
7259 /******************************************************************************/
7262 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7264 * @param cls closure
7265 * @param key current key code
7266 * @param value value in the hash map
7267 * @return GNUNET_YES if we should continue to iterate,
7271 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7273 struct MeshTunnel *t = value;
7280 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7282 * @param cls closure
7283 * @param key current key code
7284 * @param value value in the hash map
7285 * @return GNUNET_YES if we should continue to iterate,
7289 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7291 struct MeshPeerInfo *p = value;
7292 struct MeshPeerQueue *q;
7293 struct MeshPeerQueue *n;
7301 queue_destroy(q, GNUNET_YES);
7305 peer_info_destroy (p);
7310 * Task run during shutdown.
7316 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7320 if (core_handle != NULL)
7322 GNUNET_CORE_disconnect (core_handle);
7325 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7326 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7327 if (dht_handle != NULL)
7329 GNUNET_DHT_disconnect (dht_handle);
7334 GNUNET_SERVER_notification_context_destroy (nc);
7337 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7339 GNUNET_SCHEDULER_cancel (announce_id_task);
7340 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7346 * Process mesh requests.
7348 * @param cls closure
7349 * @param server the initialized server
7350 * @param c configuration to use
7353 run (void *cls, struct GNUNET_SERVER_Handle *server,
7354 const struct GNUNET_CONFIGURATION_Handle *c)
7356 struct MeshPeerInfo *peer;
7357 struct MeshPeerPath *p;
7360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7361 server_handle = server;
7362 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7363 NULL, /* Closure passed to MESH functions */
7364 &core_init, /* Call core_init once connected */
7365 &core_connect, /* Handle connects */
7366 &core_disconnect, /* remove peers on disconnects */
7367 NULL, /* Don't notify about all incoming messages */
7368 GNUNET_NO, /* For header only in notification */
7369 NULL, /* Don't notify about all outbound messages */
7370 GNUNET_NO, /* For header-only out notification */
7371 core_handlers); /* Register these handlers */
7373 if (core_handle == NULL)
7376 GNUNET_SCHEDULER_shutdown ();
7381 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
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_time (c, "MESH", "REFRESH_PATH_TIME",
7394 &refresh_path_time))
7396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7398 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7399 "refresh path time");
7400 GNUNET_SCHEDULER_shutdown ();
7405 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7406 &app_announce_time))
7408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7410 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7411 "app announce time");
7412 GNUNET_SCHEDULER_shutdown ();
7417 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7422 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7423 "id announce time");
7424 GNUNET_SCHEDULER_shutdown ();
7429 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7430 &unacknowledged_wait_time))
7432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7434 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7435 "unacknowledged wait time");
7436 GNUNET_SCHEDULER_shutdown ();
7441 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7446 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7448 GNUNET_SCHEDULER_shutdown ();
7453 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7458 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7460 GNUNET_SCHEDULER_shutdown ();
7465 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7468 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7470 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7472 GNUNET_SCHEDULER_shutdown ();
7477 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7480 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7482 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7488 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7489 &dht_replication_level))
7491 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7493 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7494 "dht replication level", 10);
7495 dht_replication_level = 10;
7499 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7500 GNUNET_free (keyfile);
7501 if (my_private_key == NULL)
7503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7504 _("Mesh service could not access hostkey. Exiting.\n"));
7505 GNUNET_SCHEDULER_shutdown ();
7508 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7509 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7510 &my_full_id.hashPubKey);
7511 myid = GNUNET_PEER_intern (&my_full_id);
7513 // transport_handle = GNUNET_TRANSPORT_connect(c,
7520 dht_handle = GNUNET_DHT_connect (c, 64);
7521 if (dht_handle == NULL)
7526 stats = GNUNET_STATISTICS_create ("mesh", c);
7530 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7532 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7533 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7534 peers = GNUNET_CONTAINER_multihashmap_create (32);
7535 applications = GNUNET_CONTAINER_multihashmap_create (32);
7536 types = GNUNET_CONTAINER_multihashmap_create (32);
7538 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7539 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7540 GNUNET_SERVER_disconnect_notify (server_handle,
7541 &handle_local_client_disconnect, NULL);
7545 clients_tail = NULL;
7548 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7549 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7551 /* Create a peer_info for the local peer */
7552 peer = peer_info_get (&my_full_id);
7555 GNUNET_PEER_change_rc (myid, 1);
7556 peer_info_add_path (peer, p, GNUNET_YES);
7558 /* Scheduled the task to clean up when shutdown is called */
7559 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7566 * The main function for the mesh service.
7568 * @param argc number of arguments from the command line
7569 * @param argv command line arguments
7570 * @return 0 ok, 1 on error
7573 main (int argc, char *const *argv)
7577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7580 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");