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 for all
516 * payload traffic going to this child.
517 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
519 struct MeshPeerQueue **send_buffer;
522 * Index of the oldest element in the send_buffer.
524 unsigned int send_buffer_start;
527 * How many elements are already in the buffer.
529 unsigned int send_buffer_n;
534 * Info about a leaf client of a tunnel, needed to perform flow control.
536 struct MeshTunnelClientInfo
539 * PID of the last packet sent to the client (FWD).
544 * PID of the last packet received from the client (BCK).
549 * Maximum PID allowed (FWD ACK received).
554 * Last ACK sent to that child (BCK ACK).
562 * Info collected during iteration of child nodes in order to get the ACK value
565 struct MeshTunnelChildIteratorContext
568 * Tunnel whose info is being collected.
570 struct MeshTunnel *t;
573 * Is this context initialized? Is the value in max_child_ack valid?
578 * Maximum child ACK so far.
580 uint32_t max_child_ack;
583 * Number of children nodes
585 unsigned int nchildren;
590 * Info needed to work with tunnel paths and peers
597 struct MeshTunnel *t;
600 * Neighbouring peer to whom we send the packet to
602 struct MeshPeerInfo *peer;
607 struct MeshPeerPath *path;
612 * Struct containing information about a client of the service
619 struct MeshClient *next;
624 struct MeshClient *prev;
627 * Tunnels that belong to this client, indexed by local id
629 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
632 * Tunnels this client has accepted, indexed by incoming local id
634 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
637 * Tunnels this client has rejected, indexed by incoming local id
639 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
641 * Handle to communicate with the client
643 struct GNUNET_SERVER_Client *handle;
646 * Applications that this client has claimed to provide
648 struct GNUNET_CONTAINER_MultiHashMap *apps;
651 * Messages that this client has declared interest in
653 struct GNUNET_CONTAINER_MultiHashMap *types;
656 * Whether the client is active or shutting down (don't send confirmations
657 * to a client that is shutting down.
662 * ID of the client, mainly for debug messages
667 * Regular expressions describing the services offered by this client.
669 char **regexes; // FIXME add timeout? API to remove a regex?
672 * Number of regular expressions in regexes.
674 unsigned int n_regex;
677 * Task to refresh all regular expresions in the DHT.
679 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
685 * Struct to keep information of searches of services described by a regex
686 * using a user-provided string service description.
688 struct MeshRegexSearchInfo
691 * Which tunnel is this for
693 struct MeshTunnel *t;
696 * User provided description of the searched service.
701 * Part of the description already consumed by the search.
708 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
711 * Results from running DHT GETs.
713 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
716 * Contexts, for each running DHT GET. Free all on end of search.
718 struct MeshRegexSearchContext **contexts;
721 * Number of contexts (branches/steps in search).
723 unsigned int n_contexts;
726 * Peer that is connecting via connect_by_string. When connected, free ctx.
731 * Other peers that are found but not yet being connected to.
733 GNUNET_PEER_Id *peers;
736 * Number of elements in peers.
738 unsigned int n_peers;
741 * Next peer to try to connect to.
746 * Timeout for a connect attempt.
747 * When reached, try to connect to a different peer, if any. If not,
748 * try the same peer again.
750 GNUNET_SCHEDULER_TaskIdentifier timeout;
755 * Struct to keep state of running searches that have consumed a part of
758 struct MeshRegexSearchContext
761 * Part of the description already consumed by
762 * this particular search branch.
767 * Information about the search.
769 struct MeshRegexSearchInfo *info;
772 * We just want to look for one edge, the longer the better.
775 unsigned int longest_match;
778 * Destination hash of the longest match.
780 struct GNUNET_HashCode hash;
783 /******************************************************************************/
784 /************************ DEBUG FUNCTIONS ****************************/
785 /******************************************************************************/
789 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
790 * @param cls string to print
791 * @param success GNUNET_OK if the PUT was transmitted,
792 * GNUNET_NO on timeout,
793 * GNUNET_SYSERR on disconnect from service
794 * after the PUT message was transmitted
795 * (so we don't know if it was received or not)
800 mesh_debug (void *cls, int success)
804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
808 unsigned int debug_fwd_ack;
809 unsigned int debug_bck_ack;
813 /******************************************************************************/
814 /*********************** GLOBAL VARIABLES ****************************/
815 /******************************************************************************/
818 * Configuration parameters
820 static struct GNUNET_TIME_Relative refresh_path_time;
821 static struct GNUNET_TIME_Relative app_announce_time;
822 static struct GNUNET_TIME_Relative id_announce_time;
823 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
824 static struct GNUNET_TIME_Relative connect_timeout;
825 static long long unsigned int default_ttl;
826 static long long unsigned int dht_replication_level;
827 static long long unsigned int max_tunnels;
828 static long long unsigned int max_msgs_queue;
832 * Hostkey generation context
834 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
837 * DLL with all the clients, head.
839 static struct MeshClient *clients;
842 * DLL with all the clients, tail.
844 static struct MeshClient *clients_tail;
847 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
849 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
852 * Number of tunnels known.
854 static unsigned long long n_tunnels;
857 * Tunnels incoming, indexed by MESH_TunnelNumber
858 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
860 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
863 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
865 static struct GNUNET_CONTAINER_MultiHashMap *peers;
868 * Handle to communicate with transport
870 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
873 * Handle to communicate with core.
875 static struct GNUNET_CORE_Handle *core_handle;
880 static struct GNUNET_DHT_Handle *dht_handle;
885 static struct GNUNET_SERVER_Handle *server_handle;
888 * Handle to the statistics service.
890 static struct GNUNET_STATISTICS_Handle *stats;
893 * Notification context, to send messages to local clients.
895 static struct GNUNET_SERVER_NotificationContext *nc;
898 * Local peer own ID (memory efficient handle).
900 static GNUNET_PEER_Id myid;
903 * Local peer own ID (full value).
905 static struct GNUNET_PeerIdentity my_full_id;
910 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
915 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
918 * Tunnel ID for the next created tunnel (global tunnel number).
920 static MESH_TunnelNumber next_tid;
923 * Tunnel ID for the next incoming tunnel (local tunnel number).
925 static MESH_TunnelNumber next_local_tid;
928 * All application types provided by this peer.
930 static struct GNUNET_CONTAINER_MultiHashMap *applications;
933 * All message types clients of this peer are interested in.
935 static struct GNUNET_CONTAINER_MultiHashMap *types;
938 * Task to periodically announce provided applications.
940 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
943 * Task to periodically announce itself in the network.
945 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
948 * Next ID to assign to a client.
950 unsigned int next_client_id;
953 /******************************************************************************/
954 /*********************** DECLARATIONS **************************/
955 /******************************************************************************/
957 /* FIXME move declarations here */
960 * Function to process paths received for a new peer addition. The recorded
961 * paths form the initial tunnel, which can be optimized later.
962 * Called on each result obtained for the DHT search.
965 * @param exp when will this value expire
966 * @param key key of the result
967 * @param type type of the result
968 * @param size number of bytes in data
969 * @param data pointer to the result data
972 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
973 const struct GNUNET_HashCode * key,
974 const struct GNUNET_PeerIdentity *get_path,
975 unsigned int get_path_length,
976 const struct GNUNET_PeerIdentity *put_path,
977 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
978 size_t size, const void *data);
982 * Function to process DHT string to regex matching.
983 * Called on each result obtained for the DHT search.
985 * @param cls closure (search context)
986 * @param exp when will this value expire
987 * @param key key of the result
988 * @param get_path path of the get request (not used)
989 * @param get_path_length lenght of get_path (not used)
990 * @param put_path path of the put request (not used)
991 * @param put_path_length length of the put_path (not used)
992 * @param type type of the result
993 * @param size number of bytes in data
994 * @param data pointer to the result data
996 * TODO: re-issue the request after certain time? cancel after X results?
999 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1000 const struct GNUNET_HashCode * key,
1001 const struct GNUNET_PeerIdentity *get_path,
1002 unsigned int get_path_length,
1003 const struct GNUNET_PeerIdentity *put_path,
1004 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1005 size_t size, const void *data);
1009 * Function to process DHT string to regex matching.
1010 * Called on each result obtained for the DHT search.
1012 * @param cls closure (search context)
1013 * @param exp when will this value expire
1014 * @param key key of the result
1015 * @param get_path path of the get request (not used)
1016 * @param get_path_length lenght of get_path (not used)
1017 * @param put_path path of the put request (not used)
1018 * @param put_path_length length of the put_path (not used)
1019 * @param type type of the result
1020 * @param size number of bytes in data
1021 * @param data pointer to the result data
1024 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1025 const struct GNUNET_HashCode * key,
1026 const struct GNUNET_PeerIdentity *get_path,
1027 unsigned int get_path_length,
1028 const struct GNUNET_PeerIdentity *put_path,
1029 unsigned int put_path_length,
1030 enum GNUNET_BLOCK_Type type,
1031 size_t size, const void *data);
1035 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1036 * and insert it in the appropiate structures if the peer is not known yet.
1038 * @param peer Short identity of the peer.
1040 * @return Existing or newly created peer info.
1042 static struct MeshPeerInfo *
1043 peer_info_get_short (const GNUNET_PEER_Id peer);
1047 * Try to establish a new connection to this peer.
1048 * Use the best path for the given tunnel.
1049 * If the peer doesn't have any path to it yet, try to get one.
1050 * If the peer already has some path, send a CREATE PATH towards it.
1052 * @param peer PeerInfo of the peer.
1053 * @param t Tunnel for which to create the path, if possible.
1056 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1060 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1061 * needed rescources.
1062 * If peer already exists, reevaluate shortest path and change if different.
1064 * @param t Tunnel we want to add a new peer to
1065 * @param peer PeerInfo of the peer being added
1069 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1073 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1074 * that are no longer needed, as well as nodes of no longer reachable peers.
1075 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1077 * @param t Tunnel from which to remove the path.
1078 * @param peer Short id of the peer which should be removed.
1081 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1085 * Search for a tunnel by global ID using full PeerIdentities.
1087 * @param oid owner of the tunnel.
1088 * @param tid global tunnel number.
1090 * @return tunnel handler, NULL if doesn't exist.
1092 static struct MeshTunnel *
1093 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1097 * Delete an active client from the tunnel.
1103 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1106 * Notify a tunnel that a connection has broken that affects at least
1107 * some of its peers.
1109 * @param t Tunnel affected.
1110 * @param p1 Peer that got disconnected from p2.
1111 * @param p2 Peer that got disconnected from p1.
1113 * @return Short ID of the peer disconnected (either p1 or p2).
1114 * 0 if the tunnel remained unaffected.
1116 static GNUNET_PEER_Id
1117 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1122 * Get the current ack value for a tunnel, for data going from root to leaves,
1123 * taking in account the tunnel mode and the status of all children and clients.
1127 * @return Maximum PID allowed.
1130 tunnel_get_fwd_ack (struct MeshTunnel *t);
1134 * Add a client to a tunnel, initializing all needed data structures.
1136 * @param t Tunnel to which add the client.
1137 * @param c Client which to add to the tunnel.
1140 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1144 * Jump to the next edge, with the longest matching token.
1146 * @param block Block found in the DHT.
1147 * @param size Size of the block.
1148 * @param ctx Context of the search.
1150 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1153 regex_next_edge (const struct MeshRegexBlock *block,
1155 struct MeshRegexSearchContext *ctx);
1159 * Find a path to a peer that offers a regex servcie compatible
1160 * with a given string.
1162 * @param key The key of the accepting state.
1163 * @param ctx Context containing info about the string, tunnel, etc.
1166 regex_find_path (const struct GNUNET_HashCode *key,
1167 struct MeshRegexSearchContext *ctx);
1171 * @brief Queue and pass message to core when possible.
1173 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1174 * and accounts for it. In case the queue is full, the message is dropped and
1177 * Otherwise, message is treated as internal and allowed to go regardless of
1180 * @param cls Closure (@c type dependant). It will be used by queue_send to
1181 * build the message to be sent if not already prebuilt.
1182 * @param type Type of the message, 0 for a raw message.
1183 * @param size Size of the message.
1184 * @param dst Neighbor to send message to.
1185 * @param t Tunnel this message belongs to.
1188 queue_add (void *cls, uint16_t type, size_t size,
1189 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1193 * Free a transmission that was already queued with all resources
1194 * associated to the request.
1196 * @param queue Queue handler to cancel.
1197 * @param clear_cls Is it necessary to free associated cls?
1200 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1204 * @brief Get the next transmittable message from the queue.
1206 * This will be the head, except in the case of being a data packet
1207 * not allowed by the destination peer.
1209 * @param peer Destination peer.
1211 * @return The next viable MeshPeerQueue element to send to that peer.
1212 * NULL when there are no transmittable messages.
1214 struct MeshPeerQueue *
1215 queue_get_next (const struct MeshPeerInfo *peer);
1219 * Core callback to write a queued packet to core buffer
1221 * @param cls Closure (peer info).
1222 * @param size Number of bytes available in buf.
1223 * @param buf Where the to write the message.
1225 * @return number of bytes written to buf
1228 queue_send (void *cls, size_t size, void *buf);
1230 /******************************************************************************/
1231 /************************ ITERATORS ****************************/
1232 /******************************************************************************/
1235 * Iterator over found existing mesh regex blocks that match an ongoing search.
1237 * @param cls closure
1238 * @param key current key code
1239 * @param value value in the hash map
1240 * @return GNUNET_YES if we should continue to iterate,
1244 regex_result_iterator (void *cls,
1245 const struct GNUNET_HashCode * key,
1248 struct MeshRegexBlock *block = value;
1249 struct MeshRegexSearchContext *ctx = cls;
1251 if (GNUNET_YES == ntohl(block->accepting) &&
1252 ctx->position == strlen (ctx->info->description))
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1255 regex_find_path (key, ctx);
1256 return GNUNET_YES; // We found an accept state!
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1261 ctx->position, strlen(ctx->info->description),
1262 ntohl(block->accepting));
1265 regex_next_edge(block, SIZE_MAX, ctx);
1272 * Iterator over edges in a regex block retrieved from the DHT.
1274 * @param cls Closure (context of the search).
1275 * @param token Token that follows to next state.
1276 * @param len Lenght of token.
1277 * @param key Hash of next state.
1279 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1282 regex_edge_iterator (void *cls,
1285 const struct GNUNET_HashCode *key)
1287 struct MeshRegexSearchContext *ctx = cls;
1288 struct MeshRegexSearchInfo *info = ctx->info;
1292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1295 current = &info->description[ctx->position];
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1297 current_len = strlen (info->description) - ctx->position;
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1302 if (len > current_len)
1304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1305 return GNUNET_YES; // Token too long, wont match
1307 if (0 != strncmp (current, token, len))
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1310 return GNUNET_YES; // Token doesn't match
1313 if (len > ctx->longest_match)
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1316 ctx->longest_match = len;
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1330 * Jump to the next edge, with the longest matching token.
1332 * @param block Block found in the DHT.
1333 * @param size Size of the block.
1334 * @param ctx Context of the search.
1336 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1339 regex_next_edge (const struct MeshRegexBlock *block,
1341 struct MeshRegexSearchContext *ctx)
1343 struct MeshRegexSearchContext *new_ctx;
1344 struct MeshRegexSearchInfo *info = ctx->info;
1345 struct GNUNET_DHT_GetHandle *get_h;
1349 /* Find the longest match for the current string position,
1350 * among tokens in the given block */
1351 ctx->longest_match = 0;
1352 result = GNUNET_MESH_regex_block_iterate (block, size,
1353 ®ex_edge_iterator, ctx);
1354 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1356 /* Did anything match? */
1357 if (0 == ctx->longest_match)
1360 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1361 new_ctx->info = info;
1362 new_ctx->position = ctx->position + ctx->longest_match;
1363 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1365 /* Check whether we already have a DHT GET running for it */
1367 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1370 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1372 ®ex_result_iterator,
1374 return; // We are already looking for it
1377 /* Start search in DHT */
1379 GNUNET_DHT_get_start (dht_handle, /* handle */
1380 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1381 &ctx->hash, /* key to search */
1382 dht_replication_level, /* replication level */
1383 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1384 NULL, /* xquery */ // FIXME BLOOMFILTER
1385 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1386 &dht_get_string_handler, new_ctx);
1388 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1391 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1400 * Iterator over hash map entries to cancel DHT GET requests after a
1401 * successful connect_by_string.
1403 * @param cls Closure (unused).
1404 * @param key Current key code (unused).
1405 * @param value Value in the hash map (get handle).
1406 * @return GNUNET_YES if we should continue to iterate,
1410 regex_cancel_dht_get (void *cls,
1411 const struct GNUNET_HashCode * key,
1414 struct GNUNET_DHT_GetHandle *h = value;
1416 GNUNET_DHT_get_stop (h);
1422 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1423 * search for connect_by_string.
1425 * @param cls Closure (unused).
1426 * @param key Current key code (unused).
1427 * @param value MeshRegexBlock in the hash map.
1428 * @return GNUNET_YES if we should continue to iterate,
1432 regex_free_result (void *cls,
1433 const struct GNUNET_HashCode * key,
1437 GNUNET_free (value);
1443 * Regex callback iterator to store own service description in the DHT.
1445 * @param cls closure.
1446 * @param key hash for current state.
1447 * @param proof proof for current state.
1448 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1449 * @param num_edges number of edges leaving current state.
1450 * @param edges edges leaving current state.
1453 regex_iterator (void *cls,
1454 const struct GNUNET_HashCode *key,
1457 unsigned int num_edges,
1458 const struct GNUNET_REGEX_Edge *edges)
1460 struct MeshRegexBlock *block;
1461 struct MeshRegexEdge *block_edge;
1462 enum GNUNET_DHT_RouteOption opt;
1466 unsigned int offset;
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 " regex dht put for state %s\n",
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1479 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1480 if (GNUNET_YES == accepting)
1482 struct MeshRegexAccept block;
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1485 " state %s is accepting, putting own id\n",
1487 size = sizeof (block);
1489 block.id = my_full_id;
1491 GNUNET_DHT_put(dht_handle, key,
1492 dht_replication_level,
1493 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1494 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1497 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1502 len = strlen(proof);
1503 size = sizeof (struct MeshRegexBlock) + len;
1504 block = GNUNET_malloc (size);
1507 block->n_proof = htonl (len);
1508 block->n_edges = htonl (num_edges);
1509 block->accepting = htonl (accepting);
1511 /* Store the proof at the end of the block. */
1512 aux = (char *) &block[1];
1513 memcpy (aux, proof, len);
1516 /* Store each edge in a variable length MeshEdge struct at the
1517 * very end of the MeshRegexBlock structure.
1519 for (i = 0; i < num_edges; i++)
1521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1522 " edge %s towards %s\n",
1524 GNUNET_h2s(&edges[i].destination));
1526 /* aux points at the end of the last block */
1527 len = strlen (edges[i].label);
1528 size += sizeof (struct MeshRegexEdge) + len;
1529 // Calculate offset FIXME is this ok? use size instead?
1530 offset = aux - (char *) block;
1531 block = GNUNET_realloc (block, size);
1532 aux = &((char *) block)[offset];
1533 block_edge = (struct MeshRegexEdge *) aux;
1534 block_edge->key = edges[i].destination;
1535 block_edge->n_token = htonl (len);
1536 aux = (char *) &block_edge[1];
1537 memcpy (aux, edges[i].label, len);
1541 GNUNET_DHT_put(dht_handle, key,
1542 dht_replication_level,
1544 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1546 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1550 GNUNET_free (block);
1555 * Store the regular expression describing a local service into the DHT.
1557 * @param regex The regular expresion.
1560 regex_put (const char *regex)
1562 struct GNUNET_REGEX_Automaton *dfa;
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1565 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1566 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1567 GNUNET_REGEX_automaton_destroy (dfa);
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1573 * Find a path to a peer that offers a regex servcie compatible
1574 * with a given string.
1576 * @param key The key of the accepting state.
1577 * @param ctx Context containing info about the string, tunnel, etc.
1580 regex_find_path (const struct GNUNET_HashCode *key,
1581 struct MeshRegexSearchContext *ctx)
1583 struct GNUNET_DHT_GetHandle *get_h;
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1586 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1587 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1588 key, /* key to search */
1589 dht_replication_level, /* replication level */
1590 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1591 GNUNET_DHT_RO_RECORD_ROUTE,
1592 NULL, /* xquery */ // FIXME BLOOMFILTER
1593 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1594 &dht_get_string_accept_handler, ctx);
1595 GNUNET_break (GNUNET_OK ==
1596 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1599 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1604 * Function called if the connect attempt to a peer found via
1605 * connect_by_string times out. Try to connect to another peer, if any.
1606 * Otherwise try to reconnect to the same peer.
1608 * @param cls Closure (info about regex search).
1609 * @param tc TaskContext.
1612 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1614 struct MeshRegexSearchInfo *info = cls;
1615 struct MeshPeerInfo *peer_info;
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1620 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1621 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1629 if (0 < info->n_peers)
1631 // Select next peer, put current in that spot.
1632 id = info->peers[info->i_peer];
1633 info->peers[info->i_peer] = info->peer;
1634 info->i_peer = (info->i_peer + 1) % info->n_peers;
1638 // Try to connect to same peer again.
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1643 peer_info = peer_info_get_short(id);
1644 tunnel_add_peer (info->t, peer_info);
1646 tunnel_delete_peer (info->t, old);
1647 peer_info_connect (peer_info, info->t);
1648 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1649 ®ex_connect_timeout,
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1656 * Cancel an ongoing regex search in the DHT and free all resources.
1658 * @param ctx The search context.
1661 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1663 struct MeshRegexSearchInfo *info = ctx->info;
1666 GNUNET_free (info->description);
1667 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1668 ®ex_cancel_dht_get, NULL);
1669 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1670 ®ex_free_result, NULL);
1671 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1672 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1673 info->t->regex_ctx = NULL;
1674 for (i = 0; i < info->n_contexts; i++)
1676 GNUNET_free (info->contexts[i]);
1678 if (0 < info->n_contexts)
1679 GNUNET_free (info->contexts);
1680 if (0 < info->n_peers)
1681 GNUNET_free (info->peers);
1682 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1684 GNUNET_SCHEDULER_cancel(info->timeout);
1690 /******************************************************************************/
1691 /************************ PERIODIC FUNCTIONS ****************************/
1692 /******************************************************************************/
1695 * Announce iterator over for each application provided by the peer
1697 * @param cls closure
1698 * @param key current key code
1699 * @param value value in the hash map
1700 * @return GNUNET_YES if we should continue to
1705 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1707 struct PBlock block;
1708 struct MeshClient *c;
1710 block.id = my_full_id;
1711 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1712 GNUNET_assert(NULL != c);
1713 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1714 if (0 == block.type)
1719 block.type = htonl (block.type);
1721 GNUNET_break (NULL !=
1722 GNUNET_DHT_put (dht_handle, key,
1723 dht_replication_level,
1724 GNUNET_DHT_RO_RECORD_ROUTE |
1725 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1726 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1728 (const char *) &block,
1729 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1731 app_announce_time, NULL, NULL));
1737 * Periodically announce what applications are provided by local clients
1740 * @param cls closure
1741 * @param tc task context
1744 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1746 struct MeshClient *c = cls;
1749 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1751 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1755 DEBUG_DHT ("Starting PUT for regex\n");
1757 for (i = 0; i < c->n_regex; i++)
1759 regex_put (c->regexes[i]);
1761 c->regex_announce_task =
1762 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1763 DEBUG_DHT ("Finished PUT for regex\n");
1770 * Periodically announce what applications are provided by local clients
1773 * @param cls closure
1774 * @param tc task context
1777 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1779 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1781 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1785 DEBUG_DHT ("Starting PUT for apps\n");
1787 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1789 announce_applications_task =
1790 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1792 DEBUG_DHT ("Finished PUT for apps\n");
1799 * Periodically announce self id in the DHT
1801 * @param cls closure
1802 * @param tc task context
1805 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1807 struct PBlock block;
1809 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1811 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1815 * - Set data expiration in function of X
1816 * - Adapt X to churn
1818 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1820 block.id = my_full_id;
1821 block.type = htonl (0);
1822 GNUNET_DHT_put (dht_handle, /* DHT handle */
1823 &my_full_id.hashPubKey, /* Key to use */
1824 dht_replication_level, /* Replication level */
1825 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1826 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1827 sizeof (block), /* Size of the data */
1828 (const char *) &block, /* Data itself */
1829 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1830 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1831 NULL, /* Continuation */
1832 NULL); /* Continuation closure */
1834 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1838 /******************************************************************************/
1839 /****************** GENERAL HELPER FUNCTIONS ************************/
1840 /******************************************************************************/
1843 * Decrements the reference counter and frees all resources if needed
1845 * @param mesh_data Data Descriptor used in a multicast message.
1846 * Freed no longer needed (last message).
1849 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1851 if (0 == --(mesh_data->reference_counter))
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1854 GNUNET_free (mesh_data->data);
1855 GNUNET_free (mesh_data);
1861 * Check if client has registered with the service and has not disconnected
1863 * @param client the client to check
1865 * @return non-NULL if client exists in the global DLL
1867 static struct MeshClient *
1868 client_get (struct GNUNET_SERVER_Client *client)
1870 struct MeshClient *c;
1875 if (c->handle == client)
1884 * Checks if a given client has subscribed to certain message type
1886 * @param message_type Type of message to check
1887 * @param c Client to check
1889 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1891 * FIXME: use of crypto_hash slows it down
1892 * The hash function alone takes 8-10us out of the ~55us for the whole
1893 * process of retransmitting the message from one local client to another.
1894 * Find faster implementation!
1897 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1899 struct GNUNET_HashCode hc;
1901 if (NULL == c->types)
1904 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1905 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1910 * Check whether client wants traffic from a tunnel.
1912 * @param c Client to check.
1913 * @param t Tunnel to be found.
1915 * @return GNUNET_YES if client knows tunnel.
1917 * TODO look in client hashmap
1920 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1924 for (i = 0; i < t->nclients; i++)
1925 if (t->clients[i] == c)
1932 * Check whether client has been informed about a tunnel.
1934 * @param c Client to check.
1935 * @param t Tunnel to be found.
1937 * @return GNUNET_YES if client knows tunnel.
1939 * TODO look in client hashmap
1942 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1946 for (i = 0; i < t->nignore; i++)
1947 if (t->ignore[i] == c)
1949 return client_wants_tunnel(c, t);
1954 * Marks a client as uninterested in traffic from the tunnel, updating both
1955 * client and tunnel to reflect this.
1957 * @param c Client that doesn't want traffic anymore.
1958 * @param t Tunnel which should be ignored.
1960 * FIXME when to delete an incoming tunnel?
1963 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1965 struct GNUNET_HashCode hash;
1967 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1968 GNUNET_break (GNUNET_YES ==
1969 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1971 GNUNET_break (GNUNET_YES ==
1972 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1973 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1974 tunnel_delete_active_client (t, c);
1975 GNUNET_array_append (t->ignore, t->nignore, c);
1980 * Deletes a tunnel from a client (either owner or destination). To be used on
1981 * tunnel destroy, otherwise, use client_ignore_tunnel.
1983 * @param c Client whose tunnel to delete.
1984 * @param t Tunnel which should be deleted.
1987 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1989 struct GNUNET_HashCode hash;
1993 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1994 GNUNET_assert (GNUNET_YES ==
1995 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2001 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2003 GNUNET_assert (GNUNET_YES ==
2004 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2008 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2016 * Send the message to all clients that have subscribed to its type
2018 * @param msg Pointer to the message itself
2019 * @param payload Pointer to the payload of the message.
2020 * @param t The tunnel to whose clients this message goes.
2022 * @return number of clients this message was sent to
2025 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2026 const struct GNUNET_MessageHeader *payload,
2027 struct MeshTunnel *t)
2029 struct MeshClient *c;
2030 MESH_TunnelNumber *tid;
2033 char cbuf[htons (msg->size)];
2035 type = ntohs (payload->type);
2036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2038 GNUNET_MESH_DEBUG_M2S (type));
2040 memcpy (cbuf, msg, sizeof (cbuf));
2041 switch (htons (msg->type))
2043 struct GNUNET_MESH_Unicast *uc;
2044 struct GNUNET_MESH_Multicast *mc;
2045 struct GNUNET_MESH_ToOrigin *to;
2047 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2048 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2051 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2052 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2055 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2056 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2064 for (count = 0, c = clients; c != NULL; c = c->next)
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2067 if (client_is_subscribed (type, c))
2069 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2073 *tid = htonl (t->local_tid);
2077 if (GNUNET_NO == client_knows_tunnel (c, t))
2079 /* This client doesn't know the tunnel */
2080 struct GNUNET_MESH_TunnelNotification tmsg;
2081 struct GNUNET_HashCode hash;
2083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2084 tmsg.header.size = htons (sizeof (tmsg));
2085 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2086 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2087 tmsg.tunnel_id = htonl (t->local_tid_dest);
2088 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2089 &tmsg.header, GNUNET_NO);
2090 tunnel_add_client (t, c);
2091 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2093 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2094 c->incoming_tunnels, &hash, t,
2095 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2097 *tid = htonl (t->local_tid_dest);
2100 /* Check if the client wants to get traffic from the tunnel */
2101 if (GNUNET_NO == client_wants_tunnel(c, t))
2104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2105 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2106 (struct GNUNET_MessageHeader
2107 *) cbuf, GNUNET_NO);
2116 * Notify the client that owns the tunnel that a peer has connected to it
2117 * (the requested path to it has been confirmed).
2119 * @param t Tunnel whose owner to notify
2120 * @param id Short id of the peer that has connected
2123 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2125 struct GNUNET_MESH_PeerControl pc;
2127 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2128 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2129 pc.tunnel_id = htonl (t->local_tid);
2130 GNUNET_PEER_resolve (id, &pc.peer);
2131 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2137 * Notify all clients (not depending on registration status) that the incoming
2138 * tunnel is no longer valid.
2140 * @param t Tunnel that was destroyed.
2143 send_clients_tunnel_destroy (struct MeshTunnel *t)
2145 struct GNUNET_MESH_TunnelMessage msg;
2147 msg.header.size = htons (sizeof (msg));
2148 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2149 msg.tunnel_id = htonl (t->local_tid_dest);
2150 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2155 * Notify clients of tunnel disconnections, if needed.
2156 * In case the origin disconnects, the destination clients get a tunnel destroy
2157 * notification. If the last destination disconnects (only one remaining client
2158 * in tunnel), the origin gets a (local ID) peer disconnected.
2159 * Note that the function must be called BEFORE removing the client from
2162 * @param t Tunnel that was destroyed.
2163 * @param c Client that disconnected.
2166 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2172 struct GNUNET_MESH_TunnelMessage msg;
2174 msg.header.size = htons (sizeof (msg));
2175 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2176 msg.tunnel_id = htonl (t->local_tid_dest);
2177 for (i = 0; i < t->nclients; i++)
2178 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2179 &msg.header, GNUNET_NO);
2181 // FIXME when to disconnect an incoming tunnel?
2182 else if (1 == t->nclients && NULL != t->owner)
2184 struct GNUNET_MESH_PeerControl msg;
2186 msg.header.size = htons (sizeof (msg));
2187 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2188 msg.tunnel_id = htonl (t->local_tid);
2189 msg.peer = my_full_id;
2190 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2191 &msg.header, GNUNET_NO);
2197 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2198 * and insert it in the appropiate structures if the peer is not known yet.
2200 * @param peer Full identity of the peer.
2202 * @return Existing or newly created peer info.
2204 static struct MeshPeerInfo *
2205 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2207 struct MeshPeerInfo *peer_info;
2209 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2210 if (NULL == peer_info)
2213 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2214 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2215 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2216 peer_info->id = GNUNET_PEER_intern (peer);
2224 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2225 * and insert it in the appropiate structures if the peer is not known yet.
2227 * @param peer Short identity of the peer.
2229 * @return Existing or newly created peer info.
2231 static struct MeshPeerInfo *
2232 peer_info_get_short (const GNUNET_PEER_Id peer)
2234 struct GNUNET_PeerIdentity id;
2236 GNUNET_PEER_resolve (peer, &id);
2237 return peer_info_get (&id);
2242 * Iterator to remove the tunnel from the list of tunnels a peer participates
2245 * @param cls Closure (tunnel info)
2246 * @param key GNUNET_PeerIdentity of the peer (unused)
2247 * @param value PeerInfo of the peer
2249 * @return always GNUNET_YES, to keep iterating
2252 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2254 struct MeshTunnel *t = cls;
2255 struct MeshPeerInfo *peer = value;
2258 for (i = 0; i < peer->ntunnels; i++)
2261 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2264 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2265 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2274 * Core callback to write a pre-constructed data packet to core buffer
2276 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2277 * @param size Number of bytes available in buf.
2278 * @param buf Where the to write the message.
2280 * @return number of bytes written to buf
2283 send_core_data_raw (void *cls, size_t size, void *buf)
2285 struct MeshTransmissionDescriptor *info = cls;
2286 struct GNUNET_MessageHeader *msg;
2289 GNUNET_assert (NULL != info);
2290 GNUNET_assert (NULL != info->mesh_data);
2291 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2292 total_size = ntohs (msg->size);
2294 if (total_size > size)
2299 memcpy (buf, msg, total_size);
2300 data_descriptor_decrement_rc (info->mesh_data);
2307 * Sends an already built non-multicast message to a peer,
2308 * properly registrating all used resources.
2310 * @param message Message to send. Function makes a copy of it.
2311 * @param peer Short ID of the neighbor whom to send the message.
2312 * @param t Tunnel on which this message is transmitted.
2315 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2316 const struct GNUNET_PeerIdentity *peer,
2317 struct MeshTunnel *t)
2319 struct MeshTransmissionDescriptor *info;
2320 struct MeshPeerInfo *neighbor;
2321 struct MeshPeerPath *p;
2325 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2327 size = ntohs (message->size);
2328 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2329 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2330 info->mesh_data->data = GNUNET_malloc (size);
2331 memcpy (info->mesh_data->data, message, size);
2332 type = ntohs(message->type);
2333 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2335 struct GNUNET_MESH_Unicast *m;
2337 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2338 m->ttl = htonl (ntohl (m->ttl) - 1);
2340 info->mesh_data->data_len = size;
2341 info->mesh_data->reference_counter = 1;
2342 info->mesh_data->total_out = 1;
2343 neighbor = peer_info_get (peer);
2344 for (p = neighbor->path_head; NULL != p; p = p->next)
2354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2355 " %s IS NOT DIRECTLY CONNECTED\n",
2357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 for (p = neighbor->path_head; NULL != p; p = p->next)
2362 struct GNUNET_PeerIdentity debug_id;
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 " path with %u hops through:\n",
2368 for (i = 0; i < p->length; i++)
2370 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 i, GNUNET_i2s(&debug_id));
2377 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2378 GNUNET_free (info->mesh_data->data);
2379 GNUNET_free (info->mesh_data);
2383 info->peer = neighbor;
2384 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2395 * Sends a CREATE PATH message for a path to a peer, properly registrating
2396 * all used resources.
2398 * @param peer PeerInfo of the final peer for whom this path is being created.
2399 * @param p Path itself.
2400 * @param t Tunnel for which the path is created.
2403 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2404 struct MeshTunnel *t)
2406 struct GNUNET_PeerIdentity id;
2407 struct MeshPathInfo *path_info;
2408 struct MeshPeerInfo *neighbor;
2414 p = tree_get_path_to_peer (t->tree, peer->id);
2421 for (i = 0; i < p->length; i++)
2423 if (p->peers[i] == myid)
2426 if (i >= p->length - 1)
2432 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2434 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2435 path_info->path = p;
2437 neighbor = peer_info_get (&id);
2438 path_info->peer = neighbor;
2439 queue_add (path_info,
2440 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2441 sizeof (struct GNUNET_MESH_ManipulatePath) +
2442 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2449 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2451 * @param t Tunnel whose path to destroy.
2452 * @param destination Short ID of the peer to whom the path to destroy.
2455 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2457 struct MeshPeerPath *p;
2460 p = tree_get_path_to_peer (t->tree, destination);
2466 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2467 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2469 struct GNUNET_MESH_ManipulatePath *msg;
2470 struct GNUNET_PeerIdentity *pi;
2474 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2475 msg->header.size = htons (size);
2476 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2477 msg->tid = htonl (t->id.tid);
2478 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2479 for (i = 0; i < p->length; i++)
2481 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2483 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2490 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2492 * @param t Tunnel which to confirm.
2495 send_path_ack (struct MeshTunnel *t)
2497 struct MeshTransmissionDescriptor *info;
2498 struct GNUNET_PeerIdentity id;
2499 GNUNET_PEER_Id peer;
2501 peer = tree_get_predecessor (t->tree);
2502 GNUNET_PEER_resolve (peer, &id);
2503 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2504 info->origin = &t->id;
2505 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2506 GNUNET_assert (NULL != info->peer);
2509 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2510 sizeof (struct GNUNET_MESH_PathACK),
2517 * Try to establish a new connection to this peer.
2518 * Use the best path for the given tunnel.
2519 * If the peer doesn't have any path to it yet, try to get one.
2520 * If the peer already has some path, send a CREATE PATH towards it.
2522 * @param peer PeerInfo of the peer.
2523 * @param t Tunnel for which to create the path, if possible.
2526 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2528 struct MeshPeerPath *p;
2529 struct MeshPathInfo *path_info;
2531 if (NULL != peer->path_head)
2533 p = tree_get_path_to_peer (t->tree, peer->id);
2540 // FIXME always send create path to self
2543 send_create_path (peer, p, t);
2547 struct GNUNET_HashCode hash;
2550 send_client_peer_connected (t, myid);
2551 t->local_tid_dest = next_local_tid++;
2552 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2555 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2556 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2563 else if (NULL == peer->dhtget)
2565 struct GNUNET_PeerIdentity id;
2567 GNUNET_PEER_resolve (peer->id, &id);
2568 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2569 path_info->peer = peer;
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2573 peer->dhtgetcls = path_info;
2574 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2575 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2576 &id.hashPubKey, /* key to search */
2577 dht_replication_level, /* replication level */
2578 GNUNET_DHT_RO_RECORD_ROUTE |
2579 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2580 NULL, /* xquery */ // FIXME BLOOMFILTER
2581 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2582 &dht_get_id_handler, path_info);
2584 /* Otherwise, there is no path but the DHT get is already started. */
2589 * Task to delay the connection of a peer
2591 * @param cls Closure (path info with tunnel and peer to connect).
2592 * Will be free'd on exection.
2593 * @param tc TaskContext
2596 peer_info_connect_task (void *cls,
2597 const struct GNUNET_SCHEDULER_TaskContext *tc)
2599 struct MeshPathInfo *path_info = cls;
2601 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2606 peer_info_connect (path_info->peer, path_info->t);
2612 * Destroy the peer_info and free any allocated resources linked to it
2614 * @param pi The peer_info to destroy.
2616 * @return GNUNET_OK on success
2619 peer_info_destroy (struct MeshPeerInfo *pi)
2621 struct GNUNET_PeerIdentity id;
2622 struct MeshPeerPath *p;
2623 struct MeshPeerPath *nextp;
2625 GNUNET_PEER_resolve (pi->id, &id);
2626 GNUNET_PEER_change_rc (pi->id, -1);
2629 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2633 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2635 if (NULL != pi->dhtget)
2637 GNUNET_DHT_get_stop (pi->dhtget);
2638 GNUNET_free (pi->dhtgetcls);
2644 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2654 * Remove all paths that rely on a direct connection between p1 and p2
2655 * from the peer itself and notify all tunnels about it.
2657 * @param peer PeerInfo of affected peer.
2658 * @param p1 GNUNET_PEER_Id of one peer.
2659 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2662 * TODO: optimize (see below)
2665 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2668 struct MeshPeerPath *p;
2669 struct MeshPeerPath *aux;
2670 struct MeshPeerInfo *peer_d;
2672 unsigned int destroyed;
2677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2679 p = peer->path_head;
2683 for (i = 0; i < (p->length - 1); i++)
2685 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2686 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2688 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2699 for (i = 0; i < peer->ntunnels; i++)
2701 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2705 * Problem: one or more peers have been deleted from the tunnel tree.
2706 * We don't know who they are to try to add them again.
2707 * We need to try to find a new path for each of the disconnected peers.
2708 * Some of them might already have a path to reach them that does not
2709 * involve p1 and p2. Adding all anew might render in a better tree than
2710 * the trivial immediate fix.
2712 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2713 * its children will be reachable trough him.
2715 peer_d = peer_info_get_short (d);
2718 for (p = peer_d->path_head; NULL != p; p = p->next)
2720 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2728 /* No callback, as peer will be already disconnected and a connection
2729 * scheduled by tunnel_notify_connection_broken.
2731 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2735 peer_info_connect (peer_d, peer->tunnels[i]);
2738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2743 * Add the path to the peer and update the path used to reach it in case this
2746 * @param peer_info Destination peer to add the path to.
2747 * @param path New path to add. Last peer must be the peer in arg 1.
2748 * Path will be either used of freed if already known.
2749 * @param trusted Do we trust that this path is real?
2752 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2755 struct MeshPeerPath *aux;
2759 if ((NULL == peer_info) || (NULL == path))
2762 path_destroy (path);
2765 if (path->peers[path->length - 1] != peer_info->id)
2768 path_destroy (path);
2771 if (path->length <= 2 && GNUNET_NO == trusted)
2773 /* Only allow CORE to tell us about direct paths */
2774 path_destroy (path);
2777 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2778 for (l = 1; l < path->length; l++)
2780 if (path->peers[l] == myid)
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2783 for (l2 = 0; l2 < path->length - l; l2++)
2785 path->peers[l2] = path->peers[l + l2];
2790 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2795 struct GNUNET_PeerIdentity id;
2797 GNUNET_PEER_resolve (peer_info->id, &id);
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2799 path->length, GNUNET_i2s (&id));
2802 l = path_get_length (path);
2809 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2810 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2812 l2 = path_get_length (aux);
2815 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2816 peer_info->path_tail, aux, path);
2821 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2823 path_destroy (path);
2828 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2835 * Add the path to the origin peer and update the path used to reach it in case
2836 * this is the shortest.
2837 * The path is given in peer_info -> destination, therefore we turn the path
2838 * upside down first.
2840 * @param peer_info Peer to add the path to, being the origin of the path.
2841 * @param path New path to add after being inversed.
2842 * @param trusted Do we trust that this path is real?
2845 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2846 struct MeshPeerPath *path, int trusted)
2849 peer_info_add_path (peer_info, path, trusted);
2854 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2855 * to obtain a local peer -> destination path and interning the peer ids.
2857 * @return Newly allocated and created path
2859 static struct MeshPeerPath *
2860 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2861 unsigned int get_path_length,
2862 const struct GNUNET_PeerIdentity *put_path,
2863 unsigned int put_path_length)
2865 struct MeshPeerPath *p;
2871 GNUNET_PEER_change_rc (myid, 1);
2872 i = get_path_length;
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2874 for (i--; i >= 0; i--)
2876 id = GNUNET_PEER_intern (&get_path[i]);
2877 if (p->length > 0 && id == p->peers[p->length - 1])
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2880 GNUNET_PEER_change_rc (id, -1);
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2885 GNUNET_i2s (&get_path[i]));
2887 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2888 p->peers[p->length - 1] = id;
2891 i = put_path_length;
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2893 for (i--; i >= 0; i--)
2895 id = GNUNET_PEER_intern (&put_path[i]);
2898 /* PUT path went through us, so discard the path up until now and start
2899 * from here to get a much shorter (and loop-free) path.
2904 if (p->length > 0 && id == p->peers[p->length - 1])
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2907 GNUNET_PEER_change_rc (id, -1);
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2912 GNUNET_i2s (&put_path[i]));
2914 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2915 p->peers[p->length - 1] = id;
2919 if (get_path_length > 0)
2920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2921 GNUNET_i2s (&get_path[0]));
2922 if (put_path_length > 0)
2923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2924 GNUNET_i2s (&put_path[0]));
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2927 for (i = 0; i < p->length; i++)
2929 struct GNUNET_PeerIdentity peer_id;
2931 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2933 GNUNET_i2s (&peer_id));
2941 * Adds a path to the peer_infos of all the peers in the path
2943 * @param p Path to process.
2944 * @param confirmed Whether we know if the path works or not.
2947 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2951 /* TODO: invert and add */
2952 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2953 for (i++; i < p->length; i++)
2955 struct MeshPeerInfo *aux;
2956 struct MeshPeerPath *copy;
2958 aux = peer_info_get_short (p->peers[i]);
2959 copy = path_duplicate (p);
2960 copy->length = i + 1;
2961 peer_info_add_path (aux, copy, GNUNET_NO);
2967 * Send keepalive packets for a peer
2969 * @param cls Closure (tunnel for which to send the keepalive).
2970 * @param tc Notification context.
2972 * TODO: implement explicit multicast keepalive?
2975 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2979 * Search for a tunnel among the incoming tunnels
2981 * @param tid the local id of the tunnel
2983 * @return tunnel handler, NULL if doesn't exist
2985 static struct MeshTunnel *
2986 tunnel_get_incoming (MESH_TunnelNumber tid)
2988 struct GNUNET_HashCode hash;
2990 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2991 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2992 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2997 * Search for a tunnel among the tunnels for a client
2999 * @param c the client whose tunnels to search in
3000 * @param tid the local id of the tunnel
3002 * @return tunnel handler, NULL if doesn't exist
3004 static struct MeshTunnel *
3005 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3007 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3009 return tunnel_get_incoming (tid);
3013 struct GNUNET_HashCode hash;
3015 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3016 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3022 * Search for a tunnel by global ID using PEER_ID
3024 * @param pi owner of the tunnel
3025 * @param tid global tunnel number
3027 * @return tunnel handler, NULL if doesn't exist
3029 static struct MeshTunnel *
3030 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3032 struct MESH_TunnelID id;
3033 struct GNUNET_HashCode hash;
3038 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3039 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3044 * Search for a tunnel by global ID using full PeerIdentities
3046 * @param oid owner of the tunnel
3047 * @param tid global tunnel number
3049 * @return tunnel handler, NULL if doesn't exist
3051 static struct MeshTunnel *
3052 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3054 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3059 * Delete an active client from the tunnel.
3065 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3069 for (i = 0; i < t->nclients; i++)
3071 if (t->clients[i] == c)
3073 t->clients[i] = t->clients[t->nclients - 1];
3074 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3075 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3077 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3085 * Delete an ignored client from the tunnel.
3091 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3095 for (i = 0; i < t->nignore; i++)
3097 if (t->ignore[i] == c)
3099 t->ignore[i] = t->ignore[t->nignore - 1];
3100 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3108 * Delete a client from the tunnel. It should be only done on
3109 * client disconnection, otherwise use client_ignore_tunnel.
3115 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3117 tunnel_delete_ignored_client (t, c);
3118 tunnel_delete_active_client (t, c);
3123 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3125 * Destroys queue elements of all waiting transmissions and frees all memory
3126 * used by the struct and its elements.
3128 * @param cls Closure (tunnel info).
3129 * @param key Hash of GNUNET_PEER_Id (unused).
3130 * @param value MeshTunnelChildInfo of the child.
3132 * @return always GNUNET_YES, to keep iterating
3135 tunnel_destroy_child (void *cls,
3136 const struct GNUNET_HashCode * key,
3139 struct MeshTunnelChildInfo *cinfo = value;
3140 struct MeshTunnel *t = cls;
3144 for (c = 0; c < cinfo->send_buffer_n; c++)
3146 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3147 if (NULL != cinfo->send_buffer[i])
3148 queue_destroy (cinfo->send_buffer[i], GNUNET_YES);
3151 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3153 GNUNET_free_non_null (cinfo->send_buffer);
3154 GNUNET_free (cinfo);
3160 * Callback used to notify a client owner of a tunnel that a peer has
3161 * disconnected, most likely because of a path change.
3163 * @param cls Closure (tunnel this notification is about).
3164 * @param peer_id Short ID of disconnected peer.
3167 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3169 struct MeshTunnel *t = cls;
3170 struct MeshPeerInfo *peer;
3171 struct MeshPathInfo *path_info;
3173 if (NULL != t->owner && NULL != nc)
3175 struct GNUNET_MESH_PeerControl msg;
3177 msg.header.size = htons (sizeof (msg));
3178 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3179 msg.tunnel_id = htonl (t->local_tid);
3180 GNUNET_PEER_resolve (peer_id, &msg.peer);
3181 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3182 &msg.header, GNUNET_NO);
3184 peer = peer_info_get_short (peer_id);
3185 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3186 path_info->peer = peer;
3188 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3193 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3194 * needed rescources.
3195 * If peer already exists, reevaluate shortest path and change if different.
3197 * @param t Tunnel we want to add a new peer to
3198 * @param peer PeerInfo of the peer being added
3202 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3204 struct GNUNET_PeerIdentity id;
3205 struct MeshPeerPath *best_p;
3206 struct MeshPeerPath *p;
3207 unsigned int best_cost;
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3211 GNUNET_PEER_resolve (peer->id, &id);
3213 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3216 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3217 GNUNET_assert (GNUNET_OK ==
3218 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3220 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3223 if (NULL != (p = peer->path_head))
3226 best_cost = tree_get_path_cost (t->tree, p);
3229 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3236 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3237 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3238 t->path_refresh_task =
3239 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3243 /* Start a DHT get */
3244 peer_info_connect (peer, t);
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3250 * Add a path to a tunnel which we don't own, just to remember the next hop.
3251 * If destination node was already in the tunnel, the first hop information
3252 * will be replaced with the new path.
3254 * @param t Tunnel we want to add a new peer to
3255 * @param p Path to add
3256 * @param own_pos Position of local node in path.
3260 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3261 unsigned int own_pos)
3263 struct GNUNET_PeerIdentity id;
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3266 GNUNET_assert (0 != own_pos);
3267 tree_add_path (t->tree, p, NULL, NULL);
3268 if (own_pos < p->length - 1)
3270 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3271 tree_update_first_hops (t->tree, myid, &id);
3273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3277 * Add a client to a tunnel, initializing all needed data structures.
3279 * @param t Tunnel to which add the client.
3280 * @param c Client which to add to the tunnel.
3283 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3285 struct MeshTunnelClientInfo clinfo;
3287 GNUNET_array_append (t->clients, t->nclients, c);
3288 clinfo.fwd_ack = t->fwd_pid + 1;
3289 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3290 clinfo.fwd_pid = t->fwd_pid;
3291 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3293 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3298 * Notifies a tunnel that a connection has broken that affects at least
3299 * some of its peers. Sends a notification towards the root of the tree.
3300 * In case the peer is the owner of the tree, notifies the client that owns
3301 * the tunnel and tries to reconnect.
3303 * @param t Tunnel affected.
3304 * @param p1 Peer that got disconnected from p2.
3305 * @param p2 Peer that got disconnected from p1.
3307 * @return Short ID of the peer disconnected (either p1 or p2).
3308 * 0 if the tunnel remained unaffected.
3310 static GNUNET_PEER_Id
3311 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3317 tree_notify_connection_broken (t->tree, p1, p2,
3318 &tunnel_notify_client_peer_disconnected,
3320 if (myid != p1 && myid != p2)
3326 if (tree_get_predecessor (t->tree) != 0)
3328 /* We are the peer still connected, notify owner of the disconnection. */
3329 struct GNUNET_MESH_PathBroken msg;
3330 struct GNUNET_PeerIdentity neighbor;
3332 msg.header.size = htons (sizeof (msg));
3333 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3334 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3335 msg.tid = htonl (t->id.tid);
3336 msg.peer1 = my_full_id;
3337 GNUNET_PEER_resolve (pid, &msg.peer2);
3338 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3339 send_prebuilt_message (&msg.header, &neighbor, t);
3347 * Send a multicast packet to a neighbor.
3349 * @param cls Closure (Info about the multicast packet)
3350 * @param neighbor_id Short ID of the neighbor to send the packet to.
3353 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3355 struct MeshData *mdata = cls;
3356 struct MeshTransmissionDescriptor *info;
3357 struct GNUNET_PeerIdentity neighbor;
3358 struct GNUNET_MessageHeader *msg;
3360 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3362 info->mesh_data = mdata;
3363 (mdata->reference_counter) ++;
3364 info->destination = neighbor_id;
3365 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3367 GNUNET_i2s (&neighbor));
3368 info->peer = peer_info_get (&neighbor);
3369 GNUNET_assert (NULL != info->peer);
3370 msg = (struct GNUNET_MessageHeader *) mdata->data;
3373 info->mesh_data->data_len,
3380 * Queue a message in a tunnel in multicast, sending a copy to each child node
3381 * down the local one in the tunnel tree.
3383 * @param t Tunnel in which to send the data.
3384 * @param msg Message to be sent.
3387 tunnel_send_multicast (struct MeshTunnel *t,
3388 const struct GNUNET_MessageHeader *msg)
3390 struct MeshData *mdata;
3392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3393 " sending a multicast packet...\n");
3395 mdata = GNUNET_malloc (sizeof (struct MeshData));
3396 mdata->data_len = ntohs (msg->size);
3398 mdata->data = GNUNET_malloc (mdata->data_len);
3399 memcpy (mdata->data, msg, mdata->data_len);
3400 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3402 struct GNUNET_MESH_Multicast *mcast;
3404 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3405 if (t->fwd_queue_n >= t->fwd_queue_max)
3408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3410 " message from %s!\n",
3411 GNUNET_i2s(&mcast->oid));
3412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3413 " message at %s!\n",
3414 GNUNET_i2s(&my_full_id));
3415 GNUNET_free (mdata->data);
3416 GNUNET_free (mdata);
3420 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3422 ntohl (mcast->ttl));
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3429 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3430 if (mdata->reference_counter == 0)
3432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3433 " no one to send data to\n");
3434 GNUNET_free (mdata->data);
3435 GNUNET_free (mdata);
3440 mdata->total_out = mdata->reference_counter;
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3443 " sending a multicast packet done\n");
3449 * Increase the SKIP value of all peers that
3450 * have not received a unicast message.
3452 * @param cls Closure (ID of the peer that HAS received the message).
3453 * @param key ID of the neighbor.
3454 * @param value Information about the neighbor.
3456 * @return GNUNET_YES to keep iterating.
3459 tunnel_add_skip (void *cls,
3460 const struct GNUNET_HashCode * key,
3463 struct GNUNET_PeerIdentity *neighbor = cls;
3464 struct MeshTunnelChildInfo *cinfo = value;
3466 /* TODO compare only pointers? key == neighbor? */
3467 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3477 * @brief Get neighbor's Flow Control information.
3479 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3480 * descendant of the local node in a certain tunnel.
3481 * If the info is not yet there (recently created path), creates the data struct
3482 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3485 * @param t Tunnel related.
3486 * @param peer Neighbor whose Flow Control info is needed.
3488 * @return Neighbor's Flow Control info.
3490 static struct MeshTunnelChildInfo *
3491 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3492 const struct GNUNET_PeerIdentity *peer)
3494 struct MeshTunnelChildInfo *cinfo;
3495 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3501 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3502 cinfo->id = GNUNET_PEER_intern (peer);
3503 cinfo->skip = t->fwd_pid;
3505 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3506 cinfo->fwd_ack = t->fwd_pid + delta;
3507 cinfo->bck_ack = delta;
3509 cinfo->send_buffer =
3510 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3512 GNUNET_assert (GNUNET_OK ==
3513 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3516 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3523 * Get the Flow Control info of a client.
3525 * @param t Tunnel on which to look.
3526 * @param c Client whose ACK to get.
3528 * @return ACK value.
3530 static struct MeshTunnelClientInfo *
3531 tunnel_get_client_fc (struct MeshTunnel *t,
3532 struct MeshClient *c)
3536 for (i = 0; i < t->nclients; i++)
3538 if (t->clients[i] != c)
3540 return &t->clients_fc[i];
3543 return NULL; // avoid compiler / coverity complaints
3548 * Iterator to get the appropiate ACK value from all children nodes.
3550 * @param cls Closue (tunnel).
3551 * @param id Id of the child node.
3554 tunnel_get_child_fwd_ack (void *cls,
3557 struct GNUNET_PeerIdentity peer_id;
3558 struct MeshTunnelChildInfo *cinfo;
3559 struct MeshTunnelChildIteratorContext *ctx = cls;
3560 struct MeshTunnel *t = ctx->t;
3563 GNUNET_PEER_resolve (id, &peer_id);
3564 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3565 ack = cinfo->fwd_ack;
3568 if (GNUNET_NO == ctx->init)
3570 ctx->max_child_ack = ack;
3571 ctx->init = GNUNET_YES;
3574 if (GNUNET_YES == t->speed_min)
3576 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3580 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3587 * Get the maximum PID allowed to transmit to any
3588 * tunnel child of the local peer, depending on the tunnel
3589 * buffering/speed settings.
3593 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3596 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3598 struct MeshTunnelChildIteratorContext ctx;
3600 ctx.max_child_ack = 0;
3602 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3604 if (0 == ctx.nchildren)
3606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3607 " tunnel has no children, no FWD ACK\n");
3611 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3612 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3614 return (int64_t) ctx.max_child_ack;
3619 * Set the FWD ACK value of a client in a particular tunnel.
3621 * @param t Tunnel affected.
3622 * @param c Client whose ACK to set.
3623 * @param ack ACK value.
3626 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3627 struct MeshClient *c,
3632 for (i = 0; i < t->nclients; i++)
3634 if (t->clients[i] != c)
3636 t->clients_fc[i].fwd_ack = ack;
3644 * Get the highest ACK value of all clients in a particular tunnel,
3645 * according to the buffering/speed settings.
3647 * @param t Tunnel on which to look.
3649 * @return Corresponding ACK value (max uint32_t).
3650 * If no clients are suscribed, -1LL.
3653 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3658 if (0 == t->nclients)
3660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3661 " tunnel has no clients, no FWD ACK\n");
3665 for (ack = -1LL, i = 0; i < t->nclients; i++)
3668 (GNUNET_YES == t->speed_min &&
3669 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3670 (GNUNET_NO == t->speed_min &&
3671 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3673 ack = t->clients_fc[i].fwd_ack;
3677 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3678 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3680 return (uint32_t) ack;
3685 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3686 * mode and the status of all children nodes.
3690 * @return Maximum PID allowed.
3693 tunnel_get_fwd_ack (struct MeshTunnel *t)
3697 uint32_t buffer_free;
3701 count = t->fwd_pid - t->skip;
3702 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3704 child_ack = tunnel_get_children_fwd_ack (t);
3705 client_ack = tunnel_get_clients_fwd_ack (t);
3706 if (-1LL == child_ack)
3708 // Node has no children, child_ack AND core buffer are irrelevant.
3709 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3710 return (uint32_t) client_ack;
3712 if (-1LL == client_ack)
3714 client_ack = ack + buffer_free; // Might overflow 32 bits, it's ok!
3716 if (GNUNET_YES == t->speed_min)
3718 ack = GMC_min_pid ((uint32_t) child_ack, ack) + buffer_free; // Might overflow 32 bits, it's ok!;
3719 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3723 ack = GMC_max_pid ((uint32_t) child_ack, ack) + buffer_free; // Might overflow 32 bits, it's ok!;
3724 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3726 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3727 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3729 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3730 count, buffer_free, child_ack, client_ack, ack);
3736 * Build a local ACK message and send it to a local client.
3738 * @param t Tunnel on which to send the ACK.
3739 * @param c Client to whom send the ACK.
3740 * @param ack Value of the ACK.
3743 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3745 struct GNUNET_MESH_LocalAck msg;
3747 msg.header.size = htons (sizeof (msg));
3748 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3749 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3750 msg.max_pid = htonl (ack);
3751 GNUNET_SERVER_notification_context_unicast(nc,
3758 * Build an ACK message and queue it to send to the given peer.
3760 * @param t Tunnel on which to send the ACK.
3761 * @param peer Peer to whom send the ACK.
3762 * @param ack Value of the ACK.
3765 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3767 struct GNUNET_MESH_ACK msg;
3769 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3770 msg.header.size = htons (sizeof (msg));
3771 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3772 msg.pid = htonl (ack);
3773 msg.tid = htonl (t->id.tid);
3775 send_prebuilt_message (&msg.header, peer, t);
3780 * Notify a the owner of a tunnel about how many more
3781 * payload packages will we accept on a given tunnel.
3783 * @param t Tunnel on which to send the ACK.
3786 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3791 "Sending client FWD ACK on tunnel %X\n",
3794 ack = tunnel_get_fwd_ack (t);
3796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3797 if (t->last_fwd_ack == ack)
3800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3801 t->last_fwd_ack = ack;
3802 send_local_ack (t, t->owner, ack);
3807 * Send an ACK informing the predecessor about the available buffer space.
3808 * In case there is no predecessor, inform the owning client.
3809 * If buffering is off, send only on behalf of children or self if endpoint.
3810 * If buffering is on, send when sent to children and buffer space is free.
3811 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3812 * the ACK itself goes "back" (towards root).
3814 * @param t Tunnel on which to send the ACK.
3815 * @param type Type of message that triggered the ACK transmission.
3818 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3820 struct GNUNET_PeerIdentity id;
3823 if (NULL != t->owner)
3825 tunnel_send_client_fwd_ack (t);
3828 /* Is it after unicast / multicast retransmission? */
3831 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3832 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3834 "ACK due to FWD DATA retransmission\n");
3835 if (GNUNET_YES == t->nobuffer)
3837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3841 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3842 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3848 /* Check if we need no retransmit the ACK */
3849 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3850 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3854 " t->qmax: %u, t->qn: %u\n",
3855 t->fwd_queue_max, t->fwd_queue_n);
3856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3857 " t->pid: %u, t->ack: %u\n",
3858 t->fwd_pid, t->last_fwd_ack);
3862 /* Ok, ACK might be necessary, what PID to ACK? */
3863 ack = tunnel_get_fwd_ack (t);
3865 /* If speed_min and not all children have ack'd, dont send yet */
3866 if (ack == t->last_fwd_ack)
3868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3872 t->last_fwd_ack = ack;
3873 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3874 send_ack (t, &id, ack);
3880 * Iterator to send a child node a BCK ACK to allow him to send more
3883 * @param cls Closure (tunnel).
3884 * @param id Id of the child node.
3887 tunnel_send_child_bck_ack (void *cls,
3890 struct MeshTunnel *t = cls;
3891 struct MeshTunnelChildInfo *cinfo;
3892 struct GNUNET_PeerIdentity peer;
3894 GNUNET_PEER_resolve (id, &peer);
3895 cinfo = tunnel_get_neighbor_fc (t, &peer);
3897 if (cinfo->bck_ack != cinfo->pid &&
3898 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3902 send_ack (t, &peer, cinfo->bck_ack);
3907 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3909 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3911 * @param t Tunnel on which to send the BCK ACKs.
3914 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3917 unsigned int tunnel_delta;
3919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3921 tunnel_delta = t->bck_ack - t->bck_pid;
3922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3924 /* Find client whom to allow to send to origin (with lowest buffer space) */
3925 for (i = 0; i < t->nclients; i++)
3927 struct MeshTunnelClientInfo *clinfo;
3930 clinfo = &t->clients_fc[i];
3931 delta = clinfo->bck_ack - clinfo->bck_pid;
3932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3933 t->clients[i]->id, delta);
3935 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3936 (GNUNET_YES == t->nobuffer && 0 == delta))
3940 ack = clinfo->bck_pid;
3941 ack += t->nobuffer ? 1 : tunnel_delta;
3942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3943 " sending ack to client %u: %u\n",
3944 t->clients[i]->id, ack);
3945 send_local_ack (t, t->clients[i], ack);
3946 clinfo->bck_ack = ack;
3953 * Send an ACK informing the children nodes and destination clients about
3954 * the available buffer space.
3955 * If buffering is off, send only on behalf of root (can be self).
3956 * If buffering is on, send when sent to predecessor and buffer space is free.
3957 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3958 * the ACK itself goes "forward" (towards children/clients).
3960 * @param t Tunnel on which to send the ACK.
3961 * @param type Type of message that triggered the ACK transmission.
3964 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3967 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3968 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3969 /* Is it after data to_origin retransmission? */
3972 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3973 if (GNUNET_YES == t->nobuffer)
3975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3976 " Not sending ACK, nobuffer\n");
3980 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3981 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3987 tunnel_send_clients_bck_ack (t);
3988 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3993 * @brief Re-initiate traffic to this peer if necessary.
3995 * Check if there is traffic queued towards this peer
3996 * and the core transmit handle is NULL (traffic was stalled).
3997 * If so, call core tmt rdy.
3999 * @param cls Closure (unused)
4000 * @param peer_id Short ID of peer to which initiate traffic.
4003 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4005 struct MeshPeerInfo *peer;
4006 struct GNUNET_PeerIdentity id;
4007 struct MeshPeerQueue *q;
4010 peer = peer_info_get_short(peer_id);
4011 if (NULL != peer->core_transmit)
4014 q = queue_get_next(peer);
4017 /* Might br multicast traffic already sent to this particular peer but
4018 * not to other children in this tunnel.
4019 * This way t->queue_n would be > 0 but the queue of this particular peer
4025 GNUNET_PEER_resolve (peer->id, &id);
4026 peer->core_transmit =
4027 GNUNET_CORE_notify_transmit_ready(core_handle,
4030 GNUNET_TIME_UNIT_FOREVER_REL,
4040 * @brief Allow transmission of FWD traffic on this tunnel
4042 * Check if there is traffic queued towards any children
4043 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4045 * @param t Tunnel on which to unlock FWD traffic.
4048 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4050 if (0 == t->fwd_queue_n)
4053 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4058 * @brief Allow transmission of BCK traffic on this tunnel
4060 * Check if there is traffic queued towards the root of the tree
4061 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4063 * @param t Tunnel on which to unlock BCK traffic.
4066 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4068 if (0 == t->bck_queue_n)
4071 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4076 * Send a message to all peers in this tunnel that the tunnel is no longer
4079 * @param t The tunnel whose peers to notify.
4082 tunnel_send_destroy (struct MeshTunnel *t)
4084 struct GNUNET_MESH_TunnelDestroy msg;
4086 msg.header.size = htons (sizeof (msg));
4087 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4088 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4089 msg.tid = htonl (t->id.tid);
4090 tunnel_send_multicast (t, &msg.header);
4095 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4097 * @param cls Closure (Tunnel which to cancel).
4098 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4101 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4103 struct MeshTunnel *t = cls;
4104 struct MeshPeerInfo *peer_info;
4105 struct MeshPeerQueue *pq;
4106 struct MeshPeerQueue *next;
4108 peer_info = peer_info_get_short (neighbor_id);
4109 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4112 if (pq->tunnel == t)
4114 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4115 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4116 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4118 // Should have been removed on destroy children
4121 queue_destroy (pq, GNUNET_YES);
4124 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4126 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4127 peer_info->core_transmit = NULL;
4132 * Destroy the tunnel and free any allocated resources linked to it.
4134 * @param t the tunnel to destroy
4136 * @return GNUNET_OK on success
4139 tunnel_destroy (struct MeshTunnel *t)
4141 struct MeshClient *c;
4142 struct GNUNET_HashCode hash;
4153 struct GNUNET_PeerIdentity id;
4155 GNUNET_PEER_resolve (t->id.oid, &id);
4156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4157 GNUNET_i2s (&id), t->id.tid);
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4163 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4164 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4170 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4173 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4178 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4179 for (i = 0; i < t->nclients; i++)
4183 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4189 for (i = 0; i < t->nignore; i++)
4193 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4199 if (t->nclients > 0)
4202 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4207 GNUNET_free (t->clients);
4209 if (NULL != t->peers)
4211 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4213 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4216 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4217 &tunnel_destroy_child,
4219 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4221 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4223 tree_destroy (t->tree);
4225 if (NULL != t->regex_ctx)
4226 regex_cancel_search (t->regex_ctx);
4227 if (NULL != t->dht_get_type)
4228 GNUNET_DHT_get_stop (t->dht_get_type);
4229 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4230 GNUNET_SCHEDULER_cancel (t->timeout_task);
4231 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4232 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4235 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4236 GNUNET_assert (0 <= n_tunnels);
4243 * Create a new tunnel
4245 * @param owner Who is the owner of the tunnel (short ID).
4246 * @param tid Tunnel Number of the tunnel.
4247 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4248 * @param local Tunnel Number for the tunnel, for the client point of view.
4250 * @return A new initialized tunnel. NULL on error.
4252 static struct MeshTunnel *
4253 tunnel_new (GNUNET_PEER_Id owner,
4254 MESH_TunnelNumber tid,
4255 struct MeshClient *client,
4256 MESH_TunnelNumber local)
4258 struct MeshTunnel *t;
4259 struct GNUNET_HashCode hash;
4261 if (n_tunnels >= max_tunnels && NULL == client)
4264 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4267 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4268 t->bck_queue_max = t->fwd_queue_max;
4269 t->tree = tree_new (owner);
4271 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4272 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4273 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4274 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4275 t->local_tid = local;
4276 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4278 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4280 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4282 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4283 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4290 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4297 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4299 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4300 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4304 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4314 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4315 * that are no longer needed, as well as nodes of no longer reachable peers.
4316 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4318 * @param t Tunnel from which to remove the path.
4319 * @param peer Short id of the peer which should be removed.
4322 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4324 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4330 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4331 * client when the client disconnects. If the client is not the owner, the
4332 * owner will get notified if no more clients are in the tunnel and the client
4333 * get removed from the tunnel's list.
4335 * @param cls closure (client that is disconnecting)
4336 * @param key the hash of the local tunnel id (used to access the hashmap)
4337 * @param value the value stored at the key (tunnel to destroy)
4339 * @return GNUNET_OK on success
4342 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4344 struct MeshTunnel *t = value;
4345 struct MeshClient *c = cls;
4348 send_client_tunnel_disconnect(t, c);
4351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4352 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4353 tunnel_delete_client(t, c);
4354 client_delete_tunnel(c, t);
4357 tunnel_send_destroy(t);
4358 r = tunnel_destroy (t);
4364 * Timeout function, destroys tunnel if called
4366 * @param cls Closure (tunnel to destroy).
4367 * @param tc TaskContext
4370 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4372 struct MeshTunnel *t = cls;
4373 struct GNUNET_PeerIdentity id;
4375 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4376 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4378 GNUNET_PEER_resolve(t->id.oid, &id);
4379 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4380 "Tunnel %s [%X] timed out. Destroying.\n",
4381 GNUNET_i2s(&id), t->id.tid);
4386 * Resets the tunnel timeout. Starts it if no timeout was running.
4388 * @param t Tunnel whose timeout to reset.
4390 * TODO use heap to improve efficiency of scheduler.
4393 tunnel_reset_timeout (struct MeshTunnel *t)
4395 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4396 GNUNET_SCHEDULER_cancel (t->timeout_task);
4398 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4399 (refresh_path_time, 4), &tunnel_timeout, t);
4403 /******************************************************************************/
4404 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4405 /******************************************************************************/
4408 * Function to send a create path packet to a peer.
4410 * @param cls closure
4411 * @param size number of bytes available in buf
4412 * @param buf where the callee should write the message
4413 * @return number of bytes written to buf
4416 send_core_path_create (void *cls, size_t size, void *buf)
4418 struct MeshPathInfo *info = cls;
4419 struct GNUNET_MESH_ManipulatePath *msg;
4420 struct GNUNET_PeerIdentity *peer_ptr;
4421 struct MeshTunnel *t = info->t;
4422 struct MeshPeerPath *p = info->path;
4427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4429 sizeof (struct GNUNET_MESH_ManipulatePath) +
4430 p->length * sizeof (struct GNUNET_PeerIdentity);
4432 if (size < size_needed || NULL == buf)
4437 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4438 msg->header.size = htons (size_needed);
4439 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4440 msg->tid = ntohl (t->id.tid);
4443 if (GNUNET_YES == t->speed_min)
4444 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4445 if (GNUNET_YES == t->nobuffer)
4446 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4447 msg->opt = htonl(opt);
4449 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4450 for (i = 0; i < p->length; i++)
4452 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4459 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4465 * Fill the core buffer
4467 * @param cls closure (data itself)
4468 * @param size number of bytes available in buf
4469 * @param buf where the callee should write the message
4471 * @return number of bytes written to buf
4474 send_core_data_multicast (void *cls, size_t size, void *buf)
4476 struct MeshTransmissionDescriptor *info = cls;
4479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4480 GNUNET_assert (NULL != info);
4481 GNUNET_assert (NULL != info->peer);
4482 total_size = info->mesh_data->data_len;
4483 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4485 if (total_size > size)
4490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4491 memcpy (buf, info->mesh_data->data, total_size);
4494 struct GNUNET_MESH_Multicast *mc;
4495 struct GNUNET_MessageHeader *mh;
4498 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4500 mc = (struct GNUNET_MESH_Multicast *) mh;
4501 mh = (struct GNUNET_MessageHeader *) &mc[1];
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4503 " multicast, payload type %s\n",
4504 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4506 " multicast, payload size %u\n", ntohs (mh->size));
4510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4511 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4515 data_descriptor_decrement_rc (info->mesh_data);
4516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4524 * Creates a path ack message in buf and frees all unused resources.
4526 * @param cls closure (MeshTransmissionDescriptor)
4527 * @param size number of bytes available in buf
4528 * @param buf where the callee should write the message
4529 * @return number of bytes written to buf
4532 send_core_path_ack (void *cls, size_t size, void *buf)
4534 struct MeshTransmissionDescriptor *info = cls;
4535 struct GNUNET_MESH_PathACK *msg = buf;
4537 GNUNET_assert (NULL != info);
4538 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4543 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4544 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4545 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4546 msg->tid = htonl (info->origin->tid);
4547 msg->peer_id = my_full_id;
4550 /* TODO add signature */
4552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4553 return sizeof (struct GNUNET_MESH_PathACK);
4558 * Free a transmission that was already queued with all resources
4559 * associated to the request.
4561 * @param queue Queue handler to cancel.
4562 * @param clear_cls Is it necessary to free associated cls?
4565 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4567 struct MeshTransmissionDescriptor *dd;
4568 struct MeshPathInfo *path_info;
4570 if (GNUNET_YES == clear_cls)
4572 switch (queue->type)
4574 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4575 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4576 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4579 data_descriptor_decrement_rc (dd->mesh_data);
4581 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4583 path_info = queue->cls;
4584 path_destroy (path_info->path);
4588 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4589 " type %s unknown!\n",
4590 GNUNET_MESH_DEBUG_M2S(queue->type));
4592 GNUNET_free_non_null (queue->cls);
4594 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4595 queue->peer->queue_tail,
4597 GNUNET_free (queue);
4602 * @brief Get the next transmittable message from the queue.
4604 * This will be the head, except in the case of being a data packet
4605 * not allowed by the destination peer.
4607 * @param peer Destination peer.
4609 * @return The next viable MeshPeerQueue element to send to that peer.
4610 * NULL when there are no transmittable messages.
4612 struct MeshPeerQueue *
4613 queue_get_next (const struct MeshPeerInfo *peer)
4615 struct MeshPeerQueue *q;
4616 struct MeshTunnel *t;
4617 struct MeshTransmissionDescriptor *info;
4618 struct MeshTunnelChildInfo *cinfo;
4619 struct GNUNET_MESH_Unicast *ucast;
4620 struct GNUNET_MESH_ToOrigin *to_orig;
4621 struct GNUNET_MESH_Multicast *mcast;
4622 struct GNUNET_PeerIdentity id;
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4627 for (q = peer->queue_head; NULL != q; q = q->next)
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4633 GNUNET_MESH_DEBUG_M2S(q->type));
4636 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4637 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4638 pid = ntohl (ucast->pid);
4639 GNUNET_PEER_resolve (info->peer->id, &id);
4640 cinfo = tunnel_get_neighbor_fc(t, &id);
4641 ack = cinfo->fwd_ack;
4643 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4644 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4645 pid = ntohl (to_orig->pid);
4648 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4649 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4650 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4652 // Not a multicast payload: multicast control traffic (destroy, etc)
4655 pid = ntohl (mcast->pid);
4656 GNUNET_PEER_resolve (info->peer->id, &id);
4657 cinfo = tunnel_get_neighbor_fc(t, &id);
4658 ack = cinfo->fwd_ack;
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4666 "********* ACK: %u, PID: %u\n",
4668 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4677 "********* NEXT!\n");
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4681 "********* nothing found\n");
4687 * Core callback to write a queued packet to core buffer
4689 * @param cls Closure (peer info).
4690 * @param size Number of bytes available in buf.
4691 * @param buf Where the to write the message.
4693 * @return number of bytes written to buf
4696 queue_send (void *cls, size_t size, void *buf)
4698 struct MeshPeerInfo *peer = cls;
4699 struct GNUNET_MessageHeader *msg;
4700 struct MeshPeerQueue *queue;
4701 struct MeshTunnel *t;
4702 struct MeshTunnelChildInfo *cinfo;
4703 struct GNUNET_PeerIdentity dst_id;
4706 peer->core_transmit = NULL;
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4709 queue = queue_get_next (peer);
4711 /* Queue has no internal mesh traffic nor sendable payload */
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4715 if (NULL == peer->queue_head)
4716 GNUNET_break (0); // Should've been canceled
4719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4721 GNUNET_PEER_resolve (peer->id, &dst_id);
4722 /* Check if buffer size is enough for the message */
4723 if (queue->size > size)
4725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4726 "********* not enough room, reissue\n");
4727 peer->core_transmit =
4728 GNUNET_CORE_notify_transmit_ready (core_handle,
4731 GNUNET_TIME_UNIT_FOREVER_REL,
4738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4741 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4745 "********* unicast: t->q (%u/%u)\n",
4746 t->fwd_queue_n, t->fwd_queue_max);
4748 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4755 switch (queue->type)
4758 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4759 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4760 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4762 "********* raw: %s\n",
4763 GNUNET_MESH_DEBUG_M2S (queue->type));
4765 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4766 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4767 data_size = send_core_data_raw (queue->cls, size, buf);
4768 msg = (struct GNUNET_MessageHeader *) buf;
4769 switch (ntohs (msg->type)) // Type of preconstructed message
4771 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4772 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4774 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4775 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4781 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4784 struct MeshTransmissionDescriptor *info = queue->cls;
4786 if ((1 == info->mesh_data->reference_counter
4787 && GNUNET_YES == t->speed_min)
4789 (info->mesh_data->total_out == info->mesh_data->reference_counter
4790 && GNUNET_NO == t->speed_min))
4792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4793 "********* considered sent\n");
4797 data_size = send_core_data_multicast(queue->cls, size, buf);
4798 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4800 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4802 data_size = send_core_path_create (queue->cls, size, buf);
4804 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4806 data_size = send_core_path_ack (queue->cls, size, buf);
4808 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4810 data_size = send_core_data_multicast (queue->cls, size, buf);
4814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4815 "********* type unknown: %u\n",
4819 switch (queue->type)
4821 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4822 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4823 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4824 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4825 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4828 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4829 "at pos %u (%p) != %p\n",
4830 cinfo->send_buffer_start,
4831 cinfo->send_buffer[cinfo->send_buffer_start],
4834 if (cinfo->send_buffer_n > 0) {
4835 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4836 cinfo->send_buffer_n--;
4837 cinfo->send_buffer_start++;
4838 cinfo->send_buffer_start %= t->fwd_queue_max;
4849 /* Free queue, but cls was freed by send_core_* */
4850 queue_destroy (queue, GNUNET_NO);
4852 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4858 /* If more data in queue, send next */
4859 queue = queue_get_next(peer);
4862 struct GNUNET_PeerIdentity id;
4864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4865 GNUNET_PEER_resolve (peer->id, &id);
4866 peer->core_transmit =
4867 GNUNET_CORE_notify_transmit_ready(core_handle,
4870 GNUNET_TIME_UNIT_FOREVER_REL,
4878 if (NULL != peer->queue_head)
4879 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4880 "********* %s stalled\n",
4881 GNUNET_i2s(&my_full_id));
4883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4889 * @brief Queue and pass message to core when possible.
4891 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4892 * and accounts for it. In case the queue is full, the message is dropped and
4895 * Otherwise, message is treated as internal and allowed to go regardless of
4898 * @param cls Closure (@c type dependant). It will be used by queue_send to
4899 * build the message to be sent if not already prebuilt.
4900 * @param type Type of the message, 0 for a raw message.
4901 * @param size Size of the message.
4902 * @param dst Neighbor to send message to.
4903 * @param t Tunnel this message belongs to.
4906 queue_add (void *cls, uint16_t type, size_t size,
4907 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4909 struct MeshPeerQueue *queue;
4910 struct MeshTunnelChildInfo *cinfo;
4911 struct GNUNET_PeerIdentity id;
4917 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4918 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4920 n = &t->fwd_queue_n;
4921 max = &t->fwd_queue_max;
4923 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4925 n = &t->bck_queue_n;
4926 max = &t->bck_queue_max;
4931 if (NULL == t->owner)
4932 GNUNET_break_op(0); // TODO: kill connection?
4935 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4937 return; // Drop message
4941 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4947 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4948 GNUNET_PEER_resolve (dst->id, &id);
4949 if (NULL == dst->core_transmit)
4951 dst->core_transmit =
4952 GNUNET_CORE_notify_transmit_ready (core_handle,
4955 GNUNET_TIME_UNIT_FOREVER_REL,
4961 if (NULL == n) // Is this internal mesh traffic?
4964 // It's payload, keep track of buffer per peer.
4965 cinfo = tunnel_get_neighbor_fc(t, &id);
4966 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4967 if (NULL != cinfo->send_buffer[i])
4969 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4970 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4971 cinfo->send_buffer_start++;
4972 cinfo->send_buffer_start %= t->fwd_queue_max;
4973 cinfo->send_buffer_n--;
4975 cinfo->send_buffer[i] = queue;
4976 cinfo->send_buffer_n++;
4977 if (cinfo->send_buffer_n > t->fwd_queue_max)
4980 cinfo->send_buffer_n = t->fwd_queue_max;
4985 /******************************************************************************/
4986 /******************** MESH NETWORK HANDLERS **************************/
4987 /******************************************************************************/
4991 * Core handler for path creation
4993 * @param cls closure
4994 * @param message message
4995 * @param peer peer identity this notification is about
4996 * @param atsi performance data
4997 * @param atsi_count number of records in 'atsi'
4999 * @return GNUNET_OK to keep the connection open,
5000 * GNUNET_SYSERR to close it (signal serious error)
5003 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5004 const struct GNUNET_MessageHeader *message,
5005 const struct GNUNET_ATS_Information *atsi,
5006 unsigned int atsi_count)
5008 unsigned int own_pos;
5011 MESH_TunnelNumber tid;
5012 struct GNUNET_MESH_ManipulatePath *msg;
5013 struct GNUNET_PeerIdentity *pi;
5014 struct GNUNET_HashCode hash;
5015 struct MeshPeerPath *path;
5016 struct MeshPeerInfo *dest_peer_info;
5017 struct MeshPeerInfo *orig_peer_info;
5018 struct MeshTunnel *t;
5020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5021 "Received a path create msg [%s]\n",
5022 GNUNET_i2s (&my_full_id));
5023 size = ntohs (message->size);
5024 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5026 GNUNET_break_op (0);
5030 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5031 if (size % sizeof (struct GNUNET_PeerIdentity))
5033 GNUNET_break_op (0);
5036 size /= sizeof (struct GNUNET_PeerIdentity);
5039 GNUNET_break_op (0);
5042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5043 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5045 tid = ntohl (msg->tid);
5046 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5048 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5049 t = tunnel_get (pi, tid);
5050 if (NULL == t) // FIXME only for INCOMING tunnels?
5054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5055 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5058 // FIXME notify failure
5061 opt = ntohl (msg->opt);
5062 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5063 GNUNET_YES : GNUNET_NO;
5064 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
5065 GNUNET_YES : GNUNET_NO;
5066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5067 " speed_min: %d, nobuffer:%d\n",
5068 t->speed_min, t->nobuffer);
5070 if (GNUNET_YES == t->nobuffer)
5072 t->bck_queue_max = 1;
5073 t->fwd_queue_max = 1;
5076 // FIXME only assign a local tid if a local client is interested (on demand)
5077 while (NULL != tunnel_get_incoming (next_local_tid))
5078 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5079 t->local_tid_dest = next_local_tid++;
5080 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5083 tunnel_reset_timeout (t);
5084 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5086 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5087 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5095 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5096 if (NULL == dest_peer_info)
5098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5099 " Creating PeerInfo for destination.\n");
5100 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5101 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5102 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5104 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5106 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5107 if (NULL == orig_peer_info)
5109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5110 " Creating PeerInfo for origin.\n");
5111 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5112 orig_peer_info->id = GNUNET_PEER_intern (pi);
5113 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5114 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5117 path = path_new (size);
5119 for (i = 0; i < size; i++)
5121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5122 GNUNET_i2s (&pi[i]));
5123 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5124 if (path->peers[i] == myid)
5127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5130 /* cannot be self, must be 'not found' */
5131 /* create path: self not found in path through self */
5132 GNUNET_break_op (0);
5133 path_destroy (path);
5137 path_add_to_peers (path, GNUNET_NO);
5138 tunnel_add_path (t, path, own_pos);
5139 if (own_pos == size - 1)
5141 /* It is for us! Send ack. */
5142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5143 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5144 if (NULL == t->peers)
5146 /* New tunnel! Notify clients on first payload message. */
5147 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5149 GNUNET_break (GNUNET_SYSERR !=
5150 GNUNET_CONTAINER_multihashmap_put (t->peers,
5151 &my_full_id.hashPubKey,
5154 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5159 struct MeshPeerPath *path2;
5161 /* It's for somebody else! Retransmit. */
5162 path2 = path_duplicate (path);
5163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5164 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5165 path2 = path_duplicate (path);
5166 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5167 send_create_path (dest_peer_info, path, t);
5174 * Core handler for path destruction
5176 * @param cls closure
5177 * @param message message
5178 * @param peer peer identity this notification is about
5179 * @param atsi performance data
5180 * @param atsi_count number of records in 'atsi'
5182 * @return GNUNET_OK to keep the connection open,
5183 * GNUNET_SYSERR to close it (signal serious error)
5186 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5187 const struct GNUNET_MessageHeader *message,
5188 const struct GNUNET_ATS_Information *atsi,
5189 unsigned int atsi_count)
5191 struct GNUNET_MESH_ManipulatePath *msg;
5192 struct GNUNET_PeerIdentity *pi;
5193 struct MeshPeerPath *path;
5194 struct MeshTunnel *t;
5195 unsigned int own_pos;
5199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5200 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5201 size = ntohs (message->size);
5202 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5204 GNUNET_break_op (0);
5208 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5209 if (size % sizeof (struct GNUNET_PeerIdentity))
5211 GNUNET_break_op (0);
5214 size /= sizeof (struct GNUNET_PeerIdentity);
5217 GNUNET_break_op (0);
5220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5222 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5223 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5225 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5227 t = tunnel_get (pi, ntohl (msg->tid));
5230 /* TODO notify back: we don't know this tunnel */
5231 GNUNET_break_op (0);
5234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5235 path = path_new (size);
5237 for (i = 0; i < size; i++)
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5240 GNUNET_i2s (&pi[i]));
5241 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5242 if (path->peers[i] == myid)
5245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5246 if (own_pos < path->length - 1)
5247 send_prebuilt_message (message, &pi[own_pos + 1], t);
5249 send_client_tunnel_disconnect(t, NULL);
5251 tunnel_delete_peer (t, path->peers[path->length - 1]);
5252 path_destroy (path);
5258 * Core handler for notifications of broken paths
5260 * @param cls closure
5261 * @param message message
5262 * @param peer peer identity this notification is about
5263 * @param atsi performance data
5264 * @param atsi_count number of records in 'atsi'
5266 * @return GNUNET_OK to keep the connection open,
5267 * GNUNET_SYSERR to close it (signal serious error)
5270 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5271 const struct GNUNET_MessageHeader *message,
5272 const struct GNUNET_ATS_Information *atsi,
5273 unsigned int atsi_count)
5275 struct GNUNET_MESH_PathBroken *msg;
5276 struct MeshTunnel *t;
5278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5279 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5280 msg = (struct GNUNET_MESH_PathBroken *) message;
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5282 GNUNET_i2s (&msg->peer1));
5283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5284 GNUNET_i2s (&msg->peer2));
5285 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5288 GNUNET_break_op (0);
5291 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5292 GNUNET_PEER_search (&msg->peer2));
5299 * Core handler for tunnel destruction
5301 * @param cls closure
5302 * @param message message
5303 * @param peer peer identity this notification is about
5304 * @param atsi performance data
5305 * @param atsi_count number of records in 'atsi'
5307 * @return GNUNET_OK to keep the connection open,
5308 * GNUNET_SYSERR to close it (signal serious error)
5311 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5312 const struct GNUNET_MessageHeader *message,
5313 const struct GNUNET_ATS_Information *atsi,
5314 unsigned int atsi_count)
5316 struct GNUNET_MESH_TunnelDestroy *msg;
5317 struct MeshTunnel *t;
5319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5320 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5321 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5323 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5324 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5327 /* Probably already got the message from another path,
5328 * destroyed the tunnel and retransmitted to children.
5331 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5334 if (t->id.oid == myid)
5336 GNUNET_break_op (0);
5339 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5341 /* Tunnel was incoming, notify clients */
5342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5343 t->local_tid, t->local_tid_dest);
5344 send_clients_tunnel_destroy (t);
5346 tunnel_send_destroy (t);
5347 t->destroy = GNUNET_YES;
5348 // TODO: add timeout to destroy the tunnel anyway
5354 * Core handler for mesh network traffic going from the origin to a peer
5356 * @param cls closure
5357 * @param peer peer identity this notification is about
5358 * @param message message
5359 * @param atsi performance data
5360 * @param atsi_count number of records in 'atsi'
5361 * @return GNUNET_OK to keep the connection open,
5362 * GNUNET_SYSERR to close it (signal serious error)
5365 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5366 const struct GNUNET_MessageHeader *message,
5367 const struct GNUNET_ATS_Information *atsi,
5368 unsigned int atsi_count)
5370 struct GNUNET_MESH_Unicast *msg;
5371 struct GNUNET_PeerIdentity *neighbor;
5372 struct MeshTunnelChildInfo *cinfo;
5373 struct MeshTunnel *t;
5374 GNUNET_PEER_Id dest_id;
5379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5382 size = ntohs (message->size);
5384 sizeof (struct GNUNET_MESH_Unicast) +
5385 sizeof (struct GNUNET_MessageHeader))
5390 msg = (struct GNUNET_MESH_Unicast *) message;
5391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5392 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5394 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5397 /* TODO notify back: we don't know this tunnel */
5398 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5399 GNUNET_break_op (0);
5402 pid = ntohl (msg->pid);
5403 if (t->fwd_pid == pid)
5405 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5406 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5407 " Already seen pid %u, DROPPING!\n", pid);
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5413 " pid %u not seen yet, forwarding\n", pid);
5416 t->skip += (pid - t->fwd_pid) - 1;
5419 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5421 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5422 GNUNET_break_op (0);
5423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5424 "Received PID %u, ACK %u\n",
5425 pid, t->last_fwd_ack);
5429 tunnel_reset_timeout (t);
5430 dest_id = GNUNET_PEER_search (&msg->destination);
5431 if (dest_id == myid)
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5434 " it's for us! sending to clients...\n");
5435 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5436 send_subscribed_clients (message, &msg[1].header, t);
5437 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5440 ttl = ntohl (msg->ttl);
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5444 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5445 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5446 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5450 " not for us, retransmitting...\n");
5452 neighbor = tree_get_first_hop (t->tree, dest_id);
5453 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5455 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5458 if (GNUNET_YES == t->nobuffer &&
5459 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5461 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5462 GNUNET_break_op (0);
5465 send_prebuilt_message (message, neighbor, t);
5466 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5472 * Core handler for mesh network traffic going from the origin to all peers
5474 * @param cls closure
5475 * @param message message
5476 * @param peer peer identity this notification is about
5477 * @param atsi performance data
5478 * @param atsi_count number of records in 'atsi'
5479 * @return GNUNET_OK to keep the connection open,
5480 * GNUNET_SYSERR to close it (signal serious error)
5482 * TODO: Check who we got this from, to validate route.
5485 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5486 const struct GNUNET_MessageHeader *message,
5487 const struct GNUNET_ATS_Information *atsi,
5488 unsigned int atsi_count)
5490 struct GNUNET_MESH_Multicast *msg;
5491 struct MeshTunnel *t;
5495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5497 size = ntohs (message->size);
5498 if (sizeof (struct GNUNET_MESH_Multicast) +
5499 sizeof (struct GNUNET_MessageHeader) > size)
5501 GNUNET_break_op (0);
5504 msg = (struct GNUNET_MESH_Multicast *) message;
5505 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5509 /* TODO notify that we dont know that tunnel */
5510 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5511 GNUNET_break_op (0);
5514 pid = ntohl (msg->pid);
5515 if (t->fwd_pid == pid)
5517 /* already seen this packet, drop */
5518 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5520 " Already seen pid %u, DROPPING!\n", pid);
5521 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5527 " pid %u not seen yet, forwarding\n", pid);
5529 t->skip += (pid - t->fwd_pid) - 1;
5531 tunnel_reset_timeout (t);
5533 /* Transmit to locally interested clients */
5534 if (NULL != t->peers &&
5535 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5537 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5538 send_subscribed_clients (message, &msg[1].header, t);
5539 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5542 if (ntohl (msg->ttl) == 0)
5544 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5546 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5549 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5550 tunnel_send_multicast (t, message);
5556 * Core handler for mesh network traffic toward the owner of a tunnel
5558 * @param cls closure
5559 * @param message message
5560 * @param peer peer identity this notification is about
5561 * @param atsi performance data
5562 * @param atsi_count number of records in 'atsi'
5564 * @return GNUNET_OK to keep the connection open,
5565 * GNUNET_SYSERR to close it (signal serious error)
5568 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5569 const struct GNUNET_MessageHeader *message,
5570 const struct GNUNET_ATS_Information *atsi,
5571 unsigned int atsi_count)
5573 struct GNUNET_MESH_ToOrigin *msg;
5574 struct GNUNET_PeerIdentity id;
5575 struct MeshPeerInfo *peer_info;
5576 struct MeshTunnel *t;
5580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5582 size = ntohs (message->size);
5583 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5584 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5586 GNUNET_break_op (0);
5589 msg = (struct GNUNET_MESH_ToOrigin *) message;
5590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5591 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5592 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5596 /* TODO notify that we dont know this tunnel (whom)? */
5597 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5598 GNUNET_break_op (0);
5602 if (NULL != t->owner)
5605 struct GNUNET_MESH_ToOrigin *copy;
5607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5608 " it's for us! sending to clients...\n");
5609 /* TODO signature verification */
5610 memcpy (cbuf, message, size);
5611 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5612 copy->tid = htonl (t->local_tid);
5613 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5614 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5615 ©->header, GNUNET_NO);
5616 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5620 " not for us, retransmitting...\n");
5622 peer_info = peer_info_get (&msg->oid);
5623 if (NULL == peer_info)
5625 /* unknown origin of tunnel */
5629 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5630 send_prebuilt_message (message, &id, t);
5631 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5638 * Core handler for mesh network traffic point-to-point acks.
5640 * @param cls closure
5641 * @param message message
5642 * @param peer peer identity this notification is about
5643 * @param atsi performance data
5644 * @param atsi_count number of records in 'atsi'
5646 * @return GNUNET_OK to keep the connection open,
5647 * GNUNET_SYSERR to close it (signal serious error)
5650 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5651 const struct GNUNET_MessageHeader *message,
5652 const struct GNUNET_ATS_Information *atsi,
5653 unsigned int atsi_count)
5655 struct GNUNET_MESH_ACK *msg;
5656 struct MeshTunnel *t;
5659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5661 msg = (struct GNUNET_MESH_ACK *) message;
5663 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5667 /* TODO notify that we dont know this tunnel (whom)? */
5668 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5669 GNUNET_break_op (0);
5672 ack = ntohl (msg->pid);
5674 /* Is this a forward or backward ACK? */
5675 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5677 struct MeshTunnelChildInfo *cinfo;
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5681 cinfo = tunnel_get_neighbor_fc (t, peer);
5682 cinfo->fwd_ack = ack;
5683 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5684 tunnel_unlock_fwd_queues (t);
5688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5690 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5691 tunnel_unlock_bck_queue (t);
5698 * Core handler for path ACKs
5700 * @param cls closure
5701 * @param message message
5702 * @param peer peer identity this notification is about
5703 * @param atsi performance data
5704 * @param atsi_count number of records in 'atsi'
5706 * @return GNUNET_OK to keep the connection open,
5707 * GNUNET_SYSERR to close it (signal serious error)
5710 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5711 const struct GNUNET_MessageHeader *message,
5712 const struct GNUNET_ATS_Information *atsi,
5713 unsigned int atsi_count)
5715 struct GNUNET_MESH_PathACK *msg;
5716 struct GNUNET_PeerIdentity id;
5717 struct MeshPeerInfo *peer_info;
5718 struct MeshPeerPath *p;
5719 struct MeshTunnel *t;
5721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5722 GNUNET_i2s (&my_full_id));
5723 msg = (struct GNUNET_MESH_PathACK *) message;
5724 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5727 /* TODO notify that we don't know the tunnel */
5728 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5730 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5734 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5736 peer_info = peer_info_get (&msg->peer_id);
5737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5738 GNUNET_i2s (&msg->peer_id));
5739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5742 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5745 "connect_by_string completed, stopping search\n");
5746 regex_cancel_search (t->regex_ctx);
5747 t->regex_ctx = NULL;
5750 /* Add paths to peers? */
5751 p = tree_get_path_to_peer (t->tree, peer_info->id);
5754 path_add_to_peers (p, GNUNET_YES);
5762 /* Message for us? */
5763 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5766 if (NULL == t->owner)
5768 GNUNET_break_op (0);
5771 if (NULL != t->dht_get_type)
5773 GNUNET_DHT_get_stop (t->dht_get_type);
5774 t->dht_get_type = NULL;
5776 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5778 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5779 send_client_peer_connected (t, peer_info->id);
5784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5785 " not for us, retransmitting...\n");
5786 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5787 peer_info = peer_info_get (&msg->oid);
5788 if (NULL == peer_info)
5790 /* If we know the tunnel, we should DEFINITELY know the peer */
5794 send_prebuilt_message (message, &id, t);
5800 * Core handler for mesh keepalives.
5802 * @param cls closure
5803 * @param message message
5804 * @param peer peer identity this notification is about
5805 * @param atsi performance data
5806 * @param atsi_count number of records in 'atsi'
5807 * @return GNUNET_OK to keep the connection open,
5808 * GNUNET_SYSERR to close it (signal serious error)
5810 * TODO: Check who we got this from, to validate route.
5813 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5814 const struct GNUNET_MessageHeader *message,
5815 const struct GNUNET_ATS_Information *atsi,
5816 unsigned int atsi_count)
5818 struct GNUNET_MESH_TunnelKeepAlive *msg;
5819 struct MeshTunnel *t;
5821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5824 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5825 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5829 /* TODO notify that we dont know that tunnel */
5830 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5831 GNUNET_break_op (0);
5835 tunnel_reset_timeout (t);
5837 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5838 tunnel_send_multicast (t, message);
5845 * Functions to handle messages from core
5847 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5848 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5849 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5850 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5851 sizeof (struct GNUNET_MESH_PathBroken)},
5852 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5853 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5854 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5855 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5856 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5857 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5858 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5859 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5860 sizeof (struct GNUNET_MESH_ACK)},
5861 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5862 sizeof (struct GNUNET_MESH_PathACK)},
5868 /******************************************************************************/
5869 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5870 /******************************************************************************/
5873 * deregister_app: iterator for removing each application registered by a client
5875 * @param cls closure
5876 * @param key the hash of the application id (used to access the hashmap)
5877 * @param value the value stored at the key (client)
5879 * @return GNUNET_OK on success
5882 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5884 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5885 GNUNET_break (GNUNET_YES ==
5886 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5892 * notify_client_connection_failure: notify a client that the connection to the
5893 * requested remote peer is not possible (for instance, no route found)
5894 * Function called when the socket is ready to queue more data. "buf" will be
5895 * NULL and "size" zero if the socket was closed for writing in the meantime.
5897 * @param cls closure
5898 * @param size number of bytes available in buf
5899 * @param buf where the callee should write the message
5900 * @return number of bytes written to buf
5903 notify_client_connection_failure (void *cls, size_t size, void *buf)
5906 struct MeshPeerInfo *peer_info;
5907 struct GNUNET_MESH_PeerControl *msg;
5908 struct GNUNET_PeerIdentity id;
5910 if (0 == size && NULL == buf)
5912 // TODO retry? cancel?
5916 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5917 peer_info = (struct MeshPeerInfo *) cls;
5918 msg = (struct GNUNET_MESH_PeerControl *) buf;
5919 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5920 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5921 // msg->tunnel_id = htonl(peer_info->t->tid);
5922 GNUNET_PEER_resolve (peer_info->id, &id);
5923 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5931 * Send keepalive packets for a peer
5933 * @param cls Closure (tunnel for which to send the keepalive).
5934 * @param tc Notification context.
5937 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5939 struct MeshTunnel *t = cls;
5940 struct GNUNET_MESH_TunnelKeepAlive *msg;
5941 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5944 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5945 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5951 "sending keepalive for tunnel %d\n", t->id.tid);
5953 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5954 msg->header.size = htons (size);
5955 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5956 msg->oid = my_full_id;
5957 msg->tid = htonl (t->id.tid);
5958 tunnel_send_multicast (t, &msg->header);
5960 t->path_refresh_task =
5961 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5962 tunnel_reset_timeout(t);
5967 * Function to process paths received for a new peer addition. The recorded
5968 * paths form the initial tunnel, which can be optimized later.
5969 * Called on each result obtained for the DHT search.
5971 * @param cls closure
5972 * @param exp when will this value expire
5973 * @param key key of the result
5974 * @param get_path path of the get request
5975 * @param get_path_length lenght of get_path
5976 * @param put_path path of the put request
5977 * @param put_path_length length of the put_path
5978 * @param type type of the result
5979 * @param size number of bytes in data
5980 * @param data pointer to the result data
5982 * TODO: re-issue the request after certain time? cancel after X results?
5985 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5986 const struct GNUNET_HashCode * key,
5987 const struct GNUNET_PeerIdentity *get_path,
5988 unsigned int get_path_length,
5989 const struct GNUNET_PeerIdentity *put_path,
5990 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5991 size_t size, const void *data)
5993 struct MeshPathInfo *path_info = cls;
5994 struct MeshPeerPath *p;
5995 struct GNUNET_PeerIdentity pi;
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5999 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6002 p = path_build_from_dht (get_path, get_path_length, put_path,
6004 path_add_to_peers (p, GNUNET_NO);
6006 for (i = 0; i < path_info->peer->ntunnels; i++)
6008 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6009 peer_info_connect (path_info->peer, path_info->t);
6017 * Function to process paths received for a new peer addition. The recorded
6018 * paths form the initial tunnel, which can be optimized later.
6019 * Called on each result obtained for the DHT search.
6021 * @param cls closure
6022 * @param exp when will this value expire
6023 * @param key key of the result
6024 * @param get_path path of the get request
6025 * @param get_path_length lenght of get_path
6026 * @param put_path path of the put request
6027 * @param put_path_length length of the put_path
6028 * @param type type of the result
6029 * @param size number of bytes in data
6030 * @param data pointer to the result data
6033 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6034 const struct GNUNET_HashCode * key,
6035 const struct GNUNET_PeerIdentity *get_path,
6036 unsigned int get_path_length,
6037 const struct GNUNET_PeerIdentity *put_path,
6038 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6039 size_t size, const void *data)
6041 const struct PBlock *pb = data;
6042 const struct GNUNET_PeerIdentity *pi = &pb->id;
6043 struct MeshTunnel *t = cls;
6044 struct MeshPeerInfo *peer_info;
6045 struct MeshPeerPath *p;
6047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6048 if (size != sizeof (struct PBlock))
6050 GNUNET_break_op (0);
6053 if (ntohl(pb->type) != t->type)
6055 GNUNET_break_op (0);
6058 GNUNET_assert (NULL != t->owner);
6059 peer_info = peer_info_get (pi);
6060 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6062 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6064 p = path_build_from_dht (get_path, get_path_length, put_path,
6066 path_add_to_peers (p, GNUNET_NO);
6068 tunnel_add_peer (t, peer_info);
6069 peer_info_connect (peer_info, t);
6074 * Function to process DHT string to regex matching.
6075 * Called on each result obtained for the DHT search.
6077 * @param cls closure (search context)
6078 * @param exp when will this value expire
6079 * @param key key of the result
6080 * @param get_path path of the get request (not used)
6081 * @param get_path_length lenght of get_path (not used)
6082 * @param put_path path of the put request (not used)
6083 * @param put_path_length length of the put_path (not used)
6084 * @param type type of the result
6085 * @param size number of bytes in data
6086 * @param data pointer to the result data
6089 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6090 const struct GNUNET_HashCode * key,
6091 const struct GNUNET_PeerIdentity *get_path,
6092 unsigned int get_path_length,
6093 const struct GNUNET_PeerIdentity *put_path,
6094 unsigned int put_path_length,
6095 enum GNUNET_BLOCK_Type type,
6096 size_t size, const void *data)
6098 const struct MeshRegexAccept *block = data;
6099 struct MeshRegexSearchContext *ctx = cls;
6100 struct MeshRegexSearchInfo *info = ctx->info;
6101 struct MeshPeerPath *p;
6102 struct MeshPeerInfo *peer_info;
6104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6107 peer_info = peer_info_get(&block->id);
6108 p = path_build_from_dht (get_path, get_path_length, put_path,
6110 path_add_to_peers (p, GNUNET_NO);
6113 tunnel_add_peer (info->t, peer_info);
6114 peer_info_connect (peer_info, info->t);
6115 if (0 == info->peer)
6117 info->peer = peer_info->id;
6121 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6124 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6125 ®ex_connect_timeout,
6133 * Function to process DHT string to regex matching.
6134 * Called on each result obtained for the DHT search.
6136 * @param cls closure (search context)
6137 * @param exp when will this value expire
6138 * @param key key of the result
6139 * @param get_path path of the get request (not used)
6140 * @param get_path_length lenght of get_path (not used)
6141 * @param put_path path of the put request (not used)
6142 * @param put_path_length length of the put_path (not used)
6143 * @param type type of the result
6144 * @param size number of bytes in data
6145 * @param data pointer to the result data
6147 * TODO: re-issue the request after certain time? cancel after X results?
6150 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6151 const struct GNUNET_HashCode * key,
6152 const struct GNUNET_PeerIdentity *get_path,
6153 unsigned int get_path_length,
6154 const struct GNUNET_PeerIdentity *put_path,
6155 unsigned int put_path_length,
6156 enum GNUNET_BLOCK_Type type,
6157 size_t size, const void *data)
6159 const struct MeshRegexBlock *block = data;
6160 struct MeshRegexSearchContext *ctx = cls;
6161 struct MeshRegexSearchInfo *info = ctx->info;
6165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6166 "DHT GET STRING RETURNED RESULTS\n");
6167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6168 " key: %s\n", GNUNET_h2s (key));
6170 copy = GNUNET_malloc (size);
6171 memcpy (copy, data, size);
6172 GNUNET_break (GNUNET_OK ==
6173 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6174 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6175 len = ntohl (block->n_proof);
6177 char proof[len + 1];
6179 memcpy (proof, &block[1], len);
6181 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6183 GNUNET_break_op (0);
6187 len = strlen (info->description);
6188 if (len == ctx->position) // String processed
6190 if (GNUNET_YES == ntohl (block->accepting))
6192 regex_find_path(key, ctx);
6196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6197 // FIXME REGEX this block not successful, wait for more? start timeout?
6202 regex_next_edge (block, size, ctx);
6207 /******************************************************************************/
6208 /********************* MESH LOCAL HANDLES **************************/
6209 /******************************************************************************/
6213 * Handler for client disconnection
6215 * @param cls closure
6216 * @param client identification of the client; NULL
6217 * for the last call when the server is destroyed
6220 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6222 struct MeshClient *c;
6223 struct MeshClient *next;
6226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6235 if (c->handle != client)
6237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6243 GNUNET_SERVER_client_drop (c->handle);
6244 c->shutting_down = GNUNET_YES;
6245 GNUNET_assert (NULL != c->own_tunnels);
6246 GNUNET_assert (NULL != c->incoming_tunnels);
6247 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6248 &tunnel_destroy_iterator, c);
6249 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6250 &tunnel_destroy_iterator, c);
6251 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6252 &tunnel_destroy_iterator, c);
6253 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6254 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6255 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6257 /* deregister clients applications */
6258 if (NULL != c->apps)
6260 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6261 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6263 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6264 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6266 GNUNET_SCHEDULER_cancel (announce_applications_task);
6267 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6269 if (NULL != c->types)
6270 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6271 for (i = 0; i < c->n_regex; i++)
6273 GNUNET_free (c->regexes[i]);
6275 GNUNET_free_non_null (c->regexes);
6276 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6277 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6279 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6282 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6291 * Handler for new clients
6293 * @param cls closure
6294 * @param client identification of the client
6295 * @param message the actual message, which includes messages the client wants
6298 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6299 const struct GNUNET_MessageHeader *message)
6301 struct GNUNET_MESH_ClientConnect *cc_msg;
6302 struct MeshClient *c;
6303 GNUNET_MESH_ApplicationType *a;
6310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6311 /* Check data sanity */
6312 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6313 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6314 ntypes = ntohs (cc_msg->types);
6315 napps = ntohs (cc_msg->applications);
6317 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6324 /* Create new client structure */
6325 c = GNUNET_malloc (sizeof (struct MeshClient));
6326 c->id = next_client_id++;
6327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6329 GNUNET_SERVER_client_keep (client);
6330 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6333 GNUNET_MESH_ApplicationType at;
6334 struct GNUNET_HashCode hc;
6336 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6337 for (i = 0; i < napps; i++)
6340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6341 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6342 /* store in clients hashmap */
6343 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6344 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6345 /* store in global hashmap, for announcements */
6346 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6347 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6349 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6350 announce_applications_task =
6351 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6357 struct GNUNET_HashCode hc;
6359 t = (uint16_t *) & a[napps];
6360 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6361 for (i = 0; i < ntypes; i++)
6364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6365 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6367 /* store in clients hashmap */
6368 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6370 /* store in global hashmap */
6371 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6372 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6376 " client has %u+%u subscriptions\n", napps, ntypes);
6378 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6379 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6380 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6381 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6382 GNUNET_SERVER_notification_context_add (nc, client);
6383 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6385 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6391 * Handler for clients announcing available services by a regular expression.
6393 * @param cls closure
6394 * @param client identification of the client
6395 * @param message the actual message, which includes messages the client wants
6398 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6399 const struct GNUNET_MessageHeader *message)
6401 struct MeshClient *c;
6405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6407 /* Sanity check for client registration */
6408 if (NULL == (c = client_get (client)))
6411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6416 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6417 regex = GNUNET_malloc (len + 1);
6418 memcpy (regex, &message[1], len);
6420 GNUNET_array_append (c->regexes, c->n_regex, regex);
6421 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6423 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6429 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6435 * Handler for requests of new tunnels
6437 * @param cls closure
6438 * @param client identification of the client
6439 * @param message the actual message
6442 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6443 const struct GNUNET_MessageHeader *message)
6445 struct GNUNET_MESH_TunnelMessage *t_msg;
6446 struct MeshTunnel *t;
6447 struct MeshClient *c;
6448 MESH_TunnelNumber tid;
6450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6452 /* Sanity check for client registration */
6453 if (NULL == (c = client_get (client)))
6456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6461 /* Message sanity check */
6462 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6469 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6470 /* Sanity check for tunnel numbering */
6471 tid = ntohl (t_msg->tunnel_id);
6472 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6475 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6478 /* Sanity check for duplicate tunnel IDs */
6479 if (NULL != tunnel_get_by_local_id (c, tid))
6482 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6486 while (NULL != tunnel_get_by_pi (myid, next_tid))
6487 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6488 t = tunnel_new (myid, next_tid++, c, tid);
6491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6493 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6496 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6498 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6499 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6502 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6508 * Handler for requests of deleting tunnels
6510 * @param cls closure
6511 * @param client identification of the client
6512 * @param message the actual message
6515 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6516 const struct GNUNET_MessageHeader *message)
6518 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6519 struct MeshClient *c;
6520 struct MeshTunnel *t;
6521 MESH_TunnelNumber tid;
6523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6524 "Got a DESTROY TUNNEL from client!\n");
6526 /* Sanity check for client registration */
6527 if (NULL == (c = client_get (client)))
6530 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6535 /* Message sanity check */
6536 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6543 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6545 /* Retrieve tunnel */
6546 tid = ntohl (tunnel_msg->tunnel_id);
6547 t = tunnel_get_by_local_id(c, tid);
6550 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6552 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6555 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6557 client_ignore_tunnel (c, t);
6559 // TODO: when to destroy incoming tunnel?
6560 if (t->nclients == 0)
6562 GNUNET_assert (GNUNET_YES ==
6563 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6565 GNUNET_assert (GNUNET_YES ==
6566 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6567 &my_full_id.hashPubKey,
6571 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6574 send_client_tunnel_disconnect(t, c);
6575 client_delete_tunnel(c, t);
6577 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6579 tunnel_send_destroy (t);
6580 t->destroy = GNUNET_YES;
6581 // The tunnel will be destroyed when the last message is transmitted.
6582 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6588 * Handler for requests of seeting tunnel's speed.
6590 * @param cls Closure (unused).
6591 * @param client Identification of the client.
6592 * @param message The actual message.
6595 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6596 const struct GNUNET_MessageHeader *message)
6598 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6599 struct MeshClient *c;
6600 struct MeshTunnel *t;
6601 MESH_TunnelNumber tid;
6603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6604 "Got a SPEED request from client!\n");
6606 /* Sanity check for client registration */
6607 if (NULL == (c = client_get (client)))
6610 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6616 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6618 /* Retrieve tunnel */
6619 tid = ntohl (tunnel_msg->tunnel_id);
6620 t = tunnel_get_by_local_id(c, tid);
6623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6629 switch (ntohs(message->type))
6631 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6632 t->speed_min = GNUNET_YES;
6634 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6635 t->speed_min = GNUNET_NO;
6640 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6645 * Handler for requests of seeting tunnel's buffering policy.
6647 * @param cls Closure (unused).
6648 * @param client Identification of the client.
6649 * @param message The actual message.
6652 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6653 const struct GNUNET_MessageHeader *message)
6655 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6656 struct MeshClient *c;
6657 struct MeshTunnel *t;
6658 MESH_TunnelNumber tid;
6660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6661 "Got a BUFFER request from client!\n");
6663 /* Sanity check for client registration */
6664 if (NULL == (c = client_get (client)))
6667 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6672 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6674 /* Retrieve tunnel */
6675 tid = ntohl (tunnel_msg->tunnel_id);
6676 t = tunnel_get_by_local_id(c, tid);
6679 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6681 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6685 switch (ntohs(message->type))
6687 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6688 t->nobuffer = GNUNET_NO;
6690 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6691 t->nobuffer = GNUNET_YES;
6697 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6702 * Handler for connection requests to new peers
6704 * @param cls closure
6705 * @param client identification of the client
6706 * @param message the actual message (PeerControl)
6709 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6710 const struct GNUNET_MessageHeader *message)
6712 struct GNUNET_MESH_PeerControl *peer_msg;
6713 struct MeshPeerInfo *peer_info;
6714 struct MeshClient *c;
6715 struct MeshTunnel *t;
6716 MESH_TunnelNumber tid;
6718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6719 /* Sanity check for client registration */
6720 if (NULL == (c = client_get (client)))
6723 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6728 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6730 /* Sanity check for message size */
6731 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6738 /* Tunnel exists? */
6739 tid = ntohl (peer_msg->tunnel_id);
6740 t = tunnel_get_by_local_id (c, tid);
6744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6748 /* Does client own tunnel? */
6749 if (t->owner->handle != client)
6752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6756 GNUNET_i2s (&peer_msg->peer));
6757 peer_info = peer_info_get (&peer_msg->peer);
6759 tunnel_add_peer (t, peer_info);
6760 peer_info_connect (peer_info, t);
6762 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6768 * Handler for disconnection requests of peers in a tunnel
6770 * @param cls closure
6771 * @param client identification of the client
6772 * @param message the actual message (PeerControl)
6775 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6776 const struct GNUNET_MessageHeader *message)
6778 struct GNUNET_MESH_PeerControl *peer_msg;
6779 struct MeshPeerInfo *peer_info;
6780 struct MeshClient *c;
6781 struct MeshTunnel *t;
6782 MESH_TunnelNumber tid;
6784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6785 /* Sanity check for client registration */
6786 if (NULL == (c = client_get (client)))
6789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6794 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6796 /* Sanity check for message size */
6797 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6804 /* Tunnel exists? */
6805 tid = ntohl (peer_msg->tunnel_id);
6806 t = tunnel_get_by_local_id (c, tid);
6810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6815 /* Does client own tunnel? */
6816 if (t->owner->handle != client)
6819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6824 GNUNET_i2s (&peer_msg->peer));
6825 /* Is the peer in the tunnel? */
6827 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6828 if (NULL == peer_info)
6831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6835 /* Ok, delete peer from tunnel */
6836 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6837 &peer_msg->peer.hashPubKey);
6839 send_destroy_path (t, peer_info->id);
6840 tunnel_delete_peer (t, peer_info->id);
6841 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6846 * Handler for blacklist requests of peers in a tunnel
6848 * @param cls closure
6849 * @param client identification of the client
6850 * @param message the actual message (PeerControl)
6852 * FIXME implement DHT block bloomfilter
6855 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6856 const struct GNUNET_MessageHeader *message)
6858 struct GNUNET_MESH_PeerControl *peer_msg;
6859 struct MeshClient *c;
6860 struct MeshTunnel *t;
6861 MESH_TunnelNumber tid;
6863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST 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 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6895 GNUNET_PEER_intern(&peer_msg->peer));
6900 * Handler for unblacklist requests of peers in a tunnel
6902 * @param cls closure
6903 * @param client identification of the client
6904 * @param message the actual message (PeerControl)
6907 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6908 const struct GNUNET_MessageHeader *message)
6910 struct GNUNET_MESH_PeerControl *peer_msg;
6911 struct MeshClient *c;
6912 struct MeshTunnel *t;
6913 MESH_TunnelNumber tid;
6917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6918 /* Sanity check for client registration */
6919 if (NULL == (c = client_get (client)))
6922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6927 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6929 /* Sanity check for message size */
6930 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6933 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6937 /* Tunnel exists? */
6938 tid = ntohl (peer_msg->tunnel_id);
6939 t = tunnel_get_by_local_id (c, tid);
6943 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6948 /* if peer is not known, complain */
6949 pid = GNUNET_PEER_search (&peer_msg->peer);
6956 /* search and remove from list */
6957 for (i = 0; i < t->nblacklisted; i++)
6959 if (t->blacklisted[i] == pid)
6961 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6962 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6967 /* if peer hasn't been blacklisted, complain */
6973 * Handler for connection requests to new peers by type
6975 * @param cls closure
6976 * @param client identification of the client
6977 * @param message the actual message (ConnectPeerByType)
6980 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6981 const struct GNUNET_MessageHeader *message)
6983 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6984 struct MeshClient *c;
6985 struct MeshTunnel *t;
6986 struct GNUNET_HashCode hash;
6987 MESH_TunnelNumber tid;
6989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6990 /* Sanity check for client registration */
6991 if (NULL == (c = client_get (client)))
6994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6999 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7001 /* Sanity check for message size */
7002 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7003 ntohs (connect_msg->header.size))
7006 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7010 /* Tunnel exists? */
7011 tid = ntohl (connect_msg->tunnel_id);
7012 t = tunnel_get_by_local_id (c, tid);
7016 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7020 /* Does client own tunnel? */
7021 if (t->owner->handle != client)
7024 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7028 /* Do WE have the service? */
7029 t->type = ntohl (connect_msg->type);
7030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7031 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7032 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7035 /* Yes! Fast forward, add ourselves to the tunnel and send the
7036 * good news to the client, and alert the destination client of
7037 * an incoming tunnel.
7039 * FIXME send a path create to self, avoid code duplication
7041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7042 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7043 peer_info_get (&my_full_id),
7044 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7047 send_client_peer_connected (t, myid);
7048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7049 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7051 t->local_tid_dest = next_local_tid++;
7052 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7053 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7054 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7058 /* Ok, lets find a peer offering the service */
7059 if (NULL != t->dht_get_type)
7061 GNUNET_DHT_get_stop (t->dht_get_type);
7063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7064 GNUNET_h2s (&hash));
7066 GNUNET_DHT_get_start (dht_handle,
7067 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7069 dht_replication_level,
7070 GNUNET_DHT_RO_RECORD_ROUTE |
7071 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7073 &dht_get_type_handler, t);
7075 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7081 * Handler for connection requests to new peers by a string service description.
7083 * @param cls closure
7084 * @param client identification of the client
7085 * @param message the actual message, which includes messages the client wants
7088 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7089 const struct GNUNET_MessageHeader *message)
7091 struct GNUNET_MESH_ConnectPeerByString *msg;
7092 struct MeshRegexSearchContext *ctx;
7093 struct MeshRegexSearchInfo *info;
7094 struct GNUNET_DHT_GetHandle *get_h;
7095 struct GNUNET_HashCode key;
7096 struct MeshTunnel *t;
7097 struct MeshClient *c;
7098 MESH_TunnelNumber tid;
7102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7103 "Connect by string started\n");
7104 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7105 size = htons (message->size);
7107 /* Sanity check for client registration */
7108 if (NULL == (c = client_get (client)))
7111 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7116 /* Message size sanity check */
7117 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7120 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7124 /* Tunnel exists? */
7125 tid = ntohl (msg->tunnel_id);
7126 t = tunnel_get_by_local_id (c, tid);
7130 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7134 /* Does client own tunnel? */
7135 if (t->owner->handle != client)
7138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7143 " on tunnel %s [%u]\n",
7144 GNUNET_i2s(&my_full_id),
7147 /* Only one connect_by_string allowed at the same time! */
7148 /* FIXME: allow more, return handle at api level to cancel, document */
7149 if (NULL != t->regex_ctx)
7152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7156 /* Find string itself */
7157 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7158 string = (const char *) &msg[1];
7160 /* Initialize context */
7161 size = GNUNET_REGEX_get_first_key(string, len, &key);
7162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7163 " consumed %u bits out of %u\n", size, len);
7164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7165 " looking for %s\n", GNUNET_h2s (&key));
7167 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7169 info->description = GNUNET_malloc (len + 1);
7170 memcpy (info->description, string, len);
7171 info->description[len] = '\0';
7172 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7173 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7176 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7177 ctx->position = size;
7181 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7183 /* Start search in DHT */
7184 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7185 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7186 &key, /* key to search */
7187 dht_replication_level, /* replication level */
7188 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7189 NULL, /* xquery */ // FIXME BLOOMFILTER
7190 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7191 &dht_get_string_handler, ctx);
7193 GNUNET_break (GNUNET_OK ==
7194 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7197 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7199 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7205 * Handler for client traffic directed to one peer
7207 * @param cls closure
7208 * @param client identification of the client
7209 * @param message the actual message
7212 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7213 const struct GNUNET_MessageHeader *message)
7215 struct MeshClient *c;
7216 struct MeshTunnel *t;
7217 struct MeshPeerInfo *pi;
7218 struct GNUNET_MESH_Unicast *data_msg;
7219 MESH_TunnelNumber tid;
7222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7223 "Got a unicast request from a client!\n");
7225 /* Sanity check for client registration */
7226 if (NULL == (c = client_get (client)))
7229 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7234 data_msg = (struct GNUNET_MESH_Unicast *) message;
7236 /* Sanity check for message size */
7237 size = ntohs (message->size);
7238 if (sizeof (struct GNUNET_MESH_Unicast) +
7239 sizeof (struct GNUNET_MessageHeader) > size)
7242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7246 /* Tunnel exists? */
7247 tid = ntohl (data_msg->tid);
7248 t = tunnel_get_by_local_id (c, tid);
7252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7256 /* Is it a local tunnel? Then, does client own the tunnel? */
7257 if (t->owner->handle != client)
7260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7264 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7265 &data_msg->destination.hashPubKey);
7266 /* Is the selected peer in the tunnel? */
7270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7274 /* PID should be as expected */
7275 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7278 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7279 "Unicast PID, expected %u, got %u\n",
7280 t->fwd_pid + 1, ntohl (data_msg->pid));
7281 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7285 /* Ok, everything is correct, send the message
7286 * (pretend we got it from a mesh peer)
7289 /* Work around const limitation */
7290 char buf[ntohs (message->size)] GNUNET_ALIGN;
7291 struct GNUNET_MESH_Unicast *copy;
7293 copy = (struct GNUNET_MESH_Unicast *) buf;
7294 memcpy (buf, data_msg, size);
7295 copy->oid = my_full_id;
7296 copy->tid = htonl (t->id.tid);
7297 copy->ttl = htonl (default_ttl);
7298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7299 " calling generic handler...\n");
7300 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7303 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7310 * Handler for client traffic directed to the origin
7312 * @param cls closure
7313 * @param client identification of the client
7314 * @param message the actual message
7317 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7318 const struct GNUNET_MessageHeader *message)
7320 struct GNUNET_MESH_ToOrigin *data_msg;
7321 struct MeshTunnelClientInfo *clinfo;
7322 struct MeshClient *c;
7323 struct MeshTunnel *t;
7324 MESH_TunnelNumber tid;
7327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7328 "Got a ToOrigin request from a client!\n");
7329 /* Sanity check for client registration */
7330 if (NULL == (c = client_get (client)))
7333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7338 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7340 /* Sanity check for message size */
7341 size = ntohs (message->size);
7342 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7343 sizeof (struct GNUNET_MessageHeader) > size)
7346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7350 /* Tunnel exists? */
7351 tid = ntohl (data_msg->tid);
7352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7353 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7356 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7359 t = tunnel_get_by_local_id (c, tid);
7362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7369 /* It should be sent by someone who has this as incoming tunnel. */
7370 if (GNUNET_NO == client_knows_tunnel (c, t))
7373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7377 /* PID should be as expected */
7378 clinfo = tunnel_get_client_fc (t, c);
7379 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7383 "To Origin PID, expected %u, got %u\n",
7384 clinfo->bck_pid + 1,
7385 ntohl (data_msg->pid));
7386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7390 /* Ok, everything is correct, send the message
7391 * (pretend we got it from a mesh peer)
7395 char buf[ntohs (message->size)] GNUNET_ALIGN;
7396 struct GNUNET_MESH_ToOrigin *copy;
7398 /* Work around const limitation */
7399 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7400 memcpy (buf, data_msg, size);
7401 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7402 copy->tid = htonl (t->id.tid);
7403 copy->ttl = htonl (default_ttl);
7404 if (ntohl (copy->pid) != (t->bck_pid + 1))
7407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7408 "To Origin PID, expected %u, got %u\n",
7414 copy->sender = my_full_id;
7415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7416 " calling generic handler...\n");
7417 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7419 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7426 * Handler for client traffic directed to all peers in a tunnel
7428 * @param cls closure
7429 * @param client identification of the client
7430 * @param message the actual message
7433 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7434 const struct GNUNET_MessageHeader *message)
7436 struct MeshClient *c;
7437 struct MeshTunnel *t;
7438 struct GNUNET_MESH_Multicast *data_msg;
7439 MESH_TunnelNumber tid;
7441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7442 "Got a multicast request from a client!\n");
7444 /* Sanity check for client registration */
7445 if (NULL == (c = client_get (client)))
7448 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7453 data_msg = (struct GNUNET_MESH_Multicast *) message;
7455 /* Sanity check for message size */
7456 if (sizeof (struct GNUNET_MESH_Multicast) +
7457 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7464 /* Tunnel exists? */
7465 tid = ntohl (data_msg->tid);
7466 t = tunnel_get_by_local_id (c, tid);
7470 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7471 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7473 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7477 /* Does client own tunnel? */
7478 if (t->owner->handle != client)
7481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7485 /* PID should be as expected */
7486 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7490 "Multicast PID, expected %u, got %u\n",
7491 t->fwd_pid + 1, ntohl (data_msg->pid));
7492 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7497 char buf[ntohs (message->size)] GNUNET_ALIGN;
7498 struct GNUNET_MESH_Multicast *copy;
7500 copy = (struct GNUNET_MESH_Multicast *) buf;
7501 memcpy (buf, message, ntohs (message->size));
7502 copy->oid = my_full_id;
7503 copy->tid = htonl (t->id.tid);
7504 copy->ttl = htonl (default_ttl);
7505 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7507 " calling generic handler...\n");
7508 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7511 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7517 * Handler for client's ACKs for payload traffic.
7519 * @param cls Closure (unused).
7520 * @param client Identification of the client.
7521 * @param message The actual message.
7524 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7525 const struct GNUNET_MessageHeader *message)
7527 struct GNUNET_MESH_LocalAck *msg;
7528 struct MeshTunnel *t;
7529 struct MeshClient *c;
7530 MESH_TunnelNumber tid;
7533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7534 /* Sanity check for client registration */
7535 if (NULL == (c = client_get (client)))
7538 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7543 msg = (struct GNUNET_MESH_LocalAck *) message;
7545 /* Tunnel exists? */
7546 tid = ntohl (msg->tunnel_id);
7547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7548 t = tunnel_get_by_local_id (c, tid);
7552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7554 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7558 ack = ntohl (msg->max_pid);
7559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7561 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7562 if (NULL != t->owner && t->owner->handle == client)
7564 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7566 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7570 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7571 tunnel_set_client_fwd_ack (t, c, ack);
7572 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7575 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7582 * Functions to handle messages from clients
7584 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7585 {&handle_local_new_client, NULL,
7586 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7587 {&handle_local_announce_regex, NULL,
7588 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7589 {&handle_local_tunnel_create, NULL,
7590 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7591 sizeof (struct GNUNET_MESH_TunnelMessage)},
7592 {&handle_local_tunnel_destroy, NULL,
7593 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7594 sizeof (struct GNUNET_MESH_TunnelMessage)},
7595 {&handle_local_tunnel_speed, NULL,
7596 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7597 sizeof (struct GNUNET_MESH_TunnelMessage)},
7598 {&handle_local_tunnel_speed, NULL,
7599 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7600 sizeof (struct GNUNET_MESH_TunnelMessage)},
7601 {&handle_local_tunnel_buffer, NULL,
7602 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7603 sizeof (struct GNUNET_MESH_TunnelMessage)},
7604 {&handle_local_tunnel_buffer, NULL,
7605 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7606 sizeof (struct GNUNET_MESH_TunnelMessage)},
7607 {&handle_local_connect_add, NULL,
7608 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7609 sizeof (struct GNUNET_MESH_PeerControl)},
7610 {&handle_local_connect_del, NULL,
7611 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7612 sizeof (struct GNUNET_MESH_PeerControl)},
7613 {&handle_local_blacklist, NULL,
7614 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7615 sizeof (struct GNUNET_MESH_PeerControl)},
7616 {&handle_local_unblacklist, NULL,
7617 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7618 sizeof (struct GNUNET_MESH_PeerControl)},
7619 {&handle_local_connect_by_type, NULL,
7620 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7621 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7622 {&handle_local_connect_by_string, NULL,
7623 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7624 {&handle_local_unicast, NULL,
7625 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7626 {&handle_local_to_origin, NULL,
7627 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7628 {&handle_local_multicast, NULL,
7629 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7630 {&handle_local_ack, NULL,
7631 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7632 sizeof (struct GNUNET_MESH_LocalAck)},
7638 * To be called on core init/fail.
7640 * @param cls service closure
7641 * @param server handle to the server for this service
7642 * @param identity the public identity of this peer
7645 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7646 const struct GNUNET_PeerIdentity *identity)
7649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7650 core_handle = server;
7651 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7654 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7655 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7657 GNUNET_abort(); // Try harder
7664 * Method called whenever a given peer connects.
7666 * @param cls closure
7667 * @param peer peer identity this notification is about
7668 * @param atsi performance data for the connection
7669 * @param atsi_count number of records in 'atsi'
7672 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7673 const struct GNUNET_ATS_Information *atsi,
7674 unsigned int atsi_count)
7676 struct MeshPeerInfo *peer_info;
7677 struct MeshPeerPath *path;
7679 DEBUG_CONN ("Peer connected\n");
7680 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7681 peer_info = peer_info_get (peer);
7682 if (myid == peer_info->id)
7684 DEBUG_CONN (" (self)\n");
7689 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7691 path = path_new (2);
7692 path->peers[0] = myid;
7693 path->peers[1] = peer_info->id;
7694 GNUNET_PEER_change_rc (myid, 1);
7695 GNUNET_PEER_change_rc (peer_info->id, 1);
7696 peer_info_add_path (peer_info, path, GNUNET_YES);
7697 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7703 * Method called whenever a peer disconnects.
7705 * @param cls closure
7706 * @param peer peer identity this notification is about
7709 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7711 struct MeshPeerInfo *pi;
7712 struct MeshPeerQueue *q;
7713 struct MeshPeerQueue *n;
7715 DEBUG_CONN ("Peer disconnected\n");
7716 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7728 /* try to reroute this traffic instead */
7729 queue_destroy(q, GNUNET_YES);
7733 peer_info_remove_path (pi, pi->id, myid);
7736 DEBUG_CONN (" (self)\n");
7738 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7743 /******************************************************************************/
7744 /************************ MAIN FUNCTIONS ****************************/
7745 /******************************************************************************/
7748 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7750 * @param cls closure
7751 * @param key current key code
7752 * @param value value in the hash map
7753 * @return GNUNET_YES if we should continue to iterate,
7757 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7759 struct MeshTunnel *t = value;
7766 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7768 * @param cls closure
7769 * @param key current key code
7770 * @param value value in the hash map
7771 * @return GNUNET_YES if we should continue to iterate,
7775 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7777 struct MeshPeerInfo *p = value;
7778 struct MeshPeerQueue *q;
7779 struct MeshPeerQueue *n;
7787 queue_destroy(q, GNUNET_YES);
7791 peer_info_destroy (p);
7797 * Task run during shutdown.
7803 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7807 if (core_handle != NULL)
7809 GNUNET_CORE_disconnect (core_handle);
7814 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
7817 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7818 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7819 if (dht_handle != NULL)
7821 GNUNET_DHT_disconnect (dht_handle);
7826 GNUNET_SERVER_notification_context_destroy (nc);
7829 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7831 GNUNET_SCHEDULER_cancel (announce_id_task);
7832 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7839 * Callback for hostkey read/generation
7842 * @param pk the private key
7843 * @param emsg error message
7846 key_generation_cb (void *cls,
7847 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
7850 struct MeshPeerInfo *peer;
7851 struct MeshPeerPath *p;
7856 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7857 _("Mesh service could not access hostkey. Exiting.\n"));
7858 GNUNET_SCHEDULER_shutdown ();
7861 my_private_key = pk;
7862 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7863 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7864 &my_full_id.hashPubKey);
7865 myid = GNUNET_PEER_intern (&my_full_id);
7866 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7867 "Mesh for peer [%s] starting\n",
7868 GNUNET_i2s(&my_full_id));
7870 // transport_handle = GNUNET_TRANSPORT_connect(c,
7880 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7883 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7884 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7885 GNUNET_SERVER_disconnect_notify (server_handle,
7886 &handle_local_client_disconnect, NULL);
7890 clients_tail = NULL;
7893 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7894 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7896 /* Create a peer_info for the local peer */
7897 peer = peer_info_get (&my_full_id);
7900 GNUNET_PEER_change_rc (myid, 1);
7901 peer_info_add_path (peer, p, GNUNET_YES);
7902 GNUNET_SERVER_resume (server_handle);
7903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7908 * Process mesh requests.
7910 * @param cls closure
7911 * @param server the initialized server
7912 * @param c configuration to use
7915 run (void *cls, struct GNUNET_SERVER_Handle *server,
7916 const struct GNUNET_CONFIGURATION_Handle *c)
7920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7921 server_handle = server;
7922 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7923 NULL, /* Closure passed to MESH functions */
7924 &core_init, /* Call core_init once connected */
7925 &core_connect, /* Handle connects */
7926 &core_disconnect, /* remove peers on disconnects */
7927 NULL, /* Don't notify about all incoming messages */
7928 GNUNET_NO, /* For header only in notification */
7929 NULL, /* Don't notify about all outbound messages */
7930 GNUNET_NO, /* For header-only out notification */
7931 core_handlers); /* Register these handlers */
7933 if (core_handle == NULL)
7936 GNUNET_SCHEDULER_shutdown ();
7941 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7944 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7946 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7948 GNUNET_SCHEDULER_shutdown ();
7953 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7954 &refresh_path_time))
7956 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7958 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7959 "refresh path time");
7960 GNUNET_SCHEDULER_shutdown ();
7965 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7966 &app_announce_time))
7968 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7970 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7971 "app announce time");
7972 GNUNET_SCHEDULER_shutdown ();
7977 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7980 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7982 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7983 "id announce time");
7984 GNUNET_SCHEDULER_shutdown ();
7989 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7990 &unacknowledged_wait_time))
7992 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7994 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7995 "unacknowledged wait time");
7996 GNUNET_SCHEDULER_shutdown ();
8001 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8004 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8006 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
8008 GNUNET_SCHEDULER_shutdown ();
8013 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8016 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8018 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
8020 GNUNET_SCHEDULER_shutdown ();
8025 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8028 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8030 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
8032 GNUNET_SCHEDULER_shutdown ();
8037 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8042 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
8048 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8049 &dht_replication_level))
8051 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8053 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
8054 "dht replication level", 10);
8055 dht_replication_level = 10;
8058 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8059 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8060 peers = GNUNET_CONTAINER_multihashmap_create (32);
8061 applications = GNUNET_CONTAINER_multihashmap_create (32);
8062 types = GNUNET_CONTAINER_multihashmap_create (32);
8064 dht_handle = GNUNET_DHT_connect (c, 64);
8065 if (NULL == dht_handle)
8069 stats = GNUNET_STATISTICS_create ("mesh", c);
8071 GNUNET_SERVER_suspend (server_handle);
8072 /* Scheduled the task to clean up when shutdown is called */
8073 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8075 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8076 GNUNET_free (keyfile);
8081 * The main function for the mesh service.
8083 * @param argc number of arguments from the command line
8084 * @param argv command line arguments
8085 * @return 0 ok, 1 on error
8088 main (int argc, char *const *argv)
8092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8095 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8101 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8102 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8103 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);