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 * Task handler for delayed connect task;
211 GNUNET_SCHEDULER_TaskIdentifier connect_task;
214 * Number of attempts to reconnect so far
216 int n_reconnect_attempts;
219 * Paths to reach the peer, ordered by ascending hop count
221 struct MeshPeerPath *path_head;
224 * Paths to reach the peer, ordered by ascending hop count
226 struct MeshPeerPath *path_tail;
229 * Handle to stop the DHT search for a path to this peer
231 struct GNUNET_DHT_GetHandle *dhtget;
234 * Closure given to the DHT GET
236 struct MeshPathInfo *dhtgetcls;
239 * Array of tunnels this peer participates in
240 * (most probably a small amount, therefore not a hashmap)
241 * When the path to the peer changes, notify these tunnels to let them
242 * re-adjust their path trees.
244 struct MeshTunnel **tunnels;
247 * Number of tunnels this peers participates in
249 unsigned int ntunnels;
252 * Transmission queue to core DLL head
254 struct MeshPeerQueue *queue_head;
257 * Transmission queue to core DLL tail
259 struct MeshPeerQueue *queue_tail;
262 * How many messages are in the queue to this peer.
264 unsigned int queue_n;
267 * Handle to for queued transmissions
269 struct GNUNET_CORE_TransmitHandle *core_transmit;
274 * Globally unique tunnel identification (owner + number)
275 * DO NOT USE OVER THE NETWORK
280 * Node that owns the tunnel
285 * Tunnel number to differentiate all the tunnels owned by the node oid
286 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
288 MESH_TunnelNumber tid;
293 * Struct containing all information regarding a tunnel
294 * For an intermediate node the improtant info used will be:
295 * - id Tunnel unique identification
296 * - paths[0] To know where to send it next
297 * - metainfo: ready, speeds, accounting
304 struct MESH_TunnelID id;
307 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
309 MESH_TunnelNumber local_tid;
312 * Local tunnel number for local destination clients (incoming number)
313 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
316 MESH_TunnelNumber local_tid_dest;
319 * Is the speed on the tunnel limited to the slowest peer?
324 * Is the tunnel bufferless (minimum latency)?
329 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
334 * Packet ID of the last bck packet sent (unique counter per hop).
339 * SKIP value for this tunnel.
344 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
345 * Contains the Flow Control info: FWD ACK value received,
346 * last BCK ACK sent, PID and SKIP values.
348 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
351 * Last ACK sent towards the origin (for traffic towards leaf node).
353 uint32_t last_fwd_ack;
356 * BCK ACK value received from the hop towards the owner of the tunnel,
357 * (previous node / owner): up to what message PID can we sent back to him.
362 * How many messages are in the forward queue (towards leaves).
364 unsigned int fwd_queue_n;
367 * How many messages do we accept in the forward queue.
369 unsigned int fwd_queue_max;
372 * How many messages are in the backward queue (towards origin).
374 unsigned int bck_queue_n;
377 * How many messages do we accept in the backward queue.
379 unsigned int bck_queue_max;
382 * Task to poll peer in case of a stall.
384 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
387 * Last time the tunnel was used
389 struct GNUNET_TIME_Absolute timestamp;
392 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
393 * containing peers added by id or by type, not intermediate peers.
395 struct GNUNET_CONTAINER_MultiHashMap *peers;
398 * Number of peers that are connected and potentially ready to receive data
400 unsigned int peers_ready;
403 * Number of peers that have been added to the tunnel
405 unsigned int peers_total;
408 * Client owner of the tunnel, if any
410 struct MeshClient *owner;
413 * Clients that have been informed about and want to stay in the tunnel.
415 struct MeshClient **clients;
418 * Flow control info for each client.
420 struct MeshTunnelClientInfo *clients_fc;
423 * Number of elements in clients/clients_fc
425 unsigned int nclients;
428 * Clients that have been informed but requested to leave the tunnel.
430 struct MeshClient **ignore;
433 * Number of elements in clients
435 unsigned int nignore;
440 GNUNET_PEER_Id *blacklisted;
443 * Number of elements in blacklisted
445 unsigned int nblacklisted;
448 * Bloomfilter (for peer identities) to stop circular routes
450 char bloomfilter[MESH_BLOOM_SIZE];
455 struct MeshTunnelTree *tree;
458 * Application type we are looking for in this tunnel
460 GNUNET_MESH_ApplicationType type;
463 * Used to search peers offering a service
465 struct GNUNET_DHT_GetHandle *dht_get_type;
468 * Initial context of the regex search for a connect_by_string
470 struct MeshRegexSearchContext *regex_ctx;
473 * Task to keep the used paths alive
475 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
478 * Task to destroy the tunnel after timeout
480 * FIXME: merge the two? a tunnel will have either
481 * a path refresh OR a timeout, never both!
483 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
486 * Flag to signal the destruction of the tunnel.
487 * If this is set GNUNET_YES the tunnel will be destroyed
488 * when the queue is empty.
495 * Info about a child node in a tunnel, needed to perform flow control.
497 struct MeshTunnelChildInfo
500 * ID of the child node.
515 * Maximum PID allowed (FWD ACK received).
520 * Last ACK sent to that child (BCK ACK).
525 * Circular buffer pointing to MeshPeerQueue elements for all
526 * payload traffic going to this child.
527 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
529 struct MeshPeerQueue **send_buffer;
532 * Index of the oldest element in the send_buffer.
534 unsigned int send_buffer_start;
537 * How many elements are already in the buffer.
539 unsigned int send_buffer_n;
542 * Tunnel this info is about
544 struct MeshTunnel *t;
547 * Task to poll peer in case of a stall.
549 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
554 * Info about a leaf client of a tunnel, needed to perform flow control.
556 struct MeshTunnelClientInfo
559 * PID of the last packet sent to the client (FWD).
564 * PID of the last packet received from the client (BCK).
569 * Maximum PID allowed (FWD ACK received).
574 * Last ACK sent to that child (BCK ACK).
582 * Info collected during iteration of child nodes in order to get the ACK value
585 struct MeshTunnelChildIteratorContext
588 * Tunnel whose info is being collected.
590 struct MeshTunnel *t;
593 * Is this context initialized? Is the value in max_child_ack valid?
598 * Maximum child ACK so far.
600 uint32_t max_child_ack;
603 * Number of children nodes
605 unsigned int nchildren;
610 * Info needed to work with tunnel paths and peers
617 struct MeshTunnel *t;
620 * Neighbouring peer to whom we send the packet to
622 struct MeshPeerInfo *peer;
627 struct MeshPeerPath *path;
632 * Struct containing information about a client of the service
639 struct MeshClient *next;
644 struct MeshClient *prev;
647 * Tunnels that belong to this client, indexed by local id
649 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
652 * Tunnels this client has accepted, indexed by incoming local id
654 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
657 * Tunnels this client has rejected, indexed by incoming local id
659 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
661 * Handle to communicate with the client
663 struct GNUNET_SERVER_Client *handle;
666 * Applications that this client has claimed to provide
668 struct GNUNET_CONTAINER_MultiHashMap *apps;
671 * Messages that this client has declared interest in
673 struct GNUNET_CONTAINER_MultiHashMap *types;
676 * Whether the client is active or shutting down (don't send confirmations
677 * to a client that is shutting down.
682 * ID of the client, mainly for debug messages
687 * Regular expressions describing the services offered by this client.
689 char **regexes; // FIXME add timeout? API to remove a regex?
692 * Number of regular expressions in regexes.
694 unsigned int n_regex;
697 * Task to refresh all regular expresions in the DHT.
699 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
705 * Struct to keep information of searches of services described by a regex
706 * using a user-provided string service description.
708 struct MeshRegexSearchInfo
711 * Which tunnel is this for
713 struct MeshTunnel *t;
716 * User provided description of the searched service.
721 * Part of the description already consumed by the search.
728 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
731 * Results from running DHT GETs.
733 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
736 * Contexts, for each running DHT GET. Free all on end of search.
738 struct MeshRegexSearchContext **contexts;
741 * Number of contexts (branches/steps in search).
743 unsigned int n_contexts;
746 * Peer that is connecting via connect_by_string. When connected, free ctx.
751 * Other peers that are found but not yet being connected to.
753 GNUNET_PEER_Id *peers;
756 * Number of elements in peers.
758 unsigned int n_peers;
761 * Next peer to try to connect to.
766 * Timeout for a connect attempt.
767 * When reached, try to connect to a different peer, if any. If not,
768 * try the same peer again.
770 GNUNET_SCHEDULER_TaskIdentifier timeout;
775 * Struct to keep state of running searches that have consumed a part of
778 struct MeshRegexSearchContext
781 * Part of the description already consumed by
782 * this particular search branch.
787 * Information about the search.
789 struct MeshRegexSearchInfo *info;
792 * We just want to look for one edge, the longer the better.
795 unsigned int longest_match;
798 * Destination hash of the longest match.
800 struct GNUNET_HashCode hash;
803 /******************************************************************************/
804 /************************ DEBUG FUNCTIONS ****************************/
805 /******************************************************************************/
809 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
810 * @param cls string to print
811 * @param success GNUNET_OK if the PUT was transmitted,
812 * GNUNET_NO on timeout,
813 * GNUNET_SYSERR on disconnect from service
814 * after the PUT message was transmitted
815 * (so we don't know if it was received or not)
820 mesh_debug (void *cls, int success)
824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
828 unsigned int debug_fwd_ack;
829 unsigned int debug_bck_ack;
833 /******************************************************************************/
834 /*********************** GLOBAL VARIABLES ****************************/
835 /******************************************************************************/
838 * Configuration parameters
840 static struct GNUNET_TIME_Relative refresh_path_time;
841 static struct GNUNET_TIME_Relative app_announce_time;
842 static struct GNUNET_TIME_Relative id_announce_time;
843 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
844 static struct GNUNET_TIME_Relative connect_timeout;
845 static unsigned long long default_ttl;
846 static unsigned long long dht_replication_level;
847 static unsigned long long max_tunnels;
848 static unsigned long long max_msgs_queue;
852 * Hostkey generation context
854 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
857 * DLL with all the clients, head.
859 static struct MeshClient *clients;
862 * DLL with all the clients, tail.
864 static struct MeshClient *clients_tail;
867 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
869 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
872 * Number of tunnels known.
874 static unsigned long long n_tunnels;
877 * Tunnels incoming, indexed by MESH_TunnelNumber
878 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
880 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
883 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
885 static struct GNUNET_CONTAINER_MultiHashMap *peers;
888 * Handle to communicate with transport
890 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
893 * Handle to communicate with core.
895 static struct GNUNET_CORE_Handle *core_handle;
900 static struct GNUNET_DHT_Handle *dht_handle;
905 static struct GNUNET_SERVER_Handle *server_handle;
908 * Handle to the statistics service.
910 static struct GNUNET_STATISTICS_Handle *stats;
913 * Notification context, to send messages to local clients.
915 static struct GNUNET_SERVER_NotificationContext *nc;
918 * Local peer own ID (memory efficient handle).
920 static GNUNET_PEER_Id myid;
923 * Local peer own ID (full value).
925 static struct GNUNET_PeerIdentity my_full_id;
930 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
935 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
938 * Tunnel ID for the next created tunnel (global tunnel number).
940 static MESH_TunnelNumber next_tid;
943 * Tunnel ID for the next incoming tunnel (local tunnel number).
945 static MESH_TunnelNumber next_local_tid;
948 * All application types provided by this peer.
950 static struct GNUNET_CONTAINER_MultiHashMap *applications;
953 * All message types clients of this peer are interested in.
955 static struct GNUNET_CONTAINER_MultiHashMap *types;
958 * Task to periodically announce provided applications.
960 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
963 * Task to periodically announce itself in the network.
965 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
968 * Next ID to assign to a client.
970 unsigned int next_client_id;
973 /******************************************************************************/
974 /*********************** DECLARATIONS **************************/
975 /******************************************************************************/
978 * Function to process paths received for a new peer addition. The recorded
979 * paths form the initial tunnel, which can be optimized later.
980 * Called on each result obtained for the DHT search.
983 * @param exp when will this value expire
984 * @param key key of the result
985 * @param type type of the result
986 * @param size number of bytes in data
987 * @param data pointer to the result data
990 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
991 const struct GNUNET_HashCode * key,
992 const struct GNUNET_PeerIdentity *get_path,
993 unsigned int get_path_length,
994 const struct GNUNET_PeerIdentity *put_path,
995 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
996 size_t size, const void *data);
1000 * Function to process DHT string to regex matching.
1001 * Called on each result obtained for the DHT search.
1003 * @param cls closure (search context)
1004 * @param exp when will this value expire
1005 * @param key key of the result
1006 * @param get_path path of the get request (not used)
1007 * @param get_path_length lenght of get_path (not used)
1008 * @param put_path path of the put request (not used)
1009 * @param put_path_length length of the put_path (not used)
1010 * @param type type of the result
1011 * @param size number of bytes in data
1012 * @param data pointer to the result data
1014 * TODO: re-issue the request after certain time? cancel after X results?
1017 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1018 const struct GNUNET_HashCode * key,
1019 const struct GNUNET_PeerIdentity *get_path,
1020 unsigned int get_path_length,
1021 const struct GNUNET_PeerIdentity *put_path,
1022 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1023 size_t size, const void *data);
1027 * Function to process DHT string to regex matching.
1028 * Called on each result obtained for the DHT search.
1030 * @param cls closure (search context)
1031 * @param exp when will this value expire
1032 * @param key key of the result
1033 * @param get_path path of the get request (not used)
1034 * @param get_path_length lenght of get_path (not used)
1035 * @param put_path path of the put request (not used)
1036 * @param put_path_length length of the put_path (not used)
1037 * @param type type of the result
1038 * @param size number of bytes in data
1039 * @param data pointer to the result data
1042 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1043 const struct GNUNET_HashCode * key,
1044 const struct GNUNET_PeerIdentity *get_path,
1045 unsigned int get_path_length,
1046 const struct GNUNET_PeerIdentity *put_path,
1047 unsigned int put_path_length,
1048 enum GNUNET_BLOCK_Type type,
1049 size_t size, const void *data);
1053 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1054 * and insert it in the appropiate structures if the peer is not known yet.
1056 * @param peer Short identity of the peer.
1058 * @return Existing or newly created peer info.
1060 static struct MeshPeerInfo *
1061 peer_info_get_short (const GNUNET_PEER_Id peer);
1065 * Try to establish a new connection to this peer.
1066 * Use the best path for the given tunnel.
1067 * If the peer doesn't have any path to it yet, try to get one.
1068 * If the peer already has some path, send a CREATE PATH towards it.
1070 * @param peer PeerInfo of the peer.
1071 * @param t Tunnel for which to create the path, if possible.
1074 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1078 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1079 * needed rescources.
1080 * If peer already exists, reevaluate shortest path and change if different.
1082 * @param t Tunnel we want to add a new peer to
1083 * @param peer PeerInfo of the peer being added
1087 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1091 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1092 * that are no longer needed, as well as nodes of no longer reachable peers.
1093 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1095 * @param t Tunnel from which to remove the path.
1096 * @param peer Short id of the peer which should be removed.
1099 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1103 * Search for a tunnel by global ID using full PeerIdentities.
1105 * @param oid owner of the tunnel.
1106 * @param tid global tunnel number.
1108 * @return tunnel handler, NULL if doesn't exist.
1110 static struct MeshTunnel *
1111 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1115 * Delete an active client from the tunnel.
1121 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1124 * Notify a tunnel that a connection has broken that affects at least
1125 * some of its peers.
1127 * @param t Tunnel affected.
1128 * @param p1 Peer that got disconnected from p2.
1129 * @param p2 Peer that got disconnected from p1.
1131 * @return Short ID of the peer disconnected (either p1 or p2).
1132 * 0 if the tunnel remained unaffected.
1134 static GNUNET_PEER_Id
1135 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1140 * Get the current ack value for a tunnel, for data going from root to leaves,
1141 * taking in account the tunnel mode and the status of all children and clients.
1145 * @return Maximum PID allowed.
1148 tunnel_get_fwd_ack (struct MeshTunnel *t);
1152 * Add a client to a tunnel, initializing all needed data structures.
1154 * @param t Tunnel to which add the client.
1155 * @param c Client which to add to the tunnel.
1158 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1162 * Jump to the next edge, with the longest matching token.
1164 * @param block Block found in the DHT.
1165 * @param size Size of the block.
1166 * @param ctx Context of the search.
1168 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1171 regex_next_edge (const struct MeshRegexBlock *block,
1173 struct MeshRegexSearchContext *ctx);
1177 * Find a path to a peer that offers a regex servcie compatible
1178 * with a given string.
1180 * @param key The key of the accepting state.
1181 * @param ctx Context containing info about the string, tunnel, etc.
1184 regex_find_path (const struct GNUNET_HashCode *key,
1185 struct MeshRegexSearchContext *ctx);
1189 * @brief Queue and pass message to core when possible.
1191 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1192 * and accounts for it. In case the queue is full, the message is dropped and
1195 * Otherwise, message is treated as internal and allowed to go regardless of
1198 * @param cls Closure (@c type dependant). It will be used by queue_send to
1199 * build the message to be sent if not already prebuilt.
1200 * @param type Type of the message, 0 for a raw message.
1201 * @param size Size of the message.
1202 * @param dst Neighbor to send message to.
1203 * @param t Tunnel this message belongs to.
1206 queue_add (void *cls, uint16_t type, size_t size,
1207 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1211 * Free a transmission that was already queued with all resources
1212 * associated to the request.
1214 * @param queue Queue handler to cancel.
1215 * @param clear_cls Is it necessary to free associated cls?
1218 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1222 * @brief Get the next transmittable message from the queue.
1224 * This will be the head, except in the case of being a data packet
1225 * not allowed by the destination peer.
1227 * @param peer Destination peer.
1229 * @return The next viable MeshPeerQueue element to send to that peer.
1230 * NULL when there are no transmittable messages.
1232 struct MeshPeerQueue *
1233 queue_get_next (const struct MeshPeerInfo *peer);
1237 * Core callback to write a queued packet to core buffer
1239 * @param cls Closure (peer info).
1240 * @param size Number of bytes available in buf.
1241 * @param buf Where the to write the message.
1243 * @return number of bytes written to buf
1246 queue_send (void *cls, size_t size, void *buf);
1248 /******************************************************************************/
1249 /************************ ITERATORS ****************************/
1250 /******************************************************************************/
1253 * Iterator over found existing mesh regex blocks that match an ongoing search.
1255 * @param cls closure
1256 * @param key current key code
1257 * @param value value in the hash map
1258 * @return GNUNET_YES if we should continue to iterate,
1262 regex_result_iterator (void *cls,
1263 const struct GNUNET_HashCode * key,
1266 struct MeshRegexBlock *block = value;
1267 struct MeshRegexSearchContext *ctx = cls;
1269 if (GNUNET_YES == ntohl(block->accepting) &&
1270 ctx->position == strlen (ctx->info->description))
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1273 regex_find_path (key, ctx);
1274 return GNUNET_YES; // We found an accept state!
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1279 ctx->position, strlen(ctx->info->description),
1280 ntohl(block->accepting));
1283 regex_next_edge(block, SIZE_MAX, ctx);
1290 * Iterator over edges in a regex block retrieved from the DHT.
1292 * @param cls Closure (context of the search).
1293 * @param token Token that follows to next state.
1294 * @param len Lenght of token.
1295 * @param key Hash of next state.
1297 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1300 regex_edge_iterator (void *cls,
1303 const struct GNUNET_HashCode *key)
1305 struct MeshRegexSearchContext *ctx = cls;
1306 struct MeshRegexSearchInfo *info = ctx->info;
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1313 current = &info->description[ctx->position];
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1315 current_len = strlen (info->description) - ctx->position;
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1320 if (len > current_len)
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1323 return GNUNET_YES; // Token too long, wont match
1325 if (0 != strncmp (current, token, len))
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1328 return GNUNET_YES; // Token doesn't match
1331 if (len > ctx->longest_match)
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1334 ctx->longest_match = len;
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1348 * Jump to the next edge, with the longest matching token.
1350 * @param block Block found in the DHT.
1351 * @param size Size of the block.
1352 * @param ctx Context of the search.
1354 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1357 regex_next_edge (const struct MeshRegexBlock *block,
1359 struct MeshRegexSearchContext *ctx)
1361 struct MeshRegexSearchContext *new_ctx;
1362 struct MeshRegexSearchInfo *info = ctx->info;
1363 struct GNUNET_DHT_GetHandle *get_h;
1367 /* Find the longest match for the current string position,
1368 * among tokens in the given block */
1369 ctx->longest_match = 0;
1370 result = GNUNET_MESH_regex_block_iterate (block, size,
1371 ®ex_edge_iterator, ctx);
1372 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1374 /* Did anything match? */
1375 if (0 == ctx->longest_match)
1378 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1379 new_ctx->info = info;
1380 new_ctx->position = ctx->position + ctx->longest_match;
1381 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1383 /* Check whether we already have a DHT GET running for it */
1385 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1388 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1390 ®ex_result_iterator,
1392 return; // We are already looking for it
1395 /* Start search in DHT */
1397 GNUNET_DHT_get_start (dht_handle, /* handle */
1398 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1399 &ctx->hash, /* key to search */
1400 dht_replication_level, /* replication level */
1401 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1402 NULL, /* xquery */ // FIXME BLOOMFILTER
1403 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1404 &dht_get_string_handler, new_ctx);
1406 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1409 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1418 * Iterator over hash map entries to cancel DHT GET requests after a
1419 * successful connect_by_string.
1421 * @param cls Closure (unused).
1422 * @param key Current key code (unused).
1423 * @param value Value in the hash map (get handle).
1424 * @return GNUNET_YES if we should continue to iterate,
1428 regex_cancel_dht_get (void *cls,
1429 const struct GNUNET_HashCode * key,
1432 struct GNUNET_DHT_GetHandle *h = value;
1434 GNUNET_DHT_get_stop (h);
1440 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1441 * search for connect_by_string.
1443 * @param cls Closure (unused).
1444 * @param key Current key code (unused).
1445 * @param value MeshRegexBlock in the hash map.
1446 * @return GNUNET_YES if we should continue to iterate,
1450 regex_free_result (void *cls,
1451 const struct GNUNET_HashCode * key,
1455 GNUNET_free (value);
1461 * Regex callback iterator to store own service description in the DHT.
1463 * @param cls closure.
1464 * @param key hash for current state.
1465 * @param proof proof for current state.
1466 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1467 * @param num_edges number of edges leaving current state.
1468 * @param edges edges leaving current state.
1471 regex_iterator (void *cls,
1472 const struct GNUNET_HashCode *key,
1475 unsigned int num_edges,
1476 const struct GNUNET_REGEX_Edge *edges)
1478 struct MeshRegexBlock *block;
1479 struct MeshRegexEdge *block_edge;
1480 enum GNUNET_DHT_RouteOption opt;
1484 unsigned int offset;
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1488 " regex dht put for state %s\n",
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1498 if (GNUNET_YES == accepting)
1500 struct MeshRegexAccept block;
1502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1503 " state %s is accepting, putting own id\n",
1505 size = sizeof (block);
1507 block.id = my_full_id;
1509 GNUNET_DHT_put(dht_handle, key,
1510 dht_replication_level,
1511 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1512 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1515 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1520 len = strlen(proof);
1521 size = sizeof (struct MeshRegexBlock) + len;
1522 block = GNUNET_malloc (size);
1525 block->n_proof = htonl (len);
1526 block->n_edges = htonl (num_edges);
1527 block->accepting = htonl (accepting);
1529 /* Store the proof at the end of the block. */
1530 aux = (char *) &block[1];
1531 memcpy (aux, proof, len);
1534 /* Store each edge in a variable length MeshEdge struct at the
1535 * very end of the MeshRegexBlock structure.
1537 for (i = 0; i < num_edges; i++)
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 " edge %s towards %s\n",
1542 GNUNET_h2s(&edges[i].destination));
1544 /* aux points at the end of the last block */
1545 len = strlen (edges[i].label);
1546 size += sizeof (struct MeshRegexEdge) + len;
1547 // Calculate offset FIXME is this ok? use size instead?
1548 offset = aux - (char *) block;
1549 block = GNUNET_realloc (block, size);
1550 aux = &((char *) block)[offset];
1551 block_edge = (struct MeshRegexEdge *) aux;
1552 block_edge->key = edges[i].destination;
1553 block_edge->n_token = htonl (len);
1554 aux = (char *) &block_edge[1];
1555 memcpy (aux, edges[i].label, len);
1559 GNUNET_DHT_put(dht_handle, key,
1560 dht_replication_level,
1562 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1564 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1568 GNUNET_free (block);
1573 * Store the regular expression describing a local service into the DHT.
1575 * @param regex The regular expresion.
1578 regex_put (const char *regex)
1580 struct GNUNET_REGEX_Automaton *dfa;
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1583 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1584 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1585 GNUNET_REGEX_automaton_destroy (dfa);
1586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1591 * Find a path to a peer that offers a regex servcie compatible
1592 * with a given string.
1594 * @param key The key of the accepting state.
1595 * @param ctx Context containing info about the string, tunnel, etc.
1598 regex_find_path (const struct GNUNET_HashCode *key,
1599 struct MeshRegexSearchContext *ctx)
1601 struct GNUNET_DHT_GetHandle *get_h;
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1604 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1605 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1606 key, /* key to search */
1607 dht_replication_level, /* replication level */
1608 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1609 GNUNET_DHT_RO_RECORD_ROUTE,
1610 NULL, /* xquery */ // FIXME BLOOMFILTER
1611 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1612 &dht_get_string_accept_handler, ctx);
1613 GNUNET_break (GNUNET_OK ==
1614 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1617 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1622 * Function called if the connect attempt to a peer found via
1623 * connect_by_string times out. Try to connect to another peer, if any.
1624 * Otherwise try to reconnect to the same peer.
1626 * @param cls Closure (info about regex search).
1627 * @param tc TaskContext.
1630 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1632 struct MeshRegexSearchInfo *info = cls;
1633 struct MeshPeerInfo *peer_info;
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1638 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1639 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1647 if (0 < info->n_peers)
1649 // Select next peer, put current in that spot.
1650 id = info->peers[info->i_peer];
1651 info->peers[info->i_peer] = info->peer;
1652 info->i_peer = (info->i_peer + 1) % info->n_peers;
1656 // Try to connect to same peer again.
1659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1661 peer_info = peer_info_get_short(id);
1662 tunnel_add_peer (info->t, peer_info);
1664 tunnel_delete_peer (info->t, old);
1665 peer_info_connect (peer_info, info->t);
1666 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1667 ®ex_connect_timeout,
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1674 * Cancel an ongoing regex search in the DHT and free all resources.
1676 * @param ctx The search context.
1679 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1681 struct MeshRegexSearchInfo *info = ctx->info;
1684 GNUNET_free (info->description);
1685 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1686 ®ex_cancel_dht_get, NULL);
1687 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1688 ®ex_free_result, NULL);
1689 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1690 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1691 info->t->regex_ctx = NULL;
1692 for (i = 0; i < info->n_contexts; i++)
1694 GNUNET_free (info->contexts[i]);
1696 if (0 < info->n_contexts)
1697 GNUNET_free (info->contexts);
1698 if (0 < info->n_peers)
1699 GNUNET_free (info->peers);
1700 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1702 GNUNET_SCHEDULER_cancel(info->timeout);
1708 /******************************************************************************/
1709 /************************ PERIODIC FUNCTIONS ****************************/
1710 /******************************************************************************/
1713 * Announce iterator over for each application provided by the peer
1715 * @param cls closure
1716 * @param key current key code
1717 * @param value value in the hash map
1718 * @return GNUNET_YES if we should continue to
1723 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1725 struct PBlock block;
1726 struct MeshClient *c;
1728 block.id = my_full_id;
1729 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1730 GNUNET_assert(NULL != c);
1731 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1732 if (0 == block.type)
1737 block.type = htonl (block.type);
1739 GNUNET_break (NULL !=
1740 GNUNET_DHT_put (dht_handle, key,
1741 dht_replication_level,
1742 GNUNET_DHT_RO_RECORD_ROUTE |
1743 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1744 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1746 (const char *) &block,
1747 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1749 app_announce_time, NULL, NULL));
1755 * Periodically announce what applications are provided by local clients
1758 * @param cls closure
1759 * @param tc task context
1762 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1764 struct MeshClient *c = cls;
1767 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1769 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1773 DEBUG_DHT ("Starting PUT for regex\n");
1775 for (i = 0; i < c->n_regex; i++)
1777 regex_put (c->regexes[i]);
1779 c->regex_announce_task =
1780 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1781 DEBUG_DHT ("Finished PUT for regex\n");
1788 * Periodically announce what applications are provided by local clients
1791 * @param cls closure
1792 * @param tc task context
1795 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1797 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1799 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1803 DEBUG_DHT ("Starting PUT for apps\n");
1805 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1807 announce_applications_task =
1808 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1810 DEBUG_DHT ("Finished PUT for apps\n");
1817 * Periodically announce self id in the DHT
1819 * @param cls closure
1820 * @param tc task context
1823 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1825 struct PBlock block;
1827 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1829 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1833 * - Set data expiration in function of X
1834 * - Adapt X to churn
1836 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1838 block.id = my_full_id;
1839 block.type = htonl (0);
1840 GNUNET_DHT_put (dht_handle, /* DHT handle */
1841 &my_full_id.hashPubKey, /* Key to use */
1842 dht_replication_level, /* Replication level */
1843 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1844 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1845 sizeof (block), /* Size of the data */
1846 (const char *) &block, /* Data itself */
1847 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1848 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1849 NULL, /* Continuation */
1850 NULL); /* Continuation closure */
1852 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1856 /******************************************************************************/
1857 /****************** GENERAL HELPER FUNCTIONS ************************/
1858 /******************************************************************************/
1861 * Decrements the reference counter and frees all resources if needed
1863 * @param mesh_data Data Descriptor used in a multicast message.
1864 * Freed no longer needed (last message).
1867 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1869 if (0 == --(mesh_data->reference_counter))
1871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1872 GNUNET_free (mesh_data->data);
1873 GNUNET_free (mesh_data);
1879 * Check if client has registered with the service and has not disconnected
1881 * @param client the client to check
1883 * @return non-NULL if client exists in the global DLL
1885 static struct MeshClient *
1886 client_get (struct GNUNET_SERVER_Client *client)
1888 struct MeshClient *c;
1893 if (c->handle == client)
1902 * Checks if a given client has subscribed to certain message type
1904 * @param message_type Type of message to check
1905 * @param c Client to check
1907 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1909 * FIXME: use of crypto_hash slows it down
1910 * The hash function alone takes 8-10us out of the ~55us for the whole
1911 * process of retransmitting the message from one local client to another.
1912 * Find faster implementation!
1915 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1917 struct GNUNET_HashCode hc;
1919 if (NULL == c->types)
1922 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1923 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1928 * Check whether client wants traffic from a tunnel.
1930 * @param c Client to check.
1931 * @param t Tunnel to be found.
1933 * @return GNUNET_YES if client knows tunnel.
1935 * TODO look in client hashmap
1938 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1942 for (i = 0; i < t->nclients; i++)
1943 if (t->clients[i] == c)
1950 * Check whether client has been informed about a tunnel.
1952 * @param c Client to check.
1953 * @param t Tunnel to be found.
1955 * @return GNUNET_YES if client knows tunnel.
1957 * TODO look in client hashmap
1960 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1964 for (i = 0; i < t->nignore; i++)
1965 if (t->ignore[i] == c)
1967 return client_wants_tunnel(c, t);
1972 * Marks a client as uninterested in traffic from the tunnel, updating both
1973 * client and tunnel to reflect this.
1975 * @param c Client that doesn't want traffic anymore.
1976 * @param t Tunnel which should be ignored.
1978 * FIXME when to delete an incoming tunnel?
1981 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1983 struct GNUNET_HashCode hash;
1985 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1986 GNUNET_break (GNUNET_YES ==
1987 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1989 GNUNET_break (GNUNET_YES ==
1990 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1991 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1992 tunnel_delete_active_client (t, c);
1993 GNUNET_array_append (t->ignore, t->nignore, c);
1998 * Deletes a tunnel from a client (either owner or destination). To be used on
1999 * tunnel destroy, otherwise, use client_ignore_tunnel.
2001 * @param c Client whose tunnel to delete.
2002 * @param t Tunnel which should be deleted.
2005 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2007 struct GNUNET_HashCode hash;
2011 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2012 GNUNET_assert (GNUNET_YES ==
2013 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2019 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2021 GNUNET_assert (GNUNET_YES ==
2022 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2026 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2034 * Send the message to all clients that have subscribed to its type
2036 * @param msg Pointer to the message itself
2037 * @param payload Pointer to the payload of the message.
2038 * @param t The tunnel to whose clients this message goes.
2040 * @return number of clients this message was sent to
2043 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2044 const struct GNUNET_MessageHeader *payload,
2045 struct MeshTunnel *t)
2047 struct MeshClient *c;
2048 MESH_TunnelNumber *tid;
2051 char cbuf[htons (msg->size)];
2053 type = ntohs (payload->type);
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2056 GNUNET_MESH_DEBUG_M2S (type));
2058 memcpy (cbuf, msg, sizeof (cbuf));
2059 switch (htons (msg->type))
2061 struct GNUNET_MESH_Unicast *uc;
2062 struct GNUNET_MESH_Multicast *mc;
2063 struct GNUNET_MESH_ToOrigin *to;
2065 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2066 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2069 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2070 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2073 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2074 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2082 for (count = 0, c = clients; c != NULL; c = c->next)
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2085 if (client_is_subscribed (type, c))
2087 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2091 *tid = htonl (t->local_tid);
2095 if (GNUNET_NO == client_knows_tunnel (c, t))
2097 /* This client doesn't know the tunnel */
2098 struct GNUNET_MESH_TunnelNotification tmsg;
2099 struct GNUNET_HashCode hash;
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2102 tmsg.header.size = htons (sizeof (tmsg));
2103 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2104 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2105 tmsg.tunnel_id = htonl (t->local_tid_dest);
2107 if (GNUNET_YES == t->speed_min)
2108 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2109 if (GNUNET_YES == t->nobuffer)
2110 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2111 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2112 &tmsg.header, GNUNET_NO);
2113 tunnel_add_client (t, c);
2114 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2116 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2117 c->incoming_tunnels, &hash, t,
2118 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2120 *tid = htonl (t->local_tid_dest);
2123 /* Check if the client wants to get traffic from the tunnel */
2124 if (GNUNET_NO == client_wants_tunnel(c, t))
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2128 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2129 (struct GNUNET_MessageHeader
2130 *) cbuf, GNUNET_NO);
2139 * Notify the client that owns the tunnel that a peer has connected to it
2140 * (the requested path to it has been confirmed).
2142 * @param t Tunnel whose owner to notify
2143 * @param id Short id of the peer that has connected
2146 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2148 struct GNUNET_MESH_PeerControl pc;
2150 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2151 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2152 pc.tunnel_id = htonl (t->local_tid);
2153 GNUNET_PEER_resolve (id, &pc.peer);
2154 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2160 * Notify all clients (not depending on registration status) that the incoming
2161 * tunnel is no longer valid.
2163 * @param t Tunnel that was destroyed.
2166 send_clients_tunnel_destroy (struct MeshTunnel *t)
2168 struct GNUNET_MESH_TunnelMessage msg;
2170 msg.header.size = htons (sizeof (msg));
2171 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2172 msg.tunnel_id = htonl (t->local_tid_dest);
2173 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2178 * Notify clients of tunnel disconnections, if needed.
2179 * In case the origin disconnects, the destination clients get a tunnel destroy
2180 * notification. If the last destination disconnects (only one remaining client
2181 * in tunnel), the origin gets a (local ID) peer disconnected.
2182 * Note that the function must be called BEFORE removing the client from
2185 * @param t Tunnel that was destroyed.
2186 * @param c Client that disconnected.
2189 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2195 struct GNUNET_MESH_TunnelMessage msg;
2197 msg.header.size = htons (sizeof (msg));
2198 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2199 msg.tunnel_id = htonl (t->local_tid_dest);
2200 for (i = 0; i < t->nclients; i++)
2201 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2202 &msg.header, GNUNET_NO);
2204 // FIXME when to disconnect an incoming tunnel?
2205 else if (1 == t->nclients && NULL != t->owner)
2207 struct GNUNET_MESH_PeerControl msg;
2209 msg.header.size = htons (sizeof (msg));
2210 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2211 msg.tunnel_id = htonl (t->local_tid);
2212 msg.peer = my_full_id;
2213 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2214 &msg.header, GNUNET_NO);
2220 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2221 * and insert it in the appropiate structures if the peer is not known yet.
2223 * @param peer Full identity of the peer.
2225 * @return Existing or newly created peer info.
2227 static struct MeshPeerInfo *
2228 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2230 struct MeshPeerInfo *peer_info;
2232 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2233 if (NULL == peer_info)
2236 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2237 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2238 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2239 peer_info->id = GNUNET_PEER_intern (peer);
2247 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2248 * and insert it in the appropiate structures if the peer is not known yet.
2250 * @param peer Short identity of the peer.
2252 * @return Existing or newly created peer info.
2254 static struct MeshPeerInfo *
2255 peer_info_get_short (const GNUNET_PEER_Id peer)
2257 struct GNUNET_PeerIdentity id;
2259 GNUNET_PEER_resolve (peer, &id);
2260 return peer_info_get (&id);
2265 * Iterator to remove the tunnel from the list of tunnels a peer participates
2268 * @param cls Closure (tunnel info)
2269 * @param key GNUNET_PeerIdentity of the peer (unused)
2270 * @param value PeerInfo of the peer
2272 * @return always GNUNET_YES, to keep iterating
2275 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2277 struct MeshTunnel *t = cls;
2278 struct MeshPeerInfo *peer = value;
2281 for (i = 0; i < peer->ntunnels; i++)
2284 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2287 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2288 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2297 * Core callback to write a pre-constructed data packet to core buffer
2299 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2300 * @param size Number of bytes available in buf.
2301 * @param buf Where the to write the message.
2303 * @return number of bytes written to buf
2306 send_core_data_raw (void *cls, size_t size, void *buf)
2308 struct MeshTransmissionDescriptor *info = cls;
2309 struct GNUNET_MessageHeader *msg;
2312 GNUNET_assert (NULL != info);
2313 GNUNET_assert (NULL != info->mesh_data);
2314 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2315 total_size = ntohs (msg->size);
2317 if (total_size > size)
2322 memcpy (buf, msg, total_size);
2323 data_descriptor_decrement_rc (info->mesh_data);
2330 * Sends an already built non-multicast message to a peer,
2331 * properly registrating all used resources.
2333 * @param message Message to send. Function makes a copy of it.
2334 * @param peer Short ID of the neighbor whom to send the message.
2335 * @param t Tunnel on which this message is transmitted.
2338 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2339 const struct GNUNET_PeerIdentity *peer,
2340 struct MeshTunnel *t)
2342 struct MeshTransmissionDescriptor *info;
2343 struct MeshPeerInfo *neighbor;
2344 struct MeshPeerPath *p;
2348 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2350 size = ntohs (message->size);
2351 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2352 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2353 info->mesh_data->data = GNUNET_malloc (size);
2354 memcpy (info->mesh_data->data, message, size);
2355 type = ntohs(message->type);
2356 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2358 struct GNUNET_MESH_Unicast *m;
2360 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2361 m->ttl = htonl (ntohl (m->ttl) - 1);
2363 info->mesh_data->data_len = size;
2364 info->mesh_data->reference_counter = 1;
2365 info->mesh_data->total_out = 1;
2366 neighbor = peer_info_get (peer);
2367 for (p = neighbor->path_head; NULL != p; p = p->next)
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 " %s IS NOT DIRECTLY CONNECTED\n",
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2383 for (p = neighbor->path_head; NULL != p; p = p->next)
2385 struct GNUNET_PeerIdentity debug_id;
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2389 " path with %u hops through:\n",
2391 for (i = 0; i < p->length; i++)
2393 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2396 i, GNUNET_i2s(&debug_id));
2400 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2401 GNUNET_free (info->mesh_data->data);
2402 GNUNET_free (info->mesh_data);
2406 info->peer = neighbor;
2407 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2418 * Sends a CREATE PATH message for a path to a peer, properly registrating
2419 * all used resources.
2421 * @param peer PeerInfo of the final peer for whom this path is being created.
2422 * @param p Path itself.
2423 * @param t Tunnel for which the path is created.
2426 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2427 struct MeshTunnel *t)
2429 struct GNUNET_PeerIdentity id;
2430 struct MeshPathInfo *path_info;
2431 struct MeshPeerInfo *neighbor;
2437 p = tree_get_path_to_peer (t->tree, peer->id);
2444 for (i = 0; i < p->length; i++)
2446 if (p->peers[i] == myid)
2449 if (i >= p->length - 1)
2455 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2457 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2458 path_info->path = p;
2460 neighbor = peer_info_get (&id);
2461 path_info->peer = neighbor;
2462 queue_add (path_info,
2463 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2464 sizeof (struct GNUNET_MESH_ManipulatePath) +
2465 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2472 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2474 * @param t Tunnel whose path to destroy.
2475 * @param destination Short ID of the peer to whom the path to destroy.
2478 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2480 struct MeshPeerPath *p;
2483 p = tree_get_path_to_peer (t->tree, destination);
2489 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2490 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2492 struct GNUNET_MESH_ManipulatePath *msg;
2493 struct GNUNET_PeerIdentity *pi;
2497 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2498 msg->header.size = htons (size);
2499 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2500 msg->tid = htonl (t->id.tid);
2501 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2502 for (i = 0; i < p->length; i++)
2504 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2506 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2513 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2515 * @param t Tunnel which to confirm.
2518 send_path_ack (struct MeshTunnel *t)
2520 struct MeshTransmissionDescriptor *info;
2521 struct GNUNET_PeerIdentity id;
2522 GNUNET_PEER_Id peer;
2524 peer = tree_get_predecessor (t->tree);
2525 GNUNET_PEER_resolve (peer, &id);
2526 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2527 info->origin = &t->id;
2528 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2529 GNUNET_assert (NULL != info->peer);
2532 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2533 sizeof (struct GNUNET_MESH_PathACK),
2540 * Try to establish a new connection to this peer.
2541 * Use the best path for the given tunnel.
2542 * If the peer doesn't have any path to it yet, try to get one.
2543 * If the peer already has some path, send a CREATE PATH towards it.
2545 * @param peer PeerInfo of the peer.
2546 * @param t Tunnel for which to create the path, if possible.
2549 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2551 struct MeshPeerPath *p;
2552 struct MeshPathInfo *path_info;
2554 if (NULL != peer->path_head)
2556 p = tree_get_path_to_peer (t->tree, peer->id);
2563 // FIXME always send create path to self
2566 send_create_path (peer, p, t);
2570 struct GNUNET_HashCode hash;
2573 send_client_peer_connected (t, myid);
2574 t->local_tid_dest = next_local_tid++;
2575 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2578 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2579 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2586 else if (NULL == peer->dhtget)
2588 struct GNUNET_PeerIdentity id;
2590 GNUNET_PEER_resolve (peer->id, &id);
2591 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2592 path_info->peer = peer;
2594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2595 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2596 peer->dhtgetcls = path_info;
2597 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2598 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2599 &id.hashPubKey, /* key to search */
2600 dht_replication_level, /* replication level */
2601 GNUNET_DHT_RO_RECORD_ROUTE |
2602 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2603 NULL, /* xquery */ // FIXME BLOOMFILTER
2604 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2605 &dht_get_id_handler, path_info);
2607 /* Otherwise, there is no path but the DHT get is already started. */
2612 * Task to delay the connection of a peer
2614 * @param cls Closure (path info with tunnel and peer to connect).
2615 * Will be free'd on exection.
2616 * @param tc TaskContext
2619 peer_info_connect_task (void *cls,
2620 const struct GNUNET_SCHEDULER_TaskContext *tc)
2622 struct MeshPathInfo *path_info = cls;
2624 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2626 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2631 peer_info_connect (path_info->peer, path_info->t);
2637 * Destroy the peer_info and free any allocated resources linked to it
2639 * @param pi The peer_info to destroy.
2641 * @return GNUNET_OK on success
2644 peer_info_destroy (struct MeshPeerInfo *pi)
2646 struct GNUNET_PeerIdentity id;
2647 struct MeshPeerPath *p;
2648 struct MeshPeerPath *nextp;
2650 GNUNET_PEER_resolve (pi->id, &id);
2651 GNUNET_PEER_change_rc (pi->id, -1);
2654 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2658 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2660 if (NULL != pi->dhtget)
2662 GNUNET_DHT_get_stop (pi->dhtget);
2663 GNUNET_free (pi->dhtgetcls);
2669 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2673 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2675 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2683 * Remove all paths that rely on a direct connection between p1 and p2
2684 * from the peer itself and notify all tunnels about it.
2686 * @param peer PeerInfo of affected peer.
2687 * @param p1 GNUNET_PEER_Id of one peer.
2688 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2691 * TODO: optimize (see below)
2694 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2697 struct MeshPeerPath *p;
2698 struct MeshPeerPath *aux;
2699 struct MeshPeerInfo *peer_d;
2701 unsigned int destroyed;
2706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2708 p = peer->path_head;
2712 for (i = 0; i < (p->length - 1); i++)
2714 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2715 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2717 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2728 for (i = 0; i < peer->ntunnels; i++)
2730 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2734 * Problem: one or more peers have been deleted from the tunnel tree.
2735 * We don't know who they are to try to add them again.
2736 * We need to try to find a new path for each of the disconnected peers.
2737 * Some of them might already have a path to reach them that does not
2738 * involve p1 and p2. Adding all anew might render in a better tree than
2739 * the trivial immediate fix.
2741 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2742 * its children will be reachable trough him.
2744 peer_d = peer_info_get_short (d);
2747 for (p = peer_d->path_head; NULL != p; p = p->next)
2749 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2757 /* No callback, as peer will be already disconnected and a connection
2758 * scheduled by tunnel_notify_connection_broken.
2760 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2764 peer_info_connect (peer_d, peer->tunnels[i]);
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2772 * Add the path to the peer and update the path used to reach it in case this
2775 * @param peer_info Destination peer to add the path to.
2776 * @param path New path to add. Last peer must be the peer in arg 1.
2777 * Path will be either used of freed if already known.
2778 * @param trusted Do we trust that this path is real?
2781 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2784 struct MeshPeerPath *aux;
2788 if ((NULL == peer_info) || (NULL == path))
2791 path_destroy (path);
2794 if (path->peers[path->length - 1] != peer_info->id)
2797 path_destroy (path);
2800 if (path->length <= 2 && GNUNET_NO == trusted)
2802 /* Only allow CORE to tell us about direct paths */
2803 path_destroy (path);
2806 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2807 for (l = 1; l < path->length; l++)
2809 if (path->peers[l] == myid)
2811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2812 for (l2 = 0; l2 < path->length - l; l2++)
2814 path->peers[l2] = path->peers[l + l2];
2819 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2824 struct GNUNET_PeerIdentity id;
2826 GNUNET_PEER_resolve (peer_info->id, &id);
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2828 path->length, GNUNET_i2s (&id));
2831 l = path_get_length (path);
2838 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2839 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2841 l2 = path_get_length (aux);
2844 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2845 peer_info->path_tail, aux, path);
2850 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2852 path_destroy (path);
2857 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2864 * Add the path to the origin peer and update the path used to reach it in case
2865 * this is the shortest.
2866 * The path is given in peer_info -> destination, therefore we turn the path
2867 * upside down first.
2869 * @param peer_info Peer to add the path to, being the origin of the path.
2870 * @param path New path to add after being inversed.
2871 * @param trusted Do we trust that this path is real?
2874 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2875 struct MeshPeerPath *path, int trusted)
2878 peer_info_add_path (peer_info, path, trusted);
2883 * Function called if the connection to the peer has been stalled for a while,
2884 * possibly due to a missed ACK. Poll the peer about its ACK status.
2886 * @param cls Closure (info about regex search).
2887 * @param tc TaskContext.
2890 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2892 struct MeshTunnelChildInfo *cinfo = cls;
2893 struct GNUNET_MESH_Poll msg;
2894 struct GNUNET_PeerIdentity id;
2895 struct MeshTunnel *t;
2897 return; // FIXME fc activate
2898 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2899 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2905 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2906 msg.header.size = htons (sizeof (msg));
2907 msg.tid = htonl (t->id.tid);
2908 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2909 msg.last_ack = htonl (cinfo->fwd_ack);
2911 GNUNET_PEER_resolve (tree_get_predecessor(cinfo->t->tree), &id);
2912 send_prebuilt_message (&msg.header, &id, cinfo->t);
2913 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2914 &tunnel_poll, cinfo);
2919 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2920 * to obtain a local peer -> destination path and interning the peer ids.
2922 * @return Newly allocated and created path
2924 static struct MeshPeerPath *
2925 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2926 unsigned int get_path_length,
2927 const struct GNUNET_PeerIdentity *put_path,
2928 unsigned int put_path_length)
2930 struct MeshPeerPath *p;
2936 GNUNET_PEER_change_rc (myid, 1);
2937 i = get_path_length;
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2939 for (i--; i >= 0; i--)
2941 id = GNUNET_PEER_intern (&get_path[i]);
2942 if (p->length > 0 && id == p->peers[p->length - 1])
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2945 GNUNET_PEER_change_rc (id, -1);
2949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2950 GNUNET_i2s (&get_path[i]));
2952 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2953 p->peers[p->length - 1] = id;
2956 i = put_path_length;
2957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2958 for (i--; i >= 0; i--)
2960 id = GNUNET_PEER_intern (&put_path[i]);
2963 /* PUT path went through us, so discard the path up until now and start
2964 * from here to get a much shorter (and loop-free) path.
2969 if (p->length > 0 && id == p->peers[p->length - 1])
2971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2972 GNUNET_PEER_change_rc (id, -1);
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2977 GNUNET_i2s (&put_path[i]));
2979 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2980 p->peers[p->length - 1] = id;
2984 if (get_path_length > 0)
2985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2986 GNUNET_i2s (&get_path[0]));
2987 if (put_path_length > 0)
2988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2989 GNUNET_i2s (&put_path[0]));
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2992 for (i = 0; i < p->length; i++)
2994 struct GNUNET_PeerIdentity peer_id;
2996 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2998 GNUNET_i2s (&peer_id));
3006 * Adds a path to the peer_infos of all the peers in the path
3008 * @param p Path to process.
3009 * @param confirmed Whether we know if the path works or not.
3012 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3016 /* TODO: invert and add */
3017 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3018 for (i++; i < p->length; i++)
3020 struct MeshPeerInfo *aux;
3021 struct MeshPeerPath *copy;
3023 aux = peer_info_get_short (p->peers[i]);
3024 copy = path_duplicate (p);
3025 copy->length = i + 1;
3026 peer_info_add_path (aux, copy, GNUNET_NO);
3032 * Send keepalive packets for a peer
3034 * @param cls Closure (tunnel for which to send the keepalive).
3035 * @param tc Notification context.
3037 * TODO: implement explicit multicast keepalive?
3040 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3044 * Search for a tunnel among the incoming tunnels
3046 * @param tid the local id of the tunnel
3048 * @return tunnel handler, NULL if doesn't exist
3050 static struct MeshTunnel *
3051 tunnel_get_incoming (MESH_TunnelNumber tid)
3053 struct GNUNET_HashCode hash;
3055 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3056 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3057 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3062 * Search for a tunnel among the tunnels for a client
3064 * @param c the client whose tunnels to search in
3065 * @param tid the local id of the tunnel
3067 * @return tunnel handler, NULL if doesn't exist
3069 static struct MeshTunnel *
3070 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3072 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3074 return tunnel_get_incoming (tid);
3078 struct GNUNET_HashCode hash;
3080 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3081 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3087 * Search for a tunnel by global ID using PEER_ID
3089 * @param pi owner of the tunnel
3090 * @param tid global tunnel number
3092 * @return tunnel handler, NULL if doesn't exist
3094 static struct MeshTunnel *
3095 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3097 struct MESH_TunnelID id;
3098 struct GNUNET_HashCode hash;
3103 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3104 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3109 * Search for a tunnel by global ID using full PeerIdentities
3111 * @param oid owner of the tunnel
3112 * @param tid global tunnel number
3114 * @return tunnel handler, NULL if doesn't exist
3116 static struct MeshTunnel *
3117 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3119 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3124 * Delete an active client from the tunnel.
3130 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3134 for (i = 0; i < t->nclients; i++)
3136 if (t->clients[i] == c)
3138 t->clients[i] = t->clients[t->nclients - 1];
3139 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3140 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3142 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3150 * Delete an ignored client from the tunnel.
3156 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3160 for (i = 0; i < t->nignore; i++)
3162 if (t->ignore[i] == c)
3164 t->ignore[i] = t->ignore[t->nignore - 1];
3165 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3173 * Delete a client from the tunnel. It should be only done on
3174 * client disconnection, otherwise use client_ignore_tunnel.
3180 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3182 tunnel_delete_ignored_client (t, c);
3183 tunnel_delete_active_client (t, c);
3188 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3190 * Destroys queue elements of all waiting transmissions and frees all memory
3191 * used by the struct and its elements.
3193 * @param cls Closure (tunnel info).
3194 * @param key Hash of GNUNET_PEER_Id (unused).
3195 * @param value MeshTunnelChildInfo of the child.
3197 * @return always GNUNET_YES, to keep iterating
3200 tunnel_destroy_child (void *cls,
3201 const struct GNUNET_HashCode * key,
3204 struct MeshTunnelChildInfo *cinfo = value;
3205 struct MeshTunnel *t = cls;
3206 struct MeshPeerQueue *q;
3210 for (c = 0; c < cinfo->send_buffer_n; c++)
3212 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3213 q = cinfo->send_buffer[i];
3214 cinfo->send_buffer[i] = NULL;
3216 queue_destroy (q, GNUNET_YES);
3219 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3221 GNUNET_free_non_null (cinfo->send_buffer);
3222 GNUNET_free (cinfo);
3228 * Callback used to notify a client owner of a tunnel that a peer has
3229 * disconnected, most likely because of a path change.
3231 * @param cls Closure (tunnel this notification is about).
3232 * @param peer_id Short ID of disconnected peer.
3235 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3237 struct MeshTunnel *t = cls;
3238 struct MeshPeerInfo *peer;
3239 struct MeshPathInfo *path_info;
3241 if (NULL != t->owner && NULL != nc)
3243 struct GNUNET_MESH_PeerControl msg;
3245 msg.header.size = htons (sizeof (msg));
3246 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3247 msg.tunnel_id = htonl (t->local_tid);
3248 GNUNET_PEER_resolve (peer_id, &msg.peer);
3249 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3250 &msg.header, GNUNET_NO);
3252 peer = peer_info_get_short (peer_id);
3253 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3254 path_info->peer = peer;
3256 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3262 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3263 * needed rescources.
3264 * If peer already exists, reevaluate shortest path and change if different.
3266 * @param t Tunnel we want to add a new peer to
3267 * @param peer PeerInfo of the peer being added
3271 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3273 struct GNUNET_PeerIdentity id;
3274 struct MeshPeerPath *best_p;
3275 struct MeshPeerPath *p;
3276 unsigned int best_cost;
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3280 GNUNET_PEER_resolve (peer->id, &id);
3282 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3285 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3286 GNUNET_assert (GNUNET_OK ==
3287 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3289 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3292 if (NULL != (p = peer->path_head))
3295 best_cost = tree_get_path_cost (t->tree, p);
3298 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3305 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3306 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3307 t->path_refresh_task =
3308 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3312 /* Start a DHT get */
3313 peer_info_connect (peer, t);
3315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3319 * Add a path to a tunnel which we don't own, just to remember the next hop.
3320 * If destination node was already in the tunnel, the first hop information
3321 * will be replaced with the new path.
3323 * @param t Tunnel we want to add a new peer to
3324 * @param p Path to add
3325 * @param own_pos Position of local node in path.
3329 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3330 unsigned int own_pos)
3332 struct GNUNET_PeerIdentity id;
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3335 GNUNET_assert (0 != own_pos);
3336 tree_add_path (t->tree, p, NULL, NULL);
3337 if (own_pos < p->length - 1)
3339 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3340 tree_update_first_hops (t->tree, myid, &id);
3342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3346 * Add a client to a tunnel, initializing all needed data structures.
3348 * @param t Tunnel to which add the client.
3349 * @param c Client which to add to the tunnel.
3352 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3354 struct MeshTunnelClientInfo clinfo;
3356 GNUNET_array_append (t->clients, t->nclients, c);
3357 clinfo.fwd_ack = t->fwd_pid + 1;
3358 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3359 clinfo.fwd_pid = t->fwd_pid;
3360 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3362 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3367 * Notifies a tunnel that a connection has broken that affects at least
3368 * some of its peers. Sends a notification towards the root of the tree.
3369 * In case the peer is the owner of the tree, notifies the client that owns
3370 * the tunnel and tries to reconnect.
3372 * @param t Tunnel affected.
3373 * @param p1 Peer that got disconnected from p2.
3374 * @param p2 Peer that got disconnected from p1.
3376 * @return Short ID of the peer disconnected (either p1 or p2).
3377 * 0 if the tunnel remained unaffected.
3379 static GNUNET_PEER_Id
3380 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3386 tree_notify_connection_broken (t->tree, p1, p2,
3387 &tunnel_notify_client_peer_disconnected,
3389 if (myid != p1 && myid != p2)
3395 if (tree_get_predecessor (t->tree) != 0)
3397 /* We are the peer still connected, notify owner of the disconnection. */
3398 struct GNUNET_MESH_PathBroken msg;
3399 struct GNUNET_PeerIdentity neighbor;
3401 msg.header.size = htons (sizeof (msg));
3402 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3403 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3404 msg.tid = htonl (t->id.tid);
3405 msg.peer1 = my_full_id;
3406 GNUNET_PEER_resolve (pid, &msg.peer2);
3407 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3408 send_prebuilt_message (&msg.header, &neighbor, t);
3416 * Send a multicast packet to a neighbor.
3418 * @param cls Closure (Info about the multicast packet)
3419 * @param neighbor_id Short ID of the neighbor to send the packet to.
3422 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3424 struct MeshData *mdata = cls;
3425 struct MeshTransmissionDescriptor *info;
3426 struct GNUNET_PeerIdentity neighbor;
3427 struct GNUNET_MessageHeader *msg;
3429 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3431 info->mesh_data = mdata;
3432 (mdata->reference_counter) ++;
3433 info->destination = neighbor_id;
3434 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3436 GNUNET_i2s (&neighbor));
3437 info->peer = peer_info_get (&neighbor);
3438 GNUNET_assert (NULL != info->peer);
3439 msg = (struct GNUNET_MessageHeader *) mdata->data;
3442 info->mesh_data->data_len,
3449 * Queue a message in a tunnel in multicast, sending a copy to each child node
3450 * down the local one in the tunnel tree.
3452 * @param t Tunnel in which to send the data.
3453 * @param msg Message to be sent.
3456 tunnel_send_multicast (struct MeshTunnel *t,
3457 const struct GNUNET_MessageHeader *msg)
3459 struct MeshData *mdata;
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3462 " sending a multicast packet...\n");
3464 mdata = GNUNET_malloc (sizeof (struct MeshData));
3465 mdata->data_len = ntohs (msg->size);
3467 mdata->data = GNUNET_malloc (mdata->data_len);
3468 memcpy (mdata->data, msg, mdata->data_len);
3469 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3471 struct GNUNET_MESH_Multicast *mcast;
3473 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3474 if (t->fwd_queue_n >= t->fwd_queue_max)
3477 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3479 " message from %s!\n",
3480 GNUNET_i2s(&mcast->oid));
3481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3482 " message at %s!\n",
3483 GNUNET_i2s(&my_full_id));
3484 GNUNET_free (mdata->data);
3485 GNUNET_free (mdata);
3489 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3491 ntohl (mcast->ttl));
3495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3498 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3499 if (mdata->reference_counter == 0)
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3502 " no one to send data to\n");
3503 GNUNET_free (mdata->data);
3504 GNUNET_free (mdata);
3509 mdata->total_out = mdata->reference_counter;
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3512 " sending a multicast packet done\n");
3518 * Increase the SKIP value of all peers that
3519 * have not received a unicast message.
3521 * @param cls Closure (ID of the peer that HAS received the message).
3522 * @param key ID of the neighbor.
3523 * @param value Information about the neighbor.
3525 * @return GNUNET_YES to keep iterating.
3528 tunnel_add_skip (void *cls,
3529 const struct GNUNET_HashCode * key,
3532 struct GNUNET_PeerIdentity *neighbor = cls;
3533 struct MeshTunnelChildInfo *cinfo = value;
3535 /* TODO compare only pointers? key == neighbor? */
3536 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3546 * @brief Get neighbor's Flow Control information.
3548 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3549 * descendant of the local node in a certain tunnel.
3550 * If the info is not yet there (recently created path), creates the data struct
3551 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3554 * @param t Tunnel related.
3555 * @param peer Neighbor whose Flow Control info is needed.
3557 * @return Neighbor's Flow Control info.
3559 static struct MeshTunnelChildInfo *
3560 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3561 const struct GNUNET_PeerIdentity *peer)
3563 struct MeshTunnelChildInfo *cinfo;
3565 if (NULL == t->children_fc)
3568 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3574 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3575 cinfo->id = GNUNET_PEER_intern (peer);
3576 cinfo->skip = t->fwd_pid;
3579 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3580 cinfo->fwd_ack = t->fwd_pid + delta;
3581 cinfo->bck_ack = delta;
3583 cinfo->send_buffer =
3584 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3586 GNUNET_assert (GNUNET_OK ==
3587 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3590 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3597 * Get the Flow Control info of a client.
3599 * @param t Tunnel on which to look.
3600 * @param c Client whose ACK to get.
3602 * @return ACK value.
3604 static struct MeshTunnelClientInfo *
3605 tunnel_get_client_fc (struct MeshTunnel *t,
3606 struct MeshClient *c)
3610 for (i = 0; i < t->nclients; i++)
3612 if (t->clients[i] != c)
3614 return &t->clients_fc[i];
3617 return NULL; // avoid compiler / coverity complaints
3622 * Iterator to get the appropiate ACK value from all children nodes.
3624 * @param cls Closue (tunnel).
3625 * @param id Id of the child node.
3628 tunnel_get_child_fwd_ack (void *cls,
3631 struct GNUNET_PeerIdentity peer_id;
3632 struct MeshTunnelChildInfo *cinfo;
3633 struct MeshTunnelChildIteratorContext *ctx = cls;
3634 struct MeshTunnel *t = ctx->t;
3637 GNUNET_PEER_resolve (id, &peer_id);
3638 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3639 ack = cinfo->fwd_ack;
3642 if (GNUNET_NO == ctx->init)
3644 ctx->max_child_ack = ack;
3645 ctx->init = GNUNET_YES;
3648 if (GNUNET_YES == t->speed_min)
3650 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3654 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3661 * Get the maximum PID allowed to transmit to any
3662 * tunnel child of the local peer, depending on the tunnel
3663 * buffering/speed settings.
3667 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3670 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3672 struct MeshTunnelChildIteratorContext ctx;
3674 ctx.max_child_ack = 0;
3676 ctx.init = GNUNET_NO;
3677 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3679 if (0 == ctx.nchildren)
3681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3682 " tunnel has no children, no FWD ACK\n");
3686 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3687 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3689 return (int64_t) ctx.max_child_ack;
3694 * Set the FWD ACK value of a client in a particular tunnel.
3696 * @param t Tunnel affected.
3697 * @param c Client whose ACK to set.
3698 * @param ack ACK value.
3701 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3702 struct MeshClient *c,
3707 for (i = 0; i < t->nclients; i++)
3709 if (t->clients[i] != c)
3711 t->clients_fc[i].fwd_ack = ack;
3719 * Get the highest ACK value of all clients in a particular tunnel,
3720 * according to the buffering/speed settings.
3722 * @param t Tunnel on which to look.
3724 * @return Corresponding ACK value (max uint32_t).
3725 * If no clients are suscribed, -1LL.
3728 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3733 if (0 == t->nclients)
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3736 " tunnel has no clients, no FWD ACK\n");
3740 for (ack = -1LL, i = 0; i < t->nclients; i++)
3743 (GNUNET_YES == t->speed_min &&
3744 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3745 (GNUNET_NO == t->speed_min &&
3746 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3748 ack = t->clients_fc[i].fwd_ack;
3752 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3753 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3755 return (uint32_t) ack;
3760 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3761 * mode and the status of all children nodes.
3765 * @return Maximum PID allowed.
3768 tunnel_get_fwd_ack (struct MeshTunnel *t)
3772 uint32_t buffer_free;
3776 count = t->fwd_pid - t->skip;
3777 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3778 child_ack = tunnel_get_children_fwd_ack (t);
3779 client_ack = tunnel_get_clients_fwd_ack (t);
3780 if (GNUNET_YES == t->nobuffer)
3783 if (-1LL == child_ack)
3784 child_ack = client_ack;
3785 if (-1LL == child_ack)
3788 client_ack = child_ack = ack;
3793 ack = count + buffer_free; // Overflow? OK!
3795 if (-1LL == child_ack)
3797 // Node has no children, child_ack AND core buffer are irrelevant.
3798 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3799 return (uint32_t) client_ack;
3801 if (-1LL == client_ack)
3805 if (GNUNET_YES == t->speed_min)
3807 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3808 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3812 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3813 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3816 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3817 count, buffer_free, child_ack, client_ack, ack);
3823 * Build a local ACK message and send it to a local client.
3825 * @param t Tunnel on which to send the ACK.
3826 * @param c Client to whom send the ACK.
3827 * @param ack Value of the ACK.
3830 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3832 struct GNUNET_MESH_LocalAck msg;
3834 msg.header.size = htons (sizeof (msg));
3835 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3836 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3837 msg.max_pid = htonl (ack);
3838 GNUNET_SERVER_notification_context_unicast(nc,
3845 * Build an ACK message and queue it to send to the given peer.
3847 * @param t Tunnel on which to send the ACK.
3848 * @param peer Peer to whom send the ACK.
3849 * @param ack Value of the ACK.
3852 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3854 struct GNUNET_MESH_ACK msg;
3856 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3857 msg.header.size = htons (sizeof (msg));
3858 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3859 msg.pid = htonl (ack);
3860 msg.tid = htonl (t->id.tid);
3862 send_prebuilt_message (&msg.header, peer, t);
3867 * Notify a the owner of a tunnel about how many more
3868 * payload packages will we accept on a given tunnel.
3870 * @param t Tunnel on which to send the ACK.
3873 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3878 "Sending client FWD ACK on tunnel %X\n",
3881 ack = tunnel_get_fwd_ack (t);
3883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3884 if (t->last_fwd_ack == ack)
3886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3891 t->last_fwd_ack = ack;
3892 send_local_ack (t, t->owner, ack);
3897 * Send an ACK informing the predecessor about the available buffer space.
3898 * In case there is no predecessor, inform the owning client.
3899 * If buffering is off, send only on behalf of children or self if endpoint.
3900 * If buffering is on, send when sent to children and buffer space is free.
3901 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3902 * the ACK itself goes "back" (towards root).
3904 * @param t Tunnel on which to send the ACK.
3905 * @param type Type of message that triggered the ACK transmission.
3908 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3910 struct GNUNET_PeerIdentity id;
3913 if (NULL != t->owner)
3915 tunnel_send_client_fwd_ack (t);
3918 /* Is it after unicast / multicast retransmission? */
3921 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3922 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3924 "ACK due to FWD DATA retransmission\n");
3925 if (GNUNET_YES == t->nobuffer)
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3931 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3932 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3938 /* Check if we need no retransmit the ACK */
3939 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3940 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3944 " t->qmax: %u, t->qn: %u\n",
3945 t->fwd_queue_max, t->fwd_queue_n);
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3947 " t->pid: %u, t->ack: %u\n",
3948 t->fwd_pid, t->last_fwd_ack);
3952 /* Ok, ACK might be necessary, what PID to ACK? */
3953 ack = tunnel_get_fwd_ack (t);
3955 /* If speed_min and not all children have ack'd, dont send yet */
3956 if (ack == t->last_fwd_ack)
3958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3962 t->last_fwd_ack = ack;
3963 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3964 send_ack (t, &id, ack);
3970 * Iterator to send a child node a BCK ACK to allow him to send more
3973 * @param cls Closure (tunnel).
3974 * @param id Id of the child node.
3977 tunnel_send_child_bck_ack (void *cls,
3980 struct MeshTunnel *t = cls;
3981 struct MeshTunnelChildInfo *cinfo;
3982 struct GNUNET_PeerIdentity peer;
3984 GNUNET_PEER_resolve (id, &peer);
3985 cinfo = tunnel_get_neighbor_fc (t, &peer);
3987 if (cinfo->bck_ack != cinfo->pid &&
3988 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3991 cinfo->bck_ack++; // FIXME window size?
3992 send_ack (t, &peer, cinfo->bck_ack);
3997 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3999 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4001 * @param t Tunnel on which to send the BCK ACKs.
4004 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4007 unsigned int tunnel_delta;
4009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4011 tunnel_delta = t->bck_ack - t->bck_pid;
4012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4014 /* Find client whom to allow to send to origin (with lowest buffer space) */
4015 for (i = 0; i < t->nclients; i++)
4017 struct MeshTunnelClientInfo *clinfo;
4020 clinfo = &t->clients_fc[i];
4021 delta = clinfo->bck_ack - clinfo->bck_pid;
4022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4023 t->clients[i]->id, delta);
4025 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4026 (GNUNET_YES == t->nobuffer && 0 == delta))
4030 ack = clinfo->bck_pid;
4031 ack += t->nobuffer ? 1 : tunnel_delta;
4032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4033 " sending ack to client %u: %u\n",
4034 t->clients[i]->id, ack);
4035 send_local_ack (t, t->clients[i], ack);
4036 clinfo->bck_ack = ack;
4040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4041 " not sending ack to client %u (td %u, d %u)\n",
4042 t->clients[i]->id, tunnel_delta, delta);
4049 * Send an ACK informing the children nodes and destination clients about
4050 * the available buffer space.
4051 * If buffering is off, send only on behalf of root (can be self).
4052 * If buffering is on, send when sent to predecessor and buffer space is free.
4053 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4054 * the ACK itself goes "forward" (towards children/clients).
4056 * @param t Tunnel on which to send the ACK.
4057 * @param type Type of message that triggered the ACK transmission.
4060 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4063 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4064 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4065 /* Is it after data to_origin retransmission? */
4068 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4069 if (GNUNET_YES == t->nobuffer)
4071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4072 " Not sending ACK, nobuffer\n");
4076 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4077 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4078 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4084 tunnel_send_clients_bck_ack (t);
4085 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4090 * @brief Re-initiate traffic to this peer if necessary.
4092 * Check if there is traffic queued towards this peer
4093 * and the core transmit handle is NULL (traffic was stalled).
4094 * If so, call core tmt rdy.
4096 * @param cls Closure (unused)
4097 * @param peer_id Short ID of peer to which initiate traffic.
4100 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4102 struct MeshPeerInfo *peer;
4103 struct GNUNET_PeerIdentity id;
4104 struct MeshPeerQueue *q;
4107 peer = peer_info_get_short(peer_id);
4108 if (NULL != peer->core_transmit)
4111 q = queue_get_next(peer);
4114 /* Might br multicast traffic already sent to this particular peer but
4115 * not to other children in this tunnel.
4116 * This way t->queue_n would be > 0 but the queue of this particular peer
4122 GNUNET_PEER_resolve (peer->id, &id);
4123 peer->core_transmit =
4124 GNUNET_CORE_notify_transmit_ready(core_handle,
4127 GNUNET_TIME_UNIT_FOREVER_REL,
4137 * @brief Allow transmission of FWD traffic on this tunnel
4139 * Check if there is traffic queued towards any children
4140 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4142 * @param t Tunnel on which to unlock FWD traffic.
4145 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4147 if (0 == t->fwd_queue_n)
4150 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4155 * @brief Allow transmission of BCK traffic on this tunnel
4157 * Check if there is traffic queued towards the root of the tree
4158 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4160 * @param t Tunnel on which to unlock BCK traffic.
4163 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4165 if (0 == t->bck_queue_n)
4168 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4173 * Send a message to all peers in this tunnel that the tunnel is no longer
4176 * @param t The tunnel whose peers to notify.
4179 tunnel_send_destroy (struct MeshTunnel *t)
4181 struct GNUNET_MESH_TunnelDestroy msg;
4183 msg.header.size = htons (sizeof (msg));
4184 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4185 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4186 msg.tid = htonl (t->id.tid);
4187 tunnel_send_multicast (t, &msg.header);
4192 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4194 * @param cls Closure (Tunnel which to cancel).
4195 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4198 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4200 struct MeshTunnel *t = cls;
4201 struct MeshPeerInfo *peer_info;
4202 struct MeshPeerQueue *pq;
4203 struct MeshPeerQueue *next;
4205 peer_info = peer_info_get_short (neighbor_id);
4206 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4209 if (pq->tunnel == t)
4211 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4212 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4213 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4215 // Should have been removed on destroy children
4218 queue_destroy (pq, GNUNET_YES);
4221 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4223 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4224 peer_info->core_transmit = NULL;
4229 * Destroy the tunnel and free any allocated resources linked to it.
4231 * @param t the tunnel to destroy
4233 * @return GNUNET_OK on success
4236 tunnel_destroy (struct MeshTunnel *t)
4238 struct MeshClient *c;
4239 struct GNUNET_HashCode hash;
4250 struct GNUNET_PeerIdentity id;
4252 GNUNET_PEER_resolve (t->id.oid, &id);
4253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4254 GNUNET_i2s (&id), t->id.tid);
4256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4260 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4261 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4269 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4271 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4278 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4279 for (i = 0; i < t->nclients; i++)
4283 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4289 for (i = 0; i < t->nignore; i++)
4293 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4300 if (t->nclients > 0)
4303 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4308 GNUNET_free (t->clients);
4309 GNUNET_free (t->clients_fc);
4312 if (NULL != t->peers)
4314 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4316 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4319 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4320 &tunnel_destroy_child,
4322 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4323 t->children_fc = NULL;
4325 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4326 tree_destroy (t->tree);
4328 if (NULL != t->regex_ctx)
4329 regex_cancel_search (t->regex_ctx);
4330 if (NULL != t->dht_get_type)
4331 GNUNET_DHT_get_stop (t->dht_get_type);
4332 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4333 GNUNET_SCHEDULER_cancel (t->timeout_task);
4334 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4335 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4338 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4345 * Create a new tunnel
4347 * @param owner Who is the owner of the tunnel (short ID).
4348 * @param tid Tunnel Number of the tunnel.
4349 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4350 * @param local Tunnel Number for the tunnel, for the client point of view.
4352 * @return A new initialized tunnel. NULL on error.
4354 static struct MeshTunnel *
4355 tunnel_new (GNUNET_PEER_Id owner,
4356 MESH_TunnelNumber tid,
4357 struct MeshClient *client,
4358 MESH_TunnelNumber local)
4360 struct MeshTunnel *t;
4361 struct GNUNET_HashCode hash;
4363 if (n_tunnels >= max_tunnels && NULL == client)
4366 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4369 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4370 t->bck_queue_max = t->fwd_queue_max;
4371 t->tree = tree_new (owner);
4373 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4374 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4375 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4376 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4377 t->local_tid = local;
4378 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4380 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4382 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4384 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4385 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4392 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4399 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4401 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4402 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4406 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4416 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4417 * that are no longer needed, as well as nodes of no longer reachable peers.
4418 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4420 * @param t Tunnel from which to remove the path.
4421 * @param peer Short id of the peer which should be removed.
4424 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4426 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4432 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4433 * client when the client disconnects. If the client is not the owner, the
4434 * owner will get notified if no more clients are in the tunnel and the client
4435 * get removed from the tunnel's list.
4437 * @param cls closure (client that is disconnecting)
4438 * @param key the hash of the local tunnel id (used to access the hashmap)
4439 * @param value the value stored at the key (tunnel to destroy)
4441 * @return GNUNET_OK, keep iterating.
4444 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4446 struct MeshTunnel *t = value;
4447 struct MeshClient *c = cls;
4449 send_client_tunnel_disconnect(t, c);
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4453 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4454 tunnel_delete_client(t, c);
4455 client_delete_tunnel(c, t);
4458 tunnel_send_destroy(t);
4460 t->destroy = GNUNET_YES;
4467 * Timeout function, destroys tunnel if called
4469 * @param cls Closure (tunnel to destroy).
4470 * @param tc TaskContext
4473 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4475 struct MeshTunnel *t = cls;
4476 struct GNUNET_PeerIdentity id;
4478 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4479 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4481 GNUNET_PEER_resolve(t->id.oid, &id);
4482 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4483 "Tunnel %s [%X] timed out. Destroying.\n",
4484 GNUNET_i2s(&id), t->id.tid);
4485 send_clients_tunnel_destroy (t);
4490 * Resets the tunnel timeout. Starts it if no timeout was running.
4492 * @param t Tunnel whose timeout to reset.
4494 * TODO use heap to improve efficiency of scheduler.
4497 tunnel_reset_timeout (struct MeshTunnel *t)
4499 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4500 GNUNET_SCHEDULER_cancel (t->timeout_task);
4502 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4503 (refresh_path_time, 4), &tunnel_timeout, t);
4507 /******************************************************************************/
4508 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4509 /******************************************************************************/
4512 * Function to send a create path packet to a peer.
4514 * @param cls closure
4515 * @param size number of bytes available in buf
4516 * @param buf where the callee should write the message
4517 * @return number of bytes written to buf
4520 send_core_path_create (void *cls, size_t size, void *buf)
4522 struct MeshPathInfo *info = cls;
4523 struct GNUNET_MESH_ManipulatePath *msg;
4524 struct GNUNET_PeerIdentity *peer_ptr;
4525 struct MeshTunnel *t = info->t;
4526 struct MeshPeerPath *p = info->path;
4531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4533 sizeof (struct GNUNET_MESH_ManipulatePath) +
4534 p->length * sizeof (struct GNUNET_PeerIdentity);
4536 if (size < size_needed || NULL == buf)
4541 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4542 msg->header.size = htons (size_needed);
4543 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4544 msg->tid = ntohl (t->id.tid);
4547 if (GNUNET_YES == t->speed_min)
4548 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4549 if (GNUNET_YES == t->nobuffer)
4550 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4551 msg->opt = htonl(opt);
4554 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4555 for (i = 0; i < p->length; i++)
4557 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4564 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4570 * Fill the core buffer
4572 * @param cls closure (data itself)
4573 * @param size number of bytes available in buf
4574 * @param buf where the callee should write the message
4576 * @return number of bytes written to buf
4579 send_core_data_multicast (void *cls, size_t size, void *buf)
4581 struct MeshTransmissionDescriptor *info = cls;
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4585 GNUNET_assert (NULL != info);
4586 GNUNET_assert (NULL != info->peer);
4587 total_size = info->mesh_data->data_len;
4588 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4590 if (total_size > size)
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4596 memcpy (buf, info->mesh_data->data, total_size);
4599 struct GNUNET_MESH_Multicast *mc;
4600 struct GNUNET_MessageHeader *mh;
4603 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4605 mc = (struct GNUNET_MESH_Multicast *) mh;
4606 mh = (struct GNUNET_MessageHeader *) &mc[1];
4607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4608 " multicast, payload type %s\n",
4609 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4611 " multicast, payload size %u\n", ntohs (mh->size));
4615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4616 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4620 data_descriptor_decrement_rc (info->mesh_data);
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4629 * Creates a path ack message in buf and frees all unused resources.
4631 * @param cls closure (MeshTransmissionDescriptor)
4632 * @param size number of bytes available in buf
4633 * @param buf where the callee should write the message
4634 * @return number of bytes written to buf
4637 send_core_path_ack (void *cls, size_t size, void *buf)
4639 struct MeshTransmissionDescriptor *info = cls;
4640 struct GNUNET_MESH_PathACK *msg = buf;
4642 GNUNET_assert (NULL != info);
4643 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4648 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4649 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4650 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4651 msg->tid = htonl (info->origin->tid);
4652 msg->peer_id = my_full_id;
4655 /* TODO add signature */
4657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4658 return sizeof (struct GNUNET_MESH_PathACK);
4663 * Free a transmission that was already queued with all resources
4664 * associated to the request.
4666 * @param queue Queue handler to cancel.
4667 * @param clear_cls Is it necessary to free associated cls?
4670 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4672 struct MeshTransmissionDescriptor *dd;
4673 struct MeshPathInfo *path_info;
4674 struct MeshTunnelChildInfo *cinfo;
4675 struct GNUNET_PeerIdentity id;
4679 if (GNUNET_YES == clear_cls)
4681 switch (queue->type)
4683 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4684 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4686 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4687 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4688 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4689 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4691 " prebuilt message\n");
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4694 GNUNET_MESH_DEBUG_M2S(queue->type));
4696 data_descriptor_decrement_rc (dd->mesh_data);
4698 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4700 path_info = queue->cls;
4701 path_destroy (path_info->path);
4705 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4706 " type %s unknown!\n",
4707 GNUNET_MESH_DEBUG_M2S(queue->type));
4709 GNUNET_free_non_null (queue->cls);
4711 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4712 queue->peer->queue_tail,
4715 /* Delete from child_fc in the appropiate tunnel */
4716 max = queue->tunnel->fwd_queue_max;
4717 GNUNET_PEER_resolve (queue->peer->id, &id);
4718 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4721 for (i = 0; i < cinfo->send_buffer_n; i++)
4724 i2 = (cinfo->send_buffer_start + i) % max;
4725 if (cinfo->send_buffer[i2] == queue)
4727 /* Found corresponding entry in the send_buffer. Move all others back. */
4732 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4734 j2 = (cinfo->send_buffer_start + j) % max;
4735 j3 = (cinfo->send_buffer_start + j + 1) % max;
4736 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4739 cinfo->send_buffer[j3] = NULL;
4740 cinfo->send_buffer_n--;
4745 GNUNET_free (queue);
4750 * @brief Get the next transmittable message from the queue.
4752 * This will be the head, except in the case of being a data packet
4753 * not allowed by the destination peer.
4755 * @param peer Destination peer.
4757 * @return The next viable MeshPeerQueue element to send to that peer.
4758 * NULL when there are no transmittable messages.
4760 struct MeshPeerQueue *
4761 queue_get_next (const struct MeshPeerInfo *peer)
4763 struct MeshPeerQueue *q;
4764 struct MeshTunnel *t;
4765 struct MeshTransmissionDescriptor *info;
4766 struct MeshTunnelChildInfo *cinfo;
4767 struct GNUNET_MESH_Unicast *ucast;
4768 struct GNUNET_MESH_ToOrigin *to_orig;
4769 struct GNUNET_MESH_Multicast *mcast;
4770 struct GNUNET_PeerIdentity id;
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4775 for (q = peer->queue_head; NULL != q; q = q->next)
4779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4781 GNUNET_MESH_DEBUG_M2S(q->type));
4784 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4785 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4786 pid = ntohl (ucast->pid);
4787 GNUNET_PEER_resolve (info->peer->id, &id);
4788 cinfo = tunnel_get_neighbor_fc(t, &id);
4789 ack = cinfo->fwd_ack;
4791 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4792 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4793 pid = ntohl (to_orig->pid);
4796 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4797 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4798 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4800 // Not a multicast payload: multicast control traffic (destroy, etc)
4803 pid = ntohl (mcast->pid);
4804 GNUNET_PEER_resolve (info->peer->id, &id);
4805 cinfo = tunnel_get_neighbor_fc(t, &id);
4806 ack = cinfo->fwd_ack;
4809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4814 "********* ACK: %u, PID: %u\n",
4816 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4825 "********* NEXT!\n");
4828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4829 "********* nothing found\n");
4835 * Core callback to write a queued packet to core buffer
4837 * @param cls Closure (peer info).
4838 * @param size Number of bytes available in buf.
4839 * @param buf Where the to write the message.
4841 * @return number of bytes written to buf
4844 queue_send (void *cls, size_t size, void *buf)
4846 struct MeshPeerInfo *peer = cls;
4847 struct GNUNET_MessageHeader *msg;
4848 struct MeshPeerQueue *queue;
4849 struct MeshTunnel *t;
4850 struct MeshTunnelChildInfo *cinfo;
4851 struct GNUNET_PeerIdentity dst_id;
4854 peer->core_transmit = NULL;
4857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4858 queue = queue_get_next (peer);
4860 /* Queue has no internal mesh traffic nor sendable payload */
4863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4864 if (NULL == peer->queue_head)
4865 GNUNET_break (0); // Should've been canceled
4868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4870 GNUNET_PEER_resolve (peer->id, &dst_id);
4871 /* Check if buffer size is enough for the message */
4872 if (queue->size > size)
4874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4875 "********* not enough room, reissue\n");
4876 peer->core_transmit =
4877 GNUNET_CORE_notify_transmit_ready (core_handle,
4880 GNUNET_TIME_UNIT_FOREVER_REL,
4887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4890 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4894 "********* unicast: t->q (%u/%u)\n",
4895 t->fwd_queue_n, t->fwd_queue_max);
4897 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4904 switch (queue->type)
4907 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4908 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4909 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4910 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4912 "********* raw: %s\n",
4913 GNUNET_MESH_DEBUG_M2S (queue->type));
4915 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4916 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4917 data_size = send_core_data_raw (queue->cls, size, buf);
4918 msg = (struct GNUNET_MessageHeader *) buf;
4919 switch (ntohs (msg->type)) // Type of preconstructed message
4921 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4922 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4924 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4925 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4931 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4934 struct MeshTransmissionDescriptor *info = queue->cls;
4936 if ((1 == info->mesh_data->reference_counter
4937 && GNUNET_YES == t->speed_min)
4939 (info->mesh_data->total_out == info->mesh_data->reference_counter
4940 && GNUNET_NO == t->speed_min))
4942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4943 "********* considered sent\n");
4947 data_size = send_core_data_multicast(queue->cls, size, buf);
4948 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4950 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4952 data_size = send_core_path_create (queue->cls, size, buf);
4954 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4956 data_size = send_core_path_ack (queue->cls, size, buf);
4958 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4960 data_size = send_core_data_multicast (queue->cls, size, buf);
4964 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4965 "********* type unknown: %u\n",
4969 switch (queue->type)
4971 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4972 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4973 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4974 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4975 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4978 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4979 "at pos %u (%p) != %p\n",
4980 cinfo->send_buffer_start,
4981 cinfo->send_buffer[cinfo->send_buffer_start],
4984 if (cinfo->send_buffer_n > 0)
4986 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4987 cinfo->send_buffer_n--;
4988 cinfo->send_buffer_start++;
4989 cinfo->send_buffer_start %= t->fwd_queue_max;
5000 /* Free queue, but cls was freed by send_core_* */
5001 queue_destroy (queue, GNUNET_NO);
5003 if (GNUNET_YES == t->destroy)
5005 // FIXME fc tunnel destroy all pending traffic? wait for it?
5006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5010 /* If more data in queue, send next */
5011 queue = queue_get_next(peer);
5014 struct GNUNET_PeerIdentity id;
5016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5017 GNUNET_PEER_resolve (peer->id, &id);
5018 peer->core_transmit =
5019 GNUNET_CORE_notify_transmit_ready(core_handle,
5022 GNUNET_TIME_UNIT_FOREVER_REL,
5030 if (NULL != peer->queue_head)
5032 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5033 "********* %s stalled\n",
5034 GNUNET_i2s(&my_full_id));
5036 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5037 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5038 &tunnel_poll, cinfo);
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5047 * @brief Queue and pass message to core when possible.
5049 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5050 * and accounts for it. In case the queue is full, the message is dropped and
5053 * Otherwise, message is treated as internal and allowed to go regardless of
5056 * @param cls Closure (@c type dependant). It will be used by queue_send to
5057 * build the message to be sent if not already prebuilt.
5058 * @param type Type of the message, 0 for a raw message.
5059 * @param size Size of the message.
5060 * @param dst Neighbor to send message to.
5061 * @param t Tunnel this message belongs to.
5064 queue_add (void *cls, uint16_t type, size_t size,
5065 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5067 struct MeshPeerQueue *queue;
5068 struct MeshTunnelChildInfo *cinfo;
5069 struct GNUNET_PeerIdentity id;
5075 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5076 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5078 n = &t->fwd_queue_n;
5079 max = &t->fwd_queue_max;
5081 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5083 n = &t->bck_queue_n;
5084 max = &t->bck_queue_max;
5089 if (NULL == t->owner)
5090 GNUNET_break_op(0); // TODO: kill connection?
5093 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5095 return; // Drop message
5099 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5105 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5106 GNUNET_PEER_resolve (dst->id, &id);
5107 if (NULL == dst->core_transmit)
5109 dst->core_transmit =
5110 GNUNET_CORE_notify_transmit_ready (core_handle,
5113 GNUNET_TIME_UNIT_FOREVER_REL,
5119 if (NULL == n) // Is this internal mesh traffic?
5122 // It's payload, keep track of buffer per peer.
5123 cinfo = tunnel_get_neighbor_fc(t, &id);
5124 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5125 if (NULL != cinfo->send_buffer[i])
5127 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5128 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5129 cinfo->send_buffer_start++;
5130 cinfo->send_buffer_start %= t->fwd_queue_max;
5134 cinfo->send_buffer_n++;
5136 cinfo->send_buffer[i] = queue;
5137 if (cinfo->send_buffer_n > t->fwd_queue_max)
5140 cinfo->send_buffer_n = t->fwd_queue_max;
5145 /******************************************************************************/
5146 /******************** MESH NETWORK HANDLERS **************************/
5147 /******************************************************************************/
5151 * Core handler for path creation
5153 * @param cls closure
5154 * @param message message
5155 * @param peer peer identity this notification is about
5156 * @param atsi performance data
5157 * @param atsi_count number of records in 'atsi'
5159 * @return GNUNET_OK to keep the connection open,
5160 * GNUNET_SYSERR to close it (signal serious error)
5163 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5164 const struct GNUNET_MessageHeader *message,
5165 const struct GNUNET_ATS_Information *atsi,
5166 unsigned int atsi_count)
5168 unsigned int own_pos;
5171 MESH_TunnelNumber tid;
5172 struct GNUNET_MESH_ManipulatePath *msg;
5173 struct GNUNET_PeerIdentity *pi;
5174 struct GNUNET_HashCode hash;
5175 struct MeshPeerPath *path;
5176 struct MeshPeerInfo *dest_peer_info;
5177 struct MeshPeerInfo *orig_peer_info;
5178 struct MeshTunnel *t;
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5181 "Received a path create msg [%s]\n",
5182 GNUNET_i2s (&my_full_id));
5183 size = ntohs (message->size);
5184 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5186 GNUNET_break_op (0);
5190 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5191 if (size % sizeof (struct GNUNET_PeerIdentity))
5193 GNUNET_break_op (0);
5196 size /= sizeof (struct GNUNET_PeerIdentity);
5199 GNUNET_break_op (0);
5202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5203 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5205 tid = ntohl (msg->tid);
5206 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5208 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5209 t = tunnel_get (pi, tid);
5210 if (NULL == t) // FIXME only for INCOMING tunnels?
5214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5215 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5218 // FIXME notify failure
5221 opt = ntohl (msg->opt);
5222 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5223 GNUNET_YES : GNUNET_NO;
5224 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5226 t->nobuffer = GNUNET_YES;
5227 t->last_fwd_ack = t->fwd_pid + 1;
5229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5230 " speed_min: %d, nobuffer:%d\n",
5231 t->speed_min, t->nobuffer);
5233 if (GNUNET_YES == t->nobuffer)
5235 t->bck_queue_max = 1;
5236 t->fwd_queue_max = 1;
5239 // FIXME only assign a local tid if a local client is interested (on demand)
5240 while (NULL != tunnel_get_incoming (next_local_tid))
5241 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5242 t->local_tid_dest = next_local_tid++;
5243 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5246 tunnel_reset_timeout (t);
5247 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5249 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5258 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5259 if (NULL == dest_peer_info)
5261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5262 " Creating PeerInfo for destination.\n");
5263 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5264 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5265 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5267 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5269 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5270 if (NULL == orig_peer_info)
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5273 " Creating PeerInfo for origin.\n");
5274 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5275 orig_peer_info->id = GNUNET_PEER_intern (pi);
5276 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5277 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5280 path = path_new (size);
5282 for (i = 0; i < size; i++)
5284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5285 GNUNET_i2s (&pi[i]));
5286 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5287 if (path->peers[i] == myid)
5290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5293 /* cannot be self, must be 'not found' */
5294 /* create path: self not found in path through self */
5295 GNUNET_break_op (0);
5296 path_destroy (path);
5300 path_add_to_peers (path, GNUNET_NO);
5301 tunnel_add_path (t, path, own_pos);
5302 if (own_pos == size - 1)
5304 /* It is for us! Send ack. */
5305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5306 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5307 if (NULL == t->peers)
5309 /* New tunnel! Notify clients on first payload message. */
5310 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5312 GNUNET_break (GNUNET_SYSERR !=
5313 GNUNET_CONTAINER_multihashmap_put (t->peers,
5314 &my_full_id.hashPubKey,
5317 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5322 struct MeshPeerPath *path2;
5324 /* It's for somebody else! Retransmit. */
5325 path2 = path_duplicate (path);
5326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5327 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5328 path2 = path_duplicate (path);
5329 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5330 send_create_path (dest_peer_info, path, t);
5337 * Core handler for path destruction
5339 * @param cls closure
5340 * @param message message
5341 * @param peer peer identity this notification is about
5342 * @param atsi performance data
5343 * @param atsi_count number of records in 'atsi'
5345 * @return GNUNET_OK to keep the connection open,
5346 * GNUNET_SYSERR to close it (signal serious error)
5349 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5350 const struct GNUNET_MessageHeader *message,
5351 const struct GNUNET_ATS_Information *atsi,
5352 unsigned int atsi_count)
5354 struct GNUNET_MESH_ManipulatePath *msg;
5355 struct GNUNET_PeerIdentity *pi;
5356 struct MeshPeerPath *path;
5357 struct MeshTunnel *t;
5358 unsigned int own_pos;
5362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5363 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5364 size = ntohs (message->size);
5365 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5367 GNUNET_break_op (0);
5371 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5372 if (size % sizeof (struct GNUNET_PeerIdentity))
5374 GNUNET_break_op (0);
5377 size /= sizeof (struct GNUNET_PeerIdentity);
5380 GNUNET_break_op (0);
5383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5385 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5386 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5388 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5390 t = tunnel_get (pi, ntohl (msg->tid));
5393 /* TODO notify back: we don't know this tunnel */
5394 GNUNET_break_op (0);
5397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5398 path = path_new (size);
5400 for (i = 0; i < size; i++)
5402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5403 GNUNET_i2s (&pi[i]));
5404 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5405 if (path->peers[i] == myid)
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5409 if (own_pos < path->length - 1)
5410 send_prebuilt_message (message, &pi[own_pos + 1], t);
5412 send_client_tunnel_disconnect(t, NULL);
5414 tunnel_delete_peer (t, path->peers[path->length - 1]);
5415 path_destroy (path);
5421 * Core handler for notifications of broken paths
5423 * @param cls closure
5424 * @param message message
5425 * @param peer peer identity this notification is about
5426 * @param atsi performance data
5427 * @param atsi_count number of records in 'atsi'
5429 * @return GNUNET_OK to keep the connection open,
5430 * GNUNET_SYSERR to close it (signal serious error)
5433 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5434 const struct GNUNET_MessageHeader *message,
5435 const struct GNUNET_ATS_Information *atsi,
5436 unsigned int atsi_count)
5438 struct GNUNET_MESH_PathBroken *msg;
5439 struct MeshTunnel *t;
5441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5442 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5443 msg = (struct GNUNET_MESH_PathBroken *) message;
5444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5445 GNUNET_i2s (&msg->peer1));
5446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5447 GNUNET_i2s (&msg->peer2));
5448 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5451 GNUNET_break_op (0);
5454 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5455 GNUNET_PEER_search (&msg->peer2));
5462 * Core handler for tunnel destruction
5464 * @param cls closure
5465 * @param message message
5466 * @param peer peer identity this notification is about
5467 * @param atsi performance data
5468 * @param atsi_count number of records in 'atsi'
5470 * @return GNUNET_OK to keep the connection open,
5471 * GNUNET_SYSERR to close it (signal serious error)
5474 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5475 const struct GNUNET_MessageHeader *message,
5476 const struct GNUNET_ATS_Information *atsi,
5477 unsigned int atsi_count)
5479 struct GNUNET_MESH_TunnelDestroy *msg;
5480 struct MeshTunnel *t;
5482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5483 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5484 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5486 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5487 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5490 /* Probably already got the message from another path,
5491 * destroyed the tunnel and retransmitted to children.
5494 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5497 if (t->id.oid == myid)
5499 GNUNET_break_op (0);
5502 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5504 /* Tunnel was incoming, notify clients */
5505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5506 t->local_tid, t->local_tid_dest);
5507 send_clients_tunnel_destroy (t);
5509 tunnel_send_destroy (t);
5510 t->destroy = GNUNET_YES;
5511 // TODO: add timeout to destroy the tunnel anyway
5517 * Core handler for mesh network traffic going from the origin to a peer
5519 * @param cls closure
5520 * @param peer peer identity this notification is about
5521 * @param message message
5522 * @param atsi performance data
5523 * @param atsi_count number of records in 'atsi'
5524 * @return GNUNET_OK to keep the connection open,
5525 * GNUNET_SYSERR to close it (signal serious error)
5528 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5529 const struct GNUNET_MessageHeader *message,
5530 const struct GNUNET_ATS_Information *atsi,
5531 unsigned int atsi_count)
5533 struct GNUNET_MESH_Unicast *msg;
5534 struct GNUNET_PeerIdentity *neighbor;
5535 struct MeshTunnelChildInfo *cinfo;
5536 struct MeshTunnel *t;
5537 GNUNET_PEER_Id dest_id;
5542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5545 size = ntohs (message->size);
5547 sizeof (struct GNUNET_MESH_Unicast) +
5548 sizeof (struct GNUNET_MessageHeader))
5553 msg = (struct GNUNET_MESH_Unicast *) message;
5554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5555 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5557 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5560 /* TODO notify back: we don't know this tunnel */
5561 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5562 GNUNET_break_op (0);
5565 pid = ntohl (msg->pid);
5566 if (t->fwd_pid == pid)
5568 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5570 " Already seen pid %u, DROPPING!\n", pid);
5575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5576 " pid %u not seen yet, forwarding\n", pid);
5579 t->skip += (pid - t->fwd_pid) - 1;
5582 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5584 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5585 GNUNET_break_op (0);
5586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5587 "Received PID %u, ACK %u\n",
5588 pid, t->last_fwd_ack);
5592 tunnel_reset_timeout (t);
5593 dest_id = GNUNET_PEER_search (&msg->destination);
5594 if (dest_id == myid)
5596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5597 " it's for us! sending to clients...\n");
5598 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5599 send_subscribed_clients (message, &msg[1].header, t);
5600 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5603 ttl = ntohl (msg->ttl);
5604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5607 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5608 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5609 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5613 " not for us, retransmitting...\n");
5615 neighbor = tree_get_first_hop (t->tree, dest_id);
5616 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5618 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5621 if (GNUNET_YES == t->nobuffer &&
5622 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5624 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5625 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5626 GNUNET_break_op (0);
5629 send_prebuilt_message (message, neighbor, t);
5630 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5636 * Core handler for mesh network traffic going from the origin to all peers
5638 * @param cls closure
5639 * @param message message
5640 * @param peer peer identity this notification is about
5641 * @param atsi performance data
5642 * @param atsi_count number of records in 'atsi'
5643 * @return GNUNET_OK to keep the connection open,
5644 * GNUNET_SYSERR to close it (signal serious error)
5646 * TODO: Check who we got this from, to validate route.
5649 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5650 const struct GNUNET_MessageHeader *message,
5651 const struct GNUNET_ATS_Information *atsi,
5652 unsigned int atsi_count)
5654 struct GNUNET_MESH_Multicast *msg;
5655 struct MeshTunnel *t;
5659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5661 size = ntohs (message->size);
5662 if (sizeof (struct GNUNET_MESH_Multicast) +
5663 sizeof (struct GNUNET_MessageHeader) > size)
5665 GNUNET_break_op (0);
5668 msg = (struct GNUNET_MESH_Multicast *) message;
5669 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5673 /* TODO notify that we dont know that tunnel */
5674 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5675 GNUNET_break_op (0);
5678 pid = ntohl (msg->pid);
5679 if (t->fwd_pid == pid)
5681 /* already seen this packet, drop */
5682 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5684 " Already seen pid %u, DROPPING!\n", pid);
5685 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5691 " pid %u not seen yet, forwarding\n", pid);
5693 t->skip += (pid - t->fwd_pid) - 1;
5695 tunnel_reset_timeout (t);
5697 /* Transmit to locally interested clients */
5698 if (NULL != t->peers &&
5699 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5701 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5702 send_subscribed_clients (message, &msg[1].header, t);
5703 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5706 if (ntohl (msg->ttl) == 0)
5708 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5709 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5710 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5713 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5714 tunnel_send_multicast (t, message);
5720 * Core handler for mesh network traffic toward the owner of a tunnel
5722 * @param cls closure
5723 * @param message message
5724 * @param peer peer identity this notification is about
5725 * @param atsi performance data
5726 * @param atsi_count number of records in 'atsi'
5728 * @return GNUNET_OK to keep the connection open,
5729 * GNUNET_SYSERR to close it (signal serious error)
5732 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5733 const struct GNUNET_MessageHeader *message,
5734 const struct GNUNET_ATS_Information *atsi,
5735 unsigned int atsi_count)
5737 struct GNUNET_MESH_ToOrigin *msg;
5738 struct GNUNET_PeerIdentity id;
5739 struct MeshPeerInfo *peer_info;
5740 struct MeshTunnel *t;
5744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5746 size = ntohs (message->size);
5747 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5748 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5750 GNUNET_break_op (0);
5753 msg = (struct GNUNET_MESH_ToOrigin *) message;
5754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5755 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5756 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5757 pid = ntohl (msg->pid);
5761 /* TODO notify that we dont know this tunnel (whom)? */
5762 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5763 GNUNET_break_op (0);
5764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5765 "Received PID %u, ACK %u\n",
5770 if (t->bck_pid == pid)
5772 /* already seen this packet, drop */
5773 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5775 " Already seen pid %u, DROPPING!\n", pid);
5776 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5782 " pid %u not seen yet, forwarding\n", pid);
5786 if (NULL != t->owner)
5789 struct GNUNET_MESH_ToOrigin *copy;
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5792 " it's for us! sending to clients...\n");
5793 /* TODO signature verification */
5794 memcpy (cbuf, message, size);
5795 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5796 copy->tid = htonl (t->local_tid);
5797 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5798 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5799 ©->header, GNUNET_NO);
5800 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5804 " not for us, retransmitting...\n");
5806 peer_info = peer_info_get (&msg->oid);
5807 if (NULL == peer_info)
5809 /* unknown origin of tunnel */
5813 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5814 send_prebuilt_message (message, &id, t);
5815 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5822 * Core handler for mesh network traffic point-to-point acks.
5824 * @param cls closure
5825 * @param message message
5826 * @param peer peer identity this notification is about
5827 * @param atsi performance data
5828 * @param atsi_count number of records in 'atsi'
5830 * @return GNUNET_OK to keep the connection open,
5831 * GNUNET_SYSERR to close it (signal serious error)
5834 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5835 const struct GNUNET_MessageHeader *message,
5836 const struct GNUNET_ATS_Information *atsi,
5837 unsigned int atsi_count)
5839 struct GNUNET_MESH_ACK *msg;
5840 struct MeshTunnel *t;
5843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5845 msg = (struct GNUNET_MESH_ACK *) message;
5847 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5851 /* TODO notify that we dont know this tunnel (whom)? */
5852 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5853 GNUNET_break_op (0);
5856 ack = ntohl (msg->pid);
5858 /* Is this a forward or backward ACK? */
5859 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5861 struct MeshTunnelChildInfo *cinfo;
5864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5865 cinfo = tunnel_get_neighbor_fc (t, peer);
5866 cinfo->fwd_ack = ack;
5867 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5868 tunnel_unlock_fwd_queues (t);
5872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5874 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5875 tunnel_unlock_bck_queue (t);
5882 * Core handler for mesh network traffic point-to-point ack polls.
5884 * @param cls closure
5885 * @param message message
5886 * @param peer peer identity this notification is about
5887 * @param atsi performance data
5888 * @param atsi_count number of records in 'atsi'
5890 * @return GNUNET_OK to keep the connection open,
5891 * GNUNET_SYSERR to close it (signal serious error)
5894 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5895 const struct GNUNET_MessageHeader *message,
5896 const struct GNUNET_ATS_Information *atsi,
5897 unsigned int atsi_count)
5899 struct GNUNET_MESH_Poll *msg;
5900 struct MeshTunnel *t;
5902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5905 msg = (struct GNUNET_MESH_Poll *) message;
5907 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5911 /* TODO notify that we dont know this tunnel (whom)? */
5912 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5913 GNUNET_break_op (0);
5917 /* Is this a forward or backward ACK? */
5918 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5920 struct MeshTunnelChildInfo *cinfo;
5922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5923 cinfo = tunnel_get_neighbor_fc (t, peer);
5924 cinfo->bck_ack = cinfo->pid; // mark as ready to send
5925 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5930 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5938 * Core handler for path ACKs
5940 * @param cls closure
5941 * @param message message
5942 * @param peer peer identity this notification is about
5943 * @param atsi performance data
5944 * @param atsi_count number of records in 'atsi'
5946 * @return GNUNET_OK to keep the connection open,
5947 * GNUNET_SYSERR to close it (signal serious error)
5950 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5951 const struct GNUNET_MessageHeader *message,
5952 const struct GNUNET_ATS_Information *atsi,
5953 unsigned int atsi_count)
5955 struct GNUNET_MESH_PathACK *msg;
5956 struct GNUNET_PeerIdentity id;
5957 struct MeshPeerInfo *peer_info;
5958 struct MeshPeerPath *p;
5959 struct MeshTunnel *t;
5961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5962 GNUNET_i2s (&my_full_id));
5963 msg = (struct GNUNET_MESH_PathACK *) message;
5964 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5967 /* TODO notify that we don't know the tunnel */
5968 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5970 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5974 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5976 peer_info = peer_info_get (&msg->peer_id);
5977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5978 GNUNET_i2s (&msg->peer_id));
5979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5982 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5985 "connect_by_string completed, stopping search\n");
5986 regex_cancel_search (t->regex_ctx);
5987 t->regex_ctx = NULL;
5990 /* Add paths to peers? */
5991 p = tree_get_path_to_peer (t->tree, peer_info->id);
5994 path_add_to_peers (p, GNUNET_YES);
6002 /* Message for us? */
6003 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6006 if (NULL == t->owner)
6008 GNUNET_break_op (0);
6011 if (NULL != t->dht_get_type)
6013 GNUNET_DHT_get_stop (t->dht_get_type);
6014 t->dht_get_type = NULL;
6016 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6018 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6019 send_client_peer_connected (t, peer_info->id);
6024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6025 " not for us, retransmitting...\n");
6026 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6027 peer_info = peer_info_get (&msg->oid);
6028 if (NULL == peer_info)
6030 /* If we know the tunnel, we should DEFINITELY know the peer */
6034 send_prebuilt_message (message, &id, t);
6040 * Core handler for mesh keepalives.
6042 * @param cls closure
6043 * @param message message
6044 * @param peer peer identity this notification is about
6045 * @param atsi performance data
6046 * @param atsi_count number of records in 'atsi'
6047 * @return GNUNET_OK to keep the connection open,
6048 * GNUNET_SYSERR to close it (signal serious error)
6050 * TODO: Check who we got this from, to validate route.
6053 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6054 const struct GNUNET_MessageHeader *message,
6055 const struct GNUNET_ATS_Information *atsi,
6056 unsigned int atsi_count)
6058 struct GNUNET_MESH_TunnelKeepAlive *msg;
6059 struct MeshTunnel *t;
6061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6064 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6065 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6069 /* TODO notify that we dont know that tunnel */
6070 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6071 GNUNET_break_op (0);
6075 tunnel_reset_timeout (t);
6077 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6078 tunnel_send_multicast (t, message);
6085 * Functions to handle messages from core
6087 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6088 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6089 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6090 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6091 sizeof (struct GNUNET_MESH_PathBroken)},
6092 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6093 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6094 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6095 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6096 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6097 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6098 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6099 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6100 sizeof (struct GNUNET_MESH_ACK)},
6101 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6102 sizeof (struct GNUNET_MESH_Poll)},
6103 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6104 sizeof (struct GNUNET_MESH_PathACK)},
6110 /******************************************************************************/
6111 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6112 /******************************************************************************/
6115 * deregister_app: iterator for removing each application registered by a client
6117 * @param cls closure
6118 * @param key the hash of the application id (used to access the hashmap)
6119 * @param value the value stored at the key (client)
6121 * @return GNUNET_OK on success
6124 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6126 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6127 GNUNET_break (GNUNET_YES ==
6128 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6134 * notify_client_connection_failure: notify a client that the connection to the
6135 * requested remote peer is not possible (for instance, no route found)
6136 * Function called when the socket is ready to queue more data. "buf" will be
6137 * NULL and "size" zero if the socket was closed for writing in the meantime.
6139 * @param cls closure
6140 * @param size number of bytes available in buf
6141 * @param buf where the callee should write the message
6142 * @return number of bytes written to buf
6145 notify_client_connection_failure (void *cls, size_t size, void *buf)
6148 struct MeshPeerInfo *peer_info;
6149 struct GNUNET_MESH_PeerControl *msg;
6150 struct GNUNET_PeerIdentity id;
6152 if (0 == size && NULL == buf)
6154 // TODO retry? cancel?
6158 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6159 peer_info = (struct MeshPeerInfo *) cls;
6160 msg = (struct GNUNET_MESH_PeerControl *) buf;
6161 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6162 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6163 // msg->tunnel_id = htonl(peer_info->t->tid);
6164 GNUNET_PEER_resolve (peer_info->id, &id);
6165 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6173 * Send keepalive packets for a peer
6175 * @param cls Closure (tunnel for which to send the keepalive).
6176 * @param tc Notification context.
6179 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6181 struct MeshTunnel *t = cls;
6182 struct GNUNET_MESH_TunnelKeepAlive *msg;
6183 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6186 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6187 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6193 "sending keepalive for tunnel %d\n", t->id.tid);
6195 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6196 msg->header.size = htons (size);
6197 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6198 msg->oid = my_full_id;
6199 msg->tid = htonl (t->id.tid);
6200 tunnel_send_multicast (t, &msg->header);
6202 t->path_refresh_task =
6203 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6204 tunnel_reset_timeout(t);
6209 * Function to process paths received for a new peer addition. The recorded
6210 * paths form the initial tunnel, which can be optimized later.
6211 * Called on each result obtained for the DHT search.
6213 * @param cls closure
6214 * @param exp when will this value expire
6215 * @param key key of the result
6216 * @param get_path path of the get request
6217 * @param get_path_length lenght of get_path
6218 * @param put_path path of the put request
6219 * @param put_path_length length of the put_path
6220 * @param type type of the result
6221 * @param size number of bytes in data
6222 * @param data pointer to the result data
6224 * TODO: re-issue the request after certain time? cancel after X results?
6227 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6228 const struct GNUNET_HashCode * key,
6229 const struct GNUNET_PeerIdentity *get_path,
6230 unsigned int get_path_length,
6231 const struct GNUNET_PeerIdentity *put_path,
6232 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6233 size_t size, const void *data)
6235 struct MeshPathInfo *path_info = cls;
6236 struct MeshPeerPath *p;
6237 struct GNUNET_PeerIdentity pi;
6240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6241 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6244 p = path_build_from_dht (get_path, get_path_length, put_path,
6246 path_add_to_peers (p, GNUNET_NO);
6248 for (i = 0; i < path_info->peer->ntunnels; i++)
6250 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6251 peer_info_connect (path_info->peer, path_info->t);
6259 * Function to process paths received for a new peer addition. The recorded
6260 * paths form the initial tunnel, which can be optimized later.
6261 * Called on each result obtained for the DHT search.
6263 * @param cls closure
6264 * @param exp when will this value expire
6265 * @param key key of the result
6266 * @param get_path path of the get request
6267 * @param get_path_length lenght of get_path
6268 * @param put_path path of the put request
6269 * @param put_path_length length of the put_path
6270 * @param type type of the result
6271 * @param size number of bytes in data
6272 * @param data pointer to the result data
6275 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6276 const struct GNUNET_HashCode * key,
6277 const struct GNUNET_PeerIdentity *get_path,
6278 unsigned int get_path_length,
6279 const struct GNUNET_PeerIdentity *put_path,
6280 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6281 size_t size, const void *data)
6283 const struct PBlock *pb = data;
6284 const struct GNUNET_PeerIdentity *pi = &pb->id;
6285 struct MeshTunnel *t = cls;
6286 struct MeshPeerInfo *peer_info;
6287 struct MeshPeerPath *p;
6289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6290 if (size != sizeof (struct PBlock))
6292 GNUNET_break_op (0);
6295 if (ntohl(pb->type) != t->type)
6297 GNUNET_break_op (0);
6300 GNUNET_assert (NULL != t->owner);
6301 peer_info = peer_info_get (pi);
6302 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6304 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6306 p = path_build_from_dht (get_path, get_path_length, put_path,
6308 path_add_to_peers (p, GNUNET_NO);
6310 tunnel_add_peer (t, peer_info);
6311 peer_info_connect (peer_info, t);
6316 * Function to process DHT string to regex matching.
6317 * Called on each result obtained for the DHT search.
6319 * @param cls closure (search context)
6320 * @param exp when will this value expire
6321 * @param key key of the result
6322 * @param get_path path of the get request (not used)
6323 * @param get_path_length lenght of get_path (not used)
6324 * @param put_path path of the put request (not used)
6325 * @param put_path_length length of the put_path (not used)
6326 * @param type type of the result
6327 * @param size number of bytes in data
6328 * @param data pointer to the result data
6331 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6332 const struct GNUNET_HashCode * key,
6333 const struct GNUNET_PeerIdentity *get_path,
6334 unsigned int get_path_length,
6335 const struct GNUNET_PeerIdentity *put_path,
6336 unsigned int put_path_length,
6337 enum GNUNET_BLOCK_Type type,
6338 size_t size, const void *data)
6340 const struct MeshRegexAccept *block = data;
6341 struct MeshRegexSearchContext *ctx = cls;
6342 struct MeshRegexSearchInfo *info = ctx->info;
6343 struct MeshPeerPath *p;
6344 struct MeshPeerInfo *peer_info;
6346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6349 peer_info = peer_info_get(&block->id);
6350 p = path_build_from_dht (get_path, get_path_length, put_path,
6352 path_add_to_peers (p, GNUNET_NO);
6355 tunnel_add_peer (info->t, peer_info);
6356 peer_info_connect (peer_info, info->t);
6357 if (0 == info->peer)
6359 info->peer = peer_info->id;
6363 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6366 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6367 ®ex_connect_timeout,
6375 * Function to process DHT string to regex matching.
6376 * Called on each result obtained for the DHT search.
6378 * @param cls closure (search context)
6379 * @param exp when will this value expire
6380 * @param key key of the result
6381 * @param get_path path of the get request (not used)
6382 * @param get_path_length lenght of get_path (not used)
6383 * @param put_path path of the put request (not used)
6384 * @param put_path_length length of the put_path (not used)
6385 * @param type type of the result
6386 * @param size number of bytes in data
6387 * @param data pointer to the result data
6389 * TODO: re-issue the request after certain time? cancel after X results?
6392 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6393 const struct GNUNET_HashCode * key,
6394 const struct GNUNET_PeerIdentity *get_path,
6395 unsigned int get_path_length,
6396 const struct GNUNET_PeerIdentity *put_path,
6397 unsigned int put_path_length,
6398 enum GNUNET_BLOCK_Type type,
6399 size_t size, const void *data)
6401 const struct MeshRegexBlock *block = data;
6402 struct MeshRegexSearchContext *ctx = cls;
6403 struct MeshRegexSearchInfo *info = ctx->info;
6407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6408 "DHT GET STRING RETURNED RESULTS\n");
6409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6410 " key: %s\n", GNUNET_h2s (key));
6412 copy = GNUNET_malloc (size);
6413 memcpy (copy, data, size);
6414 GNUNET_break (GNUNET_OK ==
6415 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6416 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6417 len = ntohl (block->n_proof);
6419 char proof[len + 1];
6421 memcpy (proof, &block[1], len);
6423 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6425 GNUNET_break_op (0);
6429 len = strlen (info->description);
6430 if (len == ctx->position) // String processed
6432 if (GNUNET_YES == ntohl (block->accepting))
6434 regex_find_path(key, ctx);
6438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6439 // FIXME REGEX this block not successful, wait for more? start timeout?
6444 regex_next_edge (block, size, ctx);
6449 /******************************************************************************/
6450 /********************* MESH LOCAL HANDLES **************************/
6451 /******************************************************************************/
6455 * Handler for client disconnection
6457 * @param cls closure
6458 * @param client identification of the client; NULL
6459 * for the last call when the server is destroyed
6462 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6464 struct MeshClient *c;
6465 struct MeshClient *next;
6468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6477 if (c->handle != client)
6479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6485 GNUNET_SERVER_client_drop (c->handle);
6486 c->shutting_down = GNUNET_YES;
6487 GNUNET_assert (NULL != c->own_tunnels);
6488 GNUNET_assert (NULL != c->incoming_tunnels);
6489 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6490 &tunnel_destroy_iterator, c);
6491 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6492 &tunnel_destroy_iterator, c);
6493 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6494 &tunnel_destroy_iterator, c);
6495 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6496 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6497 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6499 /* deregister clients applications */
6500 if (NULL != c->apps)
6502 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6503 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6505 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6506 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6508 GNUNET_SCHEDULER_cancel (announce_applications_task);
6509 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6511 if (NULL != c->types)
6512 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6513 for (i = 0; i < c->n_regex; i++)
6515 GNUNET_free (c->regexes[i]);
6517 GNUNET_free_non_null (c->regexes);
6518 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6519 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6521 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6524 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6533 * Handler for new clients
6535 * @param cls closure
6536 * @param client identification of the client
6537 * @param message the actual message, which includes messages the client wants
6540 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6541 const struct GNUNET_MessageHeader *message)
6543 struct GNUNET_MESH_ClientConnect *cc_msg;
6544 struct MeshClient *c;
6545 GNUNET_MESH_ApplicationType *a;
6552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6553 /* Check data sanity */
6554 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6555 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6556 ntypes = ntohs (cc_msg->types);
6557 napps = ntohs (cc_msg->applications);
6559 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6562 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6566 /* Create new client structure */
6567 c = GNUNET_malloc (sizeof (struct MeshClient));
6568 c->id = next_client_id++;
6569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6571 GNUNET_SERVER_client_keep (client);
6572 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6575 GNUNET_MESH_ApplicationType at;
6576 struct GNUNET_HashCode hc;
6578 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6579 for (i = 0; i < napps; i++)
6582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6583 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6584 /* store in clients hashmap */
6585 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6586 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6587 /* store in global hashmap, for announcements */
6588 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6589 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6591 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6592 announce_applications_task =
6593 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6599 struct GNUNET_HashCode hc;
6601 t = (uint16_t *) & a[napps];
6602 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6603 for (i = 0; i < ntypes; i++)
6606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6607 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6609 /* store in clients hashmap */
6610 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6611 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6612 /* store in global hashmap */
6613 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6614 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6618 " client has %u+%u subscriptions\n", napps, ntypes);
6620 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6621 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6622 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6623 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6624 GNUNET_SERVER_notification_context_add (nc, client);
6625 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6627 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6633 * Handler for clients announcing available services by a regular expression.
6635 * @param cls closure
6636 * @param client identification of the client
6637 * @param message the actual message, which includes messages the client wants
6640 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6641 const struct GNUNET_MessageHeader *message)
6643 struct MeshClient *c;
6647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6649 /* Sanity check for client registration */
6650 if (NULL == (c = client_get (client)))
6653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6658 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6659 regex = GNUNET_malloc (len + 1);
6660 memcpy (regex, &message[1], len);
6662 GNUNET_array_append (c->regexes, c->n_regex, regex);
6663 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6665 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6671 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6677 * Handler for requests of new tunnels
6679 * @param cls closure
6680 * @param client identification of the client
6681 * @param message the actual message
6684 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6685 const struct GNUNET_MessageHeader *message)
6687 struct GNUNET_MESH_TunnelMessage *t_msg;
6688 struct MeshTunnel *t;
6689 struct MeshClient *c;
6690 MESH_TunnelNumber tid;
6692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6694 /* Sanity check for client registration */
6695 if (NULL == (c = client_get (client)))
6698 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6703 /* Message sanity check */
6704 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6711 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6712 /* Sanity check for tunnel numbering */
6713 tid = ntohl (t_msg->tunnel_id);
6714 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6717 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6720 /* Sanity check for duplicate tunnel IDs */
6721 if (NULL != tunnel_get_by_local_id (c, tid))
6724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6728 while (NULL != tunnel_get_by_pi (myid, next_tid))
6729 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6730 t = tunnel_new (myid, next_tid++, c, tid);
6733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6735 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6738 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6740 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6741 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6744 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6750 * Handler for requests of deleting tunnels
6752 * @param cls closure
6753 * @param client identification of the client
6754 * @param message the actual message
6757 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6758 const struct GNUNET_MessageHeader *message)
6760 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6761 struct MeshClient *c;
6762 struct MeshTunnel *t;
6763 MESH_TunnelNumber tid;
6765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6766 "Got a DESTROY TUNNEL from client!\n");
6768 /* Sanity check for client registration */
6769 if (NULL == (c = client_get (client)))
6772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6777 /* Message sanity check */
6778 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6785 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6787 /* Retrieve tunnel */
6788 tid = ntohl (tunnel_msg->tunnel_id);
6789 t = tunnel_get_by_local_id(c, tid);
6792 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6794 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6797 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6799 client_ignore_tunnel (c, t);
6801 // TODO: when to destroy incoming tunnel?
6802 if (t->nclients == 0)
6804 GNUNET_assert (GNUNET_YES ==
6805 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6807 GNUNET_assert (GNUNET_YES ==
6808 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6809 &my_full_id.hashPubKey,
6813 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6816 send_client_tunnel_disconnect(t, c);
6817 client_delete_tunnel(c, t);
6819 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6821 tunnel_send_destroy (t);
6822 t->destroy = GNUNET_YES;
6823 // The tunnel will be destroyed when the last message is transmitted.
6824 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6830 * Handler for requests of seeting tunnel's speed.
6832 * @param cls Closure (unused).
6833 * @param client Identification of the client.
6834 * @param message The actual message.
6837 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6838 const struct GNUNET_MessageHeader *message)
6840 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6841 struct MeshClient *c;
6842 struct MeshTunnel *t;
6843 MESH_TunnelNumber tid;
6845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6846 "Got a SPEED request from client!\n");
6848 /* Sanity check for client registration */
6849 if (NULL == (c = client_get (client)))
6852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6858 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6860 /* Retrieve tunnel */
6861 tid = ntohl (tunnel_msg->tunnel_id);
6862 t = tunnel_get_by_local_id(c, tid);
6865 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6867 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6871 switch (ntohs(message->type))
6873 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6874 t->speed_min = GNUNET_YES;
6876 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6877 t->speed_min = GNUNET_NO;
6882 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6887 * Handler for requests of seeting tunnel's buffering policy.
6889 * @param cls Closure (unused).
6890 * @param client Identification of the client.
6891 * @param message The actual message.
6894 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6895 const struct GNUNET_MessageHeader *message)
6897 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6898 struct MeshClient *c;
6899 struct MeshTunnel *t;
6900 MESH_TunnelNumber tid;
6902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6903 "Got a BUFFER request from client!\n");
6905 /* Sanity check for client registration */
6906 if (NULL == (c = client_get (client)))
6909 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6914 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6916 /* Retrieve tunnel */
6917 tid = ntohl (tunnel_msg->tunnel_id);
6918 t = tunnel_get_by_local_id(c, tid);
6921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6927 switch (ntohs(message->type))
6929 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6930 t->nobuffer = GNUNET_NO;
6932 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6933 t->nobuffer = GNUNET_YES;
6939 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6944 * Handler for connection requests to new peers
6946 * @param cls closure
6947 * @param client identification of the client
6948 * @param message the actual message (PeerControl)
6951 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6952 const struct GNUNET_MessageHeader *message)
6954 struct GNUNET_MESH_PeerControl *peer_msg;
6955 struct MeshPeerInfo *peer_info;
6956 struct MeshClient *c;
6957 struct MeshTunnel *t;
6958 MESH_TunnelNumber tid;
6960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6961 /* Sanity check for client registration */
6962 if (NULL == (c = client_get (client)))
6965 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6970 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6972 /* Sanity check for message size */
6973 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6976 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6980 /* Tunnel exists? */
6981 tid = ntohl (peer_msg->tunnel_id);
6982 t = tunnel_get_by_local_id (c, tid);
6986 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6990 /* Does client own tunnel? */
6991 if (t->owner->handle != client)
6994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6998 GNUNET_i2s (&peer_msg->peer));
6999 peer_info = peer_info_get (&peer_msg->peer);
7001 tunnel_add_peer (t, peer_info);
7002 peer_info_connect (peer_info, t);
7004 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7010 * Handler for disconnection requests of peers in a tunnel
7012 * @param cls closure
7013 * @param client identification of the client
7014 * @param message the actual message (PeerControl)
7017 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7018 const struct GNUNET_MessageHeader *message)
7020 struct GNUNET_MESH_PeerControl *peer_msg;
7021 struct MeshPeerInfo *peer_info;
7022 struct MeshClient *c;
7023 struct MeshTunnel *t;
7024 MESH_TunnelNumber tid;
7026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7027 /* Sanity check for client registration */
7028 if (NULL == (c = client_get (client)))
7031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7036 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7038 /* Sanity check for message size */
7039 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7042 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7046 /* Tunnel exists? */
7047 tid = ntohl (peer_msg->tunnel_id);
7048 t = tunnel_get_by_local_id (c, tid);
7052 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7057 /* Does client own tunnel? */
7058 if (t->owner->handle != client)
7061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7066 GNUNET_i2s (&peer_msg->peer));
7067 /* Is the peer in the tunnel? */
7069 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7070 if (NULL == peer_info)
7073 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7077 /* Ok, delete peer from tunnel */
7078 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7079 &peer_msg->peer.hashPubKey);
7081 send_destroy_path (t, peer_info->id);
7082 tunnel_delete_peer (t, peer_info->id);
7083 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7088 * Handler for blacklist requests of peers in a tunnel
7090 * @param cls closure
7091 * @param client identification of the client
7092 * @param message the actual message (PeerControl)
7094 * FIXME implement DHT block bloomfilter
7097 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7098 const struct GNUNET_MessageHeader *message)
7100 struct GNUNET_MESH_PeerControl *peer_msg;
7101 struct MeshClient *c;
7102 struct MeshTunnel *t;
7103 MESH_TunnelNumber tid;
7105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7106 /* Sanity check for client registration */
7107 if (NULL == (c = client_get (client)))
7110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7115 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7117 /* Sanity check for message size */
7118 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7125 /* Tunnel exists? */
7126 tid = ntohl (peer_msg->tunnel_id);
7127 t = tunnel_get_by_local_id (c, tid);
7131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7136 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7137 GNUNET_PEER_intern(&peer_msg->peer));
7142 * Handler for unblacklist requests of peers in a tunnel
7144 * @param cls closure
7145 * @param client identification of the client
7146 * @param message the actual message (PeerControl)
7149 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7150 const struct GNUNET_MessageHeader *message)
7152 struct GNUNET_MESH_PeerControl *peer_msg;
7153 struct MeshClient *c;
7154 struct MeshTunnel *t;
7155 MESH_TunnelNumber tid;
7159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7160 /* Sanity check for client registration */
7161 if (NULL == (c = client_get (client)))
7164 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7169 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7171 /* Sanity check for message size */
7172 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7179 /* Tunnel exists? */
7180 tid = ntohl (peer_msg->tunnel_id);
7181 t = tunnel_get_by_local_id (c, tid);
7185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7190 /* if peer is not known, complain */
7191 pid = GNUNET_PEER_search (&peer_msg->peer);
7198 /* search and remove from list */
7199 for (i = 0; i < t->nblacklisted; i++)
7201 if (t->blacklisted[i] == pid)
7203 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7204 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7209 /* if peer hasn't been blacklisted, complain */
7215 * Handler for connection requests to new peers by type
7217 * @param cls closure
7218 * @param client identification of the client
7219 * @param message the actual message (ConnectPeerByType)
7222 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7223 const struct GNUNET_MessageHeader *message)
7225 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7226 struct MeshClient *c;
7227 struct MeshTunnel *t;
7228 struct GNUNET_HashCode hash;
7229 MESH_TunnelNumber tid;
7231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7232 /* Sanity check for client registration */
7233 if (NULL == (c = client_get (client)))
7236 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7241 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7243 /* Sanity check for message size */
7244 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7245 ntohs (connect_msg->header.size))
7248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7252 /* Tunnel exists? */
7253 tid = ntohl (connect_msg->tunnel_id);
7254 t = tunnel_get_by_local_id (c, tid);
7258 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7262 /* Does client own tunnel? */
7263 if (t->owner->handle != client)
7266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7270 /* Do WE have the service? */
7271 t->type = ntohl (connect_msg->type);
7272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7273 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7274 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7277 /* Yes! Fast forward, add ourselves to the tunnel and send the
7278 * good news to the client, and alert the destination client of
7279 * an incoming tunnel.
7281 * FIXME send a path create to self, avoid code duplication
7283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7284 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7285 peer_info_get (&my_full_id),
7286 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7289 send_client_peer_connected (t, myid);
7290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7291 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7293 t->local_tid_dest = next_local_tid++;
7294 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7295 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7296 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7300 /* Ok, lets find a peer offering the service */
7301 if (NULL != t->dht_get_type)
7303 GNUNET_DHT_get_stop (t->dht_get_type);
7305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7306 GNUNET_h2s (&hash));
7308 GNUNET_DHT_get_start (dht_handle,
7309 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7311 dht_replication_level,
7312 GNUNET_DHT_RO_RECORD_ROUTE |
7313 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7315 &dht_get_type_handler, t);
7317 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7323 * Handler for connection requests to new peers by a string service description.
7325 * @param cls closure
7326 * @param client identification of the client
7327 * @param message the actual message, which includes messages the client wants
7330 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7331 const struct GNUNET_MessageHeader *message)
7333 struct GNUNET_MESH_ConnectPeerByString *msg;
7334 struct MeshRegexSearchContext *ctx;
7335 struct MeshRegexSearchInfo *info;
7336 struct GNUNET_DHT_GetHandle *get_h;
7337 struct GNUNET_HashCode key;
7338 struct MeshTunnel *t;
7339 struct MeshClient *c;
7340 MESH_TunnelNumber tid;
7344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7345 "Connect by string started\n");
7346 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7347 size = htons (message->size);
7349 /* Sanity check for client registration */
7350 if (NULL == (c = client_get (client)))
7353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7358 /* Message size sanity check */
7359 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7362 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7366 /* Tunnel exists? */
7367 tid = ntohl (msg->tunnel_id);
7368 t = tunnel_get_by_local_id (c, tid);
7372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7376 /* Does client own tunnel? */
7377 if (t->owner->handle != client)
7380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7385 " on tunnel %s [%u]\n",
7386 GNUNET_i2s(&my_full_id),
7389 /* Only one connect_by_string allowed at the same time! */
7390 /* FIXME: allow more, return handle at api level to cancel, document */
7391 if (NULL != t->regex_ctx)
7394 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7398 /* Find string itself */
7399 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7400 string = (const char *) &msg[1];
7402 /* Initialize context */
7403 size = GNUNET_REGEX_get_first_key(string, len, &key);
7404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7405 " consumed %u bits out of %u\n", size, len);
7406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7407 " looking for %s\n", GNUNET_h2s (&key));
7409 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7411 info->description = GNUNET_malloc (len + 1);
7412 memcpy (info->description, string, len);
7413 info->description[len] = '\0';
7414 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7415 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7418 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7419 ctx->position = size;
7423 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7425 /* Start search in DHT */
7426 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7427 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7428 &key, /* key to search */
7429 dht_replication_level, /* replication level */
7430 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7431 NULL, /* xquery */ // FIXME BLOOMFILTER
7432 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7433 &dht_get_string_handler, ctx);
7435 GNUNET_break (GNUNET_OK ==
7436 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7439 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7441 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7447 * Handler for client traffic directed to one peer
7449 * @param cls closure
7450 * @param client identification of the client
7451 * @param message the actual message
7454 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7455 const struct GNUNET_MessageHeader *message)
7457 struct MeshClient *c;
7458 struct MeshTunnel *t;
7459 struct MeshPeerInfo *pi;
7460 struct GNUNET_MESH_Unicast *data_msg;
7461 MESH_TunnelNumber tid;
7464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7465 "Got a unicast request from a client!\n");
7467 /* Sanity check for client registration */
7468 if (NULL == (c = client_get (client)))
7471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7476 data_msg = (struct GNUNET_MESH_Unicast *) message;
7478 /* Sanity check for message size */
7479 size = ntohs (message->size);
7480 if (sizeof (struct GNUNET_MESH_Unicast) +
7481 sizeof (struct GNUNET_MessageHeader) > size)
7484 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7488 /* Tunnel exists? */
7489 tid = ntohl (data_msg->tid);
7490 t = tunnel_get_by_local_id (c, tid);
7494 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7498 /* Is it a local tunnel? Then, does client own the tunnel? */
7499 if (t->owner->handle != client)
7502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7506 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7507 &data_msg->destination.hashPubKey);
7508 /* Is the selected peer in the tunnel? */
7512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7516 /* PID should be as expected */
7517 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7521 "Unicast PID, expected %u, got %u\n",
7522 t->fwd_pid + 1, ntohl (data_msg->pid));
7523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7527 /* Ok, everything is correct, send the message
7528 * (pretend we got it from a mesh peer)
7531 /* Work around const limitation */
7532 char buf[ntohs (message->size)] GNUNET_ALIGN;
7533 struct GNUNET_MESH_Unicast *copy;
7535 copy = (struct GNUNET_MESH_Unicast *) buf;
7536 memcpy (buf, data_msg, size);
7537 copy->oid = my_full_id;
7538 copy->tid = htonl (t->id.tid);
7539 copy->ttl = htonl (default_ttl);
7540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7541 " calling generic handler...\n");
7542 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7545 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7552 * Handler for client traffic directed to the origin
7554 * @param cls closure
7555 * @param client identification of the client
7556 * @param message the actual message
7559 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7560 const struct GNUNET_MessageHeader *message)
7562 struct GNUNET_MESH_ToOrigin *data_msg;
7563 struct MeshTunnelClientInfo *clinfo;
7564 struct MeshClient *c;
7565 struct MeshTunnel *t;
7566 MESH_TunnelNumber tid;
7569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7570 "Got a ToOrigin request from a client!\n");
7571 /* Sanity check for client registration */
7572 if (NULL == (c = client_get (client)))
7575 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7580 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7582 /* Sanity check for message size */
7583 size = ntohs (message->size);
7584 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7585 sizeof (struct GNUNET_MessageHeader) > size)
7588 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7592 /* Tunnel exists? */
7593 tid = ntohl (data_msg->tid);
7594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7595 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7598 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7601 t = tunnel_get_by_local_id (c, tid);
7604 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7611 /* It should be sent by someone who has this as incoming tunnel. */
7612 if (GNUNET_NO == client_knows_tunnel (c, t))
7615 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7619 /* PID should be as expected */
7620 clinfo = tunnel_get_client_fc (t, c);
7621 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7624 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7625 "To Origin PID, expected %u, got %u\n",
7626 clinfo->bck_pid + 1,
7627 ntohl (data_msg->pid));
7628 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7632 /* Ok, everything is correct, send the message
7633 * (pretend we got it from a mesh peer)
7637 char buf[ntohs (message->size)] GNUNET_ALIGN;
7638 struct GNUNET_MESH_ToOrigin *copy;
7640 /* Work around const limitation */
7641 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7642 memcpy (buf, data_msg, size);
7643 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7644 copy->tid = htonl (t->id.tid);
7645 copy->ttl = htonl (default_ttl);
7646 if (ntohl (copy->pid) != (t->bck_pid + 1))
7649 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7650 "To Origin PID, expected %u, got %u\n",
7655 copy->sender = my_full_id;
7656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7657 " calling generic handler...\n");
7658 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7660 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7667 * Handler for client traffic directed to all peers in a tunnel
7669 * @param cls closure
7670 * @param client identification of the client
7671 * @param message the actual message
7674 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7675 const struct GNUNET_MessageHeader *message)
7677 struct MeshClient *c;
7678 struct MeshTunnel *t;
7679 struct GNUNET_MESH_Multicast *data_msg;
7680 MESH_TunnelNumber tid;
7682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7683 "Got a multicast request from a client!\n");
7685 /* Sanity check for client registration */
7686 if (NULL == (c = client_get (client)))
7689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7694 data_msg = (struct GNUNET_MESH_Multicast *) message;
7696 /* Sanity check for message size */
7697 if (sizeof (struct GNUNET_MESH_Multicast) +
7698 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7701 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7705 /* Tunnel exists? */
7706 tid = ntohl (data_msg->tid);
7707 t = tunnel_get_by_local_id (c, tid);
7711 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7712 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7714 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7718 /* Does client own tunnel? */
7719 if (t->owner->handle != client)
7722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7726 /* PID should be as expected */
7727 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7730 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7731 "Multicast PID, expected %u, got %u\n",
7732 t->fwd_pid + 1, ntohl (data_msg->pid));
7733 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7738 char buf[ntohs (message->size)] GNUNET_ALIGN;
7739 struct GNUNET_MESH_Multicast *copy;
7741 copy = (struct GNUNET_MESH_Multicast *) buf;
7742 memcpy (buf, message, ntohs (message->size));
7743 copy->oid = my_full_id;
7744 copy->tid = htonl (t->id.tid);
7745 copy->ttl = htonl (default_ttl);
7746 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7748 " calling generic handler...\n");
7749 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7752 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7758 * Handler for client's ACKs for payload traffic.
7760 * @param cls Closure (unused).
7761 * @param client Identification of the client.
7762 * @param message The actual message.
7765 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7766 const struct GNUNET_MessageHeader *message)
7768 struct GNUNET_MESH_LocalAck *msg;
7769 struct MeshTunnel *t;
7770 struct MeshClient *c;
7771 MESH_TunnelNumber tid;
7774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7775 /* Sanity check for client registration */
7776 if (NULL == (c = client_get (client)))
7779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7784 msg = (struct GNUNET_MESH_LocalAck *) message;
7786 /* Tunnel exists? */
7787 tid = ntohl (msg->tunnel_id);
7788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7789 t = tunnel_get_by_local_id (c, tid);
7793 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7794 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7799 ack = ntohl (msg->max_pid);
7800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7802 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7803 if (NULL != t->owner && t->owner->handle == client)
7805 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7807 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7811 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7812 tunnel_set_client_fwd_ack (t, c, ack);
7813 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7816 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7823 * Functions to handle messages from clients
7825 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7826 {&handle_local_new_client, NULL,
7827 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7828 {&handle_local_announce_regex, NULL,
7829 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7830 {&handle_local_tunnel_create, NULL,
7831 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7832 sizeof (struct GNUNET_MESH_TunnelMessage)},
7833 {&handle_local_tunnel_destroy, NULL,
7834 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7835 sizeof (struct GNUNET_MESH_TunnelMessage)},
7836 {&handle_local_tunnel_speed, NULL,
7837 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7838 sizeof (struct GNUNET_MESH_TunnelMessage)},
7839 {&handle_local_tunnel_speed, NULL,
7840 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7841 sizeof (struct GNUNET_MESH_TunnelMessage)},
7842 {&handle_local_tunnel_buffer, NULL,
7843 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7844 sizeof (struct GNUNET_MESH_TunnelMessage)},
7845 {&handle_local_tunnel_buffer, NULL,
7846 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7847 sizeof (struct GNUNET_MESH_TunnelMessage)},
7848 {&handle_local_connect_add, NULL,
7849 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7850 sizeof (struct GNUNET_MESH_PeerControl)},
7851 {&handle_local_connect_del, NULL,
7852 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7853 sizeof (struct GNUNET_MESH_PeerControl)},
7854 {&handle_local_blacklist, NULL,
7855 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7856 sizeof (struct GNUNET_MESH_PeerControl)},
7857 {&handle_local_unblacklist, NULL,
7858 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7859 sizeof (struct GNUNET_MESH_PeerControl)},
7860 {&handle_local_connect_by_type, NULL,
7861 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7862 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7863 {&handle_local_connect_by_string, NULL,
7864 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7865 {&handle_local_unicast, NULL,
7866 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7867 {&handle_local_to_origin, NULL,
7868 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7869 {&handle_local_multicast, NULL,
7870 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7871 {&handle_local_ack, NULL,
7872 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7873 sizeof (struct GNUNET_MESH_LocalAck)},
7879 * To be called on core init/fail.
7881 * @param cls service closure
7882 * @param server handle to the server for this service
7883 * @param identity the public identity of this peer
7886 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7887 const struct GNUNET_PeerIdentity *identity)
7890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7891 core_handle = server;
7892 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7895 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7896 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7898 GNUNET_abort(); // Try harder
7905 * Method called whenever a given peer connects.
7907 * @param cls closure
7908 * @param peer peer identity this notification is about
7909 * @param atsi performance data for the connection
7910 * @param atsi_count number of records in 'atsi'
7913 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7914 const struct GNUNET_ATS_Information *atsi,
7915 unsigned int atsi_count)
7917 struct MeshPeerInfo *peer_info;
7918 struct MeshPeerPath *path;
7920 DEBUG_CONN ("Peer connected\n");
7921 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7922 peer_info = peer_info_get (peer);
7923 if (myid == peer_info->id)
7925 DEBUG_CONN (" (self)\n");
7930 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7932 path = path_new (2);
7933 path->peers[0] = myid;
7934 path->peers[1] = peer_info->id;
7935 GNUNET_PEER_change_rc (myid, 1);
7936 GNUNET_PEER_change_rc (peer_info->id, 1);
7937 peer_info_add_path (peer_info, path, GNUNET_YES);
7938 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7944 * Method called whenever a peer disconnects.
7946 * @param cls closure
7947 * @param peer peer identity this notification is about
7950 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7952 struct MeshPeerInfo *pi;
7953 struct MeshPeerQueue *q;
7954 struct MeshPeerQueue *n;
7956 DEBUG_CONN ("Peer disconnected\n");
7957 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7967 /* TODO try to reroute this traffic instead */
7968 queue_destroy(q, GNUNET_YES);
7971 if (NULL != pi->core_transmit)
7973 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
7974 pi->core_transmit = NULL;
7976 peer_info_remove_path (pi, pi->id, myid);
7979 DEBUG_CONN (" (self)\n");
7981 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7986 /******************************************************************************/
7987 /************************ MAIN FUNCTIONS ****************************/
7988 /******************************************************************************/
7991 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7993 * @param cls closure
7994 * @param key current key code
7995 * @param value value in the hash map
7996 * @return GNUNET_YES if we should continue to iterate,
8000 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8002 struct MeshTunnel *t = value;
8009 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8011 * @param cls closure
8012 * @param key current key code
8013 * @param value value in the hash map
8014 * @return GNUNET_YES if we should continue to iterate,
8018 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8020 struct MeshPeerInfo *p = value;
8021 struct MeshPeerQueue *q;
8022 struct MeshPeerQueue *n;
8030 queue_destroy(q, GNUNET_YES);
8034 peer_info_destroy (p);
8040 * Task run during shutdown.
8046 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8050 if (core_handle != NULL)
8052 GNUNET_CORE_disconnect (core_handle);
8057 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8060 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8061 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8062 if (dht_handle != NULL)
8064 GNUNET_DHT_disconnect (dht_handle);
8069 GNUNET_SERVER_notification_context_destroy (nc);
8072 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8074 GNUNET_SCHEDULER_cancel (announce_id_task);
8075 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8082 * Callback for hostkey read/generation
8085 * @param pk the private key
8086 * @param emsg error message
8089 key_generation_cb (void *cls,
8090 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8093 struct MeshPeerInfo *peer;
8094 struct MeshPeerPath *p;
8099 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8100 _("Mesh service could not access hostkey. Exiting.\n"));
8101 GNUNET_SCHEDULER_shutdown ();
8104 my_private_key = pk;
8105 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8106 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8107 &my_full_id.hashPubKey);
8108 myid = GNUNET_PEER_intern (&my_full_id);
8109 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8110 "Mesh for peer [%s] starting\n",
8111 GNUNET_i2s(&my_full_id));
8113 // transport_handle = GNUNET_TRANSPORT_connect(c,
8123 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8126 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8127 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8128 GNUNET_SERVER_disconnect_notify (server_handle,
8129 &handle_local_client_disconnect, NULL);
8133 clients_tail = NULL;
8136 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8137 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8139 /* Create a peer_info for the local peer */
8140 peer = peer_info_get (&my_full_id);
8143 GNUNET_PEER_change_rc (myid, 1);
8144 peer_info_add_path (peer, p, GNUNET_YES);
8145 GNUNET_SERVER_resume (server_handle);
8146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8151 * Process mesh requests.
8153 * @param cls closure
8154 * @param server the initialized server
8155 * @param c configuration to use
8158 run (void *cls, struct GNUNET_SERVER_Handle *server,
8159 const struct GNUNET_CONFIGURATION_Handle *c)
8163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8164 server_handle = server;
8165 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8166 NULL, /* Closure passed to MESH functions */
8167 &core_init, /* Call core_init once connected */
8168 &core_connect, /* Handle connects */
8169 &core_disconnect, /* remove peers on disconnects */
8170 NULL, /* Don't notify about all incoming messages */
8171 GNUNET_NO, /* For header only in notification */
8172 NULL, /* Don't notify about all outbound messages */
8173 GNUNET_NO, /* For header-only out notification */
8174 core_handlers); /* Register these handlers */
8176 if (core_handle == NULL)
8179 GNUNET_SCHEDULER_shutdown ();
8184 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8189 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8191 GNUNET_SCHEDULER_shutdown ();
8196 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8197 &refresh_path_time))
8199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8201 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8202 "mesh", "refresh path time");
8203 GNUNET_SCHEDULER_shutdown ();
8208 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8209 &app_announce_time))
8211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8213 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8214 "mesh", "app announce time");
8215 GNUNET_SCHEDULER_shutdown ();
8220 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8225 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8226 "mesh", "id announce time");
8227 GNUNET_SCHEDULER_shutdown ();
8235 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8236 &unacknowledged_wait_time))
8238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8240 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8241 "mesh", "unacknowledged wait time");
8242 GNUNET_SCHEDULER_shutdown ();
8247 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8250 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8252 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8253 "mesh", "connect timeout");
8254 GNUNET_SCHEDULER_shutdown ();
8259 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8264 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8265 "mesh", "max msgs queue");
8266 GNUNET_SCHEDULER_shutdown ();
8271 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8276 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8277 "mesh", "max tunnels");
8278 GNUNET_SCHEDULER_shutdown ();
8283 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8286 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8288 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8289 "mesh", "default ttl", 64);
8294 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8295 &dht_replication_level))
8297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8299 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8300 "mesh", "dht replication level", 10);
8301 dht_replication_level = 10;
8304 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8305 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8306 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8307 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8308 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8310 dht_handle = GNUNET_DHT_connect (c, 64);
8311 if (NULL == dht_handle)
8315 stats = GNUNET_STATISTICS_create ("mesh", c);
8317 GNUNET_SERVER_suspend (server_handle);
8318 /* Scheduled the task to clean up when shutdown is called */
8319 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8321 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8322 GNUNET_free (keyfile);
8327 * The main function for the mesh service.
8329 * @param argc number of arguments from the command line
8330 * @param argv command line arguments
8331 * @return 0 ok, 1 on error
8334 main (int argc, char *const *argv)
8339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8340 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8342 ret = (GNUNET_OK == r) ? 0 : 1;
8343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8348 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8349 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);