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);
1078 * Add a client to a tunnel, initializing all needed data structures.
1080 * @param t Tunnel to which add the client.
1081 * @param c Client which to add to the tunnel.
1084 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1088 * Iterator over edges in a regex block retrieved from the DHT.
1090 * @param cls Closure.
1091 * @param token Token that follows to next state.
1092 * @param len Lenght of token.
1093 * @param key Hash of next state.
1095 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1098 regex_edge_iterator (void *cls,
1101 const struct GNUNET_HashCode *key);
1105 * Find a path to a peer that offers a regex servcie compatible
1106 * with a given string.
1108 * @param key The key of the accepting state.
1109 * @param ctx Context containing info about the string, tunnel, etc.
1112 regex_find_path (const struct GNUNET_HashCode *key,
1113 struct MeshRegexSearchContext *ctx);
1117 * Queue and pass message to core when possible.
1119 * @param cls Closure (type dependant).
1120 * @param type Type of the message, 0 for a raw message.
1121 * @param size Size of the message.
1122 * @param dst Neighbor to send message to.
1123 * @param t Tunnel this message belongs to.
1126 queue_add (void *cls, uint16_t type, size_t size,
1127 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1130 * Free a transmission that was already queued with all resources
1131 * associated to the request.
1133 * @param queue Queue handler to cancel.
1134 * @param clear_cls Is it necessary to free associated cls?
1137 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1139 /******************************************************************************/
1140 /************************ ITERATORS ****************************/
1141 /******************************************************************************/
1144 * Iterator over found existing mesh regex blocks that match an ongoing search.
1146 * @param cls closure
1147 * @param key current key code
1148 * @param value value in the hash map
1149 * @return GNUNET_YES if we should continue to iterate,
1153 regex_result_iterator (void *cls,
1154 const struct GNUNET_HashCode * key,
1157 struct MeshRegexBlock *block = value;
1158 struct MeshRegexSearchContext *ctx = cls;
1160 if (GNUNET_YES == ntohl(block->accepting) &&
1161 ctx->position == strlen (ctx->info->description))
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1164 regex_find_path (key, ctx);
1165 return GNUNET_YES; // We found an accept state!
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1170 ctx->position, strlen(ctx->info->description),
1171 ntohl(block->accepting));
1174 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1175 ®ex_edge_iterator, ctx);
1182 * Iterator over edges in a regex block retrieved from the DHT.
1184 * @param cls Closure (context of the search).
1185 * @param token Token that follows to next state.
1186 * @param len Lenght of token.
1187 * @param key Hash of next state.
1189 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1192 regex_edge_iterator (void *cls,
1195 const struct GNUNET_HashCode *key)
1197 struct MeshRegexSearchContext *ctx = cls;
1198 struct MeshRegexSearchContext *new_ctx;
1199 struct MeshRegexSearchInfo *info = ctx->info;
1200 struct GNUNET_DHT_GetHandle *get_h;
1204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1207 current = &info->description[ctx->position];
1208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1209 current_len = strlen (info->description) - ctx->position;
1210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1214 if (len > current_len)
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1217 return GNUNET_YES; // Token too long, wont match
1219 if (0 != strncmp (current, token, len))
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1222 return GNUNET_YES; // Token doesn't match
1224 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1225 new_ctx->info = info;
1226 new_ctx->position = ctx->position + len;
1227 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1229 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1232 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1233 ®ex_result_iterator,
1235 return GNUNET_YES; // We are already looking for it
1237 /* Start search in DHT */
1239 GNUNET_DHT_get_start (dht_handle, /* handle */
1240 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1241 key, /* key to search */
1242 dht_replication_level, /* replication level */
1243 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1244 NULL, /* xquery */ // FIXME BLOOMFILTER
1245 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1246 &dht_get_string_handler, new_ctx);
1248 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1249 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1259 * Iterator over hash map entries to cancel DHT GET requests after a
1260 * successful connect_by_string.
1262 * @param cls Closure (unused).
1263 * @param key Current key code (unused).
1264 * @param value Value in the hash map (get handle).
1265 * @return GNUNET_YES if we should continue to iterate,
1269 regex_cancel_dht_get (void *cls,
1270 const struct GNUNET_HashCode * key,
1273 struct GNUNET_DHT_GetHandle *h = value;
1275 GNUNET_DHT_get_stop (h);
1281 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1282 * search for connect_by_string.
1284 * @param cls Closure (unused).
1285 * @param key Current key code (unused).
1286 * @param value MeshRegexBlock in the hash map.
1287 * @return GNUNET_YES if we should continue to iterate,
1291 regex_free_result (void *cls,
1292 const struct GNUNET_HashCode * key,
1296 GNUNET_free (value);
1302 * Regex callback iterator to store own service description in the DHT.
1304 * @param cls closure.
1305 * @param key hash for current state.
1306 * @param proof proof for current state.
1307 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1308 * @param num_edges number of edges leaving current state.
1309 * @param edges edges leaving current state.
1312 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1313 int accepting, unsigned int num_edges,
1314 const struct GNUNET_REGEX_Edge *edges)
1316 struct MeshRegexBlock *block;
1317 struct MeshRegexEdge *block_edge;
1318 enum GNUNET_DHT_RouteOption opt;
1322 unsigned int offset;
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 " regex dht put for state %s\n",
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1336 if (GNUNET_YES == accepting)
1338 struct MeshRegexAccept block;
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341 " state %s is accepting, putting own id\n",
1343 size = sizeof (block);
1345 block.id = my_full_id;
1347 GNUNET_DHT_put(dht_handle, key,
1348 dht_replication_level,
1349 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1350 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1353 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1358 len = strlen(proof);
1359 size = sizeof (struct MeshRegexBlock) + len;
1360 block = GNUNET_malloc (size);
1363 block->n_proof = htonl (len);
1364 block->n_edges = htonl (num_edges);
1365 block->accepting = htonl (accepting);
1367 /* Store the proof at the end of the block. */
1368 aux = (char *) &block[1];
1369 memcpy (aux, proof, len);
1372 /* Store each edge in a variable length MeshEdge struct at the
1373 * very end of the MeshRegexBlock structure.
1375 for (i = 0; i < num_edges; i++)
1377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378 " edge %s towards %s\n",
1380 GNUNET_h2s(&edges[i].destination));
1382 /* aux points at the end of the last block */
1383 len = strlen (edges[i].label);
1384 size += sizeof (struct MeshRegexEdge) + len;
1385 // Calculate offset FIXME is this ok? use size instead?
1386 offset = aux - (char *) block;
1387 block = GNUNET_realloc (block, size);
1388 aux = &((char *) block)[offset];
1389 block_edge = (struct MeshRegexEdge *) aux;
1390 block_edge->key = edges[i].destination;
1391 block_edge->n_token = htonl (len);
1392 aux = (char *) &block_edge[1];
1393 memcpy (aux, edges[i].label, len);
1397 GNUNET_DHT_put(dht_handle, key,
1398 dht_replication_level,
1400 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1402 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1406 GNUNET_free (block);
1411 * Store the regular expression describing a local service into the DHT.
1413 * @param regex The regular expresion.
1416 regex_put (const char *regex)
1418 struct GNUNET_REGEX_Automaton *dfa;
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1421 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1422 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1423 GNUNET_REGEX_automaton_destroy (dfa);
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1429 * Find a path to a peer that offers a regex servcie compatible
1430 * with a given string.
1432 * @param key The key of the accepting state.
1433 * @param ctx Context containing info about the string, tunnel, etc.
1436 regex_find_path (const struct GNUNET_HashCode *key,
1437 struct MeshRegexSearchContext *ctx)
1439 struct GNUNET_DHT_GetHandle *get_h;
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1442 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1443 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1444 key, /* key to search */
1445 dht_replication_level, /* replication level */
1446 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1447 GNUNET_DHT_RO_RECORD_ROUTE,
1448 NULL, /* xquery */ // FIXME BLOOMFILTER
1449 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1450 &dht_get_string_accept_handler, ctx);
1451 GNUNET_break (GNUNET_OK ==
1452 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1460 * Function called if the connect attempt to a peer found via
1461 * connect_by_string times out. Try to connect to another peer, if any.
1462 * Otherwise try to reconnect to the same peer.
1464 * @param cls Closure (info about regex search).
1465 * @param tc TaskContext.
1468 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1470 struct MeshRegexSearchInfo *info = cls;
1471 struct MeshPeerInfo *peer_info;
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1476 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1477 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1485 if (0 < info->n_peers)
1487 // Select next peer, put current in that spot.
1488 id = info->peers[info->i_peer];
1489 info->peers[info->i_peer] = info->peer;
1490 info->i_peer = (info->i_peer + 1) % info->n_peers;
1494 // Try to connect to same peer again.
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1499 peer_info = peer_info_get_short(id);
1500 tunnel_add_peer (info->t, peer_info);
1502 tunnel_delete_peer (info->t, old);
1503 peer_info_connect (peer_info, info->t);
1504 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1505 ®ex_connect_timeout,
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1512 * Cancel an ongoing regex search in the DHT and free all resources.
1514 * @param ctx The search context.
1517 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1519 struct MeshRegexSearchInfo *info = ctx->info;
1522 GNUNET_free (info->description);
1523 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1524 ®ex_cancel_dht_get, NULL);
1525 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1526 ®ex_free_result, NULL);
1527 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1528 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1529 info->t->regex_ctx = NULL;
1530 for (i = 0; i < info->n_contexts; i++)
1532 GNUNET_free (info->contexts[i]);
1534 if (0 < info->n_contexts)
1535 GNUNET_free (info->contexts);
1536 if (0 < info->n_peers)
1537 GNUNET_free (info->peers);
1538 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1540 GNUNET_SCHEDULER_cancel(info->timeout);
1546 /******************************************************************************/
1547 /************************ PERIODIC FUNCTIONS ****************************/
1548 /******************************************************************************/
1551 * Announce iterator over for each application provided by the peer
1553 * @param cls closure
1554 * @param key current key code
1555 * @param value value in the hash map
1556 * @return GNUNET_YES if we should continue to
1561 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1563 struct PBlock block;
1564 struct MeshClient *c;
1566 block.id = my_full_id;
1567 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1568 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1569 if (0 == block.type)
1574 block.type = htonl (block.type);
1576 GNUNET_break (NULL !=
1577 GNUNET_DHT_put (dht_handle, key,
1578 dht_replication_level,
1579 GNUNET_DHT_RO_RECORD_ROUTE |
1580 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1581 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1583 (const char *) &block,
1584 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1586 app_announce_time, NULL, NULL));
1592 * Periodically announce what applications are provided by local clients
1595 * @param cls closure
1596 * @param tc task context
1599 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1601 struct MeshClient *c = cls;
1604 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1606 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1610 DEBUG_DHT ("Starting PUT for regex\n");
1612 for (i = 0; i < c->n_regex; i++)
1614 regex_put (c->regexes[i]);
1616 c->regex_announce_task =
1617 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1618 DEBUG_DHT ("Finished PUT for regex\n");
1625 * Periodically announce what applications are provided by local clients
1628 * @param cls closure
1629 * @param tc task context
1632 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1634 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1636 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1640 DEBUG_DHT ("Starting PUT for apps\n");
1642 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1644 announce_applications_task =
1645 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1647 DEBUG_DHT ("Finished PUT for apps\n");
1654 * Periodically announce self id in the DHT
1656 * @param cls closure
1657 * @param tc task context
1660 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1662 struct PBlock block;
1664 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1666 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1670 * - Set data expiration in function of X
1671 * - Adapt X to churn
1673 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1675 block.id = my_full_id;
1676 block.type = htonl (0);
1677 GNUNET_DHT_put (dht_handle, /* DHT handle */
1678 &my_full_id.hashPubKey, /* Key to use */
1679 dht_replication_level, /* Replication level */
1680 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1681 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1682 sizeof (block), /* Size of the data */
1683 (const char *) &block, /* Data itself */
1684 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1685 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1686 NULL, /* Continuation */
1687 NULL); /* Continuation closure */
1689 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1693 /******************************************************************************/
1694 /****************** GENERAL HELPER FUNCTIONS ************************/
1695 /******************************************************************************/
1698 * Decrements the reference counter and frees all resources if needed
1700 * @param mesh_data Data Descriptor used in a multicast message.
1701 * Freed no longer needed (last message).
1704 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1706 /* Make sure it's a multicast packet */
1707 GNUNET_assert (NULL != mesh_data->reference_counter);
1709 if (0 == --(*(mesh_data->reference_counter)))
1711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1712 if (NULL != mesh_data->task)
1714 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1716 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1718 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1720 GNUNET_free (mesh_data->task);
1722 GNUNET_free (mesh_data->reference_counter);
1723 GNUNET_free (mesh_data->data);
1724 GNUNET_free (mesh_data);
1730 * Check if client has registered with the service and has not disconnected
1732 * @param client the client to check
1734 * @return non-NULL if client exists in the global DLL
1736 static struct MeshClient *
1737 client_get (struct GNUNET_SERVER_Client *client)
1739 struct MeshClient *c;
1744 if (c->handle == client)
1753 * Checks if a given client has subscribed to certain message type
1755 * @param message_type Type of message to check
1756 * @param c Client to check
1758 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1761 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1763 struct GNUNET_HashCode hc;
1765 if (NULL == c->types)
1767 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1768 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1773 * Allow a client to send more data after transmitting a multicast message
1774 * which some neighbor has not yet accepted altough a reasonable time has
1777 * @param cls Closure (DataDescriptor containing the task identifier)
1778 * @param tc Task Context
1780 * FIXME reference counter cshould be just int
1783 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1785 struct MeshData *mdata = cls;
1787 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1789 GNUNET_assert (NULL != mdata->reference_counter);
1790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1791 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1792 *(mdata->reference_counter));
1793 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1794 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1799 * Check whether client wants traffic from a tunnel.
1801 * @param c Client to check.
1802 * @param t Tunnel to be found.
1804 * @return GNUNET_YES if client knows tunnel.
1806 * TODO look in client hashmap
1809 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1813 for (i = 0; i < t->nclients; i++)
1814 if (t->clients[i] == c)
1821 * Check whether client has been informed about a tunnel.
1823 * @param c Client to check.
1824 * @param t Tunnel to be found.
1826 * @return GNUNET_YES if client knows tunnel.
1828 * TODO look in client hashmap
1831 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1835 for (i = 0; i < t->nignore; i++)
1836 if (t->ignore[i] == c)
1838 return client_wants_tunnel(c, t);
1843 * Marks a client as uninterested in traffic from the tunnel, updating both
1844 * client and tunnel to reflect this.
1846 * @param c Client that doesn't want traffic anymore.
1847 * @param t Tunnel which should be ignored.
1849 * FIXME when to delete an incoming tunnel?
1852 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1854 struct GNUNET_HashCode hash;
1856 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1857 GNUNET_break (GNUNET_YES ==
1858 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1860 GNUNET_break (GNUNET_YES ==
1861 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1862 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1863 tunnel_delete_active_client (t, c);
1864 GNUNET_array_append (t->ignore, t->nignore, c);
1869 * Deletes a tunnel from a client (either owner or destination). To be used on
1870 * tunnel destroy, otherwise, use client_ignore_tunnel.
1872 * @param c Client whose tunnel to delete.
1873 * @param t Tunnel which should be deleted.
1876 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1878 struct GNUNET_HashCode hash;
1882 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1883 GNUNET_assert (GNUNET_YES ==
1884 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1890 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1892 GNUNET_assert (GNUNET_YES ==
1893 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1897 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1905 * Send the message to all clients that have subscribed to its type
1907 * @param msg Pointer to the message itself
1908 * @param payload Pointer to the payload of the message.
1909 * @return number of clients this message was sent to
1912 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1913 const struct GNUNET_MessageHeader *payload)
1915 struct GNUNET_PeerIdentity *oid;
1916 struct MeshClient *c;
1917 struct MeshTunnel *t;
1918 MESH_TunnelNumber *tid;
1921 char cbuf[htons (msg->size)];
1923 type = ntohs (payload->type);
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1926 GNUNET_MESH_DEBUG_M2S (type));
1928 memcpy (cbuf, msg, sizeof (cbuf));
1929 switch (htons (msg->type))
1931 struct GNUNET_MESH_Unicast *uc;
1932 struct GNUNET_MESH_Multicast *mc;
1933 struct GNUNET_MESH_ToOrigin *to;
1935 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1936 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1940 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1941 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1945 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1946 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1954 t = tunnel_get (oid, ntohl (*tid));
1961 for (count = 0, c = clients; c != NULL; c = c->next)
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1964 if (client_is_subscribed (type, c))
1966 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1970 *tid = htonl (t->local_tid);
1974 if (GNUNET_NO == client_knows_tunnel (c, t))
1976 /* This client doesn't know the tunnel */
1977 struct GNUNET_MESH_TunnelNotification tmsg;
1978 struct GNUNET_HashCode hash;
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1981 tmsg.header.size = htons (sizeof (tmsg));
1982 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1983 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1984 tmsg.tunnel_id = htonl (t->local_tid_dest);
1985 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1986 &tmsg.header, GNUNET_NO);
1987 tunnel_add_client (t, c);
1988 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1990 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1991 c->incoming_tunnels, &hash, t,
1992 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1994 *tid = htonl (t->local_tid_dest);
1997 /* Check if the client wants to get traffic from the tunnel */
1998 if (GNUNET_NO == client_wants_tunnel(c, t))
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2002 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2003 (struct GNUNET_MessageHeader
2004 *) cbuf, GNUNET_NO);
2012 * Notify the client that owns the tunnel that a peer has connected to it
2013 * (the requested path to it has been confirmed).
2015 * @param t Tunnel whose owner to notify
2016 * @param id Short id of the peer that has connected
2019 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2021 struct GNUNET_MESH_PeerControl pc;
2023 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2024 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2025 pc.tunnel_id = htonl (t->local_tid);
2026 GNUNET_PEER_resolve (id, &pc.peer);
2027 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2033 * Notify all clients (not depending on registration status) that the incoming
2034 * tunnel is no longer valid.
2036 * @param t Tunnel that was destroyed.
2039 send_clients_tunnel_destroy (struct MeshTunnel *t)
2041 struct GNUNET_MESH_TunnelMessage msg;
2043 msg.header.size = htons (sizeof (msg));
2044 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2045 msg.tunnel_id = htonl (t->local_tid_dest);
2046 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2051 * Notify clients of tunnel disconnections, if needed.
2052 * In case the origin disconnects, the destination clients get a tunnel destroy
2053 * notification. If the last destination disconnects (only one remaining client
2054 * in tunnel), the origin gets a (local ID) peer disconnected.
2055 * Note that the function must be called BEFORE removing the client from
2058 * @param t Tunnel that was destroyed.
2059 * @param c Client that disconnected.
2062 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2068 struct GNUNET_MESH_TunnelMessage msg;
2070 msg.header.size = htons (sizeof (msg));
2071 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2072 msg.tunnel_id = htonl (t->local_tid_dest);
2073 for (i = 0; i < t->nclients; i++)
2074 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2075 &msg.header, GNUNET_NO);
2077 // FIXME when to disconnect an incoming tunnel?
2078 else if (1 == t->nclients && NULL != t->owner)
2080 struct GNUNET_MESH_PeerControl msg;
2082 msg.header.size = htons (sizeof (msg));
2083 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2084 msg.tunnel_id = htonl (t->local_tid);
2085 msg.peer = my_full_id;
2086 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2087 &msg.header, GNUNET_NO);
2093 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2094 * and insert it in the appropiate structures if the peer is not known yet.
2096 * @param peer Full identity of the peer.
2098 * @return Existing or newly created peer info.
2100 static struct MeshPeerInfo *
2101 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2103 struct MeshPeerInfo *peer_info;
2105 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2106 if (NULL == peer_info)
2109 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2110 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2111 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2112 peer_info->id = GNUNET_PEER_intern (peer);
2120 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2121 * and insert it in the appropiate structures if the peer is not known yet.
2123 * @param peer Short identity of the peer.
2125 * @return Existing or newly created peer info.
2127 static struct MeshPeerInfo *
2128 peer_info_get_short (const GNUNET_PEER_Id peer)
2130 struct GNUNET_PeerIdentity id;
2132 GNUNET_PEER_resolve (peer, &id);
2133 return peer_info_get (&id);
2138 * Iterator to remove the tunnel from the list of tunnels a peer participates
2141 * @param cls Closure (tunnel info)
2142 * @param key GNUNET_PeerIdentity of the peer (unused)
2143 * @param value PeerInfo of the peer
2145 * @return always GNUNET_YES, to keep iterating
2148 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2150 struct MeshTunnel *t = cls;
2151 struct MeshPeerInfo *peer = value;
2154 for (i = 0; i < peer->ntunnels; i++)
2157 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2160 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2161 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2170 * Core callback to write a pre-constructed data packet to core buffer
2172 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2173 * @param size Number of bytes available in buf.
2174 * @param buf Where the to write the message.
2176 * @return number of bytes written to buf
2179 send_core_data_raw (void *cls, size_t size, void *buf)
2181 struct MeshTransmissionDescriptor *info = cls;
2182 struct GNUNET_MessageHeader *msg;
2185 GNUNET_assert (NULL != info);
2186 GNUNET_assert (NULL != info->mesh_data);
2187 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2188 total_size = ntohs (msg->size);
2190 if (total_size > size)
2195 memcpy (buf, msg, total_size);
2196 data_descriptor_decrement_rc (info->mesh_data);
2203 * Sends an already built non-multicast message to a peer,
2204 * properly registrating all used resources.
2206 * @param message Message to send. Function makes a copy of it.
2207 * @param peer Short ID of the neighbor whom to send the message.
2208 * @param t Tunnel on which this message is transmitted.
2211 send_message (const struct GNUNET_MessageHeader *message,
2212 const struct GNUNET_PeerIdentity *peer,
2213 struct MeshTunnel *t)
2215 struct MeshTransmissionDescriptor *info;
2216 struct MeshPeerInfo *neighbor;
2217 struct MeshPeerPath *p;
2221 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2223 size = ntohs (message->size);
2224 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2225 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2226 info->mesh_data->data = GNUNET_malloc (size);
2227 memcpy (info->mesh_data->data, message, size);
2228 type = ntohs(message->type);
2229 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2231 struct GNUNET_MESH_Unicast *m;
2233 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2234 m->ttl = htonl (ntohl (m->ttl) - 1);
2236 info->mesh_data->data_len = size;
2237 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2238 *info->mesh_data->reference_counter = 1;
2239 neighbor = peer_info_get (peer);
2240 for (p = neighbor->path_head; NULL != p; p = p->next)
2250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2251 " %s IS NOT DIRECTLY CONNECTED\n",
2253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 for (p = neighbor->path_head; NULL != p; p = p->next)
2258 struct GNUNET_PeerIdentity debug_id;
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262 " path with %u hops through:\n",
2264 for (i = 0; i < p->length; i++)
2266 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2269 i, GNUNET_i2s(&debug_id));
2273 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2274 GNUNET_free (info->mesh_data->data);
2275 GNUNET_free (info->mesh_data);
2279 info->peer = neighbor;
2280 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2283 * TODO: in this case we only need the service to retransmit
2284 * the message down the path. If we pass the real type to queue_add,
2285 * queue_send will try to build the message from scratch. This can
2286 * probably be done by some other way instead of deleteing the type
2300 * Sends a CREATE PATH message for a path to a peer, properly registrating
2301 * all used resources.
2303 * @param peer PeerInfo of the final peer for whom this path is being created.
2304 * @param p Path itself.
2305 * @param t Tunnel for which the path is created.
2308 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2309 struct MeshTunnel *t)
2311 struct GNUNET_PeerIdentity id;
2312 struct MeshPathInfo *path_info;
2313 struct MeshPeerInfo *neighbor;
2319 p = tree_get_path_to_peer (t->tree, peer->id);
2326 for (i = 0; i < p->length; i++)
2328 if (p->peers[i] == myid)
2331 if (i >= p->length - 1)
2337 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2339 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2340 path_info->path = p;
2342 neighbor = peer_info_get (&id);
2343 path_info->peer = neighbor;
2344 queue_add (path_info,
2345 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2346 sizeof (struct GNUNET_MESH_ManipulatePath) +
2347 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2354 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2356 * @param t Tunnel whose path to destroy.
2357 * @param destination Short ID of the peer to whom the path to destroy.
2360 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2362 struct MeshPeerPath *p;
2365 p = tree_get_path_to_peer (t->tree, destination);
2371 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2372 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2374 struct GNUNET_MESH_ManipulatePath *msg;
2375 struct GNUNET_PeerIdentity *pi;
2379 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2380 msg->header.size = htons (size);
2381 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2382 msg->tid = htonl (t->id.tid);
2383 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2384 for (i = 0; i < p->length; i++)
2386 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2388 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2395 * Try to establish a new connection to this peer.
2396 * Use the best path for the given tunnel.
2397 * If the peer doesn't have any path to it yet, try to get one.
2398 * If the peer already has some path, send a CREATE PATH towards it.
2400 * @param peer PeerInfo of the peer.
2401 * @param t Tunnel for which to create the path, if possible.
2404 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2406 struct MeshPeerPath *p;
2407 struct MeshPathInfo *path_info;
2409 if (NULL != peer->path_head)
2411 p = tree_get_path_to_peer (t->tree, peer->id);
2418 // FIXME always send create path to self
2421 send_create_path (peer, p, t);
2425 struct GNUNET_HashCode hash;
2428 send_client_peer_connected (t, myid);
2429 t->local_tid_dest = next_local_tid++;
2430 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2433 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2434 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2441 else if (NULL == peer->dhtget)
2443 struct GNUNET_PeerIdentity id;
2445 GNUNET_PEER_resolve (peer->id, &id);
2446 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2447 path_info->peer = peer;
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2450 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2451 peer->dhtgetcls = path_info;
2452 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2453 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2454 &id.hashPubKey, /* key to search */
2455 dht_replication_level, /* replication level */
2456 GNUNET_DHT_RO_RECORD_ROUTE |
2457 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2458 NULL, /* xquery */ // FIXME BLOOMFILTER
2459 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2460 &dht_get_id_handler, path_info);
2462 /* Otherwise, there is no path but the DHT get is already started. */
2467 * Task to delay the connection of a peer
2469 * @param cls Closure (path info with tunnel and peer to connect).
2470 * Will be free'd on exection.
2471 * @param tc TaskContext
2474 peer_info_connect_task (void *cls,
2475 const struct GNUNET_SCHEDULER_TaskContext *tc)
2477 struct MeshPathInfo *path_info = cls;
2479 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2484 peer_info_connect (path_info->peer, path_info->t);
2490 * Destroy the peer_info and free any allocated resources linked to it
2492 * @param pi The peer_info to destroy.
2494 * @return GNUNET_OK on success
2497 peer_info_destroy (struct MeshPeerInfo *pi)
2499 struct GNUNET_PeerIdentity id;
2500 struct MeshPeerPath *p;
2501 struct MeshPeerPath *nextp;
2503 GNUNET_PEER_resolve (pi->id, &id);
2504 GNUNET_PEER_change_rc (pi->id, -1);
2507 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2511 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2513 if (NULL != pi->dhtget)
2515 GNUNET_DHT_get_stop (pi->dhtget);
2516 GNUNET_free (pi->dhtgetcls);
2522 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2532 * Remove all paths that rely on a direct connection between p1 and p2
2533 * from the peer itself and notify all tunnels about it.
2535 * @param peer PeerInfo of affected peer.
2536 * @param p1 GNUNET_PEER_Id of one peer.
2537 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2540 * TODO: optimize (see below)
2543 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2546 struct MeshPeerPath *p;
2547 struct MeshPeerPath *aux;
2548 struct MeshPeerInfo *peer_d;
2550 unsigned int destroyed;
2555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2557 p = peer->path_head;
2561 for (i = 0; i < (p->length - 1); i++)
2563 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2564 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2566 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2577 for (i = 0; i < peer->ntunnels; i++)
2579 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2583 * Problem: one or more peers have been deleted from the tunnel tree.
2584 * We don't know who they are to try to add them again.
2585 * We need to try to find a new path for each of the disconnected peers.
2586 * Some of them might already have a path to reach them that does not
2587 * involve p1 and p2. Adding all anew might render in a better tree than
2588 * the trivial immediate fix.
2590 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2591 * its children will be reachable trough him.
2593 peer_d = peer_info_get_short (d);
2596 for (p = peer_d->path_head; NULL != p; p = p->next)
2598 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2606 /* No callback, as peer will be already disconnected and a connection
2607 * scheduled by tunnel_notify_connection_broken.
2609 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2613 peer_info_connect (peer_d, peer->tunnels[i]);
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2621 * Add the path to the peer and update the path used to reach it in case this
2624 * @param peer_info Destination peer to add the path to.
2625 * @param path New path to add. Last peer must be the peer in arg 1.
2626 * Path will be either used of freed if already known.
2627 * @param trusted Do we trust that this path is real?
2630 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2633 struct MeshPeerPath *aux;
2637 if ((NULL == peer_info) || (NULL == path))
2640 path_destroy (path);
2643 if (path->peers[path->length - 1] != peer_info->id)
2646 path_destroy (path);
2649 if (path->length <= 2 && GNUNET_NO == trusted)
2651 /* Only allow CORE to tell us about direct paths */
2652 path_destroy (path);
2655 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2656 for (l = 1; l < path->length; l++)
2658 if (path->peers[l] == myid)
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2661 for (l2 = 0; l2 < path->length - l; l2++)
2663 path->peers[l2] = path->peers[l + l2];
2668 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2673 struct GNUNET_PeerIdentity id;
2675 GNUNET_PEER_resolve (peer_info->id, &id);
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2677 path->length, GNUNET_i2s (&id));
2680 l = path_get_length (path);
2687 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2688 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2690 l2 = path_get_length (aux);
2693 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2694 peer_info->path_tail, aux, path);
2699 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2701 path_destroy (path);
2706 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2713 * Add the path to the origin peer and update the path used to reach it in case
2714 * this is the shortest.
2715 * The path is given in peer_info -> destination, therefore we turn the path
2716 * upside down first.
2718 * @param peer_info Peer to add the path to, being the origin of the path.
2719 * @param path New path to add after being inversed.
2720 * @param trusted Do we trust that this path is real?
2723 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2724 struct MeshPeerPath *path, int trusted)
2727 peer_info_add_path (peer_info, path, trusted);
2732 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2733 * to obtain a local peer -> destination path and interning the peer ids.
2735 * @return Newly allocated and created path
2737 static struct MeshPeerPath *
2738 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2739 unsigned int get_path_length,
2740 const struct GNUNET_PeerIdentity *put_path,
2741 unsigned int put_path_length)
2743 struct MeshPeerPath *p;
2749 GNUNET_PEER_change_rc (myid, 1);
2750 i = get_path_length;
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2752 for (i--; i >= 0; i--)
2754 id = GNUNET_PEER_intern (&get_path[i]);
2755 if (p->length > 0 && id == p->peers[p->length - 1])
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2758 GNUNET_PEER_change_rc (id, -1);
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2763 GNUNET_i2s (&get_path[i]));
2765 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2766 p->peers[p->length - 1] = id;
2769 i = put_path_length;
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2771 for (i--; i >= 0; i--)
2773 id = GNUNET_PEER_intern (&put_path[i]);
2776 /* PUT path went through us, so discard the path up until now and start
2777 * from here to get a much shorter (and loop-free) path.
2782 if (p->length > 0 && id == p->peers[p->length - 1])
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2785 GNUNET_PEER_change_rc (id, -1);
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2790 GNUNET_i2s (&put_path[i]));
2792 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2793 p->peers[p->length - 1] = id;
2797 if (get_path_length > 0)
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2799 GNUNET_i2s (&get_path[0]));
2800 if (put_path_length > 0)
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2802 GNUNET_i2s (&put_path[0]));
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2805 for (i = 0; i < p->length; i++)
2807 struct GNUNET_PeerIdentity peer_id;
2809 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2811 GNUNET_i2s (&peer_id));
2819 * Adds a path to the peer_infos of all the peers in the path
2821 * @param p Path to process.
2822 * @param confirmed Whether we know if the path works or not.
2825 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2829 /* TODO: invert and add */
2830 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2831 for (i++; i < p->length; i++)
2833 struct MeshPeerInfo *aux;
2834 struct MeshPeerPath *copy;
2836 aux = peer_info_get_short (p->peers[i]);
2837 copy = path_duplicate (p);
2838 copy->length = i + 1;
2839 peer_info_add_path (aux, copy, GNUNET_NO);
2845 * Send keepalive packets for a peer
2847 * @param cls Closure (tunnel for which to send the keepalive).
2848 * @param tc Notification context.
2850 * TODO: implement explicit multicast keepalive?
2853 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2857 * Search for a tunnel among the incoming tunnels
2859 * @param tid the local id of the tunnel
2861 * @return tunnel handler, NULL if doesn't exist
2863 static struct MeshTunnel *
2864 tunnel_get_incoming (MESH_TunnelNumber tid)
2866 struct GNUNET_HashCode hash;
2868 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2869 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2870 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2875 * Search for a tunnel among the tunnels for a client
2877 * @param c the client whose tunnels to search in
2878 * @param tid the local id of the tunnel
2880 * @return tunnel handler, NULL if doesn't exist
2882 static struct MeshTunnel *
2883 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2885 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2887 return tunnel_get_incoming (tid);
2891 struct GNUNET_HashCode hash;
2893 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2894 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2900 * Search for a tunnel by global ID using PEER_ID
2902 * @param pi owner of the tunnel
2903 * @param tid global tunnel number
2905 * @return tunnel handler, NULL if doesn't exist
2907 static struct MeshTunnel *
2908 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2910 struct MESH_TunnelID id;
2911 struct GNUNET_HashCode hash;
2916 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2917 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2922 * Search for a tunnel by global ID using full PeerIdentities
2924 * @param oid owner of the tunnel
2925 * @param tid global tunnel number
2927 * @return tunnel handler, NULL if doesn't exist
2929 static struct MeshTunnel *
2930 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2932 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2937 * Delete an active client from the tunnel.
2943 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2947 for (i = 0; i < t->nclients; i++)
2949 if (t->clients[i] == c)
2951 t->clients[i] = t->clients[t->nclients - 1];
2952 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2953 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2955 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2963 * Delete an ignored client from the tunnel.
2969 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2973 for (i = 0; i < t->nignore; i++)
2975 if (t->ignore[i] == c)
2977 t->ignore[i] = t->ignore[t->nignore - 1];
2978 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2986 * Delete a client from the tunnel. It should be only done on
2987 * client disconnection, otherwise use client_ignore_tunnel.
2993 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2995 tunnel_delete_ignored_client (t, c);
2996 tunnel_delete_active_client (t, c);
3001 * Iterator to free MeshTunnelChildInfo of tunnel children.
3003 * @param cls Closure (tunnel info).
3004 * @param key Hash of GNUNET_PEER_Id (unused).
3005 * @param value MeshTunnelChildInfo of the child.
3007 * @return always GNUNET_YES, to keep iterating
3010 tunnel_destroy_child (void *cls,
3011 const struct GNUNET_HashCode * key,
3014 GNUNET_free (value);
3020 * Callback used to notify a client owner of a tunnel that a peer has
3021 * disconnected, most likely because of a path change.
3023 * @param cls Closure (tunnel this notification is about).
3024 * @param peer_id Short ID of disconnected peer.
3027 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3029 struct MeshTunnel *t = cls;
3030 struct MeshPeerInfo *peer;
3031 struct MeshPathInfo *path_info;
3033 if (NULL != t->owner && NULL != nc)
3035 struct GNUNET_MESH_PeerControl msg;
3037 msg.header.size = htons (sizeof (msg));
3038 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3039 msg.tunnel_id = htonl (t->local_tid);
3040 GNUNET_PEER_resolve (peer_id, &msg.peer);
3041 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3042 &msg.header, GNUNET_NO);
3044 peer = peer_info_get_short (peer_id);
3045 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3046 path_info->peer = peer;
3048 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3053 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3054 * needed rescources.
3055 * If peer already exists, reevaluate shortest path and change if different.
3057 * @param t Tunnel we want to add a new peer to
3058 * @param peer PeerInfo of the peer being added
3062 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3064 struct GNUNET_PeerIdentity id;
3065 struct MeshPeerPath *best_p;
3066 struct MeshPeerPath *p;
3067 unsigned int best_cost;
3070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3071 GNUNET_PEER_resolve (peer->id, &id);
3073 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3076 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3077 GNUNET_assert (GNUNET_OK ==
3078 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3080 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3083 if (NULL != (p = peer->path_head))
3086 best_cost = tree_get_path_cost (t->tree, p);
3089 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3096 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3097 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3098 t->path_refresh_task =
3099 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3103 /* Start a DHT get */
3104 peer_info_connect (peer, t);
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3110 * Add a path to a tunnel which we don't own, just to remember the next hop.
3111 * If destination node was already in the tunnel, the first hop information
3112 * will be replaced with the new path.
3114 * @param t Tunnel we want to add a new peer to
3115 * @param p Path to add
3116 * @param own_pos Position of local node in path.
3120 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3121 unsigned int own_pos)
3123 struct GNUNET_PeerIdentity id;
3125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3126 GNUNET_assert (0 != own_pos);
3127 tree_add_path (t->tree, p, NULL, NULL);
3128 if (own_pos < p->length - 1)
3130 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3131 tree_update_first_hops (t->tree, myid, &id);
3133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3137 * Add a client to a tunnel, initializing all needed data structures.
3139 * @param t Tunnel to which add the client.
3140 * @param c Client which to add to the tunnel.
3143 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3145 struct MeshTunnelClientInfo clinfo;
3147 GNUNET_array_append (t->clients, t->nclients, c);
3148 clinfo.fwd_ack = t->fwd_pid + 1;
3149 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3150 clinfo.fwd_pid = t->fwd_pid;
3151 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3153 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3158 * Notifies a tunnel that a connection has broken that affects at least
3159 * some of its peers. Sends a notification towards the root of the tree.
3160 * In case the peer is the owner of the tree, notifies the client that owns
3161 * the tunnel and tries to reconnect.
3163 * @param t Tunnel affected.
3164 * @param p1 Peer that got disconnected from p2.
3165 * @param p2 Peer that got disconnected from p1.
3167 * @return Short ID of the peer disconnected (either p1 or p2).
3168 * 0 if the tunnel remained unaffected.
3170 static GNUNET_PEER_Id
3171 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3177 tree_notify_connection_broken (t->tree, p1, p2,
3178 &tunnel_notify_client_peer_disconnected,
3180 if (myid != p1 && myid != p2)
3186 if (tree_get_predecessor (t->tree) != 0)
3188 /* We are the peer still connected, notify owner of the disconnection. */
3189 struct GNUNET_MESH_PathBroken msg;
3190 struct GNUNET_PeerIdentity neighbor;
3192 msg.header.size = htons (sizeof (msg));
3193 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3194 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3195 msg.tid = htonl (t->id.tid);
3196 msg.peer1 = my_full_id;
3197 GNUNET_PEER_resolve (pid, &msg.peer2);
3198 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3199 send_message (&msg.header, &neighbor, t);
3207 * Send a multicast packet to a neighbor.
3209 * @param cls Closure (Info about the multicast packet)
3210 * @param neighbor_id Short ID of the neighbor to send the packet to.
3213 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3215 struct MeshData *mdata = cls;
3216 struct MeshTransmissionDescriptor *info;
3217 struct GNUNET_PeerIdentity neighbor;
3219 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3221 info->mesh_data = mdata;
3222 (*(mdata->reference_counter)) ++;
3223 info->destination = neighbor_id;
3224 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3226 GNUNET_i2s (&neighbor));
3227 info->peer = peer_info_get (&neighbor);
3228 GNUNET_assert (NULL != info->peer);
3230 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3231 info->mesh_data->data_len,
3238 * Send a message in a tunnel in multicast, sending a copy to each child node
3239 * down the local one in the tunnel tree.
3241 * @param t Tunnel in which to send the data.
3242 * @param msg Message to be sent.
3243 * @param internal Has the service generated this message?
3246 tunnel_send_multicast (struct MeshTunnel *t,
3247 const struct GNUNET_MessageHeader *msg,
3250 struct MeshData *mdata;
3252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3253 " sending a multicast packet...\n");
3254 mdata = GNUNET_malloc (sizeof (struct MeshData));
3255 mdata->data_len = ntohs (msg->size);
3256 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3258 mdata->data = GNUNET_malloc (mdata->data_len);
3259 memcpy (mdata->data, msg, mdata->data_len);
3260 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3262 struct GNUNET_MESH_Multicast *mcast;
3264 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3265 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3267 ntohl (mcast->ttl));
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3273 if (NULL != t->owner && GNUNET_YES != t->owner->shutting_down
3274 && GNUNET_NO == internal)
3276 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3278 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3284 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3285 if (*(mdata->reference_counter) == 0)
3287 GNUNET_free (mdata->data);
3288 GNUNET_free (mdata->reference_counter);
3289 if (NULL != mdata->task)
3291 GNUNET_SCHEDULER_cancel(*(mdata->task));
3292 GNUNET_free (mdata->task);
3293 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3295 // FIXME change order?
3296 GNUNET_free (mdata);
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299 " sending a multicast packet done\n");
3305 * Increase the SKIP value of all peers that
3306 * have not received a unicast message.
3308 * @param cls Closure (ID of the peer that HAS received the message).
3309 * @param key ID of the neighbor.
3310 * @param value Information about the neighbor.
3312 * @return GNUNET_YES to keep iterating.
3315 tunnel_add_skip (void *cls,
3316 const struct GNUNET_HashCode * key,
3319 struct GNUNET_PeerIdentity *neighbor = cls;
3320 struct MeshTunnelChildInfo *cinfo = value;
3322 /* TODO compare only pointers? key == neighbor? */
3323 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3333 * @brief Get neighbor's Flow Control information.
3335 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3336 * descendant of the local node in a certain tunnel.
3337 * If the info is not yet there (recently created path), creates the data struct
3338 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3341 * @param t Tunnel related.
3342 * @param peer Neighbor whose Flow Control info is needed.
3344 * @return Neighbor's Flow Control info.
3346 static struct MeshTunnelChildInfo *
3347 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3348 const struct GNUNET_PeerIdentity *peer)
3350 struct MeshTunnelChildInfo *cinfo;
3351 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3357 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3358 cinfo->id = GNUNET_PEER_intern (peer);
3359 cinfo->skip = t->fwd_pid;
3361 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3362 cinfo->fwd_ack = t->fwd_pid + delta;
3363 cinfo->bck_ack = delta;
3365 GNUNET_assert (GNUNET_OK ==
3366 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3376 * Get the Flow Control info of a client.
3378 * @param t Tunnel on which to look.
3379 * @param c Client whose ACK to get.
3381 * @return ACK value.
3383 static struct MeshTunnelClientInfo *
3384 tunnel_get_client_fc (struct MeshTunnel *t,
3385 struct MeshClient *c)
3389 for (i = 0; i < t->nclients; i++)
3391 if (t->clients[i] != c)
3393 return &t->clients_fc[i];
3396 return NULL; // avoid compiler / coverity complaints
3401 * Iterator to get the appropiate ACK value from all children nodes.
3403 * @param cls Closue (tunnel).
3404 * @param id Id of the child node.
3407 tunnel_get_child_fwd_ack (void *cls,
3410 struct GNUNET_PeerIdentity peer_id;
3411 struct MeshTunnelChildInfo *cinfo;
3412 struct MeshTunnelChildIteratorContext *ctx = cls;
3413 struct MeshTunnel *t = ctx->t;
3416 GNUNET_PEER_resolve (id, &peer_id);
3417 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3418 ack = cinfo->fwd_ack;
3420 if (GNUNET_NO == ctx->init)
3422 ctx->max_child_ack = ack;
3423 ctx->init = GNUNET_YES;
3426 if (GNUNET_YES == t->speed_min)
3428 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3432 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3439 * Get the maximum PID allowed to transmit to any
3440 * tunnel child of the local peer, depending on the tunnel
3441 * buffering/speed settings.
3445 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3448 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3450 struct MeshTunnelChildIteratorContext ctx;
3452 ctx.max_child_ack = 0;
3454 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3456 if (0 == ctx.nchildren)
3459 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3460 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3462 return (int64_t) ctx.max_child_ack;
3467 * Set the FWD ACK value of a client in a particular tunnel.
3469 * @param t Tunnel affected.
3470 * @param c Client whose ACK to set.
3471 * @param ack ACK value.
3474 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3475 struct MeshClient *c,
3480 for (i = 0; i < t->nclients; i++)
3482 if (t->clients[i] != c)
3484 t->clients_fc[i].fwd_ack = ack;
3492 * Get the highest ACK value of all clients in a particular tunnel,
3493 * according to the buffering/speed settings.
3495 * @param t Tunnel on which to look.
3497 * @return Corresponding ACK value (max uint32_t).
3498 * If no clients are suscribed, -1.
3501 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3506 if (0 == t->nclients)
3509 for (ack = -1, i = 0; i < t->nclients; i++)
3512 (GNUNET_YES == t->speed_min &&
3513 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3514 (GNUNET_NO == t->speed_min &&
3515 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3517 ack = t->clients_fc[i].fwd_ack;
3521 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3522 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3524 return (uint32_t) ack;
3529 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3530 * mode and the status of all children nodes.
3534 * @return Maximum PID allowed.
3537 tunnel_get_fwd_ack (struct MeshTunnel *t)
3540 uint32_t buffer_free;
3545 count = t->fwd_pid - t->skip;
3546 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3547 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3548 child_ack = tunnel_get_children_fwd_ack (t);
3549 client_ack = tunnel_get_clients_fwd_ack (t);
3550 if (-1 == child_ack)
3552 // Node has no children, child_ack AND core buffer are irrelevant.
3553 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good! // FIXME fc
3554 return (uint32_t) client_ack;
3557 if (GNUNET_YES == t->speed_min)
3559 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3560 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3564 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3565 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3567 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3568 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3570 count, buffer_free, child_ack, client_ack, ack);
3576 * Build a local ACK message and send it to a local client.
3578 * @param t Tunnel on which to send the ACK.
3579 * @param c Client to whom send the ACK.
3580 * @param ack Value of the ACK.
3583 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3585 struct GNUNET_MESH_LocalAck msg;
3587 msg.header.size = htons (sizeof (msg));
3588 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3589 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3590 msg.max_pid = htonl (ack);
3591 GNUNET_SERVER_notification_context_unicast(nc,
3598 * Build an ACK message and queue it to send to the given peer.
3600 * @param t Tunnel on which to send the ACK.
3601 * @param peer Peer to whom send the ACK.
3602 * @param ack Value of the ACK.
3605 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3607 struct GNUNET_MESH_ACK msg;
3609 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3610 msg.header.size = htons (sizeof (msg));
3611 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3612 msg.pid = htonl (ack);
3613 msg.tid = htonl (t->id.tid);
3615 send_message (&msg.header, peer, t);
3620 * Notify a the owner of a tunnel about how many more
3621 * payload packages will we accept on a given tunnel.
3623 * @param t Tunnel on which to send the ACK.
3626 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3631 "Sending client FWD ACK on tunnel %X\n",
3634 ack = tunnel_get_fwd_ack (t);
3636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3637 if (t->last_fwd_ack == ack)
3640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3641 t->last_fwd_ack = ack;
3642 send_local_ack (t, t->owner, ack);
3647 * Send an ACK informing the predecessor about the available buffer space.
3648 * In case there is no predecessor, inform the owning client.
3649 * If buffering is off, send only on behalf of children or self if endpoint.
3650 * If buffering is on, send when sent to children and buffer space is free.
3651 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3652 * the ACK itself goes "back" (towards root).
3654 * @param t Tunnel on which to send the ACK.
3655 * @param type Type of message that triggered the ACK transmission.
3658 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3660 struct GNUNET_PeerIdentity id;
3663 if (NULL != t->owner)
3665 tunnel_send_client_fwd_ack (t);
3668 /* Is it after unicast / multicast retransmission? */
3671 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3672 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3674 "ACK due to FWD DATA retransmission\n");
3675 if (GNUNET_YES == t->nobuffer)
3677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3680 if (t->fwd_queue_max > t->fwd_queue_n * 2)
3682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3686 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3687 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3693 /* Ok, ACK might be necessary, what PID to ACK? */
3694 ack = tunnel_get_fwd_ack (t);
3696 /* If speed_min and not all children have ack'd, dont send yet */
3697 if (ack == t->last_fwd_ack)
3699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3703 t->last_fwd_ack = ack;
3704 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3705 send_ack (t, &id, ack);
3710 * Iterator to send a child node a BCK ACK to allow him to send more
3713 * @param cls Closure (tunnel).
3714 * @param id Id of the child node.
3717 tunnel_send_child_bck_ack (void *cls,
3720 struct MeshTunnel *t = cls;
3721 struct MeshTunnelChildInfo *cinfo;
3722 struct GNUNET_PeerIdentity peer;
3724 GNUNET_PEER_resolve (id, &peer);
3725 cinfo = tunnel_get_neighbor_fc (t, &peer);
3727 if (cinfo->bck_ack != cinfo->pid &&
3728 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3732 send_ack (t, &peer, cinfo->bck_ack);
3737 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3739 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3741 * @param t Tunnel on which to send the BCK ACKs.
3744 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3747 unsigned int tunnel_delta;
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3751 tunnel_delta = t->bck_ack - t->bck_pid;
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3754 /* Find client whom to allow to send to origin (with lowest buffer space) */
3755 for (i = 0; i < t->nclients; i++)
3757 struct MeshTunnelClientInfo *clinfo;
3760 clinfo = &t->clients_fc[i];
3761 delta = clinfo->bck_ack - clinfo->bck_pid;
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3763 t->clients[i]->id, delta);
3765 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3766 (GNUNET_YES == t->nobuffer && 0 == delta))
3770 ack = clinfo->bck_pid;
3771 ack += t->nobuffer ? 1 : tunnel_delta;
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3773 " sending ack to client %u: %u\n",
3774 t->clients[i]->id, ack);
3775 send_local_ack (t, t->clients[i], ack);
3776 clinfo->bck_ack = ack;
3783 * Send an ACK informing the children nodes and destination clients about
3784 * the available buffer space.
3785 * If buffering is off, send only on behalf of root (can be self).
3786 * If buffering is on, send when sent to predecessor and buffer space is free.
3787 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3788 * the ACK itself goes "forward" (towards children/clients).
3790 * @param t Tunnel on which to send the ACK.
3791 * @param type Type of message that triggered the ACK transmission.
3794 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3797 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3798 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3799 /* Is it after data to_origin retransmission? */
3802 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3803 if (GNUNET_YES == t->nobuffer)
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3806 " Not sending ACK, nobuffer\n");
3810 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3811 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3817 tunnel_send_clients_bck_ack (t);
3818 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
3823 * Send a message to all peers in this tunnel that the tunnel is no longer
3826 * @param t The tunnel whose peers to notify.
3829 tunnel_send_destroy (struct MeshTunnel *t)
3831 struct GNUNET_MESH_TunnelDestroy msg;
3833 msg.header.size = htons (sizeof (msg));
3834 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3835 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3836 msg.tid = htonl (t->id.tid);
3837 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3842 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3844 * @param cls Closure (Tunnel which to cancel).
3845 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3848 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3850 struct MeshTunnel *t = cls;
3851 struct MeshPeerInfo *peer_info;
3852 struct MeshPeerQueue *pq;
3853 struct MeshPeerQueue *next;
3855 peer_info = peer_info_get_short (neighbor_id);
3856 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3859 if (pq->tunnel == t)
3861 queue_destroy (pq, GNUNET_YES);
3864 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3866 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3867 peer_info->core_transmit = NULL;
3872 * Destroy the tunnel and free any allocated resources linked to it.
3874 * @param t the tunnel to destroy
3876 * @return GNUNET_OK on success
3879 tunnel_destroy (struct MeshTunnel *t)
3881 struct MeshClient *c;
3882 struct GNUNET_HashCode hash;
3889 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3895 struct GNUNET_PeerIdentity id;
3897 GNUNET_PEER_resolve (t->id.oid, &id);
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3899 GNUNET_i2s (&id), t->id.tid);
3901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3905 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3906 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3911 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3914 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3918 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3919 for (i = 0; i < t->nclients; i++)
3923 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3928 for (i = 0; i < t->nignore; i++)
3932 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3937 if (t->nclients > 0)
3940 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3944 GNUNET_free (t->clients);
3946 if (NULL != t->peers)
3948 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3950 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3953 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3954 &tunnel_destroy_child,
3956 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3958 tree_destroy (t->tree);
3960 if (NULL != t->regex_ctx)
3961 regex_cancel_search (t->regex_ctx);
3962 if (NULL != t->dht_get_type)
3963 GNUNET_DHT_get_stop (t->dht_get_type);
3964 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3965 GNUNET_SCHEDULER_cancel (t->timeout_task);
3966 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3967 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3970 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3971 GNUNET_assert (0 <= n_tunnels);
3978 * Create a new tunnel
3980 * @param owner Who is the owner of the tunnel (short ID).
3981 * @param tid Tunnel Number of the tunnel.
3982 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3983 * @param local Tunnel Number for the tunnel, for the client point of view.
3985 * @return A new initialized tunnel. NULL on error.
3987 static struct MeshTunnel *
3988 tunnel_new (GNUNET_PEER_Id owner,
3989 MESH_TunnelNumber tid,
3990 struct MeshClient *client,
3991 MESH_TunnelNumber local)
3993 struct MeshTunnel *t;
3994 struct GNUNET_HashCode hash;
3996 if (n_tunnels >= max_tunnels && NULL == client)
3999 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4002 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4003 t->bck_queue_max = t->fwd_queue_max;
4004 t->tree = tree_new (owner);
4006 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4007 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4008 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4009 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4010 t->local_tid = local;
4011 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4013 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4015 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4017 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4018 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4025 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4032 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4034 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4035 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4039 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4049 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4050 * that are no longer needed, as well as nodes of no longer reachable peers.
4051 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4053 * @param t Tunnel from which to remove the path.
4054 * @param peer Short id of the peer which should be removed.
4057 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4059 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4065 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4066 * client when the client disconnects. If the client is not the owner, the
4067 * owner will get notified if no more clients are in the tunnel and the client
4068 * get removed from the tunnel's list.
4070 * @param cls closure (client that is disconnecting)
4071 * @param key the hash of the local tunnel id (used to access the hashmap)
4072 * @param value the value stored at the key (tunnel to destroy)
4074 * @return GNUNET_OK on success
4077 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4079 struct MeshTunnel *t = value;
4080 struct MeshClient *c = cls;
4083 send_client_tunnel_disconnect(t, c);
4086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4087 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4088 tunnel_delete_client(t, c);
4089 client_delete_tunnel(c, t);
4092 tunnel_send_destroy(t);
4093 r = tunnel_destroy (t);
4099 * Timeout function, destroys tunnel if called
4101 * @param cls Closure (tunnel to destroy).
4102 * @param tc TaskContext
4105 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4107 struct MeshTunnel *t = cls;
4109 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4111 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4116 * Resets the tunnel timeout. Starts it if no timeout was running.
4118 * @param t Tunnel whose timeout to reset.
4120 * TODO use heap to improve efficiency of scheduler.
4123 tunnel_reset_timeout (struct MeshTunnel *t)
4125 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4126 GNUNET_SCHEDULER_cancel (t->timeout_task);
4128 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4129 (refresh_path_time, 4), &tunnel_timeout, t);
4133 /******************************************************************************/
4134 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4135 /******************************************************************************/
4138 * Function to send a create path packet to a peer.
4140 * @param cls closure
4141 * @param size number of bytes available in buf
4142 * @param buf where the callee should write the message
4143 * @return number of bytes written to buf
4146 send_core_path_create (void *cls, size_t size, void *buf)
4148 struct MeshPathInfo *info = cls;
4149 struct GNUNET_MESH_ManipulatePath *msg;
4150 struct GNUNET_PeerIdentity *peer_ptr;
4151 struct MeshTunnel *t = info->t;
4152 struct MeshPeerPath *p = info->path;
4157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4159 sizeof (struct GNUNET_MESH_ManipulatePath) +
4160 p->length * sizeof (struct GNUNET_PeerIdentity);
4162 if (size < size_needed || NULL == buf)
4167 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4168 msg->header.size = htons (size_needed);
4169 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4170 msg->tid = ntohl (t->id.tid);
4172 if (GNUNET_YES == t->speed_min)
4173 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4174 if (GNUNET_YES == t->nobuffer)
4175 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4176 msg->opt = htonl(opt);
4178 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4179 for (i = 0; i < p->length; i++)
4181 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4188 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4194 * Fill the core buffer
4196 * @param cls closure (data itself)
4197 * @param size number of bytes available in buf
4198 * @param buf where the callee should write the message
4200 * @return number of bytes written to buf
4203 send_core_data_multicast (void *cls, size_t size, void *buf)
4205 struct MeshTransmissionDescriptor *info = cls;
4208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4209 GNUNET_assert (NULL != info);
4210 GNUNET_assert (NULL != info->peer);
4211 total_size = info->mesh_data->data_len;
4212 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4214 if (total_size > size)
4219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4220 memcpy (buf, info->mesh_data->data, total_size);
4223 struct GNUNET_MESH_Multicast *mc;
4224 struct GNUNET_MessageHeader *mh;
4227 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4229 mc = (struct GNUNET_MESH_Multicast *) mh;
4230 mh = (struct GNUNET_MessageHeader *) &mc[1];
4231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4232 " multicast, payload type %s\n",
4233 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4235 " multicast, payload size %u\n", ntohs (mh->size));
4239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4240 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4244 data_descriptor_decrement_rc (info->mesh_data);
4245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4253 * Creates a path ack message in buf and frees all unused resources.
4255 * @param cls closure (MeshTransmissionDescriptor)
4256 * @param size number of bytes available in buf
4257 * @param buf where the callee should write the message
4258 * @return number of bytes written to buf
4261 send_core_path_ack (void *cls, size_t size, void *buf)
4263 struct MeshTransmissionDescriptor *info = cls;
4264 struct GNUNET_MESH_PathACK *msg = buf;
4266 GNUNET_assert (NULL != info);
4267 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4272 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4273 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4274 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4275 msg->tid = htonl (info->origin->tid);
4276 msg->peer_id = my_full_id;
4279 /* TODO add signature */
4281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4282 return sizeof (struct GNUNET_MESH_PathACK);
4287 * Free a transmission that was already queued with all resources
4288 * associated to the request.
4290 * @param queue Queue handler to cancel.
4291 * @param clear_cls Is it necessary to free associated cls?
4294 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4296 struct MeshTransmissionDescriptor *dd;
4297 struct MeshPathInfo *path_info;
4299 if (GNUNET_YES == clear_cls)
4301 switch (queue->type)
4303 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4304 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4305 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4308 data_descriptor_decrement_rc (dd->mesh_data);
4310 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4312 path_info = queue->cls;
4313 path_destroy (path_info->path);
4317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4319 GNUNET_free_non_null (queue->cls);
4321 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4322 queue->peer->queue_tail,
4324 GNUNET_free (queue);
4329 * Core callback to write a queued packet to core buffer
4331 * @param cls Closure (peer info).
4332 * @param size Number of bytes available in buf.
4333 * @param buf Where the to write the message.
4335 * @return number of bytes written to buf
4338 queue_send (void *cls, size_t size, void *buf)
4340 struct MeshPeerInfo *peer = cls;
4341 struct GNUNET_MessageHeader *msg;
4342 struct MeshPeerQueue *queue;
4343 struct MeshTunnel *t;
4346 peer->core_transmit = NULL;
4347 queue = peer->queue_head;
4349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4351 /* If queue is empty, send should have been cancelled */
4357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4359 /* Check if buffer size is enough for the message */
4360 if (queue->size > size)
4362 struct GNUNET_PeerIdentity id;
4364 GNUNET_PEER_resolve (peer->id, &id);
4365 peer->core_transmit =
4366 GNUNET_CORE_notify_transmit_ready(core_handle,
4369 GNUNET_TIME_UNIT_FOREVER_REL,
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4379 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast: %u\n");
4384 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4391 switch (queue->type)
4394 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4395 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4396 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4398 "********* raw: %u\n",
4401 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4402 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4403 data_size = send_core_data_raw (queue->cls, size, buf);
4404 msg = (struct GNUNET_MessageHeader *) buf;
4405 switch (ntohs (msg->type)) // Type of preconstructed message
4407 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4408 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4410 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4411 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4417 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4419 data_size = send_core_data_multicast(queue->cls, size, buf);
4420 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4422 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4424 data_size = send_core_path_create(queue->cls, size, buf);
4426 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4428 data_size = send_core_path_ack(queue->cls, size, buf);
4432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4433 "********* type unknown: %u\n",
4438 /* Free queue, but cls was freed by send_core_* */
4439 queue_destroy (queue, GNUNET_NO);
4441 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4447 /* If more data in queue, send next */
4448 if (NULL != peer->queue_head)
4450 struct GNUNET_PeerIdentity id;
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4453 GNUNET_PEER_resolve (peer->id, &id);
4454 peer->core_transmit =
4455 GNUNET_CORE_notify_transmit_ready(core_handle,
4458 GNUNET_TIME_UNIT_FOREVER_REL,
4460 peer->queue_head->size,
4464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4470 * Queue and pass message to core when possible.
4472 * @param cls Closure (type dependant).
4473 * @param type Type of the message, 0 for a raw message.
4474 * @param size Size of the message.
4475 * @param dst Neighbor to send message to.
4476 * @param t Tunnel this message belongs to.
4479 queue_add (void *cls, uint16_t type, size_t size,
4480 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4482 struct MeshPeerQueue *queue;
4487 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4488 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4490 n = &t->fwd_queue_n;
4491 max = &t->fwd_queue_max;
4493 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4495 n = &t->bck_queue_n;
4496 max = &t->bck_queue_max;
4501 if (NULL == t->owner)
4502 GNUNET_break_op(0); // TODO: kill connection?
4505 return; // Drop message
4509 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4515 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4516 if (NULL == dst->core_transmit)
4518 struct GNUNET_PeerIdentity id;
4520 GNUNET_PEER_resolve (dst->id, &id);
4521 dst->core_transmit =
4522 GNUNET_CORE_notify_transmit_ready(core_handle,
4525 GNUNET_TIME_UNIT_FOREVER_REL,
4534 /******************************************************************************/
4535 /******************** MESH NETWORK HANDLERS **************************/
4536 /******************************************************************************/
4540 * Core handler for path creation
4542 * @param cls closure
4543 * @param message message
4544 * @param peer peer identity this notification is about
4545 * @param atsi performance data
4546 * @param atsi_count number of records in 'atsi'
4548 * @return GNUNET_OK to keep the connection open,
4549 * GNUNET_SYSERR to close it (signal serious error)
4552 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4553 const struct GNUNET_MessageHeader *message,
4554 const struct GNUNET_ATS_Information *atsi,
4555 unsigned int atsi_count)
4557 unsigned int own_pos;
4560 MESH_TunnelNumber tid;
4561 struct GNUNET_MESH_ManipulatePath *msg;
4562 struct GNUNET_PeerIdentity *pi;
4563 struct GNUNET_HashCode hash;
4564 struct MeshPeerPath *path;
4565 struct MeshPeerInfo *dest_peer_info;
4566 struct MeshPeerInfo *orig_peer_info;
4567 struct MeshTunnel *t;
4569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4570 "Received a path create msg [%s]\n",
4571 GNUNET_i2s (&my_full_id));
4572 size = ntohs (message->size);
4573 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4575 GNUNET_break_op (0);
4579 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4580 if (size % sizeof (struct GNUNET_PeerIdentity))
4582 GNUNET_break_op (0);
4585 size /= sizeof (struct GNUNET_PeerIdentity);
4588 GNUNET_break_op (0);
4591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4592 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4594 tid = ntohl (msg->tid);
4595 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4597 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4598 t = tunnel_get (pi, tid);
4599 if (NULL == t) // FIXME only for INCOMING tunnels?
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4604 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4607 // FIXME notify failure
4610 opt = ntohl (msg->opt);
4611 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4612 GNUNET_YES : GNUNET_NO;
4613 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4614 GNUNET_YES : GNUNET_NO;
4616 if (GNUNET_YES == t->nobuffer)
4618 t->bck_queue_max = 1;
4619 t->fwd_queue_max = 1;
4622 // FIXME only assign a local tid if a local client is interested (on demand)
4623 while (NULL != tunnel_get_incoming (next_local_tid))
4624 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4625 t->local_tid_dest = next_local_tid++;
4626 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4629 tunnel_reset_timeout (t);
4630 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4632 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4633 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4641 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4642 if (NULL == dest_peer_info)
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4645 " Creating PeerInfo for destination.\n");
4646 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4647 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4648 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4650 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4652 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4653 if (NULL == orig_peer_info)
4655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4656 " Creating PeerInfo for origin.\n");
4657 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4658 orig_peer_info->id = GNUNET_PEER_intern (pi);
4659 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4660 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4663 path = path_new (size);
4665 for (i = 0; i < size; i++)
4667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4668 GNUNET_i2s (&pi[i]));
4669 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4670 if (path->peers[i] == myid)
4673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4676 /* cannot be self, must be 'not found' */
4677 /* create path: self not found in path through self */
4678 GNUNET_break_op (0);
4679 path_destroy (path);
4683 path_add_to_peers (path, GNUNET_NO);
4684 tunnel_add_path (t, path, own_pos);
4685 if (own_pos == size - 1)
4687 /* It is for us! Send ack. */
4688 struct MeshTransmissionDescriptor *info;
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4691 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4692 if (NULL == t->peers)
4694 /* New tunnel! Notify clients on data. */
4695 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4697 GNUNET_break (GNUNET_SYSERR !=
4698 GNUNET_CONTAINER_multihashmap_put (t->peers,
4699 &my_full_id.hashPubKey,
4702 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4703 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4704 info->origin = &t->id;
4705 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4706 GNUNET_assert (NULL != info->peer);
4708 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4709 sizeof (struct GNUNET_MESH_PathACK),
4715 struct MeshPeerPath *path2;
4717 /* It's for somebody else! Retransmit. */
4718 path2 = path_duplicate (path);
4719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4720 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4721 path2 = path_duplicate (path);
4722 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4723 send_create_path (dest_peer_info, path, t);
4730 * Core handler for path destruction
4732 * @param cls closure
4733 * @param message message
4734 * @param peer peer identity this notification is about
4735 * @param atsi performance data
4736 * @param atsi_count number of records in 'atsi'
4738 * @return GNUNET_OK to keep the connection open,
4739 * GNUNET_SYSERR to close it (signal serious error)
4742 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4743 const struct GNUNET_MessageHeader *message,
4744 const struct GNUNET_ATS_Information *atsi,
4745 unsigned int atsi_count)
4747 struct GNUNET_MESH_ManipulatePath *msg;
4748 struct GNUNET_PeerIdentity *pi;
4749 struct MeshPeerPath *path;
4750 struct MeshTunnel *t;
4751 unsigned int own_pos;
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4756 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4757 size = ntohs (message->size);
4758 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4760 GNUNET_break_op (0);
4764 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4765 if (size % sizeof (struct GNUNET_PeerIdentity))
4767 GNUNET_break_op (0);
4770 size /= sizeof (struct GNUNET_PeerIdentity);
4773 GNUNET_break_op (0);
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4778 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4779 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4781 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4783 t = tunnel_get (pi, ntohl (msg->tid));
4786 /* TODO notify back: we don't know this tunnel */
4787 GNUNET_break_op (0);
4790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4791 path = path_new (size);
4793 for (i = 0; i < size; i++)
4795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4796 GNUNET_i2s (&pi[i]));
4797 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4798 if (path->peers[i] == myid)
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4802 if (own_pos < path->length - 1)
4803 send_message (message, &pi[own_pos + 1], t);
4805 send_client_tunnel_disconnect(t, NULL);
4807 tunnel_delete_peer (t, path->peers[path->length - 1]);
4808 path_destroy (path);
4814 * Core handler for notifications of broken paths
4816 * @param cls closure
4817 * @param message message
4818 * @param peer peer identity this notification is about
4819 * @param atsi performance data
4820 * @param atsi_count number of records in 'atsi'
4822 * @return GNUNET_OK to keep the connection open,
4823 * GNUNET_SYSERR to close it (signal serious error)
4826 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4827 const struct GNUNET_MessageHeader *message,
4828 const struct GNUNET_ATS_Information *atsi,
4829 unsigned int atsi_count)
4831 struct GNUNET_MESH_PathBroken *msg;
4832 struct MeshTunnel *t;
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4835 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4836 msg = (struct GNUNET_MESH_PathBroken *) message;
4837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4838 GNUNET_i2s (&msg->peer1));
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4840 GNUNET_i2s (&msg->peer2));
4841 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4844 GNUNET_break_op (0);
4847 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4848 GNUNET_PEER_search (&msg->peer2));
4855 * Core handler for tunnel destruction
4857 * @param cls closure
4858 * @param message message
4859 * @param peer peer identity this notification is about
4860 * @param atsi performance data
4861 * @param atsi_count number of records in 'atsi'
4863 * @return GNUNET_OK to keep the connection open,
4864 * GNUNET_SYSERR to close it (signal serious error)
4867 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4868 const struct GNUNET_MessageHeader *message,
4869 const struct GNUNET_ATS_Information *atsi,
4870 unsigned int atsi_count)
4872 struct GNUNET_MESH_TunnelDestroy *msg;
4873 struct MeshTunnel *t;
4875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4876 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4877 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4879 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4880 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4883 /* Probably already got the message from another path,
4884 * destroyed the tunnel and retransmitted to children.
4887 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4890 if (t->id.oid == myid)
4892 GNUNET_break_op (0);
4895 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4897 /* Tunnel was incoming, notify clients */
4898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4899 t->local_tid, t->local_tid_dest);
4900 send_clients_tunnel_destroy (t);
4902 tunnel_send_destroy (t);
4903 t->destroy = GNUNET_YES;
4904 // TODO: add timeout to destroy the tunnel anyway
4910 * Core handler for mesh network traffic going from the origin to a peer
4912 * @param cls closure
4913 * @param peer peer identity this notification is about
4914 * @param message message
4915 * @param atsi performance data
4916 * @param atsi_count number of records in 'atsi'
4917 * @return GNUNET_OK to keep the connection open,
4918 * GNUNET_SYSERR to close it (signal serious error)
4921 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4922 const struct GNUNET_MessageHeader *message,
4923 const struct GNUNET_ATS_Information *atsi,
4924 unsigned int atsi_count)
4926 struct GNUNET_MESH_Unicast *msg;
4927 struct GNUNET_PeerIdentity *neighbor;
4928 struct MeshTunnelChildInfo *cinfo;
4929 struct MeshTunnel *t;
4930 GNUNET_PEER_Id dest_id;
4935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4938 size = ntohs (message->size);
4940 sizeof (struct GNUNET_MESH_Unicast) +
4941 sizeof (struct GNUNET_MessageHeader))
4946 msg = (struct GNUNET_MESH_Unicast *) message;
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4948 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4950 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4953 /* TODO notify back: we don't know this tunnel */
4954 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4955 GNUNET_break_op (0);
4958 pid = ntohl (msg->pid);
4959 if (t->fwd_pid == pid)
4961 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4962 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4963 " Already seen pid %u, DROPPING!\n", pid);
4968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4969 " pid %u not seen yet, forwarding\n", pid);
4971 t->skip += (pid - t->fwd_pid) - 1;
4973 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4975 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4976 GNUNET_break_op (0);
4979 tunnel_reset_timeout (t);
4980 dest_id = GNUNET_PEER_search (&msg->destination);
4981 if (dest_id == myid)
4983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4984 " it's for us! sending to clients...\n");
4985 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4986 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4987 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4990 ttl = ntohl (msg->ttl);
4991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4994 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4995 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4996 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5000 " not for us, retransmitting...\n");
5002 neighbor = tree_get_first_hop (t->tree, dest_id);
5003 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5005 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5008 if (GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5010 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5011 GNUNET_break_op (0);
5014 send_message (message, neighbor, t);
5015 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5022 * Core handler for mesh network traffic going from the origin to all peers
5024 * @param cls closure
5025 * @param message message
5026 * @param peer peer identity this notification is about
5027 * @param atsi performance data
5028 * @param atsi_count number of records in 'atsi'
5029 * @return GNUNET_OK to keep the connection open,
5030 * GNUNET_SYSERR to close it (signal serious error)
5032 * TODO: Check who we got this from, to validate route.
5035 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5036 const struct GNUNET_MessageHeader *message,
5037 const struct GNUNET_ATS_Information *atsi,
5038 unsigned int atsi_count)
5040 struct GNUNET_MESH_Multicast *msg;
5041 struct MeshTunnel *t;
5045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5047 size = ntohs (message->size);
5048 if (sizeof (struct GNUNET_MESH_Multicast) +
5049 sizeof (struct GNUNET_MessageHeader) > size)
5051 GNUNET_break_op (0);
5054 msg = (struct GNUNET_MESH_Multicast *) message;
5055 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5059 /* TODO notify that we dont know that tunnel */
5060 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5061 GNUNET_break_op (0);
5064 pid = ntohl (msg->pid);
5065 if (t->fwd_pid == pid)
5067 /* already seen this packet, drop */
5068 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5070 " Already seen pid %u, DROPPING!\n", pid);
5071 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5077 " pid %u not seen yet, forwarding\n", pid);
5079 t->skip += (pid - t->fwd_pid) - 1;
5081 tunnel_reset_timeout (t);
5083 /* Transmit to locally interested clients */
5084 if (NULL != t->peers &&
5085 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5087 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5088 send_subscribed_clients (message, &msg[1].header);
5089 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5092 if (ntohl (msg->ttl) == 0)
5094 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5095 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5096 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5099 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5100 tunnel_send_multicast (t, message, GNUNET_NO);
5106 * Core handler for mesh network traffic toward the owner of a tunnel
5108 * @param cls closure
5109 * @param message message
5110 * @param peer peer identity this notification is about
5111 * @param atsi performance data
5112 * @param atsi_count number of records in 'atsi'
5114 * @return GNUNET_OK to keep the connection open,
5115 * GNUNET_SYSERR to close it (signal serious error)
5118 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5119 const struct GNUNET_MessageHeader *message,
5120 const struct GNUNET_ATS_Information *atsi,
5121 unsigned int atsi_count)
5123 struct GNUNET_MESH_ToOrigin *msg;
5124 struct GNUNET_PeerIdentity id;
5125 struct MeshPeerInfo *peer_info;
5126 struct MeshTunnel *t;
5129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5131 size = ntohs (message->size);
5132 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5133 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5135 GNUNET_break_op (0);
5138 msg = (struct GNUNET_MESH_ToOrigin *) message;
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5140 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5141 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5145 /* TODO notify that we dont know this tunnel (whom)? */
5146 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5147 GNUNET_break_op (0);
5151 if (NULL != t->owner)
5154 struct GNUNET_MESH_ToOrigin *copy;
5156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5157 " it's for us! sending to clients...\n");
5158 /* TODO signature verification */
5159 memcpy (cbuf, message, size);
5160 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5161 copy->tid = htonl (t->local_tid);
5162 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5163 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5164 ©->header, GNUNET_NO);
5165 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5169 " not for us, retransmitting...\n");
5171 peer_info = peer_info_get (&msg->oid);
5172 if (NULL == peer_info)
5174 /* unknown origin of tunnel */
5178 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5179 send_message (message, &id, t);
5180 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5187 * Core handler for mesh network traffic point-to-point acks.
5189 * @param cls closure
5190 * @param message message
5191 * @param peer peer identity this notification is about
5192 * @param atsi performance data
5193 * @param atsi_count number of records in 'atsi'
5195 * @return GNUNET_OK to keep the connection open,
5196 * GNUNET_SYSERR to close it (signal serious error)
5199 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5200 const struct GNUNET_MessageHeader *message,
5201 const struct GNUNET_ATS_Information *atsi,
5202 unsigned int atsi_count)
5204 struct GNUNET_MESH_ACK *msg;
5205 struct MeshTunnel *t;
5208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5210 msg = (struct GNUNET_MESH_ACK *) message;
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5212 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5213 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5217 /* TODO notify that we dont know this tunnel (whom)? */
5218 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5219 GNUNET_break_op (0);
5222 ack = ntohl (msg->pid);
5224 /* Is this a forward or backward ACK? */
5225 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5227 struct MeshTunnelChildInfo *cinfo;
5229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5230 cinfo = tunnel_get_neighbor_fc (t, peer);
5231 cinfo->fwd_ack = ack;
5232 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5238 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5240 // FIXME fc Unlock queues?
5246 * Core handler for path ACKs
5248 * @param cls closure
5249 * @param message message
5250 * @param peer peer identity this notification is about
5251 * @param atsi performance data
5252 * @param atsi_count number of records in 'atsi'
5254 * @return GNUNET_OK to keep the connection open,
5255 * GNUNET_SYSERR to close it (signal serious error)
5258 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5259 const struct GNUNET_MessageHeader *message,
5260 const struct GNUNET_ATS_Information *atsi,
5261 unsigned int atsi_count)
5263 struct GNUNET_MESH_PathACK *msg;
5264 struct GNUNET_PeerIdentity id;
5265 struct MeshPeerInfo *peer_info;
5266 struct MeshPeerPath *p;
5267 struct MeshTunnel *t;
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5270 GNUNET_i2s (&my_full_id));
5271 msg = (struct GNUNET_MESH_PathACK *) message;
5272 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5275 /* TODO notify that we don't know the tunnel */
5276 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5278 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5282 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5284 peer_info = peer_info_get (&msg->peer_id);
5285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5286 GNUNET_i2s (&msg->peer_id));
5287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5290 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5293 "connect_by_string completed, stopping search\n");
5294 regex_cancel_search (t->regex_ctx);
5295 t->regex_ctx = NULL;
5298 /* Add paths to peers? */
5299 p = tree_get_path_to_peer (t->tree, peer_info->id);
5302 path_add_to_peers (p, GNUNET_YES);
5310 /* Message for us? */
5311 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5314 if (NULL == t->owner)
5316 GNUNET_break_op (0);
5319 if (NULL != t->dht_get_type)
5321 GNUNET_DHT_get_stop (t->dht_get_type);
5322 t->dht_get_type = NULL;
5324 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5326 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5327 send_client_peer_connected (t, peer_info->id);
5332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5333 " not for us, retransmitting...\n");
5334 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5335 peer_info = peer_info_get (&msg->oid);
5336 if (NULL == peer_info)
5338 /* If we know the tunnel, we should DEFINITELY know the peer */
5342 send_message (message, &id, t);
5348 * Functions to handle messages from core
5350 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5351 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5352 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5353 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5354 sizeof (struct GNUNET_MESH_PathBroken)},
5355 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5356 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5357 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5358 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5359 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5360 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5361 sizeof (struct GNUNET_MESH_ACK)},
5362 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5363 sizeof (struct GNUNET_MESH_PathACK)},
5369 /******************************************************************************/
5370 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5371 /******************************************************************************/
5374 * deregister_app: iterator for removing each application registered by a client
5376 * @param cls closure
5377 * @param key the hash of the application id (used to access the hashmap)
5378 * @param value the value stored at the key (client)
5380 * @return GNUNET_OK on success
5383 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5385 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5386 GNUNET_break (GNUNET_YES ==
5387 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5393 * notify_client_connection_failure: notify a client that the connection to the
5394 * requested remote peer is not possible (for instance, no route found)
5395 * Function called when the socket is ready to queue more data. "buf" will be
5396 * NULL and "size" zero if the socket was closed for writing in the meantime.
5398 * @param cls closure
5399 * @param size number of bytes available in buf
5400 * @param buf where the callee should write the message
5401 * @return number of bytes written to buf
5404 notify_client_connection_failure (void *cls, size_t size, void *buf)
5407 struct MeshPeerInfo *peer_info;
5408 struct GNUNET_MESH_PeerControl *msg;
5409 struct GNUNET_PeerIdentity id;
5411 if (0 == size && NULL == buf)
5413 // TODO retry? cancel?
5417 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5418 peer_info = (struct MeshPeerInfo *) cls;
5419 msg = (struct GNUNET_MESH_PeerControl *) buf;
5420 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5421 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5422 // msg->tunnel_id = htonl(peer_info->t->tid);
5423 GNUNET_PEER_resolve (peer_info->id, &id);
5424 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5432 * Send keepalive packets for a peer
5434 * @param cls Closure (tunnel for which to send the keepalive).
5435 * @param tc Notification context.
5437 * TODO: implement explicit multicast keepalive?
5440 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5442 struct MeshTunnel *t = cls;
5443 struct GNUNET_MessageHeader *payload;
5444 struct GNUNET_MESH_Multicast *msg;
5446 sizeof (struct GNUNET_MESH_Multicast) +
5447 sizeof (struct GNUNET_MessageHeader);
5450 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5454 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5457 "sending keepalive for tunnel %d\n", t->id.tid);
5459 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5460 msg->header.size = htons (size);
5461 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5462 msg->oid = my_full_id;
5463 msg->tid = htonl (t->id.tid);
5464 msg->ttl = htonl (default_ttl);
5465 msg->pid = htonl (t->fwd_pid + 1);
5467 payload = (struct GNUNET_MessageHeader *) &msg[1];
5468 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5469 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5470 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5472 t->path_refresh_task =
5473 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5479 * Function to process paths received for a new peer addition. The recorded
5480 * paths form the initial tunnel, which can be optimized later.
5481 * Called on each result obtained for the DHT search.
5483 * @param cls closure
5484 * @param exp when will this value expire
5485 * @param key key of the result
5486 * @param get_path path of the get request
5487 * @param get_path_length lenght of get_path
5488 * @param put_path path of the put request
5489 * @param put_path_length length of the put_path
5490 * @param type type of the result
5491 * @param size number of bytes in data
5492 * @param data pointer to the result data
5494 * TODO: re-issue the request after certain time? cancel after X results?
5497 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5498 const struct GNUNET_HashCode * key,
5499 const struct GNUNET_PeerIdentity *get_path,
5500 unsigned int get_path_length,
5501 const struct GNUNET_PeerIdentity *put_path,
5502 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5503 size_t size, const void *data)
5505 struct MeshPathInfo *path_info = cls;
5506 struct MeshPeerPath *p;
5507 struct GNUNET_PeerIdentity pi;
5510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5511 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5514 p = path_build_from_dht (get_path, get_path_length, put_path,
5516 path_add_to_peers (p, GNUNET_NO);
5518 for (i = 0; i < path_info->peer->ntunnels; i++)
5520 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5521 peer_info_connect (path_info->peer, path_info->t);
5529 * Function to process paths received for a new peer addition. The recorded
5530 * paths form the initial tunnel, which can be optimized later.
5531 * Called on each result obtained for the DHT search.
5533 * @param cls closure
5534 * @param exp when will this value expire
5535 * @param key key of the result
5536 * @param get_path path of the get request
5537 * @param get_path_length lenght of get_path
5538 * @param put_path path of the put request
5539 * @param put_path_length length of the put_path
5540 * @param type type of the result
5541 * @param size number of bytes in data
5542 * @param data pointer to the result data
5545 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5546 const struct GNUNET_HashCode * key,
5547 const struct GNUNET_PeerIdentity *get_path,
5548 unsigned int get_path_length,
5549 const struct GNUNET_PeerIdentity *put_path,
5550 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5551 size_t size, const void *data)
5553 const struct PBlock *pb = data;
5554 const struct GNUNET_PeerIdentity *pi = &pb->id;
5555 struct MeshTunnel *t = cls;
5556 struct MeshPeerInfo *peer_info;
5557 struct MeshPeerPath *p;
5559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5560 if (size != sizeof (struct PBlock))
5562 GNUNET_break_op (0);
5565 if (ntohl(pb->type) != t->type)
5567 GNUNET_break_op (0);
5570 GNUNET_assert (NULL != t->owner);
5571 peer_info = peer_info_get (pi);
5572 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5574 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5576 p = path_build_from_dht (get_path, get_path_length, put_path,
5578 path_add_to_peers (p, GNUNET_NO);
5580 tunnel_add_peer (t, peer_info);
5581 peer_info_connect (peer_info, t);
5586 * Function to process DHT string to regex matching.
5587 * Called on each result obtained for the DHT search.
5589 * @param cls closure (search context)
5590 * @param exp when will this value expire
5591 * @param key key of the result
5592 * @param get_path path of the get request (not used)
5593 * @param get_path_length lenght of get_path (not used)
5594 * @param put_path path of the put request (not used)
5595 * @param put_path_length length of the put_path (not used)
5596 * @param type type of the result
5597 * @param size number of bytes in data
5598 * @param data pointer to the result data
5601 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5602 const struct GNUNET_HashCode * key,
5603 const struct GNUNET_PeerIdentity *get_path,
5604 unsigned int get_path_length,
5605 const struct GNUNET_PeerIdentity *put_path,
5606 unsigned int put_path_length,
5607 enum GNUNET_BLOCK_Type type,
5608 size_t size, const void *data)
5610 const struct MeshRegexAccept *block = data;
5611 struct MeshRegexSearchContext *ctx = cls;
5612 struct MeshRegexSearchInfo *info = ctx->info;
5613 struct MeshPeerPath *p;
5614 struct MeshPeerInfo *peer_info;
5616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5619 peer_info = peer_info_get(&block->id);
5620 p = path_build_from_dht (get_path, get_path_length, put_path,
5622 path_add_to_peers (p, GNUNET_NO);
5625 tunnel_add_peer (info->t, peer_info);
5626 peer_info_connect (peer_info, info->t);
5627 if (0 == info->peer)
5629 info->peer = peer_info->id;
5633 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5636 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5637 ®ex_connect_timeout,
5645 * Function to process DHT string to regex matching.
5646 * Called on each result obtained for the DHT search.
5648 * @param cls closure (search context)
5649 * @param exp when will this value expire
5650 * @param key key of the result
5651 * @param get_path path of the get request (not used)
5652 * @param get_path_length lenght of get_path (not used)
5653 * @param put_path path of the put request (not used)
5654 * @param put_path_length length of the put_path (not used)
5655 * @param type type of the result
5656 * @param size number of bytes in data
5657 * @param data pointer to the result data
5659 * TODO: re-issue the request after certain time? cancel after X results?
5662 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5663 const struct GNUNET_HashCode * key,
5664 const struct GNUNET_PeerIdentity *get_path,
5665 unsigned int get_path_length,
5666 const struct GNUNET_PeerIdentity *put_path,
5667 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5668 size_t size, const void *data)
5670 const struct MeshRegexBlock *block = data;
5671 struct MeshRegexSearchContext *ctx = cls;
5672 struct MeshRegexSearchInfo *info = ctx->info;
5676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5677 "DHT GET STRING RETURNED RESULTS\n");
5678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5679 " key: %s\n", GNUNET_h2s (key));
5681 copy = GNUNET_malloc (size);
5682 memcpy (copy, data, size);
5683 GNUNET_break (GNUNET_OK ==
5684 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5686 len = ntohl (block->n_proof);
5688 char proof[len + 1];
5690 memcpy (proof, &block[1], len);
5692 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5694 GNUNET_break_op (0);
5698 len = strlen (info->description);
5699 if (len == ctx->position) // String processed
5701 if (GNUNET_YES == ntohl (block->accepting))
5703 regex_find_path(key, ctx);
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5708 // FIXME REGEX this block not successful, wait for more? start timeout?
5712 GNUNET_break (GNUNET_OK ==
5713 GNUNET_MESH_regex_block_iterate (block, size,
5714 ®ex_edge_iterator, ctx));
5718 /******************************************************************************/
5719 /********************* MESH LOCAL HANDLES **************************/
5720 /******************************************************************************/
5724 * Handler for client disconnection
5726 * @param cls closure
5727 * @param client identification of the client; NULL
5728 * for the last call when the server is destroyed
5731 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5733 struct MeshClient *c;
5734 struct MeshClient *next;
5737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5746 if (c->handle != client)
5748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5754 GNUNET_SERVER_client_drop (c->handle);
5755 c->shutting_down = GNUNET_YES;
5756 GNUNET_assert (NULL != c->own_tunnels);
5757 GNUNET_assert (NULL != c->incoming_tunnels);
5758 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5759 &tunnel_destroy_iterator, c);
5760 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5761 &tunnel_destroy_iterator, c);
5762 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5763 &tunnel_destroy_iterator, c);
5764 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5765 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5766 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5768 /* deregister clients applications */
5769 if (NULL != c->apps)
5771 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5772 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5774 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5775 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5777 GNUNET_SCHEDULER_cancel (announce_applications_task);
5778 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5780 if (NULL != c->types)
5781 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5782 for (i = 0; i < c->n_regex; i++)
5784 GNUNET_free (c->regexes[i]);
5786 GNUNET_free_non_null (c->regexes);
5787 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5788 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5790 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5793 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5802 * Handler for new clients
5804 * @param cls closure
5805 * @param client identification of the client
5806 * @param message the actual message, which includes messages the client wants
5809 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5810 const struct GNUNET_MessageHeader *message)
5812 struct GNUNET_MESH_ClientConnect *cc_msg;
5813 struct MeshClient *c;
5814 GNUNET_MESH_ApplicationType *a;
5821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5822 /* Check data sanity */
5823 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5824 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5825 ntypes = ntohs (cc_msg->types);
5826 napps = ntohs (cc_msg->applications);
5828 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5835 /* Create new client structure */
5836 c = GNUNET_malloc (sizeof (struct MeshClient));
5837 c->id = next_client_id++;
5838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5840 GNUNET_SERVER_client_keep (client);
5841 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5844 GNUNET_MESH_ApplicationType at;
5845 struct GNUNET_HashCode hc;
5847 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5848 for (i = 0; i < napps; i++)
5851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5852 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5853 /* store in clients hashmap */
5854 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5855 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5856 /* store in global hashmap, for announcements */
5857 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5858 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5860 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5861 announce_applications_task =
5862 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5868 struct GNUNET_HashCode hc;
5870 t = (uint16_t *) & a[napps];
5871 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5872 for (i = 0; i < ntypes; i++)
5875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5876 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5878 /* store in clients hashmap */
5879 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5880 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5881 /* store in global hashmap */
5882 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5883 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5887 " client has %u+%u subscriptions\n", napps, ntypes);
5889 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5890 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5891 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5892 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5893 GNUNET_SERVER_notification_context_add (nc, client);
5894 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5896 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5902 * Handler for clients announcing available services by a regular expression.
5904 * @param cls closure
5905 * @param client identification of the client
5906 * @param message the actual message, which includes messages the client wants
5909 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5910 const struct GNUNET_MessageHeader *message)
5912 struct MeshClient *c;
5916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5918 /* Sanity check for client registration */
5919 if (NULL == (c = client_get (client)))
5922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5927 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5928 regex = GNUNET_malloc (len + 1);
5929 memcpy (regex, &message[1], len);
5931 GNUNET_array_append (c->regexes, c->n_regex, regex);
5932 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5934 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5940 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5946 * Handler for requests of new tunnels
5948 * @param cls closure
5949 * @param client identification of the client
5950 * @param message the actual message
5953 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5954 const struct GNUNET_MessageHeader *message)
5956 struct GNUNET_MESH_TunnelMessage *t_msg;
5957 struct MeshTunnel *t;
5958 struct MeshClient *c;
5959 MESH_TunnelNumber tid;
5961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5963 /* Sanity check for client registration */
5964 if (NULL == (c = client_get (client)))
5967 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5972 /* Message sanity check */
5973 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5976 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5980 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5981 /* Sanity check for tunnel numbering */
5982 tid = ntohl (t_msg->tunnel_id);
5983 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5986 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5989 /* Sanity check for duplicate tunnel IDs */
5990 if (NULL != tunnel_get_by_local_id (c, tid))
5993 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5997 while (NULL != tunnel_get_by_pi (myid, next_tid))
5998 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5999 t = tunnel_new (myid, next_tid++, c, tid);
6002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6007 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6009 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6010 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6013 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6019 * Handler for requests of deleting tunnels
6021 * @param cls closure
6022 * @param client identification of the client
6023 * @param message the actual message
6026 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6027 const struct GNUNET_MessageHeader *message)
6029 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6030 struct MeshClient *c;
6031 struct MeshTunnel *t;
6032 MESH_TunnelNumber tid;
6034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6035 "Got a DESTROY TUNNEL from client!\n");
6037 /* Sanity check for client registration */
6038 if (NULL == (c = client_get (client)))
6041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6046 /* Message sanity check */
6047 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6054 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6056 /* Retrieve tunnel */
6057 tid = ntohl (tunnel_msg->tunnel_id);
6058 t = tunnel_get_by_local_id(c, tid);
6061 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6063 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6066 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6068 client_ignore_tunnel (c, t);
6070 // TODO: when to destroy incoming tunnel?
6071 if (t->nclients == 0)
6073 GNUNET_assert (GNUNET_YES ==
6074 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6076 GNUNET_assert (GNUNET_YES ==
6077 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6078 &my_full_id.hashPubKey,
6082 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6085 send_client_tunnel_disconnect(t, c);
6086 client_delete_tunnel(c, t);
6088 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6090 tunnel_send_destroy (t);
6091 t->destroy = GNUNET_YES;
6092 // The tunnel will be destroyed when the last message is transmitted.
6093 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6099 * Handler for requests of seeting tunnel's speed.
6101 * @param cls Closure (unused).
6102 * @param client Identification of the client.
6103 * @param message The actual message.
6106 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6107 const struct GNUNET_MessageHeader *message)
6109 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6110 struct MeshClient *c;
6111 struct MeshTunnel *t;
6112 MESH_TunnelNumber tid;
6114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6115 "Got a SPEED request from client!\n");
6117 /* Sanity check for client registration */
6118 if (NULL == (c = client_get (client)))
6121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6127 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6129 /* Retrieve tunnel */
6130 tid = ntohl (tunnel_msg->tunnel_id);
6131 t = tunnel_get_by_local_id(c, tid);
6134 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6140 switch (ntohs(message->type))
6142 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6143 t->speed_min = GNUNET_YES;
6145 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6146 t->speed_min = GNUNET_NO;
6151 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6156 * Handler for requests of seeting tunnel's buffering policy.
6158 * @param cls Closure (unused).
6159 * @param client Identification of the client.
6160 * @param message The actual message.
6163 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6164 const struct GNUNET_MessageHeader *message)
6166 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6167 struct MeshClient *c;
6168 struct MeshTunnel *t;
6169 MESH_TunnelNumber tid;
6171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6172 "Got a BUFFER request from client!\n");
6174 /* Sanity check for client registration */
6175 if (NULL == (c = client_get (client)))
6178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6183 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6185 /* Retrieve tunnel */
6186 tid = ntohl (tunnel_msg->tunnel_id);
6187 t = tunnel_get_by_local_id(c, tid);
6190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6196 switch (ntohs(message->type))
6198 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6199 t->nobuffer = GNUNET_NO;
6201 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6202 t->nobuffer = GNUNET_YES;
6208 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6213 * Handler for connection requests to new peers
6215 * @param cls closure
6216 * @param client identification of the client
6217 * @param message the actual message (PeerControl)
6220 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6221 const struct GNUNET_MessageHeader *message)
6223 struct GNUNET_MESH_PeerControl *peer_msg;
6224 struct MeshPeerInfo *peer_info;
6225 struct MeshClient *c;
6226 struct MeshTunnel *t;
6227 MESH_TunnelNumber tid;
6229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6230 /* Sanity check for client registration */
6231 if (NULL == (c = client_get (client)))
6234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6239 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6241 /* Sanity check for message size */
6242 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6249 /* Tunnel exists? */
6250 tid = ntohl (peer_msg->tunnel_id);
6251 t = tunnel_get_by_local_id (c, tid);
6255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6259 /* Does client own tunnel? */
6260 if (t->owner->handle != client)
6263 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6267 GNUNET_i2s (&peer_msg->peer));
6268 peer_info = peer_info_get (&peer_msg->peer);
6270 tunnel_add_peer (t, peer_info);
6271 peer_info_connect (peer_info, t);
6273 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6279 * Handler for disconnection requests of peers in a tunnel
6281 * @param cls closure
6282 * @param client identification of the client
6283 * @param message the actual message (PeerControl)
6286 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6287 const struct GNUNET_MessageHeader *message)
6289 struct GNUNET_MESH_PeerControl *peer_msg;
6290 struct MeshPeerInfo *peer_info;
6291 struct MeshClient *c;
6292 struct MeshTunnel *t;
6293 MESH_TunnelNumber tid;
6295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6296 /* Sanity check for client registration */
6297 if (NULL == (c = client_get (client)))
6300 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6305 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6307 /* Sanity check for message size */
6308 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6315 /* Tunnel exists? */
6316 tid = ntohl (peer_msg->tunnel_id);
6317 t = tunnel_get_by_local_id (c, tid);
6321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6326 /* Does client own tunnel? */
6327 if (t->owner->handle != client)
6330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6335 GNUNET_i2s (&peer_msg->peer));
6336 /* Is the peer in the tunnel? */
6338 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6339 if (NULL == peer_info)
6342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6346 /* Ok, delete peer from tunnel */
6347 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6348 &peer_msg->peer.hashPubKey);
6350 send_destroy_path (t, peer_info->id);
6351 tunnel_delete_peer (t, peer_info->id);
6352 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6357 * Handler for blacklist requests of peers in a tunnel
6359 * @param cls closure
6360 * @param client identification of the client
6361 * @param message the actual message (PeerControl)
6363 * FIXME implement DHT block bloomfilter
6366 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6367 const struct GNUNET_MessageHeader *message)
6369 struct GNUNET_MESH_PeerControl *peer_msg;
6370 struct MeshClient *c;
6371 struct MeshTunnel *t;
6372 MESH_TunnelNumber tid;
6374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6375 /* Sanity check for client registration */
6376 if (NULL == (c = client_get (client)))
6379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6384 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6386 /* Sanity check for message size */
6387 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6394 /* Tunnel exists? */
6395 tid = ntohl (peer_msg->tunnel_id);
6396 t = tunnel_get_by_local_id (c, tid);
6400 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6405 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6406 GNUNET_PEER_intern(&peer_msg->peer));
6411 * Handler for unblacklist requests of peers in a tunnel
6413 * @param cls closure
6414 * @param client identification of the client
6415 * @param message the actual message (PeerControl)
6418 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6419 const struct GNUNET_MessageHeader *message)
6421 struct GNUNET_MESH_PeerControl *peer_msg;
6422 struct MeshClient *c;
6423 struct MeshTunnel *t;
6424 MESH_TunnelNumber tid;
6428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6429 /* Sanity check for client registration */
6430 if (NULL == (c = client_get (client)))
6433 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6438 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6440 /* Sanity check for message size */
6441 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6448 /* Tunnel exists? */
6449 tid = ntohl (peer_msg->tunnel_id);
6450 t = tunnel_get_by_local_id (c, tid);
6454 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6459 /* if peer is not known, complain */
6460 pid = GNUNET_PEER_search (&peer_msg->peer);
6467 /* search and remove from list */
6468 for (i = 0; i < t->nblacklisted; i++)
6470 if (t->blacklisted[i] == pid)
6472 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6473 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6478 /* if peer hasn't been blacklisted, complain */
6484 * Handler for connection requests to new peers by type
6486 * @param cls closure
6487 * @param client identification of the client
6488 * @param message the actual message (ConnectPeerByType)
6491 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6492 const struct GNUNET_MessageHeader *message)
6494 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6495 struct MeshClient *c;
6496 struct MeshTunnel *t;
6497 struct GNUNET_HashCode hash;
6498 MESH_TunnelNumber tid;
6500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6501 /* Sanity check for client registration */
6502 if (NULL == (c = client_get (client)))
6505 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6510 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6512 /* Sanity check for message size */
6513 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6514 ntohs (connect_msg->header.size))
6517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6521 /* Tunnel exists? */
6522 tid = ntohl (connect_msg->tunnel_id);
6523 t = tunnel_get_by_local_id (c, tid);
6527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6531 /* Does client own tunnel? */
6532 if (t->owner->handle != client)
6535 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6539 /* Do WE have the service? */
6540 t->type = ntohl (connect_msg->type);
6541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6542 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6543 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6546 /* Yes! Fast forward, add ourselves to the tunnel and send the
6547 * good news to the client, and alert the destination client of
6548 * an incoming tunnel.
6550 * FIXME send a path create to self, avoid code duplication
6552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6553 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6554 peer_info_get (&my_full_id),
6555 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6558 send_client_peer_connected (t, myid);
6559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6560 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6562 t->local_tid_dest = next_local_tid++;
6563 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6564 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6565 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6569 /* Ok, lets find a peer offering the service */
6570 if (NULL != t->dht_get_type)
6572 GNUNET_DHT_get_stop (t->dht_get_type);
6574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6575 GNUNET_h2s (&hash));
6577 GNUNET_DHT_get_start (dht_handle,
6578 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6580 dht_replication_level,
6581 GNUNET_DHT_RO_RECORD_ROUTE |
6582 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6584 &dht_get_type_handler, t);
6586 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6592 * Handler for connection requests to new peers by a string service description.
6594 * @param cls closure
6595 * @param client identification of the client
6596 * @param message the actual message, which includes messages the client wants
6599 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6600 const struct GNUNET_MessageHeader *message)
6602 struct GNUNET_MESH_ConnectPeerByString *msg;
6603 struct MeshRegexSearchContext *ctx;
6604 struct MeshRegexSearchInfo *info;
6605 struct GNUNET_DHT_GetHandle *get_h;
6606 struct GNUNET_HashCode key;
6607 struct MeshTunnel *t;
6608 struct MeshClient *c;
6609 MESH_TunnelNumber tid;
6613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6614 "Connect by string started\n");
6615 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6616 size = htons (message->size);
6618 /* Sanity check for client registration */
6619 if (NULL == (c = client_get (client)))
6622 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6627 /* Message size sanity check */
6628 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6635 /* Tunnel exists? */
6636 tid = ntohl (msg->tunnel_id);
6637 t = tunnel_get_by_local_id (c, tid);
6641 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6645 /* Does client own tunnel? */
6646 if (t->owner->handle != client)
6649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6654 " on tunnel %s [%u]\n",
6655 GNUNET_i2s(&my_full_id),
6658 /* Only one connect_by_string allowed at the same time! */
6659 /* FIXME: allow more, return handle at api level to cancel, document */
6660 if (NULL != t->regex_ctx)
6663 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6667 /* Find string itself */
6668 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6669 string = (const char *) &msg[1];
6671 /* Initialize context */
6672 size = GNUNET_REGEX_get_first_key(string, len, &key);
6673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6674 " consumed %u bits out of %u\n", size, len);
6675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6676 " looking for %s\n", GNUNET_h2s (&key));
6678 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6680 info->description = GNUNET_malloc (len + 1);
6681 memcpy (info->description, string, len);
6682 info->description[len] = '\0';
6683 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6684 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6687 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6688 ctx->position = size;
6692 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6694 /* Start search in DHT */
6695 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6696 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6697 &key, /* key to search */
6698 dht_replication_level, /* replication level */
6699 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6700 NULL, /* xquery */ // FIXME BLOOMFILTER
6701 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6702 &dht_get_string_handler, ctx);
6704 GNUNET_break (GNUNET_OK ==
6705 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6708 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6710 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6716 * Handler for client traffic directed to one peer
6718 * @param cls closure
6719 * @param client identification of the client
6720 * @param message the actual message
6723 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6724 const struct GNUNET_MessageHeader *message)
6726 struct MeshClient *c;
6727 struct MeshTunnel *t;
6728 struct MeshPeerInfo *pi;
6729 struct GNUNET_MESH_Unicast *data_msg;
6730 MESH_TunnelNumber tid;
6733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6734 "Got a unicast request from a client!\n");
6736 /* Sanity check for client registration */
6737 if (NULL == (c = client_get (client)))
6740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6745 data_msg = (struct GNUNET_MESH_Unicast *) message;
6747 /* Sanity check for message size */
6748 size = ntohs (message->size);
6749 if (sizeof (struct GNUNET_MESH_Unicast) +
6750 sizeof (struct GNUNET_MessageHeader) > size)
6753 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6757 /* Tunnel exists? */
6758 tid = ntohl (data_msg->tid);
6759 t = tunnel_get_by_local_id (c, tid);
6763 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6767 /* Is it a local tunnel? Then, does client own the tunnel? */
6768 if (t->owner->handle != client)
6771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6775 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6776 &data_msg->destination.hashPubKey);
6777 /* Is the selected peer in the tunnel? */
6781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6785 /* PID should be as expected */
6786 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6789 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6790 "Unicast PID, expected %u, got %u\n",
6791 t->fwd_pid + 1, ntohl (data_msg->pid));
6792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6796 /* Ok, everything is correct, send the message
6797 * (pretend we got it from a mesh peer)
6800 /* Work around const limitation */
6801 char buf[ntohs (message->size)] GNUNET_ALIGN;
6802 struct GNUNET_MESH_Unicast *copy;
6804 copy = (struct GNUNET_MESH_Unicast *) buf;
6805 memcpy (buf, data_msg, size);
6806 copy->oid = my_full_id;
6807 copy->tid = htonl (t->id.tid);
6808 copy->ttl = htonl (default_ttl);
6809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6810 " calling generic handler...\n");
6811 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6814 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6820 * Handler for client traffic directed to the origin
6822 * @param cls closure
6823 * @param client identification of the client
6824 * @param message the actual message
6827 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6828 const struct GNUNET_MessageHeader *message)
6830 struct GNUNET_MESH_ToOrigin *data_msg;
6831 struct MeshTunnelClientInfo *clinfo;
6832 struct MeshClient *c;
6833 struct MeshTunnel *t;
6834 MESH_TunnelNumber tid;
6837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6838 "Got a ToOrigin request from a client!\n");
6839 /* Sanity check for client registration */
6840 if (NULL == (c = client_get (client)))
6843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6848 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6850 /* Sanity check for message size */
6851 size = ntohs (message->size);
6852 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6853 sizeof (struct GNUNET_MessageHeader) > size)
6856 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6860 /* Tunnel exists? */
6861 tid = ntohl (data_msg->tid);
6862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6863 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6869 t = tunnel_get_by_local_id (c, tid);
6873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6877 /* It should be sent by someone who has this as incoming tunnel. */
6878 if (GNUNET_NO == client_knows_tunnel (c, t))
6881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6885 /* PID should be as expected */
6886 clinfo = tunnel_get_client_fc (t, c);
6887 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6890 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6891 "To Origin PID, expected %u, got %u\n",
6892 clinfo->bck_pid + 1, ntohl (data_msg->pid));
6893 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6897 /* Ok, everything is correct, send the message
6898 * (pretend we got it from a mesh peer)
6901 char buf[ntohs (message->size)] GNUNET_ALIGN;
6902 struct GNUNET_MESH_ToOrigin *copy;
6904 /* Work around const limitation */
6905 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6906 memcpy (buf, data_msg, size);
6907 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6908 copy->tid = htonl (t->id.tid);
6909 copy->ttl = htonl (default_ttl);
6910 GNUNET_assert (ntohl (copy->pid) == (t->bck_pid + 1));
6911 copy->sender = my_full_id;
6912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6913 " calling generic handler...\n");
6914 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6916 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6922 * Handler for client traffic directed to all peers in a tunnel
6924 * @param cls closure
6925 * @param client identification of the client
6926 * @param message the actual message
6929 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6930 const struct GNUNET_MessageHeader *message)
6932 struct MeshClient *c;
6933 struct MeshTunnel *t;
6934 struct GNUNET_MESH_Multicast *data_msg;
6935 MESH_TunnelNumber tid;
6937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6938 "Got a multicast request from a client!\n");
6940 /* Sanity check for client registration */
6941 if (NULL == (c = client_get (client)))
6944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6949 data_msg = (struct GNUNET_MESH_Multicast *) message;
6951 /* Sanity check for message size */
6952 if (sizeof (struct GNUNET_MESH_Multicast) +
6953 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6960 /* Tunnel exists? */
6961 tid = ntohl (data_msg->tid);
6962 t = tunnel_get_by_local_id (c, tid);
6966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6970 /* Does client own tunnel? */
6971 if (t->owner->handle != client)
6974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6978 /* PID should be as expected */
6979 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6983 "Multicast PID, expected %u, got %u\n",
6984 t->fwd_pid + 1, ntohl (data_msg->pid));
6985 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6990 char buf[ntohs (message->size)] GNUNET_ALIGN;
6991 struct GNUNET_MESH_Multicast *copy;
6993 copy = (struct GNUNET_MESH_Multicast *) buf;
6994 memcpy (buf, message, ntohs (message->size));
6995 copy->oid = my_full_id;
6996 copy->tid = htonl (t->id.tid);
6997 copy->ttl = htonl (default_ttl);
6998 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
6999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7000 " calling generic handler...\n");
7001 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7004 /* receive done gets called when last copy is sent to a neighbor */
7010 * Handler for client's ACKs for payload traffic.
7012 * @param cls Closure (unused).
7013 * @param client Identification of the client.
7014 * @param message The actual message.
7017 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7018 const struct GNUNET_MessageHeader *message)
7020 struct GNUNET_MESH_LocalAck *msg;
7021 struct MeshTunnel *t;
7022 struct MeshClient *c;
7023 MESH_TunnelNumber tid;
7026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7027 /* Sanity check for client registration */
7028 if (NULL == (c = client_get (client)))
7031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7036 msg = (struct GNUNET_MESH_LocalAck *) message;
7038 /* Tunnel exists? */
7039 tid = ntohl (msg->tunnel_id);
7040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7041 t = tunnel_get_by_local_id (c, tid);
7044 GNUNET_break (0); // FIXME fc
7045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7049 ack = ntohl (msg->max_pid);
7050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7052 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7053 if (NULL != t->owner && t->owner->handle == client)
7055 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7057 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7061 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7062 tunnel_set_client_fwd_ack (t, c, ack);
7063 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7066 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7072 * Functions to handle messages from clients
7074 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7075 {&handle_local_new_client, NULL,
7076 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7077 {&handle_local_announce_regex, NULL,
7078 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7079 {&handle_local_tunnel_create, NULL,
7080 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7081 sizeof (struct GNUNET_MESH_TunnelMessage)},
7082 {&handle_local_tunnel_destroy, NULL,
7083 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7084 sizeof (struct GNUNET_MESH_TunnelMessage)},
7085 {&handle_local_tunnel_speed, NULL,
7086 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7087 sizeof (struct GNUNET_MESH_TunnelMessage)},
7088 {&handle_local_tunnel_speed, NULL,
7089 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7090 sizeof (struct GNUNET_MESH_TunnelMessage)},
7091 {&handle_local_tunnel_buffer, NULL,
7092 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7093 sizeof (struct GNUNET_MESH_TunnelMessage)},
7094 {&handle_local_tunnel_buffer, NULL,
7095 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7096 sizeof (struct GNUNET_MESH_TunnelMessage)},
7097 {&handle_local_connect_add, NULL,
7098 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7099 sizeof (struct GNUNET_MESH_PeerControl)},
7100 {&handle_local_connect_del, NULL,
7101 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7102 sizeof (struct GNUNET_MESH_PeerControl)},
7103 {&handle_local_blacklist, NULL,
7104 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7105 sizeof (struct GNUNET_MESH_PeerControl)},
7106 {&handle_local_unblacklist, NULL,
7107 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7108 sizeof (struct GNUNET_MESH_PeerControl)},
7109 {&handle_local_connect_by_type, NULL,
7110 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7111 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7112 {&handle_local_connect_by_string, NULL,
7113 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7114 {&handle_local_unicast, NULL,
7115 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7116 {&handle_local_to_origin, NULL,
7117 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7118 {&handle_local_multicast, NULL,
7119 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7120 {&handle_local_ack, NULL,
7121 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7122 sizeof (struct GNUNET_MESH_LocalAck)},
7128 * To be called on core init/fail.
7130 * @param cls service closure
7131 * @param server handle to the server for this service
7132 * @param identity the public identity of this peer
7135 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7136 const struct GNUNET_PeerIdentity *identity)
7139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7140 core_handle = server;
7141 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7145 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7147 GNUNET_abort(); // Try harder
7154 * Method called whenever a given peer connects.
7156 * @param cls closure
7157 * @param peer peer identity this notification is about
7158 * @param atsi performance data for the connection
7159 * @param atsi_count number of records in 'atsi'
7162 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7163 const struct GNUNET_ATS_Information *atsi,
7164 unsigned int atsi_count)
7166 struct MeshPeerInfo *peer_info;
7167 struct MeshPeerPath *path;
7169 DEBUG_CONN ("Peer connected\n");
7170 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7171 peer_info = peer_info_get (peer);
7172 if (myid == peer_info->id)
7174 DEBUG_CONN (" (self)\n");
7179 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7181 path = path_new (2);
7182 path->peers[0] = myid;
7183 path->peers[1] = peer_info->id;
7184 GNUNET_PEER_change_rc (myid, 1);
7185 GNUNET_PEER_change_rc (peer_info->id, 1);
7186 peer_info_add_path (peer_info, path, GNUNET_YES);
7187 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7193 * Method called whenever a peer disconnects.
7195 * @param cls closure
7196 * @param peer peer identity this notification is about
7199 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7201 struct MeshPeerInfo *pi;
7202 struct MeshPeerQueue *q;
7203 struct MeshPeerQueue *n;
7205 DEBUG_CONN ("Peer disconnected\n");
7206 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7218 /* try to reroute this traffic instead */
7219 queue_destroy(q, GNUNET_YES);
7223 peer_info_remove_path (pi, pi->id, myid);
7226 DEBUG_CONN (" (self)\n");
7228 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7233 /******************************************************************************/
7234 /************************ MAIN FUNCTIONS ****************************/
7235 /******************************************************************************/
7238 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7240 * @param cls closure
7241 * @param key current key code
7242 * @param value value in the hash map
7243 * @return GNUNET_YES if we should continue to iterate,
7247 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7249 struct MeshTunnel *t = value;
7256 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7258 * @param cls closure
7259 * @param key current key code
7260 * @param value value in the hash map
7261 * @return GNUNET_YES if we should continue to iterate,
7265 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7267 struct MeshPeerInfo *p = value;
7268 struct MeshPeerQueue *q;
7269 struct MeshPeerQueue *n;
7277 queue_destroy(q, GNUNET_YES);
7281 peer_info_destroy (p);
7286 * Task run during shutdown.
7292 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7296 if (core_handle != NULL)
7298 GNUNET_CORE_disconnect (core_handle);
7301 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7302 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7303 if (dht_handle != NULL)
7305 GNUNET_DHT_disconnect (dht_handle);
7310 GNUNET_SERVER_notification_context_destroy (nc);
7313 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7315 GNUNET_SCHEDULER_cancel (announce_id_task);
7316 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7322 * Process mesh requests.
7324 * @param cls closure
7325 * @param server the initialized server
7326 * @param c configuration to use
7329 run (void *cls, struct GNUNET_SERVER_Handle *server,
7330 const struct GNUNET_CONFIGURATION_Handle *c)
7332 struct MeshPeerInfo *peer;
7333 struct MeshPeerPath *p;
7336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7337 server_handle = server;
7338 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7339 NULL, /* Closure passed to MESH functions */
7340 &core_init, /* Call core_init once connected */
7341 &core_connect, /* Handle connects */
7342 &core_disconnect, /* remove peers on disconnects */
7343 NULL, /* Don't notify about all incoming messages */
7344 GNUNET_NO, /* For header only in notification */
7345 NULL, /* Don't notify about all outbound messages */
7346 GNUNET_NO, /* For header-only out notification */
7347 core_handlers); /* Register these handlers */
7349 if (core_handle == NULL)
7352 GNUNET_SCHEDULER_shutdown ();
7357 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7362 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7364 GNUNET_SCHEDULER_shutdown ();
7369 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7370 &refresh_path_time))
7372 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7374 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7375 "refresh path time");
7376 GNUNET_SCHEDULER_shutdown ();
7381 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7382 &app_announce_time))
7384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7386 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7387 "app announce time");
7388 GNUNET_SCHEDULER_shutdown ();
7393 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7398 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7399 "id announce time");
7400 GNUNET_SCHEDULER_shutdown ();
7405 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7406 &unacknowledged_wait_time))
7408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7410 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7411 "unacknowledged wait time");
7412 GNUNET_SCHEDULER_shutdown ();
7417 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7422 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7424 GNUNET_SCHEDULER_shutdown ();
7429 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7434 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7436 GNUNET_SCHEDULER_shutdown ();
7441 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7446 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7448 GNUNET_SCHEDULER_shutdown ();
7453 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7458 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7464 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7465 &dht_replication_level))
7467 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7469 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7470 "dht replication level", 10);
7471 dht_replication_level = 10;
7475 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7476 GNUNET_free (keyfile);
7477 if (my_private_key == NULL)
7479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7480 _("Mesh service could not access hostkey. Exiting.\n"));
7481 GNUNET_SCHEDULER_shutdown ();
7484 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7485 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7486 &my_full_id.hashPubKey);
7487 myid = GNUNET_PEER_intern (&my_full_id);
7489 // transport_handle = GNUNET_TRANSPORT_connect(c,
7496 dht_handle = GNUNET_DHT_connect (c, 64);
7497 if (dht_handle == NULL)
7502 stats = GNUNET_STATISTICS_create ("mesh", c);
7506 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7508 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7509 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7510 peers = GNUNET_CONTAINER_multihashmap_create (32);
7511 applications = GNUNET_CONTAINER_multihashmap_create (32);
7512 types = GNUNET_CONTAINER_multihashmap_create (32);
7514 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7515 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7516 GNUNET_SERVER_disconnect_notify (server_handle,
7517 &handle_local_client_disconnect, NULL);
7521 clients_tail = NULL;
7524 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7525 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7527 /* Create a peer_info for the local peer */
7528 peer = peer_info_get (&my_full_id);
7531 GNUNET_PEER_change_rc (myid, 1);
7532 peer_info_add_path (peer, p, GNUNET_YES);
7534 /* Scheduled the task to clean up when shutdown is called */
7535 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7542 * The main function for the mesh service.
7544 * @param argc number of arguments from the command line
7545 * @param argv command line arguments
7546 * @return 0 ok, 1 on error
7549 main (int argc, char *const *argv)
7553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7556 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");