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 * Iterator over edges in a regex block retrieved from the DHT.
1145 * @param cls Closure.
1146 * @param token Token that follows to next state.
1147 * @param len Lenght of token.
1148 * @param key Hash of next state.
1150 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1153 regex_edge_iterator (void *cls,
1156 const struct GNUNET_HashCode *key);
1160 * Find a path to a peer that offers a regex servcie compatible
1161 * with a given string.
1163 * @param key The key of the accepting state.
1164 * @param ctx Context containing info about the string, tunnel, etc.
1167 regex_find_path (const struct GNUNET_HashCode *key,
1168 struct MeshRegexSearchContext *ctx);
1172 * Queue and pass message to core when possible.
1174 * @param cls Closure (type dependant).
1175 * @param type Type of the message, 0 for a raw message.
1176 * @param size Size of the message.
1177 * @param dst Neighbor to send message to.
1178 * @param t Tunnel this message belongs to.
1181 queue_add (void *cls, uint16_t type, size_t size,
1182 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1186 * Free a transmission that was already queued with all resources
1187 * associated to the request.
1189 * @param queue Queue handler to cancel.
1190 * @param clear_cls Is it necessary to free associated cls?
1193 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1197 * @brief Get the next transmittable message from the queue.
1199 * This will be the head, except in the case of being a data packet
1200 * not allowed by the destination peer.
1202 * @param peer Destination peer.
1204 * @return The next viable MeshPeerQueue element to send to that peer.
1205 * NULL when there are no transmittable messages.
1207 struct MeshPeerQueue *
1208 queue_get_next (const struct MeshPeerInfo *peer);
1212 * Core callback to write a queued packet to core buffer
1214 * @param cls Closure (peer info).
1215 * @param size Number of bytes available in buf.
1216 * @param buf Where the to write the message.
1218 * @return number of bytes written to buf
1221 queue_send (void *cls, size_t size, void *buf);
1223 /******************************************************************************/
1224 /************************ ITERATORS ****************************/
1225 /******************************************************************************/
1228 * Iterator over found existing mesh regex blocks that match an ongoing search.
1230 * @param cls closure
1231 * @param key current key code
1232 * @param value value in the hash map
1233 * @return GNUNET_YES if we should continue to iterate,
1237 regex_result_iterator (void *cls,
1238 const struct GNUNET_HashCode * key,
1241 struct MeshRegexBlock *block = value;
1242 struct MeshRegexSearchContext *ctx = cls;
1244 if (GNUNET_YES == ntohl(block->accepting) &&
1245 ctx->position == strlen (ctx->info->description))
1247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1248 regex_find_path (key, ctx);
1249 return GNUNET_YES; // We found an accept state!
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1254 ctx->position, strlen(ctx->info->description),
1255 ntohl(block->accepting));
1258 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1259 ®ex_edge_iterator, ctx);
1266 * Iterator over edges in a regex block retrieved from the DHT.
1268 * @param cls Closure (context of the search).
1269 * @param token Token that follows to next state.
1270 * @param len Lenght of token.
1271 * @param key Hash of next state.
1273 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1276 regex_edge_iterator (void *cls,
1279 const struct GNUNET_HashCode *key)
1281 struct MeshRegexSearchContext *ctx = cls;
1282 struct MeshRegexSearchInfo *info = ctx->info;
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1289 current = &info->description[ctx->position];
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1291 current_len = strlen (info->description) - ctx->position;
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1296 if (len > current_len)
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1299 return GNUNET_YES; // Token too long, wont match
1301 if (0 != strncmp (current, token, len))
1303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1304 return GNUNET_YES; // Token doesn't match
1307 if (len > ctx->longest_match)
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1310 ctx->longest_match = len;
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1320 * Jump to the next edge, with the longest matching token.
1322 * @param block Block found in the DHT.
1323 * @param size Size of the block.
1324 * @param ctx Context of the search.
1326 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1329 regex_next_edge (const struct MeshRegexBlock *block,
1331 struct MeshRegexSearchContext *ctx)
1333 struct MeshRegexSearchContext *new_ctx;
1334 struct MeshRegexSearchInfo *info = ctx->info;
1335 struct GNUNET_DHT_GetHandle *get_h;
1337 GNUNET_break (GNUNET_OK ==
1338 GNUNET_MESH_regex_block_iterate (block, size,
1339 ®ex_edge_iterator, ctx));
1341 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1342 new_ctx->info = info;
1343 new_ctx->position = ctx->position + ctx->longest_match;
1344 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1346 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1349 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1351 ®ex_result_iterator,
1353 return GNUNET_YES; // We are already looking for it
1355 /* Start search in DHT */
1357 GNUNET_DHT_get_start (dht_handle, /* handle */
1358 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1359 &ctx->hash, /* key to search */
1360 dht_replication_level, /* replication level */
1361 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1362 NULL, /* xquery */ // FIXME BLOOMFILTER
1363 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1364 &dht_get_string_handler, new_ctx);
1366 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1380 * Iterator over hash map entries to cancel DHT GET requests after a
1381 * successful connect_by_string.
1383 * @param cls Closure (unused).
1384 * @param key Current key code (unused).
1385 * @param value Value in the hash map (get handle).
1386 * @return GNUNET_YES if we should continue to iterate,
1390 regex_cancel_dht_get (void *cls,
1391 const struct GNUNET_HashCode * key,
1394 struct GNUNET_DHT_GetHandle *h = value;
1396 GNUNET_DHT_get_stop (h);
1402 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1403 * search for connect_by_string.
1405 * @param cls Closure (unused).
1406 * @param key Current key code (unused).
1407 * @param value MeshRegexBlock in the hash map.
1408 * @return GNUNET_YES if we should continue to iterate,
1412 regex_free_result (void *cls,
1413 const struct GNUNET_HashCode * key,
1417 GNUNET_free (value);
1423 * Regex callback iterator to store own service description in the DHT.
1425 * @param cls closure.
1426 * @param key hash for current state.
1427 * @param proof proof for current state.
1428 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1429 * @param num_edges number of edges leaving current state.
1430 * @param edges edges leaving current state.
1433 regex_iterator (void *cls,
1434 const struct GNUNET_HashCode *key,
1437 unsigned int num_edges,
1438 const struct GNUNET_REGEX_Edge *edges)
1440 struct MeshRegexBlock *block;
1441 struct MeshRegexEdge *block_edge;
1442 enum GNUNET_DHT_RouteOption opt;
1446 unsigned int offset;
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 " regex dht put for state %s\n",
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1460 if (GNUNET_YES == accepting)
1462 struct MeshRegexAccept block;
1464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1465 " state %s is accepting, putting own id\n",
1467 size = sizeof (block);
1469 block.id = my_full_id;
1471 GNUNET_DHT_put(dht_handle, key,
1472 dht_replication_level,
1473 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1474 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1477 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1482 len = strlen(proof);
1483 size = sizeof (struct MeshRegexBlock) + len;
1484 block = GNUNET_malloc (size);
1487 block->n_proof = htonl (len);
1488 block->n_edges = htonl (num_edges);
1489 block->accepting = htonl (accepting);
1491 /* Store the proof at the end of the block. */
1492 aux = (char *) &block[1];
1493 memcpy (aux, proof, len);
1496 /* Store each edge in a variable length MeshEdge struct at the
1497 * very end of the MeshRegexBlock structure.
1499 for (i = 0; i < num_edges; i++)
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1502 " edge %s towards %s\n",
1504 GNUNET_h2s(&edges[i].destination));
1506 /* aux points at the end of the last block */
1507 len = strlen (edges[i].label);
1508 size += sizeof (struct MeshRegexEdge) + len;
1509 // Calculate offset FIXME is this ok? use size instead?
1510 offset = aux - (char *) block;
1511 block = GNUNET_realloc (block, size);
1512 aux = &((char *) block)[offset];
1513 block_edge = (struct MeshRegexEdge *) aux;
1514 block_edge->key = edges[i].destination;
1515 block_edge->n_token = htonl (len);
1516 aux = (char *) &block_edge[1];
1517 memcpy (aux, edges[i].label, len);
1521 GNUNET_DHT_put(dht_handle, key,
1522 dht_replication_level,
1524 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1526 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1530 GNUNET_free (block);
1535 * Store the regular expression describing a local service into the DHT.
1537 * @param regex The regular expresion.
1540 regex_put (const char *regex)
1542 struct GNUNET_REGEX_Automaton *dfa;
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1545 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1546 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1547 GNUNET_REGEX_automaton_destroy (dfa);
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1553 * Find a path to a peer that offers a regex servcie compatible
1554 * with a given string.
1556 * @param key The key of the accepting state.
1557 * @param ctx Context containing info about the string, tunnel, etc.
1560 regex_find_path (const struct GNUNET_HashCode *key,
1561 struct MeshRegexSearchContext *ctx)
1563 struct GNUNET_DHT_GetHandle *get_h;
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1566 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1567 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1568 key, /* key to search */
1569 dht_replication_level, /* replication level */
1570 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1571 GNUNET_DHT_RO_RECORD_ROUTE,
1572 NULL, /* xquery */ // FIXME BLOOMFILTER
1573 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1574 &dht_get_string_accept_handler, ctx);
1575 GNUNET_break (GNUNET_OK ==
1576 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1584 * Function called if the connect attempt to a peer found via
1585 * connect_by_string times out. Try to connect to another peer, if any.
1586 * Otherwise try to reconnect to the same peer.
1588 * @param cls Closure (info about regex search).
1589 * @param tc TaskContext.
1592 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1594 struct MeshRegexSearchInfo *info = cls;
1595 struct MeshPeerInfo *peer_info;
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1600 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1601 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1609 if (0 < info->n_peers)
1611 // Select next peer, put current in that spot.
1612 id = info->peers[info->i_peer];
1613 info->peers[info->i_peer] = info->peer;
1614 info->i_peer = (info->i_peer + 1) % info->n_peers;
1618 // Try to connect to same peer again.
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1623 peer_info = peer_info_get_short(id);
1624 tunnel_add_peer (info->t, peer_info);
1626 tunnel_delete_peer (info->t, old);
1627 peer_info_connect (peer_info, info->t);
1628 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1629 ®ex_connect_timeout,
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1636 * Cancel an ongoing regex search in the DHT and free all resources.
1638 * @param ctx The search context.
1641 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1643 struct MeshRegexSearchInfo *info = ctx->info;
1646 GNUNET_free (info->description);
1647 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1648 ®ex_cancel_dht_get, NULL);
1649 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1650 ®ex_free_result, NULL);
1651 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1652 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1653 info->t->regex_ctx = NULL;
1654 for (i = 0; i < info->n_contexts; i++)
1656 GNUNET_free (info->contexts[i]);
1658 if (0 < info->n_contexts)
1659 GNUNET_free (info->contexts);
1660 if (0 < info->n_peers)
1661 GNUNET_free (info->peers);
1662 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1664 GNUNET_SCHEDULER_cancel(info->timeout);
1670 /******************************************************************************/
1671 /************************ PERIODIC FUNCTIONS ****************************/
1672 /******************************************************************************/
1675 * Announce iterator over for each application provided by the peer
1677 * @param cls closure
1678 * @param key current key code
1679 * @param value value in the hash map
1680 * @return GNUNET_YES if we should continue to
1685 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1687 struct PBlock block;
1688 struct MeshClient *c;
1690 block.id = my_full_id;
1691 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1692 GNUNET_assert(NULL != c);
1693 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1694 if (0 == block.type)
1699 block.type = htonl (block.type);
1701 GNUNET_break (NULL !=
1702 GNUNET_DHT_put (dht_handle, key,
1703 dht_replication_level,
1704 GNUNET_DHT_RO_RECORD_ROUTE |
1705 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1706 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1708 (const char *) &block,
1709 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1711 app_announce_time, NULL, NULL));
1717 * Periodically announce what applications are provided by local clients
1720 * @param cls closure
1721 * @param tc task context
1724 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1726 struct MeshClient *c = cls;
1729 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1731 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1735 DEBUG_DHT ("Starting PUT for regex\n");
1737 for (i = 0; i < c->n_regex; i++)
1739 regex_put (c->regexes[i]);
1741 c->regex_announce_task =
1742 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1743 DEBUG_DHT ("Finished PUT for regex\n");
1750 * Periodically announce what applications are provided by local clients
1753 * @param cls closure
1754 * @param tc task context
1757 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1759 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1761 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1765 DEBUG_DHT ("Starting PUT for apps\n");
1767 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1769 announce_applications_task =
1770 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1772 DEBUG_DHT ("Finished PUT for apps\n");
1779 * Periodically announce self id in the DHT
1781 * @param cls closure
1782 * @param tc task context
1785 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1787 struct PBlock block;
1789 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1791 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1795 * - Set data expiration in function of X
1796 * - Adapt X to churn
1798 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1800 block.id = my_full_id;
1801 block.type = htonl (0);
1802 GNUNET_DHT_put (dht_handle, /* DHT handle */
1803 &my_full_id.hashPubKey, /* Key to use */
1804 dht_replication_level, /* Replication level */
1805 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1806 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1807 sizeof (block), /* Size of the data */
1808 (const char *) &block, /* Data itself */
1809 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1810 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1811 NULL, /* Continuation */
1812 NULL); /* Continuation closure */
1814 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1818 /******************************************************************************/
1819 /****************** GENERAL HELPER FUNCTIONS ************************/
1820 /******************************************************************************/
1823 * Decrements the reference counter and frees all resources if needed
1825 * @param mesh_data Data Descriptor used in a multicast message.
1826 * Freed no longer needed (last message).
1829 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1831 if (0 == --(mesh_data->reference_counter))
1833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1834 GNUNET_free (mesh_data->data);
1835 GNUNET_free (mesh_data);
1841 * Check if client has registered with the service and has not disconnected
1843 * @param client the client to check
1845 * @return non-NULL if client exists in the global DLL
1847 static struct MeshClient *
1848 client_get (struct GNUNET_SERVER_Client *client)
1850 struct MeshClient *c;
1855 if (c->handle == client)
1864 * Checks if a given client has subscribed to certain message type
1866 * @param message_type Type of message to check
1867 * @param c Client to check
1869 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1871 * FIXME: use of crypto_hash slows it down
1872 * The hash function alone takes 8-10us out of the ~55us for the whole
1873 * process of retransmitting the message from one local client to another.
1874 * Find faster implementation!
1877 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1879 struct GNUNET_HashCode hc;
1881 if (NULL == c->types)
1884 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1885 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1890 * Check whether client wants traffic from a tunnel.
1892 * @param c Client to check.
1893 * @param t Tunnel to be found.
1895 * @return GNUNET_YES if client knows tunnel.
1897 * TODO look in client hashmap
1900 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1904 for (i = 0; i < t->nclients; i++)
1905 if (t->clients[i] == c)
1912 * Check whether client has been informed about a tunnel.
1914 * @param c Client to check.
1915 * @param t Tunnel to be found.
1917 * @return GNUNET_YES if client knows tunnel.
1919 * TODO look in client hashmap
1922 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1926 for (i = 0; i < t->nignore; i++)
1927 if (t->ignore[i] == c)
1929 return client_wants_tunnel(c, t);
1934 * Marks a client as uninterested in traffic from the tunnel, updating both
1935 * client and tunnel to reflect this.
1937 * @param c Client that doesn't want traffic anymore.
1938 * @param t Tunnel which should be ignored.
1940 * FIXME when to delete an incoming tunnel?
1943 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1945 struct GNUNET_HashCode hash;
1947 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1948 GNUNET_break (GNUNET_YES ==
1949 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1951 GNUNET_break (GNUNET_YES ==
1952 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1953 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1954 tunnel_delete_active_client (t, c);
1955 GNUNET_array_append (t->ignore, t->nignore, c);
1960 * Deletes a tunnel from a client (either owner or destination). To be used on
1961 * tunnel destroy, otherwise, use client_ignore_tunnel.
1963 * @param c Client whose tunnel to delete.
1964 * @param t Tunnel which should be deleted.
1967 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1969 struct GNUNET_HashCode hash;
1973 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1974 GNUNET_assert (GNUNET_YES ==
1975 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1981 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1983 GNUNET_assert (GNUNET_YES ==
1984 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1988 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1996 * Send the message to all clients that have subscribed to its type
1998 * @param msg Pointer to the message itself
1999 * @param payload Pointer to the payload of the message.
2000 * @param t The tunnel to whose clients this message goes.
2002 * @return number of clients this message was sent to
2005 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2006 const struct GNUNET_MessageHeader *payload,
2007 struct MeshTunnel *t)
2009 struct MeshClient *c;
2010 MESH_TunnelNumber *tid;
2013 char cbuf[htons (msg->size)];
2015 type = ntohs (payload->type);
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2018 GNUNET_MESH_DEBUG_M2S (type));
2020 memcpy (cbuf, msg, sizeof (cbuf));
2021 switch (htons (msg->type))
2023 struct GNUNET_MESH_Unicast *uc;
2024 struct GNUNET_MESH_Multicast *mc;
2025 struct GNUNET_MESH_ToOrigin *to;
2027 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2028 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2031 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2032 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2035 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2036 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2044 for (count = 0, c = clients; c != NULL; c = c->next)
2046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2047 if (client_is_subscribed (type, c))
2049 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2053 *tid = htonl (t->local_tid);
2057 if (GNUNET_NO == client_knows_tunnel (c, t))
2059 /* This client doesn't know the tunnel */
2060 struct GNUNET_MESH_TunnelNotification tmsg;
2061 struct GNUNET_HashCode hash;
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2064 tmsg.header.size = htons (sizeof (tmsg));
2065 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2066 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2067 tmsg.tunnel_id = htonl (t->local_tid_dest);
2068 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2069 &tmsg.header, GNUNET_NO);
2070 tunnel_add_client (t, c);
2071 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2073 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2074 c->incoming_tunnels, &hash, t,
2075 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2077 *tid = htonl (t->local_tid_dest);
2080 /* Check if the client wants to get traffic from the tunnel */
2081 if (GNUNET_NO == client_wants_tunnel(c, t))
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2085 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2086 (struct GNUNET_MessageHeader
2087 *) cbuf, GNUNET_NO);
2096 * Notify the client that owns the tunnel that a peer has connected to it
2097 * (the requested path to it has been confirmed).
2099 * @param t Tunnel whose owner to notify
2100 * @param id Short id of the peer that has connected
2103 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2105 struct GNUNET_MESH_PeerControl pc;
2107 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2108 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2109 pc.tunnel_id = htonl (t->local_tid);
2110 GNUNET_PEER_resolve (id, &pc.peer);
2111 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2117 * Notify all clients (not depending on registration status) that the incoming
2118 * tunnel is no longer valid.
2120 * @param t Tunnel that was destroyed.
2123 send_clients_tunnel_destroy (struct MeshTunnel *t)
2125 struct GNUNET_MESH_TunnelMessage msg;
2127 msg.header.size = htons (sizeof (msg));
2128 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2129 msg.tunnel_id = htonl (t->local_tid_dest);
2130 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2135 * Notify clients of tunnel disconnections, if needed.
2136 * In case the origin disconnects, the destination clients get a tunnel destroy
2137 * notification. If the last destination disconnects (only one remaining client
2138 * in tunnel), the origin gets a (local ID) peer disconnected.
2139 * Note that the function must be called BEFORE removing the client from
2142 * @param t Tunnel that was destroyed.
2143 * @param c Client that disconnected.
2146 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2152 struct GNUNET_MESH_TunnelMessage msg;
2154 msg.header.size = htons (sizeof (msg));
2155 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2156 msg.tunnel_id = htonl (t->local_tid_dest);
2157 for (i = 0; i < t->nclients; i++)
2158 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2159 &msg.header, GNUNET_NO);
2161 // FIXME when to disconnect an incoming tunnel?
2162 else if (1 == t->nclients && NULL != t->owner)
2164 struct GNUNET_MESH_PeerControl msg;
2166 msg.header.size = htons (sizeof (msg));
2167 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2168 msg.tunnel_id = htonl (t->local_tid);
2169 msg.peer = my_full_id;
2170 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2171 &msg.header, GNUNET_NO);
2177 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2178 * and insert it in the appropiate structures if the peer is not known yet.
2180 * @param peer Full identity of the peer.
2182 * @return Existing or newly created peer info.
2184 static struct MeshPeerInfo *
2185 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2187 struct MeshPeerInfo *peer_info;
2189 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2190 if (NULL == peer_info)
2193 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2194 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2195 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2196 peer_info->id = GNUNET_PEER_intern (peer);
2204 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2205 * and insert it in the appropiate structures if the peer is not known yet.
2207 * @param peer Short identity of the peer.
2209 * @return Existing or newly created peer info.
2211 static struct MeshPeerInfo *
2212 peer_info_get_short (const GNUNET_PEER_Id peer)
2214 struct GNUNET_PeerIdentity id;
2216 GNUNET_PEER_resolve (peer, &id);
2217 return peer_info_get (&id);
2222 * Iterator to remove the tunnel from the list of tunnels a peer participates
2225 * @param cls Closure (tunnel info)
2226 * @param key GNUNET_PeerIdentity of the peer (unused)
2227 * @param value PeerInfo of the peer
2229 * @return always GNUNET_YES, to keep iterating
2232 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2234 struct MeshTunnel *t = cls;
2235 struct MeshPeerInfo *peer = value;
2238 for (i = 0; i < peer->ntunnels; i++)
2241 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2244 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2245 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2254 * Core callback to write a pre-constructed data packet to core buffer
2256 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2257 * @param size Number of bytes available in buf.
2258 * @param buf Where the to write the message.
2260 * @return number of bytes written to buf
2263 send_core_data_raw (void *cls, size_t size, void *buf)
2265 struct MeshTransmissionDescriptor *info = cls;
2266 struct GNUNET_MessageHeader *msg;
2269 GNUNET_assert (NULL != info);
2270 GNUNET_assert (NULL != info->mesh_data);
2271 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2272 total_size = ntohs (msg->size);
2274 if (total_size > size)
2279 memcpy (buf, msg, total_size);
2280 data_descriptor_decrement_rc (info->mesh_data);
2287 * Sends an already built non-multicast message to a peer,
2288 * properly registrating all used resources.
2290 * @param message Message to send. Function makes a copy of it.
2291 * @param peer Short ID of the neighbor whom to send the message.
2292 * @param t Tunnel on which this message is transmitted.
2295 send_message (const struct GNUNET_MessageHeader *message,
2296 const struct GNUNET_PeerIdentity *peer,
2297 struct MeshTunnel *t)
2299 struct MeshTransmissionDescriptor *info;
2300 struct MeshPeerInfo *neighbor;
2301 struct MeshPeerPath *p;
2305 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2307 size = ntohs (message->size);
2308 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2309 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2310 info->mesh_data->data = GNUNET_malloc (size);
2311 memcpy (info->mesh_data->data, message, size);
2312 type = ntohs(message->type);
2313 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2315 struct GNUNET_MESH_Unicast *m;
2317 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2318 m->ttl = htonl (ntohl (m->ttl) - 1);
2320 info->mesh_data->data_len = size;
2321 info->mesh_data->reference_counter = 1;
2322 info->mesh_data->total_out = 1;
2323 neighbor = peer_info_get (peer);
2324 for (p = neighbor->path_head; NULL != p; p = p->next)
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 " %s IS NOT DIRECTLY CONNECTED\n",
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2340 for (p = neighbor->path_head; NULL != p; p = p->next)
2342 struct GNUNET_PeerIdentity debug_id;
2345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2346 " path with %u hops through:\n",
2348 for (i = 0; i < p->length; i++)
2350 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353 i, GNUNET_i2s(&debug_id));
2357 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2358 GNUNET_free (info->mesh_data->data);
2359 GNUNET_free (info->mesh_data);
2363 info->peer = neighbor;
2364 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2367 * TODO: in this case we only need the service to retransmit
2368 * the message down the path. If we pass the real type to queue_add,
2369 * queue_send will try to build the message from scratch. This can
2370 * probably be done by some other way instead of deleteing the type
2384 * Sends a CREATE PATH message for a path to a peer, properly registrating
2385 * all used resources.
2387 * @param peer PeerInfo of the final peer for whom this path is being created.
2388 * @param p Path itself.
2389 * @param t Tunnel for which the path is created.
2392 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2393 struct MeshTunnel *t)
2395 struct GNUNET_PeerIdentity id;
2396 struct MeshPathInfo *path_info;
2397 struct MeshPeerInfo *neighbor;
2403 p = tree_get_path_to_peer (t->tree, peer->id);
2410 for (i = 0; i < p->length; i++)
2412 if (p->peers[i] == myid)
2415 if (i >= p->length - 1)
2421 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2423 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2424 path_info->path = p;
2426 neighbor = peer_info_get (&id);
2427 path_info->peer = neighbor;
2428 queue_add (path_info,
2429 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2430 sizeof (struct GNUNET_MESH_ManipulatePath) +
2431 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2438 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2440 * @param t Tunnel whose path to destroy.
2441 * @param destination Short ID of the peer to whom the path to destroy.
2444 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2446 struct MeshPeerPath *p;
2449 p = tree_get_path_to_peer (t->tree, destination);
2455 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2456 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2458 struct GNUNET_MESH_ManipulatePath *msg;
2459 struct GNUNET_PeerIdentity *pi;
2463 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2464 msg->header.size = htons (size);
2465 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2466 msg->tid = htonl (t->id.tid);
2467 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2468 for (i = 0; i < p->length; i++)
2470 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2472 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2479 * Try to establish a new connection to this peer.
2480 * Use the best path for the given tunnel.
2481 * If the peer doesn't have any path to it yet, try to get one.
2482 * If the peer already has some path, send a CREATE PATH towards it.
2484 * @param peer PeerInfo of the peer.
2485 * @param t Tunnel for which to create the path, if possible.
2488 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2490 struct MeshPeerPath *p;
2491 struct MeshPathInfo *path_info;
2493 if (NULL != peer->path_head)
2495 p = tree_get_path_to_peer (t->tree, peer->id);
2502 // FIXME always send create path to self
2505 send_create_path (peer, p, t);
2509 struct GNUNET_HashCode hash;
2512 send_client_peer_connected (t, myid);
2513 t->local_tid_dest = next_local_tid++;
2514 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2517 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2518 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2525 else if (NULL == peer->dhtget)
2527 struct GNUNET_PeerIdentity id;
2529 GNUNET_PEER_resolve (peer->id, &id);
2530 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2531 path_info->peer = peer;
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2535 peer->dhtgetcls = path_info;
2536 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2537 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2538 &id.hashPubKey, /* key to search */
2539 dht_replication_level, /* replication level */
2540 GNUNET_DHT_RO_RECORD_ROUTE |
2541 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2542 NULL, /* xquery */ // FIXME BLOOMFILTER
2543 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2544 &dht_get_id_handler, path_info);
2546 /* Otherwise, there is no path but the DHT get is already started. */
2551 * Task to delay the connection of a peer
2553 * @param cls Closure (path info with tunnel and peer to connect).
2554 * Will be free'd on exection.
2555 * @param tc TaskContext
2558 peer_info_connect_task (void *cls,
2559 const struct GNUNET_SCHEDULER_TaskContext *tc)
2561 struct MeshPathInfo *path_info = cls;
2563 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2568 peer_info_connect (path_info->peer, path_info->t);
2574 * Destroy the peer_info and free any allocated resources linked to it
2576 * @param pi The peer_info to destroy.
2578 * @return GNUNET_OK on success
2581 peer_info_destroy (struct MeshPeerInfo *pi)
2583 struct GNUNET_PeerIdentity id;
2584 struct MeshPeerPath *p;
2585 struct MeshPeerPath *nextp;
2587 GNUNET_PEER_resolve (pi->id, &id);
2588 GNUNET_PEER_change_rc (pi->id, -1);
2591 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2595 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2597 if (NULL != pi->dhtget)
2599 GNUNET_DHT_get_stop (pi->dhtget);
2600 GNUNET_free (pi->dhtgetcls);
2606 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2616 * Remove all paths that rely on a direct connection between p1 and p2
2617 * from the peer itself and notify all tunnels about it.
2619 * @param peer PeerInfo of affected peer.
2620 * @param p1 GNUNET_PEER_Id of one peer.
2621 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2624 * TODO: optimize (see below)
2627 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2630 struct MeshPeerPath *p;
2631 struct MeshPeerPath *aux;
2632 struct MeshPeerInfo *peer_d;
2634 unsigned int destroyed;
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2641 p = peer->path_head;
2645 for (i = 0; i < (p->length - 1); i++)
2647 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2648 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2650 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2661 for (i = 0; i < peer->ntunnels; i++)
2663 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2667 * Problem: one or more peers have been deleted from the tunnel tree.
2668 * We don't know who they are to try to add them again.
2669 * We need to try to find a new path for each of the disconnected peers.
2670 * Some of them might already have a path to reach them that does not
2671 * involve p1 and p2. Adding all anew might render in a better tree than
2672 * the trivial immediate fix.
2674 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2675 * its children will be reachable trough him.
2677 peer_d = peer_info_get_short (d);
2680 for (p = peer_d->path_head; NULL != p; p = p->next)
2682 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2690 /* No callback, as peer will be already disconnected and a connection
2691 * scheduled by tunnel_notify_connection_broken.
2693 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2697 peer_info_connect (peer_d, peer->tunnels[i]);
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2705 * Add the path to the peer and update the path used to reach it in case this
2708 * @param peer_info Destination peer to add the path to.
2709 * @param path New path to add. Last peer must be the peer in arg 1.
2710 * Path will be either used of freed if already known.
2711 * @param trusted Do we trust that this path is real?
2714 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2717 struct MeshPeerPath *aux;
2721 if ((NULL == peer_info) || (NULL == path))
2724 path_destroy (path);
2727 if (path->peers[path->length - 1] != peer_info->id)
2730 path_destroy (path);
2733 if (path->length <= 2 && GNUNET_NO == trusted)
2735 /* Only allow CORE to tell us about direct paths */
2736 path_destroy (path);
2739 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2740 for (l = 1; l < path->length; l++)
2742 if (path->peers[l] == myid)
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2745 for (l2 = 0; l2 < path->length - l; l2++)
2747 path->peers[l2] = path->peers[l + l2];
2752 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2757 struct GNUNET_PeerIdentity id;
2759 GNUNET_PEER_resolve (peer_info->id, &id);
2760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2761 path->length, GNUNET_i2s (&id));
2764 l = path_get_length (path);
2771 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2772 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2774 l2 = path_get_length (aux);
2777 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2778 peer_info->path_tail, aux, path);
2783 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2785 path_destroy (path);
2790 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2797 * Add the path to the origin peer and update the path used to reach it in case
2798 * this is the shortest.
2799 * The path is given in peer_info -> destination, therefore we turn the path
2800 * upside down first.
2802 * @param peer_info Peer to add the path to, being the origin of the path.
2803 * @param path New path to add after being inversed.
2804 * @param trusted Do we trust that this path is real?
2807 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2808 struct MeshPeerPath *path, int trusted)
2811 peer_info_add_path (peer_info, path, trusted);
2816 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2817 * to obtain a local peer -> destination path and interning the peer ids.
2819 * @return Newly allocated and created path
2821 static struct MeshPeerPath *
2822 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2823 unsigned int get_path_length,
2824 const struct GNUNET_PeerIdentity *put_path,
2825 unsigned int put_path_length)
2827 struct MeshPeerPath *p;
2833 GNUNET_PEER_change_rc (myid, 1);
2834 i = get_path_length;
2835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2836 for (i--; i >= 0; i--)
2838 id = GNUNET_PEER_intern (&get_path[i]);
2839 if (p->length > 0 && id == p->peers[p->length - 1])
2841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2842 GNUNET_PEER_change_rc (id, -1);
2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2847 GNUNET_i2s (&get_path[i]));
2849 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2850 p->peers[p->length - 1] = id;
2853 i = put_path_length;
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2855 for (i--; i >= 0; i--)
2857 id = GNUNET_PEER_intern (&put_path[i]);
2860 /* PUT path went through us, so discard the path up until now and start
2861 * from here to get a much shorter (and loop-free) path.
2866 if (p->length > 0 && id == p->peers[p->length - 1])
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2869 GNUNET_PEER_change_rc (id, -1);
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2874 GNUNET_i2s (&put_path[i]));
2876 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2877 p->peers[p->length - 1] = id;
2881 if (get_path_length > 0)
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2883 GNUNET_i2s (&get_path[0]));
2884 if (put_path_length > 0)
2885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2886 GNUNET_i2s (&put_path[0]));
2887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2889 for (i = 0; i < p->length; i++)
2891 struct GNUNET_PeerIdentity peer_id;
2893 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2895 GNUNET_i2s (&peer_id));
2903 * Adds a path to the peer_infos of all the peers in the path
2905 * @param p Path to process.
2906 * @param confirmed Whether we know if the path works or not.
2909 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2913 /* TODO: invert and add */
2914 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2915 for (i++; i < p->length; i++)
2917 struct MeshPeerInfo *aux;
2918 struct MeshPeerPath *copy;
2920 aux = peer_info_get_short (p->peers[i]);
2921 copy = path_duplicate (p);
2922 copy->length = i + 1;
2923 peer_info_add_path (aux, copy, GNUNET_NO);
2929 * Send keepalive packets for a peer
2931 * @param cls Closure (tunnel for which to send the keepalive).
2932 * @param tc Notification context.
2934 * TODO: implement explicit multicast keepalive?
2937 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2941 * Search for a tunnel among the incoming tunnels
2943 * @param tid the local id of the tunnel
2945 * @return tunnel handler, NULL if doesn't exist
2947 static struct MeshTunnel *
2948 tunnel_get_incoming (MESH_TunnelNumber tid)
2950 struct GNUNET_HashCode hash;
2952 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2953 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2954 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2959 * Search for a tunnel among the tunnels for a client
2961 * @param c the client whose tunnels to search in
2962 * @param tid the local id of the tunnel
2964 * @return tunnel handler, NULL if doesn't exist
2966 static struct MeshTunnel *
2967 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2969 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2971 return tunnel_get_incoming (tid);
2975 struct GNUNET_HashCode hash;
2977 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2978 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2984 * Search for a tunnel by global ID using PEER_ID
2986 * @param pi owner of the tunnel
2987 * @param tid global tunnel number
2989 * @return tunnel handler, NULL if doesn't exist
2991 static struct MeshTunnel *
2992 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2994 struct MESH_TunnelID id;
2995 struct GNUNET_HashCode hash;
3000 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3001 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3006 * Search for a tunnel by global ID using full PeerIdentities
3008 * @param oid owner of the tunnel
3009 * @param tid global tunnel number
3011 * @return tunnel handler, NULL if doesn't exist
3013 static struct MeshTunnel *
3014 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3016 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3021 * Delete an active client from the tunnel.
3027 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3031 for (i = 0; i < t->nclients; i++)
3033 if (t->clients[i] == c)
3035 t->clients[i] = t->clients[t->nclients - 1];
3036 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3037 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3039 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3047 * Delete an ignored client from the tunnel.
3053 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3057 for (i = 0; i < t->nignore; i++)
3059 if (t->ignore[i] == c)
3061 t->ignore[i] = t->ignore[t->nignore - 1];
3062 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3070 * Delete a client from the tunnel. It should be only done on
3071 * client disconnection, otherwise use client_ignore_tunnel.
3077 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3079 tunnel_delete_ignored_client (t, c);
3080 tunnel_delete_active_client (t, c);
3085 * Iterator to free MeshTunnelChildInfo of tunnel children.
3087 * @param cls Closure (tunnel info).
3088 * @param key Hash of GNUNET_PEER_Id (unused).
3089 * @param value MeshTunnelChildInfo of the child.
3091 * @return always GNUNET_YES, to keep iterating
3094 tunnel_destroy_child (void *cls,
3095 const struct GNUNET_HashCode * key,
3098 struct MeshTunnelChildInfo *cinfo = value;
3099 struct MeshTunnel *t = cls;
3103 for (c = 0; c < cinfo->send_buffer_n; c++)
3105 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3106 if (NULL != cinfo->send_buffer[i])
3107 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3110 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3112 GNUNET_free_non_null (cinfo->send_buffer);
3113 GNUNET_free (cinfo);
3119 * Callback used to notify a client owner of a tunnel that a peer has
3120 * disconnected, most likely because of a path change.
3122 * @param cls Closure (tunnel this notification is about).
3123 * @param peer_id Short ID of disconnected peer.
3126 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3128 struct MeshTunnel *t = cls;
3129 struct MeshPeerInfo *peer;
3130 struct MeshPathInfo *path_info;
3132 if (NULL != t->owner && NULL != nc)
3134 struct GNUNET_MESH_PeerControl msg;
3136 msg.header.size = htons (sizeof (msg));
3137 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3138 msg.tunnel_id = htonl (t->local_tid);
3139 GNUNET_PEER_resolve (peer_id, &msg.peer);
3140 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3141 &msg.header, GNUNET_NO);
3143 peer = peer_info_get_short (peer_id);
3144 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3145 path_info->peer = peer;
3147 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3152 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3153 * needed rescources.
3154 * If peer already exists, reevaluate shortest path and change if different.
3156 * @param t Tunnel we want to add a new peer to
3157 * @param peer PeerInfo of the peer being added
3161 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3163 struct GNUNET_PeerIdentity id;
3164 struct MeshPeerPath *best_p;
3165 struct MeshPeerPath *p;
3166 unsigned int best_cost;
3169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3170 GNUNET_PEER_resolve (peer->id, &id);
3172 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3175 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3176 GNUNET_assert (GNUNET_OK ==
3177 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3179 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3182 if (NULL != (p = peer->path_head))
3185 best_cost = tree_get_path_cost (t->tree, p);
3188 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3195 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3196 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3197 t->path_refresh_task =
3198 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3202 /* Start a DHT get */
3203 peer_info_connect (peer, t);
3205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3209 * Add a path to a tunnel which we don't own, just to remember the next hop.
3210 * If destination node was already in the tunnel, the first hop information
3211 * will be replaced with the new path.
3213 * @param t Tunnel we want to add a new peer to
3214 * @param p Path to add
3215 * @param own_pos Position of local node in path.
3219 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3220 unsigned int own_pos)
3222 struct GNUNET_PeerIdentity id;
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3225 GNUNET_assert (0 != own_pos);
3226 tree_add_path (t->tree, p, NULL, NULL);
3227 if (own_pos < p->length - 1)
3229 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3230 tree_update_first_hops (t->tree, myid, &id);
3232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3236 * Add a client to a tunnel, initializing all needed data structures.
3238 * @param t Tunnel to which add the client.
3239 * @param c Client which to add to the tunnel.
3242 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3244 struct MeshTunnelClientInfo clinfo;
3246 GNUNET_array_append (t->clients, t->nclients, c);
3247 clinfo.fwd_ack = t->fwd_pid + 1;
3248 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3249 clinfo.fwd_pid = t->fwd_pid;
3250 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3252 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3257 * Notifies a tunnel that a connection has broken that affects at least
3258 * some of its peers. Sends a notification towards the root of the tree.
3259 * In case the peer is the owner of the tree, notifies the client that owns
3260 * the tunnel and tries to reconnect.
3262 * @param t Tunnel affected.
3263 * @param p1 Peer that got disconnected from p2.
3264 * @param p2 Peer that got disconnected from p1.
3266 * @return Short ID of the peer disconnected (either p1 or p2).
3267 * 0 if the tunnel remained unaffected.
3269 static GNUNET_PEER_Id
3270 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3276 tree_notify_connection_broken (t->tree, p1, p2,
3277 &tunnel_notify_client_peer_disconnected,
3279 if (myid != p1 && myid != p2)
3285 if (tree_get_predecessor (t->tree) != 0)
3287 /* We are the peer still connected, notify owner of the disconnection. */
3288 struct GNUNET_MESH_PathBroken msg;
3289 struct GNUNET_PeerIdentity neighbor;
3291 msg.header.size = htons (sizeof (msg));
3292 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3293 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3294 msg.tid = htonl (t->id.tid);
3295 msg.peer1 = my_full_id;
3296 GNUNET_PEER_resolve (pid, &msg.peer2);
3297 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3298 send_message (&msg.header, &neighbor, t);
3306 * Send a multicast packet to a neighbor.
3308 * @param cls Closure (Info about the multicast packet)
3309 * @param neighbor_id Short ID of the neighbor to send the packet to.
3312 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3314 struct MeshData *mdata = cls;
3315 struct MeshTransmissionDescriptor *info;
3316 struct GNUNET_PeerIdentity neighbor;
3318 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3320 info->mesh_data = mdata;
3321 (mdata->reference_counter) ++;
3322 info->destination = neighbor_id;
3323 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3325 GNUNET_i2s (&neighbor));
3326 info->peer = peer_info_get (&neighbor);
3327 GNUNET_assert (NULL != info->peer);
3329 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3330 info->mesh_data->data_len,
3337 * Queue a message in a tunnel in multicast, sending a copy to each child node
3338 * down the local one in the tunnel tree.
3340 * @param t Tunnel in which to send the data.
3341 * @param msg Message to be sent.
3342 * @param internal DEPRECATED Has the service generated this message?
3344 * FIXME remove internal if no use comes up
3347 tunnel_send_multicast (struct MeshTunnel *t,
3348 const struct GNUNET_MessageHeader *msg,
3351 struct MeshData *mdata;
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3354 " sending a multicast packet...\n");
3356 mdata = GNUNET_malloc (sizeof (struct MeshData));
3357 mdata->data_len = ntohs (msg->size);
3359 mdata->data = GNUNET_malloc (mdata->data_len);
3360 memcpy (mdata->data, msg, mdata->data_len);
3361 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3363 struct GNUNET_MESH_Multicast *mcast;
3365 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3366 if (t->fwd_queue_n >= t->fwd_queue_max)
3369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3371 " message from %s!\n",
3372 GNUNET_i2s(&mcast->oid));
3373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3374 " message at %s!\n",
3375 GNUNET_i2s(&my_full_id));
3376 GNUNET_free (mdata->data);
3377 GNUNET_free (mdata);
3381 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3383 ntohl (mcast->ttl));
3387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3390 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3391 if (mdata->reference_counter == 0)
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394 " no one to send data to\n");
3395 GNUNET_free (mdata->data);
3396 GNUNET_free (mdata);
3401 mdata->total_out = mdata->reference_counter;
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3404 " sending a multicast packet done\n");
3410 * Increase the SKIP value of all peers that
3411 * have not received a unicast message.
3413 * @param cls Closure (ID of the peer that HAS received the message).
3414 * @param key ID of the neighbor.
3415 * @param value Information about the neighbor.
3417 * @return GNUNET_YES to keep iterating.
3420 tunnel_add_skip (void *cls,
3421 const struct GNUNET_HashCode * key,
3424 struct GNUNET_PeerIdentity *neighbor = cls;
3425 struct MeshTunnelChildInfo *cinfo = value;
3427 /* TODO compare only pointers? key == neighbor? */
3428 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3438 * @brief Get neighbor's Flow Control information.
3440 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3441 * descendant of the local node in a certain tunnel.
3442 * If the info is not yet there (recently created path), creates the data struct
3443 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3446 * @param t Tunnel related.
3447 * @param peer Neighbor whose Flow Control info is needed.
3449 * @return Neighbor's Flow Control info.
3451 static struct MeshTunnelChildInfo *
3452 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3453 const struct GNUNET_PeerIdentity *peer)
3455 struct MeshTunnelChildInfo *cinfo;
3456 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3462 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3463 cinfo->id = GNUNET_PEER_intern (peer);
3464 cinfo->skip = t->fwd_pid;
3466 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3467 cinfo->fwd_ack = t->fwd_pid + delta;
3468 cinfo->bck_ack = delta;
3470 cinfo->send_buffer =
3471 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3473 GNUNET_assert (GNUNET_OK ==
3474 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3477 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3484 * Get the Flow Control info of a client.
3486 * @param t Tunnel on which to look.
3487 * @param c Client whose ACK to get.
3489 * @return ACK value.
3491 static struct MeshTunnelClientInfo *
3492 tunnel_get_client_fc (struct MeshTunnel *t,
3493 struct MeshClient *c)
3497 for (i = 0; i < t->nclients; i++)
3499 if (t->clients[i] != c)
3501 return &t->clients_fc[i];
3504 return NULL; // avoid compiler / coverity complaints
3509 * Iterator to get the appropiate ACK value from all children nodes.
3511 * @param cls Closue (tunnel).
3512 * @param id Id of the child node.
3515 tunnel_get_child_fwd_ack (void *cls,
3518 struct GNUNET_PeerIdentity peer_id;
3519 struct MeshTunnelChildInfo *cinfo;
3520 struct MeshTunnelChildIteratorContext *ctx = cls;
3521 struct MeshTunnel *t = ctx->t;
3524 GNUNET_PEER_resolve (id, &peer_id);
3525 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3526 ack = cinfo->fwd_ack;
3529 if (GNUNET_NO == ctx->init)
3531 ctx->max_child_ack = ack;
3532 ctx->init = GNUNET_YES;
3535 if (GNUNET_YES == t->speed_min)
3537 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3541 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3548 * Get the maximum PID allowed to transmit to any
3549 * tunnel child of the local peer, depending on the tunnel
3550 * buffering/speed settings.
3554 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3557 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3559 struct MeshTunnelChildIteratorContext ctx;
3561 ctx.max_child_ack = 0;
3563 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3565 if (0 == ctx.nchildren)
3567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3568 " tunnel has no children, no FWD ACK\n");
3572 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3573 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3575 return (int64_t) ctx.max_child_ack;
3580 * Set the FWD ACK value of a client in a particular tunnel.
3582 * @param t Tunnel affected.
3583 * @param c Client whose ACK to set.
3584 * @param ack ACK value.
3587 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3588 struct MeshClient *c,
3593 for (i = 0; i < t->nclients; i++)
3595 if (t->clients[i] != c)
3597 t->clients_fc[i].fwd_ack = ack;
3605 * Get the highest ACK value of all clients in a particular tunnel,
3606 * according to the buffering/speed settings.
3608 * @param t Tunnel on which to look.
3610 * @return Corresponding ACK value (max uint32_t).
3611 * If no clients are suscribed, -1LL.
3614 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3619 if (0 == t->nclients)
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3622 " tunnel has no clients, no FWD ACK\n");
3626 for (ack = -1LL, i = 0; i < t->nclients; i++)
3629 (GNUNET_YES == t->speed_min &&
3630 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3631 (GNUNET_NO == t->speed_min &&
3632 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3634 ack = t->clients_fc[i].fwd_ack;
3638 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3639 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3641 return (uint32_t) ack;
3646 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3647 * mode and the status of all children nodes.
3651 * @return Maximum PID allowed.
3654 tunnel_get_fwd_ack (struct MeshTunnel *t)
3658 uint32_t buffer_free;
3662 count = t->fwd_pid - t->skip;
3663 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3664 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3665 child_ack = tunnel_get_children_fwd_ack (t);
3666 client_ack = tunnel_get_clients_fwd_ack (t);
3667 if (-1LL == child_ack)
3669 // Node has no children, child_ack AND core buffer are irrelevant.
3670 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3671 return (uint32_t) client_ack;
3673 if (-1LL == client_ack)
3677 if (GNUNET_YES == t->speed_min)
3679 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3680 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3684 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3685 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3687 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3688 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3690 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3691 count, buffer_free, child_ack, client_ack, ack);
3697 * Build a local ACK message and send it to a local client.
3699 * @param t Tunnel on which to send the ACK.
3700 * @param c Client to whom send the ACK.
3701 * @param ack Value of the ACK.
3704 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3706 struct GNUNET_MESH_LocalAck msg;
3708 msg.header.size = htons (sizeof (msg));
3709 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3710 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3711 msg.max_pid = htonl (ack);
3712 GNUNET_SERVER_notification_context_unicast(nc,
3719 * Build an ACK message and queue it to send to the given peer.
3721 * @param t Tunnel on which to send the ACK.
3722 * @param peer Peer to whom send the ACK.
3723 * @param ack Value of the ACK.
3726 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3728 struct GNUNET_MESH_ACK msg;
3730 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3731 msg.header.size = htons (sizeof (msg));
3732 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3733 msg.pid = htonl (ack);
3734 msg.tid = htonl (t->id.tid);
3736 send_message (&msg.header, peer, t);
3741 * Notify a the owner of a tunnel about how many more
3742 * payload packages will we accept on a given tunnel.
3744 * @param t Tunnel on which to send the ACK.
3747 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3752 "Sending client FWD ACK on tunnel %X\n",
3755 ack = tunnel_get_fwd_ack (t);
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3758 if (t->last_fwd_ack == ack)
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3762 t->last_fwd_ack = ack;
3763 send_local_ack (t, t->owner, ack);
3768 * Send an ACK informing the predecessor about the available buffer space.
3769 * In case there is no predecessor, inform the owning client.
3770 * If buffering is off, send only on behalf of children or self if endpoint.
3771 * If buffering is on, send when sent to children and buffer space is free.
3772 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3773 * the ACK itself goes "back" (towards root).
3775 * @param t Tunnel on which to send the ACK.
3776 * @param type Type of message that triggered the ACK transmission.
3779 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3781 struct GNUNET_PeerIdentity id;
3784 if (NULL != t->owner)
3786 tunnel_send_client_fwd_ack (t);
3789 /* Is it after unicast / multicast retransmission? */
3792 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3793 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3795 "ACK due to FWD DATA retransmission\n");
3796 if (GNUNET_YES == t->nobuffer)
3798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3802 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3803 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3809 /* Check if we need no retransmit the ACK */
3810 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3811 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3815 " t->qmax: %u, t->qn: %u\n",
3816 t->fwd_queue_max, t->fwd_queue_n);
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3818 " t->pid: %u, t->ack: %u\n",
3819 t->fwd_pid, t->last_fwd_ack);
3823 /* Ok, ACK might be necessary, what PID to ACK? */
3824 ack = tunnel_get_fwd_ack (t);
3826 /* If speed_min and not all children have ack'd, dont send yet */
3827 if (ack == t->last_fwd_ack)
3829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3833 t->last_fwd_ack = ack;
3834 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3835 send_ack (t, &id, ack);
3841 * Iterator to send a child node a BCK ACK to allow him to send more
3844 * @param cls Closure (tunnel).
3845 * @param id Id of the child node.
3848 tunnel_send_child_bck_ack (void *cls,
3851 struct MeshTunnel *t = cls;
3852 struct MeshTunnelChildInfo *cinfo;
3853 struct GNUNET_PeerIdentity peer;
3855 GNUNET_PEER_resolve (id, &peer);
3856 cinfo = tunnel_get_neighbor_fc (t, &peer);
3858 if (cinfo->bck_ack != cinfo->pid &&
3859 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3863 send_ack (t, &peer, cinfo->bck_ack);
3868 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3870 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3872 * @param t Tunnel on which to send the BCK ACKs.
3875 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3878 unsigned int tunnel_delta;
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3882 tunnel_delta = t->bck_ack - t->bck_pid;
3883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3885 /* Find client whom to allow to send to origin (with lowest buffer space) */
3886 for (i = 0; i < t->nclients; i++)
3888 struct MeshTunnelClientInfo *clinfo;
3891 clinfo = &t->clients_fc[i];
3892 delta = clinfo->bck_ack - clinfo->bck_pid;
3893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3894 t->clients[i]->id, delta);
3896 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3897 (GNUNET_YES == t->nobuffer && 0 == delta))
3901 ack = clinfo->bck_pid;
3902 ack += t->nobuffer ? 1 : tunnel_delta;
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3904 " sending ack to client %u: %u\n",
3905 t->clients[i]->id, ack);
3906 send_local_ack (t, t->clients[i], ack);
3907 clinfo->bck_ack = ack;
3914 * Send an ACK informing the children nodes and destination clients about
3915 * the available buffer space.
3916 * If buffering is off, send only on behalf of root (can be self).
3917 * If buffering is on, send when sent to predecessor and buffer space is free.
3918 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3919 * the ACK itself goes "forward" (towards children/clients).
3921 * @param t Tunnel on which to send the ACK.
3922 * @param type Type of message that triggered the ACK transmission.
3925 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3929 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3930 /* Is it after data to_origin retransmission? */
3933 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3934 if (GNUNET_YES == t->nobuffer)
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3937 " Not sending ACK, nobuffer\n");
3941 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3942 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3948 tunnel_send_clients_bck_ack (t);
3949 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3954 * @brief Re-initiate traffic to this peer if necessary.
3956 * Check if there is traffic queued towards this peer
3957 * and the core transmit handle is NULL (traffic was stalled).
3958 * If so, call core tmt rdy.
3960 * @param cls Closure (unused)
3961 * @param peer_id Short ID of peer to which initiate traffic.
3964 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3966 struct MeshPeerInfo *peer;
3967 struct GNUNET_PeerIdentity id;
3968 struct MeshPeerQueue *q;
3971 peer = peer_info_get_short(peer_id);
3972 if (NULL != peer->core_transmit)
3975 q = queue_get_next(peer);
3978 /* Might br multicast traffic already sent to this particular peer but
3979 * not to other children in this tunnel.
3980 * This way t->queue_n would be > 0 but the queue of this particular peer
3986 GNUNET_PEER_resolve (peer->id, &id);
3987 peer->core_transmit =
3988 GNUNET_CORE_notify_transmit_ready(core_handle,
3991 GNUNET_TIME_UNIT_FOREVER_REL,
4001 * @brief Allow transmission of FWD traffic on this tunnel
4003 * Check if there is traffic queued towards any children
4004 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4006 * @param t Tunnel on which to unlock FWD traffic.
4009 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4011 if (0 == t->fwd_queue_n)
4014 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4019 * @brief Allow transmission of BCK traffic on this tunnel
4021 * Check if there is traffic queued towards the root of the tree
4022 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4024 * @param t Tunnel on which to unlock BCK traffic.
4027 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4029 if (0 == t->bck_queue_n)
4032 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4037 * Send a message to all peers in this tunnel that the tunnel is no longer
4040 * @param t The tunnel whose peers to notify.
4043 tunnel_send_destroy (struct MeshTunnel *t)
4045 struct GNUNET_MESH_TunnelDestroy msg;
4047 msg.header.size = htons (sizeof (msg));
4048 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4049 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4050 msg.tid = htonl (t->id.tid);
4051 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4056 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4058 * @param cls Closure (Tunnel which to cancel).
4059 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4062 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4064 struct MeshTunnel *t = cls;
4065 struct MeshPeerInfo *peer_info;
4066 struct MeshPeerQueue *pq;
4067 struct MeshPeerQueue *next;
4069 peer_info = peer_info_get_short (neighbor_id);
4070 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4073 if (pq->tunnel == t)
4075 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4076 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4077 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4079 // Should have been removed on destroy children
4082 queue_destroy (pq, GNUNET_YES);
4085 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4087 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4088 peer_info->core_transmit = NULL;
4093 * Destroy the tunnel and free any allocated resources linked to it.
4095 * @param t the tunnel to destroy
4097 * @return GNUNET_OK on success
4100 tunnel_destroy (struct MeshTunnel *t)
4102 struct MeshClient *c;
4103 struct GNUNET_HashCode hash;
4114 struct GNUNET_PeerIdentity id;
4116 GNUNET_PEER_resolve (t->id.oid, &id);
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4118 GNUNET_i2s (&id), t->id.tid);
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4124 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4125 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4130 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4133 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4137 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4138 for (i = 0; i < t->nclients; i++)
4142 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4147 for (i = 0; i < t->nignore; i++)
4151 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4156 if (t->nclients > 0)
4159 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4163 GNUNET_free (t->clients);
4165 if (NULL != t->peers)
4167 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4169 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4172 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4173 &tunnel_destroy_child,
4175 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4177 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4179 tree_destroy (t->tree);
4181 if (NULL != t->regex_ctx)
4182 regex_cancel_search (t->regex_ctx);
4183 if (NULL != t->dht_get_type)
4184 GNUNET_DHT_get_stop (t->dht_get_type);
4185 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4186 GNUNET_SCHEDULER_cancel (t->timeout_task);
4187 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4188 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4191 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4192 GNUNET_assert (0 <= n_tunnels);
4199 * Create a new tunnel
4201 * @param owner Who is the owner of the tunnel (short ID).
4202 * @param tid Tunnel Number of the tunnel.
4203 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4204 * @param local Tunnel Number for the tunnel, for the client point of view.
4206 * @return A new initialized tunnel. NULL on error.
4208 static struct MeshTunnel *
4209 tunnel_new (GNUNET_PEER_Id owner,
4210 MESH_TunnelNumber tid,
4211 struct MeshClient *client,
4212 MESH_TunnelNumber local)
4214 struct MeshTunnel *t;
4215 struct GNUNET_HashCode hash;
4217 if (n_tunnels >= max_tunnels && NULL == client)
4220 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4223 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4224 t->bck_queue_max = t->fwd_queue_max;
4225 t->tree = tree_new (owner);
4227 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4228 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4229 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4230 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4231 t->local_tid = local;
4232 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4234 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4236 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4238 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4246 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4253 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4255 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4260 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4270 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4271 * that are no longer needed, as well as nodes of no longer reachable peers.
4272 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4274 * @param t Tunnel from which to remove the path.
4275 * @param peer Short id of the peer which should be removed.
4278 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4280 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4286 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4287 * client when the client disconnects. If the client is not the owner, the
4288 * owner will get notified if no more clients are in the tunnel and the client
4289 * get removed from the tunnel's list.
4291 * @param cls closure (client that is disconnecting)
4292 * @param key the hash of the local tunnel id (used to access the hashmap)
4293 * @param value the value stored at the key (tunnel to destroy)
4295 * @return GNUNET_OK on success
4298 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4300 struct MeshTunnel *t = value;
4301 struct MeshClient *c = cls;
4304 send_client_tunnel_disconnect(t, c);
4307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4308 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4309 tunnel_delete_client(t, c);
4310 client_delete_tunnel(c, t);
4313 tunnel_send_destroy(t);
4314 r = tunnel_destroy (t);
4320 * Timeout function, destroys tunnel if called
4322 * @param cls Closure (tunnel to destroy).
4323 * @param tc TaskContext
4326 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4328 struct MeshTunnel *t = cls;
4330 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4332 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4337 * Resets the tunnel timeout. Starts it if no timeout was running.
4339 * @param t Tunnel whose timeout to reset.
4341 * TODO use heap to improve efficiency of scheduler.
4344 tunnel_reset_timeout (struct MeshTunnel *t)
4346 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4347 GNUNET_SCHEDULER_cancel (t->timeout_task);
4349 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4350 (refresh_path_time, 4), &tunnel_timeout, t);
4354 /******************************************************************************/
4355 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4356 /******************************************************************************/
4359 * Function to send a create path packet to a peer.
4361 * @param cls closure
4362 * @param size number of bytes available in buf
4363 * @param buf where the callee should write the message
4364 * @return number of bytes written to buf
4367 send_core_path_create (void *cls, size_t size, void *buf)
4369 struct MeshPathInfo *info = cls;
4370 struct GNUNET_MESH_ManipulatePath *msg;
4371 struct GNUNET_PeerIdentity *peer_ptr;
4372 struct MeshTunnel *t = info->t;
4373 struct MeshPeerPath *p = info->path;
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4380 sizeof (struct GNUNET_MESH_ManipulatePath) +
4381 p->length * sizeof (struct GNUNET_PeerIdentity);
4383 if (size < size_needed || NULL == buf)
4388 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4389 msg->header.size = htons (size_needed);
4390 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4391 msg->tid = ntohl (t->id.tid);
4394 if (GNUNET_YES == t->speed_min)
4395 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4396 if (GNUNET_YES == t->nobuffer)
4397 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4398 msg->opt = htonl(opt);
4400 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4401 for (i = 0; i < p->length; i++)
4403 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4410 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4416 * Fill the core buffer
4418 * @param cls closure (data itself)
4419 * @param size number of bytes available in buf
4420 * @param buf where the callee should write the message
4422 * @return number of bytes written to buf
4425 send_core_data_multicast (void *cls, size_t size, void *buf)
4427 struct MeshTransmissionDescriptor *info = cls;
4430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4431 GNUNET_assert (NULL != info);
4432 GNUNET_assert (NULL != info->peer);
4433 total_size = info->mesh_data->data_len;
4434 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4436 if (total_size > size)
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4442 memcpy (buf, info->mesh_data->data, total_size);
4445 struct GNUNET_MESH_Multicast *mc;
4446 struct GNUNET_MessageHeader *mh;
4449 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4451 mc = (struct GNUNET_MESH_Multicast *) mh;
4452 mh = (struct GNUNET_MessageHeader *) &mc[1];
4453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4454 " multicast, payload type %s\n",
4455 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4457 " multicast, payload size %u\n", ntohs (mh->size));
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4462 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4466 data_descriptor_decrement_rc (info->mesh_data);
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4475 * Creates a path ack message in buf and frees all unused resources.
4477 * @param cls closure (MeshTransmissionDescriptor)
4478 * @param size number of bytes available in buf
4479 * @param buf where the callee should write the message
4480 * @return number of bytes written to buf
4483 send_core_path_ack (void *cls, size_t size, void *buf)
4485 struct MeshTransmissionDescriptor *info = cls;
4486 struct GNUNET_MESH_PathACK *msg = buf;
4488 GNUNET_assert (NULL != info);
4489 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4494 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4495 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4496 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4497 msg->tid = htonl (info->origin->tid);
4498 msg->peer_id = my_full_id;
4501 /* TODO add signature */
4503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4504 return sizeof (struct GNUNET_MESH_PathACK);
4509 * Free a transmission that was already queued with all resources
4510 * associated to the request.
4512 * @param queue Queue handler to cancel.
4513 * @param clear_cls Is it necessary to free associated cls?
4516 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4518 struct MeshTransmissionDescriptor *dd;
4519 struct MeshPathInfo *path_info;
4521 if (GNUNET_YES == clear_cls)
4523 switch (queue->type)
4525 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4526 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4527 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4530 data_descriptor_decrement_rc (dd->mesh_data);
4532 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4534 path_info = queue->cls;
4535 path_destroy (path_info->path);
4539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4540 " type %s unknown!\n",
4541 GNUNET_MESH_DEBUG_M2S(queue->type));
4543 GNUNET_free_non_null (queue->cls);
4545 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4546 queue->peer->queue_tail,
4548 GNUNET_free (queue);
4553 * @brief Get the next transmittable message from the queue.
4555 * This will be the head, except in the case of being a data packet
4556 * not allowed by the destination peer.
4558 * @param peer Destination peer.
4560 * @return The next viable MeshPeerQueue element to send to that peer.
4561 * NULL when there are no transmittable messages.
4563 struct MeshPeerQueue *
4564 queue_get_next (const struct MeshPeerInfo *peer)
4566 struct MeshPeerQueue *q;
4567 struct MeshTunnel *t;
4568 struct MeshTransmissionDescriptor *info;
4569 struct MeshTunnelChildInfo *cinfo;
4570 struct GNUNET_MESH_Unicast *ucast;
4571 struct GNUNET_MESH_ToOrigin *to_orig;
4572 struct GNUNET_MESH_Multicast *mcast;
4573 struct GNUNET_PeerIdentity id;
4577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4578 for (q = peer->queue_head; NULL != q; q = q->next)
4582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4584 GNUNET_MESH_DEBUG_M2S(q->type));
4587 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4588 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4589 pid = ntohl (ucast->pid);
4590 GNUNET_PEER_resolve (info->peer->id, &id);
4591 cinfo = tunnel_get_neighbor_fc(t, &id);
4592 ack = cinfo->fwd_ack;
4594 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4595 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4596 pid = ntohl (to_orig->pid);
4599 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4600 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4601 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4603 // Not a multicast payload: multicast control traffic (destroy, etc)
4606 pid = ntohl (mcast->pid);
4607 GNUNET_PEER_resolve (info->peer->id, &id);
4608 cinfo = tunnel_get_neighbor_fc(t, &id);
4609 ack = cinfo->fwd_ack;
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4617 "********* ACK: %u, PID: %u\n",
4619 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4628 "********* NEXT!\n");
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4632 "********* nothing found\n");
4638 * Core callback to write a queued packet to core buffer
4640 * @param cls Closure (peer info).
4641 * @param size Number of bytes available in buf.
4642 * @param buf Where the to write the message.
4644 * @return number of bytes written to buf
4647 queue_send (void *cls, size_t size, void *buf)
4649 struct MeshPeerInfo *peer = cls;
4650 struct GNUNET_MessageHeader *msg;
4651 struct MeshPeerQueue *queue;
4652 struct MeshTunnel *t;
4653 struct MeshTunnelChildInfo *cinfo;
4654 struct GNUNET_PeerIdentity dst_id;
4657 peer->core_transmit = NULL;
4659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4660 queue = queue_get_next(peer);
4662 /* Queue has no internal mesh traffic not sendable payload */
4665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4666 if (NULL == peer->queue_head)
4667 GNUNET_break(0); // Should've been canceled
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4672 GNUNET_PEER_resolve (peer->id, &dst_id);
4673 /* Check if buffer size is enough for the message */
4674 if (queue->size > size)
4678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4679 "********* not enough room, reissue\n");
4680 peer->core_transmit =
4681 GNUNET_CORE_notify_transmit_ready(core_handle,
4684 GNUNET_TIME_UNIT_FOREVER_REL,
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4694 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4698 "********* unicast: t->q (%u/%u)\n",
4699 t->fwd_queue_n, t->fwd_queue_max);
4701 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4708 switch (queue->type)
4711 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4712 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4713 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4715 "********* raw: %s\n",
4716 GNUNET_MESH_DEBUG_M2S (queue->type));
4718 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4719 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4720 data_size = send_core_data_raw (queue->cls, size, buf);
4721 msg = (struct GNUNET_MessageHeader *) buf;
4722 switch (ntohs (msg->type)) // Type of preconstructed message
4724 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4725 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4727 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4728 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4734 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4737 struct MeshTransmissionDescriptor *info = queue->cls;
4739 if ((1 == info->mesh_data->reference_counter
4740 && GNUNET_YES == t->speed_min)
4742 (info->mesh_data->total_out == info->mesh_data->reference_counter
4743 && GNUNET_NO == t->speed_min))
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4746 "********* considered sent\n");
4750 data_size = send_core_data_multicast(queue->cls, size, buf);
4751 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4753 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4755 data_size = send_core_path_create(queue->cls, size, buf);
4757 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4759 data_size = send_core_path_ack(queue->cls, size, buf);
4763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4764 "********* type unknown: %u\n",
4768 switch (queue->type)
4770 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4771 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4772 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4773 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4774 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4777 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4778 "at pos %u (%p) != %p\n",
4779 cinfo->send_buffer_start,
4780 cinfo->send_buffer[cinfo->send_buffer_start],
4783 GNUNET_break(cinfo->send_buffer_n > 0);
4784 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4785 cinfo->send_buffer_n--;
4786 cinfo->send_buffer_start++;
4787 cinfo->send_buffer_start %= t->fwd_queue_max;
4793 /* Free queue, but cls was freed by send_core_* */
4794 queue_destroy (queue, GNUNET_NO);
4796 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4802 /* If more data in queue, send next */
4803 queue = queue_get_next(peer);
4806 struct GNUNET_PeerIdentity id;
4808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4809 GNUNET_PEER_resolve (peer->id, &id);
4810 peer->core_transmit =
4811 GNUNET_CORE_notify_transmit_ready(core_handle,
4814 GNUNET_TIME_UNIT_FOREVER_REL,
4822 if (NULL != peer->queue_head)
4823 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4824 "********* %s stalled\n",
4825 GNUNET_i2s(&my_full_id));
4827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4833 * Queue and pass message to core when possible.
4835 * @param cls Closure (type dependant).
4836 * @param type Type of the message, 0 for a raw message.
4837 * @param size Size of the message.
4838 * @param dst Neighbor to send message to.
4839 * @param t Tunnel this message belongs to.
4842 queue_add (void *cls, uint16_t type, size_t size,
4843 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4845 struct MeshPeerQueue *queue;
4846 struct MeshTunnelChildInfo *cinfo;
4847 struct GNUNET_PeerIdentity id;
4853 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4854 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4856 n = &t->fwd_queue_n;
4857 max = &t->fwd_queue_max;
4859 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4861 n = &t->bck_queue_n;
4862 max = &t->bck_queue_max;
4867 if (NULL == t->owner)
4868 GNUNET_break_op(0); // TODO: kill connection?
4871 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4873 return; // Drop message
4877 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4883 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4884 GNUNET_PEER_resolve (dst->id, &id);
4885 if (NULL == dst->core_transmit)
4887 dst->core_transmit =
4888 GNUNET_CORE_notify_transmit_ready(core_handle,
4891 GNUNET_TIME_UNIT_FOREVER_REL,
4897 if (NULL == n) // Is this internal mesh traffic?
4900 // It's payload, keep track of buffer per peer.
4901 cinfo = tunnel_get_neighbor_fc(t, &id);
4902 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4903 if (NULL != cinfo->send_buffer[i])
4905 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4906 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4907 cinfo->send_buffer_start++;
4908 cinfo->send_buffer_start %= t->fwd_queue_max;
4909 cinfo->send_buffer_n--;
4911 cinfo->send_buffer[i] = queue;
4912 cinfo->send_buffer_n++;
4913 if (cinfo->send_buffer_n > t->fwd_queue_max)
4916 cinfo->send_buffer_n = t->fwd_queue_max;
4921 /******************************************************************************/
4922 /******************** MESH NETWORK HANDLERS **************************/
4923 /******************************************************************************/
4927 * Core handler for path creation
4929 * @param cls closure
4930 * @param message message
4931 * @param peer peer identity this notification is about
4932 * @param atsi performance data
4933 * @param atsi_count number of records in 'atsi'
4935 * @return GNUNET_OK to keep the connection open,
4936 * GNUNET_SYSERR to close it (signal serious error)
4939 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4940 const struct GNUNET_MessageHeader *message,
4941 const struct GNUNET_ATS_Information *atsi,
4942 unsigned int atsi_count)
4944 unsigned int own_pos;
4947 MESH_TunnelNumber tid;
4948 struct GNUNET_MESH_ManipulatePath *msg;
4949 struct GNUNET_PeerIdentity *pi;
4950 struct GNUNET_HashCode hash;
4951 struct MeshPeerPath *path;
4952 struct MeshPeerInfo *dest_peer_info;
4953 struct MeshPeerInfo *orig_peer_info;
4954 struct MeshTunnel *t;
4956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4957 "Received a path create msg [%s]\n",
4958 GNUNET_i2s (&my_full_id));
4959 size = ntohs (message->size);
4960 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4962 GNUNET_break_op (0);
4966 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4967 if (size % sizeof (struct GNUNET_PeerIdentity))
4969 GNUNET_break_op (0);
4972 size /= sizeof (struct GNUNET_PeerIdentity);
4975 GNUNET_break_op (0);
4978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4979 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4981 tid = ntohl (msg->tid);
4982 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4984 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4985 t = tunnel_get (pi, tid);
4986 if (NULL == t) // FIXME only for INCOMING tunnels?
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4991 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4994 // FIXME notify failure
4997 opt = ntohl (msg->opt);
4998 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4999 GNUNET_YES : GNUNET_NO;
5000 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
5001 GNUNET_YES : GNUNET_NO;
5002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5003 " speed_min: %d, nobuffer:%d\n",
5004 t->speed_min, t->nobuffer);
5006 if (GNUNET_YES == t->nobuffer)
5008 t->bck_queue_max = 1;
5009 t->fwd_queue_max = 1;
5012 // FIXME only assign a local tid if a local client is interested (on demand)
5013 while (NULL != tunnel_get_incoming (next_local_tid))
5014 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5015 t->local_tid_dest = next_local_tid++;
5016 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5019 tunnel_reset_timeout (t);
5020 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5022 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5023 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5031 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5032 if (NULL == dest_peer_info)
5034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5035 " Creating PeerInfo for destination.\n");
5036 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5037 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5038 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5040 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5042 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5043 if (NULL == orig_peer_info)
5045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5046 " Creating PeerInfo for origin.\n");
5047 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5048 orig_peer_info->id = GNUNET_PEER_intern (pi);
5049 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5050 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5053 path = path_new (size);
5055 for (i = 0; i < size; i++)
5057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5058 GNUNET_i2s (&pi[i]));
5059 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5060 if (path->peers[i] == myid)
5063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5066 /* cannot be self, must be 'not found' */
5067 /* create path: self not found in path through self */
5068 GNUNET_break_op (0);
5069 path_destroy (path);
5073 path_add_to_peers (path, GNUNET_NO);
5074 tunnel_add_path (t, path, own_pos);
5075 if (own_pos == size - 1)
5077 /* It is for us! Send ack. */
5078 struct MeshTransmissionDescriptor *info;
5080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5081 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5082 if (NULL == t->peers)
5084 /* New tunnel! Notify clients on data. */
5085 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5087 GNUNET_break (GNUNET_SYSERR !=
5088 GNUNET_CONTAINER_multihashmap_put (t->peers,
5089 &my_full_id.hashPubKey,
5092 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5093 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5094 info->origin = &t->id;
5095 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5096 GNUNET_assert (NULL != info->peer);
5098 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5099 sizeof (struct GNUNET_MESH_PathACK),
5105 struct MeshPeerPath *path2;
5107 /* It's for somebody else! Retransmit. */
5108 path2 = path_duplicate (path);
5109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5110 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5111 path2 = path_duplicate (path);
5112 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5113 send_create_path (dest_peer_info, path, t);
5120 * Core handler for path destruction
5122 * @param cls closure
5123 * @param message message
5124 * @param peer peer identity this notification is about
5125 * @param atsi performance data
5126 * @param atsi_count number of records in 'atsi'
5128 * @return GNUNET_OK to keep the connection open,
5129 * GNUNET_SYSERR to close it (signal serious error)
5132 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5133 const struct GNUNET_MessageHeader *message,
5134 const struct GNUNET_ATS_Information *atsi,
5135 unsigned int atsi_count)
5137 struct GNUNET_MESH_ManipulatePath *msg;
5138 struct GNUNET_PeerIdentity *pi;
5139 struct MeshPeerPath *path;
5140 struct MeshTunnel *t;
5141 unsigned int own_pos;
5145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5146 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5147 size = ntohs (message->size);
5148 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5150 GNUNET_break_op (0);
5154 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5155 if (size % sizeof (struct GNUNET_PeerIdentity))
5157 GNUNET_break_op (0);
5160 size /= sizeof (struct GNUNET_PeerIdentity);
5163 GNUNET_break_op (0);
5166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5168 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5169 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5171 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5173 t = tunnel_get (pi, ntohl (msg->tid));
5176 /* TODO notify back: we don't know this tunnel */
5177 GNUNET_break_op (0);
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5181 path = path_new (size);
5183 for (i = 0; i < size; i++)
5185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5186 GNUNET_i2s (&pi[i]));
5187 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5188 if (path->peers[i] == myid)
5191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5192 if (own_pos < path->length - 1)
5193 send_message (message, &pi[own_pos + 1], t);
5195 send_client_tunnel_disconnect(t, NULL);
5197 tunnel_delete_peer (t, path->peers[path->length - 1]);
5198 path_destroy (path);
5204 * Core handler for notifications of broken paths
5206 * @param cls closure
5207 * @param message message
5208 * @param peer peer identity this notification is about
5209 * @param atsi performance data
5210 * @param atsi_count number of records in 'atsi'
5212 * @return GNUNET_OK to keep the connection open,
5213 * GNUNET_SYSERR to close it (signal serious error)
5216 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5217 const struct GNUNET_MessageHeader *message,
5218 const struct GNUNET_ATS_Information *atsi,
5219 unsigned int atsi_count)
5221 struct GNUNET_MESH_PathBroken *msg;
5222 struct MeshTunnel *t;
5224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5225 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5226 msg = (struct GNUNET_MESH_PathBroken *) message;
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5228 GNUNET_i2s (&msg->peer1));
5229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5230 GNUNET_i2s (&msg->peer2));
5231 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5234 GNUNET_break_op (0);
5237 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5238 GNUNET_PEER_search (&msg->peer2));
5245 * Core handler for tunnel destruction
5247 * @param cls closure
5248 * @param message message
5249 * @param peer peer identity this notification is about
5250 * @param atsi performance data
5251 * @param atsi_count number of records in 'atsi'
5253 * @return GNUNET_OK to keep the connection open,
5254 * GNUNET_SYSERR to close it (signal serious error)
5257 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5258 const struct GNUNET_MessageHeader *message,
5259 const struct GNUNET_ATS_Information *atsi,
5260 unsigned int atsi_count)
5262 struct GNUNET_MESH_TunnelDestroy *msg;
5263 struct MeshTunnel *t;
5265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5266 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5267 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5269 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5270 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5273 /* Probably already got the message from another path,
5274 * destroyed the tunnel and retransmitted to children.
5277 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5280 if (t->id.oid == myid)
5282 GNUNET_break_op (0);
5285 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5287 /* Tunnel was incoming, notify clients */
5288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5289 t->local_tid, t->local_tid_dest);
5290 send_clients_tunnel_destroy (t);
5292 tunnel_send_destroy (t);
5293 t->destroy = GNUNET_YES;
5294 // TODO: add timeout to destroy the tunnel anyway
5300 * Core handler for mesh network traffic going from the origin to a peer
5302 * @param cls closure
5303 * @param peer peer identity this notification is about
5304 * @param message message
5305 * @param atsi performance data
5306 * @param atsi_count number of records in 'atsi'
5307 * @return GNUNET_OK to keep the connection open,
5308 * GNUNET_SYSERR to close it (signal serious error)
5311 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5312 const struct GNUNET_MessageHeader *message,
5313 const struct GNUNET_ATS_Information *atsi,
5314 unsigned int atsi_count)
5316 struct GNUNET_MESH_Unicast *msg;
5317 struct GNUNET_PeerIdentity *neighbor;
5318 struct MeshTunnelChildInfo *cinfo;
5319 struct MeshTunnel *t;
5320 GNUNET_PEER_Id dest_id;
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5328 size = ntohs (message->size);
5330 sizeof (struct GNUNET_MESH_Unicast) +
5331 sizeof (struct GNUNET_MessageHeader))
5336 msg = (struct GNUNET_MESH_Unicast *) message;
5337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5338 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5340 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5343 /* TODO notify back: we don't know this tunnel */
5344 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5345 GNUNET_break_op (0);
5348 pid = ntohl (msg->pid);
5349 if (t->fwd_pid == pid)
5351 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5353 " Already seen pid %u, DROPPING!\n", pid);
5358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5359 " pid %u not seen yet, forwarding\n", pid);
5362 t->skip += (pid - t->fwd_pid) - 1;
5365 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5367 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5368 GNUNET_break_op (0);
5369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5370 "Received PID %u, ACK %u\n",
5371 pid, t->last_fwd_ack);
5375 tunnel_reset_timeout (t);
5376 dest_id = GNUNET_PEER_search (&msg->destination);
5377 if (dest_id == myid)
5379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5380 " it's for us! sending to clients...\n");
5381 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5382 send_subscribed_clients (message, &msg[1].header, t);
5383 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5386 ttl = ntohl (msg->ttl);
5387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5390 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5391 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5392 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5396 " not for us, retransmitting...\n");
5398 neighbor = tree_get_first_hop (t->tree, dest_id);
5399 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5401 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5404 if (GNUNET_YES == t->nobuffer &&
5405 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5407 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5408 GNUNET_break_op (0);
5411 send_message (message, neighbor, t);
5412 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5418 * Core handler for mesh network traffic going from the origin to all peers
5420 * @param cls closure
5421 * @param message message
5422 * @param peer peer identity this notification is about
5423 * @param atsi performance data
5424 * @param atsi_count number of records in 'atsi'
5425 * @return GNUNET_OK to keep the connection open,
5426 * GNUNET_SYSERR to close it (signal serious error)
5428 * TODO: Check who we got this from, to validate route.
5431 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5432 const struct GNUNET_MessageHeader *message,
5433 const struct GNUNET_ATS_Information *atsi,
5434 unsigned int atsi_count)
5436 struct GNUNET_MESH_Multicast *msg;
5437 struct MeshTunnel *t;
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5443 size = ntohs (message->size);
5444 if (sizeof (struct GNUNET_MESH_Multicast) +
5445 sizeof (struct GNUNET_MessageHeader) > size)
5447 GNUNET_break_op (0);
5450 msg = (struct GNUNET_MESH_Multicast *) message;
5451 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5455 /* TODO notify that we dont know that tunnel */
5456 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5457 GNUNET_break_op (0);
5460 pid = ntohl (msg->pid);
5461 if (t->fwd_pid == pid)
5463 /* already seen this packet, drop */
5464 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5466 " Already seen pid %u, DROPPING!\n", pid);
5467 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5473 " pid %u not seen yet, forwarding\n", pid);
5475 t->skip += (pid - t->fwd_pid) - 1;
5477 tunnel_reset_timeout (t);
5479 /* Transmit to locally interested clients */
5480 if (NULL != t->peers &&
5481 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5483 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5484 send_subscribed_clients (message, &msg[1].header, t);
5485 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5488 if (ntohl (msg->ttl) == 0)
5490 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5491 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5492 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5495 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5496 tunnel_send_multicast (t, message, GNUNET_NO);
5502 * Core handler for mesh network traffic toward the owner of a tunnel
5504 * @param cls closure
5505 * @param message message
5506 * @param peer peer identity this notification is about
5507 * @param atsi performance data
5508 * @param atsi_count number of records in 'atsi'
5510 * @return GNUNET_OK to keep the connection open,
5511 * GNUNET_SYSERR to close it (signal serious error)
5514 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5515 const struct GNUNET_MessageHeader *message,
5516 const struct GNUNET_ATS_Information *atsi,
5517 unsigned int atsi_count)
5519 struct GNUNET_MESH_ToOrigin *msg;
5520 struct GNUNET_PeerIdentity id;
5521 struct MeshPeerInfo *peer_info;
5522 struct MeshTunnel *t;
5526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5528 size = ntohs (message->size);
5529 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5530 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5532 GNUNET_break_op (0);
5535 msg = (struct GNUNET_MESH_ToOrigin *) message;
5536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5537 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5538 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5542 /* TODO notify that we dont know this tunnel (whom)? */
5543 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5544 GNUNET_break_op (0);
5548 if (NULL != t->owner)
5551 struct GNUNET_MESH_ToOrigin *copy;
5553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5554 " it's for us! sending to clients...\n");
5555 /* TODO signature verification */
5556 memcpy (cbuf, message, size);
5557 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5558 copy->tid = htonl (t->local_tid);
5559 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5560 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5561 ©->header, GNUNET_NO);
5562 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5566 " not for us, retransmitting...\n");
5568 peer_info = peer_info_get (&msg->oid);
5569 if (NULL == peer_info)
5571 /* unknown origin of tunnel */
5575 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5576 send_message (message, &id, t);
5577 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5584 * Core handler for mesh network traffic point-to-point acks.
5586 * @param cls closure
5587 * @param message message
5588 * @param peer peer identity this notification is about
5589 * @param atsi performance data
5590 * @param atsi_count number of records in 'atsi'
5592 * @return GNUNET_OK to keep the connection open,
5593 * GNUNET_SYSERR to close it (signal serious error)
5596 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5597 const struct GNUNET_MessageHeader *message,
5598 const struct GNUNET_ATS_Information *atsi,
5599 unsigned int atsi_count)
5601 struct GNUNET_MESH_ACK *msg;
5602 struct MeshTunnel *t;
5605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5607 msg = (struct GNUNET_MESH_ACK *) message;
5609 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5613 /* TODO notify that we dont know this tunnel (whom)? */
5614 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5615 GNUNET_break_op (0);
5618 ack = ntohl (msg->pid);
5620 /* Is this a forward or backward ACK? */
5621 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5623 struct MeshTunnelChildInfo *cinfo;
5626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5627 cinfo = tunnel_get_neighbor_fc (t, peer);
5628 cinfo->fwd_ack = ack;
5629 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5630 tunnel_unlock_fwd_queues (t);
5634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5636 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5637 tunnel_unlock_bck_queue (t);
5644 * Core handler for path ACKs
5646 * @param cls closure
5647 * @param message message
5648 * @param peer peer identity this notification is about
5649 * @param atsi performance data
5650 * @param atsi_count number of records in 'atsi'
5652 * @return GNUNET_OK to keep the connection open,
5653 * GNUNET_SYSERR to close it (signal serious error)
5656 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5657 const struct GNUNET_MessageHeader *message,
5658 const struct GNUNET_ATS_Information *atsi,
5659 unsigned int atsi_count)
5661 struct GNUNET_MESH_PathACK *msg;
5662 struct GNUNET_PeerIdentity id;
5663 struct MeshPeerInfo *peer_info;
5664 struct MeshPeerPath *p;
5665 struct MeshTunnel *t;
5667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5668 GNUNET_i2s (&my_full_id));
5669 msg = (struct GNUNET_MESH_PathACK *) message;
5670 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5673 /* TODO notify that we don't know the tunnel */
5674 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5676 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5680 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5682 peer_info = peer_info_get (&msg->peer_id);
5683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5684 GNUNET_i2s (&msg->peer_id));
5685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5688 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5691 "connect_by_string completed, stopping search\n");
5692 regex_cancel_search (t->regex_ctx);
5693 t->regex_ctx = NULL;
5696 /* Add paths to peers? */
5697 p = tree_get_path_to_peer (t->tree, peer_info->id);
5700 path_add_to_peers (p, GNUNET_YES);
5708 /* Message for us? */
5709 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5712 if (NULL == t->owner)
5714 GNUNET_break_op (0);
5717 if (NULL != t->dht_get_type)
5719 GNUNET_DHT_get_stop (t->dht_get_type);
5720 t->dht_get_type = NULL;
5722 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5724 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5725 send_client_peer_connected (t, peer_info->id);
5730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5731 " not for us, retransmitting...\n");
5732 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5733 peer_info = peer_info_get (&msg->oid);
5734 if (NULL == peer_info)
5736 /* If we know the tunnel, we should DEFINITELY know the peer */
5740 send_message (message, &id, t);
5746 * Core handler for mesh keepalives.
5748 * @param cls closure
5749 * @param message message
5750 * @param peer peer identity this notification is about
5751 * @param atsi performance data
5752 * @param atsi_count number of records in 'atsi'
5753 * @return GNUNET_OK to keep the connection open,
5754 * GNUNET_SYSERR to close it (signal serious error)
5756 * TODO: Check who we got this from, to validate route.
5759 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5760 const struct GNUNET_MessageHeader *message,
5761 const struct GNUNET_ATS_Information *atsi,
5762 unsigned int atsi_count)
5764 struct GNUNET_MESH_TunnelKeepAlive *msg;
5765 struct MeshTunnel *t;
5767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5770 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5771 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5775 /* TODO notify that we dont know that tunnel */
5776 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5777 GNUNET_break_op (0);
5781 tunnel_reset_timeout (t);
5783 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5784 tunnel_send_multicast (t, message, GNUNET_NO);
5791 * Functions to handle messages from core
5793 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5794 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5795 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5796 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5797 sizeof (struct GNUNET_MESH_PathBroken)},
5798 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5799 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5800 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5801 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5802 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5803 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5804 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5805 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5806 sizeof (struct GNUNET_MESH_ACK)},
5807 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5808 sizeof (struct GNUNET_MESH_PathACK)},
5814 /******************************************************************************/
5815 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5816 /******************************************************************************/
5819 * deregister_app: iterator for removing each application registered by a client
5821 * @param cls closure
5822 * @param key the hash of the application id (used to access the hashmap)
5823 * @param value the value stored at the key (client)
5825 * @return GNUNET_OK on success
5828 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5830 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5831 GNUNET_break (GNUNET_YES ==
5832 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5838 * notify_client_connection_failure: notify a client that the connection to the
5839 * requested remote peer is not possible (for instance, no route found)
5840 * Function called when the socket is ready to queue more data. "buf" will be
5841 * NULL and "size" zero if the socket was closed for writing in the meantime.
5843 * @param cls closure
5844 * @param size number of bytes available in buf
5845 * @param buf where the callee should write the message
5846 * @return number of bytes written to buf
5849 notify_client_connection_failure (void *cls, size_t size, void *buf)
5852 struct MeshPeerInfo *peer_info;
5853 struct GNUNET_MESH_PeerControl *msg;
5854 struct GNUNET_PeerIdentity id;
5856 if (0 == size && NULL == buf)
5858 // TODO retry? cancel?
5862 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5863 peer_info = (struct MeshPeerInfo *) cls;
5864 msg = (struct GNUNET_MESH_PeerControl *) buf;
5865 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5866 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5867 // msg->tunnel_id = htonl(peer_info->t->tid);
5868 GNUNET_PEER_resolve (peer_info->id, &id);
5869 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5877 * Send keepalive packets for a peer
5879 * @param cls Closure (tunnel for which to send the keepalive).
5880 * @param tc Notification context.
5883 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5885 struct MeshTunnel *t = cls;
5886 struct GNUNET_MESH_TunnelKeepAlive *msg;
5887 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5890 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5891 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5897 "sending keepalive for tunnel %d\n", t->id.tid);
5899 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5900 msg->header.size = htons (size);
5901 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5902 msg->oid = my_full_id;
5903 msg->tid = htonl (t->id.tid);
5904 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5906 t->path_refresh_task =
5907 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5913 * Function to process paths received for a new peer addition. The recorded
5914 * paths form the initial tunnel, which can be optimized later.
5915 * Called on each result obtained for the DHT search.
5917 * @param cls closure
5918 * @param exp when will this value expire
5919 * @param key key of the result
5920 * @param get_path path of the get request
5921 * @param get_path_length lenght of get_path
5922 * @param put_path path of the put request
5923 * @param put_path_length length of the put_path
5924 * @param type type of the result
5925 * @param size number of bytes in data
5926 * @param data pointer to the result data
5928 * TODO: re-issue the request after certain time? cancel after X results?
5931 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5932 const struct GNUNET_HashCode * key,
5933 const struct GNUNET_PeerIdentity *get_path,
5934 unsigned int get_path_length,
5935 const struct GNUNET_PeerIdentity *put_path,
5936 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5937 size_t size, const void *data)
5939 struct MeshPathInfo *path_info = cls;
5940 struct MeshPeerPath *p;
5941 struct GNUNET_PeerIdentity pi;
5944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5945 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5948 p = path_build_from_dht (get_path, get_path_length, put_path,
5950 path_add_to_peers (p, GNUNET_NO);
5952 for (i = 0; i < path_info->peer->ntunnels; i++)
5954 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5955 peer_info_connect (path_info->peer, path_info->t);
5963 * Function to process paths received for a new peer addition. The recorded
5964 * paths form the initial tunnel, which can be optimized later.
5965 * Called on each result obtained for the DHT search.
5967 * @param cls closure
5968 * @param exp when will this value expire
5969 * @param key key of the result
5970 * @param get_path path of the get request
5971 * @param get_path_length lenght of get_path
5972 * @param put_path path of the put request
5973 * @param put_path_length length of the put_path
5974 * @param type type of the result
5975 * @param size number of bytes in data
5976 * @param data pointer to the result data
5979 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5980 const struct GNUNET_HashCode * key,
5981 const struct GNUNET_PeerIdentity *get_path,
5982 unsigned int get_path_length,
5983 const struct GNUNET_PeerIdentity *put_path,
5984 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5985 size_t size, const void *data)
5987 const struct PBlock *pb = data;
5988 const struct GNUNET_PeerIdentity *pi = &pb->id;
5989 struct MeshTunnel *t = cls;
5990 struct MeshPeerInfo *peer_info;
5991 struct MeshPeerPath *p;
5993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5994 if (size != sizeof (struct PBlock))
5996 GNUNET_break_op (0);
5999 if (ntohl(pb->type) != t->type)
6001 GNUNET_break_op (0);
6004 GNUNET_assert (NULL != t->owner);
6005 peer_info = peer_info_get (pi);
6006 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6008 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6010 p = path_build_from_dht (get_path, get_path_length, put_path,
6012 path_add_to_peers (p, GNUNET_NO);
6014 tunnel_add_peer (t, peer_info);
6015 peer_info_connect (peer_info, t);
6020 * Function to process DHT string to regex matching.
6021 * Called on each result obtained for the DHT search.
6023 * @param cls closure (search context)
6024 * @param exp when will this value expire
6025 * @param key key of the result
6026 * @param get_path path of the get request (not used)
6027 * @param get_path_length lenght of get_path (not used)
6028 * @param put_path path of the put request (not used)
6029 * @param put_path_length length of the put_path (not used)
6030 * @param type type of the result
6031 * @param size number of bytes in data
6032 * @param data pointer to the result data
6035 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6036 const struct GNUNET_HashCode * key,
6037 const struct GNUNET_PeerIdentity *get_path,
6038 unsigned int get_path_length,
6039 const struct GNUNET_PeerIdentity *put_path,
6040 unsigned int put_path_length,
6041 enum GNUNET_BLOCK_Type type,
6042 size_t size, const void *data)
6044 const struct MeshRegexAccept *block = data;
6045 struct MeshRegexSearchContext *ctx = cls;
6046 struct MeshRegexSearchInfo *info = ctx->info;
6047 struct MeshPeerPath *p;
6048 struct MeshPeerInfo *peer_info;
6050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6053 peer_info = peer_info_get(&block->id);
6054 p = path_build_from_dht (get_path, get_path_length, put_path,
6056 path_add_to_peers (p, GNUNET_NO);
6059 tunnel_add_peer (info->t, peer_info);
6060 peer_info_connect (peer_info, info->t);
6061 if (0 == info->peer)
6063 info->peer = peer_info->id;
6067 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6070 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6071 ®ex_connect_timeout,
6079 * Function to process DHT string to regex matching.
6080 * Called on each result obtained for the DHT search.
6082 * @param cls closure (search context)
6083 * @param exp when will this value expire
6084 * @param key key of the result
6085 * @param get_path path of the get request (not used)
6086 * @param get_path_length lenght of get_path (not used)
6087 * @param put_path path of the put request (not used)
6088 * @param put_path_length length of the put_path (not used)
6089 * @param type type of the result
6090 * @param size number of bytes in data
6091 * @param data pointer to the result data
6093 * TODO: re-issue the request after certain time? cancel after X results?
6096 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6097 const struct GNUNET_HashCode * key,
6098 const struct GNUNET_PeerIdentity *get_path,
6099 unsigned int get_path_length,
6100 const struct GNUNET_PeerIdentity *put_path,
6101 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6102 size_t size, const void *data)
6104 const struct MeshRegexBlock *block = data;
6105 struct MeshRegexSearchContext *ctx = cls;
6106 struct MeshRegexSearchInfo *info = ctx->info;
6110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6111 "DHT GET STRING RETURNED RESULTS\n");
6112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6113 " key: %s\n", GNUNET_h2s (key));
6115 copy = GNUNET_malloc (size);
6116 memcpy (copy, data, size);
6117 GNUNET_break (GNUNET_OK ==
6118 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6119 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6120 len = ntohl (block->n_proof);
6122 char proof[len + 1];
6124 memcpy (proof, &block[1], len);
6126 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6128 GNUNET_break_op (0);
6132 len = strlen (info->description);
6133 if (len == ctx->position) // String processed
6135 if (GNUNET_YES == ntohl (block->accepting))
6137 regex_find_path(key, ctx);
6141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6142 // FIXME REGEX this block not successful, wait for more? start timeout?
6147 regex_next_edge (block, size, ctx);
6152 /******************************************************************************/
6153 /********************* MESH LOCAL HANDLES **************************/
6154 /******************************************************************************/
6158 * Handler for client disconnection
6160 * @param cls closure
6161 * @param client identification of the client; NULL
6162 * for the last call when the server is destroyed
6165 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6167 struct MeshClient *c;
6168 struct MeshClient *next;
6171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6180 if (c->handle != client)
6182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6188 GNUNET_SERVER_client_drop (c->handle);
6189 c->shutting_down = GNUNET_YES;
6190 GNUNET_assert (NULL != c->own_tunnels);
6191 GNUNET_assert (NULL != c->incoming_tunnels);
6192 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6193 &tunnel_destroy_iterator, c);
6194 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6195 &tunnel_destroy_iterator, c);
6196 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6197 &tunnel_destroy_iterator, c);
6198 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6199 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6200 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6202 /* deregister clients applications */
6203 if (NULL != c->apps)
6205 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6206 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6208 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6209 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6211 GNUNET_SCHEDULER_cancel (announce_applications_task);
6212 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6214 if (NULL != c->types)
6215 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6216 for (i = 0; i < c->n_regex; i++)
6218 GNUNET_free (c->regexes[i]);
6220 GNUNET_free_non_null (c->regexes);
6221 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6222 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6224 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6227 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6236 * Handler for new clients
6238 * @param cls closure
6239 * @param client identification of the client
6240 * @param message the actual message, which includes messages the client wants
6243 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6244 const struct GNUNET_MessageHeader *message)
6246 struct GNUNET_MESH_ClientConnect *cc_msg;
6247 struct MeshClient *c;
6248 GNUNET_MESH_ApplicationType *a;
6255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6256 /* Check data sanity */
6257 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6258 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6259 ntypes = ntohs (cc_msg->types);
6260 napps = ntohs (cc_msg->applications);
6262 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6269 /* Create new client structure */
6270 c = GNUNET_malloc (sizeof (struct MeshClient));
6271 c->id = next_client_id++;
6272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6274 GNUNET_SERVER_client_keep (client);
6275 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6278 GNUNET_MESH_ApplicationType at;
6279 struct GNUNET_HashCode hc;
6281 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6282 for (i = 0; i < napps; i++)
6285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6286 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6287 /* store in clients hashmap */
6288 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6289 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6290 /* store in global hashmap, for announcements */
6291 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6294 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6295 announce_applications_task =
6296 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6302 struct GNUNET_HashCode hc;
6304 t = (uint16_t *) & a[napps];
6305 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6306 for (i = 0; i < ntypes; i++)
6309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6310 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6312 /* store in clients hashmap */
6313 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6315 /* store in global hashmap */
6316 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6317 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6321 " client has %u+%u subscriptions\n", napps, ntypes);
6323 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6324 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6325 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6326 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6327 GNUNET_SERVER_notification_context_add (nc, client);
6328 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6330 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6336 * Handler for clients announcing available services by a regular expression.
6338 * @param cls closure
6339 * @param client identification of the client
6340 * @param message the actual message, which includes messages the client wants
6343 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6344 const struct GNUNET_MessageHeader *message)
6346 struct MeshClient *c;
6350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6352 /* Sanity check for client registration */
6353 if (NULL == (c = client_get (client)))
6356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6361 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6362 regex = GNUNET_malloc (len + 1);
6363 memcpy (regex, &message[1], len);
6365 GNUNET_array_append (c->regexes, c->n_regex, regex);
6366 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6368 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6374 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6380 * Handler for requests of new tunnels
6382 * @param cls closure
6383 * @param client identification of the client
6384 * @param message the actual message
6387 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6388 const struct GNUNET_MessageHeader *message)
6390 struct GNUNET_MESH_TunnelMessage *t_msg;
6391 struct MeshTunnel *t;
6392 struct MeshClient *c;
6393 MESH_TunnelNumber tid;
6395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6397 /* Sanity check for client registration */
6398 if (NULL == (c = client_get (client)))
6401 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6406 /* Message sanity check */
6407 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6410 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6414 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6415 /* Sanity check for tunnel numbering */
6416 tid = ntohl (t_msg->tunnel_id);
6417 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6423 /* Sanity check for duplicate tunnel IDs */
6424 if (NULL != tunnel_get_by_local_id (c, tid))
6427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6431 while (NULL != tunnel_get_by_pi (myid, next_tid))
6432 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6433 t = tunnel_new (myid, next_tid++, c, tid);
6436 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6441 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6443 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6444 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6447 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6453 * Handler for requests of deleting tunnels
6455 * @param cls closure
6456 * @param client identification of the client
6457 * @param message the actual message
6460 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6461 const struct GNUNET_MessageHeader *message)
6463 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6464 struct MeshClient *c;
6465 struct MeshTunnel *t;
6466 MESH_TunnelNumber tid;
6468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6469 "Got a DESTROY TUNNEL from client!\n");
6471 /* Sanity check for client registration */
6472 if (NULL == (c = client_get (client)))
6475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6480 /* Message sanity check */
6481 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6488 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6490 /* Retrieve tunnel */
6491 tid = ntohl (tunnel_msg->tunnel_id);
6492 t = tunnel_get_by_local_id(c, tid);
6495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6500 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6502 client_ignore_tunnel (c, t);
6504 // TODO: when to destroy incoming tunnel?
6505 if (t->nclients == 0)
6507 GNUNET_assert (GNUNET_YES ==
6508 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6510 GNUNET_assert (GNUNET_YES ==
6511 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6512 &my_full_id.hashPubKey,
6516 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6519 send_client_tunnel_disconnect(t, c);
6520 client_delete_tunnel(c, t);
6522 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6524 tunnel_send_destroy (t);
6525 t->destroy = GNUNET_YES;
6526 // The tunnel will be destroyed when the last message is transmitted.
6527 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6533 * Handler for requests of seeting tunnel's speed.
6535 * @param cls Closure (unused).
6536 * @param client Identification of the client.
6537 * @param message The actual message.
6540 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6541 const struct GNUNET_MessageHeader *message)
6543 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6544 struct MeshClient *c;
6545 struct MeshTunnel *t;
6546 MESH_TunnelNumber tid;
6548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6549 "Got a SPEED request from client!\n");
6551 /* Sanity check for client registration */
6552 if (NULL == (c = client_get (client)))
6555 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6561 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6563 /* Retrieve tunnel */
6564 tid = ntohl (tunnel_msg->tunnel_id);
6565 t = tunnel_get_by_local_id(c, tid);
6568 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6570 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6574 switch (ntohs(message->type))
6576 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6577 t->speed_min = GNUNET_YES;
6579 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6580 t->speed_min = GNUNET_NO;
6585 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6590 * Handler for requests of seeting tunnel's buffering policy.
6592 * @param cls Closure (unused).
6593 * @param client Identification of the client.
6594 * @param message The actual message.
6597 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6598 const struct GNUNET_MessageHeader *message)
6600 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6601 struct MeshClient *c;
6602 struct MeshTunnel *t;
6603 MESH_TunnelNumber tid;
6605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6606 "Got a BUFFER request from client!\n");
6608 /* Sanity check for client registration */
6609 if (NULL == (c = client_get (client)))
6612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6617 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6619 /* Retrieve tunnel */
6620 tid = ntohl (tunnel_msg->tunnel_id);
6621 t = tunnel_get_by_local_id(c, tid);
6624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6630 switch (ntohs(message->type))
6632 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6633 t->nobuffer = GNUNET_NO;
6635 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6636 t->nobuffer = GNUNET_YES;
6642 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6647 * Handler for connection requests to new peers
6649 * @param cls closure
6650 * @param client identification of the client
6651 * @param message the actual message (PeerControl)
6654 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6655 const struct GNUNET_MessageHeader *message)
6657 struct GNUNET_MESH_PeerControl *peer_msg;
6658 struct MeshPeerInfo *peer_info;
6659 struct MeshClient *c;
6660 struct MeshTunnel *t;
6661 MESH_TunnelNumber tid;
6663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6664 /* Sanity check for client registration */
6665 if (NULL == (c = client_get (client)))
6668 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6673 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6675 /* Sanity check for message size */
6676 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6683 /* Tunnel exists? */
6684 tid = ntohl (peer_msg->tunnel_id);
6685 t = tunnel_get_by_local_id (c, tid);
6689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6693 /* Does client own tunnel? */
6694 if (t->owner->handle != client)
6697 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6701 GNUNET_i2s (&peer_msg->peer));
6702 peer_info = peer_info_get (&peer_msg->peer);
6704 tunnel_add_peer (t, peer_info);
6705 peer_info_connect (peer_info, t);
6707 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6713 * Handler for disconnection requests of peers in a tunnel
6715 * @param cls closure
6716 * @param client identification of the client
6717 * @param message the actual message (PeerControl)
6720 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6721 const struct GNUNET_MessageHeader *message)
6723 struct GNUNET_MESH_PeerControl *peer_msg;
6724 struct MeshPeerInfo *peer_info;
6725 struct MeshClient *c;
6726 struct MeshTunnel *t;
6727 MESH_TunnelNumber tid;
6729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6730 /* Sanity check for client registration */
6731 if (NULL == (c = client_get (client)))
6734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6739 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6741 /* Sanity check for message size */
6742 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6745 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6749 /* Tunnel exists? */
6750 tid = ntohl (peer_msg->tunnel_id);
6751 t = tunnel_get_by_local_id (c, tid);
6755 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6760 /* Does client own tunnel? */
6761 if (t->owner->handle != client)
6764 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6769 GNUNET_i2s (&peer_msg->peer));
6770 /* Is the peer in the tunnel? */
6772 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6773 if (NULL == peer_info)
6776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6780 /* Ok, delete peer from tunnel */
6781 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6782 &peer_msg->peer.hashPubKey);
6784 send_destroy_path (t, peer_info->id);
6785 tunnel_delete_peer (t, peer_info->id);
6786 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6791 * Handler for blacklist requests of peers in a tunnel
6793 * @param cls closure
6794 * @param client identification of the client
6795 * @param message the actual message (PeerControl)
6797 * FIXME implement DHT block bloomfilter
6800 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6801 const struct GNUNET_MessageHeader *message)
6803 struct GNUNET_MESH_PeerControl *peer_msg;
6804 struct MeshClient *c;
6805 struct MeshTunnel *t;
6806 MESH_TunnelNumber tid;
6808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6809 /* Sanity check for client registration */
6810 if (NULL == (c = client_get (client)))
6813 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6818 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6820 /* Sanity check for message size */
6821 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6824 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6828 /* Tunnel exists? */
6829 tid = ntohl (peer_msg->tunnel_id);
6830 t = tunnel_get_by_local_id (c, tid);
6834 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6839 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6840 GNUNET_PEER_intern(&peer_msg->peer));
6845 * Handler for unblacklist requests of peers in a tunnel
6847 * @param cls closure
6848 * @param client identification of the client
6849 * @param message the actual message (PeerControl)
6852 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6853 const struct GNUNET_MessageHeader *message)
6855 struct GNUNET_MESH_PeerControl *peer_msg;
6856 struct MeshClient *c;
6857 struct MeshTunnel *t;
6858 MESH_TunnelNumber tid;
6862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6863 /* Sanity check for client registration */
6864 if (NULL == (c = client_get (client)))
6867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6872 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6874 /* Sanity check for message size */
6875 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6878 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6882 /* Tunnel exists? */
6883 tid = ntohl (peer_msg->tunnel_id);
6884 t = tunnel_get_by_local_id (c, tid);
6888 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6893 /* if peer is not known, complain */
6894 pid = GNUNET_PEER_search (&peer_msg->peer);
6901 /* search and remove from list */
6902 for (i = 0; i < t->nblacklisted; i++)
6904 if (t->blacklisted[i] == pid)
6906 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6907 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6912 /* if peer hasn't been blacklisted, complain */
6918 * Handler for connection requests to new peers by type
6920 * @param cls closure
6921 * @param client identification of the client
6922 * @param message the actual message (ConnectPeerByType)
6925 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6926 const struct GNUNET_MessageHeader *message)
6928 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6929 struct MeshClient *c;
6930 struct MeshTunnel *t;
6931 struct GNUNET_HashCode hash;
6932 MESH_TunnelNumber tid;
6934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6935 /* Sanity check for client registration */
6936 if (NULL == (c = client_get (client)))
6939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6944 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6946 /* Sanity check for message size */
6947 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6948 ntohs (connect_msg->header.size))
6951 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6955 /* Tunnel exists? */
6956 tid = ntohl (connect_msg->tunnel_id);
6957 t = tunnel_get_by_local_id (c, tid);
6961 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6965 /* Does client own tunnel? */
6966 if (t->owner->handle != client)
6969 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6973 /* Do WE have the service? */
6974 t->type = ntohl (connect_msg->type);
6975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6976 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6977 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6980 /* Yes! Fast forward, add ourselves to the tunnel and send the
6981 * good news to the client, and alert the destination client of
6982 * an incoming tunnel.
6984 * FIXME send a path create to self, avoid code duplication
6986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6987 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6988 peer_info_get (&my_full_id),
6989 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6992 send_client_peer_connected (t, myid);
6993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6994 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6996 t->local_tid_dest = next_local_tid++;
6997 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6998 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6999 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7003 /* Ok, lets find a peer offering the service */
7004 if (NULL != t->dht_get_type)
7006 GNUNET_DHT_get_stop (t->dht_get_type);
7008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7009 GNUNET_h2s (&hash));
7011 GNUNET_DHT_get_start (dht_handle,
7012 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7014 dht_replication_level,
7015 GNUNET_DHT_RO_RECORD_ROUTE |
7016 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7018 &dht_get_type_handler, t);
7020 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7026 * Handler for connection requests to new peers by a string service description.
7028 * @param cls closure
7029 * @param client identification of the client
7030 * @param message the actual message, which includes messages the client wants
7033 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7034 const struct GNUNET_MessageHeader *message)
7036 struct GNUNET_MESH_ConnectPeerByString *msg;
7037 struct MeshRegexSearchContext *ctx;
7038 struct MeshRegexSearchInfo *info;
7039 struct GNUNET_DHT_GetHandle *get_h;
7040 struct GNUNET_HashCode key;
7041 struct MeshTunnel *t;
7042 struct MeshClient *c;
7043 MESH_TunnelNumber tid;
7047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7048 "Connect by string started\n");
7049 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7050 size = htons (message->size);
7052 /* Sanity check for client registration */
7053 if (NULL == (c = client_get (client)))
7056 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7061 /* Message size sanity check */
7062 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7065 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7069 /* Tunnel exists? */
7070 tid = ntohl (msg->tunnel_id);
7071 t = tunnel_get_by_local_id (c, tid);
7075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7079 /* Does client own tunnel? */
7080 if (t->owner->handle != client)
7083 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7088 " on tunnel %s [%u]\n",
7089 GNUNET_i2s(&my_full_id),
7092 /* Only one connect_by_string allowed at the same time! */
7093 /* FIXME: allow more, return handle at api level to cancel, document */
7094 if (NULL != t->regex_ctx)
7097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7101 /* Find string itself */
7102 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7103 string = (const char *) &msg[1];
7105 /* Initialize context */
7106 size = GNUNET_REGEX_get_first_key(string, len, &key);
7107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7108 " consumed %u bits out of %u\n", size, len);
7109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7110 " looking for %s\n", GNUNET_h2s (&key));
7112 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7114 info->description = GNUNET_malloc (len + 1);
7115 memcpy (info->description, string, len);
7116 info->description[len] = '\0';
7117 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7118 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7121 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7122 ctx->position = size;
7126 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7128 /* Start search in DHT */
7129 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7130 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7131 &key, /* key to search */
7132 dht_replication_level, /* replication level */
7133 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7134 NULL, /* xquery */ // FIXME BLOOMFILTER
7135 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7136 &dht_get_string_handler, ctx);
7138 GNUNET_break (GNUNET_OK ==
7139 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7144 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7150 * Handler for client traffic directed to one peer
7152 * @param cls closure
7153 * @param client identification of the client
7154 * @param message the actual message
7157 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7158 const struct GNUNET_MessageHeader *message)
7160 struct MeshClient *c;
7161 struct MeshTunnel *t;
7162 struct MeshPeerInfo *pi;
7163 struct GNUNET_MESH_Unicast *data_msg;
7164 MESH_TunnelNumber tid;
7167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7168 "Got a unicast request from a client!\n");
7170 /* Sanity check for client registration */
7171 if (NULL == (c = client_get (client)))
7174 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7179 data_msg = (struct GNUNET_MESH_Unicast *) message;
7181 /* Sanity check for message size */
7182 size = ntohs (message->size);
7183 if (sizeof (struct GNUNET_MESH_Unicast) +
7184 sizeof (struct GNUNET_MessageHeader) > size)
7187 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7191 /* Tunnel exists? */
7192 tid = ntohl (data_msg->tid);
7193 t = tunnel_get_by_local_id (c, tid);
7197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7201 /* Is it a local tunnel? Then, does client own the tunnel? */
7202 if (t->owner->handle != client)
7205 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7209 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7210 &data_msg->destination.hashPubKey);
7211 /* Is the selected peer in the tunnel? */
7215 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7219 /* PID should be as expected */
7220 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7224 "Unicast PID, expected %u, got %u\n",
7225 t->fwd_pid + 1, ntohl (data_msg->pid));
7226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7230 /* Ok, everything is correct, send the message
7231 * (pretend we got it from a mesh peer)
7234 /* Work around const limitation */
7235 char buf[ntohs (message->size)] GNUNET_ALIGN;
7236 struct GNUNET_MESH_Unicast *copy;
7238 copy = (struct GNUNET_MESH_Unicast *) buf;
7239 memcpy (buf, data_msg, size);
7240 copy->oid = my_full_id;
7241 copy->tid = htonl (t->id.tid);
7242 copy->ttl = htonl (default_ttl);
7243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7244 " calling generic handler...\n");
7245 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7248 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7255 * Handler for client traffic directed to the origin
7257 * @param cls closure
7258 * @param client identification of the client
7259 * @param message the actual message
7262 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7263 const struct GNUNET_MessageHeader *message)
7265 struct GNUNET_MESH_ToOrigin *data_msg;
7266 struct MeshTunnelClientInfo *clinfo;
7267 struct MeshClient *c;
7268 struct MeshTunnel *t;
7269 MESH_TunnelNumber tid;
7272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7273 "Got a ToOrigin request from a client!\n");
7274 /* Sanity check for client registration */
7275 if (NULL == (c = client_get (client)))
7278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7283 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7285 /* Sanity check for message size */
7286 size = ntohs (message->size);
7287 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7288 sizeof (struct GNUNET_MessageHeader) > size)
7291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7295 /* Tunnel exists? */
7296 tid = ntohl (data_msg->tid);
7297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7298 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7301 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7304 t = tunnel_get_by_local_id (c, tid);
7307 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7314 /* It should be sent by someone who has this as incoming tunnel. */
7315 if (GNUNET_NO == client_knows_tunnel (c, t))
7318 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7322 /* PID should be as expected */
7323 clinfo = tunnel_get_client_fc (t, c);
7324 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7327 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7328 "To Origin PID, expected %u, got %u\n",
7329 clinfo->bck_pid + 1,
7330 ntohl (data_msg->pid));
7331 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7335 /* Ok, everything is correct, send the message
7336 * (pretend we got it from a mesh peer)
7340 char buf[ntohs (message->size)] GNUNET_ALIGN;
7341 struct GNUNET_MESH_ToOrigin *copy;
7343 /* Work around const limitation */
7344 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7345 memcpy (buf, data_msg, size);
7346 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7347 copy->tid = htonl (t->id.tid);
7348 copy->ttl = htonl (default_ttl);
7349 if (ntohl (copy->pid) != (t->bck_pid + 1))
7352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7353 "To Origin PID, expected %u, got %u\n",
7359 copy->sender = my_full_id;
7360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7361 " calling generic handler...\n");
7362 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7364 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7371 * Handler for client traffic directed to all peers in a tunnel
7373 * @param cls closure
7374 * @param client identification of the client
7375 * @param message the actual message
7378 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7379 const struct GNUNET_MessageHeader *message)
7381 struct MeshClient *c;
7382 struct MeshTunnel *t;
7383 struct GNUNET_MESH_Multicast *data_msg;
7384 MESH_TunnelNumber tid;
7386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7387 "Got a multicast request from a client!\n");
7389 /* Sanity check for client registration */
7390 if (NULL == (c = client_get (client)))
7393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7398 data_msg = (struct GNUNET_MESH_Multicast *) message;
7400 /* Sanity check for message size */
7401 if (sizeof (struct GNUNET_MESH_Multicast) +
7402 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7409 /* Tunnel exists? */
7410 tid = ntohl (data_msg->tid);
7411 t = tunnel_get_by_local_id (c, tid);
7415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7418 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7422 /* Does client own tunnel? */
7423 if (t->owner->handle != client)
7426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7430 /* PID should be as expected */
7431 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7434 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7435 "Multicast PID, expected %u, got %u\n",
7436 t->fwd_pid + 1, ntohl (data_msg->pid));
7437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7442 char buf[ntohs (message->size)] GNUNET_ALIGN;
7443 struct GNUNET_MESH_Multicast *copy;
7445 copy = (struct GNUNET_MESH_Multicast *) buf;
7446 memcpy (buf, message, ntohs (message->size));
7447 copy->oid = my_full_id;
7448 copy->tid = htonl (t->id.tid);
7449 copy->ttl = htonl (default_ttl);
7450 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7452 " calling generic handler...\n");
7453 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7456 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7462 * Handler for client's ACKs for payload traffic.
7464 * @param cls Closure (unused).
7465 * @param client Identification of the client.
7466 * @param message The actual message.
7469 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7470 const struct GNUNET_MessageHeader *message)
7472 struct GNUNET_MESH_LocalAck *msg;
7473 struct MeshTunnel *t;
7474 struct MeshClient *c;
7475 MESH_TunnelNumber tid;
7478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7479 /* Sanity check for client registration */
7480 if (NULL == (c = client_get (client)))
7483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7488 msg = (struct GNUNET_MESH_LocalAck *) message;
7490 /* Tunnel exists? */
7491 tid = ntohl (msg->tunnel_id);
7492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7493 t = tunnel_get_by_local_id (c, tid);
7497 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7498 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7503 ack = ntohl (msg->max_pid);
7504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7506 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7507 if (NULL != t->owner && t->owner->handle == client)
7509 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7511 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7515 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7516 tunnel_set_client_fwd_ack (t, c, ack);
7517 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7520 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7527 * Functions to handle messages from clients
7529 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7530 {&handle_local_new_client, NULL,
7531 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7532 {&handle_local_announce_regex, NULL,
7533 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7534 {&handle_local_tunnel_create, NULL,
7535 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7536 sizeof (struct GNUNET_MESH_TunnelMessage)},
7537 {&handle_local_tunnel_destroy, NULL,
7538 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7539 sizeof (struct GNUNET_MESH_TunnelMessage)},
7540 {&handle_local_tunnel_speed, NULL,
7541 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7542 sizeof (struct GNUNET_MESH_TunnelMessage)},
7543 {&handle_local_tunnel_speed, NULL,
7544 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7545 sizeof (struct GNUNET_MESH_TunnelMessage)},
7546 {&handle_local_tunnel_buffer, NULL,
7547 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7548 sizeof (struct GNUNET_MESH_TunnelMessage)},
7549 {&handle_local_tunnel_buffer, NULL,
7550 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7551 sizeof (struct GNUNET_MESH_TunnelMessage)},
7552 {&handle_local_connect_add, NULL,
7553 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7554 sizeof (struct GNUNET_MESH_PeerControl)},
7555 {&handle_local_connect_del, NULL,
7556 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7557 sizeof (struct GNUNET_MESH_PeerControl)},
7558 {&handle_local_blacklist, NULL,
7559 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7560 sizeof (struct GNUNET_MESH_PeerControl)},
7561 {&handle_local_unblacklist, NULL,
7562 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7563 sizeof (struct GNUNET_MESH_PeerControl)},
7564 {&handle_local_connect_by_type, NULL,
7565 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7566 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7567 {&handle_local_connect_by_string, NULL,
7568 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7569 {&handle_local_unicast, NULL,
7570 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7571 {&handle_local_to_origin, NULL,
7572 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7573 {&handle_local_multicast, NULL,
7574 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7575 {&handle_local_ack, NULL,
7576 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7577 sizeof (struct GNUNET_MESH_LocalAck)},
7583 * To be called on core init/fail.
7585 * @param cls service closure
7586 * @param server handle to the server for this service
7587 * @param identity the public identity of this peer
7590 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7591 const struct GNUNET_PeerIdentity *identity)
7594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7595 core_handle = server;
7596 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7600 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7602 GNUNET_abort(); // Try harder
7609 * Method called whenever a given peer connects.
7611 * @param cls closure
7612 * @param peer peer identity this notification is about
7613 * @param atsi performance data for the connection
7614 * @param atsi_count number of records in 'atsi'
7617 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7618 const struct GNUNET_ATS_Information *atsi,
7619 unsigned int atsi_count)
7621 struct MeshPeerInfo *peer_info;
7622 struct MeshPeerPath *path;
7624 DEBUG_CONN ("Peer connected\n");
7625 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7626 peer_info = peer_info_get (peer);
7627 if (myid == peer_info->id)
7629 DEBUG_CONN (" (self)\n");
7634 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7636 path = path_new (2);
7637 path->peers[0] = myid;
7638 path->peers[1] = peer_info->id;
7639 GNUNET_PEER_change_rc (myid, 1);
7640 GNUNET_PEER_change_rc (peer_info->id, 1);
7641 peer_info_add_path (peer_info, path, GNUNET_YES);
7642 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7648 * Method called whenever a peer disconnects.
7650 * @param cls closure
7651 * @param peer peer identity this notification is about
7654 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7656 struct MeshPeerInfo *pi;
7657 struct MeshPeerQueue *q;
7658 struct MeshPeerQueue *n;
7660 DEBUG_CONN ("Peer disconnected\n");
7661 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7673 /* try to reroute this traffic instead */
7674 queue_destroy(q, GNUNET_YES);
7678 peer_info_remove_path (pi, pi->id, myid);
7681 DEBUG_CONN (" (self)\n");
7683 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7688 /******************************************************************************/
7689 /************************ MAIN FUNCTIONS ****************************/
7690 /******************************************************************************/
7693 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7695 * @param cls closure
7696 * @param key current key code
7697 * @param value value in the hash map
7698 * @return GNUNET_YES if we should continue to iterate,
7702 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7704 struct MeshTunnel *t = value;
7711 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7713 * @param cls closure
7714 * @param key current key code
7715 * @param value value in the hash map
7716 * @return GNUNET_YES if we should continue to iterate,
7720 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7722 struct MeshPeerInfo *p = value;
7723 struct MeshPeerQueue *q;
7724 struct MeshPeerQueue *n;
7732 queue_destroy(q, GNUNET_YES);
7736 peer_info_destroy (p);
7742 * Task run during shutdown.
7748 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7752 if (core_handle != NULL)
7754 GNUNET_CORE_disconnect (core_handle);
7759 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
7762 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7763 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7764 if (dht_handle != NULL)
7766 GNUNET_DHT_disconnect (dht_handle);
7771 GNUNET_SERVER_notification_context_destroy (nc);
7774 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7776 GNUNET_SCHEDULER_cancel (announce_id_task);
7777 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7784 * Callback for hostkey read/generation
7787 * @param pk the private key
7788 * @param emsg error message
7791 key_generation_cb (void *cls,
7792 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
7795 struct MeshPeerInfo *peer;
7796 struct MeshPeerPath *p;
7801 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7802 _("Mesh service could not access hostkey. Exiting.\n"));
7803 GNUNET_SCHEDULER_shutdown ();
7806 my_private_key = pk;
7807 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7808 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7809 &my_full_id.hashPubKey);
7810 myid = GNUNET_PEER_intern (&my_full_id);
7811 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7812 "Mesh for peer [%s] starting\n",
7813 GNUNET_i2s(&my_full_id));
7815 // transport_handle = GNUNET_TRANSPORT_connect(c,
7825 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7828 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7829 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7830 GNUNET_SERVER_disconnect_notify (server_handle,
7831 &handle_local_client_disconnect, NULL);
7835 clients_tail = NULL;
7838 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7839 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7841 /* Create a peer_info for the local peer */
7842 peer = peer_info_get (&my_full_id);
7845 GNUNET_PEER_change_rc (myid, 1);
7846 peer_info_add_path (peer, p, GNUNET_YES);
7847 GNUNET_SERVER_resume (server_handle);
7848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7853 * Process mesh requests.
7855 * @param cls closure
7856 * @param server the initialized server
7857 * @param c configuration to use
7860 run (void *cls, struct GNUNET_SERVER_Handle *server,
7861 const struct GNUNET_CONFIGURATION_Handle *c)
7865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7866 server_handle = server;
7867 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7868 NULL, /* Closure passed to MESH functions */
7869 &core_init, /* Call core_init once connected */
7870 &core_connect, /* Handle connects */
7871 &core_disconnect, /* remove peers on disconnects */
7872 NULL, /* Don't notify about all incoming messages */
7873 GNUNET_NO, /* For header only in notification */
7874 NULL, /* Don't notify about all outbound messages */
7875 GNUNET_NO, /* For header-only out notification */
7876 core_handlers); /* Register these handlers */
7878 if (core_handle == NULL)
7881 GNUNET_SCHEDULER_shutdown ();
7886 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7891 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7893 GNUNET_SCHEDULER_shutdown ();
7898 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7899 &refresh_path_time))
7901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7903 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7904 "refresh path time");
7905 GNUNET_SCHEDULER_shutdown ();
7910 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7911 &app_announce_time))
7913 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7915 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7916 "app announce time");
7917 GNUNET_SCHEDULER_shutdown ();
7922 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7925 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7927 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7928 "id announce time");
7929 GNUNET_SCHEDULER_shutdown ();
7934 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7935 &unacknowledged_wait_time))
7937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7939 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7940 "unacknowledged wait time");
7941 GNUNET_SCHEDULER_shutdown ();
7946 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7949 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7951 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7953 GNUNET_SCHEDULER_shutdown ();
7958 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
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_TUNNELS",
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", "DEFAULT_TTL",
7985 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7987 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7993 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7994 &dht_replication_level))
7996 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7998 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7999 "dht replication level", 10);
8000 dht_replication_level = 10;
8003 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8004 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8005 peers = GNUNET_CONTAINER_multihashmap_create (32);
8006 applications = GNUNET_CONTAINER_multihashmap_create (32);
8007 types = GNUNET_CONTAINER_multihashmap_create (32);
8009 dht_handle = GNUNET_DHT_connect (c, 64);
8010 if (NULL == dht_handle)
8014 stats = GNUNET_STATISTICS_create ("mesh", c);
8016 GNUNET_SERVER_suspend (server_handle);
8017 /* Scheduled the task to clean up when shutdown is called */
8018 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8020 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8021 GNUNET_free (keyfile);
8026 * The main function for the mesh service.
8028 * @param argc number of arguments from the command line
8029 * @param argv command line arguments
8030 * @return 0 ok, 1 on error
8033 main (int argc, char *const *argv)
8037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8040 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8046 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8047 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8048 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);