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 cls Closure (context of the search).
1323 * @param token Token that follows to next state.
1324 * @param len Lenght of token.
1325 * @param key Hash of next state.
1327 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1330 regex_next_edge (const struct MeshRegexBlock *block,
1332 struct MeshRegexSearchContext *ctx)
1334 struct MeshRegexSearchContext *new_ctx;
1335 struct MeshRegexSearchInfo *info = ctx->info;
1336 struct GNUNET_DHT_GetHandle *get_h;
1338 GNUNET_break (GNUNET_OK ==
1339 GNUNET_MESH_regex_block_iterate (block, size,
1340 ®ex_edge_iterator, ctx));
1342 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1343 new_ctx->info = info;
1344 new_ctx->position = ctx->position + ctx->longest_match;
1345 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1347 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1350 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1352 ®ex_result_iterator,
1354 return GNUNET_YES; // We are already looking for it
1356 /* Start search in DHT */
1358 GNUNET_DHT_get_start (dht_handle, /* handle */
1359 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1360 &ctx->hash, /* key to search */
1361 dht_replication_level, /* replication level */
1362 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1363 NULL, /* xquery */ // FIXME BLOOMFILTER
1364 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1365 &dht_get_string_handler, new_ctx);
1367 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1381 * Iterator over hash map entries to cancel DHT GET requests after a
1382 * successful connect_by_string.
1384 * @param cls Closure (unused).
1385 * @param key Current key code (unused).
1386 * @param value Value in the hash map (get handle).
1387 * @return GNUNET_YES if we should continue to iterate,
1391 regex_cancel_dht_get (void *cls,
1392 const struct GNUNET_HashCode * key,
1395 struct GNUNET_DHT_GetHandle *h = value;
1397 GNUNET_DHT_get_stop (h);
1403 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1404 * search for connect_by_string.
1406 * @param cls Closure (unused).
1407 * @param key Current key code (unused).
1408 * @param value MeshRegexBlock in the hash map.
1409 * @return GNUNET_YES if we should continue to iterate,
1413 regex_free_result (void *cls,
1414 const struct GNUNET_HashCode * key,
1418 GNUNET_free (value);
1424 * Regex callback iterator to store own service description in the DHT.
1426 * @param cls closure.
1427 * @param key hash for current state.
1428 * @param proof proof for current state.
1429 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1430 * @param num_edges number of edges leaving current state.
1431 * @param edges edges leaving current state.
1434 regex_iterator (void *cls,
1435 const struct GNUNET_HashCode *key,
1438 unsigned int num_edges,
1439 const struct GNUNET_REGEX_Edge *edges)
1441 struct MeshRegexBlock *block;
1442 struct MeshRegexEdge *block_edge;
1443 enum GNUNET_DHT_RouteOption opt;
1447 unsigned int offset;
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1451 " regex dht put for state %s\n",
1453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1460 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1461 if (GNUNET_YES == accepting)
1463 struct MeshRegexAccept block;
1465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1466 " state %s is accepting, putting own id\n",
1468 size = sizeof (block);
1470 block.id = my_full_id;
1472 GNUNET_DHT_put(dht_handle, key,
1473 dht_replication_level,
1474 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1475 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1478 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1483 len = strlen(proof);
1484 size = sizeof (struct MeshRegexBlock) + len;
1485 block = GNUNET_malloc (size);
1488 block->n_proof = htonl (len);
1489 block->n_edges = htonl (num_edges);
1490 block->accepting = htonl (accepting);
1492 /* Store the proof at the end of the block. */
1493 aux = (char *) &block[1];
1494 memcpy (aux, proof, len);
1497 /* Store each edge in a variable length MeshEdge struct at the
1498 * very end of the MeshRegexBlock structure.
1500 for (i = 0; i < num_edges; i++)
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 " edge %s towards %s\n",
1505 GNUNET_h2s(&edges[i].destination));
1507 /* aux points at the end of the last block */
1508 len = strlen (edges[i].label);
1509 size += sizeof (struct MeshRegexEdge) + len;
1510 // Calculate offset FIXME is this ok? use size instead?
1511 offset = aux - (char *) block;
1512 block = GNUNET_realloc (block, size);
1513 aux = &((char *) block)[offset];
1514 block_edge = (struct MeshRegexEdge *) aux;
1515 block_edge->key = edges[i].destination;
1516 block_edge->n_token = htonl (len);
1517 aux = (char *) &block_edge[1];
1518 memcpy (aux, edges[i].label, len);
1522 GNUNET_DHT_put(dht_handle, key,
1523 dht_replication_level,
1525 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1527 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1531 GNUNET_free (block);
1536 * Store the regular expression describing a local service into the DHT.
1538 * @param regex The regular expresion.
1541 regex_put (const char *regex)
1543 struct GNUNET_REGEX_Automaton *dfa;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1546 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1547 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1548 GNUNET_REGEX_automaton_destroy (dfa);
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1554 * Find a path to a peer that offers a regex servcie compatible
1555 * with a given string.
1557 * @param key The key of the accepting state.
1558 * @param ctx Context containing info about the string, tunnel, etc.
1561 regex_find_path (const struct GNUNET_HashCode *key,
1562 struct MeshRegexSearchContext *ctx)
1564 struct GNUNET_DHT_GetHandle *get_h;
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1567 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1568 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1569 key, /* key to search */
1570 dht_replication_level, /* replication level */
1571 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1572 GNUNET_DHT_RO_RECORD_ROUTE,
1573 NULL, /* xquery */ // FIXME BLOOMFILTER
1574 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1575 &dht_get_string_accept_handler, ctx);
1576 GNUNET_break (GNUNET_OK ==
1577 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1580 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1585 * Function called if the connect attempt to a peer found via
1586 * connect_by_string times out. Try to connect to another peer, if any.
1587 * Otherwise try to reconnect to the same peer.
1589 * @param cls Closure (info about regex search).
1590 * @param tc TaskContext.
1593 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1595 struct MeshRegexSearchInfo *info = cls;
1596 struct MeshPeerInfo *peer_info;
1600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1601 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1602 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1610 if (0 < info->n_peers)
1612 // Select next peer, put current in that spot.
1613 id = info->peers[info->i_peer];
1614 info->peers[info->i_peer] = info->peer;
1615 info->i_peer = (info->i_peer + 1) % info->n_peers;
1619 // Try to connect to same peer again.
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1624 peer_info = peer_info_get_short(id);
1625 tunnel_add_peer (info->t, peer_info);
1627 tunnel_delete_peer (info->t, old);
1628 peer_info_connect (peer_info, info->t);
1629 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1630 ®ex_connect_timeout,
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1637 * Cancel an ongoing regex search in the DHT and free all resources.
1639 * @param ctx The search context.
1642 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1644 struct MeshRegexSearchInfo *info = ctx->info;
1647 GNUNET_free (info->description);
1648 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1649 ®ex_cancel_dht_get, NULL);
1650 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1651 ®ex_free_result, NULL);
1652 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1653 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1654 info->t->regex_ctx = NULL;
1655 for (i = 0; i < info->n_contexts; i++)
1657 GNUNET_free (info->contexts[i]);
1659 if (0 < info->n_contexts)
1660 GNUNET_free (info->contexts);
1661 if (0 < info->n_peers)
1662 GNUNET_free (info->peers);
1663 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1665 GNUNET_SCHEDULER_cancel(info->timeout);
1671 /******************************************************************************/
1672 /************************ PERIODIC FUNCTIONS ****************************/
1673 /******************************************************************************/
1676 * Announce iterator over for each application provided by the peer
1678 * @param cls closure
1679 * @param key current key code
1680 * @param value value in the hash map
1681 * @return GNUNET_YES if we should continue to
1686 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1688 struct PBlock block;
1689 struct MeshClient *c;
1691 block.id = my_full_id;
1692 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1693 GNUNET_assert(NULL != c);
1694 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1695 if (0 == block.type)
1700 block.type = htonl (block.type);
1702 GNUNET_break (NULL !=
1703 GNUNET_DHT_put (dht_handle, key,
1704 dht_replication_level,
1705 GNUNET_DHT_RO_RECORD_ROUTE |
1706 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1707 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1709 (const char *) &block,
1710 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1712 app_announce_time, NULL, NULL));
1718 * Periodically announce what applications are provided by local clients
1721 * @param cls closure
1722 * @param tc task context
1725 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1727 struct MeshClient *c = cls;
1730 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1732 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1736 DEBUG_DHT ("Starting PUT for regex\n");
1738 for (i = 0; i < c->n_regex; i++)
1740 regex_put (c->regexes[i]);
1742 c->regex_announce_task =
1743 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1744 DEBUG_DHT ("Finished PUT for regex\n");
1751 * Periodically announce what applications are provided by local clients
1754 * @param cls closure
1755 * @param tc task context
1758 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1760 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1762 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1766 DEBUG_DHT ("Starting PUT for apps\n");
1768 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1770 announce_applications_task =
1771 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1773 DEBUG_DHT ("Finished PUT for apps\n");
1780 * Periodically announce self id in the DHT
1782 * @param cls closure
1783 * @param tc task context
1786 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1788 struct PBlock block;
1790 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1792 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1796 * - Set data expiration in function of X
1797 * - Adapt X to churn
1799 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1801 block.id = my_full_id;
1802 block.type = htonl (0);
1803 GNUNET_DHT_put (dht_handle, /* DHT handle */
1804 &my_full_id.hashPubKey, /* Key to use */
1805 dht_replication_level, /* Replication level */
1806 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1807 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1808 sizeof (block), /* Size of the data */
1809 (const char *) &block, /* Data itself */
1810 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1811 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1812 NULL, /* Continuation */
1813 NULL); /* Continuation closure */
1815 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1819 /******************************************************************************/
1820 /****************** GENERAL HELPER FUNCTIONS ************************/
1821 /******************************************************************************/
1824 * Decrements the reference counter and frees all resources if needed
1826 * @param mesh_data Data Descriptor used in a multicast message.
1827 * Freed no longer needed (last message).
1830 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1832 if (0 == --(mesh_data->reference_counter))
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1835 GNUNET_free (mesh_data->data);
1836 GNUNET_free (mesh_data);
1842 * Check if client has registered with the service and has not disconnected
1844 * @param client the client to check
1846 * @return non-NULL if client exists in the global DLL
1848 static struct MeshClient *
1849 client_get (struct GNUNET_SERVER_Client *client)
1851 struct MeshClient *c;
1856 if (c->handle == client)
1865 * Checks if a given client has subscribed to certain message type
1867 * @param message_type Type of message to check
1868 * @param c Client to check
1870 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1872 * FIXME: use of crypto_hash slows it down
1873 * The hash function alone takes 8-10us out of the ~55us for the whole
1874 * process of retransmitting the message from one local client to another.
1875 * Find faster implementation!
1878 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1880 struct GNUNET_HashCode hc;
1882 if (NULL == c->types)
1885 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1886 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1891 * Check whether client wants traffic from a tunnel.
1893 * @param c Client to check.
1894 * @param t Tunnel to be found.
1896 * @return GNUNET_YES if client knows tunnel.
1898 * TODO look in client hashmap
1901 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1905 for (i = 0; i < t->nclients; i++)
1906 if (t->clients[i] == c)
1913 * Check whether client has been informed about a tunnel.
1915 * @param c Client to check.
1916 * @param t Tunnel to be found.
1918 * @return GNUNET_YES if client knows tunnel.
1920 * TODO look in client hashmap
1923 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1927 for (i = 0; i < t->nignore; i++)
1928 if (t->ignore[i] == c)
1930 return client_wants_tunnel(c, t);
1935 * Marks a client as uninterested in traffic from the tunnel, updating both
1936 * client and tunnel to reflect this.
1938 * @param c Client that doesn't want traffic anymore.
1939 * @param t Tunnel which should be ignored.
1941 * FIXME when to delete an incoming tunnel?
1944 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1946 struct GNUNET_HashCode hash;
1948 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1949 GNUNET_break (GNUNET_YES ==
1950 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1952 GNUNET_break (GNUNET_YES ==
1953 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1954 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1955 tunnel_delete_active_client (t, c);
1956 GNUNET_array_append (t->ignore, t->nignore, c);
1961 * Deletes a tunnel from a client (either owner or destination). To be used on
1962 * tunnel destroy, otherwise, use client_ignore_tunnel.
1964 * @param c Client whose tunnel to delete.
1965 * @param t Tunnel which should be deleted.
1968 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1970 struct GNUNET_HashCode hash;
1974 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1975 GNUNET_assert (GNUNET_YES ==
1976 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1982 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1984 GNUNET_assert (GNUNET_YES ==
1985 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1989 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1997 * Send the message to all clients that have subscribed to its type
1999 * @param msg Pointer to the message itself
2000 * @param payload Pointer to the payload of the message.
2001 * @param t The tunnel to whose clients this message goes.
2003 * @return number of clients this message was sent to
2006 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2007 const struct GNUNET_MessageHeader *payload,
2008 struct MeshTunnel *t)
2010 struct MeshClient *c;
2011 MESH_TunnelNumber *tid;
2014 char cbuf[htons (msg->size)];
2016 type = ntohs (payload->type);
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2019 GNUNET_MESH_DEBUG_M2S (type));
2021 memcpy (cbuf, msg, sizeof (cbuf));
2022 switch (htons (msg->type))
2024 struct GNUNET_MESH_Unicast *uc;
2025 struct GNUNET_MESH_Multicast *mc;
2026 struct GNUNET_MESH_ToOrigin *to;
2028 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2029 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2032 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2033 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2036 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2037 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2045 for (count = 0, c = clients; c != NULL; c = c->next)
2047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2048 if (client_is_subscribed (type, c))
2050 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2054 *tid = htonl (t->local_tid);
2058 if (GNUNET_NO == client_knows_tunnel (c, t))
2060 /* This client doesn't know the tunnel */
2061 struct GNUNET_MESH_TunnelNotification tmsg;
2062 struct GNUNET_HashCode hash;
2064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2065 tmsg.header.size = htons (sizeof (tmsg));
2066 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2067 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2068 tmsg.tunnel_id = htonl (t->local_tid_dest);
2069 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2070 &tmsg.header, GNUNET_NO);
2071 tunnel_add_client (t, c);
2072 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2074 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2075 c->incoming_tunnels, &hash, t,
2076 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2078 *tid = htonl (t->local_tid_dest);
2081 /* Check if the client wants to get traffic from the tunnel */
2082 if (GNUNET_NO == client_wants_tunnel(c, t))
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2086 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2087 (struct GNUNET_MessageHeader
2088 *) cbuf, GNUNET_NO);
2097 * Notify the client that owns the tunnel that a peer has connected to it
2098 * (the requested path to it has been confirmed).
2100 * @param t Tunnel whose owner to notify
2101 * @param id Short id of the peer that has connected
2104 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2106 struct GNUNET_MESH_PeerControl pc;
2108 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2109 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2110 pc.tunnel_id = htonl (t->local_tid);
2111 GNUNET_PEER_resolve (id, &pc.peer);
2112 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2118 * Notify all clients (not depending on registration status) that the incoming
2119 * tunnel is no longer valid.
2121 * @param t Tunnel that was destroyed.
2124 send_clients_tunnel_destroy (struct MeshTunnel *t)
2126 struct GNUNET_MESH_TunnelMessage msg;
2128 msg.header.size = htons (sizeof (msg));
2129 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2130 msg.tunnel_id = htonl (t->local_tid_dest);
2131 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2136 * Notify clients of tunnel disconnections, if needed.
2137 * In case the origin disconnects, the destination clients get a tunnel destroy
2138 * notification. If the last destination disconnects (only one remaining client
2139 * in tunnel), the origin gets a (local ID) peer disconnected.
2140 * Note that the function must be called BEFORE removing the client from
2143 * @param t Tunnel that was destroyed.
2144 * @param c Client that disconnected.
2147 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2153 struct GNUNET_MESH_TunnelMessage msg;
2155 msg.header.size = htons (sizeof (msg));
2156 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2157 msg.tunnel_id = htonl (t->local_tid_dest);
2158 for (i = 0; i < t->nclients; i++)
2159 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2160 &msg.header, GNUNET_NO);
2162 // FIXME when to disconnect an incoming tunnel?
2163 else if (1 == t->nclients && NULL != t->owner)
2165 struct GNUNET_MESH_PeerControl msg;
2167 msg.header.size = htons (sizeof (msg));
2168 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2169 msg.tunnel_id = htonl (t->local_tid);
2170 msg.peer = my_full_id;
2171 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2172 &msg.header, GNUNET_NO);
2178 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2179 * and insert it in the appropiate structures if the peer is not known yet.
2181 * @param peer Full identity of the peer.
2183 * @return Existing or newly created peer info.
2185 static struct MeshPeerInfo *
2186 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2188 struct MeshPeerInfo *peer_info;
2190 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2191 if (NULL == peer_info)
2194 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2195 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2196 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2197 peer_info->id = GNUNET_PEER_intern (peer);
2205 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2206 * and insert it in the appropiate structures if the peer is not known yet.
2208 * @param peer Short identity of the peer.
2210 * @return Existing or newly created peer info.
2212 static struct MeshPeerInfo *
2213 peer_info_get_short (const GNUNET_PEER_Id peer)
2215 struct GNUNET_PeerIdentity id;
2217 GNUNET_PEER_resolve (peer, &id);
2218 return peer_info_get (&id);
2223 * Iterator to remove the tunnel from the list of tunnels a peer participates
2226 * @param cls Closure (tunnel info)
2227 * @param key GNUNET_PeerIdentity of the peer (unused)
2228 * @param value PeerInfo of the peer
2230 * @return always GNUNET_YES, to keep iterating
2233 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2235 struct MeshTunnel *t = cls;
2236 struct MeshPeerInfo *peer = value;
2239 for (i = 0; i < peer->ntunnels; i++)
2242 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2245 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2246 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2255 * Core callback to write a pre-constructed data packet to core buffer
2257 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2258 * @param size Number of bytes available in buf.
2259 * @param buf Where the to write the message.
2261 * @return number of bytes written to buf
2264 send_core_data_raw (void *cls, size_t size, void *buf)
2266 struct MeshTransmissionDescriptor *info = cls;
2267 struct GNUNET_MessageHeader *msg;
2270 GNUNET_assert (NULL != info);
2271 GNUNET_assert (NULL != info->mesh_data);
2272 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2273 total_size = ntohs (msg->size);
2275 if (total_size > size)
2280 memcpy (buf, msg, total_size);
2281 data_descriptor_decrement_rc (info->mesh_data);
2288 * Sends an already built non-multicast message to a peer,
2289 * properly registrating all used resources.
2291 * @param message Message to send. Function makes a copy of it.
2292 * @param peer Short ID of the neighbor whom to send the message.
2293 * @param t Tunnel on which this message is transmitted.
2296 send_message (const struct GNUNET_MessageHeader *message,
2297 const struct GNUNET_PeerIdentity *peer,
2298 struct MeshTunnel *t)
2300 struct MeshTransmissionDescriptor *info;
2301 struct MeshPeerInfo *neighbor;
2302 struct MeshPeerPath *p;
2306 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2308 size = ntohs (message->size);
2309 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2310 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2311 info->mesh_data->data = GNUNET_malloc (size);
2312 memcpy (info->mesh_data->data, message, size);
2313 type = ntohs(message->type);
2314 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2316 struct GNUNET_MESH_Unicast *m;
2318 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2319 m->ttl = htonl (ntohl (m->ttl) - 1);
2321 info->mesh_data->data_len = size;
2322 info->mesh_data->reference_counter = 1;
2323 info->mesh_data->total_out = 1;
2324 neighbor = peer_info_get (peer);
2325 for (p = neighbor->path_head; NULL != p; p = p->next)
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2336 " %s IS NOT DIRECTLY CONNECTED\n",
2338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 for (p = neighbor->path_head; NULL != p; p = p->next)
2343 struct GNUNET_PeerIdentity debug_id;
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2347 " path with %u hops through:\n",
2349 for (i = 0; i < p->length; i++)
2351 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2354 i, GNUNET_i2s(&debug_id));
2358 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2359 GNUNET_free (info->mesh_data->data);
2360 GNUNET_free (info->mesh_data);
2364 info->peer = neighbor;
2365 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2368 * TODO: in this case we only need the service to retransmit
2369 * the message down the path. If we pass the real type to queue_add,
2370 * queue_send will try to build the message from scratch. This can
2371 * probably be done by some other way instead of deleteing the type
2385 * Sends a CREATE PATH message for a path to a peer, properly registrating
2386 * all used resources.
2388 * @param peer PeerInfo of the final peer for whom this path is being created.
2389 * @param p Path itself.
2390 * @param t Tunnel for which the path is created.
2393 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2394 struct MeshTunnel *t)
2396 struct GNUNET_PeerIdentity id;
2397 struct MeshPathInfo *path_info;
2398 struct MeshPeerInfo *neighbor;
2404 p = tree_get_path_to_peer (t->tree, peer->id);
2411 for (i = 0; i < p->length; i++)
2413 if (p->peers[i] == myid)
2416 if (i >= p->length - 1)
2422 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2424 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2425 path_info->path = p;
2427 neighbor = peer_info_get (&id);
2428 path_info->peer = neighbor;
2429 queue_add (path_info,
2430 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2431 sizeof (struct GNUNET_MESH_ManipulatePath) +
2432 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2439 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2441 * @param t Tunnel whose path to destroy.
2442 * @param destination Short ID of the peer to whom the path to destroy.
2445 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2447 struct MeshPeerPath *p;
2450 p = tree_get_path_to_peer (t->tree, destination);
2456 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2457 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2459 struct GNUNET_MESH_ManipulatePath *msg;
2460 struct GNUNET_PeerIdentity *pi;
2464 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2465 msg->header.size = htons (size);
2466 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2467 msg->tid = htonl (t->id.tid);
2468 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2469 for (i = 0; i < p->length; i++)
2471 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2473 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2480 * Try to establish a new connection to this peer.
2481 * Use the best path for the given tunnel.
2482 * If the peer doesn't have any path to it yet, try to get one.
2483 * If the peer already has some path, send a CREATE PATH towards it.
2485 * @param peer PeerInfo of the peer.
2486 * @param t Tunnel for which to create the path, if possible.
2489 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2491 struct MeshPeerPath *p;
2492 struct MeshPathInfo *path_info;
2494 if (NULL != peer->path_head)
2496 p = tree_get_path_to_peer (t->tree, peer->id);
2503 // FIXME always send create path to self
2506 send_create_path (peer, p, t);
2510 struct GNUNET_HashCode hash;
2513 send_client_peer_connected (t, myid);
2514 t->local_tid_dest = next_local_tid++;
2515 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2518 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2526 else if (NULL == peer->dhtget)
2528 struct GNUNET_PeerIdentity id;
2530 GNUNET_PEER_resolve (peer->id, &id);
2531 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2532 path_info->peer = peer;
2534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2536 peer->dhtgetcls = path_info;
2537 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2538 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2539 &id.hashPubKey, /* key to search */
2540 dht_replication_level, /* replication level */
2541 GNUNET_DHT_RO_RECORD_ROUTE |
2542 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2543 NULL, /* xquery */ // FIXME BLOOMFILTER
2544 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2545 &dht_get_id_handler, path_info);
2547 /* Otherwise, there is no path but the DHT get is already started. */
2552 * Task to delay the connection of a peer
2554 * @param cls Closure (path info with tunnel and peer to connect).
2555 * Will be free'd on exection.
2556 * @param tc TaskContext
2559 peer_info_connect_task (void *cls,
2560 const struct GNUNET_SCHEDULER_TaskContext *tc)
2562 struct MeshPathInfo *path_info = cls;
2564 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2569 peer_info_connect (path_info->peer, path_info->t);
2575 * Destroy the peer_info and free any allocated resources linked to it
2577 * @param pi The peer_info to destroy.
2579 * @return GNUNET_OK on success
2582 peer_info_destroy (struct MeshPeerInfo *pi)
2584 struct GNUNET_PeerIdentity id;
2585 struct MeshPeerPath *p;
2586 struct MeshPeerPath *nextp;
2588 GNUNET_PEER_resolve (pi->id, &id);
2589 GNUNET_PEER_change_rc (pi->id, -1);
2592 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2596 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2598 if (NULL != pi->dhtget)
2600 GNUNET_DHT_get_stop (pi->dhtget);
2601 GNUNET_free (pi->dhtgetcls);
2607 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2617 * Remove all paths that rely on a direct connection between p1 and p2
2618 * from the peer itself and notify all tunnels about it.
2620 * @param peer PeerInfo of affected peer.
2621 * @param p1 GNUNET_PEER_Id of one peer.
2622 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2625 * TODO: optimize (see below)
2628 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2631 struct MeshPeerPath *p;
2632 struct MeshPeerPath *aux;
2633 struct MeshPeerInfo *peer_d;
2635 unsigned int destroyed;
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2642 p = peer->path_head;
2646 for (i = 0; i < (p->length - 1); i++)
2648 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2649 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2651 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2662 for (i = 0; i < peer->ntunnels; i++)
2664 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2668 * Problem: one or more peers have been deleted from the tunnel tree.
2669 * We don't know who they are to try to add them again.
2670 * We need to try to find a new path for each of the disconnected peers.
2671 * Some of them might already have a path to reach them that does not
2672 * involve p1 and p2. Adding all anew might render in a better tree than
2673 * the trivial immediate fix.
2675 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2676 * its children will be reachable trough him.
2678 peer_d = peer_info_get_short (d);
2681 for (p = peer_d->path_head; NULL != p; p = p->next)
2683 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2691 /* No callback, as peer will be already disconnected and a connection
2692 * scheduled by tunnel_notify_connection_broken.
2694 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2698 peer_info_connect (peer_d, peer->tunnels[i]);
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2706 * Add the path to the peer and update the path used to reach it in case this
2709 * @param peer_info Destination peer to add the path to.
2710 * @param path New path to add. Last peer must be the peer in arg 1.
2711 * Path will be either used of freed if already known.
2712 * @param trusted Do we trust that this path is real?
2715 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2718 struct MeshPeerPath *aux;
2722 if ((NULL == peer_info) || (NULL == path))
2725 path_destroy (path);
2728 if (path->peers[path->length - 1] != peer_info->id)
2731 path_destroy (path);
2734 if (path->length <= 2 && GNUNET_NO == trusted)
2736 /* Only allow CORE to tell us about direct paths */
2737 path_destroy (path);
2740 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2741 for (l = 1; l < path->length; l++)
2743 if (path->peers[l] == myid)
2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2746 for (l2 = 0; l2 < path->length - l; l2++)
2748 path->peers[l2] = path->peers[l + l2];
2753 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2758 struct GNUNET_PeerIdentity id;
2760 GNUNET_PEER_resolve (peer_info->id, &id);
2761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2762 path->length, GNUNET_i2s (&id));
2765 l = path_get_length (path);
2772 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2773 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2775 l2 = path_get_length (aux);
2778 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2779 peer_info->path_tail, aux, path);
2784 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2786 path_destroy (path);
2791 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2798 * Add the path to the origin peer and update the path used to reach it in case
2799 * this is the shortest.
2800 * The path is given in peer_info -> destination, therefore we turn the path
2801 * upside down first.
2803 * @param peer_info Peer to add the path to, being the origin of the path.
2804 * @param path New path to add after being inversed.
2805 * @param trusted Do we trust that this path is real?
2808 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2809 struct MeshPeerPath *path, int trusted)
2812 peer_info_add_path (peer_info, path, trusted);
2817 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2818 * to obtain a local peer -> destination path and interning the peer ids.
2820 * @return Newly allocated and created path
2822 static struct MeshPeerPath *
2823 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2824 unsigned int get_path_length,
2825 const struct GNUNET_PeerIdentity *put_path,
2826 unsigned int put_path_length)
2828 struct MeshPeerPath *p;
2834 GNUNET_PEER_change_rc (myid, 1);
2835 i = get_path_length;
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2837 for (i--; i >= 0; i--)
2839 id = GNUNET_PEER_intern (&get_path[i]);
2840 if (p->length > 0 && id == p->peers[p->length - 1])
2842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2843 GNUNET_PEER_change_rc (id, -1);
2847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2848 GNUNET_i2s (&get_path[i]));
2850 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2851 p->peers[p->length - 1] = id;
2854 i = put_path_length;
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2856 for (i--; i >= 0; i--)
2858 id = GNUNET_PEER_intern (&put_path[i]);
2861 /* PUT path went through us, so discard the path up until now and start
2862 * from here to get a much shorter (and loop-free) path.
2867 if (p->length > 0 && id == p->peers[p->length - 1])
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2870 GNUNET_PEER_change_rc (id, -1);
2874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2875 GNUNET_i2s (&put_path[i]));
2877 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2878 p->peers[p->length - 1] = id;
2882 if (get_path_length > 0)
2883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2884 GNUNET_i2s (&get_path[0]));
2885 if (put_path_length > 0)
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2887 GNUNET_i2s (&put_path[0]));
2888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2890 for (i = 0; i < p->length; i++)
2892 struct GNUNET_PeerIdentity peer_id;
2894 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2896 GNUNET_i2s (&peer_id));
2904 * Adds a path to the peer_infos of all the peers in the path
2906 * @param p Path to process.
2907 * @param confirmed Whether we know if the path works or not.
2910 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2914 /* TODO: invert and add */
2915 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2916 for (i++; i < p->length; i++)
2918 struct MeshPeerInfo *aux;
2919 struct MeshPeerPath *copy;
2921 aux = peer_info_get_short (p->peers[i]);
2922 copy = path_duplicate (p);
2923 copy->length = i + 1;
2924 peer_info_add_path (aux, copy, GNUNET_NO);
2930 * Send keepalive packets for a peer
2932 * @param cls Closure (tunnel for which to send the keepalive).
2933 * @param tc Notification context.
2935 * TODO: implement explicit multicast keepalive?
2938 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2942 * Search for a tunnel among the incoming tunnels
2944 * @param tid the local id of the tunnel
2946 * @return tunnel handler, NULL if doesn't exist
2948 static struct MeshTunnel *
2949 tunnel_get_incoming (MESH_TunnelNumber tid)
2951 struct GNUNET_HashCode hash;
2953 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2954 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2955 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2960 * Search for a tunnel among the tunnels for a client
2962 * @param c the client whose tunnels to search in
2963 * @param tid the local id of the tunnel
2965 * @return tunnel handler, NULL if doesn't exist
2967 static struct MeshTunnel *
2968 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2970 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2972 return tunnel_get_incoming (tid);
2976 struct GNUNET_HashCode hash;
2978 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2979 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2985 * Search for a tunnel by global ID using PEER_ID
2987 * @param pi owner of the tunnel
2988 * @param tid global tunnel number
2990 * @return tunnel handler, NULL if doesn't exist
2992 static struct MeshTunnel *
2993 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2995 struct MESH_TunnelID id;
2996 struct GNUNET_HashCode hash;
3001 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3002 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3007 * Search for a tunnel by global ID using full PeerIdentities
3009 * @param oid owner of the tunnel
3010 * @param tid global tunnel number
3012 * @return tunnel handler, NULL if doesn't exist
3014 static struct MeshTunnel *
3015 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3017 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3022 * Delete an active client from the tunnel.
3028 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3032 for (i = 0; i < t->nclients; i++)
3034 if (t->clients[i] == c)
3036 t->clients[i] = t->clients[t->nclients - 1];
3037 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3038 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3040 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3048 * Delete an ignored client from the tunnel.
3054 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3058 for (i = 0; i < t->nignore; i++)
3060 if (t->ignore[i] == c)
3062 t->ignore[i] = t->ignore[t->nignore - 1];
3063 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3071 * Delete a client from the tunnel. It should be only done on
3072 * client disconnection, otherwise use client_ignore_tunnel.
3078 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3080 tunnel_delete_ignored_client (t, c);
3081 tunnel_delete_active_client (t, c);
3086 * Iterator to free MeshTunnelChildInfo of tunnel children.
3088 * @param cls Closure (tunnel info).
3089 * @param key Hash of GNUNET_PEER_Id (unused).
3090 * @param value MeshTunnelChildInfo of the child.
3092 * @return always GNUNET_YES, to keep iterating
3095 tunnel_destroy_child (void *cls,
3096 const struct GNUNET_HashCode * key,
3099 struct MeshTunnelChildInfo *cinfo = value;
3100 struct MeshTunnel *t = cls;
3104 for (c = 0; c < cinfo->send_buffer_n; c++)
3106 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3107 if (NULL != cinfo->send_buffer[i])
3108 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3111 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3113 GNUNET_free_non_null (cinfo->send_buffer);
3114 GNUNET_free (cinfo);
3120 * Callback used to notify a client owner of a tunnel that a peer has
3121 * disconnected, most likely because of a path change.
3123 * @param cls Closure (tunnel this notification is about).
3124 * @param peer_id Short ID of disconnected peer.
3127 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3129 struct MeshTunnel *t = cls;
3130 struct MeshPeerInfo *peer;
3131 struct MeshPathInfo *path_info;
3133 if (NULL != t->owner && NULL != nc)
3135 struct GNUNET_MESH_PeerControl msg;
3137 msg.header.size = htons (sizeof (msg));
3138 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3139 msg.tunnel_id = htonl (t->local_tid);
3140 GNUNET_PEER_resolve (peer_id, &msg.peer);
3141 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3142 &msg.header, GNUNET_NO);
3144 peer = peer_info_get_short (peer_id);
3145 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3146 path_info->peer = peer;
3148 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3153 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3154 * needed rescources.
3155 * If peer already exists, reevaluate shortest path and change if different.
3157 * @param t Tunnel we want to add a new peer to
3158 * @param peer PeerInfo of the peer being added
3162 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3164 struct GNUNET_PeerIdentity id;
3165 struct MeshPeerPath *best_p;
3166 struct MeshPeerPath *p;
3167 unsigned int best_cost;
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3171 GNUNET_PEER_resolve (peer->id, &id);
3173 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3176 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3177 GNUNET_assert (GNUNET_OK ==
3178 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3180 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3183 if (NULL != (p = peer->path_head))
3186 best_cost = tree_get_path_cost (t->tree, p);
3189 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3196 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3197 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3198 t->path_refresh_task =
3199 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3203 /* Start a DHT get */
3204 peer_info_connect (peer, t);
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3210 * Add a path to a tunnel which we don't own, just to remember the next hop.
3211 * If destination node was already in the tunnel, the first hop information
3212 * will be replaced with the new path.
3214 * @param t Tunnel we want to add a new peer to
3215 * @param p Path to add
3216 * @param own_pos Position of local node in path.
3220 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3221 unsigned int own_pos)
3223 struct GNUNET_PeerIdentity id;
3225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3226 GNUNET_assert (0 != own_pos);
3227 tree_add_path (t->tree, p, NULL, NULL);
3228 if (own_pos < p->length - 1)
3230 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3231 tree_update_first_hops (t->tree, myid, &id);
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3237 * Add a client to a tunnel, initializing all needed data structures.
3239 * @param t Tunnel to which add the client.
3240 * @param c Client which to add to the tunnel.
3243 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3245 struct MeshTunnelClientInfo clinfo;
3247 GNUNET_array_append (t->clients, t->nclients, c);
3248 clinfo.fwd_ack = t->fwd_pid + 1;
3249 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3250 clinfo.fwd_pid = t->fwd_pid;
3251 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3253 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3258 * Notifies a tunnel that a connection has broken that affects at least
3259 * some of its peers. Sends a notification towards the root of the tree.
3260 * In case the peer is the owner of the tree, notifies the client that owns
3261 * the tunnel and tries to reconnect.
3263 * @param t Tunnel affected.
3264 * @param p1 Peer that got disconnected from p2.
3265 * @param p2 Peer that got disconnected from p1.
3267 * @return Short ID of the peer disconnected (either p1 or p2).
3268 * 0 if the tunnel remained unaffected.
3270 static GNUNET_PEER_Id
3271 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3277 tree_notify_connection_broken (t->tree, p1, p2,
3278 &tunnel_notify_client_peer_disconnected,
3280 if (myid != p1 && myid != p2)
3286 if (tree_get_predecessor (t->tree) != 0)
3288 /* We are the peer still connected, notify owner of the disconnection. */
3289 struct GNUNET_MESH_PathBroken msg;
3290 struct GNUNET_PeerIdentity neighbor;
3292 msg.header.size = htons (sizeof (msg));
3293 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3294 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3295 msg.tid = htonl (t->id.tid);
3296 msg.peer1 = my_full_id;
3297 GNUNET_PEER_resolve (pid, &msg.peer2);
3298 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3299 send_message (&msg.header, &neighbor, t);
3307 * Send a multicast packet to a neighbor.
3309 * @param cls Closure (Info about the multicast packet)
3310 * @param neighbor_id Short ID of the neighbor to send the packet to.
3313 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3315 struct MeshData *mdata = cls;
3316 struct MeshTransmissionDescriptor *info;
3317 struct GNUNET_PeerIdentity neighbor;
3319 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3321 info->mesh_data = mdata;
3322 (mdata->reference_counter) ++;
3323 info->destination = neighbor_id;
3324 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3326 GNUNET_i2s (&neighbor));
3327 info->peer = peer_info_get (&neighbor);
3328 GNUNET_assert (NULL != info->peer);
3330 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3331 info->mesh_data->data_len,
3338 * Queue a message in a tunnel in multicast, sending a copy to each child node
3339 * down the local one in the tunnel tree.
3341 * @param t Tunnel in which to send the data.
3342 * @param msg Message to be sent.
3343 * @param internal DEPRECATED Has the service generated this message?
3345 * FIXME remove internal if no use comes up
3348 tunnel_send_multicast (struct MeshTunnel *t,
3349 const struct GNUNET_MessageHeader *msg,
3352 struct MeshData *mdata;
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3355 " sending a multicast packet...\n");
3357 mdata = GNUNET_malloc (sizeof (struct MeshData));
3358 mdata->data_len = ntohs (msg->size);
3360 mdata->data = GNUNET_malloc (mdata->data_len);
3361 memcpy (mdata->data, msg, mdata->data_len);
3362 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3364 struct GNUNET_MESH_Multicast *mcast;
3366 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3367 if (t->fwd_queue_n >= t->fwd_queue_max)
3370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3372 " message from %s!\n",
3373 GNUNET_i2s(&mcast->oid));
3374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3375 " message at %s!\n",
3376 GNUNET_i2s(&my_full_id));
3377 GNUNET_free (mdata->data);
3378 GNUNET_free (mdata);
3382 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3384 ntohl (mcast->ttl));
3388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3391 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3392 if (mdata->reference_counter == 0)
3394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3395 " no one to send data to\n");
3396 GNUNET_free (mdata->data);
3397 GNUNET_free (mdata);
3402 mdata->total_out = mdata->reference_counter;
3404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3405 " sending a multicast packet done\n");
3411 * Increase the SKIP value of all peers that
3412 * have not received a unicast message.
3414 * @param cls Closure (ID of the peer that HAS received the message).
3415 * @param key ID of the neighbor.
3416 * @param value Information about the neighbor.
3418 * @return GNUNET_YES to keep iterating.
3421 tunnel_add_skip (void *cls,
3422 const struct GNUNET_HashCode * key,
3425 struct GNUNET_PeerIdentity *neighbor = cls;
3426 struct MeshTunnelChildInfo *cinfo = value;
3428 /* TODO compare only pointers? key == neighbor? */
3429 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3439 * @brief Get neighbor's Flow Control information.
3441 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3442 * descendant of the local node in a certain tunnel.
3443 * If the info is not yet there (recently created path), creates the data struct
3444 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3447 * @param t Tunnel related.
3448 * @param peer Neighbor whose Flow Control info is needed.
3450 * @return Neighbor's Flow Control info.
3452 static struct MeshTunnelChildInfo *
3453 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3454 const struct GNUNET_PeerIdentity *peer)
3456 struct MeshTunnelChildInfo *cinfo;
3457 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3463 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3464 cinfo->id = GNUNET_PEER_intern (peer);
3465 cinfo->skip = t->fwd_pid;
3467 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3468 cinfo->fwd_ack = t->fwd_pid + delta;
3469 cinfo->bck_ack = delta;
3471 cinfo->send_buffer =
3472 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3474 GNUNET_assert (GNUNET_OK ==
3475 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3478 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3485 * Get the Flow Control info of a client.
3487 * @param t Tunnel on which to look.
3488 * @param c Client whose ACK to get.
3490 * @return ACK value.
3492 static struct MeshTunnelClientInfo *
3493 tunnel_get_client_fc (struct MeshTunnel *t,
3494 struct MeshClient *c)
3498 for (i = 0; i < t->nclients; i++)
3500 if (t->clients[i] != c)
3502 return &t->clients_fc[i];
3505 return NULL; // avoid compiler / coverity complaints
3510 * Iterator to get the appropiate ACK value from all children nodes.
3512 * @param cls Closue (tunnel).
3513 * @param id Id of the child node.
3516 tunnel_get_child_fwd_ack (void *cls,
3519 struct GNUNET_PeerIdentity peer_id;
3520 struct MeshTunnelChildInfo *cinfo;
3521 struct MeshTunnelChildIteratorContext *ctx = cls;
3522 struct MeshTunnel *t = ctx->t;
3525 GNUNET_PEER_resolve (id, &peer_id);
3526 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3527 ack = cinfo->fwd_ack;
3530 if (GNUNET_NO == ctx->init)
3532 ctx->max_child_ack = ack;
3533 ctx->init = GNUNET_YES;
3536 if (GNUNET_YES == t->speed_min)
3538 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3542 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3549 * Get the maximum PID allowed to transmit to any
3550 * tunnel child of the local peer, depending on the tunnel
3551 * buffering/speed settings.
3555 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3558 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3560 struct MeshTunnelChildIteratorContext ctx;
3562 ctx.max_child_ack = 0;
3564 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3566 if (0 == ctx.nchildren)
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3569 " tunnel has no children, no FWD ACK\n");
3573 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3574 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3576 return (int64_t) ctx.max_child_ack;
3581 * Set the FWD ACK value of a client in a particular tunnel.
3583 * @param t Tunnel affected.
3584 * @param c Client whose ACK to set.
3585 * @param ack ACK value.
3588 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3589 struct MeshClient *c,
3594 for (i = 0; i < t->nclients; i++)
3596 if (t->clients[i] != c)
3598 t->clients_fc[i].fwd_ack = ack;
3606 * Get the highest ACK value of all clients in a particular tunnel,
3607 * according to the buffering/speed settings.
3609 * @param t Tunnel on which to look.
3611 * @return Corresponding ACK value (max uint32_t).
3612 * If no clients are suscribed, -1LL.
3615 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3620 if (0 == t->nclients)
3622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3623 " tunnel has no clients, no FWD ACK\n");
3627 for (ack = -1LL, i = 0; i < t->nclients; i++)
3630 (GNUNET_YES == t->speed_min &&
3631 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3632 (GNUNET_NO == t->speed_min &&
3633 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3635 ack = t->clients_fc[i].fwd_ack;
3639 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3640 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3642 return (uint32_t) ack;
3647 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3648 * mode and the status of all children nodes.
3652 * @return Maximum PID allowed.
3655 tunnel_get_fwd_ack (struct MeshTunnel *t)
3659 uint32_t buffer_free;
3663 count = t->fwd_pid - t->skip;
3664 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3665 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3666 child_ack = tunnel_get_children_fwd_ack (t);
3667 client_ack = tunnel_get_clients_fwd_ack (t);
3668 if (-1LL == child_ack)
3670 // Node has no children, child_ack AND core buffer are irrelevant.
3671 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3672 return (uint32_t) client_ack;
3674 if (-1LL == client_ack)
3678 if (GNUNET_YES == t->speed_min)
3680 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3681 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3685 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3686 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3688 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3689 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3691 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3692 count, buffer_free, child_ack, client_ack, ack);
3698 * Build a local ACK message and send it to a local client.
3700 * @param t Tunnel on which to send the ACK.
3701 * @param c Client to whom send the ACK.
3702 * @param ack Value of the ACK.
3705 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3707 struct GNUNET_MESH_LocalAck msg;
3709 msg.header.size = htons (sizeof (msg));
3710 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3711 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3712 msg.max_pid = htonl (ack);
3713 GNUNET_SERVER_notification_context_unicast(nc,
3720 * Build an ACK message and queue it to send to the given peer.
3722 * @param t Tunnel on which to send the ACK.
3723 * @param peer Peer to whom send the ACK.
3724 * @param ack Value of the ACK.
3727 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3729 struct GNUNET_MESH_ACK msg;
3731 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3732 msg.header.size = htons (sizeof (msg));
3733 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3734 msg.pid = htonl (ack);
3735 msg.tid = htonl (t->id.tid);
3737 send_message (&msg.header, peer, t);
3742 * Notify a the owner of a tunnel about how many more
3743 * payload packages will we accept on a given tunnel.
3745 * @param t Tunnel on which to send the ACK.
3748 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3753 "Sending client FWD ACK on tunnel %X\n",
3756 ack = tunnel_get_fwd_ack (t);
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3759 if (t->last_fwd_ack == ack)
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3763 t->last_fwd_ack = ack;
3764 send_local_ack (t, t->owner, ack);
3769 * Send an ACK informing the predecessor about the available buffer space.
3770 * In case there is no predecessor, inform the owning client.
3771 * If buffering is off, send only on behalf of children or self if endpoint.
3772 * If buffering is on, send when sent to children and buffer space is free.
3773 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3774 * the ACK itself goes "back" (towards root).
3776 * @param t Tunnel on which to send the ACK.
3777 * @param type Type of message that triggered the ACK transmission.
3780 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3782 struct GNUNET_PeerIdentity id;
3785 if (NULL != t->owner)
3787 tunnel_send_client_fwd_ack (t);
3790 /* Is it after unicast / multicast retransmission? */
3793 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3794 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3796 "ACK due to FWD DATA retransmission\n");
3797 if (GNUNET_YES == t->nobuffer)
3799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3803 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3804 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3810 /* Check if we need no retransmit the ACK */
3811 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3812 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3816 " t->qmax: %u, t->qn: %u\n",
3817 t->fwd_queue_max, t->fwd_queue_n);
3818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3819 " t->pid: %u, t->ack: %u\n",
3820 t->fwd_pid, t->last_fwd_ack);
3824 /* Ok, ACK might be necessary, what PID to ACK? */
3825 ack = tunnel_get_fwd_ack (t);
3827 /* If speed_min and not all children have ack'd, dont send yet */
3828 if (ack == t->last_fwd_ack)
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3834 t->last_fwd_ack = ack;
3835 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3836 send_ack (t, &id, ack);
3842 * Iterator to send a child node a BCK ACK to allow him to send more
3845 * @param cls Closure (tunnel).
3846 * @param id Id of the child node.
3849 tunnel_send_child_bck_ack (void *cls,
3852 struct MeshTunnel *t = cls;
3853 struct MeshTunnelChildInfo *cinfo;
3854 struct GNUNET_PeerIdentity peer;
3856 GNUNET_PEER_resolve (id, &peer);
3857 cinfo = tunnel_get_neighbor_fc (t, &peer);
3859 if (cinfo->bck_ack != cinfo->pid &&
3860 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3864 send_ack (t, &peer, cinfo->bck_ack);
3869 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3871 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3873 * @param t Tunnel on which to send the BCK ACKs.
3876 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3879 unsigned int tunnel_delta;
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3883 tunnel_delta = t->bck_ack - t->bck_pid;
3884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3886 /* Find client whom to allow to send to origin (with lowest buffer space) */
3887 for (i = 0; i < t->nclients; i++)
3889 struct MeshTunnelClientInfo *clinfo;
3892 clinfo = &t->clients_fc[i];
3893 delta = clinfo->bck_ack - clinfo->bck_pid;
3894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3895 t->clients[i]->id, delta);
3897 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3898 (GNUNET_YES == t->nobuffer && 0 == delta))
3902 ack = clinfo->bck_pid;
3903 ack += t->nobuffer ? 1 : tunnel_delta;
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3905 " sending ack to client %u: %u\n",
3906 t->clients[i]->id, ack);
3907 send_local_ack (t, t->clients[i], ack);
3908 clinfo->bck_ack = ack;
3915 * Send an ACK informing the children nodes and destination clients about
3916 * the available buffer space.
3917 * If buffering is off, send only on behalf of root (can be self).
3918 * If buffering is on, send when sent to predecessor and buffer space is free.
3919 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3920 * the ACK itself goes "forward" (towards children/clients).
3922 * @param t Tunnel on which to send the ACK.
3923 * @param type Type of message that triggered the ACK transmission.
3926 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3929 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3930 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3931 /* Is it after data to_origin retransmission? */
3934 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3935 if (GNUNET_YES == t->nobuffer)
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3938 " Not sending ACK, nobuffer\n");
3942 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3943 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3949 tunnel_send_clients_bck_ack (t);
3950 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3955 * @brief Re-initiate traffic to this peer if necessary.
3957 * Check if there is traffic queued towards this peer
3958 * and the core transmit handle is NULL (traffic was stalled).
3959 * If so, call core tmt rdy.
3961 * @param cls Closure (unused)
3962 * @param peer_id Short ID of peer to which initiate traffic.
3965 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3967 struct MeshPeerInfo *peer;
3968 struct GNUNET_PeerIdentity id;
3969 struct MeshPeerQueue *q;
3972 peer = peer_info_get_short(peer_id);
3973 if (NULL != peer->core_transmit)
3976 q = queue_get_next(peer);
3979 /* Might br multicast traffic already sent to this particular peer but
3980 * not to other children in this tunnel.
3981 * This way t->queue_n would be > 0 but the queue of this particular peer
3987 GNUNET_PEER_resolve (peer->id, &id);
3988 peer->core_transmit =
3989 GNUNET_CORE_notify_transmit_ready(core_handle,
3992 GNUNET_TIME_UNIT_FOREVER_REL,
4002 * @brief Allow transmission of FWD traffic on this tunnel
4004 * Check if there is traffic queued towards any children
4005 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4007 * @param t Tunnel on which to unlock FWD traffic.
4010 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4012 if (0 == t->fwd_queue_n)
4015 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4020 * @brief Allow transmission of BCK traffic on this tunnel
4022 * Check if there is traffic queued towards the root of the tree
4023 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4025 * @param t Tunnel on which to unlock BCK traffic.
4028 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4030 if (0 == t->bck_queue_n)
4033 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4038 * Send a message to all peers in this tunnel that the tunnel is no longer
4041 * @param t The tunnel whose peers to notify.
4044 tunnel_send_destroy (struct MeshTunnel *t)
4046 struct GNUNET_MESH_TunnelDestroy msg;
4048 msg.header.size = htons (sizeof (msg));
4049 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4050 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4051 msg.tid = htonl (t->id.tid);
4052 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4057 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4059 * @param cls Closure (Tunnel which to cancel).
4060 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4063 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4065 struct MeshTunnel *t = cls;
4066 struct MeshPeerInfo *peer_info;
4067 struct MeshPeerQueue *pq;
4068 struct MeshPeerQueue *next;
4070 peer_info = peer_info_get_short (neighbor_id);
4071 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4074 if (pq->tunnel == t)
4076 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4077 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4078 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4080 // Should have been removed on destroy children
4083 queue_destroy (pq, GNUNET_YES);
4086 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4088 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4089 peer_info->core_transmit = NULL;
4094 * Destroy the tunnel and free any allocated resources linked to it.
4096 * @param t the tunnel to destroy
4098 * @return GNUNET_OK on success
4101 tunnel_destroy (struct MeshTunnel *t)
4103 struct MeshClient *c;
4104 struct GNUNET_HashCode hash;
4115 struct GNUNET_PeerIdentity id;
4117 GNUNET_PEER_resolve (t->id.oid, &id);
4118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4119 GNUNET_i2s (&id), t->id.tid);
4121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4125 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4126 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4131 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4134 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4138 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4139 for (i = 0; i < t->nclients; i++)
4143 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4148 for (i = 0; i < t->nignore; i++)
4152 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4157 if (t->nclients > 0)
4160 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4164 GNUNET_free (t->clients);
4166 if (NULL != t->peers)
4168 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4170 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4173 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4174 &tunnel_destroy_child,
4176 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4178 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4180 tree_destroy (t->tree);
4182 if (NULL != t->regex_ctx)
4183 regex_cancel_search (t->regex_ctx);
4184 if (NULL != t->dht_get_type)
4185 GNUNET_DHT_get_stop (t->dht_get_type);
4186 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4187 GNUNET_SCHEDULER_cancel (t->timeout_task);
4188 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4189 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4192 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4193 GNUNET_assert (0 <= n_tunnels);
4200 * Create a new tunnel
4202 * @param owner Who is the owner of the tunnel (short ID).
4203 * @param tid Tunnel Number of the tunnel.
4204 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4205 * @param local Tunnel Number for the tunnel, for the client point of view.
4207 * @return A new initialized tunnel. NULL on error.
4209 static struct MeshTunnel *
4210 tunnel_new (GNUNET_PEER_Id owner,
4211 MESH_TunnelNumber tid,
4212 struct MeshClient *client,
4213 MESH_TunnelNumber local)
4215 struct MeshTunnel *t;
4216 struct GNUNET_HashCode hash;
4218 if (n_tunnels >= max_tunnels && NULL == client)
4221 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4224 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4225 t->bck_queue_max = t->fwd_queue_max;
4226 t->tree = tree_new (owner);
4228 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4229 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4230 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4231 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4232 t->local_tid = local;
4233 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4235 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4237 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4239 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4240 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4247 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4254 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4256 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4257 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4261 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4271 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4272 * that are no longer needed, as well as nodes of no longer reachable peers.
4273 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4275 * @param t Tunnel from which to remove the path.
4276 * @param peer Short id of the peer which should be removed.
4279 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4281 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4287 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4288 * client when the client disconnects. If the client is not the owner, the
4289 * owner will get notified if no more clients are in the tunnel and the client
4290 * get removed from the tunnel's list.
4292 * @param cls closure (client that is disconnecting)
4293 * @param key the hash of the local tunnel id (used to access the hashmap)
4294 * @param value the value stored at the key (tunnel to destroy)
4296 * @return GNUNET_OK on success
4299 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4301 struct MeshTunnel *t = value;
4302 struct MeshClient *c = cls;
4305 send_client_tunnel_disconnect(t, c);
4308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4309 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4310 tunnel_delete_client(t, c);
4311 client_delete_tunnel(c, t);
4314 tunnel_send_destroy(t);
4315 r = tunnel_destroy (t);
4321 * Timeout function, destroys tunnel if called
4323 * @param cls Closure (tunnel to destroy).
4324 * @param tc TaskContext
4327 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4329 struct MeshTunnel *t = cls;
4331 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4333 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4338 * Resets the tunnel timeout. Starts it if no timeout was running.
4340 * @param t Tunnel whose timeout to reset.
4342 * TODO use heap to improve efficiency of scheduler.
4345 tunnel_reset_timeout (struct MeshTunnel *t)
4347 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4348 GNUNET_SCHEDULER_cancel (t->timeout_task);
4350 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4351 (refresh_path_time, 4), &tunnel_timeout, t);
4355 /******************************************************************************/
4356 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4357 /******************************************************************************/
4360 * Function to send a create path packet to a peer.
4362 * @param cls closure
4363 * @param size number of bytes available in buf
4364 * @param buf where the callee should write the message
4365 * @return number of bytes written to buf
4368 send_core_path_create (void *cls, size_t size, void *buf)
4370 struct MeshPathInfo *info = cls;
4371 struct GNUNET_MESH_ManipulatePath *msg;
4372 struct GNUNET_PeerIdentity *peer_ptr;
4373 struct MeshTunnel *t = info->t;
4374 struct MeshPeerPath *p = info->path;
4379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4381 sizeof (struct GNUNET_MESH_ManipulatePath) +
4382 p->length * sizeof (struct GNUNET_PeerIdentity);
4384 if (size < size_needed || NULL == buf)
4389 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4390 msg->header.size = htons (size_needed);
4391 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4392 msg->tid = ntohl (t->id.tid);
4395 if (GNUNET_YES == t->speed_min)
4396 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4397 if (GNUNET_YES == t->nobuffer)
4398 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4399 msg->opt = htonl(opt);
4401 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4402 for (i = 0; i < p->length; i++)
4404 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4411 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4417 * Fill the core buffer
4419 * @param cls closure (data itself)
4420 * @param size number of bytes available in buf
4421 * @param buf where the callee should write the message
4423 * @return number of bytes written to buf
4426 send_core_data_multicast (void *cls, size_t size, void *buf)
4428 struct MeshTransmissionDescriptor *info = cls;
4431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4432 GNUNET_assert (NULL != info);
4433 GNUNET_assert (NULL != info->peer);
4434 total_size = info->mesh_data->data_len;
4435 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4437 if (total_size > size)
4442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4443 memcpy (buf, info->mesh_data->data, total_size);
4446 struct GNUNET_MESH_Multicast *mc;
4447 struct GNUNET_MessageHeader *mh;
4450 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4452 mc = (struct GNUNET_MESH_Multicast *) mh;
4453 mh = (struct GNUNET_MessageHeader *) &mc[1];
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4455 " multicast, payload type %s\n",
4456 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4458 " multicast, payload size %u\n", ntohs (mh->size));
4462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4463 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4467 data_descriptor_decrement_rc (info->mesh_data);
4468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4476 * Creates a path ack message in buf and frees all unused resources.
4478 * @param cls closure (MeshTransmissionDescriptor)
4479 * @param size number of bytes available in buf
4480 * @param buf where the callee should write the message
4481 * @return number of bytes written to buf
4484 send_core_path_ack (void *cls, size_t size, void *buf)
4486 struct MeshTransmissionDescriptor *info = cls;
4487 struct GNUNET_MESH_PathACK *msg = buf;
4489 GNUNET_assert (NULL != info);
4490 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4495 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4496 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4497 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4498 msg->tid = htonl (info->origin->tid);
4499 msg->peer_id = my_full_id;
4502 /* TODO add signature */
4504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4505 return sizeof (struct GNUNET_MESH_PathACK);
4510 * Free a transmission that was already queued with all resources
4511 * associated to the request.
4513 * @param queue Queue handler to cancel.
4514 * @param clear_cls Is it necessary to free associated cls?
4517 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4519 struct MeshTransmissionDescriptor *dd;
4520 struct MeshPathInfo *path_info;
4522 if (GNUNET_YES == clear_cls)
4524 switch (queue->type)
4526 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4527 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4528 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4531 data_descriptor_decrement_rc (dd->mesh_data);
4533 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4535 path_info = queue->cls;
4536 path_destroy (path_info->path);
4540 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4541 " type %s unknown!\n",
4542 GNUNET_MESH_DEBUG_M2S(queue->type));
4544 GNUNET_free_non_null (queue->cls);
4546 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4547 queue->peer->queue_tail,
4549 GNUNET_free (queue);
4554 * @brief Get the next transmittable message from the queue.
4556 * This will be the head, except in the case of being a data packet
4557 * not allowed by the destination peer.
4559 * @param peer Destination peer.
4561 * @return The next viable MeshPeerQueue element to send to that peer.
4562 * NULL when there are no transmittable messages.
4564 struct MeshPeerQueue *
4565 queue_get_next (const struct MeshPeerInfo *peer)
4567 struct MeshPeerQueue *q;
4568 struct MeshTunnel *t;
4569 struct MeshTransmissionDescriptor *info;
4570 struct MeshTunnelChildInfo *cinfo;
4571 struct GNUNET_MESH_Unicast *ucast;
4572 struct GNUNET_MESH_ToOrigin *to_orig;
4573 struct GNUNET_MESH_Multicast *mcast;
4574 struct GNUNET_PeerIdentity id;
4578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4579 for (q = peer->queue_head; NULL != q; q = q->next)
4583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4585 GNUNET_MESH_DEBUG_M2S(q->type));
4588 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4589 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4590 pid = ntohl (ucast->pid);
4591 GNUNET_PEER_resolve (info->peer->id, &id);
4592 cinfo = tunnel_get_neighbor_fc(t, &id);
4593 ack = cinfo->fwd_ack;
4595 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4596 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4597 pid = ntohl (to_orig->pid);
4600 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4601 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4602 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4604 // Not a multicast payload: multicast control traffic (destroy, etc)
4607 pid = ntohl (mcast->pid);
4608 GNUNET_PEER_resolve (info->peer->id, &id);
4609 cinfo = tunnel_get_neighbor_fc(t, &id);
4610 ack = cinfo->fwd_ack;
4613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4618 "********* ACK: %u, PID: %u\n",
4620 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4629 "********* NEXT!\n");
4632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4633 "********* nothing found\n");
4639 * Core callback to write a queued packet to core buffer
4641 * @param cls Closure (peer info).
4642 * @param size Number of bytes available in buf.
4643 * @param buf Where the to write the message.
4645 * @return number of bytes written to buf
4648 queue_send (void *cls, size_t size, void *buf)
4650 struct MeshPeerInfo *peer = cls;
4651 struct GNUNET_MessageHeader *msg;
4652 struct MeshPeerQueue *queue;
4653 struct MeshTunnel *t;
4654 struct MeshTunnelChildInfo *cinfo;
4655 struct GNUNET_PeerIdentity dst_id;
4658 peer->core_transmit = NULL;
4660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4661 queue = queue_get_next(peer);
4663 /* Queue has no internal mesh traffic not sendable payload */
4666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4667 if (NULL == peer->queue_head)
4668 GNUNET_break(0); // Should've been canceled
4671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4673 GNUNET_PEER_resolve (peer->id, &dst_id);
4674 /* Check if buffer size is enough for the message */
4675 if (queue->size > size)
4679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4680 "********* not enough room, reissue\n");
4681 peer->core_transmit =
4682 GNUNET_CORE_notify_transmit_ready(core_handle,
4685 GNUNET_TIME_UNIT_FOREVER_REL,
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4695 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4699 "********* unicast: t->q (%u/%u)\n",
4700 t->fwd_queue_n, t->fwd_queue_max);
4702 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4709 switch (queue->type)
4712 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4713 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4714 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4716 "********* raw: %s\n",
4717 GNUNET_MESH_DEBUG_M2S (queue->type));
4719 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4720 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4721 data_size = send_core_data_raw (queue->cls, size, buf);
4722 msg = (struct GNUNET_MessageHeader *) buf;
4723 switch (ntohs (msg->type)) // Type of preconstructed message
4725 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4726 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4728 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4729 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4735 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4738 struct MeshTransmissionDescriptor *info = queue->cls;
4740 if ((1 == info->mesh_data->reference_counter
4741 && GNUNET_YES == t->speed_min)
4743 (info->mesh_data->total_out == info->mesh_data->reference_counter
4744 && GNUNET_NO == t->speed_min))
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4747 "********* considered sent\n");
4751 data_size = send_core_data_multicast(queue->cls, size, buf);
4752 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4754 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4756 data_size = send_core_path_create(queue->cls, size, buf);
4758 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4760 data_size = send_core_path_ack(queue->cls, size, buf);
4764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4765 "********* type unknown: %u\n",
4769 switch (queue->type)
4771 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4772 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4773 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4774 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4775 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4779 "at pos %u (%p) != %p\n",
4780 cinfo->send_buffer_start,
4781 cinfo->send_buffer[cinfo->send_buffer_start],
4784 GNUNET_break(cinfo->send_buffer_n > 0);
4785 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4786 cinfo->send_buffer_n--;
4787 cinfo->send_buffer_start++;
4788 cinfo->send_buffer_start %= t->fwd_queue_max;
4794 /* Free queue, but cls was freed by send_core_* */
4795 queue_destroy (queue, GNUNET_NO);
4797 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4803 /* If more data in queue, send next */
4804 queue = queue_get_next(peer);
4807 struct GNUNET_PeerIdentity id;
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4810 GNUNET_PEER_resolve (peer->id, &id);
4811 peer->core_transmit =
4812 GNUNET_CORE_notify_transmit_ready(core_handle,
4815 GNUNET_TIME_UNIT_FOREVER_REL,
4823 if (NULL != peer->queue_head)
4824 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4825 "********* %s stalled\n",
4826 GNUNET_i2s(&my_full_id));
4828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4834 * Queue and pass message to core when possible.
4836 * @param cls Closure (type dependant).
4837 * @param type Type of the message, 0 for a raw message.
4838 * @param size Size of the message.
4839 * @param dst Neighbor to send message to.
4840 * @param t Tunnel this message belongs to.
4843 queue_add (void *cls, uint16_t type, size_t size,
4844 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4846 struct MeshPeerQueue *queue;
4847 struct MeshTunnelChildInfo *cinfo;
4848 struct GNUNET_PeerIdentity id;
4854 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4855 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4857 n = &t->fwd_queue_n;
4858 max = &t->fwd_queue_max;
4860 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4862 n = &t->bck_queue_n;
4863 max = &t->bck_queue_max;
4868 if (NULL == t->owner)
4869 GNUNET_break_op(0); // TODO: kill connection?
4872 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4874 return; // Drop message
4878 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4884 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4885 GNUNET_PEER_resolve (dst->id, &id);
4886 if (NULL == dst->core_transmit)
4888 dst->core_transmit =
4889 GNUNET_CORE_notify_transmit_ready(core_handle,
4892 GNUNET_TIME_UNIT_FOREVER_REL,
4898 if (NULL == n) // Is this internal mesh traffic?
4901 // It's payload, keep track of buffer per peer.
4902 cinfo = tunnel_get_neighbor_fc(t, &id);
4903 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4904 if (NULL != cinfo->send_buffer[i])
4906 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4907 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4908 cinfo->send_buffer_start++;
4909 cinfo->send_buffer_start %= t->fwd_queue_max;
4910 cinfo->send_buffer_n--;
4912 cinfo->send_buffer[i] = queue;
4913 cinfo->send_buffer_n++;
4914 if (cinfo->send_buffer_n > t->fwd_queue_max)
4917 cinfo->send_buffer_n = t->fwd_queue_max;
4922 /******************************************************************************/
4923 /******************** MESH NETWORK HANDLERS **************************/
4924 /******************************************************************************/
4928 * Core handler for path creation
4930 * @param cls closure
4931 * @param message message
4932 * @param peer peer identity this notification is about
4933 * @param atsi performance data
4934 * @param atsi_count number of records in 'atsi'
4936 * @return GNUNET_OK to keep the connection open,
4937 * GNUNET_SYSERR to close it (signal serious error)
4940 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4941 const struct GNUNET_MessageHeader *message,
4942 const struct GNUNET_ATS_Information *atsi,
4943 unsigned int atsi_count)
4945 unsigned int own_pos;
4948 MESH_TunnelNumber tid;
4949 struct GNUNET_MESH_ManipulatePath *msg;
4950 struct GNUNET_PeerIdentity *pi;
4951 struct GNUNET_HashCode hash;
4952 struct MeshPeerPath *path;
4953 struct MeshPeerInfo *dest_peer_info;
4954 struct MeshPeerInfo *orig_peer_info;
4955 struct MeshTunnel *t;
4957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4958 "Received a path create msg [%s]\n",
4959 GNUNET_i2s (&my_full_id));
4960 size = ntohs (message->size);
4961 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4963 GNUNET_break_op (0);
4967 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4968 if (size % sizeof (struct GNUNET_PeerIdentity))
4970 GNUNET_break_op (0);
4973 size /= sizeof (struct GNUNET_PeerIdentity);
4976 GNUNET_break_op (0);
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4980 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4982 tid = ntohl (msg->tid);
4983 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4985 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4986 t = tunnel_get (pi, tid);
4987 if (NULL == t) // FIXME only for INCOMING tunnels?
4991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4992 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4995 // FIXME notify failure
4998 opt = ntohl (msg->opt);
4999 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5000 GNUNET_YES : GNUNET_NO;
5001 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
5002 GNUNET_YES : GNUNET_NO;
5003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5004 " speed_min: %d, nobuffer:%d\n",
5005 t->speed_min, t->nobuffer);
5007 if (GNUNET_YES == t->nobuffer)
5009 t->bck_queue_max = 1;
5010 t->fwd_queue_max = 1;
5013 // FIXME only assign a local tid if a local client is interested (on demand)
5014 while (NULL != tunnel_get_incoming (next_local_tid))
5015 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5016 t->local_tid_dest = next_local_tid++;
5017 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5020 tunnel_reset_timeout (t);
5021 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5023 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5024 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5032 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5033 if (NULL == dest_peer_info)
5035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5036 " Creating PeerInfo for destination.\n");
5037 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5038 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5039 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5041 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5043 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5044 if (NULL == orig_peer_info)
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5047 " Creating PeerInfo for origin.\n");
5048 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5049 orig_peer_info->id = GNUNET_PEER_intern (pi);
5050 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5051 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5054 path = path_new (size);
5056 for (i = 0; i < size; i++)
5058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5059 GNUNET_i2s (&pi[i]));
5060 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5061 if (path->peers[i] == myid)
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5067 /* cannot be self, must be 'not found' */
5068 /* create path: self not found in path through self */
5069 GNUNET_break_op (0);
5070 path_destroy (path);
5074 path_add_to_peers (path, GNUNET_NO);
5075 tunnel_add_path (t, path, own_pos);
5076 if (own_pos == size - 1)
5078 /* It is for us! Send ack. */
5079 struct MeshTransmissionDescriptor *info;
5081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5082 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5083 if (NULL == t->peers)
5085 /* New tunnel! Notify clients on data. */
5086 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5088 GNUNET_break (GNUNET_SYSERR !=
5089 GNUNET_CONTAINER_multihashmap_put (t->peers,
5090 &my_full_id.hashPubKey,
5093 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5094 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5095 info->origin = &t->id;
5096 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5097 GNUNET_assert (NULL != info->peer);
5099 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5100 sizeof (struct GNUNET_MESH_PathACK),
5106 struct MeshPeerPath *path2;
5108 /* It's for somebody else! Retransmit. */
5109 path2 = path_duplicate (path);
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5111 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5112 path2 = path_duplicate (path);
5113 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5114 send_create_path (dest_peer_info, path, t);
5121 * Core handler for path destruction
5123 * @param cls closure
5124 * @param message message
5125 * @param peer peer identity this notification is about
5126 * @param atsi performance data
5127 * @param atsi_count number of records in 'atsi'
5129 * @return GNUNET_OK to keep the connection open,
5130 * GNUNET_SYSERR to close it (signal serious error)
5133 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5134 const struct GNUNET_MessageHeader *message,
5135 const struct GNUNET_ATS_Information *atsi,
5136 unsigned int atsi_count)
5138 struct GNUNET_MESH_ManipulatePath *msg;
5139 struct GNUNET_PeerIdentity *pi;
5140 struct MeshPeerPath *path;
5141 struct MeshTunnel *t;
5142 unsigned int own_pos;
5146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5147 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5148 size = ntohs (message->size);
5149 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5151 GNUNET_break_op (0);
5155 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5156 if (size % sizeof (struct GNUNET_PeerIdentity))
5158 GNUNET_break_op (0);
5161 size /= sizeof (struct GNUNET_PeerIdentity);
5164 GNUNET_break_op (0);
5167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5169 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5170 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5172 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5174 t = tunnel_get (pi, ntohl (msg->tid));
5177 /* TODO notify back: we don't know this tunnel */
5178 GNUNET_break_op (0);
5181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5182 path = path_new (size);
5184 for (i = 0; i < size; i++)
5186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5187 GNUNET_i2s (&pi[i]));
5188 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5189 if (path->peers[i] == myid)
5192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5193 if (own_pos < path->length - 1)
5194 send_message (message, &pi[own_pos + 1], t);
5196 send_client_tunnel_disconnect(t, NULL);
5198 tunnel_delete_peer (t, path->peers[path->length - 1]);
5199 path_destroy (path);
5205 * Core handler for notifications of broken paths
5207 * @param cls closure
5208 * @param message message
5209 * @param peer peer identity this notification is about
5210 * @param atsi performance data
5211 * @param atsi_count number of records in 'atsi'
5213 * @return GNUNET_OK to keep the connection open,
5214 * GNUNET_SYSERR to close it (signal serious error)
5217 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5218 const struct GNUNET_MessageHeader *message,
5219 const struct GNUNET_ATS_Information *atsi,
5220 unsigned int atsi_count)
5222 struct GNUNET_MESH_PathBroken *msg;
5223 struct MeshTunnel *t;
5225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5226 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5227 msg = (struct GNUNET_MESH_PathBroken *) message;
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5229 GNUNET_i2s (&msg->peer1));
5230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5231 GNUNET_i2s (&msg->peer2));
5232 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5235 GNUNET_break_op (0);
5238 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5239 GNUNET_PEER_search (&msg->peer2));
5246 * Core handler for tunnel destruction
5248 * @param cls closure
5249 * @param message message
5250 * @param peer peer identity this notification is about
5251 * @param atsi performance data
5252 * @param atsi_count number of records in 'atsi'
5254 * @return GNUNET_OK to keep the connection open,
5255 * GNUNET_SYSERR to close it (signal serious error)
5258 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5259 const struct GNUNET_MessageHeader *message,
5260 const struct GNUNET_ATS_Information *atsi,
5261 unsigned int atsi_count)
5263 struct GNUNET_MESH_TunnelDestroy *msg;
5264 struct MeshTunnel *t;
5266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5267 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5268 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5270 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5271 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5274 /* Probably already got the message from another path,
5275 * destroyed the tunnel and retransmitted to children.
5278 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5281 if (t->id.oid == myid)
5283 GNUNET_break_op (0);
5286 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5288 /* Tunnel was incoming, notify clients */
5289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5290 t->local_tid, t->local_tid_dest);
5291 send_clients_tunnel_destroy (t);
5293 tunnel_send_destroy (t);
5294 t->destroy = GNUNET_YES;
5295 // TODO: add timeout to destroy the tunnel anyway
5301 * Core handler for mesh network traffic going from the origin to a peer
5303 * @param cls closure
5304 * @param peer peer identity this notification is about
5305 * @param message message
5306 * @param atsi performance data
5307 * @param atsi_count number of records in 'atsi'
5308 * @return GNUNET_OK to keep the connection open,
5309 * GNUNET_SYSERR to close it (signal serious error)
5312 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5313 const struct GNUNET_MessageHeader *message,
5314 const struct GNUNET_ATS_Information *atsi,
5315 unsigned int atsi_count)
5317 struct GNUNET_MESH_Unicast *msg;
5318 struct GNUNET_PeerIdentity *neighbor;
5319 struct MeshTunnelChildInfo *cinfo;
5320 struct MeshTunnel *t;
5321 GNUNET_PEER_Id dest_id;
5326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5329 size = ntohs (message->size);
5331 sizeof (struct GNUNET_MESH_Unicast) +
5332 sizeof (struct GNUNET_MessageHeader))
5337 msg = (struct GNUNET_MESH_Unicast *) message;
5338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5339 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5341 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5344 /* TODO notify back: we don't know this tunnel */
5345 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5346 GNUNET_break_op (0);
5349 pid = ntohl (msg->pid);
5350 if (t->fwd_pid == pid)
5352 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5354 " Already seen pid %u, DROPPING!\n", pid);
5359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5360 " pid %u not seen yet, forwarding\n", pid);
5363 t->skip += (pid - t->fwd_pid) - 1;
5366 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5368 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5369 GNUNET_break_op (0);
5370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5371 "Received PID %u, ACK %u\n",
5372 pid, t->last_fwd_ack);
5376 tunnel_reset_timeout (t);
5377 dest_id = GNUNET_PEER_search (&msg->destination);
5378 if (dest_id == myid)
5380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5381 " it's for us! sending to clients...\n");
5382 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5383 send_subscribed_clients (message, &msg[1].header, t);
5384 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5387 ttl = ntohl (msg->ttl);
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5391 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5392 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5393 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5397 " not for us, retransmitting...\n");
5399 neighbor = tree_get_first_hop (t->tree, dest_id);
5400 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5402 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5405 if (GNUNET_YES == t->nobuffer &&
5406 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5408 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5409 GNUNET_break_op (0);
5412 send_message (message, neighbor, t);
5413 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5419 * Core handler for mesh network traffic going from the origin to all peers
5421 * @param cls closure
5422 * @param message message
5423 * @param peer peer identity this notification is about
5424 * @param atsi performance data
5425 * @param atsi_count number of records in 'atsi'
5426 * @return GNUNET_OK to keep the connection open,
5427 * GNUNET_SYSERR to close it (signal serious error)
5429 * TODO: Check who we got this from, to validate route.
5432 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5433 const struct GNUNET_MessageHeader *message,
5434 const struct GNUNET_ATS_Information *atsi,
5435 unsigned int atsi_count)
5437 struct GNUNET_MESH_Multicast *msg;
5438 struct MeshTunnel *t;
5442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5444 size = ntohs (message->size);
5445 if (sizeof (struct GNUNET_MESH_Multicast) +
5446 sizeof (struct GNUNET_MessageHeader) > size)
5448 GNUNET_break_op (0);
5451 msg = (struct GNUNET_MESH_Multicast *) message;
5452 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5456 /* TODO notify that we dont know that tunnel */
5457 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5458 GNUNET_break_op (0);
5461 pid = ntohl (msg->pid);
5462 if (t->fwd_pid == pid)
5464 /* already seen this packet, drop */
5465 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5467 " Already seen pid %u, DROPPING!\n", pid);
5468 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5474 " pid %u not seen yet, forwarding\n", pid);
5476 t->skip += (pid - t->fwd_pid) - 1;
5478 tunnel_reset_timeout (t);
5480 /* Transmit to locally interested clients */
5481 if (NULL != t->peers &&
5482 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5484 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5485 send_subscribed_clients (message, &msg[1].header, t);
5486 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5489 if (ntohl (msg->ttl) == 0)
5491 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5493 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5496 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5497 tunnel_send_multicast (t, message, GNUNET_NO);
5503 * Core handler for mesh network traffic toward the owner of a tunnel
5505 * @param cls closure
5506 * @param message message
5507 * @param peer peer identity this notification is about
5508 * @param atsi performance data
5509 * @param atsi_count number of records in 'atsi'
5511 * @return GNUNET_OK to keep the connection open,
5512 * GNUNET_SYSERR to close it (signal serious error)
5515 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5516 const struct GNUNET_MessageHeader *message,
5517 const struct GNUNET_ATS_Information *atsi,
5518 unsigned int atsi_count)
5520 struct GNUNET_MESH_ToOrigin *msg;
5521 struct GNUNET_PeerIdentity id;
5522 struct MeshPeerInfo *peer_info;
5523 struct MeshTunnel *t;
5527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5529 size = ntohs (message->size);
5530 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5531 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5533 GNUNET_break_op (0);
5536 msg = (struct GNUNET_MESH_ToOrigin *) message;
5537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5538 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5539 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5543 /* TODO notify that we dont know this tunnel (whom)? */
5544 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5545 GNUNET_break_op (0);
5549 if (NULL != t->owner)
5552 struct GNUNET_MESH_ToOrigin *copy;
5554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5555 " it's for us! sending to clients...\n");
5556 /* TODO signature verification */
5557 memcpy (cbuf, message, size);
5558 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5559 copy->tid = htonl (t->local_tid);
5560 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5561 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5562 ©->header, GNUNET_NO);
5563 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5567 " not for us, retransmitting...\n");
5569 peer_info = peer_info_get (&msg->oid);
5570 if (NULL == peer_info)
5572 /* unknown origin of tunnel */
5576 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5577 send_message (message, &id, t);
5578 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5585 * Core handler for mesh network traffic point-to-point acks.
5587 * @param cls closure
5588 * @param message message
5589 * @param peer peer identity this notification is about
5590 * @param atsi performance data
5591 * @param atsi_count number of records in 'atsi'
5593 * @return GNUNET_OK to keep the connection open,
5594 * GNUNET_SYSERR to close it (signal serious error)
5597 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5598 const struct GNUNET_MessageHeader *message,
5599 const struct GNUNET_ATS_Information *atsi,
5600 unsigned int atsi_count)
5602 struct GNUNET_MESH_ACK *msg;
5603 struct MeshTunnel *t;
5606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5608 msg = (struct GNUNET_MESH_ACK *) message;
5610 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5614 /* TODO notify that we dont know this tunnel (whom)? */
5615 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5616 GNUNET_break_op (0);
5619 ack = ntohl (msg->pid);
5621 /* Is this a forward or backward ACK? */
5622 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5624 struct MeshTunnelChildInfo *cinfo;
5627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5628 cinfo = tunnel_get_neighbor_fc (t, peer);
5629 cinfo->fwd_ack = ack;
5630 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5631 tunnel_unlock_fwd_queues (t);
5635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5637 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5638 tunnel_unlock_bck_queue (t);
5645 * Core handler for path ACKs
5647 * @param cls closure
5648 * @param message message
5649 * @param peer peer identity this notification is about
5650 * @param atsi performance data
5651 * @param atsi_count number of records in 'atsi'
5653 * @return GNUNET_OK to keep the connection open,
5654 * GNUNET_SYSERR to close it (signal serious error)
5657 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5658 const struct GNUNET_MessageHeader *message,
5659 const struct GNUNET_ATS_Information *atsi,
5660 unsigned int atsi_count)
5662 struct GNUNET_MESH_PathACK *msg;
5663 struct GNUNET_PeerIdentity id;
5664 struct MeshPeerInfo *peer_info;
5665 struct MeshPeerPath *p;
5666 struct MeshTunnel *t;
5668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5669 GNUNET_i2s (&my_full_id));
5670 msg = (struct GNUNET_MESH_PathACK *) message;
5671 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5674 /* TODO notify that we don't know the tunnel */
5675 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5677 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5681 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5683 peer_info = peer_info_get (&msg->peer_id);
5684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5685 GNUNET_i2s (&msg->peer_id));
5686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5689 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5692 "connect_by_string completed, stopping search\n");
5693 regex_cancel_search (t->regex_ctx);
5694 t->regex_ctx = NULL;
5697 /* Add paths to peers? */
5698 p = tree_get_path_to_peer (t->tree, peer_info->id);
5701 path_add_to_peers (p, GNUNET_YES);
5709 /* Message for us? */
5710 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5713 if (NULL == t->owner)
5715 GNUNET_break_op (0);
5718 if (NULL != t->dht_get_type)
5720 GNUNET_DHT_get_stop (t->dht_get_type);
5721 t->dht_get_type = NULL;
5723 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5725 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5726 send_client_peer_connected (t, peer_info->id);
5731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5732 " not for us, retransmitting...\n");
5733 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5734 peer_info = peer_info_get (&msg->oid);
5735 if (NULL == peer_info)
5737 /* If we know the tunnel, we should DEFINITELY know the peer */
5741 send_message (message, &id, t);
5747 * Core handler for mesh keepalives.
5749 * @param cls closure
5750 * @param message message
5751 * @param peer peer identity this notification is about
5752 * @param atsi performance data
5753 * @param atsi_count number of records in 'atsi'
5754 * @return GNUNET_OK to keep the connection open,
5755 * GNUNET_SYSERR to close it (signal serious error)
5757 * TODO: Check who we got this from, to validate route.
5760 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5761 const struct GNUNET_MessageHeader *message,
5762 const struct GNUNET_ATS_Information *atsi,
5763 unsigned int atsi_count)
5765 struct GNUNET_MESH_TunnelKeepAlive *msg;
5766 struct MeshTunnel *t;
5768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5771 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5772 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5776 /* TODO notify that we dont know that tunnel */
5777 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5778 GNUNET_break_op (0);
5782 tunnel_reset_timeout (t);
5784 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5785 tunnel_send_multicast (t, message, GNUNET_NO);
5792 * Functions to handle messages from core
5794 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5795 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5796 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5797 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5798 sizeof (struct GNUNET_MESH_PathBroken)},
5799 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5800 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5801 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5802 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5803 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5804 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5805 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5806 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5807 sizeof (struct GNUNET_MESH_ACK)},
5808 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5809 sizeof (struct GNUNET_MESH_PathACK)},
5815 /******************************************************************************/
5816 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5817 /******************************************************************************/
5820 * deregister_app: iterator for removing each application registered by a client
5822 * @param cls closure
5823 * @param key the hash of the application id (used to access the hashmap)
5824 * @param value the value stored at the key (client)
5826 * @return GNUNET_OK on success
5829 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5831 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5832 GNUNET_break (GNUNET_YES ==
5833 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5839 * notify_client_connection_failure: notify a client that the connection to the
5840 * requested remote peer is not possible (for instance, no route found)
5841 * Function called when the socket is ready to queue more data. "buf" will be
5842 * NULL and "size" zero if the socket was closed for writing in the meantime.
5844 * @param cls closure
5845 * @param size number of bytes available in buf
5846 * @param buf where the callee should write the message
5847 * @return number of bytes written to buf
5850 notify_client_connection_failure (void *cls, size_t size, void *buf)
5853 struct MeshPeerInfo *peer_info;
5854 struct GNUNET_MESH_PeerControl *msg;
5855 struct GNUNET_PeerIdentity id;
5857 if (0 == size && NULL == buf)
5859 // TODO retry? cancel?
5863 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5864 peer_info = (struct MeshPeerInfo *) cls;
5865 msg = (struct GNUNET_MESH_PeerControl *) buf;
5866 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5867 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5868 // msg->tunnel_id = htonl(peer_info->t->tid);
5869 GNUNET_PEER_resolve (peer_info->id, &id);
5870 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5878 * Send keepalive packets for a peer
5880 * @param cls Closure (tunnel for which to send the keepalive).
5881 * @param tc Notification context.
5884 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5886 struct MeshTunnel *t = cls;
5887 struct GNUNET_MESH_TunnelKeepAlive *msg;
5888 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5891 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5892 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5898 "sending keepalive for tunnel %d\n", t->id.tid);
5900 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5901 msg->header.size = htons (size);
5902 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5903 msg->oid = my_full_id;
5904 msg->tid = htonl (t->id.tid);
5905 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5907 t->path_refresh_task =
5908 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5914 * Function to process paths received for a new peer addition. The recorded
5915 * paths form the initial tunnel, which can be optimized later.
5916 * Called on each result obtained for the DHT search.
5918 * @param cls closure
5919 * @param exp when will this value expire
5920 * @param key key of the result
5921 * @param get_path path of the get request
5922 * @param get_path_length lenght of get_path
5923 * @param put_path path of the put request
5924 * @param put_path_length length of the put_path
5925 * @param type type of the result
5926 * @param size number of bytes in data
5927 * @param data pointer to the result data
5929 * TODO: re-issue the request after certain time? cancel after X results?
5932 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5933 const struct GNUNET_HashCode * key,
5934 const struct GNUNET_PeerIdentity *get_path,
5935 unsigned int get_path_length,
5936 const struct GNUNET_PeerIdentity *put_path,
5937 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5938 size_t size, const void *data)
5940 struct MeshPathInfo *path_info = cls;
5941 struct MeshPeerPath *p;
5942 struct GNUNET_PeerIdentity pi;
5945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5946 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5949 p = path_build_from_dht (get_path, get_path_length, put_path,
5951 path_add_to_peers (p, GNUNET_NO);
5953 for (i = 0; i < path_info->peer->ntunnels; i++)
5955 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5956 peer_info_connect (path_info->peer, path_info->t);
5964 * Function to process paths received for a new peer addition. The recorded
5965 * paths form the initial tunnel, which can be optimized later.
5966 * Called on each result obtained for the DHT search.
5968 * @param cls closure
5969 * @param exp when will this value expire
5970 * @param key key of the result
5971 * @param get_path path of the get request
5972 * @param get_path_length lenght of get_path
5973 * @param put_path path of the put request
5974 * @param put_path_length length of the put_path
5975 * @param type type of the result
5976 * @param size number of bytes in data
5977 * @param data pointer to the result data
5980 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5981 const struct GNUNET_HashCode * key,
5982 const struct GNUNET_PeerIdentity *get_path,
5983 unsigned int get_path_length,
5984 const struct GNUNET_PeerIdentity *put_path,
5985 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5986 size_t size, const void *data)
5988 const struct PBlock *pb = data;
5989 const struct GNUNET_PeerIdentity *pi = &pb->id;
5990 struct MeshTunnel *t = cls;
5991 struct MeshPeerInfo *peer_info;
5992 struct MeshPeerPath *p;
5994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5995 if (size != sizeof (struct PBlock))
5997 GNUNET_break_op (0);
6000 if (ntohl(pb->type) != t->type)
6002 GNUNET_break_op (0);
6005 GNUNET_assert (NULL != t->owner);
6006 peer_info = peer_info_get (pi);
6007 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6009 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6011 p = path_build_from_dht (get_path, get_path_length, put_path,
6013 path_add_to_peers (p, GNUNET_NO);
6015 tunnel_add_peer (t, peer_info);
6016 peer_info_connect (peer_info, t);
6021 * Function to process DHT string to regex matching.
6022 * Called on each result obtained for the DHT search.
6024 * @param cls closure (search context)
6025 * @param exp when will this value expire
6026 * @param key key of the result
6027 * @param get_path path of the get request (not used)
6028 * @param get_path_length lenght of get_path (not used)
6029 * @param put_path path of the put request (not used)
6030 * @param put_path_length length of the put_path (not used)
6031 * @param type type of the result
6032 * @param size number of bytes in data
6033 * @param data pointer to the result data
6036 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6037 const struct GNUNET_HashCode * key,
6038 const struct GNUNET_PeerIdentity *get_path,
6039 unsigned int get_path_length,
6040 const struct GNUNET_PeerIdentity *put_path,
6041 unsigned int put_path_length,
6042 enum GNUNET_BLOCK_Type type,
6043 size_t size, const void *data)
6045 const struct MeshRegexAccept *block = data;
6046 struct MeshRegexSearchContext *ctx = cls;
6047 struct MeshRegexSearchInfo *info = ctx->info;
6048 struct MeshPeerPath *p;
6049 struct MeshPeerInfo *peer_info;
6051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6054 peer_info = peer_info_get(&block->id);
6055 p = path_build_from_dht (get_path, get_path_length, put_path,
6057 path_add_to_peers (p, GNUNET_NO);
6060 tunnel_add_peer (info->t, peer_info);
6061 peer_info_connect (peer_info, info->t);
6062 if (0 == info->peer)
6064 info->peer = peer_info->id;
6068 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6071 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6072 ®ex_connect_timeout,
6080 * Function to process DHT string to regex matching.
6081 * Called on each result obtained for the DHT search.
6083 * @param cls closure (search context)
6084 * @param exp when will this value expire
6085 * @param key key of the result
6086 * @param get_path path of the get request (not used)
6087 * @param get_path_length lenght of get_path (not used)
6088 * @param put_path path of the put request (not used)
6089 * @param put_path_length length of the put_path (not used)
6090 * @param type type of the result
6091 * @param size number of bytes in data
6092 * @param data pointer to the result data
6094 * TODO: re-issue the request after certain time? cancel after X results?
6097 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6098 const struct GNUNET_HashCode * key,
6099 const struct GNUNET_PeerIdentity *get_path,
6100 unsigned int get_path_length,
6101 const struct GNUNET_PeerIdentity *put_path,
6102 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6103 size_t size, const void *data)
6105 const struct MeshRegexBlock *block = data;
6106 struct MeshRegexSearchContext *ctx = cls;
6107 struct MeshRegexSearchInfo *info = ctx->info;
6111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6112 "DHT GET STRING RETURNED RESULTS\n");
6113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6114 " key: %s\n", GNUNET_h2s (key));
6116 copy = GNUNET_malloc (size);
6117 memcpy (copy, data, size);
6118 GNUNET_break (GNUNET_OK ==
6119 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6120 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6121 len = ntohl (block->n_proof);
6123 char proof[len + 1];
6125 memcpy (proof, &block[1], len);
6127 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6129 GNUNET_break_op (0);
6133 len = strlen (info->description);
6134 if (len == ctx->position) // String processed
6136 if (GNUNET_YES == ntohl (block->accepting))
6138 regex_find_path(key, ctx);
6142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6143 // FIXME REGEX this block not successful, wait for more? start timeout?
6148 regex_next_edge (block, size, ctx);
6153 /******************************************************************************/
6154 /********************* MESH LOCAL HANDLES **************************/
6155 /******************************************************************************/
6159 * Handler for client disconnection
6161 * @param cls closure
6162 * @param client identification of the client; NULL
6163 * for the last call when the server is destroyed
6166 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6168 struct MeshClient *c;
6169 struct MeshClient *next;
6172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6181 if (c->handle != client)
6183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6189 GNUNET_SERVER_client_drop (c->handle);
6190 c->shutting_down = GNUNET_YES;
6191 GNUNET_assert (NULL != c->own_tunnels);
6192 GNUNET_assert (NULL != c->incoming_tunnels);
6193 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6194 &tunnel_destroy_iterator, c);
6195 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6196 &tunnel_destroy_iterator, c);
6197 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6198 &tunnel_destroy_iterator, c);
6199 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6200 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6201 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6203 /* deregister clients applications */
6204 if (NULL != c->apps)
6206 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6207 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6209 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6210 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6212 GNUNET_SCHEDULER_cancel (announce_applications_task);
6213 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6215 if (NULL != c->types)
6216 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6217 for (i = 0; i < c->n_regex; i++)
6219 GNUNET_free (c->regexes[i]);
6221 GNUNET_free_non_null (c->regexes);
6222 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6223 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6225 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6228 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6237 * Handler for new clients
6239 * @param cls closure
6240 * @param client identification of the client
6241 * @param message the actual message, which includes messages the client wants
6244 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6245 const struct GNUNET_MessageHeader *message)
6247 struct GNUNET_MESH_ClientConnect *cc_msg;
6248 struct MeshClient *c;
6249 GNUNET_MESH_ApplicationType *a;
6256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6257 /* Check data sanity */
6258 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6259 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6260 ntypes = ntohs (cc_msg->types);
6261 napps = ntohs (cc_msg->applications);
6263 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6270 /* Create new client structure */
6271 c = GNUNET_malloc (sizeof (struct MeshClient));
6272 c->id = next_client_id++;
6273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6275 GNUNET_SERVER_client_keep (client);
6276 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6279 GNUNET_MESH_ApplicationType at;
6280 struct GNUNET_HashCode hc;
6282 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6283 for (i = 0; i < napps; i++)
6286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6287 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6288 /* store in clients hashmap */
6289 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6290 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6291 /* store in global hashmap, for announcements */
6292 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6293 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6295 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6296 announce_applications_task =
6297 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6303 struct GNUNET_HashCode hc;
6305 t = (uint16_t *) & a[napps];
6306 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6307 for (i = 0; i < ntypes; i++)
6310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6311 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6313 /* store in clients hashmap */
6314 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6315 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6316 /* store in global hashmap */
6317 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6318 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6322 " client has %u+%u subscriptions\n", napps, ntypes);
6324 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6325 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6326 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6327 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6328 GNUNET_SERVER_notification_context_add (nc, client);
6329 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6331 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6337 * Handler for clients announcing available services by a regular expression.
6339 * @param cls closure
6340 * @param client identification of the client
6341 * @param message the actual message, which includes messages the client wants
6344 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6345 const struct GNUNET_MessageHeader *message)
6347 struct MeshClient *c;
6351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6353 /* Sanity check for client registration */
6354 if (NULL == (c = client_get (client)))
6357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6362 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6363 regex = GNUNET_malloc (len + 1);
6364 memcpy (regex, &message[1], len);
6366 GNUNET_array_append (c->regexes, c->n_regex, regex);
6367 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6369 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6375 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6381 * Handler for requests of new tunnels
6383 * @param cls closure
6384 * @param client identification of the client
6385 * @param message the actual message
6388 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6389 const struct GNUNET_MessageHeader *message)
6391 struct GNUNET_MESH_TunnelMessage *t_msg;
6392 struct MeshTunnel *t;
6393 struct MeshClient *c;
6394 MESH_TunnelNumber tid;
6396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6398 /* Sanity check for client registration */
6399 if (NULL == (c = client_get (client)))
6402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6407 /* Message sanity check */
6408 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6415 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6416 /* Sanity check for tunnel numbering */
6417 tid = ntohl (t_msg->tunnel_id);
6418 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6424 /* Sanity check for duplicate tunnel IDs */
6425 if (NULL != tunnel_get_by_local_id (c, tid))
6428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6432 while (NULL != tunnel_get_by_pi (myid, next_tid))
6433 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6434 t = tunnel_new (myid, next_tid++, c, tid);
6437 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6442 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6444 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6445 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6448 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6454 * Handler for requests of deleting tunnels
6456 * @param cls closure
6457 * @param client identification of the client
6458 * @param message the actual message
6461 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6462 const struct GNUNET_MessageHeader *message)
6464 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6465 struct MeshClient *c;
6466 struct MeshTunnel *t;
6467 MESH_TunnelNumber tid;
6469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6470 "Got a DESTROY TUNNEL from client!\n");
6472 /* Sanity check for client registration */
6473 if (NULL == (c = client_get (client)))
6476 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6481 /* Message sanity check */
6482 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6489 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6491 /* Retrieve tunnel */
6492 tid = ntohl (tunnel_msg->tunnel_id);
6493 t = tunnel_get_by_local_id(c, tid);
6496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6498 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6501 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6503 client_ignore_tunnel (c, t);
6505 // TODO: when to destroy incoming tunnel?
6506 if (t->nclients == 0)
6508 GNUNET_assert (GNUNET_YES ==
6509 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6511 GNUNET_assert (GNUNET_YES ==
6512 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6513 &my_full_id.hashPubKey,
6517 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6520 send_client_tunnel_disconnect(t, c);
6521 client_delete_tunnel(c, t);
6523 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6525 tunnel_send_destroy (t);
6526 t->destroy = GNUNET_YES;
6527 // The tunnel will be destroyed when the last message is transmitted.
6528 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6534 * Handler for requests of seeting tunnel's speed.
6536 * @param cls Closure (unused).
6537 * @param client Identification of the client.
6538 * @param message The actual message.
6541 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6542 const struct GNUNET_MessageHeader *message)
6544 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6545 struct MeshClient *c;
6546 struct MeshTunnel *t;
6547 MESH_TunnelNumber tid;
6549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6550 "Got a SPEED request from client!\n");
6552 /* Sanity check for client registration */
6553 if (NULL == (c = client_get (client)))
6556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6562 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6564 /* Retrieve tunnel */
6565 tid = ntohl (tunnel_msg->tunnel_id);
6566 t = tunnel_get_by_local_id(c, tid);
6569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6571 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6575 switch (ntohs(message->type))
6577 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6578 t->speed_min = GNUNET_YES;
6580 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6581 t->speed_min = GNUNET_NO;
6586 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6591 * Handler for requests of seeting tunnel's buffering policy.
6593 * @param cls Closure (unused).
6594 * @param client Identification of the client.
6595 * @param message The actual message.
6598 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6599 const struct GNUNET_MessageHeader *message)
6601 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6602 struct MeshClient *c;
6603 struct MeshTunnel *t;
6604 MESH_TunnelNumber tid;
6606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6607 "Got a BUFFER request from client!\n");
6609 /* Sanity check for client registration */
6610 if (NULL == (c = client_get (client)))
6613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6618 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6620 /* Retrieve tunnel */
6621 tid = ntohl (tunnel_msg->tunnel_id);
6622 t = tunnel_get_by_local_id(c, tid);
6625 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6627 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6631 switch (ntohs(message->type))
6633 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6634 t->nobuffer = GNUNET_NO;
6636 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6637 t->nobuffer = GNUNET_YES;
6643 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6648 * Handler for connection requests to new peers
6650 * @param cls closure
6651 * @param client identification of the client
6652 * @param message the actual message (PeerControl)
6655 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6656 const struct GNUNET_MessageHeader *message)
6658 struct GNUNET_MESH_PeerControl *peer_msg;
6659 struct MeshPeerInfo *peer_info;
6660 struct MeshClient *c;
6661 struct MeshTunnel *t;
6662 MESH_TunnelNumber tid;
6664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6665 /* Sanity check for client registration */
6666 if (NULL == (c = client_get (client)))
6669 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6674 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6676 /* Sanity check for message size */
6677 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6680 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6684 /* Tunnel exists? */
6685 tid = ntohl (peer_msg->tunnel_id);
6686 t = tunnel_get_by_local_id (c, tid);
6690 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6694 /* Does client own tunnel? */
6695 if (t->owner->handle != client)
6698 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6702 GNUNET_i2s (&peer_msg->peer));
6703 peer_info = peer_info_get (&peer_msg->peer);
6705 tunnel_add_peer (t, peer_info);
6706 peer_info_connect (peer_info, t);
6708 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6714 * Handler for disconnection requests of peers in a tunnel
6716 * @param cls closure
6717 * @param client identification of the client
6718 * @param message the actual message (PeerControl)
6721 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6722 const struct GNUNET_MessageHeader *message)
6724 struct GNUNET_MESH_PeerControl *peer_msg;
6725 struct MeshPeerInfo *peer_info;
6726 struct MeshClient *c;
6727 struct MeshTunnel *t;
6728 MESH_TunnelNumber tid;
6730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6731 /* Sanity check for client registration */
6732 if (NULL == (c = client_get (client)))
6735 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6740 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6742 /* Sanity check for message size */
6743 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6746 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6750 /* Tunnel exists? */
6751 tid = ntohl (peer_msg->tunnel_id);
6752 t = tunnel_get_by_local_id (c, tid);
6756 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6761 /* Does client own tunnel? */
6762 if (t->owner->handle != client)
6765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6770 GNUNET_i2s (&peer_msg->peer));
6771 /* Is the peer in the tunnel? */
6773 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6774 if (NULL == peer_info)
6777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6781 /* Ok, delete peer from tunnel */
6782 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6783 &peer_msg->peer.hashPubKey);
6785 send_destroy_path (t, peer_info->id);
6786 tunnel_delete_peer (t, peer_info->id);
6787 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6792 * Handler for blacklist requests of peers in a tunnel
6794 * @param cls closure
6795 * @param client identification of the client
6796 * @param message the actual message (PeerControl)
6798 * FIXME implement DHT block bloomfilter
6801 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6802 const struct GNUNET_MessageHeader *message)
6804 struct GNUNET_MESH_PeerControl *peer_msg;
6805 struct MeshClient *c;
6806 struct MeshTunnel *t;
6807 MESH_TunnelNumber tid;
6809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6810 /* Sanity check for client registration */
6811 if (NULL == (c = client_get (client)))
6814 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6819 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6821 /* Sanity check for message size */
6822 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6825 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6829 /* Tunnel exists? */
6830 tid = ntohl (peer_msg->tunnel_id);
6831 t = tunnel_get_by_local_id (c, tid);
6835 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6840 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6841 GNUNET_PEER_intern(&peer_msg->peer));
6846 * Handler for unblacklist requests of peers in a tunnel
6848 * @param cls closure
6849 * @param client identification of the client
6850 * @param message the actual message (PeerControl)
6853 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6854 const struct GNUNET_MessageHeader *message)
6856 struct GNUNET_MESH_PeerControl *peer_msg;
6857 struct MeshClient *c;
6858 struct MeshTunnel *t;
6859 MESH_TunnelNumber tid;
6863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6864 /* Sanity check for client registration */
6865 if (NULL == (c = client_get (client)))
6868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6873 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6875 /* Sanity check for message size */
6876 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6883 /* Tunnel exists? */
6884 tid = ntohl (peer_msg->tunnel_id);
6885 t = tunnel_get_by_local_id (c, tid);
6889 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6894 /* if peer is not known, complain */
6895 pid = GNUNET_PEER_search (&peer_msg->peer);
6902 /* search and remove from list */
6903 for (i = 0; i < t->nblacklisted; i++)
6905 if (t->blacklisted[i] == pid)
6907 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6908 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6913 /* if peer hasn't been blacklisted, complain */
6919 * Handler for connection requests to new peers by type
6921 * @param cls closure
6922 * @param client identification of the client
6923 * @param message the actual message (ConnectPeerByType)
6926 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6927 const struct GNUNET_MessageHeader *message)
6929 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6930 struct MeshClient *c;
6931 struct MeshTunnel *t;
6932 struct GNUNET_HashCode hash;
6933 MESH_TunnelNumber tid;
6935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6936 /* Sanity check for client registration */
6937 if (NULL == (c = client_get (client)))
6940 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6945 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6947 /* Sanity check for message size */
6948 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6949 ntohs (connect_msg->header.size))
6952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6956 /* Tunnel exists? */
6957 tid = ntohl (connect_msg->tunnel_id);
6958 t = tunnel_get_by_local_id (c, tid);
6962 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6966 /* Does client own tunnel? */
6967 if (t->owner->handle != client)
6970 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6974 /* Do WE have the service? */
6975 t->type = ntohl (connect_msg->type);
6976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6977 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6978 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6981 /* Yes! Fast forward, add ourselves to the tunnel and send the
6982 * good news to the client, and alert the destination client of
6983 * an incoming tunnel.
6985 * FIXME send a path create to self, avoid code duplication
6987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6988 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6989 peer_info_get (&my_full_id),
6990 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6993 send_client_peer_connected (t, myid);
6994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6995 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6997 t->local_tid_dest = next_local_tid++;
6998 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6999 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7000 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7004 /* Ok, lets find a peer offering the service */
7005 if (NULL != t->dht_get_type)
7007 GNUNET_DHT_get_stop (t->dht_get_type);
7009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7010 GNUNET_h2s (&hash));
7012 GNUNET_DHT_get_start (dht_handle,
7013 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7015 dht_replication_level,
7016 GNUNET_DHT_RO_RECORD_ROUTE |
7017 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7019 &dht_get_type_handler, t);
7021 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7027 * Handler for connection requests to new peers by a string service description.
7029 * @param cls closure
7030 * @param client identification of the client
7031 * @param message the actual message, which includes messages the client wants
7034 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7035 const struct GNUNET_MessageHeader *message)
7037 struct GNUNET_MESH_ConnectPeerByString *msg;
7038 struct MeshRegexSearchContext *ctx;
7039 struct MeshRegexSearchInfo *info;
7040 struct GNUNET_DHT_GetHandle *get_h;
7041 struct GNUNET_HashCode key;
7042 struct MeshTunnel *t;
7043 struct MeshClient *c;
7044 MESH_TunnelNumber tid;
7048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7049 "Connect by string started\n");
7050 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7051 size = htons (message->size);
7053 /* Sanity check for client registration */
7054 if (NULL == (c = client_get (client)))
7057 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7062 /* Message size sanity check */
7063 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7066 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7070 /* Tunnel exists? */
7071 tid = ntohl (msg->tunnel_id);
7072 t = tunnel_get_by_local_id (c, tid);
7076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7080 /* Does client own tunnel? */
7081 if (t->owner->handle != client)
7084 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7089 " on tunnel %s [%u]\n",
7090 GNUNET_i2s(&my_full_id),
7093 /* Only one connect_by_string allowed at the same time! */
7094 /* FIXME: allow more, return handle at api level to cancel, document */
7095 if (NULL != t->regex_ctx)
7098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7102 /* Find string itself */
7103 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7104 string = (const char *) &msg[1];
7106 /* Initialize context */
7107 size = GNUNET_REGEX_get_first_key(string, len, &key);
7108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7109 " consumed %u bits out of %u\n", size, len);
7110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7111 " looking for %s\n", GNUNET_h2s (&key));
7113 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7115 info->description = GNUNET_malloc (len + 1);
7116 memcpy (info->description, string, len);
7117 info->description[len] = '\0';
7118 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7119 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7122 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7123 ctx->position = size;
7127 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7129 /* Start search in DHT */
7130 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7131 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7132 &key, /* key to search */
7133 dht_replication_level, /* replication level */
7134 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7135 NULL, /* xquery */ // FIXME BLOOMFILTER
7136 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7137 &dht_get_string_handler, ctx);
7139 GNUNET_break (GNUNET_OK ==
7140 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7143 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7145 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7151 * Handler for client traffic directed to one peer
7153 * @param cls closure
7154 * @param client identification of the client
7155 * @param message the actual message
7158 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7159 const struct GNUNET_MessageHeader *message)
7161 struct MeshClient *c;
7162 struct MeshTunnel *t;
7163 struct MeshPeerInfo *pi;
7164 struct GNUNET_MESH_Unicast *data_msg;
7165 MESH_TunnelNumber tid;
7168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7169 "Got a unicast request from a client!\n");
7171 /* Sanity check for client registration */
7172 if (NULL == (c = client_get (client)))
7175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7180 data_msg = (struct GNUNET_MESH_Unicast *) message;
7182 /* Sanity check for message size */
7183 size = ntohs (message->size);
7184 if (sizeof (struct GNUNET_MESH_Unicast) +
7185 sizeof (struct GNUNET_MessageHeader) > size)
7188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7192 /* Tunnel exists? */
7193 tid = ntohl (data_msg->tid);
7194 t = tunnel_get_by_local_id (c, tid);
7198 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7202 /* Is it a local tunnel? Then, does client own the tunnel? */
7203 if (t->owner->handle != client)
7206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7210 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7211 &data_msg->destination.hashPubKey);
7212 /* Is the selected peer in the tunnel? */
7216 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7220 /* PID should be as expected */
7221 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7224 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7225 "Unicast PID, expected %u, got %u\n",
7226 t->fwd_pid + 1, ntohl (data_msg->pid));
7227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7231 /* Ok, everything is correct, send the message
7232 * (pretend we got it from a mesh peer)
7235 /* Work around const limitation */
7236 char buf[ntohs (message->size)] GNUNET_ALIGN;
7237 struct GNUNET_MESH_Unicast *copy;
7239 copy = (struct GNUNET_MESH_Unicast *) buf;
7240 memcpy (buf, data_msg, size);
7241 copy->oid = my_full_id;
7242 copy->tid = htonl (t->id.tid);
7243 copy->ttl = htonl (default_ttl);
7244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7245 " calling generic handler...\n");
7246 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7249 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7256 * Handler for client traffic directed to the origin
7258 * @param cls closure
7259 * @param client identification of the client
7260 * @param message the actual message
7263 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7264 const struct GNUNET_MessageHeader *message)
7266 struct GNUNET_MESH_ToOrigin *data_msg;
7267 struct MeshTunnelClientInfo *clinfo;
7268 struct MeshClient *c;
7269 struct MeshTunnel *t;
7270 MESH_TunnelNumber tid;
7273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7274 "Got a ToOrigin request from a client!\n");
7275 /* Sanity check for client registration */
7276 if (NULL == (c = client_get (client)))
7279 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7284 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7286 /* Sanity check for message size */
7287 size = ntohs (message->size);
7288 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7289 sizeof (struct GNUNET_MessageHeader) > size)
7292 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7296 /* Tunnel exists? */
7297 tid = ntohl (data_msg->tid);
7298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7299 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7302 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7305 t = tunnel_get_by_local_id (c, tid);
7308 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7309 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7315 /* It should be sent by someone who has this as incoming tunnel. */
7316 if (GNUNET_NO == client_knows_tunnel (c, t))
7319 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7323 /* PID should be as expected */
7324 clinfo = tunnel_get_client_fc (t, c);
7325 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7328 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7329 "To Origin PID, expected %u, got %u\n",
7330 clinfo->bck_pid + 1,
7331 ntohl (data_msg->pid));
7332 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7336 /* Ok, everything is correct, send the message
7337 * (pretend we got it from a mesh peer)
7341 char buf[ntohs (message->size)] GNUNET_ALIGN;
7342 struct GNUNET_MESH_ToOrigin *copy;
7344 /* Work around const limitation */
7345 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7346 memcpy (buf, data_msg, size);
7347 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7348 copy->tid = htonl (t->id.tid);
7349 copy->ttl = htonl (default_ttl);
7350 if (ntohl (copy->pid) != (t->bck_pid + 1))
7353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7354 "To Origin PID, expected %u, got %u\n",
7360 copy->sender = my_full_id;
7361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7362 " calling generic handler...\n");
7363 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7365 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7372 * Handler for client traffic directed to all peers in a tunnel
7374 * @param cls closure
7375 * @param client identification of the client
7376 * @param message the actual message
7379 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7380 const struct GNUNET_MessageHeader *message)
7382 struct MeshClient *c;
7383 struct MeshTunnel *t;
7384 struct GNUNET_MESH_Multicast *data_msg;
7385 MESH_TunnelNumber tid;
7387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7388 "Got a multicast request from a client!\n");
7390 /* Sanity check for client registration */
7391 if (NULL == (c = client_get (client)))
7394 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7399 data_msg = (struct GNUNET_MESH_Multicast *) message;
7401 /* Sanity check for message size */
7402 if (sizeof (struct GNUNET_MESH_Multicast) +
7403 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7406 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7410 /* Tunnel exists? */
7411 tid = ntohl (data_msg->tid);
7412 t = tunnel_get_by_local_id (c, tid);
7416 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7423 /* Does client own tunnel? */
7424 if (t->owner->handle != client)
7427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7431 /* PID should be as expected */
7432 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7436 "Multicast PID, expected %u, got %u\n",
7437 t->fwd_pid + 1, ntohl (data_msg->pid));
7438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7443 char buf[ntohs (message->size)] GNUNET_ALIGN;
7444 struct GNUNET_MESH_Multicast *copy;
7446 copy = (struct GNUNET_MESH_Multicast *) buf;
7447 memcpy (buf, message, ntohs (message->size));
7448 copy->oid = my_full_id;
7449 copy->tid = htonl (t->id.tid);
7450 copy->ttl = htonl (default_ttl);
7451 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7453 " calling generic handler...\n");
7454 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7457 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7463 * Handler for client's ACKs for payload traffic.
7465 * @param cls Closure (unused).
7466 * @param client Identification of the client.
7467 * @param message The actual message.
7470 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7471 const struct GNUNET_MessageHeader *message)
7473 struct GNUNET_MESH_LocalAck *msg;
7474 struct MeshTunnel *t;
7475 struct MeshClient *c;
7476 MESH_TunnelNumber tid;
7479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7480 /* Sanity check for client registration */
7481 if (NULL == (c = client_get (client)))
7484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7489 msg = (struct GNUNET_MESH_LocalAck *) message;
7491 /* Tunnel exists? */
7492 tid = ntohl (msg->tunnel_id);
7493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7494 t = tunnel_get_by_local_id (c, tid);
7498 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7499 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7504 ack = ntohl (msg->max_pid);
7505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7507 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7508 if (NULL != t->owner && t->owner->handle == client)
7510 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7512 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7516 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7517 tunnel_set_client_fwd_ack (t, c, ack);
7518 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7521 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7528 * Functions to handle messages from clients
7530 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7531 {&handle_local_new_client, NULL,
7532 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7533 {&handle_local_announce_regex, NULL,
7534 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7535 {&handle_local_tunnel_create, NULL,
7536 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7537 sizeof (struct GNUNET_MESH_TunnelMessage)},
7538 {&handle_local_tunnel_destroy, NULL,
7539 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7540 sizeof (struct GNUNET_MESH_TunnelMessage)},
7541 {&handle_local_tunnel_speed, NULL,
7542 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7543 sizeof (struct GNUNET_MESH_TunnelMessage)},
7544 {&handle_local_tunnel_speed, NULL,
7545 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7546 sizeof (struct GNUNET_MESH_TunnelMessage)},
7547 {&handle_local_tunnel_buffer, NULL,
7548 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7549 sizeof (struct GNUNET_MESH_TunnelMessage)},
7550 {&handle_local_tunnel_buffer, NULL,
7551 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7552 sizeof (struct GNUNET_MESH_TunnelMessage)},
7553 {&handle_local_connect_add, NULL,
7554 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7555 sizeof (struct GNUNET_MESH_PeerControl)},
7556 {&handle_local_connect_del, NULL,
7557 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7558 sizeof (struct GNUNET_MESH_PeerControl)},
7559 {&handle_local_blacklist, NULL,
7560 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7561 sizeof (struct GNUNET_MESH_PeerControl)},
7562 {&handle_local_unblacklist, NULL,
7563 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7564 sizeof (struct GNUNET_MESH_PeerControl)},
7565 {&handle_local_connect_by_type, NULL,
7566 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7567 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7568 {&handle_local_connect_by_string, NULL,
7569 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7570 {&handle_local_unicast, NULL,
7571 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7572 {&handle_local_to_origin, NULL,
7573 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7574 {&handle_local_multicast, NULL,
7575 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7576 {&handle_local_ack, NULL,
7577 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7578 sizeof (struct GNUNET_MESH_LocalAck)},
7584 * To be called on core init/fail.
7586 * @param cls service closure
7587 * @param server handle to the server for this service
7588 * @param identity the public identity of this peer
7591 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7592 const struct GNUNET_PeerIdentity *identity)
7595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7596 core_handle = server;
7597 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7600 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7601 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7603 GNUNET_abort(); // Try harder
7610 * Method called whenever a given peer connects.
7612 * @param cls closure
7613 * @param peer peer identity this notification is about
7614 * @param atsi performance data for the connection
7615 * @param atsi_count number of records in 'atsi'
7618 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7619 const struct GNUNET_ATS_Information *atsi,
7620 unsigned int atsi_count)
7622 struct MeshPeerInfo *peer_info;
7623 struct MeshPeerPath *path;
7625 DEBUG_CONN ("Peer connected\n");
7626 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7627 peer_info = peer_info_get (peer);
7628 if (myid == peer_info->id)
7630 DEBUG_CONN (" (self)\n");
7635 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7637 path = path_new (2);
7638 path->peers[0] = myid;
7639 path->peers[1] = peer_info->id;
7640 GNUNET_PEER_change_rc (myid, 1);
7641 GNUNET_PEER_change_rc (peer_info->id, 1);
7642 peer_info_add_path (peer_info, path, GNUNET_YES);
7643 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7649 * Method called whenever a peer disconnects.
7651 * @param cls closure
7652 * @param peer peer identity this notification is about
7655 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7657 struct MeshPeerInfo *pi;
7658 struct MeshPeerQueue *q;
7659 struct MeshPeerQueue *n;
7661 DEBUG_CONN ("Peer disconnected\n");
7662 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7674 /* try to reroute this traffic instead */
7675 queue_destroy(q, GNUNET_YES);
7679 peer_info_remove_path (pi, pi->id, myid);
7682 DEBUG_CONN (" (self)\n");
7684 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7689 /******************************************************************************/
7690 /************************ MAIN FUNCTIONS ****************************/
7691 /******************************************************************************/
7694 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7696 * @param cls closure
7697 * @param key current key code
7698 * @param value value in the hash map
7699 * @return GNUNET_YES if we should continue to iterate,
7703 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7705 struct MeshTunnel *t = value;
7712 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7714 * @param cls closure
7715 * @param key current key code
7716 * @param value value in the hash map
7717 * @return GNUNET_YES if we should continue to iterate,
7721 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7723 struct MeshPeerInfo *p = value;
7724 struct MeshPeerQueue *q;
7725 struct MeshPeerQueue *n;
7733 queue_destroy(q, GNUNET_YES);
7737 peer_info_destroy (p);
7743 * Task run during shutdown.
7749 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7753 if (core_handle != NULL)
7755 GNUNET_CORE_disconnect (core_handle);
7760 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
7763 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7764 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7765 if (dht_handle != NULL)
7767 GNUNET_DHT_disconnect (dht_handle);
7772 GNUNET_SERVER_notification_context_destroy (nc);
7775 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7777 GNUNET_SCHEDULER_cancel (announce_id_task);
7778 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7785 * Callback for hostkey read/generation
7788 * @param pk the private key
7789 * @param emsg error message
7792 key_generation_cb (void *cls,
7793 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
7796 struct MeshPeerInfo *peer;
7797 struct MeshPeerPath *p;
7802 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7803 _("Mesh service could not access hostkey. Exiting.\n"));
7804 GNUNET_SCHEDULER_shutdown ();
7807 my_private_key = pk;
7808 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7809 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7810 &my_full_id.hashPubKey);
7811 myid = GNUNET_PEER_intern (&my_full_id);
7812 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7813 "Mesh for peer [%s] starting\n",
7814 GNUNET_i2s(&my_full_id));
7816 // transport_handle = GNUNET_TRANSPORT_connect(c,
7826 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7829 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7830 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7831 GNUNET_SERVER_disconnect_notify (server_handle,
7832 &handle_local_client_disconnect, NULL);
7836 clients_tail = NULL;
7839 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7840 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7842 /* Create a peer_info for the local peer */
7843 peer = peer_info_get (&my_full_id);
7846 GNUNET_PEER_change_rc (myid, 1);
7847 peer_info_add_path (peer, p, GNUNET_YES);
7848 GNUNET_SERVER_resume (server_handle);
7849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7854 * Process mesh requests.
7856 * @param cls closure
7857 * @param server the initialized server
7858 * @param c configuration to use
7861 run (void *cls, struct GNUNET_SERVER_Handle *server,
7862 const struct GNUNET_CONFIGURATION_Handle *c)
7866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7867 server_handle = server;
7868 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7869 NULL, /* Closure passed to MESH functions */
7870 &core_init, /* Call core_init once connected */
7871 &core_connect, /* Handle connects */
7872 &core_disconnect, /* remove peers on disconnects */
7873 NULL, /* Don't notify about all incoming messages */
7874 GNUNET_NO, /* For header only in notification */
7875 NULL, /* Don't notify about all outbound messages */
7876 GNUNET_NO, /* For header-only out notification */
7877 core_handlers); /* Register these handlers */
7879 if (core_handle == NULL)
7882 GNUNET_SCHEDULER_shutdown ();
7887 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7892 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7894 GNUNET_SCHEDULER_shutdown ();
7899 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7900 &refresh_path_time))
7902 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7904 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7905 "refresh path time");
7906 GNUNET_SCHEDULER_shutdown ();
7911 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7912 &app_announce_time))
7914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7916 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7917 "app announce time");
7918 GNUNET_SCHEDULER_shutdown ();
7923 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7926 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7928 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7929 "id announce time");
7930 GNUNET_SCHEDULER_shutdown ();
7935 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7936 &unacknowledged_wait_time))
7938 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7940 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7941 "unacknowledged wait time");
7942 GNUNET_SCHEDULER_shutdown ();
7947 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7950 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7952 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7954 GNUNET_SCHEDULER_shutdown ();
7959 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7962 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7964 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7966 GNUNET_SCHEDULER_shutdown ();
7971 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7974 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7976 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7978 GNUNET_SCHEDULER_shutdown ();
7983 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7986 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7988 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7994 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7995 &dht_replication_level))
7997 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7999 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
8000 "dht replication level", 10);
8001 dht_replication_level = 10;
8004 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8005 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8006 peers = GNUNET_CONTAINER_multihashmap_create (32);
8007 applications = GNUNET_CONTAINER_multihashmap_create (32);
8008 types = GNUNET_CONTAINER_multihashmap_create (32);
8010 dht_handle = GNUNET_DHT_connect (c, 64);
8011 if (NULL == dht_handle)
8015 stats = GNUNET_STATISTICS_create ("mesh", c);
8017 GNUNET_SERVER_suspend (server_handle);
8018 /* Scheduled the task to clean up when shutdown is called */
8019 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8021 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8022 GNUNET_free (keyfile);
8027 * The main function for the mesh service.
8029 * @param argc number of arguments from the command line
8030 * @param argv command line arguments
8031 * @return 0 ok, 1 on error
8034 main (int argc, char *const *argv)
8038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8041 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8047 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8048 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8049 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);