2 This file is part of GNUnet.
3 (C) 2001 - 2011 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?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
42 * - relay corking down to core
43 * - set ttl relative to tree depth
44 * - Add data ACK count in path ACK
45 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
51 #include "mesh_protocol.h"
52 #include "mesh_tunnel_tree.h"
53 #include "block_mesh.h"
54 #include "mesh_block_lib.h"
55 #include "gnunet_dht_service.h"
56 #include "gnunet_statistics_service.h"
57 #include "gnunet_regex_lib.h"
59 #define MESH_BLOOM_SIZE 128
61 #define MESH_DEBUG_DHT GNUNET_NO
62 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_YES
65 #if MESH_DEBUG_CONNECTION
66 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
68 #define DEBUG_CONN(...)
72 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
74 #define DEBUG_DHT(...)
81 struct timespec __mesh_start;
82 struct timespec __mesh_end;
83 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
84 #define INTERVAL_END \
86 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
87 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
88 if (__diff < 0) __diff += 1000000000;\
92 #define INTERVAL_SHOW \
94 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
96 #define INTERVAL_START
101 /******************************************************************************/
102 /************************ DATA STRUCTURES ****************************/
103 /******************************************************************************/
105 /** FWD declaration */
111 * Struct representing a piece of data being sent to other peers
115 /** Tunnel it belongs to. */
116 struct MeshTunnel *t;
118 /** In case of a multicast, task to allow a client to send more data if
119 * some neighbor is too slow. */
120 GNUNET_SCHEDULER_TaskIdentifier *task;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int *reference_counter;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct containing all info possibly needed to build a package when called
179 struct MeshTransmissionDescriptor
181 /** ID of the tunnel this packet travels in */
182 struct MESH_TunnelID *origin;
184 /** Who was this message being sent to */
185 struct MeshPeerInfo *peer;
187 /** Ultimate destination of the packet */
188 GNUNET_PEER_Id destination;
190 /** Data descriptor */
191 struct MeshData* mesh_data;
196 * Struct containing all information regarding a given peer
206 * Last time we heard from this peer
208 struct GNUNET_TIME_Absolute last_contact;
211 * Number of attempts to reconnect so far
213 int n_reconnect_attempts;
216 * Paths to reach the peer, ordered by ascending hop count
218 struct MeshPeerPath *path_head;
221 * Paths to reach the peer, ordered by ascending hop count
223 struct MeshPeerPath *path_tail;
226 * Handle to stop the DHT search for a path to this peer
228 struct GNUNET_DHT_GetHandle *dhtget;
231 * Closure given to the DHT GET
233 struct MeshPathInfo *dhtgetcls;
236 * Array of tunnels this peer participates in
237 * (most probably a small amount, therefore not a hashmap)
238 * When the path to the peer changes, notify these tunnels to let them
239 * re-adjust their path trees.
241 struct MeshTunnel **tunnels;
244 * Number of tunnels this peers participates in
246 unsigned int ntunnels;
249 * Transmission queue to core DLL head
251 struct MeshPeerQueue *queue_head;
254 * Transmission queue to core DLL tail
256 struct MeshPeerQueue *queue_tail;
259 * How many messages are in the queue to this peer.
261 unsigned int queue_n;
264 * Handle to for queued transmissions
266 struct GNUNET_CORE_TransmitHandle *core_transmit;
271 * Globally unique tunnel identification (owner + number)
272 * DO NOT USE OVER THE NETWORK
277 * Node that owns the tunnel
282 * Tunnel number to differentiate all the tunnels owned by the node oid
283 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
285 MESH_TunnelNumber tid;
290 * Struct containing all information regarding a tunnel
291 * For an intermediate node the improtant info used will be:
292 * - id Tunnel unique identification
293 * - paths[0] To know where to send it next
294 * - metainfo: ready, speeds, accounting
301 struct MESH_TunnelID id;
304 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
306 MESH_TunnelNumber local_tid;
309 * Local tunnel number for local destination clients (incoming number)
310 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
313 MESH_TunnelNumber local_tid_dest;
316 * Is the speed on the tunnel limited to the slowest peer?
321 * Is the tunnel bufferless (minimum latency)?
326 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
331 * Packet ID of the last bck packet sent (unique counter per hop).
336 * SKIP value for this tunnel.
341 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
342 * Contains the Flow Control info: FWD ACK value received,
343 * last BCK ACK sent, PID and SKIP values.
345 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
348 * Last ACK sent towards the origin (for traffic towards leaf node).
350 uint32_t last_fwd_ack;
353 * BCK ACK value received from the hop towards the owner of the tunnel,
354 * (previous node / owner): up to what message PID can we sent back to him.
359 * How many messages are in the forward queue (towards leaves).
361 unsigned int fwd_queue_n;
364 * How many messages do we accept in the forward queue.
366 unsigned int fwd_queue_max;
369 * How many messages are in the backward queue (towards origin).
371 unsigned int bck_queue_n;
374 * How many messages do we accept in the backward queue.
376 unsigned int bck_queue_max;
379 * Last time the tunnel was used
381 struct GNUNET_TIME_Absolute timestamp;
384 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
385 * containing peers added by id or by type, not intermediate peers.
387 struct GNUNET_CONTAINER_MultiHashMap *peers;
390 * Number of peers that are connected and potentially ready to receive data
392 unsigned int peers_ready;
395 * Number of peers that have been added to the tunnel
397 unsigned int peers_total;
400 * Client owner of the tunnel, if any
402 struct MeshClient *owner;
405 * Clients that have been informed about and want to stay in the tunnel.
407 struct MeshClient **clients;
410 * Flow control info for each client.
412 struct MeshTunnelClientInfo *clients_fc;
415 * Number of elements in clients/clients_fc
417 unsigned int nclients;
420 * Clients that have been informed but requested to leave the tunnel.
422 struct MeshClient **ignore;
425 * Number of elements in clients
427 unsigned int nignore;
432 GNUNET_PEER_Id *blacklisted;
435 * Number of elements in blacklisted
437 unsigned int nblacklisted;
440 * Bloomfilter (for peer identities) to stop circular routes
442 char bloomfilter[MESH_BLOOM_SIZE];
447 struct MeshTunnelTree *tree;
450 * Application type we are looking for in this tunnel
452 GNUNET_MESH_ApplicationType type;
455 * Used to search peers offering a service
457 struct GNUNET_DHT_GetHandle *dht_get_type;
460 * Initial context of the regex search for a connect_by_string
462 struct MeshRegexSearchContext *regex_ctx;
465 * Task to keep the used paths alive
467 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
470 * Task to destroy the tunnel after timeout
472 * FIXME: merge the two? a tunnel will have either
473 * a path refresh OR a timeout, never both!
475 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
478 * Flag to signal the destruction of the tunnel.
479 * If this is set GNUNET_YES the tunnel will be destroyed
480 * when the queue is empty.
487 * Info about a child node in a tunnel, needed to perform flow control.
489 struct MeshTunnelChildInfo
492 * ID of the child node.
507 * Maximum PID allowed (FWD ACK received).
512 * Last ACK sent to that child (BCK ACK).
517 * Circular buffer pointing to MeshPeerQueue elements.
518 * Size determined by the tunnel queue size.
520 struct MeshPeerQueue **send_buffer;
523 * Index of the oldest element in the send_buffer.
525 unsigned int send_buffer_start;
528 * How many elements are already in the buffer.
530 unsigned int send_buffer_n;
535 * Info about a leaf client of a tunnel, needed to perform flow control.
537 struct MeshTunnelClientInfo
540 * PID of the last packet sent to the client (FWD).
545 * PID of the last packet received from the client (BCK).
550 * Maximum PID allowed (FWD ACK received).
555 * Last ACK sent to that child (BCK ACK).
563 * Info collected during iteration of child nodes in order to get the ACK value
566 struct MeshTunnelChildIteratorContext
569 * Tunnel whose info is being collected.
571 struct MeshTunnel *t;
574 * Is this context initialized? Is the value in max_child_ack valid?
579 * Maximum child ACK so far.
581 uint32_t max_child_ack;
584 * Number of children nodes
586 unsigned int nchildren;
591 * Info needed to work with tunnel paths and peers
598 struct MeshTunnel *t;
601 * Neighbouring peer to whom we send the packet to
603 struct MeshPeerInfo *peer;
608 struct MeshPeerPath *path;
613 * Struct containing information about a client of the service
620 struct MeshClient *next;
625 struct MeshClient *prev;
628 * Tunnels that belong to this client, indexed by local id
630 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
633 * Tunnels this client has accepted, indexed by incoming local id
635 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
638 * Tunnels this client has rejected, indexed by incoming local id
640 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
642 * Handle to communicate with the client
644 struct GNUNET_SERVER_Client *handle;
647 * Applications that this client has claimed to provide
649 struct GNUNET_CONTAINER_MultiHashMap *apps;
652 * Messages that this client has declared interest in
654 struct GNUNET_CONTAINER_MultiHashMap *types;
657 * Whether the client is active or shutting down (don't send confirmations
658 * to a client that is shutting down.
663 * ID of the client, mainly for debug messages
668 * Regular expressions describing the services offered by this client.
670 char **regexes; // FIXME add timeout? API to remove a regex?
673 * Number of regular expressions in regexes.
675 unsigned int n_regex;
678 * Task to refresh all regular expresions in the DHT.
680 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
686 * Struct to keep information of searches of services described by a regex
687 * using a user-provided string service description.
689 struct MeshRegexSearchInfo
692 * Which tunnel is this for
694 struct MeshTunnel *t;
697 * User provided description of the searched service.
702 * Part of the description already consumed by the search.
709 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
712 * Results from running DHT GETs.
714 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
717 * Contexts, for each running DHT GET. Free all on end of search.
719 struct MeshRegexSearchContext **contexts;
722 * Number of contexts (branches/steps in search).
724 unsigned int n_contexts;
727 * Peer that is connecting via connect_by_string. When connected, free ctx.
732 * Other peers that are found but not yet being connected to.
734 GNUNET_PEER_Id *peers;
737 * Number of elements in peers.
739 unsigned int n_peers;
742 * Next peer to try to connect to.
747 * Timeout for a connect attempt.
748 * When reached, try to connect to a different peer, if any. If not,
749 * try the same peer again.
751 GNUNET_SCHEDULER_TaskIdentifier timeout;
756 * Struct to keep state of running searches that have consumed a part of
759 struct MeshRegexSearchContext
762 * Part of the description already consumed by
763 * this particular search branch.
768 * Information about the search.
770 struct MeshRegexSearchInfo *info;
774 /******************************************************************************/
775 /************************ DEBUG FUNCTIONS ****************************/
776 /******************************************************************************/
780 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
781 * @param cls string to print
782 * @param success GNUNET_OK if the PUT was transmitted,
783 * GNUNET_NO on timeout,
784 * GNUNET_SYSERR on disconnect from service
785 * after the PUT message was transmitted
786 * (so we don't know if it was received or not)
791 mesh_debug (void *cls, int success)
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
801 /******************************************************************************/
802 /*********************** GLOBAL VARIABLES ****************************/
803 /******************************************************************************/
806 * Configuration parameters
808 static struct GNUNET_TIME_Relative refresh_path_time;
809 static struct GNUNET_TIME_Relative app_announce_time;
810 static struct GNUNET_TIME_Relative id_announce_time;
811 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
812 static struct GNUNET_TIME_Relative connect_timeout;
813 static long long unsigned int default_ttl;
814 static long long unsigned int dht_replication_level;
815 static long long unsigned int max_tunnels;
816 static long long unsigned int max_msgs_queue;
819 * DLL with all the clients, head.
821 static struct MeshClient *clients;
824 * DLL with all the clients, tail.
826 static struct MeshClient *clients_tail;
829 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
831 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
834 * Number of tunnels known.
836 static unsigned long long n_tunnels;
839 * Tunnels incoming, indexed by MESH_TunnelNumber
840 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
842 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
845 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
847 static struct GNUNET_CONTAINER_MultiHashMap *peers;
850 * Handle to communicate with transport
852 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
855 * Handle to communicate with core.
857 static struct GNUNET_CORE_Handle *core_handle;
862 static struct GNUNET_DHT_Handle *dht_handle;
867 static struct GNUNET_SERVER_Handle *server_handle;
870 * Handle to the statistics service.
872 static struct GNUNET_STATISTICS_Handle *stats;
875 * Notification context, to send messages to local clients.
877 static struct GNUNET_SERVER_NotificationContext *nc;
880 * Local peer own ID (memory efficient handle).
882 static GNUNET_PEER_Id myid;
885 * Local peer own ID (full value).
887 static struct GNUNET_PeerIdentity my_full_id;
892 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
897 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
900 * Tunnel ID for the next created tunnel (global tunnel number).
902 static MESH_TunnelNumber next_tid;
905 * Tunnel ID for the next incoming tunnel (local tunnel number).
907 static MESH_TunnelNumber next_local_tid;
910 * All application types provided by this peer.
912 static struct GNUNET_CONTAINER_MultiHashMap *applications;
915 * All message types clients of this peer are interested in.
917 static struct GNUNET_CONTAINER_MultiHashMap *types;
920 * Task to periodically announce provided applications.
922 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
925 * Task to periodically announce itself in the network.
927 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
930 * Next ID to assign to a client.
932 unsigned int next_client_id;
935 /******************************************************************************/
936 /*********************** DECLARATIONS **************************/
937 /******************************************************************************/
939 /* FIXME move declarations here */
942 * Function to process paths received for a new peer addition. The recorded
943 * paths form the initial tunnel, which can be optimized later.
944 * Called on each result obtained for the DHT search.
947 * @param exp when will this value expire
948 * @param key key of the result
949 * @param type type of the result
950 * @param size number of bytes in data
951 * @param data pointer to the result data
954 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
955 const struct GNUNET_HashCode * key,
956 const struct GNUNET_PeerIdentity *get_path,
957 unsigned int get_path_length,
958 const struct GNUNET_PeerIdentity *put_path,
959 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
960 size_t size, const void *data);
964 * Function to process DHT string to regex matching.
965 * Called on each result obtained for the DHT search.
967 * @param cls closure (search context)
968 * @param exp when will this value expire
969 * @param key key of the result
970 * @param get_path path of the get request (not used)
971 * @param get_path_length lenght of get_path (not used)
972 * @param put_path path of the put request (not used)
973 * @param put_path_length length of the put_path (not used)
974 * @param type type of the result
975 * @param size number of bytes in data
976 * @param data pointer to the result data
978 * TODO: re-issue the request after certain time? cancel after X results?
981 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
982 const struct GNUNET_HashCode * key,
983 const struct GNUNET_PeerIdentity *get_path,
984 unsigned int get_path_length,
985 const struct GNUNET_PeerIdentity *put_path,
986 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
987 size_t size, const void *data);
991 * Function to process DHT string to regex matching.
992 * Called on each result obtained for the DHT search.
994 * @param cls closure (search context)
995 * @param exp when will this value expire
996 * @param key key of the result
997 * @param get_path path of the get request (not used)
998 * @param get_path_length lenght of get_path (not used)
999 * @param put_path path of the put request (not used)
1000 * @param put_path_length length of the put_path (not used)
1001 * @param type type of the result
1002 * @param size number of bytes in data
1003 * @param data pointer to the result data
1006 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1007 const struct GNUNET_HashCode * key,
1008 const struct GNUNET_PeerIdentity *get_path,
1009 unsigned int get_path_length,
1010 const struct GNUNET_PeerIdentity *put_path,
1011 unsigned int put_path_length,
1012 enum GNUNET_BLOCK_Type type,
1013 size_t size, const void *data);
1017 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1018 * and insert it in the appropiate structures if the peer is not known yet.
1020 * @param peer Short identity of the peer.
1022 * @return Existing or newly created peer info.
1024 static struct MeshPeerInfo *
1025 peer_info_get_short (const GNUNET_PEER_Id peer);
1029 * Try to establish a new connection to this peer.
1030 * Use the best path for the given tunnel.
1031 * If the peer doesn't have any path to it yet, try to get one.
1032 * If the peer already has some path, send a CREATE PATH towards it.
1034 * @param peer PeerInfo of the peer.
1035 * @param t Tunnel for which to create the path, if possible.
1038 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1042 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1043 * needed rescources.
1044 * If peer already exists, reevaluate shortest path and change if different.
1046 * @param t Tunnel we want to add a new peer to
1047 * @param peer PeerInfo of the peer being added
1051 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1055 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1056 * that are no longer needed, as well as nodes of no longer reachable peers.
1057 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1059 * @param t Tunnel from which to remove the path.
1060 * @param peer Short id of the peer which should be removed.
1063 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1067 * Search for a tunnel by global ID using full PeerIdentities.
1069 * @param oid owner of the tunnel.
1070 * @param tid global tunnel number.
1072 * @return tunnel handler, NULL if doesn't exist.
1074 static struct MeshTunnel *
1075 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1079 * Delete an active client from the tunnel.
1085 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1088 * Notify a tunnel that a connection has broken that affects at least
1089 * some of its peers.
1091 * @param t Tunnel affected.
1092 * @param p1 Peer that got disconnected from p2.
1093 * @param p2 Peer that got disconnected from p1.
1095 * @return Short ID of the peer disconnected (either p1 or p2).
1096 * 0 if the tunnel remained unaffected.
1098 static GNUNET_PEER_Id
1099 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1104 * Get the current ack value for a tunnel, for data going from root to leaves,
1105 * taking in account the tunnel mode and the status of all children and clients.
1109 * @return Maximum PID allowed.
1112 tunnel_get_fwd_ack (struct MeshTunnel *t);
1116 * Add a client to a tunnel, initializing all needed data structures.
1118 * @param t Tunnel to which add the client.
1119 * @param c Client which to add to the tunnel.
1122 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1126 * Iterator over edges in a regex block retrieved from the DHT.
1128 * @param cls Closure.
1129 * @param token Token that follows to next state.
1130 * @param len Lenght of token.
1131 * @param key Hash of next state.
1133 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1136 regex_edge_iterator (void *cls,
1139 const struct GNUNET_HashCode *key);
1143 * Find a path to a peer that offers a regex servcie compatible
1144 * with a given string.
1146 * @param key The key of the accepting state.
1147 * @param ctx Context containing info about the string, tunnel, etc.
1150 regex_find_path (const struct GNUNET_HashCode *key,
1151 struct MeshRegexSearchContext *ctx);
1155 * Queue and pass message to core when possible.
1157 * @param cls Closure (type dependant).
1158 * @param type Type of the message, 0 for a raw message.
1159 * @param size Size of the message.
1160 * @param dst Neighbor to send message to.
1161 * @param t Tunnel this message belongs to.
1164 queue_add (void *cls, uint16_t type, size_t size,
1165 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1169 * Free a transmission that was already queued with all resources
1170 * associated to the request.
1172 * @param queue Queue handler to cancel.
1173 * @param clear_cls Is it necessary to free associated cls?
1176 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1180 * @brief Get the next transmittable message from the queue.
1182 * This will be the head, except in the case of being a data packet
1183 * not allowed by the destination peer.
1185 * @param peer Destination peer.
1187 * @return The next viable MeshPeerQueue element to send to that peer.
1188 * NULL when there are no transmittable messages.
1190 struct MeshPeerQueue *
1191 queue_get_next (const struct MeshPeerInfo *peer);
1195 * Core callback to write a queued packet to core buffer
1197 * @param cls Closure (peer info).
1198 * @param size Number of bytes available in buf.
1199 * @param buf Where the to write the message.
1201 * @return number of bytes written to buf
1204 queue_send (void *cls, size_t size, void *buf);
1206 /******************************************************************************/
1207 /************************ ITERATORS ****************************/
1208 /******************************************************************************/
1211 * Iterator over found existing mesh regex blocks that match an ongoing search.
1213 * @param cls closure
1214 * @param key current key code
1215 * @param value value in the hash map
1216 * @return GNUNET_YES if we should continue to iterate,
1220 regex_result_iterator (void *cls,
1221 const struct GNUNET_HashCode * key,
1224 struct MeshRegexBlock *block = value;
1225 struct MeshRegexSearchContext *ctx = cls;
1227 if (GNUNET_YES == ntohl(block->accepting) &&
1228 ctx->position == strlen (ctx->info->description))
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1231 regex_find_path (key, ctx);
1232 return GNUNET_YES; // We found an accept state!
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1237 ctx->position, strlen(ctx->info->description),
1238 ntohl(block->accepting));
1241 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1242 ®ex_edge_iterator, ctx);
1249 * Iterator over edges in a regex block retrieved from the DHT.
1251 * @param cls Closure (context of the search).
1252 * @param token Token that follows to next state.
1253 * @param len Lenght of token.
1254 * @param key Hash of next state.
1256 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1259 regex_edge_iterator (void *cls,
1262 const struct GNUNET_HashCode *key)
1264 struct MeshRegexSearchContext *ctx = cls;
1265 struct MeshRegexSearchContext *new_ctx;
1266 struct MeshRegexSearchInfo *info = ctx->info;
1267 struct GNUNET_DHT_GetHandle *get_h;
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1274 current = &info->description[ctx->position];
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1276 current_len = strlen (info->description) - ctx->position;
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1281 if (len > current_len)
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1284 return GNUNET_YES; // Token too long, wont match
1286 if (0 != strncmp (current, token, len))
1288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1289 return GNUNET_YES; // Token doesn't match
1291 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1292 new_ctx->info = info;
1293 new_ctx->position = ctx->position + len;
1294 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1296 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1299 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1300 ®ex_result_iterator,
1302 return GNUNET_YES; // We are already looking for it
1304 /* Start search in DHT */
1306 GNUNET_DHT_get_start (dht_handle, /* handle */
1307 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1308 key, /* key to search */
1309 dht_replication_level, /* replication level */
1310 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1311 NULL, /* xquery */ // FIXME BLOOMFILTER
1312 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1313 &dht_get_string_handler, new_ctx);
1315 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1316 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1326 * Iterator over hash map entries to cancel DHT GET requests after a
1327 * successful connect_by_string.
1329 * @param cls Closure (unused).
1330 * @param key Current key code (unused).
1331 * @param value Value in the hash map (get handle).
1332 * @return GNUNET_YES if we should continue to iterate,
1336 regex_cancel_dht_get (void *cls,
1337 const struct GNUNET_HashCode * key,
1340 struct GNUNET_DHT_GetHandle *h = value;
1342 GNUNET_DHT_get_stop (h);
1348 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1349 * search for connect_by_string.
1351 * @param cls Closure (unused).
1352 * @param key Current key code (unused).
1353 * @param value MeshRegexBlock in the hash map.
1354 * @return GNUNET_YES if we should continue to iterate,
1358 regex_free_result (void *cls,
1359 const struct GNUNET_HashCode * key,
1363 GNUNET_free (value);
1369 * Regex callback iterator to store own service description in the DHT.
1371 * @param cls closure.
1372 * @param key hash for current state.
1373 * @param proof proof for current state.
1374 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1375 * @param num_edges number of edges leaving current state.
1376 * @param edges edges leaving current state.
1379 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1380 int accepting, unsigned int num_edges,
1381 const struct GNUNET_REGEX_Edge *edges)
1383 struct MeshRegexBlock *block;
1384 struct MeshRegexEdge *block_edge;
1385 enum GNUNET_DHT_RouteOption opt;
1389 unsigned int offset;
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 " regex dht put for state %s\n",
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1403 if (GNUNET_YES == accepting)
1405 struct MeshRegexAccept block;
1407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1408 " state %s is accepting, putting own id\n",
1410 size = sizeof (block);
1412 block.id = my_full_id;
1414 GNUNET_DHT_put(dht_handle, key,
1415 dht_replication_level,
1416 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1417 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1420 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1425 len = strlen(proof);
1426 size = sizeof (struct MeshRegexBlock) + len;
1427 block = GNUNET_malloc (size);
1430 block->n_proof = htonl (len);
1431 block->n_edges = htonl (num_edges);
1432 block->accepting = htonl (accepting);
1434 /* Store the proof at the end of the block. */
1435 aux = (char *) &block[1];
1436 memcpy (aux, proof, len);
1439 /* Store each edge in a variable length MeshEdge struct at the
1440 * very end of the MeshRegexBlock structure.
1442 for (i = 0; i < num_edges; i++)
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1445 " edge %s towards %s\n",
1447 GNUNET_h2s(&edges[i].destination));
1449 /* aux points at the end of the last block */
1450 len = strlen (edges[i].label);
1451 size += sizeof (struct MeshRegexEdge) + len;
1452 // Calculate offset FIXME is this ok? use size instead?
1453 offset = aux - (char *) block;
1454 block = GNUNET_realloc (block, size);
1455 aux = &((char *) block)[offset];
1456 block_edge = (struct MeshRegexEdge *) aux;
1457 block_edge->key = edges[i].destination;
1458 block_edge->n_token = htonl (len);
1459 aux = (char *) &block_edge[1];
1460 memcpy (aux, edges[i].label, len);
1464 GNUNET_DHT_put(dht_handle, key,
1465 dht_replication_level,
1467 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1469 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1473 GNUNET_free (block);
1478 * Store the regular expression describing a local service into the DHT.
1480 * @param regex The regular expresion.
1483 regex_put (const char *regex)
1485 struct GNUNET_REGEX_Automaton *dfa;
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1488 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1489 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1490 GNUNET_REGEX_automaton_destroy (dfa);
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1496 * Find a path to a peer that offers a regex servcie compatible
1497 * with a given string.
1499 * @param key The key of the accepting state.
1500 * @param ctx Context containing info about the string, tunnel, etc.
1503 regex_find_path (const struct GNUNET_HashCode *key,
1504 struct MeshRegexSearchContext *ctx)
1506 struct GNUNET_DHT_GetHandle *get_h;
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1509 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1510 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1511 key, /* key to search */
1512 dht_replication_level, /* replication level */
1513 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1514 GNUNET_DHT_RO_RECORD_ROUTE,
1515 NULL, /* xquery */ // FIXME BLOOMFILTER
1516 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1517 &dht_get_string_accept_handler, ctx);
1518 GNUNET_break (GNUNET_OK ==
1519 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1522 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1527 * Function called if the connect attempt to a peer found via
1528 * connect_by_string times out. Try to connect to another peer, if any.
1529 * Otherwise try to reconnect to the same peer.
1531 * @param cls Closure (info about regex search).
1532 * @param tc TaskContext.
1535 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1537 struct MeshRegexSearchInfo *info = cls;
1538 struct MeshPeerInfo *peer_info;
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1543 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1544 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1552 if (0 < info->n_peers)
1554 // Select next peer, put current in that spot.
1555 id = info->peers[info->i_peer];
1556 info->peers[info->i_peer] = info->peer;
1557 info->i_peer = (info->i_peer + 1) % info->n_peers;
1561 // Try to connect to same peer again.
1564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1566 peer_info = peer_info_get_short(id);
1567 tunnel_add_peer (info->t, peer_info);
1569 tunnel_delete_peer (info->t, old);
1570 peer_info_connect (peer_info, info->t);
1571 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1572 ®ex_connect_timeout,
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1579 * Cancel an ongoing regex search in the DHT and free all resources.
1581 * @param ctx The search context.
1584 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1586 struct MeshRegexSearchInfo *info = ctx->info;
1589 GNUNET_free (info->description);
1590 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1591 ®ex_cancel_dht_get, NULL);
1592 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1593 ®ex_free_result, NULL);
1594 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1595 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1596 info->t->regex_ctx = NULL;
1597 for (i = 0; i < info->n_contexts; i++)
1599 GNUNET_free (info->contexts[i]);
1601 if (0 < info->n_contexts)
1602 GNUNET_free (info->contexts);
1603 if (0 < info->n_peers)
1604 GNUNET_free (info->peers);
1605 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1607 GNUNET_SCHEDULER_cancel(info->timeout);
1613 /******************************************************************************/
1614 /************************ PERIODIC FUNCTIONS ****************************/
1615 /******************************************************************************/
1618 * Announce iterator over for each application provided by the peer
1620 * @param cls closure
1621 * @param key current key code
1622 * @param value value in the hash map
1623 * @return GNUNET_YES if we should continue to
1628 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1630 struct PBlock block;
1631 struct MeshClient *c;
1633 block.id = my_full_id;
1634 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1635 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1636 if (0 == block.type)
1641 block.type = htonl (block.type);
1643 GNUNET_break (NULL !=
1644 GNUNET_DHT_put (dht_handle, key,
1645 dht_replication_level,
1646 GNUNET_DHT_RO_RECORD_ROUTE |
1647 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1648 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1650 (const char *) &block,
1651 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1653 app_announce_time, NULL, NULL));
1659 * Periodically announce what applications are provided by local clients
1662 * @param cls closure
1663 * @param tc task context
1666 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1668 struct MeshClient *c = cls;
1671 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1673 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1677 DEBUG_DHT ("Starting PUT for regex\n");
1679 for (i = 0; i < c->n_regex; i++)
1681 regex_put (c->regexes[i]);
1683 c->regex_announce_task =
1684 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1685 DEBUG_DHT ("Finished PUT for regex\n");
1692 * Periodically announce what applications are provided by local clients
1695 * @param cls closure
1696 * @param tc task context
1699 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1701 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1703 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1707 DEBUG_DHT ("Starting PUT for apps\n");
1709 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1711 announce_applications_task =
1712 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1714 DEBUG_DHT ("Finished PUT for apps\n");
1721 * Periodically announce self id in the DHT
1723 * @param cls closure
1724 * @param tc task context
1727 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1729 struct PBlock block;
1731 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1733 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1737 * - Set data expiration in function of X
1738 * - Adapt X to churn
1740 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1742 block.id = my_full_id;
1743 block.type = htonl (0);
1744 GNUNET_DHT_put (dht_handle, /* DHT handle */
1745 &my_full_id.hashPubKey, /* Key to use */
1746 dht_replication_level, /* Replication level */
1747 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1748 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1749 sizeof (block), /* Size of the data */
1750 (const char *) &block, /* Data itself */
1751 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1752 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1753 NULL, /* Continuation */
1754 NULL); /* Continuation closure */
1756 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1760 /******************************************************************************/
1761 /****************** GENERAL HELPER FUNCTIONS ************************/
1762 /******************************************************************************/
1765 * Decrements the reference counter and frees all resources if needed
1767 * @param mesh_data Data Descriptor used in a multicast message.
1768 * Freed no longer needed (last message).
1771 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1773 /* Make sure it's a multicast packet */
1774 GNUNET_assert (NULL != mesh_data->reference_counter);
1776 if (0 == --(*(mesh_data->reference_counter)))
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1779 if (NULL != mesh_data->task)
1781 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1783 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1785 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1787 GNUNET_free (mesh_data->task);
1789 GNUNET_free (mesh_data->reference_counter);
1790 GNUNET_free (mesh_data->data);
1791 GNUNET_free (mesh_data);
1797 * Check if client has registered with the service and has not disconnected
1799 * @param client the client to check
1801 * @return non-NULL if client exists in the global DLL
1803 static struct MeshClient *
1804 client_get (struct GNUNET_SERVER_Client *client)
1806 struct MeshClient *c;
1811 if (c->handle == client)
1820 * Checks if a given client has subscribed to certain message type
1822 * @param message_type Type of message to check
1823 * @param c Client to check
1825 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1827 * FIXME: use of crypto_hash slows it down
1828 * The hash function alone takes 8-10us out of the ~55us for the whole
1829 * process of retransmitting the message from one local client to another.
1830 * Find faster implementation!
1833 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1835 struct GNUNET_HashCode hc;
1837 if (NULL == c->types)
1840 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1841 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1846 * Allow a client to send more data after transmitting a multicast message
1847 * which some neighbor has not yet accepted altough a reasonable time has
1850 * @param cls Closure (DataDescriptor containing the task identifier)
1851 * @param tc Task Context
1853 * FIXME reference counter cshould be just int
1856 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1858 struct MeshData *mdata = cls;
1860 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1862 GNUNET_assert (NULL != mdata->reference_counter);
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1865 *(mdata->reference_counter));
1866 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1867 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1872 * Check whether client wants traffic from a tunnel.
1874 * @param c Client to check.
1875 * @param t Tunnel to be found.
1877 * @return GNUNET_YES if client knows tunnel.
1879 * TODO look in client hashmap
1882 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1886 for (i = 0; i < t->nclients; i++)
1887 if (t->clients[i] == c)
1894 * Check whether client has been informed about a tunnel.
1896 * @param c Client to check.
1897 * @param t Tunnel to be found.
1899 * @return GNUNET_YES if client knows tunnel.
1901 * TODO look in client hashmap
1904 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1908 for (i = 0; i < t->nignore; i++)
1909 if (t->ignore[i] == c)
1911 return client_wants_tunnel(c, t);
1916 * Marks a client as uninterested in traffic from the tunnel, updating both
1917 * client and tunnel to reflect this.
1919 * @param c Client that doesn't want traffic anymore.
1920 * @param t Tunnel which should be ignored.
1922 * FIXME when to delete an incoming tunnel?
1925 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1927 struct GNUNET_HashCode hash;
1929 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1930 GNUNET_break (GNUNET_YES ==
1931 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1933 GNUNET_break (GNUNET_YES ==
1934 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1935 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1936 tunnel_delete_active_client (t, c);
1937 GNUNET_array_append (t->ignore, t->nignore, c);
1942 * Deletes a tunnel from a client (either owner or destination). To be used on
1943 * tunnel destroy, otherwise, use client_ignore_tunnel.
1945 * @param c Client whose tunnel to delete.
1946 * @param t Tunnel which should be deleted.
1949 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1951 struct GNUNET_HashCode hash;
1955 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1956 GNUNET_assert (GNUNET_YES ==
1957 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1963 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1965 GNUNET_assert (GNUNET_YES ==
1966 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1970 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1978 * Send the message to all clients that have subscribed to its type
1980 * @param msg Pointer to the message itself
1981 * @param payload Pointer to the payload of the message.
1982 * @param t The tunnel to whose clients this message goes.
1984 * @return number of clients this message was sent to
1987 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1988 const struct GNUNET_MessageHeader *payload,
1989 struct MeshTunnel *t)
1991 struct MeshClient *c;
1992 MESH_TunnelNumber *tid;
1995 char cbuf[htons (msg->size)];
1997 type = ntohs (payload->type);
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2000 GNUNET_MESH_DEBUG_M2S (type));
2002 memcpy (cbuf, msg, sizeof (cbuf));
2003 switch (htons (msg->type))
2005 struct GNUNET_MESH_Unicast *uc;
2006 struct GNUNET_MESH_Multicast *mc;
2007 struct GNUNET_MESH_ToOrigin *to;
2009 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2010 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2013 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2014 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2017 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2018 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2026 for (count = 0, c = clients; c != NULL; c = c->next)
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2029 if (client_is_subscribed (type, c))
2031 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2035 *tid = htonl (t->local_tid);
2039 if (GNUNET_NO == client_knows_tunnel (c, t))
2041 /* This client doesn't know the tunnel */
2042 struct GNUNET_MESH_TunnelNotification tmsg;
2043 struct GNUNET_HashCode hash;
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2046 tmsg.header.size = htons (sizeof (tmsg));
2047 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2048 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2049 tmsg.tunnel_id = htonl (t->local_tid_dest);
2050 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2051 &tmsg.header, GNUNET_NO);
2052 tunnel_add_client (t, c);
2053 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2055 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2056 c->incoming_tunnels, &hash, t,
2057 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2059 *tid = htonl (t->local_tid_dest);
2062 /* Check if the client wants to get traffic from the tunnel */
2063 if (GNUNET_NO == client_wants_tunnel(c, t))
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2067 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2068 (struct GNUNET_MessageHeader
2069 *) cbuf, GNUNET_NO);
2078 * Notify the client that owns the tunnel that a peer has connected to it
2079 * (the requested path to it has been confirmed).
2081 * @param t Tunnel whose owner to notify
2082 * @param id Short id of the peer that has connected
2085 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2087 struct GNUNET_MESH_PeerControl pc;
2089 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2090 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2091 pc.tunnel_id = htonl (t->local_tid);
2092 GNUNET_PEER_resolve (id, &pc.peer);
2093 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2099 * Notify all clients (not depending on registration status) that the incoming
2100 * tunnel is no longer valid.
2102 * @param t Tunnel that was destroyed.
2105 send_clients_tunnel_destroy (struct MeshTunnel *t)
2107 struct GNUNET_MESH_TunnelMessage msg;
2109 msg.header.size = htons (sizeof (msg));
2110 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2111 msg.tunnel_id = htonl (t->local_tid_dest);
2112 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2117 * Notify clients of tunnel disconnections, if needed.
2118 * In case the origin disconnects, the destination clients get a tunnel destroy
2119 * notification. If the last destination disconnects (only one remaining client
2120 * in tunnel), the origin gets a (local ID) peer disconnected.
2121 * Note that the function must be called BEFORE removing the client from
2124 * @param t Tunnel that was destroyed.
2125 * @param c Client that disconnected.
2128 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2134 struct GNUNET_MESH_TunnelMessage msg;
2136 msg.header.size = htons (sizeof (msg));
2137 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2138 msg.tunnel_id = htonl (t->local_tid_dest);
2139 for (i = 0; i < t->nclients; i++)
2140 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2141 &msg.header, GNUNET_NO);
2143 // FIXME when to disconnect an incoming tunnel?
2144 else if (1 == t->nclients && NULL != t->owner)
2146 struct GNUNET_MESH_PeerControl msg;
2148 msg.header.size = htons (sizeof (msg));
2149 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2150 msg.tunnel_id = htonl (t->local_tid);
2151 msg.peer = my_full_id;
2152 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2153 &msg.header, GNUNET_NO);
2159 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2160 * and insert it in the appropiate structures if the peer is not known yet.
2162 * @param peer Full identity of the peer.
2164 * @return Existing or newly created peer info.
2166 static struct MeshPeerInfo *
2167 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2169 struct MeshPeerInfo *peer_info;
2171 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2172 if (NULL == peer_info)
2175 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2176 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2177 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2178 peer_info->id = GNUNET_PEER_intern (peer);
2186 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2187 * and insert it in the appropiate structures if the peer is not known yet.
2189 * @param peer Short identity of the peer.
2191 * @return Existing or newly created peer info.
2193 static struct MeshPeerInfo *
2194 peer_info_get_short (const GNUNET_PEER_Id peer)
2196 struct GNUNET_PeerIdentity id;
2198 GNUNET_PEER_resolve (peer, &id);
2199 return peer_info_get (&id);
2204 * Iterator to remove the tunnel from the list of tunnels a peer participates
2207 * @param cls Closure (tunnel info)
2208 * @param key GNUNET_PeerIdentity of the peer (unused)
2209 * @param value PeerInfo of the peer
2211 * @return always GNUNET_YES, to keep iterating
2214 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2216 struct MeshTunnel *t = cls;
2217 struct MeshPeerInfo *peer = value;
2220 for (i = 0; i < peer->ntunnels; i++)
2223 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2226 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2227 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2236 * Core callback to write a pre-constructed data packet to core buffer
2238 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2239 * @param size Number of bytes available in buf.
2240 * @param buf Where the to write the message.
2242 * @return number of bytes written to buf
2245 send_core_data_raw (void *cls, size_t size, void *buf)
2247 struct MeshTransmissionDescriptor *info = cls;
2248 struct GNUNET_MessageHeader *msg;
2251 GNUNET_assert (NULL != info);
2252 GNUNET_assert (NULL != info->mesh_data);
2253 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2254 total_size = ntohs (msg->size);
2256 if (total_size > size)
2261 memcpy (buf, msg, total_size);
2262 data_descriptor_decrement_rc (info->mesh_data);
2269 * Sends an already built non-multicast message to a peer,
2270 * properly registrating all used resources.
2272 * @param message Message to send. Function makes a copy of it.
2273 * @param peer Short ID of the neighbor whom to send the message.
2274 * @param t Tunnel on which this message is transmitted.
2277 send_message (const struct GNUNET_MessageHeader *message,
2278 const struct GNUNET_PeerIdentity *peer,
2279 struct MeshTunnel *t)
2281 struct MeshTransmissionDescriptor *info;
2282 struct MeshPeerInfo *neighbor;
2283 struct MeshPeerPath *p;
2287 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2289 size = ntohs (message->size);
2290 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2291 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2292 info->mesh_data->data = GNUNET_malloc (size);
2293 memcpy (info->mesh_data->data, message, size);
2294 type = ntohs(message->type);
2295 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2297 struct GNUNET_MESH_Unicast *m;
2299 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2300 m->ttl = htonl (ntohl (m->ttl) - 1);
2302 info->mesh_data->data_len = size;
2303 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2304 *info->mesh_data->reference_counter = 1;
2305 neighbor = peer_info_get (peer);
2306 for (p = neighbor->path_head; NULL != p; p = p->next)
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2317 " %s IS NOT DIRECTLY CONNECTED\n",
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 for (p = neighbor->path_head; NULL != p; p = p->next)
2324 struct GNUNET_PeerIdentity debug_id;
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2328 " path with %u hops through:\n",
2330 for (i = 0; i < p->length; i++)
2332 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 i, GNUNET_i2s(&debug_id));
2339 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2340 GNUNET_free (info->mesh_data->data);
2341 GNUNET_free (info->mesh_data);
2345 info->peer = neighbor;
2346 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2349 * TODO: in this case we only need the service to retransmit
2350 * the message down the path. If we pass the real type to queue_add,
2351 * queue_send will try to build the message from scratch. This can
2352 * probably be done by some other way instead of deleteing the type
2366 * Sends a CREATE PATH message for a path to a peer, properly registrating
2367 * all used resources.
2369 * @param peer PeerInfo of the final peer for whom this path is being created.
2370 * @param p Path itself.
2371 * @param t Tunnel for which the path is created.
2374 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2375 struct MeshTunnel *t)
2377 struct GNUNET_PeerIdentity id;
2378 struct MeshPathInfo *path_info;
2379 struct MeshPeerInfo *neighbor;
2385 p = tree_get_path_to_peer (t->tree, peer->id);
2392 for (i = 0; i < p->length; i++)
2394 if (p->peers[i] == myid)
2397 if (i >= p->length - 1)
2403 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2405 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2406 path_info->path = p;
2408 neighbor = peer_info_get (&id);
2409 path_info->peer = neighbor;
2410 queue_add (path_info,
2411 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2412 sizeof (struct GNUNET_MESH_ManipulatePath) +
2413 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2420 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2422 * @param t Tunnel whose path to destroy.
2423 * @param destination Short ID of the peer to whom the path to destroy.
2426 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2428 struct MeshPeerPath *p;
2431 p = tree_get_path_to_peer (t->tree, destination);
2437 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2438 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2440 struct GNUNET_MESH_ManipulatePath *msg;
2441 struct GNUNET_PeerIdentity *pi;
2445 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2446 msg->header.size = htons (size);
2447 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2448 msg->tid = htonl (t->id.tid);
2449 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2450 for (i = 0; i < p->length; i++)
2452 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2454 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2461 * Try to establish a new connection to this peer.
2462 * Use the best path for the given tunnel.
2463 * If the peer doesn't have any path to it yet, try to get one.
2464 * If the peer already has some path, send a CREATE PATH towards it.
2466 * @param peer PeerInfo of the peer.
2467 * @param t Tunnel for which to create the path, if possible.
2470 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2472 struct MeshPeerPath *p;
2473 struct MeshPathInfo *path_info;
2475 if (NULL != peer->path_head)
2477 p = tree_get_path_to_peer (t->tree, peer->id);
2484 // FIXME always send create path to self
2487 send_create_path (peer, p, t);
2491 struct GNUNET_HashCode hash;
2494 send_client_peer_connected (t, myid);
2495 t->local_tid_dest = next_local_tid++;
2496 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2499 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2500 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2507 else if (NULL == peer->dhtget)
2509 struct GNUNET_PeerIdentity id;
2511 GNUNET_PEER_resolve (peer->id, &id);
2512 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2513 path_info->peer = peer;
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2517 peer->dhtgetcls = path_info;
2518 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2519 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2520 &id.hashPubKey, /* key to search */
2521 dht_replication_level, /* replication level */
2522 GNUNET_DHT_RO_RECORD_ROUTE |
2523 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2524 NULL, /* xquery */ // FIXME BLOOMFILTER
2525 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2526 &dht_get_id_handler, path_info);
2528 /* Otherwise, there is no path but the DHT get is already started. */
2533 * Task to delay the connection of a peer
2535 * @param cls Closure (path info with tunnel and peer to connect).
2536 * Will be free'd on exection.
2537 * @param tc TaskContext
2540 peer_info_connect_task (void *cls,
2541 const struct GNUNET_SCHEDULER_TaskContext *tc)
2543 struct MeshPathInfo *path_info = cls;
2545 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2550 peer_info_connect (path_info->peer, path_info->t);
2556 * Destroy the peer_info and free any allocated resources linked to it
2558 * @param pi The peer_info to destroy.
2560 * @return GNUNET_OK on success
2563 peer_info_destroy (struct MeshPeerInfo *pi)
2565 struct GNUNET_PeerIdentity id;
2566 struct MeshPeerPath *p;
2567 struct MeshPeerPath *nextp;
2569 GNUNET_PEER_resolve (pi->id, &id);
2570 GNUNET_PEER_change_rc (pi->id, -1);
2573 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2577 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2579 if (NULL != pi->dhtget)
2581 GNUNET_DHT_get_stop (pi->dhtget);
2582 GNUNET_free (pi->dhtgetcls);
2588 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2598 * Remove all paths that rely on a direct connection between p1 and p2
2599 * from the peer itself and notify all tunnels about it.
2601 * @param peer PeerInfo of affected peer.
2602 * @param p1 GNUNET_PEER_Id of one peer.
2603 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2606 * TODO: optimize (see below)
2609 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2612 struct MeshPeerPath *p;
2613 struct MeshPeerPath *aux;
2614 struct MeshPeerInfo *peer_d;
2616 unsigned int destroyed;
2621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2623 p = peer->path_head;
2627 for (i = 0; i < (p->length - 1); i++)
2629 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2630 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2632 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2643 for (i = 0; i < peer->ntunnels; i++)
2645 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2649 * Problem: one or more peers have been deleted from the tunnel tree.
2650 * We don't know who they are to try to add them again.
2651 * We need to try to find a new path for each of the disconnected peers.
2652 * Some of them might already have a path to reach them that does not
2653 * involve p1 and p2. Adding all anew might render in a better tree than
2654 * the trivial immediate fix.
2656 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2657 * its children will be reachable trough him.
2659 peer_d = peer_info_get_short (d);
2662 for (p = peer_d->path_head; NULL != p; p = p->next)
2664 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2672 /* No callback, as peer will be already disconnected and a connection
2673 * scheduled by tunnel_notify_connection_broken.
2675 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2679 peer_info_connect (peer_d, peer->tunnels[i]);
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2687 * Add the path to the peer and update the path used to reach it in case this
2690 * @param peer_info Destination peer to add the path to.
2691 * @param path New path to add. Last peer must be the peer in arg 1.
2692 * Path will be either used of freed if already known.
2693 * @param trusted Do we trust that this path is real?
2696 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2699 struct MeshPeerPath *aux;
2703 if ((NULL == peer_info) || (NULL == path))
2706 path_destroy (path);
2709 if (path->peers[path->length - 1] != peer_info->id)
2712 path_destroy (path);
2715 if (path->length <= 2 && GNUNET_NO == trusted)
2717 /* Only allow CORE to tell us about direct paths */
2718 path_destroy (path);
2721 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2722 for (l = 1; l < path->length; l++)
2724 if (path->peers[l] == myid)
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2727 for (l2 = 0; l2 < path->length - l; l2++)
2729 path->peers[l2] = path->peers[l + l2];
2734 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2739 struct GNUNET_PeerIdentity id;
2741 GNUNET_PEER_resolve (peer_info->id, &id);
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2743 path->length, GNUNET_i2s (&id));
2746 l = path_get_length (path);
2753 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2754 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2756 l2 = path_get_length (aux);
2759 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2760 peer_info->path_tail, aux, path);
2765 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2767 path_destroy (path);
2772 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2779 * Add the path to the origin peer and update the path used to reach it in case
2780 * this is the shortest.
2781 * The path is given in peer_info -> destination, therefore we turn the path
2782 * upside down first.
2784 * @param peer_info Peer to add the path to, being the origin of the path.
2785 * @param path New path to add after being inversed.
2786 * @param trusted Do we trust that this path is real?
2789 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2790 struct MeshPeerPath *path, int trusted)
2793 peer_info_add_path (peer_info, path, trusted);
2798 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2799 * to obtain a local peer -> destination path and interning the peer ids.
2801 * @return Newly allocated and created path
2803 static struct MeshPeerPath *
2804 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2805 unsigned int get_path_length,
2806 const struct GNUNET_PeerIdentity *put_path,
2807 unsigned int put_path_length)
2809 struct MeshPeerPath *p;
2815 GNUNET_PEER_change_rc (myid, 1);
2816 i = get_path_length;
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2818 for (i--; i >= 0; i--)
2820 id = GNUNET_PEER_intern (&get_path[i]);
2821 if (p->length > 0 && id == p->peers[p->length - 1])
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2824 GNUNET_PEER_change_rc (id, -1);
2828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2829 GNUNET_i2s (&get_path[i]));
2831 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2832 p->peers[p->length - 1] = id;
2835 i = put_path_length;
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2837 for (i--; i >= 0; i--)
2839 id = GNUNET_PEER_intern (&put_path[i]);
2842 /* PUT path went through us, so discard the path up until now and start
2843 * from here to get a much shorter (and loop-free) path.
2848 if (p->length > 0 && id == p->peers[p->length - 1])
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2851 GNUNET_PEER_change_rc (id, -1);
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2856 GNUNET_i2s (&put_path[i]));
2858 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2859 p->peers[p->length - 1] = id;
2863 if (get_path_length > 0)
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2865 GNUNET_i2s (&get_path[0]));
2866 if (put_path_length > 0)
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2868 GNUNET_i2s (&put_path[0]));
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2871 for (i = 0; i < p->length; i++)
2873 struct GNUNET_PeerIdentity peer_id;
2875 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2877 GNUNET_i2s (&peer_id));
2885 * Adds a path to the peer_infos of all the peers in the path
2887 * @param p Path to process.
2888 * @param confirmed Whether we know if the path works or not.
2891 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2895 /* TODO: invert and add */
2896 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2897 for (i++; i < p->length; i++)
2899 struct MeshPeerInfo *aux;
2900 struct MeshPeerPath *copy;
2902 aux = peer_info_get_short (p->peers[i]);
2903 copy = path_duplicate (p);
2904 copy->length = i + 1;
2905 peer_info_add_path (aux, copy, GNUNET_NO);
2911 * Send keepalive packets for a peer
2913 * @param cls Closure (tunnel for which to send the keepalive).
2914 * @param tc Notification context.
2916 * TODO: implement explicit multicast keepalive?
2919 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2923 * Search for a tunnel among the incoming tunnels
2925 * @param tid the local id of the tunnel
2927 * @return tunnel handler, NULL if doesn't exist
2929 static struct MeshTunnel *
2930 tunnel_get_incoming (MESH_TunnelNumber tid)
2932 struct GNUNET_HashCode hash;
2934 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2935 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2936 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2941 * Search for a tunnel among the tunnels for a client
2943 * @param c the client whose tunnels to search in
2944 * @param tid the local id of the tunnel
2946 * @return tunnel handler, NULL if doesn't exist
2948 static struct MeshTunnel *
2949 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2951 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2953 return tunnel_get_incoming (tid);
2957 struct GNUNET_HashCode hash;
2959 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2960 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2966 * Search for a tunnel by global ID using PEER_ID
2968 * @param pi owner of the tunnel
2969 * @param tid global tunnel number
2971 * @return tunnel handler, NULL if doesn't exist
2973 static struct MeshTunnel *
2974 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2976 struct MESH_TunnelID id;
2977 struct GNUNET_HashCode hash;
2982 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2983 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2988 * Search for a tunnel by global ID using full PeerIdentities
2990 * @param oid owner of the tunnel
2991 * @param tid global tunnel number
2993 * @return tunnel handler, NULL if doesn't exist
2995 static struct MeshTunnel *
2996 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2998 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3003 * Delete an active client from the tunnel.
3009 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3013 for (i = 0; i < t->nclients; i++)
3015 if (t->clients[i] == c)
3017 t->clients[i] = t->clients[t->nclients - 1];
3018 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3019 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3021 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3029 * Delete an ignored client from the tunnel.
3035 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3039 for (i = 0; i < t->nignore; i++)
3041 if (t->ignore[i] == c)
3043 t->ignore[i] = t->ignore[t->nignore - 1];
3044 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3052 * Delete a client from the tunnel. It should be only done on
3053 * client disconnection, otherwise use client_ignore_tunnel.
3059 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3061 tunnel_delete_ignored_client (t, c);
3062 tunnel_delete_active_client (t, c);
3067 * Iterator to free MeshTunnelChildInfo of tunnel children.
3069 * @param cls Closure (tunnel info).
3070 * @param key Hash of GNUNET_PEER_Id (unused).
3071 * @param value MeshTunnelChildInfo of the child.
3073 * @return always GNUNET_YES, to keep iterating
3076 tunnel_destroy_child (void *cls,
3077 const struct GNUNET_HashCode * key,
3080 GNUNET_free (value);
3086 * Callback used to notify a client owner of a tunnel that a peer has
3087 * disconnected, most likely because of a path change.
3089 * @param cls Closure (tunnel this notification is about).
3090 * @param peer_id Short ID of disconnected peer.
3093 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3095 struct MeshTunnel *t = cls;
3096 struct MeshPeerInfo *peer;
3097 struct MeshPathInfo *path_info;
3099 if (NULL != t->owner && NULL != nc)
3101 struct GNUNET_MESH_PeerControl msg;
3103 msg.header.size = htons (sizeof (msg));
3104 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3105 msg.tunnel_id = htonl (t->local_tid);
3106 GNUNET_PEER_resolve (peer_id, &msg.peer);
3107 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3108 &msg.header, GNUNET_NO);
3110 peer = peer_info_get_short (peer_id);
3111 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3112 path_info->peer = peer;
3114 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3119 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3120 * needed rescources.
3121 * If peer already exists, reevaluate shortest path and change if different.
3123 * @param t Tunnel we want to add a new peer to
3124 * @param peer PeerInfo of the peer being added
3128 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3130 struct GNUNET_PeerIdentity id;
3131 struct MeshPeerPath *best_p;
3132 struct MeshPeerPath *p;
3133 unsigned int best_cost;
3136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3137 GNUNET_PEER_resolve (peer->id, &id);
3139 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3142 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3143 GNUNET_assert (GNUNET_OK ==
3144 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3146 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3149 if (NULL != (p = peer->path_head))
3152 best_cost = tree_get_path_cost (t->tree, p);
3155 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3162 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3163 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3164 t->path_refresh_task =
3165 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3169 /* Start a DHT get */
3170 peer_info_connect (peer, t);
3172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3176 * Add a path to a tunnel which we don't own, just to remember the next hop.
3177 * If destination node was already in the tunnel, the first hop information
3178 * will be replaced with the new path.
3180 * @param t Tunnel we want to add a new peer to
3181 * @param p Path to add
3182 * @param own_pos Position of local node in path.
3186 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3187 unsigned int own_pos)
3189 struct GNUNET_PeerIdentity id;
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3192 GNUNET_assert (0 != own_pos);
3193 tree_add_path (t->tree, p, NULL, NULL);
3194 if (own_pos < p->length - 1)
3196 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3197 tree_update_first_hops (t->tree, myid, &id);
3199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3203 * Add a client to a tunnel, initializing all needed data structures.
3205 * @param t Tunnel to which add the client.
3206 * @param c Client which to add to the tunnel.
3209 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3211 struct MeshTunnelClientInfo clinfo;
3213 GNUNET_array_append (t->clients, t->nclients, c);
3214 clinfo.fwd_ack = t->fwd_pid + 1;
3215 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3216 clinfo.fwd_pid = t->fwd_pid;
3217 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3219 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3224 * Notifies a tunnel that a connection has broken that affects at least
3225 * some of its peers. Sends a notification towards the root of the tree.
3226 * In case the peer is the owner of the tree, notifies the client that owns
3227 * the tunnel and tries to reconnect.
3229 * @param t Tunnel affected.
3230 * @param p1 Peer that got disconnected from p2.
3231 * @param p2 Peer that got disconnected from p1.
3233 * @return Short ID of the peer disconnected (either p1 or p2).
3234 * 0 if the tunnel remained unaffected.
3236 static GNUNET_PEER_Id
3237 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3243 tree_notify_connection_broken (t->tree, p1, p2,
3244 &tunnel_notify_client_peer_disconnected,
3246 if (myid != p1 && myid != p2)
3252 if (tree_get_predecessor (t->tree) != 0)
3254 /* We are the peer still connected, notify owner of the disconnection. */
3255 struct GNUNET_MESH_PathBroken msg;
3256 struct GNUNET_PeerIdentity neighbor;
3258 msg.header.size = htons (sizeof (msg));
3259 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3260 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3261 msg.tid = htonl (t->id.tid);
3262 msg.peer1 = my_full_id;
3263 GNUNET_PEER_resolve (pid, &msg.peer2);
3264 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3265 send_message (&msg.header, &neighbor, t);
3273 * Send a multicast packet to a neighbor.
3275 * @param cls Closure (Info about the multicast packet)
3276 * @param neighbor_id Short ID of the neighbor to send the packet to.
3279 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3281 struct MeshData *mdata = cls;
3282 struct MeshTransmissionDescriptor *info;
3283 struct GNUNET_PeerIdentity neighbor;
3285 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3287 info->mesh_data = mdata;
3288 (*(mdata->reference_counter)) ++;
3289 info->destination = neighbor_id;
3290 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3292 GNUNET_i2s (&neighbor));
3293 info->peer = peer_info_get (&neighbor);
3294 GNUNET_assert (NULL != info->peer);
3296 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3297 info->mesh_data->data_len,
3304 * Queue a message in a tunnel in multicast, sending a copy to each child node
3305 * down the local one in the tunnel tree.
3307 * @param t Tunnel in which to send the data.
3308 * @param msg Message to be sent.
3309 * @param internal Has the service generated this message?
3312 tunnel_send_multicast (struct MeshTunnel *t,
3313 const struct GNUNET_MessageHeader *msg,
3316 struct MeshData *mdata;
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3319 " sending a multicast packet...\n");
3321 mdata = GNUNET_malloc (sizeof (struct MeshData));
3322 mdata->data_len = ntohs (msg->size);
3323 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3325 mdata->data = GNUNET_malloc (mdata->data_len);
3326 memcpy (mdata->data, msg, mdata->data_len);
3327 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3329 struct GNUNET_MESH_Multicast *mcast;
3331 if (t->fwd_queue_n >= t->fwd_queue_max)
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " queue full!\n");
3335 GNUNET_free (mdata->data);
3336 GNUNET_free (mdata->reference_counter);
3337 GNUNET_free (mdata);
3341 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3342 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3344 ntohl (mcast->ttl));
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3350 if (NULL != t->owner &&
3351 GNUNET_YES != t->owner->shutting_down &&
3352 GNUNET_NO == internal)
3354 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3356 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3362 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3363 if (*(mdata->reference_counter) == 0)
3365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3366 " no one to send data to\n");
3367 GNUNET_free (mdata->data);
3368 GNUNET_free (mdata->reference_counter);
3369 if (NULL != mdata->task)
3371 GNUNET_SCHEDULER_cancel(*(mdata->task));
3372 GNUNET_free (mdata->task);
3373 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3375 GNUNET_free (mdata);
3378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3379 " sending a multicast packet done\n");
3385 * Increase the SKIP value of all peers that
3386 * have not received a unicast message.
3388 * @param cls Closure (ID of the peer that HAS received the message).
3389 * @param key ID of the neighbor.
3390 * @param value Information about the neighbor.
3392 * @return GNUNET_YES to keep iterating.
3395 tunnel_add_skip (void *cls,
3396 const struct GNUNET_HashCode * key,
3399 struct GNUNET_PeerIdentity *neighbor = cls;
3400 struct MeshTunnelChildInfo *cinfo = value;
3402 /* TODO compare only pointers? key == neighbor? */
3403 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3413 * @brief Get neighbor's Flow Control information.
3415 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3416 * descendant of the local node in a certain tunnel.
3417 * If the info is not yet there (recently created path), creates the data struct
3418 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3421 * @param t Tunnel related.
3422 * @param peer Neighbor whose Flow Control info is needed.
3424 * @return Neighbor's Flow Control info.
3426 static struct MeshTunnelChildInfo *
3427 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3428 const struct GNUNET_PeerIdentity *peer)
3430 struct MeshTunnelChildInfo *cinfo;
3431 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3437 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3438 cinfo->id = GNUNET_PEER_intern (peer);
3439 cinfo->skip = t->fwd_pid;
3441 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3442 cinfo->fwd_ack = t->fwd_pid + delta;
3443 cinfo->bck_ack = delta;
3445 cinfo->send_buffer =
3446 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3448 GNUNET_assert (GNUNET_OK ==
3449 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3452 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3459 * Get the Flow Control info of a client.
3461 * @param t Tunnel on which to look.
3462 * @param c Client whose ACK to get.
3464 * @return ACK value.
3466 static struct MeshTunnelClientInfo *
3467 tunnel_get_client_fc (struct MeshTunnel *t,
3468 struct MeshClient *c)
3472 for (i = 0; i < t->nclients; i++)
3474 if (t->clients[i] != c)
3476 return &t->clients_fc[i];
3479 return NULL; // avoid compiler / coverity complaints
3484 * Iterator to get the appropiate ACK value from all children nodes.
3486 * @param cls Closue (tunnel).
3487 * @param id Id of the child node.
3490 tunnel_get_child_fwd_ack (void *cls,
3493 struct GNUNET_PeerIdentity peer_id;
3494 struct MeshTunnelChildInfo *cinfo;
3495 struct MeshTunnelChildIteratorContext *ctx = cls;
3496 struct MeshTunnel *t = ctx->t;
3499 GNUNET_PEER_resolve (id, &peer_id);
3500 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3501 ack = cinfo->fwd_ack;
3504 if (GNUNET_NO == ctx->init)
3506 ctx->max_child_ack = ack;
3507 ctx->init = GNUNET_YES;
3510 if (GNUNET_YES == t->speed_min)
3512 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3516 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3523 * Get the maximum PID allowed to transmit to any
3524 * tunnel child of the local peer, depending on the tunnel
3525 * buffering/speed settings.
3529 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3532 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3534 struct MeshTunnelChildIteratorContext ctx;
3536 ctx.max_child_ack = 0;
3538 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3540 if (0 == ctx.nchildren)
3542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3543 " tunnel has no children, no FWD ACK\n");
3547 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3548 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3550 return (int64_t) ctx.max_child_ack;
3555 * Set the FWD ACK value of a client in a particular tunnel.
3557 * @param t Tunnel affected.
3558 * @param c Client whose ACK to set.
3559 * @param ack ACK value.
3562 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3563 struct MeshClient *c,
3568 for (i = 0; i < t->nclients; i++)
3570 if (t->clients[i] != c)
3572 t->clients_fc[i].fwd_ack = ack;
3580 * Get the highest ACK value of all clients in a particular tunnel,
3581 * according to the buffering/speed settings.
3583 * @param t Tunnel on which to look.
3585 * @return Corresponding ACK value (max uint32_t).
3586 * If no clients are suscribed, -1.
3589 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3594 if (0 == t->nclients)
3596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3597 " tunnel has no clients, no FWD ACK\n");
3601 for (ack = -1, i = 0; i < t->nclients; i++)
3604 (GNUNET_YES == t->speed_min &&
3605 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3606 (GNUNET_NO == t->speed_min &&
3607 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3609 ack = t->clients_fc[i].fwd_ack;
3613 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3614 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3616 return (uint32_t) ack;
3621 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3622 * mode and the status of all children nodes.
3626 * @return Maximum PID allowed.
3629 tunnel_get_fwd_ack (struct MeshTunnel *t)
3632 uint32_t buffer_free;
3637 count = t->fwd_pid - t->skip;
3638 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3639 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3640 child_ack = tunnel_get_children_fwd_ack (t);
3641 client_ack = tunnel_get_clients_fwd_ack (t);
3642 if (-1 == child_ack)
3644 // Node has no children, child_ack AND core buffer are irrelevant.
3645 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good!
3646 return (uint32_t) client_ack;
3649 if (GNUNET_YES == t->speed_min)
3651 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3652 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3656 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3657 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3659 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3660 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "c %u, bf %u, ch %u, cl %u, ACK: %u\n",
3662 count, buffer_free, child_ack, client_ack, ack);
3668 * Build a local ACK message and send it to a local client.
3670 * @param t Tunnel on which to send the ACK.
3671 * @param c Client to whom send the ACK.
3672 * @param ack Value of the ACK.
3675 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3677 struct GNUNET_MESH_LocalAck msg;
3679 msg.header.size = htons (sizeof (msg));
3680 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3681 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3682 msg.max_pid = htonl (ack);
3683 GNUNET_SERVER_notification_context_unicast(nc,
3690 * Build an ACK message and queue it to send to the given peer.
3692 * @param t Tunnel on which to send the ACK.
3693 * @param peer Peer to whom send the ACK.
3694 * @param ack Value of the ACK.
3697 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3699 struct GNUNET_MESH_ACK msg;
3701 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3702 msg.header.size = htons (sizeof (msg));
3703 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3704 msg.pid = htonl (ack);
3705 msg.tid = htonl (t->id.tid);
3707 send_message (&msg.header, peer, t);
3712 * Notify a the owner of a tunnel about how many more
3713 * payload packages will we accept on a given tunnel.
3715 * @param t Tunnel on which to send the ACK.
3718 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3723 "Sending client FWD ACK on tunnel %X\n",
3726 ack = tunnel_get_fwd_ack (t);
3728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3729 if (t->last_fwd_ack == ack)
3732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3733 t->last_fwd_ack = ack;
3734 send_local_ack (t, t->owner, ack);
3739 * Send an ACK informing the predecessor about the available buffer space.
3740 * In case there is no predecessor, inform the owning client.
3741 * If buffering is off, send only on behalf of children or self if endpoint.
3742 * If buffering is on, send when sent to children and buffer space is free.
3743 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3744 * the ACK itself goes "back" (towards root).
3746 * @param t Tunnel on which to send the ACK.
3747 * @param type Type of message that triggered the ACK transmission.
3750 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3752 struct GNUNET_PeerIdentity id;
3755 if (NULL != t->owner)
3757 tunnel_send_client_fwd_ack (t);
3760 /* Is it after unicast / multicast retransmission? */
3763 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3764 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3766 "ACK due to FWD DATA retransmission\n");
3767 if (GNUNET_YES == t->nobuffer)
3769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3772 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3773 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3777 " t->qmax: %u, t->qn: %u\n",
3778 t->fwd_queue_max, t->fwd_queue_n);
3779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3780 " t->pid: %u, t->ack: %u\n",
3781 t->fwd_pid, t->last_fwd_ack);
3785 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3786 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3792 /* Ok, ACK might be necessary, what PID to ACK? */
3793 ack = tunnel_get_fwd_ack (t);
3795 /* If speed_min and not all children have ack'd, dont send yet */
3796 if (ack == t->last_fwd_ack)
3798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3802 t->last_fwd_ack = ack;
3803 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3804 send_ack (t, &id, ack);
3809 * Iterator to send a child node a BCK ACK to allow him to send more
3812 * @param cls Closure (tunnel).
3813 * @param id Id of the child node.
3816 tunnel_send_child_bck_ack (void *cls,
3819 struct MeshTunnel *t = cls;
3820 struct MeshTunnelChildInfo *cinfo;
3821 struct GNUNET_PeerIdentity peer;
3823 GNUNET_PEER_resolve (id, &peer);
3824 cinfo = tunnel_get_neighbor_fc (t, &peer);
3826 if (cinfo->bck_ack != cinfo->pid &&
3827 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3831 send_ack (t, &peer, cinfo->bck_ack);
3836 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3838 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3840 * @param t Tunnel on which to send the BCK ACKs.
3843 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3846 unsigned int tunnel_delta;
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3850 tunnel_delta = t->bck_ack - t->bck_pid;
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3853 /* Find client whom to allow to send to origin (with lowest buffer space) */
3854 for (i = 0; i < t->nclients; i++)
3856 struct MeshTunnelClientInfo *clinfo;
3859 clinfo = &t->clients_fc[i];
3860 delta = clinfo->bck_ack - clinfo->bck_pid;
3861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3862 t->clients[i]->id, delta);
3864 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3865 (GNUNET_YES == t->nobuffer && 0 == delta))
3869 ack = clinfo->bck_pid;
3870 ack += t->nobuffer ? 1 : tunnel_delta;
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3872 " sending ack to client %u: %u\n",
3873 t->clients[i]->id, ack);
3874 send_local_ack (t, t->clients[i], ack);
3875 clinfo->bck_ack = ack;
3882 * Send an ACK informing the children nodes and destination clients about
3883 * the available buffer space.
3884 * If buffering is off, send only on behalf of root (can be self).
3885 * If buffering is on, send when sent to predecessor and buffer space is free.
3886 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3887 * the ACK itself goes "forward" (towards children/clients).
3889 * @param t Tunnel on which to send the ACK.
3890 * @param type Type of message that triggered the ACK transmission.
3893 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3896 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3897 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3898 /* Is it after data to_origin retransmission? */
3901 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3902 if (GNUNET_YES == t->nobuffer)
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3905 " Not sending ACK, nobuffer\n");
3909 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3910 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3916 tunnel_send_clients_bck_ack (t);
3917 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3922 * @brief Re-initiate traffic to this peer if necessary.
3924 * Check if there is traffic queued towards this peer
3925 * and the core transmit handle is NULL (traffic was stalled).
3926 * If so, call core tmt rdy.
3928 * @param cls Closure (unused)
3929 * @param peer_id Short ID of peer to which initiate traffic.
3932 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3934 struct MeshPeerInfo *peer;
3935 struct GNUNET_PeerIdentity id;
3936 struct MeshPeerQueue *q;
3939 peer = peer_info_get_short(peer_id);
3940 if (NULL != peer->core_transmit)
3943 q = queue_get_next(peer);
3946 /* Might br multicast traffic already sent to this particular peer but
3947 * not to other children in this tunnel.
3948 * This way t->queue_n would be > 0 but the queue of this particular peer
3954 GNUNET_PEER_resolve (peer->id, &id);
3955 peer->core_transmit =
3956 GNUNET_CORE_notify_transmit_ready(core_handle,
3959 GNUNET_TIME_UNIT_FOREVER_REL,
3969 * @brief Allow transmission of FWD traffic on this tunnel
3971 * Check if there is traffic queued towards any children
3972 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3974 * @param t Tunnel on which to unlock FWD traffic.
3977 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3979 if (0 == t->fwd_queue_n)
3982 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3987 * @brief Allow transmission of BCK traffic on this tunnel
3989 * Check if there is traffic queued towards the root of the tree
3990 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3992 * @param t Tunnel on which to unlock BCK traffic.
3995 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3997 if (0 == t->bck_queue_n)
4000 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4005 * Send a message to all peers in this tunnel that the tunnel is no longer
4008 * @param t The tunnel whose peers to notify.
4011 tunnel_send_destroy (struct MeshTunnel *t)
4013 struct GNUNET_MESH_TunnelDestroy msg;
4015 msg.header.size = htons (sizeof (msg));
4016 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4017 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4018 msg.tid = htonl (t->id.tid);
4019 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4024 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4026 * @param cls Closure (Tunnel which to cancel).
4027 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4030 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4032 struct MeshTunnel *t = cls;
4033 struct MeshPeerInfo *peer_info;
4034 struct MeshPeerQueue *pq;
4035 struct MeshPeerQueue *next;
4037 peer_info = peer_info_get_short (neighbor_id);
4038 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4041 if (pq->tunnel == t)
4043 queue_destroy (pq, GNUNET_YES);
4046 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4048 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4049 peer_info->core_transmit = NULL;
4054 * Destroy the tunnel and free any allocated resources linked to it.
4056 * @param t the tunnel to destroy
4058 * @return GNUNET_OK on success
4061 tunnel_destroy (struct MeshTunnel *t)
4063 struct MeshClient *c;
4064 struct GNUNET_HashCode hash;
4071 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4077 struct GNUNET_PeerIdentity id;
4079 GNUNET_PEER_resolve (t->id.oid, &id);
4080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4081 GNUNET_i2s (&id), t->id.tid);
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4087 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4088 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4093 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4096 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4100 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4101 for (i = 0; i < t->nclients; i++)
4105 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4110 for (i = 0; i < t->nignore; i++)
4114 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4119 if (t->nclients > 0)
4122 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4126 GNUNET_free (t->clients);
4128 if (NULL != t->peers)
4130 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4132 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4135 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4136 &tunnel_destroy_child,
4138 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4140 tree_destroy (t->tree);
4142 if (NULL != t->regex_ctx)
4143 regex_cancel_search (t->regex_ctx);
4144 if (NULL != t->dht_get_type)
4145 GNUNET_DHT_get_stop (t->dht_get_type);
4146 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4147 GNUNET_SCHEDULER_cancel (t->timeout_task);
4148 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4149 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4152 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4153 GNUNET_assert (0 <= n_tunnels);
4160 * Create a new tunnel
4162 * @param owner Who is the owner of the tunnel (short ID).
4163 * @param tid Tunnel Number of the tunnel.
4164 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4165 * @param local Tunnel Number for the tunnel, for the client point of view.
4167 * @return A new initialized tunnel. NULL on error.
4169 static struct MeshTunnel *
4170 tunnel_new (GNUNET_PEER_Id owner,
4171 MESH_TunnelNumber tid,
4172 struct MeshClient *client,
4173 MESH_TunnelNumber local)
4175 struct MeshTunnel *t;
4176 struct GNUNET_HashCode hash;
4178 if (n_tunnels >= max_tunnels && NULL == client)
4181 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4184 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4185 t->bck_queue_max = t->fwd_queue_max;
4186 t->tree = tree_new (owner);
4188 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4189 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4190 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4191 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4192 t->local_tid = local;
4193 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4195 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4197 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4199 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4200 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4207 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4214 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4216 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4217 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4221 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4231 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4232 * that are no longer needed, as well as nodes of no longer reachable peers.
4233 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4235 * @param t Tunnel from which to remove the path.
4236 * @param peer Short id of the peer which should be removed.
4239 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4241 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4247 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4248 * client when the client disconnects. If the client is not the owner, the
4249 * owner will get notified if no more clients are in the tunnel and the client
4250 * get removed from the tunnel's list.
4252 * @param cls closure (client that is disconnecting)
4253 * @param key the hash of the local tunnel id (used to access the hashmap)
4254 * @param value the value stored at the key (tunnel to destroy)
4256 * @return GNUNET_OK on success
4259 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4261 struct MeshTunnel *t = value;
4262 struct MeshClient *c = cls;
4265 send_client_tunnel_disconnect(t, c);
4268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4269 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4270 tunnel_delete_client(t, c);
4271 client_delete_tunnel(c, t);
4274 tunnel_send_destroy(t);
4275 r = tunnel_destroy (t);
4281 * Timeout function, destroys tunnel if called
4283 * @param cls Closure (tunnel to destroy).
4284 * @param tc TaskContext
4287 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4289 struct MeshTunnel *t = cls;
4291 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4293 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4298 * Resets the tunnel timeout. Starts it if no timeout was running.
4300 * @param t Tunnel whose timeout to reset.
4302 * TODO use heap to improve efficiency of scheduler.
4305 tunnel_reset_timeout (struct MeshTunnel *t)
4307 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4308 GNUNET_SCHEDULER_cancel (t->timeout_task);
4310 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4311 (refresh_path_time, 4), &tunnel_timeout, t);
4315 /******************************************************************************/
4316 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4317 /******************************************************************************/
4320 * Function to send a create path packet to a peer.
4322 * @param cls closure
4323 * @param size number of bytes available in buf
4324 * @param buf where the callee should write the message
4325 * @return number of bytes written to buf
4328 send_core_path_create (void *cls, size_t size, void *buf)
4330 struct MeshPathInfo *info = cls;
4331 struct GNUNET_MESH_ManipulatePath *msg;
4332 struct GNUNET_PeerIdentity *peer_ptr;
4333 struct MeshTunnel *t = info->t;
4334 struct MeshPeerPath *p = info->path;
4339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4341 sizeof (struct GNUNET_MESH_ManipulatePath) +
4342 p->length * sizeof (struct GNUNET_PeerIdentity);
4344 if (size < size_needed || NULL == buf)
4349 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4350 msg->header.size = htons (size_needed);
4351 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4352 msg->tid = ntohl (t->id.tid);
4354 if (GNUNET_YES == t->speed_min)
4355 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4356 if (GNUNET_YES == t->nobuffer)
4357 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4358 msg->opt = htonl(opt);
4360 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4361 for (i = 0; i < p->length; i++)
4363 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4370 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4376 * Fill the core buffer
4378 * @param cls closure (data itself)
4379 * @param size number of bytes available in buf
4380 * @param buf where the callee should write the message
4382 * @return number of bytes written to buf
4385 send_core_data_multicast (void *cls, size_t size, void *buf)
4387 struct MeshTransmissionDescriptor *info = cls;
4390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4391 GNUNET_assert (NULL != info);
4392 GNUNET_assert (NULL != info->peer);
4393 total_size = info->mesh_data->data_len;
4394 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4396 if (total_size > size)
4401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4402 memcpy (buf, info->mesh_data->data, total_size);
4405 struct GNUNET_MESH_Multicast *mc;
4406 struct GNUNET_MessageHeader *mh;
4409 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4411 mc = (struct GNUNET_MESH_Multicast *) mh;
4412 mh = (struct GNUNET_MessageHeader *) &mc[1];
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4414 " multicast, payload type %s\n",
4415 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4417 " multicast, payload size %u\n", ntohs (mh->size));
4421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4422 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4426 data_descriptor_decrement_rc (info->mesh_data);
4427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4435 * Creates a path ack message in buf and frees all unused resources.
4437 * @param cls closure (MeshTransmissionDescriptor)
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_ack (void *cls, size_t size, void *buf)
4445 struct MeshTransmissionDescriptor *info = cls;
4446 struct GNUNET_MESH_PathACK *msg = buf;
4448 GNUNET_assert (NULL != info);
4449 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4454 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4455 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4456 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4457 msg->tid = htonl (info->origin->tid);
4458 msg->peer_id = my_full_id;
4461 /* TODO add signature */
4463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4464 return sizeof (struct GNUNET_MESH_PathACK);
4469 * Free a transmission that was already queued with all resources
4470 * associated to the request.
4472 * @param queue Queue handler to cancel.
4473 * @param clear_cls Is it necessary to free associated cls?
4476 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4478 struct MeshTransmissionDescriptor *dd;
4479 struct MeshPathInfo *path_info;
4481 if (GNUNET_YES == clear_cls)
4483 switch (queue->type)
4485 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4486 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4487 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4490 data_descriptor_decrement_rc (dd->mesh_data);
4492 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4494 path_info = queue->cls;
4495 path_destroy (path_info->path);
4499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type unknown!\n");
4501 GNUNET_free_non_null (queue->cls);
4503 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4504 queue->peer->queue_tail,
4506 GNUNET_free (queue);
4511 * @brief Get the next transmittable message from the queue.
4513 * This will be the head, except in the case of being a data packet
4514 * not allowed by the destination peer.
4516 * @param peer Destination peer.
4518 * @return The next viable MeshPeerQueue element to send to that peer.
4519 * NULL when there are no transmittable messages.
4521 struct MeshPeerQueue *
4522 queue_get_next (const struct MeshPeerInfo *peer)
4524 struct MeshPeerQueue *q;
4525 struct MeshTunnel *t;
4526 struct MeshTransmissionDescriptor *info;
4527 struct MeshTunnelChildInfo *cinfo;
4528 struct GNUNET_MESH_Unicast *ucast;
4529 struct GNUNET_MESH_ToOrigin *to_orig;
4530 struct GNUNET_MESH_Multicast *mcast;
4531 struct GNUNET_PeerIdentity id;
4535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4536 for (q = peer->queue_head; NULL != q; q = q->next)
4540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4542 GNUNET_MESH_DEBUG_M2S(q->type));
4545 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4546 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4547 pid = ntohl (ucast->pid);
4548 GNUNET_PEER_resolve (info->peer->id, &id);
4549 cinfo = tunnel_get_neighbor_fc(t, &id);
4550 ack = cinfo->fwd_ack;
4552 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4553 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4554 pid = ntohl (to_orig->pid);
4557 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4558 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4559 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4561 // Not a multicast payload: multicast control traffic (destroy, etc)
4564 pid = ntohl (mcast->pid);
4565 GNUNET_PEER_resolve (info->peer->id, &id);
4566 cinfo = tunnel_get_neighbor_fc(t, &id);
4567 ack = cinfo->fwd_ack;
4570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4575 "********* ACK: %u, PID: %u\n",
4577 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4586 "********* NEXT!\n");
4589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4590 "********* nothing found\n");
4596 * Core callback to write a queued packet to core buffer
4598 * @param cls Closure (peer info).
4599 * @param size Number of bytes available in buf.
4600 * @param buf Where the to write the message.
4602 * @return number of bytes written to buf
4605 queue_send (void *cls, size_t size, void *buf)
4607 struct MeshPeerInfo *peer = cls;
4608 struct GNUNET_MessageHeader *msg;
4609 struct MeshPeerQueue *queue;
4610 struct MeshTunnel *t;
4613 peer->core_transmit = NULL;
4615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4616 queue = queue_get_next(peer);
4618 /* Queue has no internal mesh traffic not sendable payload */
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4622 if (NULL == peer->queue_head)
4623 GNUNET_break(0); // Should've been canceled
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4628 /* Check if buffer size is enough for the message */
4629 if (queue->size > size)
4631 struct GNUNET_PeerIdentity id;
4633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4634 "********* not enough room, reissue\n");
4635 GNUNET_PEER_resolve (peer->id, &id);
4636 peer->core_transmit =
4637 GNUNET_CORE_notify_transmit_ready(core_handle,
4640 GNUNET_TIME_UNIT_FOREVER_REL,
4647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4650 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4654 "********* unicast: t->q (%u/%u)\n",
4655 t->fwd_queue_n, t->fwd_queue_max);
4657 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4664 switch (queue->type)
4667 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4668 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4669 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4671 "********* raw: %s\n",
4672 GNUNET_MESH_DEBUG_M2S (queue->type));
4674 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4675 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4676 data_size = send_core_data_raw (queue->cls, size, buf);
4677 msg = (struct GNUNET_MessageHeader *) buf;
4678 switch (ntohs (msg->type)) // Type of preconstructed message
4680 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4681 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4683 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4684 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4690 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4692 data_size = send_core_data_multicast(queue->cls, size, buf);
4693 // FIXME fc substract when? depending on the tunnel conf.
4694 // t->fwd_queue_n--;
4695 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4697 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4699 data_size = send_core_path_create(queue->cls, size, buf);
4701 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4703 data_size = send_core_path_ack(queue->cls, size, buf);
4707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4708 "********* type unknown: %u\n",
4713 /* Free queue, but cls was freed by send_core_* */
4714 queue_destroy (queue, GNUNET_NO);
4716 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4722 /* If more data in queue, send next */
4723 if (NULL != peer->queue_head)
4725 struct GNUNET_PeerIdentity id;
4727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4728 GNUNET_PEER_resolve (peer->id, &id);
4729 peer->core_transmit =
4730 GNUNET_CORE_notify_transmit_ready(core_handle,
4733 GNUNET_TIME_UNIT_FOREVER_REL,
4735 peer->queue_head->size,
4739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4745 * Queue and pass message to core when possible.
4747 * @param cls Closure (type dependant).
4748 * @param type Type of the message, 0 for a raw message.
4749 * @param size Size of the message.
4750 * @param dst Neighbor to send message to.
4751 * @param t Tunnel this message belongs to.
4754 queue_add (void *cls, uint16_t type, size_t size,
4755 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4757 struct MeshPeerQueue *queue;
4758 struct MeshTunnelChildInfo *cinfo;
4759 struct GNUNET_PeerIdentity id;
4765 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4766 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4768 n = &t->fwd_queue_n;
4769 max = &t->fwd_queue_max;
4771 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4773 n = &t->bck_queue_n;
4774 max = &t->bck_queue_max;
4779 if (NULL == t->owner)
4780 GNUNET_break_op(0); // TODO: kill connection?
4783 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4785 return; // Drop message
4789 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4795 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4796 GNUNET_PEER_resolve (dst->id, &id);
4797 if (NULL == dst->core_transmit)
4799 dst->core_transmit =
4800 GNUNET_CORE_notify_transmit_ready(core_handle,
4803 GNUNET_TIME_UNIT_FOREVER_REL,
4809 if (NULL == n) // Is this internal mesh traffic?
4812 // It's payload, keep track of buffer per peer.
4813 cinfo = tunnel_get_neighbor_fc(t, &id);
4814 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4815 if (NULL != cinfo->send_buffer[i])
4817 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
4818 GNUNET_break (cinfo->send_buffer_n > 0);
4819 cinfo->send_buffer_n--;
4821 cinfo->send_buffer[i] = queue;
4822 cinfo->send_buffer_n++;
4823 if (cinfo->send_buffer_n > t->fwd_queue_max)
4826 cinfo->send_buffer_n = t->fwd_queue_max;
4831 /******************************************************************************/
4832 /******************** MESH NETWORK HANDLERS **************************/
4833 /******************************************************************************/
4837 * Core handler for path creation
4839 * @param cls closure
4840 * @param message message
4841 * @param peer peer identity this notification is about
4842 * @param atsi performance data
4843 * @param atsi_count number of records in 'atsi'
4845 * @return GNUNET_OK to keep the connection open,
4846 * GNUNET_SYSERR to close it (signal serious error)
4849 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4850 const struct GNUNET_MessageHeader *message,
4851 const struct GNUNET_ATS_Information *atsi,
4852 unsigned int atsi_count)
4854 unsigned int own_pos;
4857 MESH_TunnelNumber tid;
4858 struct GNUNET_MESH_ManipulatePath *msg;
4859 struct GNUNET_PeerIdentity *pi;
4860 struct GNUNET_HashCode hash;
4861 struct MeshPeerPath *path;
4862 struct MeshPeerInfo *dest_peer_info;
4863 struct MeshPeerInfo *orig_peer_info;
4864 struct MeshTunnel *t;
4866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4867 "Received a path create msg [%s]\n",
4868 GNUNET_i2s (&my_full_id));
4869 size = ntohs (message->size);
4870 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4872 GNUNET_break_op (0);
4876 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4877 if (size % sizeof (struct GNUNET_PeerIdentity))
4879 GNUNET_break_op (0);
4882 size /= sizeof (struct GNUNET_PeerIdentity);
4885 GNUNET_break_op (0);
4888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4889 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4891 tid = ntohl (msg->tid);
4892 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4894 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4895 t = tunnel_get (pi, tid);
4896 if (NULL == t) // FIXME only for INCOMING tunnels?
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4901 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4904 // FIXME notify failure
4907 opt = ntohl (msg->opt);
4908 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4909 GNUNET_YES : GNUNET_NO;
4910 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4911 GNUNET_YES : GNUNET_NO;
4913 if (GNUNET_YES == t->nobuffer)
4915 t->bck_queue_max = 1;
4916 t->fwd_queue_max = 1;
4919 // FIXME only assign a local tid if a local client is interested (on demand)
4920 while (NULL != tunnel_get_incoming (next_local_tid))
4921 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4922 t->local_tid_dest = next_local_tid++;
4923 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4926 tunnel_reset_timeout (t);
4927 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4929 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4930 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4938 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4939 if (NULL == dest_peer_info)
4941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4942 " Creating PeerInfo for destination.\n");
4943 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4944 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4945 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4947 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4949 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4950 if (NULL == orig_peer_info)
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4953 " Creating PeerInfo for origin.\n");
4954 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4955 orig_peer_info->id = GNUNET_PEER_intern (pi);
4956 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4957 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4960 path = path_new (size);
4962 for (i = 0; i < size; i++)
4964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4965 GNUNET_i2s (&pi[i]));
4966 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4967 if (path->peers[i] == myid)
4970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4973 /* cannot be self, must be 'not found' */
4974 /* create path: self not found in path through self */
4975 GNUNET_break_op (0);
4976 path_destroy (path);
4980 path_add_to_peers (path, GNUNET_NO);
4981 tunnel_add_path (t, path, own_pos);
4982 if (own_pos == size - 1)
4984 /* It is for us! Send ack. */
4985 struct MeshTransmissionDescriptor *info;
4987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4988 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4989 if (NULL == t->peers)
4991 /* New tunnel! Notify clients on data. */
4992 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
4994 GNUNET_break (GNUNET_SYSERR !=
4995 GNUNET_CONTAINER_multihashmap_put (t->peers,
4996 &my_full_id.hashPubKey,
4999 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5000 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5001 info->origin = &t->id;
5002 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5003 GNUNET_assert (NULL != info->peer);
5005 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5006 sizeof (struct GNUNET_MESH_PathACK),
5012 struct MeshPeerPath *path2;
5014 /* It's for somebody else! Retransmit. */
5015 path2 = path_duplicate (path);
5016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5017 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5018 path2 = path_duplicate (path);
5019 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5020 send_create_path (dest_peer_info, path, t);
5027 * Core handler for path destruction
5029 * @param cls closure
5030 * @param message message
5031 * @param peer peer identity this notification is about
5032 * @param atsi performance data
5033 * @param atsi_count number of records in 'atsi'
5035 * @return GNUNET_OK to keep the connection open,
5036 * GNUNET_SYSERR to close it (signal serious error)
5039 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5040 const struct GNUNET_MessageHeader *message,
5041 const struct GNUNET_ATS_Information *atsi,
5042 unsigned int atsi_count)
5044 struct GNUNET_MESH_ManipulatePath *msg;
5045 struct GNUNET_PeerIdentity *pi;
5046 struct MeshPeerPath *path;
5047 struct MeshTunnel *t;
5048 unsigned int own_pos;
5052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5053 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5054 size = ntohs (message->size);
5055 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5057 GNUNET_break_op (0);
5061 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5062 if (size % sizeof (struct GNUNET_PeerIdentity))
5064 GNUNET_break_op (0);
5067 size /= sizeof (struct GNUNET_PeerIdentity);
5070 GNUNET_break_op (0);
5073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5075 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5076 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5078 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5080 t = tunnel_get (pi, ntohl (msg->tid));
5083 /* TODO notify back: we don't know this tunnel */
5084 GNUNET_break_op (0);
5087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5088 path = path_new (size);
5090 for (i = 0; i < size; i++)
5092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5093 GNUNET_i2s (&pi[i]));
5094 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5095 if (path->peers[i] == myid)
5098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5099 if (own_pos < path->length - 1)
5100 send_message (message, &pi[own_pos + 1], t);
5102 send_client_tunnel_disconnect(t, NULL);
5104 tunnel_delete_peer (t, path->peers[path->length - 1]);
5105 path_destroy (path);
5111 * Core handler for notifications of broken paths
5113 * @param cls closure
5114 * @param message message
5115 * @param peer peer identity this notification is about
5116 * @param atsi performance data
5117 * @param atsi_count number of records in 'atsi'
5119 * @return GNUNET_OK to keep the connection open,
5120 * GNUNET_SYSERR to close it (signal serious error)
5123 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5124 const struct GNUNET_MessageHeader *message,
5125 const struct GNUNET_ATS_Information *atsi,
5126 unsigned int atsi_count)
5128 struct GNUNET_MESH_PathBroken *msg;
5129 struct MeshTunnel *t;
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5132 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5133 msg = (struct GNUNET_MESH_PathBroken *) message;
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5135 GNUNET_i2s (&msg->peer1));
5136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5137 GNUNET_i2s (&msg->peer2));
5138 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5141 GNUNET_break_op (0);
5144 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5145 GNUNET_PEER_search (&msg->peer2));
5152 * Core handler for tunnel destruction
5154 * @param cls closure
5155 * @param message message
5156 * @param peer peer identity this notification is about
5157 * @param atsi performance data
5158 * @param atsi_count number of records in 'atsi'
5160 * @return GNUNET_OK to keep the connection open,
5161 * GNUNET_SYSERR to close it (signal serious error)
5164 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5165 const struct GNUNET_MessageHeader *message,
5166 const struct GNUNET_ATS_Information *atsi,
5167 unsigned int atsi_count)
5169 struct GNUNET_MESH_TunnelDestroy *msg;
5170 struct MeshTunnel *t;
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5173 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5174 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5176 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5177 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5180 /* Probably already got the message from another path,
5181 * destroyed the tunnel and retransmitted to children.
5184 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5187 if (t->id.oid == myid)
5189 GNUNET_break_op (0);
5192 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5194 /* Tunnel was incoming, notify clients */
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5196 t->local_tid, t->local_tid_dest);
5197 send_clients_tunnel_destroy (t);
5199 tunnel_send_destroy (t);
5200 t->destroy = GNUNET_YES;
5201 // TODO: add timeout to destroy the tunnel anyway
5207 * Core handler for mesh network traffic going from the origin to a peer
5209 * @param cls closure
5210 * @param peer peer identity this notification is about
5211 * @param message message
5212 * @param atsi performance data
5213 * @param atsi_count number of records in 'atsi'
5214 * @return GNUNET_OK to keep the connection open,
5215 * GNUNET_SYSERR to close it (signal serious error)
5218 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5219 const struct GNUNET_MessageHeader *message,
5220 const struct GNUNET_ATS_Information *atsi,
5221 unsigned int atsi_count)
5223 struct GNUNET_MESH_Unicast *msg;
5224 struct GNUNET_PeerIdentity *neighbor;
5225 struct MeshTunnelChildInfo *cinfo;
5226 struct MeshTunnel *t;
5227 GNUNET_PEER_Id dest_id;
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5235 size = ntohs (message->size);
5237 sizeof (struct GNUNET_MESH_Unicast) +
5238 sizeof (struct GNUNET_MessageHeader))
5243 msg = (struct GNUNET_MESH_Unicast *) message;
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5245 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5247 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5250 /* TODO notify back: we don't know this tunnel */
5251 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5252 GNUNET_break_op (0);
5255 pid = ntohl (msg->pid);
5256 if (t->fwd_pid == pid)
5258 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5260 " Already seen pid %u, DROPPING!\n", pid);
5265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5266 " pid %u not seen yet, forwarding\n", pid);
5269 t->skip += (pid - t->fwd_pid) - 1;
5272 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5274 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5275 GNUNET_break_op (0);
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5277 "Received PID %u, ACK %u\n",
5278 pid, t->last_fwd_ack);
5282 tunnel_reset_timeout (t);
5283 dest_id = GNUNET_PEER_search (&msg->destination);
5284 if (dest_id == myid)
5286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5287 " it's for us! sending to clients...\n");
5288 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5289 send_subscribed_clients (message, &msg[1].header, t);
5290 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5293 ttl = ntohl (msg->ttl);
5294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5297 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5298 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5299 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5303 " not for us, retransmitting...\n");
5305 neighbor = tree_get_first_hop (t->tree, dest_id);
5306 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5308 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5311 if (GNUNET_YES == t->nobuffer &&
5312 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5314 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5315 GNUNET_break_op (0);
5318 send_message (message, neighbor, t);
5319 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5325 * Core handler for mesh network traffic going from the origin to all peers
5327 * @param cls closure
5328 * @param message message
5329 * @param peer peer identity this notification is about
5330 * @param atsi performance data
5331 * @param atsi_count number of records in 'atsi'
5332 * @return GNUNET_OK to keep the connection open,
5333 * GNUNET_SYSERR to close it (signal serious error)
5335 * TODO: Check who we got this from, to validate route.
5338 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5339 const struct GNUNET_MessageHeader *message,
5340 const struct GNUNET_ATS_Information *atsi,
5341 unsigned int atsi_count)
5343 struct GNUNET_MESH_Multicast *msg;
5344 struct MeshTunnel *t;
5348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5350 size = ntohs (message->size);
5351 if (sizeof (struct GNUNET_MESH_Multicast) +
5352 sizeof (struct GNUNET_MessageHeader) > size)
5354 GNUNET_break_op (0);
5357 msg = (struct GNUNET_MESH_Multicast *) message;
5358 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5362 /* TODO notify that we dont know that tunnel */
5363 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5364 GNUNET_break_op (0);
5367 pid = ntohl (msg->pid);
5368 if (t->fwd_pid == pid)
5370 /* already seen this packet, drop */
5371 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5373 " Already seen pid %u, DROPPING!\n", pid);
5374 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5380 " pid %u not seen yet, forwarding\n", pid);
5382 t->skip += (pid - t->fwd_pid) - 1;
5384 tunnel_reset_timeout (t);
5386 /* Transmit to locally interested clients */
5387 if (NULL != t->peers &&
5388 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5390 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5391 send_subscribed_clients (message, &msg[1].header, t);
5392 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5395 if (ntohl (msg->ttl) == 0)
5397 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5398 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5399 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5402 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5403 tunnel_send_multicast (t, message, GNUNET_NO);
5409 * Core handler for mesh network traffic toward the owner of a tunnel
5411 * @param cls closure
5412 * @param message message
5413 * @param peer peer identity this notification is about
5414 * @param atsi performance data
5415 * @param atsi_count number of records in 'atsi'
5417 * @return GNUNET_OK to keep the connection open,
5418 * GNUNET_SYSERR to close it (signal serious error)
5421 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5422 const struct GNUNET_MessageHeader *message,
5423 const struct GNUNET_ATS_Information *atsi,
5424 unsigned int atsi_count)
5426 struct GNUNET_MESH_ToOrigin *msg;
5427 struct GNUNET_PeerIdentity id;
5428 struct MeshPeerInfo *peer_info;
5429 struct MeshTunnel *t;
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5435 size = ntohs (message->size);
5436 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5437 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5439 GNUNET_break_op (0);
5442 msg = (struct GNUNET_MESH_ToOrigin *) message;
5443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5444 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5445 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5449 /* TODO notify that we dont know this tunnel (whom)? */
5450 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5451 GNUNET_break_op (0);
5455 if (NULL != t->owner)
5458 struct GNUNET_MESH_ToOrigin *copy;
5460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5461 " it's for us! sending to clients...\n");
5462 /* TODO signature verification */
5463 memcpy (cbuf, message, size);
5464 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5465 copy->tid = htonl (t->local_tid);
5466 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5467 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5468 ©->header, GNUNET_NO);
5469 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5473 " not for us, retransmitting...\n");
5475 peer_info = peer_info_get (&msg->oid);
5476 if (NULL == peer_info)
5478 /* unknown origin of tunnel */
5482 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5483 send_message (message, &id, t);
5484 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5491 * Core handler for mesh network traffic point-to-point acks.
5493 * @param cls closure
5494 * @param message message
5495 * @param peer peer identity this notification is about
5496 * @param atsi performance data
5497 * @param atsi_count number of records in 'atsi'
5499 * @return GNUNET_OK to keep the connection open,
5500 * GNUNET_SYSERR to close it (signal serious error)
5503 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5504 const struct GNUNET_MessageHeader *message,
5505 const struct GNUNET_ATS_Information *atsi,
5506 unsigned int atsi_count)
5508 struct GNUNET_MESH_ACK *msg;
5509 struct MeshTunnel *t;
5512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5514 msg = (struct GNUNET_MESH_ACK *) message;
5516 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5520 /* TODO notify that we dont know this tunnel (whom)? */
5521 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5522 GNUNET_break_op (0);
5525 ack = ntohl (msg->pid);
5527 /* Is this a forward or backward ACK? */
5528 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5530 struct MeshTunnelChildInfo *cinfo;
5532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5533 cinfo = tunnel_get_neighbor_fc (t, peer);
5534 cinfo->fwd_ack = ack;
5535 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5536 tunnel_unlock_fwd_queues (t);
5540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5542 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5543 tunnel_unlock_bck_queue (t);
5550 * Core handler for path ACKs
5552 * @param cls closure
5553 * @param message message
5554 * @param peer peer identity this notification is about
5555 * @param atsi performance data
5556 * @param atsi_count number of records in 'atsi'
5558 * @return GNUNET_OK to keep the connection open,
5559 * GNUNET_SYSERR to close it (signal serious error)
5562 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5563 const struct GNUNET_MessageHeader *message,
5564 const struct GNUNET_ATS_Information *atsi,
5565 unsigned int atsi_count)
5567 struct GNUNET_MESH_PathACK *msg;
5568 struct GNUNET_PeerIdentity id;
5569 struct MeshPeerInfo *peer_info;
5570 struct MeshPeerPath *p;
5571 struct MeshTunnel *t;
5573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5574 GNUNET_i2s (&my_full_id));
5575 msg = (struct GNUNET_MESH_PathACK *) message;
5576 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5579 /* TODO notify that we don't know the tunnel */
5580 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5582 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5586 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5588 peer_info = peer_info_get (&msg->peer_id);
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5590 GNUNET_i2s (&msg->peer_id));
5591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5594 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5597 "connect_by_string completed, stopping search\n");
5598 regex_cancel_search (t->regex_ctx);
5599 t->regex_ctx = NULL;
5602 /* Add paths to peers? */
5603 p = tree_get_path_to_peer (t->tree, peer_info->id);
5606 path_add_to_peers (p, GNUNET_YES);
5614 /* Message for us? */
5615 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5618 if (NULL == t->owner)
5620 GNUNET_break_op (0);
5623 if (NULL != t->dht_get_type)
5625 GNUNET_DHT_get_stop (t->dht_get_type);
5626 t->dht_get_type = NULL;
5628 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5630 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5631 send_client_peer_connected (t, peer_info->id);
5636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5637 " not for us, retransmitting...\n");
5638 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5639 peer_info = peer_info_get (&msg->oid);
5640 if (NULL == peer_info)
5642 /* If we know the tunnel, we should DEFINITELY know the peer */
5646 send_message (message, &id, t);
5652 * Functions to handle messages from core
5654 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5655 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5656 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5657 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5658 sizeof (struct GNUNET_MESH_PathBroken)},
5659 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5660 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5661 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5662 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5663 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5664 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5665 sizeof (struct GNUNET_MESH_ACK)},
5666 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5667 sizeof (struct GNUNET_MESH_PathACK)},
5673 /******************************************************************************/
5674 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5675 /******************************************************************************/
5678 * deregister_app: iterator for removing each application registered by a client
5680 * @param cls closure
5681 * @param key the hash of the application id (used to access the hashmap)
5682 * @param value the value stored at the key (client)
5684 * @return GNUNET_OK on success
5687 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5689 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5690 GNUNET_break (GNUNET_YES ==
5691 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5697 * notify_client_connection_failure: notify a client that the connection to the
5698 * requested remote peer is not possible (for instance, no route found)
5699 * Function called when the socket is ready to queue more data. "buf" will be
5700 * NULL and "size" zero if the socket was closed for writing in the meantime.
5702 * @param cls closure
5703 * @param size number of bytes available in buf
5704 * @param buf where the callee should write the message
5705 * @return number of bytes written to buf
5708 notify_client_connection_failure (void *cls, size_t size, void *buf)
5711 struct MeshPeerInfo *peer_info;
5712 struct GNUNET_MESH_PeerControl *msg;
5713 struct GNUNET_PeerIdentity id;
5715 if (0 == size && NULL == buf)
5717 // TODO retry? cancel?
5721 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5722 peer_info = (struct MeshPeerInfo *) cls;
5723 msg = (struct GNUNET_MESH_PeerControl *) buf;
5724 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5725 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5726 // msg->tunnel_id = htonl(peer_info->t->tid);
5727 GNUNET_PEER_resolve (peer_info->id, &id);
5728 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5736 * Send keepalive packets for a peer
5738 * @param cls Closure (tunnel for which to send the keepalive).
5739 * @param tc Notification context.
5741 * TODO: implement explicit multicast keepalive?
5744 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5746 struct MeshTunnel *t = cls;
5747 struct GNUNET_MessageHeader *payload;
5748 struct GNUNET_MESH_Multicast *msg;
5750 sizeof (struct GNUNET_MESH_Multicast) +
5751 sizeof (struct GNUNET_MessageHeader);
5754 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5758 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5761 "sending keepalive for tunnel %d\n", t->id.tid);
5763 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5764 msg->header.size = htons (size);
5765 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5766 // FIXME: change type to != MULTICAST
5767 msg->oid = my_full_id;
5768 msg->tid = htonl (t->id.tid);
5769 msg->ttl = htonl (default_ttl);
5770 msg->pid = htonl (t->fwd_pid + 1);
5772 payload = (struct GNUNET_MessageHeader *) &msg[1];
5773 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5774 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5775 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5777 t->path_refresh_task =
5778 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5784 * Function to process paths received for a new peer addition. The recorded
5785 * paths form the initial tunnel, which can be optimized later.
5786 * Called on each result obtained for the DHT search.
5788 * @param cls closure
5789 * @param exp when will this value expire
5790 * @param key key of the result
5791 * @param get_path path of the get request
5792 * @param get_path_length lenght of get_path
5793 * @param put_path path of the put request
5794 * @param put_path_length length of the put_path
5795 * @param type type of the result
5796 * @param size number of bytes in data
5797 * @param data pointer to the result data
5799 * TODO: re-issue the request after certain time? cancel after X results?
5802 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5803 const struct GNUNET_HashCode * key,
5804 const struct GNUNET_PeerIdentity *get_path,
5805 unsigned int get_path_length,
5806 const struct GNUNET_PeerIdentity *put_path,
5807 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5808 size_t size, const void *data)
5810 struct MeshPathInfo *path_info = cls;
5811 struct MeshPeerPath *p;
5812 struct GNUNET_PeerIdentity pi;
5815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5816 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5819 p = path_build_from_dht (get_path, get_path_length, put_path,
5821 path_add_to_peers (p, GNUNET_NO);
5823 for (i = 0; i < path_info->peer->ntunnels; i++)
5825 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5826 peer_info_connect (path_info->peer, path_info->t);
5834 * Function to process paths received for a new peer addition. The recorded
5835 * paths form the initial tunnel, which can be optimized later.
5836 * Called on each result obtained for the DHT search.
5838 * @param cls closure
5839 * @param exp when will this value expire
5840 * @param key key of the result
5841 * @param get_path path of the get request
5842 * @param get_path_length lenght of get_path
5843 * @param put_path path of the put request
5844 * @param put_path_length length of the put_path
5845 * @param type type of the result
5846 * @param size number of bytes in data
5847 * @param data pointer to the result data
5850 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5851 const struct GNUNET_HashCode * key,
5852 const struct GNUNET_PeerIdentity *get_path,
5853 unsigned int get_path_length,
5854 const struct GNUNET_PeerIdentity *put_path,
5855 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5856 size_t size, const void *data)
5858 const struct PBlock *pb = data;
5859 const struct GNUNET_PeerIdentity *pi = &pb->id;
5860 struct MeshTunnel *t = cls;
5861 struct MeshPeerInfo *peer_info;
5862 struct MeshPeerPath *p;
5864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5865 if (size != sizeof (struct PBlock))
5867 GNUNET_break_op (0);
5870 if (ntohl(pb->type) != t->type)
5872 GNUNET_break_op (0);
5875 GNUNET_assert (NULL != t->owner);
5876 peer_info = peer_info_get (pi);
5877 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5879 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5881 p = path_build_from_dht (get_path, get_path_length, put_path,
5883 path_add_to_peers (p, GNUNET_NO);
5885 tunnel_add_peer (t, peer_info);
5886 peer_info_connect (peer_info, t);
5891 * Function to process DHT string to regex matching.
5892 * Called on each result obtained for the DHT search.
5894 * @param cls closure (search context)
5895 * @param exp when will this value expire
5896 * @param key key of the result
5897 * @param get_path path of the get request (not used)
5898 * @param get_path_length lenght of get_path (not used)
5899 * @param put_path path of the put request (not used)
5900 * @param put_path_length length of the put_path (not used)
5901 * @param type type of the result
5902 * @param size number of bytes in data
5903 * @param data pointer to the result data
5906 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5907 const struct GNUNET_HashCode * key,
5908 const struct GNUNET_PeerIdentity *get_path,
5909 unsigned int get_path_length,
5910 const struct GNUNET_PeerIdentity *put_path,
5911 unsigned int put_path_length,
5912 enum GNUNET_BLOCK_Type type,
5913 size_t size, const void *data)
5915 const struct MeshRegexAccept *block = data;
5916 struct MeshRegexSearchContext *ctx = cls;
5917 struct MeshRegexSearchInfo *info = ctx->info;
5918 struct MeshPeerPath *p;
5919 struct MeshPeerInfo *peer_info;
5921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5924 peer_info = peer_info_get(&block->id);
5925 p = path_build_from_dht (get_path, get_path_length, put_path,
5927 path_add_to_peers (p, GNUNET_NO);
5930 tunnel_add_peer (info->t, peer_info);
5931 peer_info_connect (peer_info, info->t);
5932 if (0 == info->peer)
5934 info->peer = peer_info->id;
5938 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5941 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5942 ®ex_connect_timeout,
5950 * Function to process DHT string to regex matching.
5951 * Called on each result obtained for the DHT search.
5953 * @param cls closure (search context)
5954 * @param exp when will this value expire
5955 * @param key key of the result
5956 * @param get_path path of the get request (not used)
5957 * @param get_path_length lenght of get_path (not used)
5958 * @param put_path path of the put request (not used)
5959 * @param put_path_length length of the put_path (not used)
5960 * @param type type of the result
5961 * @param size number of bytes in data
5962 * @param data pointer to the result data
5964 * TODO: re-issue the request after certain time? cancel after X results?
5967 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5968 const struct GNUNET_HashCode * key,
5969 const struct GNUNET_PeerIdentity *get_path,
5970 unsigned int get_path_length,
5971 const struct GNUNET_PeerIdentity *put_path,
5972 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5973 size_t size, const void *data)
5975 const struct MeshRegexBlock *block = data;
5976 struct MeshRegexSearchContext *ctx = cls;
5977 struct MeshRegexSearchInfo *info = ctx->info;
5981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5982 "DHT GET STRING RETURNED RESULTS\n");
5983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5984 " key: %s\n", GNUNET_h2s (key));
5986 copy = GNUNET_malloc (size);
5987 memcpy (copy, data, size);
5988 GNUNET_break (GNUNET_OK ==
5989 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
5990 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
5991 len = ntohl (block->n_proof);
5993 char proof[len + 1];
5995 memcpy (proof, &block[1], len);
5997 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
5999 GNUNET_break_op (0);
6003 len = strlen (info->description);
6004 if (len == ctx->position) // String processed
6006 if (GNUNET_YES == ntohl (block->accepting))
6008 regex_find_path(key, ctx);
6012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6013 // FIXME REGEX this block not successful, wait for more? start timeout?
6017 GNUNET_break (GNUNET_OK ==
6018 GNUNET_MESH_regex_block_iterate (block, size,
6019 ®ex_edge_iterator, ctx));
6023 /******************************************************************************/
6024 /********************* MESH LOCAL HANDLES **************************/
6025 /******************************************************************************/
6029 * Handler for client disconnection
6031 * @param cls closure
6032 * @param client identification of the client; NULL
6033 * for the last call when the server is destroyed
6036 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6038 struct MeshClient *c;
6039 struct MeshClient *next;
6042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6051 if (c->handle != client)
6053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6059 GNUNET_SERVER_client_drop (c->handle);
6060 c->shutting_down = GNUNET_YES;
6061 GNUNET_assert (NULL != c->own_tunnels);
6062 GNUNET_assert (NULL != c->incoming_tunnels);
6063 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6064 &tunnel_destroy_iterator, c);
6065 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6066 &tunnel_destroy_iterator, c);
6067 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6068 &tunnel_destroy_iterator, c);
6069 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6070 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6071 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6073 /* deregister clients applications */
6074 if (NULL != c->apps)
6076 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6077 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6079 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6080 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6082 GNUNET_SCHEDULER_cancel (announce_applications_task);
6083 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6085 if (NULL != c->types)
6086 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6087 for (i = 0; i < c->n_regex; i++)
6089 GNUNET_free (c->regexes[i]);
6091 GNUNET_free_non_null (c->regexes);
6092 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6093 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6095 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6098 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6107 * Handler for new clients
6109 * @param cls closure
6110 * @param client identification of the client
6111 * @param message the actual message, which includes messages the client wants
6114 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6115 const struct GNUNET_MessageHeader *message)
6117 struct GNUNET_MESH_ClientConnect *cc_msg;
6118 struct MeshClient *c;
6119 GNUNET_MESH_ApplicationType *a;
6126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6127 /* Check data sanity */
6128 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6129 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6130 ntypes = ntohs (cc_msg->types);
6131 napps = ntohs (cc_msg->applications);
6133 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6140 /* Create new client structure */
6141 c = GNUNET_malloc (sizeof (struct MeshClient));
6142 c->id = next_client_id++;
6143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6145 GNUNET_SERVER_client_keep (client);
6146 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6149 GNUNET_MESH_ApplicationType at;
6150 struct GNUNET_HashCode hc;
6152 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6153 for (i = 0; i < napps; i++)
6156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6157 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6158 /* store in clients hashmap */
6159 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6160 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6161 /* store in global hashmap, for announcements */
6162 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6163 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6165 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6166 announce_applications_task =
6167 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6173 struct GNUNET_HashCode hc;
6175 t = (uint16_t *) & a[napps];
6176 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6177 for (i = 0; i < ntypes; i++)
6180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6181 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6183 /* store in clients hashmap */
6184 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6185 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6186 /* store in global hashmap */
6187 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6188 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6192 " client has %u+%u subscriptions\n", napps, ntypes);
6194 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6195 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6196 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6197 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6198 GNUNET_SERVER_notification_context_add (nc, client);
6199 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6201 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6207 * Handler for clients announcing available services by a regular expression.
6209 * @param cls closure
6210 * @param client identification of the client
6211 * @param message the actual message, which includes messages the client wants
6214 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6215 const struct GNUNET_MessageHeader *message)
6217 struct MeshClient *c;
6221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6223 /* Sanity check for client registration */
6224 if (NULL == (c = client_get (client)))
6227 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6232 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6233 regex = GNUNET_malloc (len + 1);
6234 memcpy (regex, &message[1], len);
6236 GNUNET_array_append (c->regexes, c->n_regex, regex);
6237 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6239 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6245 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6251 * Handler for requests of new tunnels
6253 * @param cls closure
6254 * @param client identification of the client
6255 * @param message the actual message
6258 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6259 const struct GNUNET_MessageHeader *message)
6261 struct GNUNET_MESH_TunnelMessage *t_msg;
6262 struct MeshTunnel *t;
6263 struct MeshClient *c;
6264 MESH_TunnelNumber tid;
6266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6268 /* Sanity check for client registration */
6269 if (NULL == (c = client_get (client)))
6272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6277 /* Message sanity check */
6278 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6281 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6285 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6286 /* Sanity check for tunnel numbering */
6287 tid = ntohl (t_msg->tunnel_id);
6288 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6294 /* Sanity check for duplicate tunnel IDs */
6295 if (NULL != tunnel_get_by_local_id (c, tid))
6298 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6302 while (NULL != tunnel_get_by_pi (myid, next_tid))
6303 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6304 t = tunnel_new (myid, next_tid++, c, tid);
6307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6312 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6314 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6315 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6318 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6324 * Handler for requests of deleting tunnels
6326 * @param cls closure
6327 * @param client identification of the client
6328 * @param message the actual message
6331 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6332 const struct GNUNET_MessageHeader *message)
6334 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6335 struct MeshClient *c;
6336 struct MeshTunnel *t;
6337 MESH_TunnelNumber tid;
6339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6340 "Got a DESTROY TUNNEL from client!\n");
6342 /* Sanity check for client registration */
6343 if (NULL == (c = client_get (client)))
6346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6351 /* Message sanity check */
6352 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6355 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6359 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6361 /* Retrieve tunnel */
6362 tid = ntohl (tunnel_msg->tunnel_id);
6363 t = tunnel_get_by_local_id(c, tid);
6366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6368 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6371 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6373 client_ignore_tunnel (c, t);
6375 // TODO: when to destroy incoming tunnel?
6376 if (t->nclients == 0)
6378 GNUNET_assert (GNUNET_YES ==
6379 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6381 GNUNET_assert (GNUNET_YES ==
6382 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6383 &my_full_id.hashPubKey,
6387 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6390 send_client_tunnel_disconnect(t, c);
6391 client_delete_tunnel(c, t);
6393 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6395 tunnel_send_destroy (t);
6396 t->destroy = GNUNET_YES;
6397 // The tunnel will be destroyed when the last message is transmitted.
6398 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6404 * Handler for requests of seeting tunnel's speed.
6406 * @param cls Closure (unused).
6407 * @param client Identification of the client.
6408 * @param message The actual message.
6411 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6412 const struct GNUNET_MessageHeader *message)
6414 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6415 struct MeshClient *c;
6416 struct MeshTunnel *t;
6417 MESH_TunnelNumber tid;
6419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6420 "Got a SPEED request from client!\n");
6422 /* Sanity check for client registration */
6423 if (NULL == (c = client_get (client)))
6426 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6432 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6434 /* Retrieve tunnel */
6435 tid = ntohl (tunnel_msg->tunnel_id);
6436 t = tunnel_get_by_local_id(c, tid);
6439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6445 switch (ntohs(message->type))
6447 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6448 t->speed_min = GNUNET_YES;
6450 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6451 t->speed_min = GNUNET_NO;
6456 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6461 * Handler for requests of seeting tunnel's buffering policy.
6463 * @param cls Closure (unused).
6464 * @param client Identification of the client.
6465 * @param message The actual message.
6468 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6469 const struct GNUNET_MessageHeader *message)
6471 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6472 struct MeshClient *c;
6473 struct MeshTunnel *t;
6474 MESH_TunnelNumber tid;
6476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6477 "Got a BUFFER request from client!\n");
6479 /* Sanity check for client registration */
6480 if (NULL == (c = client_get (client)))
6483 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6488 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6490 /* Retrieve tunnel */
6491 tid = ntohl (tunnel_msg->tunnel_id);
6492 t = tunnel_get_by_local_id(c, tid);
6495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6497 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6501 switch (ntohs(message->type))
6503 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6504 t->nobuffer = GNUNET_NO;
6506 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6507 t->nobuffer = GNUNET_YES;
6513 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6518 * Handler for connection requests to new peers
6520 * @param cls closure
6521 * @param client identification of the client
6522 * @param message the actual message (PeerControl)
6525 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6526 const struct GNUNET_MessageHeader *message)
6528 struct GNUNET_MESH_PeerControl *peer_msg;
6529 struct MeshPeerInfo *peer_info;
6530 struct MeshClient *c;
6531 struct MeshTunnel *t;
6532 MESH_TunnelNumber tid;
6534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6535 /* Sanity check for client registration */
6536 if (NULL == (c = client_get (client)))
6539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6544 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6546 /* Sanity check for message size */
6547 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6550 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6554 /* Tunnel exists? */
6555 tid = ntohl (peer_msg->tunnel_id);
6556 t = tunnel_get_by_local_id (c, tid);
6560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6564 /* Does client own tunnel? */
6565 if (t->owner->handle != client)
6568 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6572 GNUNET_i2s (&peer_msg->peer));
6573 peer_info = peer_info_get (&peer_msg->peer);
6575 tunnel_add_peer (t, peer_info);
6576 peer_info_connect (peer_info, t);
6578 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6584 * Handler for disconnection requests of peers in a tunnel
6586 * @param cls closure
6587 * @param client identification of the client
6588 * @param message the actual message (PeerControl)
6591 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6592 const struct GNUNET_MessageHeader *message)
6594 struct GNUNET_MESH_PeerControl *peer_msg;
6595 struct MeshPeerInfo *peer_info;
6596 struct MeshClient *c;
6597 struct MeshTunnel *t;
6598 MESH_TunnelNumber tid;
6600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6601 /* Sanity check for client registration */
6602 if (NULL == (c = client_get (client)))
6605 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6610 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6612 /* Sanity check for message size */
6613 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6616 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6620 /* Tunnel exists? */
6621 tid = ntohl (peer_msg->tunnel_id);
6622 t = tunnel_get_by_local_id (c, tid);
6626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6631 /* Does client own tunnel? */
6632 if (t->owner->handle != client)
6635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6640 GNUNET_i2s (&peer_msg->peer));
6641 /* Is the peer in the tunnel? */
6643 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6644 if (NULL == peer_info)
6647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6651 /* Ok, delete peer from tunnel */
6652 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6653 &peer_msg->peer.hashPubKey);
6655 send_destroy_path (t, peer_info->id);
6656 tunnel_delete_peer (t, peer_info->id);
6657 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6662 * Handler for blacklist requests of peers in a tunnel
6664 * @param cls closure
6665 * @param client identification of the client
6666 * @param message the actual message (PeerControl)
6668 * FIXME implement DHT block bloomfilter
6671 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6672 const struct GNUNET_MessageHeader *message)
6674 struct GNUNET_MESH_PeerControl *peer_msg;
6675 struct MeshClient *c;
6676 struct MeshTunnel *t;
6677 MESH_TunnelNumber tid;
6679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6680 /* Sanity check for client registration */
6681 if (NULL == (c = client_get (client)))
6684 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6689 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6691 /* Sanity check for message size */
6692 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6695 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6699 /* Tunnel exists? */
6700 tid = ntohl (peer_msg->tunnel_id);
6701 t = tunnel_get_by_local_id (c, tid);
6705 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6710 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6711 GNUNET_PEER_intern(&peer_msg->peer));
6716 * Handler for unblacklist requests of peers in a tunnel
6718 * @param cls closure
6719 * @param client identification of the client
6720 * @param message the actual message (PeerControl)
6723 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6724 const struct GNUNET_MessageHeader *message)
6726 struct GNUNET_MESH_PeerControl *peer_msg;
6727 struct MeshClient *c;
6728 struct MeshTunnel *t;
6729 MESH_TunnelNumber tid;
6733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6734 /* Sanity check for client registration */
6735 if (NULL == (c = client_get (client)))
6738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6743 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6745 /* Sanity check for message size */
6746 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6749 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6753 /* Tunnel exists? */
6754 tid = ntohl (peer_msg->tunnel_id);
6755 t = tunnel_get_by_local_id (c, tid);
6759 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6764 /* if peer is not known, complain */
6765 pid = GNUNET_PEER_search (&peer_msg->peer);
6772 /* search and remove from list */
6773 for (i = 0; i < t->nblacklisted; i++)
6775 if (t->blacklisted[i] == pid)
6777 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6778 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6783 /* if peer hasn't been blacklisted, complain */
6789 * Handler for connection requests to new peers by type
6791 * @param cls closure
6792 * @param client identification of the client
6793 * @param message the actual message (ConnectPeerByType)
6796 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6797 const struct GNUNET_MessageHeader *message)
6799 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6800 struct MeshClient *c;
6801 struct MeshTunnel *t;
6802 struct GNUNET_HashCode hash;
6803 MESH_TunnelNumber tid;
6805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6806 /* Sanity check for client registration */
6807 if (NULL == (c = client_get (client)))
6810 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6815 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6817 /* Sanity check for message size */
6818 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6819 ntohs (connect_msg->header.size))
6822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6826 /* Tunnel exists? */
6827 tid = ntohl (connect_msg->tunnel_id);
6828 t = tunnel_get_by_local_id (c, tid);
6832 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6836 /* Does client own tunnel? */
6837 if (t->owner->handle != client)
6840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6844 /* Do WE have the service? */
6845 t->type = ntohl (connect_msg->type);
6846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6847 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6848 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6851 /* Yes! Fast forward, add ourselves to the tunnel and send the
6852 * good news to the client, and alert the destination client of
6853 * an incoming tunnel.
6855 * FIXME send a path create to self, avoid code duplication
6857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6858 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6859 peer_info_get (&my_full_id),
6860 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6863 send_client_peer_connected (t, myid);
6864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6865 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6867 t->local_tid_dest = next_local_tid++;
6868 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6869 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6870 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6874 /* Ok, lets find a peer offering the service */
6875 if (NULL != t->dht_get_type)
6877 GNUNET_DHT_get_stop (t->dht_get_type);
6879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6880 GNUNET_h2s (&hash));
6882 GNUNET_DHT_get_start (dht_handle,
6883 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6885 dht_replication_level,
6886 GNUNET_DHT_RO_RECORD_ROUTE |
6887 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6889 &dht_get_type_handler, t);
6891 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6897 * Handler for connection requests to new peers by a string service description.
6899 * @param cls closure
6900 * @param client identification of the client
6901 * @param message the actual message, which includes messages the client wants
6904 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6905 const struct GNUNET_MessageHeader *message)
6907 struct GNUNET_MESH_ConnectPeerByString *msg;
6908 struct MeshRegexSearchContext *ctx;
6909 struct MeshRegexSearchInfo *info;
6910 struct GNUNET_DHT_GetHandle *get_h;
6911 struct GNUNET_HashCode key;
6912 struct MeshTunnel *t;
6913 struct MeshClient *c;
6914 MESH_TunnelNumber tid;
6918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6919 "Connect by string started\n");
6920 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6921 size = htons (message->size);
6923 /* Sanity check for client registration */
6924 if (NULL == (c = client_get (client)))
6927 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6932 /* Message size sanity check */
6933 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6936 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6940 /* Tunnel exists? */
6941 tid = ntohl (msg->tunnel_id);
6942 t = tunnel_get_by_local_id (c, tid);
6946 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6950 /* Does client own tunnel? */
6951 if (t->owner->handle != client)
6954 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6959 " on tunnel %s [%u]\n",
6960 GNUNET_i2s(&my_full_id),
6963 /* Only one connect_by_string allowed at the same time! */
6964 /* FIXME: allow more, return handle at api level to cancel, document */
6965 if (NULL != t->regex_ctx)
6968 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6972 /* Find string itself */
6973 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
6974 string = (const char *) &msg[1];
6976 /* Initialize context */
6977 size = GNUNET_REGEX_get_first_key(string, len, &key);
6978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6979 " consumed %u bits out of %u\n", size, len);
6980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6981 " looking for %s\n", GNUNET_h2s (&key));
6983 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
6985 info->description = GNUNET_malloc (len + 1);
6986 memcpy (info->description, string, len);
6987 info->description[len] = '\0';
6988 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
6989 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
6990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
6992 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
6993 ctx->position = size;
6997 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
6999 /* Start search in DHT */
7000 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7001 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7002 &key, /* key to search */
7003 dht_replication_level, /* replication level */
7004 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7005 NULL, /* xquery */ // FIXME BLOOMFILTER
7006 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7007 &dht_get_string_handler, ctx);
7009 GNUNET_break (GNUNET_OK ==
7010 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7013 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7015 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7021 * Handler for client traffic directed to one peer
7023 * @param cls closure
7024 * @param client identification of the client
7025 * @param message the actual message
7028 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7029 const struct GNUNET_MessageHeader *message)
7031 struct MeshClient *c;
7032 struct MeshTunnel *t;
7033 struct MeshPeerInfo *pi;
7034 struct GNUNET_MESH_Unicast *data_msg;
7035 MESH_TunnelNumber tid;
7038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7039 "Got a unicast request from a client!\n");
7041 /* Sanity check for client registration */
7042 if (NULL == (c = client_get (client)))
7045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7050 data_msg = (struct GNUNET_MESH_Unicast *) message;
7052 /* Sanity check for message size */
7053 size = ntohs (message->size);
7054 if (sizeof (struct GNUNET_MESH_Unicast) +
7055 sizeof (struct GNUNET_MessageHeader) > size)
7058 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7062 /* Tunnel exists? */
7063 tid = ntohl (data_msg->tid);
7064 t = tunnel_get_by_local_id (c, tid);
7068 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7072 /* Is it a local tunnel? Then, does client own the tunnel? */
7073 if (t->owner->handle != client)
7076 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7080 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7081 &data_msg->destination.hashPubKey);
7082 /* Is the selected peer in the tunnel? */
7086 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7090 /* PID should be as expected */
7091 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7094 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7095 "Unicast PID, expected %u, got %u\n",
7096 t->fwd_pid + 1, ntohl (data_msg->pid));
7097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7101 /* Ok, everything is correct, send the message
7102 * (pretend we got it from a mesh peer)
7105 /* Work around const limitation */
7106 char buf[ntohs (message->size)] GNUNET_ALIGN;
7107 struct GNUNET_MESH_Unicast *copy;
7109 copy = (struct GNUNET_MESH_Unicast *) buf;
7110 memcpy (buf, data_msg, size);
7111 copy->oid = my_full_id;
7112 copy->tid = htonl (t->id.tid);
7113 copy->ttl = htonl (default_ttl);
7114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7115 " calling generic handler...\n");
7116 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7119 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7126 * Handler for client traffic directed to the origin
7128 * @param cls closure
7129 * @param client identification of the client
7130 * @param message the actual message
7133 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7134 const struct GNUNET_MessageHeader *message)
7136 struct GNUNET_MESH_ToOrigin *data_msg;
7137 struct MeshTunnelClientInfo *clinfo;
7138 struct MeshClient *c;
7139 struct MeshTunnel *t;
7140 MESH_TunnelNumber tid;
7143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7144 "Got a ToOrigin request from a client!\n");
7145 /* Sanity check for client registration */
7146 if (NULL == (c = client_get (client)))
7149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7154 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7156 /* Sanity check for message size */
7157 size = ntohs (message->size);
7158 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7159 sizeof (struct GNUNET_MessageHeader) > size)
7162 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7166 /* Tunnel exists? */
7167 tid = ntohl (data_msg->tid);
7168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7169 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7172 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7175 t = tunnel_get_by_local_id (c, tid);
7178 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7179 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7181 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7185 /* It should be sent by someone who has this as incoming tunnel. */
7186 if (GNUNET_NO == client_knows_tunnel (c, t))
7189 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7193 /* PID should be as expected */
7194 clinfo = tunnel_get_client_fc (t, c);
7195 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7198 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7199 "To Origin PID, expected %u, got %u\n",
7200 clinfo->bck_pid + 1,
7201 ntohl (data_msg->pid));
7202 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7206 /* Ok, everything is correct, send the message
7207 * (pretend we got it from a mesh peer)
7211 char buf[ntohs (message->size)] GNUNET_ALIGN;
7212 struct GNUNET_MESH_ToOrigin *copy;
7214 /* Work around const limitation */
7215 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7216 memcpy (buf, data_msg, size);
7217 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7218 copy->tid = htonl (t->id.tid);
7219 copy->ttl = htonl (default_ttl);
7220 if (ntohl (copy->pid) != (t->bck_pid + 1))
7223 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7224 "To Origin PID, expected %u, got %u\n",
7230 copy->sender = my_full_id;
7231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7232 " calling generic handler...\n");
7233 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7235 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7242 * Handler for client traffic directed to all peers in a tunnel
7244 * @param cls closure
7245 * @param client identification of the client
7246 * @param message the actual message
7249 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7250 const struct GNUNET_MessageHeader *message)
7252 struct MeshClient *c;
7253 struct MeshTunnel *t;
7254 struct GNUNET_MESH_Multicast *data_msg;
7255 MESH_TunnelNumber tid;
7257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7258 "Got a multicast request from a client!\n");
7260 /* Sanity check for client registration */
7261 if (NULL == (c = client_get (client)))
7264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7269 data_msg = (struct GNUNET_MESH_Multicast *) message;
7271 /* Sanity check for message size */
7272 if (sizeof (struct GNUNET_MESH_Multicast) +
7273 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7276 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7280 /* Tunnel exists? */
7281 tid = ntohl (data_msg->tid);
7282 t = tunnel_get_by_local_id (c, tid);
7286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7287 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7289 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7293 /* Does client own tunnel? */
7294 if (t->owner->handle != client)
7297 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7301 /* PID should be as expected */
7302 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7306 "Multicast PID, expected %u, got %u\n",
7307 t->fwd_pid + 1, ntohl (data_msg->pid));
7308 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7313 char buf[ntohs (message->size)] GNUNET_ALIGN;
7314 struct GNUNET_MESH_Multicast *copy;
7316 copy = (struct GNUNET_MESH_Multicast *) buf;
7317 memcpy (buf, message, ntohs (message->size));
7318 copy->oid = my_full_id;
7319 copy->tid = htonl (t->id.tid);
7320 copy->ttl = htonl (default_ttl);
7321 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7323 " calling generic handler...\n");
7324 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7327 /* receive done gets called when last copy is sent to a neighbor */
7333 * Handler for client's ACKs for payload traffic.
7335 * @param cls Closure (unused).
7336 * @param client Identification of the client.
7337 * @param message The actual message.
7340 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7341 const struct GNUNET_MessageHeader *message)
7343 struct GNUNET_MESH_LocalAck *msg;
7344 struct MeshTunnel *t;
7345 struct MeshClient *c;
7346 MESH_TunnelNumber tid;
7349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7350 /* Sanity check for client registration */
7351 if (NULL == (c = client_get (client)))
7354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7359 msg = (struct GNUNET_MESH_LocalAck *) message;
7361 /* Tunnel exists? */
7362 tid = ntohl (msg->tunnel_id);
7363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7364 t = tunnel_get_by_local_id (c, tid);
7368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7374 ack = ntohl (msg->max_pid);
7375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7377 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7378 if (NULL != t->owner && t->owner->handle == client)
7380 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7382 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7386 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7387 tunnel_set_client_fwd_ack (t, c, ack);
7388 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7398 * Functions to handle messages from clients
7400 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7401 {&handle_local_new_client, NULL,
7402 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7403 {&handle_local_announce_regex, NULL,
7404 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7405 {&handle_local_tunnel_create, NULL,
7406 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7407 sizeof (struct GNUNET_MESH_TunnelMessage)},
7408 {&handle_local_tunnel_destroy, NULL,
7409 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7410 sizeof (struct GNUNET_MESH_TunnelMessage)},
7411 {&handle_local_tunnel_speed, NULL,
7412 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7413 sizeof (struct GNUNET_MESH_TunnelMessage)},
7414 {&handle_local_tunnel_speed, NULL,
7415 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7416 sizeof (struct GNUNET_MESH_TunnelMessage)},
7417 {&handle_local_tunnel_buffer, NULL,
7418 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7419 sizeof (struct GNUNET_MESH_TunnelMessage)},
7420 {&handle_local_tunnel_buffer, NULL,
7421 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7422 sizeof (struct GNUNET_MESH_TunnelMessage)},
7423 {&handle_local_connect_add, NULL,
7424 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7425 sizeof (struct GNUNET_MESH_PeerControl)},
7426 {&handle_local_connect_del, NULL,
7427 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7428 sizeof (struct GNUNET_MESH_PeerControl)},
7429 {&handle_local_blacklist, NULL,
7430 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7431 sizeof (struct GNUNET_MESH_PeerControl)},
7432 {&handle_local_unblacklist, NULL,
7433 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7434 sizeof (struct GNUNET_MESH_PeerControl)},
7435 {&handle_local_connect_by_type, NULL,
7436 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7437 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7438 {&handle_local_connect_by_string, NULL,
7439 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7440 {&handle_local_unicast, NULL,
7441 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7442 {&handle_local_to_origin, NULL,
7443 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7444 {&handle_local_multicast, NULL,
7445 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7446 {&handle_local_ack, NULL,
7447 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7448 sizeof (struct GNUNET_MESH_LocalAck)},
7454 * To be called on core init/fail.
7456 * @param cls service closure
7457 * @param server handle to the server for this service
7458 * @param identity the public identity of this peer
7461 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7462 const struct GNUNET_PeerIdentity *identity)
7465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7466 core_handle = server;
7467 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7470 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7471 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7473 GNUNET_abort(); // Try harder
7480 * Method called whenever a given peer connects.
7482 * @param cls closure
7483 * @param peer peer identity this notification is about
7484 * @param atsi performance data for the connection
7485 * @param atsi_count number of records in 'atsi'
7488 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7489 const struct GNUNET_ATS_Information *atsi,
7490 unsigned int atsi_count)
7492 struct MeshPeerInfo *peer_info;
7493 struct MeshPeerPath *path;
7495 DEBUG_CONN ("Peer connected\n");
7496 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7497 peer_info = peer_info_get (peer);
7498 if (myid == peer_info->id)
7500 DEBUG_CONN (" (self)\n");
7505 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7507 path = path_new (2);
7508 path->peers[0] = myid;
7509 path->peers[1] = peer_info->id;
7510 GNUNET_PEER_change_rc (myid, 1);
7511 GNUNET_PEER_change_rc (peer_info->id, 1);
7512 peer_info_add_path (peer_info, path, GNUNET_YES);
7513 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7519 * Method called whenever a peer disconnects.
7521 * @param cls closure
7522 * @param peer peer identity this notification is about
7525 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7527 struct MeshPeerInfo *pi;
7528 struct MeshPeerQueue *q;
7529 struct MeshPeerQueue *n;
7531 DEBUG_CONN ("Peer disconnected\n");
7532 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7544 /* try to reroute this traffic instead */
7545 queue_destroy(q, GNUNET_YES);
7549 peer_info_remove_path (pi, pi->id, myid);
7552 DEBUG_CONN (" (self)\n");
7554 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7559 /******************************************************************************/
7560 /************************ MAIN FUNCTIONS ****************************/
7561 /******************************************************************************/
7564 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7566 * @param cls closure
7567 * @param key current key code
7568 * @param value value in the hash map
7569 * @return GNUNET_YES if we should continue to iterate,
7573 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7575 struct MeshTunnel *t = value;
7582 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7584 * @param cls closure
7585 * @param key current key code
7586 * @param value value in the hash map
7587 * @return GNUNET_YES if we should continue to iterate,
7591 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7593 struct MeshPeerInfo *p = value;
7594 struct MeshPeerQueue *q;
7595 struct MeshPeerQueue *n;
7603 queue_destroy(q, GNUNET_YES);
7607 peer_info_destroy (p);
7612 * Task run during shutdown.
7618 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7622 if (core_handle != NULL)
7624 GNUNET_CORE_disconnect (core_handle);
7627 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7628 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7629 if (dht_handle != NULL)
7631 GNUNET_DHT_disconnect (dht_handle);
7636 GNUNET_SERVER_notification_context_destroy (nc);
7639 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7641 GNUNET_SCHEDULER_cancel (announce_id_task);
7642 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7648 * Process mesh requests.
7650 * @param cls closure
7651 * @param server the initialized server
7652 * @param c configuration to use
7655 run (void *cls, struct GNUNET_SERVER_Handle *server,
7656 const struct GNUNET_CONFIGURATION_Handle *c)
7658 struct MeshPeerInfo *peer;
7659 struct MeshPeerPath *p;
7662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7663 server_handle = server;
7664 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7665 NULL, /* Closure passed to MESH functions */
7666 &core_init, /* Call core_init once connected */
7667 &core_connect, /* Handle connects */
7668 &core_disconnect, /* remove peers on disconnects */
7669 NULL, /* Don't notify about all incoming messages */
7670 GNUNET_NO, /* For header only in notification */
7671 NULL, /* Don't notify about all outbound messages */
7672 GNUNET_NO, /* For header-only out notification */
7673 core_handlers); /* Register these handlers */
7675 if (core_handle == NULL)
7678 GNUNET_SCHEDULER_shutdown ();
7683 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7686 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7688 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7690 GNUNET_SCHEDULER_shutdown ();
7695 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7696 &refresh_path_time))
7698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7700 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7701 "refresh path time");
7702 GNUNET_SCHEDULER_shutdown ();
7707 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7708 &app_announce_time))
7710 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7712 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7713 "app announce time");
7714 GNUNET_SCHEDULER_shutdown ();
7719 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7722 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7724 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7725 "id announce time");
7726 GNUNET_SCHEDULER_shutdown ();
7731 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7732 &unacknowledged_wait_time))
7734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7736 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7737 "unacknowledged wait time");
7738 GNUNET_SCHEDULER_shutdown ();
7743 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7746 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7748 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7750 GNUNET_SCHEDULER_shutdown ();
7755 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7758 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7760 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7762 GNUNET_SCHEDULER_shutdown ();
7767 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7770 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7772 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7774 GNUNET_SCHEDULER_shutdown ();
7779 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7782 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7784 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7790 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7791 &dht_replication_level))
7793 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7795 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7796 "dht replication level", 10);
7797 dht_replication_level = 10;
7801 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7802 GNUNET_free (keyfile);
7803 if (my_private_key == NULL)
7805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7806 _("Mesh service could not access hostkey. Exiting.\n"));
7807 GNUNET_SCHEDULER_shutdown ();
7810 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7811 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7812 &my_full_id.hashPubKey);
7813 myid = GNUNET_PEER_intern (&my_full_id);
7814 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7815 "Mesh for peer [%s] starting\n",
7816 GNUNET_i2s(&my_full_id));
7818 // transport_handle = GNUNET_TRANSPORT_connect(c,
7825 dht_handle = GNUNET_DHT_connect (c, 64);
7826 if (dht_handle == NULL)
7831 stats = GNUNET_STATISTICS_create ("mesh", c);
7835 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7837 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7838 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7839 peers = GNUNET_CONTAINER_multihashmap_create (32);
7840 applications = GNUNET_CONTAINER_multihashmap_create (32);
7841 types = GNUNET_CONTAINER_multihashmap_create (32);
7843 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7844 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7845 GNUNET_SERVER_disconnect_notify (server_handle,
7846 &handle_local_client_disconnect, NULL);
7850 clients_tail = NULL;
7853 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7854 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7856 /* Create a peer_info for the local peer */
7857 peer = peer_info_get (&my_full_id);
7860 GNUNET_PEER_change_rc (myid, 1);
7861 peer_info_add_path (peer, p, GNUNET_YES);
7863 /* Scheduled the task to clean up when shutdown is called */
7864 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7871 * The main function for the mesh service.
7873 * @param argc number of arguments from the command line
7874 * @param argv command line arguments
7875 * @return 0 ok, 1 on error
7878 main (int argc, char *const *argv)
7882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7885 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");