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 * Queue and pass message to core when possible.
1172 * @param cls Closure (type dependant).
1173 * @param type Type of the message, 0 for a raw message.
1174 * @param size Size of the message.
1175 * @param dst Neighbor to send message to.
1176 * @param t Tunnel this message belongs to.
1179 queue_add (void *cls, uint16_t type, size_t size,
1180 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1184 * Free a transmission that was already queued with all resources
1185 * associated to the request.
1187 * @param queue Queue handler to cancel.
1188 * @param clear_cls Is it necessary to free associated cls?
1191 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1195 * @brief Get the next transmittable message from the queue.
1197 * This will be the head, except in the case of being a data packet
1198 * not allowed by the destination peer.
1200 * @param peer Destination peer.
1202 * @return The next viable MeshPeerQueue element to send to that peer.
1203 * NULL when there are no transmittable messages.
1205 struct MeshPeerQueue *
1206 queue_get_next (const struct MeshPeerInfo *peer);
1210 * Core callback to write a queued packet to core buffer
1212 * @param cls Closure (peer info).
1213 * @param size Number of bytes available in buf.
1214 * @param buf Where the to write the message.
1216 * @return number of bytes written to buf
1219 queue_send (void *cls, size_t size, void *buf);
1221 /******************************************************************************/
1222 /************************ ITERATORS ****************************/
1223 /******************************************************************************/
1226 * Iterator over found existing mesh regex blocks that match an ongoing search.
1228 * @param cls closure
1229 * @param key current key code
1230 * @param value value in the hash map
1231 * @return GNUNET_YES if we should continue to iterate,
1235 regex_result_iterator (void *cls,
1236 const struct GNUNET_HashCode * key,
1239 struct MeshRegexBlock *block = value;
1240 struct MeshRegexSearchContext *ctx = cls;
1242 if (GNUNET_YES == ntohl(block->accepting) &&
1243 ctx->position == strlen (ctx->info->description))
1245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1246 regex_find_path (key, ctx);
1247 return GNUNET_YES; // We found an accept state!
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1252 ctx->position, strlen(ctx->info->description),
1253 ntohl(block->accepting));
1256 regex_next_edge(block, SIZE_MAX, ctx);
1263 * Iterator over edges in a regex block retrieved from the DHT.
1265 * @param cls Closure (context of the search).
1266 * @param token Token that follows to next state.
1267 * @param len Lenght of token.
1268 * @param key Hash of next state.
1270 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1273 regex_edge_iterator (void *cls,
1276 const struct GNUNET_HashCode *key)
1278 struct MeshRegexSearchContext *ctx = cls;
1279 struct MeshRegexSearchInfo *info = ctx->info;
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1286 current = &info->description[ctx->position];
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1288 current_len = strlen (info->description) - ctx->position;
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1293 if (len > current_len)
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1296 return GNUNET_YES; // Token too long, wont match
1298 if (0 != strncmp (current, token, len))
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1301 return GNUNET_YES; // Token doesn't match
1304 if (len > ctx->longest_match)
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1307 ctx->longest_match = len;
1312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1321 * Jump to the next edge, with the longest matching token.
1323 * @param block Block found in the DHT.
1324 * @param size Size of the block.
1325 * @param ctx Context of the search.
1327 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1330 regex_next_edge (const struct MeshRegexBlock *block,
1332 struct MeshRegexSearchContext *ctx)
1334 struct MeshRegexSearchContext *new_ctx;
1335 struct MeshRegexSearchInfo *info = ctx->info;
1336 struct GNUNET_DHT_GetHandle *get_h;
1338 /* Find the longest match for the current string position,
1339 * among tokens in the given block */
1340 GNUNET_break (GNUNET_OK ==
1341 GNUNET_MESH_regex_block_iterate (block, size,
1342 ®ex_edge_iterator, ctx));
1344 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1345 new_ctx->info = info;
1346 new_ctx->position = ctx->position + ctx->longest_match;
1347 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1349 /* Check whether we already have a DHT GET running for it */
1351 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1354 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1356 ®ex_result_iterator,
1358 return; // We are already looking for it
1361 /* Start search in DHT */
1363 GNUNET_DHT_get_start (dht_handle, /* handle */
1364 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1365 &ctx->hash, /* key to search */
1366 dht_replication_level, /* replication level */
1367 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1368 NULL, /* xquery */ // FIXME BLOOMFILTER
1369 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1370 &dht_get_string_handler, new_ctx);
1372 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1375 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1384 * Iterator over hash map entries to cancel DHT GET requests after a
1385 * successful connect_by_string.
1387 * @param cls Closure (unused).
1388 * @param key Current key code (unused).
1389 * @param value Value in the hash map (get handle).
1390 * @return GNUNET_YES if we should continue to iterate,
1394 regex_cancel_dht_get (void *cls,
1395 const struct GNUNET_HashCode * key,
1398 struct GNUNET_DHT_GetHandle *h = value;
1400 GNUNET_DHT_get_stop (h);
1406 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1407 * search for connect_by_string.
1409 * @param cls Closure (unused).
1410 * @param key Current key code (unused).
1411 * @param value MeshRegexBlock in the hash map.
1412 * @return GNUNET_YES if we should continue to iterate,
1416 regex_free_result (void *cls,
1417 const struct GNUNET_HashCode * key,
1421 GNUNET_free (value);
1427 * Regex callback iterator to store own service description in the DHT.
1429 * @param cls closure.
1430 * @param key hash for current state.
1431 * @param proof proof for current state.
1432 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1433 * @param num_edges number of edges leaving current state.
1434 * @param edges edges leaving current state.
1437 regex_iterator (void *cls,
1438 const struct GNUNET_HashCode *key,
1441 unsigned int num_edges,
1442 const struct GNUNET_REGEX_Edge *edges)
1444 struct MeshRegexBlock *block;
1445 struct MeshRegexEdge *block_edge;
1446 enum GNUNET_DHT_RouteOption opt;
1450 unsigned int offset;
1453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1454 " regex dht put for state %s\n",
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1463 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1464 if (GNUNET_YES == accepting)
1466 struct MeshRegexAccept block;
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1469 " state %s is accepting, putting own id\n",
1471 size = sizeof (block);
1473 block.id = my_full_id;
1475 GNUNET_DHT_put(dht_handle, key,
1476 dht_replication_level,
1477 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1478 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1481 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1486 len = strlen(proof);
1487 size = sizeof (struct MeshRegexBlock) + len;
1488 block = GNUNET_malloc (size);
1491 block->n_proof = htonl (len);
1492 block->n_edges = htonl (num_edges);
1493 block->accepting = htonl (accepting);
1495 /* Store the proof at the end of the block. */
1496 aux = (char *) &block[1];
1497 memcpy (aux, proof, len);
1500 /* Store each edge in a variable length MeshEdge struct at the
1501 * very end of the MeshRegexBlock structure.
1503 for (i = 0; i < num_edges; i++)
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1506 " edge %s towards %s\n",
1508 GNUNET_h2s(&edges[i].destination));
1510 /* aux points at the end of the last block */
1511 len = strlen (edges[i].label);
1512 size += sizeof (struct MeshRegexEdge) + len;
1513 // Calculate offset FIXME is this ok? use size instead?
1514 offset = aux - (char *) block;
1515 block = GNUNET_realloc (block, size);
1516 aux = &((char *) block)[offset];
1517 block_edge = (struct MeshRegexEdge *) aux;
1518 block_edge->key = edges[i].destination;
1519 block_edge->n_token = htonl (len);
1520 aux = (char *) &block_edge[1];
1521 memcpy (aux, edges[i].label, len);
1525 GNUNET_DHT_put(dht_handle, key,
1526 dht_replication_level,
1528 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1530 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1534 GNUNET_free (block);
1539 * Store the regular expression describing a local service into the DHT.
1541 * @param regex The regular expresion.
1544 regex_put (const char *regex)
1546 struct GNUNET_REGEX_Automaton *dfa;
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1549 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1550 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1551 GNUNET_REGEX_automaton_destroy (dfa);
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1557 * Find a path to a peer that offers a regex servcie compatible
1558 * with a given string.
1560 * @param key The key of the accepting state.
1561 * @param ctx Context containing info about the string, tunnel, etc.
1564 regex_find_path (const struct GNUNET_HashCode *key,
1565 struct MeshRegexSearchContext *ctx)
1567 struct GNUNET_DHT_GetHandle *get_h;
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1570 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1571 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1572 key, /* key to search */
1573 dht_replication_level, /* replication level */
1574 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1575 GNUNET_DHT_RO_RECORD_ROUTE,
1576 NULL, /* xquery */ // FIXME BLOOMFILTER
1577 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1578 &dht_get_string_accept_handler, ctx);
1579 GNUNET_break (GNUNET_OK ==
1580 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1583 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1588 * Function called if the connect attempt to a peer found via
1589 * connect_by_string times out. Try to connect to another peer, if any.
1590 * Otherwise try to reconnect to the same peer.
1592 * @param cls Closure (info about regex search).
1593 * @param tc TaskContext.
1596 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1598 struct MeshRegexSearchInfo *info = cls;
1599 struct MeshPeerInfo *peer_info;
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1604 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1605 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1613 if (0 < info->n_peers)
1615 // Select next peer, put current in that spot.
1616 id = info->peers[info->i_peer];
1617 info->peers[info->i_peer] = info->peer;
1618 info->i_peer = (info->i_peer + 1) % info->n_peers;
1622 // Try to connect to same peer again.
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1627 peer_info = peer_info_get_short(id);
1628 tunnel_add_peer (info->t, peer_info);
1630 tunnel_delete_peer (info->t, old);
1631 peer_info_connect (peer_info, info->t);
1632 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1633 ®ex_connect_timeout,
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1640 * Cancel an ongoing regex search in the DHT and free all resources.
1642 * @param ctx The search context.
1645 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1647 struct MeshRegexSearchInfo *info = ctx->info;
1650 GNUNET_free (info->description);
1651 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1652 ®ex_cancel_dht_get, NULL);
1653 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1654 ®ex_free_result, NULL);
1655 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1656 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1657 info->t->regex_ctx = NULL;
1658 for (i = 0; i < info->n_contexts; i++)
1660 GNUNET_free (info->contexts[i]);
1662 if (0 < info->n_contexts)
1663 GNUNET_free (info->contexts);
1664 if (0 < info->n_peers)
1665 GNUNET_free (info->peers);
1666 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1668 GNUNET_SCHEDULER_cancel(info->timeout);
1674 /******************************************************************************/
1675 /************************ PERIODIC FUNCTIONS ****************************/
1676 /******************************************************************************/
1679 * Announce iterator over for each application provided by the peer
1681 * @param cls closure
1682 * @param key current key code
1683 * @param value value in the hash map
1684 * @return GNUNET_YES if we should continue to
1689 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1691 struct PBlock block;
1692 struct MeshClient *c;
1694 block.id = my_full_id;
1695 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1696 GNUNET_assert(NULL != c);
1697 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1698 if (0 == block.type)
1703 block.type = htonl (block.type);
1705 GNUNET_break (NULL !=
1706 GNUNET_DHT_put (dht_handle, key,
1707 dht_replication_level,
1708 GNUNET_DHT_RO_RECORD_ROUTE |
1709 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1710 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1712 (const char *) &block,
1713 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1715 app_announce_time, NULL, NULL));
1721 * Periodically announce what applications are provided by local clients
1724 * @param cls closure
1725 * @param tc task context
1728 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1730 struct MeshClient *c = cls;
1733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1735 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1739 DEBUG_DHT ("Starting PUT for regex\n");
1741 for (i = 0; i < c->n_regex; i++)
1743 regex_put (c->regexes[i]);
1745 c->regex_announce_task =
1746 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1747 DEBUG_DHT ("Finished PUT for regex\n");
1754 * Periodically announce what applications are provided by local clients
1757 * @param cls closure
1758 * @param tc task context
1761 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1763 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1765 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1769 DEBUG_DHT ("Starting PUT for apps\n");
1771 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1773 announce_applications_task =
1774 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1776 DEBUG_DHT ("Finished PUT for apps\n");
1783 * Periodically announce self id in the DHT
1785 * @param cls closure
1786 * @param tc task context
1789 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1791 struct PBlock block;
1793 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1795 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1799 * - Set data expiration in function of X
1800 * - Adapt X to churn
1802 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1804 block.id = my_full_id;
1805 block.type = htonl (0);
1806 GNUNET_DHT_put (dht_handle, /* DHT handle */
1807 &my_full_id.hashPubKey, /* Key to use */
1808 dht_replication_level, /* Replication level */
1809 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1810 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1811 sizeof (block), /* Size of the data */
1812 (const char *) &block, /* Data itself */
1813 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1814 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1815 NULL, /* Continuation */
1816 NULL); /* Continuation closure */
1818 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1822 /******************************************************************************/
1823 /****************** GENERAL HELPER FUNCTIONS ************************/
1824 /******************************************************************************/
1827 * Decrements the reference counter and frees all resources if needed
1829 * @param mesh_data Data Descriptor used in a multicast message.
1830 * Freed no longer needed (last message).
1833 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1835 if (0 == --(mesh_data->reference_counter))
1837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1838 GNUNET_free (mesh_data->data);
1839 GNUNET_free (mesh_data);
1845 * Check if client has registered with the service and has not disconnected
1847 * @param client the client to check
1849 * @return non-NULL if client exists in the global DLL
1851 static struct MeshClient *
1852 client_get (struct GNUNET_SERVER_Client *client)
1854 struct MeshClient *c;
1859 if (c->handle == client)
1868 * Checks if a given client has subscribed to certain message type
1870 * @param message_type Type of message to check
1871 * @param c Client to check
1873 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1875 * FIXME: use of crypto_hash slows it down
1876 * The hash function alone takes 8-10us out of the ~55us for the whole
1877 * process of retransmitting the message from one local client to another.
1878 * Find faster implementation!
1881 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1883 struct GNUNET_HashCode hc;
1885 if (NULL == c->types)
1888 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1889 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1894 * Check whether client wants traffic from a tunnel.
1896 * @param c Client to check.
1897 * @param t Tunnel to be found.
1899 * @return GNUNET_YES if client knows tunnel.
1901 * TODO look in client hashmap
1904 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1908 for (i = 0; i < t->nclients; i++)
1909 if (t->clients[i] == c)
1916 * Check whether client has been informed about a tunnel.
1918 * @param c Client to check.
1919 * @param t Tunnel to be found.
1921 * @return GNUNET_YES if client knows tunnel.
1923 * TODO look in client hashmap
1926 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1930 for (i = 0; i < t->nignore; i++)
1931 if (t->ignore[i] == c)
1933 return client_wants_tunnel(c, t);
1938 * Marks a client as uninterested in traffic from the tunnel, updating both
1939 * client and tunnel to reflect this.
1941 * @param c Client that doesn't want traffic anymore.
1942 * @param t Tunnel which should be ignored.
1944 * FIXME when to delete an incoming tunnel?
1947 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1949 struct GNUNET_HashCode hash;
1951 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1952 GNUNET_break (GNUNET_YES ==
1953 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1955 GNUNET_break (GNUNET_YES ==
1956 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1957 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1958 tunnel_delete_active_client (t, c);
1959 GNUNET_array_append (t->ignore, t->nignore, c);
1964 * Deletes a tunnel from a client (either owner or destination). To be used on
1965 * tunnel destroy, otherwise, use client_ignore_tunnel.
1967 * @param c Client whose tunnel to delete.
1968 * @param t Tunnel which should be deleted.
1971 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1973 struct GNUNET_HashCode hash;
1977 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1978 GNUNET_assert (GNUNET_YES ==
1979 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1985 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1987 GNUNET_assert (GNUNET_YES ==
1988 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1992 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2000 * Send the message to all clients that have subscribed to its type
2002 * @param msg Pointer to the message itself
2003 * @param payload Pointer to the payload of the message.
2004 * @param t The tunnel to whose clients this message goes.
2006 * @return number of clients this message was sent to
2009 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2010 const struct GNUNET_MessageHeader *payload,
2011 struct MeshTunnel *t)
2013 struct MeshClient *c;
2014 MESH_TunnelNumber *tid;
2017 char cbuf[htons (msg->size)];
2019 type = ntohs (payload->type);
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2022 GNUNET_MESH_DEBUG_M2S (type));
2024 memcpy (cbuf, msg, sizeof (cbuf));
2025 switch (htons (msg->type))
2027 struct GNUNET_MESH_Unicast *uc;
2028 struct GNUNET_MESH_Multicast *mc;
2029 struct GNUNET_MESH_ToOrigin *to;
2031 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2032 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2035 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2036 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2039 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2040 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2048 for (count = 0, c = clients; c != NULL; c = c->next)
2050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2051 if (client_is_subscribed (type, c))
2053 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2057 *tid = htonl (t->local_tid);
2061 if (GNUNET_NO == client_knows_tunnel (c, t))
2063 /* This client doesn't know the tunnel */
2064 struct GNUNET_MESH_TunnelNotification tmsg;
2065 struct GNUNET_HashCode hash;
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2068 tmsg.header.size = htons (sizeof (tmsg));
2069 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2070 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2071 tmsg.tunnel_id = htonl (t->local_tid_dest);
2072 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2073 &tmsg.header, GNUNET_NO);
2074 tunnel_add_client (t, c);
2075 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2077 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2078 c->incoming_tunnels, &hash, t,
2079 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2081 *tid = htonl (t->local_tid_dest);
2084 /* Check if the client wants to get traffic from the tunnel */
2085 if (GNUNET_NO == client_wants_tunnel(c, t))
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2089 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2090 (struct GNUNET_MessageHeader
2091 *) cbuf, GNUNET_NO);
2100 * Notify the client that owns the tunnel that a peer has connected to it
2101 * (the requested path to it has been confirmed).
2103 * @param t Tunnel whose owner to notify
2104 * @param id Short id of the peer that has connected
2107 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2109 struct GNUNET_MESH_PeerControl pc;
2111 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2112 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2113 pc.tunnel_id = htonl (t->local_tid);
2114 GNUNET_PEER_resolve (id, &pc.peer);
2115 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2121 * Notify all clients (not depending on registration status) that the incoming
2122 * tunnel is no longer valid.
2124 * @param t Tunnel that was destroyed.
2127 send_clients_tunnel_destroy (struct MeshTunnel *t)
2129 struct GNUNET_MESH_TunnelMessage msg;
2131 msg.header.size = htons (sizeof (msg));
2132 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2133 msg.tunnel_id = htonl (t->local_tid_dest);
2134 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2139 * Notify clients of tunnel disconnections, if needed.
2140 * In case the origin disconnects, the destination clients get a tunnel destroy
2141 * notification. If the last destination disconnects (only one remaining client
2142 * in tunnel), the origin gets a (local ID) peer disconnected.
2143 * Note that the function must be called BEFORE removing the client from
2146 * @param t Tunnel that was destroyed.
2147 * @param c Client that disconnected.
2150 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2156 struct GNUNET_MESH_TunnelMessage msg;
2158 msg.header.size = htons (sizeof (msg));
2159 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2160 msg.tunnel_id = htonl (t->local_tid_dest);
2161 for (i = 0; i < t->nclients; i++)
2162 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2163 &msg.header, GNUNET_NO);
2165 // FIXME when to disconnect an incoming tunnel?
2166 else if (1 == t->nclients && NULL != t->owner)
2168 struct GNUNET_MESH_PeerControl msg;
2170 msg.header.size = htons (sizeof (msg));
2171 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2172 msg.tunnel_id = htonl (t->local_tid);
2173 msg.peer = my_full_id;
2174 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2175 &msg.header, GNUNET_NO);
2181 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2182 * and insert it in the appropiate structures if the peer is not known yet.
2184 * @param peer Full identity of the peer.
2186 * @return Existing or newly created peer info.
2188 static struct MeshPeerInfo *
2189 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2191 struct MeshPeerInfo *peer_info;
2193 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2194 if (NULL == peer_info)
2197 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2198 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2199 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2200 peer_info->id = GNUNET_PEER_intern (peer);
2208 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2209 * and insert it in the appropiate structures if the peer is not known yet.
2211 * @param peer Short identity of the peer.
2213 * @return Existing or newly created peer info.
2215 static struct MeshPeerInfo *
2216 peer_info_get_short (const GNUNET_PEER_Id peer)
2218 struct GNUNET_PeerIdentity id;
2220 GNUNET_PEER_resolve (peer, &id);
2221 return peer_info_get (&id);
2226 * Iterator to remove the tunnel from the list of tunnels a peer participates
2229 * @param cls Closure (tunnel info)
2230 * @param key GNUNET_PeerIdentity of the peer (unused)
2231 * @param value PeerInfo of the peer
2233 * @return always GNUNET_YES, to keep iterating
2236 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2238 struct MeshTunnel *t = cls;
2239 struct MeshPeerInfo *peer = value;
2242 for (i = 0; i < peer->ntunnels; i++)
2245 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2248 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2249 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2258 * Core callback to write a pre-constructed data packet to core buffer
2260 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2261 * @param size Number of bytes available in buf.
2262 * @param buf Where the to write the message.
2264 * @return number of bytes written to buf
2267 send_core_data_raw (void *cls, size_t size, void *buf)
2269 struct MeshTransmissionDescriptor *info = cls;
2270 struct GNUNET_MessageHeader *msg;
2273 GNUNET_assert (NULL != info);
2274 GNUNET_assert (NULL != info->mesh_data);
2275 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2276 total_size = ntohs (msg->size);
2278 if (total_size > size)
2283 memcpy (buf, msg, total_size);
2284 data_descriptor_decrement_rc (info->mesh_data);
2291 * Sends an already built non-multicast message to a peer,
2292 * properly registrating all used resources.
2294 * @param message Message to send. Function makes a copy of it.
2295 * @param peer Short ID of the neighbor whom to send the message.
2296 * @param t Tunnel on which this message is transmitted.
2299 send_message (const struct GNUNET_MessageHeader *message,
2300 const struct GNUNET_PeerIdentity *peer,
2301 struct MeshTunnel *t)
2303 struct MeshTransmissionDescriptor *info;
2304 struct MeshPeerInfo *neighbor;
2305 struct MeshPeerPath *p;
2309 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2311 size = ntohs (message->size);
2312 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2313 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2314 info->mesh_data->data = GNUNET_malloc (size);
2315 memcpy (info->mesh_data->data, message, size);
2316 type = ntohs(message->type);
2317 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2319 struct GNUNET_MESH_Unicast *m;
2321 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2322 m->ttl = htonl (ntohl (m->ttl) - 1);
2324 info->mesh_data->data_len = size;
2325 info->mesh_data->reference_counter = 1;
2326 info->mesh_data->total_out = 1;
2327 neighbor = peer_info_get (peer);
2328 for (p = neighbor->path_head; NULL != p; p = p->next)
2338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2339 " %s IS NOT DIRECTLY CONNECTED\n",
2341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2344 for (p = neighbor->path_head; NULL != p; p = p->next)
2346 struct GNUNET_PeerIdentity debug_id;
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 " path with %u hops through:\n",
2352 for (i = 0; i < p->length; i++)
2354 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 i, GNUNET_i2s(&debug_id));
2361 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2362 GNUNET_free (info->mesh_data->data);
2363 GNUNET_free (info->mesh_data);
2367 info->peer = neighbor;
2368 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2371 * TODO: in this case we only need the service to retransmit
2372 * the message down the path. If we pass the real type to queue_add,
2373 * queue_send will try to build the message from scratch. This can
2374 * probably be done by some other way instead of deleteing the type
2388 * Sends a CREATE PATH message for a path to a peer, properly registrating
2389 * all used resources.
2391 * @param peer PeerInfo of the final peer for whom this path is being created.
2392 * @param p Path itself.
2393 * @param t Tunnel for which the path is created.
2396 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2397 struct MeshTunnel *t)
2399 struct GNUNET_PeerIdentity id;
2400 struct MeshPathInfo *path_info;
2401 struct MeshPeerInfo *neighbor;
2407 p = tree_get_path_to_peer (t->tree, peer->id);
2414 for (i = 0; i < p->length; i++)
2416 if (p->peers[i] == myid)
2419 if (i >= p->length - 1)
2425 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2427 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2428 path_info->path = p;
2430 neighbor = peer_info_get (&id);
2431 path_info->peer = neighbor;
2432 queue_add (path_info,
2433 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2434 sizeof (struct GNUNET_MESH_ManipulatePath) +
2435 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2442 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2444 * @param t Tunnel whose path to destroy.
2445 * @param destination Short ID of the peer to whom the path to destroy.
2448 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2450 struct MeshPeerPath *p;
2453 p = tree_get_path_to_peer (t->tree, destination);
2459 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2460 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2462 struct GNUNET_MESH_ManipulatePath *msg;
2463 struct GNUNET_PeerIdentity *pi;
2467 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2468 msg->header.size = htons (size);
2469 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2470 msg->tid = htonl (t->id.tid);
2471 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2472 for (i = 0; i < p->length; i++)
2474 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2476 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2483 * Try to establish a new connection to this peer.
2484 * Use the best path for the given tunnel.
2485 * If the peer doesn't have any path to it yet, try to get one.
2486 * If the peer already has some path, send a CREATE PATH towards it.
2488 * @param peer PeerInfo of the peer.
2489 * @param t Tunnel for which to create the path, if possible.
2492 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2494 struct MeshPeerPath *p;
2495 struct MeshPathInfo *path_info;
2497 if (NULL != peer->path_head)
2499 p = tree_get_path_to_peer (t->tree, peer->id);
2506 // FIXME always send create path to self
2509 send_create_path (peer, p, t);
2513 struct GNUNET_HashCode hash;
2516 send_client_peer_connected (t, myid);
2517 t->local_tid_dest = next_local_tid++;
2518 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2521 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2522 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2529 else if (NULL == peer->dhtget)
2531 struct GNUNET_PeerIdentity id;
2533 GNUNET_PEER_resolve (peer->id, &id);
2534 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2535 path_info->peer = peer;
2537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2538 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2539 peer->dhtgetcls = path_info;
2540 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2541 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2542 &id.hashPubKey, /* key to search */
2543 dht_replication_level, /* replication level */
2544 GNUNET_DHT_RO_RECORD_ROUTE |
2545 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2546 NULL, /* xquery */ // FIXME BLOOMFILTER
2547 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2548 &dht_get_id_handler, path_info);
2550 /* Otherwise, there is no path but the DHT get is already started. */
2555 * Task to delay the connection of a peer
2557 * @param cls Closure (path info with tunnel and peer to connect).
2558 * Will be free'd on exection.
2559 * @param tc TaskContext
2562 peer_info_connect_task (void *cls,
2563 const struct GNUNET_SCHEDULER_TaskContext *tc)
2565 struct MeshPathInfo *path_info = cls;
2567 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2572 peer_info_connect (path_info->peer, path_info->t);
2578 * Destroy the peer_info and free any allocated resources linked to it
2580 * @param pi The peer_info to destroy.
2582 * @return GNUNET_OK on success
2585 peer_info_destroy (struct MeshPeerInfo *pi)
2587 struct GNUNET_PeerIdentity id;
2588 struct MeshPeerPath *p;
2589 struct MeshPeerPath *nextp;
2591 GNUNET_PEER_resolve (pi->id, &id);
2592 GNUNET_PEER_change_rc (pi->id, -1);
2595 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2599 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2601 if (NULL != pi->dhtget)
2603 GNUNET_DHT_get_stop (pi->dhtget);
2604 GNUNET_free (pi->dhtgetcls);
2610 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2620 * Remove all paths that rely on a direct connection between p1 and p2
2621 * from the peer itself and notify all tunnels about it.
2623 * @param peer PeerInfo of affected peer.
2624 * @param p1 GNUNET_PEER_Id of one peer.
2625 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2628 * TODO: optimize (see below)
2631 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2634 struct MeshPeerPath *p;
2635 struct MeshPeerPath *aux;
2636 struct MeshPeerInfo *peer_d;
2638 unsigned int destroyed;
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2645 p = peer->path_head;
2649 for (i = 0; i < (p->length - 1); i++)
2651 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2652 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2654 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2665 for (i = 0; i < peer->ntunnels; i++)
2667 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2671 * Problem: one or more peers have been deleted from the tunnel tree.
2672 * We don't know who they are to try to add them again.
2673 * We need to try to find a new path for each of the disconnected peers.
2674 * Some of them might already have a path to reach them that does not
2675 * involve p1 and p2. Adding all anew might render in a better tree than
2676 * the trivial immediate fix.
2678 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2679 * its children will be reachable trough him.
2681 peer_d = peer_info_get_short (d);
2684 for (p = peer_d->path_head; NULL != p; p = p->next)
2686 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2694 /* No callback, as peer will be already disconnected and a connection
2695 * scheduled by tunnel_notify_connection_broken.
2697 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2701 peer_info_connect (peer_d, peer->tunnels[i]);
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2709 * Add the path to the peer and update the path used to reach it in case this
2712 * @param peer_info Destination peer to add the path to.
2713 * @param path New path to add. Last peer must be the peer in arg 1.
2714 * Path will be either used of freed if already known.
2715 * @param trusted Do we trust that this path is real?
2718 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2721 struct MeshPeerPath *aux;
2725 if ((NULL == peer_info) || (NULL == path))
2728 path_destroy (path);
2731 if (path->peers[path->length - 1] != peer_info->id)
2734 path_destroy (path);
2737 if (path->length <= 2 && GNUNET_NO == trusted)
2739 /* Only allow CORE to tell us about direct paths */
2740 path_destroy (path);
2743 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2744 for (l = 1; l < path->length; l++)
2746 if (path->peers[l] == myid)
2748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2749 for (l2 = 0; l2 < path->length - l; l2++)
2751 path->peers[l2] = path->peers[l + l2];
2756 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2761 struct GNUNET_PeerIdentity id;
2763 GNUNET_PEER_resolve (peer_info->id, &id);
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2765 path->length, GNUNET_i2s (&id));
2768 l = path_get_length (path);
2775 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2776 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2778 l2 = path_get_length (aux);
2781 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2782 peer_info->path_tail, aux, path);
2787 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2789 path_destroy (path);
2794 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2801 * Add the path to the origin peer and update the path used to reach it in case
2802 * this is the shortest.
2803 * The path is given in peer_info -> destination, therefore we turn the path
2804 * upside down first.
2806 * @param peer_info Peer to add the path to, being the origin of the path.
2807 * @param path New path to add after being inversed.
2808 * @param trusted Do we trust that this path is real?
2811 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2812 struct MeshPeerPath *path, int trusted)
2815 peer_info_add_path (peer_info, path, trusted);
2820 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2821 * to obtain a local peer -> destination path and interning the peer ids.
2823 * @return Newly allocated and created path
2825 static struct MeshPeerPath *
2826 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2827 unsigned int get_path_length,
2828 const struct GNUNET_PeerIdentity *put_path,
2829 unsigned int put_path_length)
2831 struct MeshPeerPath *p;
2837 GNUNET_PEER_change_rc (myid, 1);
2838 i = get_path_length;
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2840 for (i--; i >= 0; i--)
2842 id = GNUNET_PEER_intern (&get_path[i]);
2843 if (p->length > 0 && id == p->peers[p->length - 1])
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2846 GNUNET_PEER_change_rc (id, -1);
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2851 GNUNET_i2s (&get_path[i]));
2853 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2854 p->peers[p->length - 1] = id;
2857 i = put_path_length;
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2859 for (i--; i >= 0; i--)
2861 id = GNUNET_PEER_intern (&put_path[i]);
2864 /* PUT path went through us, so discard the path up until now and start
2865 * from here to get a much shorter (and loop-free) path.
2870 if (p->length > 0 && id == p->peers[p->length - 1])
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2873 GNUNET_PEER_change_rc (id, -1);
2877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2878 GNUNET_i2s (&put_path[i]));
2880 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2881 p->peers[p->length - 1] = id;
2885 if (get_path_length > 0)
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2887 GNUNET_i2s (&get_path[0]));
2888 if (put_path_length > 0)
2889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2890 GNUNET_i2s (&put_path[0]));
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2893 for (i = 0; i < p->length; i++)
2895 struct GNUNET_PeerIdentity peer_id;
2897 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2899 GNUNET_i2s (&peer_id));
2907 * Adds a path to the peer_infos of all the peers in the path
2909 * @param p Path to process.
2910 * @param confirmed Whether we know if the path works or not.
2913 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2917 /* TODO: invert and add */
2918 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2919 for (i++; i < p->length; i++)
2921 struct MeshPeerInfo *aux;
2922 struct MeshPeerPath *copy;
2924 aux = peer_info_get_short (p->peers[i]);
2925 copy = path_duplicate (p);
2926 copy->length = i + 1;
2927 peer_info_add_path (aux, copy, GNUNET_NO);
2933 * Send keepalive packets for a peer
2935 * @param cls Closure (tunnel for which to send the keepalive).
2936 * @param tc Notification context.
2938 * TODO: implement explicit multicast keepalive?
2941 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2945 * Search for a tunnel among the incoming tunnels
2947 * @param tid the local id of the tunnel
2949 * @return tunnel handler, NULL if doesn't exist
2951 static struct MeshTunnel *
2952 tunnel_get_incoming (MESH_TunnelNumber tid)
2954 struct GNUNET_HashCode hash;
2956 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2957 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2958 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2963 * Search for a tunnel among the tunnels for a client
2965 * @param c the client whose tunnels to search in
2966 * @param tid the local id of the tunnel
2968 * @return tunnel handler, NULL if doesn't exist
2970 static struct MeshTunnel *
2971 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2973 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2975 return tunnel_get_incoming (tid);
2979 struct GNUNET_HashCode hash;
2981 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2982 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2988 * Search for a tunnel by global ID using PEER_ID
2990 * @param pi owner of the tunnel
2991 * @param tid global tunnel number
2993 * @return tunnel handler, NULL if doesn't exist
2995 static struct MeshTunnel *
2996 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2998 struct MESH_TunnelID id;
2999 struct GNUNET_HashCode hash;
3004 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3005 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3010 * Search for a tunnel by global ID using full PeerIdentities
3012 * @param oid owner of the tunnel
3013 * @param tid global tunnel number
3015 * @return tunnel handler, NULL if doesn't exist
3017 static struct MeshTunnel *
3018 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3020 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3025 * Delete an active client from the tunnel.
3031 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3035 for (i = 0; i < t->nclients; i++)
3037 if (t->clients[i] == c)
3039 t->clients[i] = t->clients[t->nclients - 1];
3040 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3041 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3043 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3051 * Delete an ignored client from the tunnel.
3057 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3061 for (i = 0; i < t->nignore; i++)
3063 if (t->ignore[i] == c)
3065 t->ignore[i] = t->ignore[t->nignore - 1];
3066 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3074 * Delete a client from the tunnel. It should be only done on
3075 * client disconnection, otherwise use client_ignore_tunnel.
3081 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3083 tunnel_delete_ignored_client (t, c);
3084 tunnel_delete_active_client (t, c);
3089 * Iterator to free MeshTunnelChildInfo of tunnel children.
3091 * @param cls Closure (tunnel info).
3092 * @param key Hash of GNUNET_PEER_Id (unused).
3093 * @param value MeshTunnelChildInfo of the child.
3095 * @return always GNUNET_YES, to keep iterating
3098 tunnel_destroy_child (void *cls,
3099 const struct GNUNET_HashCode * key,
3102 struct MeshTunnelChildInfo *cinfo = value;
3103 struct MeshTunnel *t = cls;
3107 for (c = 0; c < cinfo->send_buffer_n; c++)
3109 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3110 if (NULL != cinfo->send_buffer[i])
3111 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3114 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3116 GNUNET_free_non_null (cinfo->send_buffer);
3117 GNUNET_free (cinfo);
3123 * Callback used to notify a client owner of a tunnel that a peer has
3124 * disconnected, most likely because of a path change.
3126 * @param cls Closure (tunnel this notification is about).
3127 * @param peer_id Short ID of disconnected peer.
3130 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3132 struct MeshTunnel *t = cls;
3133 struct MeshPeerInfo *peer;
3134 struct MeshPathInfo *path_info;
3136 if (NULL != t->owner && NULL != nc)
3138 struct GNUNET_MESH_PeerControl msg;
3140 msg.header.size = htons (sizeof (msg));
3141 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3142 msg.tunnel_id = htonl (t->local_tid);
3143 GNUNET_PEER_resolve (peer_id, &msg.peer);
3144 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3145 &msg.header, GNUNET_NO);
3147 peer = peer_info_get_short (peer_id);
3148 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3149 path_info->peer = peer;
3151 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3156 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3157 * needed rescources.
3158 * If peer already exists, reevaluate shortest path and change if different.
3160 * @param t Tunnel we want to add a new peer to
3161 * @param peer PeerInfo of the peer being added
3165 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3167 struct GNUNET_PeerIdentity id;
3168 struct MeshPeerPath *best_p;
3169 struct MeshPeerPath *p;
3170 unsigned int best_cost;
3173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3174 GNUNET_PEER_resolve (peer->id, &id);
3176 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3179 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3180 GNUNET_assert (GNUNET_OK ==
3181 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3186 if (NULL != (p = peer->path_head))
3189 best_cost = tree_get_path_cost (t->tree, p);
3192 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3199 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3200 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3201 t->path_refresh_task =
3202 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3206 /* Start a DHT get */
3207 peer_info_connect (peer, t);
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3213 * Add a path to a tunnel which we don't own, just to remember the next hop.
3214 * If destination node was already in the tunnel, the first hop information
3215 * will be replaced with the new path.
3217 * @param t Tunnel we want to add a new peer to
3218 * @param p Path to add
3219 * @param own_pos Position of local node in path.
3223 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3224 unsigned int own_pos)
3226 struct GNUNET_PeerIdentity id;
3228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3229 GNUNET_assert (0 != own_pos);
3230 tree_add_path (t->tree, p, NULL, NULL);
3231 if (own_pos < p->length - 1)
3233 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3234 tree_update_first_hops (t->tree, myid, &id);
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3240 * Add a client to a tunnel, initializing all needed data structures.
3242 * @param t Tunnel to which add the client.
3243 * @param c Client which to add to the tunnel.
3246 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3248 struct MeshTunnelClientInfo clinfo;
3250 GNUNET_array_append (t->clients, t->nclients, c);
3251 clinfo.fwd_ack = t->fwd_pid + 1;
3252 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3253 clinfo.fwd_pid = t->fwd_pid;
3254 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3256 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3261 * Notifies a tunnel that a connection has broken that affects at least
3262 * some of its peers. Sends a notification towards the root of the tree.
3263 * In case the peer is the owner of the tree, notifies the client that owns
3264 * the tunnel and tries to reconnect.
3266 * @param t Tunnel affected.
3267 * @param p1 Peer that got disconnected from p2.
3268 * @param p2 Peer that got disconnected from p1.
3270 * @return Short ID of the peer disconnected (either p1 or p2).
3271 * 0 if the tunnel remained unaffected.
3273 static GNUNET_PEER_Id
3274 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3280 tree_notify_connection_broken (t->tree, p1, p2,
3281 &tunnel_notify_client_peer_disconnected,
3283 if (myid != p1 && myid != p2)
3289 if (tree_get_predecessor (t->tree) != 0)
3291 /* We are the peer still connected, notify owner of the disconnection. */
3292 struct GNUNET_MESH_PathBroken msg;
3293 struct GNUNET_PeerIdentity neighbor;
3295 msg.header.size = htons (sizeof (msg));
3296 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3297 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3298 msg.tid = htonl (t->id.tid);
3299 msg.peer1 = my_full_id;
3300 GNUNET_PEER_resolve (pid, &msg.peer2);
3301 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3302 send_message (&msg.header, &neighbor, t);
3310 * Send a multicast packet to a neighbor.
3312 * @param cls Closure (Info about the multicast packet)
3313 * @param neighbor_id Short ID of the neighbor to send the packet to.
3316 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3318 struct MeshData *mdata = cls;
3319 struct MeshTransmissionDescriptor *info;
3320 struct GNUNET_PeerIdentity neighbor;
3322 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3324 info->mesh_data = mdata;
3325 (mdata->reference_counter) ++;
3326 info->destination = neighbor_id;
3327 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3329 GNUNET_i2s (&neighbor));
3330 info->peer = peer_info_get (&neighbor);
3331 GNUNET_assert (NULL != info->peer);
3333 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3334 info->mesh_data->data_len,
3341 * Queue a message in a tunnel in multicast, sending a copy to each child node
3342 * down the local one in the tunnel tree.
3344 * @param t Tunnel in which to send the data.
3345 * @param msg Message to be sent.
3346 * @param internal DEPRECATED Has the service generated this message?
3348 * FIXME remove internal if no use comes up
3351 tunnel_send_multicast (struct MeshTunnel *t,
3352 const struct GNUNET_MessageHeader *msg,
3355 struct MeshData *mdata;
3357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3358 " sending a multicast packet...\n");
3360 mdata = GNUNET_malloc (sizeof (struct MeshData));
3361 mdata->data_len = ntohs (msg->size);
3363 mdata->data = GNUNET_malloc (mdata->data_len);
3364 memcpy (mdata->data, msg, mdata->data_len);
3365 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3367 struct GNUNET_MESH_Multicast *mcast;
3369 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3370 if (t->fwd_queue_n >= t->fwd_queue_max)
3373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3375 " message from %s!\n",
3376 GNUNET_i2s(&mcast->oid));
3377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3378 " message at %s!\n",
3379 GNUNET_i2s(&my_full_id));
3380 GNUNET_free (mdata->data);
3381 GNUNET_free (mdata);
3385 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3387 ntohl (mcast->ttl));
3391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3394 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3395 if (mdata->reference_counter == 0)
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3398 " no one to send data to\n");
3399 GNUNET_free (mdata->data);
3400 GNUNET_free (mdata);
3405 mdata->total_out = mdata->reference_counter;
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3408 " sending a multicast packet done\n");
3414 * Increase the SKIP value of all peers that
3415 * have not received a unicast message.
3417 * @param cls Closure (ID of the peer that HAS received the message).
3418 * @param key ID of the neighbor.
3419 * @param value Information about the neighbor.
3421 * @return GNUNET_YES to keep iterating.
3424 tunnel_add_skip (void *cls,
3425 const struct GNUNET_HashCode * key,
3428 struct GNUNET_PeerIdentity *neighbor = cls;
3429 struct MeshTunnelChildInfo *cinfo = value;
3431 /* TODO compare only pointers? key == neighbor? */
3432 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3442 * @brief Get neighbor's Flow Control information.
3444 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3445 * descendant of the local node in a certain tunnel.
3446 * If the info is not yet there (recently created path), creates the data struct
3447 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3450 * @param t Tunnel related.
3451 * @param peer Neighbor whose Flow Control info is needed.
3453 * @return Neighbor's Flow Control info.
3455 static struct MeshTunnelChildInfo *
3456 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3457 const struct GNUNET_PeerIdentity *peer)
3459 struct MeshTunnelChildInfo *cinfo;
3460 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3466 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3467 cinfo->id = GNUNET_PEER_intern (peer);
3468 cinfo->skip = t->fwd_pid;
3470 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3471 cinfo->fwd_ack = t->fwd_pid + delta;
3472 cinfo->bck_ack = delta;
3474 cinfo->send_buffer =
3475 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3477 GNUNET_assert (GNUNET_OK ==
3478 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3481 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3488 * Get the Flow Control info of a client.
3490 * @param t Tunnel on which to look.
3491 * @param c Client whose ACK to get.
3493 * @return ACK value.
3495 static struct MeshTunnelClientInfo *
3496 tunnel_get_client_fc (struct MeshTunnel *t,
3497 struct MeshClient *c)
3501 for (i = 0; i < t->nclients; i++)
3503 if (t->clients[i] != c)
3505 return &t->clients_fc[i];
3508 return NULL; // avoid compiler / coverity complaints
3513 * Iterator to get the appropiate ACK value from all children nodes.
3515 * @param cls Closue (tunnel).
3516 * @param id Id of the child node.
3519 tunnel_get_child_fwd_ack (void *cls,
3522 struct GNUNET_PeerIdentity peer_id;
3523 struct MeshTunnelChildInfo *cinfo;
3524 struct MeshTunnelChildIteratorContext *ctx = cls;
3525 struct MeshTunnel *t = ctx->t;
3528 GNUNET_PEER_resolve (id, &peer_id);
3529 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3530 ack = cinfo->fwd_ack;
3533 if (GNUNET_NO == ctx->init)
3535 ctx->max_child_ack = ack;
3536 ctx->init = GNUNET_YES;
3539 if (GNUNET_YES == t->speed_min)
3541 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3545 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3552 * Get the maximum PID allowed to transmit to any
3553 * tunnel child of the local peer, depending on the tunnel
3554 * buffering/speed settings.
3558 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3561 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3563 struct MeshTunnelChildIteratorContext ctx;
3565 ctx.max_child_ack = 0;
3567 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3569 if (0 == ctx.nchildren)
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 " tunnel has no children, no FWD ACK\n");
3576 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3577 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3579 return (int64_t) ctx.max_child_ack;
3584 * Set the FWD ACK value of a client in a particular tunnel.
3586 * @param t Tunnel affected.
3587 * @param c Client whose ACK to set.
3588 * @param ack ACK value.
3591 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3592 struct MeshClient *c,
3597 for (i = 0; i < t->nclients; i++)
3599 if (t->clients[i] != c)
3601 t->clients_fc[i].fwd_ack = ack;
3609 * Get the highest ACK value of all clients in a particular tunnel,
3610 * according to the buffering/speed settings.
3612 * @param t Tunnel on which to look.
3614 * @return Corresponding ACK value (max uint32_t).
3615 * If no clients are suscribed, -1LL.
3618 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3623 if (0 == t->nclients)
3625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3626 " tunnel has no clients, no FWD ACK\n");
3630 for (ack = -1LL, i = 0; i < t->nclients; i++)
3633 (GNUNET_YES == t->speed_min &&
3634 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3635 (GNUNET_NO == t->speed_min &&
3636 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3638 ack = t->clients_fc[i].fwd_ack;
3642 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3643 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3645 return (uint32_t) ack;
3650 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3651 * mode and the status of all children nodes.
3655 * @return Maximum PID allowed.
3658 tunnel_get_fwd_ack (struct MeshTunnel *t)
3662 uint32_t buffer_free;
3666 count = t->fwd_pid - t->skip;
3667 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3668 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3669 child_ack = tunnel_get_children_fwd_ack (t);
3670 client_ack = tunnel_get_clients_fwd_ack (t);
3671 if (-1LL == child_ack)
3673 // Node has no children, child_ack AND core buffer are irrelevant.
3674 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3675 return (uint32_t) client_ack;
3677 if (-1LL == client_ack)
3681 if (GNUNET_YES == t->speed_min)
3683 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3684 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3688 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3689 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3691 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3692 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3694 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3695 count, buffer_free, child_ack, client_ack, ack);
3701 * Build a local ACK message and send it to a local client.
3703 * @param t Tunnel on which to send the ACK.
3704 * @param c Client to whom send the ACK.
3705 * @param ack Value of the ACK.
3708 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3710 struct GNUNET_MESH_LocalAck msg;
3712 msg.header.size = htons (sizeof (msg));
3713 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3714 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3715 msg.max_pid = htonl (ack);
3716 GNUNET_SERVER_notification_context_unicast(nc,
3723 * Build an ACK message and queue it to send to the given peer.
3725 * @param t Tunnel on which to send the ACK.
3726 * @param peer Peer to whom send the ACK.
3727 * @param ack Value of the ACK.
3730 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3732 struct GNUNET_MESH_ACK msg;
3734 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3735 msg.header.size = htons (sizeof (msg));
3736 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3737 msg.pid = htonl (ack);
3738 msg.tid = htonl (t->id.tid);
3740 send_message (&msg.header, peer, t);
3745 * Notify a the owner of a tunnel about how many more
3746 * payload packages will we accept on a given tunnel.
3748 * @param t Tunnel on which to send the ACK.
3751 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3756 "Sending client FWD ACK on tunnel %X\n",
3759 ack = tunnel_get_fwd_ack (t);
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3762 if (t->last_fwd_ack == ack)
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3766 t->last_fwd_ack = ack;
3767 send_local_ack (t, t->owner, ack);
3772 * Send an ACK informing the predecessor about the available buffer space.
3773 * In case there is no predecessor, inform the owning client.
3774 * If buffering is off, send only on behalf of children or self if endpoint.
3775 * If buffering is on, send when sent to children and buffer space is free.
3776 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3777 * the ACK itself goes "back" (towards root).
3779 * @param t Tunnel on which to send the ACK.
3780 * @param type Type of message that triggered the ACK transmission.
3783 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3785 struct GNUNET_PeerIdentity id;
3788 if (NULL != t->owner)
3790 tunnel_send_client_fwd_ack (t);
3793 /* Is it after unicast / multicast retransmission? */
3796 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3797 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3799 "ACK due to FWD DATA retransmission\n");
3800 if (GNUNET_YES == t->nobuffer)
3802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3806 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3807 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3813 /* Check if we need no retransmit the ACK */
3814 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3815 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3819 " t->qmax: %u, t->qn: %u\n",
3820 t->fwd_queue_max, t->fwd_queue_n);
3821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3822 " t->pid: %u, t->ack: %u\n",
3823 t->fwd_pid, t->last_fwd_ack);
3827 /* Ok, ACK might be necessary, what PID to ACK? */
3828 ack = tunnel_get_fwd_ack (t);
3830 /* If speed_min and not all children have ack'd, dont send yet */
3831 if (ack == t->last_fwd_ack)
3833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3837 t->last_fwd_ack = ack;
3838 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3839 send_ack (t, &id, ack);
3845 * Iterator to send a child node a BCK ACK to allow him to send more
3848 * @param cls Closure (tunnel).
3849 * @param id Id of the child node.
3852 tunnel_send_child_bck_ack (void *cls,
3855 struct MeshTunnel *t = cls;
3856 struct MeshTunnelChildInfo *cinfo;
3857 struct GNUNET_PeerIdentity peer;
3859 GNUNET_PEER_resolve (id, &peer);
3860 cinfo = tunnel_get_neighbor_fc (t, &peer);
3862 if (cinfo->bck_ack != cinfo->pid &&
3863 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3867 send_ack (t, &peer, cinfo->bck_ack);
3872 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3874 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3876 * @param t Tunnel on which to send the BCK ACKs.
3879 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3882 unsigned int tunnel_delta;
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3886 tunnel_delta = t->bck_ack - t->bck_pid;
3887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3889 /* Find client whom to allow to send to origin (with lowest buffer space) */
3890 for (i = 0; i < t->nclients; i++)
3892 struct MeshTunnelClientInfo *clinfo;
3895 clinfo = &t->clients_fc[i];
3896 delta = clinfo->bck_ack - clinfo->bck_pid;
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3898 t->clients[i]->id, delta);
3900 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3901 (GNUNET_YES == t->nobuffer && 0 == delta))
3905 ack = clinfo->bck_pid;
3906 ack += t->nobuffer ? 1 : tunnel_delta;
3907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3908 " sending ack to client %u: %u\n",
3909 t->clients[i]->id, ack);
3910 send_local_ack (t, t->clients[i], ack);
3911 clinfo->bck_ack = ack;
3918 * Send an ACK informing the children nodes and destination clients about
3919 * the available buffer space.
3920 * If buffering is off, send only on behalf of root (can be self).
3921 * If buffering is on, send when sent to predecessor and buffer space is free.
3922 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3923 * the ACK itself goes "forward" (towards children/clients).
3925 * @param t Tunnel on which to send the ACK.
3926 * @param type Type of message that triggered the ACK transmission.
3929 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3932 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3933 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3934 /* Is it after data to_origin retransmission? */
3937 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3938 if (GNUNET_YES == t->nobuffer)
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3941 " Not sending ACK, nobuffer\n");
3945 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3946 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3952 tunnel_send_clients_bck_ack (t);
3953 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3958 * @brief Re-initiate traffic to this peer if necessary.
3960 * Check if there is traffic queued towards this peer
3961 * and the core transmit handle is NULL (traffic was stalled).
3962 * If so, call core tmt rdy.
3964 * @param cls Closure (unused)
3965 * @param peer_id Short ID of peer to which initiate traffic.
3968 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3970 struct MeshPeerInfo *peer;
3971 struct GNUNET_PeerIdentity id;
3972 struct MeshPeerQueue *q;
3975 peer = peer_info_get_short(peer_id);
3976 if (NULL != peer->core_transmit)
3979 q = queue_get_next(peer);
3982 /* Might br multicast traffic already sent to this particular peer but
3983 * not to other children in this tunnel.
3984 * This way t->queue_n would be > 0 but the queue of this particular peer
3990 GNUNET_PEER_resolve (peer->id, &id);
3991 peer->core_transmit =
3992 GNUNET_CORE_notify_transmit_ready(core_handle,
3995 GNUNET_TIME_UNIT_FOREVER_REL,
4005 * @brief Allow transmission of FWD traffic on this tunnel
4007 * Check if there is traffic queued towards any children
4008 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4010 * @param t Tunnel on which to unlock FWD traffic.
4013 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4015 if (0 == t->fwd_queue_n)
4018 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4023 * @brief Allow transmission of BCK traffic on this tunnel
4025 * Check if there is traffic queued towards the root of the tree
4026 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4028 * @param t Tunnel on which to unlock BCK traffic.
4031 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4033 if (0 == t->bck_queue_n)
4036 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4041 * Send a message to all peers in this tunnel that the tunnel is no longer
4044 * @param t The tunnel whose peers to notify.
4047 tunnel_send_destroy (struct MeshTunnel *t)
4049 struct GNUNET_MESH_TunnelDestroy msg;
4051 msg.header.size = htons (sizeof (msg));
4052 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4053 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4054 msg.tid = htonl (t->id.tid);
4055 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4060 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4062 * @param cls Closure (Tunnel which to cancel).
4063 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4066 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4068 struct MeshTunnel *t = cls;
4069 struct MeshPeerInfo *peer_info;
4070 struct MeshPeerQueue *pq;
4071 struct MeshPeerQueue *next;
4073 peer_info = peer_info_get_short (neighbor_id);
4074 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4077 if (pq->tunnel == t)
4079 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4080 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4081 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4083 // Should have been removed on destroy children
4086 queue_destroy (pq, GNUNET_YES);
4089 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4091 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4092 peer_info->core_transmit = NULL;
4097 * Destroy the tunnel and free any allocated resources linked to it.
4099 * @param t the tunnel to destroy
4101 * @return GNUNET_OK on success
4104 tunnel_destroy (struct MeshTunnel *t)
4106 struct MeshClient *c;
4107 struct GNUNET_HashCode hash;
4118 struct GNUNET_PeerIdentity id;
4120 GNUNET_PEER_resolve (t->id.oid, &id);
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4122 GNUNET_i2s (&id), t->id.tid);
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4128 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4129 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4134 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4137 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4141 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4142 for (i = 0; i < t->nclients; i++)
4146 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4151 for (i = 0; i < t->nignore; i++)
4155 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4160 if (t->nclients > 0)
4163 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4167 GNUNET_free (t->clients);
4169 if (NULL != t->peers)
4171 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4173 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4176 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4177 &tunnel_destroy_child,
4179 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4181 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4183 tree_destroy (t->tree);
4185 if (NULL != t->regex_ctx)
4186 regex_cancel_search (t->regex_ctx);
4187 if (NULL != t->dht_get_type)
4188 GNUNET_DHT_get_stop (t->dht_get_type);
4189 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4190 GNUNET_SCHEDULER_cancel (t->timeout_task);
4191 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4192 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4195 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4196 GNUNET_assert (0 <= n_tunnels);
4203 * Create a new tunnel
4205 * @param owner Who is the owner of the tunnel (short ID).
4206 * @param tid Tunnel Number of the tunnel.
4207 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4208 * @param local Tunnel Number for the tunnel, for the client point of view.
4210 * @return A new initialized tunnel. NULL on error.
4212 static struct MeshTunnel *
4213 tunnel_new (GNUNET_PEER_Id owner,
4214 MESH_TunnelNumber tid,
4215 struct MeshClient *client,
4216 MESH_TunnelNumber local)
4218 struct MeshTunnel *t;
4219 struct GNUNET_HashCode hash;
4221 if (n_tunnels >= max_tunnels && NULL == client)
4224 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4227 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4228 t->bck_queue_max = t->fwd_queue_max;
4229 t->tree = tree_new (owner);
4231 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4232 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4233 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4234 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4235 t->local_tid = local;
4236 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4238 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4240 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4242 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4243 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4250 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4257 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4259 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4260 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4264 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4274 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4275 * that are no longer needed, as well as nodes of no longer reachable peers.
4276 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4278 * @param t Tunnel from which to remove the path.
4279 * @param peer Short id of the peer which should be removed.
4282 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4284 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4290 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4291 * client when the client disconnects. If the client is not the owner, the
4292 * owner will get notified if no more clients are in the tunnel and the client
4293 * get removed from the tunnel's list.
4295 * @param cls closure (client that is disconnecting)
4296 * @param key the hash of the local tunnel id (used to access the hashmap)
4297 * @param value the value stored at the key (tunnel to destroy)
4299 * @return GNUNET_OK on success
4302 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4304 struct MeshTunnel *t = value;
4305 struct MeshClient *c = cls;
4308 send_client_tunnel_disconnect(t, c);
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4312 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4313 tunnel_delete_client(t, c);
4314 client_delete_tunnel(c, t);
4317 tunnel_send_destroy(t);
4318 r = tunnel_destroy (t);
4324 * Timeout function, destroys tunnel if called
4326 * @param cls Closure (tunnel to destroy).
4327 * @param tc TaskContext
4330 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4332 struct MeshTunnel *t = cls;
4334 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4336 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4341 * Resets the tunnel timeout. Starts it if no timeout was running.
4343 * @param t Tunnel whose timeout to reset.
4345 * TODO use heap to improve efficiency of scheduler.
4348 tunnel_reset_timeout (struct MeshTunnel *t)
4350 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4351 GNUNET_SCHEDULER_cancel (t->timeout_task);
4353 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4354 (refresh_path_time, 4), &tunnel_timeout, t);
4358 /******************************************************************************/
4359 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4360 /******************************************************************************/
4363 * Function to send a create path packet to a peer.
4365 * @param cls closure
4366 * @param size number of bytes available in buf
4367 * @param buf where the callee should write the message
4368 * @return number of bytes written to buf
4371 send_core_path_create (void *cls, size_t size, void *buf)
4373 struct MeshPathInfo *info = cls;
4374 struct GNUNET_MESH_ManipulatePath *msg;
4375 struct GNUNET_PeerIdentity *peer_ptr;
4376 struct MeshTunnel *t = info->t;
4377 struct MeshPeerPath *p = info->path;
4382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4384 sizeof (struct GNUNET_MESH_ManipulatePath) +
4385 p->length * sizeof (struct GNUNET_PeerIdentity);
4387 if (size < size_needed || NULL == buf)
4392 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4393 msg->header.size = htons (size_needed);
4394 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4395 msg->tid = ntohl (t->id.tid);
4398 if (GNUNET_YES == t->speed_min)
4399 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4400 if (GNUNET_YES == t->nobuffer)
4401 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4402 msg->opt = htonl(opt);
4404 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4405 for (i = 0; i < p->length; i++)
4407 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4414 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4420 * Fill the core buffer
4422 * @param cls closure (data itself)
4423 * @param size number of bytes available in buf
4424 * @param buf where the callee should write the message
4426 * @return number of bytes written to buf
4429 send_core_data_multicast (void *cls, size_t size, void *buf)
4431 struct MeshTransmissionDescriptor *info = cls;
4434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4435 GNUNET_assert (NULL != info);
4436 GNUNET_assert (NULL != info->peer);
4437 total_size = info->mesh_data->data_len;
4438 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4440 if (total_size > size)
4445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4446 memcpy (buf, info->mesh_data->data, total_size);
4449 struct GNUNET_MESH_Multicast *mc;
4450 struct GNUNET_MessageHeader *mh;
4453 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4455 mc = (struct GNUNET_MESH_Multicast *) mh;
4456 mh = (struct GNUNET_MessageHeader *) &mc[1];
4457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4458 " multicast, payload type %s\n",
4459 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4461 " multicast, payload size %u\n", ntohs (mh->size));
4465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4466 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4470 data_descriptor_decrement_rc (info->mesh_data);
4471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4479 * Creates a path ack message in buf and frees all unused resources.
4481 * @param cls closure (MeshTransmissionDescriptor)
4482 * @param size number of bytes available in buf
4483 * @param buf where the callee should write the message
4484 * @return number of bytes written to buf
4487 send_core_path_ack (void *cls, size_t size, void *buf)
4489 struct MeshTransmissionDescriptor *info = cls;
4490 struct GNUNET_MESH_PathACK *msg = buf;
4492 GNUNET_assert (NULL != info);
4493 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4498 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4499 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4500 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4501 msg->tid = htonl (info->origin->tid);
4502 msg->peer_id = my_full_id;
4505 /* TODO add signature */
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4508 return sizeof (struct GNUNET_MESH_PathACK);
4513 * Free a transmission that was already queued with all resources
4514 * associated to the request.
4516 * @param queue Queue handler to cancel.
4517 * @param clear_cls Is it necessary to free associated cls?
4520 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4522 struct MeshTransmissionDescriptor *dd;
4523 struct MeshPathInfo *path_info;
4525 if (GNUNET_YES == clear_cls)
4527 switch (queue->type)
4529 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4530 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4531 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4534 data_descriptor_decrement_rc (dd->mesh_data);
4536 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4538 path_info = queue->cls;
4539 path_destroy (path_info->path);
4543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4544 " type %s unknown!\n",
4545 GNUNET_MESH_DEBUG_M2S(queue->type));
4547 GNUNET_free_non_null (queue->cls);
4549 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4550 queue->peer->queue_tail,
4552 GNUNET_free (queue);
4557 * @brief Get the next transmittable message from the queue.
4559 * This will be the head, except in the case of being a data packet
4560 * not allowed by the destination peer.
4562 * @param peer Destination peer.
4564 * @return The next viable MeshPeerQueue element to send to that peer.
4565 * NULL when there are no transmittable messages.
4567 struct MeshPeerQueue *
4568 queue_get_next (const struct MeshPeerInfo *peer)
4570 struct MeshPeerQueue *q;
4571 struct MeshTunnel *t;
4572 struct MeshTransmissionDescriptor *info;
4573 struct MeshTunnelChildInfo *cinfo;
4574 struct GNUNET_MESH_Unicast *ucast;
4575 struct GNUNET_MESH_ToOrigin *to_orig;
4576 struct GNUNET_MESH_Multicast *mcast;
4577 struct GNUNET_PeerIdentity id;
4581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4582 for (q = peer->queue_head; NULL != q; q = q->next)
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4588 GNUNET_MESH_DEBUG_M2S(q->type));
4591 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4592 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4593 pid = ntohl (ucast->pid);
4594 GNUNET_PEER_resolve (info->peer->id, &id);
4595 cinfo = tunnel_get_neighbor_fc(t, &id);
4596 ack = cinfo->fwd_ack;
4598 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4599 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4600 pid = ntohl (to_orig->pid);
4603 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4604 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4605 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4607 // Not a multicast payload: multicast control traffic (destroy, etc)
4610 pid = ntohl (mcast->pid);
4611 GNUNET_PEER_resolve (info->peer->id, &id);
4612 cinfo = tunnel_get_neighbor_fc(t, &id);
4613 ack = cinfo->fwd_ack;
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4621 "********* ACK: %u, PID: %u\n",
4623 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4632 "********* NEXT!\n");
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4636 "********* nothing found\n");
4642 * Core callback to write a queued packet to core buffer
4644 * @param cls Closure (peer info).
4645 * @param size Number of bytes available in buf.
4646 * @param buf Where the to write the message.
4648 * @return number of bytes written to buf
4651 queue_send (void *cls, size_t size, void *buf)
4653 struct MeshPeerInfo *peer = cls;
4654 struct GNUNET_MessageHeader *msg;
4655 struct MeshPeerQueue *queue;
4656 struct MeshTunnel *t;
4657 struct MeshTunnelChildInfo *cinfo;
4658 struct GNUNET_PeerIdentity dst_id;
4661 peer->core_transmit = NULL;
4663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4664 queue = queue_get_next(peer);
4666 /* Queue has no internal mesh traffic not sendable payload */
4669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4670 if (NULL == peer->queue_head)
4671 GNUNET_break(0); // Should've been canceled
4674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4676 GNUNET_PEER_resolve (peer->id, &dst_id);
4677 /* Check if buffer size is enough for the message */
4678 if (queue->size > size)
4682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4683 "********* not enough room, reissue\n");
4684 peer->core_transmit =
4685 GNUNET_CORE_notify_transmit_ready(core_handle,
4688 GNUNET_TIME_UNIT_FOREVER_REL,
4695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4698 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4702 "********* unicast: t->q (%u/%u)\n",
4703 t->fwd_queue_n, t->fwd_queue_max);
4705 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4712 switch (queue->type)
4715 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4716 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4717 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4719 "********* raw: %s\n",
4720 GNUNET_MESH_DEBUG_M2S (queue->type));
4722 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4723 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4724 data_size = send_core_data_raw (queue->cls, size, buf);
4725 msg = (struct GNUNET_MessageHeader *) buf;
4726 switch (ntohs (msg->type)) // Type of preconstructed message
4728 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4729 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4731 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4732 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4738 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4741 struct MeshTransmissionDescriptor *info = queue->cls;
4743 if ((1 == info->mesh_data->reference_counter
4744 && GNUNET_YES == t->speed_min)
4746 (info->mesh_data->total_out == info->mesh_data->reference_counter
4747 && GNUNET_NO == t->speed_min))
4749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4750 "********* considered sent\n");
4754 data_size = send_core_data_multicast(queue->cls, size, buf);
4755 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4757 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4759 data_size = send_core_path_create(queue->cls, size, buf);
4761 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4763 data_size = send_core_path_ack(queue->cls, size, buf);
4767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4768 "********* type unknown: %u\n",
4772 switch (queue->type)
4774 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4775 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4776 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4777 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4778 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4781 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4782 "at pos %u (%p) != %p\n",
4783 cinfo->send_buffer_start,
4784 cinfo->send_buffer[cinfo->send_buffer_start],
4787 GNUNET_break(cinfo->send_buffer_n > 0);
4788 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4789 cinfo->send_buffer_n--;
4790 cinfo->send_buffer_start++;
4791 cinfo->send_buffer_start %= t->fwd_queue_max;
4797 /* Free queue, but cls was freed by send_core_* */
4798 queue_destroy (queue, GNUNET_NO);
4800 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4806 /* If more data in queue, send next */
4807 queue = queue_get_next(peer);
4810 struct GNUNET_PeerIdentity id;
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4813 GNUNET_PEER_resolve (peer->id, &id);
4814 peer->core_transmit =
4815 GNUNET_CORE_notify_transmit_ready(core_handle,
4818 GNUNET_TIME_UNIT_FOREVER_REL,
4826 if (NULL != peer->queue_head)
4827 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4828 "********* %s stalled\n",
4829 GNUNET_i2s(&my_full_id));
4831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4837 * Queue and pass message to core when possible.
4839 * @param cls Closure (type dependant).
4840 * @param type Type of the message, 0 for a raw message.
4841 * @param size Size of the message.
4842 * @param dst Neighbor to send message to.
4843 * @param t Tunnel this message belongs to.
4846 queue_add (void *cls, uint16_t type, size_t size,
4847 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4849 struct MeshPeerQueue *queue;
4850 struct MeshTunnelChildInfo *cinfo;
4851 struct GNUNET_PeerIdentity id;
4857 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4858 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4860 n = &t->fwd_queue_n;
4861 max = &t->fwd_queue_max;
4863 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4865 n = &t->bck_queue_n;
4866 max = &t->bck_queue_max;
4871 if (NULL == t->owner)
4872 GNUNET_break_op(0); // TODO: kill connection?
4875 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4877 return; // Drop message
4881 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4887 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4888 GNUNET_PEER_resolve (dst->id, &id);
4889 if (NULL == dst->core_transmit)
4891 dst->core_transmit =
4892 GNUNET_CORE_notify_transmit_ready(core_handle,
4895 GNUNET_TIME_UNIT_FOREVER_REL,
4901 if (NULL == n) // Is this internal mesh traffic?
4904 // It's payload, keep track of buffer per peer.
4905 cinfo = tunnel_get_neighbor_fc(t, &id);
4906 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4907 if (NULL != cinfo->send_buffer[i])
4909 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4910 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4911 cinfo->send_buffer_start++;
4912 cinfo->send_buffer_start %= t->fwd_queue_max;
4913 cinfo->send_buffer_n--;
4915 cinfo->send_buffer[i] = queue;
4916 cinfo->send_buffer_n++;
4917 if (cinfo->send_buffer_n > t->fwd_queue_max)
4920 cinfo->send_buffer_n = t->fwd_queue_max;
4925 /******************************************************************************/
4926 /******************** MESH NETWORK HANDLERS **************************/
4927 /******************************************************************************/
4931 * Core handler for path creation
4933 * @param cls closure
4934 * @param message message
4935 * @param peer peer identity this notification is about
4936 * @param atsi performance data
4937 * @param atsi_count number of records in 'atsi'
4939 * @return GNUNET_OK to keep the connection open,
4940 * GNUNET_SYSERR to close it (signal serious error)
4943 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4944 const struct GNUNET_MessageHeader *message,
4945 const struct GNUNET_ATS_Information *atsi,
4946 unsigned int atsi_count)
4948 unsigned int own_pos;
4951 MESH_TunnelNumber tid;
4952 struct GNUNET_MESH_ManipulatePath *msg;
4953 struct GNUNET_PeerIdentity *pi;
4954 struct GNUNET_HashCode hash;
4955 struct MeshPeerPath *path;
4956 struct MeshPeerInfo *dest_peer_info;
4957 struct MeshPeerInfo *orig_peer_info;
4958 struct MeshTunnel *t;
4960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4961 "Received a path create msg [%s]\n",
4962 GNUNET_i2s (&my_full_id));
4963 size = ntohs (message->size);
4964 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4966 GNUNET_break_op (0);
4970 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4971 if (size % sizeof (struct GNUNET_PeerIdentity))
4973 GNUNET_break_op (0);
4976 size /= sizeof (struct GNUNET_PeerIdentity);
4979 GNUNET_break_op (0);
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4983 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4985 tid = ntohl (msg->tid);
4986 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4988 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4989 t = tunnel_get (pi, tid);
4990 if (NULL == t) // FIXME only for INCOMING tunnels?
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4995 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4998 // FIXME notify failure
5001 opt = ntohl (msg->opt);
5002 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5003 GNUNET_YES : GNUNET_NO;
5004 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
5005 GNUNET_YES : GNUNET_NO;
5006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5007 " speed_min: %d, nobuffer:%d\n",
5008 t->speed_min, t->nobuffer);
5010 if (GNUNET_YES == t->nobuffer)
5012 t->bck_queue_max = 1;
5013 t->fwd_queue_max = 1;
5016 // FIXME only assign a local tid if a local client is interested (on demand)
5017 while (NULL != tunnel_get_incoming (next_local_tid))
5018 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5019 t->local_tid_dest = next_local_tid++;
5020 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5023 tunnel_reset_timeout (t);
5024 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5026 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5027 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5035 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5036 if (NULL == dest_peer_info)
5038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5039 " Creating PeerInfo for destination.\n");
5040 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5041 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5042 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5044 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5046 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5047 if (NULL == orig_peer_info)
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5050 " Creating PeerInfo for origin.\n");
5051 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5052 orig_peer_info->id = GNUNET_PEER_intern (pi);
5053 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5054 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5057 path = path_new (size);
5059 for (i = 0; i < size; i++)
5061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5062 GNUNET_i2s (&pi[i]));
5063 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5064 if (path->peers[i] == myid)
5067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5070 /* cannot be self, must be 'not found' */
5071 /* create path: self not found in path through self */
5072 GNUNET_break_op (0);
5073 path_destroy (path);
5077 path_add_to_peers (path, GNUNET_NO);
5078 tunnel_add_path (t, path, own_pos);
5079 if (own_pos == size - 1)
5081 /* It is for us! Send ack. */
5082 struct MeshTransmissionDescriptor *info;
5084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5085 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5086 if (NULL == t->peers)
5088 /* New tunnel! Notify clients on data. */
5089 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5091 GNUNET_break (GNUNET_SYSERR !=
5092 GNUNET_CONTAINER_multihashmap_put (t->peers,
5093 &my_full_id.hashPubKey,
5096 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5097 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5098 info->origin = &t->id;
5099 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5100 GNUNET_assert (NULL != info->peer);
5102 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5103 sizeof (struct GNUNET_MESH_PathACK),
5109 struct MeshPeerPath *path2;
5111 /* It's for somebody else! Retransmit. */
5112 path2 = path_duplicate (path);
5113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5114 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5115 path2 = path_duplicate (path);
5116 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5117 send_create_path (dest_peer_info, path, t);
5124 * Core handler for path destruction
5126 * @param cls closure
5127 * @param message message
5128 * @param peer peer identity this notification is about
5129 * @param atsi performance data
5130 * @param atsi_count number of records in 'atsi'
5132 * @return GNUNET_OK to keep the connection open,
5133 * GNUNET_SYSERR to close it (signal serious error)
5136 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5137 const struct GNUNET_MessageHeader *message,
5138 const struct GNUNET_ATS_Information *atsi,
5139 unsigned int atsi_count)
5141 struct GNUNET_MESH_ManipulatePath *msg;
5142 struct GNUNET_PeerIdentity *pi;
5143 struct MeshPeerPath *path;
5144 struct MeshTunnel *t;
5145 unsigned int own_pos;
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5150 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5151 size = ntohs (message->size);
5152 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5154 GNUNET_break_op (0);
5158 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5159 if (size % sizeof (struct GNUNET_PeerIdentity))
5161 GNUNET_break_op (0);
5164 size /= sizeof (struct GNUNET_PeerIdentity);
5167 GNUNET_break_op (0);
5170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5172 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5173 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5175 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5177 t = tunnel_get (pi, ntohl (msg->tid));
5180 /* TODO notify back: we don't know this tunnel */
5181 GNUNET_break_op (0);
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5185 path = path_new (size);
5187 for (i = 0; i < size; i++)
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5190 GNUNET_i2s (&pi[i]));
5191 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5192 if (path->peers[i] == myid)
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5196 if (own_pos < path->length - 1)
5197 send_message (message, &pi[own_pos + 1], t);
5199 send_client_tunnel_disconnect(t, NULL);
5201 tunnel_delete_peer (t, path->peers[path->length - 1]);
5202 path_destroy (path);
5208 * Core handler for notifications of broken paths
5210 * @param cls closure
5211 * @param message message
5212 * @param peer peer identity this notification is about
5213 * @param atsi performance data
5214 * @param atsi_count number of records in 'atsi'
5216 * @return GNUNET_OK to keep the connection open,
5217 * GNUNET_SYSERR to close it (signal serious error)
5220 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5221 const struct GNUNET_MessageHeader *message,
5222 const struct GNUNET_ATS_Information *atsi,
5223 unsigned int atsi_count)
5225 struct GNUNET_MESH_PathBroken *msg;
5226 struct MeshTunnel *t;
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5229 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5230 msg = (struct GNUNET_MESH_PathBroken *) message;
5231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5232 GNUNET_i2s (&msg->peer1));
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5234 GNUNET_i2s (&msg->peer2));
5235 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5238 GNUNET_break_op (0);
5241 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5242 GNUNET_PEER_search (&msg->peer2));
5249 * Core handler for tunnel destruction
5251 * @param cls closure
5252 * @param message message
5253 * @param peer peer identity this notification is about
5254 * @param atsi performance data
5255 * @param atsi_count number of records in 'atsi'
5257 * @return GNUNET_OK to keep the connection open,
5258 * GNUNET_SYSERR to close it (signal serious error)
5261 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5262 const struct GNUNET_MessageHeader *message,
5263 const struct GNUNET_ATS_Information *atsi,
5264 unsigned int atsi_count)
5266 struct GNUNET_MESH_TunnelDestroy *msg;
5267 struct MeshTunnel *t;
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5270 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5271 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5273 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5274 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5277 /* Probably already got the message from another path,
5278 * destroyed the tunnel and retransmitted to children.
5281 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5284 if (t->id.oid == myid)
5286 GNUNET_break_op (0);
5289 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5291 /* Tunnel was incoming, notify clients */
5292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5293 t->local_tid, t->local_tid_dest);
5294 send_clients_tunnel_destroy (t);
5296 tunnel_send_destroy (t);
5297 t->destroy = GNUNET_YES;
5298 // TODO: add timeout to destroy the tunnel anyway
5304 * Core handler for mesh network traffic going from the origin to a peer
5306 * @param cls closure
5307 * @param peer peer identity this notification is about
5308 * @param message message
5309 * @param atsi performance data
5310 * @param atsi_count number of records in 'atsi'
5311 * @return GNUNET_OK to keep the connection open,
5312 * GNUNET_SYSERR to close it (signal serious error)
5315 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5316 const struct GNUNET_MessageHeader *message,
5317 const struct GNUNET_ATS_Information *atsi,
5318 unsigned int atsi_count)
5320 struct GNUNET_MESH_Unicast *msg;
5321 struct GNUNET_PeerIdentity *neighbor;
5322 struct MeshTunnelChildInfo *cinfo;
5323 struct MeshTunnel *t;
5324 GNUNET_PEER_Id dest_id;
5329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5332 size = ntohs (message->size);
5334 sizeof (struct GNUNET_MESH_Unicast) +
5335 sizeof (struct GNUNET_MessageHeader))
5340 msg = (struct GNUNET_MESH_Unicast *) message;
5341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5342 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5344 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5347 /* TODO notify back: we don't know this tunnel */
5348 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5349 GNUNET_break_op (0);
5352 pid = ntohl (msg->pid);
5353 if (t->fwd_pid == pid)
5355 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5356 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5357 " Already seen pid %u, DROPPING!\n", pid);
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5363 " pid %u not seen yet, forwarding\n", pid);
5366 t->skip += (pid - t->fwd_pid) - 1;
5369 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5371 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5372 GNUNET_break_op (0);
5373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5374 "Received PID %u, ACK %u\n",
5375 pid, t->last_fwd_ack);
5379 tunnel_reset_timeout (t);
5380 dest_id = GNUNET_PEER_search (&msg->destination);
5381 if (dest_id == myid)
5383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5384 " it's for us! sending to clients...\n");
5385 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5386 send_subscribed_clients (message, &msg[1].header, t);
5387 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5390 ttl = ntohl (msg->ttl);
5391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5394 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5395 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5396 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5400 " not for us, retransmitting...\n");
5402 neighbor = tree_get_first_hop (t->tree, dest_id);
5403 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5405 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5408 if (GNUNET_YES == t->nobuffer &&
5409 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5411 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5412 GNUNET_break_op (0);
5415 send_message (message, neighbor, t);
5416 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5422 * Core handler for mesh network traffic going from the origin to all peers
5424 * @param cls closure
5425 * @param message message
5426 * @param peer peer identity this notification is about
5427 * @param atsi performance data
5428 * @param atsi_count number of records in 'atsi'
5429 * @return GNUNET_OK to keep the connection open,
5430 * GNUNET_SYSERR to close it (signal serious error)
5432 * TODO: Check who we got this from, to validate route.
5435 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5436 const struct GNUNET_MessageHeader *message,
5437 const struct GNUNET_ATS_Information *atsi,
5438 unsigned int atsi_count)
5440 struct GNUNET_MESH_Multicast *msg;
5441 struct MeshTunnel *t;
5445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5447 size = ntohs (message->size);
5448 if (sizeof (struct GNUNET_MESH_Multicast) +
5449 sizeof (struct GNUNET_MessageHeader) > size)
5451 GNUNET_break_op (0);
5454 msg = (struct GNUNET_MESH_Multicast *) message;
5455 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5459 /* TODO notify that we dont know that tunnel */
5460 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5461 GNUNET_break_op (0);
5464 pid = ntohl (msg->pid);
5465 if (t->fwd_pid == pid)
5467 /* already seen this packet, drop */
5468 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5470 " Already seen pid %u, DROPPING!\n", pid);
5471 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5477 " pid %u not seen yet, forwarding\n", pid);
5479 t->skip += (pid - t->fwd_pid) - 1;
5481 tunnel_reset_timeout (t);
5483 /* Transmit to locally interested clients */
5484 if (NULL != t->peers &&
5485 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5487 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5488 send_subscribed_clients (message, &msg[1].header, t);
5489 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5492 if (ntohl (msg->ttl) == 0)
5494 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5495 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5496 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5499 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5500 tunnel_send_multicast (t, message, GNUNET_NO);
5506 * Core handler for mesh network traffic toward the owner of a tunnel
5508 * @param cls closure
5509 * @param message message
5510 * @param peer peer identity this notification is about
5511 * @param atsi performance data
5512 * @param atsi_count number of records in 'atsi'
5514 * @return GNUNET_OK to keep the connection open,
5515 * GNUNET_SYSERR to close it (signal serious error)
5518 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5519 const struct GNUNET_MessageHeader *message,
5520 const struct GNUNET_ATS_Information *atsi,
5521 unsigned int atsi_count)
5523 struct GNUNET_MESH_ToOrigin *msg;
5524 struct GNUNET_PeerIdentity id;
5525 struct MeshPeerInfo *peer_info;
5526 struct MeshTunnel *t;
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5532 size = ntohs (message->size);
5533 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5534 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5536 GNUNET_break_op (0);
5539 msg = (struct GNUNET_MESH_ToOrigin *) message;
5540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5541 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5542 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5546 /* TODO notify that we dont know this tunnel (whom)? */
5547 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5548 GNUNET_break_op (0);
5552 if (NULL != t->owner)
5555 struct GNUNET_MESH_ToOrigin *copy;
5557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5558 " it's for us! sending to clients...\n");
5559 /* TODO signature verification */
5560 memcpy (cbuf, message, size);
5561 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5562 copy->tid = htonl (t->local_tid);
5563 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5564 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5565 ©->header, GNUNET_NO);
5566 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5570 " not for us, retransmitting...\n");
5572 peer_info = peer_info_get (&msg->oid);
5573 if (NULL == peer_info)
5575 /* unknown origin of tunnel */
5579 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5580 send_message (message, &id, t);
5581 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5588 * Core handler for mesh network traffic point-to-point acks.
5590 * @param cls closure
5591 * @param message message
5592 * @param peer peer identity this notification is about
5593 * @param atsi performance data
5594 * @param atsi_count number of records in 'atsi'
5596 * @return GNUNET_OK to keep the connection open,
5597 * GNUNET_SYSERR to close it (signal serious error)
5600 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5601 const struct GNUNET_MessageHeader *message,
5602 const struct GNUNET_ATS_Information *atsi,
5603 unsigned int atsi_count)
5605 struct GNUNET_MESH_ACK *msg;
5606 struct MeshTunnel *t;
5609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5611 msg = (struct GNUNET_MESH_ACK *) message;
5613 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5617 /* TODO notify that we dont know this tunnel (whom)? */
5618 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5619 GNUNET_break_op (0);
5622 ack = ntohl (msg->pid);
5624 /* Is this a forward or backward ACK? */
5625 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5627 struct MeshTunnelChildInfo *cinfo;
5630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5631 cinfo = tunnel_get_neighbor_fc (t, peer);
5632 cinfo->fwd_ack = ack;
5633 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5634 tunnel_unlock_fwd_queues (t);
5638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5640 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5641 tunnel_unlock_bck_queue (t);
5648 * Core handler for path ACKs
5650 * @param cls closure
5651 * @param message message
5652 * @param peer peer identity this notification is about
5653 * @param atsi performance data
5654 * @param atsi_count number of records in 'atsi'
5656 * @return GNUNET_OK to keep the connection open,
5657 * GNUNET_SYSERR to close it (signal serious error)
5660 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5661 const struct GNUNET_MessageHeader *message,
5662 const struct GNUNET_ATS_Information *atsi,
5663 unsigned int atsi_count)
5665 struct GNUNET_MESH_PathACK *msg;
5666 struct GNUNET_PeerIdentity id;
5667 struct MeshPeerInfo *peer_info;
5668 struct MeshPeerPath *p;
5669 struct MeshTunnel *t;
5671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5672 GNUNET_i2s (&my_full_id));
5673 msg = (struct GNUNET_MESH_PathACK *) message;
5674 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5677 /* TODO notify that we don't know the tunnel */
5678 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5680 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5684 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5686 peer_info = peer_info_get (&msg->peer_id);
5687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5688 GNUNET_i2s (&msg->peer_id));
5689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5692 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5695 "connect_by_string completed, stopping search\n");
5696 regex_cancel_search (t->regex_ctx);
5697 t->regex_ctx = NULL;
5700 /* Add paths to peers? */
5701 p = tree_get_path_to_peer (t->tree, peer_info->id);
5704 path_add_to_peers (p, GNUNET_YES);
5712 /* Message for us? */
5713 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5716 if (NULL == t->owner)
5718 GNUNET_break_op (0);
5721 if (NULL != t->dht_get_type)
5723 GNUNET_DHT_get_stop (t->dht_get_type);
5724 t->dht_get_type = NULL;
5726 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5728 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5729 send_client_peer_connected (t, peer_info->id);
5734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5735 " not for us, retransmitting...\n");
5736 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5737 peer_info = peer_info_get (&msg->oid);
5738 if (NULL == peer_info)
5740 /* If we know the tunnel, we should DEFINITELY know the peer */
5744 send_message (message, &id, t);
5750 * Core handler for mesh keepalives.
5752 * @param cls closure
5753 * @param message message
5754 * @param peer peer identity this notification is about
5755 * @param atsi performance data
5756 * @param atsi_count number of records in 'atsi'
5757 * @return GNUNET_OK to keep the connection open,
5758 * GNUNET_SYSERR to close it (signal serious error)
5760 * TODO: Check who we got this from, to validate route.
5763 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5764 const struct GNUNET_MessageHeader *message,
5765 const struct GNUNET_ATS_Information *atsi,
5766 unsigned int atsi_count)
5768 struct GNUNET_MESH_TunnelKeepAlive *msg;
5769 struct MeshTunnel *t;
5771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5774 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5775 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5779 /* TODO notify that we dont know that tunnel */
5780 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5781 GNUNET_break_op (0);
5785 tunnel_reset_timeout (t);
5787 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5788 tunnel_send_multicast (t, message, GNUNET_NO);
5795 * Functions to handle messages from core
5797 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5798 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5799 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5800 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5801 sizeof (struct GNUNET_MESH_PathBroken)},
5802 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5803 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5804 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5805 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5806 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5807 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5808 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5809 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5810 sizeof (struct GNUNET_MESH_ACK)},
5811 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5812 sizeof (struct GNUNET_MESH_PathACK)},
5818 /******************************************************************************/
5819 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5820 /******************************************************************************/
5823 * deregister_app: iterator for removing each application registered by a client
5825 * @param cls closure
5826 * @param key the hash of the application id (used to access the hashmap)
5827 * @param value the value stored at the key (client)
5829 * @return GNUNET_OK on success
5832 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5834 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5835 GNUNET_break (GNUNET_YES ==
5836 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5842 * notify_client_connection_failure: notify a client that the connection to the
5843 * requested remote peer is not possible (for instance, no route found)
5844 * Function called when the socket is ready to queue more data. "buf" will be
5845 * NULL and "size" zero if the socket was closed for writing in the meantime.
5847 * @param cls closure
5848 * @param size number of bytes available in buf
5849 * @param buf where the callee should write the message
5850 * @return number of bytes written to buf
5853 notify_client_connection_failure (void *cls, size_t size, void *buf)
5856 struct MeshPeerInfo *peer_info;
5857 struct GNUNET_MESH_PeerControl *msg;
5858 struct GNUNET_PeerIdentity id;
5860 if (0 == size && NULL == buf)
5862 // TODO retry? cancel?
5866 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5867 peer_info = (struct MeshPeerInfo *) cls;
5868 msg = (struct GNUNET_MESH_PeerControl *) buf;
5869 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5870 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5871 // msg->tunnel_id = htonl(peer_info->t->tid);
5872 GNUNET_PEER_resolve (peer_info->id, &id);
5873 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5881 * Send keepalive packets for a peer
5883 * @param cls Closure (tunnel for which to send the keepalive).
5884 * @param tc Notification context.
5887 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5889 struct MeshTunnel *t = cls;
5890 struct GNUNET_MESH_TunnelKeepAlive *msg;
5891 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5894 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5895 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5901 "sending keepalive for tunnel %d\n", t->id.tid);
5903 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5904 msg->header.size = htons (size);
5905 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5906 msg->oid = my_full_id;
5907 msg->tid = htonl (t->id.tid);
5908 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5910 t->path_refresh_task =
5911 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5917 * Function to process paths received for a new peer addition. The recorded
5918 * paths form the initial tunnel, which can be optimized later.
5919 * Called on each result obtained for the DHT search.
5921 * @param cls closure
5922 * @param exp when will this value expire
5923 * @param key key of the result
5924 * @param get_path path of the get request
5925 * @param get_path_length lenght of get_path
5926 * @param put_path path of the put request
5927 * @param put_path_length length of the put_path
5928 * @param type type of the result
5929 * @param size number of bytes in data
5930 * @param data pointer to the result data
5932 * TODO: re-issue the request after certain time? cancel after X results?
5935 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5936 const struct GNUNET_HashCode * key,
5937 const struct GNUNET_PeerIdentity *get_path,
5938 unsigned int get_path_length,
5939 const struct GNUNET_PeerIdentity *put_path,
5940 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5941 size_t size, const void *data)
5943 struct MeshPathInfo *path_info = cls;
5944 struct MeshPeerPath *p;
5945 struct GNUNET_PeerIdentity pi;
5948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5949 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5952 p = path_build_from_dht (get_path, get_path_length, put_path,
5954 path_add_to_peers (p, GNUNET_NO);
5956 for (i = 0; i < path_info->peer->ntunnels; i++)
5958 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5959 peer_info_connect (path_info->peer, path_info->t);
5967 * Function to process paths received for a new peer addition. The recorded
5968 * paths form the initial tunnel, which can be optimized later.
5969 * Called on each result obtained for the DHT search.
5971 * @param cls closure
5972 * @param exp when will this value expire
5973 * @param key key of the result
5974 * @param get_path path of the get request
5975 * @param get_path_length lenght of get_path
5976 * @param put_path path of the put request
5977 * @param put_path_length length of the put_path
5978 * @param type type of the result
5979 * @param size number of bytes in data
5980 * @param data pointer to the result data
5983 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5984 const struct GNUNET_HashCode * key,
5985 const struct GNUNET_PeerIdentity *get_path,
5986 unsigned int get_path_length,
5987 const struct GNUNET_PeerIdentity *put_path,
5988 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5989 size_t size, const void *data)
5991 const struct PBlock *pb = data;
5992 const struct GNUNET_PeerIdentity *pi = &pb->id;
5993 struct MeshTunnel *t = cls;
5994 struct MeshPeerInfo *peer_info;
5995 struct MeshPeerPath *p;
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5998 if (size != sizeof (struct PBlock))
6000 GNUNET_break_op (0);
6003 if (ntohl(pb->type) != t->type)
6005 GNUNET_break_op (0);
6008 GNUNET_assert (NULL != t->owner);
6009 peer_info = peer_info_get (pi);
6010 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6012 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6014 p = path_build_from_dht (get_path, get_path_length, put_path,
6016 path_add_to_peers (p, GNUNET_NO);
6018 tunnel_add_peer (t, peer_info);
6019 peer_info_connect (peer_info, t);
6024 * Function to process DHT string to regex matching.
6025 * Called on each result obtained for the DHT search.
6027 * @param cls closure (search context)
6028 * @param exp when will this value expire
6029 * @param key key of the result
6030 * @param get_path path of the get request (not used)
6031 * @param get_path_length lenght of get_path (not used)
6032 * @param put_path path of the put request (not used)
6033 * @param put_path_length length of the put_path (not used)
6034 * @param type type of the result
6035 * @param size number of bytes in data
6036 * @param data pointer to the result data
6039 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6040 const struct GNUNET_HashCode * key,
6041 const struct GNUNET_PeerIdentity *get_path,
6042 unsigned int get_path_length,
6043 const struct GNUNET_PeerIdentity *put_path,
6044 unsigned int put_path_length,
6045 enum GNUNET_BLOCK_Type type,
6046 size_t size, const void *data)
6048 const struct MeshRegexAccept *block = data;
6049 struct MeshRegexSearchContext *ctx = cls;
6050 struct MeshRegexSearchInfo *info = ctx->info;
6051 struct MeshPeerPath *p;
6052 struct MeshPeerInfo *peer_info;
6054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6057 peer_info = peer_info_get(&block->id);
6058 p = path_build_from_dht (get_path, get_path_length, put_path,
6060 path_add_to_peers (p, GNUNET_NO);
6063 tunnel_add_peer (info->t, peer_info);
6064 peer_info_connect (peer_info, info->t);
6065 if (0 == info->peer)
6067 info->peer = peer_info->id;
6071 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6074 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6075 ®ex_connect_timeout,
6083 * Function to process DHT string to regex matching.
6084 * Called on each result obtained for the DHT search.
6086 * @param cls closure (search context)
6087 * @param exp when will this value expire
6088 * @param key key of the result
6089 * @param get_path path of the get request (not used)
6090 * @param get_path_length lenght of get_path (not used)
6091 * @param put_path path of the put request (not used)
6092 * @param put_path_length length of the put_path (not used)
6093 * @param type type of the result
6094 * @param size number of bytes in data
6095 * @param data pointer to the result data
6097 * TODO: re-issue the request after certain time? cancel after X results?
6100 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6101 const struct GNUNET_HashCode * key,
6102 const struct GNUNET_PeerIdentity *get_path,
6103 unsigned int get_path_length,
6104 const struct GNUNET_PeerIdentity *put_path,
6105 unsigned int put_path_length,
6106 enum GNUNET_BLOCK_Type type,
6107 size_t size, const void *data)
6109 const struct MeshRegexBlock *block = data;
6110 struct MeshRegexSearchContext *ctx = cls;
6111 struct MeshRegexSearchInfo *info = ctx->info;
6115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6116 "DHT GET STRING RETURNED RESULTS\n");
6117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6118 " key: %s\n", GNUNET_h2s (key));
6120 copy = GNUNET_malloc (size);
6121 memcpy (copy, data, size);
6122 GNUNET_break (GNUNET_OK ==
6123 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6124 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6125 len = ntohl (block->n_proof);
6127 char proof[len + 1];
6129 memcpy (proof, &block[1], len);
6131 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6133 GNUNET_break_op (0);
6137 len = strlen (info->description);
6138 if (len == ctx->position) // String processed
6140 if (GNUNET_YES == ntohl (block->accepting))
6142 regex_find_path(key, ctx);
6146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6147 // FIXME REGEX this block not successful, wait for more? start timeout?
6152 regex_next_edge (block, size, ctx);
6157 /******************************************************************************/
6158 /********************* MESH LOCAL HANDLES **************************/
6159 /******************************************************************************/
6163 * Handler for client disconnection
6165 * @param cls closure
6166 * @param client identification of the client; NULL
6167 * for the last call when the server is destroyed
6170 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6172 struct MeshClient *c;
6173 struct MeshClient *next;
6176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6185 if (c->handle != client)
6187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6193 GNUNET_SERVER_client_drop (c->handle);
6194 c->shutting_down = GNUNET_YES;
6195 GNUNET_assert (NULL != c->own_tunnels);
6196 GNUNET_assert (NULL != c->incoming_tunnels);
6197 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6198 &tunnel_destroy_iterator, c);
6199 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6200 &tunnel_destroy_iterator, c);
6201 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6202 &tunnel_destroy_iterator, c);
6203 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6204 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6205 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6207 /* deregister clients applications */
6208 if (NULL != c->apps)
6210 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6211 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6213 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6214 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6216 GNUNET_SCHEDULER_cancel (announce_applications_task);
6217 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6219 if (NULL != c->types)
6220 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6221 for (i = 0; i < c->n_regex; i++)
6223 GNUNET_free (c->regexes[i]);
6225 GNUNET_free_non_null (c->regexes);
6226 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6227 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6229 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6232 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6241 * Handler for new clients
6243 * @param cls closure
6244 * @param client identification of the client
6245 * @param message the actual message, which includes messages the client wants
6248 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6249 const struct GNUNET_MessageHeader *message)
6251 struct GNUNET_MESH_ClientConnect *cc_msg;
6252 struct MeshClient *c;
6253 GNUNET_MESH_ApplicationType *a;
6260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6261 /* Check data sanity */
6262 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6263 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6264 ntypes = ntohs (cc_msg->types);
6265 napps = ntohs (cc_msg->applications);
6267 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6274 /* Create new client structure */
6275 c = GNUNET_malloc (sizeof (struct MeshClient));
6276 c->id = next_client_id++;
6277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6279 GNUNET_SERVER_client_keep (client);
6280 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6283 GNUNET_MESH_ApplicationType at;
6284 struct GNUNET_HashCode hc;
6286 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6287 for (i = 0; i < napps; i++)
6290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6291 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6292 /* store in clients hashmap */
6293 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6294 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6295 /* store in global hashmap, for announcements */
6296 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6297 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6299 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6300 announce_applications_task =
6301 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6307 struct GNUNET_HashCode hc;
6309 t = (uint16_t *) & a[napps];
6310 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6311 for (i = 0; i < ntypes; i++)
6314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6315 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6317 /* store in clients hashmap */
6318 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6319 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6320 /* store in global hashmap */
6321 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6322 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6326 " client has %u+%u subscriptions\n", napps, ntypes);
6328 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6329 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6330 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6331 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6332 GNUNET_SERVER_notification_context_add (nc, client);
6333 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6335 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6341 * Handler for clients announcing available services by a regular expression.
6343 * @param cls closure
6344 * @param client identification of the client
6345 * @param message the actual message, which includes messages the client wants
6348 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6349 const struct GNUNET_MessageHeader *message)
6351 struct MeshClient *c;
6355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6357 /* Sanity check for client registration */
6358 if (NULL == (c = client_get (client)))
6361 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6366 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6367 regex = GNUNET_malloc (len + 1);
6368 memcpy (regex, &message[1], len);
6370 GNUNET_array_append (c->regexes, c->n_regex, regex);
6371 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6373 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6379 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6385 * Handler for requests of new tunnels
6387 * @param cls closure
6388 * @param client identification of the client
6389 * @param message the actual message
6392 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6393 const struct GNUNET_MessageHeader *message)
6395 struct GNUNET_MESH_TunnelMessage *t_msg;
6396 struct MeshTunnel *t;
6397 struct MeshClient *c;
6398 MESH_TunnelNumber tid;
6400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6402 /* Sanity check for client registration */
6403 if (NULL == (c = client_get (client)))
6406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6411 /* Message sanity check */
6412 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6415 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6419 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6420 /* Sanity check for tunnel numbering */
6421 tid = ntohl (t_msg->tunnel_id);
6422 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6425 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6428 /* Sanity check for duplicate tunnel IDs */
6429 if (NULL != tunnel_get_by_local_id (c, tid))
6432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6436 while (NULL != tunnel_get_by_pi (myid, next_tid))
6437 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6438 t = tunnel_new (myid, next_tid++, c, tid);
6441 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6446 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6448 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6449 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6452 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6458 * Handler for requests of deleting tunnels
6460 * @param cls closure
6461 * @param client identification of the client
6462 * @param message the actual message
6465 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6466 const struct GNUNET_MessageHeader *message)
6468 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6469 struct MeshClient *c;
6470 struct MeshTunnel *t;
6471 MESH_TunnelNumber tid;
6473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6474 "Got a DESTROY TUNNEL from client!\n");
6476 /* Sanity check for client registration */
6477 if (NULL == (c = client_get (client)))
6480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6485 /* Message sanity check */
6486 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6493 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6495 /* Retrieve tunnel */
6496 tid = ntohl (tunnel_msg->tunnel_id);
6497 t = tunnel_get_by_local_id(c, tid);
6500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6505 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6507 client_ignore_tunnel (c, t);
6509 // TODO: when to destroy incoming tunnel?
6510 if (t->nclients == 0)
6512 GNUNET_assert (GNUNET_YES ==
6513 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6515 GNUNET_assert (GNUNET_YES ==
6516 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6517 &my_full_id.hashPubKey,
6521 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6524 send_client_tunnel_disconnect(t, c);
6525 client_delete_tunnel(c, t);
6527 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6529 tunnel_send_destroy (t);
6530 t->destroy = GNUNET_YES;
6531 // The tunnel will be destroyed when the last message is transmitted.
6532 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6538 * Handler for requests of seeting tunnel's speed.
6540 * @param cls Closure (unused).
6541 * @param client Identification of the client.
6542 * @param message The actual message.
6545 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6546 const struct GNUNET_MessageHeader *message)
6548 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6549 struct MeshClient *c;
6550 struct MeshTunnel *t;
6551 MESH_TunnelNumber tid;
6553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6554 "Got a SPEED request from client!\n");
6556 /* Sanity check for client registration */
6557 if (NULL == (c = client_get (client)))
6560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6566 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6568 /* Retrieve tunnel */
6569 tid = ntohl (tunnel_msg->tunnel_id);
6570 t = tunnel_get_by_local_id(c, tid);
6573 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6579 switch (ntohs(message->type))
6581 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6582 t->speed_min = GNUNET_YES;
6584 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6585 t->speed_min = GNUNET_NO;
6590 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6595 * Handler for requests of seeting tunnel's buffering policy.
6597 * @param cls Closure (unused).
6598 * @param client Identification of the client.
6599 * @param message The actual message.
6602 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6603 const struct GNUNET_MessageHeader *message)
6605 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6606 struct MeshClient *c;
6607 struct MeshTunnel *t;
6608 MESH_TunnelNumber tid;
6610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6611 "Got a BUFFER request from client!\n");
6613 /* Sanity check for client registration */
6614 if (NULL == (c = client_get (client)))
6617 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6622 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6624 /* Retrieve tunnel */
6625 tid = ntohl (tunnel_msg->tunnel_id);
6626 t = tunnel_get_by_local_id(c, tid);
6629 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6635 switch (ntohs(message->type))
6637 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6638 t->nobuffer = GNUNET_NO;
6640 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6641 t->nobuffer = GNUNET_YES;
6647 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6652 * Handler for connection requests to new peers
6654 * @param cls closure
6655 * @param client identification of the client
6656 * @param message the actual message (PeerControl)
6659 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6660 const struct GNUNET_MessageHeader *message)
6662 struct GNUNET_MESH_PeerControl *peer_msg;
6663 struct MeshPeerInfo *peer_info;
6664 struct MeshClient *c;
6665 struct MeshTunnel *t;
6666 MESH_TunnelNumber tid;
6668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6669 /* Sanity check for client registration */
6670 if (NULL == (c = client_get (client)))
6673 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6678 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6680 /* Sanity check for message size */
6681 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6688 /* Tunnel exists? */
6689 tid = ntohl (peer_msg->tunnel_id);
6690 t = tunnel_get_by_local_id (c, tid);
6694 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6698 /* Does client own tunnel? */
6699 if (t->owner->handle != client)
6702 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6706 GNUNET_i2s (&peer_msg->peer));
6707 peer_info = peer_info_get (&peer_msg->peer);
6709 tunnel_add_peer (t, peer_info);
6710 peer_info_connect (peer_info, t);
6712 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6718 * Handler for disconnection requests of peers in a tunnel
6720 * @param cls closure
6721 * @param client identification of the client
6722 * @param message the actual message (PeerControl)
6725 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6726 const struct GNUNET_MessageHeader *message)
6728 struct GNUNET_MESH_PeerControl *peer_msg;
6729 struct MeshPeerInfo *peer_info;
6730 struct MeshClient *c;
6731 struct MeshTunnel *t;
6732 MESH_TunnelNumber tid;
6734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6735 /* Sanity check for client registration */
6736 if (NULL == (c = client_get (client)))
6739 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6744 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6746 /* Sanity check for message size */
6747 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6754 /* Tunnel exists? */
6755 tid = ntohl (peer_msg->tunnel_id);
6756 t = tunnel_get_by_local_id (c, tid);
6760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6765 /* Does client own tunnel? */
6766 if (t->owner->handle != client)
6769 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6774 GNUNET_i2s (&peer_msg->peer));
6775 /* Is the peer in the tunnel? */
6777 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6778 if (NULL == peer_info)
6781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6785 /* Ok, delete peer from tunnel */
6786 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6787 &peer_msg->peer.hashPubKey);
6789 send_destroy_path (t, peer_info->id);
6790 tunnel_delete_peer (t, peer_info->id);
6791 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6796 * Handler for blacklist requests of peers in a tunnel
6798 * @param cls closure
6799 * @param client identification of the client
6800 * @param message the actual message (PeerControl)
6802 * FIXME implement DHT block bloomfilter
6805 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6806 const struct GNUNET_MessageHeader *message)
6808 struct GNUNET_MESH_PeerControl *peer_msg;
6809 struct MeshClient *c;
6810 struct MeshTunnel *t;
6811 MESH_TunnelNumber tid;
6813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6814 /* Sanity check for client registration */
6815 if (NULL == (c = client_get (client)))
6818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6823 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6825 /* Sanity check for message size */
6826 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6833 /* Tunnel exists? */
6834 tid = ntohl (peer_msg->tunnel_id);
6835 t = tunnel_get_by_local_id (c, tid);
6839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6844 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6845 GNUNET_PEER_intern(&peer_msg->peer));
6850 * Handler for unblacklist requests of peers in a tunnel
6852 * @param cls closure
6853 * @param client identification of the client
6854 * @param message the actual message (PeerControl)
6857 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6858 const struct GNUNET_MessageHeader *message)
6860 struct GNUNET_MESH_PeerControl *peer_msg;
6861 struct MeshClient *c;
6862 struct MeshTunnel *t;
6863 MESH_TunnelNumber tid;
6867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6868 /* Sanity check for client registration */
6869 if (NULL == (c = client_get (client)))
6872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6877 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6879 /* Sanity check for message size */
6880 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6887 /* Tunnel exists? */
6888 tid = ntohl (peer_msg->tunnel_id);
6889 t = tunnel_get_by_local_id (c, tid);
6893 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6898 /* if peer is not known, complain */
6899 pid = GNUNET_PEER_search (&peer_msg->peer);
6906 /* search and remove from list */
6907 for (i = 0; i < t->nblacklisted; i++)
6909 if (t->blacklisted[i] == pid)
6911 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6912 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6917 /* if peer hasn't been blacklisted, complain */
6923 * Handler for connection requests to new peers by type
6925 * @param cls closure
6926 * @param client identification of the client
6927 * @param message the actual message (ConnectPeerByType)
6930 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6931 const struct GNUNET_MessageHeader *message)
6933 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6934 struct MeshClient *c;
6935 struct MeshTunnel *t;
6936 struct GNUNET_HashCode hash;
6937 MESH_TunnelNumber tid;
6939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6940 /* Sanity check for client registration */
6941 if (NULL == (c = client_get (client)))
6944 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6949 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6951 /* Sanity check for message size */
6952 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6953 ntohs (connect_msg->header.size))
6956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6960 /* Tunnel exists? */
6961 tid = ntohl (connect_msg->tunnel_id);
6962 t = tunnel_get_by_local_id (c, tid);
6966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6970 /* Does client own tunnel? */
6971 if (t->owner->handle != client)
6974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6978 /* Do WE have the service? */
6979 t->type = ntohl (connect_msg->type);
6980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6981 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6982 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6985 /* Yes! Fast forward, add ourselves to the tunnel and send the
6986 * good news to the client, and alert the destination client of
6987 * an incoming tunnel.
6989 * FIXME send a path create to self, avoid code duplication
6991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6992 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6993 peer_info_get (&my_full_id),
6994 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6997 send_client_peer_connected (t, myid);
6998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6999 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7001 t->local_tid_dest = next_local_tid++;
7002 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7003 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7004 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7008 /* Ok, lets find a peer offering the service */
7009 if (NULL != t->dht_get_type)
7011 GNUNET_DHT_get_stop (t->dht_get_type);
7013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7014 GNUNET_h2s (&hash));
7016 GNUNET_DHT_get_start (dht_handle,
7017 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7019 dht_replication_level,
7020 GNUNET_DHT_RO_RECORD_ROUTE |
7021 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7023 &dht_get_type_handler, t);
7025 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7031 * Handler for connection requests to new peers by a string service description.
7033 * @param cls closure
7034 * @param client identification of the client
7035 * @param message the actual message, which includes messages the client wants
7038 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7039 const struct GNUNET_MessageHeader *message)
7041 struct GNUNET_MESH_ConnectPeerByString *msg;
7042 struct MeshRegexSearchContext *ctx;
7043 struct MeshRegexSearchInfo *info;
7044 struct GNUNET_DHT_GetHandle *get_h;
7045 struct GNUNET_HashCode key;
7046 struct MeshTunnel *t;
7047 struct MeshClient *c;
7048 MESH_TunnelNumber tid;
7052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7053 "Connect by string started\n");
7054 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7055 size = htons (message->size);
7057 /* Sanity check for client registration */
7058 if (NULL == (c = client_get (client)))
7061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7066 /* Message size sanity check */
7067 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7074 /* Tunnel exists? */
7075 tid = ntohl (msg->tunnel_id);
7076 t = tunnel_get_by_local_id (c, tid);
7080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7084 /* Does client own tunnel? */
7085 if (t->owner->handle != client)
7088 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7093 " on tunnel %s [%u]\n",
7094 GNUNET_i2s(&my_full_id),
7097 /* Only one connect_by_string allowed at the same time! */
7098 /* FIXME: allow more, return handle at api level to cancel, document */
7099 if (NULL != t->regex_ctx)
7102 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7106 /* Find string itself */
7107 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7108 string = (const char *) &msg[1];
7110 /* Initialize context */
7111 size = GNUNET_REGEX_get_first_key(string, len, &key);
7112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7113 " consumed %u bits out of %u\n", size, len);
7114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7115 " looking for %s\n", GNUNET_h2s (&key));
7117 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7119 info->description = GNUNET_malloc (len + 1);
7120 memcpy (info->description, string, len);
7121 info->description[len] = '\0';
7122 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7123 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7126 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7127 ctx->position = size;
7131 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7133 /* Start search in DHT */
7134 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7135 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7136 &key, /* key to search */
7137 dht_replication_level, /* replication level */
7138 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7139 NULL, /* xquery */ // FIXME BLOOMFILTER
7140 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7141 &dht_get_string_handler, ctx);
7143 GNUNET_break (GNUNET_OK ==
7144 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7147 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7149 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7155 * Handler for client traffic directed to one peer
7157 * @param cls closure
7158 * @param client identification of the client
7159 * @param message the actual message
7162 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7163 const struct GNUNET_MessageHeader *message)
7165 struct MeshClient *c;
7166 struct MeshTunnel *t;
7167 struct MeshPeerInfo *pi;
7168 struct GNUNET_MESH_Unicast *data_msg;
7169 MESH_TunnelNumber tid;
7172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7173 "Got a unicast request from a client!\n");
7175 /* Sanity check for client registration */
7176 if (NULL == (c = client_get (client)))
7179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7184 data_msg = (struct GNUNET_MESH_Unicast *) message;
7186 /* Sanity check for message size */
7187 size = ntohs (message->size);
7188 if (sizeof (struct GNUNET_MESH_Unicast) +
7189 sizeof (struct GNUNET_MessageHeader) > size)
7192 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7196 /* Tunnel exists? */
7197 tid = ntohl (data_msg->tid);
7198 t = tunnel_get_by_local_id (c, tid);
7202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7206 /* Is it a local tunnel? Then, does client own the tunnel? */
7207 if (t->owner->handle != client)
7210 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7214 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7215 &data_msg->destination.hashPubKey);
7216 /* Is the selected peer in the tunnel? */
7220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7224 /* PID should be as expected */
7225 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7228 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7229 "Unicast PID, expected %u, got %u\n",
7230 t->fwd_pid + 1, ntohl (data_msg->pid));
7231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7235 /* Ok, everything is correct, send the message
7236 * (pretend we got it from a mesh peer)
7239 /* Work around const limitation */
7240 char buf[ntohs (message->size)] GNUNET_ALIGN;
7241 struct GNUNET_MESH_Unicast *copy;
7243 copy = (struct GNUNET_MESH_Unicast *) buf;
7244 memcpy (buf, data_msg, size);
7245 copy->oid = my_full_id;
7246 copy->tid = htonl (t->id.tid);
7247 copy->ttl = htonl (default_ttl);
7248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7249 " calling generic handler...\n");
7250 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7253 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7260 * Handler for client traffic directed to the origin
7262 * @param cls closure
7263 * @param client identification of the client
7264 * @param message the actual message
7267 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7268 const struct GNUNET_MessageHeader *message)
7270 struct GNUNET_MESH_ToOrigin *data_msg;
7271 struct MeshTunnelClientInfo *clinfo;
7272 struct MeshClient *c;
7273 struct MeshTunnel *t;
7274 MESH_TunnelNumber tid;
7277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7278 "Got a ToOrigin request from a client!\n");
7279 /* Sanity check for client registration */
7280 if (NULL == (c = client_get (client)))
7283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7288 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7290 /* Sanity check for message size */
7291 size = ntohs (message->size);
7292 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7293 sizeof (struct GNUNET_MessageHeader) > size)
7296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7300 /* Tunnel exists? */
7301 tid = ntohl (data_msg->tid);
7302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7303 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7309 t = tunnel_get_by_local_id (c, tid);
7312 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7319 /* It should be sent by someone who has this as incoming tunnel. */
7320 if (GNUNET_NO == client_knows_tunnel (c, t))
7323 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7327 /* PID should be as expected */
7328 clinfo = tunnel_get_client_fc (t, c);
7329 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7332 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7333 "To Origin PID, expected %u, got %u\n",
7334 clinfo->bck_pid + 1,
7335 ntohl (data_msg->pid));
7336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7340 /* Ok, everything is correct, send the message
7341 * (pretend we got it from a mesh peer)
7345 char buf[ntohs (message->size)] GNUNET_ALIGN;
7346 struct GNUNET_MESH_ToOrigin *copy;
7348 /* Work around const limitation */
7349 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7350 memcpy (buf, data_msg, size);
7351 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7352 copy->tid = htonl (t->id.tid);
7353 copy->ttl = htonl (default_ttl);
7354 if (ntohl (copy->pid) != (t->bck_pid + 1))
7357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7358 "To Origin PID, expected %u, got %u\n",
7364 copy->sender = my_full_id;
7365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7366 " calling generic handler...\n");
7367 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7369 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7376 * Handler for client traffic directed to all peers in a tunnel
7378 * @param cls closure
7379 * @param client identification of the client
7380 * @param message the actual message
7383 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7384 const struct GNUNET_MessageHeader *message)
7386 struct MeshClient *c;
7387 struct MeshTunnel *t;
7388 struct GNUNET_MESH_Multicast *data_msg;
7389 MESH_TunnelNumber tid;
7391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7392 "Got a multicast request from a client!\n");
7394 /* Sanity check for client registration */
7395 if (NULL == (c = client_get (client)))
7398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7403 data_msg = (struct GNUNET_MESH_Multicast *) message;
7405 /* Sanity check for message size */
7406 if (sizeof (struct GNUNET_MESH_Multicast) +
7407 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7414 /* Tunnel exists? */
7415 tid = ntohl (data_msg->tid);
7416 t = tunnel_get_by_local_id (c, tid);
7420 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7423 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7427 /* Does client own tunnel? */
7428 if (t->owner->handle != client)
7431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7435 /* PID should be as expected */
7436 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7440 "Multicast PID, expected %u, got %u\n",
7441 t->fwd_pid + 1, ntohl (data_msg->pid));
7442 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7447 char buf[ntohs (message->size)] GNUNET_ALIGN;
7448 struct GNUNET_MESH_Multicast *copy;
7450 copy = (struct GNUNET_MESH_Multicast *) buf;
7451 memcpy (buf, message, ntohs (message->size));
7452 copy->oid = my_full_id;
7453 copy->tid = htonl (t->id.tid);
7454 copy->ttl = htonl (default_ttl);
7455 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7457 " calling generic handler...\n");
7458 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7461 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7467 * Handler for client's ACKs for payload traffic.
7469 * @param cls Closure (unused).
7470 * @param client Identification of the client.
7471 * @param message The actual message.
7474 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7475 const struct GNUNET_MessageHeader *message)
7477 struct GNUNET_MESH_LocalAck *msg;
7478 struct MeshTunnel *t;
7479 struct MeshClient *c;
7480 MESH_TunnelNumber tid;
7483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7484 /* Sanity check for client registration */
7485 if (NULL == (c = client_get (client)))
7488 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7493 msg = (struct GNUNET_MESH_LocalAck *) message;
7495 /* Tunnel exists? */
7496 tid = ntohl (msg->tunnel_id);
7497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7498 t = tunnel_get_by_local_id (c, tid);
7502 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7503 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7508 ack = ntohl (msg->max_pid);
7509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7511 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7512 if (NULL != t->owner && t->owner->handle == client)
7514 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7516 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7520 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7521 tunnel_set_client_fwd_ack (t, c, ack);
7522 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7525 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7532 * Functions to handle messages from clients
7534 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7535 {&handle_local_new_client, NULL,
7536 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7537 {&handle_local_announce_regex, NULL,
7538 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7539 {&handle_local_tunnel_create, NULL,
7540 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7541 sizeof (struct GNUNET_MESH_TunnelMessage)},
7542 {&handle_local_tunnel_destroy, NULL,
7543 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7544 sizeof (struct GNUNET_MESH_TunnelMessage)},
7545 {&handle_local_tunnel_speed, NULL,
7546 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7547 sizeof (struct GNUNET_MESH_TunnelMessage)},
7548 {&handle_local_tunnel_speed, NULL,
7549 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7550 sizeof (struct GNUNET_MESH_TunnelMessage)},
7551 {&handle_local_tunnel_buffer, NULL,
7552 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7553 sizeof (struct GNUNET_MESH_TunnelMessage)},
7554 {&handle_local_tunnel_buffer, NULL,
7555 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7556 sizeof (struct GNUNET_MESH_TunnelMessage)},
7557 {&handle_local_connect_add, NULL,
7558 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7559 sizeof (struct GNUNET_MESH_PeerControl)},
7560 {&handle_local_connect_del, NULL,
7561 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7562 sizeof (struct GNUNET_MESH_PeerControl)},
7563 {&handle_local_blacklist, NULL,
7564 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7565 sizeof (struct GNUNET_MESH_PeerControl)},
7566 {&handle_local_unblacklist, NULL,
7567 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7568 sizeof (struct GNUNET_MESH_PeerControl)},
7569 {&handle_local_connect_by_type, NULL,
7570 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7571 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7572 {&handle_local_connect_by_string, NULL,
7573 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7574 {&handle_local_unicast, NULL,
7575 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7576 {&handle_local_to_origin, NULL,
7577 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7578 {&handle_local_multicast, NULL,
7579 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7580 {&handle_local_ack, NULL,
7581 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7582 sizeof (struct GNUNET_MESH_LocalAck)},
7588 * To be called on core init/fail.
7590 * @param cls service closure
7591 * @param server handle to the server for this service
7592 * @param identity the public identity of this peer
7595 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7596 const struct GNUNET_PeerIdentity *identity)
7599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7600 core_handle = server;
7601 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7604 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7605 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7607 GNUNET_abort(); // Try harder
7614 * Method called whenever a given peer connects.
7616 * @param cls closure
7617 * @param peer peer identity this notification is about
7618 * @param atsi performance data for the connection
7619 * @param atsi_count number of records in 'atsi'
7622 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7623 const struct GNUNET_ATS_Information *atsi,
7624 unsigned int atsi_count)
7626 struct MeshPeerInfo *peer_info;
7627 struct MeshPeerPath *path;
7629 DEBUG_CONN ("Peer connected\n");
7630 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7631 peer_info = peer_info_get (peer);
7632 if (myid == peer_info->id)
7634 DEBUG_CONN (" (self)\n");
7639 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7641 path = path_new (2);
7642 path->peers[0] = myid;
7643 path->peers[1] = peer_info->id;
7644 GNUNET_PEER_change_rc (myid, 1);
7645 GNUNET_PEER_change_rc (peer_info->id, 1);
7646 peer_info_add_path (peer_info, path, GNUNET_YES);
7647 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7653 * Method called whenever a peer disconnects.
7655 * @param cls closure
7656 * @param peer peer identity this notification is about
7659 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7661 struct MeshPeerInfo *pi;
7662 struct MeshPeerQueue *q;
7663 struct MeshPeerQueue *n;
7665 DEBUG_CONN ("Peer disconnected\n");
7666 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7678 /* try to reroute this traffic instead */
7679 queue_destroy(q, GNUNET_YES);
7683 peer_info_remove_path (pi, pi->id, myid);
7686 DEBUG_CONN (" (self)\n");
7688 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7693 /******************************************************************************/
7694 /************************ MAIN FUNCTIONS ****************************/
7695 /******************************************************************************/
7698 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7700 * @param cls closure
7701 * @param key current key code
7702 * @param value value in the hash map
7703 * @return GNUNET_YES if we should continue to iterate,
7707 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7709 struct MeshTunnel *t = value;
7716 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7718 * @param cls closure
7719 * @param key current key code
7720 * @param value value in the hash map
7721 * @return GNUNET_YES if we should continue to iterate,
7725 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7727 struct MeshPeerInfo *p = value;
7728 struct MeshPeerQueue *q;
7729 struct MeshPeerQueue *n;
7737 queue_destroy(q, GNUNET_YES);
7741 peer_info_destroy (p);
7747 * Task run during shutdown.
7753 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7757 if (core_handle != NULL)
7759 GNUNET_CORE_disconnect (core_handle);
7764 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
7767 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7768 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7769 if (dht_handle != NULL)
7771 GNUNET_DHT_disconnect (dht_handle);
7776 GNUNET_SERVER_notification_context_destroy (nc);
7779 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7781 GNUNET_SCHEDULER_cancel (announce_id_task);
7782 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7789 * Callback for hostkey read/generation
7792 * @param pk the private key
7793 * @param emsg error message
7796 key_generation_cb (void *cls,
7797 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
7800 struct MeshPeerInfo *peer;
7801 struct MeshPeerPath *p;
7806 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7807 _("Mesh service could not access hostkey. Exiting.\n"));
7808 GNUNET_SCHEDULER_shutdown ();
7811 my_private_key = pk;
7812 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7813 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7814 &my_full_id.hashPubKey);
7815 myid = GNUNET_PEER_intern (&my_full_id);
7816 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7817 "Mesh for peer [%s] starting\n",
7818 GNUNET_i2s(&my_full_id));
7820 // transport_handle = GNUNET_TRANSPORT_connect(c,
7830 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7833 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7834 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7835 GNUNET_SERVER_disconnect_notify (server_handle,
7836 &handle_local_client_disconnect, NULL);
7840 clients_tail = NULL;
7843 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7844 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7846 /* Create a peer_info for the local peer */
7847 peer = peer_info_get (&my_full_id);
7850 GNUNET_PEER_change_rc (myid, 1);
7851 peer_info_add_path (peer, p, GNUNET_YES);
7852 GNUNET_SERVER_resume (server_handle);
7853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7858 * Process mesh requests.
7860 * @param cls closure
7861 * @param server the initialized server
7862 * @param c configuration to use
7865 run (void *cls, struct GNUNET_SERVER_Handle *server,
7866 const struct GNUNET_CONFIGURATION_Handle *c)
7870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7871 server_handle = server;
7872 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7873 NULL, /* Closure passed to MESH functions */
7874 &core_init, /* Call core_init once connected */
7875 &core_connect, /* Handle connects */
7876 &core_disconnect, /* remove peers on disconnects */
7877 NULL, /* Don't notify about all incoming messages */
7878 GNUNET_NO, /* For header only in notification */
7879 NULL, /* Don't notify about all outbound messages */
7880 GNUNET_NO, /* For header-only out notification */
7881 core_handlers); /* Register these handlers */
7883 if (core_handle == NULL)
7886 GNUNET_SCHEDULER_shutdown ();
7891 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7894 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7896 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7898 GNUNET_SCHEDULER_shutdown ();
7903 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7904 &refresh_path_time))
7906 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7908 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7909 "refresh path time");
7910 GNUNET_SCHEDULER_shutdown ();
7915 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7916 &app_announce_time))
7918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7920 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7921 "app announce time");
7922 GNUNET_SCHEDULER_shutdown ();
7927 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7930 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7932 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7933 "id announce time");
7934 GNUNET_SCHEDULER_shutdown ();
7939 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7940 &unacknowledged_wait_time))
7942 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7944 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7945 "unacknowledged wait time");
7946 GNUNET_SCHEDULER_shutdown ();
7951 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7954 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7956 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7958 GNUNET_SCHEDULER_shutdown ();
7963 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7968 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7970 GNUNET_SCHEDULER_shutdown ();
7975 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7978 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7980 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7982 GNUNET_SCHEDULER_shutdown ();
7987 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7990 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7992 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7998 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7999 &dht_replication_level))
8001 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8003 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
8004 "dht replication level", 10);
8005 dht_replication_level = 10;
8008 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8009 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8010 peers = GNUNET_CONTAINER_multihashmap_create (32);
8011 applications = GNUNET_CONTAINER_multihashmap_create (32);
8012 types = GNUNET_CONTAINER_multihashmap_create (32);
8014 dht_handle = GNUNET_DHT_connect (c, 64);
8015 if (NULL == dht_handle)
8019 stats = GNUNET_STATISTICS_create ("mesh", c);
8021 GNUNET_SERVER_suspend (server_handle);
8022 /* Scheduled the task to clean up when shutdown is called */
8023 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8025 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8026 GNUNET_free (keyfile);
8031 * The main function for the mesh service.
8033 * @param argc number of arguments from the command line
8034 * @param argv command line arguments
8035 * @return 0 ok, 1 on error
8038 main (int argc, char *const *argv)
8042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8045 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8051 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8052 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8053 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);