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 * Check if one pid is bigger than other, accounting for overflow.
1710 * @param bigger Argument that should be bigger.
1711 * @param smaller Argument that should be smaller.
1713 * @return True if bigger (arg1) has a higher value than smaller (arg 2).
1716 is_pid_bigger (uint32_t bigger, uint32_t smaller)
1718 return (GNUNET_YES == PID_OVERFLOW(smaller, bigger) ||
1719 (bigger > smaller && GNUNET_NO == PID_OVERFLOW(bigger, smaller)));
1723 * Get the higher ACK value out of two values, taking in account overflow.
1725 * @param a First ACK value.
1726 * @param b Second ACK value.
1728 * @return Highest ACK value from the two.
1731 max_pid (uint32_t a, uint32_t b)
1733 if (is_pid_bigger(a, b))
1740 * Get the lower ACK value out of two values, taking in account overflow.
1742 * @param a First ACK value.
1743 * @param b Second ACK value.
1745 * @return Lowest ACK value from the two.
1748 min_pid (uint32_t a, uint32_t b)
1750 if (is_pid_bigger(a, b))
1757 * Decrements the reference counter and frees all resources if needed
1759 * @param mesh_data Data Descriptor used in a multicast message.
1760 * Freed no longer needed (last message).
1763 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1765 /* Make sure it's a multicast packet */
1766 GNUNET_assert (NULL != mesh_data->reference_counter);
1768 if (0 == --(*(mesh_data->reference_counter)))
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1771 if (NULL != mesh_data->task)
1773 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1775 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1777 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1779 GNUNET_free (mesh_data->task);
1781 GNUNET_free (mesh_data->reference_counter);
1782 GNUNET_free (mesh_data->data);
1783 GNUNET_free (mesh_data);
1789 * Check if client has registered with the service and has not disconnected
1791 * @param client the client to check
1793 * @return non-NULL if client exists in the global DLL
1795 static struct MeshClient *
1796 client_get (struct GNUNET_SERVER_Client *client)
1798 struct MeshClient *c;
1803 if (c->handle == client)
1812 * Checks if a given client has subscribed to certain message type
1814 * @param message_type Type of message to check
1815 * @param c Client to check
1817 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1820 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1822 struct GNUNET_HashCode hc;
1824 if (NULL == c->types)
1826 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1827 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1832 * Allow a client to send more data after transmitting a multicast message
1833 * which some neighbor has not yet accepted altough a reasonable time has
1836 * @param cls Closure (DataDescriptor containing the task identifier)
1837 * @param tc Task Context
1839 * FIXME reference counter cshould be just int
1842 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1844 struct MeshData *mdata = cls;
1846 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1848 GNUNET_assert (NULL != mdata->reference_counter);
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1851 *(mdata->reference_counter));
1852 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1853 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1858 * Check whether client wants traffic from a tunnel.
1860 * @param c Client to check.
1861 * @param t Tunnel to be found.
1863 * @return GNUNET_YES if client knows tunnel.
1865 * TODO look in client hashmap
1868 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1872 for (i = 0; i < t->nclients; i++)
1873 if (t->clients[i] == c)
1880 * Check whether client has been informed about a tunnel.
1882 * @param c Client to check.
1883 * @param t Tunnel to be found.
1885 * @return GNUNET_YES if client knows tunnel.
1887 * TODO look in client hashmap
1890 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1894 for (i = 0; i < t->nignore; i++)
1895 if (t->ignore[i] == c)
1897 return client_wants_tunnel(c, t);
1902 * Marks a client as uninterested in traffic from the tunnel, updating both
1903 * client and tunnel to reflect this.
1905 * @param c Client that doesn't want traffic anymore.
1906 * @param t Tunnel which should be ignored.
1908 * FIXME when to delete an incoming tunnel?
1911 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1913 struct GNUNET_HashCode hash;
1915 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1916 GNUNET_break (GNUNET_YES ==
1917 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1919 GNUNET_break (GNUNET_YES ==
1920 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1921 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1922 tunnel_delete_active_client (t, c);
1923 GNUNET_array_append (t->ignore, t->nignore, c);
1928 * Deletes a tunnel from a client (either owner or destination). To be used on
1929 * tunnel destroy, otherwise, use client_ignore_tunnel.
1931 * @param c Client whose tunnel to delete.
1932 * @param t Tunnel which should be deleted.
1935 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1937 struct GNUNET_HashCode hash;
1941 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1942 GNUNET_assert (GNUNET_YES ==
1943 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1949 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1951 GNUNET_assert (GNUNET_YES ==
1952 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1956 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1964 * Send the message to all clients that have subscribed to its type
1966 * @param msg Pointer to the message itself
1967 * @param payload Pointer to the payload of the message.
1968 * @return number of clients this message was sent to
1971 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1972 const struct GNUNET_MessageHeader *payload)
1974 struct GNUNET_PeerIdentity *oid;
1975 struct MeshClient *c;
1976 struct MeshTunnel *t;
1977 MESH_TunnelNumber *tid;
1980 char cbuf[htons (msg->size)];
1982 type = ntohs (payload->type);
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %u\n", type);
1986 memcpy (cbuf, msg, sizeof (cbuf));
1987 switch (htons (msg->type))
1989 struct GNUNET_MESH_Unicast *uc;
1990 struct GNUNET_MESH_Multicast *mc;
1991 struct GNUNET_MESH_ToOrigin *to;
1993 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1994 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1998 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1999 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2003 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2004 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2012 t = tunnel_get (oid, ntohl (*tid));
2019 for (count = 0, c = clients; c != NULL; c = c->next)
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2022 if (client_is_subscribed (type, c))
2024 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2028 *tid = htonl (t->local_tid);
2032 if (GNUNET_NO == client_knows_tunnel (c, t))
2034 /* This client doesn't know the tunnel */
2035 struct GNUNET_MESH_TunnelNotification tmsg;
2036 struct GNUNET_HashCode hash;
2038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2039 tmsg.header.size = htons (sizeof (tmsg));
2040 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2041 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2042 tmsg.tunnel_id = htonl (t->local_tid_dest);
2043 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2044 &tmsg.header, GNUNET_NO);
2045 tunnel_add_client (t, c);
2046 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2048 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2049 c->incoming_tunnels, &hash, t,
2050 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2052 *tid = htonl (t->local_tid_dest);
2055 /* Check if the client wants to get traffic from the tunnel */
2056 if (GNUNET_NO == client_wants_tunnel(c, t))
2059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2060 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2061 (struct GNUNET_MessageHeader
2062 *) cbuf, GNUNET_NO);
2070 * Notify the client that owns the tunnel that a peer has connected to it
2071 * (the requested path to it has been confirmed).
2073 * @param t Tunnel whose owner to notify
2074 * @param id Short id of the peer that has connected
2077 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2079 struct GNUNET_MESH_PeerControl pc;
2081 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2082 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2083 pc.tunnel_id = htonl (t->local_tid);
2084 GNUNET_PEER_resolve (id, &pc.peer);
2085 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2091 * Notify a the client of a tunnel about how many more
2092 * payload packages will we accept on a given tunnel,
2093 * distinguishing between root and leaf clients.
2095 * @param c Client whom to send the ACK.
2096 * @param t Tunnel on which to send the ACK.
2099 send_client_tunnel_ack (struct MeshClient *c, struct MeshTunnel *t)
2101 struct GNUNET_MESH_LocalAck msg;
2102 MESH_TunnelNumber tid;
2105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2106 "Sending client ACK on tunnel %X\n",
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " to client %u\n", c->id);
2115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (owner, FWD ACK)\n");
2116 ack = tunnel_get_fwd_ack (t);
2121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (leaf, BCK ACK)\n");
2122 ack = tunnel_get_bck_ack (t);
2123 tid = t->local_tid_dest;
2126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
2127 if (t->last_fwd_ack == ack)
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
2131 t->last_fwd_ack = ack;
2132 msg.header.size = htons (sizeof (msg));
2133 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
2134 msg.tunnel_id = htonl (tid);
2135 msg.max_pid = htonl (ack);
2137 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2138 &msg.header, GNUNET_NO);
2143 * Notify all clients (not depending on registration status) that the incoming
2144 * tunnel is no longer valid.
2146 * @param t Tunnel that was destroyed.
2149 send_clients_tunnel_destroy (struct MeshTunnel *t)
2151 struct GNUNET_MESH_TunnelMessage msg;
2153 msg.header.size = htons (sizeof (msg));
2154 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2155 msg.tunnel_id = htonl (t->local_tid_dest);
2156 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2161 * Notify clients of tunnel disconnections, if needed.
2162 * In case the origin disconnects, the destination clients get a tunnel destroy
2163 * notification. If the last destination disconnects (only one remaining client
2164 * in tunnel), the origin gets a (local ID) peer disconnected.
2165 * Note that the function must be called BEFORE removing the client from
2168 * @param t Tunnel that was destroyed.
2169 * @param c Client that disconnected.
2172 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2178 struct GNUNET_MESH_TunnelMessage msg;
2180 msg.header.size = htons (sizeof (msg));
2181 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2182 msg.tunnel_id = htonl (t->local_tid_dest);
2183 for (i = 0; i < t->nclients; i++)
2184 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2185 &msg.header, GNUNET_NO);
2187 // FIXME when to disconnect an incoming tunnel?
2188 else if (1 == t->nclients && NULL != t->owner)
2190 struct GNUNET_MESH_PeerControl msg;
2192 msg.header.size = htons (sizeof (msg));
2193 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2194 msg.tunnel_id = htonl (t->local_tid);
2195 msg.peer = my_full_id;
2196 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2197 &msg.header, GNUNET_NO);
2203 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2204 * and insert it in the appropiate structures if the peer is not known yet.
2206 * @param peer Full identity of the peer.
2208 * @return Existing or newly created peer info.
2210 static struct MeshPeerInfo *
2211 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2213 struct MeshPeerInfo *peer_info;
2215 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2216 if (NULL == peer_info)
2219 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2220 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2221 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2222 peer_info->id = GNUNET_PEER_intern (peer);
2230 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2231 * and insert it in the appropiate structures if the peer is not known yet.
2233 * @param peer Short identity of the peer.
2235 * @return Existing or newly created peer info.
2237 static struct MeshPeerInfo *
2238 peer_info_get_short (const GNUNET_PEER_Id peer)
2240 struct GNUNET_PeerIdentity id;
2242 GNUNET_PEER_resolve (peer, &id);
2243 return peer_info_get (&id);
2248 * Iterator to remove the tunnel from the list of tunnels a peer participates
2251 * @param cls Closure (tunnel info)
2252 * @param key GNUNET_PeerIdentity of the peer (unused)
2253 * @param value PeerInfo of the peer
2255 * @return always GNUNET_YES, to keep iterating
2258 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2260 struct MeshTunnel *t = cls;
2261 struct MeshPeerInfo *peer = value;
2264 for (i = 0; i < peer->ntunnels; i++)
2267 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2270 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2271 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2280 * Core callback to write a pre-constructed data packet to core buffer
2282 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2283 * @param size Number of bytes available in buf.
2284 * @param buf Where the to write the message.
2286 * @return number of bytes written to buf
2289 send_core_data_raw (void *cls, size_t size, void *buf)
2291 struct MeshTransmissionDescriptor *info = cls;
2292 struct GNUNET_MessageHeader *msg;
2295 GNUNET_assert (NULL != info);
2296 GNUNET_assert (NULL != info->mesh_data);
2297 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2298 total_size = ntohs (msg->size);
2300 if (total_size > size)
2305 memcpy (buf, msg, total_size);
2306 data_descriptor_decrement_rc (info->mesh_data);
2313 * Sends an already built non-multicast message to a peer,
2314 * properly registrating all used resources.
2316 * @param message Message to send. Function makes a copy of it.
2317 * @param peer Short ID of the neighbor whom to send the message.
2318 * @param t Tunnel on which this message is transmitted.
2321 send_message (const struct GNUNET_MessageHeader *message,
2322 const struct GNUNET_PeerIdentity *peer,
2323 struct MeshTunnel *t)
2325 struct MeshTransmissionDescriptor *info;
2326 struct MeshPeerInfo *neighbor;
2327 struct MeshPeerPath *p;
2331 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2333 size = ntohs (message->size);
2334 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2335 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2336 info->mesh_data->data = GNUNET_malloc (size);
2337 memcpy (info->mesh_data->data, message, size);
2338 type = ntohs(message->type);
2339 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2341 struct GNUNET_MESH_Unicast *m;
2343 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2344 m->ttl = htonl (ntohl (m->ttl) - 1);
2346 info->mesh_data->data_len = size;
2347 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2348 *info->mesh_data->reference_counter = 1;
2349 neighbor = peer_info_get (peer);
2350 for (p = neighbor->path_head; NULL != p; p = p->next)
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 " %s IS NOT DIRECTLY CONNECTED\n",
2363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 for (p = neighbor->path_head; NULL != p; p = p->next)
2368 struct GNUNET_PeerIdentity debug_id;
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 " path with %u hops through:\n",
2374 for (i = 0; i < p->length; i++)
2376 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2379 i, GNUNET_i2s(&debug_id));
2383 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2384 GNUNET_free (info->mesh_data->data);
2385 GNUNET_free (info->mesh_data);
2389 info->peer = neighbor;
2390 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2393 * TODO: in this case we only need the service to retransmit
2394 * the message down the path. If we pass the real type to queue_add,
2395 * queue_send will try to build the message from scratch. This can
2396 * probably be done by some other way instead of deleteing the type
2410 * Sends a CREATE PATH message for a path to a peer, properly registrating
2411 * all used resources.
2413 * @param peer PeerInfo of the final peer for whom this path is being created.
2414 * @param p Path itself.
2415 * @param t Tunnel for which the path is created.
2418 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2419 struct MeshTunnel *t)
2421 struct GNUNET_PeerIdentity id;
2422 struct MeshPathInfo *path_info;
2423 struct MeshPeerInfo *neighbor;
2429 p = tree_get_path_to_peer (t->tree, peer->id);
2436 for (i = 0; i < p->length; i++)
2438 if (p->peers[i] == myid)
2441 if (i >= p->length - 1)
2447 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2449 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2450 path_info->path = p;
2452 neighbor = peer_info_get (&id);
2453 path_info->peer = neighbor;
2454 queue_add (path_info,
2455 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2456 sizeof (struct GNUNET_MESH_ManipulatePath) +
2457 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2464 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2466 * @param t Tunnel whose path to destroy.
2467 * @param destination Short ID of the peer to whom the path to destroy.
2470 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2472 struct MeshPeerPath *p;
2475 p = tree_get_path_to_peer (t->tree, destination);
2481 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2482 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2484 struct GNUNET_MESH_ManipulatePath *msg;
2485 struct GNUNET_PeerIdentity *pi;
2489 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2490 msg->header.size = htons (size);
2491 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2492 msg->tid = htonl (t->id.tid);
2493 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2494 for (i = 0; i < p->length; i++)
2496 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2498 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2505 * Try to establish a new connection to this peer.
2506 * Use the best path for the given tunnel.
2507 * If the peer doesn't have any path to it yet, try to get one.
2508 * If the peer already has some path, send a CREATE PATH towards it.
2510 * @param peer PeerInfo of the peer.
2511 * @param t Tunnel for which to create the path, if possible.
2514 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2516 struct MeshPeerPath *p;
2517 struct MeshPathInfo *path_info;
2519 if (NULL != peer->path_head)
2521 p = tree_get_path_to_peer (t->tree, peer->id);
2528 // FIXME always send create path to self
2531 send_create_path (peer, p, t);
2535 struct GNUNET_HashCode hash;
2538 send_client_peer_connected (t, myid);
2539 t->local_tid_dest = next_local_tid++;
2540 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2543 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2544 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2551 else if (NULL == peer->dhtget)
2553 struct GNUNET_PeerIdentity id;
2555 GNUNET_PEER_resolve (peer->id, &id);
2556 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2557 path_info->peer = peer;
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2561 peer->dhtgetcls = path_info;
2562 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2563 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2564 &id.hashPubKey, /* key to search */
2565 dht_replication_level, /* replication level */
2566 GNUNET_DHT_RO_RECORD_ROUTE |
2567 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2568 NULL, /* xquery */ // FIXME BLOOMFILTER
2569 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2570 &dht_get_id_handler, path_info);
2572 /* Otherwise, there is no path but the DHT get is already started. */
2577 * Task to delay the connection of a peer
2579 * @param cls Closure (path info with tunnel and peer to connect).
2580 * Will be free'd on exection.
2581 * @param tc TaskContext
2584 peer_info_connect_task (void *cls,
2585 const struct GNUNET_SCHEDULER_TaskContext *tc)
2587 struct MeshPathInfo *path_info = cls;
2589 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2594 peer_info_connect (path_info->peer, path_info->t);
2600 * Destroy the peer_info and free any allocated resources linked to it
2602 * @param pi The peer_info to destroy.
2604 * @return GNUNET_OK on success
2607 peer_info_destroy (struct MeshPeerInfo *pi)
2609 struct GNUNET_PeerIdentity id;
2610 struct MeshPeerPath *p;
2611 struct MeshPeerPath *nextp;
2613 GNUNET_PEER_resolve (pi->id, &id);
2614 GNUNET_PEER_change_rc (pi->id, -1);
2617 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2620 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2621 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2623 if (NULL != pi->dhtget)
2625 GNUNET_DHT_get_stop (pi->dhtget);
2626 GNUNET_free (pi->dhtgetcls);
2632 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2642 * Remove all paths that rely on a direct connection between p1 and p2
2643 * from the peer itself and notify all tunnels about it.
2645 * @param peer PeerInfo of affected peer.
2646 * @param p1 GNUNET_PEER_Id of one peer.
2647 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2650 * TODO: optimize (see below)
2653 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2656 struct MeshPeerPath *p;
2657 struct MeshPeerPath *aux;
2658 struct MeshPeerInfo *peer_d;
2660 unsigned int destroyed;
2665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2667 p = peer->path_head;
2671 for (i = 0; i < (p->length - 1); i++)
2673 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2674 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2676 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2687 for (i = 0; i < peer->ntunnels; i++)
2689 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2693 * Problem: one or more peers have been deleted from the tunnel tree.
2694 * We don't know who they are to try to add them again.
2695 * We need to try to find a new path for each of the disconnected peers.
2696 * Some of them might already have a path to reach them that does not
2697 * involve p1 and p2. Adding all anew might render in a better tree than
2698 * the trivial immediate fix.
2700 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2701 * its children will be reachable trough him.
2703 peer_d = peer_info_get_short (d);
2706 for (p = peer_d->path_head; NULL != p; p = p->next)
2708 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2716 /* No callback, as peer will be already disconnected and a connection
2717 * scheduled by tunnel_notify_connection_broken.
2719 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2723 peer_info_connect (peer_d, peer->tunnels[i]);
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2731 * Add the path to the peer and update the path used to reach it in case this
2734 * @param peer_info Destination peer to add the path to.
2735 * @param path New path to add. Last peer must be the peer in arg 1.
2736 * Path will be either used of freed if already known.
2737 * @param trusted Do we trust that this path is real?
2740 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2743 struct MeshPeerPath *aux;
2747 if ((NULL == peer_info) || (NULL == path))
2750 path_destroy (path);
2753 if (path->peers[path->length - 1] != peer_info->id)
2756 path_destroy (path);
2759 if (path->length <= 2 && GNUNET_NO == trusted)
2761 /* Only allow CORE to tell us about direct paths */
2762 path_destroy (path);
2765 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2766 for (l = 1; l < path->length; l++)
2768 if (path->peers[l] == myid)
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2771 for (l2 = 0; l2 < path->length - l; l2++)
2773 path->peers[l2] = path->peers[l + l2];
2778 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2783 struct GNUNET_PeerIdentity id;
2785 GNUNET_PEER_resolve (peer_info->id, &id);
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2787 path->length, GNUNET_i2s (&id));
2790 l = path_get_length (path);
2797 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2798 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2800 l2 = path_get_length (aux);
2803 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2804 peer_info->path_tail, aux, path);
2809 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2811 path_destroy (path);
2816 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2823 * Add the path to the origin peer and update the path used to reach it in case
2824 * this is the shortest.
2825 * The path is given in peer_info -> destination, therefore we turn the path
2826 * upside down first.
2828 * @param peer_info Peer to add the path to, being the origin of the path.
2829 * @param path New path to add after being inversed.
2830 * @param trusted Do we trust that this path is real?
2833 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2834 struct MeshPeerPath *path, int trusted)
2837 peer_info_add_path (peer_info, path, trusted);
2842 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2843 * to obtain a local peer -> destination path and interning the peer ids.
2845 * @return Newly allocated and created path
2847 static struct MeshPeerPath *
2848 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2849 unsigned int get_path_length,
2850 const struct GNUNET_PeerIdentity *put_path,
2851 unsigned int put_path_length)
2853 struct MeshPeerPath *p;
2859 GNUNET_PEER_change_rc (myid, 1);
2860 i = get_path_length;
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2862 for (i--; i >= 0; i--)
2864 id = GNUNET_PEER_intern (&get_path[i]);
2865 if (p->length > 0 && id == p->peers[p->length - 1])
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2868 GNUNET_PEER_change_rc (id, -1);
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2873 GNUNET_i2s (&get_path[i]));
2875 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2876 p->peers[p->length - 1] = id;
2879 i = put_path_length;
2880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2881 for (i--; i >= 0; i--)
2883 id = GNUNET_PEER_intern (&put_path[i]);
2886 /* PUT path went through us, so discard the path up until now and start
2887 * from here to get a much shorter (and loop-free) path.
2892 if (p->length > 0 && id == p->peers[p->length - 1])
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2895 GNUNET_PEER_change_rc (id, -1);
2899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2900 GNUNET_i2s (&put_path[i]));
2902 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2903 p->peers[p->length - 1] = id;
2907 if (get_path_length > 0)
2908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2909 GNUNET_i2s (&get_path[0]));
2910 if (put_path_length > 0)
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2912 GNUNET_i2s (&put_path[0]));
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2915 for (i = 0; i < p->length; i++)
2917 struct GNUNET_PeerIdentity peer_id;
2919 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2921 GNUNET_i2s (&peer_id));
2929 * Adds a path to the peer_infos of all the peers in the path
2931 * @param p Path to process.
2932 * @param confirmed Whether we know if the path works or not.
2935 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2939 /* TODO: invert and add */
2940 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2941 for (i++; i < p->length; i++)
2943 struct MeshPeerInfo *aux;
2944 struct MeshPeerPath *copy;
2946 aux = peer_info_get_short (p->peers[i]);
2947 copy = path_duplicate (p);
2948 copy->length = i + 1;
2949 peer_info_add_path (aux, copy, GNUNET_NO);
2955 * Send keepalive packets for a peer
2957 * @param cls Closure (tunnel for which to send the keepalive).
2958 * @param tc Notification context.
2960 * TODO: implement explicit multicast keepalive?
2963 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2967 * Search for a tunnel among the incoming tunnels
2969 * @param tid the local id of the tunnel
2971 * @return tunnel handler, NULL if doesn't exist
2973 static struct MeshTunnel *
2974 tunnel_get_incoming (MESH_TunnelNumber tid)
2976 struct GNUNET_HashCode hash;
2978 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2979 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2980 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2985 * Search for a tunnel among the tunnels for a client
2987 * @param c the client whose tunnels to search in
2988 * @param tid the local id of the tunnel
2990 * @return tunnel handler, NULL if doesn't exist
2992 static struct MeshTunnel *
2993 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2995 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2997 return tunnel_get_incoming (tid);
3001 struct GNUNET_HashCode hash;
3003 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3004 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3010 * Search for a tunnel by global ID using PEER_ID
3012 * @param pi owner of the tunnel
3013 * @param tid global tunnel number
3015 * @return tunnel handler, NULL if doesn't exist
3017 static struct MeshTunnel *
3018 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3020 struct MESH_TunnelID id;
3021 struct GNUNET_HashCode hash;
3026 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3027 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3032 * Search for a tunnel by global ID using full PeerIdentities
3034 * @param oid owner of the tunnel
3035 * @param tid global tunnel number
3037 * @return tunnel handler, NULL if doesn't exist
3039 static struct MeshTunnel *
3040 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3042 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3047 * Delete an active client from the tunnel.
3053 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3057 for (i = 0; i < t->nclients; i++)
3059 if (t->clients[i] == c)
3061 t->clients[i] = t->clients[t->nclients - 1];
3062 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3063 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3065 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3073 * Delete an ignored client from the tunnel.
3079 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3083 for (i = 0; i < t->nignore; i++)
3085 if (t->ignore[i] == c)
3087 t->ignore[i] = t->ignore[t->nignore - 1];
3088 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3096 * Delete a client from the tunnel. It should be only done on
3097 * client disconnection, otherwise use client_ignore_tunnel.
3103 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3105 tunnel_delete_ignored_client (t, c);
3106 tunnel_delete_active_client (t, c);
3111 * Iterator to free MeshTunnelChildInfo of tunnel children.
3113 * @param cls Closure (tunnel info).
3114 * @param key Hash of GNUNET_PEER_Id (unused).
3115 * @param value MeshTunnelChildInfo of the child.
3117 * @return always GNUNET_YES, to keep iterating
3120 tunnel_destroy_child (void *cls,
3121 const struct GNUNET_HashCode * key,
3124 GNUNET_free (value);
3130 * Callback used to notify a client owner of a tunnel that a peer has
3131 * disconnected, most likely because of a path change.
3133 * @param cls Closure (tunnel this notification is about).
3134 * @param peer_id Short ID of disconnected peer.
3137 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3139 struct MeshTunnel *t = cls;
3140 struct MeshPeerInfo *peer;
3141 struct MeshPathInfo *path_info;
3143 if (NULL != t->owner && NULL != nc)
3145 struct GNUNET_MESH_PeerControl msg;
3147 msg.header.size = htons (sizeof (msg));
3148 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3149 msg.tunnel_id = htonl (t->local_tid);
3150 GNUNET_PEER_resolve (peer_id, &msg.peer);
3151 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3152 &msg.header, GNUNET_NO);
3154 peer = peer_info_get_short (peer_id);
3155 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3156 path_info->peer = peer;
3158 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3163 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3164 * needed rescources.
3165 * If peer already exists, reevaluate shortest path and change if different.
3167 * @param t Tunnel we want to add a new peer to
3168 * @param peer PeerInfo of the peer being added
3172 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3174 struct GNUNET_PeerIdentity id;
3175 struct MeshPeerPath *best_p;
3176 struct MeshPeerPath *p;
3177 unsigned int best_cost;
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3181 GNUNET_PEER_resolve (peer->id, &id);
3183 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3186 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3187 GNUNET_assert (GNUNET_OK ==
3188 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3190 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3193 if (NULL != (p = peer->path_head))
3196 best_cost = tree_get_path_cost (t->tree, p);
3199 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3206 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3207 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3208 t->path_refresh_task =
3209 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3213 /* Start a DHT get */
3214 peer_info_connect (peer, t);
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3220 * Add a path to a tunnel which we don't own, just to remember the next hop.
3221 * If destination node was already in the tunnel, the first hop information
3222 * will be replaced with the new path.
3224 * @param t Tunnel we want to add a new peer to
3225 * @param p Path to add
3226 * @param own_pos Position of local node in path.
3230 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3231 unsigned int own_pos)
3233 struct GNUNET_PeerIdentity id;
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3236 GNUNET_assert (0 != own_pos);
3237 tree_add_path (t->tree, p, NULL, NULL);
3238 if (own_pos < p->length - 1)
3240 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3241 tree_update_first_hops (t->tree, myid, &id);
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3247 * Add a client to a tunnel, initializing all needed data structures.
3249 * @param t Tunnel to which add the client.
3250 * @param c Client which to add to the tunnel.
3253 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3255 struct MeshTunnelClientInfo clinfo;
3257 GNUNET_array_append (t->clients, t->nclients, c);
3258 clinfo.fwd_ack = t->fwd_pid + 1;
3259 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3260 clinfo.fwd_pid = t->fwd_pid;
3262 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3267 * Notifies a tunnel that a connection has broken that affects at least
3268 * some of its peers. Sends a notification towards the root of the tree.
3269 * In case the peer is the owner of the tree, notifies the client that owns
3270 * the tunnel and tries to reconnect.
3272 * @param t Tunnel affected.
3273 * @param p1 Peer that got disconnected from p2.
3274 * @param p2 Peer that got disconnected from p1.
3276 * @return Short ID of the peer disconnected (either p1 or p2).
3277 * 0 if the tunnel remained unaffected.
3279 static GNUNET_PEER_Id
3280 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3286 tree_notify_connection_broken (t->tree, p1, p2,
3287 &tunnel_notify_client_peer_disconnected,
3289 if (myid != p1 && myid != p2)
3295 if (tree_get_predecessor (t->tree) != 0)
3297 /* We are the peer still connected, notify owner of the disconnection. */
3298 struct GNUNET_MESH_PathBroken msg;
3299 struct GNUNET_PeerIdentity neighbor;
3301 msg.header.size = htons (sizeof (msg));
3302 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3303 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3304 msg.tid = htonl (t->id.tid);
3305 msg.peer1 = my_full_id;
3306 GNUNET_PEER_resolve (pid, &msg.peer2);
3307 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3308 send_message (&msg.header, &neighbor, t);
3316 * Send a multicast packet to a neighbor.
3318 * @param cls Closure (Info about the multicast packet)
3319 * @param neighbor_id Short ID of the neighbor to send the packet to.
3322 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3324 struct MeshData *mdata = cls;
3325 struct MeshTransmissionDescriptor *info;
3326 struct GNUNET_PeerIdentity neighbor;
3328 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3330 info->mesh_data = mdata;
3331 (*(mdata->reference_counter)) ++;
3332 info->destination = neighbor_id;
3333 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3335 GNUNET_i2s (&neighbor));
3336 info->peer = peer_info_get (&neighbor);
3337 GNUNET_assert (NULL != info->peer);
3339 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3340 info->mesh_data->data_len,
3347 * Send a message in a tunnel in multicast, sending a copy to each child node
3348 * down the local one in the tunnel tree.
3350 * @param t Tunnel in which to send the data.
3351 * @param msg Message to be sent.
3352 * @param internal Has the service generated this message?
3355 tunnel_send_multicast (struct MeshTunnel *t,
3356 const struct GNUNET_MessageHeader *msg,
3359 struct MeshData *mdata;
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3362 " sending a multicast packet...\n");
3363 mdata = GNUNET_malloc (sizeof (struct MeshData));
3364 mdata->data_len = ntohs (msg->size);
3365 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3367 mdata->data = GNUNET_malloc (mdata->data_len);
3368 memcpy (mdata->data, msg, mdata->data_len);
3369 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3371 struct GNUNET_MESH_Multicast *mcast;
3373 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3374 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3376 ntohl (mcast->ttl));
3380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3382 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3383 && GNUNET_NO == internal)
3385 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3387 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3393 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3394 if (*(mdata->reference_counter) == 0)
3396 GNUNET_free (mdata->data);
3397 GNUNET_free (mdata->reference_counter);
3398 if (NULL != mdata->task)
3400 GNUNET_SCHEDULER_cancel(*(mdata->task));
3401 GNUNET_free (mdata->task);
3402 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3404 // FIXME change order?
3405 GNUNET_free (mdata);
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3408 " sending a multicast packet done\n");
3414 * Increase the SKIP value of all peers that
3415 * have not received a unicast message.
3417 * @param cls Closure (ID of the peer that HAS received the message).
3418 * @param key ID of the neighbor.
3419 * @param value Information about the neighbor.
3421 * @return GNUNET_YES to keep iterating.
3424 tunnel_add_skip (void *cls,
3425 const struct GNUNET_HashCode * key,
3428 struct GNUNET_PeerIdentity *neighbor = cls;
3429 struct MeshTunnelChildInfo *cinfo = value;
3431 /* TODO compare only pointers? key == neighbor? */
3432 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3442 * @brief Get neighbor's Flow Control information.
3444 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3445 * descendant of the local node in a certain tunnel.
3446 * If the info is not yet there (recently created path), creates the data struct
3447 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3450 * @param t Tunnel related.
3451 * @param peer Neighbor whose Flow Control info is needed.
3453 * @return Neighbor's Flow Control info.
3455 static struct MeshTunnelChildInfo *
3456 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3457 const struct GNUNET_PeerIdentity *peer)
3459 struct MeshTunnelChildInfo *cinfo;
3460 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3466 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3467 cinfo->id = GNUNET_PEER_intern (peer);
3468 cinfo->skip = t->fwd_pid;
3470 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3471 cinfo->fwd_ack = t->fwd_pid + delta;
3472 cinfo->bck_ack = delta;
3474 GNUNET_assert (GNUNET_OK ==
3475 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3478 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3485 * Iterator to get the appropiate ACK value from all children nodes.
3487 * @param cls Closue (tunnel).
3488 * @param id Id of the child node.
3491 tunnel_get_child_fwd_ack (void *cls,
3494 struct GNUNET_PeerIdentity peer_id;
3495 struct MeshTunnelChildInfo *cinfo;
3496 struct MeshTunnelChildIteratorContext *ctx = cls;
3497 struct MeshTunnel *t = ctx->t;
3500 GNUNET_PEER_resolve (id, &peer_id);
3501 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3502 ack = cinfo->fwd_ack;
3504 if (GNUNET_NO == ctx->init)
3506 ctx->max_child_ack = ack;
3507 ctx->init = GNUNET_YES;
3510 if (GNUNET_YES == t->speed_min)
3512 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3516 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3523 * Get the maximum PID allowed to transmit to any
3524 * tunnel child of the local peer, depending on the tunnel
3525 * buffering/speed settings.
3529 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3532 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3534 struct MeshTunnelChildIteratorContext ctx;
3536 ctx.max_child_ack = 0;
3538 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3540 if (0 == ctx.nchildren)
3543 if (GNUNET_YES == t->nobuffer && is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3544 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3546 return (int64_t) ctx.max_child_ack;
3551 * Set the FWD ACK value of a client in a particular tunnel.
3553 * @param t Tunnel affected.
3554 * @param c Client whose ACK to set.
3555 * @param ack ACK value.
3558 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3559 struct MeshClient *c,
3564 for (i = 0; i < t->nclients; i++)
3566 if (t->clients[i] != c)
3568 t->clients_fc[i].fwd_ack = ack;
3576 * Get the ACK value of a client in a particular tunnel.
3578 * @param t Tunnel on which to look.
3579 * @param c Client whose ACK to get.
3581 * @return ACK value.
3583 uint32_t // FIXME fc Is this funcion needed anymore?
3584 tunnel_get_client_fwd_ack (struct MeshTunnel *t,
3585 struct MeshClient *c)
3589 for (i = 0; i < t->nclients; i++)
3591 if (t->clients[i] != c)
3593 return t->clients_fc[i].fwd_ack;
3601 * Get the highest ACK value of all clients in a particular tunnel,
3602 * according to the buffering/speed settings.
3604 * @param t Tunnel on which to look.
3606 * @return Corresponding ACK value (max uint32_t).
3607 * If no clients are suscribed, -1.
3610 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3615 if (0 == t->nclients)
3618 for (ack = -1, i = 0; i < t->nclients; i++)
3621 (GNUNET_YES == t->speed_min &&
3622 GNUNET_YES == is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3623 (GNUNET_NO == t->speed_min &&
3624 GNUNET_YES == is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3626 ack = t->clients_fc[i].fwd_ack;
3630 if (GNUNET_YES == t->nobuffer && is_pid_bigger(ack, t->fwd_pid))
3631 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3633 return (uint32_t) ack;
3638 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3639 * mode and the status of all children nodes.
3643 * @return Maximum PID allowed.
3646 tunnel_get_fwd_ack (struct MeshTunnel *t)
3649 uint32_t buffer_free;
3654 count = t->fwd_pid - t->skip;
3655 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3656 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3657 child_ack = tunnel_get_children_fwd_ack (t);
3658 client_ack = tunnel_get_clients_fwd_ack (t);
3659 if (-1 == child_ack)
3661 // Node has no children, child_ack AND core buffer are irrelevant.
3662 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good!
3663 return (uint32_t) client_ack;
3666 if (GNUNET_YES == t->speed_min)
3668 ack = min_pid ((uint32_t) child_ack, ack);
3669 ack = min_pid ((uint32_t) client_ack, ack);
3673 ack = max_pid ((uint32_t) child_ack, ack);
3674 ack = max_pid ((uint32_t) client_ack, ack);
3676 if (GNUNET_YES == t->nobuffer && is_pid_bigger(ack, t->fwd_pid))
3677 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3679 count, buffer_free, child_ack, client_ack, ack);
3684 * Get the current bck ack value for a tunnel, taking in account the tunnel
3685 * mode and the status of all children nodes.
3689 * @return Maximum PID allowed.
3692 tunnel_get_bck_ack (struct MeshTunnel *t)
3696 if (GNUNET_YES == t->nobuffer)
3698 if (t->bck_ack > t->bck_pid)
3700 return t->bck_pid + 1;
3707 ack = t->bck_pid + t->bck_queue_max - t->bck_queue_n;
3714 send_local_ack (struct MeshClient *c, struct MeshTunnel *t, uint32_t ack)
3716 struct GNUNET_MESH_LocalAck msg;
3718 msg.header.size = htons (sizeof (msg));
3719 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3720 msg.tunnel_id = htonl (t->local_tid_dest);
3721 msg.max_pid = htonl (ack);
3722 GNUNET_SERVER_notification_context_unicast(nc,
3729 * Build an ACK message and send it to the given peer.
3732 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3734 struct GNUNET_MESH_ACK msg;
3736 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3737 msg.header.size = htons (sizeof (msg));
3738 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3739 msg.pid = htonl (ack);
3740 msg.tid = htonl (t->id.tid);
3742 send_message (&msg.header, peer, t);
3747 * Send an ACK informing the predecessor about the available buffer space.
3748 * In case there is no predecessor, inform the owning client.
3749 * If buffering is off, send only on behalf of children or self if endpoint.
3750 * If buffering is on, send when sent to children and buffer space is free.
3751 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3752 * the ACK itself goes "back" (towards root).
3754 * @param t Tunnel on which to send the ACK.
3755 * @param type Type of message that triggered the ACK transmission.
3758 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3760 struct GNUNET_PeerIdentity id;
3763 if (NULL != t->owner)
3765 send_client_tunnel_ack (t->owner, t);
3768 /* Is it after unicast / multicast retransmission? */
3771 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3772 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3774 "ACK due to FWD DATA retransmission\n");
3775 if (GNUNET_YES == t->nobuffer)
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3780 if (t->fwd_queue_max > t->fwd_queue_n * 2)
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3786 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3787 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3793 /* Ok, ACK might be necessary, what PID to ACK? */
3794 ack = tunnel_get_fwd_ack (t);
3796 /* If speed_min and not all children have ack'd, dont send yet */
3797 if (ack == t->last_fwd_ack)
3799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3803 t->last_fwd_ack = ack;
3804 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3805 send_ack (t, &id, ack);
3810 * Iterator to send a child node a BCK ACK to allow him to send more
3813 * @param cls Closure (tunnel).
3814 * @param id Id of the child node.
3817 tunnel_send_child_bck_ack (void *cls,
3820 struct MeshTunnel *t = cls;
3821 struct MeshTunnelChildInfo *cinfo;
3822 struct GNUNET_PeerIdentity peer;
3824 GNUNET_PEER_resolve (id, &peer);
3825 cinfo = tunnel_get_neighbor_fc (t, &peer);
3827 if (cinfo->bck_ack != cinfo->pid &&
3828 GNUNET_NO == is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3832 send_ack (t, &peer, cinfo->bck_ack);
3837 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3839 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3841 * @param t Tunnel on which to send the BCK ACKs.
3844 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3848 /* Find client whom to allow to send to origin (with lowest buffer space) */
3849 for (i = 0; i < t->nclients; i++)
3851 struct MeshTunnelClientInfo *clinfo;
3854 clinfo = &t->clients_fc[i];
3855 delta = clinfo->bck_ack - clinfo->bck_pid;
3857 if ((GNUNET_NO == t->nobuffer && ACK_THRESHOLD > delta) ||
3858 (GNUNET_YES == t->nobuffer && 0 == delta))
3862 ack = clinfo->bck_pid;
3863 ack += t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3864 send_local_ack(t->clients[i], t, ack);
3865 clinfo->bck_ack = ack;
3872 * Send an ACK informing the children nodes and destination clients about
3873 * the available buffer space.
3874 * If buffering is off, send only on behalf of root (can be self).
3875 * If buffering is on, send when sent to predecessor and buffer space is free.
3876 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3877 * the ACK itself goes "forward" (towards children/clients).
3879 * @param t Tunnel on which to send the ACK.
3880 * @param type Type of message that triggered the ACK transmission.
3883 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3885 if (NULL != t->owner)
3887 send_client_tunnel_ack (t->owner, t);
3890 /* Is it after data to_origin retransmission? */
3893 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3895 "ACK due to BCK DATA retransmission\n");
3896 if (GNUNET_YES == t->nobuffer)
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3901 if (t->bck_queue_max > t->bck_queue_n * 2)
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3907 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3908 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3914 tunnel_send_clients_bck_ack (t);
3915 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
3920 * Send a message to all peers in this tunnel that the tunnel is no longer
3923 * @param t The tunnel whose peers to notify.
3926 tunnel_send_destroy (struct MeshTunnel *t)
3928 struct GNUNET_MESH_TunnelDestroy msg;
3930 msg.header.size = htons (sizeof (msg));
3931 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3932 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3933 msg.tid = htonl (t->id.tid);
3934 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3939 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3941 * @param cls Closure (Tunnel which to cancel).
3942 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3945 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3947 struct MeshTunnel *t = cls;
3948 struct MeshPeerInfo *peer_info;
3949 struct MeshPeerQueue *pq;
3950 struct MeshPeerQueue *next;
3952 peer_info = peer_info_get_short (neighbor_id);
3953 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3956 if (pq->tunnel == t)
3958 queue_destroy (pq, GNUNET_YES);
3961 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3963 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3964 peer_info->core_transmit = NULL;
3969 * Destroy the tunnel and free any allocated resources linked to it.
3971 * @param t the tunnel to destroy
3973 * @return GNUNET_OK on success
3976 tunnel_destroy (struct MeshTunnel *t)
3978 struct MeshClient *c;
3979 struct GNUNET_HashCode hash;
3986 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3992 struct GNUNET_PeerIdentity id;
3994 GNUNET_PEER_resolve (t->id.oid, &id);
3995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3996 GNUNET_i2s (&id), t->id.tid);
3998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4002 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4003 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4008 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4011 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4015 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4016 for (i = 0; i < t->nclients; i++)
4020 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4025 for (i = 0; i < t->nignore; i++)
4029 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4034 if (t->nclients > 0)
4037 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4041 GNUNET_free (t->clients);
4043 if (NULL != t->peers)
4045 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4047 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4050 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4051 &tunnel_destroy_child,
4053 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4055 tree_destroy (t->tree);
4057 if (NULL != t->regex_ctx)
4058 regex_cancel_search (t->regex_ctx);
4059 if (NULL != t->dht_get_type)
4060 GNUNET_DHT_get_stop (t->dht_get_type);
4061 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4062 GNUNET_SCHEDULER_cancel (t->timeout_task);
4063 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4064 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4067 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4068 GNUNET_assert (0 <= n_tunnels);
4075 * Create a new tunnel
4077 * @param owner Who is the owner of the tunnel (short ID).
4078 * @param tid Tunnel Number of the tunnel.
4079 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4080 * @param local Tunnel Number for the tunnel, for the client point of view.
4082 * @return A new initialized tunnel. NULL on error.
4084 static struct MeshTunnel *
4085 tunnel_new (GNUNET_PEER_Id owner,
4086 MESH_TunnelNumber tid,
4087 struct MeshClient *client,
4088 MESH_TunnelNumber local)
4090 struct MeshTunnel *t;
4091 struct GNUNET_HashCode hash;
4093 if (n_tunnels >= max_tunnels && NULL == client)
4096 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4099 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4100 t->bck_queue_max = t->fwd_queue_max;
4101 t->tree = tree_new (owner);
4103 t->bck_ack = INITIAL_WINDOW_SIZE;
4104 t->last_fwd_ack = INITIAL_WINDOW_SIZE;
4105 t->local_tid = local;
4106 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4108 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4110 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4112 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4113 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4120 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4127 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4129 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4130 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4134 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4144 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4145 * that are no longer needed, as well as nodes of no longer reachable peers.
4146 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4148 * @param t Tunnel from which to remove the path.
4149 * @param peer Short id of the peer which should be removed.
4152 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4154 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4160 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4161 * client when the client disconnects. If the client is not the owner, the
4162 * owner will get notified if no more clients are in the tunnel and the client
4163 * get removed from the tunnel's list.
4165 * @param cls closure (client that is disconnecting)
4166 * @param key the hash of the local tunnel id (used to access the hashmap)
4167 * @param value the value stored at the key (tunnel to destroy)
4169 * @return GNUNET_OK on success
4172 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4174 struct MeshTunnel *t = value;
4175 struct MeshClient *c = cls;
4178 send_client_tunnel_disconnect(t, c);
4181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4182 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4183 tunnel_delete_client(t, c);
4184 client_delete_tunnel(c, t);
4187 tunnel_send_destroy(t);
4188 r = tunnel_destroy (t);
4194 * Timeout function, destroys tunnel if called
4196 * @param cls Closure (tunnel to destroy).
4197 * @param tc TaskContext
4200 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4202 struct MeshTunnel *t = cls;
4204 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4206 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4211 * Resets the tunnel timeout. Starts it if no timeout was running.
4213 * @param t Tunnel whose timeout to reset.
4215 * TODO use heap to improve efficiency of scheduler.
4218 tunnel_reset_timeout (struct MeshTunnel *t)
4220 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4221 GNUNET_SCHEDULER_cancel (t->timeout_task);
4223 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4224 (refresh_path_time, 4), &tunnel_timeout, t);
4228 /******************************************************************************/
4229 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4230 /******************************************************************************/
4233 * Function to send a create path packet to a peer.
4235 * @param cls closure
4236 * @param size number of bytes available in buf
4237 * @param buf where the callee should write the message
4238 * @return number of bytes written to buf
4241 send_core_path_create (void *cls, size_t size, void *buf)
4243 struct MeshPathInfo *info = cls;
4244 struct GNUNET_MESH_ManipulatePath *msg;
4245 struct GNUNET_PeerIdentity *peer_ptr;
4246 struct MeshTunnel *t = info->t;
4247 struct MeshPeerPath *p = info->path;
4252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4254 sizeof (struct GNUNET_MESH_ManipulatePath) +
4255 p->length * sizeof (struct GNUNET_PeerIdentity);
4257 if (size < size_needed || NULL == buf)
4262 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4263 msg->header.size = htons (size_needed);
4264 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4265 msg->tid = ntohl (t->id.tid);
4267 if (GNUNET_YES == t->speed_min)
4268 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4269 if (GNUNET_YES == t->nobuffer)
4270 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4271 msg->opt = htonl(opt);
4273 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4274 for (i = 0; i < p->length; i++)
4276 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4283 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4289 * Fill the core buffer
4291 * @param cls closure (data itself)
4292 * @param size number of bytes available in buf
4293 * @param buf where the callee should write the message
4295 * @return number of bytes written to buf
4298 send_core_data_multicast (void *cls, size_t size, void *buf)
4300 struct MeshTransmissionDescriptor *info = cls;
4303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4304 GNUNET_assert (NULL != info);
4305 GNUNET_assert (NULL != info->peer);
4306 total_size = info->mesh_data->data_len;
4307 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4309 if (total_size > size)
4314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4315 memcpy (buf, info->mesh_data->data, total_size);
4318 struct GNUNET_MESH_Multicast *mc;
4319 struct GNUNET_MessageHeader *mh;
4322 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4324 mc = (struct GNUNET_MESH_Multicast *) mh;
4325 mh = (struct GNUNET_MessageHeader *) &mc[1];
4326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4327 " multicast, payload type %u\n", ntohs (mh->type));
4328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4329 " multicast, payload size %u\n", ntohs (mh->size));
4333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %u\n",
4338 data_descriptor_decrement_rc (info->mesh_data);
4339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4347 * Creates a path ack message in buf and frees all unused resources.
4349 * @param cls closure (MeshTransmissionDescriptor)
4350 * @param size number of bytes available in buf
4351 * @param buf where the callee should write the message
4352 * @return number of bytes written to buf
4355 send_core_path_ack (void *cls, size_t size, void *buf)
4357 struct MeshTransmissionDescriptor *info = cls;
4358 struct GNUNET_MESH_PathACK *msg = buf;
4360 GNUNET_assert (NULL != info);
4361 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4366 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4367 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4368 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4369 msg->tid = htonl (info->origin->tid);
4370 msg->peer_id = my_full_id;
4373 /* TODO add signature */
4375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4376 return sizeof (struct GNUNET_MESH_PathACK);
4381 * Free a transmission that was already queued with all resources
4382 * associated to the request.
4384 * @param queue Queue handler to cancel.
4385 * @param clear_cls Is it necessary to free associated cls?
4388 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4390 struct MeshTransmissionDescriptor *dd;
4391 struct MeshPathInfo *path_info;
4393 if (GNUNET_YES == clear_cls)
4395 switch (queue->type)
4397 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4398 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4399 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4402 data_descriptor_decrement_rc (dd->mesh_data);
4404 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4406 path_info = queue->cls;
4407 path_destroy (path_info->path);
4411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4413 GNUNET_free_non_null (queue->cls);
4415 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4416 queue->peer->queue_tail,
4418 GNUNET_free (queue);
4423 * Core callback to write a queued packet to core buffer
4425 * @param cls Closure (peer info).
4426 * @param size Number of bytes available in buf.
4427 * @param buf Where the to write the message.
4429 * @return number of bytes written to buf
4432 queue_send (void *cls, size_t size, void *buf)
4434 struct MeshPeerInfo *peer = cls;
4435 struct GNUNET_MessageHeader *msg;
4436 struct MeshPeerQueue *queue;
4437 struct MeshTunnel *t;
4440 peer->core_transmit = NULL;
4441 queue = peer->queue_head;
4443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4445 /* If queue is empty, send should have been cancelled */
4451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4453 /* Check if buffer size is enough for the message */
4454 if (queue->size > size)
4456 struct GNUNET_PeerIdentity id;
4458 GNUNET_PEER_resolve (peer->id, &id);
4459 peer->core_transmit =
4460 GNUNET_CORE_notify_transmit_ready(core_handle,
4463 GNUNET_TIME_UNIT_FOREVER_REL,
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4473 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast: %u\n");
4478 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4485 switch (queue->type)
4488 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4489 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4490 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4492 "********* raw: %u\n",
4495 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4496 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4497 data_size = send_core_data_raw (queue->cls, size, buf);
4498 msg = (struct GNUNET_MessageHeader *) buf;
4499 switch (ntohs (msg->type)) // Type of preconstructed message
4501 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4502 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4504 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4505 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4511 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4513 data_size = send_core_data_multicast(queue->cls, size, buf);
4514 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4516 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4518 data_size = send_core_path_create(queue->cls, size, buf);
4520 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4522 data_size = send_core_path_ack(queue->cls, size, buf);
4526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4527 "********* type unknown: %u\n",
4532 /* Free queue, but cls was freed by send_core_* */
4533 queue_destroy (queue, GNUNET_NO);
4535 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4541 /* If more data in queue, send next */
4542 if (NULL != peer->queue_head)
4544 struct GNUNET_PeerIdentity id;
4546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4547 GNUNET_PEER_resolve (peer->id, &id);
4548 peer->core_transmit =
4549 GNUNET_CORE_notify_transmit_ready(core_handle,
4552 GNUNET_TIME_UNIT_FOREVER_REL,
4554 peer->queue_head->size,
4558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4564 * Queue and pass message to core when possible.
4566 * @param cls Closure (type dependant).
4567 * @param type Type of the message, 0 for a raw message.
4568 * @param size Size of the message.
4569 * @param dst Neighbor to send message to.
4570 * @param t Tunnel this message belongs to.
4573 queue_add (void *cls, uint16_t type, size_t size,
4574 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4576 struct MeshPeerQueue *queue;
4581 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4582 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4584 n = &t->fwd_queue_n;
4585 max = &t->fwd_queue_max;
4587 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4589 n = &t->bck_queue_n;
4590 max = &t->bck_queue_max;
4595 if (NULL == t->owner)
4596 GNUNET_break_op(0); // TODO: kill connection?
4599 return; // Drop message
4603 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4609 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4610 if (NULL == dst->core_transmit)
4612 struct GNUNET_PeerIdentity id;
4614 GNUNET_PEER_resolve (dst->id, &id);
4615 dst->core_transmit =
4616 GNUNET_CORE_notify_transmit_ready(core_handle,
4619 GNUNET_TIME_UNIT_FOREVER_REL,
4628 /******************************************************************************/
4629 /******************** MESH NETWORK HANDLERS **************************/
4630 /******************************************************************************/
4634 * Core handler for path creation
4636 * @param cls closure
4637 * @param message message
4638 * @param peer peer identity this notification is about
4639 * @param atsi performance data
4640 * @param atsi_count number of records in 'atsi'
4642 * @return GNUNET_OK to keep the connection open,
4643 * GNUNET_SYSERR to close it (signal serious error)
4646 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4647 const struct GNUNET_MessageHeader *message,
4648 const struct GNUNET_ATS_Information *atsi,
4649 unsigned int atsi_count)
4651 unsigned int own_pos;
4654 MESH_TunnelNumber tid;
4655 struct GNUNET_MESH_ManipulatePath *msg;
4656 struct GNUNET_PeerIdentity *pi;
4657 struct GNUNET_HashCode hash;
4658 struct MeshPeerPath *path;
4659 struct MeshPeerInfo *dest_peer_info;
4660 struct MeshPeerInfo *orig_peer_info;
4661 struct MeshTunnel *t;
4663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4664 "Received a path create msg [%s]\n",
4665 GNUNET_i2s (&my_full_id));
4666 size = ntohs (message->size);
4667 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4669 GNUNET_break_op (0);
4673 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4674 if (size % sizeof (struct GNUNET_PeerIdentity))
4676 GNUNET_break_op (0);
4679 size /= sizeof (struct GNUNET_PeerIdentity);
4682 GNUNET_break_op (0);
4685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4686 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4688 tid = ntohl (msg->tid);
4689 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4691 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4692 t = tunnel_get (pi, tid);
4693 if (NULL == t) // FIXME only for INCOMING tunnels?
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4698 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4701 // FIXME notify failure
4704 opt = ntohl (msg->opt);
4705 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4706 GNUNET_YES : GNUNET_NO;
4707 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4708 GNUNET_YES : GNUNET_NO;
4710 if (GNUNET_YES == t->nobuffer)
4712 t->bck_queue_max = 1;
4713 t->fwd_queue_max = 1;
4716 // FIXME only assign a local tid if a local client is interested (on demand)
4717 while (NULL != tunnel_get_incoming (next_local_tid))
4718 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4719 t->local_tid_dest = next_local_tid++;
4720 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4723 tunnel_reset_timeout (t);
4724 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4726 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4727 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4735 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4736 if (NULL == dest_peer_info)
4738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4739 " Creating PeerInfo for destination.\n");
4740 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4741 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4742 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4744 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4746 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4747 if (NULL == orig_peer_info)
4749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4750 " Creating PeerInfo for origin.\n");
4751 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4752 orig_peer_info->id = GNUNET_PEER_intern (pi);
4753 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4754 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4757 path = path_new (size);
4759 for (i = 0; i < size; i++)
4761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4762 GNUNET_i2s (&pi[i]));
4763 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4764 if (path->peers[i] == myid)
4767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4770 /* cannot be self, must be 'not found' */
4771 /* create path: self not found in path through self */
4772 GNUNET_break_op (0);
4773 path_destroy (path);
4777 path_add_to_peers (path, GNUNET_NO);
4778 tunnel_add_path (t, path, own_pos);
4779 if (own_pos == size - 1)
4781 /* It is for us! Send ack. */
4782 struct MeshTransmissionDescriptor *info;
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4785 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4786 if (NULL == t->peers)
4788 /* New tunnel! Notify clients on data. */
4789 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4791 GNUNET_break (GNUNET_SYSERR !=
4792 GNUNET_CONTAINER_multihashmap_put (t->peers,
4793 &my_full_id.hashPubKey,
4796 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4797 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4798 info->origin = &t->id;
4799 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4800 GNUNET_assert (NULL != info->peer);
4802 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4803 sizeof (struct GNUNET_MESH_PathACK),
4809 struct MeshPeerPath *path2;
4811 /* It's for somebody else! Retransmit. */
4812 path2 = path_duplicate (path);
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4814 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4815 path2 = path_duplicate (path);
4816 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4817 send_create_path (dest_peer_info, path, t);
4824 * Core handler for path destruction
4826 * @param cls closure
4827 * @param message message
4828 * @param peer peer identity this notification is about
4829 * @param atsi performance data
4830 * @param atsi_count number of records in 'atsi'
4832 * @return GNUNET_OK to keep the connection open,
4833 * GNUNET_SYSERR to close it (signal serious error)
4836 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4837 const struct GNUNET_MessageHeader *message,
4838 const struct GNUNET_ATS_Information *atsi,
4839 unsigned int atsi_count)
4841 struct GNUNET_MESH_ManipulatePath *msg;
4842 struct GNUNET_PeerIdentity *pi;
4843 struct MeshPeerPath *path;
4844 struct MeshTunnel *t;
4845 unsigned int own_pos;
4849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4850 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4851 size = ntohs (message->size);
4852 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4854 GNUNET_break_op (0);
4858 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4859 if (size % sizeof (struct GNUNET_PeerIdentity))
4861 GNUNET_break_op (0);
4864 size /= sizeof (struct GNUNET_PeerIdentity);
4867 GNUNET_break_op (0);
4870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4872 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4873 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4875 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4877 t = tunnel_get (pi, ntohl (msg->tid));
4880 /* TODO notify back: we don't know this tunnel */
4881 GNUNET_break_op (0);
4884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4885 path = path_new (size);
4887 for (i = 0; i < size; i++)
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4890 GNUNET_i2s (&pi[i]));
4891 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4892 if (path->peers[i] == myid)
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4896 if (own_pos < path->length - 1)
4897 send_message (message, &pi[own_pos + 1], t);
4899 send_client_tunnel_disconnect(t, NULL);
4901 tunnel_delete_peer (t, path->peers[path->length - 1]);
4902 path_destroy (path);
4908 * Core handler for notifications of broken paths
4910 * @param cls closure
4911 * @param message message
4912 * @param peer peer identity this notification is about
4913 * @param atsi performance data
4914 * @param atsi_count number of records in 'atsi'
4916 * @return GNUNET_OK to keep the connection open,
4917 * GNUNET_SYSERR to close it (signal serious error)
4920 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4921 const struct GNUNET_MessageHeader *message,
4922 const struct GNUNET_ATS_Information *atsi,
4923 unsigned int atsi_count)
4925 struct GNUNET_MESH_PathBroken *msg;
4926 struct MeshTunnel *t;
4928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4929 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4930 msg = (struct GNUNET_MESH_PathBroken *) message;
4931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4932 GNUNET_i2s (&msg->peer1));
4933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4934 GNUNET_i2s (&msg->peer2));
4935 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4938 GNUNET_break_op (0);
4941 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4942 GNUNET_PEER_search (&msg->peer2));
4949 * Core handler for tunnel destruction
4951 * @param cls closure
4952 * @param message message
4953 * @param peer peer identity this notification is about
4954 * @param atsi performance data
4955 * @param atsi_count number of records in 'atsi'
4957 * @return GNUNET_OK to keep the connection open,
4958 * GNUNET_SYSERR to close it (signal serious error)
4961 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4962 const struct GNUNET_MessageHeader *message,
4963 const struct GNUNET_ATS_Information *atsi,
4964 unsigned int atsi_count)
4966 struct GNUNET_MESH_TunnelDestroy *msg;
4967 struct MeshTunnel *t;
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4970 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4971 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4973 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4974 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4977 /* Probably already got the message from another path,
4978 * destroyed the tunnel and retransmitted to children.
4981 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4984 if (t->id.oid == myid)
4986 GNUNET_break_op (0);
4989 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4991 /* Tunnel was incoming, notify clients */
4992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4993 t->local_tid, t->local_tid_dest);
4994 send_clients_tunnel_destroy (t);
4996 tunnel_send_destroy (t);
4997 t->destroy = GNUNET_YES;
4998 // TODO: add timeout to destroy the tunnel anyway
5004 * Core handler for mesh network traffic going from the origin to a peer
5006 * @param cls closure
5007 * @param peer peer identity this notification is about
5008 * @param message message
5009 * @param atsi performance data
5010 * @param atsi_count number of records in 'atsi'
5011 * @return GNUNET_OK to keep the connection open,
5012 * GNUNET_SYSERR to close it (signal serious error)
5015 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5016 const struct GNUNET_MessageHeader *message,
5017 const struct GNUNET_ATS_Information *atsi,
5018 unsigned int atsi_count)
5020 struct GNUNET_MESH_Unicast *msg;
5021 struct GNUNET_PeerIdentity *neighbor;
5022 struct MeshTunnelChildInfo *cinfo;
5023 struct MeshTunnel *t;
5024 GNUNET_PEER_Id dest_id;
5029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5032 size = ntohs (message->size);
5034 sizeof (struct GNUNET_MESH_Unicast) +
5035 sizeof (struct GNUNET_MessageHeader))
5040 msg = (struct GNUNET_MESH_Unicast *) message;
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5042 ntohs (msg[1].header.type));
5044 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5047 /* TODO notify back: we don't know this tunnel */
5048 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5049 GNUNET_break_op (0);
5052 pid = ntohl (msg->pid);
5053 if (t->fwd_pid == pid)
5055 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5056 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5057 " Already seen pid %u, DROPPING!\n", pid);
5062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5063 " pid %u not seen yet, forwarding\n", pid);
5065 t->skip += (pid - t->fwd_pid) - 1;
5067 if (is_pid_bigger (pid, t->last_fwd_ack))
5069 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5070 GNUNET_break_op (0);
5073 tunnel_reset_timeout (t);
5074 dest_id = GNUNET_PEER_search (&msg->destination);
5075 if (dest_id == myid)
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5078 " it's for us! sending to clients...\n");
5079 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5080 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
5081 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5084 ttl = ntohl (msg->ttl);
5085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5088 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5089 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5090 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5094 " not for us, retransmitting...\n");
5096 neighbor = tree_get_first_hop (t->tree, dest_id);
5097 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5099 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5102 if (is_pid_bigger (pid, cinfo->fwd_ack))
5104 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5105 GNUNET_break_op (0);
5108 send_message (message, neighbor, t);
5109 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5116 * Core handler for mesh network traffic going from the origin to all peers
5118 * @param cls closure
5119 * @param message message
5120 * @param peer peer identity this notification is about
5121 * @param atsi performance data
5122 * @param atsi_count number of records in 'atsi'
5123 * @return GNUNET_OK to keep the connection open,
5124 * GNUNET_SYSERR to close it (signal serious error)
5126 * TODO: Check who we got this from, to validate route.
5129 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5130 const struct GNUNET_MessageHeader *message,
5131 const struct GNUNET_ATS_Information *atsi,
5132 unsigned int atsi_count)
5134 struct GNUNET_MESH_Multicast *msg;
5135 struct MeshTunnel *t;
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5141 size = ntohs (message->size);
5142 if (sizeof (struct GNUNET_MESH_Multicast) +
5143 sizeof (struct GNUNET_MessageHeader) > size)
5145 GNUNET_break_op (0);
5148 msg = (struct GNUNET_MESH_Multicast *) message;
5149 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5153 /* TODO notify that we dont know that tunnel */
5154 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5155 GNUNET_break_op (0);
5158 pid = ntohl (msg->pid);
5159 if (t->fwd_pid == pid)
5161 /* already seen this packet, drop */
5162 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5164 " Already seen pid %u, DROPPING!\n", pid);
5165 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5171 " pid %u not seen yet, forwarding\n", pid);
5173 t->skip += (pid - t->fwd_pid) - 1;
5175 tunnel_reset_timeout (t);
5177 /* Transmit to locally interested clients */
5178 if (NULL != t->peers &&
5179 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5181 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5182 send_subscribed_clients (message, &msg[1].header);
5183 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5186 if (ntohl (msg->ttl) == 0)
5188 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5189 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5190 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5193 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5194 tunnel_send_multicast (t, message, GNUNET_NO);
5200 * Core handler for mesh network traffic toward the owner of a tunnel
5202 * @param cls closure
5203 * @param message message
5204 * @param peer peer identity this notification is about
5205 * @param atsi performance data
5206 * @param atsi_count number of records in 'atsi'
5208 * @return GNUNET_OK to keep the connection open,
5209 * GNUNET_SYSERR to close it (signal serious error)
5212 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5213 const struct GNUNET_MessageHeader *message,
5214 const struct GNUNET_ATS_Information *atsi,
5215 unsigned int atsi_count)
5217 struct GNUNET_MESH_ToOrigin *msg;
5218 struct GNUNET_PeerIdentity id;
5219 struct MeshPeerInfo *peer_info;
5220 struct MeshTunnel *t;
5223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5225 size = ntohs (message->size);
5226 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5227 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5229 GNUNET_break_op (0);
5232 msg = (struct GNUNET_MESH_ToOrigin *) message;
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5234 ntohs (msg[1].header.type));
5235 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5239 /* TODO notify that we dont know this tunnel (whom)? */
5240 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5241 GNUNET_break_op (0);
5245 if (NULL != t->owner)
5248 struct GNUNET_MESH_ToOrigin *copy;
5250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5251 " it's for us! sending to clients...\n");
5252 /* TODO signature verification */
5253 memcpy (cbuf, message, size);
5254 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5255 copy->tid = htonl (t->local_tid);
5256 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5257 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5258 ©->header, GNUNET_NO);
5259 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5263 " not for us, retransmitting...\n");
5265 peer_info = peer_info_get (&msg->oid);
5266 if (NULL == peer_info)
5268 /* unknown origin of tunnel */
5272 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5273 send_message (message, &id, t);
5274 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5281 * Core handler for mesh network traffic point-to-point acks.
5283 * @param cls closure
5284 * @param message message
5285 * @param peer peer identity this notification is about
5286 * @param atsi performance data
5287 * @param atsi_count number of records in 'atsi'
5289 * @return GNUNET_OK to keep the connection open,
5290 * GNUNET_SYSERR to close it (signal serious error)
5293 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5294 const struct GNUNET_MessageHeader *message,
5295 const struct GNUNET_ATS_Information *atsi,
5296 unsigned int atsi_count)
5298 struct GNUNET_MESH_ACK *msg;
5299 struct MeshTunnel *t;
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5304 msg = (struct GNUNET_MESH_ACK *) message;
5305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %u\n",
5306 ntohs (msg[1].header.type));
5307 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5311 /* TODO notify that we dont know this tunnel (whom)? */
5312 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5313 GNUNET_break_op (0);
5316 ack = ntohl (msg->pid);
5318 /* Is this a forward or backward ACK? */
5319 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5321 struct MeshTunnelChildInfo *cinfo;
5323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5324 cinfo = tunnel_get_neighbor_fc (t, peer);
5325 cinfo->fwd_ack = ack;
5326 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5332 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5334 // FIXME fc Unlock queues?
5340 * Core handler for path ACKs
5342 * @param cls closure
5343 * @param message message
5344 * @param peer peer identity this notification is about
5345 * @param atsi performance data
5346 * @param atsi_count number of records in 'atsi'
5348 * @return GNUNET_OK to keep the connection open,
5349 * GNUNET_SYSERR to close it (signal serious error)
5352 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5353 const struct GNUNET_MessageHeader *message,
5354 const struct GNUNET_ATS_Information *atsi,
5355 unsigned int atsi_count)
5357 struct GNUNET_MESH_PathACK *msg;
5358 struct GNUNET_PeerIdentity id;
5359 struct MeshPeerInfo *peer_info;
5360 struct MeshPeerPath *p;
5361 struct MeshTunnel *t;
5363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5364 GNUNET_i2s (&my_full_id));
5365 msg = (struct GNUNET_MESH_PathACK *) message;
5366 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5369 /* TODO notify that we don't know the tunnel */
5370 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5372 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5376 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5378 peer_info = peer_info_get (&msg->peer_id);
5379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5380 GNUNET_i2s (&msg->peer_id));
5381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5384 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5387 "connect_by_string completed, stopping search\n");
5388 regex_cancel_search (t->regex_ctx);
5389 t->regex_ctx = NULL;
5392 /* Add paths to peers? */
5393 p = tree_get_path_to_peer (t->tree, peer_info->id);
5396 path_add_to_peers (p, GNUNET_YES);
5404 /* Message for us? */
5405 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5408 if (NULL == t->owner)
5410 GNUNET_break_op (0);
5413 if (NULL != t->dht_get_type)
5415 GNUNET_DHT_get_stop (t->dht_get_type);
5416 t->dht_get_type = NULL;
5418 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5420 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5421 send_client_peer_connected (t, peer_info->id);
5426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5427 " not for us, retransmitting...\n");
5428 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5429 peer_info = peer_info_get (&msg->oid);
5430 if (NULL == peer_info)
5432 /* If we know the tunnel, we should DEFINITELY know the peer */
5436 send_message (message, &id, t);
5442 * Functions to handle messages from core
5444 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5445 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5446 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5447 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5448 sizeof (struct GNUNET_MESH_PathBroken)},
5449 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5450 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5451 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5452 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5453 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5454 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5455 sizeof (struct GNUNET_MESH_ACK)},
5456 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5457 sizeof (struct GNUNET_MESH_PathACK)},
5463 /******************************************************************************/
5464 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5465 /******************************************************************************/
5468 * deregister_app: iterator for removing each application registered by a client
5470 * @param cls closure
5471 * @param key the hash of the application id (used to access the hashmap)
5472 * @param value the value stored at the key (client)
5474 * @return GNUNET_OK on success
5477 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5479 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5480 GNUNET_break (GNUNET_YES ==
5481 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5487 * notify_client_connection_failure: notify a client that the connection to the
5488 * requested remote peer is not possible (for instance, no route found)
5489 * Function called when the socket is ready to queue more data. "buf" will be
5490 * NULL and "size" zero if the socket was closed for writing in the meantime.
5492 * @param cls closure
5493 * @param size number of bytes available in buf
5494 * @param buf where the callee should write the message
5495 * @return number of bytes written to buf
5498 notify_client_connection_failure (void *cls, size_t size, void *buf)
5501 struct MeshPeerInfo *peer_info;
5502 struct GNUNET_MESH_PeerControl *msg;
5503 struct GNUNET_PeerIdentity id;
5505 if (0 == size && NULL == buf)
5507 // TODO retry? cancel?
5511 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5512 peer_info = (struct MeshPeerInfo *) cls;
5513 msg = (struct GNUNET_MESH_PeerControl *) buf;
5514 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5515 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5516 // msg->tunnel_id = htonl(peer_info->t->tid);
5517 GNUNET_PEER_resolve (peer_info->id, &id);
5518 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5526 * Send keepalive packets for a peer
5528 * @param cls Closure (tunnel for which to send the keepalive).
5529 * @param tc Notification context.
5531 * TODO: implement explicit multicast keepalive?
5534 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5536 struct MeshTunnel *t = cls;
5537 struct GNUNET_MessageHeader *payload;
5538 struct GNUNET_MESH_Multicast *msg;
5540 sizeof (struct GNUNET_MESH_Multicast) +
5541 sizeof (struct GNUNET_MessageHeader);
5544 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5548 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5551 "sending keepalive for tunnel %d\n", t->id.tid);
5553 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5554 msg->header.size = htons (size);
5555 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5556 msg->oid = my_full_id;
5557 msg->tid = htonl (t->id.tid);
5558 msg->ttl = htonl (default_ttl);
5559 msg->pid = htonl (t->fwd_pid + 1);
5561 payload = (struct GNUNET_MessageHeader *) &msg[1];
5562 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5563 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5564 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5566 t->path_refresh_task =
5567 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5573 * Function to process paths received for a new peer addition. The recorded
5574 * paths form the initial tunnel, which can be optimized later.
5575 * Called on each result obtained for the DHT search.
5577 * @param cls closure
5578 * @param exp when will this value expire
5579 * @param key key of the result
5580 * @param get_path path of the get request
5581 * @param get_path_length lenght of get_path
5582 * @param put_path path of the put request
5583 * @param put_path_length length of the put_path
5584 * @param type type of the result
5585 * @param size number of bytes in data
5586 * @param data pointer to the result data
5588 * TODO: re-issue the request after certain time? cancel after X results?
5591 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5592 const struct GNUNET_HashCode * key,
5593 const struct GNUNET_PeerIdentity *get_path,
5594 unsigned int get_path_length,
5595 const struct GNUNET_PeerIdentity *put_path,
5596 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5597 size_t size, const void *data)
5599 struct MeshPathInfo *path_info = cls;
5600 struct MeshPeerPath *p;
5601 struct GNUNET_PeerIdentity pi;
5604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5605 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5608 p = path_build_from_dht (get_path, get_path_length, put_path,
5610 path_add_to_peers (p, GNUNET_NO);
5612 for (i = 0; i < path_info->peer->ntunnels; i++)
5614 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5615 peer_info_connect (path_info->peer, path_info->t);
5623 * Function to process paths received for a new peer addition. The recorded
5624 * paths form the initial tunnel, which can be optimized later.
5625 * Called on each result obtained for the DHT search.
5627 * @param cls closure
5628 * @param exp when will this value expire
5629 * @param key key of the result
5630 * @param get_path path of the get request
5631 * @param get_path_length lenght of get_path
5632 * @param put_path path of the put request
5633 * @param put_path_length length of the put_path
5634 * @param type type of the result
5635 * @param size number of bytes in data
5636 * @param data pointer to the result data
5639 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5640 const struct GNUNET_HashCode * key,
5641 const struct GNUNET_PeerIdentity *get_path,
5642 unsigned int get_path_length,
5643 const struct GNUNET_PeerIdentity *put_path,
5644 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5645 size_t size, const void *data)
5647 const struct PBlock *pb = data;
5648 const struct GNUNET_PeerIdentity *pi = &pb->id;
5649 struct MeshTunnel *t = cls;
5650 struct MeshPeerInfo *peer_info;
5651 struct MeshPeerPath *p;
5653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5654 if (size != sizeof (struct PBlock))
5656 GNUNET_break_op (0);
5659 if (ntohl(pb->type) != t->type)
5661 GNUNET_break_op (0);
5664 GNUNET_assert (NULL != t->owner);
5665 peer_info = peer_info_get (pi);
5666 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5670 p = path_build_from_dht (get_path, get_path_length, put_path,
5672 path_add_to_peers (p, GNUNET_NO);
5674 tunnel_add_peer (t, peer_info);
5675 peer_info_connect (peer_info, t);
5680 * Function to process DHT string to regex matching.
5681 * Called on each result obtained for the DHT search.
5683 * @param cls closure (search context)
5684 * @param exp when will this value expire
5685 * @param key key of the result
5686 * @param get_path path of the get request (not used)
5687 * @param get_path_length lenght of get_path (not used)
5688 * @param put_path path of the put request (not used)
5689 * @param put_path_length length of the put_path (not used)
5690 * @param type type of the result
5691 * @param size number of bytes in data
5692 * @param data pointer to the result data
5695 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5696 const struct GNUNET_HashCode * key,
5697 const struct GNUNET_PeerIdentity *get_path,
5698 unsigned int get_path_length,
5699 const struct GNUNET_PeerIdentity *put_path,
5700 unsigned int put_path_length,
5701 enum GNUNET_BLOCK_Type type,
5702 size_t size, const void *data)
5704 const struct MeshRegexAccept *block = data;
5705 struct MeshRegexSearchContext *ctx = cls;
5706 struct MeshRegexSearchInfo *info = ctx->info;
5707 struct MeshPeerPath *p;
5708 struct MeshPeerInfo *peer_info;
5710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5713 peer_info = peer_info_get(&block->id);
5714 p = path_build_from_dht (get_path, get_path_length, put_path,
5716 path_add_to_peers (p, GNUNET_NO);
5719 tunnel_add_peer (info->t, peer_info);
5720 peer_info_connect (peer_info, info->t);
5721 if (0 == info->peer)
5723 info->peer = peer_info->id;
5727 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5730 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5731 ®ex_connect_timeout,
5739 * Function to process DHT string to regex matching.
5740 * Called on each result obtained for the DHT search.
5742 * @param cls closure (search context)
5743 * @param exp when will this value expire
5744 * @param key key of the result
5745 * @param get_path path of the get request (not used)
5746 * @param get_path_length lenght of get_path (not used)
5747 * @param put_path path of the put request (not used)
5748 * @param put_path_length length of the put_path (not used)
5749 * @param type type of the result
5750 * @param size number of bytes in data
5751 * @param data pointer to the result data
5753 * TODO: re-issue the request after certain time? cancel after X results?
5756 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5757 const struct GNUNET_HashCode * key,
5758 const struct GNUNET_PeerIdentity *get_path,
5759 unsigned int get_path_length,
5760 const struct GNUNET_PeerIdentity *put_path,
5761 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5762 size_t size, const void *data)
5764 const struct MeshRegexBlock *block = data;
5765 struct MeshRegexSearchContext *ctx = cls;
5766 struct MeshRegexSearchInfo *info = ctx->info;
5770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5771 "DHT GET STRING RETURNED RESULTS\n");
5772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5773 " key: %s\n", GNUNET_h2s (key));
5775 copy = GNUNET_malloc (size);
5776 memcpy (copy, data, size);
5777 GNUNET_break (GNUNET_OK ==
5778 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5779 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5780 len = ntohl (block->n_proof);
5782 char proof[len + 1];
5784 memcpy (proof, &block[1], len);
5786 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5788 GNUNET_break_op (0);
5792 len = strlen (info->description);
5793 if (len == ctx->position) // String processed
5795 if (GNUNET_YES == ntohl (block->accepting))
5797 regex_find_path(key, ctx);
5801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5802 // FIXME REGEX this block not successful, wait for more? start timeout?
5806 GNUNET_break (GNUNET_OK ==
5807 GNUNET_MESH_regex_block_iterate (block, size,
5808 ®ex_edge_iterator, ctx));
5812 /******************************************************************************/
5813 /********************* MESH LOCAL HANDLES **************************/
5814 /******************************************************************************/
5818 * Handler for client disconnection
5820 * @param cls closure
5821 * @param client identification of the client; NULL
5822 * for the last call when the server is destroyed
5825 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5827 struct MeshClient *c;
5828 struct MeshClient *next;
5831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5840 if (c->handle != client)
5842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5848 GNUNET_SERVER_client_drop (c->handle);
5849 c->shutting_down = GNUNET_YES;
5850 GNUNET_assert (NULL != c->own_tunnels);
5851 GNUNET_assert (NULL != c->incoming_tunnels);
5852 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5853 &tunnel_destroy_iterator, c);
5854 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5855 &tunnel_destroy_iterator, c);
5856 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5857 &tunnel_destroy_iterator, c);
5858 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5859 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5860 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5862 /* deregister clients applications */
5863 if (NULL != c->apps)
5865 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5866 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5868 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5869 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5871 GNUNET_SCHEDULER_cancel (announce_applications_task);
5872 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5874 if (NULL != c->types)
5875 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5876 for (i = 0; i < c->n_regex; i++)
5878 GNUNET_free (c->regexes[i]);
5880 GNUNET_free_non_null (c->regexes);
5881 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5882 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5884 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5887 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5896 * Handler for new clients
5898 * @param cls closure
5899 * @param client identification of the client
5900 * @param message the actual message, which includes messages the client wants
5903 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5904 const struct GNUNET_MessageHeader *message)
5906 struct GNUNET_MESH_ClientConnect *cc_msg;
5907 struct MeshClient *c;
5908 GNUNET_MESH_ApplicationType *a;
5915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5916 /* Check data sanity */
5917 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5918 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5919 ntypes = ntohs (cc_msg->types);
5920 napps = ntohs (cc_msg->applications);
5922 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5925 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5929 /* Create new client structure */
5930 c = GNUNET_malloc (sizeof (struct MeshClient));
5931 c->id = next_client_id++;
5932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5934 GNUNET_SERVER_client_keep (client);
5935 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5938 GNUNET_MESH_ApplicationType at;
5939 struct GNUNET_HashCode hc;
5941 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5942 for (i = 0; i < napps; i++)
5945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5946 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5947 /* store in clients hashmap */
5948 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5949 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5950 /* store in global hashmap, for announcements */
5951 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5952 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5954 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5955 announce_applications_task =
5956 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5962 struct GNUNET_HashCode hc;
5964 t = (uint16_t *) & a[napps];
5965 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5966 for (i = 0; i < ntypes; i++)
5969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5970 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5972 /* store in clients hashmap */
5973 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5974 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5975 /* store in global hashmap */
5976 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5977 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5981 " client has %u+%u subscriptions\n", napps, ntypes);
5983 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5984 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5985 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5986 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5987 GNUNET_SERVER_notification_context_add (nc, client);
5988 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5990 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5996 * Handler for clients announcing available services by a regular expression.
5998 * @param cls closure
5999 * @param client identification of the client
6000 * @param message the actual message, which includes messages the client wants
6003 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6004 const struct GNUNET_MessageHeader *message)
6006 struct MeshClient *c;
6010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6012 /* Sanity check for client registration */
6013 if (NULL == (c = client_get (client)))
6016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6021 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6022 regex = GNUNET_malloc (len + 1);
6023 memcpy (regex, &message[1], len);
6025 GNUNET_array_append (c->regexes, c->n_regex, regex);
6026 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6028 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6034 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6040 * Handler for requests of new tunnels
6042 * @param cls closure
6043 * @param client identification of the client
6044 * @param message the actual message
6047 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6048 const struct GNUNET_MessageHeader *message)
6050 struct GNUNET_MESH_TunnelMessage *t_msg;
6051 struct MeshTunnel *t;
6052 struct MeshClient *c;
6053 MESH_TunnelNumber tid;
6055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6057 /* Sanity check for client registration */
6058 if (NULL == (c = client_get (client)))
6061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6066 /* Message sanity check */
6067 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6074 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6075 /* Sanity check for tunnel numbering */
6076 tid = ntohl (t_msg->tunnel_id);
6077 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6083 /* Sanity check for duplicate tunnel IDs */
6084 if (NULL != tunnel_get_by_local_id (c, tid))
6087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6091 while (NULL != tunnel_get_by_pi (myid, next_tid))
6092 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6093 t = tunnel_new (myid, next_tid++, c, tid);
6096 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6101 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6103 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6104 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6107 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6113 * Handler for requests of deleting tunnels
6115 * @param cls closure
6116 * @param client identification of the client
6117 * @param message the actual message
6120 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6121 const struct GNUNET_MessageHeader *message)
6123 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6124 struct MeshClient *c;
6125 struct MeshTunnel *t;
6126 MESH_TunnelNumber tid;
6128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6129 "Got a DESTROY TUNNEL from client!\n");
6131 /* Sanity check for client registration */
6132 if (NULL == (c = client_get (client)))
6135 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6138 /* Message sanity check */
6139 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6142 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6146 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6148 /* Retrieve tunnel */
6149 tid = ntohl (tunnel_msg->tunnel_id);
6150 t = tunnel_get_by_local_id(c, tid);
6153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6155 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6158 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6160 client_ignore_tunnel (c, t);
6162 // TODO: when to destroy incoming tunnel?
6163 if (t->nclients == 0)
6165 GNUNET_assert (GNUNET_YES ==
6166 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6168 GNUNET_assert (GNUNET_YES ==
6169 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6170 &my_full_id.hashPubKey,
6174 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6177 send_client_tunnel_disconnect(t, c);
6178 client_delete_tunnel(c, t);
6180 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6182 tunnel_send_destroy (t);
6183 t->destroy = GNUNET_YES;
6184 // The tunnel will be destroyed when the last message is transmitted.
6185 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6191 * Handler for requests of seeting tunnel's speed.
6193 * @param cls Closure (unused).
6194 * @param client Identification of the client.
6195 * @param message The actual message.
6198 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6199 const struct GNUNET_MessageHeader *message)
6201 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6202 struct MeshClient *c;
6203 struct MeshTunnel *t;
6204 MESH_TunnelNumber tid;
6206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6207 "Got a SPEED request from client!\n");
6209 /* Sanity check for client registration */
6210 if (NULL == (c = client_get (client)))
6213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6218 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6220 /* Retrieve tunnel */
6221 tid = ntohl (tunnel_msg->tunnel_id);
6222 t = tunnel_get_by_local_id(c, tid);
6225 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6231 switch (ntohs(message->type))
6233 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6234 t->speed_min = GNUNET_YES;
6236 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6237 t->speed_min = GNUNET_NO;
6242 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6247 * Handler for requests of seeting tunnel's buffering policy.
6249 * @param cls Closure (unused).
6250 * @param client Identification of the client.
6251 * @param message The actual message.
6254 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6255 const struct GNUNET_MessageHeader *message)
6257 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6258 struct MeshClient *c;
6259 struct MeshTunnel *t;
6260 MESH_TunnelNumber tid;
6262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6263 "Got a BUFFER request from client!\n");
6265 /* Sanity check for client registration */
6266 if (NULL == (c = client_get (client)))
6269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6274 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6276 /* Retrieve tunnel */
6277 tid = ntohl (tunnel_msg->tunnel_id);
6278 t = tunnel_get_by_local_id(c, tid);
6281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6287 switch (ntohs(message->type))
6289 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6290 t->nobuffer = GNUNET_NO;
6292 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6293 t->nobuffer = GNUNET_YES;
6299 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6304 * Handler for connection requests to new peers
6306 * @param cls closure
6307 * @param client identification of the client
6308 * @param message the actual message (PeerControl)
6311 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6312 const struct GNUNET_MessageHeader *message)
6314 struct GNUNET_MESH_PeerControl *peer_msg;
6315 struct MeshPeerInfo *peer_info;
6316 struct MeshClient *c;
6317 struct MeshTunnel *t;
6318 MESH_TunnelNumber tid;
6320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6321 /* Sanity check for client registration */
6322 if (NULL == (c = client_get (client)))
6325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6329 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6330 /* Sanity check for message size */
6331 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6338 /* Tunnel exists? */
6339 tid = ntohl (peer_msg->tunnel_id);
6340 t = tunnel_get_by_local_id (c, tid);
6344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6348 /* Does client own tunnel? */
6349 if (t->owner->handle != client)
6352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6356 GNUNET_i2s (&peer_msg->peer));
6357 peer_info = peer_info_get (&peer_msg->peer);
6359 tunnel_add_peer (t, peer_info);
6360 peer_info_connect (peer_info, t);
6362 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6368 * Handler for disconnection requests of peers in a tunnel
6370 * @param cls closure
6371 * @param client identification of the client
6372 * @param message the actual message (PeerControl)
6375 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6376 const struct GNUNET_MessageHeader *message)
6378 struct GNUNET_MESH_PeerControl *peer_msg;
6379 struct MeshPeerInfo *peer_info;
6380 struct MeshClient *c;
6381 struct MeshTunnel *t;
6382 MESH_TunnelNumber tid;
6384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6385 /* Sanity check for client registration */
6386 if (NULL == (c = client_get (client)))
6389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6392 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6393 /* Sanity check for message size */
6394 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6401 /* Tunnel exists? */
6402 tid = ntohl (peer_msg->tunnel_id);
6403 t = tunnel_get_by_local_id (c, tid);
6407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6412 /* Does client own tunnel? */
6413 if (t->owner->handle != client)
6416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6421 GNUNET_i2s (&peer_msg->peer));
6422 /* Is the peer in the tunnel? */
6424 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6425 if (NULL == peer_info)
6428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6432 /* Ok, delete peer from tunnel */
6433 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6434 &peer_msg->peer.hashPubKey);
6436 send_destroy_path (t, peer_info->id);
6437 tunnel_delete_peer (t, peer_info->id);
6438 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6443 * Handler for blacklist requests of peers in a tunnel
6445 * @param cls closure
6446 * @param client identification of the client
6447 * @param message the actual message (PeerControl)
6450 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6451 const struct GNUNET_MessageHeader *message)
6453 struct GNUNET_MESH_PeerControl *peer_msg;
6454 struct MeshClient *c;
6455 struct MeshTunnel *t;
6456 MESH_TunnelNumber tid;
6458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6459 /* Sanity check for client registration */
6460 if (NULL == (c = client_get (client)))
6463 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6466 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6468 /* Sanity check for message size */
6469 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6476 /* Tunnel exists? */
6477 tid = ntohl (peer_msg->tunnel_id);
6478 t = tunnel_get_by_local_id (c, tid);
6482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6487 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6488 GNUNET_PEER_intern(&peer_msg->peer));
6493 * Handler for unblacklist requests of peers in a tunnel
6495 * @param cls closure
6496 * @param client identification of the client
6497 * @param message the actual message (PeerControl)
6500 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6501 const struct GNUNET_MessageHeader *message)
6503 struct GNUNET_MESH_PeerControl *peer_msg;
6504 struct MeshClient *c;
6505 struct MeshTunnel *t;
6506 MESH_TunnelNumber tid;
6510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6511 /* Sanity check for client registration */
6512 if (NULL == (c = client_get (client)))
6515 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6518 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6520 /* Sanity check for message size */
6521 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6528 /* Tunnel exists? */
6529 tid = ntohl (peer_msg->tunnel_id);
6530 t = tunnel_get_by_local_id (c, tid);
6534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6539 /* if peer is not known, complain */
6540 pid = GNUNET_PEER_search (&peer_msg->peer);
6547 /* search and remove from list */
6548 for (i = 0; i < t->nblacklisted; i++)
6550 if (t->blacklisted[i] == pid)
6552 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6553 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6558 /* if peer hasn't been blacklisted, complain */
6564 * Handler for connection requests to new peers by type
6566 * @param cls closure
6567 * @param client identification of the client
6568 * @param message the actual message (ConnectPeerByType)
6571 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6572 const struct GNUNET_MessageHeader *message)
6574 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6575 struct MeshClient *c;
6576 struct MeshTunnel *t;
6577 struct GNUNET_HashCode hash;
6578 MESH_TunnelNumber tid;
6580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6581 /* Sanity check for client registration */
6582 if (NULL == (c = client_get (client)))
6585 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6589 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6590 /* Sanity check for message size */
6591 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6592 ntohs (connect_msg->header.size))
6595 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6599 /* Tunnel exists? */
6600 tid = ntohl (connect_msg->tunnel_id);
6601 t = tunnel_get_by_local_id (c, tid);
6605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6609 /* Does client own tunnel? */
6610 if (t->owner->handle != client)
6613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6617 /* Do WE have the service? */
6618 t->type = ntohl (connect_msg->type);
6619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6620 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6621 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6624 /* Yes! Fast forward, add ourselves to the tunnel and send the
6625 * good news to the client, and alert the destination client of
6626 * an incoming tunnel.
6628 * FIXME send a path create to self, avoid code duplication
6630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6631 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6632 peer_info_get (&my_full_id),
6633 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6636 send_client_peer_connected (t, myid);
6637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6638 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6640 t->local_tid_dest = next_local_tid++;
6641 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6642 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6643 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6647 /* Ok, lets find a peer offering the service */
6648 if (NULL != t->dht_get_type)
6650 GNUNET_DHT_get_stop (t->dht_get_type);
6652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6653 GNUNET_h2s (&hash));
6655 GNUNET_DHT_get_start (dht_handle,
6656 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6658 dht_replication_level,
6659 GNUNET_DHT_RO_RECORD_ROUTE |
6660 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6662 &dht_get_type_handler, t);
6664 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6670 * Handler for connection requests to new peers by a string service description.
6672 * @param cls closure
6673 * @param client identification of the client
6674 * @param message the actual message, which includes messages the client wants
6677 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6678 const struct GNUNET_MessageHeader *message)
6680 struct GNUNET_MESH_ConnectPeerByString *msg;
6681 struct MeshRegexSearchContext *ctx;
6682 struct MeshRegexSearchInfo *info;
6683 struct GNUNET_DHT_GetHandle *get_h;
6684 struct GNUNET_HashCode key;
6685 struct MeshTunnel *t;
6686 struct MeshClient *c;
6687 MESH_TunnelNumber tid;
6691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6692 "Connect by string started\n");
6693 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6694 size = htons (message->size);
6696 /* Sanity check for client registration */
6697 if (NULL == (c = client_get (client)))
6700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6704 /* Message size sanity check */
6705 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6712 /* Tunnel exists? */
6713 tid = ntohl (msg->tunnel_id);
6714 t = tunnel_get_by_local_id (c, tid);
6718 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6722 /* Does client own tunnel? */
6723 if (t->owner->handle != client)
6726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6731 " on tunnel %s [%u]\n",
6732 GNUNET_i2s(&my_full_id),
6735 /* Only one connect_by_string allowed at the same time! */
6736 /* FIXME: allow more, return handle at api level to cancel, document */
6737 if (NULL != t->regex_ctx)
6740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6744 /* Find string itself */
6745 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6746 string = (const char *) &msg[1];
6748 /* Initialize context */
6749 size = GNUNET_REGEX_get_first_key(string, len, &key);
6750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6751 " consumed %u bits out of %u\n", size, len);
6752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6753 " looking for %s\n", GNUNET_h2s (&key));
6755 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6757 info->description = GNUNET_malloc (len + 1);
6758 memcpy (info->description, string, len);
6759 info->description[len] = '\0';
6760 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6761 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6764 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6765 ctx->position = size;
6769 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6771 /* Start search in DHT */
6772 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6773 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6774 &key, /* key to search */
6775 dht_replication_level, /* replication level */
6776 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6777 NULL, /* xquery */ // FIXME BLOOMFILTER
6778 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6779 &dht_get_string_handler, ctx);
6781 GNUNET_break (GNUNET_OK ==
6782 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6785 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6787 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6793 * Handler for client traffic directed to one peer
6795 * @param cls closure
6796 * @param client identification of the client
6797 * @param message the actual message
6800 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6801 const struct GNUNET_MessageHeader *message)
6803 struct MeshClient *c;
6804 struct MeshTunnel *t;
6805 struct MeshPeerInfo *pi;
6806 struct GNUNET_MESH_Unicast *data_msg;
6807 MESH_TunnelNumber tid;
6810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6811 "Got a unicast request from a client!\n");
6813 /* Sanity check for client registration */
6814 if (NULL == (c = client_get (client)))
6817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6820 data_msg = (struct GNUNET_MESH_Unicast *) message;
6821 /* Sanity check for message size */
6822 size = ntohs (message->size);
6823 if (sizeof (struct GNUNET_MESH_Unicast) +
6824 sizeof (struct GNUNET_MessageHeader) > size)
6827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6831 /* Tunnel exists? */
6832 tid = ntohl (data_msg->tid);
6833 t = tunnel_get_by_local_id (c, tid);
6837 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6841 /* Is it a local tunnel? Then, does client own the tunnel? */
6842 if (t->owner->handle != client)
6845 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6849 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6850 &data_msg->destination.hashPubKey);
6851 /* Is the selected peer in the tunnel? */
6855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6860 /* Ok, everything is correct, send the message
6861 * (pretend we got it from a mesh peer)
6864 /* Work around const limitation */
6865 char buf[ntohs (message->size)] GNUNET_ALIGN;
6866 struct GNUNET_MESH_Unicast *copy;
6868 copy = (struct GNUNET_MESH_Unicast *) buf;
6869 memcpy (buf, data_msg, size);
6870 copy->oid = my_full_id;
6871 copy->tid = htonl (t->id.tid);
6872 copy->ttl = htonl (default_ttl);
6873 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
6874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6875 " calling generic handler...\n");
6876 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6885 * Handler for client traffic directed to the origin
6887 * @param cls closure
6888 * @param client identification of the client
6889 * @param message the actual message
6892 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6893 const struct GNUNET_MessageHeader *message)
6895 struct GNUNET_MESH_ToOrigin *data_msg;
6896 struct GNUNET_PeerIdentity id;
6897 struct MeshClient *c;
6898 struct MeshTunnel *t;
6899 MESH_TunnelNumber tid;
6902 /* Sanity check for client registration */
6903 if (NULL == (c = client_get (client)))
6906 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6909 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6910 /* Sanity check for message size */
6911 size = ntohs (message->size);
6912 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6913 sizeof (struct GNUNET_MessageHeader) > size)
6916 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6920 /* Tunnel exists? */
6921 tid = ntohl (data_msg->tid);
6922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6923 "Got a ToOrigin request from a client! Tunnel %X\n", tid);
6924 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6930 t = tunnel_get_by_local_id (c, tid);
6934 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6938 /* It should be sent by someone who has this as incoming tunnel. */
6939 if (GNUNET_NO == client_knows_tunnel (c, t))
6942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6945 GNUNET_PEER_resolve (t->id.oid, &id);
6947 /* Ok, everything is correct, send the message
6948 * (pretend we got it from a mesh peer)
6951 char buf[ntohs (message->size)] GNUNET_ALIGN;
6952 struct GNUNET_MESH_ToOrigin *copy;
6954 /* Work around const limitation */
6955 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6956 memcpy (buf, data_msg, size);
6958 copy->tid = htonl (t->id.tid);
6959 copy->ttl = htonl (default_ttl);
6960 GNUNET_assert (ntohl (copy->pid) == (t->bck_pid + 1));
6961 copy->sender = my_full_id;
6962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6963 " calling generic handler...\n");
6964 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6966 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6972 * Handler for client traffic directed to all peers in a tunnel
6974 * @param cls closure
6975 * @param client identification of the client
6976 * @param message the actual message
6979 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6980 const struct GNUNET_MessageHeader *message)
6982 struct MeshClient *c;
6983 struct MeshTunnel *t;
6984 struct GNUNET_MESH_Multicast *data_msg;
6985 MESH_TunnelNumber tid;
6987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6988 "Got a multicast request from a client!\n");
6990 /* Sanity check for client registration */
6991 if (NULL == (c = client_get (client)))
6994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6997 data_msg = (struct GNUNET_MESH_Multicast *) message;
6998 /* Sanity check for message size */
6999 if (sizeof (struct GNUNET_MESH_Multicast) +
7000 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7007 /* Tunnel exists? */
7008 tid = ntohl (data_msg->tid);
7009 t = tunnel_get_by_local_id (c, tid);
7013 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7017 /* Does client own tunnel? */
7018 if (t->owner->handle != client)
7021 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7026 char buf[ntohs (message->size)] GNUNET_ALIGN;
7027 struct GNUNET_MESH_Multicast *copy;
7029 copy = (struct GNUNET_MESH_Multicast *) buf;
7030 memcpy (buf, message, ntohs (message->size));
7031 copy->oid = my_full_id;
7032 copy->tid = htonl (t->id.tid);
7033 copy->ttl = htonl (default_ttl);
7034 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7036 " calling generic handler...\n");
7037 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7040 /* receive done gets called when last copy is sent to a neighbor */
7046 * Handler for client's ACKs for payload traffic.
7048 * @param cls Closure (unused).
7049 * @param client Identification of the client.
7050 * @param message The actual message.
7053 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7054 const struct GNUNET_MessageHeader *message)
7056 struct GNUNET_MESH_LocalAck *msg;
7057 struct MeshTunnel *t;
7058 struct MeshClient *c;
7059 MESH_TunnelNumber tid;
7061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7062 /* Sanity check for client registration */
7063 if (NULL == (c = client_get (client)))
7066 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7070 msg = (struct GNUNET_MESH_LocalAck *) message;
7071 /* Tunnel exists? */
7072 tid = ntohl (msg->tunnel_id);
7073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7074 t = tunnel_get_by_local_id (c, tid);
7078 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7082 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7083 if (NULL != t->owner && t->owner->handle == client)
7085 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7086 t->bck_ack = ntohl(msg->max_pid);
7087 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7091 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7092 tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
7093 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7096 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7102 * Functions to handle messages from clients
7104 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7105 {&handle_local_new_client, NULL,
7106 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7107 {&handle_local_announce_regex, NULL,
7108 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7109 {&handle_local_tunnel_create, NULL,
7110 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7111 sizeof (struct GNUNET_MESH_TunnelMessage)},
7112 {&handle_local_tunnel_destroy, NULL,
7113 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7114 sizeof (struct GNUNET_MESH_TunnelMessage)},
7115 {&handle_local_tunnel_speed, NULL,
7116 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7117 sizeof (struct GNUNET_MESH_TunnelMessage)},
7118 {&handle_local_tunnel_speed, NULL,
7119 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7120 sizeof (struct GNUNET_MESH_TunnelMessage)},
7121 {&handle_local_tunnel_buffer, NULL,
7122 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7123 sizeof (struct GNUNET_MESH_TunnelMessage)},
7124 {&handle_local_tunnel_buffer, NULL,
7125 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7126 sizeof (struct GNUNET_MESH_TunnelMessage)},
7127 {&handle_local_connect_add, NULL,
7128 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7129 sizeof (struct GNUNET_MESH_PeerControl)},
7130 {&handle_local_connect_del, NULL,
7131 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7132 sizeof (struct GNUNET_MESH_PeerControl)},
7133 {&handle_local_blacklist, NULL,
7134 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7135 sizeof (struct GNUNET_MESH_PeerControl)},
7136 {&handle_local_unblacklist, NULL,
7137 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7138 sizeof (struct GNUNET_MESH_PeerControl)},
7139 {&handle_local_connect_by_type, NULL,
7140 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7141 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7142 {&handle_local_connect_by_string, NULL,
7143 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7144 {&handle_local_unicast, NULL,
7145 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7146 {&handle_local_to_origin, NULL,
7147 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7148 {&handle_local_multicast, NULL,
7149 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7150 {&handle_local_ack, NULL,
7151 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7152 sizeof (struct GNUNET_MESH_LocalAck)},
7158 * To be called on core init/fail.
7160 * @param cls service closure
7161 * @param server handle to the server for this service
7162 * @param identity the public identity of this peer
7165 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7166 const struct GNUNET_PeerIdentity *identity)
7168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7169 core_handle = server;
7170 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7174 GNUNET_SCHEDULER_shutdown ();
7181 * Method called whenever a given peer connects.
7183 * @param cls closure
7184 * @param peer peer identity this notification is about
7185 * @param atsi performance data for the connection
7186 * @param atsi_count number of records in 'atsi'
7189 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7190 const struct GNUNET_ATS_Information *atsi,
7191 unsigned int atsi_count)
7193 struct MeshPeerInfo *peer_info;
7194 struct MeshPeerPath *path;
7196 DEBUG_CONN ("Peer connected\n");
7197 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7198 peer_info = peer_info_get (peer);
7199 if (myid == peer_info->id)
7201 DEBUG_CONN (" (self)\n");
7206 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7208 path = path_new (2);
7209 path->peers[0] = myid;
7210 path->peers[1] = peer_info->id;
7211 GNUNET_PEER_change_rc (myid, 1);
7212 GNUNET_PEER_change_rc (peer_info->id, 1);
7213 peer_info_add_path (peer_info, path, GNUNET_YES);
7214 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7220 * Method called whenever a peer disconnects.
7222 * @param cls closure
7223 * @param peer peer identity this notification is about
7226 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7228 struct MeshPeerInfo *pi;
7229 struct MeshPeerQueue *q;
7230 struct MeshPeerQueue *n;
7232 DEBUG_CONN ("Peer disconnected\n");
7233 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7245 /* try to reroute this traffic instead */
7246 queue_destroy(q, GNUNET_YES);
7250 peer_info_remove_path (pi, pi->id, myid);
7253 DEBUG_CONN (" (self)\n");
7255 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7260 /******************************************************************************/
7261 /************************ MAIN FUNCTIONS ****************************/
7262 /******************************************************************************/
7265 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7267 * @param cls closure
7268 * @param key current key code
7269 * @param value value in the hash map
7270 * @return GNUNET_YES if we should continue to iterate,
7274 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7276 struct MeshTunnel *t = value;
7283 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7285 * @param cls closure
7286 * @param key current key code
7287 * @param value value in the hash map
7288 * @return GNUNET_YES if we should continue to iterate,
7292 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7294 struct MeshPeerInfo *p = value;
7295 struct MeshPeerQueue *q;
7296 struct MeshPeerQueue *n;
7304 queue_destroy(q, GNUNET_YES);
7308 peer_info_destroy (p);
7313 * Task run during shutdown.
7319 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7323 if (core_handle != NULL)
7325 GNUNET_CORE_disconnect (core_handle);
7328 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7329 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7330 if (dht_handle != NULL)
7332 GNUNET_DHT_disconnect (dht_handle);
7337 GNUNET_SERVER_notification_context_destroy (nc);
7340 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7342 GNUNET_SCHEDULER_cancel (announce_id_task);
7343 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7349 * Process mesh requests.
7351 * @param cls closure
7352 * @param server the initialized server
7353 * @param c configuration to use
7356 run (void *cls, struct GNUNET_SERVER_Handle *server,
7357 const struct GNUNET_CONFIGURATION_Handle *c)
7359 struct MeshPeerInfo *peer;
7360 struct MeshPeerPath *p;
7363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7364 server_handle = server;
7365 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7366 NULL, /* Closure passed to MESH functions */
7367 &core_init, /* Call core_init once connected */
7368 &core_connect, /* Handle connects */
7369 &core_disconnect, /* remove peers on disconnects */
7370 NULL, /* Don't notify about all incoming messages */
7371 GNUNET_NO, /* For header only in notification */
7372 NULL, /* Don't notify about all outbound messages */
7373 GNUNET_NO, /* For header-only out notification */
7374 core_handlers); /* Register these handlers */
7376 if (core_handle == NULL)
7379 GNUNET_SCHEDULER_shutdown ();
7384 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7389 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7391 GNUNET_SCHEDULER_shutdown ();
7396 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7397 &refresh_path_time))
7399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7401 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7402 "refresh path time");
7403 GNUNET_SCHEDULER_shutdown ();
7408 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7409 &app_announce_time))
7411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7413 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7414 "app announce time");
7415 GNUNET_SCHEDULER_shutdown ();
7420 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7425 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7426 "id announce time");
7427 GNUNET_SCHEDULER_shutdown ();
7432 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7433 &unacknowledged_wait_time))
7435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7437 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7438 "unacknowledged wait time");
7439 GNUNET_SCHEDULER_shutdown ();
7444 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7449 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7451 GNUNET_SCHEDULER_shutdown ();
7456 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7461 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7463 GNUNET_SCHEDULER_shutdown ();
7468 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7473 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7475 GNUNET_SCHEDULER_shutdown ();
7480 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7483 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7485 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7491 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7492 &dht_replication_level))
7494 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7496 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7497 "dht replication level", 10);
7498 dht_replication_level = 10;
7502 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7503 GNUNET_free (keyfile);
7504 if (my_private_key == NULL)
7506 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7507 _("Mesh service could not access hostkey. Exiting.\n"));
7508 GNUNET_SCHEDULER_shutdown ();
7511 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7512 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7513 &my_full_id.hashPubKey);
7514 myid = GNUNET_PEER_intern (&my_full_id);
7516 // transport_handle = GNUNET_TRANSPORT_connect(c,
7523 dht_handle = GNUNET_DHT_connect (c, 64);
7524 if (dht_handle == NULL)
7529 stats = GNUNET_STATISTICS_create ("mesh", c);
7533 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7535 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7536 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7537 peers = GNUNET_CONTAINER_multihashmap_create (32);
7538 applications = GNUNET_CONTAINER_multihashmap_create (32);
7539 types = GNUNET_CONTAINER_multihashmap_create (32);
7541 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7542 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7543 GNUNET_SERVER_disconnect_notify (server_handle,
7544 &handle_local_client_disconnect, NULL);
7548 clients_tail = NULL;
7551 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7552 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7554 /* Create a peer_info for the local peer */
7555 peer = peer_info_get (&my_full_id);
7558 GNUNET_PEER_change_rc (myid, 1);
7559 peer_info_add_path (peer, p, GNUNET_YES);
7561 /* Scheduled the task to clean up when shutdown is called */
7562 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7569 * The main function for the mesh service.
7571 * @param argc number of arguments from the command line
7572 * @param argv command line arguments
7573 * @return 0 ok, 1 on error
7576 main (int argc, char *const *argv)
7580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7583 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");