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;
1340 /* Find the longest match for the current string position,
1341 * among tokens in the given block */
1342 ctx->longest_match = 0;
1343 result = GNUNET_MESH_regex_block_iterate (block, size,
1344 ®ex_edge_iterator, ctx);
1345 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1347 /* Did anything match? */
1348 if (0 == ctx->longest_match)
1351 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1352 new_ctx->info = info;
1353 new_ctx->position = ctx->position + ctx->longest_match;
1354 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1356 /* Check whether we already have a DHT GET running for it */
1358 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1361 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1363 ®ex_result_iterator,
1365 return; // We are already looking for it
1368 /* Start search in DHT */
1370 GNUNET_DHT_get_start (dht_handle, /* handle */
1371 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1372 &ctx->hash, /* key to search */
1373 dht_replication_level, /* replication level */
1374 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1375 NULL, /* xquery */ // FIXME BLOOMFILTER
1376 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1377 &dht_get_string_handler, new_ctx);
1379 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1382 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1391 * Iterator over hash map entries to cancel DHT GET requests after a
1392 * successful connect_by_string.
1394 * @param cls Closure (unused).
1395 * @param key Current key code (unused).
1396 * @param value Value in the hash map (get handle).
1397 * @return GNUNET_YES if we should continue to iterate,
1401 regex_cancel_dht_get (void *cls,
1402 const struct GNUNET_HashCode * key,
1405 struct GNUNET_DHT_GetHandle *h = value;
1407 GNUNET_DHT_get_stop (h);
1413 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1414 * search for connect_by_string.
1416 * @param cls Closure (unused).
1417 * @param key Current key code (unused).
1418 * @param value MeshRegexBlock in the hash map.
1419 * @return GNUNET_YES if we should continue to iterate,
1423 regex_free_result (void *cls,
1424 const struct GNUNET_HashCode * key,
1428 GNUNET_free (value);
1434 * Regex callback iterator to store own service description in the DHT.
1436 * @param cls closure.
1437 * @param key hash for current state.
1438 * @param proof proof for current state.
1439 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1440 * @param num_edges number of edges leaving current state.
1441 * @param edges edges leaving current state.
1444 regex_iterator (void *cls,
1445 const struct GNUNET_HashCode *key,
1448 unsigned int num_edges,
1449 const struct GNUNET_REGEX_Edge *edges)
1451 struct MeshRegexBlock *block;
1452 struct MeshRegexEdge *block_edge;
1453 enum GNUNET_DHT_RouteOption opt;
1457 unsigned int offset;
1460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1461 " regex dht put for state %s\n",
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1471 if (GNUNET_YES == accepting)
1473 struct MeshRegexAccept block;
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1476 " state %s is accepting, putting own id\n",
1478 size = sizeof (block);
1480 block.id = my_full_id;
1482 GNUNET_DHT_put(dht_handle, key,
1483 dht_replication_level,
1484 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1485 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1488 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1493 len = strlen(proof);
1494 size = sizeof (struct MeshRegexBlock) + len;
1495 block = GNUNET_malloc (size);
1498 block->n_proof = htonl (len);
1499 block->n_edges = htonl (num_edges);
1500 block->accepting = htonl (accepting);
1502 /* Store the proof at the end of the block. */
1503 aux = (char *) &block[1];
1504 memcpy (aux, proof, len);
1507 /* Store each edge in a variable length MeshEdge struct at the
1508 * very end of the MeshRegexBlock structure.
1510 for (i = 0; i < num_edges; i++)
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1513 " edge %s towards %s\n",
1515 GNUNET_h2s(&edges[i].destination));
1517 /* aux points at the end of the last block */
1518 len = strlen (edges[i].label);
1519 size += sizeof (struct MeshRegexEdge) + len;
1520 // Calculate offset FIXME is this ok? use size instead?
1521 offset = aux - (char *) block;
1522 block = GNUNET_realloc (block, size);
1523 aux = &((char *) block)[offset];
1524 block_edge = (struct MeshRegexEdge *) aux;
1525 block_edge->key = edges[i].destination;
1526 block_edge->n_token = htonl (len);
1527 aux = (char *) &block_edge[1];
1528 memcpy (aux, edges[i].label, len);
1532 GNUNET_DHT_put(dht_handle, key,
1533 dht_replication_level,
1535 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1537 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1541 GNUNET_free (block);
1546 * Store the regular expression describing a local service into the DHT.
1548 * @param regex The regular expresion.
1551 regex_put (const char *regex)
1553 struct GNUNET_REGEX_Automaton *dfa;
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1556 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1557 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1558 GNUNET_REGEX_automaton_destroy (dfa);
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1564 * Find a path to a peer that offers a regex servcie compatible
1565 * with a given string.
1567 * @param key The key of the accepting state.
1568 * @param ctx Context containing info about the string, tunnel, etc.
1571 regex_find_path (const struct GNUNET_HashCode *key,
1572 struct MeshRegexSearchContext *ctx)
1574 struct GNUNET_DHT_GetHandle *get_h;
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1577 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1578 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1579 key, /* key to search */
1580 dht_replication_level, /* replication level */
1581 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1582 GNUNET_DHT_RO_RECORD_ROUTE,
1583 NULL, /* xquery */ // FIXME BLOOMFILTER
1584 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1585 &dht_get_string_accept_handler, ctx);
1586 GNUNET_break (GNUNET_OK ==
1587 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1590 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1595 * Function called if the connect attempt to a peer found via
1596 * connect_by_string times out. Try to connect to another peer, if any.
1597 * Otherwise try to reconnect to the same peer.
1599 * @param cls Closure (info about regex search).
1600 * @param tc TaskContext.
1603 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1605 struct MeshRegexSearchInfo *info = cls;
1606 struct MeshPeerInfo *peer_info;
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1611 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1612 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1620 if (0 < info->n_peers)
1622 // Select next peer, put current in that spot.
1623 id = info->peers[info->i_peer];
1624 info->peers[info->i_peer] = info->peer;
1625 info->i_peer = (info->i_peer + 1) % info->n_peers;
1629 // Try to connect to same peer again.
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1634 peer_info = peer_info_get_short(id);
1635 tunnel_add_peer (info->t, peer_info);
1637 tunnel_delete_peer (info->t, old);
1638 peer_info_connect (peer_info, info->t);
1639 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1640 ®ex_connect_timeout,
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1647 * Cancel an ongoing regex search in the DHT and free all resources.
1649 * @param ctx The search context.
1652 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1654 struct MeshRegexSearchInfo *info = ctx->info;
1657 GNUNET_free (info->description);
1658 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1659 ®ex_cancel_dht_get, NULL);
1660 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1661 ®ex_free_result, NULL);
1662 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1663 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1664 info->t->regex_ctx = NULL;
1665 for (i = 0; i < info->n_contexts; i++)
1667 GNUNET_free (info->contexts[i]);
1669 if (0 < info->n_contexts)
1670 GNUNET_free (info->contexts);
1671 if (0 < info->n_peers)
1672 GNUNET_free (info->peers);
1673 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1675 GNUNET_SCHEDULER_cancel(info->timeout);
1681 /******************************************************************************/
1682 /************************ PERIODIC FUNCTIONS ****************************/
1683 /******************************************************************************/
1686 * Announce iterator over for each application provided by the peer
1688 * @param cls closure
1689 * @param key current key code
1690 * @param value value in the hash map
1691 * @return GNUNET_YES if we should continue to
1696 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1698 struct PBlock block;
1699 struct MeshClient *c;
1701 block.id = my_full_id;
1702 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1703 GNUNET_assert(NULL != c);
1704 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1705 if (0 == block.type)
1710 block.type = htonl (block.type);
1712 GNUNET_break (NULL !=
1713 GNUNET_DHT_put (dht_handle, key,
1714 dht_replication_level,
1715 GNUNET_DHT_RO_RECORD_ROUTE |
1716 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1717 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1719 (const char *) &block,
1720 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1722 app_announce_time, NULL, NULL));
1728 * Periodically announce what applications are provided by local clients
1731 * @param cls closure
1732 * @param tc task context
1735 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1737 struct MeshClient *c = cls;
1740 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1742 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1746 DEBUG_DHT ("Starting PUT for regex\n");
1748 for (i = 0; i < c->n_regex; i++)
1750 regex_put (c->regexes[i]);
1752 c->regex_announce_task =
1753 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1754 DEBUG_DHT ("Finished PUT for regex\n");
1761 * Periodically announce what applications are provided by local clients
1764 * @param cls closure
1765 * @param tc task context
1768 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1770 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1772 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1776 DEBUG_DHT ("Starting PUT for apps\n");
1778 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1780 announce_applications_task =
1781 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1783 DEBUG_DHT ("Finished PUT for apps\n");
1790 * Periodically announce self id in the DHT
1792 * @param cls closure
1793 * @param tc task context
1796 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1798 struct PBlock block;
1800 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1802 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1806 * - Set data expiration in function of X
1807 * - Adapt X to churn
1809 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1811 block.id = my_full_id;
1812 block.type = htonl (0);
1813 GNUNET_DHT_put (dht_handle, /* DHT handle */
1814 &my_full_id.hashPubKey, /* Key to use */
1815 dht_replication_level, /* Replication level */
1816 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1817 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1818 sizeof (block), /* Size of the data */
1819 (const char *) &block, /* Data itself */
1820 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1821 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1822 NULL, /* Continuation */
1823 NULL); /* Continuation closure */
1825 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1829 /******************************************************************************/
1830 /****************** GENERAL HELPER FUNCTIONS ************************/
1831 /******************************************************************************/
1834 * Decrements the reference counter and frees all resources if needed
1836 * @param mesh_data Data Descriptor used in a multicast message.
1837 * Freed no longer needed (last message).
1840 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1842 if (0 == --(mesh_data->reference_counter))
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1845 GNUNET_free (mesh_data->data);
1846 GNUNET_free (mesh_data);
1852 * Check if client has registered with the service and has not disconnected
1854 * @param client the client to check
1856 * @return non-NULL if client exists in the global DLL
1858 static struct MeshClient *
1859 client_get (struct GNUNET_SERVER_Client *client)
1861 struct MeshClient *c;
1866 if (c->handle == client)
1875 * Checks if a given client has subscribed to certain message type
1877 * @param message_type Type of message to check
1878 * @param c Client to check
1880 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1882 * FIXME: use of crypto_hash slows it down
1883 * The hash function alone takes 8-10us out of the ~55us for the whole
1884 * process of retransmitting the message from one local client to another.
1885 * Find faster implementation!
1888 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1890 struct GNUNET_HashCode hc;
1892 if (NULL == c->types)
1895 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1896 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1901 * Check whether client wants traffic from a tunnel.
1903 * @param c Client to check.
1904 * @param t Tunnel to be found.
1906 * @return GNUNET_YES if client knows tunnel.
1908 * TODO look in client hashmap
1911 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1915 for (i = 0; i < t->nclients; i++)
1916 if (t->clients[i] == c)
1923 * Check whether client has been informed about a tunnel.
1925 * @param c Client to check.
1926 * @param t Tunnel to be found.
1928 * @return GNUNET_YES if client knows tunnel.
1930 * TODO look in client hashmap
1933 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1937 for (i = 0; i < t->nignore; i++)
1938 if (t->ignore[i] == c)
1940 return client_wants_tunnel(c, t);
1945 * Marks a client as uninterested in traffic from the tunnel, updating both
1946 * client and tunnel to reflect this.
1948 * @param c Client that doesn't want traffic anymore.
1949 * @param t Tunnel which should be ignored.
1951 * FIXME when to delete an incoming tunnel?
1954 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1956 struct GNUNET_HashCode hash;
1958 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1959 GNUNET_break (GNUNET_YES ==
1960 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1962 GNUNET_break (GNUNET_YES ==
1963 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1964 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1965 tunnel_delete_active_client (t, c);
1966 GNUNET_array_append (t->ignore, t->nignore, c);
1971 * Deletes a tunnel from a client (either owner or destination). To be used on
1972 * tunnel destroy, otherwise, use client_ignore_tunnel.
1974 * @param c Client whose tunnel to delete.
1975 * @param t Tunnel which should be deleted.
1978 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1980 struct GNUNET_HashCode hash;
1984 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1985 GNUNET_assert (GNUNET_YES ==
1986 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1992 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1994 GNUNET_assert (GNUNET_YES ==
1995 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1999 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2007 * Send the message to all clients that have subscribed to its type
2009 * @param msg Pointer to the message itself
2010 * @param payload Pointer to the payload of the message.
2011 * @param t The tunnel to whose clients this message goes.
2013 * @return number of clients this message was sent to
2016 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2017 const struct GNUNET_MessageHeader *payload,
2018 struct MeshTunnel *t)
2020 struct MeshClient *c;
2021 MESH_TunnelNumber *tid;
2024 char cbuf[htons (msg->size)];
2026 type = ntohs (payload->type);
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2029 GNUNET_MESH_DEBUG_M2S (type));
2031 memcpy (cbuf, msg, sizeof (cbuf));
2032 switch (htons (msg->type))
2034 struct GNUNET_MESH_Unicast *uc;
2035 struct GNUNET_MESH_Multicast *mc;
2036 struct GNUNET_MESH_ToOrigin *to;
2038 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2039 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2042 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2043 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2046 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2047 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2055 for (count = 0, c = clients; c != NULL; c = c->next)
2057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2058 if (client_is_subscribed (type, c))
2060 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2064 *tid = htonl (t->local_tid);
2068 if (GNUNET_NO == client_knows_tunnel (c, t))
2070 /* This client doesn't know the tunnel */
2071 struct GNUNET_MESH_TunnelNotification tmsg;
2072 struct GNUNET_HashCode hash;
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2075 tmsg.header.size = htons (sizeof (tmsg));
2076 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2077 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2078 tmsg.tunnel_id = htonl (t->local_tid_dest);
2079 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2080 &tmsg.header, GNUNET_NO);
2081 tunnel_add_client (t, c);
2082 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2084 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2085 c->incoming_tunnels, &hash, t,
2086 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2088 *tid = htonl (t->local_tid_dest);
2091 /* Check if the client wants to get traffic from the tunnel */
2092 if (GNUNET_NO == client_wants_tunnel(c, t))
2095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2096 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2097 (struct GNUNET_MessageHeader
2098 *) cbuf, GNUNET_NO);
2107 * Notify the client that owns the tunnel that a peer has connected to it
2108 * (the requested path to it has been confirmed).
2110 * @param t Tunnel whose owner to notify
2111 * @param id Short id of the peer that has connected
2114 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2116 struct GNUNET_MESH_PeerControl pc;
2118 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2119 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2120 pc.tunnel_id = htonl (t->local_tid);
2121 GNUNET_PEER_resolve (id, &pc.peer);
2122 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2128 * Notify all clients (not depending on registration status) that the incoming
2129 * tunnel is no longer valid.
2131 * @param t Tunnel that was destroyed.
2134 send_clients_tunnel_destroy (struct MeshTunnel *t)
2136 struct GNUNET_MESH_TunnelMessage msg;
2138 msg.header.size = htons (sizeof (msg));
2139 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2140 msg.tunnel_id = htonl (t->local_tid_dest);
2141 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2146 * Notify clients of tunnel disconnections, if needed.
2147 * In case the origin disconnects, the destination clients get a tunnel destroy
2148 * notification. If the last destination disconnects (only one remaining client
2149 * in tunnel), the origin gets a (local ID) peer disconnected.
2150 * Note that the function must be called BEFORE removing the client from
2153 * @param t Tunnel that was destroyed.
2154 * @param c Client that disconnected.
2157 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2163 struct GNUNET_MESH_TunnelMessage msg;
2165 msg.header.size = htons (sizeof (msg));
2166 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2167 msg.tunnel_id = htonl (t->local_tid_dest);
2168 for (i = 0; i < t->nclients; i++)
2169 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2170 &msg.header, GNUNET_NO);
2172 // FIXME when to disconnect an incoming tunnel?
2173 else if (1 == t->nclients && NULL != t->owner)
2175 struct GNUNET_MESH_PeerControl msg;
2177 msg.header.size = htons (sizeof (msg));
2178 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2179 msg.tunnel_id = htonl (t->local_tid);
2180 msg.peer = my_full_id;
2181 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2182 &msg.header, GNUNET_NO);
2188 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2189 * and insert it in the appropiate structures if the peer is not known yet.
2191 * @param peer Full identity of the peer.
2193 * @return Existing or newly created peer info.
2195 static struct MeshPeerInfo *
2196 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2198 struct MeshPeerInfo *peer_info;
2200 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2201 if (NULL == peer_info)
2204 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2205 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2206 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2207 peer_info->id = GNUNET_PEER_intern (peer);
2215 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2216 * and insert it in the appropiate structures if the peer is not known yet.
2218 * @param peer Short identity of the peer.
2220 * @return Existing or newly created peer info.
2222 static struct MeshPeerInfo *
2223 peer_info_get_short (const GNUNET_PEER_Id peer)
2225 struct GNUNET_PeerIdentity id;
2227 GNUNET_PEER_resolve (peer, &id);
2228 return peer_info_get (&id);
2233 * Iterator to remove the tunnel from the list of tunnels a peer participates
2236 * @param cls Closure (tunnel info)
2237 * @param key GNUNET_PeerIdentity of the peer (unused)
2238 * @param value PeerInfo of the peer
2240 * @return always GNUNET_YES, to keep iterating
2243 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2245 struct MeshTunnel *t = cls;
2246 struct MeshPeerInfo *peer = value;
2249 for (i = 0; i < peer->ntunnels; i++)
2252 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2255 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2256 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2265 * Core callback to write a pre-constructed data packet to core buffer
2267 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2268 * @param size Number of bytes available in buf.
2269 * @param buf Where the to write the message.
2271 * @return number of bytes written to buf
2274 send_core_data_raw (void *cls, size_t size, void *buf)
2276 struct MeshTransmissionDescriptor *info = cls;
2277 struct GNUNET_MessageHeader *msg;
2280 GNUNET_assert (NULL != info);
2281 GNUNET_assert (NULL != info->mesh_data);
2282 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2283 total_size = ntohs (msg->size);
2285 if (total_size > size)
2290 memcpy (buf, msg, total_size);
2291 data_descriptor_decrement_rc (info->mesh_data);
2298 * Sends an already built non-multicast message to a peer,
2299 * properly registrating all used resources.
2301 * @param message Message to send. Function makes a copy of it.
2302 * @param peer Short ID of the neighbor whom to send the message.
2303 * @param t Tunnel on which this message is transmitted.
2306 send_message (const struct GNUNET_MessageHeader *message,
2307 const struct GNUNET_PeerIdentity *peer,
2308 struct MeshTunnel *t)
2310 struct MeshTransmissionDescriptor *info;
2311 struct MeshPeerInfo *neighbor;
2312 struct MeshPeerPath *p;
2316 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2318 size = ntohs (message->size);
2319 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2320 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2321 info->mesh_data->data = GNUNET_malloc (size);
2322 memcpy (info->mesh_data->data, message, size);
2323 type = ntohs(message->type);
2324 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2326 struct GNUNET_MESH_Unicast *m;
2328 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2329 m->ttl = htonl (ntohl (m->ttl) - 1);
2331 info->mesh_data->data_len = size;
2332 info->mesh_data->reference_counter = 1;
2333 info->mesh_data->total_out = 1;
2334 neighbor = peer_info_get (peer);
2335 for (p = neighbor->path_head; NULL != p; p = p->next)
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 " %s IS NOT DIRECTLY CONNECTED\n",
2348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 for (p = neighbor->path_head; NULL != p; p = p->next)
2353 struct GNUNET_PeerIdentity debug_id;
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 " path with %u hops through:\n",
2359 for (i = 0; i < p->length; i++)
2361 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2364 i, GNUNET_i2s(&debug_id));
2368 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2369 GNUNET_free (info->mesh_data->data);
2370 GNUNET_free (info->mesh_data);
2374 info->peer = neighbor;
2375 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2378 * TODO: in this case we only need the service to retransmit
2379 * the message down the path. If we pass the real type to queue_add,
2380 * queue_send will try to build the message from scratch. This can
2381 * probably be done by some other way instead of deleteing the type
2395 * Sends a CREATE PATH message for a path to a peer, properly registrating
2396 * all used resources.
2398 * @param peer PeerInfo of the final peer for whom this path is being created.
2399 * @param p Path itself.
2400 * @param t Tunnel for which the path is created.
2403 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2404 struct MeshTunnel *t)
2406 struct GNUNET_PeerIdentity id;
2407 struct MeshPathInfo *path_info;
2408 struct MeshPeerInfo *neighbor;
2414 p = tree_get_path_to_peer (t->tree, peer->id);
2421 for (i = 0; i < p->length; i++)
2423 if (p->peers[i] == myid)
2426 if (i >= p->length - 1)
2432 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2434 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2435 path_info->path = p;
2437 neighbor = peer_info_get (&id);
2438 path_info->peer = neighbor;
2439 queue_add (path_info,
2440 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2441 sizeof (struct GNUNET_MESH_ManipulatePath) +
2442 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2449 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2451 * @param t Tunnel whose path to destroy.
2452 * @param destination Short ID of the peer to whom the path to destroy.
2455 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2457 struct MeshPeerPath *p;
2460 p = tree_get_path_to_peer (t->tree, destination);
2466 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2467 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2469 struct GNUNET_MESH_ManipulatePath *msg;
2470 struct GNUNET_PeerIdentity *pi;
2474 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2475 msg->header.size = htons (size);
2476 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2477 msg->tid = htonl (t->id.tid);
2478 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2479 for (i = 0; i < p->length; i++)
2481 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2483 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2490 * Try to establish a new connection to this peer.
2491 * Use the best path for the given tunnel.
2492 * If the peer doesn't have any path to it yet, try to get one.
2493 * If the peer already has some path, send a CREATE PATH towards it.
2495 * @param peer PeerInfo of the peer.
2496 * @param t Tunnel for which to create the path, if possible.
2499 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2501 struct MeshPeerPath *p;
2502 struct MeshPathInfo *path_info;
2504 if (NULL != peer->path_head)
2506 p = tree_get_path_to_peer (t->tree, peer->id);
2513 // FIXME always send create path to self
2516 send_create_path (peer, p, t);
2520 struct GNUNET_HashCode hash;
2523 send_client_peer_connected (t, myid);
2524 t->local_tid_dest = next_local_tid++;
2525 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2528 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2529 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2536 else if (NULL == peer->dhtget)
2538 struct GNUNET_PeerIdentity id;
2540 GNUNET_PEER_resolve (peer->id, &id);
2541 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2542 path_info->peer = peer;
2544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2545 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2546 peer->dhtgetcls = path_info;
2547 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2548 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2549 &id.hashPubKey, /* key to search */
2550 dht_replication_level, /* replication level */
2551 GNUNET_DHT_RO_RECORD_ROUTE |
2552 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2553 NULL, /* xquery */ // FIXME BLOOMFILTER
2554 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2555 &dht_get_id_handler, path_info);
2557 /* Otherwise, there is no path but the DHT get is already started. */
2562 * Task to delay the connection of a peer
2564 * @param cls Closure (path info with tunnel and peer to connect).
2565 * Will be free'd on exection.
2566 * @param tc TaskContext
2569 peer_info_connect_task (void *cls,
2570 const struct GNUNET_SCHEDULER_TaskContext *tc)
2572 struct MeshPathInfo *path_info = cls;
2574 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2579 peer_info_connect (path_info->peer, path_info->t);
2585 * Destroy the peer_info and free any allocated resources linked to it
2587 * @param pi The peer_info to destroy.
2589 * @return GNUNET_OK on success
2592 peer_info_destroy (struct MeshPeerInfo *pi)
2594 struct GNUNET_PeerIdentity id;
2595 struct MeshPeerPath *p;
2596 struct MeshPeerPath *nextp;
2598 GNUNET_PEER_resolve (pi->id, &id);
2599 GNUNET_PEER_change_rc (pi->id, -1);
2602 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2606 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2608 if (NULL != pi->dhtget)
2610 GNUNET_DHT_get_stop (pi->dhtget);
2611 GNUNET_free (pi->dhtgetcls);
2617 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2627 * Remove all paths that rely on a direct connection between p1 and p2
2628 * from the peer itself and notify all tunnels about it.
2630 * @param peer PeerInfo of affected peer.
2631 * @param p1 GNUNET_PEER_Id of one peer.
2632 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2635 * TODO: optimize (see below)
2638 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2641 struct MeshPeerPath *p;
2642 struct MeshPeerPath *aux;
2643 struct MeshPeerInfo *peer_d;
2645 unsigned int destroyed;
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2652 p = peer->path_head;
2656 for (i = 0; i < (p->length - 1); i++)
2658 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2659 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2661 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2672 for (i = 0; i < peer->ntunnels; i++)
2674 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2678 * Problem: one or more peers have been deleted from the tunnel tree.
2679 * We don't know who they are to try to add them again.
2680 * We need to try to find a new path for each of the disconnected peers.
2681 * Some of them might already have a path to reach them that does not
2682 * involve p1 and p2. Adding all anew might render in a better tree than
2683 * the trivial immediate fix.
2685 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2686 * its children will be reachable trough him.
2688 peer_d = peer_info_get_short (d);
2691 for (p = peer_d->path_head; NULL != p; p = p->next)
2693 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2701 /* No callback, as peer will be already disconnected and a connection
2702 * scheduled by tunnel_notify_connection_broken.
2704 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2708 peer_info_connect (peer_d, peer->tunnels[i]);
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2716 * Add the path to the peer and update the path used to reach it in case this
2719 * @param peer_info Destination peer to add the path to.
2720 * @param path New path to add. Last peer must be the peer in arg 1.
2721 * Path will be either used of freed if already known.
2722 * @param trusted Do we trust that this path is real?
2725 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2728 struct MeshPeerPath *aux;
2732 if ((NULL == peer_info) || (NULL == path))
2735 path_destroy (path);
2738 if (path->peers[path->length - 1] != peer_info->id)
2741 path_destroy (path);
2744 if (path->length <= 2 && GNUNET_NO == trusted)
2746 /* Only allow CORE to tell us about direct paths */
2747 path_destroy (path);
2750 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2751 for (l = 1; l < path->length; l++)
2753 if (path->peers[l] == myid)
2755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2756 for (l2 = 0; l2 < path->length - l; l2++)
2758 path->peers[l2] = path->peers[l + l2];
2763 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2768 struct GNUNET_PeerIdentity id;
2770 GNUNET_PEER_resolve (peer_info->id, &id);
2771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2772 path->length, GNUNET_i2s (&id));
2775 l = path_get_length (path);
2782 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2783 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2785 l2 = path_get_length (aux);
2788 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2789 peer_info->path_tail, aux, path);
2794 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2796 path_destroy (path);
2801 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2808 * Add the path to the origin peer and update the path used to reach it in case
2809 * this is the shortest.
2810 * The path is given in peer_info -> destination, therefore we turn the path
2811 * upside down first.
2813 * @param peer_info Peer to add the path to, being the origin of the path.
2814 * @param path New path to add after being inversed.
2815 * @param trusted Do we trust that this path is real?
2818 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2819 struct MeshPeerPath *path, int trusted)
2822 peer_info_add_path (peer_info, path, trusted);
2827 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2828 * to obtain a local peer -> destination path and interning the peer ids.
2830 * @return Newly allocated and created path
2832 static struct MeshPeerPath *
2833 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2834 unsigned int get_path_length,
2835 const struct GNUNET_PeerIdentity *put_path,
2836 unsigned int put_path_length)
2838 struct MeshPeerPath *p;
2844 GNUNET_PEER_change_rc (myid, 1);
2845 i = get_path_length;
2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2847 for (i--; i >= 0; i--)
2849 id = GNUNET_PEER_intern (&get_path[i]);
2850 if (p->length > 0 && id == p->peers[p->length - 1])
2852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2853 GNUNET_PEER_change_rc (id, -1);
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2858 GNUNET_i2s (&get_path[i]));
2860 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2861 p->peers[p->length - 1] = id;
2864 i = put_path_length;
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2866 for (i--; i >= 0; i--)
2868 id = GNUNET_PEER_intern (&put_path[i]);
2871 /* PUT path went through us, so discard the path up until now and start
2872 * from here to get a much shorter (and loop-free) path.
2877 if (p->length > 0 && id == p->peers[p->length - 1])
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2880 GNUNET_PEER_change_rc (id, -1);
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2885 GNUNET_i2s (&put_path[i]));
2887 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2888 p->peers[p->length - 1] = id;
2892 if (get_path_length > 0)
2893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2894 GNUNET_i2s (&get_path[0]));
2895 if (put_path_length > 0)
2896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2897 GNUNET_i2s (&put_path[0]));
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2900 for (i = 0; i < p->length; i++)
2902 struct GNUNET_PeerIdentity peer_id;
2904 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2906 GNUNET_i2s (&peer_id));
2914 * Adds a path to the peer_infos of all the peers in the path
2916 * @param p Path to process.
2917 * @param confirmed Whether we know if the path works or not.
2920 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2924 /* TODO: invert and add */
2925 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2926 for (i++; i < p->length; i++)
2928 struct MeshPeerInfo *aux;
2929 struct MeshPeerPath *copy;
2931 aux = peer_info_get_short (p->peers[i]);
2932 copy = path_duplicate (p);
2933 copy->length = i + 1;
2934 peer_info_add_path (aux, copy, GNUNET_NO);
2940 * Send keepalive packets for a peer
2942 * @param cls Closure (tunnel for which to send the keepalive).
2943 * @param tc Notification context.
2945 * TODO: implement explicit multicast keepalive?
2948 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2952 * Search for a tunnel among the incoming tunnels
2954 * @param tid the local id of the tunnel
2956 * @return tunnel handler, NULL if doesn't exist
2958 static struct MeshTunnel *
2959 tunnel_get_incoming (MESH_TunnelNumber tid)
2961 struct GNUNET_HashCode hash;
2963 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2964 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2965 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2970 * Search for a tunnel among the tunnels for a client
2972 * @param c the client whose tunnels to search in
2973 * @param tid the local id of the tunnel
2975 * @return tunnel handler, NULL if doesn't exist
2977 static struct MeshTunnel *
2978 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2980 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2982 return tunnel_get_incoming (tid);
2986 struct GNUNET_HashCode hash;
2988 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2989 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2995 * Search for a tunnel by global ID using PEER_ID
2997 * @param pi owner of the tunnel
2998 * @param tid global tunnel number
3000 * @return tunnel handler, NULL if doesn't exist
3002 static struct MeshTunnel *
3003 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3005 struct MESH_TunnelID id;
3006 struct GNUNET_HashCode hash;
3011 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3012 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3017 * Search for a tunnel by global ID using full PeerIdentities
3019 * @param oid owner of the tunnel
3020 * @param tid global tunnel number
3022 * @return tunnel handler, NULL if doesn't exist
3024 static struct MeshTunnel *
3025 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3027 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3032 * Delete an active client from the tunnel.
3038 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3042 for (i = 0; i < t->nclients; i++)
3044 if (t->clients[i] == c)
3046 t->clients[i] = t->clients[t->nclients - 1];
3047 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3048 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3050 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3058 * Delete an ignored client from the tunnel.
3064 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3068 for (i = 0; i < t->nignore; i++)
3070 if (t->ignore[i] == c)
3072 t->ignore[i] = t->ignore[t->nignore - 1];
3073 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3081 * Delete a client from the tunnel. It should be only done on
3082 * client disconnection, otherwise use client_ignore_tunnel.
3088 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3090 tunnel_delete_ignored_client (t, c);
3091 tunnel_delete_active_client (t, c);
3096 * Iterator to free MeshTunnelChildInfo of tunnel children.
3098 * @param cls Closure (tunnel info).
3099 * @param key Hash of GNUNET_PEER_Id (unused).
3100 * @param value MeshTunnelChildInfo of the child.
3102 * @return always GNUNET_YES, to keep iterating
3105 tunnel_destroy_child (void *cls,
3106 const struct GNUNET_HashCode * key,
3109 struct MeshTunnelChildInfo *cinfo = value;
3110 struct MeshTunnel *t = cls;
3114 for (c = 0; c < cinfo->send_buffer_n; c++)
3116 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3117 if (NULL != cinfo->send_buffer[i])
3118 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3121 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3123 GNUNET_free_non_null (cinfo->send_buffer);
3124 GNUNET_free (cinfo);
3130 * Callback used to notify a client owner of a tunnel that a peer has
3131 * disconnected, most likely because of a path change.
3133 * @param cls Closure (tunnel this notification is about).
3134 * @param peer_id Short ID of disconnected peer.
3137 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3139 struct MeshTunnel *t = cls;
3140 struct MeshPeerInfo *peer;
3141 struct MeshPathInfo *path_info;
3143 if (NULL != t->owner && NULL != nc)
3145 struct GNUNET_MESH_PeerControl msg;
3147 msg.header.size = htons (sizeof (msg));
3148 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3149 msg.tunnel_id = htonl (t->local_tid);
3150 GNUNET_PEER_resolve (peer_id, &msg.peer);
3151 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3152 &msg.header, GNUNET_NO);
3154 peer = peer_info_get_short (peer_id);
3155 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3156 path_info->peer = peer;
3158 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3163 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3164 * needed rescources.
3165 * If peer already exists, reevaluate shortest path and change if different.
3167 * @param t Tunnel we want to add a new peer to
3168 * @param peer PeerInfo of the peer being added
3172 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3174 struct GNUNET_PeerIdentity id;
3175 struct MeshPeerPath *best_p;
3176 struct MeshPeerPath *p;
3177 unsigned int best_cost;
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3181 GNUNET_PEER_resolve (peer->id, &id);
3183 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3186 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3187 GNUNET_assert (GNUNET_OK ==
3188 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3190 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3193 if (NULL != (p = peer->path_head))
3196 best_cost = tree_get_path_cost (t->tree, p);
3199 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3206 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3207 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3208 t->path_refresh_task =
3209 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3213 /* Start a DHT get */
3214 peer_info_connect (peer, t);
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3220 * Add a path to a tunnel which we don't own, just to remember the next hop.
3221 * If destination node was already in the tunnel, the first hop information
3222 * will be replaced with the new path.
3224 * @param t Tunnel we want to add a new peer to
3225 * @param p Path to add
3226 * @param own_pos Position of local node in path.
3230 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3231 unsigned int own_pos)
3233 struct GNUNET_PeerIdentity id;
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3236 GNUNET_assert (0 != own_pos);
3237 tree_add_path (t->tree, p, NULL, NULL);
3238 if (own_pos < p->length - 1)
3240 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3241 tree_update_first_hops (t->tree, myid, &id);
3243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3247 * Add a client to a tunnel, initializing all needed data structures.
3249 * @param t Tunnel to which add the client.
3250 * @param c Client which to add to the tunnel.
3253 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3255 struct MeshTunnelClientInfo clinfo;
3257 GNUNET_array_append (t->clients, t->nclients, c);
3258 clinfo.fwd_ack = t->fwd_pid + 1;
3259 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3260 clinfo.fwd_pid = t->fwd_pid;
3261 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3263 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3268 * Notifies a tunnel that a connection has broken that affects at least
3269 * some of its peers. Sends a notification towards the root of the tree.
3270 * In case the peer is the owner of the tree, notifies the client that owns
3271 * the tunnel and tries to reconnect.
3273 * @param t Tunnel affected.
3274 * @param p1 Peer that got disconnected from p2.
3275 * @param p2 Peer that got disconnected from p1.
3277 * @return Short ID of the peer disconnected (either p1 or p2).
3278 * 0 if the tunnel remained unaffected.
3280 static GNUNET_PEER_Id
3281 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3287 tree_notify_connection_broken (t->tree, p1, p2,
3288 &tunnel_notify_client_peer_disconnected,
3290 if (myid != p1 && myid != p2)
3296 if (tree_get_predecessor (t->tree) != 0)
3298 /* We are the peer still connected, notify owner of the disconnection. */
3299 struct GNUNET_MESH_PathBroken msg;
3300 struct GNUNET_PeerIdentity neighbor;
3302 msg.header.size = htons (sizeof (msg));
3303 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3304 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3305 msg.tid = htonl (t->id.tid);
3306 msg.peer1 = my_full_id;
3307 GNUNET_PEER_resolve (pid, &msg.peer2);
3308 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3309 send_message (&msg.header, &neighbor, t);
3317 * Send a multicast packet to a neighbor.
3319 * @param cls Closure (Info about the multicast packet)
3320 * @param neighbor_id Short ID of the neighbor to send the packet to.
3323 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3325 struct MeshData *mdata = cls;
3326 struct MeshTransmissionDescriptor *info;
3327 struct GNUNET_PeerIdentity neighbor;
3329 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3331 info->mesh_data = mdata;
3332 (mdata->reference_counter) ++;
3333 info->destination = neighbor_id;
3334 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3336 GNUNET_i2s (&neighbor));
3337 info->peer = peer_info_get (&neighbor);
3338 GNUNET_assert (NULL != info->peer);
3340 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3341 info->mesh_data->data_len,
3348 * Queue a message in a tunnel in multicast, sending a copy to each child node
3349 * down the local one in the tunnel tree.
3351 * @param t Tunnel in which to send the data.
3352 * @param msg Message to be sent.
3353 * @param internal DEPRECATED Has the service generated this message?
3355 * FIXME remove internal if no use comes up
3358 tunnel_send_multicast (struct MeshTunnel *t,
3359 const struct GNUNET_MessageHeader *msg,
3362 struct MeshData *mdata;
3364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3365 " sending a multicast packet...\n");
3367 mdata = GNUNET_malloc (sizeof (struct MeshData));
3368 mdata->data_len = ntohs (msg->size);
3370 mdata->data = GNUNET_malloc (mdata->data_len);
3371 memcpy (mdata->data, msg, mdata->data_len);
3372 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3374 struct GNUNET_MESH_Multicast *mcast;
3376 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3377 if (t->fwd_queue_n >= t->fwd_queue_max)
3380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3382 " message from %s!\n",
3383 GNUNET_i2s(&mcast->oid));
3384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3385 " message at %s!\n",
3386 GNUNET_i2s(&my_full_id));
3387 GNUNET_free (mdata->data);
3388 GNUNET_free (mdata);
3392 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3394 ntohl (mcast->ttl));
3398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3401 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3402 if (mdata->reference_counter == 0)
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3405 " no one to send data to\n");
3406 GNUNET_free (mdata->data);
3407 GNUNET_free (mdata);
3412 mdata->total_out = mdata->reference_counter;
3414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3415 " sending a multicast packet done\n");
3421 * Increase the SKIP value of all peers that
3422 * have not received a unicast message.
3424 * @param cls Closure (ID of the peer that HAS received the message).
3425 * @param key ID of the neighbor.
3426 * @param value Information about the neighbor.
3428 * @return GNUNET_YES to keep iterating.
3431 tunnel_add_skip (void *cls,
3432 const struct GNUNET_HashCode * key,
3435 struct GNUNET_PeerIdentity *neighbor = cls;
3436 struct MeshTunnelChildInfo *cinfo = value;
3438 /* TODO compare only pointers? key == neighbor? */
3439 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3449 * @brief Get neighbor's Flow Control information.
3451 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3452 * descendant of the local node in a certain tunnel.
3453 * If the info is not yet there (recently created path), creates the data struct
3454 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3457 * @param t Tunnel related.
3458 * @param peer Neighbor whose Flow Control info is needed.
3460 * @return Neighbor's Flow Control info.
3462 static struct MeshTunnelChildInfo *
3463 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3464 const struct GNUNET_PeerIdentity *peer)
3466 struct MeshTunnelChildInfo *cinfo;
3467 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3473 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3474 cinfo->id = GNUNET_PEER_intern (peer);
3475 cinfo->skip = t->fwd_pid;
3477 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3478 cinfo->fwd_ack = t->fwd_pid + delta;
3479 cinfo->bck_ack = delta;
3481 cinfo->send_buffer =
3482 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3484 GNUNET_assert (GNUNET_OK ==
3485 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3488 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3495 * Get the Flow Control info of a client.
3497 * @param t Tunnel on which to look.
3498 * @param c Client whose ACK to get.
3500 * @return ACK value.
3502 static struct MeshTunnelClientInfo *
3503 tunnel_get_client_fc (struct MeshTunnel *t,
3504 struct MeshClient *c)
3508 for (i = 0; i < t->nclients; i++)
3510 if (t->clients[i] != c)
3512 return &t->clients_fc[i];
3515 return NULL; // avoid compiler / coverity complaints
3520 * Iterator to get the appropiate ACK value from all children nodes.
3522 * @param cls Closue (tunnel).
3523 * @param id Id of the child node.
3526 tunnel_get_child_fwd_ack (void *cls,
3529 struct GNUNET_PeerIdentity peer_id;
3530 struct MeshTunnelChildInfo *cinfo;
3531 struct MeshTunnelChildIteratorContext *ctx = cls;
3532 struct MeshTunnel *t = ctx->t;
3535 GNUNET_PEER_resolve (id, &peer_id);
3536 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3537 ack = cinfo->fwd_ack;
3540 if (GNUNET_NO == ctx->init)
3542 ctx->max_child_ack = ack;
3543 ctx->init = GNUNET_YES;
3546 if (GNUNET_YES == t->speed_min)
3548 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3552 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3559 * Get the maximum PID allowed to transmit to any
3560 * tunnel child of the local peer, depending on the tunnel
3561 * buffering/speed settings.
3565 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3568 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3570 struct MeshTunnelChildIteratorContext ctx;
3572 ctx.max_child_ack = 0;
3574 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3576 if (0 == ctx.nchildren)
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3579 " tunnel has no children, no FWD ACK\n");
3583 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3584 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3586 return (int64_t) ctx.max_child_ack;
3591 * Set the FWD ACK value of a client in a particular tunnel.
3593 * @param t Tunnel affected.
3594 * @param c Client whose ACK to set.
3595 * @param ack ACK value.
3598 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3599 struct MeshClient *c,
3604 for (i = 0; i < t->nclients; i++)
3606 if (t->clients[i] != c)
3608 t->clients_fc[i].fwd_ack = ack;
3616 * Get the highest ACK value of all clients in a particular tunnel,
3617 * according to the buffering/speed settings.
3619 * @param t Tunnel on which to look.
3621 * @return Corresponding ACK value (max uint32_t).
3622 * If no clients are suscribed, -1LL.
3625 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3630 if (0 == t->nclients)
3632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3633 " tunnel has no clients, no FWD ACK\n");
3637 for (ack = -1LL, i = 0; i < t->nclients; i++)
3640 (GNUNET_YES == t->speed_min &&
3641 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3642 (GNUNET_NO == t->speed_min &&
3643 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3645 ack = t->clients_fc[i].fwd_ack;
3649 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3650 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3652 return (uint32_t) ack;
3657 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3658 * mode and the status of all children nodes.
3662 * @return Maximum PID allowed.
3665 tunnel_get_fwd_ack (struct MeshTunnel *t)
3669 uint32_t buffer_free;
3673 count = t->fwd_pid - t->skip;
3674 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3675 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3676 child_ack = tunnel_get_children_fwd_ack (t);
3677 client_ack = tunnel_get_clients_fwd_ack (t);
3678 if (-1LL == child_ack)
3680 // Node has no children, child_ack AND core buffer are irrelevant.
3681 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3682 return (uint32_t) client_ack;
3684 if (-1LL == client_ack)
3688 if (GNUNET_YES == t->speed_min)
3690 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3691 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3695 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3696 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3698 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3699 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3701 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3702 count, buffer_free, child_ack, client_ack, ack);
3708 * Build a local ACK message and send it to a local client.
3710 * @param t Tunnel on which to send the ACK.
3711 * @param c Client to whom send the ACK.
3712 * @param ack Value of the ACK.
3715 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3717 struct GNUNET_MESH_LocalAck msg;
3719 msg.header.size = htons (sizeof (msg));
3720 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3721 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3722 msg.max_pid = htonl (ack);
3723 GNUNET_SERVER_notification_context_unicast(nc,
3730 * Build an ACK message and queue it to send to the given peer.
3732 * @param t Tunnel on which to send the ACK.
3733 * @param peer Peer to whom send the ACK.
3734 * @param ack Value of the ACK.
3737 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3739 struct GNUNET_MESH_ACK msg;
3741 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3742 msg.header.size = htons (sizeof (msg));
3743 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3744 msg.pid = htonl (ack);
3745 msg.tid = htonl (t->id.tid);
3747 send_message (&msg.header, peer, t);
3752 * Notify a the owner of a tunnel about how many more
3753 * payload packages will we accept on a given tunnel.
3755 * @param t Tunnel on which to send the ACK.
3758 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3763 "Sending client FWD ACK on tunnel %X\n",
3766 ack = tunnel_get_fwd_ack (t);
3768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3769 if (t->last_fwd_ack == ack)
3772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3773 t->last_fwd_ack = ack;
3774 send_local_ack (t, t->owner, ack);
3779 * Send an ACK informing the predecessor about the available buffer space.
3780 * In case there is no predecessor, inform the owning client.
3781 * If buffering is off, send only on behalf of children or self if endpoint.
3782 * If buffering is on, send when sent to children and buffer space is free.
3783 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3784 * the ACK itself goes "back" (towards root).
3786 * @param t Tunnel on which to send the ACK.
3787 * @param type Type of message that triggered the ACK transmission.
3790 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3792 struct GNUNET_PeerIdentity id;
3795 if (NULL != t->owner)
3797 tunnel_send_client_fwd_ack (t);
3800 /* Is it after unicast / multicast retransmission? */
3803 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3804 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3806 "ACK due to FWD DATA retransmission\n");
3807 if (GNUNET_YES == t->nobuffer)
3809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3813 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3814 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3820 /* Check if we need no retransmit the ACK */
3821 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3822 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3826 " t->qmax: %u, t->qn: %u\n",
3827 t->fwd_queue_max, t->fwd_queue_n);
3828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3829 " t->pid: %u, t->ack: %u\n",
3830 t->fwd_pid, t->last_fwd_ack);
3834 /* Ok, ACK might be necessary, what PID to ACK? */
3835 ack = tunnel_get_fwd_ack (t);
3837 /* If speed_min and not all children have ack'd, dont send yet */
3838 if (ack == t->last_fwd_ack)
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3844 t->last_fwd_ack = ack;
3845 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3846 send_ack (t, &id, ack);
3852 * Iterator to send a child node a BCK ACK to allow him to send more
3855 * @param cls Closure (tunnel).
3856 * @param id Id of the child node.
3859 tunnel_send_child_bck_ack (void *cls,
3862 struct MeshTunnel *t = cls;
3863 struct MeshTunnelChildInfo *cinfo;
3864 struct GNUNET_PeerIdentity peer;
3866 GNUNET_PEER_resolve (id, &peer);
3867 cinfo = tunnel_get_neighbor_fc (t, &peer);
3869 if (cinfo->bck_ack != cinfo->pid &&
3870 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3874 send_ack (t, &peer, cinfo->bck_ack);
3879 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3881 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3883 * @param t Tunnel on which to send the BCK ACKs.
3886 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3889 unsigned int tunnel_delta;
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3893 tunnel_delta = t->bck_ack - t->bck_pid;
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3896 /* Find client whom to allow to send to origin (with lowest buffer space) */
3897 for (i = 0; i < t->nclients; i++)
3899 struct MeshTunnelClientInfo *clinfo;
3902 clinfo = &t->clients_fc[i];
3903 delta = clinfo->bck_ack - clinfo->bck_pid;
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3905 t->clients[i]->id, delta);
3907 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3908 (GNUNET_YES == t->nobuffer && 0 == delta))
3912 ack = clinfo->bck_pid;
3913 ack += t->nobuffer ? 1 : tunnel_delta;
3914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3915 " sending ack to client %u: %u\n",
3916 t->clients[i]->id, ack);
3917 send_local_ack (t, t->clients[i], ack);
3918 clinfo->bck_ack = ack;
3925 * Send an ACK informing the children nodes and destination clients about
3926 * the available buffer space.
3927 * If buffering is off, send only on behalf of root (can be self).
3928 * If buffering is on, send when sent to predecessor and buffer space is free.
3929 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3930 * the ACK itself goes "forward" (towards children/clients).
3932 * @param t Tunnel on which to send the ACK.
3933 * @param type Type of message that triggered the ACK transmission.
3936 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3939 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3940 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3941 /* Is it after data to_origin retransmission? */
3944 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3945 if (GNUNET_YES == t->nobuffer)
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3948 " Not sending ACK, nobuffer\n");
3952 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3953 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3959 tunnel_send_clients_bck_ack (t);
3960 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3965 * @brief Re-initiate traffic to this peer if necessary.
3967 * Check if there is traffic queued towards this peer
3968 * and the core transmit handle is NULL (traffic was stalled).
3969 * If so, call core tmt rdy.
3971 * @param cls Closure (unused)
3972 * @param peer_id Short ID of peer to which initiate traffic.
3975 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3977 struct MeshPeerInfo *peer;
3978 struct GNUNET_PeerIdentity id;
3979 struct MeshPeerQueue *q;
3982 peer = peer_info_get_short(peer_id);
3983 if (NULL != peer->core_transmit)
3986 q = queue_get_next(peer);
3989 /* Might br multicast traffic already sent to this particular peer but
3990 * not to other children in this tunnel.
3991 * This way t->queue_n would be > 0 but the queue of this particular peer
3997 GNUNET_PEER_resolve (peer->id, &id);
3998 peer->core_transmit =
3999 GNUNET_CORE_notify_transmit_ready(core_handle,
4002 GNUNET_TIME_UNIT_FOREVER_REL,
4012 * @brief Allow transmission of FWD traffic on this tunnel
4014 * Check if there is traffic queued towards any children
4015 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4017 * @param t Tunnel on which to unlock FWD traffic.
4020 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4022 if (0 == t->fwd_queue_n)
4025 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4030 * @brief Allow transmission of BCK traffic on this tunnel
4032 * Check if there is traffic queued towards the root of the tree
4033 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4035 * @param t Tunnel on which to unlock BCK traffic.
4038 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4040 if (0 == t->bck_queue_n)
4043 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4048 * Send a message to all peers in this tunnel that the tunnel is no longer
4051 * @param t The tunnel whose peers to notify.
4054 tunnel_send_destroy (struct MeshTunnel *t)
4056 struct GNUNET_MESH_TunnelDestroy msg;
4058 msg.header.size = htons (sizeof (msg));
4059 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4060 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4061 msg.tid = htonl (t->id.tid);
4062 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4067 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4069 * @param cls Closure (Tunnel which to cancel).
4070 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4073 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4075 struct MeshTunnel *t = cls;
4076 struct MeshPeerInfo *peer_info;
4077 struct MeshPeerQueue *pq;
4078 struct MeshPeerQueue *next;
4080 peer_info = peer_info_get_short (neighbor_id);
4081 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4084 if (pq->tunnel == t)
4086 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4087 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4088 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4090 // Should have been removed on destroy children
4093 queue_destroy (pq, GNUNET_YES);
4096 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4098 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4099 peer_info->core_transmit = NULL;
4104 * Destroy the tunnel and free any allocated resources linked to it.
4106 * @param t the tunnel to destroy
4108 * @return GNUNET_OK on success
4111 tunnel_destroy (struct MeshTunnel *t)
4113 struct MeshClient *c;
4114 struct GNUNET_HashCode hash;
4125 struct GNUNET_PeerIdentity id;
4127 GNUNET_PEER_resolve (t->id.oid, &id);
4128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4129 GNUNET_i2s (&id), t->id.tid);
4131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4135 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4136 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4141 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4144 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4148 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4149 for (i = 0; i < t->nclients; i++)
4153 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4158 for (i = 0; i < t->nignore; i++)
4162 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4167 if (t->nclients > 0)
4170 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4174 GNUNET_free (t->clients);
4176 if (NULL != t->peers)
4178 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4180 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4183 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4184 &tunnel_destroy_child,
4186 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4188 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4190 tree_destroy (t->tree);
4192 if (NULL != t->regex_ctx)
4193 regex_cancel_search (t->regex_ctx);
4194 if (NULL != t->dht_get_type)
4195 GNUNET_DHT_get_stop (t->dht_get_type);
4196 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4197 GNUNET_SCHEDULER_cancel (t->timeout_task);
4198 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4199 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4202 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4203 GNUNET_assert (0 <= n_tunnels);
4210 * Create a new tunnel
4212 * @param owner Who is the owner of the tunnel (short ID).
4213 * @param tid Tunnel Number of the tunnel.
4214 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4215 * @param local Tunnel Number for the tunnel, for the client point of view.
4217 * @return A new initialized tunnel. NULL on error.
4219 static struct MeshTunnel *
4220 tunnel_new (GNUNET_PEER_Id owner,
4221 MESH_TunnelNumber tid,
4222 struct MeshClient *client,
4223 MESH_TunnelNumber local)
4225 struct MeshTunnel *t;
4226 struct GNUNET_HashCode hash;
4228 if (n_tunnels >= max_tunnels && NULL == client)
4231 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4234 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4235 t->bck_queue_max = t->fwd_queue_max;
4236 t->tree = tree_new (owner);
4238 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4239 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4240 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4241 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4242 t->local_tid = local;
4243 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4245 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4247 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4249 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4257 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4264 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4266 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4267 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4271 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4281 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4282 * that are no longer needed, as well as nodes of no longer reachable peers.
4283 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4285 * @param t Tunnel from which to remove the path.
4286 * @param peer Short id of the peer which should be removed.
4289 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4291 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4297 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4298 * client when the client disconnects. If the client is not the owner, the
4299 * owner will get notified if no more clients are in the tunnel and the client
4300 * get removed from the tunnel's list.
4302 * @param cls closure (client that is disconnecting)
4303 * @param key the hash of the local tunnel id (used to access the hashmap)
4304 * @param value the value stored at the key (tunnel to destroy)
4306 * @return GNUNET_OK on success
4309 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4311 struct MeshTunnel *t = value;
4312 struct MeshClient *c = cls;
4315 send_client_tunnel_disconnect(t, c);
4318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4319 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4320 tunnel_delete_client(t, c);
4321 client_delete_tunnel(c, t);
4324 tunnel_send_destroy(t);
4325 r = tunnel_destroy (t);
4331 * Timeout function, destroys tunnel if called
4333 * @param cls Closure (tunnel to destroy).
4334 * @param tc TaskContext
4337 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4339 struct MeshTunnel *t = cls;
4341 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4343 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4348 * Resets the tunnel timeout. Starts it if no timeout was running.
4350 * @param t Tunnel whose timeout to reset.
4352 * TODO use heap to improve efficiency of scheduler.
4355 tunnel_reset_timeout (struct MeshTunnel *t)
4357 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4358 GNUNET_SCHEDULER_cancel (t->timeout_task);
4360 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4361 (refresh_path_time, 4), &tunnel_timeout, t);
4365 /******************************************************************************/
4366 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4367 /******************************************************************************/
4370 * Function to send a create path packet to a peer.
4372 * @param cls closure
4373 * @param size number of bytes available in buf
4374 * @param buf where the callee should write the message
4375 * @return number of bytes written to buf
4378 send_core_path_create (void *cls, size_t size, void *buf)
4380 struct MeshPathInfo *info = cls;
4381 struct GNUNET_MESH_ManipulatePath *msg;
4382 struct GNUNET_PeerIdentity *peer_ptr;
4383 struct MeshTunnel *t = info->t;
4384 struct MeshPeerPath *p = info->path;
4389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4391 sizeof (struct GNUNET_MESH_ManipulatePath) +
4392 p->length * sizeof (struct GNUNET_PeerIdentity);
4394 if (size < size_needed || NULL == buf)
4399 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4400 msg->header.size = htons (size_needed);
4401 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4402 msg->tid = ntohl (t->id.tid);
4405 if (GNUNET_YES == t->speed_min)
4406 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4407 if (GNUNET_YES == t->nobuffer)
4408 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4409 msg->opt = htonl(opt);
4411 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4412 for (i = 0; i < p->length; i++)
4414 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4421 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4427 * Fill the core buffer
4429 * @param cls closure (data itself)
4430 * @param size number of bytes available in buf
4431 * @param buf where the callee should write the message
4433 * @return number of bytes written to buf
4436 send_core_data_multicast (void *cls, size_t size, void *buf)
4438 struct MeshTransmissionDescriptor *info = cls;
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4442 GNUNET_assert (NULL != info);
4443 GNUNET_assert (NULL != info->peer);
4444 total_size = info->mesh_data->data_len;
4445 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4447 if (total_size > size)
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4453 memcpy (buf, info->mesh_data->data, total_size);
4456 struct GNUNET_MESH_Multicast *mc;
4457 struct GNUNET_MessageHeader *mh;
4460 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4462 mc = (struct GNUNET_MESH_Multicast *) mh;
4463 mh = (struct GNUNET_MessageHeader *) &mc[1];
4464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4465 " multicast, payload type %s\n",
4466 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4468 " multicast, payload size %u\n", ntohs (mh->size));
4472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4473 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4477 data_descriptor_decrement_rc (info->mesh_data);
4478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4486 * Creates a path ack message in buf and frees all unused resources.
4488 * @param cls closure (MeshTransmissionDescriptor)
4489 * @param size number of bytes available in buf
4490 * @param buf where the callee should write the message
4491 * @return number of bytes written to buf
4494 send_core_path_ack (void *cls, size_t size, void *buf)
4496 struct MeshTransmissionDescriptor *info = cls;
4497 struct GNUNET_MESH_PathACK *msg = buf;
4499 GNUNET_assert (NULL != info);
4500 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4505 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4506 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4507 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4508 msg->tid = htonl (info->origin->tid);
4509 msg->peer_id = my_full_id;
4512 /* TODO add signature */
4514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4515 return sizeof (struct GNUNET_MESH_PathACK);
4520 * Free a transmission that was already queued with all resources
4521 * associated to the request.
4523 * @param queue Queue handler to cancel.
4524 * @param clear_cls Is it necessary to free associated cls?
4527 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4529 struct MeshTransmissionDescriptor *dd;
4530 struct MeshPathInfo *path_info;
4532 if (GNUNET_YES == clear_cls)
4534 switch (queue->type)
4536 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4537 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4538 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4541 data_descriptor_decrement_rc (dd->mesh_data);
4543 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4545 path_info = queue->cls;
4546 path_destroy (path_info->path);
4550 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4551 " type %s unknown!\n",
4552 GNUNET_MESH_DEBUG_M2S(queue->type));
4554 GNUNET_free_non_null (queue->cls);
4556 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4557 queue->peer->queue_tail,
4559 GNUNET_free (queue);
4564 * @brief Get the next transmittable message from the queue.
4566 * This will be the head, except in the case of being a data packet
4567 * not allowed by the destination peer.
4569 * @param peer Destination peer.
4571 * @return The next viable MeshPeerQueue element to send to that peer.
4572 * NULL when there are no transmittable messages.
4574 struct MeshPeerQueue *
4575 queue_get_next (const struct MeshPeerInfo *peer)
4577 struct MeshPeerQueue *q;
4578 struct MeshTunnel *t;
4579 struct MeshTransmissionDescriptor *info;
4580 struct MeshTunnelChildInfo *cinfo;
4581 struct GNUNET_MESH_Unicast *ucast;
4582 struct GNUNET_MESH_ToOrigin *to_orig;
4583 struct GNUNET_MESH_Multicast *mcast;
4584 struct GNUNET_PeerIdentity id;
4588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4589 for (q = peer->queue_head; NULL != q; q = q->next)
4593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4595 GNUNET_MESH_DEBUG_M2S(q->type));
4598 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4599 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4600 pid = ntohl (ucast->pid);
4601 GNUNET_PEER_resolve (info->peer->id, &id);
4602 cinfo = tunnel_get_neighbor_fc(t, &id);
4603 ack = cinfo->fwd_ack;
4605 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4606 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4607 pid = ntohl (to_orig->pid);
4610 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4611 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4612 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4614 // Not a multicast payload: multicast control traffic (destroy, etc)
4617 pid = ntohl (mcast->pid);
4618 GNUNET_PEER_resolve (info->peer->id, &id);
4619 cinfo = tunnel_get_neighbor_fc(t, &id);
4620 ack = cinfo->fwd_ack;
4623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4628 "********* ACK: %u, PID: %u\n",
4630 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4639 "********* NEXT!\n");
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 "********* nothing found\n");
4649 * Core callback to write a queued packet to core buffer
4651 * @param cls Closure (peer info).
4652 * @param size Number of bytes available in buf.
4653 * @param buf Where the to write the message.
4655 * @return number of bytes written to buf
4658 queue_send (void *cls, size_t size, void *buf)
4660 struct MeshPeerInfo *peer = cls;
4661 struct GNUNET_MessageHeader *msg;
4662 struct MeshPeerQueue *queue;
4663 struct MeshTunnel *t;
4664 struct MeshTunnelChildInfo *cinfo;
4665 struct GNUNET_PeerIdentity dst_id;
4668 peer->core_transmit = NULL;
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4671 queue = queue_get_next(peer);
4673 /* Queue has no internal mesh traffic not sendable payload */
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4677 if (NULL == peer->queue_head)
4678 GNUNET_break(0); // Should've been canceled
4681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4683 GNUNET_PEER_resolve (peer->id, &dst_id);
4684 /* Check if buffer size is enough for the message */
4685 if (queue->size > size)
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4690 "********* not enough room, reissue\n");
4691 peer->core_transmit =
4692 GNUNET_CORE_notify_transmit_ready(core_handle,
4695 GNUNET_TIME_UNIT_FOREVER_REL,
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4705 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4709 "********* unicast: t->q (%u/%u)\n",
4710 t->fwd_queue_n, t->fwd_queue_max);
4712 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4719 switch (queue->type)
4722 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4723 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4724 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4726 "********* raw: %s\n",
4727 GNUNET_MESH_DEBUG_M2S (queue->type));
4729 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4730 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4731 data_size = send_core_data_raw (queue->cls, size, buf);
4732 msg = (struct GNUNET_MessageHeader *) buf;
4733 switch (ntohs (msg->type)) // Type of preconstructed message
4735 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4736 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4738 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4739 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4745 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4748 struct MeshTransmissionDescriptor *info = queue->cls;
4750 if ((1 == info->mesh_data->reference_counter
4751 && GNUNET_YES == t->speed_min)
4753 (info->mesh_data->total_out == info->mesh_data->reference_counter
4754 && GNUNET_NO == t->speed_min))
4756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4757 "********* considered sent\n");
4761 data_size = send_core_data_multicast(queue->cls, size, buf);
4762 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4764 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4766 data_size = send_core_path_create(queue->cls, size, buf);
4768 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4770 data_size = send_core_path_ack(queue->cls, size, buf);
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4775 "********* type unknown: %u\n",
4779 switch (queue->type)
4781 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4782 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4783 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4784 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4785 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4788 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4789 "at pos %u (%p) != %p\n",
4790 cinfo->send_buffer_start,
4791 cinfo->send_buffer[cinfo->send_buffer_start],
4794 GNUNET_break(cinfo->send_buffer_n > 0);
4795 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4796 cinfo->send_buffer_n--;
4797 cinfo->send_buffer_start++;
4798 cinfo->send_buffer_start %= t->fwd_queue_max;
4804 /* Free queue, but cls was freed by send_core_* */
4805 queue_destroy (queue, GNUNET_NO);
4807 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4813 /* If more data in queue, send next */
4814 queue = queue_get_next(peer);
4817 struct GNUNET_PeerIdentity id;
4819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4820 GNUNET_PEER_resolve (peer->id, &id);
4821 peer->core_transmit =
4822 GNUNET_CORE_notify_transmit_ready(core_handle,
4825 GNUNET_TIME_UNIT_FOREVER_REL,
4833 if (NULL != peer->queue_head)
4834 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4835 "********* %s stalled\n",
4836 GNUNET_i2s(&my_full_id));
4838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4844 * Queue and pass message to core when possible.
4846 * @param cls Closure (type dependant).
4847 * @param type Type of the message, 0 for a raw message.
4848 * @param size Size of the message.
4849 * @param dst Neighbor to send message to.
4850 * @param t Tunnel this message belongs to.
4853 queue_add (void *cls, uint16_t type, size_t size,
4854 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4856 struct MeshPeerQueue *queue;
4857 struct MeshTunnelChildInfo *cinfo;
4858 struct GNUNET_PeerIdentity id;
4864 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4865 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4867 n = &t->fwd_queue_n;
4868 max = &t->fwd_queue_max;
4870 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4872 n = &t->bck_queue_n;
4873 max = &t->bck_queue_max;
4878 if (NULL == t->owner)
4879 GNUNET_break_op(0); // TODO: kill connection?
4882 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4884 return; // Drop message
4888 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4894 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4895 GNUNET_PEER_resolve (dst->id, &id);
4896 if (NULL == dst->core_transmit)
4898 dst->core_transmit =
4899 GNUNET_CORE_notify_transmit_ready(core_handle,
4902 GNUNET_TIME_UNIT_FOREVER_REL,
4908 if (NULL == n) // Is this internal mesh traffic?
4911 // It's payload, keep track of buffer per peer.
4912 cinfo = tunnel_get_neighbor_fc(t, &id);
4913 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4914 if (NULL != cinfo->send_buffer[i])
4916 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4917 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4918 cinfo->send_buffer_start++;
4919 cinfo->send_buffer_start %= t->fwd_queue_max;
4920 cinfo->send_buffer_n--;
4922 cinfo->send_buffer[i] = queue;
4923 cinfo->send_buffer_n++;
4924 if (cinfo->send_buffer_n > t->fwd_queue_max)
4927 cinfo->send_buffer_n = t->fwd_queue_max;
4932 /******************************************************************************/
4933 /******************** MESH NETWORK HANDLERS **************************/
4934 /******************************************************************************/
4938 * Core handler for path creation
4940 * @param cls closure
4941 * @param message message
4942 * @param peer peer identity this notification is about
4943 * @param atsi performance data
4944 * @param atsi_count number of records in 'atsi'
4946 * @return GNUNET_OK to keep the connection open,
4947 * GNUNET_SYSERR to close it (signal serious error)
4950 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4951 const struct GNUNET_MessageHeader *message,
4952 const struct GNUNET_ATS_Information *atsi,
4953 unsigned int atsi_count)
4955 unsigned int own_pos;
4958 MESH_TunnelNumber tid;
4959 struct GNUNET_MESH_ManipulatePath *msg;
4960 struct GNUNET_PeerIdentity *pi;
4961 struct GNUNET_HashCode hash;
4962 struct MeshPeerPath *path;
4963 struct MeshPeerInfo *dest_peer_info;
4964 struct MeshPeerInfo *orig_peer_info;
4965 struct MeshTunnel *t;
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4968 "Received a path create msg [%s]\n",
4969 GNUNET_i2s (&my_full_id));
4970 size = ntohs (message->size);
4971 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4973 GNUNET_break_op (0);
4977 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4978 if (size % sizeof (struct GNUNET_PeerIdentity))
4980 GNUNET_break_op (0);
4983 size /= sizeof (struct GNUNET_PeerIdentity);
4986 GNUNET_break_op (0);
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4990 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4992 tid = ntohl (msg->tid);
4993 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4995 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4996 t = tunnel_get (pi, tid);
4997 if (NULL == t) // FIXME only for INCOMING tunnels?
5001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5002 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5005 // FIXME notify failure
5008 opt = ntohl (msg->opt);
5009 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5010 GNUNET_YES : GNUNET_NO;
5011 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
5012 GNUNET_YES : GNUNET_NO;
5013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5014 " speed_min: %d, nobuffer:%d\n",
5015 t->speed_min, t->nobuffer);
5017 if (GNUNET_YES == t->nobuffer)
5019 t->bck_queue_max = 1;
5020 t->fwd_queue_max = 1;
5023 // FIXME only assign a local tid if a local client is interested (on demand)
5024 while (NULL != tunnel_get_incoming (next_local_tid))
5025 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5026 t->local_tid_dest = next_local_tid++;
5027 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5030 tunnel_reset_timeout (t);
5031 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5033 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5034 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5042 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5043 if (NULL == dest_peer_info)
5045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5046 " Creating PeerInfo for destination.\n");
5047 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5048 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5049 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5053 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5054 if (NULL == orig_peer_info)
5056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5057 " Creating PeerInfo for origin.\n");
5058 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5059 orig_peer_info->id = GNUNET_PEER_intern (pi);
5060 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5061 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5064 path = path_new (size);
5066 for (i = 0; i < size; i++)
5068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5069 GNUNET_i2s (&pi[i]));
5070 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5071 if (path->peers[i] == myid)
5074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5077 /* cannot be self, must be 'not found' */
5078 /* create path: self not found in path through self */
5079 GNUNET_break_op (0);
5080 path_destroy (path);
5084 path_add_to_peers (path, GNUNET_NO);
5085 tunnel_add_path (t, path, own_pos);
5086 if (own_pos == size - 1)
5088 /* It is for us! Send ack. */
5089 struct MeshTransmissionDescriptor *info;
5091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5092 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5093 if (NULL == t->peers)
5095 /* New tunnel! Notify clients on data. */
5096 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5098 GNUNET_break (GNUNET_SYSERR !=
5099 GNUNET_CONTAINER_multihashmap_put (t->peers,
5100 &my_full_id.hashPubKey,
5103 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5104 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5105 info->origin = &t->id;
5106 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5107 GNUNET_assert (NULL != info->peer);
5109 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5110 sizeof (struct GNUNET_MESH_PathACK),
5116 struct MeshPeerPath *path2;
5118 /* It's for somebody else! Retransmit. */
5119 path2 = path_duplicate (path);
5120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5121 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5122 path2 = path_duplicate (path);
5123 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5124 send_create_path (dest_peer_info, path, t);
5131 * Core handler for path destruction
5133 * @param cls closure
5134 * @param message message
5135 * @param peer peer identity this notification is about
5136 * @param atsi performance data
5137 * @param atsi_count number of records in 'atsi'
5139 * @return GNUNET_OK to keep the connection open,
5140 * GNUNET_SYSERR to close it (signal serious error)
5143 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5144 const struct GNUNET_MessageHeader *message,
5145 const struct GNUNET_ATS_Information *atsi,
5146 unsigned int atsi_count)
5148 struct GNUNET_MESH_ManipulatePath *msg;
5149 struct GNUNET_PeerIdentity *pi;
5150 struct MeshPeerPath *path;
5151 struct MeshTunnel *t;
5152 unsigned int own_pos;
5156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5157 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5158 size = ntohs (message->size);
5159 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5161 GNUNET_break_op (0);
5165 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5166 if (size % sizeof (struct GNUNET_PeerIdentity))
5168 GNUNET_break_op (0);
5171 size /= sizeof (struct GNUNET_PeerIdentity);
5174 GNUNET_break_op (0);
5177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5179 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5180 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5182 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5184 t = tunnel_get (pi, ntohl (msg->tid));
5187 /* TODO notify back: we don't know this tunnel */
5188 GNUNET_break_op (0);
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5192 path = path_new (size);
5194 for (i = 0; i < size; i++)
5196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5197 GNUNET_i2s (&pi[i]));
5198 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5199 if (path->peers[i] == myid)
5202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5203 if (own_pos < path->length - 1)
5204 send_message (message, &pi[own_pos + 1], t);
5206 send_client_tunnel_disconnect(t, NULL);
5208 tunnel_delete_peer (t, path->peers[path->length - 1]);
5209 path_destroy (path);
5215 * Core handler for notifications of broken paths
5217 * @param cls closure
5218 * @param message message
5219 * @param peer peer identity this notification is about
5220 * @param atsi performance data
5221 * @param atsi_count number of records in 'atsi'
5223 * @return GNUNET_OK to keep the connection open,
5224 * GNUNET_SYSERR to close it (signal serious error)
5227 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5228 const struct GNUNET_MessageHeader *message,
5229 const struct GNUNET_ATS_Information *atsi,
5230 unsigned int atsi_count)
5232 struct GNUNET_MESH_PathBroken *msg;
5233 struct MeshTunnel *t;
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5236 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5237 msg = (struct GNUNET_MESH_PathBroken *) message;
5238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5239 GNUNET_i2s (&msg->peer1));
5240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5241 GNUNET_i2s (&msg->peer2));
5242 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5245 GNUNET_break_op (0);
5248 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5249 GNUNET_PEER_search (&msg->peer2));
5256 * Core handler for tunnel destruction
5258 * @param cls closure
5259 * @param message message
5260 * @param peer peer identity this notification is about
5261 * @param atsi performance data
5262 * @param atsi_count number of records in 'atsi'
5264 * @return GNUNET_OK to keep the connection open,
5265 * GNUNET_SYSERR to close it (signal serious error)
5268 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5269 const struct GNUNET_MessageHeader *message,
5270 const struct GNUNET_ATS_Information *atsi,
5271 unsigned int atsi_count)
5273 struct GNUNET_MESH_TunnelDestroy *msg;
5274 struct MeshTunnel *t;
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5277 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5278 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5280 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5281 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5284 /* Probably already got the message from another path,
5285 * destroyed the tunnel and retransmitted to children.
5288 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5291 if (t->id.oid == myid)
5293 GNUNET_break_op (0);
5296 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5298 /* Tunnel was incoming, notify clients */
5299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5300 t->local_tid, t->local_tid_dest);
5301 send_clients_tunnel_destroy (t);
5303 tunnel_send_destroy (t);
5304 t->destroy = GNUNET_YES;
5305 // TODO: add timeout to destroy the tunnel anyway
5311 * Core handler for mesh network traffic going from the origin to a peer
5313 * @param cls closure
5314 * @param peer peer identity this notification is about
5315 * @param message message
5316 * @param atsi performance data
5317 * @param atsi_count number of records in 'atsi'
5318 * @return GNUNET_OK to keep the connection open,
5319 * GNUNET_SYSERR to close it (signal serious error)
5322 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5323 const struct GNUNET_MessageHeader *message,
5324 const struct GNUNET_ATS_Information *atsi,
5325 unsigned int atsi_count)
5327 struct GNUNET_MESH_Unicast *msg;
5328 struct GNUNET_PeerIdentity *neighbor;
5329 struct MeshTunnelChildInfo *cinfo;
5330 struct MeshTunnel *t;
5331 GNUNET_PEER_Id dest_id;
5336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5339 size = ntohs (message->size);
5341 sizeof (struct GNUNET_MESH_Unicast) +
5342 sizeof (struct GNUNET_MessageHeader))
5347 msg = (struct GNUNET_MESH_Unicast *) message;
5348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5349 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5351 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5354 /* TODO notify back: we don't know this tunnel */
5355 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5356 GNUNET_break_op (0);
5359 pid = ntohl (msg->pid);
5360 if (t->fwd_pid == pid)
5362 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5364 " Already seen pid %u, DROPPING!\n", pid);
5369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5370 " pid %u not seen yet, forwarding\n", pid);
5373 t->skip += (pid - t->fwd_pid) - 1;
5376 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5378 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5379 GNUNET_break_op (0);
5380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5381 "Received PID %u, ACK %u\n",
5382 pid, t->last_fwd_ack);
5386 tunnel_reset_timeout (t);
5387 dest_id = GNUNET_PEER_search (&msg->destination);
5388 if (dest_id == myid)
5390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5391 " it's for us! sending to clients...\n");
5392 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5393 send_subscribed_clients (message, &msg[1].header, t);
5394 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5397 ttl = ntohl (msg->ttl);
5398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5401 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5402 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5403 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5407 " not for us, retransmitting...\n");
5409 neighbor = tree_get_first_hop (t->tree, dest_id);
5410 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5412 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5415 if (GNUNET_YES == t->nobuffer &&
5416 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5418 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5419 GNUNET_break_op (0);
5422 send_message (message, neighbor, t);
5423 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5429 * Core handler for mesh network traffic going from the origin to all peers
5431 * @param cls closure
5432 * @param message message
5433 * @param peer peer identity this notification is about
5434 * @param atsi performance data
5435 * @param atsi_count number of records in 'atsi'
5436 * @return GNUNET_OK to keep the connection open,
5437 * GNUNET_SYSERR to close it (signal serious error)
5439 * TODO: Check who we got this from, to validate route.
5442 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5443 const struct GNUNET_MessageHeader *message,
5444 const struct GNUNET_ATS_Information *atsi,
5445 unsigned int atsi_count)
5447 struct GNUNET_MESH_Multicast *msg;
5448 struct MeshTunnel *t;
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5454 size = ntohs (message->size);
5455 if (sizeof (struct GNUNET_MESH_Multicast) +
5456 sizeof (struct GNUNET_MessageHeader) > size)
5458 GNUNET_break_op (0);
5461 msg = (struct GNUNET_MESH_Multicast *) message;
5462 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5466 /* TODO notify that we dont know that tunnel */
5467 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5468 GNUNET_break_op (0);
5471 pid = ntohl (msg->pid);
5472 if (t->fwd_pid == pid)
5474 /* already seen this packet, drop */
5475 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5477 " Already seen pid %u, DROPPING!\n", pid);
5478 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5484 " pid %u not seen yet, forwarding\n", pid);
5486 t->skip += (pid - t->fwd_pid) - 1;
5488 tunnel_reset_timeout (t);
5490 /* Transmit to locally interested clients */
5491 if (NULL != t->peers &&
5492 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5494 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5495 send_subscribed_clients (message, &msg[1].header, t);
5496 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5499 if (ntohl (msg->ttl) == 0)
5501 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5502 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5503 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5506 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5507 tunnel_send_multicast (t, message, GNUNET_NO);
5513 * Core handler for mesh network traffic toward the owner of a tunnel
5515 * @param cls closure
5516 * @param message message
5517 * @param peer peer identity this notification is about
5518 * @param atsi performance data
5519 * @param atsi_count number of records in 'atsi'
5521 * @return GNUNET_OK to keep the connection open,
5522 * GNUNET_SYSERR to close it (signal serious error)
5525 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5526 const struct GNUNET_MessageHeader *message,
5527 const struct GNUNET_ATS_Information *atsi,
5528 unsigned int atsi_count)
5530 struct GNUNET_MESH_ToOrigin *msg;
5531 struct GNUNET_PeerIdentity id;
5532 struct MeshPeerInfo *peer_info;
5533 struct MeshTunnel *t;
5537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5539 size = ntohs (message->size);
5540 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5541 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5543 GNUNET_break_op (0);
5546 msg = (struct GNUNET_MESH_ToOrigin *) message;
5547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5548 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5549 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5553 /* TODO notify that we dont know this tunnel (whom)? */
5554 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5555 GNUNET_break_op (0);
5559 if (NULL != t->owner)
5562 struct GNUNET_MESH_ToOrigin *copy;
5564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5565 " it's for us! sending to clients...\n");
5566 /* TODO signature verification */
5567 memcpy (cbuf, message, size);
5568 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5569 copy->tid = htonl (t->local_tid);
5570 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5571 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5572 ©->header, GNUNET_NO);
5573 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5577 " not for us, retransmitting...\n");
5579 peer_info = peer_info_get (&msg->oid);
5580 if (NULL == peer_info)
5582 /* unknown origin of tunnel */
5586 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5587 send_message (message, &id, t);
5588 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5595 * Core handler for mesh network traffic point-to-point acks.
5597 * @param cls closure
5598 * @param message message
5599 * @param peer peer identity this notification is about
5600 * @param atsi performance data
5601 * @param atsi_count number of records in 'atsi'
5603 * @return GNUNET_OK to keep the connection open,
5604 * GNUNET_SYSERR to close it (signal serious error)
5607 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5608 const struct GNUNET_MessageHeader *message,
5609 const struct GNUNET_ATS_Information *atsi,
5610 unsigned int atsi_count)
5612 struct GNUNET_MESH_ACK *msg;
5613 struct MeshTunnel *t;
5616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5618 msg = (struct GNUNET_MESH_ACK *) message;
5620 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5624 /* TODO notify that we dont know this tunnel (whom)? */
5625 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5626 GNUNET_break_op (0);
5629 ack = ntohl (msg->pid);
5631 /* Is this a forward or backward ACK? */
5632 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5634 struct MeshTunnelChildInfo *cinfo;
5637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5638 cinfo = tunnel_get_neighbor_fc (t, peer);
5639 cinfo->fwd_ack = ack;
5640 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5641 tunnel_unlock_fwd_queues (t);
5645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5647 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5648 tunnel_unlock_bck_queue (t);
5655 * Core handler for path ACKs
5657 * @param cls closure
5658 * @param message message
5659 * @param peer peer identity this notification is about
5660 * @param atsi performance data
5661 * @param atsi_count number of records in 'atsi'
5663 * @return GNUNET_OK to keep the connection open,
5664 * GNUNET_SYSERR to close it (signal serious error)
5667 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5668 const struct GNUNET_MessageHeader *message,
5669 const struct GNUNET_ATS_Information *atsi,
5670 unsigned int atsi_count)
5672 struct GNUNET_MESH_PathACK *msg;
5673 struct GNUNET_PeerIdentity id;
5674 struct MeshPeerInfo *peer_info;
5675 struct MeshPeerPath *p;
5676 struct MeshTunnel *t;
5678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5679 GNUNET_i2s (&my_full_id));
5680 msg = (struct GNUNET_MESH_PathACK *) message;
5681 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5684 /* TODO notify that we don't know the tunnel */
5685 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5687 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5691 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5693 peer_info = peer_info_get (&msg->peer_id);
5694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5695 GNUNET_i2s (&msg->peer_id));
5696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5699 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5702 "connect_by_string completed, stopping search\n");
5703 regex_cancel_search (t->regex_ctx);
5704 t->regex_ctx = NULL;
5707 /* Add paths to peers? */
5708 p = tree_get_path_to_peer (t->tree, peer_info->id);
5711 path_add_to_peers (p, GNUNET_YES);
5719 /* Message for us? */
5720 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5723 if (NULL == t->owner)
5725 GNUNET_break_op (0);
5728 if (NULL != t->dht_get_type)
5730 GNUNET_DHT_get_stop (t->dht_get_type);
5731 t->dht_get_type = NULL;
5733 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5735 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5736 send_client_peer_connected (t, peer_info->id);
5741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5742 " not for us, retransmitting...\n");
5743 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5744 peer_info = peer_info_get (&msg->oid);
5745 if (NULL == peer_info)
5747 /* If we know the tunnel, we should DEFINITELY know the peer */
5751 send_message (message, &id, t);
5757 * Core handler for mesh keepalives.
5759 * @param cls closure
5760 * @param message message
5761 * @param peer peer identity this notification is about
5762 * @param atsi performance data
5763 * @param atsi_count number of records in 'atsi'
5764 * @return GNUNET_OK to keep the connection open,
5765 * GNUNET_SYSERR to close it (signal serious error)
5767 * TODO: Check who we got this from, to validate route.
5770 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5771 const struct GNUNET_MessageHeader *message,
5772 const struct GNUNET_ATS_Information *atsi,
5773 unsigned int atsi_count)
5775 struct GNUNET_MESH_TunnelKeepAlive *msg;
5776 struct MeshTunnel *t;
5778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5781 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5782 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5786 /* TODO notify that we dont know that tunnel */
5787 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5788 GNUNET_break_op (0);
5792 tunnel_reset_timeout (t);
5794 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5795 tunnel_send_multicast (t, message, GNUNET_NO);
5802 * Functions to handle messages from core
5804 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5805 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5806 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5807 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5808 sizeof (struct GNUNET_MESH_PathBroken)},
5809 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5810 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5811 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5812 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5813 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5814 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5815 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5816 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5817 sizeof (struct GNUNET_MESH_ACK)},
5818 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5819 sizeof (struct GNUNET_MESH_PathACK)},
5825 /******************************************************************************/
5826 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5827 /******************************************************************************/
5830 * deregister_app: iterator for removing each application registered by a client
5832 * @param cls closure
5833 * @param key the hash of the application id (used to access the hashmap)
5834 * @param value the value stored at the key (client)
5836 * @return GNUNET_OK on success
5839 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5841 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5842 GNUNET_break (GNUNET_YES ==
5843 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5849 * notify_client_connection_failure: notify a client that the connection to the
5850 * requested remote peer is not possible (for instance, no route found)
5851 * Function called when the socket is ready to queue more data. "buf" will be
5852 * NULL and "size" zero if the socket was closed for writing in the meantime.
5854 * @param cls closure
5855 * @param size number of bytes available in buf
5856 * @param buf where the callee should write the message
5857 * @return number of bytes written to buf
5860 notify_client_connection_failure (void *cls, size_t size, void *buf)
5863 struct MeshPeerInfo *peer_info;
5864 struct GNUNET_MESH_PeerControl *msg;
5865 struct GNUNET_PeerIdentity id;
5867 if (0 == size && NULL == buf)
5869 // TODO retry? cancel?
5873 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5874 peer_info = (struct MeshPeerInfo *) cls;
5875 msg = (struct GNUNET_MESH_PeerControl *) buf;
5876 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5877 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5878 // msg->tunnel_id = htonl(peer_info->t->tid);
5879 GNUNET_PEER_resolve (peer_info->id, &id);
5880 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5888 * Send keepalive packets for a peer
5890 * @param cls Closure (tunnel for which to send the keepalive).
5891 * @param tc Notification context.
5894 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5896 struct MeshTunnel *t = cls;
5897 struct GNUNET_MESH_TunnelKeepAlive *msg;
5898 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5901 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5902 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5908 "sending keepalive for tunnel %d\n", t->id.tid);
5910 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5911 msg->header.size = htons (size);
5912 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5913 msg->oid = my_full_id;
5914 msg->tid = htonl (t->id.tid);
5915 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5917 t->path_refresh_task =
5918 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5924 * Function to process paths received for a new peer addition. The recorded
5925 * paths form the initial tunnel, which can be optimized later.
5926 * Called on each result obtained for the DHT search.
5928 * @param cls closure
5929 * @param exp when will this value expire
5930 * @param key key of the result
5931 * @param get_path path of the get request
5932 * @param get_path_length lenght of get_path
5933 * @param put_path path of the put request
5934 * @param put_path_length length of the put_path
5935 * @param type type of the result
5936 * @param size number of bytes in data
5937 * @param data pointer to the result data
5939 * TODO: re-issue the request after certain time? cancel after X results?
5942 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5943 const struct GNUNET_HashCode * key,
5944 const struct GNUNET_PeerIdentity *get_path,
5945 unsigned int get_path_length,
5946 const struct GNUNET_PeerIdentity *put_path,
5947 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5948 size_t size, const void *data)
5950 struct MeshPathInfo *path_info = cls;
5951 struct MeshPeerPath *p;
5952 struct GNUNET_PeerIdentity pi;
5955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5956 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5959 p = path_build_from_dht (get_path, get_path_length, put_path,
5961 path_add_to_peers (p, GNUNET_NO);
5963 for (i = 0; i < path_info->peer->ntunnels; i++)
5965 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5966 peer_info_connect (path_info->peer, path_info->t);
5974 * Function to process paths received for a new peer addition. The recorded
5975 * paths form the initial tunnel, which can be optimized later.
5976 * Called on each result obtained for the DHT search.
5978 * @param cls closure
5979 * @param exp when will this value expire
5980 * @param key key of the result
5981 * @param get_path path of the get request
5982 * @param get_path_length lenght of get_path
5983 * @param put_path path of the put request
5984 * @param put_path_length length of the put_path
5985 * @param type type of the result
5986 * @param size number of bytes in data
5987 * @param data pointer to the result data
5990 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5991 const struct GNUNET_HashCode * key,
5992 const struct GNUNET_PeerIdentity *get_path,
5993 unsigned int get_path_length,
5994 const struct GNUNET_PeerIdentity *put_path,
5995 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5996 size_t size, const void *data)
5998 const struct PBlock *pb = data;
5999 const struct GNUNET_PeerIdentity *pi = &pb->id;
6000 struct MeshTunnel *t = cls;
6001 struct MeshPeerInfo *peer_info;
6002 struct MeshPeerPath *p;
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6005 if (size != sizeof (struct PBlock))
6007 GNUNET_break_op (0);
6010 if (ntohl(pb->type) != t->type)
6012 GNUNET_break_op (0);
6015 GNUNET_assert (NULL != t->owner);
6016 peer_info = peer_info_get (pi);
6017 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6019 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6021 p = path_build_from_dht (get_path, get_path_length, put_path,
6023 path_add_to_peers (p, GNUNET_NO);
6025 tunnel_add_peer (t, peer_info);
6026 peer_info_connect (peer_info, t);
6031 * Function to process DHT string to regex matching.
6032 * Called on each result obtained for the DHT search.
6034 * @param cls closure (search context)
6035 * @param exp when will this value expire
6036 * @param key key of the result
6037 * @param get_path path of the get request (not used)
6038 * @param get_path_length lenght of get_path (not used)
6039 * @param put_path path of the put request (not used)
6040 * @param put_path_length length of the put_path (not used)
6041 * @param type type of the result
6042 * @param size number of bytes in data
6043 * @param data pointer to the result data
6046 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6047 const struct GNUNET_HashCode * key,
6048 const struct GNUNET_PeerIdentity *get_path,
6049 unsigned int get_path_length,
6050 const struct GNUNET_PeerIdentity *put_path,
6051 unsigned int put_path_length,
6052 enum GNUNET_BLOCK_Type type,
6053 size_t size, const void *data)
6055 const struct MeshRegexAccept *block = data;
6056 struct MeshRegexSearchContext *ctx = cls;
6057 struct MeshRegexSearchInfo *info = ctx->info;
6058 struct MeshPeerPath *p;
6059 struct MeshPeerInfo *peer_info;
6061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6064 peer_info = peer_info_get(&block->id);
6065 p = path_build_from_dht (get_path, get_path_length, put_path,
6067 path_add_to_peers (p, GNUNET_NO);
6070 tunnel_add_peer (info->t, peer_info);
6071 peer_info_connect (peer_info, info->t);
6072 if (0 == info->peer)
6074 info->peer = peer_info->id;
6078 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6081 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6082 ®ex_connect_timeout,
6090 * Function to process DHT string to regex matching.
6091 * Called on each result obtained for the DHT search.
6093 * @param cls closure (search context)
6094 * @param exp when will this value expire
6095 * @param key key of the result
6096 * @param get_path path of the get request (not used)
6097 * @param get_path_length lenght of get_path (not used)
6098 * @param put_path path of the put request (not used)
6099 * @param put_path_length length of the put_path (not used)
6100 * @param type type of the result
6101 * @param size number of bytes in data
6102 * @param data pointer to the result data
6104 * TODO: re-issue the request after certain time? cancel after X results?
6107 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6108 const struct GNUNET_HashCode * key,
6109 const struct GNUNET_PeerIdentity *get_path,
6110 unsigned int get_path_length,
6111 const struct GNUNET_PeerIdentity *put_path,
6112 unsigned int put_path_length,
6113 enum GNUNET_BLOCK_Type type,
6114 size_t size, const void *data)
6116 const struct MeshRegexBlock *block = data;
6117 struct MeshRegexSearchContext *ctx = cls;
6118 struct MeshRegexSearchInfo *info = ctx->info;
6122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6123 "DHT GET STRING RETURNED RESULTS\n");
6124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6125 " key: %s\n", GNUNET_h2s (key));
6127 copy = GNUNET_malloc (size);
6128 memcpy (copy, data, size);
6129 GNUNET_break (GNUNET_OK ==
6130 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6131 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6132 len = ntohl (block->n_proof);
6134 char proof[len + 1];
6136 memcpy (proof, &block[1], len);
6138 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6140 GNUNET_break_op (0);
6144 len = strlen (info->description);
6145 if (len == ctx->position) // String processed
6147 if (GNUNET_YES == ntohl (block->accepting))
6149 regex_find_path(key, ctx);
6153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6154 // FIXME REGEX this block not successful, wait for more? start timeout?
6159 regex_next_edge (block, size, ctx);
6164 /******************************************************************************/
6165 /********************* MESH LOCAL HANDLES **************************/
6166 /******************************************************************************/
6170 * Handler for client disconnection
6172 * @param cls closure
6173 * @param client identification of the client; NULL
6174 * for the last call when the server is destroyed
6177 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6179 struct MeshClient *c;
6180 struct MeshClient *next;
6183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6192 if (c->handle != client)
6194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6200 GNUNET_SERVER_client_drop (c->handle);
6201 c->shutting_down = GNUNET_YES;
6202 GNUNET_assert (NULL != c->own_tunnels);
6203 GNUNET_assert (NULL != c->incoming_tunnels);
6204 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6205 &tunnel_destroy_iterator, c);
6206 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6207 &tunnel_destroy_iterator, c);
6208 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6209 &tunnel_destroy_iterator, c);
6210 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6211 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6212 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6214 /* deregister clients applications */
6215 if (NULL != c->apps)
6217 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6218 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6220 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6221 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6223 GNUNET_SCHEDULER_cancel (announce_applications_task);
6224 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6226 if (NULL != c->types)
6227 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6228 for (i = 0; i < c->n_regex; i++)
6230 GNUNET_free (c->regexes[i]);
6232 GNUNET_free_non_null (c->regexes);
6233 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6234 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6236 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6239 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6248 * Handler for new clients
6250 * @param cls closure
6251 * @param client identification of the client
6252 * @param message the actual message, which includes messages the client wants
6255 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6256 const struct GNUNET_MessageHeader *message)
6258 struct GNUNET_MESH_ClientConnect *cc_msg;
6259 struct MeshClient *c;
6260 GNUNET_MESH_ApplicationType *a;
6267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6268 /* Check data sanity */
6269 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6270 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6271 ntypes = ntohs (cc_msg->types);
6272 napps = ntohs (cc_msg->applications);
6274 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6281 /* Create new client structure */
6282 c = GNUNET_malloc (sizeof (struct MeshClient));
6283 c->id = next_client_id++;
6284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6286 GNUNET_SERVER_client_keep (client);
6287 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6290 GNUNET_MESH_ApplicationType at;
6291 struct GNUNET_HashCode hc;
6293 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6294 for (i = 0; i < napps; i++)
6297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6298 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6299 /* store in clients hashmap */
6300 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6302 /* store in global hashmap, for announcements */
6303 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6304 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6306 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6307 announce_applications_task =
6308 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6314 struct GNUNET_HashCode hc;
6316 t = (uint16_t *) & a[napps];
6317 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6318 for (i = 0; i < ntypes; i++)
6321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6322 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6324 /* store in clients hashmap */
6325 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6327 /* store in global hashmap */
6328 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6333 " client has %u+%u subscriptions\n", napps, ntypes);
6335 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6336 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6337 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6338 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6339 GNUNET_SERVER_notification_context_add (nc, client);
6340 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6342 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6348 * Handler for clients announcing available services by a regular expression.
6350 * @param cls closure
6351 * @param client identification of the client
6352 * @param message the actual message, which includes messages the client wants
6355 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6356 const struct GNUNET_MessageHeader *message)
6358 struct MeshClient *c;
6362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6364 /* Sanity check for client registration */
6365 if (NULL == (c = client_get (client)))
6368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6373 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6374 regex = GNUNET_malloc (len + 1);
6375 memcpy (regex, &message[1], len);
6377 GNUNET_array_append (c->regexes, c->n_regex, regex);
6378 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6380 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6386 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6392 * Handler for requests of new tunnels
6394 * @param cls closure
6395 * @param client identification of the client
6396 * @param message the actual message
6399 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6400 const struct GNUNET_MessageHeader *message)
6402 struct GNUNET_MESH_TunnelMessage *t_msg;
6403 struct MeshTunnel *t;
6404 struct MeshClient *c;
6405 MESH_TunnelNumber tid;
6407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6409 /* Sanity check for client registration */
6410 if (NULL == (c = client_get (client)))
6413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6418 /* Message sanity check */
6419 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6426 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6427 /* Sanity check for tunnel numbering */
6428 tid = ntohl (t_msg->tunnel_id);
6429 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6432 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6435 /* Sanity check for duplicate tunnel IDs */
6436 if (NULL != tunnel_get_by_local_id (c, tid))
6439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6443 while (NULL != tunnel_get_by_pi (myid, next_tid))
6444 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6445 t = tunnel_new (myid, next_tid++, c, tid);
6448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6453 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6455 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6456 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6465 * Handler for requests of deleting tunnels
6467 * @param cls closure
6468 * @param client identification of the client
6469 * @param message the actual message
6472 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6473 const struct GNUNET_MessageHeader *message)
6475 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6476 struct MeshClient *c;
6477 struct MeshTunnel *t;
6478 MESH_TunnelNumber tid;
6480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6481 "Got a DESTROY TUNNEL from client!\n");
6483 /* Sanity check for client registration */
6484 if (NULL == (c = client_get (client)))
6487 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6492 /* Message sanity check */
6493 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6496 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6500 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6502 /* Retrieve tunnel */
6503 tid = ntohl (tunnel_msg->tunnel_id);
6504 t = tunnel_get_by_local_id(c, tid);
6507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6512 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6514 client_ignore_tunnel (c, t);
6516 // TODO: when to destroy incoming tunnel?
6517 if (t->nclients == 0)
6519 GNUNET_assert (GNUNET_YES ==
6520 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6522 GNUNET_assert (GNUNET_YES ==
6523 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6524 &my_full_id.hashPubKey,
6528 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6531 send_client_tunnel_disconnect(t, c);
6532 client_delete_tunnel(c, t);
6534 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6536 tunnel_send_destroy (t);
6537 t->destroy = GNUNET_YES;
6538 // The tunnel will be destroyed when the last message is transmitted.
6539 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6545 * Handler for requests of seeting tunnel's speed.
6547 * @param cls Closure (unused).
6548 * @param client Identification of the client.
6549 * @param message The actual message.
6552 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6553 const struct GNUNET_MessageHeader *message)
6555 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6556 struct MeshClient *c;
6557 struct MeshTunnel *t;
6558 MESH_TunnelNumber tid;
6560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6561 "Got a SPEED request from client!\n");
6563 /* Sanity check for client registration */
6564 if (NULL == (c = client_get (client)))
6567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6573 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6575 /* Retrieve tunnel */
6576 tid = ntohl (tunnel_msg->tunnel_id);
6577 t = tunnel_get_by_local_id(c, tid);
6580 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6582 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6586 switch (ntohs(message->type))
6588 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6589 t->speed_min = GNUNET_YES;
6591 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6592 t->speed_min = GNUNET_NO;
6597 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6602 * Handler for requests of seeting tunnel's buffering policy.
6604 * @param cls Closure (unused).
6605 * @param client Identification of the client.
6606 * @param message The actual message.
6609 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6610 const struct GNUNET_MessageHeader *message)
6612 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6613 struct MeshClient *c;
6614 struct MeshTunnel *t;
6615 MESH_TunnelNumber tid;
6617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6618 "Got a BUFFER request from client!\n");
6620 /* Sanity check for client registration */
6621 if (NULL == (c = client_get (client)))
6624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6629 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6631 /* Retrieve tunnel */
6632 tid = ntohl (tunnel_msg->tunnel_id);
6633 t = tunnel_get_by_local_id(c, tid);
6636 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6638 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6642 switch (ntohs(message->type))
6644 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6645 t->nobuffer = GNUNET_NO;
6647 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6648 t->nobuffer = GNUNET_YES;
6654 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6659 * Handler for connection requests to new peers
6661 * @param cls closure
6662 * @param client identification of the client
6663 * @param message the actual message (PeerControl)
6666 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6667 const struct GNUNET_MessageHeader *message)
6669 struct GNUNET_MESH_PeerControl *peer_msg;
6670 struct MeshPeerInfo *peer_info;
6671 struct MeshClient *c;
6672 struct MeshTunnel *t;
6673 MESH_TunnelNumber tid;
6675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6676 /* Sanity check for client registration */
6677 if (NULL == (c = client_get (client)))
6680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6685 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6687 /* Sanity check for message size */
6688 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6691 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6695 /* Tunnel exists? */
6696 tid = ntohl (peer_msg->tunnel_id);
6697 t = tunnel_get_by_local_id (c, tid);
6701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6705 /* Does client own tunnel? */
6706 if (t->owner->handle != client)
6709 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6713 GNUNET_i2s (&peer_msg->peer));
6714 peer_info = peer_info_get (&peer_msg->peer);
6716 tunnel_add_peer (t, peer_info);
6717 peer_info_connect (peer_info, t);
6719 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6725 * Handler for disconnection requests of peers in a tunnel
6727 * @param cls closure
6728 * @param client identification of the client
6729 * @param message the actual message (PeerControl)
6732 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6733 const struct GNUNET_MessageHeader *message)
6735 struct GNUNET_MESH_PeerControl *peer_msg;
6736 struct MeshPeerInfo *peer_info;
6737 struct MeshClient *c;
6738 struct MeshTunnel *t;
6739 MESH_TunnelNumber tid;
6741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6742 /* Sanity check for client registration */
6743 if (NULL == (c = client_get (client)))
6746 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6751 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6753 /* Sanity check for message size */
6754 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6757 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6761 /* Tunnel exists? */
6762 tid = ntohl (peer_msg->tunnel_id);
6763 t = tunnel_get_by_local_id (c, tid);
6767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6772 /* Does client own tunnel? */
6773 if (t->owner->handle != client)
6776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6781 GNUNET_i2s (&peer_msg->peer));
6782 /* Is the peer in the tunnel? */
6784 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6785 if (NULL == peer_info)
6788 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6792 /* Ok, delete peer from tunnel */
6793 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6794 &peer_msg->peer.hashPubKey);
6796 send_destroy_path (t, peer_info->id);
6797 tunnel_delete_peer (t, peer_info->id);
6798 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6803 * Handler for blacklist requests of peers in a tunnel
6805 * @param cls closure
6806 * @param client identification of the client
6807 * @param message the actual message (PeerControl)
6809 * FIXME implement DHT block bloomfilter
6812 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6813 const struct GNUNET_MessageHeader *message)
6815 struct GNUNET_MESH_PeerControl *peer_msg;
6816 struct MeshClient *c;
6817 struct MeshTunnel *t;
6818 MESH_TunnelNumber tid;
6820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6821 /* Sanity check for client registration */
6822 if (NULL == (c = client_get (client)))
6825 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6830 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6832 /* Sanity check for message size */
6833 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6840 /* Tunnel exists? */
6841 tid = ntohl (peer_msg->tunnel_id);
6842 t = tunnel_get_by_local_id (c, tid);
6846 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6851 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6852 GNUNET_PEER_intern(&peer_msg->peer));
6857 * Handler for unblacklist requests of peers in a tunnel
6859 * @param cls closure
6860 * @param client identification of the client
6861 * @param message the actual message (PeerControl)
6864 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6865 const struct GNUNET_MessageHeader *message)
6867 struct GNUNET_MESH_PeerControl *peer_msg;
6868 struct MeshClient *c;
6869 struct MeshTunnel *t;
6870 MESH_TunnelNumber tid;
6874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6875 /* Sanity check for client registration */
6876 if (NULL == (c = client_get (client)))
6879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6884 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6886 /* Sanity check for message size */
6887 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6890 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6894 /* Tunnel exists? */
6895 tid = ntohl (peer_msg->tunnel_id);
6896 t = tunnel_get_by_local_id (c, tid);
6900 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6905 /* if peer is not known, complain */
6906 pid = GNUNET_PEER_search (&peer_msg->peer);
6913 /* search and remove from list */
6914 for (i = 0; i < t->nblacklisted; i++)
6916 if (t->blacklisted[i] == pid)
6918 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6919 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6924 /* if peer hasn't been blacklisted, complain */
6930 * Handler for connection requests to new peers by type
6932 * @param cls closure
6933 * @param client identification of the client
6934 * @param message the actual message (ConnectPeerByType)
6937 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6938 const struct GNUNET_MessageHeader *message)
6940 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6941 struct MeshClient *c;
6942 struct MeshTunnel *t;
6943 struct GNUNET_HashCode hash;
6944 MESH_TunnelNumber tid;
6946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6947 /* Sanity check for client registration */
6948 if (NULL == (c = client_get (client)))
6951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6956 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6958 /* Sanity check for message size */
6959 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6960 ntohs (connect_msg->header.size))
6963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6967 /* Tunnel exists? */
6968 tid = ntohl (connect_msg->tunnel_id);
6969 t = tunnel_get_by_local_id (c, tid);
6973 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6977 /* Does client own tunnel? */
6978 if (t->owner->handle != client)
6981 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6985 /* Do WE have the service? */
6986 t->type = ntohl (connect_msg->type);
6987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6988 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6989 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6992 /* Yes! Fast forward, add ourselves to the tunnel and send the
6993 * good news to the client, and alert the destination client of
6994 * an incoming tunnel.
6996 * FIXME send a path create to self, avoid code duplication
6998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6999 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7000 peer_info_get (&my_full_id),
7001 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7004 send_client_peer_connected (t, myid);
7005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7006 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7008 t->local_tid_dest = next_local_tid++;
7009 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7010 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7011 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7015 /* Ok, lets find a peer offering the service */
7016 if (NULL != t->dht_get_type)
7018 GNUNET_DHT_get_stop (t->dht_get_type);
7020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7021 GNUNET_h2s (&hash));
7023 GNUNET_DHT_get_start (dht_handle,
7024 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7026 dht_replication_level,
7027 GNUNET_DHT_RO_RECORD_ROUTE |
7028 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7030 &dht_get_type_handler, t);
7032 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7038 * Handler for connection requests to new peers by a string service description.
7040 * @param cls closure
7041 * @param client identification of the client
7042 * @param message the actual message, which includes messages the client wants
7045 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7046 const struct GNUNET_MessageHeader *message)
7048 struct GNUNET_MESH_ConnectPeerByString *msg;
7049 struct MeshRegexSearchContext *ctx;
7050 struct MeshRegexSearchInfo *info;
7051 struct GNUNET_DHT_GetHandle *get_h;
7052 struct GNUNET_HashCode key;
7053 struct MeshTunnel *t;
7054 struct MeshClient *c;
7055 MESH_TunnelNumber tid;
7059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7060 "Connect by string started\n");
7061 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7062 size = htons (message->size);
7064 /* Sanity check for client registration */
7065 if (NULL == (c = client_get (client)))
7068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7073 /* Message size sanity check */
7074 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7077 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7081 /* Tunnel exists? */
7082 tid = ntohl (msg->tunnel_id);
7083 t = tunnel_get_by_local_id (c, tid);
7087 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7091 /* Does client own tunnel? */
7092 if (t->owner->handle != client)
7095 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7100 " on tunnel %s [%u]\n",
7101 GNUNET_i2s(&my_full_id),
7104 /* Only one connect_by_string allowed at the same time! */
7105 /* FIXME: allow more, return handle at api level to cancel, document */
7106 if (NULL != t->regex_ctx)
7109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7113 /* Find string itself */
7114 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7115 string = (const char *) &msg[1];
7117 /* Initialize context */
7118 size = GNUNET_REGEX_get_first_key(string, len, &key);
7119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7120 " consumed %u bits out of %u\n", size, len);
7121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7122 " looking for %s\n", GNUNET_h2s (&key));
7124 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7126 info->description = GNUNET_malloc (len + 1);
7127 memcpy (info->description, string, len);
7128 info->description[len] = '\0';
7129 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7130 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7133 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7134 ctx->position = size;
7138 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7140 /* Start search in DHT */
7141 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7142 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7143 &key, /* key to search */
7144 dht_replication_level, /* replication level */
7145 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7146 NULL, /* xquery */ // FIXME BLOOMFILTER
7147 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7148 &dht_get_string_handler, ctx);
7150 GNUNET_break (GNUNET_OK ==
7151 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7154 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7156 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7162 * Handler for client traffic directed to one peer
7164 * @param cls closure
7165 * @param client identification of the client
7166 * @param message the actual message
7169 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7170 const struct GNUNET_MessageHeader *message)
7172 struct MeshClient *c;
7173 struct MeshTunnel *t;
7174 struct MeshPeerInfo *pi;
7175 struct GNUNET_MESH_Unicast *data_msg;
7176 MESH_TunnelNumber tid;
7179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7180 "Got a unicast request from a client!\n");
7182 /* Sanity check for client registration */
7183 if (NULL == (c = client_get (client)))
7186 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7191 data_msg = (struct GNUNET_MESH_Unicast *) message;
7193 /* Sanity check for message size */
7194 size = ntohs (message->size);
7195 if (sizeof (struct GNUNET_MESH_Unicast) +
7196 sizeof (struct GNUNET_MessageHeader) > size)
7199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7203 /* Tunnel exists? */
7204 tid = ntohl (data_msg->tid);
7205 t = tunnel_get_by_local_id (c, tid);
7209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7213 /* Is it a local tunnel? Then, does client own the tunnel? */
7214 if (t->owner->handle != client)
7217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7221 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7222 &data_msg->destination.hashPubKey);
7223 /* Is the selected peer in the tunnel? */
7227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7231 /* PID should be as expected */
7232 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7235 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7236 "Unicast PID, expected %u, got %u\n",
7237 t->fwd_pid + 1, ntohl (data_msg->pid));
7238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7242 /* Ok, everything is correct, send the message
7243 * (pretend we got it from a mesh peer)
7246 /* Work around const limitation */
7247 char buf[ntohs (message->size)] GNUNET_ALIGN;
7248 struct GNUNET_MESH_Unicast *copy;
7250 copy = (struct GNUNET_MESH_Unicast *) buf;
7251 memcpy (buf, data_msg, size);
7252 copy->oid = my_full_id;
7253 copy->tid = htonl (t->id.tid);
7254 copy->ttl = htonl (default_ttl);
7255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7256 " calling generic handler...\n");
7257 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7260 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7267 * Handler for client traffic directed to the origin
7269 * @param cls closure
7270 * @param client identification of the client
7271 * @param message the actual message
7274 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7275 const struct GNUNET_MessageHeader *message)
7277 struct GNUNET_MESH_ToOrigin *data_msg;
7278 struct MeshTunnelClientInfo *clinfo;
7279 struct MeshClient *c;
7280 struct MeshTunnel *t;
7281 MESH_TunnelNumber tid;
7284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7285 "Got a ToOrigin request from a client!\n");
7286 /* Sanity check for client registration */
7287 if (NULL == (c = client_get (client)))
7290 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7295 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7297 /* Sanity check for message size */
7298 size = ntohs (message->size);
7299 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7300 sizeof (struct GNUNET_MessageHeader) > size)
7303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7307 /* Tunnel exists? */
7308 tid = ntohl (data_msg->tid);
7309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7310 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7313 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7316 t = tunnel_get_by_local_id (c, tid);
7319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7320 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7322 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7326 /* It should be sent by someone who has this as incoming tunnel. */
7327 if (GNUNET_NO == client_knows_tunnel (c, t))
7330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7334 /* PID should be as expected */
7335 clinfo = tunnel_get_client_fc (t, c);
7336 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7340 "To Origin PID, expected %u, got %u\n",
7341 clinfo->bck_pid + 1,
7342 ntohl (data_msg->pid));
7343 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7347 /* Ok, everything is correct, send the message
7348 * (pretend we got it from a mesh peer)
7352 char buf[ntohs (message->size)] GNUNET_ALIGN;
7353 struct GNUNET_MESH_ToOrigin *copy;
7355 /* Work around const limitation */
7356 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7357 memcpy (buf, data_msg, size);
7358 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7359 copy->tid = htonl (t->id.tid);
7360 copy->ttl = htonl (default_ttl);
7361 if (ntohl (copy->pid) != (t->bck_pid + 1))
7364 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7365 "To Origin PID, expected %u, got %u\n",
7371 copy->sender = my_full_id;
7372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7373 " calling generic handler...\n");
7374 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7376 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7383 * Handler for client traffic directed to all peers in a tunnel
7385 * @param cls closure
7386 * @param client identification of the client
7387 * @param message the actual message
7390 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7391 const struct GNUNET_MessageHeader *message)
7393 struct MeshClient *c;
7394 struct MeshTunnel *t;
7395 struct GNUNET_MESH_Multicast *data_msg;
7396 MESH_TunnelNumber tid;
7398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7399 "Got a multicast request from a client!\n");
7401 /* Sanity check for client registration */
7402 if (NULL == (c = client_get (client)))
7405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7410 data_msg = (struct GNUNET_MESH_Multicast *) message;
7412 /* Sanity check for message size */
7413 if (sizeof (struct GNUNET_MESH_Multicast) +
7414 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7421 /* Tunnel exists? */
7422 tid = ntohl (data_msg->tid);
7423 t = tunnel_get_by_local_id (c, tid);
7427 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7428 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7434 /* Does client own tunnel? */
7435 if (t->owner->handle != client)
7438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7442 /* PID should be as expected */
7443 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7446 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7447 "Multicast PID, expected %u, got %u\n",
7448 t->fwd_pid + 1, ntohl (data_msg->pid));
7449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7454 char buf[ntohs (message->size)] GNUNET_ALIGN;
7455 struct GNUNET_MESH_Multicast *copy;
7457 copy = (struct GNUNET_MESH_Multicast *) buf;
7458 memcpy (buf, message, ntohs (message->size));
7459 copy->oid = my_full_id;
7460 copy->tid = htonl (t->id.tid);
7461 copy->ttl = htonl (default_ttl);
7462 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7464 " calling generic handler...\n");
7465 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7468 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7474 * Handler for client's ACKs for payload traffic.
7476 * @param cls Closure (unused).
7477 * @param client Identification of the client.
7478 * @param message The actual message.
7481 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7482 const struct GNUNET_MessageHeader *message)
7484 struct GNUNET_MESH_LocalAck *msg;
7485 struct MeshTunnel *t;
7486 struct MeshClient *c;
7487 MESH_TunnelNumber tid;
7490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7491 /* Sanity check for client registration */
7492 if (NULL == (c = client_get (client)))
7495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7500 msg = (struct GNUNET_MESH_LocalAck *) message;
7502 /* Tunnel exists? */
7503 tid = ntohl (msg->tunnel_id);
7504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7505 t = tunnel_get_by_local_id (c, tid);
7509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7511 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7515 ack = ntohl (msg->max_pid);
7516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7518 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7519 if (NULL != t->owner && t->owner->handle == client)
7521 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7523 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7527 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7528 tunnel_set_client_fwd_ack (t, c, ack);
7529 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7532 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7539 * Functions to handle messages from clients
7541 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7542 {&handle_local_new_client, NULL,
7543 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7544 {&handle_local_announce_regex, NULL,
7545 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7546 {&handle_local_tunnel_create, NULL,
7547 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7548 sizeof (struct GNUNET_MESH_TunnelMessage)},
7549 {&handle_local_tunnel_destroy, NULL,
7550 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7551 sizeof (struct GNUNET_MESH_TunnelMessage)},
7552 {&handle_local_tunnel_speed, NULL,
7553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7554 sizeof (struct GNUNET_MESH_TunnelMessage)},
7555 {&handle_local_tunnel_speed, NULL,
7556 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7557 sizeof (struct GNUNET_MESH_TunnelMessage)},
7558 {&handle_local_tunnel_buffer, NULL,
7559 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7560 sizeof (struct GNUNET_MESH_TunnelMessage)},
7561 {&handle_local_tunnel_buffer, NULL,
7562 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7563 sizeof (struct GNUNET_MESH_TunnelMessage)},
7564 {&handle_local_connect_add, NULL,
7565 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7566 sizeof (struct GNUNET_MESH_PeerControl)},
7567 {&handle_local_connect_del, NULL,
7568 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7569 sizeof (struct GNUNET_MESH_PeerControl)},
7570 {&handle_local_blacklist, NULL,
7571 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7572 sizeof (struct GNUNET_MESH_PeerControl)},
7573 {&handle_local_unblacklist, NULL,
7574 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7575 sizeof (struct GNUNET_MESH_PeerControl)},
7576 {&handle_local_connect_by_type, NULL,
7577 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7578 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7579 {&handle_local_connect_by_string, NULL,
7580 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7581 {&handle_local_unicast, NULL,
7582 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7583 {&handle_local_to_origin, NULL,
7584 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7585 {&handle_local_multicast, NULL,
7586 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7587 {&handle_local_ack, NULL,
7588 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7589 sizeof (struct GNUNET_MESH_LocalAck)},
7595 * To be called on core init/fail.
7597 * @param cls service closure
7598 * @param server handle to the server for this service
7599 * @param identity the public identity of this peer
7602 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7603 const struct GNUNET_PeerIdentity *identity)
7606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7607 core_handle = server;
7608 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7612 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7614 GNUNET_abort(); // Try harder
7621 * Method called whenever a given peer connects.
7623 * @param cls closure
7624 * @param peer peer identity this notification is about
7625 * @param atsi performance data for the connection
7626 * @param atsi_count number of records in 'atsi'
7629 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7630 const struct GNUNET_ATS_Information *atsi,
7631 unsigned int atsi_count)
7633 struct MeshPeerInfo *peer_info;
7634 struct MeshPeerPath *path;
7636 DEBUG_CONN ("Peer connected\n");
7637 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7638 peer_info = peer_info_get (peer);
7639 if (myid == peer_info->id)
7641 DEBUG_CONN (" (self)\n");
7646 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7648 path = path_new (2);
7649 path->peers[0] = myid;
7650 path->peers[1] = peer_info->id;
7651 GNUNET_PEER_change_rc (myid, 1);
7652 GNUNET_PEER_change_rc (peer_info->id, 1);
7653 peer_info_add_path (peer_info, path, GNUNET_YES);
7654 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7660 * Method called whenever a peer disconnects.
7662 * @param cls closure
7663 * @param peer peer identity this notification is about
7666 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7668 struct MeshPeerInfo *pi;
7669 struct MeshPeerQueue *q;
7670 struct MeshPeerQueue *n;
7672 DEBUG_CONN ("Peer disconnected\n");
7673 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7685 /* try to reroute this traffic instead */
7686 queue_destroy(q, GNUNET_YES);
7690 peer_info_remove_path (pi, pi->id, myid);
7693 DEBUG_CONN (" (self)\n");
7695 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7700 /******************************************************************************/
7701 /************************ MAIN FUNCTIONS ****************************/
7702 /******************************************************************************/
7705 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7707 * @param cls closure
7708 * @param key current key code
7709 * @param value value in the hash map
7710 * @return GNUNET_YES if we should continue to iterate,
7714 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7716 struct MeshTunnel *t = value;
7723 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7725 * @param cls closure
7726 * @param key current key code
7727 * @param value value in the hash map
7728 * @return GNUNET_YES if we should continue to iterate,
7732 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7734 struct MeshPeerInfo *p = value;
7735 struct MeshPeerQueue *q;
7736 struct MeshPeerQueue *n;
7744 queue_destroy(q, GNUNET_YES);
7748 peer_info_destroy (p);
7754 * Task run during shutdown.
7760 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7764 if (core_handle != NULL)
7766 GNUNET_CORE_disconnect (core_handle);
7771 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
7774 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7775 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7776 if (dht_handle != NULL)
7778 GNUNET_DHT_disconnect (dht_handle);
7783 GNUNET_SERVER_notification_context_destroy (nc);
7786 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7788 GNUNET_SCHEDULER_cancel (announce_id_task);
7789 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7796 * Callback for hostkey read/generation
7799 * @param pk the private key
7800 * @param emsg error message
7803 key_generation_cb (void *cls,
7804 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
7807 struct MeshPeerInfo *peer;
7808 struct MeshPeerPath *p;
7813 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7814 _("Mesh service could not access hostkey. Exiting.\n"));
7815 GNUNET_SCHEDULER_shutdown ();
7818 my_private_key = pk;
7819 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7820 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7821 &my_full_id.hashPubKey);
7822 myid = GNUNET_PEER_intern (&my_full_id);
7823 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7824 "Mesh for peer [%s] starting\n",
7825 GNUNET_i2s(&my_full_id));
7827 // transport_handle = GNUNET_TRANSPORT_connect(c,
7837 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7840 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7841 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7842 GNUNET_SERVER_disconnect_notify (server_handle,
7843 &handle_local_client_disconnect, NULL);
7847 clients_tail = NULL;
7850 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7851 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7853 /* Create a peer_info for the local peer */
7854 peer = peer_info_get (&my_full_id);
7857 GNUNET_PEER_change_rc (myid, 1);
7858 peer_info_add_path (peer, p, GNUNET_YES);
7859 GNUNET_SERVER_resume (server_handle);
7860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7865 * Process mesh requests.
7867 * @param cls closure
7868 * @param server the initialized server
7869 * @param c configuration to use
7872 run (void *cls, struct GNUNET_SERVER_Handle *server,
7873 const struct GNUNET_CONFIGURATION_Handle *c)
7877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7878 server_handle = server;
7879 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7880 NULL, /* Closure passed to MESH functions */
7881 &core_init, /* Call core_init once connected */
7882 &core_connect, /* Handle connects */
7883 &core_disconnect, /* remove peers on disconnects */
7884 NULL, /* Don't notify about all incoming messages */
7885 GNUNET_NO, /* For header only in notification */
7886 NULL, /* Don't notify about all outbound messages */
7887 GNUNET_NO, /* For header-only out notification */
7888 core_handlers); /* Register these handlers */
7890 if (core_handle == NULL)
7893 GNUNET_SCHEDULER_shutdown ();
7898 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7903 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7905 GNUNET_SCHEDULER_shutdown ();
7910 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7911 &refresh_path_time))
7913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7915 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7916 "refresh path time");
7917 GNUNET_SCHEDULER_shutdown ();
7922 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7923 &app_announce_time))
7925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7927 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7928 "app announce time");
7929 GNUNET_SCHEDULER_shutdown ();
7934 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7939 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7940 "id announce time");
7941 GNUNET_SCHEDULER_shutdown ();
7946 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7947 &unacknowledged_wait_time))
7949 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7951 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7952 "unacknowledged wait time");
7953 GNUNET_SCHEDULER_shutdown ();
7958 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7961 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7963 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7965 GNUNET_SCHEDULER_shutdown ();
7970 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7973 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7975 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7977 GNUNET_SCHEDULER_shutdown ();
7982 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7987 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7989 GNUNET_SCHEDULER_shutdown ();
7994 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7997 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7999 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
8005 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8006 &dht_replication_level))
8008 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8010 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
8011 "dht replication level", 10);
8012 dht_replication_level = 10;
8015 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8016 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8017 peers = GNUNET_CONTAINER_multihashmap_create (32);
8018 applications = GNUNET_CONTAINER_multihashmap_create (32);
8019 types = GNUNET_CONTAINER_multihashmap_create (32);
8021 dht_handle = GNUNET_DHT_connect (c, 64);
8022 if (NULL == dht_handle)
8026 stats = GNUNET_STATISTICS_create ("mesh", c);
8028 GNUNET_SERVER_suspend (server_handle);
8029 /* Scheduled the task to clean up when shutdown is called */
8030 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8032 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8033 GNUNET_free (keyfile);
8038 * The main function for the mesh service.
8040 * @param argc number of arguments from the command line
8041 * @param argv command line arguments
8042 * @return 0 ok, 1 on error
8045 main (int argc, char *const *argv)
8049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8052 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8058 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8059 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8060 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);