2 This file is part of GNUnet.
3 (C) 2001-2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
64 #if MESH_DEBUG_CONNECTION
65 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_CONN(...)
71 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
73 #define DEBUG_DHT(...)
80 struct timespec __mesh_start;
81 struct timespec __mesh_end;
82 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
83 #define INTERVAL_END \
85 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
86 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
87 if (__diff < 0) __diff += 1000000000;\
91 #define INTERVAL_SHOW \
93 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
95 #define INTERVAL_START
100 /******************************************************************************/
101 /************************ DATA STRUCTURES ****************************/
102 /******************************************************************************/
104 /** FWD declaration */
110 * Struct representing a piece of data being sent to other peers
114 /** Tunnel it belongs to. */
115 struct MeshTunnel *t;
117 /** How many remaining neighbors still hav't got it. */
118 unsigned int reference_counter;
120 /** How many remaining neighbors we need to send this to. */
121 unsigned int total_out;
123 /** Size of the data. */
132 * Struct containing info about a queued transmission to this peer
139 struct MeshPeerQueue *next;
144 struct MeshPeerQueue *prev;
147 * Peer this transmission is directed to.
149 struct MeshPeerInfo *peer;
152 * Tunnel this message belongs to.
154 struct MeshTunnel *tunnel;
157 * Pointer to info stucture used as cls.
167 * Size of the message
174 * Struct containing all info possibly needed to build a package when called
177 struct MeshTransmissionDescriptor
179 /** ID of the tunnel this packet travels in */
180 struct MESH_TunnelID *origin;
182 /** Who was this message being sent to */
183 struct MeshPeerInfo *peer;
185 /** Ultimate destination of the packet */
186 GNUNET_PEER_Id destination;
188 /** Data descriptor */
189 struct MeshData* mesh_data;
194 * Struct containing all information regarding a given peer
204 * Last time we heard from this peer
206 struct GNUNET_TIME_Absolute last_contact;
209 * Number of attempts to reconnect so far
211 int n_reconnect_attempts;
214 * Paths to reach the peer, ordered by ascending hop count
216 struct MeshPeerPath *path_head;
219 * Paths to reach the peer, ordered by ascending hop count
221 struct MeshPeerPath *path_tail;
224 * Handle to stop the DHT search for a path to this peer
226 struct GNUNET_DHT_GetHandle *dhtget;
229 * Closure given to the DHT GET
231 struct MeshPathInfo *dhtgetcls;
234 * Array of tunnels this peer participates in
235 * (most probably a small amount, therefore not a hashmap)
236 * When the path to the peer changes, notify these tunnels to let them
237 * re-adjust their path trees.
239 struct MeshTunnel **tunnels;
242 * Number of tunnels this peers participates in
244 unsigned int ntunnels;
247 * Transmission queue to core DLL head
249 struct MeshPeerQueue *queue_head;
252 * Transmission queue to core DLL tail
254 struct MeshPeerQueue *queue_tail;
257 * How many messages are in the queue to this peer.
259 unsigned int queue_n;
262 * Handle to for queued transmissions
264 struct GNUNET_CORE_TransmitHandle *core_transmit;
269 * Globally unique tunnel identification (owner + number)
270 * DO NOT USE OVER THE NETWORK
275 * Node that owns the tunnel
280 * Tunnel number to differentiate all the tunnels owned by the node oid
281 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
283 MESH_TunnelNumber tid;
288 * Struct containing all information regarding a tunnel
289 * For an intermediate node the improtant info used will be:
290 * - id Tunnel unique identification
291 * - paths[0] To know where to send it next
292 * - metainfo: ready, speeds, accounting
299 struct MESH_TunnelID id;
302 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
304 MESH_TunnelNumber local_tid;
307 * Local tunnel number for local destination clients (incoming number)
308 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
311 MESH_TunnelNumber local_tid_dest;
314 * Is the speed on the tunnel limited to the slowest peer?
319 * Is the tunnel bufferless (minimum latency)?
324 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
329 * Packet ID of the last bck packet sent (unique counter per hop).
334 * SKIP value for this tunnel.
339 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
340 * Contains the Flow Control info: FWD ACK value received,
341 * last BCK ACK sent, PID and SKIP values.
343 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
346 * Last ACK sent towards the origin (for traffic towards leaf node).
348 uint32_t last_fwd_ack;
351 * BCK ACK value received from the hop towards the owner of the tunnel,
352 * (previous node / owner): up to what message PID can we sent back to him.
357 * How many messages are in the forward queue (towards leaves).
359 unsigned int fwd_queue_n;
362 * How many messages do we accept in the forward queue.
364 unsigned int fwd_queue_max;
367 * How many messages are in the backward queue (towards origin).
369 unsigned int bck_queue_n;
372 * How many messages do we accept in the backward queue.
374 unsigned int bck_queue_max;
377 * Task to poll peer in case of a stall.
379 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
382 * Last time the tunnel was used
384 struct GNUNET_TIME_Absolute timestamp;
387 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
388 * containing peers added by id or by type, not intermediate peers.
390 struct GNUNET_CONTAINER_MultiHashMap *peers;
393 * Number of peers that are connected and potentially ready to receive data
395 unsigned int peers_ready;
398 * Number of peers that have been added to the tunnel
400 unsigned int peers_total;
403 * Client owner of the tunnel, if any
405 struct MeshClient *owner;
408 * Clients that have been informed about and want to stay in the tunnel.
410 struct MeshClient **clients;
413 * Flow control info for each client.
415 struct MeshTunnelClientInfo *clients_fc;
418 * Number of elements in clients/clients_fc
420 unsigned int nclients;
423 * Clients that have been informed but requested to leave the tunnel.
425 struct MeshClient **ignore;
428 * Number of elements in clients
430 unsigned int nignore;
435 GNUNET_PEER_Id *blacklisted;
438 * Number of elements in blacklisted
440 unsigned int nblacklisted;
443 * Bloomfilter (for peer identities) to stop circular routes
445 char bloomfilter[MESH_BLOOM_SIZE];
450 struct MeshTunnelTree *tree;
453 * Application type we are looking for in this tunnel
455 GNUNET_MESH_ApplicationType type;
458 * Used to search peers offering a service
460 struct GNUNET_DHT_GetHandle *dht_get_type;
463 * Initial context of the regex search for a connect_by_string
465 struct MeshRegexSearchContext *regex_ctx;
468 * Task to keep the used paths alive
470 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
473 * Task to destroy the tunnel after timeout
475 * FIXME: merge the two? a tunnel will have either
476 * a path refresh OR a timeout, never both!
478 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
481 * Flag to signal the destruction of the tunnel.
482 * If this is set GNUNET_YES the tunnel will be destroyed
483 * when the queue is empty.
490 * Info about a child node in a tunnel, needed to perform flow control.
492 struct MeshTunnelChildInfo
495 * ID of the child node.
510 * Maximum PID allowed (FWD ACK received).
515 * Last ACK sent to that child (BCK ACK).
520 * Circular buffer pointing to MeshPeerQueue elements for all
521 * payload traffic going to this child.
522 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
524 struct MeshPeerQueue **send_buffer;
527 * Index of the oldest element in the send_buffer.
529 unsigned int send_buffer_start;
532 * How many elements are already in the buffer.
534 unsigned int send_buffer_n;
537 * Tunnel this info is about
539 struct MeshTunnel *t;
542 * Task to poll peer in case of a stall.
544 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
549 * Info about a leaf client of a tunnel, needed to perform flow control.
551 struct MeshTunnelClientInfo
554 * PID of the last packet sent to the client (FWD).
559 * PID of the last packet received from the client (BCK).
564 * Maximum PID allowed (FWD ACK received).
569 * Last ACK sent to that child (BCK ACK).
577 * Info collected during iteration of child nodes in order to get the ACK value
580 struct MeshTunnelChildIteratorContext
583 * Tunnel whose info is being collected.
585 struct MeshTunnel *t;
588 * Is this context initialized? Is the value in max_child_ack valid?
593 * Maximum child ACK so far.
595 uint32_t max_child_ack;
598 * Number of children nodes
600 unsigned int nchildren;
605 * Info needed to work with tunnel paths and peers
612 struct MeshTunnel *t;
615 * Neighbouring peer to whom we send the packet to
617 struct MeshPeerInfo *peer;
622 struct MeshPeerPath *path;
627 * Struct containing information about a client of the service
634 struct MeshClient *next;
639 struct MeshClient *prev;
642 * Tunnels that belong to this client, indexed by local id
644 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
647 * Tunnels this client has accepted, indexed by incoming local id
649 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
652 * Tunnels this client has rejected, indexed by incoming local id
654 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
656 * Handle to communicate with the client
658 struct GNUNET_SERVER_Client *handle;
661 * Applications that this client has claimed to provide
663 struct GNUNET_CONTAINER_MultiHashMap *apps;
666 * Messages that this client has declared interest in
668 struct GNUNET_CONTAINER_MultiHashMap *types;
671 * Whether the client is active or shutting down (don't send confirmations
672 * to a client that is shutting down.
677 * ID of the client, mainly for debug messages
682 * Regular expressions describing the services offered by this client.
684 char **regexes; // FIXME add timeout? API to remove a regex?
687 * Number of regular expressions in regexes.
689 unsigned int n_regex;
692 * Task to refresh all regular expresions in the DHT.
694 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
700 * Struct to keep information of searches of services described by a regex
701 * using a user-provided string service description.
703 struct MeshRegexSearchInfo
706 * Which tunnel is this for
708 struct MeshTunnel *t;
711 * User provided description of the searched service.
716 * Part of the description already consumed by the search.
723 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
726 * Results from running DHT GETs.
728 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
731 * Contexts, for each running DHT GET. Free all on end of search.
733 struct MeshRegexSearchContext **contexts;
736 * Number of contexts (branches/steps in search).
738 unsigned int n_contexts;
741 * Peer that is connecting via connect_by_string. When connected, free ctx.
746 * Other peers that are found but not yet being connected to.
748 GNUNET_PEER_Id *peers;
751 * Number of elements in peers.
753 unsigned int n_peers;
756 * Next peer to try to connect to.
761 * Timeout for a connect attempt.
762 * When reached, try to connect to a different peer, if any. If not,
763 * try the same peer again.
765 GNUNET_SCHEDULER_TaskIdentifier timeout;
770 * Struct to keep state of running searches that have consumed a part of
773 struct MeshRegexSearchContext
776 * Part of the description already consumed by
777 * this particular search branch.
782 * Information about the search.
784 struct MeshRegexSearchInfo *info;
787 * We just want to look for one edge, the longer the better.
790 unsigned int longest_match;
793 * Destination hash of the longest match.
795 struct GNUNET_HashCode hash;
798 /******************************************************************************/
799 /************************ DEBUG FUNCTIONS ****************************/
800 /******************************************************************************/
804 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
805 * @param cls string to print
806 * @param success GNUNET_OK if the PUT was transmitted,
807 * GNUNET_NO on timeout,
808 * GNUNET_SYSERR on disconnect from service
809 * after the PUT message was transmitted
810 * (so we don't know if it was received or not)
815 mesh_debug (void *cls, int success)
819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
823 unsigned int debug_fwd_ack;
824 unsigned int debug_bck_ack;
828 /******************************************************************************/
829 /*********************** GLOBAL VARIABLES ****************************/
830 /******************************************************************************/
833 * Configuration parameters
835 static struct GNUNET_TIME_Relative refresh_path_time;
836 static struct GNUNET_TIME_Relative app_announce_time;
837 static struct GNUNET_TIME_Relative id_announce_time;
838 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
839 static struct GNUNET_TIME_Relative connect_timeout;
840 static unsigned long long default_ttl;
841 static unsigned long long dht_replication_level;
842 static unsigned long long max_tunnels;
843 static unsigned long long max_msgs_queue;
847 * Hostkey generation context
849 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
852 * DLL with all the clients, head.
854 static struct MeshClient *clients;
857 * DLL with all the clients, tail.
859 static struct MeshClient *clients_tail;
862 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
864 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
867 * Number of tunnels known.
869 static unsigned long long n_tunnels;
872 * Tunnels incoming, indexed by MESH_TunnelNumber
873 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
875 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
878 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
880 static struct GNUNET_CONTAINER_MultiHashMap *peers;
883 * Handle to communicate with transport
885 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
888 * Handle to communicate with core.
890 static struct GNUNET_CORE_Handle *core_handle;
895 static struct GNUNET_DHT_Handle *dht_handle;
900 static struct GNUNET_SERVER_Handle *server_handle;
903 * Handle to the statistics service.
905 static struct GNUNET_STATISTICS_Handle *stats;
908 * Notification context, to send messages to local clients.
910 static struct GNUNET_SERVER_NotificationContext *nc;
913 * Local peer own ID (memory efficient handle).
915 static GNUNET_PEER_Id myid;
918 * Local peer own ID (full value).
920 static struct GNUNET_PeerIdentity my_full_id;
925 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
930 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
933 * Tunnel ID for the next created tunnel (global tunnel number).
935 static MESH_TunnelNumber next_tid;
938 * Tunnel ID for the next incoming tunnel (local tunnel number).
940 static MESH_TunnelNumber next_local_tid;
943 * All application types provided by this peer.
945 static struct GNUNET_CONTAINER_MultiHashMap *applications;
948 * All message types clients of this peer are interested in.
950 static struct GNUNET_CONTAINER_MultiHashMap *types;
953 * Task to periodically announce provided applications.
955 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
958 * Task to periodically announce itself in the network.
960 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
963 * Next ID to assign to a client.
965 unsigned int next_client_id;
968 /******************************************************************************/
969 /*********************** DECLARATIONS **************************/
970 /******************************************************************************/
973 * Function to process paths received for a new peer addition. The recorded
974 * paths form the initial tunnel, which can be optimized later.
975 * Called on each result obtained for the DHT search.
978 * @param exp when will this value expire
979 * @param key key of the result
980 * @param type type of the result
981 * @param size number of bytes in data
982 * @param data pointer to the result data
985 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
986 const struct GNUNET_HashCode * key,
987 const struct GNUNET_PeerIdentity *get_path,
988 unsigned int get_path_length,
989 const struct GNUNET_PeerIdentity *put_path,
990 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
991 size_t size, const void *data);
995 * Function to process DHT string to regex matching.
996 * Called on each result obtained for the DHT search.
998 * @param cls closure (search context)
999 * @param exp when will this value expire
1000 * @param key key of the result
1001 * @param get_path path of the get request (not used)
1002 * @param get_path_length lenght of get_path (not used)
1003 * @param put_path path of the put request (not used)
1004 * @param put_path_length length of the put_path (not used)
1005 * @param type type of the result
1006 * @param size number of bytes in data
1007 * @param data pointer to the result data
1009 * TODO: re-issue the request after certain time? cancel after X results?
1012 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1013 const struct GNUNET_HashCode * key,
1014 const struct GNUNET_PeerIdentity *get_path,
1015 unsigned int get_path_length,
1016 const struct GNUNET_PeerIdentity *put_path,
1017 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1018 size_t size, const void *data);
1022 * Function to process DHT string to regex matching.
1023 * Called on each result obtained for the DHT search.
1025 * @param cls closure (search context)
1026 * @param exp when will this value expire
1027 * @param key key of the result
1028 * @param get_path path of the get request (not used)
1029 * @param get_path_length lenght of get_path (not used)
1030 * @param put_path path of the put request (not used)
1031 * @param put_path_length length of the put_path (not used)
1032 * @param type type of the result
1033 * @param size number of bytes in data
1034 * @param data pointer to the result data
1037 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1038 const struct GNUNET_HashCode * key,
1039 const struct GNUNET_PeerIdentity *get_path,
1040 unsigned int get_path_length,
1041 const struct GNUNET_PeerIdentity *put_path,
1042 unsigned int put_path_length,
1043 enum GNUNET_BLOCK_Type type,
1044 size_t size, const void *data);
1048 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1049 * and insert it in the appropiate structures if the peer is not known yet.
1051 * @param peer Short identity of the peer.
1053 * @return Existing or newly created peer info.
1055 static struct MeshPeerInfo *
1056 peer_info_get_short (const GNUNET_PEER_Id peer);
1060 * Try to establish a new connection to this peer.
1061 * Use the best path for the given tunnel.
1062 * If the peer doesn't have any path to it yet, try to get one.
1063 * If the peer already has some path, send a CREATE PATH towards it.
1065 * @param peer PeerInfo of the peer.
1066 * @param t Tunnel for which to create the path, if possible.
1069 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1073 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1074 * needed rescources.
1075 * If peer already exists, reevaluate shortest path and change if different.
1077 * @param t Tunnel we want to add a new peer to
1078 * @param peer PeerInfo of the peer being added
1082 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1086 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1087 * that are no longer needed, as well as nodes of no longer reachable peers.
1088 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1090 * @param t Tunnel from which to remove the path.
1091 * @param peer Short id of the peer which should be removed.
1094 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1098 * Search for a tunnel by global ID using full PeerIdentities.
1100 * @param oid owner of the tunnel.
1101 * @param tid global tunnel number.
1103 * @return tunnel handler, NULL if doesn't exist.
1105 static struct MeshTunnel *
1106 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1110 * Delete an active client from the tunnel.
1116 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1119 * Notify a tunnel that a connection has broken that affects at least
1120 * some of its peers.
1122 * @param t Tunnel affected.
1123 * @param p1 Peer that got disconnected from p2.
1124 * @param p2 Peer that got disconnected from p1.
1126 * @return Short ID of the peer disconnected (either p1 or p2).
1127 * 0 if the tunnel remained unaffected.
1129 static GNUNET_PEER_Id
1130 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1135 * Get the current ack value for a tunnel, for data going from root to leaves,
1136 * taking in account the tunnel mode and the status of all children and clients.
1140 * @return Maximum PID allowed.
1143 tunnel_get_fwd_ack (struct MeshTunnel *t);
1147 * Add a client to a tunnel, initializing all needed data structures.
1149 * @param t Tunnel to which add the client.
1150 * @param c Client which to add to the tunnel.
1153 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1157 * Jump to the next edge, with the longest matching token.
1159 * @param block Block found in the DHT.
1160 * @param size Size of the block.
1161 * @param ctx Context of the search.
1163 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1166 regex_next_edge (const struct MeshRegexBlock *block,
1168 struct MeshRegexSearchContext *ctx);
1172 * Find a path to a peer that offers a regex servcie compatible
1173 * with a given string.
1175 * @param key The key of the accepting state.
1176 * @param ctx Context containing info about the string, tunnel, etc.
1179 regex_find_path (const struct GNUNET_HashCode *key,
1180 struct MeshRegexSearchContext *ctx);
1184 * @brief Queue and pass message to core when possible.
1186 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1187 * and accounts for it. In case the queue is full, the message is dropped and
1190 * Otherwise, message is treated as internal and allowed to go regardless of
1193 * @param cls Closure (@c type dependant). It will be used by queue_send to
1194 * build the message to be sent if not already prebuilt.
1195 * @param type Type of the message, 0 for a raw message.
1196 * @param size Size of the message.
1197 * @param dst Neighbor to send message to.
1198 * @param t Tunnel this message belongs to.
1201 queue_add (void *cls, uint16_t type, size_t size,
1202 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1206 * Free a transmission that was already queued with all resources
1207 * associated to the request.
1209 * @param queue Queue handler to cancel.
1210 * @param clear_cls Is it necessary to free associated cls?
1213 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1217 * @brief Get the next transmittable message from the queue.
1219 * This will be the head, except in the case of being a data packet
1220 * not allowed by the destination peer.
1222 * @param peer Destination peer.
1224 * @return The next viable MeshPeerQueue element to send to that peer.
1225 * NULL when there are no transmittable messages.
1227 struct MeshPeerQueue *
1228 queue_get_next (const struct MeshPeerInfo *peer);
1232 * Core callback to write a queued packet to core buffer
1234 * @param cls Closure (peer info).
1235 * @param size Number of bytes available in buf.
1236 * @param buf Where the to write the message.
1238 * @return number of bytes written to buf
1241 queue_send (void *cls, size_t size, void *buf);
1243 /******************************************************************************/
1244 /************************ ITERATORS ****************************/
1245 /******************************************************************************/
1248 * Iterator over found existing mesh regex blocks that match an ongoing search.
1250 * @param cls closure
1251 * @param key current key code
1252 * @param value value in the hash map
1253 * @return GNUNET_YES if we should continue to iterate,
1257 regex_result_iterator (void *cls,
1258 const struct GNUNET_HashCode * key,
1261 struct MeshRegexBlock *block = value;
1262 struct MeshRegexSearchContext *ctx = cls;
1264 if (GNUNET_YES == ntohl(block->accepting) &&
1265 ctx->position == strlen (ctx->info->description))
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1268 regex_find_path (key, ctx);
1269 return GNUNET_YES; // We found an accept state!
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1274 ctx->position, strlen(ctx->info->description),
1275 ntohl(block->accepting));
1278 regex_next_edge(block, SIZE_MAX, ctx);
1285 * Iterator over edges in a regex block retrieved from the DHT.
1287 * @param cls Closure (context of the search).
1288 * @param token Token that follows to next state.
1289 * @param len Lenght of token.
1290 * @param key Hash of next state.
1292 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1295 regex_edge_iterator (void *cls,
1298 const struct GNUNET_HashCode *key)
1300 struct MeshRegexSearchContext *ctx = cls;
1301 struct MeshRegexSearchInfo *info = ctx->info;
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1308 current = &info->description[ctx->position];
1309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1310 current_len = strlen (info->description) - ctx->position;
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1315 if (len > current_len)
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1318 return GNUNET_YES; // Token too long, wont match
1320 if (0 != strncmp (current, token, len))
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1323 return GNUNET_YES; // Token doesn't match
1326 if (len > ctx->longest_match)
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1329 ctx->longest_match = len;
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1343 * Jump to the next edge, with the longest matching token.
1345 * @param block Block found in the DHT.
1346 * @param size Size of the block.
1347 * @param ctx Context of the search.
1349 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1352 regex_next_edge (const struct MeshRegexBlock *block,
1354 struct MeshRegexSearchContext *ctx)
1356 struct MeshRegexSearchContext *new_ctx;
1357 struct MeshRegexSearchInfo *info = ctx->info;
1358 struct GNUNET_DHT_GetHandle *get_h;
1362 /* Find the longest match for the current string position,
1363 * among tokens in the given block */
1364 ctx->longest_match = 0;
1365 result = GNUNET_MESH_regex_block_iterate (block, size,
1366 ®ex_edge_iterator, ctx);
1367 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1369 /* Did anything match? */
1370 if (0 == ctx->longest_match)
1373 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1374 new_ctx->info = info;
1375 new_ctx->position = ctx->position + ctx->longest_match;
1376 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1378 /* Check whether we already have a DHT GET running for it */
1380 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1383 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1385 ®ex_result_iterator,
1387 return; // We are already looking for it
1390 /* Start search in DHT */
1392 GNUNET_DHT_get_start (dht_handle, /* handle */
1393 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1394 &ctx->hash, /* key to search */
1395 dht_replication_level, /* replication level */
1396 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1397 NULL, /* xquery */ // FIXME BLOOMFILTER
1398 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1399 &dht_get_string_handler, new_ctx);
1401 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1404 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1413 * Iterator over hash map entries to cancel DHT GET requests after a
1414 * successful connect_by_string.
1416 * @param cls Closure (unused).
1417 * @param key Current key code (unused).
1418 * @param value Value in the hash map (get handle).
1419 * @return GNUNET_YES if we should continue to iterate,
1423 regex_cancel_dht_get (void *cls,
1424 const struct GNUNET_HashCode * key,
1427 struct GNUNET_DHT_GetHandle *h = value;
1429 GNUNET_DHT_get_stop (h);
1435 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1436 * search for connect_by_string.
1438 * @param cls Closure (unused).
1439 * @param key Current key code (unused).
1440 * @param value MeshRegexBlock in the hash map.
1441 * @return GNUNET_YES if we should continue to iterate,
1445 regex_free_result (void *cls,
1446 const struct GNUNET_HashCode * key,
1450 GNUNET_free (value);
1456 * Regex callback iterator to store own service description in the DHT.
1458 * @param cls closure.
1459 * @param key hash for current state.
1460 * @param proof proof for current state.
1461 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1462 * @param num_edges number of edges leaving current state.
1463 * @param edges edges leaving current state.
1466 regex_iterator (void *cls,
1467 const struct GNUNET_HashCode *key,
1470 unsigned int num_edges,
1471 const struct GNUNET_REGEX_Edge *edges)
1473 struct MeshRegexBlock *block;
1474 struct MeshRegexEdge *block_edge;
1475 enum GNUNET_DHT_RouteOption opt;
1479 unsigned int offset;
1482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1483 " regex dht put for state %s\n",
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1492 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1493 if (GNUNET_YES == accepting)
1495 struct MeshRegexAccept block;
1497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 " state %s is accepting, putting own id\n",
1500 size = sizeof (block);
1502 block.id = my_full_id;
1504 GNUNET_DHT_put(dht_handle, key,
1505 dht_replication_level,
1506 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1507 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1510 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1515 len = strlen(proof);
1516 size = sizeof (struct MeshRegexBlock) + len;
1517 block = GNUNET_malloc (size);
1520 block->n_proof = htonl (len);
1521 block->n_edges = htonl (num_edges);
1522 block->accepting = htonl (accepting);
1524 /* Store the proof at the end of the block. */
1525 aux = (char *) &block[1];
1526 memcpy (aux, proof, len);
1529 /* Store each edge in a variable length MeshEdge struct at the
1530 * very end of the MeshRegexBlock structure.
1532 for (i = 0; i < num_edges; i++)
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1535 " edge %s towards %s\n",
1537 GNUNET_h2s(&edges[i].destination));
1539 /* aux points at the end of the last block */
1540 len = strlen (edges[i].label);
1541 size += sizeof (struct MeshRegexEdge) + len;
1542 // Calculate offset FIXME is this ok? use size instead?
1543 offset = aux - (char *) block;
1544 block = GNUNET_realloc (block, size);
1545 aux = &((char *) block)[offset];
1546 block_edge = (struct MeshRegexEdge *) aux;
1547 block_edge->key = edges[i].destination;
1548 block_edge->n_token = htonl (len);
1549 aux = (char *) &block_edge[1];
1550 memcpy (aux, edges[i].label, len);
1554 GNUNET_DHT_put(dht_handle, key,
1555 dht_replication_level,
1557 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1559 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1563 GNUNET_free (block);
1568 * Store the regular expression describing a local service into the DHT.
1570 * @param regex The regular expresion.
1573 regex_put (const char *regex)
1575 struct GNUNET_REGEX_Automaton *dfa;
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1578 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1579 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1580 GNUNET_REGEX_automaton_destroy (dfa);
1581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1586 * Find a path to a peer that offers a regex servcie compatible
1587 * with a given string.
1589 * @param key The key of the accepting state.
1590 * @param ctx Context containing info about the string, tunnel, etc.
1593 regex_find_path (const struct GNUNET_HashCode *key,
1594 struct MeshRegexSearchContext *ctx)
1596 struct GNUNET_DHT_GetHandle *get_h;
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1599 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1600 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1601 key, /* key to search */
1602 dht_replication_level, /* replication level */
1603 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1604 GNUNET_DHT_RO_RECORD_ROUTE,
1605 NULL, /* xquery */ // FIXME BLOOMFILTER
1606 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1607 &dht_get_string_accept_handler, ctx);
1608 GNUNET_break (GNUNET_OK ==
1609 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1612 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1617 * Function called if the connect attempt to a peer found via
1618 * connect_by_string times out. Try to connect to another peer, if any.
1619 * Otherwise try to reconnect to the same peer.
1621 * @param cls Closure (info about regex search).
1622 * @param tc TaskContext.
1625 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1627 struct MeshRegexSearchInfo *info = cls;
1628 struct MeshPeerInfo *peer_info;
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1633 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1634 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1642 if (0 < info->n_peers)
1644 // Select next peer, put current in that spot.
1645 id = info->peers[info->i_peer];
1646 info->peers[info->i_peer] = info->peer;
1647 info->i_peer = (info->i_peer + 1) % info->n_peers;
1651 // Try to connect to same peer again.
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1656 peer_info = peer_info_get_short(id);
1657 tunnel_add_peer (info->t, peer_info);
1659 tunnel_delete_peer (info->t, old);
1660 peer_info_connect (peer_info, info->t);
1661 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1662 ®ex_connect_timeout,
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1669 * Cancel an ongoing regex search in the DHT and free all resources.
1671 * @param ctx The search context.
1674 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1676 struct MeshRegexSearchInfo *info = ctx->info;
1679 GNUNET_free (info->description);
1680 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1681 ®ex_cancel_dht_get, NULL);
1682 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1683 ®ex_free_result, NULL);
1684 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1685 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1686 info->t->regex_ctx = NULL;
1687 for (i = 0; i < info->n_contexts; i++)
1689 GNUNET_free (info->contexts[i]);
1691 if (0 < info->n_contexts)
1692 GNUNET_free (info->contexts);
1693 if (0 < info->n_peers)
1694 GNUNET_free (info->peers);
1695 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1697 GNUNET_SCHEDULER_cancel(info->timeout);
1703 /******************************************************************************/
1704 /************************ PERIODIC FUNCTIONS ****************************/
1705 /******************************************************************************/
1708 * Announce iterator over for each application provided by the peer
1710 * @param cls closure
1711 * @param key current key code
1712 * @param value value in the hash map
1713 * @return GNUNET_YES if we should continue to
1718 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1720 struct PBlock block;
1721 struct MeshClient *c;
1723 block.id = my_full_id;
1724 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1725 GNUNET_assert(NULL != c);
1726 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1727 if (0 == block.type)
1732 block.type = htonl (block.type);
1734 GNUNET_break (NULL !=
1735 GNUNET_DHT_put (dht_handle, key,
1736 dht_replication_level,
1737 GNUNET_DHT_RO_RECORD_ROUTE |
1738 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1739 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1741 (const char *) &block,
1742 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1744 app_announce_time, NULL, NULL));
1750 * Periodically announce what applications are provided by local clients
1753 * @param cls closure
1754 * @param tc task context
1757 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1759 struct MeshClient *c = cls;
1762 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1764 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1768 DEBUG_DHT ("Starting PUT for regex\n");
1770 for (i = 0; i < c->n_regex; i++)
1772 regex_put (c->regexes[i]);
1774 c->regex_announce_task =
1775 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1776 DEBUG_DHT ("Finished PUT for regex\n");
1783 * Periodically announce what applications are provided by local clients
1786 * @param cls closure
1787 * @param tc task context
1790 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1792 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1794 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1798 DEBUG_DHT ("Starting PUT for apps\n");
1800 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1802 announce_applications_task =
1803 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1805 DEBUG_DHT ("Finished PUT for apps\n");
1812 * Periodically announce self id in the DHT
1814 * @param cls closure
1815 * @param tc task context
1818 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1820 struct PBlock block;
1822 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1824 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1828 * - Set data expiration in function of X
1829 * - Adapt X to churn
1831 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1833 block.id = my_full_id;
1834 block.type = htonl (0);
1835 GNUNET_DHT_put (dht_handle, /* DHT handle */
1836 &my_full_id.hashPubKey, /* Key to use */
1837 dht_replication_level, /* Replication level */
1838 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1839 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1840 sizeof (block), /* Size of the data */
1841 (const char *) &block, /* Data itself */
1842 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1843 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1844 NULL, /* Continuation */
1845 NULL); /* Continuation closure */
1847 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1851 /******************************************************************************/
1852 /****************** GENERAL HELPER FUNCTIONS ************************/
1853 /******************************************************************************/
1856 * Decrements the reference counter and frees all resources if needed
1858 * @param mesh_data Data Descriptor used in a multicast message.
1859 * Freed no longer needed (last message).
1862 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1864 if (0 == --(mesh_data->reference_counter))
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1867 GNUNET_free (mesh_data->data);
1868 GNUNET_free (mesh_data);
1874 * Check if client has registered with the service and has not disconnected
1876 * @param client the client to check
1878 * @return non-NULL if client exists in the global DLL
1880 static struct MeshClient *
1881 client_get (struct GNUNET_SERVER_Client *client)
1883 struct MeshClient *c;
1888 if (c->handle == client)
1897 * Checks if a given client has subscribed to certain message type
1899 * @param message_type Type of message to check
1900 * @param c Client to check
1902 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1904 * FIXME: use of crypto_hash slows it down
1905 * The hash function alone takes 8-10us out of the ~55us for the whole
1906 * process of retransmitting the message from one local client to another.
1907 * Find faster implementation!
1910 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1912 struct GNUNET_HashCode hc;
1914 if (NULL == c->types)
1917 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1918 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1923 * Check whether client wants traffic from a tunnel.
1925 * @param c Client to check.
1926 * @param t Tunnel to be found.
1928 * @return GNUNET_YES if client knows tunnel.
1930 * TODO look in client hashmap
1933 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1937 for (i = 0; i < t->nclients; i++)
1938 if (t->clients[i] == c)
1945 * Check whether client has been informed about a tunnel.
1947 * @param c Client to check.
1948 * @param t Tunnel to be found.
1950 * @return GNUNET_YES if client knows tunnel.
1952 * TODO look in client hashmap
1955 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1959 for (i = 0; i < t->nignore; i++)
1960 if (t->ignore[i] == c)
1962 return client_wants_tunnel(c, t);
1967 * Marks a client as uninterested in traffic from the tunnel, updating both
1968 * client and tunnel to reflect this.
1970 * @param c Client that doesn't want traffic anymore.
1971 * @param t Tunnel which should be ignored.
1973 * FIXME when to delete an incoming tunnel?
1976 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1978 struct GNUNET_HashCode hash;
1980 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1981 GNUNET_break (GNUNET_YES ==
1982 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1984 GNUNET_break (GNUNET_YES ==
1985 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1986 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1987 tunnel_delete_active_client (t, c);
1988 GNUNET_array_append (t->ignore, t->nignore, c);
1993 * Deletes a tunnel from a client (either owner or destination). To be used on
1994 * tunnel destroy, otherwise, use client_ignore_tunnel.
1996 * @param c Client whose tunnel to delete.
1997 * @param t Tunnel which should be deleted.
2000 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2002 struct GNUNET_HashCode hash;
2006 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2007 GNUNET_assert (GNUNET_YES ==
2008 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2014 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2016 GNUNET_assert (GNUNET_YES ==
2017 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2021 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2029 * Send the message to all clients that have subscribed to its type
2031 * @param msg Pointer to the message itself
2032 * @param payload Pointer to the payload of the message.
2033 * @param t The tunnel to whose clients this message goes.
2035 * @return number of clients this message was sent to
2038 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2039 const struct GNUNET_MessageHeader *payload,
2040 struct MeshTunnel *t)
2042 struct MeshClient *c;
2043 MESH_TunnelNumber *tid;
2046 char cbuf[htons (msg->size)];
2048 type = ntohs (payload->type);
2049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2051 GNUNET_MESH_DEBUG_M2S (type));
2053 memcpy (cbuf, msg, sizeof (cbuf));
2054 switch (htons (msg->type))
2056 struct GNUNET_MESH_Unicast *uc;
2057 struct GNUNET_MESH_Multicast *mc;
2058 struct GNUNET_MESH_ToOrigin *to;
2060 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2061 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2064 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2065 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2068 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2069 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2077 for (count = 0, c = clients; c != NULL; c = c->next)
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2080 if (client_is_subscribed (type, c))
2082 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2086 *tid = htonl (t->local_tid);
2090 if (GNUNET_NO == client_knows_tunnel (c, t))
2092 /* This client doesn't know the tunnel */
2093 struct GNUNET_MESH_TunnelNotification tmsg;
2094 struct GNUNET_HashCode hash;
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2097 tmsg.header.size = htons (sizeof (tmsg));
2098 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2099 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2100 tmsg.tunnel_id = htonl (t->local_tid_dest);
2102 if (GNUNET_YES == t->speed_min)
2103 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2104 if (GNUNET_YES == t->nobuffer)
2105 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2106 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2107 &tmsg.header, GNUNET_NO);
2108 tunnel_add_client (t, c);
2109 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2111 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2112 c->incoming_tunnels, &hash, t,
2113 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2115 *tid = htonl (t->local_tid_dest);
2118 /* Check if the client wants to get traffic from the tunnel */
2119 if (GNUNET_NO == client_wants_tunnel(c, t))
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2123 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2124 (struct GNUNET_MessageHeader
2125 *) cbuf, GNUNET_NO);
2134 * Notify the client that owns the tunnel that a peer has connected to it
2135 * (the requested path to it has been confirmed).
2137 * @param t Tunnel whose owner to notify
2138 * @param id Short id of the peer that has connected
2141 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2143 struct GNUNET_MESH_PeerControl pc;
2145 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2146 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2147 pc.tunnel_id = htonl (t->local_tid);
2148 GNUNET_PEER_resolve (id, &pc.peer);
2149 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2155 * Notify all clients (not depending on registration status) that the incoming
2156 * tunnel is no longer valid.
2158 * @param t Tunnel that was destroyed.
2161 send_clients_tunnel_destroy (struct MeshTunnel *t)
2163 struct GNUNET_MESH_TunnelMessage msg;
2165 msg.header.size = htons (sizeof (msg));
2166 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2167 msg.tunnel_id = htonl (t->local_tid_dest);
2168 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2173 * Notify clients of tunnel disconnections, if needed.
2174 * In case the origin disconnects, the destination clients get a tunnel destroy
2175 * notification. If the last destination disconnects (only one remaining client
2176 * in tunnel), the origin gets a (local ID) peer disconnected.
2177 * Note that the function must be called BEFORE removing the client from
2180 * @param t Tunnel that was destroyed.
2181 * @param c Client that disconnected.
2184 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2190 struct GNUNET_MESH_TunnelMessage msg;
2192 msg.header.size = htons (sizeof (msg));
2193 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2194 msg.tunnel_id = htonl (t->local_tid_dest);
2195 for (i = 0; i < t->nclients; i++)
2196 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2197 &msg.header, GNUNET_NO);
2199 // FIXME when to disconnect an incoming tunnel?
2200 else if (1 == t->nclients && NULL != t->owner)
2202 struct GNUNET_MESH_PeerControl msg;
2204 msg.header.size = htons (sizeof (msg));
2205 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2206 msg.tunnel_id = htonl (t->local_tid);
2207 msg.peer = my_full_id;
2208 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2209 &msg.header, GNUNET_NO);
2215 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2216 * and insert it in the appropiate structures if the peer is not known yet.
2218 * @param peer Full identity of the peer.
2220 * @return Existing or newly created peer info.
2222 static struct MeshPeerInfo *
2223 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2225 struct MeshPeerInfo *peer_info;
2227 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2228 if (NULL == peer_info)
2231 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2232 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2233 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2234 peer_info->id = GNUNET_PEER_intern (peer);
2242 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2243 * and insert it in the appropiate structures if the peer is not known yet.
2245 * @param peer Short identity of the peer.
2247 * @return Existing or newly created peer info.
2249 static struct MeshPeerInfo *
2250 peer_info_get_short (const GNUNET_PEER_Id peer)
2252 struct GNUNET_PeerIdentity id;
2254 GNUNET_PEER_resolve (peer, &id);
2255 return peer_info_get (&id);
2260 * Iterator to remove the tunnel from the list of tunnels a peer participates
2263 * @param cls Closure (tunnel info)
2264 * @param key GNUNET_PeerIdentity of the peer (unused)
2265 * @param value PeerInfo of the peer
2267 * @return always GNUNET_YES, to keep iterating
2270 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2272 struct MeshTunnel *t = cls;
2273 struct MeshPeerInfo *peer = value;
2276 for (i = 0; i < peer->ntunnels; i++)
2279 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2282 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2283 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2292 * Core callback to write a pre-constructed data packet to core buffer
2294 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2295 * @param size Number of bytes available in buf.
2296 * @param buf Where the to write the message.
2298 * @return number of bytes written to buf
2301 send_core_data_raw (void *cls, size_t size, void *buf)
2303 struct MeshTransmissionDescriptor *info = cls;
2304 struct GNUNET_MessageHeader *msg;
2307 GNUNET_assert (NULL != info);
2308 GNUNET_assert (NULL != info->mesh_data);
2309 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2310 total_size = ntohs (msg->size);
2312 if (total_size > size)
2317 memcpy (buf, msg, total_size);
2318 data_descriptor_decrement_rc (info->mesh_data);
2325 * Sends an already built non-multicast message to a peer,
2326 * properly registrating all used resources.
2328 * @param message Message to send. Function makes a copy of it.
2329 * @param peer Short ID of the neighbor whom to send the message.
2330 * @param t Tunnel on which this message is transmitted.
2333 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2334 const struct GNUNET_PeerIdentity *peer,
2335 struct MeshTunnel *t)
2337 struct MeshTransmissionDescriptor *info;
2338 struct MeshPeerInfo *neighbor;
2339 struct MeshPeerPath *p;
2343 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2345 size = ntohs (message->size);
2346 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2347 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2348 info->mesh_data->data = GNUNET_malloc (size);
2349 memcpy (info->mesh_data->data, message, size);
2350 type = ntohs(message->type);
2351 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2353 struct GNUNET_MESH_Unicast *m;
2355 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2356 m->ttl = htonl (ntohl (m->ttl) - 1);
2358 info->mesh_data->data_len = size;
2359 info->mesh_data->reference_counter = 1;
2360 info->mesh_data->total_out = 1;
2361 neighbor = peer_info_get (peer);
2362 for (p = neighbor->path_head; NULL != p; p = p->next)
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 " %s IS NOT DIRECTLY CONNECTED\n",
2375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 for (p = neighbor->path_head; NULL != p; p = p->next)
2380 struct GNUNET_PeerIdentity debug_id;
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2384 " path with %u hops through:\n",
2386 for (i = 0; i < p->length; i++)
2388 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2391 i, GNUNET_i2s(&debug_id));
2395 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2396 GNUNET_free (info->mesh_data->data);
2397 GNUNET_free (info->mesh_data);
2401 info->peer = neighbor;
2402 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2413 * Sends a CREATE PATH message for a path to a peer, properly registrating
2414 * all used resources.
2416 * @param peer PeerInfo of the final peer for whom this path is being created.
2417 * @param p Path itself.
2418 * @param t Tunnel for which the path is created.
2421 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2422 struct MeshTunnel *t)
2424 struct GNUNET_PeerIdentity id;
2425 struct MeshPathInfo *path_info;
2426 struct MeshPeerInfo *neighbor;
2432 p = tree_get_path_to_peer (t->tree, peer->id);
2439 for (i = 0; i < p->length; i++)
2441 if (p->peers[i] == myid)
2444 if (i >= p->length - 1)
2450 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2452 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2453 path_info->path = p;
2455 neighbor = peer_info_get (&id);
2456 path_info->peer = neighbor;
2457 queue_add (path_info,
2458 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2459 sizeof (struct GNUNET_MESH_ManipulatePath) +
2460 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2467 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2469 * @param t Tunnel whose path to destroy.
2470 * @param destination Short ID of the peer to whom the path to destroy.
2473 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2475 struct MeshPeerPath *p;
2478 p = tree_get_path_to_peer (t->tree, destination);
2484 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2485 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2487 struct GNUNET_MESH_ManipulatePath *msg;
2488 struct GNUNET_PeerIdentity *pi;
2492 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2493 msg->header.size = htons (size);
2494 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2495 msg->tid = htonl (t->id.tid);
2496 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2497 for (i = 0; i < p->length; i++)
2499 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2501 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2508 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2510 * @param t Tunnel which to confirm.
2513 send_path_ack (struct MeshTunnel *t)
2515 struct MeshTransmissionDescriptor *info;
2516 struct GNUNET_PeerIdentity id;
2517 GNUNET_PEER_Id peer;
2519 peer = tree_get_predecessor (t->tree);
2520 GNUNET_PEER_resolve (peer, &id);
2521 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2522 info->origin = &t->id;
2523 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2524 GNUNET_assert (NULL != info->peer);
2527 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2528 sizeof (struct GNUNET_MESH_PathACK),
2535 * Try to establish a new connection to this peer.
2536 * Use the best path for the given tunnel.
2537 * If the peer doesn't have any path to it yet, try to get one.
2538 * If the peer already has some path, send a CREATE PATH towards it.
2540 * @param peer PeerInfo of the peer.
2541 * @param t Tunnel for which to create the path, if possible.
2544 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2546 struct MeshPeerPath *p;
2547 struct MeshPathInfo *path_info;
2549 if (NULL != peer->path_head)
2551 p = tree_get_path_to_peer (t->tree, peer->id);
2558 // FIXME always send create path to self
2561 send_create_path (peer, p, t);
2565 struct GNUNET_HashCode hash;
2568 send_client_peer_connected (t, myid);
2569 t->local_tid_dest = next_local_tid++;
2570 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2573 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2574 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2581 else if (NULL == peer->dhtget)
2583 struct GNUNET_PeerIdentity id;
2585 GNUNET_PEER_resolve (peer->id, &id);
2586 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2587 path_info->peer = peer;
2589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2590 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2591 peer->dhtgetcls = path_info;
2592 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2593 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2594 &id.hashPubKey, /* key to search */
2595 dht_replication_level, /* replication level */
2596 GNUNET_DHT_RO_RECORD_ROUTE |
2597 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2598 NULL, /* xquery */ // FIXME BLOOMFILTER
2599 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2600 &dht_get_id_handler, path_info);
2602 /* Otherwise, there is no path but the DHT get is already started. */
2607 * Task to delay the connection of a peer
2609 * @param cls Closure (path info with tunnel and peer to connect).
2610 * Will be free'd on exection.
2611 * @param tc TaskContext
2614 peer_info_connect_task (void *cls,
2615 const struct GNUNET_SCHEDULER_TaskContext *tc)
2617 struct MeshPathInfo *path_info = cls;
2619 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2624 peer_info_connect (path_info->peer, path_info->t);
2630 * Destroy the peer_info and free any allocated resources linked to it
2632 * @param pi The peer_info to destroy.
2634 * @return GNUNET_OK on success
2637 peer_info_destroy (struct MeshPeerInfo *pi)
2639 struct GNUNET_PeerIdentity id;
2640 struct MeshPeerPath *p;
2641 struct MeshPeerPath *nextp;
2643 GNUNET_PEER_resolve (pi->id, &id);
2644 GNUNET_PEER_change_rc (pi->id, -1);
2647 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2650 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2651 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2653 if (NULL != pi->dhtget)
2655 GNUNET_DHT_get_stop (pi->dhtget);
2656 GNUNET_free (pi->dhtgetcls);
2662 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2672 * Remove all paths that rely on a direct connection between p1 and p2
2673 * from the peer itself and notify all tunnels about it.
2675 * @param peer PeerInfo of affected peer.
2676 * @param p1 GNUNET_PEER_Id of one peer.
2677 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2680 * TODO: optimize (see below)
2683 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2686 struct MeshPeerPath *p;
2687 struct MeshPeerPath *aux;
2688 struct MeshPeerInfo *peer_d;
2690 unsigned int destroyed;
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2697 p = peer->path_head;
2701 for (i = 0; i < (p->length - 1); i++)
2703 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2704 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2706 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2717 for (i = 0; i < peer->ntunnels; i++)
2719 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2723 * Problem: one or more peers have been deleted from the tunnel tree.
2724 * We don't know who they are to try to add them again.
2725 * We need to try to find a new path for each of the disconnected peers.
2726 * Some of them might already have a path to reach them that does not
2727 * involve p1 and p2. Adding all anew might render in a better tree than
2728 * the trivial immediate fix.
2730 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2731 * its children will be reachable trough him.
2733 peer_d = peer_info_get_short (d);
2736 for (p = peer_d->path_head; NULL != p; p = p->next)
2738 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2746 /* No callback, as peer will be already disconnected and a connection
2747 * scheduled by tunnel_notify_connection_broken.
2749 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2753 peer_info_connect (peer_d, peer->tunnels[i]);
2756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2761 * Add the path to the peer and update the path used to reach it in case this
2764 * @param peer_info Destination peer to add the path to.
2765 * @param path New path to add. Last peer must be the peer in arg 1.
2766 * Path will be either used of freed if already known.
2767 * @param trusted Do we trust that this path is real?
2770 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2773 struct MeshPeerPath *aux;
2777 if ((NULL == peer_info) || (NULL == path))
2780 path_destroy (path);
2783 if (path->peers[path->length - 1] != peer_info->id)
2786 path_destroy (path);
2789 if (path->length <= 2 && GNUNET_NO == trusted)
2791 /* Only allow CORE to tell us about direct paths */
2792 path_destroy (path);
2795 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2796 for (l = 1; l < path->length; l++)
2798 if (path->peers[l] == myid)
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2801 for (l2 = 0; l2 < path->length - l; l2++)
2803 path->peers[l2] = path->peers[l + l2];
2808 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2813 struct GNUNET_PeerIdentity id;
2815 GNUNET_PEER_resolve (peer_info->id, &id);
2816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2817 path->length, GNUNET_i2s (&id));
2820 l = path_get_length (path);
2827 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2828 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2830 l2 = path_get_length (aux);
2833 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2834 peer_info->path_tail, aux, path);
2839 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2841 path_destroy (path);
2846 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2853 * Add the path to the origin peer and update the path used to reach it in case
2854 * this is the shortest.
2855 * The path is given in peer_info -> destination, therefore we turn the path
2856 * upside down first.
2858 * @param peer_info Peer to add the path to, being the origin of the path.
2859 * @param path New path to add after being inversed.
2860 * @param trusted Do we trust that this path is real?
2863 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2864 struct MeshPeerPath *path, int trusted)
2867 peer_info_add_path (peer_info, path, trusted);
2872 * Function called if the connection to the peer has been stalled for a while,
2873 * possibly due to a missed ACK. Poll the peer about its ACK status.
2875 * @param cls Closure (info about regex search).
2876 * @param tc TaskContext.
2879 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2881 struct MeshTunnelChildInfo *cinfo = cls;
2882 struct GNUNET_MESH_Poll msg;
2883 struct GNUNET_PeerIdentity id;
2884 struct MeshTunnel *t;
2886 return; // FIXME fc activate
2887 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2888 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2894 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2895 msg.header.size = htons (sizeof (msg));
2896 msg.tid = htonl (t->id.tid);
2897 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2898 msg.last_ack = htonl (cinfo->fwd_ack);
2900 GNUNET_PEER_resolve (tree_get_predecessor(cinfo->t->tree), &id);
2901 send_prebuilt_message (&msg.header, &id, cinfo->t);
2902 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2903 &tunnel_poll, cinfo);
2908 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2909 * to obtain a local peer -> destination path and interning the peer ids.
2911 * @return Newly allocated and created path
2913 static struct MeshPeerPath *
2914 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2915 unsigned int get_path_length,
2916 const struct GNUNET_PeerIdentity *put_path,
2917 unsigned int put_path_length)
2919 struct MeshPeerPath *p;
2925 GNUNET_PEER_change_rc (myid, 1);
2926 i = get_path_length;
2927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2928 for (i--; i >= 0; i--)
2930 id = GNUNET_PEER_intern (&get_path[i]);
2931 if (p->length > 0 && id == p->peers[p->length - 1])
2933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2934 GNUNET_PEER_change_rc (id, -1);
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2939 GNUNET_i2s (&get_path[i]));
2941 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2942 p->peers[p->length - 1] = id;
2945 i = put_path_length;
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2947 for (i--; i >= 0; i--)
2949 id = GNUNET_PEER_intern (&put_path[i]);
2952 /* PUT path went through us, so discard the path up until now and start
2953 * from here to get a much shorter (and loop-free) path.
2958 if (p->length > 0 && id == p->peers[p->length - 1])
2960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2961 GNUNET_PEER_change_rc (id, -1);
2965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2966 GNUNET_i2s (&put_path[i]));
2968 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2969 p->peers[p->length - 1] = id;
2973 if (get_path_length > 0)
2974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2975 GNUNET_i2s (&get_path[0]));
2976 if (put_path_length > 0)
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2978 GNUNET_i2s (&put_path[0]));
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2981 for (i = 0; i < p->length; i++)
2983 struct GNUNET_PeerIdentity peer_id;
2985 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2987 GNUNET_i2s (&peer_id));
2995 * Adds a path to the peer_infos of all the peers in the path
2997 * @param p Path to process.
2998 * @param confirmed Whether we know if the path works or not.
3001 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3005 /* TODO: invert and add */
3006 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3007 for (i++; i < p->length; i++)
3009 struct MeshPeerInfo *aux;
3010 struct MeshPeerPath *copy;
3012 aux = peer_info_get_short (p->peers[i]);
3013 copy = path_duplicate (p);
3014 copy->length = i + 1;
3015 peer_info_add_path (aux, copy, GNUNET_NO);
3021 * Send keepalive packets for a peer
3023 * @param cls Closure (tunnel for which to send the keepalive).
3024 * @param tc Notification context.
3026 * TODO: implement explicit multicast keepalive?
3029 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3033 * Search for a tunnel among the incoming tunnels
3035 * @param tid the local id of the tunnel
3037 * @return tunnel handler, NULL if doesn't exist
3039 static struct MeshTunnel *
3040 tunnel_get_incoming (MESH_TunnelNumber tid)
3042 struct GNUNET_HashCode hash;
3044 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3045 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3046 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3051 * Search for a tunnel among the tunnels for a client
3053 * @param c the client whose tunnels to search in
3054 * @param tid the local id of the tunnel
3056 * @return tunnel handler, NULL if doesn't exist
3058 static struct MeshTunnel *
3059 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3061 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3063 return tunnel_get_incoming (tid);
3067 struct GNUNET_HashCode hash;
3069 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3070 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3076 * Search for a tunnel by global ID using PEER_ID
3078 * @param pi owner of the tunnel
3079 * @param tid global tunnel number
3081 * @return tunnel handler, NULL if doesn't exist
3083 static struct MeshTunnel *
3084 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3086 struct MESH_TunnelID id;
3087 struct GNUNET_HashCode hash;
3092 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3093 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3098 * Search for a tunnel by global ID using full PeerIdentities
3100 * @param oid owner of the tunnel
3101 * @param tid global tunnel number
3103 * @return tunnel handler, NULL if doesn't exist
3105 static struct MeshTunnel *
3106 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3108 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3113 * Delete an active client from the tunnel.
3119 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3123 for (i = 0; i < t->nclients; i++)
3125 if (t->clients[i] == c)
3127 t->clients[i] = t->clients[t->nclients - 1];
3128 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3129 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3131 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3139 * Delete an ignored client from the tunnel.
3145 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3149 for (i = 0; i < t->nignore; i++)
3151 if (t->ignore[i] == c)
3153 t->ignore[i] = t->ignore[t->nignore - 1];
3154 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3162 * Delete a client from the tunnel. It should be only done on
3163 * client disconnection, otherwise use client_ignore_tunnel.
3169 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3171 tunnel_delete_ignored_client (t, c);
3172 tunnel_delete_active_client (t, c);
3177 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3179 * Destroys queue elements of all waiting transmissions and frees all memory
3180 * used by the struct and its elements.
3182 * @param cls Closure (tunnel info).
3183 * @param key Hash of GNUNET_PEER_Id (unused).
3184 * @param value MeshTunnelChildInfo of the child.
3186 * @return always GNUNET_YES, to keep iterating
3189 tunnel_destroy_child (void *cls,
3190 const struct GNUNET_HashCode * key,
3193 struct MeshTunnelChildInfo *cinfo = value;
3194 struct MeshTunnel *t = cls;
3195 struct MeshPeerQueue *q;
3199 for (c = 0; c < cinfo->send_buffer_n; c++)
3201 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3202 q = cinfo->send_buffer[i];
3203 cinfo->send_buffer[i] = NULL;
3205 queue_destroy (q, GNUNET_YES);
3208 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3210 GNUNET_free_non_null (cinfo->send_buffer);
3211 GNUNET_free (cinfo);
3217 * Callback used to notify a client owner of a tunnel that a peer has
3218 * disconnected, most likely because of a path change.
3220 * @param cls Closure (tunnel this notification is about).
3221 * @param peer_id Short ID of disconnected peer.
3224 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3226 struct MeshTunnel *t = cls;
3227 struct MeshPeerInfo *peer;
3228 struct MeshPathInfo *path_info;
3230 if (NULL != t->owner && NULL != nc)
3232 struct GNUNET_MESH_PeerControl msg;
3234 msg.header.size = htons (sizeof (msg));
3235 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3236 msg.tunnel_id = htonl (t->local_tid);
3237 GNUNET_PEER_resolve (peer_id, &msg.peer);
3238 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3239 &msg.header, GNUNET_NO);
3241 peer = peer_info_get_short (peer_id);
3242 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3243 path_info->peer = peer;
3245 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3250 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3251 * needed rescources.
3252 * If peer already exists, reevaluate shortest path and change if different.
3254 * @param t Tunnel we want to add a new peer to
3255 * @param peer PeerInfo of the peer being added
3259 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3261 struct GNUNET_PeerIdentity id;
3262 struct MeshPeerPath *best_p;
3263 struct MeshPeerPath *p;
3264 unsigned int best_cost;
3267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3268 GNUNET_PEER_resolve (peer->id, &id);
3270 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3273 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3274 GNUNET_assert (GNUNET_OK ==
3275 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3277 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3280 if (NULL != (p = peer->path_head))
3283 best_cost = tree_get_path_cost (t->tree, p);
3286 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3293 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3294 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3295 t->path_refresh_task =
3296 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3300 /* Start a DHT get */
3301 peer_info_connect (peer, t);
3303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3307 * Add a path to a tunnel which we don't own, just to remember the next hop.
3308 * If destination node was already in the tunnel, the first hop information
3309 * will be replaced with the new path.
3311 * @param t Tunnel we want to add a new peer to
3312 * @param p Path to add
3313 * @param own_pos Position of local node in path.
3317 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3318 unsigned int own_pos)
3320 struct GNUNET_PeerIdentity id;
3322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3323 GNUNET_assert (0 != own_pos);
3324 tree_add_path (t->tree, p, NULL, NULL);
3325 if (own_pos < p->length - 1)
3327 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3328 tree_update_first_hops (t->tree, myid, &id);
3330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3334 * Add a client to a tunnel, initializing all needed data structures.
3336 * @param t Tunnel to which add the client.
3337 * @param c Client which to add to the tunnel.
3340 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3342 struct MeshTunnelClientInfo clinfo;
3344 GNUNET_array_append (t->clients, t->nclients, c);
3345 clinfo.fwd_ack = t->fwd_pid + 1;
3346 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3347 clinfo.fwd_pid = t->fwd_pid;
3348 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3350 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3355 * Notifies a tunnel that a connection has broken that affects at least
3356 * some of its peers. Sends a notification towards the root of the tree.
3357 * In case the peer is the owner of the tree, notifies the client that owns
3358 * the tunnel and tries to reconnect.
3360 * @param t Tunnel affected.
3361 * @param p1 Peer that got disconnected from p2.
3362 * @param p2 Peer that got disconnected from p1.
3364 * @return Short ID of the peer disconnected (either p1 or p2).
3365 * 0 if the tunnel remained unaffected.
3367 static GNUNET_PEER_Id
3368 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3374 tree_notify_connection_broken (t->tree, p1, p2,
3375 &tunnel_notify_client_peer_disconnected,
3377 if (myid != p1 && myid != p2)
3383 if (tree_get_predecessor (t->tree) != 0)
3385 /* We are the peer still connected, notify owner of the disconnection. */
3386 struct GNUNET_MESH_PathBroken msg;
3387 struct GNUNET_PeerIdentity neighbor;
3389 msg.header.size = htons (sizeof (msg));
3390 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3391 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3392 msg.tid = htonl (t->id.tid);
3393 msg.peer1 = my_full_id;
3394 GNUNET_PEER_resolve (pid, &msg.peer2);
3395 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3396 send_prebuilt_message (&msg.header, &neighbor, t);
3404 * Send a multicast packet to a neighbor.
3406 * @param cls Closure (Info about the multicast packet)
3407 * @param neighbor_id Short ID of the neighbor to send the packet to.
3410 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3412 struct MeshData *mdata = cls;
3413 struct MeshTransmissionDescriptor *info;
3414 struct GNUNET_PeerIdentity neighbor;
3415 struct GNUNET_MessageHeader *msg;
3417 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3419 info->mesh_data = mdata;
3420 (mdata->reference_counter) ++;
3421 info->destination = neighbor_id;
3422 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3424 GNUNET_i2s (&neighbor));
3425 info->peer = peer_info_get (&neighbor);
3426 GNUNET_assert (NULL != info->peer);
3427 msg = (struct GNUNET_MessageHeader *) mdata->data;
3430 info->mesh_data->data_len,
3437 * Queue a message in a tunnel in multicast, sending a copy to each child node
3438 * down the local one in the tunnel tree.
3440 * @param t Tunnel in which to send the data.
3441 * @param msg Message to be sent.
3444 tunnel_send_multicast (struct MeshTunnel *t,
3445 const struct GNUNET_MessageHeader *msg)
3447 struct MeshData *mdata;
3449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3450 " sending a multicast packet...\n");
3452 mdata = GNUNET_malloc (sizeof (struct MeshData));
3453 mdata->data_len = ntohs (msg->size);
3455 mdata->data = GNUNET_malloc (mdata->data_len);
3456 memcpy (mdata->data, msg, mdata->data_len);
3457 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3459 struct GNUNET_MESH_Multicast *mcast;
3461 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3462 if (t->fwd_queue_n >= t->fwd_queue_max)
3465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3466 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3467 " message from %s!\n",
3468 GNUNET_i2s(&mcast->oid));
3469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3470 " message at %s!\n",
3471 GNUNET_i2s(&my_full_id));
3472 GNUNET_free (mdata->data);
3473 GNUNET_free (mdata);
3477 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3479 ntohl (mcast->ttl));
3483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3486 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3487 if (mdata->reference_counter == 0)
3489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3490 " no one to send data to\n");
3491 GNUNET_free (mdata->data);
3492 GNUNET_free (mdata);
3497 mdata->total_out = mdata->reference_counter;
3499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3500 " sending a multicast packet done\n");
3506 * Increase the SKIP value of all peers that
3507 * have not received a unicast message.
3509 * @param cls Closure (ID of the peer that HAS received the message).
3510 * @param key ID of the neighbor.
3511 * @param value Information about the neighbor.
3513 * @return GNUNET_YES to keep iterating.
3516 tunnel_add_skip (void *cls,
3517 const struct GNUNET_HashCode * key,
3520 struct GNUNET_PeerIdentity *neighbor = cls;
3521 struct MeshTunnelChildInfo *cinfo = value;
3523 /* TODO compare only pointers? key == neighbor? */
3524 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3534 * @brief Get neighbor's Flow Control information.
3536 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3537 * descendant of the local node in a certain tunnel.
3538 * If the info is not yet there (recently created path), creates the data struct
3539 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3542 * @param t Tunnel related.
3543 * @param peer Neighbor whose Flow Control info is needed.
3545 * @return Neighbor's Flow Control info.
3547 static struct MeshTunnelChildInfo *
3548 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3549 const struct GNUNET_PeerIdentity *peer)
3551 struct MeshTunnelChildInfo *cinfo;
3553 if (NULL == t->children_fc)
3556 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3562 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3563 cinfo->id = GNUNET_PEER_intern (peer);
3564 cinfo->skip = t->fwd_pid;
3567 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3568 cinfo->fwd_ack = t->fwd_pid + delta;
3569 cinfo->bck_ack = delta;
3571 cinfo->send_buffer =
3572 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3574 GNUNET_assert (GNUNET_OK ==
3575 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3578 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3585 * Get the Flow Control info of a client.
3587 * @param t Tunnel on which to look.
3588 * @param c Client whose ACK to get.
3590 * @return ACK value.
3592 static struct MeshTunnelClientInfo *
3593 tunnel_get_client_fc (struct MeshTunnel *t,
3594 struct MeshClient *c)
3598 for (i = 0; i < t->nclients; i++)
3600 if (t->clients[i] != c)
3602 return &t->clients_fc[i];
3605 return NULL; // avoid compiler / coverity complaints
3610 * Iterator to get the appropiate ACK value from all children nodes.
3612 * @param cls Closue (tunnel).
3613 * @param id Id of the child node.
3616 tunnel_get_child_fwd_ack (void *cls,
3619 struct GNUNET_PeerIdentity peer_id;
3620 struct MeshTunnelChildInfo *cinfo;
3621 struct MeshTunnelChildIteratorContext *ctx = cls;
3622 struct MeshTunnel *t = ctx->t;
3625 GNUNET_PEER_resolve (id, &peer_id);
3626 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3627 ack = cinfo->fwd_ack;
3630 if (GNUNET_NO == ctx->init)
3632 ctx->max_child_ack = ack;
3633 ctx->init = GNUNET_YES;
3636 if (GNUNET_YES == t->speed_min)
3638 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3642 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3649 * Get the maximum PID allowed to transmit to any
3650 * tunnel child of the local peer, depending on the tunnel
3651 * buffering/speed settings.
3655 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3658 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3660 struct MeshTunnelChildIteratorContext ctx;
3662 ctx.max_child_ack = 0;
3664 ctx.init = GNUNET_NO;
3665 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3667 if (0 == ctx.nchildren)
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3670 " tunnel has no children, no FWD ACK\n");
3674 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3675 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3677 return (int64_t) ctx.max_child_ack;
3682 * Set the FWD ACK value of a client in a particular tunnel.
3684 * @param t Tunnel affected.
3685 * @param c Client whose ACK to set.
3686 * @param ack ACK value.
3689 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3690 struct MeshClient *c,
3695 for (i = 0; i < t->nclients; i++)
3697 if (t->clients[i] != c)
3699 t->clients_fc[i].fwd_ack = ack;
3707 * Get the highest ACK value of all clients in a particular tunnel,
3708 * according to the buffering/speed settings.
3710 * @param t Tunnel on which to look.
3712 * @return Corresponding ACK value (max uint32_t).
3713 * If no clients are suscribed, -1LL.
3716 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3721 if (0 == t->nclients)
3723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3724 " tunnel has no clients, no FWD ACK\n");
3728 for (ack = -1LL, i = 0; i < t->nclients; i++)
3731 (GNUNET_YES == t->speed_min &&
3732 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3733 (GNUNET_NO == t->speed_min &&
3734 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3736 ack = t->clients_fc[i].fwd_ack;
3740 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3741 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3743 return (uint32_t) ack;
3748 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3749 * mode and the status of all children nodes.
3753 * @return Maximum PID allowed.
3756 tunnel_get_fwd_ack (struct MeshTunnel *t)
3760 uint32_t buffer_free;
3764 count = t->fwd_pid - t->skip;
3765 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3766 child_ack = tunnel_get_children_fwd_ack (t);
3767 client_ack = tunnel_get_clients_fwd_ack (t);
3768 if (GNUNET_YES == t->nobuffer)
3771 if (-1LL == child_ack)
3772 child_ack = client_ack;
3773 if (-1LL == child_ack)
3776 client_ack = child_ack = ack;
3781 ack = count + buffer_free; // Overflow? OK!
3783 if (-1LL == child_ack)
3785 // Node has no children, child_ack AND core buffer are irrelevant.
3786 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3787 return (uint32_t) client_ack;
3789 if (-1LL == client_ack)
3793 if (GNUNET_YES == t->speed_min)
3795 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3796 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3800 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3801 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3804 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3805 count, buffer_free, child_ack, client_ack, ack);
3811 * Build a local ACK message and send it to a local client.
3813 * @param t Tunnel on which to send the ACK.
3814 * @param c Client to whom send the ACK.
3815 * @param ack Value of the ACK.
3818 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3820 struct GNUNET_MESH_LocalAck msg;
3822 msg.header.size = htons (sizeof (msg));
3823 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3824 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3825 msg.max_pid = htonl (ack);
3826 GNUNET_SERVER_notification_context_unicast(nc,
3833 * Build an ACK message and queue it to send to the given peer.
3835 * @param t Tunnel on which to send the ACK.
3836 * @param peer Peer to whom send the ACK.
3837 * @param ack Value of the ACK.
3840 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3842 struct GNUNET_MESH_ACK msg;
3844 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3845 msg.header.size = htons (sizeof (msg));
3846 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3847 msg.pid = htonl (ack);
3848 msg.tid = htonl (t->id.tid);
3850 send_prebuilt_message (&msg.header, peer, t);
3855 * Notify a the owner of a tunnel about how many more
3856 * payload packages will we accept on a given tunnel.
3858 * @param t Tunnel on which to send the ACK.
3861 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3866 "Sending client FWD ACK on tunnel %X\n",
3869 ack = tunnel_get_fwd_ack (t);
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3872 if (t->last_fwd_ack == ack)
3874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3879 t->last_fwd_ack = ack;
3880 send_local_ack (t, t->owner, ack);
3885 * Send an ACK informing the predecessor about the available buffer space.
3886 * In case there is no predecessor, inform the owning client.
3887 * If buffering is off, send only on behalf of children or self if endpoint.
3888 * If buffering is on, send when sent to children and buffer space is free.
3889 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3890 * the ACK itself goes "back" (towards root).
3892 * @param t Tunnel on which to send the ACK.
3893 * @param type Type of message that triggered the ACK transmission.
3896 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3898 struct GNUNET_PeerIdentity id;
3901 if (NULL != t->owner)
3903 tunnel_send_client_fwd_ack (t);
3906 /* Is it after unicast / multicast retransmission? */
3909 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3910 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3912 "ACK due to FWD DATA retransmission\n");
3913 if (GNUNET_YES == t->nobuffer)
3915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3919 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3920 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3926 /* Check if we need no retransmit the ACK */
3927 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3928 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3932 " t->qmax: %u, t->qn: %u\n",
3933 t->fwd_queue_max, t->fwd_queue_n);
3934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3935 " t->pid: %u, t->ack: %u\n",
3936 t->fwd_pid, t->last_fwd_ack);
3940 /* Ok, ACK might be necessary, what PID to ACK? */
3941 ack = tunnel_get_fwd_ack (t);
3943 /* If speed_min and not all children have ack'd, dont send yet */
3944 if (ack == t->last_fwd_ack)
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3950 t->last_fwd_ack = ack;
3951 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3952 send_ack (t, &id, ack);
3958 * Iterator to send a child node a BCK ACK to allow him to send more
3961 * @param cls Closure (tunnel).
3962 * @param id Id of the child node.
3965 tunnel_send_child_bck_ack (void *cls,
3968 struct MeshTunnel *t = cls;
3969 struct MeshTunnelChildInfo *cinfo;
3970 struct GNUNET_PeerIdentity peer;
3972 GNUNET_PEER_resolve (id, &peer);
3973 cinfo = tunnel_get_neighbor_fc (t, &peer);
3975 if (cinfo->bck_ack != cinfo->pid &&
3976 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3979 cinfo->bck_ack++; // FIXME window size?
3980 send_ack (t, &peer, cinfo->bck_ack);
3985 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3987 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3989 * @param t Tunnel on which to send the BCK ACKs.
3992 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3995 unsigned int tunnel_delta;
3997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3999 tunnel_delta = t->bck_ack - t->bck_pid;
4000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4002 /* Find client whom to allow to send to origin (with lowest buffer space) */
4003 for (i = 0; i < t->nclients; i++)
4005 struct MeshTunnelClientInfo *clinfo;
4008 clinfo = &t->clients_fc[i];
4009 delta = clinfo->bck_ack - clinfo->bck_pid;
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4011 t->clients[i]->id, delta);
4013 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4014 (GNUNET_YES == t->nobuffer && 0 == delta))
4018 ack = clinfo->bck_pid;
4019 ack += t->nobuffer ? 1 : tunnel_delta;
4020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4021 " sending ack to client %u: %u\n",
4022 t->clients[i]->id, ack);
4023 send_local_ack (t, t->clients[i], ack);
4024 clinfo->bck_ack = ack;
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4029 " not sending ack to client %u (td %u, d %u)\n",
4030 t->clients[i]->id, tunnel_delta, delta);
4037 * Send an ACK informing the children nodes and destination clients about
4038 * the available buffer space.
4039 * If buffering is off, send only on behalf of root (can be self).
4040 * If buffering is on, send when sent to predecessor and buffer space is free.
4041 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4042 * the ACK itself goes "forward" (towards children/clients).
4044 * @param t Tunnel on which to send the ACK.
4045 * @param type Type of message that triggered the ACK transmission.
4048 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4051 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4052 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4053 /* Is it after data to_origin retransmission? */
4056 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4057 if (GNUNET_YES == t->nobuffer)
4059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4060 " Not sending ACK, nobuffer\n");
4064 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4065 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4066 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4072 tunnel_send_clients_bck_ack (t);
4073 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4078 * @brief Re-initiate traffic to this peer if necessary.
4080 * Check if there is traffic queued towards this peer
4081 * and the core transmit handle is NULL (traffic was stalled).
4082 * If so, call core tmt rdy.
4084 * @param cls Closure (unused)
4085 * @param peer_id Short ID of peer to which initiate traffic.
4088 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4090 struct MeshPeerInfo *peer;
4091 struct GNUNET_PeerIdentity id;
4092 struct MeshPeerQueue *q;
4095 peer = peer_info_get_short(peer_id);
4096 if (NULL != peer->core_transmit)
4099 q = queue_get_next(peer);
4102 /* Might br multicast traffic already sent to this particular peer but
4103 * not to other children in this tunnel.
4104 * This way t->queue_n would be > 0 but the queue of this particular peer
4110 GNUNET_PEER_resolve (peer->id, &id);
4111 peer->core_transmit =
4112 GNUNET_CORE_notify_transmit_ready(core_handle,
4115 GNUNET_TIME_UNIT_FOREVER_REL,
4125 * @brief Allow transmission of FWD traffic on this tunnel
4127 * Check if there is traffic queued towards any children
4128 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4130 * @param t Tunnel on which to unlock FWD traffic.
4133 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4135 if (0 == t->fwd_queue_n)
4138 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4143 * @brief Allow transmission of BCK traffic on this tunnel
4145 * Check if there is traffic queued towards the root of the tree
4146 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4148 * @param t Tunnel on which to unlock BCK traffic.
4151 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4153 if (0 == t->bck_queue_n)
4156 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4161 * Send a message to all peers in this tunnel that the tunnel is no longer
4164 * @param t The tunnel whose peers to notify.
4167 tunnel_send_destroy (struct MeshTunnel *t)
4169 struct GNUNET_MESH_TunnelDestroy msg;
4171 msg.header.size = htons (sizeof (msg));
4172 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4173 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4174 msg.tid = htonl (t->id.tid);
4175 tunnel_send_multicast (t, &msg.header);
4180 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4182 * @param cls Closure (Tunnel which to cancel).
4183 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4186 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4188 struct MeshTunnel *t = cls;
4189 struct MeshPeerInfo *peer_info;
4190 struct MeshPeerQueue *pq;
4191 struct MeshPeerQueue *next;
4193 peer_info = peer_info_get_short (neighbor_id);
4194 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4197 if (pq->tunnel == t)
4199 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4200 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4201 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4203 // Should have been removed on destroy children
4206 queue_destroy (pq, GNUNET_YES);
4209 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4211 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4212 peer_info->core_transmit = NULL;
4217 * Destroy the tunnel and free any allocated resources linked to it.
4219 * @param t the tunnel to destroy
4221 * @return GNUNET_OK on success
4224 tunnel_destroy (struct MeshTunnel *t)
4226 struct MeshClient *c;
4227 struct GNUNET_HashCode hash;
4238 struct GNUNET_PeerIdentity id;
4240 GNUNET_PEER_resolve (t->id.oid, &id);
4241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4242 GNUNET_i2s (&id), t->id.tid);
4244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4248 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4249 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4257 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4259 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4266 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4267 for (i = 0; i < t->nclients; i++)
4271 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4277 for (i = 0; i < t->nignore; i++)
4281 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4288 if (t->nclients > 0)
4291 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4296 GNUNET_free (t->clients);
4297 GNUNET_free (t->clients_fc);
4300 if (NULL != t->peers)
4302 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4304 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4307 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4308 &tunnel_destroy_child,
4310 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4311 t->children_fc = NULL;
4313 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4314 tree_destroy (t->tree);
4316 if (NULL != t->regex_ctx)
4317 regex_cancel_search (t->regex_ctx);
4318 if (NULL != t->dht_get_type)
4319 GNUNET_DHT_get_stop (t->dht_get_type);
4320 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4321 GNUNET_SCHEDULER_cancel (t->timeout_task);
4322 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4323 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4326 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4333 * Create a new tunnel
4335 * @param owner Who is the owner of the tunnel (short ID).
4336 * @param tid Tunnel Number of the tunnel.
4337 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4338 * @param local Tunnel Number for the tunnel, for the client point of view.
4340 * @return A new initialized tunnel. NULL on error.
4342 static struct MeshTunnel *
4343 tunnel_new (GNUNET_PEER_Id owner,
4344 MESH_TunnelNumber tid,
4345 struct MeshClient *client,
4346 MESH_TunnelNumber local)
4348 struct MeshTunnel *t;
4349 struct GNUNET_HashCode hash;
4351 if (n_tunnels >= max_tunnels && NULL == client)
4354 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4357 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4358 t->bck_queue_max = t->fwd_queue_max;
4359 t->tree = tree_new (owner);
4361 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4362 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4363 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4364 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4365 t->local_tid = local;
4366 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4368 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4370 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4372 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4373 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4380 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4387 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4389 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4390 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4394 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4404 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4405 * that are no longer needed, as well as nodes of no longer reachable peers.
4406 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4408 * @param t Tunnel from which to remove the path.
4409 * @param peer Short id of the peer which should be removed.
4412 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4414 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4420 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4421 * client when the client disconnects. If the client is not the owner, the
4422 * owner will get notified if no more clients are in the tunnel and the client
4423 * get removed from the tunnel's list.
4425 * @param cls closure (client that is disconnecting)
4426 * @param key the hash of the local tunnel id (used to access the hashmap)
4427 * @param value the value stored at the key (tunnel to destroy)
4429 * @return GNUNET_OK, keep iterating.
4432 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4434 struct MeshTunnel *t = value;
4435 struct MeshClient *c = cls;
4437 send_client_tunnel_disconnect(t, c);
4440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4441 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4442 tunnel_delete_client(t, c);
4443 client_delete_tunnel(c, t);
4446 tunnel_send_destroy(t);
4448 t->destroy = GNUNET_YES;
4455 * Timeout function, destroys tunnel if called
4457 * @param cls Closure (tunnel to destroy).
4458 * @param tc TaskContext
4461 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4463 struct MeshTunnel *t = cls;
4464 struct GNUNET_PeerIdentity id;
4466 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4467 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4469 GNUNET_PEER_resolve(t->id.oid, &id);
4470 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4471 "Tunnel %s [%X] timed out. Destroying.\n",
4472 GNUNET_i2s(&id), t->id.tid);
4473 send_clients_tunnel_destroy (t);
4478 * Resets the tunnel timeout. Starts it if no timeout was running.
4480 * @param t Tunnel whose timeout to reset.
4482 * TODO use heap to improve efficiency of scheduler.
4485 tunnel_reset_timeout (struct MeshTunnel *t)
4487 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4488 GNUNET_SCHEDULER_cancel (t->timeout_task);
4490 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4491 (refresh_path_time, 4), &tunnel_timeout, t);
4495 /******************************************************************************/
4496 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4497 /******************************************************************************/
4500 * Function to send a create path packet to a peer.
4502 * @param cls closure
4503 * @param size number of bytes available in buf
4504 * @param buf where the callee should write the message
4505 * @return number of bytes written to buf
4508 send_core_path_create (void *cls, size_t size, void *buf)
4510 struct MeshPathInfo *info = cls;
4511 struct GNUNET_MESH_ManipulatePath *msg;
4512 struct GNUNET_PeerIdentity *peer_ptr;
4513 struct MeshTunnel *t = info->t;
4514 struct MeshPeerPath *p = info->path;
4519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4521 sizeof (struct GNUNET_MESH_ManipulatePath) +
4522 p->length * sizeof (struct GNUNET_PeerIdentity);
4524 if (size < size_needed || NULL == buf)
4529 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4530 msg->header.size = htons (size_needed);
4531 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4532 msg->tid = ntohl (t->id.tid);
4535 if (GNUNET_YES == t->speed_min)
4536 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4537 if (GNUNET_YES == t->nobuffer)
4538 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4539 msg->opt = htonl(opt);
4542 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4543 for (i = 0; i < p->length; i++)
4545 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4552 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4558 * Fill the core buffer
4560 * @param cls closure (data itself)
4561 * @param size number of bytes available in buf
4562 * @param buf where the callee should write the message
4564 * @return number of bytes written to buf
4567 send_core_data_multicast (void *cls, size_t size, void *buf)
4569 struct MeshTransmissionDescriptor *info = cls;
4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4573 GNUNET_assert (NULL != info);
4574 GNUNET_assert (NULL != info->peer);
4575 total_size = info->mesh_data->data_len;
4576 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4578 if (total_size > size)
4583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4584 memcpy (buf, info->mesh_data->data, total_size);
4587 struct GNUNET_MESH_Multicast *mc;
4588 struct GNUNET_MessageHeader *mh;
4591 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4593 mc = (struct GNUNET_MESH_Multicast *) mh;
4594 mh = (struct GNUNET_MessageHeader *) &mc[1];
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4596 " multicast, payload type %s\n",
4597 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4599 " multicast, payload size %u\n", ntohs (mh->size));
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4604 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4608 data_descriptor_decrement_rc (info->mesh_data);
4609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4617 * Creates a path ack message in buf and frees all unused resources.
4619 * @param cls closure (MeshTransmissionDescriptor)
4620 * @param size number of bytes available in buf
4621 * @param buf where the callee should write the message
4622 * @return number of bytes written to buf
4625 send_core_path_ack (void *cls, size_t size, void *buf)
4627 struct MeshTransmissionDescriptor *info = cls;
4628 struct GNUNET_MESH_PathACK *msg = buf;
4630 GNUNET_assert (NULL != info);
4631 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4636 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4637 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4638 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4639 msg->tid = htonl (info->origin->tid);
4640 msg->peer_id = my_full_id;
4643 /* TODO add signature */
4645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4646 return sizeof (struct GNUNET_MESH_PathACK);
4651 * Free a transmission that was already queued with all resources
4652 * associated to the request.
4654 * @param queue Queue handler to cancel.
4655 * @param clear_cls Is it necessary to free associated cls?
4658 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4660 struct MeshTransmissionDescriptor *dd;
4661 struct MeshPathInfo *path_info;
4662 struct MeshTunnelChildInfo *cinfo;
4663 struct GNUNET_PeerIdentity id;
4667 if (GNUNET_YES == clear_cls)
4669 switch (queue->type)
4671 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4674 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4675 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4676 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4677 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4679 " prebuilt message\n");
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4682 GNUNET_MESH_DEBUG_M2S(queue->type));
4684 data_descriptor_decrement_rc (dd->mesh_data);
4686 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4688 path_info = queue->cls;
4689 path_destroy (path_info->path);
4693 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4694 " type %s unknown!\n",
4695 GNUNET_MESH_DEBUG_M2S(queue->type));
4697 GNUNET_free_non_null (queue->cls);
4699 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4700 queue->peer->queue_tail,
4703 /* Delete from child_fc in the appropiate tunnel */
4704 max = queue->tunnel->fwd_queue_max;
4705 GNUNET_PEER_resolve (queue->peer->id, &id);
4706 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4709 for (i = 0; i < cinfo->send_buffer_n; i++)
4712 i2 = (cinfo->send_buffer_start + i) % max;
4713 if (cinfo->send_buffer[i2] == queue)
4715 /* Found corresponding entry in the send_buffer. Move all others back. */
4720 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4722 j2 = (cinfo->send_buffer_start + j) % max;
4723 j3 = (cinfo->send_buffer_start + j + 1) % max;
4724 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4727 cinfo->send_buffer[j3] = NULL;
4728 cinfo->send_buffer_n--;
4733 GNUNET_free (queue);
4738 * @brief Get the next transmittable message from the queue.
4740 * This will be the head, except in the case of being a data packet
4741 * not allowed by the destination peer.
4743 * @param peer Destination peer.
4745 * @return The next viable MeshPeerQueue element to send to that peer.
4746 * NULL when there are no transmittable messages.
4748 struct MeshPeerQueue *
4749 queue_get_next (const struct MeshPeerInfo *peer)
4751 struct MeshPeerQueue *q;
4752 struct MeshTunnel *t;
4753 struct MeshTransmissionDescriptor *info;
4754 struct MeshTunnelChildInfo *cinfo;
4755 struct GNUNET_MESH_Unicast *ucast;
4756 struct GNUNET_MESH_ToOrigin *to_orig;
4757 struct GNUNET_MESH_Multicast *mcast;
4758 struct GNUNET_PeerIdentity id;
4762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4763 for (q = peer->queue_head; NULL != q; q = q->next)
4767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4769 GNUNET_MESH_DEBUG_M2S(q->type));
4772 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4773 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4774 pid = ntohl (ucast->pid);
4775 GNUNET_PEER_resolve (info->peer->id, &id);
4776 cinfo = tunnel_get_neighbor_fc(t, &id);
4777 ack = cinfo->fwd_ack;
4779 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4780 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4781 pid = ntohl (to_orig->pid);
4784 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4785 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4786 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4788 // Not a multicast payload: multicast control traffic (destroy, etc)
4791 pid = ntohl (mcast->pid);
4792 GNUNET_PEER_resolve (info->peer->id, &id);
4793 cinfo = tunnel_get_neighbor_fc(t, &id);
4794 ack = cinfo->fwd_ack;
4797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4802 "********* ACK: %u, PID: %u\n",
4804 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4813 "********* NEXT!\n");
4816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4817 "********* nothing found\n");
4823 * Core callback to write a queued packet to core buffer
4825 * @param cls Closure (peer info).
4826 * @param size Number of bytes available in buf.
4827 * @param buf Where the to write the message.
4829 * @return number of bytes written to buf
4832 queue_send (void *cls, size_t size, void *buf)
4834 struct MeshPeerInfo *peer = cls;
4835 struct GNUNET_MessageHeader *msg;
4836 struct MeshPeerQueue *queue;
4837 struct MeshTunnel *t;
4838 struct MeshTunnelChildInfo *cinfo;
4839 struct GNUNET_PeerIdentity dst_id;
4842 peer->core_transmit = NULL;
4845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4846 queue = queue_get_next (peer);
4848 /* Queue has no internal mesh traffic nor sendable payload */
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4852 if (NULL == peer->queue_head)
4853 GNUNET_break (0); // Should've been canceled
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4858 GNUNET_PEER_resolve (peer->id, &dst_id);
4859 /* Check if buffer size is enough for the message */
4860 if (queue->size > size)
4862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4863 "********* not enough room, reissue\n");
4864 peer->core_transmit =
4865 GNUNET_CORE_notify_transmit_ready (core_handle,
4868 GNUNET_TIME_UNIT_FOREVER_REL,
4875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4878 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4882 "********* unicast: t->q (%u/%u)\n",
4883 t->fwd_queue_n, t->fwd_queue_max);
4885 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4892 switch (queue->type)
4895 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4896 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4897 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4898 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4900 "********* raw: %s\n",
4901 GNUNET_MESH_DEBUG_M2S (queue->type));
4903 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4904 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4905 data_size = send_core_data_raw (queue->cls, size, buf);
4906 msg = (struct GNUNET_MessageHeader *) buf;
4907 switch (ntohs (msg->type)) // Type of preconstructed message
4909 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4910 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4912 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4913 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4919 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4922 struct MeshTransmissionDescriptor *info = queue->cls;
4924 if ((1 == info->mesh_data->reference_counter
4925 && GNUNET_YES == t->speed_min)
4927 (info->mesh_data->total_out == info->mesh_data->reference_counter
4928 && GNUNET_NO == t->speed_min))
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4931 "********* considered sent\n");
4935 data_size = send_core_data_multicast(queue->cls, size, buf);
4936 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4938 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4940 data_size = send_core_path_create (queue->cls, size, buf);
4942 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4944 data_size = send_core_path_ack (queue->cls, size, buf);
4946 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4948 data_size = send_core_data_multicast (queue->cls, size, buf);
4952 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4953 "********* type unknown: %u\n",
4957 switch (queue->type)
4959 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4960 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4961 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4962 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4963 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4967 "at pos %u (%p) != %p\n",
4968 cinfo->send_buffer_start,
4969 cinfo->send_buffer[cinfo->send_buffer_start],
4972 if (cinfo->send_buffer_n > 0)
4974 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4975 cinfo->send_buffer_n--;
4976 cinfo->send_buffer_start++;
4977 cinfo->send_buffer_start %= t->fwd_queue_max;
4988 /* Free queue, but cls was freed by send_core_* */
4989 queue_destroy (queue, GNUNET_NO);
4991 if (GNUNET_YES == t->destroy)
4993 // FIXME fc tunnel destroy all pending traffic? wait for it?
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4998 /* If more data in queue, send next */
4999 queue = queue_get_next(peer);
5002 struct GNUNET_PeerIdentity id;
5004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5005 GNUNET_PEER_resolve (peer->id, &id);
5006 peer->core_transmit =
5007 GNUNET_CORE_notify_transmit_ready(core_handle,
5010 GNUNET_TIME_UNIT_FOREVER_REL,
5018 if (NULL != peer->queue_head)
5020 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5021 "********* %s stalled\n",
5022 GNUNET_i2s(&my_full_id));
5024 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5025 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5026 &tunnel_poll, cinfo);
5029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5035 * @brief Queue and pass message to core when possible.
5037 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5038 * and accounts for it. In case the queue is full, the message is dropped and
5041 * Otherwise, message is treated as internal and allowed to go regardless of
5044 * @param cls Closure (@c type dependant). It will be used by queue_send to
5045 * build the message to be sent if not already prebuilt.
5046 * @param type Type of the message, 0 for a raw message.
5047 * @param size Size of the message.
5048 * @param dst Neighbor to send message to.
5049 * @param t Tunnel this message belongs to.
5052 queue_add (void *cls, uint16_t type, size_t size,
5053 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5055 struct MeshPeerQueue *queue;
5056 struct MeshTunnelChildInfo *cinfo;
5057 struct GNUNET_PeerIdentity id;
5063 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5064 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5066 n = &t->fwd_queue_n;
5067 max = &t->fwd_queue_max;
5069 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5071 n = &t->bck_queue_n;
5072 max = &t->bck_queue_max;
5077 if (NULL == t->owner)
5078 GNUNET_break_op(0); // TODO: kill connection?
5081 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5083 return; // Drop message
5087 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5093 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5094 GNUNET_PEER_resolve (dst->id, &id);
5095 if (NULL == dst->core_transmit)
5097 dst->core_transmit =
5098 GNUNET_CORE_notify_transmit_ready (core_handle,
5101 GNUNET_TIME_UNIT_FOREVER_REL,
5107 if (NULL == n) // Is this internal mesh traffic?
5110 // It's payload, keep track of buffer per peer.
5111 cinfo = tunnel_get_neighbor_fc(t, &id);
5112 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5113 if (NULL != cinfo->send_buffer[i])
5115 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5116 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5117 cinfo->send_buffer_start++;
5118 cinfo->send_buffer_start %= t->fwd_queue_max;
5122 cinfo->send_buffer_n++;
5124 cinfo->send_buffer[i] = queue;
5125 if (cinfo->send_buffer_n > t->fwd_queue_max)
5128 cinfo->send_buffer_n = t->fwd_queue_max;
5133 /******************************************************************************/
5134 /******************** MESH NETWORK HANDLERS **************************/
5135 /******************************************************************************/
5139 * Core handler for path creation
5141 * @param cls closure
5142 * @param message message
5143 * @param peer peer identity this notification is about
5144 * @param atsi performance data
5145 * @param atsi_count number of records in 'atsi'
5147 * @return GNUNET_OK to keep the connection open,
5148 * GNUNET_SYSERR to close it (signal serious error)
5151 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5152 const struct GNUNET_MessageHeader *message,
5153 const struct GNUNET_ATS_Information *atsi,
5154 unsigned int atsi_count)
5156 unsigned int own_pos;
5159 MESH_TunnelNumber tid;
5160 struct GNUNET_MESH_ManipulatePath *msg;
5161 struct GNUNET_PeerIdentity *pi;
5162 struct GNUNET_HashCode hash;
5163 struct MeshPeerPath *path;
5164 struct MeshPeerInfo *dest_peer_info;
5165 struct MeshPeerInfo *orig_peer_info;
5166 struct MeshTunnel *t;
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5169 "Received a path create msg [%s]\n",
5170 GNUNET_i2s (&my_full_id));
5171 size = ntohs (message->size);
5172 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5174 GNUNET_break_op (0);
5178 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5179 if (size % sizeof (struct GNUNET_PeerIdentity))
5181 GNUNET_break_op (0);
5184 size /= sizeof (struct GNUNET_PeerIdentity);
5187 GNUNET_break_op (0);
5190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5191 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5193 tid = ntohl (msg->tid);
5194 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5196 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5197 t = tunnel_get (pi, tid);
5198 if (NULL == t) // FIXME only for INCOMING tunnels?
5202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5203 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5206 // FIXME notify failure
5209 opt = ntohl (msg->opt);
5210 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5211 GNUNET_YES : GNUNET_NO;
5212 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5214 t->nobuffer = GNUNET_YES;
5215 t->last_fwd_ack = t->fwd_pid + 1;
5217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5218 " speed_min: %d, nobuffer:%d\n",
5219 t->speed_min, t->nobuffer);
5221 if (GNUNET_YES == t->nobuffer)
5223 t->bck_queue_max = 1;
5224 t->fwd_queue_max = 1;
5227 // FIXME only assign a local tid if a local client is interested (on demand)
5228 while (NULL != tunnel_get_incoming (next_local_tid))
5229 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5230 t->local_tid_dest = next_local_tid++;
5231 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5234 tunnel_reset_timeout (t);
5235 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5237 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5238 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5246 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5247 if (NULL == dest_peer_info)
5249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5250 " Creating PeerInfo for destination.\n");
5251 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5252 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5253 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5257 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5258 if (NULL == orig_peer_info)
5260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5261 " Creating PeerInfo for origin.\n");
5262 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5263 orig_peer_info->id = GNUNET_PEER_intern (pi);
5264 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5268 path = path_new (size);
5270 for (i = 0; i < size; i++)
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5273 GNUNET_i2s (&pi[i]));
5274 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5275 if (path->peers[i] == myid)
5278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5281 /* cannot be self, must be 'not found' */
5282 /* create path: self not found in path through self */
5283 GNUNET_break_op (0);
5284 path_destroy (path);
5288 path_add_to_peers (path, GNUNET_NO);
5289 tunnel_add_path (t, path, own_pos);
5290 if (own_pos == size - 1)
5292 /* It is for us! Send ack. */
5293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5294 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5295 if (NULL == t->peers)
5297 /* New tunnel! Notify clients on first payload message. */
5298 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5300 GNUNET_break (GNUNET_SYSERR !=
5301 GNUNET_CONTAINER_multihashmap_put (t->peers,
5302 &my_full_id.hashPubKey,
5305 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5310 struct MeshPeerPath *path2;
5312 /* It's for somebody else! Retransmit. */
5313 path2 = path_duplicate (path);
5314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5315 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5316 path2 = path_duplicate (path);
5317 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5318 send_create_path (dest_peer_info, path, t);
5325 * Core handler for path destruction
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'
5333 * @return GNUNET_OK to keep the connection open,
5334 * GNUNET_SYSERR to close it (signal serious error)
5337 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5338 const struct GNUNET_MessageHeader *message,
5339 const struct GNUNET_ATS_Information *atsi,
5340 unsigned int atsi_count)
5342 struct GNUNET_MESH_ManipulatePath *msg;
5343 struct GNUNET_PeerIdentity *pi;
5344 struct MeshPeerPath *path;
5345 struct MeshTunnel *t;
5346 unsigned int own_pos;
5350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5351 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5352 size = ntohs (message->size);
5353 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5355 GNUNET_break_op (0);
5359 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5360 if (size % sizeof (struct GNUNET_PeerIdentity))
5362 GNUNET_break_op (0);
5365 size /= sizeof (struct GNUNET_PeerIdentity);
5368 GNUNET_break_op (0);
5371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5373 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5374 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5376 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5378 t = tunnel_get (pi, ntohl (msg->tid));
5381 /* TODO notify back: we don't know this tunnel */
5382 GNUNET_break_op (0);
5385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5386 path = path_new (size);
5388 for (i = 0; i < size; i++)
5390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5391 GNUNET_i2s (&pi[i]));
5392 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5393 if (path->peers[i] == myid)
5396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5397 if (own_pos < path->length - 1)
5398 send_prebuilt_message (message, &pi[own_pos + 1], t);
5400 send_client_tunnel_disconnect(t, NULL);
5402 tunnel_delete_peer (t, path->peers[path->length - 1]);
5403 path_destroy (path);
5409 * Core handler for notifications of broken paths
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_path_broken (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_PathBroken *msg;
5427 struct MeshTunnel *t;
5429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5430 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5431 msg = (struct GNUNET_MESH_PathBroken *) message;
5432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5433 GNUNET_i2s (&msg->peer1));
5434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5435 GNUNET_i2s (&msg->peer2));
5436 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5439 GNUNET_break_op (0);
5442 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5443 GNUNET_PEER_search (&msg->peer2));
5450 * Core handler for tunnel destruction
5452 * @param cls closure
5453 * @param message message
5454 * @param peer peer identity this notification is about
5455 * @param atsi performance data
5456 * @param atsi_count number of records in 'atsi'
5458 * @return GNUNET_OK to keep the connection open,
5459 * GNUNET_SYSERR to close it (signal serious error)
5462 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5463 const struct GNUNET_MessageHeader *message,
5464 const struct GNUNET_ATS_Information *atsi,
5465 unsigned int atsi_count)
5467 struct GNUNET_MESH_TunnelDestroy *msg;
5468 struct MeshTunnel *t;
5470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5471 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5472 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5474 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5475 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5478 /* Probably already got the message from another path,
5479 * destroyed the tunnel and retransmitted to children.
5482 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5485 if (t->id.oid == myid)
5487 GNUNET_break_op (0);
5490 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5492 /* Tunnel was incoming, notify clients */
5493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5494 t->local_tid, t->local_tid_dest);
5495 send_clients_tunnel_destroy (t);
5497 tunnel_send_destroy (t);
5498 t->destroy = GNUNET_YES;
5499 // TODO: add timeout to destroy the tunnel anyway
5505 * Core handler for mesh network traffic going from the origin to a peer
5507 * @param cls closure
5508 * @param peer peer identity this notification is about
5509 * @param message message
5510 * @param atsi performance data
5511 * @param atsi_count number of records in 'atsi'
5512 * @return GNUNET_OK to keep the connection open,
5513 * GNUNET_SYSERR to close it (signal serious error)
5516 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5517 const struct GNUNET_MessageHeader *message,
5518 const struct GNUNET_ATS_Information *atsi,
5519 unsigned int atsi_count)
5521 struct GNUNET_MESH_Unicast *msg;
5522 struct GNUNET_PeerIdentity *neighbor;
5523 struct MeshTunnelChildInfo *cinfo;
5524 struct MeshTunnel *t;
5525 GNUNET_PEER_Id dest_id;
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5533 size = ntohs (message->size);
5535 sizeof (struct GNUNET_MESH_Unicast) +
5536 sizeof (struct GNUNET_MessageHeader))
5541 msg = (struct GNUNET_MESH_Unicast *) message;
5542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5543 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5545 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5548 /* TODO notify back: we don't know this tunnel */
5549 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5550 GNUNET_break_op (0);
5553 pid = ntohl (msg->pid);
5554 if (t->fwd_pid == pid)
5556 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5557 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5558 " Already seen pid %u, DROPPING!\n", pid);
5563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5564 " pid %u not seen yet, forwarding\n", pid);
5567 t->skip += (pid - t->fwd_pid) - 1;
5570 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5572 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5573 GNUNET_break_op (0);
5574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5575 "Received PID %u, ACK %u\n",
5576 pid, t->last_fwd_ack);
5580 tunnel_reset_timeout (t);
5581 dest_id = GNUNET_PEER_search (&msg->destination);
5582 if (dest_id == myid)
5584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5585 " it's for us! sending to clients...\n");
5586 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5587 send_subscribed_clients (message, &msg[1].header, t);
5588 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5591 ttl = ntohl (msg->ttl);
5592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5595 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5596 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5597 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5601 " not for us, retransmitting...\n");
5603 neighbor = tree_get_first_hop (t->tree, dest_id);
5604 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5606 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5609 if (GNUNET_YES == t->nobuffer &&
5610 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5612 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5613 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5614 GNUNET_break_op (0);
5617 send_prebuilt_message (message, neighbor, t);
5618 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5624 * Core handler for mesh network traffic going from the origin to all peers
5626 * @param cls closure
5627 * @param message message
5628 * @param peer peer identity this notification is about
5629 * @param atsi performance data
5630 * @param atsi_count number of records in 'atsi'
5631 * @return GNUNET_OK to keep the connection open,
5632 * GNUNET_SYSERR to close it (signal serious error)
5634 * TODO: Check who we got this from, to validate route.
5637 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5638 const struct GNUNET_MessageHeader *message,
5639 const struct GNUNET_ATS_Information *atsi,
5640 unsigned int atsi_count)
5642 struct GNUNET_MESH_Multicast *msg;
5643 struct MeshTunnel *t;
5647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5649 size = ntohs (message->size);
5650 if (sizeof (struct GNUNET_MESH_Multicast) +
5651 sizeof (struct GNUNET_MessageHeader) > size)
5653 GNUNET_break_op (0);
5656 msg = (struct GNUNET_MESH_Multicast *) message;
5657 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5661 /* TODO notify that we dont know that tunnel */
5662 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5663 GNUNET_break_op (0);
5666 pid = ntohl (msg->pid);
5667 if (t->fwd_pid == pid)
5669 /* already seen this packet, drop */
5670 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5672 " Already seen pid %u, DROPPING!\n", pid);
5673 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5679 " pid %u not seen yet, forwarding\n", pid);
5681 t->skip += (pid - t->fwd_pid) - 1;
5683 tunnel_reset_timeout (t);
5685 /* Transmit to locally interested clients */
5686 if (NULL != t->peers &&
5687 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5689 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5690 send_subscribed_clients (message, &msg[1].header, t);
5691 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5694 if (ntohl (msg->ttl) == 0)
5696 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5697 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5698 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5701 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5702 tunnel_send_multicast (t, message);
5708 * Core handler for mesh network traffic toward the owner of a tunnel
5710 * @param cls closure
5711 * @param message message
5712 * @param peer peer identity this notification is about
5713 * @param atsi performance data
5714 * @param atsi_count number of records in 'atsi'
5716 * @return GNUNET_OK to keep the connection open,
5717 * GNUNET_SYSERR to close it (signal serious error)
5720 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5721 const struct GNUNET_MessageHeader *message,
5722 const struct GNUNET_ATS_Information *atsi,
5723 unsigned int atsi_count)
5725 struct GNUNET_MESH_ToOrigin *msg;
5726 struct GNUNET_PeerIdentity id;
5727 struct MeshPeerInfo *peer_info;
5728 struct MeshTunnel *t;
5732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5734 size = ntohs (message->size);
5735 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5736 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5738 GNUNET_break_op (0);
5741 msg = (struct GNUNET_MESH_ToOrigin *) message;
5742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5743 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5744 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5745 pid = ntohl (msg->pid);
5749 /* TODO notify that we dont know this tunnel (whom)? */
5750 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5751 GNUNET_break_op (0);
5752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5753 "Received PID %u, ACK %u\n",
5758 if (NULL != t->owner)
5761 struct GNUNET_MESH_ToOrigin *copy;
5763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5764 " it's for us! sending to clients...\n");
5765 /* TODO signature verification */
5766 memcpy (cbuf, message, size);
5767 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5768 copy->tid = htonl (t->local_tid);
5769 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5770 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5771 ©->header, GNUNET_NO);
5772 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5776 " not for us, retransmitting...\n");
5778 peer_info = peer_info_get (&msg->oid);
5779 if (NULL == peer_info)
5781 /* unknown origin of tunnel */
5785 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5786 send_prebuilt_message (message, &id, t);
5787 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5794 * Core handler for mesh network traffic point-to-point acks.
5796 * @param cls closure
5797 * @param message message
5798 * @param peer peer identity this notification is about
5799 * @param atsi performance data
5800 * @param atsi_count number of records in 'atsi'
5802 * @return GNUNET_OK to keep the connection open,
5803 * GNUNET_SYSERR to close it (signal serious error)
5806 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5807 const struct GNUNET_MessageHeader *message,
5808 const struct GNUNET_ATS_Information *atsi,
5809 unsigned int atsi_count)
5811 struct GNUNET_MESH_ACK *msg;
5812 struct MeshTunnel *t;
5815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5817 msg = (struct GNUNET_MESH_ACK *) message;
5819 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5823 /* TODO notify that we dont know this tunnel (whom)? */
5824 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5825 GNUNET_break_op (0);
5828 ack = ntohl (msg->pid);
5830 /* Is this a forward or backward ACK? */
5831 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5833 struct MeshTunnelChildInfo *cinfo;
5836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5837 cinfo = tunnel_get_neighbor_fc (t, peer);
5838 cinfo->fwd_ack = ack;
5839 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5840 tunnel_unlock_fwd_queues (t);
5844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5846 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5847 tunnel_unlock_bck_queue (t);
5854 * Core handler for mesh network traffic point-to-point ack polls.
5856 * @param cls closure
5857 * @param message message
5858 * @param peer peer identity this notification is about
5859 * @param atsi performance data
5860 * @param atsi_count number of records in 'atsi'
5862 * @return GNUNET_OK to keep the connection open,
5863 * GNUNET_SYSERR to close it (signal serious error)
5866 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5867 const struct GNUNET_MessageHeader *message,
5868 const struct GNUNET_ATS_Information *atsi,
5869 unsigned int atsi_count)
5871 struct GNUNET_MESH_Poll *msg;
5872 struct MeshTunnel *t;
5874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5877 msg = (struct GNUNET_MESH_Poll *) message;
5879 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5883 /* TODO notify that we dont know this tunnel (whom)? */
5884 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5885 GNUNET_break_op (0);
5889 /* Is this a forward or backward ACK? */
5890 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5892 struct MeshTunnelChildInfo *cinfo;
5894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5895 cinfo = tunnel_get_neighbor_fc (t, peer);
5896 cinfo->bck_ack = cinfo->pid; // mark as ready to send
5897 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5902 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5910 * Core handler for path ACKs
5912 * @param cls closure
5913 * @param message message
5914 * @param peer peer identity this notification is about
5915 * @param atsi performance data
5916 * @param atsi_count number of records in 'atsi'
5918 * @return GNUNET_OK to keep the connection open,
5919 * GNUNET_SYSERR to close it (signal serious error)
5922 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5923 const struct GNUNET_MessageHeader *message,
5924 const struct GNUNET_ATS_Information *atsi,
5925 unsigned int atsi_count)
5927 struct GNUNET_MESH_PathACK *msg;
5928 struct GNUNET_PeerIdentity id;
5929 struct MeshPeerInfo *peer_info;
5930 struct MeshPeerPath *p;
5931 struct MeshTunnel *t;
5933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5934 GNUNET_i2s (&my_full_id));
5935 msg = (struct GNUNET_MESH_PathACK *) message;
5936 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5939 /* TODO notify that we don't know the tunnel */
5940 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5942 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5946 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5948 peer_info = peer_info_get (&msg->peer_id);
5949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5950 GNUNET_i2s (&msg->peer_id));
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5954 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5957 "connect_by_string completed, stopping search\n");
5958 regex_cancel_search (t->regex_ctx);
5959 t->regex_ctx = NULL;
5962 /* Add paths to peers? */
5963 p = tree_get_path_to_peer (t->tree, peer_info->id);
5966 path_add_to_peers (p, GNUNET_YES);
5974 /* Message for us? */
5975 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5978 if (NULL == t->owner)
5980 GNUNET_break_op (0);
5983 if (NULL != t->dht_get_type)
5985 GNUNET_DHT_get_stop (t->dht_get_type);
5986 t->dht_get_type = NULL;
5988 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5990 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5991 send_client_peer_connected (t, peer_info->id);
5996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5997 " not for us, retransmitting...\n");
5998 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5999 peer_info = peer_info_get (&msg->oid);
6000 if (NULL == peer_info)
6002 /* If we know the tunnel, we should DEFINITELY know the peer */
6006 send_prebuilt_message (message, &id, t);
6012 * Core handler for mesh keepalives.
6014 * @param cls closure
6015 * @param message message
6016 * @param peer peer identity this notification is about
6017 * @param atsi performance data
6018 * @param atsi_count number of records in 'atsi'
6019 * @return GNUNET_OK to keep the connection open,
6020 * GNUNET_SYSERR to close it (signal serious error)
6022 * TODO: Check who we got this from, to validate route.
6025 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6026 const struct GNUNET_MessageHeader *message,
6027 const struct GNUNET_ATS_Information *atsi,
6028 unsigned int atsi_count)
6030 struct GNUNET_MESH_TunnelKeepAlive *msg;
6031 struct MeshTunnel *t;
6033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6036 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6037 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6041 /* TODO notify that we dont know that tunnel */
6042 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6043 GNUNET_break_op (0);
6047 tunnel_reset_timeout (t);
6049 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6050 tunnel_send_multicast (t, message);
6057 * Functions to handle messages from core
6059 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6060 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6061 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6062 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6063 sizeof (struct GNUNET_MESH_PathBroken)},
6064 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6065 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6066 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6067 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6068 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6069 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6070 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6071 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6072 sizeof (struct GNUNET_MESH_ACK)},
6073 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6074 sizeof (struct GNUNET_MESH_Poll)},
6075 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6076 sizeof (struct GNUNET_MESH_PathACK)},
6082 /******************************************************************************/
6083 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6084 /******************************************************************************/
6087 * deregister_app: iterator for removing each application registered by a client
6089 * @param cls closure
6090 * @param key the hash of the application id (used to access the hashmap)
6091 * @param value the value stored at the key (client)
6093 * @return GNUNET_OK on success
6096 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6098 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6099 GNUNET_break (GNUNET_YES ==
6100 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6106 * notify_client_connection_failure: notify a client that the connection to the
6107 * requested remote peer is not possible (for instance, no route found)
6108 * Function called when the socket is ready to queue more data. "buf" will be
6109 * NULL and "size" zero if the socket was closed for writing in the meantime.
6111 * @param cls closure
6112 * @param size number of bytes available in buf
6113 * @param buf where the callee should write the message
6114 * @return number of bytes written to buf
6117 notify_client_connection_failure (void *cls, size_t size, void *buf)
6120 struct MeshPeerInfo *peer_info;
6121 struct GNUNET_MESH_PeerControl *msg;
6122 struct GNUNET_PeerIdentity id;
6124 if (0 == size && NULL == buf)
6126 // TODO retry? cancel?
6130 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6131 peer_info = (struct MeshPeerInfo *) cls;
6132 msg = (struct GNUNET_MESH_PeerControl *) buf;
6133 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6134 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6135 // msg->tunnel_id = htonl(peer_info->t->tid);
6136 GNUNET_PEER_resolve (peer_info->id, &id);
6137 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6145 * Send keepalive packets for a peer
6147 * @param cls Closure (tunnel for which to send the keepalive).
6148 * @param tc Notification context.
6151 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6153 struct MeshTunnel *t = cls;
6154 struct GNUNET_MESH_TunnelKeepAlive *msg;
6155 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6158 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6159 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6165 "sending keepalive for tunnel %d\n", t->id.tid);
6167 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6168 msg->header.size = htons (size);
6169 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6170 msg->oid = my_full_id;
6171 msg->tid = htonl (t->id.tid);
6172 tunnel_send_multicast (t, &msg->header);
6174 t->path_refresh_task =
6175 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6176 tunnel_reset_timeout(t);
6181 * Function to process paths received for a new peer addition. The recorded
6182 * paths form the initial tunnel, which can be optimized later.
6183 * Called on each result obtained for the DHT search.
6185 * @param cls closure
6186 * @param exp when will this value expire
6187 * @param key key of the result
6188 * @param get_path path of the get request
6189 * @param get_path_length lenght of get_path
6190 * @param put_path path of the put request
6191 * @param put_path_length length of the put_path
6192 * @param type type of the result
6193 * @param size number of bytes in data
6194 * @param data pointer to the result data
6196 * TODO: re-issue the request after certain time? cancel after X results?
6199 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6200 const struct GNUNET_HashCode * key,
6201 const struct GNUNET_PeerIdentity *get_path,
6202 unsigned int get_path_length,
6203 const struct GNUNET_PeerIdentity *put_path,
6204 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6205 size_t size, const void *data)
6207 struct MeshPathInfo *path_info = cls;
6208 struct MeshPeerPath *p;
6209 struct GNUNET_PeerIdentity pi;
6212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6213 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6216 p = path_build_from_dht (get_path, get_path_length, put_path,
6218 path_add_to_peers (p, GNUNET_NO);
6220 for (i = 0; i < path_info->peer->ntunnels; i++)
6222 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6223 peer_info_connect (path_info->peer, path_info->t);
6231 * Function to process paths received for a new peer addition. The recorded
6232 * paths form the initial tunnel, which can be optimized later.
6233 * Called on each result obtained for the DHT search.
6235 * @param cls closure
6236 * @param exp when will this value expire
6237 * @param key key of the result
6238 * @param get_path path of the get request
6239 * @param get_path_length lenght of get_path
6240 * @param put_path path of the put request
6241 * @param put_path_length length of the put_path
6242 * @param type type of the result
6243 * @param size number of bytes in data
6244 * @param data pointer to the result data
6247 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6248 const struct GNUNET_HashCode * key,
6249 const struct GNUNET_PeerIdentity *get_path,
6250 unsigned int get_path_length,
6251 const struct GNUNET_PeerIdentity *put_path,
6252 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6253 size_t size, const void *data)
6255 const struct PBlock *pb = data;
6256 const struct GNUNET_PeerIdentity *pi = &pb->id;
6257 struct MeshTunnel *t = cls;
6258 struct MeshPeerInfo *peer_info;
6259 struct MeshPeerPath *p;
6261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6262 if (size != sizeof (struct PBlock))
6264 GNUNET_break_op (0);
6267 if (ntohl(pb->type) != t->type)
6269 GNUNET_break_op (0);
6272 GNUNET_assert (NULL != t->owner);
6273 peer_info = peer_info_get (pi);
6274 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6276 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6278 p = path_build_from_dht (get_path, get_path_length, put_path,
6280 path_add_to_peers (p, GNUNET_NO);
6282 tunnel_add_peer (t, peer_info);
6283 peer_info_connect (peer_info, t);
6288 * Function to process DHT string to regex matching.
6289 * Called on each result obtained for the DHT search.
6291 * @param cls closure (search context)
6292 * @param exp when will this value expire
6293 * @param key key of the result
6294 * @param get_path path of the get request (not used)
6295 * @param get_path_length lenght of get_path (not used)
6296 * @param put_path path of the put request (not used)
6297 * @param put_path_length length of the put_path (not used)
6298 * @param type type of the result
6299 * @param size number of bytes in data
6300 * @param data pointer to the result data
6303 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6304 const struct GNUNET_HashCode * key,
6305 const struct GNUNET_PeerIdentity *get_path,
6306 unsigned int get_path_length,
6307 const struct GNUNET_PeerIdentity *put_path,
6308 unsigned int put_path_length,
6309 enum GNUNET_BLOCK_Type type,
6310 size_t size, const void *data)
6312 const struct MeshRegexAccept *block = data;
6313 struct MeshRegexSearchContext *ctx = cls;
6314 struct MeshRegexSearchInfo *info = ctx->info;
6315 struct MeshPeerPath *p;
6316 struct MeshPeerInfo *peer_info;
6318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6321 peer_info = peer_info_get(&block->id);
6322 p = path_build_from_dht (get_path, get_path_length, put_path,
6324 path_add_to_peers (p, GNUNET_NO);
6327 tunnel_add_peer (info->t, peer_info);
6328 peer_info_connect (peer_info, info->t);
6329 if (0 == info->peer)
6331 info->peer = peer_info->id;
6335 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6338 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6339 ®ex_connect_timeout,
6347 * Function to process DHT string to regex matching.
6348 * Called on each result obtained for the DHT search.
6350 * @param cls closure (search context)
6351 * @param exp when will this value expire
6352 * @param key key of the result
6353 * @param get_path path of the get request (not used)
6354 * @param get_path_length lenght of get_path (not used)
6355 * @param put_path path of the put request (not used)
6356 * @param put_path_length length of the put_path (not used)
6357 * @param type type of the result
6358 * @param size number of bytes in data
6359 * @param data pointer to the result data
6361 * TODO: re-issue the request after certain time? cancel after X results?
6364 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6365 const struct GNUNET_HashCode * key,
6366 const struct GNUNET_PeerIdentity *get_path,
6367 unsigned int get_path_length,
6368 const struct GNUNET_PeerIdentity *put_path,
6369 unsigned int put_path_length,
6370 enum GNUNET_BLOCK_Type type,
6371 size_t size, const void *data)
6373 const struct MeshRegexBlock *block = data;
6374 struct MeshRegexSearchContext *ctx = cls;
6375 struct MeshRegexSearchInfo *info = ctx->info;
6379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6380 "DHT GET STRING RETURNED RESULTS\n");
6381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6382 " key: %s\n", GNUNET_h2s (key));
6384 copy = GNUNET_malloc (size);
6385 memcpy (copy, data, size);
6386 GNUNET_break (GNUNET_OK ==
6387 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6388 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6389 len = ntohl (block->n_proof);
6391 char proof[len + 1];
6393 memcpy (proof, &block[1], len);
6395 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6397 GNUNET_break_op (0);
6401 len = strlen (info->description);
6402 if (len == ctx->position) // String processed
6404 if (GNUNET_YES == ntohl (block->accepting))
6406 regex_find_path(key, ctx);
6410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6411 // FIXME REGEX this block not successful, wait for more? start timeout?
6416 regex_next_edge (block, size, ctx);
6421 /******************************************************************************/
6422 /********************* MESH LOCAL HANDLES **************************/
6423 /******************************************************************************/
6427 * Handler for client disconnection
6429 * @param cls closure
6430 * @param client identification of the client; NULL
6431 * for the last call when the server is destroyed
6434 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6436 struct MeshClient *c;
6437 struct MeshClient *next;
6440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6449 if (c->handle != client)
6451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6457 GNUNET_SERVER_client_drop (c->handle);
6458 c->shutting_down = GNUNET_YES;
6459 GNUNET_assert (NULL != c->own_tunnels);
6460 GNUNET_assert (NULL != c->incoming_tunnels);
6461 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6462 &tunnel_destroy_iterator, c);
6463 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6464 &tunnel_destroy_iterator, c);
6465 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6466 &tunnel_destroy_iterator, c);
6467 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6468 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6469 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6471 /* deregister clients applications */
6472 if (NULL != c->apps)
6474 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6475 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6477 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6478 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6480 GNUNET_SCHEDULER_cancel (announce_applications_task);
6481 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6483 if (NULL != c->types)
6484 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6485 for (i = 0; i < c->n_regex; i++)
6487 GNUNET_free (c->regexes[i]);
6489 GNUNET_free_non_null (c->regexes);
6490 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6491 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6493 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6496 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6505 * Handler for new clients
6507 * @param cls closure
6508 * @param client identification of the client
6509 * @param message the actual message, which includes messages the client wants
6512 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6513 const struct GNUNET_MessageHeader *message)
6515 struct GNUNET_MESH_ClientConnect *cc_msg;
6516 struct MeshClient *c;
6517 GNUNET_MESH_ApplicationType *a;
6524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6525 /* Check data sanity */
6526 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6527 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6528 ntypes = ntohs (cc_msg->types);
6529 napps = ntohs (cc_msg->applications);
6531 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6538 /* Create new client structure */
6539 c = GNUNET_malloc (sizeof (struct MeshClient));
6540 c->id = next_client_id++;
6541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6543 GNUNET_SERVER_client_keep (client);
6544 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6547 GNUNET_MESH_ApplicationType at;
6548 struct GNUNET_HashCode hc;
6550 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6551 for (i = 0; i < napps; i++)
6554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6555 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6556 /* store in clients hashmap */
6557 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6558 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6559 /* store in global hashmap, for announcements */
6560 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6561 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6563 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6564 announce_applications_task =
6565 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6571 struct GNUNET_HashCode hc;
6573 t = (uint16_t *) & a[napps];
6574 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6575 for (i = 0; i < ntypes; i++)
6578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6579 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6581 /* store in clients hashmap */
6582 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6583 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6584 /* store in global hashmap */
6585 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6586 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6590 " client has %u+%u subscriptions\n", napps, ntypes);
6592 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6593 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6594 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6595 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6596 GNUNET_SERVER_notification_context_add (nc, client);
6597 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6599 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6605 * Handler for clients announcing available services by a regular expression.
6607 * @param cls closure
6608 * @param client identification of the client
6609 * @param message the actual message, which includes messages the client wants
6612 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6613 const struct GNUNET_MessageHeader *message)
6615 struct MeshClient *c;
6619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6621 /* Sanity check for client registration */
6622 if (NULL == (c = client_get (client)))
6625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6630 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6631 regex = GNUNET_malloc (len + 1);
6632 memcpy (regex, &message[1], len);
6634 GNUNET_array_append (c->regexes, c->n_regex, regex);
6635 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6637 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6643 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6649 * Handler for requests of new tunnels
6651 * @param cls closure
6652 * @param client identification of the client
6653 * @param message the actual message
6656 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6657 const struct GNUNET_MessageHeader *message)
6659 struct GNUNET_MESH_TunnelMessage *t_msg;
6660 struct MeshTunnel *t;
6661 struct MeshClient *c;
6662 MESH_TunnelNumber tid;
6664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6666 /* Sanity check for client registration */
6667 if (NULL == (c = client_get (client)))
6670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6675 /* Message sanity check */
6676 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6679 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6683 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6684 /* Sanity check for tunnel numbering */
6685 tid = ntohl (t_msg->tunnel_id);
6686 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6692 /* Sanity check for duplicate tunnel IDs */
6693 if (NULL != tunnel_get_by_local_id (c, tid))
6696 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6700 while (NULL != tunnel_get_by_pi (myid, next_tid))
6701 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6702 t = tunnel_new (myid, next_tid++, c, tid);
6705 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6710 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6712 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6713 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6716 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6722 * Handler for requests of deleting tunnels
6724 * @param cls closure
6725 * @param client identification of the client
6726 * @param message the actual message
6729 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6730 const struct GNUNET_MessageHeader *message)
6732 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6733 struct MeshClient *c;
6734 struct MeshTunnel *t;
6735 MESH_TunnelNumber tid;
6737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6738 "Got a DESTROY TUNNEL from client!\n");
6740 /* Sanity check for client registration */
6741 if (NULL == (c = client_get (client)))
6744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6749 /* Message sanity check */
6750 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6753 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6757 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6759 /* Retrieve tunnel */
6760 tid = ntohl (tunnel_msg->tunnel_id);
6761 t = tunnel_get_by_local_id(c, tid);
6764 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6766 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6769 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6771 client_ignore_tunnel (c, t);
6773 // TODO: when to destroy incoming tunnel?
6774 if (t->nclients == 0)
6776 GNUNET_assert (GNUNET_YES ==
6777 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6779 GNUNET_assert (GNUNET_YES ==
6780 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6781 &my_full_id.hashPubKey,
6785 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6788 send_client_tunnel_disconnect(t, c);
6789 client_delete_tunnel(c, t);
6791 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6793 tunnel_send_destroy (t);
6794 t->destroy = GNUNET_YES;
6795 // The tunnel will be destroyed when the last message is transmitted.
6796 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6802 * Handler for requests of seeting tunnel's speed.
6804 * @param cls Closure (unused).
6805 * @param client Identification of the client.
6806 * @param message The actual message.
6809 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6810 const struct GNUNET_MessageHeader *message)
6812 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6813 struct MeshClient *c;
6814 struct MeshTunnel *t;
6815 MESH_TunnelNumber tid;
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6818 "Got a SPEED request from client!\n");
6820 /* Sanity check for client registration */
6821 if (NULL == (c = client_get (client)))
6824 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6830 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6832 /* Retrieve tunnel */
6833 tid = ntohl (tunnel_msg->tunnel_id);
6834 t = tunnel_get_by_local_id(c, tid);
6837 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6843 switch (ntohs(message->type))
6845 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6846 t->speed_min = GNUNET_YES;
6848 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6849 t->speed_min = GNUNET_NO;
6854 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6859 * Handler for requests of seeting tunnel's buffering policy.
6861 * @param cls Closure (unused).
6862 * @param client Identification of the client.
6863 * @param message The actual message.
6866 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6867 const struct GNUNET_MessageHeader *message)
6869 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6870 struct MeshClient *c;
6871 struct MeshTunnel *t;
6872 MESH_TunnelNumber tid;
6874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6875 "Got a BUFFER request from client!\n");
6877 /* Sanity check for client registration */
6878 if (NULL == (c = client_get (client)))
6881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6886 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6888 /* Retrieve tunnel */
6889 tid = ntohl (tunnel_msg->tunnel_id);
6890 t = tunnel_get_by_local_id(c, tid);
6893 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6899 switch (ntohs(message->type))
6901 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6902 t->nobuffer = GNUNET_NO;
6904 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6905 t->nobuffer = GNUNET_YES;
6911 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6916 * Handler for connection requests to new peers
6918 * @param cls closure
6919 * @param client identification of the client
6920 * @param message the actual message (PeerControl)
6923 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6924 const struct GNUNET_MessageHeader *message)
6926 struct GNUNET_MESH_PeerControl *peer_msg;
6927 struct MeshPeerInfo *peer_info;
6928 struct MeshClient *c;
6929 struct MeshTunnel *t;
6930 MESH_TunnelNumber tid;
6932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6933 /* Sanity check for client registration */
6934 if (NULL == (c = client_get (client)))
6937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6942 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6944 /* Sanity check for message size */
6945 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6948 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6952 /* Tunnel exists? */
6953 tid = ntohl (peer_msg->tunnel_id);
6954 t = tunnel_get_by_local_id (c, tid);
6958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6962 /* Does client own tunnel? */
6963 if (t->owner->handle != client)
6966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6970 GNUNET_i2s (&peer_msg->peer));
6971 peer_info = peer_info_get (&peer_msg->peer);
6973 tunnel_add_peer (t, peer_info);
6974 peer_info_connect (peer_info, t);
6976 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6982 * Handler for disconnection requests of peers in a tunnel
6984 * @param cls closure
6985 * @param client identification of the client
6986 * @param message the actual message (PeerControl)
6989 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6990 const struct GNUNET_MessageHeader *message)
6992 struct GNUNET_MESH_PeerControl *peer_msg;
6993 struct MeshPeerInfo *peer_info;
6994 struct MeshClient *c;
6995 struct MeshTunnel *t;
6996 MESH_TunnelNumber tid;
6998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6999 /* Sanity check for client registration */
7000 if (NULL == (c = client_get (client)))
7003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7008 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7010 /* Sanity check for message size */
7011 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7018 /* Tunnel exists? */
7019 tid = ntohl (peer_msg->tunnel_id);
7020 t = tunnel_get_by_local_id (c, tid);
7024 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7029 /* Does client own tunnel? */
7030 if (t->owner->handle != client)
7033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7038 GNUNET_i2s (&peer_msg->peer));
7039 /* Is the peer in the tunnel? */
7041 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7042 if (NULL == peer_info)
7045 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7049 /* Ok, delete peer from tunnel */
7050 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7051 &peer_msg->peer.hashPubKey);
7053 send_destroy_path (t, peer_info->id);
7054 tunnel_delete_peer (t, peer_info->id);
7055 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7060 * Handler for blacklist requests of peers in a tunnel
7062 * @param cls closure
7063 * @param client identification of the client
7064 * @param message the actual message (PeerControl)
7066 * FIXME implement DHT block bloomfilter
7069 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7070 const struct GNUNET_MessageHeader *message)
7072 struct GNUNET_MESH_PeerControl *peer_msg;
7073 struct MeshClient *c;
7074 struct MeshTunnel *t;
7075 MESH_TunnelNumber tid;
7077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7078 /* Sanity check for client registration */
7079 if (NULL == (c = client_get (client)))
7082 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7087 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7089 /* Sanity check for message size */
7090 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7093 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7097 /* Tunnel exists? */
7098 tid = ntohl (peer_msg->tunnel_id);
7099 t = tunnel_get_by_local_id (c, tid);
7103 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7108 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7109 GNUNET_PEER_intern(&peer_msg->peer));
7114 * Handler for unblacklist requests of peers in a tunnel
7116 * @param cls closure
7117 * @param client identification of the client
7118 * @param message the actual message (PeerControl)
7121 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7122 const struct GNUNET_MessageHeader *message)
7124 struct GNUNET_MESH_PeerControl *peer_msg;
7125 struct MeshClient *c;
7126 struct MeshTunnel *t;
7127 MESH_TunnelNumber tid;
7131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7132 /* Sanity check for client registration */
7133 if (NULL == (c = client_get (client)))
7136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7141 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7143 /* Sanity check for message size */
7144 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7147 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7151 /* Tunnel exists? */
7152 tid = ntohl (peer_msg->tunnel_id);
7153 t = tunnel_get_by_local_id (c, tid);
7157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7162 /* if peer is not known, complain */
7163 pid = GNUNET_PEER_search (&peer_msg->peer);
7170 /* search and remove from list */
7171 for (i = 0; i < t->nblacklisted; i++)
7173 if (t->blacklisted[i] == pid)
7175 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7176 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7181 /* if peer hasn't been blacklisted, complain */
7187 * Handler for connection requests to new peers by type
7189 * @param cls closure
7190 * @param client identification of the client
7191 * @param message the actual message (ConnectPeerByType)
7194 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7195 const struct GNUNET_MessageHeader *message)
7197 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7198 struct MeshClient *c;
7199 struct MeshTunnel *t;
7200 struct GNUNET_HashCode hash;
7201 MESH_TunnelNumber tid;
7203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7204 /* Sanity check for client registration */
7205 if (NULL == (c = client_get (client)))
7208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7213 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7215 /* Sanity check for message size */
7216 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7217 ntohs (connect_msg->header.size))
7220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7224 /* Tunnel exists? */
7225 tid = ntohl (connect_msg->tunnel_id);
7226 t = tunnel_get_by_local_id (c, tid);
7230 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7234 /* Does client own tunnel? */
7235 if (t->owner->handle != client)
7238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7242 /* Do WE have the service? */
7243 t->type = ntohl (connect_msg->type);
7244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7245 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7246 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7249 /* Yes! Fast forward, add ourselves to the tunnel and send the
7250 * good news to the client, and alert the destination client of
7251 * an incoming tunnel.
7253 * FIXME send a path create to self, avoid code duplication
7255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7256 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7257 peer_info_get (&my_full_id),
7258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7261 send_client_peer_connected (t, myid);
7262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7263 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7265 t->local_tid_dest = next_local_tid++;
7266 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7267 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7268 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7272 /* Ok, lets find a peer offering the service */
7273 if (NULL != t->dht_get_type)
7275 GNUNET_DHT_get_stop (t->dht_get_type);
7277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7278 GNUNET_h2s (&hash));
7280 GNUNET_DHT_get_start (dht_handle,
7281 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7283 dht_replication_level,
7284 GNUNET_DHT_RO_RECORD_ROUTE |
7285 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7287 &dht_get_type_handler, t);
7289 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7295 * Handler for connection requests to new peers by a string service description.
7297 * @param cls closure
7298 * @param client identification of the client
7299 * @param message the actual message, which includes messages the client wants
7302 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7303 const struct GNUNET_MessageHeader *message)
7305 struct GNUNET_MESH_ConnectPeerByString *msg;
7306 struct MeshRegexSearchContext *ctx;
7307 struct MeshRegexSearchInfo *info;
7308 struct GNUNET_DHT_GetHandle *get_h;
7309 struct GNUNET_HashCode key;
7310 struct MeshTunnel *t;
7311 struct MeshClient *c;
7312 MESH_TunnelNumber tid;
7316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7317 "Connect by string started\n");
7318 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7319 size = htons (message->size);
7321 /* Sanity check for client registration */
7322 if (NULL == (c = client_get (client)))
7325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7330 /* Message size sanity check */
7331 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7334 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7338 /* Tunnel exists? */
7339 tid = ntohl (msg->tunnel_id);
7340 t = tunnel_get_by_local_id (c, tid);
7344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7348 /* Does client own tunnel? */
7349 if (t->owner->handle != client)
7352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7357 " on tunnel %s [%u]\n",
7358 GNUNET_i2s(&my_full_id),
7361 /* Only one connect_by_string allowed at the same time! */
7362 /* FIXME: allow more, return handle at api level to cancel, document */
7363 if (NULL != t->regex_ctx)
7366 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7370 /* Find string itself */
7371 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7372 string = (const char *) &msg[1];
7374 /* Initialize context */
7375 size = GNUNET_REGEX_get_first_key(string, len, &key);
7376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7377 " consumed %u bits out of %u\n", size, len);
7378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7379 " looking for %s\n", GNUNET_h2s (&key));
7381 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7383 info->description = GNUNET_malloc (len + 1);
7384 memcpy (info->description, string, len);
7385 info->description[len] = '\0';
7386 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7387 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7390 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7391 ctx->position = size;
7395 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7397 /* Start search in DHT */
7398 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7399 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7400 &key, /* key to search */
7401 dht_replication_level, /* replication level */
7402 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7403 NULL, /* xquery */ // FIXME BLOOMFILTER
7404 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7405 &dht_get_string_handler, ctx);
7407 GNUNET_break (GNUNET_OK ==
7408 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7411 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7413 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7419 * Handler for client traffic directed to one peer
7421 * @param cls closure
7422 * @param client identification of the client
7423 * @param message the actual message
7426 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7427 const struct GNUNET_MessageHeader *message)
7429 struct MeshClient *c;
7430 struct MeshTunnel *t;
7431 struct MeshPeerInfo *pi;
7432 struct GNUNET_MESH_Unicast *data_msg;
7433 MESH_TunnelNumber tid;
7436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7437 "Got a unicast request from a client!\n");
7439 /* Sanity check for client registration */
7440 if (NULL == (c = client_get (client)))
7443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7448 data_msg = (struct GNUNET_MESH_Unicast *) message;
7450 /* Sanity check for message size */
7451 size = ntohs (message->size);
7452 if (sizeof (struct GNUNET_MESH_Unicast) +
7453 sizeof (struct GNUNET_MessageHeader) > size)
7456 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7460 /* Tunnel exists? */
7461 tid = ntohl (data_msg->tid);
7462 t = tunnel_get_by_local_id (c, tid);
7466 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7470 /* Is it a local tunnel? Then, does client own the tunnel? */
7471 if (t->owner->handle != client)
7474 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7478 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7479 &data_msg->destination.hashPubKey);
7480 /* Is the selected peer in the tunnel? */
7484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7488 /* PID should be as expected */
7489 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7493 "Unicast PID, expected %u, got %u\n",
7494 t->fwd_pid + 1, ntohl (data_msg->pid));
7495 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7499 /* Ok, everything is correct, send the message
7500 * (pretend we got it from a mesh peer)
7503 /* Work around const limitation */
7504 char buf[ntohs (message->size)] GNUNET_ALIGN;
7505 struct GNUNET_MESH_Unicast *copy;
7507 copy = (struct GNUNET_MESH_Unicast *) buf;
7508 memcpy (buf, data_msg, size);
7509 copy->oid = my_full_id;
7510 copy->tid = htonl (t->id.tid);
7511 copy->ttl = htonl (default_ttl);
7512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7513 " calling generic handler...\n");
7514 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7517 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7524 * Handler for client traffic directed to the origin
7526 * @param cls closure
7527 * @param client identification of the client
7528 * @param message the actual message
7531 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7532 const struct GNUNET_MessageHeader *message)
7534 struct GNUNET_MESH_ToOrigin *data_msg;
7535 struct MeshTunnelClientInfo *clinfo;
7536 struct MeshClient *c;
7537 struct MeshTunnel *t;
7538 MESH_TunnelNumber tid;
7541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7542 "Got a ToOrigin request from a client!\n");
7543 /* Sanity check for client registration */
7544 if (NULL == (c = client_get (client)))
7547 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7552 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7554 /* Sanity check for message size */
7555 size = ntohs (message->size);
7556 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7557 sizeof (struct GNUNET_MessageHeader) > size)
7560 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7564 /* Tunnel exists? */
7565 tid = ntohl (data_msg->tid);
7566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7567 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7570 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7573 t = tunnel_get_by_local_id (c, tid);
7576 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7583 /* It should be sent by someone who has this as incoming tunnel. */
7584 if (GNUNET_NO == client_knows_tunnel (c, t))
7587 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7591 /* PID should be as expected */
7592 clinfo = tunnel_get_client_fc (t, c);
7593 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7596 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7597 "To Origin PID, expected %u, got %u\n",
7598 clinfo->bck_pid + 1,
7599 ntohl (data_msg->pid));
7600 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7604 /* Ok, everything is correct, send the message
7605 * (pretend we got it from a mesh peer)
7609 char buf[ntohs (message->size)] GNUNET_ALIGN;
7610 struct GNUNET_MESH_ToOrigin *copy;
7612 /* Work around const limitation */
7613 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7614 memcpy (buf, data_msg, size);
7615 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7616 copy->tid = htonl (t->id.tid);
7617 copy->ttl = htonl (default_ttl);
7618 if (ntohl (copy->pid) != (t->bck_pid + 1))
7621 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7622 "To Origin PID, expected %u, got %u\n",
7628 copy->sender = my_full_id;
7629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7630 " calling generic handler...\n");
7631 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7633 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7640 * Handler for client traffic directed to all peers in a tunnel
7642 * @param cls closure
7643 * @param client identification of the client
7644 * @param message the actual message
7647 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7648 const struct GNUNET_MessageHeader *message)
7650 struct MeshClient *c;
7651 struct MeshTunnel *t;
7652 struct GNUNET_MESH_Multicast *data_msg;
7653 MESH_TunnelNumber tid;
7655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7656 "Got a multicast request from a client!\n");
7658 /* Sanity check for client registration */
7659 if (NULL == (c = client_get (client)))
7662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7667 data_msg = (struct GNUNET_MESH_Multicast *) message;
7669 /* Sanity check for message size */
7670 if (sizeof (struct GNUNET_MESH_Multicast) +
7671 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7674 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7678 /* Tunnel exists? */
7679 tid = ntohl (data_msg->tid);
7680 t = tunnel_get_by_local_id (c, tid);
7684 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7685 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7691 /* Does client own tunnel? */
7692 if (t->owner->handle != client)
7695 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7699 /* PID should be as expected */
7700 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7703 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7704 "Multicast PID, expected %u, got %u\n",
7705 t->fwd_pid + 1, ntohl (data_msg->pid));
7706 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7711 char buf[ntohs (message->size)] GNUNET_ALIGN;
7712 struct GNUNET_MESH_Multicast *copy;
7714 copy = (struct GNUNET_MESH_Multicast *) buf;
7715 memcpy (buf, message, ntohs (message->size));
7716 copy->oid = my_full_id;
7717 copy->tid = htonl (t->id.tid);
7718 copy->ttl = htonl (default_ttl);
7719 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7721 " calling generic handler...\n");
7722 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7725 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7731 * Handler for client's ACKs for payload traffic.
7733 * @param cls Closure (unused).
7734 * @param client Identification of the client.
7735 * @param message The actual message.
7738 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7739 const struct GNUNET_MessageHeader *message)
7741 struct GNUNET_MESH_LocalAck *msg;
7742 struct MeshTunnel *t;
7743 struct MeshClient *c;
7744 MESH_TunnelNumber tid;
7747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7748 /* Sanity check for client registration */
7749 if (NULL == (c = client_get (client)))
7752 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7757 msg = (struct GNUNET_MESH_LocalAck *) message;
7759 /* Tunnel exists? */
7760 tid = ntohl (msg->tunnel_id);
7761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7762 t = tunnel_get_by_local_id (c, tid);
7766 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7772 ack = ntohl (msg->max_pid);
7773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7775 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7776 if (NULL != t->owner && t->owner->handle == client)
7778 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7780 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7784 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7785 tunnel_set_client_fwd_ack (t, c, ack);
7786 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7789 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7796 * Functions to handle messages from clients
7798 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7799 {&handle_local_new_client, NULL,
7800 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7801 {&handle_local_announce_regex, NULL,
7802 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7803 {&handle_local_tunnel_create, NULL,
7804 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7805 sizeof (struct GNUNET_MESH_TunnelMessage)},
7806 {&handle_local_tunnel_destroy, NULL,
7807 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7808 sizeof (struct GNUNET_MESH_TunnelMessage)},
7809 {&handle_local_tunnel_speed, NULL,
7810 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7811 sizeof (struct GNUNET_MESH_TunnelMessage)},
7812 {&handle_local_tunnel_speed, NULL,
7813 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7814 sizeof (struct GNUNET_MESH_TunnelMessage)},
7815 {&handle_local_tunnel_buffer, NULL,
7816 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7817 sizeof (struct GNUNET_MESH_TunnelMessage)},
7818 {&handle_local_tunnel_buffer, NULL,
7819 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7820 sizeof (struct GNUNET_MESH_TunnelMessage)},
7821 {&handle_local_connect_add, NULL,
7822 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7823 sizeof (struct GNUNET_MESH_PeerControl)},
7824 {&handle_local_connect_del, NULL,
7825 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7826 sizeof (struct GNUNET_MESH_PeerControl)},
7827 {&handle_local_blacklist, NULL,
7828 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7829 sizeof (struct GNUNET_MESH_PeerControl)},
7830 {&handle_local_unblacklist, NULL,
7831 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7832 sizeof (struct GNUNET_MESH_PeerControl)},
7833 {&handle_local_connect_by_type, NULL,
7834 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7835 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7836 {&handle_local_connect_by_string, NULL,
7837 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7838 {&handle_local_unicast, NULL,
7839 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7840 {&handle_local_to_origin, NULL,
7841 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7842 {&handle_local_multicast, NULL,
7843 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7844 {&handle_local_ack, NULL,
7845 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7846 sizeof (struct GNUNET_MESH_LocalAck)},
7852 * To be called on core init/fail.
7854 * @param cls service closure
7855 * @param server handle to the server for this service
7856 * @param identity the public identity of this peer
7859 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7860 const struct GNUNET_PeerIdentity *identity)
7863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7864 core_handle = server;
7865 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7868 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7869 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7871 GNUNET_abort(); // Try harder
7878 * Method called whenever a given peer connects.
7880 * @param cls closure
7881 * @param peer peer identity this notification is about
7882 * @param atsi performance data for the connection
7883 * @param atsi_count number of records in 'atsi'
7886 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7887 const struct GNUNET_ATS_Information *atsi,
7888 unsigned int atsi_count)
7890 struct MeshPeerInfo *peer_info;
7891 struct MeshPeerPath *path;
7893 DEBUG_CONN ("Peer connected\n");
7894 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7895 peer_info = peer_info_get (peer);
7896 if (myid == peer_info->id)
7898 DEBUG_CONN (" (self)\n");
7903 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7905 path = path_new (2);
7906 path->peers[0] = myid;
7907 path->peers[1] = peer_info->id;
7908 GNUNET_PEER_change_rc (myid, 1);
7909 GNUNET_PEER_change_rc (peer_info->id, 1);
7910 peer_info_add_path (peer_info, path, GNUNET_YES);
7911 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7917 * Method called whenever a peer disconnects.
7919 * @param cls closure
7920 * @param peer peer identity this notification is about
7923 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7925 struct MeshPeerInfo *pi;
7926 struct MeshPeerQueue *q;
7927 struct MeshPeerQueue *n;
7929 DEBUG_CONN ("Peer disconnected\n");
7930 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7940 /* TODO try to reroute this traffic instead */
7941 queue_destroy(q, GNUNET_YES);
7944 if (NULL != pi->core_transmit)
7946 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
7947 pi->core_transmit = NULL;
7949 peer_info_remove_path (pi, pi->id, myid);
7952 DEBUG_CONN (" (self)\n");
7954 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7959 /******************************************************************************/
7960 /************************ MAIN FUNCTIONS ****************************/
7961 /******************************************************************************/
7964 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7966 * @param cls closure
7967 * @param key current key code
7968 * @param value value in the hash map
7969 * @return GNUNET_YES if we should continue to iterate,
7973 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7975 struct MeshTunnel *t = value;
7982 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7984 * @param cls closure
7985 * @param key current key code
7986 * @param value value in the hash map
7987 * @return GNUNET_YES if we should continue to iterate,
7991 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7993 struct MeshPeerInfo *p = value;
7994 struct MeshPeerQueue *q;
7995 struct MeshPeerQueue *n;
8003 queue_destroy(q, GNUNET_YES);
8007 peer_info_destroy (p);
8013 * Task run during shutdown.
8019 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8023 if (core_handle != NULL)
8025 GNUNET_CORE_disconnect (core_handle);
8030 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8033 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8034 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8035 if (dht_handle != NULL)
8037 GNUNET_DHT_disconnect (dht_handle);
8042 GNUNET_SERVER_notification_context_destroy (nc);
8045 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8047 GNUNET_SCHEDULER_cancel (announce_id_task);
8048 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8055 * Callback for hostkey read/generation
8058 * @param pk the private key
8059 * @param emsg error message
8062 key_generation_cb (void *cls,
8063 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8066 struct MeshPeerInfo *peer;
8067 struct MeshPeerPath *p;
8072 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8073 _("Mesh service could not access hostkey. Exiting.\n"));
8074 GNUNET_SCHEDULER_shutdown ();
8077 my_private_key = pk;
8078 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8079 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8080 &my_full_id.hashPubKey);
8081 myid = GNUNET_PEER_intern (&my_full_id);
8082 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8083 "Mesh for peer [%s] starting\n",
8084 GNUNET_i2s(&my_full_id));
8086 // transport_handle = GNUNET_TRANSPORT_connect(c,
8096 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8099 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8100 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8101 GNUNET_SERVER_disconnect_notify (server_handle,
8102 &handle_local_client_disconnect, NULL);
8106 clients_tail = NULL;
8109 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8110 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8112 /* Create a peer_info for the local peer */
8113 peer = peer_info_get (&my_full_id);
8116 GNUNET_PEER_change_rc (myid, 1);
8117 peer_info_add_path (peer, p, GNUNET_YES);
8118 GNUNET_SERVER_resume (server_handle);
8119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8124 * Process mesh requests.
8126 * @param cls closure
8127 * @param server the initialized server
8128 * @param c configuration to use
8131 run (void *cls, struct GNUNET_SERVER_Handle *server,
8132 const struct GNUNET_CONFIGURATION_Handle *c)
8136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8137 server_handle = server;
8138 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8139 NULL, /* Closure passed to MESH functions */
8140 &core_init, /* Call core_init once connected */
8141 &core_connect, /* Handle connects */
8142 &core_disconnect, /* remove peers on disconnects */
8143 NULL, /* Don't notify about all incoming messages */
8144 GNUNET_NO, /* For header only in notification */
8145 NULL, /* Don't notify about all outbound messages */
8146 GNUNET_NO, /* For header-only out notification */
8147 core_handlers); /* Register these handlers */
8149 if (core_handle == NULL)
8152 GNUNET_SCHEDULER_shutdown ();
8157 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8162 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8164 GNUNET_SCHEDULER_shutdown ();
8169 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8170 &refresh_path_time))
8172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8174 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8175 "mesh", "refresh path time");
8176 GNUNET_SCHEDULER_shutdown ();
8181 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8182 &app_announce_time))
8184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8186 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8187 "mesh", "app announce time");
8188 GNUNET_SCHEDULER_shutdown ();
8193 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8198 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8199 "mesh", "id announce time");
8200 GNUNET_SCHEDULER_shutdown ();
8208 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8209 &unacknowledged_wait_time))
8211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8213 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8214 "mesh", "unacknowledged wait time");
8215 GNUNET_SCHEDULER_shutdown ();
8220 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8225 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8226 "mesh", "connect timeout");
8227 GNUNET_SCHEDULER_shutdown ();
8232 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8237 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8238 "mesh", "max msgs queue");
8239 GNUNET_SCHEDULER_shutdown ();
8244 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8249 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8250 "mesh", "max tunnels");
8251 GNUNET_SCHEDULER_shutdown ();
8256 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8261 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8262 "mesh", "default ttl", 64);
8267 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8268 &dht_replication_level))
8270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8272 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8273 "mesh", "dht replication level", 10);
8274 dht_replication_level = 10;
8277 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8278 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8279 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8280 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8281 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8283 dht_handle = GNUNET_DHT_connect (c, 64);
8284 if (NULL == dht_handle)
8288 stats = GNUNET_STATISTICS_create ("mesh", c);
8290 GNUNET_SERVER_suspend (server_handle);
8291 /* Scheduled the task to clean up when shutdown is called */
8292 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8294 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8295 GNUNET_free (keyfile);
8300 * The main function for the mesh service.
8302 * @param argc number of arguments from the command line
8303 * @param argv command line arguments
8304 * @return 0 ok, 1 on error
8307 main (int argc, char *const *argv)
8312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8313 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8315 ret = (GNUNET_OK == r) ? 0 : 1;
8316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8320 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8321 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8322 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);