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_NO
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 unsigned long long default_ttl;
826 static unsigned long long dht_replication_level;
827 static unsigned long long max_tunnels;
828 static unsigned long long 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);
2089 if (GNUNET_YES == t->speed_min)
2090 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2091 if (GNUNET_YES == t->nobuffer)
2092 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2093 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2094 &tmsg.header, GNUNET_NO);
2095 tunnel_add_client (t, c);
2096 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2098 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2099 c->incoming_tunnels, &hash, t,
2100 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2102 *tid = htonl (t->local_tid_dest);
2105 /* Check if the client wants to get traffic from the tunnel */
2106 if (GNUNET_NO == client_wants_tunnel(c, t))
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2110 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2111 (struct GNUNET_MessageHeader
2112 *) cbuf, GNUNET_NO);
2121 * Notify the client that owns the tunnel that a peer has connected to it
2122 * (the requested path to it has been confirmed).
2124 * @param t Tunnel whose owner to notify
2125 * @param id Short id of the peer that has connected
2128 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2130 struct GNUNET_MESH_PeerControl pc;
2132 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2133 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2134 pc.tunnel_id = htonl (t->local_tid);
2135 GNUNET_PEER_resolve (id, &pc.peer);
2136 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2142 * Notify all clients (not depending on registration status) that the incoming
2143 * tunnel is no longer valid.
2145 * @param t Tunnel that was destroyed.
2148 send_clients_tunnel_destroy (struct MeshTunnel *t)
2150 struct GNUNET_MESH_TunnelMessage msg;
2152 msg.header.size = htons (sizeof (msg));
2153 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2154 msg.tunnel_id = htonl (t->local_tid_dest);
2155 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2160 * Notify clients of tunnel disconnections, if needed.
2161 * In case the origin disconnects, the destination clients get a tunnel destroy
2162 * notification. If the last destination disconnects (only one remaining client
2163 * in tunnel), the origin gets a (local ID) peer disconnected.
2164 * Note that the function must be called BEFORE removing the client from
2167 * @param t Tunnel that was destroyed.
2168 * @param c Client that disconnected.
2171 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2177 struct GNUNET_MESH_TunnelMessage msg;
2179 msg.header.size = htons (sizeof (msg));
2180 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2181 msg.tunnel_id = htonl (t->local_tid_dest);
2182 for (i = 0; i < t->nclients; i++)
2183 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2184 &msg.header, GNUNET_NO);
2186 // FIXME when to disconnect an incoming tunnel?
2187 else if (1 == t->nclients && NULL != t->owner)
2189 struct GNUNET_MESH_PeerControl msg;
2191 msg.header.size = htons (sizeof (msg));
2192 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2193 msg.tunnel_id = htonl (t->local_tid);
2194 msg.peer = my_full_id;
2195 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2196 &msg.header, GNUNET_NO);
2202 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2203 * and insert it in the appropiate structures if the peer is not known yet.
2205 * @param peer Full identity of the peer.
2207 * @return Existing or newly created peer info.
2209 static struct MeshPeerInfo *
2210 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2212 struct MeshPeerInfo *peer_info;
2214 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2215 if (NULL == peer_info)
2218 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2219 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2220 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2221 peer_info->id = GNUNET_PEER_intern (peer);
2229 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2230 * and insert it in the appropiate structures if the peer is not known yet.
2232 * @param peer Short identity of the peer.
2234 * @return Existing or newly created peer info.
2236 static struct MeshPeerInfo *
2237 peer_info_get_short (const GNUNET_PEER_Id peer)
2239 struct GNUNET_PeerIdentity id;
2241 GNUNET_PEER_resolve (peer, &id);
2242 return peer_info_get (&id);
2247 * Iterator to remove the tunnel from the list of tunnels a peer participates
2250 * @param cls Closure (tunnel info)
2251 * @param key GNUNET_PeerIdentity of the peer (unused)
2252 * @param value PeerInfo of the peer
2254 * @return always GNUNET_YES, to keep iterating
2257 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2259 struct MeshTunnel *t = cls;
2260 struct MeshPeerInfo *peer = value;
2263 for (i = 0; i < peer->ntunnels; i++)
2266 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2269 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2270 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2279 * Core callback to write a pre-constructed data packet to core buffer
2281 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2282 * @param size Number of bytes available in buf.
2283 * @param buf Where the to write the message.
2285 * @return number of bytes written to buf
2288 send_core_data_raw (void *cls, size_t size, void *buf)
2290 struct MeshTransmissionDescriptor *info = cls;
2291 struct GNUNET_MessageHeader *msg;
2294 GNUNET_assert (NULL != info);
2295 GNUNET_assert (NULL != info->mesh_data);
2296 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2297 total_size = ntohs (msg->size);
2299 if (total_size > size)
2304 memcpy (buf, msg, total_size);
2305 data_descriptor_decrement_rc (info->mesh_data);
2312 * Sends an already built non-multicast message to a peer,
2313 * properly registrating all used resources.
2315 * @param message Message to send. Function makes a copy of it.
2316 * @param peer Short ID of the neighbor whom to send the message.
2317 * @param t Tunnel on which this message is transmitted.
2320 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2321 const struct GNUNET_PeerIdentity *peer,
2322 struct MeshTunnel *t)
2324 struct MeshTransmissionDescriptor *info;
2325 struct MeshPeerInfo *neighbor;
2326 struct MeshPeerPath *p;
2330 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2332 size = ntohs (message->size);
2333 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2334 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2335 info->mesh_data->data = GNUNET_malloc (size);
2336 memcpy (info->mesh_data->data, message, size);
2337 type = ntohs(message->type);
2338 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2340 struct GNUNET_MESH_Unicast *m;
2342 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2343 m->ttl = htonl (ntohl (m->ttl) - 1);
2345 info->mesh_data->data_len = size;
2346 info->mesh_data->reference_counter = 1;
2347 info->mesh_data->total_out = 1;
2348 neighbor = peer_info_get (peer);
2349 for (p = neighbor->path_head; NULL != p; p = p->next)
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 " %s IS NOT DIRECTLY CONNECTED\n",
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 for (p = neighbor->path_head; NULL != p; p = p->next)
2367 struct GNUNET_PeerIdentity debug_id;
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 " path with %u hops through:\n",
2373 for (i = 0; i < p->length; i++)
2375 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 i, GNUNET_i2s(&debug_id));
2382 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2383 GNUNET_free (info->mesh_data->data);
2384 GNUNET_free (info->mesh_data);
2388 info->peer = neighbor;
2389 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2400 * Sends a CREATE PATH message for a path to a peer, properly registrating
2401 * all used resources.
2403 * @param peer PeerInfo of the final peer for whom this path is being created.
2404 * @param p Path itself.
2405 * @param t Tunnel for which the path is created.
2408 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2409 struct MeshTunnel *t)
2411 struct GNUNET_PeerIdentity id;
2412 struct MeshPathInfo *path_info;
2413 struct MeshPeerInfo *neighbor;
2419 p = tree_get_path_to_peer (t->tree, peer->id);
2426 for (i = 0; i < p->length; i++)
2428 if (p->peers[i] == myid)
2431 if (i >= p->length - 1)
2437 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2439 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2440 path_info->path = p;
2442 neighbor = peer_info_get (&id);
2443 path_info->peer = neighbor;
2444 queue_add (path_info,
2445 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2446 sizeof (struct GNUNET_MESH_ManipulatePath) +
2447 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2454 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2456 * @param t Tunnel whose path to destroy.
2457 * @param destination Short ID of the peer to whom the path to destroy.
2460 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2462 struct MeshPeerPath *p;
2465 p = tree_get_path_to_peer (t->tree, destination);
2471 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2472 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2474 struct GNUNET_MESH_ManipulatePath *msg;
2475 struct GNUNET_PeerIdentity *pi;
2479 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2480 msg->header.size = htons (size);
2481 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2482 msg->tid = htonl (t->id.tid);
2483 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2484 for (i = 0; i < p->length; i++)
2486 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2488 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2495 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2497 * @param t Tunnel which to confirm.
2500 send_path_ack (struct MeshTunnel *t)
2502 struct MeshTransmissionDescriptor *info;
2503 struct GNUNET_PeerIdentity id;
2504 GNUNET_PEER_Id peer;
2506 peer = tree_get_predecessor (t->tree);
2507 GNUNET_PEER_resolve (peer, &id);
2508 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2509 info->origin = &t->id;
2510 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2511 GNUNET_assert (NULL != info->peer);
2514 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2515 sizeof (struct GNUNET_MESH_PathACK),
2522 * Try to establish a new connection to this peer.
2523 * Use the best path for the given tunnel.
2524 * If the peer doesn't have any path to it yet, try to get one.
2525 * If the peer already has some path, send a CREATE PATH towards it.
2527 * @param peer PeerInfo of the peer.
2528 * @param t Tunnel for which to create the path, if possible.
2531 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2533 struct MeshPeerPath *p;
2534 struct MeshPathInfo *path_info;
2536 if (NULL != peer->path_head)
2538 p = tree_get_path_to_peer (t->tree, peer->id);
2545 // FIXME always send create path to self
2548 send_create_path (peer, p, t);
2552 struct GNUNET_HashCode hash;
2555 send_client_peer_connected (t, myid);
2556 t->local_tid_dest = next_local_tid++;
2557 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2560 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2561 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2568 else if (NULL == peer->dhtget)
2570 struct GNUNET_PeerIdentity id;
2572 GNUNET_PEER_resolve (peer->id, &id);
2573 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2574 path_info->peer = peer;
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2577 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2578 peer->dhtgetcls = path_info;
2579 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2580 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2581 &id.hashPubKey, /* key to search */
2582 dht_replication_level, /* replication level */
2583 GNUNET_DHT_RO_RECORD_ROUTE |
2584 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2585 NULL, /* xquery */ // FIXME BLOOMFILTER
2586 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2587 &dht_get_id_handler, path_info);
2589 /* Otherwise, there is no path but the DHT get is already started. */
2594 * Task to delay the connection of a peer
2596 * @param cls Closure (path info with tunnel and peer to connect).
2597 * Will be free'd on exection.
2598 * @param tc TaskContext
2601 peer_info_connect_task (void *cls,
2602 const struct GNUNET_SCHEDULER_TaskContext *tc)
2604 struct MeshPathInfo *path_info = cls;
2606 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2611 peer_info_connect (path_info->peer, path_info->t);
2617 * Destroy the peer_info and free any allocated resources linked to it
2619 * @param pi The peer_info to destroy.
2621 * @return GNUNET_OK on success
2624 peer_info_destroy (struct MeshPeerInfo *pi)
2626 struct GNUNET_PeerIdentity id;
2627 struct MeshPeerPath *p;
2628 struct MeshPeerPath *nextp;
2630 GNUNET_PEER_resolve (pi->id, &id);
2631 GNUNET_PEER_change_rc (pi->id, -1);
2634 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2638 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2640 if (NULL != pi->dhtget)
2642 GNUNET_DHT_get_stop (pi->dhtget);
2643 GNUNET_free (pi->dhtgetcls);
2649 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2659 * Remove all paths that rely on a direct connection between p1 and p2
2660 * from the peer itself and notify all tunnels about it.
2662 * @param peer PeerInfo of affected peer.
2663 * @param p1 GNUNET_PEER_Id of one peer.
2664 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2667 * TODO: optimize (see below)
2670 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2673 struct MeshPeerPath *p;
2674 struct MeshPeerPath *aux;
2675 struct MeshPeerInfo *peer_d;
2677 unsigned int destroyed;
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2684 p = peer->path_head;
2688 for (i = 0; i < (p->length - 1); i++)
2690 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2691 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2693 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2704 for (i = 0; i < peer->ntunnels; i++)
2706 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2710 * Problem: one or more peers have been deleted from the tunnel tree.
2711 * We don't know who they are to try to add them again.
2712 * We need to try to find a new path for each of the disconnected peers.
2713 * Some of them might already have a path to reach them that does not
2714 * involve p1 and p2. Adding all anew might render in a better tree than
2715 * the trivial immediate fix.
2717 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2718 * its children will be reachable trough him.
2720 peer_d = peer_info_get_short (d);
2723 for (p = peer_d->path_head; NULL != p; p = p->next)
2725 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2733 /* No callback, as peer will be already disconnected and a connection
2734 * scheduled by tunnel_notify_connection_broken.
2736 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2740 peer_info_connect (peer_d, peer->tunnels[i]);
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2748 * Add the path to the peer and update the path used to reach it in case this
2751 * @param peer_info Destination peer to add the path to.
2752 * @param path New path to add. Last peer must be the peer in arg 1.
2753 * Path will be either used of freed if already known.
2754 * @param trusted Do we trust that this path is real?
2757 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2760 struct MeshPeerPath *aux;
2764 if ((NULL == peer_info) || (NULL == path))
2767 path_destroy (path);
2770 if (path->peers[path->length - 1] != peer_info->id)
2773 path_destroy (path);
2776 if (path->length <= 2 && GNUNET_NO == trusted)
2778 /* Only allow CORE to tell us about direct paths */
2779 path_destroy (path);
2782 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2783 for (l = 1; l < path->length; l++)
2785 if (path->peers[l] == myid)
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2788 for (l2 = 0; l2 < path->length - l; l2++)
2790 path->peers[l2] = path->peers[l + l2];
2795 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2800 struct GNUNET_PeerIdentity id;
2802 GNUNET_PEER_resolve (peer_info->id, &id);
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2804 path->length, GNUNET_i2s (&id));
2807 l = path_get_length (path);
2814 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2815 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2817 l2 = path_get_length (aux);
2820 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2821 peer_info->path_tail, aux, path);
2826 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2828 path_destroy (path);
2833 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2840 * Add the path to the origin peer and update the path used to reach it in case
2841 * this is the shortest.
2842 * The path is given in peer_info -> destination, therefore we turn the path
2843 * upside down first.
2845 * @param peer_info Peer to add the path to, being the origin of the path.
2846 * @param path New path to add after being inversed.
2847 * @param trusted Do we trust that this path is real?
2850 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2851 struct MeshPeerPath *path, int trusted)
2854 peer_info_add_path (peer_info, path, trusted);
2859 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2860 * to obtain a local peer -> destination path and interning the peer ids.
2862 * @return Newly allocated and created path
2864 static struct MeshPeerPath *
2865 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2866 unsigned int get_path_length,
2867 const struct GNUNET_PeerIdentity *put_path,
2868 unsigned int put_path_length)
2870 struct MeshPeerPath *p;
2876 GNUNET_PEER_change_rc (myid, 1);
2877 i = get_path_length;
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2879 for (i--; i >= 0; i--)
2881 id = GNUNET_PEER_intern (&get_path[i]);
2882 if (p->length > 0 && id == p->peers[p->length - 1])
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2885 GNUNET_PEER_change_rc (id, -1);
2889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2890 GNUNET_i2s (&get_path[i]));
2892 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2893 p->peers[p->length - 1] = id;
2896 i = put_path_length;
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2898 for (i--; i >= 0; i--)
2900 id = GNUNET_PEER_intern (&put_path[i]);
2903 /* PUT path went through us, so discard the path up until now and start
2904 * from here to get a much shorter (and loop-free) path.
2909 if (p->length > 0 && id == p->peers[p->length - 1])
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2912 GNUNET_PEER_change_rc (id, -1);
2916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2917 GNUNET_i2s (&put_path[i]));
2919 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2920 p->peers[p->length - 1] = id;
2924 if (get_path_length > 0)
2925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2926 GNUNET_i2s (&get_path[0]));
2927 if (put_path_length > 0)
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2929 GNUNET_i2s (&put_path[0]));
2930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2932 for (i = 0; i < p->length; i++)
2934 struct GNUNET_PeerIdentity peer_id;
2936 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2938 GNUNET_i2s (&peer_id));
2946 * Adds a path to the peer_infos of all the peers in the path
2948 * @param p Path to process.
2949 * @param confirmed Whether we know if the path works or not.
2952 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2956 /* TODO: invert and add */
2957 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2958 for (i++; i < p->length; i++)
2960 struct MeshPeerInfo *aux;
2961 struct MeshPeerPath *copy;
2963 aux = peer_info_get_short (p->peers[i]);
2964 copy = path_duplicate (p);
2965 copy->length = i + 1;
2966 peer_info_add_path (aux, copy, GNUNET_NO);
2972 * Send keepalive packets for a peer
2974 * @param cls Closure (tunnel for which to send the keepalive).
2975 * @param tc Notification context.
2977 * TODO: implement explicit multicast keepalive?
2980 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2984 * Search for a tunnel among the incoming tunnels
2986 * @param tid the local id of the tunnel
2988 * @return tunnel handler, NULL if doesn't exist
2990 static struct MeshTunnel *
2991 tunnel_get_incoming (MESH_TunnelNumber tid)
2993 struct GNUNET_HashCode hash;
2995 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2996 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2997 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3002 * Search for a tunnel among the tunnels for a client
3004 * @param c the client whose tunnels to search in
3005 * @param tid the local id of the tunnel
3007 * @return tunnel handler, NULL if doesn't exist
3009 static struct MeshTunnel *
3010 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3012 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3014 return tunnel_get_incoming (tid);
3018 struct GNUNET_HashCode hash;
3020 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3021 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3027 * Search for a tunnel by global ID using PEER_ID
3029 * @param pi owner of the tunnel
3030 * @param tid global tunnel number
3032 * @return tunnel handler, NULL if doesn't exist
3034 static struct MeshTunnel *
3035 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3037 struct MESH_TunnelID id;
3038 struct GNUNET_HashCode hash;
3043 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3044 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3049 * Search for a tunnel by global ID using full PeerIdentities
3051 * @param oid owner of the tunnel
3052 * @param tid global tunnel number
3054 * @return tunnel handler, NULL if doesn't exist
3056 static struct MeshTunnel *
3057 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3059 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3064 * Delete an active client from the tunnel.
3070 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3074 for (i = 0; i < t->nclients; i++)
3076 if (t->clients[i] == c)
3078 t->clients[i] = t->clients[t->nclients - 1];
3079 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3080 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3082 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3090 * Delete an ignored client from the tunnel.
3096 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3100 for (i = 0; i < t->nignore; i++)
3102 if (t->ignore[i] == c)
3104 t->ignore[i] = t->ignore[t->nignore - 1];
3105 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3113 * Delete a client from the tunnel. It should be only done on
3114 * client disconnection, otherwise use client_ignore_tunnel.
3120 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3122 tunnel_delete_ignored_client (t, c);
3123 tunnel_delete_active_client (t, c);
3128 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3130 * Destroys queue elements of all waiting transmissions and frees all memory
3131 * used by the struct and its elements.
3133 * @param cls Closure (tunnel info).
3134 * @param key Hash of GNUNET_PEER_Id (unused).
3135 * @param value MeshTunnelChildInfo of the child.
3137 * @return always GNUNET_YES, to keep iterating
3140 tunnel_destroy_child (void *cls,
3141 const struct GNUNET_HashCode * key,
3144 struct MeshTunnelChildInfo *cinfo = value;
3145 struct MeshTunnel *t = cls;
3149 for (c = 0; c < cinfo->send_buffer_n; c++)
3151 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3152 if (NULL != cinfo->send_buffer[i])
3153 queue_destroy (cinfo->send_buffer[i], GNUNET_YES);
3156 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3158 GNUNET_free_non_null (cinfo->send_buffer);
3159 GNUNET_free (cinfo);
3165 * Callback used to notify a client owner of a tunnel that a peer has
3166 * disconnected, most likely because of a path change.
3168 * @param cls Closure (tunnel this notification is about).
3169 * @param peer_id Short ID of disconnected peer.
3172 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3174 struct MeshTunnel *t = cls;
3175 struct MeshPeerInfo *peer;
3176 struct MeshPathInfo *path_info;
3178 if (NULL != t->owner && NULL != nc)
3180 struct GNUNET_MESH_PeerControl msg;
3182 msg.header.size = htons (sizeof (msg));
3183 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3184 msg.tunnel_id = htonl (t->local_tid);
3185 GNUNET_PEER_resolve (peer_id, &msg.peer);
3186 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3187 &msg.header, GNUNET_NO);
3189 peer = peer_info_get_short (peer_id);
3190 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3191 path_info->peer = peer;
3193 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3198 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3199 * needed rescources.
3200 * If peer already exists, reevaluate shortest path and change if different.
3202 * @param t Tunnel we want to add a new peer to
3203 * @param peer PeerInfo of the peer being added
3207 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3209 struct GNUNET_PeerIdentity id;
3210 struct MeshPeerPath *best_p;
3211 struct MeshPeerPath *p;
3212 unsigned int best_cost;
3215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3216 GNUNET_PEER_resolve (peer->id, &id);
3218 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3221 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3222 GNUNET_assert (GNUNET_OK ==
3223 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3225 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3228 if (NULL != (p = peer->path_head))
3231 best_cost = tree_get_path_cost (t->tree, p);
3234 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3241 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3242 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3243 t->path_refresh_task =
3244 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3248 /* Start a DHT get */
3249 peer_info_connect (peer, t);
3251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3255 * Add a path to a tunnel which we don't own, just to remember the next hop.
3256 * If destination node was already in the tunnel, the first hop information
3257 * will be replaced with the new path.
3259 * @param t Tunnel we want to add a new peer to
3260 * @param p Path to add
3261 * @param own_pos Position of local node in path.
3265 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3266 unsigned int own_pos)
3268 struct GNUNET_PeerIdentity id;
3270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3271 GNUNET_assert (0 != own_pos);
3272 tree_add_path (t->tree, p, NULL, NULL);
3273 if (own_pos < p->length - 1)
3275 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3276 tree_update_first_hops (t->tree, myid, &id);
3278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3282 * Add a client to a tunnel, initializing all needed data structures.
3284 * @param t Tunnel to which add the client.
3285 * @param c Client which to add to the tunnel.
3288 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3290 struct MeshTunnelClientInfo clinfo;
3292 GNUNET_array_append (t->clients, t->nclients, c);
3293 clinfo.fwd_ack = t->fwd_pid + 1;
3294 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3295 clinfo.fwd_pid = t->fwd_pid;
3296 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3298 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3303 * Notifies a tunnel that a connection has broken that affects at least
3304 * some of its peers. Sends a notification towards the root of the tree.
3305 * In case the peer is the owner of the tree, notifies the client that owns
3306 * the tunnel and tries to reconnect.
3308 * @param t Tunnel affected.
3309 * @param p1 Peer that got disconnected from p2.
3310 * @param p2 Peer that got disconnected from p1.
3312 * @return Short ID of the peer disconnected (either p1 or p2).
3313 * 0 if the tunnel remained unaffected.
3315 static GNUNET_PEER_Id
3316 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3322 tree_notify_connection_broken (t->tree, p1, p2,
3323 &tunnel_notify_client_peer_disconnected,
3325 if (myid != p1 && myid != p2)
3331 if (tree_get_predecessor (t->tree) != 0)
3333 /* We are the peer still connected, notify owner of the disconnection. */
3334 struct GNUNET_MESH_PathBroken msg;
3335 struct GNUNET_PeerIdentity neighbor;
3337 msg.header.size = htons (sizeof (msg));
3338 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3339 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3340 msg.tid = htonl (t->id.tid);
3341 msg.peer1 = my_full_id;
3342 GNUNET_PEER_resolve (pid, &msg.peer2);
3343 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3344 send_prebuilt_message (&msg.header, &neighbor, t);
3352 * Send a multicast packet to a neighbor.
3354 * @param cls Closure (Info about the multicast packet)
3355 * @param neighbor_id Short ID of the neighbor to send the packet to.
3358 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3360 struct MeshData *mdata = cls;
3361 struct MeshTransmissionDescriptor *info;
3362 struct GNUNET_PeerIdentity neighbor;
3363 struct GNUNET_MessageHeader *msg;
3365 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3367 info->mesh_data = mdata;
3368 (mdata->reference_counter) ++;
3369 info->destination = neighbor_id;
3370 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3372 GNUNET_i2s (&neighbor));
3373 info->peer = peer_info_get (&neighbor);
3374 GNUNET_assert (NULL != info->peer);
3375 msg = (struct GNUNET_MessageHeader *) mdata->data;
3378 info->mesh_data->data_len,
3385 * Queue a message in a tunnel in multicast, sending a copy to each child node
3386 * down the local one in the tunnel tree.
3388 * @param t Tunnel in which to send the data.
3389 * @param msg Message to be sent.
3392 tunnel_send_multicast (struct MeshTunnel *t,
3393 const struct GNUNET_MessageHeader *msg)
3395 struct MeshData *mdata;
3397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3398 " sending a multicast packet...\n");
3400 mdata = GNUNET_malloc (sizeof (struct MeshData));
3401 mdata->data_len = ntohs (msg->size);
3403 mdata->data = GNUNET_malloc (mdata->data_len);
3404 memcpy (mdata->data, msg, mdata->data_len);
3405 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3407 struct GNUNET_MESH_Multicast *mcast;
3409 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3410 if (t->fwd_queue_n >= t->fwd_queue_max)
3413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3415 " message from %s!\n",
3416 GNUNET_i2s(&mcast->oid));
3417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3418 " message at %s!\n",
3419 GNUNET_i2s(&my_full_id));
3420 GNUNET_free (mdata->data);
3421 GNUNET_free (mdata);
3425 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3427 ntohl (mcast->ttl));
3431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3434 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3435 if (mdata->reference_counter == 0)
3437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3438 " no one to send data to\n");
3439 GNUNET_free (mdata->data);
3440 GNUNET_free (mdata);
3445 mdata->total_out = mdata->reference_counter;
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3448 " sending a multicast packet done\n");
3454 * Increase the SKIP value of all peers that
3455 * have not received a unicast message.
3457 * @param cls Closure (ID of the peer that HAS received the message).
3458 * @param key ID of the neighbor.
3459 * @param value Information about the neighbor.
3461 * @return GNUNET_YES to keep iterating.
3464 tunnel_add_skip (void *cls,
3465 const struct GNUNET_HashCode * key,
3468 struct GNUNET_PeerIdentity *neighbor = cls;
3469 struct MeshTunnelChildInfo *cinfo = value;
3471 /* TODO compare only pointers? key == neighbor? */
3472 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3482 * @brief Get neighbor's Flow Control information.
3484 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3485 * descendant of the local node in a certain tunnel.
3486 * If the info is not yet there (recently created path), creates the data struct
3487 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3490 * @param t Tunnel related.
3491 * @param peer Neighbor whose Flow Control info is needed.
3493 * @return Neighbor's Flow Control info.
3495 static struct MeshTunnelChildInfo *
3496 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3497 const struct GNUNET_PeerIdentity *peer)
3499 struct MeshTunnelChildInfo *cinfo;
3500 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3506 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3507 cinfo->id = GNUNET_PEER_intern (peer);
3508 cinfo->skip = t->fwd_pid;
3510 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3511 cinfo->fwd_ack = t->fwd_pid + delta;
3512 cinfo->bck_ack = delta;
3514 cinfo->send_buffer =
3515 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3517 GNUNET_assert (GNUNET_OK ==
3518 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3521 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3528 * Get the Flow Control info of a client.
3530 * @param t Tunnel on which to look.
3531 * @param c Client whose ACK to get.
3533 * @return ACK value.
3535 static struct MeshTunnelClientInfo *
3536 tunnel_get_client_fc (struct MeshTunnel *t,
3537 struct MeshClient *c)
3541 for (i = 0; i < t->nclients; i++)
3543 if (t->clients[i] != c)
3545 return &t->clients_fc[i];
3548 return NULL; // avoid compiler / coverity complaints
3553 * Iterator to get the appropiate ACK value from all children nodes.
3555 * @param cls Closue (tunnel).
3556 * @param id Id of the child node.
3559 tunnel_get_child_fwd_ack (void *cls,
3562 struct GNUNET_PeerIdentity peer_id;
3563 struct MeshTunnelChildInfo *cinfo;
3564 struct MeshTunnelChildIteratorContext *ctx = cls;
3565 struct MeshTunnel *t = ctx->t;
3568 GNUNET_PEER_resolve (id, &peer_id);
3569 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3570 ack = cinfo->fwd_ack;
3573 if (GNUNET_NO == ctx->init)
3575 ctx->max_child_ack = ack;
3576 ctx->init = GNUNET_YES;
3579 if (GNUNET_YES == t->speed_min)
3581 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3585 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3592 * Get the maximum PID allowed to transmit to any
3593 * tunnel child of the local peer, depending on the tunnel
3594 * buffering/speed settings.
3598 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3601 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3603 struct MeshTunnelChildIteratorContext ctx;
3605 ctx.max_child_ack = 0;
3607 ctx.init = GNUNET_NO;
3608 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3610 if (0 == ctx.nchildren)
3612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3613 " tunnel has no children, no FWD ACK\n");
3617 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3618 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3620 return (int64_t) ctx.max_child_ack;
3625 * Set the FWD ACK value of a client in a particular tunnel.
3627 * @param t Tunnel affected.
3628 * @param c Client whose ACK to set.
3629 * @param ack ACK value.
3632 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3633 struct MeshClient *c,
3638 for (i = 0; i < t->nclients; i++)
3640 if (t->clients[i] != c)
3642 t->clients_fc[i].fwd_ack = ack;
3650 * Get the highest ACK value of all clients in a particular tunnel,
3651 * according to the buffering/speed settings.
3653 * @param t Tunnel on which to look.
3655 * @return Corresponding ACK value (max uint32_t).
3656 * If no clients are suscribed, -1LL.
3659 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3664 if (0 == t->nclients)
3666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3667 " tunnel has no clients, no FWD ACK\n");
3671 for (ack = -1LL, i = 0; i < t->nclients; i++)
3674 (GNUNET_YES == t->speed_min &&
3675 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3676 (GNUNET_NO == t->speed_min &&
3677 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3679 ack = t->clients_fc[i].fwd_ack;
3683 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3684 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3686 return (uint32_t) ack;
3691 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3692 * mode and the status of all children nodes.
3696 * @return Maximum PID allowed.
3699 tunnel_get_fwd_ack (struct MeshTunnel *t)
3703 uint32_t buffer_free;
3707 count = t->fwd_pid - t->skip;
3708 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3709 child_ack = tunnel_get_children_fwd_ack (t);
3710 client_ack = tunnel_get_clients_fwd_ack (t);
3711 if (GNUNET_YES == t->nobuffer)
3714 if (-1LL == child_ack)
3715 child_ack = client_ack;
3716 if (-1LL == child_ack)
3719 client_ack = child_ack = ack;
3724 ack = count + buffer_free; // Overflow? OK!
3726 if (-1LL == child_ack)
3728 // Node has no children, child_ack AND core buffer are irrelevant.
3729 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3730 return (uint32_t) client_ack;
3732 if (-1LL == client_ack)
3736 if (GNUNET_YES == t->speed_min)
3738 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3739 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3743 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3744 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3747 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3748 count, buffer_free, child_ack, client_ack, ack);
3754 * Build a local ACK message and send it to a local client.
3756 * @param t Tunnel on which to send the ACK.
3757 * @param c Client to whom send the ACK.
3758 * @param ack Value of the ACK.
3761 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3763 struct GNUNET_MESH_LocalAck msg;
3765 msg.header.size = htons (sizeof (msg));
3766 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3767 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3768 msg.max_pid = htonl (ack);
3769 GNUNET_SERVER_notification_context_unicast(nc,
3776 * Build an ACK message and queue it to send to the given peer.
3778 * @param t Tunnel on which to send the ACK.
3779 * @param peer Peer to whom send the ACK.
3780 * @param ack Value of the ACK.
3783 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3785 struct GNUNET_MESH_ACK msg;
3787 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3788 msg.header.size = htons (sizeof (msg));
3789 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3790 msg.pid = htonl (ack);
3791 msg.tid = htonl (t->id.tid);
3793 send_prebuilt_message (&msg.header, peer, t);
3798 * Notify a the owner of a tunnel about how many more
3799 * payload packages will we accept on a given tunnel.
3801 * @param t Tunnel on which to send the ACK.
3804 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3809 "Sending client FWD ACK on tunnel %X\n",
3812 ack = tunnel_get_fwd_ack (t);
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3815 if (t->last_fwd_ack == ack)
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3822 t->last_fwd_ack = ack;
3823 send_local_ack (t, t->owner, ack);
3828 * Send an ACK informing the predecessor about the available buffer space.
3829 * In case there is no predecessor, inform the owning client.
3830 * If buffering is off, send only on behalf of children or self if endpoint.
3831 * If buffering is on, send when sent to children and buffer space is free.
3832 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3833 * the ACK itself goes "back" (towards root).
3835 * @param t Tunnel on which to send the ACK.
3836 * @param type Type of message that triggered the ACK transmission.
3839 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3841 struct GNUNET_PeerIdentity id;
3844 if (NULL != t->owner)
3846 tunnel_send_client_fwd_ack (t);
3849 /* Is it after unicast / multicast retransmission? */
3852 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3853 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3855 "ACK due to FWD DATA retransmission\n");
3856 if (GNUNET_YES == t->nobuffer)
3858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3862 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3863 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3869 /* Check if we need no retransmit the ACK */
3870 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3871 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3875 " t->qmax: %u, t->qn: %u\n",
3876 t->fwd_queue_max, t->fwd_queue_n);
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3878 " t->pid: %u, t->ack: %u\n",
3879 t->fwd_pid, t->last_fwd_ack);
3883 /* Ok, ACK might be necessary, what PID to ACK? */
3884 ack = tunnel_get_fwd_ack (t);
3886 /* If speed_min and not all children have ack'd, dont send yet */
3887 if (ack == t->last_fwd_ack)
3889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3893 t->last_fwd_ack = ack;
3894 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3895 send_ack (t, &id, ack);
3901 * Iterator to send a child node a BCK ACK to allow him to send more
3904 * @param cls Closure (tunnel).
3905 * @param id Id of the child node.
3908 tunnel_send_child_bck_ack (void *cls,
3911 struct MeshTunnel *t = cls;
3912 struct MeshTunnelChildInfo *cinfo;
3913 struct GNUNET_PeerIdentity peer;
3915 GNUNET_PEER_resolve (id, &peer);
3916 cinfo = tunnel_get_neighbor_fc (t, &peer);
3918 if (cinfo->bck_ack != cinfo->pid &&
3919 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3923 send_ack (t, &peer, cinfo->bck_ack);
3928 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3930 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3932 * @param t Tunnel on which to send the BCK ACKs.
3935 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3938 unsigned int tunnel_delta;
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3942 tunnel_delta = t->bck_ack - t->bck_pid;
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3945 /* Find client whom to allow to send to origin (with lowest buffer space) */
3946 for (i = 0; i < t->nclients; i++)
3948 struct MeshTunnelClientInfo *clinfo;
3951 clinfo = &t->clients_fc[i];
3952 delta = clinfo->bck_ack - clinfo->bck_pid;
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3954 t->clients[i]->id, delta);
3956 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3957 (GNUNET_YES == t->nobuffer && 0 == delta))
3961 ack = clinfo->bck_pid;
3962 ack += t->nobuffer ? 1 : tunnel_delta;
3963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3964 " sending ack to client %u: %u\n",
3965 t->clients[i]->id, ack);
3966 send_local_ack (t, t->clients[i], ack);
3967 clinfo->bck_ack = ack;
3974 * Send an ACK informing the children nodes and destination clients about
3975 * the available buffer space.
3976 * If buffering is off, send only on behalf of root (can be self).
3977 * If buffering is on, send when sent to predecessor and buffer space is free.
3978 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3979 * the ACK itself goes "forward" (towards children/clients).
3981 * @param t Tunnel on which to send the ACK.
3982 * @param type Type of message that triggered the ACK transmission.
3985 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3988 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3989 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3990 /* Is it after data to_origin retransmission? */
3993 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3994 if (GNUNET_YES == t->nobuffer)
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3997 " Not sending ACK, nobuffer\n");
4001 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4002 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4008 tunnel_send_clients_bck_ack (t);
4009 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4014 * @brief Re-initiate traffic to this peer if necessary.
4016 * Check if there is traffic queued towards this peer
4017 * and the core transmit handle is NULL (traffic was stalled).
4018 * If so, call core tmt rdy.
4020 * @param cls Closure (unused)
4021 * @param peer_id Short ID of peer to which initiate traffic.
4024 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4026 struct MeshPeerInfo *peer;
4027 struct GNUNET_PeerIdentity id;
4028 struct MeshPeerQueue *q;
4031 peer = peer_info_get_short(peer_id);
4032 if (NULL != peer->core_transmit)
4035 q = queue_get_next(peer);
4038 /* Might br multicast traffic already sent to this particular peer but
4039 * not to other children in this tunnel.
4040 * This way t->queue_n would be > 0 but the queue of this particular peer
4046 GNUNET_PEER_resolve (peer->id, &id);
4047 peer->core_transmit =
4048 GNUNET_CORE_notify_transmit_ready(core_handle,
4051 GNUNET_TIME_UNIT_FOREVER_REL,
4061 * @brief Allow transmission of FWD traffic on this tunnel
4063 * Check if there is traffic queued towards any children
4064 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4066 * @param t Tunnel on which to unlock FWD traffic.
4069 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4071 if (0 == t->fwd_queue_n)
4074 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4079 * @brief Allow transmission of BCK traffic on this tunnel
4081 * Check if there is traffic queued towards the root of the tree
4082 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4084 * @param t Tunnel on which to unlock BCK traffic.
4087 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4089 if (0 == t->bck_queue_n)
4092 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4097 * Send a message to all peers in this tunnel that the tunnel is no longer
4100 * @param t The tunnel whose peers to notify.
4103 tunnel_send_destroy (struct MeshTunnel *t)
4105 struct GNUNET_MESH_TunnelDestroy msg;
4107 msg.header.size = htons (sizeof (msg));
4108 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4109 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4110 msg.tid = htonl (t->id.tid);
4111 tunnel_send_multicast (t, &msg.header);
4116 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4118 * @param cls Closure (Tunnel which to cancel).
4119 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4122 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4124 struct MeshTunnel *t = cls;
4125 struct MeshPeerInfo *peer_info;
4126 struct MeshPeerQueue *pq;
4127 struct MeshPeerQueue *next;
4129 peer_info = peer_info_get_short (neighbor_id);
4130 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4133 if (pq->tunnel == t)
4135 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4136 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4137 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4139 // Should have been removed on destroy children
4142 queue_destroy (pq, GNUNET_YES);
4145 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4147 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4148 peer_info->core_transmit = NULL;
4153 * Destroy the tunnel and free any allocated resources linked to it.
4155 * @param t the tunnel to destroy
4157 * @return GNUNET_OK on success
4160 tunnel_destroy (struct MeshTunnel *t)
4162 struct MeshClient *c;
4163 struct GNUNET_HashCode hash;
4174 struct GNUNET_PeerIdentity id;
4176 GNUNET_PEER_resolve (t->id.oid, &id);
4177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4178 GNUNET_i2s (&id), t->id.tid);
4180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4184 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4185 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4193 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4195 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4202 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4203 for (i = 0; i < t->nclients; i++)
4207 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4213 for (i = 0; i < t->nignore; i++)
4217 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4224 if (t->nclients > 0)
4227 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4232 GNUNET_free (t->clients);
4235 if (NULL != t->peers)
4237 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4239 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4242 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4243 &tunnel_destroy_child,
4245 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4247 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4249 tree_destroy (t->tree);
4251 if (NULL != t->regex_ctx)
4252 regex_cancel_search (t->regex_ctx);
4253 if (NULL != t->dht_get_type)
4254 GNUNET_DHT_get_stop (t->dht_get_type);
4255 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4256 GNUNET_SCHEDULER_cancel (t->timeout_task);
4257 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4258 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4261 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4262 GNUNET_assert (0 <= n_tunnels);
4269 * Create a new tunnel
4271 * @param owner Who is the owner of the tunnel (short ID).
4272 * @param tid Tunnel Number of the tunnel.
4273 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4274 * @param local Tunnel Number for the tunnel, for the client point of view.
4276 * @return A new initialized tunnel. NULL on error.
4278 static struct MeshTunnel *
4279 tunnel_new (GNUNET_PEER_Id owner,
4280 MESH_TunnelNumber tid,
4281 struct MeshClient *client,
4282 MESH_TunnelNumber local)
4284 struct MeshTunnel *t;
4285 struct GNUNET_HashCode hash;
4287 if (n_tunnels >= max_tunnels && NULL == client)
4290 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4293 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4294 t->bck_queue_max = t->fwd_queue_max;
4295 t->tree = tree_new (owner);
4297 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4298 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4299 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4300 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4301 t->local_tid = local;
4302 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4304 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4306 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4308 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4309 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4316 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4323 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4325 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4330 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4340 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4341 * that are no longer needed, as well as nodes of no longer reachable peers.
4342 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4344 * @param t Tunnel from which to remove the path.
4345 * @param peer Short id of the peer which should be removed.
4348 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4350 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4356 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4357 * client when the client disconnects. If the client is not the owner, the
4358 * owner will get notified if no more clients are in the tunnel and the client
4359 * get removed from the tunnel's list.
4361 * @param cls closure (client that is disconnecting)
4362 * @param key the hash of the local tunnel id (used to access the hashmap)
4363 * @param value the value stored at the key (tunnel to destroy)
4365 * @return GNUNET_OK, keep iterating.
4368 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4370 struct MeshTunnel *t = value;
4371 struct MeshClient *c = cls;
4373 send_client_tunnel_disconnect(t, c);
4376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4377 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4378 tunnel_delete_client(t, c);
4379 client_delete_tunnel(c, t);
4382 tunnel_send_destroy(t);
4384 t->destroy = GNUNET_YES;
4391 * Timeout function, destroys tunnel if called
4393 * @param cls Closure (tunnel to destroy).
4394 * @param tc TaskContext
4397 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4399 struct MeshTunnel *t = cls;
4400 struct GNUNET_PeerIdentity id;
4402 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4403 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4405 GNUNET_PEER_resolve(t->id.oid, &id);
4406 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4407 "Tunnel %s [%X] timed out. Destroying.\n",
4408 GNUNET_i2s(&id), t->id.tid);
4413 * Resets the tunnel timeout. Starts it if no timeout was running.
4415 * @param t Tunnel whose timeout to reset.
4417 * TODO use heap to improve efficiency of scheduler.
4420 tunnel_reset_timeout (struct MeshTunnel *t)
4422 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4423 GNUNET_SCHEDULER_cancel (t->timeout_task);
4425 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4426 (refresh_path_time, 4), &tunnel_timeout, t);
4430 /******************************************************************************/
4431 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4432 /******************************************************************************/
4435 * Function to send a create path packet to a peer.
4437 * @param cls closure
4438 * @param size number of bytes available in buf
4439 * @param buf where the callee should write the message
4440 * @return number of bytes written to buf
4443 send_core_path_create (void *cls, size_t size, void *buf)
4445 struct MeshPathInfo *info = cls;
4446 struct GNUNET_MESH_ManipulatePath *msg;
4447 struct GNUNET_PeerIdentity *peer_ptr;
4448 struct MeshTunnel *t = info->t;
4449 struct MeshPeerPath *p = info->path;
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4456 sizeof (struct GNUNET_MESH_ManipulatePath) +
4457 p->length * sizeof (struct GNUNET_PeerIdentity);
4459 if (size < size_needed || NULL == buf)
4464 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4465 msg->header.size = htons (size_needed);
4466 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4467 msg->tid = ntohl (t->id.tid);
4470 if (GNUNET_YES == t->speed_min)
4471 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4472 if (GNUNET_YES == t->nobuffer)
4473 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4474 msg->opt = htonl(opt);
4477 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4478 for (i = 0; i < p->length; i++)
4480 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4487 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4493 * Fill the core buffer
4495 * @param cls closure (data itself)
4496 * @param size number of bytes available in buf
4497 * @param buf where the callee should write the message
4499 * @return number of bytes written to buf
4502 send_core_data_multicast (void *cls, size_t size, void *buf)
4504 struct MeshTransmissionDescriptor *info = cls;
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4508 GNUNET_assert (NULL != info);
4509 GNUNET_assert (NULL != info->peer);
4510 total_size = info->mesh_data->data_len;
4511 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4513 if (total_size > size)
4518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4519 memcpy (buf, info->mesh_data->data, total_size);
4522 struct GNUNET_MESH_Multicast *mc;
4523 struct GNUNET_MessageHeader *mh;
4526 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4528 mc = (struct GNUNET_MESH_Multicast *) mh;
4529 mh = (struct GNUNET_MessageHeader *) &mc[1];
4530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4531 " multicast, payload type %s\n",
4532 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4534 " multicast, payload size %u\n", ntohs (mh->size));
4538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4539 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4543 data_descriptor_decrement_rc (info->mesh_data);
4544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4552 * Creates a path ack message in buf and frees all unused resources.
4554 * @param cls closure (MeshTransmissionDescriptor)
4555 * @param size number of bytes available in buf
4556 * @param buf where the callee should write the message
4557 * @return number of bytes written to buf
4560 send_core_path_ack (void *cls, size_t size, void *buf)
4562 struct MeshTransmissionDescriptor *info = cls;
4563 struct GNUNET_MESH_PathACK *msg = buf;
4565 GNUNET_assert (NULL != info);
4566 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4571 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4572 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4573 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4574 msg->tid = htonl (info->origin->tid);
4575 msg->peer_id = my_full_id;
4578 /* TODO add signature */
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4581 return sizeof (struct GNUNET_MESH_PathACK);
4586 * Free a transmission that was already queued with all resources
4587 * associated to the request.
4589 * @param queue Queue handler to cancel.
4590 * @param clear_cls Is it necessary to free associated cls?
4593 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4595 struct MeshTransmissionDescriptor *dd;
4596 struct MeshPathInfo *path_info;
4597 struct MeshTunnelChildInfo *cinfo;
4598 struct GNUNET_PeerIdentity id;
4602 if (GNUNET_YES == clear_cls)
4604 switch (queue->type)
4606 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4607 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4609 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4610 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4611 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4613 " type prebuilt (payload, tunnel destroy)\n");
4615 data_descriptor_decrement_rc (dd->mesh_data);
4617 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4619 path_info = queue->cls;
4620 path_destroy (path_info->path);
4624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4625 " type %s unknown!\n",
4626 GNUNET_MESH_DEBUG_M2S(queue->type));
4628 GNUNET_free_non_null (queue->cls);
4630 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4631 queue->peer->queue_tail,
4634 /* Delete from child_fc in the appropiate tunnel */
4635 max = queue->tunnel->fwd_queue_max;
4636 GNUNET_PEER_resolve (queue->peer->id, &id);
4637 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4638 for (i = 0; i < cinfo->send_buffer_n; i++)
4641 i2 = (cinfo->send_buffer_start + i) % max;
4642 if (cinfo->send_buffer[i2] == queue)
4644 /* Found corresponding entry in the send_buffer. Move all others back. */
4649 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4651 j2 = (cinfo->send_buffer_start + j) % max;
4652 j3 = (cinfo->send_buffer_start + j + 1) % max;
4653 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4656 cinfo->send_buffer[j3] = NULL;
4658 cinfo->send_buffer_n--;
4663 GNUNET_free (queue);
4668 * @brief Get the next transmittable message from the queue.
4670 * This will be the head, except in the case of being a data packet
4671 * not allowed by the destination peer.
4673 * @param peer Destination peer.
4675 * @return The next viable MeshPeerQueue element to send to that peer.
4676 * NULL when there are no transmittable messages.
4678 struct MeshPeerQueue *
4679 queue_get_next (const struct MeshPeerInfo *peer)
4681 struct MeshPeerQueue *q;
4682 struct MeshTunnel *t;
4683 struct MeshTransmissionDescriptor *info;
4684 struct MeshTunnelChildInfo *cinfo;
4685 struct GNUNET_MESH_Unicast *ucast;
4686 struct GNUNET_MESH_ToOrigin *to_orig;
4687 struct GNUNET_MESH_Multicast *mcast;
4688 struct GNUNET_PeerIdentity id;
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4693 for (q = peer->queue_head; NULL != q; q = q->next)
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4699 GNUNET_MESH_DEBUG_M2S(q->type));
4702 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4703 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4704 pid = ntohl (ucast->pid);
4705 GNUNET_PEER_resolve (info->peer->id, &id);
4706 cinfo = tunnel_get_neighbor_fc(t, &id);
4707 ack = cinfo->fwd_ack;
4709 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4710 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4711 pid = ntohl (to_orig->pid);
4714 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4715 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4716 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4718 // Not a multicast payload: multicast control traffic (destroy, etc)
4721 pid = ntohl (mcast->pid);
4722 GNUNET_PEER_resolve (info->peer->id, &id);
4723 cinfo = tunnel_get_neighbor_fc(t, &id);
4724 ack = cinfo->fwd_ack;
4727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4732 "********* ACK: %u, PID: %u\n",
4734 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4743 "********* NEXT!\n");
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4747 "********* nothing found\n");
4753 * Core callback to write a queued packet to core buffer
4755 * @param cls Closure (peer info).
4756 * @param size Number of bytes available in buf.
4757 * @param buf Where the to write the message.
4759 * @return number of bytes written to buf
4762 queue_send (void *cls, size_t size, void *buf)
4764 struct MeshPeerInfo *peer = cls;
4765 struct GNUNET_MessageHeader *msg;
4766 struct MeshPeerQueue *queue;
4767 struct MeshTunnel *t;
4768 struct MeshTunnelChildInfo *cinfo;
4769 struct GNUNET_PeerIdentity dst_id;
4772 peer->core_transmit = NULL;
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4775 queue = queue_get_next (peer);
4777 /* Queue has no internal mesh traffic nor sendable payload */
4780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4781 if (NULL == peer->queue_head)
4782 GNUNET_break (0); // Should've been canceled
4785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4787 GNUNET_PEER_resolve (peer->id, &dst_id);
4788 /* Check if buffer size is enough for the message */
4789 if (queue->size > size)
4791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4792 "********* not enough room, reissue\n");
4793 peer->core_transmit =
4794 GNUNET_CORE_notify_transmit_ready (core_handle,
4797 GNUNET_TIME_UNIT_FOREVER_REL,
4804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4807 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4810 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4811 "********* unicast: t->q (%u/%u)\n",
4812 t->fwd_queue_n, t->fwd_queue_max);
4814 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4821 switch (queue->type)
4824 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4825 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4826 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4827 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4829 "********* raw: %s\n",
4830 GNUNET_MESH_DEBUG_M2S (queue->type));
4832 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4833 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4834 data_size = send_core_data_raw (queue->cls, size, buf);
4835 msg = (struct GNUNET_MessageHeader *) buf;
4836 switch (ntohs (msg->type)) // Type of preconstructed message
4838 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4839 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4841 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4842 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4848 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4851 struct MeshTransmissionDescriptor *info = queue->cls;
4853 if ((1 == info->mesh_data->reference_counter
4854 && GNUNET_YES == t->speed_min)
4856 (info->mesh_data->total_out == info->mesh_data->reference_counter
4857 && GNUNET_NO == t->speed_min))
4859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4860 "********* considered sent\n");
4864 data_size = send_core_data_multicast(queue->cls, size, buf);
4865 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4867 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4869 data_size = send_core_path_create (queue->cls, size, buf);
4871 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4873 data_size = send_core_path_ack (queue->cls, size, buf);
4875 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4877 data_size = send_core_data_multicast (queue->cls, size, buf);
4881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4882 "********* type unknown: %u\n",
4886 switch (queue->type)
4888 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4889 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4890 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4891 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4892 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4896 "at pos %u (%p) != %p\n",
4897 cinfo->send_buffer_start,
4898 cinfo->send_buffer[cinfo->send_buffer_start],
4901 if (cinfo->send_buffer_n > 0)
4903 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4904 cinfo->send_buffer_n--;
4905 cinfo->send_buffer_start++;
4906 cinfo->send_buffer_start %= t->fwd_queue_max;
4917 /* Free queue, but cls was freed by send_core_* */
4918 queue_destroy (queue, GNUNET_NO);
4920 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4926 /* If more data in queue, send next */
4927 queue = queue_get_next(peer);
4930 struct GNUNET_PeerIdentity id;
4932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4933 GNUNET_PEER_resolve (peer->id, &id);
4934 peer->core_transmit =
4935 GNUNET_CORE_notify_transmit_ready(core_handle,
4938 GNUNET_TIME_UNIT_FOREVER_REL,
4946 if (NULL != peer->queue_head)
4947 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4948 "********* %s stalled\n",
4949 GNUNET_i2s(&my_full_id));
4951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4957 * @brief Queue and pass message to core when possible.
4959 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4960 * and accounts for it. In case the queue is full, the message is dropped and
4963 * Otherwise, message is treated as internal and allowed to go regardless of
4966 * @param cls Closure (@c type dependant). It will be used by queue_send to
4967 * build the message to be sent if not already prebuilt.
4968 * @param type Type of the message, 0 for a raw message.
4969 * @param size Size of the message.
4970 * @param dst Neighbor to send message to.
4971 * @param t Tunnel this message belongs to.
4974 queue_add (void *cls, uint16_t type, size_t size,
4975 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4977 struct MeshPeerQueue *queue;
4978 struct MeshTunnelChildInfo *cinfo;
4979 struct GNUNET_PeerIdentity id;
4985 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4986 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4988 n = &t->fwd_queue_n;
4989 max = &t->fwd_queue_max;
4991 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4993 n = &t->bck_queue_n;
4994 max = &t->bck_queue_max;
4999 if (NULL == t->owner)
5000 GNUNET_break_op(0); // TODO: kill connection?
5003 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5005 return; // Drop message
5009 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5015 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5016 GNUNET_PEER_resolve (dst->id, &id);
5017 if (NULL == dst->core_transmit)
5019 dst->core_transmit =
5020 GNUNET_CORE_notify_transmit_ready (core_handle,
5023 GNUNET_TIME_UNIT_FOREVER_REL,
5029 if (NULL == n) // Is this internal mesh traffic?
5032 // It's payload, keep track of buffer per peer.
5033 cinfo = tunnel_get_neighbor_fc(t, &id);
5034 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5035 if (NULL != cinfo->send_buffer[i])
5037 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5038 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5039 cinfo->send_buffer_start++;
5040 cinfo->send_buffer_start %= t->fwd_queue_max;
5044 cinfo->send_buffer_n++;
5046 cinfo->send_buffer[i] = queue;
5047 if (cinfo->send_buffer_n > t->fwd_queue_max)
5050 cinfo->send_buffer_n = t->fwd_queue_max;
5055 /******************************************************************************/
5056 /******************** MESH NETWORK HANDLERS **************************/
5057 /******************************************************************************/
5061 * Core handler for path creation
5063 * @param cls closure
5064 * @param message message
5065 * @param peer peer identity this notification is about
5066 * @param atsi performance data
5067 * @param atsi_count number of records in 'atsi'
5069 * @return GNUNET_OK to keep the connection open,
5070 * GNUNET_SYSERR to close it (signal serious error)
5073 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5074 const struct GNUNET_MessageHeader *message,
5075 const struct GNUNET_ATS_Information *atsi,
5076 unsigned int atsi_count)
5078 unsigned int own_pos;
5081 MESH_TunnelNumber tid;
5082 struct GNUNET_MESH_ManipulatePath *msg;
5083 struct GNUNET_PeerIdentity *pi;
5084 struct GNUNET_HashCode hash;
5085 struct MeshPeerPath *path;
5086 struct MeshPeerInfo *dest_peer_info;
5087 struct MeshPeerInfo *orig_peer_info;
5088 struct MeshTunnel *t;
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5091 "Received a path create msg [%s]\n",
5092 GNUNET_i2s (&my_full_id));
5093 size = ntohs (message->size);
5094 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5096 GNUNET_break_op (0);
5100 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5101 if (size % sizeof (struct GNUNET_PeerIdentity))
5103 GNUNET_break_op (0);
5106 size /= sizeof (struct GNUNET_PeerIdentity);
5109 GNUNET_break_op (0);
5112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5113 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5115 tid = ntohl (msg->tid);
5116 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5118 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5119 t = tunnel_get (pi, tid);
5120 if (NULL == t) // FIXME only for INCOMING tunnels?
5124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5125 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5128 // FIXME notify failure
5131 opt = ntohl (msg->opt);
5132 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5133 GNUNET_YES : GNUNET_NO;
5134 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5136 t->nobuffer = GNUNET_YES;
5137 t->last_fwd_ack = t->fwd_pid + 1;
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5140 " speed_min: %d, nobuffer:%d\n",
5141 t->speed_min, t->nobuffer);
5143 if (GNUNET_YES == t->nobuffer)
5145 t->bck_queue_max = 1;
5146 t->fwd_queue_max = 1;
5149 // FIXME only assign a local tid if a local client is interested (on demand)
5150 while (NULL != tunnel_get_incoming (next_local_tid))
5151 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5152 t->local_tid_dest = next_local_tid++;
5153 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5156 tunnel_reset_timeout (t);
5157 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5159 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5160 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5168 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5169 if (NULL == dest_peer_info)
5171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5172 " Creating PeerInfo for destination.\n");
5173 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5174 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5175 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5177 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5179 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5180 if (NULL == orig_peer_info)
5182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5183 " Creating PeerInfo for origin.\n");
5184 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5185 orig_peer_info->id = GNUNET_PEER_intern (pi);
5186 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5187 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5190 path = path_new (size);
5192 for (i = 0; i < size; i++)
5194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5195 GNUNET_i2s (&pi[i]));
5196 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5197 if (path->peers[i] == myid)
5200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5203 /* cannot be self, must be 'not found' */
5204 /* create path: self not found in path through self */
5205 GNUNET_break_op (0);
5206 path_destroy (path);
5210 path_add_to_peers (path, GNUNET_NO);
5211 tunnel_add_path (t, path, own_pos);
5212 if (own_pos == size - 1)
5214 /* It is for us! Send ack. */
5215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5216 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5217 if (NULL == t->peers)
5219 /* New tunnel! Notify clients on first payload message. */
5220 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5222 GNUNET_break (GNUNET_SYSERR !=
5223 GNUNET_CONTAINER_multihashmap_put (t->peers,
5224 &my_full_id.hashPubKey,
5227 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5232 struct MeshPeerPath *path2;
5234 /* It's for somebody else! Retransmit. */
5235 path2 = path_duplicate (path);
5236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5237 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5238 path2 = path_duplicate (path);
5239 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5240 send_create_path (dest_peer_info, path, t);
5247 * Core handler for path destruction
5249 * @param cls closure
5250 * @param message message
5251 * @param peer peer identity this notification is about
5252 * @param atsi performance data
5253 * @param atsi_count number of records in 'atsi'
5255 * @return GNUNET_OK to keep the connection open,
5256 * GNUNET_SYSERR to close it (signal serious error)
5259 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5260 const struct GNUNET_MessageHeader *message,
5261 const struct GNUNET_ATS_Information *atsi,
5262 unsigned int atsi_count)
5264 struct GNUNET_MESH_ManipulatePath *msg;
5265 struct GNUNET_PeerIdentity *pi;
5266 struct MeshPeerPath *path;
5267 struct MeshTunnel *t;
5268 unsigned int own_pos;
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5273 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5274 size = ntohs (message->size);
5275 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5277 GNUNET_break_op (0);
5281 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5282 if (size % sizeof (struct GNUNET_PeerIdentity))
5284 GNUNET_break_op (0);
5287 size /= sizeof (struct GNUNET_PeerIdentity);
5290 GNUNET_break_op (0);
5293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5295 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5296 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5298 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5300 t = tunnel_get (pi, ntohl (msg->tid));
5303 /* TODO notify back: we don't know this tunnel */
5304 GNUNET_break_op (0);
5307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5308 path = path_new (size);
5310 for (i = 0; i < size; i++)
5312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5313 GNUNET_i2s (&pi[i]));
5314 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5315 if (path->peers[i] == myid)
5318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5319 if (own_pos < path->length - 1)
5320 send_prebuilt_message (message, &pi[own_pos + 1], t);
5322 send_client_tunnel_disconnect(t, NULL);
5324 tunnel_delete_peer (t, path->peers[path->length - 1]);
5325 path_destroy (path);
5331 * Core handler for notifications of broken paths
5333 * @param cls closure
5334 * @param message message
5335 * @param peer peer identity this notification is about
5336 * @param atsi performance data
5337 * @param atsi_count number of records in 'atsi'
5339 * @return GNUNET_OK to keep the connection open,
5340 * GNUNET_SYSERR to close it (signal serious error)
5343 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5344 const struct GNUNET_MessageHeader *message,
5345 const struct GNUNET_ATS_Information *atsi,
5346 unsigned int atsi_count)
5348 struct GNUNET_MESH_PathBroken *msg;
5349 struct MeshTunnel *t;
5351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5352 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5353 msg = (struct GNUNET_MESH_PathBroken *) message;
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5355 GNUNET_i2s (&msg->peer1));
5356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5357 GNUNET_i2s (&msg->peer2));
5358 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5361 GNUNET_break_op (0);
5364 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5365 GNUNET_PEER_search (&msg->peer2));
5372 * Core handler for tunnel destruction
5374 * @param cls closure
5375 * @param message message
5376 * @param peer peer identity this notification is about
5377 * @param atsi performance data
5378 * @param atsi_count number of records in 'atsi'
5380 * @return GNUNET_OK to keep the connection open,
5381 * GNUNET_SYSERR to close it (signal serious error)
5384 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5385 const struct GNUNET_MessageHeader *message,
5386 const struct GNUNET_ATS_Information *atsi,
5387 unsigned int atsi_count)
5389 struct GNUNET_MESH_TunnelDestroy *msg;
5390 struct MeshTunnel *t;
5392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5393 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5394 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5396 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5397 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5400 /* Probably already got the message from another path,
5401 * destroyed the tunnel and retransmitted to children.
5404 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5407 if (t->id.oid == myid)
5409 GNUNET_break_op (0);
5412 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5414 /* Tunnel was incoming, notify clients */
5415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5416 t->local_tid, t->local_tid_dest);
5417 send_clients_tunnel_destroy (t);
5419 tunnel_send_destroy (t);
5420 t->destroy = GNUNET_YES;
5421 // TODO: add timeout to destroy the tunnel anyway
5427 * Core handler for mesh network traffic going from the origin to a peer
5429 * @param cls closure
5430 * @param peer peer identity this notification is about
5431 * @param message message
5432 * @param atsi performance data
5433 * @param atsi_count number of records in 'atsi'
5434 * @return GNUNET_OK to keep the connection open,
5435 * GNUNET_SYSERR to close it (signal serious error)
5438 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5439 const struct GNUNET_MessageHeader *message,
5440 const struct GNUNET_ATS_Information *atsi,
5441 unsigned int atsi_count)
5443 struct GNUNET_MESH_Unicast *msg;
5444 struct GNUNET_PeerIdentity *neighbor;
5445 struct MeshTunnelChildInfo *cinfo;
5446 struct MeshTunnel *t;
5447 GNUNET_PEER_Id dest_id;
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5455 size = ntohs (message->size);
5457 sizeof (struct GNUNET_MESH_Unicast) +
5458 sizeof (struct GNUNET_MessageHeader))
5463 msg = (struct GNUNET_MESH_Unicast *) message;
5464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5465 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5467 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5470 /* TODO notify back: we don't know this tunnel */
5471 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5472 GNUNET_break_op (0);
5475 pid = ntohl (msg->pid);
5476 if (t->fwd_pid == pid)
5478 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5480 " Already seen pid %u, DROPPING!\n", pid);
5485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5486 " pid %u not seen yet, forwarding\n", pid);
5489 t->skip += (pid - t->fwd_pid) - 1;
5492 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5494 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5495 GNUNET_break_op (0);
5496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5497 "Received PID %u, ACK %u\n",
5498 pid, t->last_fwd_ack);
5502 tunnel_reset_timeout (t);
5503 dest_id = GNUNET_PEER_search (&msg->destination);
5504 if (dest_id == myid)
5506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5507 " it's for us! sending to clients...\n");
5508 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5509 send_subscribed_clients (message, &msg[1].header, t);
5510 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5513 ttl = ntohl (msg->ttl);
5514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5517 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5518 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5519 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5523 " not for us, retransmitting...\n");
5525 neighbor = tree_get_first_hop (t->tree, dest_id);
5526 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5528 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5531 if (GNUNET_YES == t->nobuffer &&
5532 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5534 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5535 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5536 GNUNET_break_op (0);
5539 send_prebuilt_message (message, neighbor, t);
5540 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5546 * Core handler for mesh network traffic going from the origin to all peers
5548 * @param cls closure
5549 * @param message message
5550 * @param peer peer identity this notification is about
5551 * @param atsi performance data
5552 * @param atsi_count number of records in 'atsi'
5553 * @return GNUNET_OK to keep the connection open,
5554 * GNUNET_SYSERR to close it (signal serious error)
5556 * TODO: Check who we got this from, to validate route.
5559 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5560 const struct GNUNET_MessageHeader *message,
5561 const struct GNUNET_ATS_Information *atsi,
5562 unsigned int atsi_count)
5564 struct GNUNET_MESH_Multicast *msg;
5565 struct MeshTunnel *t;
5569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5571 size = ntohs (message->size);
5572 if (sizeof (struct GNUNET_MESH_Multicast) +
5573 sizeof (struct GNUNET_MessageHeader) > size)
5575 GNUNET_break_op (0);
5578 msg = (struct GNUNET_MESH_Multicast *) message;
5579 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5583 /* TODO notify that we dont know that tunnel */
5584 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5585 GNUNET_break_op (0);
5588 pid = ntohl (msg->pid);
5589 if (t->fwd_pid == pid)
5591 /* already seen this packet, drop */
5592 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5594 " Already seen pid %u, DROPPING!\n", pid);
5595 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5601 " pid %u not seen yet, forwarding\n", pid);
5603 t->skip += (pid - t->fwd_pid) - 1;
5605 tunnel_reset_timeout (t);
5607 /* Transmit to locally interested clients */
5608 if (NULL != t->peers &&
5609 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5611 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5612 send_subscribed_clients (message, &msg[1].header, t);
5613 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5616 if (ntohl (msg->ttl) == 0)
5618 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5619 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5620 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5623 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5624 tunnel_send_multicast (t, message);
5630 * Core handler for mesh network traffic toward the owner of a tunnel
5632 * @param cls closure
5633 * @param message message
5634 * @param peer peer identity this notification is about
5635 * @param atsi performance data
5636 * @param atsi_count number of records in 'atsi'
5638 * @return GNUNET_OK to keep the connection open,
5639 * GNUNET_SYSERR to close it (signal serious error)
5642 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5643 const struct GNUNET_MessageHeader *message,
5644 const struct GNUNET_ATS_Information *atsi,
5645 unsigned int atsi_count)
5647 struct GNUNET_MESH_ToOrigin *msg;
5648 struct GNUNET_PeerIdentity id;
5649 struct MeshPeerInfo *peer_info;
5650 struct MeshTunnel *t;
5654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5656 size = ntohs (message->size);
5657 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5658 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5660 GNUNET_break_op (0);
5663 msg = (struct GNUNET_MESH_ToOrigin *) message;
5664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5665 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5666 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5670 /* TODO notify that we dont know this tunnel (whom)? */
5671 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5672 GNUNET_break_op (0);
5676 if (NULL != t->owner)
5679 struct GNUNET_MESH_ToOrigin *copy;
5681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5682 " it's for us! sending to clients...\n");
5683 /* TODO signature verification */
5684 memcpy (cbuf, message, size);
5685 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5686 copy->tid = htonl (t->local_tid);
5687 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5688 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5689 ©->header, GNUNET_NO);
5690 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5694 " not for us, retransmitting...\n");
5696 peer_info = peer_info_get (&msg->oid);
5697 if (NULL == peer_info)
5699 /* unknown origin of tunnel */
5703 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5704 send_prebuilt_message (message, &id, t);
5705 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5712 * Core handler for mesh network traffic point-to-point acks.
5714 * @param cls closure
5715 * @param message message
5716 * @param peer peer identity this notification is about
5717 * @param atsi performance data
5718 * @param atsi_count number of records in 'atsi'
5720 * @return GNUNET_OK to keep the connection open,
5721 * GNUNET_SYSERR to close it (signal serious error)
5724 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5725 const struct GNUNET_MessageHeader *message,
5726 const struct GNUNET_ATS_Information *atsi,
5727 unsigned int atsi_count)
5729 struct GNUNET_MESH_ACK *msg;
5730 struct MeshTunnel *t;
5733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5735 msg = (struct GNUNET_MESH_ACK *) message;
5737 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5741 /* TODO notify that we dont know this tunnel (whom)? */
5742 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5743 GNUNET_break_op (0);
5746 ack = ntohl (msg->pid);
5748 /* Is this a forward or backward ACK? */
5749 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5751 struct MeshTunnelChildInfo *cinfo;
5754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5755 cinfo = tunnel_get_neighbor_fc (t, peer);
5756 cinfo->fwd_ack = ack;
5757 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5758 tunnel_unlock_fwd_queues (t);
5762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5764 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5765 tunnel_unlock_bck_queue (t);
5772 * Core handler for path ACKs
5774 * @param cls closure
5775 * @param message message
5776 * @param peer peer identity this notification is about
5777 * @param atsi performance data
5778 * @param atsi_count number of records in 'atsi'
5780 * @return GNUNET_OK to keep the connection open,
5781 * GNUNET_SYSERR to close it (signal serious error)
5784 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5785 const struct GNUNET_MessageHeader *message,
5786 const struct GNUNET_ATS_Information *atsi,
5787 unsigned int atsi_count)
5789 struct GNUNET_MESH_PathACK *msg;
5790 struct GNUNET_PeerIdentity id;
5791 struct MeshPeerInfo *peer_info;
5792 struct MeshPeerPath *p;
5793 struct MeshTunnel *t;
5795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5796 GNUNET_i2s (&my_full_id));
5797 msg = (struct GNUNET_MESH_PathACK *) message;
5798 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5801 /* TODO notify that we don't know the tunnel */
5802 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5804 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5808 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5810 peer_info = peer_info_get (&msg->peer_id);
5811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5812 GNUNET_i2s (&msg->peer_id));
5813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5816 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5819 "connect_by_string completed, stopping search\n");
5820 regex_cancel_search (t->regex_ctx);
5821 t->regex_ctx = NULL;
5824 /* Add paths to peers? */
5825 p = tree_get_path_to_peer (t->tree, peer_info->id);
5828 path_add_to_peers (p, GNUNET_YES);
5836 /* Message for us? */
5837 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5840 if (NULL == t->owner)
5842 GNUNET_break_op (0);
5845 if (NULL != t->dht_get_type)
5847 GNUNET_DHT_get_stop (t->dht_get_type);
5848 t->dht_get_type = NULL;
5850 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5852 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5853 send_client_peer_connected (t, peer_info->id);
5858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5859 " not for us, retransmitting...\n");
5860 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5861 peer_info = peer_info_get (&msg->oid);
5862 if (NULL == peer_info)
5864 /* If we know the tunnel, we should DEFINITELY know the peer */
5868 send_prebuilt_message (message, &id, t);
5874 * Core handler for mesh keepalives.
5876 * @param cls closure
5877 * @param message message
5878 * @param peer peer identity this notification is about
5879 * @param atsi performance data
5880 * @param atsi_count number of records in 'atsi'
5881 * @return GNUNET_OK to keep the connection open,
5882 * GNUNET_SYSERR to close it (signal serious error)
5884 * TODO: Check who we got this from, to validate route.
5887 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5888 const struct GNUNET_MessageHeader *message,
5889 const struct GNUNET_ATS_Information *atsi,
5890 unsigned int atsi_count)
5892 struct GNUNET_MESH_TunnelKeepAlive *msg;
5893 struct MeshTunnel *t;
5895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5898 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5899 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5903 /* TODO notify that we dont know that tunnel */
5904 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5905 GNUNET_break_op (0);
5909 tunnel_reset_timeout (t);
5911 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5912 tunnel_send_multicast (t, message);
5919 * Functions to handle messages from core
5921 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5922 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5923 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5924 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5925 sizeof (struct GNUNET_MESH_PathBroken)},
5926 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5927 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5928 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5929 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5930 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5931 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5932 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5933 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5934 sizeof (struct GNUNET_MESH_ACK)},
5935 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5936 sizeof (struct GNUNET_MESH_PathACK)},
5942 /******************************************************************************/
5943 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5944 /******************************************************************************/
5947 * deregister_app: iterator for removing each application registered by a client
5949 * @param cls closure
5950 * @param key the hash of the application id (used to access the hashmap)
5951 * @param value the value stored at the key (client)
5953 * @return GNUNET_OK on success
5956 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5958 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5959 GNUNET_break (GNUNET_YES ==
5960 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5966 * notify_client_connection_failure: notify a client that the connection to the
5967 * requested remote peer is not possible (for instance, no route found)
5968 * Function called when the socket is ready to queue more data. "buf" will be
5969 * NULL and "size" zero if the socket was closed for writing in the meantime.
5971 * @param cls closure
5972 * @param size number of bytes available in buf
5973 * @param buf where the callee should write the message
5974 * @return number of bytes written to buf
5977 notify_client_connection_failure (void *cls, size_t size, void *buf)
5980 struct MeshPeerInfo *peer_info;
5981 struct GNUNET_MESH_PeerControl *msg;
5982 struct GNUNET_PeerIdentity id;
5984 if (0 == size && NULL == buf)
5986 // TODO retry? cancel?
5990 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5991 peer_info = (struct MeshPeerInfo *) cls;
5992 msg = (struct GNUNET_MESH_PeerControl *) buf;
5993 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5994 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5995 // msg->tunnel_id = htonl(peer_info->t->tid);
5996 GNUNET_PEER_resolve (peer_info->id, &id);
5997 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6005 * Send keepalive packets for a peer
6007 * @param cls Closure (tunnel for which to send the keepalive).
6008 * @param tc Notification context.
6011 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6013 struct MeshTunnel *t = cls;
6014 struct GNUNET_MESH_TunnelKeepAlive *msg;
6015 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6018 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6019 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6025 "sending keepalive for tunnel %d\n", t->id.tid);
6027 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6028 msg->header.size = htons (size);
6029 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6030 msg->oid = my_full_id;
6031 msg->tid = htonl (t->id.tid);
6032 tunnel_send_multicast (t, &msg->header);
6034 t->path_refresh_task =
6035 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6036 tunnel_reset_timeout(t);
6041 * Function to process paths received for a new peer addition. The recorded
6042 * paths form the initial tunnel, which can be optimized later.
6043 * Called on each result obtained for the DHT search.
6045 * @param cls closure
6046 * @param exp when will this value expire
6047 * @param key key of the result
6048 * @param get_path path of the get request
6049 * @param get_path_length lenght of get_path
6050 * @param put_path path of the put request
6051 * @param put_path_length length of the put_path
6052 * @param type type of the result
6053 * @param size number of bytes in data
6054 * @param data pointer to the result data
6056 * TODO: re-issue the request after certain time? cancel after X results?
6059 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6060 const struct GNUNET_HashCode * key,
6061 const struct GNUNET_PeerIdentity *get_path,
6062 unsigned int get_path_length,
6063 const struct GNUNET_PeerIdentity *put_path,
6064 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6065 size_t size, const void *data)
6067 struct MeshPathInfo *path_info = cls;
6068 struct MeshPeerPath *p;
6069 struct GNUNET_PeerIdentity pi;
6072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6073 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6076 p = path_build_from_dht (get_path, get_path_length, put_path,
6078 path_add_to_peers (p, GNUNET_NO);
6080 for (i = 0; i < path_info->peer->ntunnels; i++)
6082 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6083 peer_info_connect (path_info->peer, path_info->t);
6091 * Function to process paths received for a new peer addition. The recorded
6092 * paths form the initial tunnel, which can be optimized later.
6093 * Called on each result obtained for the DHT search.
6095 * @param cls closure
6096 * @param exp when will this value expire
6097 * @param key key of the result
6098 * @param get_path path of the get request
6099 * @param get_path_length lenght of get_path
6100 * @param put_path path of the put request
6101 * @param put_path_length length of the put_path
6102 * @param type type of the result
6103 * @param size number of bytes in data
6104 * @param data pointer to the result data
6107 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6108 const struct GNUNET_HashCode * key,
6109 const struct GNUNET_PeerIdentity *get_path,
6110 unsigned int get_path_length,
6111 const struct GNUNET_PeerIdentity *put_path,
6112 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6113 size_t size, const void *data)
6115 const struct PBlock *pb = data;
6116 const struct GNUNET_PeerIdentity *pi = &pb->id;
6117 struct MeshTunnel *t = cls;
6118 struct MeshPeerInfo *peer_info;
6119 struct MeshPeerPath *p;
6121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6122 if (size != sizeof (struct PBlock))
6124 GNUNET_break_op (0);
6127 if (ntohl(pb->type) != t->type)
6129 GNUNET_break_op (0);
6132 GNUNET_assert (NULL != t->owner);
6133 peer_info = peer_info_get (pi);
6134 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6136 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6138 p = path_build_from_dht (get_path, get_path_length, put_path,
6140 path_add_to_peers (p, GNUNET_NO);
6142 tunnel_add_peer (t, peer_info);
6143 peer_info_connect (peer_info, t);
6148 * Function to process DHT string to regex matching.
6149 * Called on each result obtained for the DHT search.
6151 * @param cls closure (search context)
6152 * @param exp when will this value expire
6153 * @param key key of the result
6154 * @param get_path path of the get request (not used)
6155 * @param get_path_length lenght of get_path (not used)
6156 * @param put_path path of the put request (not used)
6157 * @param put_path_length length of the put_path (not used)
6158 * @param type type of the result
6159 * @param size number of bytes in data
6160 * @param data pointer to the result data
6163 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6164 const struct GNUNET_HashCode * key,
6165 const struct GNUNET_PeerIdentity *get_path,
6166 unsigned int get_path_length,
6167 const struct GNUNET_PeerIdentity *put_path,
6168 unsigned int put_path_length,
6169 enum GNUNET_BLOCK_Type type,
6170 size_t size, const void *data)
6172 const struct MeshRegexAccept *block = data;
6173 struct MeshRegexSearchContext *ctx = cls;
6174 struct MeshRegexSearchInfo *info = ctx->info;
6175 struct MeshPeerPath *p;
6176 struct MeshPeerInfo *peer_info;
6178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6181 peer_info = peer_info_get(&block->id);
6182 p = path_build_from_dht (get_path, get_path_length, put_path,
6184 path_add_to_peers (p, GNUNET_NO);
6187 tunnel_add_peer (info->t, peer_info);
6188 peer_info_connect (peer_info, info->t);
6189 if (0 == info->peer)
6191 info->peer = peer_info->id;
6195 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6198 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6199 ®ex_connect_timeout,
6207 * Function to process DHT string to regex matching.
6208 * Called on each result obtained for the DHT search.
6210 * @param cls closure (search context)
6211 * @param exp when will this value expire
6212 * @param key key of the result
6213 * @param get_path path of the get request (not used)
6214 * @param get_path_length lenght of get_path (not used)
6215 * @param put_path path of the put request (not used)
6216 * @param put_path_length length of the put_path (not used)
6217 * @param type type of the result
6218 * @param size number of bytes in data
6219 * @param data pointer to the result data
6221 * TODO: re-issue the request after certain time? cancel after X results?
6224 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6225 const struct GNUNET_HashCode * key,
6226 const struct GNUNET_PeerIdentity *get_path,
6227 unsigned int get_path_length,
6228 const struct GNUNET_PeerIdentity *put_path,
6229 unsigned int put_path_length,
6230 enum GNUNET_BLOCK_Type type,
6231 size_t size, const void *data)
6233 const struct MeshRegexBlock *block = data;
6234 struct MeshRegexSearchContext *ctx = cls;
6235 struct MeshRegexSearchInfo *info = ctx->info;
6239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6240 "DHT GET STRING RETURNED RESULTS\n");
6241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6242 " key: %s\n", GNUNET_h2s (key));
6244 copy = GNUNET_malloc (size);
6245 memcpy (copy, data, size);
6246 GNUNET_break (GNUNET_OK ==
6247 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6248 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6249 len = ntohl (block->n_proof);
6251 char proof[len + 1];
6253 memcpy (proof, &block[1], len);
6255 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6257 GNUNET_break_op (0);
6261 len = strlen (info->description);
6262 if (len == ctx->position) // String processed
6264 if (GNUNET_YES == ntohl (block->accepting))
6266 regex_find_path(key, ctx);
6270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6271 // FIXME REGEX this block not successful, wait for more? start timeout?
6276 regex_next_edge (block, size, ctx);
6281 /******************************************************************************/
6282 /********************* MESH LOCAL HANDLES **************************/
6283 /******************************************************************************/
6287 * Handler for client disconnection
6289 * @param cls closure
6290 * @param client identification of the client; NULL
6291 * for the last call when the server is destroyed
6294 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6296 struct MeshClient *c;
6297 struct MeshClient *next;
6300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6309 if (c->handle != client)
6311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6317 GNUNET_SERVER_client_drop (c->handle);
6318 c->shutting_down = GNUNET_YES;
6319 GNUNET_assert (NULL != c->own_tunnels);
6320 GNUNET_assert (NULL != c->incoming_tunnels);
6321 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6322 &tunnel_destroy_iterator, c);
6323 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6324 &tunnel_destroy_iterator, c);
6325 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6326 &tunnel_destroy_iterator, c);
6327 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6328 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6329 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6331 /* deregister clients applications */
6332 if (NULL != c->apps)
6334 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6335 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6337 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6338 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6340 GNUNET_SCHEDULER_cancel (announce_applications_task);
6341 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6343 if (NULL != c->types)
6344 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6345 for (i = 0; i < c->n_regex; i++)
6347 GNUNET_free (c->regexes[i]);
6349 GNUNET_free_non_null (c->regexes);
6350 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6351 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6353 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6356 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6365 * Handler for new clients
6367 * @param cls closure
6368 * @param client identification of the client
6369 * @param message the actual message, which includes messages the client wants
6372 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6373 const struct GNUNET_MessageHeader *message)
6375 struct GNUNET_MESH_ClientConnect *cc_msg;
6376 struct MeshClient *c;
6377 GNUNET_MESH_ApplicationType *a;
6384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6385 /* Check data sanity */
6386 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6387 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6388 ntypes = ntohs (cc_msg->types);
6389 napps = ntohs (cc_msg->applications);
6391 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6394 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6398 /* Create new client structure */
6399 c = GNUNET_malloc (sizeof (struct MeshClient));
6400 c->id = next_client_id++;
6401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6403 GNUNET_SERVER_client_keep (client);
6404 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6407 GNUNET_MESH_ApplicationType at;
6408 struct GNUNET_HashCode hc;
6410 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6411 for (i = 0; i < napps; i++)
6414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6415 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6416 /* store in clients hashmap */
6417 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6418 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6419 /* store in global hashmap, for announcements */
6420 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6421 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6423 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6424 announce_applications_task =
6425 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6431 struct GNUNET_HashCode hc;
6433 t = (uint16_t *) & a[napps];
6434 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6435 for (i = 0; i < ntypes; i++)
6438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6439 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6441 /* store in clients hashmap */
6442 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6443 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6444 /* store in global hashmap */
6445 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6450 " client has %u+%u subscriptions\n", napps, ntypes);
6452 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6453 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6454 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6455 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6456 GNUNET_SERVER_notification_context_add (nc, client);
6457 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6459 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6465 * Handler for clients announcing available services by a regular expression.
6467 * @param cls closure
6468 * @param client identification of the client
6469 * @param message the actual message, which includes messages the client wants
6472 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6473 const struct GNUNET_MessageHeader *message)
6475 struct MeshClient *c;
6479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6481 /* Sanity check for client registration */
6482 if (NULL == (c = client_get (client)))
6485 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6490 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6491 regex = GNUNET_malloc (len + 1);
6492 memcpy (regex, &message[1], len);
6494 GNUNET_array_append (c->regexes, c->n_regex, regex);
6495 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6497 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6503 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6509 * Handler for requests of new tunnels
6511 * @param cls closure
6512 * @param client identification of the client
6513 * @param message the actual message
6516 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6517 const struct GNUNET_MessageHeader *message)
6519 struct GNUNET_MESH_TunnelMessage *t_msg;
6520 struct MeshTunnel *t;
6521 struct MeshClient *c;
6522 MESH_TunnelNumber tid;
6524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\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 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6544 /* Sanity check for tunnel numbering */
6545 tid = ntohl (t_msg->tunnel_id);
6546 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6552 /* Sanity check for duplicate tunnel IDs */
6553 if (NULL != tunnel_get_by_local_id (c, tid))
6556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6560 while (NULL != tunnel_get_by_pi (myid, next_tid))
6561 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6562 t = tunnel_new (myid, next_tid++, c, tid);
6565 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6570 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6572 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6573 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6576 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6582 * Handler for requests of deleting tunnels
6584 * @param cls closure
6585 * @param client identification of the client
6586 * @param message the actual message
6589 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6590 const struct GNUNET_MessageHeader *message)
6592 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6593 struct MeshClient *c;
6594 struct MeshTunnel *t;
6595 MESH_TunnelNumber tid;
6597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6598 "Got a DESTROY TUNNEL from client!\n");
6600 /* Sanity check for client registration */
6601 if (NULL == (c = client_get (client)))
6604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6609 /* Message sanity check */
6610 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6613 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6617 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6619 /* Retrieve tunnel */
6620 tid = ntohl (tunnel_msg->tunnel_id);
6621 t = tunnel_get_by_local_id(c, tid);
6624 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6629 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6631 client_ignore_tunnel (c, t);
6633 // TODO: when to destroy incoming tunnel?
6634 if (t->nclients == 0)
6636 GNUNET_assert (GNUNET_YES ==
6637 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6639 GNUNET_assert (GNUNET_YES ==
6640 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6641 &my_full_id.hashPubKey,
6645 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6648 send_client_tunnel_disconnect(t, c);
6649 client_delete_tunnel(c, t);
6651 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6653 tunnel_send_destroy (t);
6654 t->destroy = GNUNET_YES;
6655 // The tunnel will be destroyed when the last message is transmitted.
6656 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6662 * Handler for requests of seeting tunnel's speed.
6664 * @param cls Closure (unused).
6665 * @param client Identification of the client.
6666 * @param message The actual message.
6669 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6670 const struct GNUNET_MessageHeader *message)
6672 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6673 struct MeshClient *c;
6674 struct MeshTunnel *t;
6675 MESH_TunnelNumber tid;
6677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6678 "Got a SPEED request from client!\n");
6680 /* Sanity check for client registration */
6681 if (NULL == (c = client_get (client)))
6684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6690 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6692 /* Retrieve tunnel */
6693 tid = ntohl (tunnel_msg->tunnel_id);
6694 t = tunnel_get_by_local_id(c, tid);
6697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6703 switch (ntohs(message->type))
6705 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6706 t->speed_min = GNUNET_YES;
6708 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6709 t->speed_min = GNUNET_NO;
6714 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6719 * Handler for requests of seeting tunnel's buffering policy.
6721 * @param cls Closure (unused).
6722 * @param client Identification of the client.
6723 * @param message The actual message.
6726 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6727 const struct GNUNET_MessageHeader *message)
6729 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6730 struct MeshClient *c;
6731 struct MeshTunnel *t;
6732 MESH_TunnelNumber tid;
6734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6735 "Got a BUFFER request from client!\n");
6737 /* Sanity check for client registration */
6738 if (NULL == (c = client_get (client)))
6741 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6746 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6748 /* Retrieve tunnel */
6749 tid = ntohl (tunnel_msg->tunnel_id);
6750 t = tunnel_get_by_local_id(c, tid);
6753 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6755 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6759 switch (ntohs(message->type))
6761 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6762 t->nobuffer = GNUNET_NO;
6764 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6765 t->nobuffer = GNUNET_YES;
6771 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6776 * Handler for connection requests to new peers
6778 * @param cls closure
6779 * @param client identification of the client
6780 * @param message the actual message (PeerControl)
6783 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6784 const struct GNUNET_MessageHeader *message)
6786 struct GNUNET_MESH_PeerControl *peer_msg;
6787 struct MeshPeerInfo *peer_info;
6788 struct MeshClient *c;
6789 struct MeshTunnel *t;
6790 MESH_TunnelNumber tid;
6792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6793 /* Sanity check for client registration */
6794 if (NULL == (c = client_get (client)))
6797 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6802 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6804 /* Sanity check for message size */
6805 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6808 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6812 /* Tunnel exists? */
6813 tid = ntohl (peer_msg->tunnel_id);
6814 t = tunnel_get_by_local_id (c, tid);
6818 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6822 /* Does client own tunnel? */
6823 if (t->owner->handle != client)
6826 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6830 GNUNET_i2s (&peer_msg->peer));
6831 peer_info = peer_info_get (&peer_msg->peer);
6833 tunnel_add_peer (t, peer_info);
6834 peer_info_connect (peer_info, t);
6836 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6842 * Handler for disconnection requests of peers in a tunnel
6844 * @param cls closure
6845 * @param client identification of the client
6846 * @param message the actual message (PeerControl)
6849 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6850 const struct GNUNET_MessageHeader *message)
6852 struct GNUNET_MESH_PeerControl *peer_msg;
6853 struct MeshPeerInfo *peer_info;
6854 struct MeshClient *c;
6855 struct MeshTunnel *t;
6856 MESH_TunnelNumber tid;
6858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6859 /* Sanity check for client registration */
6860 if (NULL == (c = client_get (client)))
6863 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6868 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6870 /* Sanity check for message size */
6871 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6874 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6878 /* Tunnel exists? */
6879 tid = ntohl (peer_msg->tunnel_id);
6880 t = tunnel_get_by_local_id (c, tid);
6884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6889 /* Does client own tunnel? */
6890 if (t->owner->handle != client)
6893 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6898 GNUNET_i2s (&peer_msg->peer));
6899 /* Is the peer in the tunnel? */
6901 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6902 if (NULL == peer_info)
6905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6909 /* Ok, delete peer from tunnel */
6910 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6911 &peer_msg->peer.hashPubKey);
6913 send_destroy_path (t, peer_info->id);
6914 tunnel_delete_peer (t, peer_info->id);
6915 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6920 * Handler for blacklist requests of peers in a tunnel
6922 * @param cls closure
6923 * @param client identification of the client
6924 * @param message the actual message (PeerControl)
6926 * FIXME implement DHT block bloomfilter
6929 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6930 const struct GNUNET_MessageHeader *message)
6932 struct GNUNET_MESH_PeerControl *peer_msg;
6933 struct MeshClient *c;
6934 struct MeshTunnel *t;
6935 MESH_TunnelNumber tid;
6937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6938 /* Sanity check for client registration */
6939 if (NULL == (c = client_get (client)))
6942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6947 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6949 /* Sanity check for message size */
6950 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6953 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6957 /* Tunnel exists? */
6958 tid = ntohl (peer_msg->tunnel_id);
6959 t = tunnel_get_by_local_id (c, tid);
6963 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6968 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6969 GNUNET_PEER_intern(&peer_msg->peer));
6974 * Handler for unblacklist requests of peers in a tunnel
6976 * @param cls closure
6977 * @param client identification of the client
6978 * @param message the actual message (PeerControl)
6981 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6982 const struct GNUNET_MessageHeader *message)
6984 struct GNUNET_MESH_PeerControl *peer_msg;
6985 struct MeshClient *c;
6986 struct MeshTunnel *t;
6987 MESH_TunnelNumber tid;
6991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6992 /* Sanity check for client registration */
6993 if (NULL == (c = client_get (client)))
6996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7001 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7003 /* Sanity check for message size */
7004 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7011 /* Tunnel exists? */
7012 tid = ntohl (peer_msg->tunnel_id);
7013 t = tunnel_get_by_local_id (c, tid);
7017 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7022 /* if peer is not known, complain */
7023 pid = GNUNET_PEER_search (&peer_msg->peer);
7030 /* search and remove from list */
7031 for (i = 0; i < t->nblacklisted; i++)
7033 if (t->blacklisted[i] == pid)
7035 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7036 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7041 /* if peer hasn't been blacklisted, complain */
7047 * Handler for connection requests to new peers by type
7049 * @param cls closure
7050 * @param client identification of the client
7051 * @param message the actual message (ConnectPeerByType)
7054 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7055 const struct GNUNET_MessageHeader *message)
7057 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7058 struct MeshClient *c;
7059 struct MeshTunnel *t;
7060 struct GNUNET_HashCode hash;
7061 MESH_TunnelNumber tid;
7063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7064 /* Sanity check for client registration */
7065 if (NULL == (c = client_get (client)))
7068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7073 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7075 /* Sanity check for message size */
7076 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7077 ntohs (connect_msg->header.size))
7080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7084 /* Tunnel exists? */
7085 tid = ntohl (connect_msg->tunnel_id);
7086 t = tunnel_get_by_local_id (c, tid);
7090 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7094 /* Does client own tunnel? */
7095 if (t->owner->handle != client)
7098 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7102 /* Do WE have the service? */
7103 t->type = ntohl (connect_msg->type);
7104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7105 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7106 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7109 /* Yes! Fast forward, add ourselves to the tunnel and send the
7110 * good news to the client, and alert the destination client of
7111 * an incoming tunnel.
7113 * FIXME send a path create to self, avoid code duplication
7115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7116 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7117 peer_info_get (&my_full_id),
7118 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7121 send_client_peer_connected (t, myid);
7122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7123 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7125 t->local_tid_dest = next_local_tid++;
7126 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7127 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7128 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7132 /* Ok, lets find a peer offering the service */
7133 if (NULL != t->dht_get_type)
7135 GNUNET_DHT_get_stop (t->dht_get_type);
7137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7138 GNUNET_h2s (&hash));
7140 GNUNET_DHT_get_start (dht_handle,
7141 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7143 dht_replication_level,
7144 GNUNET_DHT_RO_RECORD_ROUTE |
7145 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7147 &dht_get_type_handler, t);
7149 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7155 * Handler for connection requests to new peers by a string service description.
7157 * @param cls closure
7158 * @param client identification of the client
7159 * @param message the actual message, which includes messages the client wants
7162 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7163 const struct GNUNET_MessageHeader *message)
7165 struct GNUNET_MESH_ConnectPeerByString *msg;
7166 struct MeshRegexSearchContext *ctx;
7167 struct MeshRegexSearchInfo *info;
7168 struct GNUNET_DHT_GetHandle *get_h;
7169 struct GNUNET_HashCode key;
7170 struct MeshTunnel *t;
7171 struct MeshClient *c;
7172 MESH_TunnelNumber tid;
7176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7177 "Connect by string started\n");
7178 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7179 size = htons (message->size);
7181 /* Sanity check for client registration */
7182 if (NULL == (c = client_get (client)))
7185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7190 /* Message size sanity check */
7191 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7194 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7198 /* Tunnel exists? */
7199 tid = ntohl (msg->tunnel_id);
7200 t = tunnel_get_by_local_id (c, tid);
7204 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7208 /* Does client own tunnel? */
7209 if (t->owner->handle != client)
7212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7217 " on tunnel %s [%u]\n",
7218 GNUNET_i2s(&my_full_id),
7221 /* Only one connect_by_string allowed at the same time! */
7222 /* FIXME: allow more, return handle at api level to cancel, document */
7223 if (NULL != t->regex_ctx)
7226 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7230 /* Find string itself */
7231 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7232 string = (const char *) &msg[1];
7234 /* Initialize context */
7235 size = GNUNET_REGEX_get_first_key(string, len, &key);
7236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7237 " consumed %u bits out of %u\n", size, len);
7238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7239 " looking for %s\n", GNUNET_h2s (&key));
7241 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7243 info->description = GNUNET_malloc (len + 1);
7244 memcpy (info->description, string, len);
7245 info->description[len] = '\0';
7246 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7247 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7250 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7251 ctx->position = size;
7255 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7257 /* Start search in DHT */
7258 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7259 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7260 &key, /* key to search */
7261 dht_replication_level, /* replication level */
7262 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7263 NULL, /* xquery */ // FIXME BLOOMFILTER
7264 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7265 &dht_get_string_handler, ctx);
7267 GNUNET_break (GNUNET_OK ==
7268 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7271 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7273 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7279 * Handler for client traffic directed to one peer
7281 * @param cls closure
7282 * @param client identification of the client
7283 * @param message the actual message
7286 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7287 const struct GNUNET_MessageHeader *message)
7289 struct MeshClient *c;
7290 struct MeshTunnel *t;
7291 struct MeshPeerInfo *pi;
7292 struct GNUNET_MESH_Unicast *data_msg;
7293 MESH_TunnelNumber tid;
7296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7297 "Got a unicast request from a client!\n");
7299 /* Sanity check for client registration */
7300 if (NULL == (c = client_get (client)))
7303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7308 data_msg = (struct GNUNET_MESH_Unicast *) message;
7310 /* Sanity check for message size */
7311 size = ntohs (message->size);
7312 if (sizeof (struct GNUNET_MESH_Unicast) +
7313 sizeof (struct GNUNET_MessageHeader) > size)
7316 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7320 /* Tunnel exists? */
7321 tid = ntohl (data_msg->tid);
7322 t = tunnel_get_by_local_id (c, tid);
7326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7330 /* Is it a local tunnel? Then, does client own the tunnel? */
7331 if (t->owner->handle != client)
7334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7338 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7339 &data_msg->destination.hashPubKey);
7340 /* Is the selected peer in the tunnel? */
7344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7348 /* PID should be as expected */
7349 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7352 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7353 "Unicast PID, expected %u, got %u\n",
7354 t->fwd_pid + 1, ntohl (data_msg->pid));
7355 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7359 /* Ok, everything is correct, send the message
7360 * (pretend we got it from a mesh peer)
7363 /* Work around const limitation */
7364 char buf[ntohs (message->size)] GNUNET_ALIGN;
7365 struct GNUNET_MESH_Unicast *copy;
7367 copy = (struct GNUNET_MESH_Unicast *) buf;
7368 memcpy (buf, data_msg, size);
7369 copy->oid = my_full_id;
7370 copy->tid = htonl (t->id.tid);
7371 copy->ttl = htonl (default_ttl);
7372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7373 " calling generic handler...\n");
7374 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7377 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7384 * Handler for client traffic directed to the origin
7386 * @param cls closure
7387 * @param client identification of the client
7388 * @param message the actual message
7391 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7392 const struct GNUNET_MessageHeader *message)
7394 struct GNUNET_MESH_ToOrigin *data_msg;
7395 struct MeshTunnelClientInfo *clinfo;
7396 struct MeshClient *c;
7397 struct MeshTunnel *t;
7398 MESH_TunnelNumber tid;
7401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7402 "Got a ToOrigin request from a client!\n");
7403 /* Sanity check for client registration */
7404 if (NULL == (c = client_get (client)))
7407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7412 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7414 /* Sanity check for message size */
7415 size = ntohs (message->size);
7416 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7417 sizeof (struct GNUNET_MessageHeader) > size)
7420 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7424 /* Tunnel exists? */
7425 tid = ntohl (data_msg->tid);
7426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7427 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7433 t = tunnel_get_by_local_id (c, tid);
7436 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7437 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7439 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7443 /* It should be sent by someone who has this as incoming tunnel. */
7444 if (GNUNET_NO == client_knows_tunnel (c, t))
7447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7451 /* PID should be as expected */
7452 clinfo = tunnel_get_client_fc (t, c);
7453 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7457 "To Origin PID, expected %u, got %u\n",
7458 clinfo->bck_pid + 1,
7459 ntohl (data_msg->pid));
7460 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7464 /* Ok, everything is correct, send the message
7465 * (pretend we got it from a mesh peer)
7469 char buf[ntohs (message->size)] GNUNET_ALIGN;
7470 struct GNUNET_MESH_ToOrigin *copy;
7472 /* Work around const limitation */
7473 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7474 memcpy (buf, data_msg, size);
7475 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7476 copy->tid = htonl (t->id.tid);
7477 copy->ttl = htonl (default_ttl);
7478 if (ntohl (copy->pid) != (t->bck_pid + 1))
7481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7482 "To Origin PID, expected %u, got %u\n",
7488 copy->sender = my_full_id;
7489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7490 " calling generic handler...\n");
7491 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7493 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7500 * Handler for client traffic directed to all peers in a tunnel
7502 * @param cls closure
7503 * @param client identification of the client
7504 * @param message the actual message
7507 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7508 const struct GNUNET_MessageHeader *message)
7510 struct MeshClient *c;
7511 struct MeshTunnel *t;
7512 struct GNUNET_MESH_Multicast *data_msg;
7513 MESH_TunnelNumber tid;
7515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7516 "Got a multicast request from a client!\n");
7518 /* Sanity check for client registration */
7519 if (NULL == (c = client_get (client)))
7522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7527 data_msg = (struct GNUNET_MESH_Multicast *) message;
7529 /* Sanity check for message size */
7530 if (sizeof (struct GNUNET_MESH_Multicast) +
7531 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7538 /* Tunnel exists? */
7539 tid = ntohl (data_msg->tid);
7540 t = tunnel_get_by_local_id (c, tid);
7544 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7551 /* Does client own tunnel? */
7552 if (t->owner->handle != client)
7555 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7559 /* PID should be as expected */
7560 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7564 "Multicast PID, expected %u, got %u\n",
7565 t->fwd_pid + 1, ntohl (data_msg->pid));
7566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7571 char buf[ntohs (message->size)] GNUNET_ALIGN;
7572 struct GNUNET_MESH_Multicast *copy;
7574 copy = (struct GNUNET_MESH_Multicast *) buf;
7575 memcpy (buf, message, ntohs (message->size));
7576 copy->oid = my_full_id;
7577 copy->tid = htonl (t->id.tid);
7578 copy->ttl = htonl (default_ttl);
7579 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7581 " calling generic handler...\n");
7582 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7585 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7591 * Handler for client's ACKs for payload traffic.
7593 * @param cls Closure (unused).
7594 * @param client Identification of the client.
7595 * @param message The actual message.
7598 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7599 const struct GNUNET_MessageHeader *message)
7601 struct GNUNET_MESH_LocalAck *msg;
7602 struct MeshTunnel *t;
7603 struct MeshClient *c;
7604 MESH_TunnelNumber tid;
7607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7608 /* Sanity check for client registration */
7609 if (NULL == (c = client_get (client)))
7612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7617 msg = (struct GNUNET_MESH_LocalAck *) message;
7619 /* Tunnel exists? */
7620 tid = ntohl (msg->tunnel_id);
7621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7622 t = tunnel_get_by_local_id (c, tid);
7626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7632 ack = ntohl (msg->max_pid);
7633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7635 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7636 if (NULL != t->owner && t->owner->handle == client)
7638 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7640 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7644 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7645 tunnel_set_client_fwd_ack (t, c, ack);
7646 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7649 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7656 * Functions to handle messages from clients
7658 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7659 {&handle_local_new_client, NULL,
7660 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7661 {&handle_local_announce_regex, NULL,
7662 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7663 {&handle_local_tunnel_create, NULL,
7664 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7665 sizeof (struct GNUNET_MESH_TunnelMessage)},
7666 {&handle_local_tunnel_destroy, NULL,
7667 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7668 sizeof (struct GNUNET_MESH_TunnelMessage)},
7669 {&handle_local_tunnel_speed, NULL,
7670 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7671 sizeof (struct GNUNET_MESH_TunnelMessage)},
7672 {&handle_local_tunnel_speed, NULL,
7673 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7674 sizeof (struct GNUNET_MESH_TunnelMessage)},
7675 {&handle_local_tunnel_buffer, NULL,
7676 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7677 sizeof (struct GNUNET_MESH_TunnelMessage)},
7678 {&handle_local_tunnel_buffer, NULL,
7679 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7680 sizeof (struct GNUNET_MESH_TunnelMessage)},
7681 {&handle_local_connect_add, NULL,
7682 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7683 sizeof (struct GNUNET_MESH_PeerControl)},
7684 {&handle_local_connect_del, NULL,
7685 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7686 sizeof (struct GNUNET_MESH_PeerControl)},
7687 {&handle_local_blacklist, NULL,
7688 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7689 sizeof (struct GNUNET_MESH_PeerControl)},
7690 {&handle_local_unblacklist, NULL,
7691 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7692 sizeof (struct GNUNET_MESH_PeerControl)},
7693 {&handle_local_connect_by_type, NULL,
7694 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7695 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7696 {&handle_local_connect_by_string, NULL,
7697 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7698 {&handle_local_unicast, NULL,
7699 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7700 {&handle_local_to_origin, NULL,
7701 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7702 {&handle_local_multicast, NULL,
7703 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7704 {&handle_local_ack, NULL,
7705 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7706 sizeof (struct GNUNET_MESH_LocalAck)},
7712 * To be called on core init/fail.
7714 * @param cls service closure
7715 * @param server handle to the server for this service
7716 * @param identity the public identity of this peer
7719 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7720 const struct GNUNET_PeerIdentity *identity)
7723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7724 core_handle = server;
7725 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7728 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7729 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7731 GNUNET_abort(); // Try harder
7738 * Method called whenever a given peer connects.
7740 * @param cls closure
7741 * @param peer peer identity this notification is about
7742 * @param atsi performance data for the connection
7743 * @param atsi_count number of records in 'atsi'
7746 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7747 const struct GNUNET_ATS_Information *atsi,
7748 unsigned int atsi_count)
7750 struct MeshPeerInfo *peer_info;
7751 struct MeshPeerPath *path;
7753 DEBUG_CONN ("Peer connected\n");
7754 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7755 peer_info = peer_info_get (peer);
7756 if (myid == peer_info->id)
7758 DEBUG_CONN (" (self)\n");
7763 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7765 path = path_new (2);
7766 path->peers[0] = myid;
7767 path->peers[1] = peer_info->id;
7768 GNUNET_PEER_change_rc (myid, 1);
7769 GNUNET_PEER_change_rc (peer_info->id, 1);
7770 peer_info_add_path (peer_info, path, GNUNET_YES);
7771 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7777 * Method called whenever a peer disconnects.
7779 * @param cls closure
7780 * @param peer peer identity this notification is about
7783 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7785 struct MeshPeerInfo *pi;
7786 struct MeshPeerQueue *q;
7787 struct MeshPeerQueue *n;
7789 DEBUG_CONN ("Peer disconnected\n");
7790 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7800 /* TODO try to reroute this traffic instead */
7801 queue_destroy(q, GNUNET_YES);
7804 if (NULL != pi->core_transmit)
7806 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
7807 pi->core_transmit = NULL;
7809 peer_info_remove_path (pi, pi->id, myid);
7812 DEBUG_CONN (" (self)\n");
7814 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7819 /******************************************************************************/
7820 /************************ MAIN FUNCTIONS ****************************/
7821 /******************************************************************************/
7824 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7826 * @param cls closure
7827 * @param key current key code
7828 * @param value value in the hash map
7829 * @return GNUNET_YES if we should continue to iterate,
7833 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7835 struct MeshTunnel *t = value;
7842 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7844 * @param cls closure
7845 * @param key current key code
7846 * @param value value in the hash map
7847 * @return GNUNET_YES if we should continue to iterate,
7851 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7853 struct MeshPeerInfo *p = value;
7854 struct MeshPeerQueue *q;
7855 struct MeshPeerQueue *n;
7863 queue_destroy(q, GNUNET_YES);
7867 peer_info_destroy (p);
7873 * Task run during shutdown.
7879 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7883 if (core_handle != NULL)
7885 GNUNET_CORE_disconnect (core_handle);
7890 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
7893 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7894 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7895 if (dht_handle != NULL)
7897 GNUNET_DHT_disconnect (dht_handle);
7902 GNUNET_SERVER_notification_context_destroy (nc);
7905 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7907 GNUNET_SCHEDULER_cancel (announce_id_task);
7908 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7915 * Callback for hostkey read/generation
7918 * @param pk the private key
7919 * @param emsg error message
7922 key_generation_cb (void *cls,
7923 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
7926 struct MeshPeerInfo *peer;
7927 struct MeshPeerPath *p;
7932 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7933 _("Mesh service could not access hostkey. Exiting.\n"));
7934 GNUNET_SCHEDULER_shutdown ();
7937 my_private_key = pk;
7938 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7939 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7940 &my_full_id.hashPubKey);
7941 myid = GNUNET_PEER_intern (&my_full_id);
7942 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7943 "Mesh for peer [%s] starting\n",
7944 GNUNET_i2s(&my_full_id));
7946 // transport_handle = GNUNET_TRANSPORT_connect(c,
7956 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7959 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7960 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7961 GNUNET_SERVER_disconnect_notify (server_handle,
7962 &handle_local_client_disconnect, NULL);
7966 clients_tail = NULL;
7969 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7970 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7972 /* Create a peer_info for the local peer */
7973 peer = peer_info_get (&my_full_id);
7976 GNUNET_PEER_change_rc (myid, 1);
7977 peer_info_add_path (peer, p, GNUNET_YES);
7978 GNUNET_SERVER_resume (server_handle);
7979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
7984 * Process mesh requests.
7986 * @param cls closure
7987 * @param server the initialized server
7988 * @param c configuration to use
7991 run (void *cls, struct GNUNET_SERVER_Handle *server,
7992 const struct GNUNET_CONFIGURATION_Handle *c)
7996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7997 server_handle = server;
7998 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7999 NULL, /* Closure passed to MESH functions */
8000 &core_init, /* Call core_init once connected */
8001 &core_connect, /* Handle connects */
8002 &core_disconnect, /* remove peers on disconnects */
8003 NULL, /* Don't notify about all incoming messages */
8004 GNUNET_NO, /* For header only in notification */
8005 NULL, /* Don't notify about all outbound messages */
8006 GNUNET_NO, /* For header-only out notification */
8007 core_handlers); /* Register these handlers */
8009 if (core_handle == NULL)
8012 GNUNET_SCHEDULER_shutdown ();
8017 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8020 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8022 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8024 GNUNET_SCHEDULER_shutdown ();
8029 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8030 &refresh_path_time))
8032 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8034 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8035 "mesh", "refresh path time");
8036 GNUNET_SCHEDULER_shutdown ();
8041 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8042 &app_announce_time))
8044 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8046 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8047 "mesh", "app announce time");
8048 GNUNET_SCHEDULER_shutdown ();
8053 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8058 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8059 "mesh", "id announce time");
8060 GNUNET_SCHEDULER_shutdown ();
8068 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8069 &unacknowledged_wait_time))
8071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8073 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8074 "mesh", "unacknowledged wait time");
8075 GNUNET_SCHEDULER_shutdown ();
8080 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8083 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8085 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8086 "mesh", "connect timeout");
8087 GNUNET_SCHEDULER_shutdown ();
8092 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8095 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8097 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8098 "mesh", "max msgs queue");
8099 GNUNET_SCHEDULER_shutdown ();
8104 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8109 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8110 "mesh", "max tunnels");
8111 GNUNET_SCHEDULER_shutdown ();
8116 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8119 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8121 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8122 "mesh", "default ttl", 64);
8127 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8128 &dht_replication_level))
8130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8132 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8133 "mesh", "dht replication level", 10);
8134 dht_replication_level = 10;
8137 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8138 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
8139 peers = GNUNET_CONTAINER_multihashmap_create (32);
8140 applications = GNUNET_CONTAINER_multihashmap_create (32);
8141 types = GNUNET_CONTAINER_multihashmap_create (32);
8143 dht_handle = GNUNET_DHT_connect (c, 64);
8144 if (NULL == dht_handle)
8148 stats = GNUNET_STATISTICS_create ("mesh", c);
8150 GNUNET_SERVER_suspend (server_handle);
8151 /* Scheduled the task to clean up when shutdown is called */
8152 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8154 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8155 GNUNET_free (keyfile);
8160 * The main function for the mesh service.
8162 * @param argc number of arguments from the command line
8163 * @param argv command line arguments
8164 * @return 0 ok, 1 on error
8167 main (int argc, char *const *argv)
8172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8173 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8175 ret = (GNUNET_OK == r) ? 0 : 1;
8176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8180 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8181 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8182 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);