2 This file is part of GNUnet.
3 (C) 2001-2012 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?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
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_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_YES
64 #if MESH_DEBUG_CONNECTION
65 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_CONN(...)
71 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
73 #define DEBUG_DHT(...)
80 struct timespec __mesh_start;
81 struct timespec __mesh_end;
82 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
83 #define INTERVAL_END \
85 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
86 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
87 if (__diff < 0) __diff += 1000000000;\
91 #define INTERVAL_SHOW \
93 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
95 #define INTERVAL_START
100 /******************************************************************************/
101 /************************ DATA STRUCTURES ****************************/
102 /******************************************************************************/
104 /** FWD declaration */
110 * Struct representing a piece of data being sent to other peers
114 /** Tunnel it belongs to. */
115 struct MeshTunnel *t;
117 /** How many remaining neighbors still hav't got it. */
118 unsigned int reference_counter;
120 /** How many remaining neighbors we need to send this to. */
121 unsigned int total_out;
123 /** Size of the data. */
132 * Struct containing info about a queued transmission to this peer
139 struct MeshPeerQueue *next;
144 struct MeshPeerQueue *prev;
147 * Peer this transmission is directed to.
149 struct MeshPeerInfo *peer;
152 * Tunnel this message belongs to.
154 struct MeshTunnel *tunnel;
157 * Pointer to info stucture used as cls.
167 * Size of the message
174 * Struct containing all info possibly needed to build a package when called
177 struct MeshTransmissionDescriptor
179 /** ID of the tunnel this packet travels in */
180 struct MESH_TunnelID *origin;
182 /** Who was this message being sent to */
183 struct MeshPeerInfo *peer;
185 /** Ultimate destination of the packet */
186 GNUNET_PEER_Id destination;
188 /** Data descriptor */
189 struct MeshData* mesh_data;
194 * Struct containing all information regarding a given peer
204 * Last time we heard from this peer
206 struct GNUNET_TIME_Absolute last_contact;
209 * Number of attempts to reconnect so far
211 int n_reconnect_attempts;
214 * Paths to reach the peer, ordered by ascending hop count
216 struct MeshPeerPath *path_head;
219 * Paths to reach the peer, ordered by ascending hop count
221 struct MeshPeerPath *path_tail;
224 * Handle to stop the DHT search for a path to this peer
226 struct GNUNET_DHT_GetHandle *dhtget;
229 * Closure given to the DHT GET
231 struct MeshPathInfo *dhtgetcls;
234 * Array of tunnels this peer participates in
235 * (most probably a small amount, therefore not a hashmap)
236 * When the path to the peer changes, notify these tunnels to let them
237 * re-adjust their path trees.
239 struct MeshTunnel **tunnels;
242 * Number of tunnels this peers participates in
244 unsigned int ntunnels;
247 * Transmission queue to core DLL head
249 struct MeshPeerQueue *queue_head;
252 * Transmission queue to core DLL tail
254 struct MeshPeerQueue *queue_tail;
257 * How many messages are in the queue to this peer.
259 unsigned int queue_n;
262 * Handle to for queued transmissions
264 struct GNUNET_CORE_TransmitHandle *core_transmit;
269 * Globally unique tunnel identification (owner + number)
270 * DO NOT USE OVER THE NETWORK
275 * Node that owns the tunnel
280 * Tunnel number to differentiate all the tunnels owned by the node oid
281 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
283 MESH_TunnelNumber tid;
288 * Struct containing all information regarding a tunnel
289 * For an intermediate node the improtant info used will be:
290 * - id Tunnel unique identification
291 * - paths[0] To know where to send it next
292 * - metainfo: ready, speeds, accounting
299 struct MESH_TunnelID id;
302 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
304 MESH_TunnelNumber local_tid;
307 * Local tunnel number for local destination clients (incoming number)
308 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
311 MESH_TunnelNumber local_tid_dest;
314 * Is the speed on the tunnel limited to the slowest peer?
319 * Is the tunnel bufferless (minimum latency)?
324 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
329 * Packet ID of the last bck packet sent (unique counter per hop).
334 * SKIP value for this tunnel.
339 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
340 * Contains the Flow Control info: FWD ACK value received,
341 * last BCK ACK sent, PID and SKIP values.
343 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
346 * Last ACK sent towards the origin (for traffic towards leaf node).
348 uint32_t last_fwd_ack;
351 * BCK ACK value received from the hop towards the owner of the tunnel,
352 * (previous node / owner): up to what message PID can we sent back to him.
357 * How many messages are in the forward queue (towards leaves).
359 unsigned int fwd_queue_n;
362 * How many messages do we accept in the forward queue.
364 unsigned int fwd_queue_max;
367 * How many messages are in the backward queue (towards origin).
369 unsigned int bck_queue_n;
372 * How many messages do we accept in the backward queue.
374 unsigned int bck_queue_max;
377 * Last time the tunnel was used
379 struct GNUNET_TIME_Absolute timestamp;
382 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
383 * containing peers added by id or by type, not intermediate peers.
385 struct GNUNET_CONTAINER_MultiHashMap *peers;
388 * Number of peers that are connected and potentially ready to receive data
390 unsigned int peers_ready;
393 * Number of peers that have been added to the tunnel
395 unsigned int peers_total;
398 * Client owner of the tunnel, if any
400 struct MeshClient *owner;
403 * Clients that have been informed about and want to stay in the tunnel.
405 struct MeshClient **clients;
408 * Flow control info for each client.
410 struct MeshTunnelClientInfo *clients_fc;
413 * Number of elements in clients/clients_fc
415 unsigned int nclients;
418 * Clients that have been informed but requested to leave the tunnel.
420 struct MeshClient **ignore;
423 * Number of elements in clients
425 unsigned int nignore;
430 GNUNET_PEER_Id *blacklisted;
433 * Number of elements in blacklisted
435 unsigned int nblacklisted;
438 * Bloomfilter (for peer identities) to stop circular routes
440 char bloomfilter[MESH_BLOOM_SIZE];
445 struct MeshTunnelTree *tree;
448 * Application type we are looking for in this tunnel
450 GNUNET_MESH_ApplicationType type;
453 * Used to search peers offering a service
455 struct GNUNET_DHT_GetHandle *dht_get_type;
458 * Initial context of the regex search for a connect_by_string
460 struct MeshRegexSearchContext *regex_ctx;
463 * Task to keep the used paths alive
465 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
468 * Task to destroy the tunnel after timeout
470 * FIXME: merge the two? a tunnel will have either
471 * a path refresh OR a timeout, never both!
473 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
476 * Flag to signal the destruction of the tunnel.
477 * If this is set GNUNET_YES the tunnel will be destroyed
478 * when the queue is empty.
485 * Info about a child node in a tunnel, needed to perform flow control.
487 struct MeshTunnelChildInfo
490 * ID of the child node.
505 * Maximum PID allowed (FWD ACK received).
510 * Last ACK sent to that child (BCK ACK).
515 * Circular buffer pointing to MeshPeerQueue elements.
516 * Size determined by the tunnel queue size.
518 struct MeshPeerQueue **send_buffer;
521 * Index of the oldest element in the send_buffer.
523 unsigned int send_buffer_start;
526 * How many elements are already in the buffer.
528 unsigned int send_buffer_n;
533 * Info about a leaf client of a tunnel, needed to perform flow control.
535 struct MeshTunnelClientInfo
538 * PID of the last packet sent to the client (FWD).
543 * PID of the last packet received from the client (BCK).
548 * Maximum PID allowed (FWD ACK received).
553 * Last ACK sent to that child (BCK ACK).
561 * Info collected during iteration of child nodes in order to get the ACK value
564 struct MeshTunnelChildIteratorContext
567 * Tunnel whose info is being collected.
569 struct MeshTunnel *t;
572 * Is this context initialized? Is the value in max_child_ack valid?
577 * Maximum child ACK so far.
579 uint32_t max_child_ack;
582 * Number of children nodes
584 unsigned int nchildren;
589 * Info needed to work with tunnel paths and peers
596 struct MeshTunnel *t;
599 * Neighbouring peer to whom we send the packet to
601 struct MeshPeerInfo *peer;
606 struct MeshPeerPath *path;
611 * Struct containing information about a client of the service
618 struct MeshClient *next;
623 struct MeshClient *prev;
626 * Tunnels that belong to this client, indexed by local id
628 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
631 * Tunnels this client has accepted, indexed by incoming local id
633 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
636 * Tunnels this client has rejected, indexed by incoming local id
638 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
640 * Handle to communicate with the client
642 struct GNUNET_SERVER_Client *handle;
645 * Applications that this client has claimed to provide
647 struct GNUNET_CONTAINER_MultiHashMap *apps;
650 * Messages that this client has declared interest in
652 struct GNUNET_CONTAINER_MultiHashMap *types;
655 * Whether the client is active or shutting down (don't send confirmations
656 * to a client that is shutting down.
661 * ID of the client, mainly for debug messages
666 * Regular expressions describing the services offered by this client.
668 char **regexes; // FIXME add timeout? API to remove a regex?
671 * Number of regular expressions in regexes.
673 unsigned int n_regex;
676 * Task to refresh all regular expresions in the DHT.
678 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
684 * Struct to keep information of searches of services described by a regex
685 * using a user-provided string service description.
687 struct MeshRegexSearchInfo
690 * Which tunnel is this for
692 struct MeshTunnel *t;
695 * User provided description of the searched service.
700 * Part of the description already consumed by the search.
707 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
710 * Results from running DHT GETs.
712 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
715 * Contexts, for each running DHT GET. Free all on end of search.
717 struct MeshRegexSearchContext **contexts;
720 * Number of contexts (branches/steps in search).
722 unsigned int n_contexts;
725 * Peer that is connecting via connect_by_string. When connected, free ctx.
730 * Other peers that are found but not yet being connected to.
732 GNUNET_PEER_Id *peers;
735 * Number of elements in peers.
737 unsigned int n_peers;
740 * Next peer to try to connect to.
745 * Timeout for a connect attempt.
746 * When reached, try to connect to a different peer, if any. If not,
747 * try the same peer again.
749 GNUNET_SCHEDULER_TaskIdentifier timeout;
754 * Struct to keep state of running searches that have consumed a part of
757 struct MeshRegexSearchContext
760 * Part of the description already consumed by
761 * this particular search branch.
766 * Information about the search.
768 struct MeshRegexSearchInfo *info;
771 * We just want to look for one edge, the longer the better.
774 unsigned int longest_match;
777 * Destination hash of the longest match.
779 struct GNUNET_HashCode hash;
782 /******************************************************************************/
783 /************************ DEBUG FUNCTIONS ****************************/
784 /******************************************************************************/
788 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
789 * @param cls string to print
790 * @param success GNUNET_OK if the PUT was transmitted,
791 * GNUNET_NO on timeout,
792 * GNUNET_SYSERR on disconnect from service
793 * after the PUT message was transmitted
794 * (so we don't know if it was received or not)
799 mesh_debug (void *cls, int success)
803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
807 unsigned int debug_fwd_ack;
808 unsigned int debug_bck_ack;
812 /******************************************************************************/
813 /*********************** GLOBAL VARIABLES ****************************/
814 /******************************************************************************/
817 * Configuration parameters
819 static struct GNUNET_TIME_Relative refresh_path_time;
820 static struct GNUNET_TIME_Relative app_announce_time;
821 static struct GNUNET_TIME_Relative id_announce_time;
822 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
823 static struct GNUNET_TIME_Relative connect_timeout;
824 static long long unsigned int default_ttl;
825 static long long unsigned int dht_replication_level;
826 static long long unsigned int max_tunnels;
827 static long long unsigned int max_msgs_queue;
831 * Hostkey generation context
833 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
836 * DLL with all the clients, head.
838 static struct MeshClient *clients;
841 * DLL with all the clients, tail.
843 static struct MeshClient *clients_tail;
846 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
848 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
851 * Number of tunnels known.
853 static unsigned long long n_tunnels;
856 * Tunnels incoming, indexed by MESH_TunnelNumber
857 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
859 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
862 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
864 static struct GNUNET_CONTAINER_MultiHashMap *peers;
867 * Handle to communicate with transport
869 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
872 * Handle to communicate with core.
874 static struct GNUNET_CORE_Handle *core_handle;
879 static struct GNUNET_DHT_Handle *dht_handle;
884 static struct GNUNET_SERVER_Handle *server_handle;
887 * Handle to the statistics service.
889 static struct GNUNET_STATISTICS_Handle *stats;
892 * Notification context, to send messages to local clients.
894 static struct GNUNET_SERVER_NotificationContext *nc;
897 * Local peer own ID (memory efficient handle).
899 static GNUNET_PEER_Id myid;
902 * Local peer own ID (full value).
904 static struct GNUNET_PeerIdentity my_full_id;
909 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
914 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
917 * Tunnel ID for the next created tunnel (global tunnel number).
919 static MESH_TunnelNumber next_tid;
922 * Tunnel ID for the next incoming tunnel (local tunnel number).
924 static MESH_TunnelNumber next_local_tid;
927 * All application types provided by this peer.
929 static struct GNUNET_CONTAINER_MultiHashMap *applications;
932 * All message types clients of this peer are interested in.
934 static struct GNUNET_CONTAINER_MultiHashMap *types;
937 * Task to periodically announce provided applications.
939 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
942 * Task to periodically announce itself in the network.
944 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
947 * Next ID to assign to a client.
949 unsigned int next_client_id;
952 /******************************************************************************/
953 /*********************** DECLARATIONS **************************/
954 /******************************************************************************/
956 /* FIXME move declarations here */
959 * Function to process paths received for a new peer addition. The recorded
960 * paths form the initial tunnel, which can be optimized later.
961 * Called on each result obtained for the DHT search.
964 * @param exp when will this value expire
965 * @param key key of the result
966 * @param type type of the result
967 * @param size number of bytes in data
968 * @param data pointer to the result data
971 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
972 const struct GNUNET_HashCode * key,
973 const struct GNUNET_PeerIdentity *get_path,
974 unsigned int get_path_length,
975 const struct GNUNET_PeerIdentity *put_path,
976 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
977 size_t size, const void *data);
981 * Function to process DHT string to regex matching.
982 * Called on each result obtained for the DHT search.
984 * @param cls closure (search context)
985 * @param exp when will this value expire
986 * @param key key of the result
987 * @param get_path path of the get request (not used)
988 * @param get_path_length lenght of get_path (not used)
989 * @param put_path path of the put request (not used)
990 * @param put_path_length length of the put_path (not used)
991 * @param type type of the result
992 * @param size number of bytes in data
993 * @param data pointer to the result data
995 * TODO: re-issue the request after certain time? cancel after X results?
998 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
999 const struct GNUNET_HashCode * key,
1000 const struct GNUNET_PeerIdentity *get_path,
1001 unsigned int get_path_length,
1002 const struct GNUNET_PeerIdentity *put_path,
1003 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1004 size_t size, const void *data);
1008 * Function to process DHT string to regex matching.
1009 * Called on each result obtained for the DHT search.
1011 * @param cls closure (search context)
1012 * @param exp when will this value expire
1013 * @param key key of the result
1014 * @param get_path path of the get request (not used)
1015 * @param get_path_length lenght of get_path (not used)
1016 * @param put_path path of the put request (not used)
1017 * @param put_path_length length of the put_path (not used)
1018 * @param type type of the result
1019 * @param size number of bytes in data
1020 * @param data pointer to the result data
1023 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1024 const struct GNUNET_HashCode * key,
1025 const struct GNUNET_PeerIdentity *get_path,
1026 unsigned int get_path_length,
1027 const struct GNUNET_PeerIdentity *put_path,
1028 unsigned int put_path_length,
1029 enum GNUNET_BLOCK_Type type,
1030 size_t size, const void *data);
1034 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1035 * and insert it in the appropiate structures if the peer is not known yet.
1037 * @param peer Short identity of the peer.
1039 * @return Existing or newly created peer info.
1041 static struct MeshPeerInfo *
1042 peer_info_get_short (const GNUNET_PEER_Id peer);
1046 * Try to establish a new connection to this peer.
1047 * Use the best path for the given tunnel.
1048 * If the peer doesn't have any path to it yet, try to get one.
1049 * If the peer already has some path, send a CREATE PATH towards it.
1051 * @param peer PeerInfo of the peer.
1052 * @param t Tunnel for which to create the path, if possible.
1055 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1059 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1060 * needed rescources.
1061 * If peer already exists, reevaluate shortest path and change if different.
1063 * @param t Tunnel we want to add a new peer to
1064 * @param peer PeerInfo of the peer being added
1068 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1072 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1073 * that are no longer needed, as well as nodes of no longer reachable peers.
1074 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1076 * @param t Tunnel from which to remove the path.
1077 * @param peer Short id of the peer which should be removed.
1080 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1084 * Search for a tunnel by global ID using full PeerIdentities.
1086 * @param oid owner of the tunnel.
1087 * @param tid global tunnel number.
1089 * @return tunnel handler, NULL if doesn't exist.
1091 static struct MeshTunnel *
1092 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1096 * Delete an active client from the tunnel.
1102 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1105 * Notify a tunnel that a connection has broken that affects at least
1106 * some of its peers.
1108 * @param t Tunnel affected.
1109 * @param p1 Peer that got disconnected from p2.
1110 * @param p2 Peer that got disconnected from p1.
1112 * @return Short ID of the peer disconnected (either p1 or p2).
1113 * 0 if the tunnel remained unaffected.
1115 static GNUNET_PEER_Id
1116 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1121 * Get the current ack value for a tunnel, for data going from root to leaves,
1122 * taking in account the tunnel mode and the status of all children and clients.
1126 * @return Maximum PID allowed.
1129 tunnel_get_fwd_ack (struct MeshTunnel *t);
1133 * Add a client to a tunnel, initializing all needed data structures.
1135 * @param t Tunnel to which add the client.
1136 * @param c Client which to add to the tunnel.
1139 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1143 * Jump to the next edge, with the longest matching token.
1145 * @param block Block found in the DHT.
1146 * @param size Size of the block.
1147 * @param ctx Context of the search.
1149 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1152 regex_next_edge (const struct MeshRegexBlock *block,
1154 struct MeshRegexSearchContext *ctx);
1158 * Find a path to a peer that offers a regex servcie compatible
1159 * with a given string.
1161 * @param key The key of the accepting state.
1162 * @param ctx Context containing info about the string, tunnel, etc.
1165 regex_find_path (const struct GNUNET_HashCode *key,
1166 struct MeshRegexSearchContext *ctx);
1170 * @brief Queue and pass message to core when possible.
1172 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1173 * and accounts for it. In case the queue is full, the message is dropped and
1176 * Otherwise, message is treated as internal and allowed to go regardless of
1179 * @param cls Closure (@c type dependant). It will be used by queue_send to
1180 * build the message to be sent if not already prebuilt.
1181 * @param type Type of the message, 0 for a raw message.
1182 * @param size Size of the message.
1183 * @param dst Neighbor to send message to.
1184 * @param t Tunnel this message belongs to.
1187 queue_add (void *cls, uint16_t type, size_t size,
1188 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1192 * Free a transmission that was already queued with all resources
1193 * associated to the request.
1195 * @param queue Queue handler to cancel.
1196 * @param clear_cls Is it necessary to free associated cls?
1199 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1203 * @brief Get the next transmittable message from the queue.
1205 * This will be the head, except in the case of being a data packet
1206 * not allowed by the destination peer.
1208 * @param peer Destination peer.
1210 * @return The next viable MeshPeerQueue element to send to that peer.
1211 * NULL when there are no transmittable messages.
1213 struct MeshPeerQueue *
1214 queue_get_next (const struct MeshPeerInfo *peer);
1218 * Core callback to write a queued packet to core buffer
1220 * @param cls Closure (peer info).
1221 * @param size Number of bytes available in buf.
1222 * @param buf Where the to write the message.
1224 * @return number of bytes written to buf
1227 queue_send (void *cls, size_t size, void *buf);
1229 /******************************************************************************/
1230 /************************ ITERATORS ****************************/
1231 /******************************************************************************/
1234 * Iterator over found existing mesh regex blocks that match an ongoing search.
1236 * @param cls closure
1237 * @param key current key code
1238 * @param value value in the hash map
1239 * @return GNUNET_YES if we should continue to iterate,
1243 regex_result_iterator (void *cls,
1244 const struct GNUNET_HashCode * key,
1247 struct MeshRegexBlock *block = value;
1248 struct MeshRegexSearchContext *ctx = cls;
1250 if (GNUNET_YES == ntohl(block->accepting) &&
1251 ctx->position == strlen (ctx->info->description))
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1254 regex_find_path (key, ctx);
1255 return GNUNET_YES; // We found an accept state!
1259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1260 ctx->position, strlen(ctx->info->description),
1261 ntohl(block->accepting));
1264 regex_next_edge(block, SIZE_MAX, ctx);
1271 * Iterator over edges in a regex block retrieved from the DHT.
1273 * @param cls Closure (context of the search).
1274 * @param token Token that follows to next state.
1275 * @param len Lenght of token.
1276 * @param key Hash of next state.
1278 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1281 regex_edge_iterator (void *cls,
1284 const struct GNUNET_HashCode *key)
1286 struct MeshRegexSearchContext *ctx = cls;
1287 struct MeshRegexSearchInfo *info = ctx->info;
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1294 current = &info->description[ctx->position];
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1296 current_len = strlen (info->description) - ctx->position;
1297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1301 if (len > current_len)
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1304 return GNUNET_YES; // Token too long, wont match
1306 if (0 != strncmp (current, token, len))
1308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1309 return GNUNET_YES; // Token doesn't match
1312 if (len > ctx->longest_match)
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1315 ctx->longest_match = len;
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1329 * Jump to the next edge, with the longest matching token.
1331 * @param block Block found in the DHT.
1332 * @param size Size of the block.
1333 * @param ctx Context of the search.
1335 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1338 regex_next_edge (const struct MeshRegexBlock *block,
1340 struct MeshRegexSearchContext *ctx)
1342 struct MeshRegexSearchContext *new_ctx;
1343 struct MeshRegexSearchInfo *info = ctx->info;
1344 struct GNUNET_DHT_GetHandle *get_h;
1348 /* Find the longest match for the current string position,
1349 * among tokens in the given block */
1350 ctx->longest_match = 0;
1351 result = GNUNET_MESH_regex_block_iterate (block, size,
1352 ®ex_edge_iterator, ctx);
1353 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1355 /* Did anything match? */
1356 if (0 == ctx->longest_match)
1359 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1360 new_ctx->info = info;
1361 new_ctx->position = ctx->position + ctx->longest_match;
1362 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1364 /* Check whether we already have a DHT GET running for it */
1366 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1369 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1371 ®ex_result_iterator,
1373 return; // We are already looking for it
1376 /* Start search in DHT */
1378 GNUNET_DHT_get_start (dht_handle, /* handle */
1379 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1380 &ctx->hash, /* key to search */
1381 dht_replication_level, /* replication level */
1382 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1383 NULL, /* xquery */ // FIXME BLOOMFILTER
1384 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1385 &dht_get_string_handler, new_ctx);
1387 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1390 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1399 * Iterator over hash map entries to cancel DHT GET requests after a
1400 * successful connect_by_string.
1402 * @param cls Closure (unused).
1403 * @param key Current key code (unused).
1404 * @param value Value in the hash map (get handle).
1405 * @return GNUNET_YES if we should continue to iterate,
1409 regex_cancel_dht_get (void *cls,
1410 const struct GNUNET_HashCode * key,
1413 struct GNUNET_DHT_GetHandle *h = value;
1415 GNUNET_DHT_get_stop (h);
1421 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1422 * search for connect_by_string.
1424 * @param cls Closure (unused).
1425 * @param key Current key code (unused).
1426 * @param value MeshRegexBlock in the hash map.
1427 * @return GNUNET_YES if we should continue to iterate,
1431 regex_free_result (void *cls,
1432 const struct GNUNET_HashCode * key,
1436 GNUNET_free (value);
1442 * Regex callback iterator to store own service description in the DHT.
1444 * @param cls closure.
1445 * @param key hash for current state.
1446 * @param proof proof for current state.
1447 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1448 * @param num_edges number of edges leaving current state.
1449 * @param edges edges leaving current state.
1452 regex_iterator (void *cls,
1453 const struct GNUNET_HashCode *key,
1456 unsigned int num_edges,
1457 const struct GNUNET_REGEX_Edge *edges)
1459 struct MeshRegexBlock *block;
1460 struct MeshRegexEdge *block_edge;
1461 enum GNUNET_DHT_RouteOption opt;
1465 unsigned int offset;
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1469 " regex dht put for state %s\n",
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1479 if (GNUNET_YES == accepting)
1481 struct MeshRegexAccept block;
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 " state %s is accepting, putting own id\n",
1486 size = sizeof (block);
1488 block.id = my_full_id;
1490 GNUNET_DHT_put(dht_handle, key,
1491 dht_replication_level,
1492 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1493 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1496 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1501 len = strlen(proof);
1502 size = sizeof (struct MeshRegexBlock) + len;
1503 block = GNUNET_malloc (size);
1506 block->n_proof = htonl (len);
1507 block->n_edges = htonl (num_edges);
1508 block->accepting = htonl (accepting);
1510 /* Store the proof at the end of the block. */
1511 aux = (char *) &block[1];
1512 memcpy (aux, proof, len);
1515 /* Store each edge in a variable length MeshEdge struct at the
1516 * very end of the MeshRegexBlock structure.
1518 for (i = 0; i < num_edges; i++)
1520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1521 " edge %s towards %s\n",
1523 GNUNET_h2s(&edges[i].destination));
1525 /* aux points at the end of the last block */
1526 len = strlen (edges[i].label);
1527 size += sizeof (struct MeshRegexEdge) + len;
1528 // Calculate offset FIXME is this ok? use size instead?
1529 offset = aux - (char *) block;
1530 block = GNUNET_realloc (block, size);
1531 aux = &((char *) block)[offset];
1532 block_edge = (struct MeshRegexEdge *) aux;
1533 block_edge->key = edges[i].destination;
1534 block_edge->n_token = htonl (len);
1535 aux = (char *) &block_edge[1];
1536 memcpy (aux, edges[i].label, len);
1540 GNUNET_DHT_put(dht_handle, key,
1541 dht_replication_level,
1543 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1545 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1549 GNUNET_free (block);
1554 * Store the regular expression describing a local service into the DHT.
1556 * @param regex The regular expresion.
1559 regex_put (const char *regex)
1561 struct GNUNET_REGEX_Automaton *dfa;
1563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1564 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1565 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1566 GNUNET_REGEX_automaton_destroy (dfa);
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1572 * Find a path to a peer that offers a regex servcie compatible
1573 * with a given string.
1575 * @param key The key of the accepting state.
1576 * @param ctx Context containing info about the string, tunnel, etc.
1579 regex_find_path (const struct GNUNET_HashCode *key,
1580 struct MeshRegexSearchContext *ctx)
1582 struct GNUNET_DHT_GetHandle *get_h;
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1585 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1586 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1587 key, /* key to search */
1588 dht_replication_level, /* replication level */
1589 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1590 GNUNET_DHT_RO_RECORD_ROUTE,
1591 NULL, /* xquery */ // FIXME BLOOMFILTER
1592 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1593 &dht_get_string_accept_handler, ctx);
1594 GNUNET_break (GNUNET_OK ==
1595 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1598 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1603 * Function called if the connect attempt to a peer found via
1604 * connect_by_string times out. Try to connect to another peer, if any.
1605 * Otherwise try to reconnect to the same peer.
1607 * @param cls Closure (info about regex search).
1608 * @param tc TaskContext.
1611 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1613 struct MeshRegexSearchInfo *info = cls;
1614 struct MeshPeerInfo *peer_info;
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1619 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1620 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1628 if (0 < info->n_peers)
1630 // Select next peer, put current in that spot.
1631 id = info->peers[info->i_peer];
1632 info->peers[info->i_peer] = info->peer;
1633 info->i_peer = (info->i_peer + 1) % info->n_peers;
1637 // Try to connect to same peer again.
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1642 peer_info = peer_info_get_short(id);
1643 tunnel_add_peer (info->t, peer_info);
1645 tunnel_delete_peer (info->t, old);
1646 peer_info_connect (peer_info, info->t);
1647 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1648 ®ex_connect_timeout,
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1655 * Cancel an ongoing regex search in the DHT and free all resources.
1657 * @param ctx The search context.
1660 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1662 struct MeshRegexSearchInfo *info = ctx->info;
1665 GNUNET_free (info->description);
1666 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1667 ®ex_cancel_dht_get, NULL);
1668 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1669 ®ex_free_result, NULL);
1670 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1671 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1672 info->t->regex_ctx = NULL;
1673 for (i = 0; i < info->n_contexts; i++)
1675 GNUNET_free (info->contexts[i]);
1677 if (0 < info->n_contexts)
1678 GNUNET_free (info->contexts);
1679 if (0 < info->n_peers)
1680 GNUNET_free (info->peers);
1681 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1683 GNUNET_SCHEDULER_cancel(info->timeout);
1689 /******************************************************************************/
1690 /************************ PERIODIC FUNCTIONS ****************************/
1691 /******************************************************************************/
1694 * Announce iterator over for each application provided by the peer
1696 * @param cls closure
1697 * @param key current key code
1698 * @param value value in the hash map
1699 * @return GNUNET_YES if we should continue to
1704 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1706 struct PBlock block;
1707 struct MeshClient *c;
1709 block.id = my_full_id;
1710 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1711 GNUNET_assert(NULL != c);
1712 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1713 if (0 == block.type)
1718 block.type = htonl (block.type);
1720 GNUNET_break (NULL !=
1721 GNUNET_DHT_put (dht_handle, key,
1722 dht_replication_level,
1723 GNUNET_DHT_RO_RECORD_ROUTE |
1724 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1725 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1727 (const char *) &block,
1728 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1730 app_announce_time, NULL, NULL));
1736 * Periodically announce what applications are provided by local clients
1739 * @param cls closure
1740 * @param tc task context
1743 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1745 struct MeshClient *c = cls;
1748 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1750 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1754 DEBUG_DHT ("Starting PUT for regex\n");
1756 for (i = 0; i < c->n_regex; i++)
1758 regex_put (c->regexes[i]);
1760 c->regex_announce_task =
1761 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1762 DEBUG_DHT ("Finished PUT for regex\n");
1769 * Periodically announce what applications are provided by local clients
1772 * @param cls closure
1773 * @param tc task context
1776 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1778 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1780 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1784 DEBUG_DHT ("Starting PUT for apps\n");
1786 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1788 announce_applications_task =
1789 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1791 DEBUG_DHT ("Finished PUT for apps\n");
1798 * Periodically announce self id in the DHT
1800 * @param cls closure
1801 * @param tc task context
1804 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1806 struct PBlock block;
1808 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1810 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1814 * - Set data expiration in function of X
1815 * - Adapt X to churn
1817 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1819 block.id = my_full_id;
1820 block.type = htonl (0);
1821 GNUNET_DHT_put (dht_handle, /* DHT handle */
1822 &my_full_id.hashPubKey, /* Key to use */
1823 dht_replication_level, /* Replication level */
1824 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1825 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1826 sizeof (block), /* Size of the data */
1827 (const char *) &block, /* Data itself */
1828 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1829 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1830 NULL, /* Continuation */
1831 NULL); /* Continuation closure */
1833 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1837 /******************************************************************************/
1838 /****************** GENERAL HELPER FUNCTIONS ************************/
1839 /******************************************************************************/
1842 * Decrements the reference counter and frees all resources if needed
1844 * @param mesh_data Data Descriptor used in a multicast message.
1845 * Freed no longer needed (last message).
1848 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1850 if (0 == --(mesh_data->reference_counter))
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1853 GNUNET_free (mesh_data->data);
1854 GNUNET_free (mesh_data);
1860 * Check if client has registered with the service and has not disconnected
1862 * @param client the client to check
1864 * @return non-NULL if client exists in the global DLL
1866 static struct MeshClient *
1867 client_get (struct GNUNET_SERVER_Client *client)
1869 struct MeshClient *c;
1874 if (c->handle == client)
1883 * Checks if a given client has subscribed to certain message type
1885 * @param message_type Type of message to check
1886 * @param c Client to check
1888 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1890 * FIXME: use of crypto_hash slows it down
1891 * The hash function alone takes 8-10us out of the ~55us for the whole
1892 * process of retransmitting the message from one local client to another.
1893 * Find faster implementation!
1896 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1898 struct GNUNET_HashCode hc;
1900 if (NULL == c->types)
1903 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1904 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1909 * Check whether client wants traffic from a tunnel.
1911 * @param c Client to check.
1912 * @param t Tunnel to be found.
1914 * @return GNUNET_YES if client knows tunnel.
1916 * TODO look in client hashmap
1919 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1923 for (i = 0; i < t->nclients; i++)
1924 if (t->clients[i] == c)
1931 * Check whether client has been informed about a tunnel.
1933 * @param c Client to check.
1934 * @param t Tunnel to be found.
1936 * @return GNUNET_YES if client knows tunnel.
1938 * TODO look in client hashmap
1941 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1945 for (i = 0; i < t->nignore; i++)
1946 if (t->ignore[i] == c)
1948 return client_wants_tunnel(c, t);
1953 * Marks a client as uninterested in traffic from the tunnel, updating both
1954 * client and tunnel to reflect this.
1956 * @param c Client that doesn't want traffic anymore.
1957 * @param t Tunnel which should be ignored.
1959 * FIXME when to delete an incoming tunnel?
1962 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1964 struct GNUNET_HashCode hash;
1966 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1967 GNUNET_break (GNUNET_YES ==
1968 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1970 GNUNET_break (GNUNET_YES ==
1971 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1972 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1973 tunnel_delete_active_client (t, c);
1974 GNUNET_array_append (t->ignore, t->nignore, c);
1979 * Deletes a tunnel from a client (either owner or destination). To be used on
1980 * tunnel destroy, otherwise, use client_ignore_tunnel.
1982 * @param c Client whose tunnel to delete.
1983 * @param t Tunnel which should be deleted.
1986 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1988 struct GNUNET_HashCode hash;
1992 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1993 GNUNET_assert (GNUNET_YES ==
1994 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2000 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2002 GNUNET_assert (GNUNET_YES ==
2003 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2007 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2015 * Send the message to all clients that have subscribed to its type
2017 * @param msg Pointer to the message itself
2018 * @param payload Pointer to the payload of the message.
2019 * @param t The tunnel to whose clients this message goes.
2021 * @return number of clients this message was sent to
2024 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2025 const struct GNUNET_MessageHeader *payload,
2026 struct MeshTunnel *t)
2028 struct MeshClient *c;
2029 MESH_TunnelNumber *tid;
2032 char cbuf[htons (msg->size)];
2034 type = ntohs (payload->type);
2035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2037 GNUNET_MESH_DEBUG_M2S (type));
2039 memcpy (cbuf, msg, sizeof (cbuf));
2040 switch (htons (msg->type))
2042 struct GNUNET_MESH_Unicast *uc;
2043 struct GNUNET_MESH_Multicast *mc;
2044 struct GNUNET_MESH_ToOrigin *to;
2046 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2047 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2050 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2051 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2054 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2055 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2063 for (count = 0, c = clients; c != NULL; c = c->next)
2065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2066 if (client_is_subscribed (type, c))
2068 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2072 *tid = htonl (t->local_tid);
2076 if (GNUNET_NO == client_knows_tunnel (c, t))
2078 /* This client doesn't know the tunnel */
2079 struct GNUNET_MESH_TunnelNotification tmsg;
2080 struct GNUNET_HashCode hash;
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2083 tmsg.header.size = htons (sizeof (tmsg));
2084 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2085 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2086 tmsg.tunnel_id = htonl (t->local_tid_dest);
2087 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2088 &tmsg.header, GNUNET_NO);
2089 tunnel_add_client (t, c);
2090 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2092 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2093 c->incoming_tunnels, &hash, t,
2094 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2096 *tid = htonl (t->local_tid_dest);
2099 /* Check if the client wants to get traffic from the tunnel */
2100 if (GNUNET_NO == client_wants_tunnel(c, t))
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2104 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2105 (struct GNUNET_MessageHeader
2106 *) cbuf, GNUNET_NO);
2115 * Notify the client that owns the tunnel that a peer has connected to it
2116 * (the requested path to it has been confirmed).
2118 * @param t Tunnel whose owner to notify
2119 * @param id Short id of the peer that has connected
2122 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2124 struct GNUNET_MESH_PeerControl pc;
2126 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2127 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2128 pc.tunnel_id = htonl (t->local_tid);
2129 GNUNET_PEER_resolve (id, &pc.peer);
2130 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2136 * Notify all clients (not depending on registration status) that the incoming
2137 * tunnel is no longer valid.
2139 * @param t Tunnel that was destroyed.
2142 send_clients_tunnel_destroy (struct MeshTunnel *t)
2144 struct GNUNET_MESH_TunnelMessage msg;
2146 msg.header.size = htons (sizeof (msg));
2147 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2148 msg.tunnel_id = htonl (t->local_tid_dest);
2149 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2154 * Notify clients of tunnel disconnections, if needed.
2155 * In case the origin disconnects, the destination clients get a tunnel destroy
2156 * notification. If the last destination disconnects (only one remaining client
2157 * in tunnel), the origin gets a (local ID) peer disconnected.
2158 * Note that the function must be called BEFORE removing the client from
2161 * @param t Tunnel that was destroyed.
2162 * @param c Client that disconnected.
2165 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2171 struct GNUNET_MESH_TunnelMessage msg;
2173 msg.header.size = htons (sizeof (msg));
2174 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2175 msg.tunnel_id = htonl (t->local_tid_dest);
2176 for (i = 0; i < t->nclients; i++)
2177 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2178 &msg.header, GNUNET_NO);
2180 // FIXME when to disconnect an incoming tunnel?
2181 else if (1 == t->nclients && NULL != t->owner)
2183 struct GNUNET_MESH_PeerControl msg;
2185 msg.header.size = htons (sizeof (msg));
2186 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2187 msg.tunnel_id = htonl (t->local_tid);
2188 msg.peer = my_full_id;
2189 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2190 &msg.header, GNUNET_NO);
2196 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2197 * and insert it in the appropiate structures if the peer is not known yet.
2199 * @param peer Full identity of the peer.
2201 * @return Existing or newly created peer info.
2203 static struct MeshPeerInfo *
2204 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2206 struct MeshPeerInfo *peer_info;
2208 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2209 if (NULL == peer_info)
2212 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2213 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2215 peer_info->id = GNUNET_PEER_intern (peer);
2223 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2224 * and insert it in the appropiate structures if the peer is not known yet.
2226 * @param peer Short identity of the peer.
2228 * @return Existing or newly created peer info.
2230 static struct MeshPeerInfo *
2231 peer_info_get_short (const GNUNET_PEER_Id peer)
2233 struct GNUNET_PeerIdentity id;
2235 GNUNET_PEER_resolve (peer, &id);
2236 return peer_info_get (&id);
2241 * Iterator to remove the tunnel from the list of tunnels a peer participates
2244 * @param cls Closure (tunnel info)
2245 * @param key GNUNET_PeerIdentity of the peer (unused)
2246 * @param value PeerInfo of the peer
2248 * @return always GNUNET_YES, to keep iterating
2251 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2253 struct MeshTunnel *t = cls;
2254 struct MeshPeerInfo *peer = value;
2257 for (i = 0; i < peer->ntunnels; i++)
2260 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2263 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2264 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2273 * Core callback to write a pre-constructed data packet to core buffer
2275 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2276 * @param size Number of bytes available in buf.
2277 * @param buf Where the to write the message.
2279 * @return number of bytes written to buf
2282 send_core_data_raw (void *cls, size_t size, void *buf)
2284 struct MeshTransmissionDescriptor *info = cls;
2285 struct GNUNET_MessageHeader *msg;
2288 GNUNET_assert (NULL != info);
2289 GNUNET_assert (NULL != info->mesh_data);
2290 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2291 total_size = ntohs (msg->size);
2293 if (total_size > size)
2298 memcpy (buf, msg, total_size);
2299 data_descriptor_decrement_rc (info->mesh_data);
2306 * Sends an already built non-multicast message to a peer,
2307 * properly registrating all used resources.
2309 * @param message Message to send. Function makes a copy of it.
2310 * @param peer Short ID of the neighbor whom to send the message.
2311 * @param t Tunnel on which this message is transmitted.
2314 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2315 const struct GNUNET_PeerIdentity *peer,
2316 struct MeshTunnel *t)
2318 struct MeshTransmissionDescriptor *info;
2319 struct MeshPeerInfo *neighbor;
2320 struct MeshPeerPath *p;
2324 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2326 size = ntohs (message->size);
2327 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2328 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2329 info->mesh_data->data = GNUNET_malloc (size);
2330 memcpy (info->mesh_data->data, message, size);
2331 type = ntohs(message->type);
2332 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2334 struct GNUNET_MESH_Unicast *m;
2336 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2337 m->ttl = htonl (ntohl (m->ttl) - 1);
2339 info->mesh_data->data_len = size;
2340 info->mesh_data->reference_counter = 1;
2341 info->mesh_data->total_out = 1;
2342 neighbor = peer_info_get (peer);
2343 for (p = neighbor->path_head; NULL != p; p = p->next)
2353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2354 " %s IS NOT DIRECTLY CONNECTED\n",
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2359 for (p = neighbor->path_head; NULL != p; p = p->next)
2361 struct GNUNET_PeerIdentity debug_id;
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 " path with %u hops through:\n",
2367 for (i = 0; i < p->length; i++)
2369 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 i, GNUNET_i2s(&debug_id));
2376 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2377 GNUNET_free (info->mesh_data->data);
2378 GNUNET_free (info->mesh_data);
2382 info->peer = neighbor;
2383 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2386 * TODO: in this case we only need the service to retransmit
2387 * the message down the path. If we pass the real type to queue_add,
2388 * queue_send will try to build the message from scratch. This can
2389 * probably be done by some other way instead of deleteing the type
2403 * Sends a CREATE PATH message for a path to a peer, properly registrating
2404 * all used resources.
2406 * @param peer PeerInfo of the final peer for whom this path is being created.
2407 * @param p Path itself.
2408 * @param t Tunnel for which the path is created.
2411 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2412 struct MeshTunnel *t)
2414 struct GNUNET_PeerIdentity id;
2415 struct MeshPathInfo *path_info;
2416 struct MeshPeerInfo *neighbor;
2422 p = tree_get_path_to_peer (t->tree, peer->id);
2429 for (i = 0; i < p->length; i++)
2431 if (p->peers[i] == myid)
2434 if (i >= p->length - 1)
2440 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2442 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2443 path_info->path = p;
2445 neighbor = peer_info_get (&id);
2446 path_info->peer = neighbor;
2447 queue_add (path_info,
2448 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2449 sizeof (struct GNUNET_MESH_ManipulatePath) +
2450 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2457 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2459 * @param t Tunnel whose path to destroy.
2460 * @param destination Short ID of the peer to whom the path to destroy.
2463 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2465 struct MeshPeerPath *p;
2468 p = tree_get_path_to_peer (t->tree, destination);
2474 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2475 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2477 struct GNUNET_MESH_ManipulatePath *msg;
2478 struct GNUNET_PeerIdentity *pi;
2482 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2483 msg->header.size = htons (size);
2484 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2485 msg->tid = htonl (t->id.tid);
2486 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2487 for (i = 0; i < p->length; i++)
2489 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2491 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2498 * Try to establish a new connection to this peer.
2499 * Use the best path for the given tunnel.
2500 * If the peer doesn't have any path to it yet, try to get one.
2501 * If the peer already has some path, send a CREATE PATH towards it.
2503 * @param peer PeerInfo of the peer.
2504 * @param t Tunnel for which to create the path, if possible.
2507 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2509 struct MeshPeerPath *p;
2510 struct MeshPathInfo *path_info;
2512 if (NULL != peer->path_head)
2514 p = tree_get_path_to_peer (t->tree, peer->id);
2521 // FIXME always send create path to self
2524 send_create_path (peer, p, t);
2528 struct GNUNET_HashCode hash;
2531 send_client_peer_connected (t, myid);
2532 t->local_tid_dest = next_local_tid++;
2533 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2536 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2537 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2544 else if (NULL == peer->dhtget)
2546 struct GNUNET_PeerIdentity id;
2548 GNUNET_PEER_resolve (peer->id, &id);
2549 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2550 path_info->peer = peer;
2552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2553 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2554 peer->dhtgetcls = path_info;
2555 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2556 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2557 &id.hashPubKey, /* key to search */
2558 dht_replication_level, /* replication level */
2559 GNUNET_DHT_RO_RECORD_ROUTE |
2560 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2561 NULL, /* xquery */ // FIXME BLOOMFILTER
2562 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2563 &dht_get_id_handler, path_info);
2565 /* Otherwise, there is no path but the DHT get is already started. */
2570 * Task to delay the connection of a peer
2572 * @param cls Closure (path info with tunnel and peer to connect).
2573 * Will be free'd on exection.
2574 * @param tc TaskContext
2577 peer_info_connect_task (void *cls,
2578 const struct GNUNET_SCHEDULER_TaskContext *tc)
2580 struct MeshPathInfo *path_info = cls;
2582 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2587 peer_info_connect (path_info->peer, path_info->t);
2593 * Destroy the peer_info and free any allocated resources linked to it
2595 * @param pi The peer_info to destroy.
2597 * @return GNUNET_OK on success
2600 peer_info_destroy (struct MeshPeerInfo *pi)
2602 struct GNUNET_PeerIdentity id;
2603 struct MeshPeerPath *p;
2604 struct MeshPeerPath *nextp;
2606 GNUNET_PEER_resolve (pi->id, &id);
2607 GNUNET_PEER_change_rc (pi->id, -1);
2610 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2613 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2614 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2616 if (NULL != pi->dhtget)
2618 GNUNET_DHT_get_stop (pi->dhtget);
2619 GNUNET_free (pi->dhtgetcls);
2625 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2635 * Remove all paths that rely on a direct connection between p1 and p2
2636 * from the peer itself and notify all tunnels about it.
2638 * @param peer PeerInfo of affected peer.
2639 * @param p1 GNUNET_PEER_Id of one peer.
2640 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2643 * TODO: optimize (see below)
2646 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2649 struct MeshPeerPath *p;
2650 struct MeshPeerPath *aux;
2651 struct MeshPeerInfo *peer_d;
2653 unsigned int destroyed;
2658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2660 p = peer->path_head;
2664 for (i = 0; i < (p->length - 1); i++)
2666 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2667 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2669 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2680 for (i = 0; i < peer->ntunnels; i++)
2682 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2686 * Problem: one or more peers have been deleted from the tunnel tree.
2687 * We don't know who they are to try to add them again.
2688 * We need to try to find a new path for each of the disconnected peers.
2689 * Some of them might already have a path to reach them that does not
2690 * involve p1 and p2. Adding all anew might render in a better tree than
2691 * the trivial immediate fix.
2693 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2694 * its children will be reachable trough him.
2696 peer_d = peer_info_get_short (d);
2699 for (p = peer_d->path_head; NULL != p; p = p->next)
2701 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2709 /* No callback, as peer will be already disconnected and a connection
2710 * scheduled by tunnel_notify_connection_broken.
2712 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2716 peer_info_connect (peer_d, peer->tunnels[i]);
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2724 * Add the path to the peer and update the path used to reach it in case this
2727 * @param peer_info Destination peer to add the path to.
2728 * @param path New path to add. Last peer must be the peer in arg 1.
2729 * Path will be either used of freed if already known.
2730 * @param trusted Do we trust that this path is real?
2733 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2736 struct MeshPeerPath *aux;
2740 if ((NULL == peer_info) || (NULL == path))
2743 path_destroy (path);
2746 if (path->peers[path->length - 1] != peer_info->id)
2749 path_destroy (path);
2752 if (path->length <= 2 && GNUNET_NO == trusted)
2754 /* Only allow CORE to tell us about direct paths */
2755 path_destroy (path);
2758 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2759 for (l = 1; l < path->length; l++)
2761 if (path->peers[l] == myid)
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2764 for (l2 = 0; l2 < path->length - l; l2++)
2766 path->peers[l2] = path->peers[l + l2];
2771 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2776 struct GNUNET_PeerIdentity id;
2778 GNUNET_PEER_resolve (peer_info->id, &id);
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2780 path->length, GNUNET_i2s (&id));
2783 l = path_get_length (path);
2790 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2791 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2793 l2 = path_get_length (aux);
2796 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2797 peer_info->path_tail, aux, path);
2802 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2804 path_destroy (path);
2809 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2816 * Add the path to the origin peer and update the path used to reach it in case
2817 * this is the shortest.
2818 * The path is given in peer_info -> destination, therefore we turn the path
2819 * upside down first.
2821 * @param peer_info Peer to add the path to, being the origin of the path.
2822 * @param path New path to add after being inversed.
2823 * @param trusted Do we trust that this path is real?
2826 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2827 struct MeshPeerPath *path, int trusted)
2830 peer_info_add_path (peer_info, path, trusted);
2835 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2836 * to obtain a local peer -> destination path and interning the peer ids.
2838 * @return Newly allocated and created path
2840 static struct MeshPeerPath *
2841 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2842 unsigned int get_path_length,
2843 const struct GNUNET_PeerIdentity *put_path,
2844 unsigned int put_path_length)
2846 struct MeshPeerPath *p;
2852 GNUNET_PEER_change_rc (myid, 1);
2853 i = get_path_length;
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2855 for (i--; i >= 0; i--)
2857 id = GNUNET_PEER_intern (&get_path[i]);
2858 if (p->length > 0 && id == p->peers[p->length - 1])
2860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2861 GNUNET_PEER_change_rc (id, -1);
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2866 GNUNET_i2s (&get_path[i]));
2868 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2869 p->peers[p->length - 1] = id;
2872 i = put_path_length;
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2874 for (i--; i >= 0; i--)
2876 id = GNUNET_PEER_intern (&put_path[i]);
2879 /* PUT path went through us, so discard the path up until now and start
2880 * from here to get a much shorter (and loop-free) path.
2885 if (p->length > 0 && id == p->peers[p->length - 1])
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2888 GNUNET_PEER_change_rc (id, -1);
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2893 GNUNET_i2s (&put_path[i]));
2895 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2896 p->peers[p->length - 1] = id;
2900 if (get_path_length > 0)
2901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2902 GNUNET_i2s (&get_path[0]));
2903 if (put_path_length > 0)
2904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2905 GNUNET_i2s (&put_path[0]));
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2908 for (i = 0; i < p->length; i++)
2910 struct GNUNET_PeerIdentity peer_id;
2912 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2914 GNUNET_i2s (&peer_id));
2922 * Adds a path to the peer_infos of all the peers in the path
2924 * @param p Path to process.
2925 * @param confirmed Whether we know if the path works or not.
2928 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2932 /* TODO: invert and add */
2933 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2934 for (i++; i < p->length; i++)
2936 struct MeshPeerInfo *aux;
2937 struct MeshPeerPath *copy;
2939 aux = peer_info_get_short (p->peers[i]);
2940 copy = path_duplicate (p);
2941 copy->length = i + 1;
2942 peer_info_add_path (aux, copy, GNUNET_NO);
2948 * Send keepalive packets for a peer
2950 * @param cls Closure (tunnel for which to send the keepalive).
2951 * @param tc Notification context.
2953 * TODO: implement explicit multicast keepalive?
2956 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2960 * Search for a tunnel among the incoming tunnels
2962 * @param tid the local id of the tunnel
2964 * @return tunnel handler, NULL if doesn't exist
2966 static struct MeshTunnel *
2967 tunnel_get_incoming (MESH_TunnelNumber tid)
2969 struct GNUNET_HashCode hash;
2971 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2972 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2973 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2978 * Search for a tunnel among the tunnels for a client
2980 * @param c the client whose tunnels to search in
2981 * @param tid the local id of the tunnel
2983 * @return tunnel handler, NULL if doesn't exist
2985 static struct MeshTunnel *
2986 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2988 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2990 return tunnel_get_incoming (tid);
2994 struct GNUNET_HashCode hash;
2996 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2997 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3003 * Search for a tunnel by global ID using PEER_ID
3005 * @param pi owner of the tunnel
3006 * @param tid global tunnel number
3008 * @return tunnel handler, NULL if doesn't exist
3010 static struct MeshTunnel *
3011 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3013 struct MESH_TunnelID id;
3014 struct GNUNET_HashCode hash;
3019 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3020 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3025 * Search for a tunnel by global ID using full PeerIdentities
3027 * @param oid owner of the tunnel
3028 * @param tid global tunnel number
3030 * @return tunnel handler, NULL if doesn't exist
3032 static struct MeshTunnel *
3033 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3035 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3040 * Delete an active client from the tunnel.
3046 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3050 for (i = 0; i < t->nclients; i++)
3052 if (t->clients[i] == c)
3054 t->clients[i] = t->clients[t->nclients - 1];
3055 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3056 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3058 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3066 * Delete an ignored client from the tunnel.
3072 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3076 for (i = 0; i < t->nignore; i++)
3078 if (t->ignore[i] == c)
3080 t->ignore[i] = t->ignore[t->nignore - 1];
3081 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3089 * Delete a client from the tunnel. It should be only done on
3090 * client disconnection, otherwise use client_ignore_tunnel.
3096 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3098 tunnel_delete_ignored_client (t, c);
3099 tunnel_delete_active_client (t, c);
3104 * Iterator to free MeshTunnelChildInfo of tunnel children.
3106 * @param cls Closure (tunnel info).
3107 * @param key Hash of GNUNET_PEER_Id (unused).
3108 * @param value MeshTunnelChildInfo of the child.
3110 * @return always GNUNET_YES, to keep iterating
3113 tunnel_destroy_child (void *cls,
3114 const struct GNUNET_HashCode * key,
3117 struct MeshTunnelChildInfo *cinfo = value;
3118 struct MeshTunnel *t = cls;
3122 for (c = 0; c < cinfo->send_buffer_n; c++)
3124 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3125 if (NULL != cinfo->send_buffer[i])
3126 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3129 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3131 GNUNET_free_non_null (cinfo->send_buffer);
3132 GNUNET_free (cinfo);
3138 * Callback used to notify a client owner of a tunnel that a peer has
3139 * disconnected, most likely because of a path change.
3141 * @param cls Closure (tunnel this notification is about).
3142 * @param peer_id Short ID of disconnected peer.
3145 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3147 struct MeshTunnel *t = cls;
3148 struct MeshPeerInfo *peer;
3149 struct MeshPathInfo *path_info;
3151 if (NULL != t->owner && NULL != nc)
3153 struct GNUNET_MESH_PeerControl msg;
3155 msg.header.size = htons (sizeof (msg));
3156 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3157 msg.tunnel_id = htonl (t->local_tid);
3158 GNUNET_PEER_resolve (peer_id, &msg.peer);
3159 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3160 &msg.header, GNUNET_NO);
3162 peer = peer_info_get_short (peer_id);
3163 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3164 path_info->peer = peer;
3166 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3171 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3172 * needed rescources.
3173 * If peer already exists, reevaluate shortest path and change if different.
3175 * @param t Tunnel we want to add a new peer to
3176 * @param peer PeerInfo of the peer being added
3180 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3182 struct GNUNET_PeerIdentity id;
3183 struct MeshPeerPath *best_p;
3184 struct MeshPeerPath *p;
3185 unsigned int best_cost;
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3189 GNUNET_PEER_resolve (peer->id, &id);
3191 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3194 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3195 GNUNET_assert (GNUNET_OK ==
3196 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3198 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3201 if (NULL != (p = peer->path_head))
3204 best_cost = tree_get_path_cost (t->tree, p);
3207 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3214 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3215 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3216 t->path_refresh_task =
3217 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3221 /* Start a DHT get */
3222 peer_info_connect (peer, t);
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3228 * Add a path to a tunnel which we don't own, just to remember the next hop.
3229 * If destination node was already in the tunnel, the first hop information
3230 * will be replaced with the new path.
3232 * @param t Tunnel we want to add a new peer to
3233 * @param p Path to add
3234 * @param own_pos Position of local node in path.
3238 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3239 unsigned int own_pos)
3241 struct GNUNET_PeerIdentity id;
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3244 GNUNET_assert (0 != own_pos);
3245 tree_add_path (t->tree, p, NULL, NULL);
3246 if (own_pos < p->length - 1)
3248 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3249 tree_update_first_hops (t->tree, myid, &id);
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3255 * Add a client to a tunnel, initializing all needed data structures.
3257 * @param t Tunnel to which add the client.
3258 * @param c Client which to add to the tunnel.
3261 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3263 struct MeshTunnelClientInfo clinfo;
3265 GNUNET_array_append (t->clients, t->nclients, c);
3266 clinfo.fwd_ack = t->fwd_pid + 1;
3267 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3268 clinfo.fwd_pid = t->fwd_pid;
3269 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3271 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3276 * Notifies a tunnel that a connection has broken that affects at least
3277 * some of its peers. Sends a notification towards the root of the tree.
3278 * In case the peer is the owner of the tree, notifies the client that owns
3279 * the tunnel and tries to reconnect.
3281 * @param t Tunnel affected.
3282 * @param p1 Peer that got disconnected from p2.
3283 * @param p2 Peer that got disconnected from p1.
3285 * @return Short ID of the peer disconnected (either p1 or p2).
3286 * 0 if the tunnel remained unaffected.
3288 static GNUNET_PEER_Id
3289 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3295 tree_notify_connection_broken (t->tree, p1, p2,
3296 &tunnel_notify_client_peer_disconnected,
3298 if (myid != p1 && myid != p2)
3304 if (tree_get_predecessor (t->tree) != 0)
3306 /* We are the peer still connected, notify owner of the disconnection. */
3307 struct GNUNET_MESH_PathBroken msg;
3308 struct GNUNET_PeerIdentity neighbor;
3310 msg.header.size = htons (sizeof (msg));
3311 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3312 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3313 msg.tid = htonl (t->id.tid);
3314 msg.peer1 = my_full_id;
3315 GNUNET_PEER_resolve (pid, &msg.peer2);
3316 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3317 send_prebuilt_message (&msg.header, &neighbor, t);
3325 * Send a multicast packet to a neighbor.
3327 * @param cls Closure (Info about the multicast packet)
3328 * @param neighbor_id Short ID of the neighbor to send the packet to.
3331 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3333 struct MeshData *mdata = cls;
3334 struct MeshTransmissionDescriptor *info;
3335 struct GNUNET_PeerIdentity neighbor;
3336 struct GNUNET_MessageHeader *msg;
3338 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3340 info->mesh_data = mdata;
3341 (mdata->reference_counter) ++;
3342 info->destination = neighbor_id;
3343 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3345 GNUNET_i2s (&neighbor));
3346 info->peer = peer_info_get (&neighbor);
3347 GNUNET_assert (NULL != info->peer);
3348 msg = (struct GNUNET_MessageHeader *) mdata->data;
3351 info->mesh_data->data_len,
3358 * Queue a message in a tunnel in multicast, sending a copy to each child node
3359 * down the local one in the tunnel tree.
3361 * @param t Tunnel in which to send the data.
3362 * @param msg Message to be sent.
3365 tunnel_send_multicast (struct MeshTunnel *t,
3366 const struct GNUNET_MessageHeader *msg)
3368 struct MeshData *mdata;
3370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3371 " sending a multicast packet...\n");
3373 mdata = GNUNET_malloc (sizeof (struct MeshData));
3374 mdata->data_len = ntohs (msg->size);
3376 mdata->data = GNUNET_malloc (mdata->data_len);
3377 memcpy (mdata->data, msg, mdata->data_len);
3378 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3380 struct GNUNET_MESH_Multicast *mcast;
3382 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3383 if (t->fwd_queue_n >= t->fwd_queue_max)
3386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3388 " message from %s!\n",
3389 GNUNET_i2s(&mcast->oid));
3390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3391 " message at %s!\n",
3392 GNUNET_i2s(&my_full_id));
3393 GNUNET_free (mdata->data);
3394 GNUNET_free (mdata);
3398 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3400 ntohl (mcast->ttl));
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3407 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3408 if (mdata->reference_counter == 0)
3410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3411 " no one to send data to\n");
3412 GNUNET_free (mdata->data);
3413 GNUNET_free (mdata);
3418 mdata->total_out = mdata->reference_counter;
3420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3421 " sending a multicast packet done\n");
3427 * Increase the SKIP value of all peers that
3428 * have not received a unicast message.
3430 * @param cls Closure (ID of the peer that HAS received the message).
3431 * @param key ID of the neighbor.
3432 * @param value Information about the neighbor.
3434 * @return GNUNET_YES to keep iterating.
3437 tunnel_add_skip (void *cls,
3438 const struct GNUNET_HashCode * key,
3441 struct GNUNET_PeerIdentity *neighbor = cls;
3442 struct MeshTunnelChildInfo *cinfo = value;
3444 /* TODO compare only pointers? key == neighbor? */
3445 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3455 * @brief Get neighbor's Flow Control information.
3457 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3458 * descendant of the local node in a certain tunnel.
3459 * If the info is not yet there (recently created path), creates the data struct
3460 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3463 * @param t Tunnel related.
3464 * @param peer Neighbor whose Flow Control info is needed.
3466 * @return Neighbor's Flow Control info.
3468 static struct MeshTunnelChildInfo *
3469 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3470 const struct GNUNET_PeerIdentity *peer)
3472 struct MeshTunnelChildInfo *cinfo;
3473 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3479 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3480 cinfo->id = GNUNET_PEER_intern (peer);
3481 cinfo->skip = t->fwd_pid;
3483 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3484 cinfo->fwd_ack = t->fwd_pid + delta;
3485 cinfo->bck_ack = delta;
3487 cinfo->send_buffer =
3488 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3490 GNUNET_assert (GNUNET_OK ==
3491 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3494 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3501 * Get the Flow Control info of a client.
3503 * @param t Tunnel on which to look.
3504 * @param c Client whose ACK to get.
3506 * @return ACK value.
3508 static struct MeshTunnelClientInfo *
3509 tunnel_get_client_fc (struct MeshTunnel *t,
3510 struct MeshClient *c)
3514 for (i = 0; i < t->nclients; i++)
3516 if (t->clients[i] != c)
3518 return &t->clients_fc[i];
3521 return NULL; // avoid compiler / coverity complaints
3526 * Iterator to get the appropiate ACK value from all children nodes.
3528 * @param cls Closue (tunnel).
3529 * @param id Id of the child node.
3532 tunnel_get_child_fwd_ack (void *cls,
3535 struct GNUNET_PeerIdentity peer_id;
3536 struct MeshTunnelChildInfo *cinfo;
3537 struct MeshTunnelChildIteratorContext *ctx = cls;
3538 struct MeshTunnel *t = ctx->t;
3541 GNUNET_PEER_resolve (id, &peer_id);
3542 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3543 ack = cinfo->fwd_ack;
3546 if (GNUNET_NO == ctx->init)
3548 ctx->max_child_ack = ack;
3549 ctx->init = GNUNET_YES;
3552 if (GNUNET_YES == t->speed_min)
3554 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3558 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3565 * Get the maximum PID allowed to transmit to any
3566 * tunnel child of the local peer, depending on the tunnel
3567 * buffering/speed settings.
3571 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3574 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3576 struct MeshTunnelChildIteratorContext ctx;
3578 ctx.max_child_ack = 0;
3580 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3582 if (0 == ctx.nchildren)
3584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3585 " tunnel has no children, no FWD ACK\n");
3589 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3590 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3592 return (int64_t) ctx.max_child_ack;
3597 * Set the FWD ACK value of a client in a particular tunnel.
3599 * @param t Tunnel affected.
3600 * @param c Client whose ACK to set.
3601 * @param ack ACK value.
3604 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3605 struct MeshClient *c,
3610 for (i = 0; i < t->nclients; i++)
3612 if (t->clients[i] != c)
3614 t->clients_fc[i].fwd_ack = ack;
3622 * Get the highest ACK value of all clients in a particular tunnel,
3623 * according to the buffering/speed settings.
3625 * @param t Tunnel on which to look.
3627 * @return Corresponding ACK value (max uint32_t).
3628 * If no clients are suscribed, -1LL.
3631 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3636 if (0 == t->nclients)
3638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3639 " tunnel has no clients, no FWD ACK\n");
3643 for (ack = -1LL, i = 0; i < t->nclients; i++)
3646 (GNUNET_YES == t->speed_min &&
3647 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3648 (GNUNET_NO == t->speed_min &&
3649 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3651 ack = t->clients_fc[i].fwd_ack;
3655 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3656 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3658 return (uint32_t) ack;
3663 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3664 * mode and the status of all children nodes.
3668 * @return Maximum PID allowed.
3671 tunnel_get_fwd_ack (struct MeshTunnel *t)
3675 uint32_t buffer_free;
3679 count = t->fwd_pid - t->skip;
3680 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3682 child_ack = tunnel_get_children_fwd_ack (t);
3683 client_ack = tunnel_get_clients_fwd_ack (t);
3684 if (-1LL == child_ack)
3686 // Node has no children, child_ack AND core buffer are irrelevant.
3687 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3688 return (uint32_t) client_ack;
3690 if (-1LL == client_ack)
3692 client_ack = ack + buffer_free; // Might overflow 32 bits, it's ok!
3694 if (GNUNET_YES == t->speed_min)
3696 ack = GMC_min_pid ((uint32_t) child_ack, ack) + buffer_free; // Might overflow 32 bits, it's ok!;
3697 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3701 ack = GMC_max_pid ((uint32_t) child_ack, ack) + buffer_free; // Might overflow 32 bits, it's ok!;
3702 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3704 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3705 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3707 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3708 count, buffer_free, child_ack, client_ack, ack);
3714 * Build a local ACK message and send it to a local client.
3716 * @param t Tunnel on which to send the ACK.
3717 * @param c Client to whom send the ACK.
3718 * @param ack Value of the ACK.
3721 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3723 struct GNUNET_MESH_LocalAck msg;
3725 msg.header.size = htons (sizeof (msg));
3726 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3727 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3728 msg.max_pid = htonl (ack);
3729 GNUNET_SERVER_notification_context_unicast(nc,
3736 * Build an ACK message and queue it to send to the given peer.
3738 * @param t Tunnel on which to send the ACK.
3739 * @param peer Peer to whom send the ACK.
3740 * @param ack Value of the ACK.
3743 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3745 struct GNUNET_MESH_ACK msg;
3747 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3748 msg.header.size = htons (sizeof (msg));
3749 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3750 msg.pid = htonl (ack);
3751 msg.tid = htonl (t->id.tid);
3753 send_prebuilt_message (&msg.header, peer, t);
3758 * Notify a the owner of a tunnel about how many more
3759 * payload packages will we accept on a given tunnel.
3761 * @param t Tunnel on which to send the ACK.
3764 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3769 "Sending client FWD ACK on tunnel %X\n",
3772 ack = tunnel_get_fwd_ack (t);
3774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3775 if (t->last_fwd_ack == ack)
3778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3779 t->last_fwd_ack = ack;
3780 send_local_ack (t, t->owner, ack);
3785 * Send an ACK informing the predecessor about the available buffer space.
3786 * In case there is no predecessor, inform the owning client.
3787 * If buffering is off, send only on behalf of children or self if endpoint.
3788 * If buffering is on, send when sent to children and buffer space is free.
3789 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3790 * the ACK itself goes "back" (towards root).
3792 * @param t Tunnel on which to send the ACK.
3793 * @param type Type of message that triggered the ACK transmission.
3796 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3798 struct GNUNET_PeerIdentity id;
3801 if (NULL != t->owner)
3803 tunnel_send_client_fwd_ack (t);
3806 /* Is it after unicast / multicast retransmission? */
3809 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3810 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3812 "ACK due to FWD DATA retransmission\n");
3813 if (GNUNET_YES == t->nobuffer)
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3819 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3820 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3826 /* Check if we need no retransmit the ACK */
3827 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3828 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3832 " t->qmax: %u, t->qn: %u\n",
3833 t->fwd_queue_max, t->fwd_queue_n);
3834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3835 " t->pid: %u, t->ack: %u\n",
3836 t->fwd_pid, t->last_fwd_ack);
3840 /* Ok, ACK might be necessary, what PID to ACK? */
3841 ack = tunnel_get_fwd_ack (t);
3843 /* If speed_min and not all children have ack'd, dont send yet */
3844 if (ack == t->last_fwd_ack)
3846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3850 t->last_fwd_ack = ack;
3851 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3852 send_ack (t, &id, ack);
3858 * Iterator to send a child node a BCK ACK to allow him to send more
3861 * @param cls Closure (tunnel).
3862 * @param id Id of the child node.
3865 tunnel_send_child_bck_ack (void *cls,
3868 struct MeshTunnel *t = cls;
3869 struct MeshTunnelChildInfo *cinfo;
3870 struct GNUNET_PeerIdentity peer;
3872 GNUNET_PEER_resolve (id, &peer);
3873 cinfo = tunnel_get_neighbor_fc (t, &peer);
3875 if (cinfo->bck_ack != cinfo->pid &&
3876 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3880 send_ack (t, &peer, cinfo->bck_ack);
3885 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3887 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3889 * @param t Tunnel on which to send the BCK ACKs.
3892 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3895 unsigned int tunnel_delta;
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3899 tunnel_delta = t->bck_ack - t->bck_pid;
3900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3902 /* Find client whom to allow to send to origin (with lowest buffer space) */
3903 for (i = 0; i < t->nclients; i++)
3905 struct MeshTunnelClientInfo *clinfo;
3908 clinfo = &t->clients_fc[i];
3909 delta = clinfo->bck_ack - clinfo->bck_pid;
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3911 t->clients[i]->id, delta);
3913 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3914 (GNUNET_YES == t->nobuffer && 0 == delta))
3918 ack = clinfo->bck_pid;
3919 ack += t->nobuffer ? 1 : tunnel_delta;
3920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3921 " sending ack to client %u: %u\n",
3922 t->clients[i]->id, ack);
3923 send_local_ack (t, t->clients[i], ack);
3924 clinfo->bck_ack = ack;
3931 * Send an ACK informing the children nodes and destination clients about
3932 * the available buffer space.
3933 * If buffering is off, send only on behalf of root (can be self).
3934 * If buffering is on, send when sent to predecessor and buffer space is free.
3935 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3936 * the ACK itself goes "forward" (towards children/clients).
3938 * @param t Tunnel on which to send the ACK.
3939 * @param type Type of message that triggered the ACK transmission.
3942 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3945 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3946 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3947 /* Is it after data to_origin retransmission? */
3950 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3951 if (GNUNET_YES == t->nobuffer)
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3954 " Not sending ACK, nobuffer\n");
3958 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3959 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3965 tunnel_send_clients_bck_ack (t);
3966 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3971 * @brief Re-initiate traffic to this peer if necessary.
3973 * Check if there is traffic queued towards this peer
3974 * and the core transmit handle is NULL (traffic was stalled).
3975 * If so, call core tmt rdy.
3977 * @param cls Closure (unused)
3978 * @param peer_id Short ID of peer to which initiate traffic.
3981 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3983 struct MeshPeerInfo *peer;
3984 struct GNUNET_PeerIdentity id;
3985 struct MeshPeerQueue *q;
3988 peer = peer_info_get_short(peer_id);
3989 if (NULL != peer->core_transmit)
3992 q = queue_get_next(peer);
3995 /* Might br multicast traffic already sent to this particular peer but
3996 * not to other children in this tunnel.
3997 * This way t->queue_n would be > 0 but the queue of this particular peer
4003 GNUNET_PEER_resolve (peer->id, &id);
4004 peer->core_transmit =
4005 GNUNET_CORE_notify_transmit_ready(core_handle,
4008 GNUNET_TIME_UNIT_FOREVER_REL,
4018 * @brief Allow transmission of FWD traffic on this tunnel
4020 * Check if there is traffic queued towards any children
4021 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4023 * @param t Tunnel on which to unlock FWD traffic.
4026 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4028 if (0 == t->fwd_queue_n)
4031 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4036 * @brief Allow transmission of BCK traffic on this tunnel
4038 * Check if there is traffic queued towards the root of the tree
4039 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4041 * @param t Tunnel on which to unlock BCK traffic.
4044 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4046 if (0 == t->bck_queue_n)
4049 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4054 * Send a message to all peers in this tunnel that the tunnel is no longer
4057 * @param t The tunnel whose peers to notify.
4060 tunnel_send_destroy (struct MeshTunnel *t)
4062 struct GNUNET_MESH_TunnelDestroy msg;
4064 msg.header.size = htons (sizeof (msg));
4065 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4066 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4067 msg.tid = htonl (t->id.tid);
4068 tunnel_send_multicast (t, &msg.header);
4073 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4075 * @param cls Closure (Tunnel which to cancel).
4076 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4079 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4081 struct MeshTunnel *t = cls;
4082 struct MeshPeerInfo *peer_info;
4083 struct MeshPeerQueue *pq;
4084 struct MeshPeerQueue *next;
4086 peer_info = peer_info_get_short (neighbor_id);
4087 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4090 if (pq->tunnel == t)
4092 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4093 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4094 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4096 // Should have been removed on destroy children
4099 queue_destroy (pq, GNUNET_YES);
4102 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4104 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4105 peer_info->core_transmit = NULL;
4110 * Destroy the tunnel and free any allocated resources linked to it.
4112 * @param t the tunnel to destroy
4114 * @return GNUNET_OK on success
4117 tunnel_destroy (struct MeshTunnel *t)
4119 struct MeshClient *c;
4120 struct GNUNET_HashCode hash;
4131 struct GNUNET_PeerIdentity id;
4133 GNUNET_PEER_resolve (t->id.oid, &id);
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4135 GNUNET_i2s (&id), t->id.tid);
4137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4141 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4142 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4147 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4150 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4154 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4155 for (i = 0; i < t->nclients; i++)
4159 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4164 for (i = 0; i < t->nignore; i++)
4168 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4173 if (t->nclients > 0)
4176 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4180 GNUNET_free (t->clients);
4182 if (NULL != t->peers)
4184 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4186 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4189 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4190 &tunnel_destroy_child,
4192 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4194 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4196 tree_destroy (t->tree);
4198 if (NULL != t->regex_ctx)
4199 regex_cancel_search (t->regex_ctx);
4200 if (NULL != t->dht_get_type)
4201 GNUNET_DHT_get_stop (t->dht_get_type);
4202 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4203 GNUNET_SCHEDULER_cancel (t->timeout_task);
4204 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4205 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4208 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4209 GNUNET_assert (0 <= n_tunnels);
4216 * Create a new tunnel
4218 * @param owner Who is the owner of the tunnel (short ID).
4219 * @param tid Tunnel Number of the tunnel.
4220 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4221 * @param local Tunnel Number for the tunnel, for the client point of view.
4223 * @return A new initialized tunnel. NULL on error.
4225 static struct MeshTunnel *
4226 tunnel_new (GNUNET_PEER_Id owner,
4227 MESH_TunnelNumber tid,
4228 struct MeshClient *client,
4229 MESH_TunnelNumber local)
4231 struct MeshTunnel *t;
4232 struct GNUNET_HashCode hash;
4234 if (n_tunnels >= max_tunnels && NULL == client)
4237 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4240 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4241 t->bck_queue_max = t->fwd_queue_max;
4242 t->tree = tree_new (owner);
4244 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4245 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4246 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4247 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4248 t->local_tid = local;
4249 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4251 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4253 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4255 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4263 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4270 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4272 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4273 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4277 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4287 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4288 * that are no longer needed, as well as nodes of no longer reachable peers.
4289 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4291 * @param t Tunnel from which to remove the path.
4292 * @param peer Short id of the peer which should be removed.
4295 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4297 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4303 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4304 * client when the client disconnects. If the client is not the owner, the
4305 * owner will get notified if no more clients are in the tunnel and the client
4306 * get removed from the tunnel's list.
4308 * @param cls closure (client that is disconnecting)
4309 * @param key the hash of the local tunnel id (used to access the hashmap)
4310 * @param value the value stored at the key (tunnel to destroy)
4312 * @return GNUNET_OK on success
4315 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4317 struct MeshTunnel *t = value;
4318 struct MeshClient *c = cls;
4321 send_client_tunnel_disconnect(t, c);
4324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4325 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4326 tunnel_delete_client(t, c);
4327 client_delete_tunnel(c, t);
4330 tunnel_send_destroy(t);
4331 r = tunnel_destroy (t);
4337 * Timeout function, destroys tunnel if called
4339 * @param cls Closure (tunnel to destroy).
4340 * @param tc TaskContext
4343 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4345 struct MeshTunnel *t = cls;
4346 struct GNUNET_PeerIdentity id;
4348 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4349 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4351 GNUNET_PEER_resolve(t->id.oid, &id);
4352 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4353 "Tunnel %s [%X] timed out. Destroying.\n",
4354 GNUNET_i2s(&id), t->id.tid);
4359 * Resets the tunnel timeout. Starts it if no timeout was running.
4361 * @param t Tunnel whose timeout to reset.
4363 * TODO use heap to improve efficiency of scheduler.
4366 tunnel_reset_timeout (struct MeshTunnel *t)
4368 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4369 GNUNET_SCHEDULER_cancel (t->timeout_task);
4371 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4372 (refresh_path_time, 4), &tunnel_timeout, t);
4376 /******************************************************************************/
4377 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4378 /******************************************************************************/
4381 * Function to send a create path packet to a peer.
4383 * @param cls closure
4384 * @param size number of bytes available in buf
4385 * @param buf where the callee should write the message
4386 * @return number of bytes written to buf
4389 send_core_path_create (void *cls, size_t size, void *buf)
4391 struct MeshPathInfo *info = cls;
4392 struct GNUNET_MESH_ManipulatePath *msg;
4393 struct GNUNET_PeerIdentity *peer_ptr;
4394 struct MeshTunnel *t = info->t;
4395 struct MeshPeerPath *p = info->path;
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4402 sizeof (struct GNUNET_MESH_ManipulatePath) +
4403 p->length * sizeof (struct GNUNET_PeerIdentity);
4405 if (size < size_needed || NULL == buf)
4410 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4411 msg->header.size = htons (size_needed);
4412 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4413 msg->tid = ntohl (t->id.tid);
4416 if (GNUNET_YES == t->speed_min)
4417 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4418 if (GNUNET_YES == t->nobuffer)
4419 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4420 msg->opt = htonl(opt);
4422 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4423 for (i = 0; i < p->length; i++)
4425 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4432 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4438 * Fill the core buffer
4440 * @param cls closure (data itself)
4441 * @param size number of bytes available in buf
4442 * @param buf where the callee should write the message
4444 * @return number of bytes written to buf
4447 send_core_data_multicast (void *cls, size_t size, void *buf)
4449 struct MeshTransmissionDescriptor *info = cls;
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4453 GNUNET_assert (NULL != info);
4454 GNUNET_assert (NULL != info->peer);
4455 total_size = info->mesh_data->data_len;
4456 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4458 if (total_size > size)
4463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4464 memcpy (buf, info->mesh_data->data, total_size);
4467 struct GNUNET_MESH_Multicast *mc;
4468 struct GNUNET_MessageHeader *mh;
4471 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4473 mc = (struct GNUNET_MESH_Multicast *) mh;
4474 mh = (struct GNUNET_MessageHeader *) &mc[1];
4475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4476 " multicast, payload type %s\n",
4477 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4479 " multicast, payload size %u\n", ntohs (mh->size));
4483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4484 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4488 data_descriptor_decrement_rc (info->mesh_data);
4489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4497 * Creates a path ack message in buf and frees all unused resources.
4499 * @param cls closure (MeshTransmissionDescriptor)
4500 * @param size number of bytes available in buf
4501 * @param buf where the callee should write the message
4502 * @return number of bytes written to buf
4505 send_core_path_ack (void *cls, size_t size, void *buf)
4507 struct MeshTransmissionDescriptor *info = cls;
4508 struct GNUNET_MESH_PathACK *msg = buf;
4510 GNUNET_assert (NULL != info);
4511 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4516 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4517 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4518 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4519 msg->tid = htonl (info->origin->tid);
4520 msg->peer_id = my_full_id;
4523 /* TODO add signature */
4525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4526 return sizeof (struct GNUNET_MESH_PathACK);
4531 * Free a transmission that was already queued with all resources
4532 * associated to the request.
4534 * @param queue Queue handler to cancel.
4535 * @param clear_cls Is it necessary to free associated cls?
4538 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4540 struct MeshTransmissionDescriptor *dd;
4541 struct MeshPathInfo *path_info;
4543 if (GNUNET_YES == clear_cls)
4545 switch (queue->type)
4547 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4548 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4549 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4552 data_descriptor_decrement_rc (dd->mesh_data);
4554 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4556 path_info = queue->cls;
4557 path_destroy (path_info->path);
4561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4562 " type %s unknown!\n",
4563 GNUNET_MESH_DEBUG_M2S(queue->type));
4565 GNUNET_free_non_null (queue->cls);
4567 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4568 queue->peer->queue_tail,
4570 GNUNET_free (queue);
4575 * @brief Get the next transmittable message from the queue.
4577 * This will be the head, except in the case of being a data packet
4578 * not allowed by the destination peer.
4580 * @param peer Destination peer.
4582 * @return The next viable MeshPeerQueue element to send to that peer.
4583 * NULL when there are no transmittable messages.
4585 struct MeshPeerQueue *
4586 queue_get_next (const struct MeshPeerInfo *peer)
4588 struct MeshPeerQueue *q;
4589 struct MeshTunnel *t;
4590 struct MeshTransmissionDescriptor *info;
4591 struct MeshTunnelChildInfo *cinfo;
4592 struct GNUNET_MESH_Unicast *ucast;
4593 struct GNUNET_MESH_ToOrigin *to_orig;
4594 struct GNUNET_MESH_Multicast *mcast;
4595 struct GNUNET_PeerIdentity id;
4599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4600 for (q = peer->queue_head; NULL != q; q = q->next)
4604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4606 GNUNET_MESH_DEBUG_M2S(q->type));
4609 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4610 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4611 pid = ntohl (ucast->pid);
4612 GNUNET_PEER_resolve (info->peer->id, &id);
4613 cinfo = tunnel_get_neighbor_fc(t, &id);
4614 ack = cinfo->fwd_ack;
4616 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4617 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4618 pid = ntohl (to_orig->pid);
4621 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4622 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4623 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4625 // Not a multicast payload: multicast control traffic (destroy, etc)
4628 pid = ntohl (mcast->pid);
4629 GNUNET_PEER_resolve (info->peer->id, &id);
4630 cinfo = tunnel_get_neighbor_fc(t, &id);
4631 ack = cinfo->fwd_ack;
4634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4639 "********* ACK: %u, PID: %u\n",
4641 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4650 "********* NEXT!\n");
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4654 "********* nothing found\n");
4660 * Core callback to write a queued packet to core buffer
4662 * @param cls Closure (peer info).
4663 * @param size Number of bytes available in buf.
4664 * @param buf Where the to write the message.
4666 * @return number of bytes written to buf
4669 queue_send (void *cls, size_t size, void *buf)
4671 struct MeshPeerInfo *peer = cls;
4672 struct GNUNET_MessageHeader *msg;
4673 struct MeshPeerQueue *queue;
4674 struct MeshTunnel *t;
4675 struct MeshTunnelChildInfo *cinfo;
4676 struct GNUNET_PeerIdentity dst_id;
4679 peer->core_transmit = NULL;
4681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4682 queue = queue_get_next (peer);
4684 /* Queue has no internal mesh traffic nor sendable payload */
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4688 if (NULL == peer->queue_head)
4689 GNUNET_break (0); // Should've been canceled
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4694 GNUNET_PEER_resolve (peer->id, &dst_id);
4695 /* Check if buffer size is enough for the message */
4696 if (queue->size > size)
4700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4701 "********* not enough room, reissue\n");
4702 peer->core_transmit =
4703 GNUNET_CORE_notify_transmit_ready(core_handle,
4706 GNUNET_TIME_UNIT_FOREVER_REL,
4713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4716 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4720 "********* unicast: t->q (%u/%u)\n",
4721 t->fwd_queue_n, t->fwd_queue_max);
4723 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4730 switch (queue->type)
4733 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4734 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4735 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4737 "********* raw: %s\n",
4738 GNUNET_MESH_DEBUG_M2S (queue->type));
4740 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4741 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4742 data_size = send_core_data_raw (queue->cls, size, buf);
4743 msg = (struct GNUNET_MessageHeader *) buf;
4744 switch (ntohs (msg->type)) // Type of preconstructed message
4746 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4747 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4749 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4750 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4756 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4759 struct MeshTransmissionDescriptor *info = queue->cls;
4761 if ((1 == info->mesh_data->reference_counter
4762 && GNUNET_YES == t->speed_min)
4764 (info->mesh_data->total_out == info->mesh_data->reference_counter
4765 && GNUNET_NO == t->speed_min))
4767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4768 "********* considered sent\n");
4772 data_size = send_core_data_multicast(queue->cls, size, buf);
4773 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4775 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4777 data_size = send_core_path_create (queue->cls, size, buf);
4779 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4781 data_size = send_core_path_ack (queue->cls, size, buf);
4783 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4785 data_size = send_core_data_multicast (queue->cls, size, buf);
4789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4790 "********* type unknown: %u\n",
4794 switch (queue->type)
4796 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4797 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4798 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4799 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4800 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4803 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4804 "at pos %u (%p) != %p\n",
4805 cinfo->send_buffer_start,
4806 cinfo->send_buffer[cinfo->send_buffer_start],
4809 if (cinfo->send_buffer_n > 0) {
4810 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4811 cinfo->send_buffer_n--;
4812 cinfo->send_buffer_start++;
4813 cinfo->send_buffer_start %= t->fwd_queue_max;
4824 /* Free queue, but cls was freed by send_core_* */
4825 queue_destroy (queue, GNUNET_NO);
4827 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4833 /* If more data in queue, send next */
4834 queue = queue_get_next(peer);
4837 struct GNUNET_PeerIdentity id;
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4840 GNUNET_PEER_resolve (peer->id, &id);
4841 peer->core_transmit =
4842 GNUNET_CORE_notify_transmit_ready(core_handle,
4845 GNUNET_TIME_UNIT_FOREVER_REL,
4853 if (NULL != peer->queue_head)
4854 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4855 "********* %s stalled\n",
4856 GNUNET_i2s(&my_full_id));
4858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4864 * @brief Queue and pass message to core when possible.
4866 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4867 * and accounts for it. In case the queue is full, the message is dropped and
4870 * Otherwise, message is treated as internal and allowed to go regardless of
4873 * @param cls Closure (@c type dependant). It will be used by queue_send to
4874 * build the message to be sent if not already prebuilt.
4875 * @param type Type of the message, 0 for a raw message.
4876 * @param size Size of the message.
4877 * @param dst Neighbor to send message to.
4878 * @param t Tunnel this message belongs to.
4881 queue_add (void *cls, uint16_t type, size_t size,
4882 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4884 struct MeshPeerQueue *queue;
4885 struct MeshTunnelChildInfo *cinfo;
4886 struct GNUNET_PeerIdentity id;
4892 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4893 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4895 n = &t->fwd_queue_n;
4896 max = &t->fwd_queue_max;
4898 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4900 n = &t->bck_queue_n;
4901 max = &t->bck_queue_max;
4906 if (NULL == t->owner)
4907 GNUNET_break_op(0); // TODO: kill connection?
4910 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4912 return; // Drop message
4916 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4922 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4923 GNUNET_PEER_resolve (dst->id, &id);
4924 if (NULL == dst->core_transmit)
4926 dst->core_transmit =
4927 GNUNET_CORE_notify_transmit_ready(core_handle,
4930 GNUNET_TIME_UNIT_FOREVER_REL,
4936 if (NULL == n) // Is this internal mesh traffic?
4939 // It's payload, keep track of buffer per peer.
4940 cinfo = tunnel_get_neighbor_fc(t, &id);
4941 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4942 if (NULL != cinfo->send_buffer[i])
4944 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4945 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4946 cinfo->send_buffer_start++;
4947 cinfo->send_buffer_start %= t->fwd_queue_max;
4948 cinfo->send_buffer_n--;
4950 cinfo->send_buffer[i] = queue;
4951 cinfo->send_buffer_n++;
4952 if (cinfo->send_buffer_n > t->fwd_queue_max)
4955 cinfo->send_buffer_n = t->fwd_queue_max;
4960 /******************************************************************************/
4961 /******************** MESH NETWORK HANDLERS **************************/
4962 /******************************************************************************/
4966 * Core handler for path creation
4968 * @param cls closure
4969 * @param message message
4970 * @param peer peer identity this notification is about
4971 * @param atsi performance data
4972 * @param atsi_count number of records in 'atsi'
4974 * @return GNUNET_OK to keep the connection open,
4975 * GNUNET_SYSERR to close it (signal serious error)
4978 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4979 const struct GNUNET_MessageHeader *message,
4980 const struct GNUNET_ATS_Information *atsi,
4981 unsigned int atsi_count)
4983 unsigned int own_pos;
4986 MESH_TunnelNumber tid;
4987 struct GNUNET_MESH_ManipulatePath *msg;
4988 struct GNUNET_PeerIdentity *pi;
4989 struct GNUNET_HashCode hash;
4990 struct MeshPeerPath *path;
4991 struct MeshPeerInfo *dest_peer_info;
4992 struct MeshPeerInfo *orig_peer_info;
4993 struct MeshTunnel *t;
4995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4996 "Received a path create msg [%s]\n",
4997 GNUNET_i2s (&my_full_id));
4998 size = ntohs (message->size);
4999 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5001 GNUNET_break_op (0);
5005 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5006 if (size % sizeof (struct GNUNET_PeerIdentity))
5008 GNUNET_break_op (0);
5011 size /= sizeof (struct GNUNET_PeerIdentity);
5014 GNUNET_break_op (0);
5017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5018 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5020 tid = ntohl (msg->tid);
5021 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5023 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5024 t = tunnel_get (pi, tid);
5025 if (NULL == t) // FIXME only for INCOMING tunnels?
5029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5030 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5033 // FIXME notify failure
5036 opt = ntohl (msg->opt);
5037 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5038 GNUNET_YES : GNUNET_NO;
5039 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
5040 GNUNET_YES : GNUNET_NO;
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5042 " speed_min: %d, nobuffer:%d\n",
5043 t->speed_min, t->nobuffer);
5045 if (GNUNET_YES == t->nobuffer)
5047 t->bck_queue_max = 1;
5048 t->fwd_queue_max = 1;
5051 // FIXME only assign a local tid if a local client is interested (on demand)
5052 while (NULL != tunnel_get_incoming (next_local_tid))
5053 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5054 t->local_tid_dest = next_local_tid++;
5055 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5058 tunnel_reset_timeout (t);
5059 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5061 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5062 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5070 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5071 if (NULL == dest_peer_info)
5073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5074 " Creating PeerInfo for destination.\n");
5075 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5076 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5077 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5079 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5081 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5082 if (NULL == orig_peer_info)
5084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5085 " Creating PeerInfo for origin.\n");
5086 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5087 orig_peer_info->id = GNUNET_PEER_intern (pi);
5088 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5089 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5092 path = path_new (size);
5094 for (i = 0; i < size; i++)
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5097 GNUNET_i2s (&pi[i]));
5098 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5099 if (path->peers[i] == myid)
5102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5105 /* cannot be self, must be 'not found' */
5106 /* create path: self not found in path through self */
5107 GNUNET_break_op (0);
5108 path_destroy (path);
5112 path_add_to_peers (path, GNUNET_NO);
5113 tunnel_add_path (t, path, own_pos);
5114 if (own_pos == size - 1)
5116 /* It is for us! Send ack. */
5117 struct MeshTransmissionDescriptor *info;
5119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5120 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5121 if (NULL == t->peers)
5123 /* New tunnel! Notify clients on data. */
5124 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5126 GNUNET_break (GNUNET_SYSERR !=
5127 GNUNET_CONTAINER_multihashmap_put (t->peers,
5128 &my_full_id.hashPubKey,
5131 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5132 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5133 info->origin = &t->id;
5134 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5135 GNUNET_assert (NULL != info->peer);
5136 // FIXME don't use queue_add directly, abstact in a send_message_* funcion
5138 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5139 sizeof (struct GNUNET_MESH_PathACK),
5145 struct MeshPeerPath *path2;
5147 /* It's for somebody else! Retransmit. */
5148 path2 = path_duplicate (path);
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5150 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5151 path2 = path_duplicate (path);
5152 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5153 send_create_path (dest_peer_info, path, t);
5160 * Core handler for path destruction
5162 * @param cls closure
5163 * @param message message
5164 * @param peer peer identity this notification is about
5165 * @param atsi performance data
5166 * @param atsi_count number of records in 'atsi'
5168 * @return GNUNET_OK to keep the connection open,
5169 * GNUNET_SYSERR to close it (signal serious error)
5172 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5173 const struct GNUNET_MessageHeader *message,
5174 const struct GNUNET_ATS_Information *atsi,
5175 unsigned int atsi_count)
5177 struct GNUNET_MESH_ManipulatePath *msg;
5178 struct GNUNET_PeerIdentity *pi;
5179 struct MeshPeerPath *path;
5180 struct MeshTunnel *t;
5181 unsigned int own_pos;
5185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5186 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5187 size = ntohs (message->size);
5188 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5190 GNUNET_break_op (0);
5194 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5195 if (size % sizeof (struct GNUNET_PeerIdentity))
5197 GNUNET_break_op (0);
5200 size /= sizeof (struct GNUNET_PeerIdentity);
5203 GNUNET_break_op (0);
5206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5208 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5209 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5211 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5213 t = tunnel_get (pi, ntohl (msg->tid));
5216 /* TODO notify back: we don't know this tunnel */
5217 GNUNET_break_op (0);
5220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5221 path = path_new (size);
5223 for (i = 0; i < size; i++)
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5226 GNUNET_i2s (&pi[i]));
5227 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5228 if (path->peers[i] == myid)
5231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5232 if (own_pos < path->length - 1)
5233 send_prebuilt_message (message, &pi[own_pos + 1], t);
5235 send_client_tunnel_disconnect(t, NULL);
5237 tunnel_delete_peer (t, path->peers[path->length - 1]);
5238 path_destroy (path);
5244 * Core handler for notifications of broken paths
5246 * @param cls closure
5247 * @param message message
5248 * @param peer peer identity this notification is about
5249 * @param atsi performance data
5250 * @param atsi_count number of records in 'atsi'
5252 * @return GNUNET_OK to keep the connection open,
5253 * GNUNET_SYSERR to close it (signal serious error)
5256 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5257 const struct GNUNET_MessageHeader *message,
5258 const struct GNUNET_ATS_Information *atsi,
5259 unsigned int atsi_count)
5261 struct GNUNET_MESH_PathBroken *msg;
5262 struct MeshTunnel *t;
5264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5265 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5266 msg = (struct GNUNET_MESH_PathBroken *) message;
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5268 GNUNET_i2s (&msg->peer1));
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5270 GNUNET_i2s (&msg->peer2));
5271 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5274 GNUNET_break_op (0);
5277 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5278 GNUNET_PEER_search (&msg->peer2));
5285 * Core handler for tunnel destruction
5287 * @param cls closure
5288 * @param message message
5289 * @param peer peer identity this notification is about
5290 * @param atsi performance data
5291 * @param atsi_count number of records in 'atsi'
5293 * @return GNUNET_OK to keep the connection open,
5294 * GNUNET_SYSERR to close it (signal serious error)
5297 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5298 const struct GNUNET_MessageHeader *message,
5299 const struct GNUNET_ATS_Information *atsi,
5300 unsigned int atsi_count)
5302 struct GNUNET_MESH_TunnelDestroy *msg;
5303 struct MeshTunnel *t;
5305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5306 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5307 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5309 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5310 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5313 /* Probably already got the message from another path,
5314 * destroyed the tunnel and retransmitted to children.
5317 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5320 if (t->id.oid == myid)
5322 GNUNET_break_op (0);
5325 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5327 /* Tunnel was incoming, notify clients */
5328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5329 t->local_tid, t->local_tid_dest);
5330 send_clients_tunnel_destroy (t);
5332 tunnel_send_destroy (t);
5333 t->destroy = GNUNET_YES;
5334 // TODO: add timeout to destroy the tunnel anyway
5340 * Core handler for mesh network traffic going from the origin to a peer
5342 * @param cls closure
5343 * @param peer peer identity this notification is about
5344 * @param message message
5345 * @param atsi performance data
5346 * @param atsi_count number of records in 'atsi'
5347 * @return GNUNET_OK to keep the connection open,
5348 * GNUNET_SYSERR to close it (signal serious error)
5351 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5352 const struct GNUNET_MessageHeader *message,
5353 const struct GNUNET_ATS_Information *atsi,
5354 unsigned int atsi_count)
5356 struct GNUNET_MESH_Unicast *msg;
5357 struct GNUNET_PeerIdentity *neighbor;
5358 struct MeshTunnelChildInfo *cinfo;
5359 struct MeshTunnel *t;
5360 GNUNET_PEER_Id dest_id;
5365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5368 size = ntohs (message->size);
5370 sizeof (struct GNUNET_MESH_Unicast) +
5371 sizeof (struct GNUNET_MessageHeader))
5376 msg = (struct GNUNET_MESH_Unicast *) message;
5377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5378 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5380 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5383 /* TODO notify back: we don't know this tunnel */
5384 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5385 GNUNET_break_op (0);
5388 pid = ntohl (msg->pid);
5389 if (t->fwd_pid == pid)
5391 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5392 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5393 " Already seen pid %u, DROPPING!\n", pid);
5398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5399 " pid %u not seen yet, forwarding\n", pid);
5402 t->skip += (pid - t->fwd_pid) - 1;
5405 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5407 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5408 GNUNET_break_op (0);
5409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5410 "Received PID %u, ACK %u\n",
5411 pid, t->last_fwd_ack);
5415 tunnel_reset_timeout (t);
5416 dest_id = GNUNET_PEER_search (&msg->destination);
5417 if (dest_id == myid)
5419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5420 " it's for us! sending to clients...\n");
5421 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5422 send_subscribed_clients (message, &msg[1].header, t);
5423 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5426 ttl = ntohl (msg->ttl);
5427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5430 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5432 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5436 " not for us, retransmitting...\n");
5438 neighbor = tree_get_first_hop (t->tree, dest_id);
5439 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5441 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5444 if (GNUNET_YES == t->nobuffer &&
5445 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5447 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5448 GNUNET_break_op (0);
5451 send_prebuilt_message (message, neighbor, t);
5452 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5458 * Core handler for mesh network traffic going from the origin to all peers
5460 * @param cls closure
5461 * @param message message
5462 * @param peer peer identity this notification is about
5463 * @param atsi performance data
5464 * @param atsi_count number of records in 'atsi'
5465 * @return GNUNET_OK to keep the connection open,
5466 * GNUNET_SYSERR to close it (signal serious error)
5468 * TODO: Check who we got this from, to validate route.
5471 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5472 const struct GNUNET_MessageHeader *message,
5473 const struct GNUNET_ATS_Information *atsi,
5474 unsigned int atsi_count)
5476 struct GNUNET_MESH_Multicast *msg;
5477 struct MeshTunnel *t;
5481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5483 size = ntohs (message->size);
5484 if (sizeof (struct GNUNET_MESH_Multicast) +
5485 sizeof (struct GNUNET_MessageHeader) > size)
5487 GNUNET_break_op (0);
5490 msg = (struct GNUNET_MESH_Multicast *) message;
5491 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5495 /* TODO notify that we dont know that tunnel */
5496 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5497 GNUNET_break_op (0);
5500 pid = ntohl (msg->pid);
5501 if (t->fwd_pid == pid)
5503 /* already seen this packet, drop */
5504 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5506 " Already seen pid %u, DROPPING!\n", pid);
5507 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5513 " pid %u not seen yet, forwarding\n", pid);
5515 t->skip += (pid - t->fwd_pid) - 1;
5517 tunnel_reset_timeout (t);
5519 /* Transmit to locally interested clients */
5520 if (NULL != t->peers &&
5521 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5523 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5524 send_subscribed_clients (message, &msg[1].header, t);
5525 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5528 if (ntohl (msg->ttl) == 0)
5530 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5531 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5532 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5535 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5536 tunnel_send_multicast (t, message);
5542 * Core handler for mesh network traffic toward the owner of a tunnel
5544 * @param cls closure
5545 * @param message message
5546 * @param peer peer identity this notification is about
5547 * @param atsi performance data
5548 * @param atsi_count number of records in 'atsi'
5550 * @return GNUNET_OK to keep the connection open,
5551 * GNUNET_SYSERR to close it (signal serious error)
5554 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5555 const struct GNUNET_MessageHeader *message,
5556 const struct GNUNET_ATS_Information *atsi,
5557 unsigned int atsi_count)
5559 struct GNUNET_MESH_ToOrigin *msg;
5560 struct GNUNET_PeerIdentity id;
5561 struct MeshPeerInfo *peer_info;
5562 struct MeshTunnel *t;
5566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5568 size = ntohs (message->size);
5569 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5570 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5572 GNUNET_break_op (0);
5575 msg = (struct GNUNET_MESH_ToOrigin *) message;
5576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5577 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5578 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5582 /* TODO notify that we dont know this tunnel (whom)? */
5583 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5584 GNUNET_break_op (0);
5588 if (NULL != t->owner)
5591 struct GNUNET_MESH_ToOrigin *copy;
5593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5594 " it's for us! sending to clients...\n");
5595 /* TODO signature verification */
5596 memcpy (cbuf, message, size);
5597 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5598 copy->tid = htonl (t->local_tid);
5599 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5600 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5601 ©->header, GNUNET_NO);
5602 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5606 " not for us, retransmitting...\n");
5608 peer_info = peer_info_get (&msg->oid);
5609 if (NULL == peer_info)
5611 /* unknown origin of tunnel */
5615 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5616 send_prebuilt_message (message, &id, t);
5617 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5624 * Core handler for mesh network traffic point-to-point acks.
5626 * @param cls closure
5627 * @param message message
5628 * @param peer peer identity this notification is about
5629 * @param atsi performance data
5630 * @param atsi_count number of records in 'atsi'
5632 * @return GNUNET_OK to keep the connection open,
5633 * GNUNET_SYSERR to close it (signal serious error)
5636 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5637 const struct GNUNET_MessageHeader *message,
5638 const struct GNUNET_ATS_Information *atsi,
5639 unsigned int atsi_count)
5641 struct GNUNET_MESH_ACK *msg;
5642 struct MeshTunnel *t;
5645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5647 msg = (struct GNUNET_MESH_ACK *) message;
5649 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5653 /* TODO notify that we dont know this tunnel (whom)? */
5654 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5655 GNUNET_break_op (0);
5658 ack = ntohl (msg->pid);
5660 /* Is this a forward or backward ACK? */
5661 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5663 struct MeshTunnelChildInfo *cinfo;
5666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5667 cinfo = tunnel_get_neighbor_fc (t, peer);
5668 cinfo->fwd_ack = ack;
5669 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5670 tunnel_unlock_fwd_queues (t);
5674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5676 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5677 tunnel_unlock_bck_queue (t);
5684 * Core handler for path ACKs
5686 * @param cls closure
5687 * @param message message
5688 * @param peer peer identity this notification is about
5689 * @param atsi performance data
5690 * @param atsi_count number of records in 'atsi'
5692 * @return GNUNET_OK to keep the connection open,
5693 * GNUNET_SYSERR to close it (signal serious error)
5696 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5697 const struct GNUNET_MessageHeader *message,
5698 const struct GNUNET_ATS_Information *atsi,
5699 unsigned int atsi_count)
5701 struct GNUNET_MESH_PathACK *msg;
5702 struct GNUNET_PeerIdentity id;
5703 struct MeshPeerInfo *peer_info;
5704 struct MeshPeerPath *p;
5705 struct MeshTunnel *t;
5707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5708 GNUNET_i2s (&my_full_id));
5709 msg = (struct GNUNET_MESH_PathACK *) message;
5710 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5713 /* TODO notify that we don't know the tunnel */
5714 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5716 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5720 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5722 peer_info = peer_info_get (&msg->peer_id);
5723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5724 GNUNET_i2s (&msg->peer_id));
5725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5728 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5731 "connect_by_string completed, stopping search\n");
5732 regex_cancel_search (t->regex_ctx);
5733 t->regex_ctx = NULL;
5736 /* Add paths to peers? */
5737 p = tree_get_path_to_peer (t->tree, peer_info->id);
5740 path_add_to_peers (p, GNUNET_YES);
5748 /* Message for us? */
5749 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5752 if (NULL == t->owner)
5754 GNUNET_break_op (0);
5757 if (NULL != t->dht_get_type)
5759 GNUNET_DHT_get_stop (t->dht_get_type);
5760 t->dht_get_type = NULL;
5762 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5764 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5765 send_client_peer_connected (t, peer_info->id);
5770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5771 " not for us, retransmitting...\n");
5772 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5773 peer_info = peer_info_get (&msg->oid);
5774 if (NULL == peer_info)
5776 /* If we know the tunnel, we should DEFINITELY know the peer */
5780 send_prebuilt_message (message, &id, t);
5786 * Core handler for mesh keepalives.
5788 * @param cls closure
5789 * @param message message
5790 * @param peer peer identity this notification is about
5791 * @param atsi performance data
5792 * @param atsi_count number of records in 'atsi'
5793 * @return GNUNET_OK to keep the connection open,
5794 * GNUNET_SYSERR to close it (signal serious error)
5796 * TODO: Check who we got this from, to validate route.
5799 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5800 const struct GNUNET_MessageHeader *message,
5801 const struct GNUNET_ATS_Information *atsi,
5802 unsigned int atsi_count)
5804 struct GNUNET_MESH_TunnelKeepAlive *msg;
5805 struct MeshTunnel *t;
5807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5810 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5811 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5815 /* TODO notify that we dont know that tunnel */
5816 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5817 GNUNET_break_op (0);
5821 tunnel_reset_timeout (t);
5823 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5824 tunnel_send_multicast (t, message);
5831 * Functions to handle messages from core
5833 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5834 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5835 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5836 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5837 sizeof (struct GNUNET_MESH_PathBroken)},
5838 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5839 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5840 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5841 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5842 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5843 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5844 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5845 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5846 sizeof (struct GNUNET_MESH_ACK)},
5847 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5848 sizeof (struct GNUNET_MESH_PathACK)},
5854 /******************************************************************************/
5855 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5856 /******************************************************************************/
5859 * deregister_app: iterator for removing each application registered by a client
5861 * @param cls closure
5862 * @param key the hash of the application id (used to access the hashmap)
5863 * @param value the value stored at the key (client)
5865 * @return GNUNET_OK on success
5868 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5870 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5871 GNUNET_break (GNUNET_YES ==
5872 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5878 * notify_client_connection_failure: notify a client that the connection to the
5879 * requested remote peer is not possible (for instance, no route found)
5880 * Function called when the socket is ready to queue more data. "buf" will be
5881 * NULL and "size" zero if the socket was closed for writing in the meantime.
5883 * @param cls closure
5884 * @param size number of bytes available in buf
5885 * @param buf where the callee should write the message
5886 * @return number of bytes written to buf
5889 notify_client_connection_failure (void *cls, size_t size, void *buf)
5892 struct MeshPeerInfo *peer_info;
5893 struct GNUNET_MESH_PeerControl *msg;
5894 struct GNUNET_PeerIdentity id;
5896 if (0 == size && NULL == buf)
5898 // TODO retry? cancel?
5902 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5903 peer_info = (struct MeshPeerInfo *) cls;
5904 msg = (struct GNUNET_MESH_PeerControl *) buf;
5905 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5906 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5907 // msg->tunnel_id = htonl(peer_info->t->tid);
5908 GNUNET_PEER_resolve (peer_info->id, &id);
5909 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5917 * Send keepalive packets for a peer
5919 * @param cls Closure (tunnel for which to send the keepalive).
5920 * @param tc Notification context.
5923 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5925 struct MeshTunnel *t = cls;
5926 struct GNUNET_MESH_TunnelKeepAlive *msg;
5927 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5930 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5931 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5937 "sending keepalive for tunnel %d\n", t->id.tid);
5939 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5940 msg->header.size = htons (size);
5941 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5942 msg->oid = my_full_id;
5943 msg->tid = htonl (t->id.tid);
5944 tunnel_send_multicast (t, &msg->header);
5946 t->path_refresh_task =
5947 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5948 tunnel_reset_timeout(t);
5953 * Function to process paths received for a new peer addition. The recorded
5954 * paths form the initial tunnel, which can be optimized later.
5955 * Called on each result obtained for the DHT search.
5957 * @param cls closure
5958 * @param exp when will this value expire
5959 * @param key key of the result
5960 * @param get_path path of the get request
5961 * @param get_path_length lenght of get_path
5962 * @param put_path path of the put request
5963 * @param put_path_length length of the put_path
5964 * @param type type of the result
5965 * @param size number of bytes in data
5966 * @param data pointer to the result data
5968 * TODO: re-issue the request after certain time? cancel after X results?
5971 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5972 const struct GNUNET_HashCode * key,
5973 const struct GNUNET_PeerIdentity *get_path,
5974 unsigned int get_path_length,
5975 const struct GNUNET_PeerIdentity *put_path,
5976 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5977 size_t size, const void *data)
5979 struct MeshPathInfo *path_info = cls;
5980 struct MeshPeerPath *p;
5981 struct GNUNET_PeerIdentity pi;
5984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5985 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5988 p = path_build_from_dht (get_path, get_path_length, put_path,
5990 path_add_to_peers (p, GNUNET_NO);
5992 for (i = 0; i < path_info->peer->ntunnels; i++)
5994 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5995 peer_info_connect (path_info->peer, path_info->t);
6003 * Function to process paths received for a new peer addition. The recorded
6004 * paths form the initial tunnel, which can be optimized later.
6005 * Called on each result obtained for the DHT search.
6007 * @param cls closure
6008 * @param exp when will this value expire
6009 * @param key key of the result
6010 * @param get_path path of the get request
6011 * @param get_path_length lenght of get_path
6012 * @param put_path path of the put request
6013 * @param put_path_length length of the put_path
6014 * @param type type of the result
6015 * @param size number of bytes in data
6016 * @param data pointer to the result data
6019 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6020 const struct GNUNET_HashCode * key,
6021 const struct GNUNET_PeerIdentity *get_path,
6022 unsigned int get_path_length,
6023 const struct GNUNET_PeerIdentity *put_path,
6024 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6025 size_t size, const void *data)
6027 const struct PBlock *pb = data;
6028 const struct GNUNET_PeerIdentity *pi = &pb->id;
6029 struct MeshTunnel *t = cls;
6030 struct MeshPeerInfo *peer_info;
6031 struct MeshPeerPath *p;
6033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6034 if (size != sizeof (struct PBlock))
6036 GNUNET_break_op (0);
6039 if (ntohl(pb->type) != t->type)
6041 GNUNET_break_op (0);
6044 GNUNET_assert (NULL != t->owner);
6045 peer_info = peer_info_get (pi);
6046 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6048 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6050 p = path_build_from_dht (get_path, get_path_length, put_path,
6052 path_add_to_peers (p, GNUNET_NO);
6054 tunnel_add_peer (t, peer_info);
6055 peer_info_connect (peer_info, t);
6060 * Function to process DHT string to regex matching.
6061 * Called on each result obtained for the DHT search.
6063 * @param cls closure (search context)
6064 * @param exp when will this value expire
6065 * @param key key of the result
6066 * @param get_path path of the get request (not used)
6067 * @param get_path_length lenght of get_path (not used)
6068 * @param put_path path of the put request (not used)
6069 * @param put_path_length length of the put_path (not used)
6070 * @param type type of the result
6071 * @param size number of bytes in data
6072 * @param data pointer to the result data
6075 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6076 const struct GNUNET_HashCode * key,
6077 const struct GNUNET_PeerIdentity *get_path,
6078 unsigned int get_path_length,
6079 const struct GNUNET_PeerIdentity *put_path,
6080 unsigned int put_path_length,
6081 enum GNUNET_BLOCK_Type type,
6082 size_t size, const void *data)
6084 const struct MeshRegexAccept *block = data;
6085 struct MeshRegexSearchContext *ctx = cls;
6086 struct MeshRegexSearchInfo *info = ctx->info;
6087 struct MeshPeerPath *p;
6088 struct MeshPeerInfo *peer_info;
6090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6093 peer_info = peer_info_get(&block->id);
6094 p = path_build_from_dht (get_path, get_path_length, put_path,
6096 path_add_to_peers (p, GNUNET_NO);
6099 tunnel_add_peer (info->t, peer_info);
6100 peer_info_connect (peer_info, info->t);
6101 if (0 == info->peer)
6103 info->peer = peer_info->id;
6107 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6110 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6111 ®ex_connect_timeout,
6119 * Function to process DHT string to regex matching.
6120 * Called on each result obtained for the DHT search.
6122 * @param cls closure (search context)
6123 * @param exp when will this value expire
6124 * @param key key of the result
6125 * @param get_path path of the get request (not used)
6126 * @param get_path_length lenght of get_path (not used)
6127 * @param put_path path of the put request (not used)
6128 * @param put_path_length length of the put_path (not used)
6129 * @param type type of the result
6130 * @param size number of bytes in data
6131 * @param data pointer to the result data
6133 * TODO: re-issue the request after certain time? cancel after X results?
6136 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6137 const struct GNUNET_HashCode * key,
6138 const struct GNUNET_PeerIdentity *get_path,
6139 unsigned int get_path_length,
6140 const struct GNUNET_PeerIdentity *put_path,
6141 unsigned int put_path_length,
6142 enum GNUNET_BLOCK_Type type,
6143 size_t size, const void *data)
6145 const struct MeshRegexBlock *block = data;
6146 struct MeshRegexSearchContext *ctx = cls;
6147 struct MeshRegexSearchInfo *info = ctx->info;
6151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6152 "DHT GET STRING RETURNED RESULTS\n");
6153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6154 " key: %s\n", GNUNET_h2s (key));
6156 copy = GNUNET_malloc (size);
6157 memcpy (copy, data, size);
6158 GNUNET_break (GNUNET_OK ==
6159 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6160 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6161 len = ntohl (block->n_proof);
6163 char proof[len + 1];
6165 memcpy (proof, &block[1], len);
6167 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6169 GNUNET_break_op (0);
6173 len = strlen (info->description);
6174 if (len == ctx->position) // String processed
6176 if (GNUNET_YES == ntohl (block->accepting))
6178 regex_find_path(key, ctx);
6182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6183 // FIXME REGEX this block not successful, wait for more? start timeout?
6188 regex_next_edge (block, size, ctx);
6193 /******************************************************************************/
6194 /********************* MESH LOCAL HANDLES **************************/
6195 /******************************************************************************/
6199 * Handler for client disconnection
6201 * @param cls closure
6202 * @param client identification of the client; NULL
6203 * for the last call when the server is destroyed
6206 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6208 struct MeshClient *c;
6209 struct MeshClient *next;
6212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6221 if (c->handle != client)
6223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6229 GNUNET_SERVER_client_drop (c->handle);
6230 c->shutting_down = GNUNET_YES;
6231 GNUNET_assert (NULL != c->own_tunnels);
6232 GNUNET_assert (NULL != c->incoming_tunnels);
6233 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6234 &tunnel_destroy_iterator, c);
6235 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6236 &tunnel_destroy_iterator, c);
6237 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6238 &tunnel_destroy_iterator, c);
6239 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6240 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6241 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6243 /* deregister clients applications */
6244 if (NULL != c->apps)
6246 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6247 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6249 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6250 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6252 GNUNET_SCHEDULER_cancel (announce_applications_task);
6253 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6255 if (NULL != c->types)
6256 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6257 for (i = 0; i < c->n_regex; i++)
6259 GNUNET_free (c->regexes[i]);
6261 GNUNET_free_non_null (c->regexes);
6262 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6263 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6265 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6268 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6277 * Handler for new clients
6279 * @param cls closure
6280 * @param client identification of the client
6281 * @param message the actual message, which includes messages the client wants
6284 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6285 const struct GNUNET_MessageHeader *message)
6287 struct GNUNET_MESH_ClientConnect *cc_msg;
6288 struct MeshClient *c;
6289 GNUNET_MESH_ApplicationType *a;
6296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6297 /* Check data sanity */
6298 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6299 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6300 ntypes = ntohs (cc_msg->types);
6301 napps = ntohs (cc_msg->applications);
6303 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6310 /* Create new client structure */
6311 c = GNUNET_malloc (sizeof (struct MeshClient));
6312 c->id = next_client_id++;
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6315 GNUNET_SERVER_client_keep (client);
6316 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6319 GNUNET_MESH_ApplicationType at;
6320 struct GNUNET_HashCode hc;
6322 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6323 for (i = 0; i < napps; i++)
6326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6327 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6328 /* store in clients hashmap */
6329 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6330 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6331 /* store in global hashmap, for announcements */
6332 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6335 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6336 announce_applications_task =
6337 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6343 struct GNUNET_HashCode hc;
6345 t = (uint16_t *) & a[napps];
6346 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6347 for (i = 0; i < ntypes; i++)
6350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6351 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6353 /* store in clients hashmap */
6354 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6355 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6356 /* store in global hashmap */
6357 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6358 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6362 " client has %u+%u subscriptions\n", napps, ntypes);
6364 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6365 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6366 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6367 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6368 GNUNET_SERVER_notification_context_add (nc, client);
6369 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6371 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6377 * Handler for clients announcing available services by a regular expression.
6379 * @param cls closure
6380 * @param client identification of the client
6381 * @param message the actual message, which includes messages the client wants
6384 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6385 const struct GNUNET_MessageHeader *message)
6387 struct MeshClient *c;
6391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6393 /* Sanity check for client registration */
6394 if (NULL == (c = client_get (client)))
6397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6402 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6403 regex = GNUNET_malloc (len + 1);
6404 memcpy (regex, &message[1], len);
6406 GNUNET_array_append (c->regexes, c->n_regex, regex);
6407 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6409 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6415 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6421 * Handler for requests of new tunnels
6423 * @param cls closure
6424 * @param client identification of the client
6425 * @param message the actual message
6428 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6429 const struct GNUNET_MessageHeader *message)
6431 struct GNUNET_MESH_TunnelMessage *t_msg;
6432 struct MeshTunnel *t;
6433 struct MeshClient *c;
6434 MESH_TunnelNumber tid;
6436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6438 /* Sanity check for client registration */
6439 if (NULL == (c = client_get (client)))
6442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6447 /* Message sanity check */
6448 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6455 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6456 /* Sanity check for tunnel numbering */
6457 tid = ntohl (t_msg->tunnel_id);
6458 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6464 /* Sanity check for duplicate tunnel IDs */
6465 if (NULL != tunnel_get_by_local_id (c, tid))
6468 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6472 while (NULL != tunnel_get_by_pi (myid, next_tid))
6473 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6474 t = tunnel_new (myid, next_tid++, c, tid);
6477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6482 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6484 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6485 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6488 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6494 * Handler for requests of deleting tunnels
6496 * @param cls closure
6497 * @param client identification of the client
6498 * @param message the actual message
6501 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6502 const struct GNUNET_MessageHeader *message)
6504 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6505 struct MeshClient *c;
6506 struct MeshTunnel *t;
6507 MESH_TunnelNumber tid;
6509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6510 "Got a DESTROY TUNNEL from client!\n");
6512 /* Sanity check for client registration */
6513 if (NULL == (c = client_get (client)))
6516 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6521 /* Message sanity check */
6522 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6525 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6529 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6531 /* Retrieve tunnel */
6532 tid = ntohl (tunnel_msg->tunnel_id);
6533 t = tunnel_get_by_local_id(c, tid);
6536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6541 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6543 client_ignore_tunnel (c, t);
6545 // TODO: when to destroy incoming tunnel?
6546 if (t->nclients == 0)
6548 GNUNET_assert (GNUNET_YES ==
6549 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6551 GNUNET_assert (GNUNET_YES ==
6552 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6553 &my_full_id.hashPubKey,
6557 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6560 send_client_tunnel_disconnect(t, c);
6561 client_delete_tunnel(c, t);
6563 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6565 tunnel_send_destroy (t);
6566 t->destroy = GNUNET_YES;
6567 // The tunnel will be destroyed when the last message is transmitted.
6568 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6574 * Handler for requests of seeting tunnel's speed.
6576 * @param cls Closure (unused).
6577 * @param client Identification of the client.
6578 * @param message The actual message.
6581 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6582 const struct GNUNET_MessageHeader *message)
6584 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6585 struct MeshClient *c;
6586 struct MeshTunnel *t;
6587 MESH_TunnelNumber tid;
6589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6590 "Got a SPEED request from client!\n");
6592 /* Sanity check for client registration */
6593 if (NULL == (c = client_get (client)))
6596 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6602 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6604 /* Retrieve tunnel */
6605 tid = ntohl (tunnel_msg->tunnel_id);
6606 t = tunnel_get_by_local_id(c, tid);
6609 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6611 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6615 switch (ntohs(message->type))
6617 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6618 t->speed_min = GNUNET_YES;
6620 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6621 t->speed_min = GNUNET_NO;
6626 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6631 * Handler for requests of seeting tunnel's buffering policy.
6633 * @param cls Closure (unused).
6634 * @param client Identification of the client.
6635 * @param message The actual message.
6638 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6639 const struct GNUNET_MessageHeader *message)
6641 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6642 struct MeshClient *c;
6643 struct MeshTunnel *t;
6644 MESH_TunnelNumber tid;
6646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6647 "Got a BUFFER request from client!\n");
6649 /* Sanity check for client registration */
6650 if (NULL == (c = client_get (client)))
6653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6658 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6660 /* Retrieve tunnel */
6661 tid = ntohl (tunnel_msg->tunnel_id);
6662 t = tunnel_get_by_local_id(c, tid);
6665 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6667 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6671 switch (ntohs(message->type))
6673 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6674 t->nobuffer = GNUNET_NO;
6676 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6677 t->nobuffer = GNUNET_YES;
6683 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6688 * Handler for connection requests to new peers
6690 * @param cls closure
6691 * @param client identification of the client
6692 * @param message the actual message (PeerControl)
6695 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6696 const struct GNUNET_MessageHeader *message)
6698 struct GNUNET_MESH_PeerControl *peer_msg;
6699 struct MeshPeerInfo *peer_info;
6700 struct MeshClient *c;
6701 struct MeshTunnel *t;
6702 MESH_TunnelNumber tid;
6704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6705 /* Sanity check for client registration */
6706 if (NULL == (c = client_get (client)))
6709 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6714 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6716 /* Sanity check for message size */
6717 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6724 /* Tunnel exists? */
6725 tid = ntohl (peer_msg->tunnel_id);
6726 t = tunnel_get_by_local_id (c, tid);
6730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6734 /* Does client own tunnel? */
6735 if (t->owner->handle != client)
6738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6742 GNUNET_i2s (&peer_msg->peer));
6743 peer_info = peer_info_get (&peer_msg->peer);
6745 tunnel_add_peer (t, peer_info);
6746 peer_info_connect (peer_info, t);
6748 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6754 * Handler for disconnection requests of peers in a tunnel
6756 * @param cls closure
6757 * @param client identification of the client
6758 * @param message the actual message (PeerControl)
6761 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6762 const struct GNUNET_MessageHeader *message)
6764 struct GNUNET_MESH_PeerControl *peer_msg;
6765 struct MeshPeerInfo *peer_info;
6766 struct MeshClient *c;
6767 struct MeshTunnel *t;
6768 MESH_TunnelNumber tid;
6770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6771 /* Sanity check for client registration */
6772 if (NULL == (c = client_get (client)))
6775 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6780 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6782 /* Sanity check for message size */
6783 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6790 /* Tunnel exists? */
6791 tid = ntohl (peer_msg->tunnel_id);
6792 t = tunnel_get_by_local_id (c, tid);
6796 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6801 /* Does client own tunnel? */
6802 if (t->owner->handle != client)
6805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6810 GNUNET_i2s (&peer_msg->peer));
6811 /* Is the peer in the tunnel? */
6813 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6814 if (NULL == peer_info)
6817 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6821 /* Ok, delete peer from tunnel */
6822 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6823 &peer_msg->peer.hashPubKey);
6825 send_destroy_path (t, peer_info->id);
6826 tunnel_delete_peer (t, peer_info->id);
6827 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6832 * Handler for blacklist requests of peers in a tunnel
6834 * @param cls closure
6835 * @param client identification of the client
6836 * @param message the actual message (PeerControl)
6838 * FIXME implement DHT block bloomfilter
6841 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6842 const struct GNUNET_MessageHeader *message)
6844 struct GNUNET_MESH_PeerControl *peer_msg;
6845 struct MeshClient *c;
6846 struct MeshTunnel *t;
6847 MESH_TunnelNumber tid;
6849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6850 /* Sanity check for client registration */
6851 if (NULL == (c = client_get (client)))
6854 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6859 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6861 /* Sanity check for message size */
6862 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6869 /* Tunnel exists? */
6870 tid = ntohl (peer_msg->tunnel_id);
6871 t = tunnel_get_by_local_id (c, tid);
6875 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6880 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6881 GNUNET_PEER_intern(&peer_msg->peer));
6886 * Handler for unblacklist requests of peers in a tunnel
6888 * @param cls closure
6889 * @param client identification of the client
6890 * @param message the actual message (PeerControl)
6893 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6894 const struct GNUNET_MessageHeader *message)
6896 struct GNUNET_MESH_PeerControl *peer_msg;
6897 struct MeshClient *c;
6898 struct MeshTunnel *t;
6899 MESH_TunnelNumber tid;
6903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6904 /* Sanity check for client registration */
6905 if (NULL == (c = client_get (client)))
6908 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6913 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6915 /* Sanity check for message size */
6916 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6919 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6923 /* Tunnel exists? */
6924 tid = ntohl (peer_msg->tunnel_id);
6925 t = tunnel_get_by_local_id (c, tid);
6929 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6934 /* if peer is not known, complain */
6935 pid = GNUNET_PEER_search (&peer_msg->peer);
6942 /* search and remove from list */
6943 for (i = 0; i < t->nblacklisted; i++)
6945 if (t->blacklisted[i] == pid)
6947 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6948 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6953 /* if peer hasn't been blacklisted, complain */
6959 * Handler for connection requests to new peers by type
6961 * @param cls closure
6962 * @param client identification of the client
6963 * @param message the actual message (ConnectPeerByType)
6966 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6967 const struct GNUNET_MessageHeader *message)
6969 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6970 struct MeshClient *c;
6971 struct MeshTunnel *t;
6972 struct GNUNET_HashCode hash;
6973 MESH_TunnelNumber tid;
6975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6976 /* Sanity check for client registration */
6977 if (NULL == (c = client_get (client)))
6980 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6985 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6987 /* Sanity check for message size */
6988 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6989 ntohs (connect_msg->header.size))
6992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6996 /* Tunnel exists? */
6997 tid = ntohl (connect_msg->tunnel_id);
6998 t = tunnel_get_by_local_id (c, tid);
7002 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7006 /* Does client own tunnel? */
7007 if (t->owner->handle != client)
7010 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7014 /* Do WE have the service? */
7015 t->type = ntohl (connect_msg->type);
7016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7017 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7018 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7021 /* Yes! Fast forward, add ourselves to the tunnel and send the
7022 * good news to the client, and alert the destination client of
7023 * an incoming tunnel.
7025 * FIXME send a path create to self, avoid code duplication
7027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7028 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7029 peer_info_get (&my_full_id),
7030 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7033 send_client_peer_connected (t, myid);
7034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7035 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7037 t->local_tid_dest = next_local_tid++;
7038 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7039 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7040 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7044 /* Ok, lets find a peer offering the service */
7045 if (NULL != t->dht_get_type)
7047 GNUNET_DHT_get_stop (t->dht_get_type);
7049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7050 GNUNET_h2s (&hash));
7052 GNUNET_DHT_get_start (dht_handle,
7053 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7055 dht_replication_level,
7056 GNUNET_DHT_RO_RECORD_ROUTE |
7057 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7059 &dht_get_type_handler, t);
7061 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7067 * Handler for connection requests to new peers by a string service description.
7069 * @param cls closure
7070 * @param client identification of the client
7071 * @param message the actual message, which includes messages the client wants
7074 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7075 const struct GNUNET_MessageHeader *message)
7077 struct GNUNET_MESH_ConnectPeerByString *msg;
7078 struct MeshRegexSearchContext *ctx;
7079 struct MeshRegexSearchInfo *info;
7080 struct GNUNET_DHT_GetHandle *get_h;
7081 struct GNUNET_HashCode key;
7082 struct MeshTunnel *t;
7083 struct MeshClient *c;
7084 MESH_TunnelNumber tid;
7088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7089 "Connect by string started\n");
7090 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7091 size = htons (message->size);
7093 /* Sanity check for client registration */
7094 if (NULL == (c = client_get (client)))
7097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7102 /* Message size sanity check */
7103 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7106 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7110 /* Tunnel exists? */
7111 tid = ntohl (msg->tunnel_id);
7112 t = tunnel_get_by_local_id (c, tid);
7116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7120 /* Does client own tunnel? */
7121 if (t->owner->handle != client)
7124 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7129 " on tunnel %s [%u]\n",
7130 GNUNET_i2s(&my_full_id),
7133 /* Only one connect_by_string allowed at the same time! */
7134 /* FIXME: allow more, return handle at api level to cancel, document */
7135 if (NULL != t->regex_ctx)
7138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7142 /* Find string itself */
7143 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7144 string = (const char *) &msg[1];
7146 /* Initialize context */
7147 size = GNUNET_REGEX_get_first_key(string, len, &key);
7148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7149 " consumed %u bits out of %u\n", size, len);
7150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7151 " looking for %s\n", GNUNET_h2s (&key));
7153 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7155 info->description = GNUNET_malloc (len + 1);
7156 memcpy (info->description, string, len);
7157 info->description[len] = '\0';
7158 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7159 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7162 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7163 ctx->position = size;
7167 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7169 /* Start search in DHT */
7170 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7171 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7172 &key, /* key to search */
7173 dht_replication_level, /* replication level */
7174 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7175 NULL, /* xquery */ // FIXME BLOOMFILTER
7176 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7177 &dht_get_string_handler, ctx);
7179 GNUNET_break (GNUNET_OK ==
7180 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7185 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7191 * Handler for client traffic directed to one peer
7193 * @param cls closure
7194 * @param client identification of the client
7195 * @param message the actual message
7198 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7199 const struct GNUNET_MessageHeader *message)
7201 struct MeshClient *c;
7202 struct MeshTunnel *t;
7203 struct MeshPeerInfo *pi;
7204 struct GNUNET_MESH_Unicast *data_msg;
7205 MESH_TunnelNumber tid;
7208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7209 "Got a unicast request from a client!\n");
7211 /* Sanity check for client registration */
7212 if (NULL == (c = client_get (client)))
7215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7220 data_msg = (struct GNUNET_MESH_Unicast *) message;
7222 /* Sanity check for message size */
7223 size = ntohs (message->size);
7224 if (sizeof (struct GNUNET_MESH_Unicast) +
7225 sizeof (struct GNUNET_MessageHeader) > size)
7228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7232 /* Tunnel exists? */
7233 tid = ntohl (data_msg->tid);
7234 t = tunnel_get_by_local_id (c, tid);
7238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7242 /* Is it a local tunnel? Then, does client own the tunnel? */
7243 if (t->owner->handle != client)
7246 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7250 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7251 &data_msg->destination.hashPubKey);
7252 /* Is the selected peer in the tunnel? */
7256 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7260 /* PID should be as expected */
7261 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7264 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7265 "Unicast PID, expected %u, got %u\n",
7266 t->fwd_pid + 1, ntohl (data_msg->pid));
7267 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7271 /* Ok, everything is correct, send the message
7272 * (pretend we got it from a mesh peer)
7275 /* Work around const limitation */
7276 char buf[ntohs (message->size)] GNUNET_ALIGN;
7277 struct GNUNET_MESH_Unicast *copy;
7279 copy = (struct GNUNET_MESH_Unicast *) buf;
7280 memcpy (buf, data_msg, size);
7281 copy->oid = my_full_id;
7282 copy->tid = htonl (t->id.tid);
7283 copy->ttl = htonl (default_ttl);
7284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7285 " calling generic handler...\n");
7286 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7289 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7296 * Handler for client traffic directed to the origin
7298 * @param cls closure
7299 * @param client identification of the client
7300 * @param message the actual message
7303 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7304 const struct GNUNET_MessageHeader *message)
7306 struct GNUNET_MESH_ToOrigin *data_msg;
7307 struct MeshTunnelClientInfo *clinfo;
7308 struct MeshClient *c;
7309 struct MeshTunnel *t;
7310 MESH_TunnelNumber tid;
7313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7314 "Got a ToOrigin request from a client!\n");
7315 /* Sanity check for client registration */
7316 if (NULL == (c = client_get (client)))
7319 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7324 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7326 /* Sanity check for message size */
7327 size = ntohs (message->size);
7328 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7329 sizeof (struct GNUNET_MessageHeader) > size)
7332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7336 /* Tunnel exists? */
7337 tid = ntohl (data_msg->tid);
7338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7339 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7342 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7345 t = tunnel_get_by_local_id (c, tid);
7348 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7349 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7355 /* It should be sent by someone who has this as incoming tunnel. */
7356 if (GNUNET_NO == client_knows_tunnel (c, t))
7359 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7363 /* PID should be as expected */
7364 clinfo = tunnel_get_client_fc (t, c);
7365 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7369 "To Origin PID, expected %u, got %u\n",
7370 clinfo->bck_pid + 1,
7371 ntohl (data_msg->pid));
7372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7376 /* Ok, everything is correct, send the message
7377 * (pretend we got it from a mesh peer)
7381 char buf[ntohs (message->size)] GNUNET_ALIGN;
7382 struct GNUNET_MESH_ToOrigin *copy;
7384 /* Work around const limitation */
7385 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7386 memcpy (buf, data_msg, size);
7387 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7388 copy->tid = htonl (t->id.tid);
7389 copy->ttl = htonl (default_ttl);
7390 if (ntohl (copy->pid) != (t->bck_pid + 1))
7393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7394 "To Origin PID, expected %u, got %u\n",
7400 copy->sender = my_full_id;
7401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7402 " calling generic handler...\n");
7403 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7405 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7412 * Handler for client traffic directed to all peers in a tunnel
7414 * @param cls closure
7415 * @param client identification of the client
7416 * @param message the actual message
7419 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7420 const struct GNUNET_MessageHeader *message)
7422 struct MeshClient *c;
7423 struct MeshTunnel *t;
7424 struct GNUNET_MESH_Multicast *data_msg;
7425 MESH_TunnelNumber tid;
7427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7428 "Got a multicast request from a client!\n");
7430 /* Sanity check for client registration */
7431 if (NULL == (c = client_get (client)))
7434 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7439 data_msg = (struct GNUNET_MESH_Multicast *) message;
7441 /* Sanity check for message size */
7442 if (sizeof (struct GNUNET_MESH_Multicast) +
7443 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7450 /* Tunnel exists? */
7451 tid = ntohl (data_msg->tid);
7452 t = tunnel_get_by_local_id (c, tid);
7456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7457 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7463 /* Does client own tunnel? */
7464 if (t->owner->handle != client)
7467 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7471 /* PID should be as expected */
7472 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7475 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7476 "Multicast PID, expected %u, got %u\n",
7477 t->fwd_pid + 1, ntohl (data_msg->pid));
7478 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7483 char buf[ntohs (message->size)] GNUNET_ALIGN;
7484 struct GNUNET_MESH_Multicast *copy;
7486 copy = (struct GNUNET_MESH_Multicast *) buf;
7487 memcpy (buf, message, ntohs (message->size));
7488 copy->oid = my_full_id;
7489 copy->tid = htonl (t->id.tid);
7490 copy->ttl = htonl (default_ttl);
7491 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7493 " calling generic handler...\n");
7494 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7497 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7503 * Handler for client's ACKs for payload traffic.
7505 * @param cls Closure (unused).
7506 * @param client Identification of the client.
7507 * @param message The actual message.
7510 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7511 const struct GNUNET_MessageHeader *message)
7513 struct GNUNET_MESH_LocalAck *msg;
7514 struct MeshTunnel *t;
7515 struct MeshClient *c;
7516 MESH_TunnelNumber tid;
7519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7520 /* Sanity check for client registration */
7521 if (NULL == (c = client_get (client)))
7524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7529 msg = (struct GNUNET_MESH_LocalAck *) message;
7531 /* Tunnel exists? */
7532 tid = ntohl (msg->tunnel_id);
7533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7534 t = tunnel_get_by_local_id (c, tid);
7538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7540 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7544 ack = ntohl (msg->max_pid);
7545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7547 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7548 if (NULL != t->owner && t->owner->handle == client)
7550 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7552 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7556 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7557 tunnel_set_client_fwd_ack (t, c, ack);
7558 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7561 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7568 * Functions to handle messages from clients
7570 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7571 {&handle_local_new_client, NULL,
7572 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7573 {&handle_local_announce_regex, NULL,
7574 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7575 {&handle_local_tunnel_create, NULL,
7576 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7577 sizeof (struct GNUNET_MESH_TunnelMessage)},
7578 {&handle_local_tunnel_destroy, NULL,
7579 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7580 sizeof (struct GNUNET_MESH_TunnelMessage)},
7581 {&handle_local_tunnel_speed, NULL,
7582 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7583 sizeof (struct GNUNET_MESH_TunnelMessage)},
7584 {&handle_local_tunnel_speed, NULL,
7585 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7586 sizeof (struct GNUNET_MESH_TunnelMessage)},
7587 {&handle_local_tunnel_buffer, NULL,
7588 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7589 sizeof (struct GNUNET_MESH_TunnelMessage)},
7590 {&handle_local_tunnel_buffer, NULL,
7591 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7592 sizeof (struct GNUNET_MESH_TunnelMessage)},
7593 {&handle_local_connect_add, NULL,
7594 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7595 sizeof (struct GNUNET_MESH_PeerControl)},
7596 {&handle_local_connect_del, NULL,
7597 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7598 sizeof (struct GNUNET_MESH_PeerControl)},
7599 {&handle_local_blacklist, NULL,
7600 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7601 sizeof (struct GNUNET_MESH_PeerControl)},
7602 {&handle_local_unblacklist, NULL,
7603 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7604 sizeof (struct GNUNET_MESH_PeerControl)},
7605 {&handle_local_connect_by_type, NULL,
7606 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7607 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7608 {&handle_local_connect_by_string, NULL,
7609 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7610 {&handle_local_unicast, NULL,
7611 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7612 {&handle_local_to_origin, NULL,
7613 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7614 {&handle_local_multicast, NULL,
7615 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7616 {&handle_local_ack, NULL,
7617 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7618 sizeof (struct GNUNET_MESH_LocalAck)},
7624 * To be called on core init/fail.
7626 * @param cls service closure
7627 * @param server handle to the server for this service
7628 * @param identity the public identity of this peer
7631 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7632 const struct GNUNET_PeerIdentity *identity)
7635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7636 core_handle = server;
7637 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7640 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7641 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7643 GNUNET_abort(); // Try harder
7650 * Method called whenever a given peer connects.
7652 * @param cls closure
7653 * @param peer peer identity this notification is about
7654 * @param atsi performance data for the connection
7655 * @param atsi_count number of records in 'atsi'
7658 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7659 const struct GNUNET_ATS_Information *atsi,
7660 unsigned int atsi_count)
7662 struct MeshPeerInfo *peer_info;
7663 struct MeshPeerPath *path;
7665 DEBUG_CONN ("Peer connected\n");
7666 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7667 peer_info = peer_info_get (peer);
7668 if (myid == peer_info->id)
7670 DEBUG_CONN (" (self)\n");
7675 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7677 path = path_new (2);
7678 path->peers[0] = myid;
7679 path->peers[1] = peer_info->id;
7680 GNUNET_PEER_change_rc (myid, 1);
7681 GNUNET_PEER_change_rc (peer_info->id, 1);
7682 peer_info_add_path (peer_info, path, GNUNET_YES);
7683 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7689 * Method called whenever a peer disconnects.
7691 * @param cls closure
7692 * @param peer peer identity this notification is about
7695 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7697 struct MeshPeerInfo *pi;
7698 struct MeshPeerQueue *q;
7699 struct MeshPeerQueue *n;
7701 DEBUG_CONN ("Peer disconnected\n");
7702 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7714 /* try to reroute this traffic instead */
7715 queue_destroy(q, GNUNET_YES);
7719 peer_info_remove_path (pi, pi->id, myid);
7722 DEBUG_CONN (" (self)\n");
7724 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7729 /******************************************************************************/
7730 /************************ MAIN FUNCTIONS ****************************/
7731 /******************************************************************************/
7734 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7736 * @param cls closure
7737 * @param key current key code
7738 * @param value value in the hash map
7739 * @return GNUNET_YES if we should continue to iterate,
7743 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7745 struct MeshTunnel *t = value;
7752 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7754 * @param cls closure
7755 * @param key current key code
7756 * @param value value in the hash map
7757 * @return GNUNET_YES if we should continue to iterate,
7761 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7763 struct MeshPeerInfo *p = value;
7764 struct MeshPeerQueue *q;
7765 struct MeshPeerQueue *n;
7773 queue_destroy(q, GNUNET_YES);
7777 peer_info_destroy (p);
7783 * Task run during shutdown.
7789 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7793 if (core_handle != NULL)
7795 GNUNET_CORE_disconnect (core_handle);
7800 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
7803 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7804 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7805 if (dht_handle != NULL)
7807 GNUNET_DHT_disconnect (dht_handle);
7812 GNUNET_SERVER_notification_context_destroy (nc);
7815 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7817 GNUNET_SCHEDULER_cancel (announce_id_task);
7818 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7825 * Callback for hostkey read/generation
7828 * @param pk the private key
7829 * @param emsg error message
7832 key_generation_cb (void *cls,
7833 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
7836 struct MeshPeerInfo *peer;
7837 struct MeshPeerPath *p;
7842 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7843 _("Mesh service could not access hostkey. Exiting.\n"));
7844 GNUNET_SCHEDULER_shutdown ();
7847 my_private_key = pk;
7848 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7849 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7850 &my_full_id.hashPubKey);
7851 myid = GNUNET_PEER_intern (&my_full_id);
7852 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7853 "Mesh for peer [%s] starting\n",
7854 GNUNET_i2s(&my_full_id));
7856 // transport_handle = GNUNET_TRANSPORT_connect(c,
7866 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7869 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7870 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7871 GNUNET_SERVER_disconnect_notify (server_handle,
7872 &handle_local_client_disconnect, NULL);
7876 clients_tail = NULL;
7879 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7880 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7882 /* Create a peer_info for the local peer */
7883 peer = peer_info_get (&my_full_id);
7886 GNUNET_PEER_change_rc (myid, 1);
7887 peer_info_add_path (peer, p, GNUNET_YES);
7888 GNUNET_SERVER_resume (server_handle);
7889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7894 * Process mesh requests.
7896 * @param cls closure
7897 * @param server the initialized server
7898 * @param c configuration to use
7901 run (void *cls, struct GNUNET_SERVER_Handle *server,
7902 const struct GNUNET_CONFIGURATION_Handle *c)
7906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7907 server_handle = server;
7908 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7909 NULL, /* Closure passed to MESH functions */
7910 &core_init, /* Call core_init once connected */
7911 &core_connect, /* Handle connects */
7912 &core_disconnect, /* remove peers on disconnects */
7913 NULL, /* Don't notify about all incoming messages */
7914 GNUNET_NO, /* For header only in notification */
7915 NULL, /* Don't notify about all outbound messages */
7916 GNUNET_NO, /* For header-only out notification */
7917 core_handlers); /* Register these handlers */
7919 if (core_handle == NULL)
7922 GNUNET_SCHEDULER_shutdown ();
7927 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7930 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7932 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7934 GNUNET_SCHEDULER_shutdown ();
7939 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7940 &refresh_path_time))
7942 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7944 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7945 "refresh path time");
7946 GNUNET_SCHEDULER_shutdown ();
7951 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7952 &app_announce_time))
7954 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7956 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7957 "app announce time");
7958 GNUNET_SCHEDULER_shutdown ();
7963 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7968 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7969 "id announce time");
7970 GNUNET_SCHEDULER_shutdown ();
7975 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7976 &unacknowledged_wait_time))
7978 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7980 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7981 "unacknowledged wait time");
7982 GNUNET_SCHEDULER_shutdown ();
7987 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7990 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7992 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7994 GNUNET_SCHEDULER_shutdown ();
7999 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8002 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8004 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
8006 GNUNET_SCHEDULER_shutdown ();
8011 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8014 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8016 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
8018 GNUNET_SCHEDULER_shutdown ();
8023 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8026 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8028 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
8034 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8035 &dht_replication_level))
8037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8039 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
8040 "dht replication level", 10);
8041 dht_replication_level = 10;
8044 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8045 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8046 peers = GNUNET_CONTAINER_multihashmap_create (32);
8047 applications = GNUNET_CONTAINER_multihashmap_create (32);
8048 types = GNUNET_CONTAINER_multihashmap_create (32);
8050 dht_handle = GNUNET_DHT_connect (c, 64);
8051 if (NULL == dht_handle)
8055 stats = GNUNET_STATISTICS_create ("mesh", c);
8057 GNUNET_SERVER_suspend (server_handle);
8058 /* Scheduled the task to clean up when shutdown is called */
8059 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8061 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8062 GNUNET_free (keyfile);
8067 * The main function for the mesh service.
8069 * @param argc number of arguments from the command line
8070 * @param argv command line arguments
8071 * @return 0 ok, 1 on error
8074 main (int argc, char *const *argv)
8078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8081 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8087 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8088 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8089 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);