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)
3748 /* Find client whom to allow to send to origin (with lowest buffer space) */
3749 for (i = 0; i < t->nclients; i++)
3751 struct MeshTunnelClientInfo *clinfo;
3754 clinfo = &t->clients_fc[i];
3755 delta = clinfo->bck_ack - clinfo->bck_pid;
3757 if ((GNUNET_NO == t->nobuffer && ACK_THRESHOLD > delta) ||
3758 (GNUNET_YES == t->nobuffer && 0 == delta))
3762 ack = clinfo->bck_pid;
3763 ack += t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3765 " sending ack to client %u: %u\n",
3766 t->clients[i]->id, ack);
3767 send_local_ack (t, t->clients[i], ack);
3768 clinfo->bck_ack = ack;
3775 * Send an ACK informing the children nodes and destination clients about
3776 * the available buffer space.
3777 * If buffering is off, send only on behalf of root (can be self).
3778 * If buffering is on, send when sent to predecessor and buffer space is free.
3779 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3780 * the ACK itself goes "forward" (towards children/clients).
3782 * @param t Tunnel on which to send the ACK.
3783 * @param type Type of message that triggered the ACK transmission.
3786 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3789 "Sending BCK ACK on tunnel %u [%u]\n",
3790 t->id.oid, t->id.tid);
3791 /* Is it after data to_origin retransmission? */
3794 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3796 "ACK due to BCK DATA retransmission\n");
3797 if (GNUNET_YES == t->nobuffer)
3799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3802 if (t->bck_queue_max > t->bck_queue_n * 2)
3804 // GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3808 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3809 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3816 tunnel_send_clients_bck_ack (t);
3817 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, NULL);
3822 * Send a message to all peers in this tunnel that the tunnel is no longer
3825 * @param t The tunnel whose peers to notify.
3828 tunnel_send_destroy (struct MeshTunnel *t)
3830 struct GNUNET_MESH_TunnelDestroy msg;
3832 msg.header.size = htons (sizeof (msg));
3833 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3834 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3835 msg.tid = htonl (t->id.tid);
3836 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
3841 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3843 * @param cls Closure (Tunnel which to cancel).
3844 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3847 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3849 struct MeshTunnel *t = cls;
3850 struct MeshPeerInfo *peer_info;
3851 struct MeshPeerQueue *pq;
3852 struct MeshPeerQueue *next;
3854 peer_info = peer_info_get_short (neighbor_id);
3855 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3858 if (pq->tunnel == t)
3860 queue_destroy (pq, GNUNET_YES);
3863 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3865 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3866 peer_info->core_transmit = NULL;
3871 * Destroy the tunnel and free any allocated resources linked to it.
3873 * @param t the tunnel to destroy
3875 * @return GNUNET_OK on success
3878 tunnel_destroy (struct MeshTunnel *t)
3880 struct MeshClient *c;
3881 struct GNUNET_HashCode hash;
3888 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3894 struct GNUNET_PeerIdentity id;
3896 GNUNET_PEER_resolve (t->id.oid, &id);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3898 GNUNET_i2s (&id), t->id.tid);
3900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3904 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3905 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3910 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3913 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3917 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3918 for (i = 0; i < t->nclients; i++)
3922 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3927 for (i = 0; i < t->nignore; i++)
3931 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3936 if (t->nclients > 0)
3939 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
3943 GNUNET_free (t->clients);
3945 if (NULL != t->peers)
3947 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
3949 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
3952 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3953 &tunnel_destroy_child,
3955 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3957 tree_destroy (t->tree);
3959 if (NULL != t->regex_ctx)
3960 regex_cancel_search (t->regex_ctx);
3961 if (NULL != t->dht_get_type)
3962 GNUNET_DHT_get_stop (t->dht_get_type);
3963 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3964 GNUNET_SCHEDULER_cancel (t->timeout_task);
3965 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3966 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3969 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3970 GNUNET_assert (0 <= n_tunnels);
3977 * Create a new tunnel
3979 * @param owner Who is the owner of the tunnel (short ID).
3980 * @param tid Tunnel Number of the tunnel.
3981 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3982 * @param local Tunnel Number for the tunnel, for the client point of view.
3984 * @return A new initialized tunnel. NULL on error.
3986 static struct MeshTunnel *
3987 tunnel_new (GNUNET_PEER_Id owner,
3988 MESH_TunnelNumber tid,
3989 struct MeshClient *client,
3990 MESH_TunnelNumber local)
3992 struct MeshTunnel *t;
3993 struct GNUNET_HashCode hash;
3995 if (n_tunnels >= max_tunnels && NULL == client)
3998 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4001 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4002 t->bck_queue_max = t->fwd_queue_max;
4003 t->tree = tree_new (owner);
4005 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4006 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4007 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4008 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4009 t->local_tid = local;
4010 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4012 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4014 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4016 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4017 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4024 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4031 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4033 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4034 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4038 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4048 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4049 * that are no longer needed, as well as nodes of no longer reachable peers.
4050 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4052 * @param t Tunnel from which to remove the path.
4053 * @param peer Short id of the peer which should be removed.
4056 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4058 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4064 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4065 * client when the client disconnects. If the client is not the owner, the
4066 * owner will get notified if no more clients are in the tunnel and the client
4067 * get removed from the tunnel's list.
4069 * @param cls closure (client that is disconnecting)
4070 * @param key the hash of the local tunnel id (used to access the hashmap)
4071 * @param value the value stored at the key (tunnel to destroy)
4073 * @return GNUNET_OK on success
4076 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4078 struct MeshTunnel *t = value;
4079 struct MeshClient *c = cls;
4082 send_client_tunnel_disconnect(t, c);
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4086 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4087 tunnel_delete_client(t, c);
4088 client_delete_tunnel(c, t);
4091 tunnel_send_destroy(t);
4092 r = tunnel_destroy (t);
4098 * Timeout function, destroys tunnel if called
4100 * @param cls Closure (tunnel to destroy).
4101 * @param tc TaskContext
4104 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4106 struct MeshTunnel *t = cls;
4108 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4110 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4115 * Resets the tunnel timeout. Starts it if no timeout was running.
4117 * @param t Tunnel whose timeout to reset.
4119 * TODO use heap to improve efficiency of scheduler.
4122 tunnel_reset_timeout (struct MeshTunnel *t)
4124 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4125 GNUNET_SCHEDULER_cancel (t->timeout_task);
4127 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4128 (refresh_path_time, 4), &tunnel_timeout, t);
4132 /******************************************************************************/
4133 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4134 /******************************************************************************/
4137 * Function to send a create path packet to a peer.
4139 * @param cls closure
4140 * @param size number of bytes available in buf
4141 * @param buf where the callee should write the message
4142 * @return number of bytes written to buf
4145 send_core_path_create (void *cls, size_t size, void *buf)
4147 struct MeshPathInfo *info = cls;
4148 struct GNUNET_MESH_ManipulatePath *msg;
4149 struct GNUNET_PeerIdentity *peer_ptr;
4150 struct MeshTunnel *t = info->t;
4151 struct MeshPeerPath *p = info->path;
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4158 sizeof (struct GNUNET_MESH_ManipulatePath) +
4159 p->length * sizeof (struct GNUNET_PeerIdentity);
4161 if (size < size_needed || NULL == buf)
4166 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4167 msg->header.size = htons (size_needed);
4168 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4169 msg->tid = ntohl (t->id.tid);
4171 if (GNUNET_YES == t->speed_min)
4172 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4173 if (GNUNET_YES == t->nobuffer)
4174 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4175 msg->opt = htonl(opt);
4177 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4178 for (i = 0; i < p->length; i++)
4180 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4187 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4193 * Fill the core buffer
4195 * @param cls closure (data itself)
4196 * @param size number of bytes available in buf
4197 * @param buf where the callee should write the message
4199 * @return number of bytes written to buf
4202 send_core_data_multicast (void *cls, size_t size, void *buf)
4204 struct MeshTransmissionDescriptor *info = cls;
4207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4208 GNUNET_assert (NULL != info);
4209 GNUNET_assert (NULL != info->peer);
4210 total_size = info->mesh_data->data_len;
4211 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4213 if (total_size > size)
4218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4219 memcpy (buf, info->mesh_data->data, total_size);
4222 struct GNUNET_MESH_Multicast *mc;
4223 struct GNUNET_MessageHeader *mh;
4226 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4228 mc = (struct GNUNET_MESH_Multicast *) mh;
4229 mh = (struct GNUNET_MessageHeader *) &mc[1];
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4231 " multicast, payload type %s\n",
4232 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4234 " multicast, payload size %u\n", ntohs (mh->size));
4238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4239 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4243 data_descriptor_decrement_rc (info->mesh_data);
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4252 * Creates a path ack message in buf and frees all unused resources.
4254 * @param cls closure (MeshTransmissionDescriptor)
4255 * @param size number of bytes available in buf
4256 * @param buf where the callee should write the message
4257 * @return number of bytes written to buf
4260 send_core_path_ack (void *cls, size_t size, void *buf)
4262 struct MeshTransmissionDescriptor *info = cls;
4263 struct GNUNET_MESH_PathACK *msg = buf;
4265 GNUNET_assert (NULL != info);
4266 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4271 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4272 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4273 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4274 msg->tid = htonl (info->origin->tid);
4275 msg->peer_id = my_full_id;
4278 /* TODO add signature */
4280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4281 return sizeof (struct GNUNET_MESH_PathACK);
4286 * Free a transmission that was already queued with all resources
4287 * associated to the request.
4289 * @param queue Queue handler to cancel.
4290 * @param clear_cls Is it necessary to free associated cls?
4293 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4295 struct MeshTransmissionDescriptor *dd;
4296 struct MeshPathInfo *path_info;
4298 if (GNUNET_YES == clear_cls)
4300 switch (queue->type)
4302 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4303 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4304 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4307 data_descriptor_decrement_rc (dd->mesh_data);
4309 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4311 path_info = queue->cls;
4312 path_destroy (path_info->path);
4316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4318 GNUNET_free_non_null (queue->cls);
4320 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4321 queue->peer->queue_tail,
4323 GNUNET_free (queue);
4328 * Core callback to write a queued packet to core buffer
4330 * @param cls Closure (peer info).
4331 * @param size Number of bytes available in buf.
4332 * @param buf Where the to write the message.
4334 * @return number of bytes written to buf
4337 queue_send (void *cls, size_t size, void *buf)
4339 struct MeshPeerInfo *peer = cls;
4340 struct GNUNET_MessageHeader *msg;
4341 struct MeshPeerQueue *queue;
4342 struct MeshTunnel *t;
4345 peer->core_transmit = NULL;
4346 queue = peer->queue_head;
4348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4350 /* If queue is empty, send should have been cancelled */
4356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4358 /* Check if buffer size is enough for the message */
4359 if (queue->size > size)
4361 struct GNUNET_PeerIdentity id;
4363 GNUNET_PEER_resolve (peer->id, &id);
4364 peer->core_transmit =
4365 GNUNET_CORE_notify_transmit_ready(core_handle,
4368 GNUNET_TIME_UNIT_FOREVER_REL,
4375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4378 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* unicast: %u\n");
4383 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4390 switch (queue->type)
4393 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4394 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4395 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4397 "********* raw: %u\n",
4400 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4401 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4402 data_size = send_core_data_raw (queue->cls, size, buf);
4403 msg = (struct GNUNET_MessageHeader *) buf;
4404 switch (ntohs (msg->type)) // Type of preconstructed message
4406 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4407 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4409 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4410 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4416 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4418 data_size = send_core_data_multicast(queue->cls, size, buf);
4419 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4421 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4423 data_size = send_core_path_create(queue->cls, size, buf);
4425 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4427 data_size = send_core_path_ack(queue->cls, size, buf);
4431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4432 "********* type unknown: %u\n",
4437 /* Free queue, but cls was freed by send_core_* */
4438 queue_destroy (queue, GNUNET_NO);
4440 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4446 /* If more data in queue, send next */
4447 if (NULL != peer->queue_head)
4449 struct GNUNET_PeerIdentity id;
4451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4452 GNUNET_PEER_resolve (peer->id, &id);
4453 peer->core_transmit =
4454 GNUNET_CORE_notify_transmit_ready(core_handle,
4457 GNUNET_TIME_UNIT_FOREVER_REL,
4459 peer->queue_head->size,
4463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4469 * Queue and pass message to core when possible.
4471 * @param cls Closure (type dependant).
4472 * @param type Type of the message, 0 for a raw message.
4473 * @param size Size of the message.
4474 * @param dst Neighbor to send message to.
4475 * @param t Tunnel this message belongs to.
4478 queue_add (void *cls, uint16_t type, size_t size,
4479 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4481 struct MeshPeerQueue *queue;
4486 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4487 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4489 n = &t->fwd_queue_n;
4490 max = &t->fwd_queue_max;
4492 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4494 n = &t->bck_queue_n;
4495 max = &t->bck_queue_max;
4500 if (NULL == t->owner)
4501 GNUNET_break_op(0); // TODO: kill connection?
4504 return; // Drop message
4508 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4514 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4515 if (NULL == dst->core_transmit)
4517 struct GNUNET_PeerIdentity id;
4519 GNUNET_PEER_resolve (dst->id, &id);
4520 dst->core_transmit =
4521 GNUNET_CORE_notify_transmit_ready(core_handle,
4524 GNUNET_TIME_UNIT_FOREVER_REL,
4533 /******************************************************************************/
4534 /******************** MESH NETWORK HANDLERS **************************/
4535 /******************************************************************************/
4539 * Core handler for path creation
4541 * @param cls closure
4542 * @param message message
4543 * @param peer peer identity this notification is about
4544 * @param atsi performance data
4545 * @param atsi_count number of records in 'atsi'
4547 * @return GNUNET_OK to keep the connection open,
4548 * GNUNET_SYSERR to close it (signal serious error)
4551 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4552 const struct GNUNET_MessageHeader *message,
4553 const struct GNUNET_ATS_Information *atsi,
4554 unsigned int atsi_count)
4556 unsigned int own_pos;
4559 MESH_TunnelNumber tid;
4560 struct GNUNET_MESH_ManipulatePath *msg;
4561 struct GNUNET_PeerIdentity *pi;
4562 struct GNUNET_HashCode hash;
4563 struct MeshPeerPath *path;
4564 struct MeshPeerInfo *dest_peer_info;
4565 struct MeshPeerInfo *orig_peer_info;
4566 struct MeshTunnel *t;
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4569 "Received a path create msg [%s]\n",
4570 GNUNET_i2s (&my_full_id));
4571 size = ntohs (message->size);
4572 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4574 GNUNET_break_op (0);
4578 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4579 if (size % sizeof (struct GNUNET_PeerIdentity))
4581 GNUNET_break_op (0);
4584 size /= sizeof (struct GNUNET_PeerIdentity);
4587 GNUNET_break_op (0);
4590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4591 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4593 tid = ntohl (msg->tid);
4594 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4596 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4597 t = tunnel_get (pi, tid);
4598 if (NULL == t) // FIXME only for INCOMING tunnels?
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4603 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4606 // FIXME notify failure
4609 opt = ntohl (msg->opt);
4610 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4611 GNUNET_YES : GNUNET_NO;
4612 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4613 GNUNET_YES : GNUNET_NO;
4615 if (GNUNET_YES == t->nobuffer)
4617 t->bck_queue_max = 1;
4618 t->fwd_queue_max = 1;
4621 // FIXME only assign a local tid if a local client is interested (on demand)
4622 while (NULL != tunnel_get_incoming (next_local_tid))
4623 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4624 t->local_tid_dest = next_local_tid++;
4625 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4628 tunnel_reset_timeout (t);
4629 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4631 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4632 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4640 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4641 if (NULL == dest_peer_info)
4643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4644 " Creating PeerInfo for destination.\n");
4645 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4646 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4647 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4649 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4651 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4652 if (NULL == orig_peer_info)
4654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4655 " Creating PeerInfo for origin.\n");
4656 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4657 orig_peer_info->id = GNUNET_PEER_intern (pi);
4658 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4659 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4662 path = path_new (size);
4664 for (i = 0; i < size; i++)
4666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4667 GNUNET_i2s (&pi[i]));
4668 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4669 if (path->peers[i] == myid)
4672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4675 /* cannot be self, must be 'not found' */
4676 /* create path: self not found in path through self */
4677 GNUNET_break_op (0);
4678 path_destroy (path);
4682 path_add_to_peers (path, GNUNET_NO);
4683 tunnel_add_path (t, path, own_pos);
4684 if (own_pos == size - 1)
4686 /* It is for us! Send ack. */
4687 struct MeshTransmissionDescriptor *info;
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4690 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4691 if (NULL == t->peers)
4693 /* New tunnel! Notify clients on data. */
4694 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4696 GNUNET_break (GNUNET_SYSERR !=
4697 GNUNET_CONTAINER_multihashmap_put (t->peers,
4698 &my_full_id.hashPubKey,
4701 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
4702 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
4703 info->origin = &t->id;
4704 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
4705 GNUNET_assert (NULL != info->peer);
4707 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
4708 sizeof (struct GNUNET_MESH_PathACK),
4714 struct MeshPeerPath *path2;
4716 /* It's for somebody else! Retransmit. */
4717 path2 = path_duplicate (path);
4718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4719 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4720 path2 = path_duplicate (path);
4721 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4722 send_create_path (dest_peer_info, path, t);
4729 * Core handler for path destruction
4731 * @param cls closure
4732 * @param message message
4733 * @param peer peer identity this notification is about
4734 * @param atsi performance data
4735 * @param atsi_count number of records in 'atsi'
4737 * @return GNUNET_OK to keep the connection open,
4738 * GNUNET_SYSERR to close it (signal serious error)
4741 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4742 const struct GNUNET_MessageHeader *message,
4743 const struct GNUNET_ATS_Information *atsi,
4744 unsigned int atsi_count)
4746 struct GNUNET_MESH_ManipulatePath *msg;
4747 struct GNUNET_PeerIdentity *pi;
4748 struct MeshPeerPath *path;
4749 struct MeshTunnel *t;
4750 unsigned int own_pos;
4754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4755 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4756 size = ntohs (message->size);
4757 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4759 GNUNET_break_op (0);
4763 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4764 if (size % sizeof (struct GNUNET_PeerIdentity))
4766 GNUNET_break_op (0);
4769 size /= sizeof (struct GNUNET_PeerIdentity);
4772 GNUNET_break_op (0);
4775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4777 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4778 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4780 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4782 t = tunnel_get (pi, ntohl (msg->tid));
4785 /* TODO notify back: we don't know this tunnel */
4786 GNUNET_break_op (0);
4789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4790 path = path_new (size);
4792 for (i = 0; i < size; i++)
4794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4795 GNUNET_i2s (&pi[i]));
4796 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4797 if (path->peers[i] == myid)
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4801 if (own_pos < path->length - 1)
4802 send_message (message, &pi[own_pos + 1], t);
4804 send_client_tunnel_disconnect(t, NULL);
4806 tunnel_delete_peer (t, path->peers[path->length - 1]);
4807 path_destroy (path);
4813 * Core handler for notifications of broken paths
4815 * @param cls closure
4816 * @param message message
4817 * @param peer peer identity this notification is about
4818 * @param atsi performance data
4819 * @param atsi_count number of records in 'atsi'
4821 * @return GNUNET_OK to keep the connection open,
4822 * GNUNET_SYSERR to close it (signal serious error)
4825 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4826 const struct GNUNET_MessageHeader *message,
4827 const struct GNUNET_ATS_Information *atsi,
4828 unsigned int atsi_count)
4830 struct GNUNET_MESH_PathBroken *msg;
4831 struct MeshTunnel *t;
4833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4834 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4835 msg = (struct GNUNET_MESH_PathBroken *) message;
4836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4837 GNUNET_i2s (&msg->peer1));
4838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4839 GNUNET_i2s (&msg->peer2));
4840 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4843 GNUNET_break_op (0);
4846 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4847 GNUNET_PEER_search (&msg->peer2));
4854 * Core handler for tunnel destruction
4856 * @param cls closure
4857 * @param message message
4858 * @param peer peer identity this notification is about
4859 * @param atsi performance data
4860 * @param atsi_count number of records in 'atsi'
4862 * @return GNUNET_OK to keep the connection open,
4863 * GNUNET_SYSERR to close it (signal serious error)
4866 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4867 const struct GNUNET_MessageHeader *message,
4868 const struct GNUNET_ATS_Information *atsi,
4869 unsigned int atsi_count)
4871 struct GNUNET_MESH_TunnelDestroy *msg;
4872 struct MeshTunnel *t;
4874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4875 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
4876 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
4878 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4879 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4882 /* Probably already got the message from another path,
4883 * destroyed the tunnel and retransmitted to children.
4886 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
4889 if (t->id.oid == myid)
4891 GNUNET_break_op (0);
4894 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4896 /* Tunnel was incoming, notify clients */
4897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4898 t->local_tid, t->local_tid_dest);
4899 send_clients_tunnel_destroy (t);
4901 tunnel_send_destroy (t);
4902 t->destroy = GNUNET_YES;
4903 // TODO: add timeout to destroy the tunnel anyway
4909 * Core handler for mesh network traffic going from the origin to a peer
4911 * @param cls closure
4912 * @param peer peer identity this notification is about
4913 * @param message message
4914 * @param atsi performance data
4915 * @param atsi_count number of records in 'atsi'
4916 * @return GNUNET_OK to keep the connection open,
4917 * GNUNET_SYSERR to close it (signal serious error)
4920 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4921 const struct GNUNET_MessageHeader *message,
4922 const struct GNUNET_ATS_Information *atsi,
4923 unsigned int atsi_count)
4925 struct GNUNET_MESH_Unicast *msg;
4926 struct GNUNET_PeerIdentity *neighbor;
4927 struct MeshTunnelChildInfo *cinfo;
4928 struct MeshTunnel *t;
4929 GNUNET_PEER_Id dest_id;
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4937 size = ntohs (message->size);
4939 sizeof (struct GNUNET_MESH_Unicast) +
4940 sizeof (struct GNUNET_MessageHeader))
4945 msg = (struct GNUNET_MESH_Unicast *) message;
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4947 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4949 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4952 /* TODO notify back: we don't know this tunnel */
4953 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4954 GNUNET_break_op (0);
4957 pid = ntohl (msg->pid);
4958 if (t->fwd_pid == pid)
4960 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4961 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4962 " Already seen pid %u, DROPPING!\n", pid);
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4968 " pid %u not seen yet, forwarding\n", pid);
4970 t->skip += (pid - t->fwd_pid) - 1;
4972 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4974 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4975 GNUNET_break_op (0);
4978 tunnel_reset_timeout (t);
4979 dest_id = GNUNET_PEER_search (&msg->destination);
4980 if (dest_id == myid)
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4983 " it's for us! sending to clients...\n");
4984 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4985 send_subscribed_clients (message, (struct GNUNET_MessageHeader *) &msg[1]);
4986 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4989 ttl = ntohl (msg->ttl);
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4993 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4994 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4995 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4999 " not for us, retransmitting...\n");
5001 neighbor = tree_get_first_hop (t->tree, dest_id);
5002 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5004 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5007 if (GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5009 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5010 GNUNET_break_op (0);
5013 send_message (message, neighbor, t);
5014 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5021 * Core handler for mesh network traffic going from the origin to all peers
5023 * @param cls closure
5024 * @param message message
5025 * @param peer peer identity this notification is about
5026 * @param atsi performance data
5027 * @param atsi_count number of records in 'atsi'
5028 * @return GNUNET_OK to keep the connection open,
5029 * GNUNET_SYSERR to close it (signal serious error)
5031 * TODO: Check who we got this from, to validate route.
5034 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5035 const struct GNUNET_MessageHeader *message,
5036 const struct GNUNET_ATS_Information *atsi,
5037 unsigned int atsi_count)
5039 struct GNUNET_MESH_Multicast *msg;
5040 struct MeshTunnel *t;
5044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5046 size = ntohs (message->size);
5047 if (sizeof (struct GNUNET_MESH_Multicast) +
5048 sizeof (struct GNUNET_MessageHeader) > size)
5050 GNUNET_break_op (0);
5053 msg = (struct GNUNET_MESH_Multicast *) message;
5054 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5058 /* TODO notify that we dont know that tunnel */
5059 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5060 GNUNET_break_op (0);
5063 pid = ntohl (msg->pid);
5064 if (t->fwd_pid == pid)
5066 /* already seen this packet, drop */
5067 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5069 " Already seen pid %u, DROPPING!\n", pid);
5070 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5076 " pid %u not seen yet, forwarding\n", pid);
5078 t->skip += (pid - t->fwd_pid) - 1;
5080 tunnel_reset_timeout (t);
5082 /* Transmit to locally interested clients */
5083 if (NULL != t->peers &&
5084 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5086 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5087 send_subscribed_clients (message, &msg[1].header);
5088 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5091 if (ntohl (msg->ttl) == 0)
5093 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5094 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5095 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5098 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5099 tunnel_send_multicast (t, message, GNUNET_NO);
5105 * Core handler for mesh network traffic toward the owner of a tunnel
5107 * @param cls closure
5108 * @param message message
5109 * @param peer peer identity this notification is about
5110 * @param atsi performance data
5111 * @param atsi_count number of records in 'atsi'
5113 * @return GNUNET_OK to keep the connection open,
5114 * GNUNET_SYSERR to close it (signal serious error)
5117 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5118 const struct GNUNET_MessageHeader *message,
5119 const struct GNUNET_ATS_Information *atsi,
5120 unsigned int atsi_count)
5122 struct GNUNET_MESH_ToOrigin *msg;
5123 struct GNUNET_PeerIdentity id;
5124 struct MeshPeerInfo *peer_info;
5125 struct MeshTunnel *t;
5128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5130 size = ntohs (message->size);
5131 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5132 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5134 GNUNET_break_op (0);
5137 msg = (struct GNUNET_MESH_ToOrigin *) message;
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5139 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5140 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5144 /* TODO notify that we dont know this tunnel (whom)? */
5145 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5146 GNUNET_break_op (0);
5150 if (NULL != t->owner)
5153 struct GNUNET_MESH_ToOrigin *copy;
5155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5156 " it's for us! sending to clients...\n");
5157 /* TODO signature verification */
5158 memcpy (cbuf, message, size);
5159 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5160 copy->tid = htonl (t->local_tid);
5161 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5162 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5163 ©->header, GNUNET_NO);
5164 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5168 " not for us, retransmitting...\n");
5170 peer_info = peer_info_get (&msg->oid);
5171 if (NULL == peer_info)
5173 /* unknown origin of tunnel */
5177 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5178 send_message (message, &id, t);
5179 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5186 * Core handler for mesh network traffic point-to-point acks.
5188 * @param cls closure
5189 * @param message message
5190 * @param peer peer identity this notification is about
5191 * @param atsi performance data
5192 * @param atsi_count number of records in 'atsi'
5194 * @return GNUNET_OK to keep the connection open,
5195 * GNUNET_SYSERR to close it (signal serious error)
5198 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5199 const struct GNUNET_MessageHeader *message,
5200 const struct GNUNET_ATS_Information *atsi,
5201 unsigned int atsi_count)
5203 struct GNUNET_MESH_ACK *msg;
5204 struct MeshTunnel *t;
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5209 msg = (struct GNUNET_MESH_ACK *) message;
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5211 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5212 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5216 /* TODO notify that we dont know this tunnel (whom)? */
5217 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5218 GNUNET_break_op (0);
5221 ack = ntohl (msg->pid);
5223 /* Is this a forward or backward ACK? */
5224 if (tree_get_predecessor(t->tree) == GNUNET_PEER_search(peer))
5226 struct MeshTunnelChildInfo *cinfo;
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5229 cinfo = tunnel_get_neighbor_fc (t, peer);
5230 cinfo->fwd_ack = ack;
5231 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5237 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5239 // FIXME fc Unlock queues?
5245 * Core handler for path ACKs
5247 * @param cls closure
5248 * @param message message
5249 * @param peer peer identity this notification is about
5250 * @param atsi performance data
5251 * @param atsi_count number of records in 'atsi'
5253 * @return GNUNET_OK to keep the connection open,
5254 * GNUNET_SYSERR to close it (signal serious error)
5257 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5258 const struct GNUNET_MessageHeader *message,
5259 const struct GNUNET_ATS_Information *atsi,
5260 unsigned int atsi_count)
5262 struct GNUNET_MESH_PathACK *msg;
5263 struct GNUNET_PeerIdentity id;
5264 struct MeshPeerInfo *peer_info;
5265 struct MeshPeerPath *p;
5266 struct MeshTunnel *t;
5268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5269 GNUNET_i2s (&my_full_id));
5270 msg = (struct GNUNET_MESH_PathACK *) message;
5271 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5274 /* TODO notify that we don't know the tunnel */
5275 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5277 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5281 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5283 peer_info = peer_info_get (&msg->peer_id);
5284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5285 GNUNET_i2s (&msg->peer_id));
5286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5289 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5292 "connect_by_string completed, stopping search\n");
5293 regex_cancel_search (t->regex_ctx);
5294 t->regex_ctx = NULL;
5297 /* Add paths to peers? */
5298 p = tree_get_path_to_peer (t->tree, peer_info->id);
5301 path_add_to_peers (p, GNUNET_YES);
5309 /* Message for us? */
5310 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5313 if (NULL == t->owner)
5315 GNUNET_break_op (0);
5318 if (NULL != t->dht_get_type)
5320 GNUNET_DHT_get_stop (t->dht_get_type);
5321 t->dht_get_type = NULL;
5323 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5325 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5326 send_client_peer_connected (t, peer_info->id);
5331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5332 " not for us, retransmitting...\n");
5333 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5334 peer_info = peer_info_get (&msg->oid);
5335 if (NULL == peer_info)
5337 /* If we know the tunnel, we should DEFINITELY know the peer */
5341 send_message (message, &id, t);
5347 * Functions to handle messages from core
5349 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5350 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5351 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5352 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5353 sizeof (struct GNUNET_MESH_PathBroken)},
5354 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5355 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5356 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5357 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5358 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5359 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5360 sizeof (struct GNUNET_MESH_ACK)},
5361 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5362 sizeof (struct GNUNET_MESH_PathACK)},
5368 /******************************************************************************/
5369 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5370 /******************************************************************************/
5373 * deregister_app: iterator for removing each application registered by a client
5375 * @param cls closure
5376 * @param key the hash of the application id (used to access the hashmap)
5377 * @param value the value stored at the key (client)
5379 * @return GNUNET_OK on success
5382 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5384 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5385 GNUNET_break (GNUNET_YES ==
5386 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5392 * notify_client_connection_failure: notify a client that the connection to the
5393 * requested remote peer is not possible (for instance, no route found)
5394 * Function called when the socket is ready to queue more data. "buf" will be
5395 * NULL and "size" zero if the socket was closed for writing in the meantime.
5397 * @param cls closure
5398 * @param size number of bytes available in buf
5399 * @param buf where the callee should write the message
5400 * @return number of bytes written to buf
5403 notify_client_connection_failure (void *cls, size_t size, void *buf)
5406 struct MeshPeerInfo *peer_info;
5407 struct GNUNET_MESH_PeerControl *msg;
5408 struct GNUNET_PeerIdentity id;
5410 if (0 == size && NULL == buf)
5412 // TODO retry? cancel?
5416 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5417 peer_info = (struct MeshPeerInfo *) cls;
5418 msg = (struct GNUNET_MESH_PeerControl *) buf;
5419 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5420 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5421 // msg->tunnel_id = htonl(peer_info->t->tid);
5422 GNUNET_PEER_resolve (peer_info->id, &id);
5423 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5431 * Send keepalive packets for a peer
5433 * @param cls Closure (tunnel for which to send the keepalive).
5434 * @param tc Notification context.
5436 * TODO: implement explicit multicast keepalive?
5439 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5441 struct MeshTunnel *t = cls;
5442 struct GNUNET_MessageHeader *payload;
5443 struct GNUNET_MESH_Multicast *msg;
5445 sizeof (struct GNUNET_MESH_Multicast) +
5446 sizeof (struct GNUNET_MessageHeader);
5449 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5453 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5456 "sending keepalive for tunnel %d\n", t->id.tid);
5458 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5459 msg->header.size = htons (size);
5460 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5461 msg->oid = my_full_id;
5462 msg->tid = htonl (t->id.tid);
5463 msg->ttl = htonl (default_ttl);
5464 msg->pid = htonl (t->fwd_pid + 1);
5466 payload = (struct GNUNET_MessageHeader *) &msg[1];
5467 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5468 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5469 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5471 t->path_refresh_task =
5472 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5478 * Function to process paths received for a new peer addition. The recorded
5479 * paths form the initial tunnel, which can be optimized later.
5480 * Called on each result obtained for the DHT search.
5482 * @param cls closure
5483 * @param exp when will this value expire
5484 * @param key key of the result
5485 * @param get_path path of the get request
5486 * @param get_path_length lenght of get_path
5487 * @param put_path path of the put request
5488 * @param put_path_length length of the put_path
5489 * @param type type of the result
5490 * @param size number of bytes in data
5491 * @param data pointer to the result data
5493 * TODO: re-issue the request after certain time? cancel after X results?
5496 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5497 const struct GNUNET_HashCode * key,
5498 const struct GNUNET_PeerIdentity *get_path,
5499 unsigned int get_path_length,
5500 const struct GNUNET_PeerIdentity *put_path,
5501 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5502 size_t size, const void *data)
5504 struct MeshPathInfo *path_info = cls;
5505 struct MeshPeerPath *p;
5506 struct GNUNET_PeerIdentity pi;
5509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5510 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5513 p = path_build_from_dht (get_path, get_path_length, put_path,
5515 path_add_to_peers (p, GNUNET_NO);
5517 for (i = 0; i < path_info->peer->ntunnels; i++)
5519 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5520 peer_info_connect (path_info->peer, path_info->t);
5528 * Function to process paths received for a new peer addition. The recorded
5529 * paths form the initial tunnel, which can be optimized later.
5530 * Called on each result obtained for the DHT search.
5532 * @param cls closure
5533 * @param exp when will this value expire
5534 * @param key key of the result
5535 * @param get_path path of the get request
5536 * @param get_path_length lenght of get_path
5537 * @param put_path path of the put request
5538 * @param put_path_length length of the put_path
5539 * @param type type of the result
5540 * @param size number of bytes in data
5541 * @param data pointer to the result data
5544 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5545 const struct GNUNET_HashCode * key,
5546 const struct GNUNET_PeerIdentity *get_path,
5547 unsigned int get_path_length,
5548 const struct GNUNET_PeerIdentity *put_path,
5549 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5550 size_t size, const void *data)
5552 const struct PBlock *pb = data;
5553 const struct GNUNET_PeerIdentity *pi = &pb->id;
5554 struct MeshTunnel *t = cls;
5555 struct MeshPeerInfo *peer_info;
5556 struct MeshPeerPath *p;
5558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5559 if (size != sizeof (struct PBlock))
5561 GNUNET_break_op (0);
5564 if (ntohl(pb->type) != t->type)
5566 GNUNET_break_op (0);
5569 GNUNET_assert (NULL != t->owner);
5570 peer_info = peer_info_get (pi);
5571 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5573 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5575 p = path_build_from_dht (get_path, get_path_length, put_path,
5577 path_add_to_peers (p, GNUNET_NO);
5579 tunnel_add_peer (t, peer_info);
5580 peer_info_connect (peer_info, t);
5585 * Function to process DHT string to regex matching.
5586 * Called on each result obtained for the DHT search.
5588 * @param cls closure (search context)
5589 * @param exp when will this value expire
5590 * @param key key of the result
5591 * @param get_path path of the get request (not used)
5592 * @param get_path_length lenght of get_path (not used)
5593 * @param put_path path of the put request (not used)
5594 * @param put_path_length length of the put_path (not used)
5595 * @param type type of the result
5596 * @param size number of bytes in data
5597 * @param data pointer to the result data
5600 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5601 const struct GNUNET_HashCode * key,
5602 const struct GNUNET_PeerIdentity *get_path,
5603 unsigned int get_path_length,
5604 const struct GNUNET_PeerIdentity *put_path,
5605 unsigned int put_path_length,
5606 enum GNUNET_BLOCK_Type type,
5607 size_t size, const void *data)
5609 const struct MeshRegexAccept *block = data;
5610 struct MeshRegexSearchContext *ctx = cls;
5611 struct MeshRegexSearchInfo *info = ctx->info;
5612 struct MeshPeerPath *p;
5613 struct MeshPeerInfo *peer_info;
5615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5618 peer_info = peer_info_get(&block->id);
5619 p = path_build_from_dht (get_path, get_path_length, put_path,
5621 path_add_to_peers (p, GNUNET_NO);
5624 tunnel_add_peer (info->t, peer_info);
5625 peer_info_connect (peer_info, info->t);
5626 if (0 == info->peer)
5628 info->peer = peer_info->id;
5632 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5635 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5636 ®ex_connect_timeout,
5644 * Function to process DHT string to regex matching.
5645 * Called on each result obtained for the DHT search.
5647 * @param cls closure (search context)
5648 * @param exp when will this value expire
5649 * @param key key of the result
5650 * @param get_path path of the get request (not used)
5651 * @param get_path_length lenght of get_path (not used)
5652 * @param put_path path of the put request (not used)
5653 * @param put_path_length length of the put_path (not used)
5654 * @param type type of the result
5655 * @param size number of bytes in data
5656 * @param data pointer to the result data
5658 * TODO: re-issue the request after certain time? cancel after X results?
5661 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5662 const struct GNUNET_HashCode * key,
5663 const struct GNUNET_PeerIdentity *get_path,
5664 unsigned int get_path_length,
5665 const struct GNUNET_PeerIdentity *put_path,
5666 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5667 size_t size, const void *data)
5669 const struct MeshRegexBlock *block = data;
5670 struct MeshRegexSearchContext *ctx = cls;
5671 struct MeshRegexSearchInfo *info = ctx->info;
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5676 "DHT GET STRING RETURNED RESULTS\n");
5677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5678 " key: %s\n", GNUNET_h2s (key));
5680 copy = GNUNET_malloc (size);
5681 memcpy (copy, data, size);
5682 GNUNET_break (GNUNET_OK ==
5683 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5685 len = ntohl (block->n_proof);
5687 char proof[len + 1];
5689 memcpy (proof, &block[1], len);
5691 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5693 GNUNET_break_op (0);
5697 len = strlen (info->description);
5698 if (len == ctx->position) // String processed
5700 if (GNUNET_YES == ntohl (block->accepting))
5702 regex_find_path(key, ctx);
5706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
5707 // FIXME REGEX this block not successful, wait for more? start timeout?
5711 GNUNET_break (GNUNET_OK ==
5712 GNUNET_MESH_regex_block_iterate (block, size,
5713 ®ex_edge_iterator, ctx));
5717 /******************************************************************************/
5718 /********************* MESH LOCAL HANDLES **************************/
5719 /******************************************************************************/
5723 * Handler for client disconnection
5725 * @param cls closure
5726 * @param client identification of the client; NULL
5727 * for the last call when the server is destroyed
5730 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5732 struct MeshClient *c;
5733 struct MeshClient *next;
5736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5745 if (c->handle != client)
5747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5753 GNUNET_SERVER_client_drop (c->handle);
5754 c->shutting_down = GNUNET_YES;
5755 GNUNET_assert (NULL != c->own_tunnels);
5756 GNUNET_assert (NULL != c->incoming_tunnels);
5757 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5758 &tunnel_destroy_iterator, c);
5759 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5760 &tunnel_destroy_iterator, c);
5761 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5762 &tunnel_destroy_iterator, c);
5763 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5764 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5765 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5767 /* deregister clients applications */
5768 if (NULL != c->apps)
5770 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
5771 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
5773 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
5774 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
5776 GNUNET_SCHEDULER_cancel (announce_applications_task);
5777 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
5779 if (NULL != c->types)
5780 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5781 for (i = 0; i < c->n_regex; i++)
5783 GNUNET_free (c->regexes[i]);
5785 GNUNET_free_non_null (c->regexes);
5786 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5787 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5789 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5792 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5801 * Handler for new clients
5803 * @param cls closure
5804 * @param client identification of the client
5805 * @param message the actual message, which includes messages the client wants
5808 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5809 const struct GNUNET_MessageHeader *message)
5811 struct GNUNET_MESH_ClientConnect *cc_msg;
5812 struct MeshClient *c;
5813 GNUNET_MESH_ApplicationType *a;
5820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5821 /* Check data sanity */
5822 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5823 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5824 ntypes = ntohs (cc_msg->types);
5825 napps = ntohs (cc_msg->applications);
5827 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
5830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5834 /* Create new client structure */
5835 c = GNUNET_malloc (sizeof (struct MeshClient));
5836 c->id = next_client_id++;
5837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5839 GNUNET_SERVER_client_keep (client);
5840 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
5843 GNUNET_MESH_ApplicationType at;
5844 struct GNUNET_HashCode hc;
5846 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
5847 for (i = 0; i < napps; i++)
5850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
5851 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
5852 /* store in clients hashmap */
5853 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
5854 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5855 /* store in global hashmap, for announcements */
5856 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
5857 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5859 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
5860 announce_applications_task =
5861 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
5867 struct GNUNET_HashCode hc;
5869 t = (uint16_t *) & a[napps];
5870 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
5871 for (i = 0; i < ntypes; i++)
5874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5875 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5877 /* store in clients hashmap */
5878 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5880 /* store in global hashmap */
5881 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5882 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5886 " client has %u+%u subscriptions\n", napps, ntypes);
5888 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5889 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5890 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5891 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
5892 GNUNET_SERVER_notification_context_add (nc, client);
5893 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5895 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5901 * Handler for clients announcing available services by a regular expression.
5903 * @param cls closure
5904 * @param client identification of the client
5905 * @param message the actual message, which includes messages the client wants
5908 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
5909 const struct GNUNET_MessageHeader *message)
5911 struct MeshClient *c;
5915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
5917 /* Sanity check for client registration */
5918 if (NULL == (c = client_get (client)))
5921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5926 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
5927 regex = GNUNET_malloc (len + 1);
5928 memcpy (regex, &message[1], len);
5930 GNUNET_array_append (c->regexes, c->n_regex, regex);
5931 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
5933 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
5939 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
5945 * Handler for requests of new tunnels
5947 * @param cls closure
5948 * @param client identification of the client
5949 * @param message the actual message
5952 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5953 const struct GNUNET_MessageHeader *message)
5955 struct GNUNET_MESH_TunnelMessage *t_msg;
5956 struct MeshTunnel *t;
5957 struct MeshClient *c;
5958 MESH_TunnelNumber tid;
5960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5962 /* Sanity check for client registration */
5963 if (NULL == (c = client_get (client)))
5966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5971 /* Message sanity check */
5972 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5979 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5980 /* Sanity check for tunnel numbering */
5981 tid = ntohl (t_msg->tunnel_id);
5982 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5985 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5988 /* Sanity check for duplicate tunnel IDs */
5989 if (NULL != tunnel_get_by_local_id (c, tid))
5992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5996 while (NULL != tunnel_get_by_pi (myid, next_tid))
5997 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5998 t = tunnel_new (myid, next_tid++, c, tid);
6001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6006 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6008 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6009 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6012 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6018 * Handler for requests of deleting tunnels
6020 * @param cls closure
6021 * @param client identification of the client
6022 * @param message the actual message
6025 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6026 const struct GNUNET_MessageHeader *message)
6028 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6029 struct MeshClient *c;
6030 struct MeshTunnel *t;
6031 MESH_TunnelNumber tid;
6033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6034 "Got a DESTROY TUNNEL from client!\n");
6036 /* Sanity check for client registration */
6037 if (NULL == (c = client_get (client)))
6040 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6045 /* Message sanity check */
6046 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6053 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6055 /* Retrieve tunnel */
6056 tid = ntohl (tunnel_msg->tunnel_id);
6057 t = tunnel_get_by_local_id(c, tid);
6060 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6062 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6065 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6067 client_ignore_tunnel (c, t);
6069 // TODO: when to destroy incoming tunnel?
6070 if (t->nclients == 0)
6072 GNUNET_assert (GNUNET_YES ==
6073 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6075 GNUNET_assert (GNUNET_YES ==
6076 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6077 &my_full_id.hashPubKey,
6081 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6084 send_client_tunnel_disconnect(t, c);
6085 client_delete_tunnel(c, t);
6087 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6089 tunnel_send_destroy (t);
6090 t->destroy = GNUNET_YES;
6091 // The tunnel will be destroyed when the last message is transmitted.
6092 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6098 * Handler for requests of seeting tunnel's speed.
6100 * @param cls Closure (unused).
6101 * @param client Identification of the client.
6102 * @param message The actual message.
6105 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6106 const struct GNUNET_MessageHeader *message)
6108 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6109 struct MeshClient *c;
6110 struct MeshTunnel *t;
6111 MESH_TunnelNumber tid;
6113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6114 "Got a SPEED request from client!\n");
6116 /* Sanity check for client registration */
6117 if (NULL == (c = client_get (client)))
6120 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6126 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6128 /* Retrieve tunnel */
6129 tid = ntohl (tunnel_msg->tunnel_id);
6130 t = tunnel_get_by_local_id(c, tid);
6133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6135 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6139 switch (ntohs(message->type))
6141 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6142 t->speed_min = GNUNET_YES;
6144 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6145 t->speed_min = GNUNET_NO;
6150 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6155 * Handler for requests of seeting tunnel's buffering policy.
6157 * @param cls Closure (unused).
6158 * @param client Identification of the client.
6159 * @param message The actual message.
6162 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6163 const struct GNUNET_MessageHeader *message)
6165 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6166 struct MeshClient *c;
6167 struct MeshTunnel *t;
6168 MESH_TunnelNumber tid;
6170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6171 "Got a BUFFER request from client!\n");
6173 /* Sanity check for client registration */
6174 if (NULL == (c = client_get (client)))
6177 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6182 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6184 /* Retrieve tunnel */
6185 tid = ntohl (tunnel_msg->tunnel_id);
6186 t = tunnel_get_by_local_id(c, tid);
6189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6195 switch (ntohs(message->type))
6197 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6198 t->nobuffer = GNUNET_NO;
6200 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6201 t->nobuffer = GNUNET_YES;
6207 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6212 * Handler for connection requests to new peers
6214 * @param cls closure
6215 * @param client identification of the client
6216 * @param message the actual message (PeerControl)
6219 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6220 const struct GNUNET_MessageHeader *message)
6222 struct GNUNET_MESH_PeerControl *peer_msg;
6223 struct MeshPeerInfo *peer_info;
6224 struct MeshClient *c;
6225 struct MeshTunnel *t;
6226 MESH_TunnelNumber tid;
6228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6229 /* Sanity check for client registration */
6230 if (NULL == (c = client_get (client)))
6233 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6238 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6240 /* Sanity check for message size */
6241 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6244 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6248 /* Tunnel exists? */
6249 tid = ntohl (peer_msg->tunnel_id);
6250 t = tunnel_get_by_local_id (c, tid);
6254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6258 /* Does client own tunnel? */
6259 if (t->owner->handle != client)
6262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6266 GNUNET_i2s (&peer_msg->peer));
6267 peer_info = peer_info_get (&peer_msg->peer);
6269 tunnel_add_peer (t, peer_info);
6270 peer_info_connect (peer_info, t);
6272 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6278 * Handler for disconnection requests of peers in a tunnel
6280 * @param cls closure
6281 * @param client identification of the client
6282 * @param message the actual message (PeerControl)
6285 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6286 const struct GNUNET_MessageHeader *message)
6288 struct GNUNET_MESH_PeerControl *peer_msg;
6289 struct MeshPeerInfo *peer_info;
6290 struct MeshClient *c;
6291 struct MeshTunnel *t;
6292 MESH_TunnelNumber tid;
6294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6295 /* Sanity check for client registration */
6296 if (NULL == (c = client_get (client)))
6299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6304 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6306 /* Sanity check for message size */
6307 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6314 /* Tunnel exists? */
6315 tid = ntohl (peer_msg->tunnel_id);
6316 t = tunnel_get_by_local_id (c, tid);
6320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6325 /* Does client own tunnel? */
6326 if (t->owner->handle != client)
6329 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6334 GNUNET_i2s (&peer_msg->peer));
6335 /* Is the peer in the tunnel? */
6337 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6338 if (NULL == peer_info)
6341 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6345 /* Ok, delete peer from tunnel */
6346 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6347 &peer_msg->peer.hashPubKey);
6349 send_destroy_path (t, peer_info->id);
6350 tunnel_delete_peer (t, peer_info->id);
6351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6356 * Handler for blacklist requests of peers in a tunnel
6358 * @param cls closure
6359 * @param client identification of the client
6360 * @param message the actual message (PeerControl)
6362 * FIXME implement DHT block bloomfilter
6365 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6366 const struct GNUNET_MessageHeader *message)
6368 struct GNUNET_MESH_PeerControl *peer_msg;
6369 struct MeshClient *c;
6370 struct MeshTunnel *t;
6371 MESH_TunnelNumber tid;
6373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6374 /* Sanity check for client registration */
6375 if (NULL == (c = client_get (client)))
6378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6383 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6385 /* Sanity check for message size */
6386 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6393 /* Tunnel exists? */
6394 tid = ntohl (peer_msg->tunnel_id);
6395 t = tunnel_get_by_local_id (c, tid);
6399 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6404 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6405 GNUNET_PEER_intern(&peer_msg->peer));
6410 * Handler for unblacklist requests of peers in a tunnel
6412 * @param cls closure
6413 * @param client identification of the client
6414 * @param message the actual message (PeerControl)
6417 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6418 const struct GNUNET_MessageHeader *message)
6420 struct GNUNET_MESH_PeerControl *peer_msg;
6421 struct MeshClient *c;
6422 struct MeshTunnel *t;
6423 MESH_TunnelNumber tid;
6427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6428 /* Sanity check for client registration */
6429 if (NULL == (c = client_get (client)))
6432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6437 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6439 /* Sanity check for message size */
6440 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6447 /* Tunnel exists? */
6448 tid = ntohl (peer_msg->tunnel_id);
6449 t = tunnel_get_by_local_id (c, tid);
6453 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6458 /* if peer is not known, complain */
6459 pid = GNUNET_PEER_search (&peer_msg->peer);
6466 /* search and remove from list */
6467 for (i = 0; i < t->nblacklisted; i++)
6469 if (t->blacklisted[i] == pid)
6471 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6472 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6477 /* if peer hasn't been blacklisted, complain */
6483 * Handler for connection requests to new peers by type
6485 * @param cls closure
6486 * @param client identification of the client
6487 * @param message the actual message (ConnectPeerByType)
6490 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6491 const struct GNUNET_MessageHeader *message)
6493 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6494 struct MeshClient *c;
6495 struct MeshTunnel *t;
6496 struct GNUNET_HashCode hash;
6497 MESH_TunnelNumber tid;
6499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6500 /* Sanity check for client registration */
6501 if (NULL == (c = client_get (client)))
6504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6509 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6511 /* Sanity check for message size */
6512 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6513 ntohs (connect_msg->header.size))
6516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6520 /* Tunnel exists? */
6521 tid = ntohl (connect_msg->tunnel_id);
6522 t = tunnel_get_by_local_id (c, tid);
6526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6530 /* Does client own tunnel? */
6531 if (t->owner->handle != client)
6534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6538 /* Do WE have the service? */
6539 t->type = ntohl (connect_msg->type);
6540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6541 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6542 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6545 /* Yes! Fast forward, add ourselves to the tunnel and send the
6546 * good news to the client, and alert the destination client of
6547 * an incoming tunnel.
6549 * FIXME send a path create to self, avoid code duplication
6551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6552 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6553 peer_info_get (&my_full_id),
6554 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6557 send_client_peer_connected (t, myid);
6558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6559 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6561 t->local_tid_dest = next_local_tid++;
6562 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6563 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6564 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6568 /* Ok, lets find a peer offering the service */
6569 if (NULL != t->dht_get_type)
6571 GNUNET_DHT_get_stop (t->dht_get_type);
6573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6574 GNUNET_h2s (&hash));
6576 GNUNET_DHT_get_start (dht_handle,
6577 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6579 dht_replication_level,
6580 GNUNET_DHT_RO_RECORD_ROUTE |
6581 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6583 &dht_get_type_handler, t);
6585 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6591 * Handler for connection requests to new peers by a string service description.
6593 * @param cls closure
6594 * @param client identification of the client
6595 * @param message the actual message, which includes messages the client wants
6598 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6599 const struct GNUNET_MessageHeader *message)
6601 struct GNUNET_MESH_ConnectPeerByString *msg;
6602 struct MeshRegexSearchContext *ctx;
6603 struct MeshRegexSearchInfo *info;
6604 struct GNUNET_DHT_GetHandle *get_h;
6605 struct GNUNET_HashCode key;
6606 struct MeshTunnel *t;
6607 struct MeshClient *c;
6608 MESH_TunnelNumber tid;
6612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6613 "Connect by string started\n");
6614 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6615 size = htons (message->size);
6617 /* Sanity check for client registration */
6618 if (NULL == (c = client_get (client)))
6621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6626 /* Message size sanity check */
6627 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6630 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6634 /* Tunnel exists? */
6635 tid = ntohl (msg->tunnel_id);
6636 t = tunnel_get_by_local_id (c, tid);
6640 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6644 /* Does client own tunnel? */
6645 if (t->owner->handle != client)
6648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6653 " on tunnel %s [%u]\n",
6654 GNUNET_i2s(&my_full_id),
6657 /* Only one connect_by_string allowed at the same time! */
6658 /* FIXME: allow more, return handle at api level to cancel, document */
6659 if (NULL != t->regex_ctx)
6662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6666 /* Find string itself */
6667 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6668 string = (const char *) &msg[1];
6670 /* Initialize context */
6671 size = GNUNET_REGEX_get_first_key(string, len, &key);
6672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6673 " consumed %u bits out of %u\n", size, len);
6674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6675 " looking for %s\n", GNUNET_h2s (&key));
6677 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6679 info->description = GNUNET_malloc (len + 1);
6680 memcpy (info->description, string, len);
6681 info->description[len] = '\0';
6682 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6683 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6686 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6687 ctx->position = size;
6691 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6693 /* Start search in DHT */
6694 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
6695 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
6696 &key, /* key to search */
6697 dht_replication_level, /* replication level */
6698 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6699 NULL, /* xquery */ // FIXME BLOOMFILTER
6700 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
6701 &dht_get_string_handler, ctx);
6703 GNUNET_break (GNUNET_OK ==
6704 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
6707 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
6709 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
6715 * Handler for client traffic directed to one peer
6717 * @param cls closure
6718 * @param client identification of the client
6719 * @param message the actual message
6722 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
6723 const struct GNUNET_MessageHeader *message)
6725 struct MeshClient *c;
6726 struct MeshTunnel *t;
6727 struct MeshPeerInfo *pi;
6728 struct GNUNET_MESH_Unicast *data_msg;
6729 MESH_TunnelNumber tid;
6732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6733 "Got a unicast request from a client!\n");
6735 /* Sanity check for client registration */
6736 if (NULL == (c = client_get (client)))
6739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6744 data_msg = (struct GNUNET_MESH_Unicast *) message;
6746 /* Sanity check for message size */
6747 size = ntohs (message->size);
6748 if (sizeof (struct GNUNET_MESH_Unicast) +
6749 sizeof (struct GNUNET_MessageHeader) > size)
6752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6756 /* Tunnel exists? */
6757 tid = ntohl (data_msg->tid);
6758 t = tunnel_get_by_local_id (c, tid);
6762 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6766 /* Is it a local tunnel? Then, does client own the tunnel? */
6767 if (t->owner->handle != client)
6770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6774 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
6775 &data_msg->destination.hashPubKey);
6776 /* Is the selected peer in the tunnel? */
6780 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6784 /* PID should be as expected */
6785 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6788 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6789 "Unicast PID, expected %u, got %u\n",
6790 t->fwd_pid + 1, ntohl (data_msg->pid));
6791 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6795 /* Ok, everything is correct, send the message
6796 * (pretend we got it from a mesh peer)
6799 /* Work around const limitation */
6800 char buf[ntohs (message->size)] GNUNET_ALIGN;
6801 struct GNUNET_MESH_Unicast *copy;
6803 copy = (struct GNUNET_MESH_Unicast *) buf;
6804 memcpy (buf, data_msg, size);
6805 copy->oid = my_full_id;
6806 copy->tid = htonl (t->id.tid);
6807 copy->ttl = htonl (default_ttl);
6808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6809 " calling generic handler...\n");
6810 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
6812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
6813 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6819 * Handler for client traffic directed to the origin
6821 * @param cls closure
6822 * @param client identification of the client
6823 * @param message the actual message
6826 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
6827 const struct GNUNET_MessageHeader *message)
6829 struct GNUNET_MESH_ToOrigin *data_msg;
6830 struct MeshTunnelClientInfo *clinfo;
6831 struct MeshClient *c;
6832 struct MeshTunnel *t;
6833 MESH_TunnelNumber tid;
6836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6837 "Got a ToOrigin request from a client!\n");
6838 /* Sanity check for client registration */
6839 if (NULL == (c = client_get (client)))
6842 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6847 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
6849 /* Sanity check for message size */
6850 size = ntohs (message->size);
6851 if (sizeof (struct GNUNET_MESH_ToOrigin) +
6852 sizeof (struct GNUNET_MessageHeader) > size)
6855 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6859 /* Tunnel exists? */
6860 tid = ntohl (data_msg->tid);
6861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6862 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6868 t = tunnel_get_by_local_id (c, tid);
6872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6876 /* It should be sent by someone who has this as incoming tunnel. */
6877 if (GNUNET_NO == client_knows_tunnel (c, t))
6880 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6884 /* PID should be as expected */
6885 clinfo = tunnel_get_client_fc (t, c);
6886 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
6889 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6890 "To Origin PID, expected %u, got %u\n",
6891 clinfo->bck_pid + 1, ntohl (data_msg->pid));
6892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6896 /* Ok, everything is correct, send the message
6897 * (pretend we got it from a mesh peer)
6900 char buf[ntohs (message->size)] GNUNET_ALIGN;
6901 struct GNUNET_MESH_ToOrigin *copy;
6903 /* Work around const limitation */
6904 copy = (struct GNUNET_MESH_ToOrigin *) buf;
6905 memcpy (buf, data_msg, size);
6906 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6907 copy->tid = htonl (t->id.tid);
6908 copy->ttl = htonl (default_ttl);
6909 GNUNET_assert (ntohl (copy->pid) == (t->bck_pid + 1));
6910 copy->sender = my_full_id;
6911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6912 " calling generic handler...\n");
6913 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
6915 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6921 * Handler for client traffic directed to all peers in a tunnel
6923 * @param cls closure
6924 * @param client identification of the client
6925 * @param message the actual message
6928 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
6929 const struct GNUNET_MessageHeader *message)
6931 struct MeshClient *c;
6932 struct MeshTunnel *t;
6933 struct GNUNET_MESH_Multicast *data_msg;
6934 MESH_TunnelNumber tid;
6936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6937 "Got a multicast request from a client!\n");
6939 /* Sanity check for client registration */
6940 if (NULL == (c = client_get (client)))
6943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6948 data_msg = (struct GNUNET_MESH_Multicast *) message;
6950 /* Sanity check for message size */
6951 if (sizeof (struct GNUNET_MESH_Multicast) +
6952 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
6955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6959 /* Tunnel exists? */
6960 tid = ntohl (data_msg->tid);
6961 t = tunnel_get_by_local_id (c, tid);
6965 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6969 /* Does client own tunnel? */
6970 if (t->owner->handle != client)
6973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6977 /* PID should be as expected */
6978 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
6981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6982 "Multicast PID, expected %u, got %u\n",
6983 t->fwd_pid + 1, ntohl (data_msg->pid));
6984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6989 char buf[ntohs (message->size)] GNUNET_ALIGN;
6990 struct GNUNET_MESH_Multicast *copy;
6992 copy = (struct GNUNET_MESH_Multicast *) buf;
6993 memcpy (buf, message, ntohs (message->size));
6994 copy->oid = my_full_id;
6995 copy->tid = htonl (t->id.tid);
6996 copy->ttl = htonl (default_ttl);
6997 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
6998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6999 " calling generic handler...\n");
7000 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7003 /* receive done gets called when last copy is sent to a neighbor */
7009 * Handler for client's ACKs for payload traffic.
7011 * @param cls Closure (unused).
7012 * @param client Identification of the client.
7013 * @param message The actual message.
7016 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7017 const struct GNUNET_MessageHeader *message)
7019 struct GNUNET_MESH_LocalAck *msg;
7020 struct MeshTunnel *t;
7021 struct MeshClient *c;
7022 MESH_TunnelNumber tid;
7024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7025 /* Sanity check for client registration */
7026 if (NULL == (c = client_get (client)))
7029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7034 msg = (struct GNUNET_MESH_LocalAck *) message;
7036 /* Tunnel exists? */
7037 tid = ntohl (msg->tunnel_id);
7038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7039 t = tunnel_get_by_local_id (c, tid);
7042 GNUNET_break (0); // FIXME fc
7043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7047 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7048 if (NULL != t->owner && t->owner->handle == client)
7050 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7051 t->bck_ack = ntohl(msg->max_pid);
7052 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7056 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7057 tunnel_set_client_fwd_ack (t, c, ntohl (msg->max_pid));
7058 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7061 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7067 * Functions to handle messages from clients
7069 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7070 {&handle_local_new_client, NULL,
7071 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7072 {&handle_local_announce_regex, NULL,
7073 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7074 {&handle_local_tunnel_create, NULL,
7075 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7076 sizeof (struct GNUNET_MESH_TunnelMessage)},
7077 {&handle_local_tunnel_destroy, NULL,
7078 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7079 sizeof (struct GNUNET_MESH_TunnelMessage)},
7080 {&handle_local_tunnel_speed, NULL,
7081 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7082 sizeof (struct GNUNET_MESH_TunnelMessage)},
7083 {&handle_local_tunnel_speed, NULL,
7084 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7085 sizeof (struct GNUNET_MESH_TunnelMessage)},
7086 {&handle_local_tunnel_buffer, NULL,
7087 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7088 sizeof (struct GNUNET_MESH_TunnelMessage)},
7089 {&handle_local_tunnel_buffer, NULL,
7090 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7091 sizeof (struct GNUNET_MESH_TunnelMessage)},
7092 {&handle_local_connect_add, NULL,
7093 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7094 sizeof (struct GNUNET_MESH_PeerControl)},
7095 {&handle_local_connect_del, NULL,
7096 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7097 sizeof (struct GNUNET_MESH_PeerControl)},
7098 {&handle_local_blacklist, NULL,
7099 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7100 sizeof (struct GNUNET_MESH_PeerControl)},
7101 {&handle_local_unblacklist, NULL,
7102 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7103 sizeof (struct GNUNET_MESH_PeerControl)},
7104 {&handle_local_connect_by_type, NULL,
7105 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7106 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7107 {&handle_local_connect_by_string, NULL,
7108 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7109 {&handle_local_unicast, NULL,
7110 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7111 {&handle_local_to_origin, NULL,
7112 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7113 {&handle_local_multicast, NULL,
7114 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7115 {&handle_local_ack, NULL,
7116 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7117 sizeof (struct GNUNET_MESH_LocalAck)},
7123 * To be called on core init/fail.
7125 * @param cls service closure
7126 * @param server handle to the server for this service
7127 * @param identity the public identity of this peer
7130 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7131 const struct GNUNET_PeerIdentity *identity)
7134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7135 core_handle = server;
7136 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7140 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7142 GNUNET_abort(); // Try harder
7149 * Method called whenever a given peer connects.
7151 * @param cls closure
7152 * @param peer peer identity this notification is about
7153 * @param atsi performance data for the connection
7154 * @param atsi_count number of records in 'atsi'
7157 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7158 const struct GNUNET_ATS_Information *atsi,
7159 unsigned int atsi_count)
7161 struct MeshPeerInfo *peer_info;
7162 struct MeshPeerPath *path;
7164 DEBUG_CONN ("Peer connected\n");
7165 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7166 peer_info = peer_info_get (peer);
7167 if (myid == peer_info->id)
7169 DEBUG_CONN (" (self)\n");
7174 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7176 path = path_new (2);
7177 path->peers[0] = myid;
7178 path->peers[1] = peer_info->id;
7179 GNUNET_PEER_change_rc (myid, 1);
7180 GNUNET_PEER_change_rc (peer_info->id, 1);
7181 peer_info_add_path (peer_info, path, GNUNET_YES);
7182 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7188 * Method called whenever a peer disconnects.
7190 * @param cls closure
7191 * @param peer peer identity this notification is about
7194 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7196 struct MeshPeerInfo *pi;
7197 struct MeshPeerQueue *q;
7198 struct MeshPeerQueue *n;
7200 DEBUG_CONN ("Peer disconnected\n");
7201 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7213 /* try to reroute this traffic instead */
7214 queue_destroy(q, GNUNET_YES);
7218 peer_info_remove_path (pi, pi->id, myid);
7221 DEBUG_CONN (" (self)\n");
7223 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7228 /******************************************************************************/
7229 /************************ MAIN FUNCTIONS ****************************/
7230 /******************************************************************************/
7233 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7235 * @param cls closure
7236 * @param key current key code
7237 * @param value value in the hash map
7238 * @return GNUNET_YES if we should continue to iterate,
7242 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7244 struct MeshTunnel *t = value;
7251 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7253 * @param cls closure
7254 * @param key current key code
7255 * @param value value in the hash map
7256 * @return GNUNET_YES if we should continue to iterate,
7260 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7262 struct MeshPeerInfo *p = value;
7263 struct MeshPeerQueue *q;
7264 struct MeshPeerQueue *n;
7272 queue_destroy(q, GNUNET_YES);
7276 peer_info_destroy (p);
7281 * Task run during shutdown.
7287 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7291 if (core_handle != NULL)
7293 GNUNET_CORE_disconnect (core_handle);
7296 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7297 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7298 if (dht_handle != NULL)
7300 GNUNET_DHT_disconnect (dht_handle);
7305 GNUNET_SERVER_notification_context_destroy (nc);
7308 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7310 GNUNET_SCHEDULER_cancel (announce_id_task);
7311 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7317 * Process mesh requests.
7319 * @param cls closure
7320 * @param server the initialized server
7321 * @param c configuration to use
7324 run (void *cls, struct GNUNET_SERVER_Handle *server,
7325 const struct GNUNET_CONFIGURATION_Handle *c)
7327 struct MeshPeerInfo *peer;
7328 struct MeshPeerPath *p;
7331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7332 server_handle = server;
7333 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7334 NULL, /* Closure passed to MESH functions */
7335 &core_init, /* Call core_init once connected */
7336 &core_connect, /* Handle connects */
7337 &core_disconnect, /* remove peers on disconnects */
7338 NULL, /* Don't notify about all incoming messages */
7339 GNUNET_NO, /* For header only in notification */
7340 NULL, /* Don't notify about all outbound messages */
7341 GNUNET_NO, /* For header-only out notification */
7342 core_handlers); /* Register these handlers */
7344 if (core_handle == NULL)
7347 GNUNET_SCHEDULER_shutdown ();
7352 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7357 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7359 GNUNET_SCHEDULER_shutdown ();
7364 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7365 &refresh_path_time))
7367 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7369 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7370 "refresh path time");
7371 GNUNET_SCHEDULER_shutdown ();
7376 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7377 &app_announce_time))
7379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7381 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7382 "app announce time");
7383 GNUNET_SCHEDULER_shutdown ();
7388 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7391 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7393 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7394 "id announce time");
7395 GNUNET_SCHEDULER_shutdown ();
7400 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7401 &unacknowledged_wait_time))
7403 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7405 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7406 "unacknowledged wait time");
7407 GNUNET_SCHEDULER_shutdown ();
7412 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7417 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7419 GNUNET_SCHEDULER_shutdown ();
7424 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7427 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7429 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7431 GNUNET_SCHEDULER_shutdown ();
7436 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7441 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7443 GNUNET_SCHEDULER_shutdown ();
7448 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7453 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7459 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7460 &dht_replication_level))
7462 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7464 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7465 "dht replication level", 10);
7466 dht_replication_level = 10;
7470 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7471 GNUNET_free (keyfile);
7472 if (my_private_key == NULL)
7474 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7475 _("Mesh service could not access hostkey. Exiting.\n"));
7476 GNUNET_SCHEDULER_shutdown ();
7479 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7480 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7481 &my_full_id.hashPubKey);
7482 myid = GNUNET_PEER_intern (&my_full_id);
7484 // transport_handle = GNUNET_TRANSPORT_connect(c,
7491 dht_handle = GNUNET_DHT_connect (c, 64);
7492 if (dht_handle == NULL)
7497 stats = GNUNET_STATISTICS_create ("mesh", c);
7501 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7503 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7504 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7505 peers = GNUNET_CONTAINER_multihashmap_create (32);
7506 applications = GNUNET_CONTAINER_multihashmap_create (32);
7507 types = GNUNET_CONTAINER_multihashmap_create (32);
7509 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7510 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7511 GNUNET_SERVER_disconnect_notify (server_handle,
7512 &handle_local_client_disconnect, NULL);
7516 clients_tail = NULL;
7519 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7520 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7522 /* Create a peer_info for the local peer */
7523 peer = peer_info_get (&my_full_id);
7526 GNUNET_PEER_change_rc (myid, 1);
7527 peer_info_add_path (peer, p, GNUNET_YES);
7529 /* Scheduled the task to clean up when shutdown is called */
7530 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7537 * The main function for the mesh service.
7539 * @param argc number of arguments from the command line
7540 * @param argv command line arguments
7541 * @return 0 ok, 1 on error
7544 main (int argc, char *const *argv)
7548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7551 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");