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.
520 * Maximum PID allowed (FWD ACK received).
525 * Last ACK sent to that child (BCK ACK).
530 * Circular buffer pointing to MeshPeerQueue elements for all
531 * payload traffic going to this child.
532 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
534 struct MeshPeerQueue **send_buffer;
537 * Index of the oldest element in the send_buffer.
539 unsigned int send_buffer_start;
542 * How many elements are already in the buffer.
544 unsigned int send_buffer_n;
547 * Tunnel this info is about
549 struct MeshTunnel *t;
552 * Task to poll peer in case of a stall.
554 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
559 * Info about a leaf client of a tunnel, needed to perform flow control.
561 struct MeshTunnelClientInfo
564 * PID of the last packet sent to the client (FWD).
569 * PID of the last packet received from the client (BCK).
574 * Maximum PID allowed (FWD ACK received).
579 * Last ACK sent to that child (BCK ACK).
587 * Info collected during iteration of child nodes in order to get the ACK value
590 struct MeshTunnelChildIteratorContext
593 * Tunnel whose info is being collected.
595 struct MeshTunnel *t;
598 * Is this context initialized? Is the value in max_child_ack valid?
603 * Maximum child ACK so far.
605 uint32_t max_child_ack;
608 * Number of children nodes
610 unsigned int nchildren;
615 * Info needed to work with tunnel paths and peers
622 struct MeshTunnel *t;
625 * Neighbouring peer to whom we send the packet to
627 struct MeshPeerInfo *peer;
632 struct MeshPeerPath *path;
637 * Struct containing information about a client of the service
644 struct MeshClient *next;
649 struct MeshClient *prev;
652 * Tunnels that belong to this client, indexed by local id
654 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
657 * Tunnels this client has accepted, indexed by incoming local id
659 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
662 * Tunnels this client has rejected, indexed by incoming local id
664 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
666 * Handle to communicate with the client
668 struct GNUNET_SERVER_Client *handle;
671 * Applications that this client has claimed to provide
673 struct GNUNET_CONTAINER_MultiHashMap *apps;
676 * Messages that this client has declared interest in
678 struct GNUNET_CONTAINER_MultiHashMap *types;
681 * Whether the client is active or shutting down (don't send confirmations
682 * to a client that is shutting down.
687 * ID of the client, mainly for debug messages
692 * Regular expressions describing the services offered by this client.
694 char **regexes; // FIXME add timeout? API to remove a regex?
697 * Number of regular expressions in regexes.
699 unsigned int n_regex;
702 * Task to refresh all regular expresions in the DHT.
704 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
710 * Struct to keep information of searches of services described by a regex
711 * using a user-provided string service description.
713 struct MeshRegexSearchInfo
716 * Which tunnel is this for
718 struct MeshTunnel *t;
721 * User provided description of the searched service.
726 * Part of the description already consumed by the search.
733 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
736 * Results from running DHT GETs.
738 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
741 * Contexts, for each running DHT GET. Free all on end of search.
743 struct MeshRegexSearchContext **contexts;
746 * Number of contexts (branches/steps in search).
748 unsigned int n_contexts;
751 * Peer that is connecting via connect_by_string. When connected, free ctx.
756 * Other peers that are found but not yet being connected to.
758 GNUNET_PEER_Id *peers;
761 * Number of elements in peers.
763 unsigned int n_peers;
766 * Next peer to try to connect to.
771 * Timeout for a connect attempt.
772 * When reached, try to connect to a different peer, if any. If not,
773 * try the same peer again.
775 GNUNET_SCHEDULER_TaskIdentifier timeout;
780 * Struct to keep state of running searches that have consumed a part of
783 struct MeshRegexSearchContext
786 * Part of the description already consumed by
787 * this particular search branch.
792 * Information about the search.
794 struct MeshRegexSearchInfo *info;
797 * We just want to look for one edge, the longer the better.
800 unsigned int longest_match;
803 * Destination hash of the longest match.
805 struct GNUNET_HashCode hash;
808 /******************************************************************************/
809 /************************ DEBUG FUNCTIONS ****************************/
810 /******************************************************************************/
814 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
815 * @param cls string to print
816 * @param success GNUNET_OK if the PUT was transmitted,
817 * GNUNET_NO on timeout,
818 * GNUNET_SYSERR on disconnect from service
819 * after the PUT message was transmitted
820 * (so we don't know if it was received or not)
825 mesh_debug (void *cls, int success)
829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
833 unsigned int debug_fwd_ack;
834 unsigned int debug_bck_ack;
838 /******************************************************************************/
839 /*********************** GLOBAL VARIABLES ****************************/
840 /******************************************************************************/
843 * Configuration parameters
845 static struct GNUNET_TIME_Relative refresh_path_time;
846 static struct GNUNET_TIME_Relative app_announce_time;
847 static struct GNUNET_TIME_Relative id_announce_time;
848 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
849 static struct GNUNET_TIME_Relative connect_timeout;
850 static unsigned long long default_ttl;
851 static unsigned long long dht_replication_level;
852 static unsigned long long max_tunnels;
853 static unsigned long long max_msgs_queue;
857 * Hostkey generation context
859 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
862 * DLL with all the clients, head.
864 static struct MeshClient *clients;
867 * DLL with all the clients, tail.
869 static struct MeshClient *clients_tail;
872 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
874 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
877 * Number of tunnels known.
879 static unsigned long long n_tunnels;
882 * Tunnels incoming, indexed by MESH_TunnelNumber
883 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
885 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
888 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
890 static struct GNUNET_CONTAINER_MultiHashMap *peers;
893 * Handle to communicate with transport
895 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
898 * Handle to communicate with core.
900 static struct GNUNET_CORE_Handle *core_handle;
905 static struct GNUNET_DHT_Handle *dht_handle;
910 static struct GNUNET_SERVER_Handle *server_handle;
913 * Handle to the statistics service.
915 static struct GNUNET_STATISTICS_Handle *stats;
918 * Notification context, to send messages to local clients.
920 static struct GNUNET_SERVER_NotificationContext *nc;
923 * Local peer own ID (memory efficient handle).
925 static GNUNET_PEER_Id myid;
928 * Local peer own ID (full value).
930 static struct GNUNET_PeerIdentity my_full_id;
935 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
940 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
943 * Tunnel ID for the next created tunnel (global tunnel number).
945 static MESH_TunnelNumber next_tid;
948 * Tunnel ID for the next incoming tunnel (local tunnel number).
950 static MESH_TunnelNumber next_local_tid;
953 * All application types provided by this peer.
955 static struct GNUNET_CONTAINER_MultiHashMap *applications;
958 * All message types clients of this peer are interested in.
960 static struct GNUNET_CONTAINER_MultiHashMap *types;
963 * Task to periodically announce provided applications.
965 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
968 * Task to periodically announce itself in the network.
970 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
973 * Next ID to assign to a client.
975 unsigned int next_client_id;
978 /******************************************************************************/
979 /*********************** DECLARATIONS **************************/
980 /******************************************************************************/
983 * Function to process paths received for a new peer addition. The recorded
984 * paths form the initial tunnel, which can be optimized later.
985 * Called on each result obtained for the DHT search.
988 * @param exp when will this value expire
989 * @param key key of the result
990 * @param type type of the result
991 * @param size number of bytes in data
992 * @param data pointer to the result data
995 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
996 const struct GNUNET_HashCode * key,
997 const struct GNUNET_PeerIdentity *get_path,
998 unsigned int get_path_length,
999 const struct GNUNET_PeerIdentity *put_path,
1000 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1001 size_t size, const void *data);
1005 * Function to process DHT string to regex matching.
1006 * Called on each result obtained for the DHT search.
1008 * @param cls closure (search context)
1009 * @param exp when will this value expire
1010 * @param key key of the result
1011 * @param get_path path of the get request (not used)
1012 * @param get_path_length lenght of get_path (not used)
1013 * @param put_path path of the put request (not used)
1014 * @param put_path_length length of the put_path (not used)
1015 * @param type type of the result
1016 * @param size number of bytes in data
1017 * @param data pointer to the result data
1019 * TODO: re-issue the request after certain time? cancel after X results?
1022 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1023 const struct GNUNET_HashCode * key,
1024 const struct GNUNET_PeerIdentity *get_path,
1025 unsigned int get_path_length,
1026 const struct GNUNET_PeerIdentity *put_path,
1027 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1028 size_t size, const void *data);
1032 * Function to process DHT string to regex matching.
1033 * Called on each result obtained for the DHT search.
1035 * @param cls closure (search context)
1036 * @param exp when will this value expire
1037 * @param key key of the result
1038 * @param get_path path of the get request (not used)
1039 * @param get_path_length lenght of get_path (not used)
1040 * @param put_path path of the put request (not used)
1041 * @param put_path_length length of the put_path (not used)
1042 * @param type type of the result
1043 * @param size number of bytes in data
1044 * @param data pointer to the result data
1047 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1048 const struct GNUNET_HashCode * key,
1049 const struct GNUNET_PeerIdentity *get_path,
1050 unsigned int get_path_length,
1051 const struct GNUNET_PeerIdentity *put_path,
1052 unsigned int put_path_length,
1053 enum GNUNET_BLOCK_Type type,
1054 size_t size, const void *data);
1058 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1059 * and insert it in the appropiate structures if the peer is not known yet.
1061 * @param peer Short identity of the peer.
1063 * @return Existing or newly created peer info.
1065 static struct MeshPeerInfo *
1066 peer_info_get_short (const GNUNET_PEER_Id peer);
1070 * Try to establish a new connection to this peer.
1071 * Use the best path for the given tunnel.
1072 * If the peer doesn't have any path to it yet, try to get one.
1073 * If the peer already has some path, send a CREATE PATH towards it.
1075 * @param peer PeerInfo of the peer.
1076 * @param t Tunnel for which to create the path, if possible.
1079 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1083 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1084 * needed rescources.
1085 * If peer already exists, reevaluate shortest path and change if different.
1087 * @param t Tunnel we want to add a new peer to
1088 * @param peer PeerInfo of the peer being added
1092 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1096 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1097 * that are no longer needed, as well as nodes of no longer reachable peers.
1098 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1100 * @param t Tunnel from which to remove the path.
1101 * @param peer Short id of the peer which should be removed.
1104 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1108 * Search for a tunnel by global ID using full PeerIdentities.
1110 * @param oid owner of the tunnel.
1111 * @param tid global tunnel number.
1113 * @return tunnel handler, NULL if doesn't exist.
1115 static struct MeshTunnel *
1116 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1120 * Delete an active client from the tunnel.
1126 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1129 * Notify a tunnel that a connection has broken that affects at least
1130 * some of its peers.
1132 * @param t Tunnel affected.
1133 * @param p1 Peer that got disconnected from p2.
1134 * @param p2 Peer that got disconnected from p1.
1136 * @return Short ID of the peer disconnected (either p1 or p2).
1137 * 0 if the tunnel remained unaffected.
1139 static GNUNET_PEER_Id
1140 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1145 * Get the current ack value for a tunnel, for data going from root to leaves,
1146 * taking in account the tunnel mode and the status of all children and clients.
1150 * @return Maximum PID allowed.
1153 tunnel_get_fwd_ack (struct MeshTunnel *t);
1157 * Add a client to a tunnel, initializing all needed data structures.
1159 * @param t Tunnel to which add the client.
1160 * @param c Client which to add to the tunnel.
1163 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1167 * Jump to the next edge, with the longest matching token.
1169 * @param block Block found in the DHT.
1170 * @param size Size of the block.
1171 * @param ctx Context of the search.
1173 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1176 regex_next_edge (const struct MeshRegexBlock *block,
1178 struct MeshRegexSearchContext *ctx);
1182 * Find a path to a peer that offers a regex servcie compatible
1183 * with a given string.
1185 * @param key The key of the accepting state.
1186 * @param ctx Context containing info about the string, tunnel, etc.
1189 regex_find_path (const struct GNUNET_HashCode *key,
1190 struct MeshRegexSearchContext *ctx);
1194 * @brief Queue and pass message to core when possible.
1196 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1197 * and accounts for it. In case the queue is full, the message is dropped and
1200 * Otherwise, message is treated as internal and allowed to go regardless of
1203 * @param cls Closure (@c type dependant). It will be used by queue_send to
1204 * build the message to be sent if not already prebuilt.
1205 * @param type Type of the message, 0 for a raw message.
1206 * @param size Size of the message.
1207 * @param dst Neighbor to send message to.
1208 * @param t Tunnel this message belongs to.
1211 queue_add (void *cls, uint16_t type, size_t size,
1212 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1216 * Free a transmission that was already queued with all resources
1217 * associated to the request.
1219 * @param queue Queue handler to cancel.
1220 * @param clear_cls Is it necessary to free associated cls?
1223 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1227 * @brief Get the next transmittable message from the queue.
1229 * This will be the head, except in the case of being a data packet
1230 * not allowed by the destination peer.
1232 * @param peer Destination peer.
1234 * @return The next viable MeshPeerQueue element to send to that peer.
1235 * NULL when there are no transmittable messages.
1237 struct MeshPeerQueue *
1238 queue_get_next (const struct MeshPeerInfo *peer);
1242 * Core callback to write a queued packet to core buffer
1244 * @param cls Closure (peer info).
1245 * @param size Number of bytes available in buf.
1246 * @param buf Where the to write the message.
1248 * @return number of bytes written to buf
1251 queue_send (void *cls, size_t size, void *buf);
1253 /******************************************************************************/
1254 /************************ ITERATORS ****************************/
1255 /******************************************************************************/
1258 * Iterator over found existing mesh regex blocks that match an ongoing search.
1260 * @param cls closure
1261 * @param key current key code
1262 * @param value value in the hash map
1263 * @return GNUNET_YES if we should continue to iterate,
1267 regex_result_iterator (void *cls,
1268 const struct GNUNET_HashCode * key,
1271 struct MeshRegexBlock *block = value;
1272 struct MeshRegexSearchContext *ctx = cls;
1274 if (GNUNET_YES == ntohl(block->accepting) &&
1275 ctx->position == strlen (ctx->info->description))
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1278 regex_find_path (key, ctx);
1279 return GNUNET_YES; // We found an accept state!
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1284 ctx->position, strlen(ctx->info->description),
1285 ntohl(block->accepting));
1288 regex_next_edge(block, SIZE_MAX, ctx);
1295 * Iterator over edges in a regex block retrieved from the DHT.
1297 * @param cls Closure (context of the search).
1298 * @param token Token that follows to next state.
1299 * @param len Lenght of token.
1300 * @param key Hash of next state.
1302 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1305 regex_edge_iterator (void *cls,
1308 const struct GNUNET_HashCode *key)
1310 struct MeshRegexSearchContext *ctx = cls;
1311 struct MeshRegexSearchInfo *info = ctx->info;
1315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1318 current = &info->description[ctx->position];
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1320 current_len = strlen (info->description) - ctx->position;
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1325 if (len > current_len)
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1328 return GNUNET_YES; // Token too long, wont match
1330 if (0 != strncmp (current, token, len))
1332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1333 return GNUNET_YES; // Token doesn't match
1336 if (len > ctx->longest_match)
1338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1339 ctx->longest_match = len;
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1353 * Jump to the next edge, with the longest matching token.
1355 * @param block Block found in the DHT.
1356 * @param size Size of the block.
1357 * @param ctx Context of the search.
1359 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1362 regex_next_edge (const struct MeshRegexBlock *block,
1364 struct MeshRegexSearchContext *ctx)
1366 struct MeshRegexSearchContext *new_ctx;
1367 struct MeshRegexSearchInfo *info = ctx->info;
1368 struct GNUNET_DHT_GetHandle *get_h;
1372 /* Find the longest match for the current string position,
1373 * among tokens in the given block */
1374 ctx->longest_match = 0;
1375 result = GNUNET_MESH_regex_block_iterate (block, size,
1376 ®ex_edge_iterator, ctx);
1377 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1379 /* Did anything match? */
1380 if (0 == ctx->longest_match)
1383 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1384 new_ctx->info = info;
1385 new_ctx->position = ctx->position + ctx->longest_match;
1386 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1388 /* Check whether we already have a DHT GET running for it */
1390 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1393 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1395 ®ex_result_iterator,
1397 return; // We are already looking for it
1400 /* Start search in DHT */
1402 GNUNET_DHT_get_start (dht_handle, /* handle */
1403 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1404 &ctx->hash, /* key to search */
1405 dht_replication_level, /* replication level */
1406 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1407 NULL, /* xquery */ // FIXME BLOOMFILTER
1408 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1409 &dht_get_string_handler, new_ctx);
1411 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1414 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1423 * Iterator over hash map entries to cancel DHT GET requests after a
1424 * successful connect_by_string.
1426 * @param cls Closure (unused).
1427 * @param key Current key code (unused).
1428 * @param value Value in the hash map (get handle).
1429 * @return GNUNET_YES if we should continue to iterate,
1433 regex_cancel_dht_get (void *cls,
1434 const struct GNUNET_HashCode * key,
1437 struct GNUNET_DHT_GetHandle *h = value;
1439 GNUNET_DHT_get_stop (h);
1445 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1446 * search for connect_by_string.
1448 * @param cls Closure (unused).
1449 * @param key Current key code (unused).
1450 * @param value MeshRegexBlock in the hash map.
1451 * @return GNUNET_YES if we should continue to iterate,
1455 regex_free_result (void *cls,
1456 const struct GNUNET_HashCode * key,
1460 GNUNET_free (value);
1466 * Regex callback iterator to store own service description in the DHT.
1468 * @param cls closure.
1469 * @param key hash for current state.
1470 * @param proof proof for current state.
1471 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1472 * @param num_edges number of edges leaving current state.
1473 * @param edges edges leaving current state.
1476 regex_iterator (void *cls,
1477 const struct GNUNET_HashCode *key,
1480 unsigned int num_edges,
1481 const struct GNUNET_REGEX_Edge *edges)
1483 struct MeshRegexBlock *block;
1484 struct MeshRegexEdge *block_edge;
1485 enum GNUNET_DHT_RouteOption opt;
1489 unsigned int offset;
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 " regex dht put for state %s\n",
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1502 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1503 if (GNUNET_YES == accepting)
1505 struct MeshRegexAccept block;
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 " state %s is accepting, putting own id\n",
1510 size = sizeof (block);
1512 block.id = my_full_id;
1514 GNUNET_DHT_put(dht_handle, key,
1515 dht_replication_level,
1516 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1517 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1520 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1525 len = strlen(proof);
1526 size = sizeof (struct MeshRegexBlock) + len;
1527 block = GNUNET_malloc (size);
1530 block->n_proof = htonl (len);
1531 block->n_edges = htonl (num_edges);
1532 block->accepting = htonl (accepting);
1534 /* Store the proof at the end of the block. */
1535 aux = (char *) &block[1];
1536 memcpy (aux, proof, len);
1539 /* Store each edge in a variable length MeshEdge struct at the
1540 * very end of the MeshRegexBlock structure.
1542 for (i = 0; i < num_edges; i++)
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1545 " edge %s towards %s\n",
1547 GNUNET_h2s(&edges[i].destination));
1549 /* aux points at the end of the last block */
1550 len = strlen (edges[i].label);
1551 size += sizeof (struct MeshRegexEdge) + len;
1552 // Calculate offset FIXME is this ok? use size instead?
1553 offset = aux - (char *) block;
1554 block = GNUNET_realloc (block, size);
1555 aux = &((char *) block)[offset];
1556 block_edge = (struct MeshRegexEdge *) aux;
1557 block_edge->key = edges[i].destination;
1558 block_edge->n_token = htonl (len);
1559 aux = (char *) &block_edge[1];
1560 memcpy (aux, edges[i].label, len);
1564 GNUNET_DHT_put(dht_handle, key,
1565 dht_replication_level,
1567 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1569 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1573 GNUNET_free (block);
1578 * Store the regular expression describing a local service into the DHT.
1580 * @param regex The regular expresion.
1583 regex_put (const char *regex)
1585 struct GNUNET_REGEX_Automaton *dfa;
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1588 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1589 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1590 GNUNET_REGEX_automaton_destroy (dfa);
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1596 * Find a path to a peer that offers a regex servcie compatible
1597 * with a given string.
1599 * @param key The key of the accepting state.
1600 * @param ctx Context containing info about the string, tunnel, etc.
1603 regex_find_path (const struct GNUNET_HashCode *key,
1604 struct MeshRegexSearchContext *ctx)
1606 struct GNUNET_DHT_GetHandle *get_h;
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1609 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1610 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1611 key, /* key to search */
1612 dht_replication_level, /* replication level */
1613 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1614 GNUNET_DHT_RO_RECORD_ROUTE,
1615 NULL, /* xquery */ // FIXME BLOOMFILTER
1616 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1617 &dht_get_string_accept_handler, ctx);
1618 GNUNET_break (GNUNET_OK ==
1619 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1627 * Function called if the connect attempt to a peer found via
1628 * connect_by_string times out. Try to connect to another peer, if any.
1629 * Otherwise try to reconnect to the same peer.
1631 * @param cls Closure (info about regex search).
1632 * @param tc TaskContext.
1635 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1637 struct MeshRegexSearchInfo *info = cls;
1638 struct MeshPeerInfo *peer_info;
1642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1643 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1644 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1652 if (0 < info->n_peers)
1654 // Select next peer, put current in that spot.
1655 id = info->peers[info->i_peer];
1656 info->peers[info->i_peer] = info->peer;
1657 info->i_peer = (info->i_peer + 1) % info->n_peers;
1661 // Try to connect to same peer again.
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1666 peer_info = peer_info_get_short(id);
1667 tunnel_add_peer (info->t, peer_info);
1669 tunnel_delete_peer (info->t, old);
1670 peer_info_connect (peer_info, info->t);
1671 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1672 ®ex_connect_timeout,
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1679 * Cancel an ongoing regex search in the DHT and free all resources.
1681 * @param ctx The search context.
1684 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1686 struct MeshRegexSearchInfo *info = ctx->info;
1689 GNUNET_free (info->description);
1690 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1691 ®ex_cancel_dht_get, NULL);
1692 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1693 ®ex_free_result, NULL);
1694 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1695 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1696 info->t->regex_ctx = NULL;
1697 for (i = 0; i < info->n_contexts; i++)
1699 GNUNET_free (info->contexts[i]);
1701 if (0 < info->n_contexts)
1702 GNUNET_free (info->contexts);
1703 if (0 < info->n_peers)
1704 GNUNET_free (info->peers);
1705 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1707 GNUNET_SCHEDULER_cancel(info->timeout);
1713 /******************************************************************************/
1714 /************************ PERIODIC FUNCTIONS ****************************/
1715 /******************************************************************************/
1718 * Announce iterator over for each application provided by the peer
1720 * @param cls closure
1721 * @param key current key code
1722 * @param value value in the hash map
1723 * @return GNUNET_YES if we should continue to
1728 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1730 struct PBlock block;
1731 struct MeshClient *c;
1733 block.id = my_full_id;
1734 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1735 GNUNET_assert(NULL != c);
1736 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1737 if (0 == block.type)
1742 block.type = htonl (block.type);
1744 GNUNET_break (NULL !=
1745 GNUNET_DHT_put (dht_handle, key,
1746 dht_replication_level,
1747 GNUNET_DHT_RO_RECORD_ROUTE |
1748 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1749 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1751 (const char *) &block,
1752 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1754 app_announce_time, NULL, NULL));
1760 * Periodically announce what applications are provided by local clients
1763 * @param cls closure
1764 * @param tc task context
1767 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1769 struct MeshClient *c = cls;
1772 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1774 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1778 DEBUG_DHT ("Starting PUT for regex\n");
1780 for (i = 0; i < c->n_regex; i++)
1782 regex_put (c->regexes[i]);
1784 c->regex_announce_task =
1785 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1786 DEBUG_DHT ("Finished PUT for regex\n");
1793 * Periodically announce what applications are provided by local clients
1796 * @param cls closure
1797 * @param tc task context
1800 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1802 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1804 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1808 DEBUG_DHT ("Starting PUT for apps\n");
1810 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1812 announce_applications_task =
1813 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1815 DEBUG_DHT ("Finished PUT for apps\n");
1822 * Periodically announce self id in the DHT
1824 * @param cls closure
1825 * @param tc task context
1828 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1830 struct PBlock block;
1832 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1834 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1838 * - Set data expiration in function of X
1839 * - Adapt X to churn
1841 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1843 block.id = my_full_id;
1844 block.type = htonl (0);
1845 GNUNET_DHT_put (dht_handle, /* DHT handle */
1846 &my_full_id.hashPubKey, /* Key to use */
1847 dht_replication_level, /* Replication level */
1848 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1849 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1850 sizeof (block), /* Size of the data */
1851 (const char *) &block, /* Data itself */
1852 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1853 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1854 NULL, /* Continuation */
1855 NULL); /* Continuation closure */
1857 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1861 /******************************************************************************/
1862 /****************** GENERAL HELPER FUNCTIONS ************************/
1863 /******************************************************************************/
1866 * Decrements the reference counter and frees all resources if needed
1868 * @param mesh_data Data Descriptor used in a multicast message.
1869 * Freed no longer needed (last message).
1872 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1874 if (0 == --(mesh_data->reference_counter))
1876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1877 GNUNET_free (mesh_data->data);
1878 GNUNET_free (mesh_data);
1884 * Check if client has registered with the service and has not disconnected
1886 * @param client the client to check
1888 * @return non-NULL if client exists in the global DLL
1890 static struct MeshClient *
1891 client_get (struct GNUNET_SERVER_Client *client)
1893 struct MeshClient *c;
1898 if (c->handle == client)
1907 * Checks if a given client has subscribed to certain message type
1909 * @param message_type Type of message to check
1910 * @param c Client to check
1912 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1914 * FIXME: use of crypto_hash slows it down
1915 * The hash function alone takes 8-10us out of the ~55us for the whole
1916 * process of retransmitting the message from one local client to another.
1917 * Find faster implementation!
1920 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1922 struct GNUNET_HashCode hc;
1924 if (NULL == c->types)
1927 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1928 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1933 * Check whether client wants traffic from a tunnel.
1935 * @param c Client to check.
1936 * @param t Tunnel to be found.
1938 * @return GNUNET_YES if client knows tunnel.
1940 * TODO look in client hashmap
1943 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1947 for (i = 0; i < t->nclients; i++)
1948 if (t->clients[i] == c)
1955 * Check whether client has been informed about a tunnel.
1957 * @param c Client to check.
1958 * @param t Tunnel to be found.
1960 * @return GNUNET_YES if client knows tunnel.
1962 * TODO look in client hashmap
1965 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1969 for (i = 0; i < t->nignore; i++)
1970 if (t->ignore[i] == c)
1972 return client_wants_tunnel(c, t);
1977 * Marks a client as uninterested in traffic from the tunnel, updating both
1978 * client and tunnel to reflect this.
1980 * @param c Client that doesn't want traffic anymore.
1981 * @param t Tunnel which should be ignored.
1983 * FIXME when to delete an incoming tunnel?
1986 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1988 struct GNUNET_HashCode hash;
1990 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1991 GNUNET_break (GNUNET_YES ==
1992 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1994 GNUNET_break (GNUNET_YES ==
1995 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1996 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1997 tunnel_delete_active_client (t, c);
1998 GNUNET_array_append (t->ignore, t->nignore, c);
2003 * Deletes a tunnel from a client (either owner or destination). To be used on
2004 * tunnel destroy, otherwise, use client_ignore_tunnel.
2006 * @param c Client whose tunnel to delete.
2007 * @param t Tunnel which should be deleted.
2010 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2012 struct GNUNET_HashCode hash;
2016 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2017 GNUNET_assert (GNUNET_YES ==
2018 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2024 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2026 GNUNET_assert (GNUNET_YES ==
2027 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2031 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2039 * Send the message to all clients that have subscribed to its type
2041 * @param msg Pointer to the message itself
2042 * @param payload Pointer to the payload of the message.
2043 * @param t The tunnel to whose clients this message goes.
2045 * @return number of clients this message was sent to
2048 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2049 const struct GNUNET_MessageHeader *payload,
2050 struct MeshTunnel *t)
2052 struct MeshClient *c;
2053 MESH_TunnelNumber *tid;
2056 char cbuf[htons (msg->size)];
2058 type = ntohs (payload->type);
2059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2061 GNUNET_MESH_DEBUG_M2S (type));
2063 memcpy (cbuf, msg, sizeof (cbuf));
2064 switch (htons (msg->type))
2066 struct GNUNET_MESH_Unicast *uc;
2067 struct GNUNET_MESH_Multicast *mc;
2068 struct GNUNET_MESH_ToOrigin *to;
2070 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2071 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2074 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2075 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2078 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2079 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2087 for (count = 0, c = clients; c != NULL; c = c->next)
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2090 if (client_is_subscribed (type, c))
2092 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2096 *tid = htonl (t->local_tid);
2100 if (GNUNET_NO == client_knows_tunnel (c, t))
2102 /* This client doesn't know the tunnel */
2103 struct GNUNET_MESH_TunnelNotification tmsg;
2104 struct GNUNET_HashCode hash;
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2107 tmsg.header.size = htons (sizeof (tmsg));
2108 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2109 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2110 tmsg.tunnel_id = htonl (t->local_tid_dest);
2112 if (GNUNET_YES == t->speed_min)
2113 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2114 if (GNUNET_YES == t->nobuffer)
2115 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2116 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2117 &tmsg.header, GNUNET_NO);
2118 tunnel_add_client (t, c);
2119 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2121 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2122 c->incoming_tunnels, &hash, t,
2123 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2125 *tid = htonl (t->local_tid_dest);
2128 /* Check if the client wants to get traffic from the tunnel */
2129 if (GNUNET_NO == client_wants_tunnel(c, t))
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2133 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2134 (struct GNUNET_MessageHeader
2135 *) cbuf, GNUNET_NO);
2144 * Notify the client that owns the tunnel that a peer has connected to it
2145 * (the requested path to it has been confirmed).
2147 * @param t Tunnel whose owner to notify
2148 * @param id Short id of the peer that has connected
2151 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2153 struct GNUNET_MESH_PeerControl pc;
2155 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2156 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2157 pc.tunnel_id = htonl (t->local_tid);
2158 GNUNET_PEER_resolve (id, &pc.peer);
2159 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2165 * Notify all clients (not depending on registration status) that the incoming
2166 * tunnel is no longer valid.
2168 * @param t Tunnel that was destroyed.
2171 send_clients_tunnel_destroy (struct MeshTunnel *t)
2173 struct GNUNET_MESH_TunnelMessage msg;
2175 msg.header.size = htons (sizeof (msg));
2176 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2177 msg.tunnel_id = htonl (t->local_tid_dest);
2178 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2183 * Notify clients of tunnel disconnections, if needed.
2184 * In case the origin disconnects, the destination clients get a tunnel destroy
2185 * notification. If the last destination disconnects (only one remaining client
2186 * in tunnel), the origin gets a (local ID) peer disconnected.
2187 * Note that the function must be called BEFORE removing the client from
2190 * @param t Tunnel that was destroyed.
2191 * @param c Client that disconnected.
2194 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2200 struct GNUNET_MESH_TunnelMessage msg;
2202 msg.header.size = htons (sizeof (msg));
2203 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2204 msg.tunnel_id = htonl (t->local_tid_dest);
2205 for (i = 0; i < t->nclients; i++)
2206 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2207 &msg.header, GNUNET_NO);
2209 // FIXME when to disconnect an incoming tunnel?
2210 else if (1 == t->nclients && NULL != t->owner)
2212 struct GNUNET_MESH_PeerControl msg;
2214 msg.header.size = htons (sizeof (msg));
2215 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2216 msg.tunnel_id = htonl (t->local_tid);
2217 msg.peer = my_full_id;
2218 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2219 &msg.header, GNUNET_NO);
2225 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2226 * and insert it in the appropiate structures if the peer is not known yet.
2228 * @param peer Full identity of the peer.
2230 * @return Existing or newly created peer info.
2232 static struct MeshPeerInfo *
2233 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2235 struct MeshPeerInfo *peer_info;
2237 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2238 if (NULL == peer_info)
2241 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2242 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2243 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2244 peer_info->id = GNUNET_PEER_intern (peer);
2252 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2253 * and insert it in the appropiate structures if the peer is not known yet.
2255 * @param peer Short identity of the peer.
2257 * @return Existing or newly created peer info.
2259 static struct MeshPeerInfo *
2260 peer_info_get_short (const GNUNET_PEER_Id peer)
2262 struct GNUNET_PeerIdentity id;
2264 GNUNET_PEER_resolve (peer, &id);
2265 return peer_info_get (&id);
2270 * Iterator to remove the tunnel from the list of tunnels a peer participates
2273 * @param cls Closure (tunnel info)
2274 * @param key GNUNET_PeerIdentity of the peer (unused)
2275 * @param value PeerInfo of the peer
2277 * @return always GNUNET_YES, to keep iterating
2280 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2282 struct MeshTunnel *t = cls;
2283 struct MeshPeerInfo *peer = value;
2286 for (i = 0; i < peer->ntunnels; i++)
2289 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2292 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2293 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2302 * Core callback to write a pre-constructed data packet to core buffer
2304 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2305 * @param size Number of bytes available in buf.
2306 * @param buf Where the to write the message.
2308 * @return number of bytes written to buf
2311 send_core_data_raw (void *cls, size_t size, void *buf)
2313 struct MeshTransmissionDescriptor *info = cls;
2314 struct GNUNET_MessageHeader *msg;
2317 GNUNET_assert (NULL != info);
2318 GNUNET_assert (NULL != info->mesh_data);
2319 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2320 total_size = ntohs (msg->size);
2322 if (total_size > size)
2327 memcpy (buf, msg, total_size);
2328 data_descriptor_decrement_rc (info->mesh_data);
2335 * Sends an already built non-multicast message to a peer,
2336 * properly registrating all used resources.
2338 * @param message Message to send. Function makes a copy of it.
2339 * @param peer Short ID of the neighbor whom to send the message.
2340 * @param t Tunnel on which this message is transmitted.
2343 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2344 const struct GNUNET_PeerIdentity *peer,
2345 struct MeshTunnel *t)
2347 struct MeshTransmissionDescriptor *info;
2348 struct MeshPeerInfo *neighbor;
2349 struct MeshPeerPath *p;
2353 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2355 size = ntohs (message->size);
2356 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2357 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2358 info->mesh_data->data = GNUNET_malloc (size);
2359 memcpy (info->mesh_data->data, message, size);
2360 type = ntohs(message->type);
2363 struct GNUNET_MESH_Unicast *m;
2364 struct GNUNET_MESH_ToOrigin *to;
2366 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2367 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2368 m->ttl = htonl (ntohl (m->ttl) - 1);
2370 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2371 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2373 to->pid = htonl(t->bck_pid);
2375 info->mesh_data->data_len = size;
2376 info->mesh_data->reference_counter = 1;
2377 info->mesh_data->total_out = 1;
2378 neighbor = peer_info_get (peer);
2379 for (p = neighbor->path_head; NULL != p; p = p->next)
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 " %s IS NOT DIRECTLY CONNECTED\n",
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 for (p = neighbor->path_head; NULL != p; p = p->next)
2397 struct GNUNET_PeerIdentity debug_id;
2400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2401 " path with %u hops through:\n",
2403 for (i = 0; i < p->length; i++)
2405 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 i, GNUNET_i2s(&debug_id));
2412 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2413 GNUNET_free (info->mesh_data->data);
2414 GNUNET_free (info->mesh_data);
2418 info->peer = neighbor;
2419 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2430 * Sends a CREATE PATH message for a path to a peer, properly registrating
2431 * all used resources.
2433 * @param peer PeerInfo of the final peer for whom this path is being created.
2434 * @param p Path itself.
2435 * @param t Tunnel for which the path is created.
2438 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2439 struct MeshTunnel *t)
2441 struct GNUNET_PeerIdentity id;
2442 struct MeshPathInfo *path_info;
2443 struct MeshPeerInfo *neighbor;
2449 p = tree_get_path_to_peer (t->tree, peer->id);
2456 for (i = 0; i < p->length; i++)
2458 if (p->peers[i] == myid)
2461 if (i >= p->length - 1)
2467 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2469 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2470 path_info->path = p;
2472 neighbor = peer_info_get (&id);
2473 path_info->peer = neighbor;
2474 queue_add (path_info,
2475 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2476 sizeof (struct GNUNET_MESH_ManipulatePath) +
2477 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2484 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2486 * @param t Tunnel whose path to destroy.
2487 * @param destination Short ID of the peer to whom the path to destroy.
2490 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2492 struct MeshPeerPath *p;
2495 p = tree_get_path_to_peer (t->tree, destination);
2501 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2502 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2504 struct GNUNET_MESH_ManipulatePath *msg;
2505 struct GNUNET_PeerIdentity *pi;
2509 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2510 msg->header.size = htons (size);
2511 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2512 msg->tid = htonl (t->id.tid);
2513 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2514 for (i = 0; i < p->length; i++)
2516 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2518 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2525 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2527 * @param t Tunnel which to confirm.
2530 send_path_ack (struct MeshTunnel *t)
2532 struct MeshTransmissionDescriptor *info;
2533 struct GNUNET_PeerIdentity id;
2534 GNUNET_PEER_Id peer;
2536 peer = tree_get_predecessor (t->tree);
2537 GNUNET_PEER_resolve (peer, &id);
2538 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2539 info->origin = &t->id;
2540 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2541 GNUNET_assert (NULL != info->peer);
2544 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2545 sizeof (struct GNUNET_MESH_PathACK),
2552 * Try to establish a new connection to this peer.
2553 * Use the best path for the given tunnel.
2554 * If the peer doesn't have any path to it yet, try to get one.
2555 * If the peer already has some path, send a CREATE PATH towards it.
2557 * @param peer PeerInfo of the peer.
2558 * @param t Tunnel for which to create the path, if possible.
2561 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2563 struct MeshPeerPath *p;
2564 struct MeshPathInfo *path_info;
2566 if (NULL != peer->path_head)
2568 p = tree_get_path_to_peer (t->tree, peer->id);
2575 // FIXME always send create path to self
2578 send_create_path (peer, p, t);
2582 struct GNUNET_HashCode hash;
2585 send_client_peer_connected (t, myid);
2586 t->local_tid_dest = next_local_tid++;
2587 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2590 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2591 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2598 else if (NULL == peer->dhtget)
2600 struct GNUNET_PeerIdentity id;
2602 GNUNET_PEER_resolve (peer->id, &id);
2603 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2604 path_info->peer = peer;
2606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2607 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2608 peer->dhtgetcls = path_info;
2609 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2610 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2611 &id.hashPubKey, /* key to search */
2612 dht_replication_level, /* replication level */
2613 GNUNET_DHT_RO_RECORD_ROUTE |
2614 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2615 NULL, /* xquery */ // FIXME BLOOMFILTER
2616 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2617 &dht_get_id_handler, path_info);
2619 /* Otherwise, there is no path but the DHT get is already started. */
2624 * Task to delay the connection of a peer
2626 * @param cls Closure (path info with tunnel and peer to connect).
2627 * Will be free'd on exection.
2628 * @param tc TaskContext
2631 peer_info_connect_task (void *cls,
2632 const struct GNUNET_SCHEDULER_TaskContext *tc)
2634 struct MeshPathInfo *path_info = cls;
2636 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2638 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2643 peer_info_connect (path_info->peer, path_info->t);
2649 * Destroy the peer_info and free any allocated resources linked to it
2651 * @param pi The peer_info to destroy.
2653 * @return GNUNET_OK on success
2656 peer_info_destroy (struct MeshPeerInfo *pi)
2658 struct GNUNET_PeerIdentity id;
2659 struct MeshPeerPath *p;
2660 struct MeshPeerPath *nextp;
2662 GNUNET_PEER_resolve (pi->id, &id);
2663 GNUNET_PEER_change_rc (pi->id, -1);
2666 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2669 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2670 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2672 if (NULL != pi->dhtget)
2674 GNUNET_DHT_get_stop (pi->dhtget);
2675 GNUNET_free (pi->dhtgetcls);
2681 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2685 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2687 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2695 * Remove all paths that rely on a direct connection between p1 and p2
2696 * from the peer itself and notify all tunnels about it.
2698 * @param peer PeerInfo of affected peer.
2699 * @param p1 GNUNET_PEER_Id of one peer.
2700 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2703 * TODO: optimize (see below)
2706 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2709 struct MeshPeerPath *p;
2710 struct MeshPeerPath *aux;
2711 struct MeshPeerInfo *peer_d;
2713 unsigned int destroyed;
2718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2720 p = peer->path_head;
2724 for (i = 0; i < (p->length - 1); i++)
2726 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2727 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2729 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2740 for (i = 0; i < peer->ntunnels; i++)
2742 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2746 * Problem: one or more peers have been deleted from the tunnel tree.
2747 * We don't know who they are to try to add them again.
2748 * We need to try to find a new path for each of the disconnected peers.
2749 * Some of them might already have a path to reach them that does not
2750 * involve p1 and p2. Adding all anew might render in a better tree than
2751 * the trivial immediate fix.
2753 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2754 * its children will be reachable trough him.
2756 peer_d = peer_info_get_short (d);
2759 for (p = peer_d->path_head; NULL != p; p = p->next)
2761 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2769 /* No callback, as peer will be already disconnected and a connection
2770 * scheduled by tunnel_notify_connection_broken.
2772 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2776 peer_info_connect (peer_d, peer->tunnels[i]);
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2784 * Add the path to the peer and update the path used to reach it in case this
2787 * @param peer_info Destination peer to add the path to.
2788 * @param path New path to add. Last peer must be the peer in arg 1.
2789 * Path will be either used of freed if already known.
2790 * @param trusted Do we trust that this path is real?
2793 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2796 struct MeshPeerPath *aux;
2800 if ((NULL == peer_info) || (NULL == path))
2803 path_destroy (path);
2806 if (path->peers[path->length - 1] != peer_info->id)
2809 path_destroy (path);
2812 if (path->length <= 2 && GNUNET_NO == trusted)
2814 /* Only allow CORE to tell us about direct paths */
2815 path_destroy (path);
2818 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2819 for (l = 1; l < path->length; l++)
2821 if (path->peers[l] == myid)
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2824 for (l2 = 0; l2 < path->length - l; l2++)
2826 path->peers[l2] = path->peers[l + l2];
2831 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2836 struct GNUNET_PeerIdentity id;
2838 GNUNET_PEER_resolve (peer_info->id, &id);
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2840 path->length, GNUNET_i2s (&id));
2843 l = path_get_length (path);
2850 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2851 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2853 l2 = path_get_length (aux);
2856 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2857 peer_info->path_tail, aux, path);
2862 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2864 path_destroy (path);
2869 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2876 * Add the path to the origin peer and update the path used to reach it in case
2877 * this is the shortest.
2878 * The path is given in peer_info -> destination, therefore we turn the path
2879 * upside down first.
2881 * @param peer_info Peer to add the path to, being the origin of the path.
2882 * @param path New path to add after being inversed.
2883 * @param trusted Do we trust that this path is real?
2886 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2887 struct MeshPeerPath *path, int trusted)
2890 peer_info_add_path (peer_info, path, trusted);
2895 * Function called if the connection to the peer has been stalled for a while,
2896 * possibly due to a missed ACK. Poll the peer about its ACK status.
2898 * @param cls Closure (info about regex search).
2899 * @param tc TaskContext.
2902 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2904 struct MeshTunnelChildInfo *cinfo = cls;
2905 struct GNUNET_MESH_Poll msg;
2906 struct GNUNET_PeerIdentity id;
2907 struct MeshTunnel *t;
2909 return; // FIXME fc activate
2910 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2911 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2917 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2918 msg.header.size = htons (sizeof (msg));
2919 msg.tid = htonl (t->id.tid);
2920 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2921 msg.last_ack = htonl (cinfo->fwd_ack);
2923 GNUNET_PEER_resolve (tree_get_predecessor(cinfo->t->tree), &id);
2924 send_prebuilt_message (&msg.header, &id, cinfo->t);
2925 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2926 &tunnel_poll, cinfo);
2931 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2932 * to obtain a local peer -> destination path and interning the peer ids.
2934 * @return Newly allocated and created path
2936 static struct MeshPeerPath *
2937 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2938 unsigned int get_path_length,
2939 const struct GNUNET_PeerIdentity *put_path,
2940 unsigned int put_path_length)
2942 struct MeshPeerPath *p;
2948 GNUNET_PEER_change_rc (myid, 1);
2949 i = get_path_length;
2950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2951 for (i--; i >= 0; i--)
2953 id = GNUNET_PEER_intern (&get_path[i]);
2954 if (p->length > 0 && id == p->peers[p->length - 1])
2956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2957 GNUNET_PEER_change_rc (id, -1);
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2962 GNUNET_i2s (&get_path[i]));
2964 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2965 p->peers[p->length - 1] = id;
2968 i = put_path_length;
2969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2970 for (i--; i >= 0; i--)
2972 id = GNUNET_PEER_intern (&put_path[i]);
2975 /* PUT path went through us, so discard the path up until now and start
2976 * from here to get a much shorter (and loop-free) path.
2981 if (p->length > 0 && id == p->peers[p->length - 1])
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2984 GNUNET_PEER_change_rc (id, -1);
2988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2989 GNUNET_i2s (&put_path[i]));
2991 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2992 p->peers[p->length - 1] = id;
2996 if (get_path_length > 0)
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2998 GNUNET_i2s (&get_path[0]));
2999 if (put_path_length > 0)
3000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3001 GNUNET_i2s (&put_path[0]));
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3004 for (i = 0; i < p->length; i++)
3006 struct GNUNET_PeerIdentity peer_id;
3008 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3010 GNUNET_i2s (&peer_id));
3018 * Adds a path to the peer_infos of all the peers in the path
3020 * @param p Path to process.
3021 * @param confirmed Whether we know if the path works or not.
3024 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3028 /* TODO: invert and add */
3029 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3030 for (i++; i < p->length; i++)
3032 struct MeshPeerInfo *aux;
3033 struct MeshPeerPath *copy;
3035 aux = peer_info_get_short (p->peers[i]);
3036 copy = path_duplicate (p);
3037 copy->length = i + 1;
3038 peer_info_add_path (aux, copy, GNUNET_NO);
3044 * Send keepalive packets for a peer
3046 * @param cls Closure (tunnel for which to send the keepalive).
3047 * @param tc Notification context.
3049 * TODO: implement explicit multicast keepalive?
3052 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3056 * Search for a tunnel among the incoming tunnels
3058 * @param tid the local id of the tunnel
3060 * @return tunnel handler, NULL if doesn't exist
3062 static struct MeshTunnel *
3063 tunnel_get_incoming (MESH_TunnelNumber tid)
3065 struct GNUNET_HashCode hash;
3067 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3068 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3069 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3074 * Search for a tunnel among the tunnels for a client
3076 * @param c the client whose tunnels to search in
3077 * @param tid the local id of the tunnel
3079 * @return tunnel handler, NULL if doesn't exist
3081 static struct MeshTunnel *
3082 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3084 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3086 return tunnel_get_incoming (tid);
3090 struct GNUNET_HashCode hash;
3092 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3093 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3099 * Search for a tunnel by global ID using PEER_ID
3101 * @param pi owner of the tunnel
3102 * @param tid global tunnel number
3104 * @return tunnel handler, NULL if doesn't exist
3106 static struct MeshTunnel *
3107 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3109 struct MESH_TunnelID id;
3110 struct GNUNET_HashCode hash;
3115 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3116 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3121 * Search for a tunnel by global ID using full PeerIdentities
3123 * @param oid owner of the tunnel
3124 * @param tid global tunnel number
3126 * @return tunnel handler, NULL if doesn't exist
3128 static struct MeshTunnel *
3129 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3131 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3136 * Delete an active client from the tunnel.
3142 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3146 for (i = 0; i < t->nclients; i++)
3148 if (t->clients[i] == c)
3150 t->clients[i] = t->clients[t->nclients - 1];
3151 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3152 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3154 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3162 * Delete an ignored client from the tunnel.
3168 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3172 for (i = 0; i < t->nignore; i++)
3174 if (t->ignore[i] == c)
3176 t->ignore[i] = t->ignore[t->nignore - 1];
3177 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3185 * Delete a client from the tunnel. It should be only done on
3186 * client disconnection, otherwise use client_ignore_tunnel.
3192 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3194 tunnel_delete_ignored_client (t, c);
3195 tunnel_delete_active_client (t, c);
3200 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3202 * Destroys queue elements of all waiting transmissions and frees all memory
3203 * used by the struct and its elements.
3205 * @param cls Closure (tunnel info).
3206 * @param key Hash of GNUNET_PEER_Id (unused).
3207 * @param value MeshTunnelChildInfo of the child.
3209 * @return always GNUNET_YES, to keep iterating
3212 tunnel_destroy_child (void *cls,
3213 const struct GNUNET_HashCode * key,
3216 struct MeshTunnelChildInfo *cinfo = value;
3217 struct MeshTunnel *t = cls;
3218 struct MeshPeerQueue *q;
3222 for (c = 0; c < cinfo->send_buffer_n; c++)
3224 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3225 q = cinfo->send_buffer[i];
3226 cinfo->send_buffer[i] = NULL;
3228 queue_destroy (q, GNUNET_YES);
3231 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3233 GNUNET_free_non_null (cinfo->send_buffer);
3234 GNUNET_free (cinfo);
3240 * Callback used to notify a client owner of a tunnel that a peer has
3241 * disconnected, most likely because of a path change.
3243 * @param cls Closure (tunnel this notification is about).
3244 * @param peer_id Short ID of disconnected peer.
3247 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3249 struct MeshTunnel *t = cls;
3250 struct MeshPeerInfo *peer;
3251 struct MeshPathInfo *path_info;
3253 if (NULL != t->owner && NULL != nc)
3255 struct GNUNET_MESH_PeerControl msg;
3257 msg.header.size = htons (sizeof (msg));
3258 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3259 msg.tunnel_id = htonl (t->local_tid);
3260 GNUNET_PEER_resolve (peer_id, &msg.peer);
3261 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3262 &msg.header, GNUNET_NO);
3264 peer = peer_info_get_short (peer_id);
3265 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3266 path_info->peer = peer;
3268 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3274 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3275 * needed rescources.
3276 * If peer already exists, reevaluate shortest path and change if different.
3278 * @param t Tunnel we want to add a new peer to
3279 * @param peer PeerInfo of the peer being added
3283 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3285 struct GNUNET_PeerIdentity id;
3286 struct MeshPeerPath *best_p;
3287 struct MeshPeerPath *p;
3288 unsigned int best_cost;
3291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3292 GNUNET_PEER_resolve (peer->id, &id);
3294 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3297 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3298 GNUNET_assert (GNUNET_OK ==
3299 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3301 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3304 if (NULL != (p = peer->path_head))
3307 best_cost = tree_get_path_cost (t->tree, p);
3310 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3317 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3318 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3319 t->path_refresh_task =
3320 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3324 /* Start a DHT get */
3325 peer_info_connect (peer, t);
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3331 * Add a path to a tunnel which we don't own, just to remember the next hop.
3332 * If destination node was already in the tunnel, the first hop information
3333 * will be replaced with the new path.
3335 * @param t Tunnel we want to add a new peer to
3336 * @param p Path to add
3337 * @param own_pos Position of local node in path.
3341 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3342 unsigned int own_pos)
3344 struct GNUNET_PeerIdentity id;
3346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3347 GNUNET_assert (0 != own_pos);
3348 tree_add_path (t->tree, p, NULL, NULL);
3349 if (own_pos < p->length - 1)
3351 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3352 tree_update_first_hops (t->tree, myid, &id);
3354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3358 * Add a client to a tunnel, initializing all needed data structures.
3360 * @param t Tunnel to which add the client.
3361 * @param c Client which to add to the tunnel.
3364 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3366 struct MeshTunnelClientInfo clinfo;
3368 GNUNET_array_append (t->clients, t->nclients, c);
3369 clinfo.fwd_ack = t->fwd_pid + 1;
3370 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3371 clinfo.fwd_pid = t->fwd_pid;
3372 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3374 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3379 * Notifies a tunnel that a connection has broken that affects at least
3380 * some of its peers. Sends a notification towards the root of the tree.
3381 * In case the peer is the owner of the tree, notifies the client that owns
3382 * the tunnel and tries to reconnect.
3384 * @param t Tunnel affected.
3385 * @param p1 Peer that got disconnected from p2.
3386 * @param p2 Peer that got disconnected from p1.
3388 * @return Short ID of the peer disconnected (either p1 or p2).
3389 * 0 if the tunnel remained unaffected.
3391 static GNUNET_PEER_Id
3392 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3398 tree_notify_connection_broken (t->tree, p1, p2,
3399 &tunnel_notify_client_peer_disconnected,
3401 if (myid != p1 && myid != p2)
3407 if (tree_get_predecessor (t->tree) != 0)
3409 /* We are the peer still connected, notify owner of the disconnection. */
3410 struct GNUNET_MESH_PathBroken msg;
3411 struct GNUNET_PeerIdentity neighbor;
3413 msg.header.size = htons (sizeof (msg));
3414 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3415 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3416 msg.tid = htonl (t->id.tid);
3417 msg.peer1 = my_full_id;
3418 GNUNET_PEER_resolve (pid, &msg.peer2);
3419 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3420 send_prebuilt_message (&msg.header, &neighbor, t);
3428 * Send a multicast packet to a neighbor.
3430 * @param cls Closure (Info about the multicast packet)
3431 * @param neighbor_id Short ID of the neighbor to send the packet to.
3434 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3436 struct MeshData *mdata = cls;
3437 struct MeshTransmissionDescriptor *info;
3438 struct GNUNET_PeerIdentity neighbor;
3439 struct GNUNET_MessageHeader *msg;
3441 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3443 info->mesh_data = mdata;
3444 (mdata->reference_counter) ++;
3445 info->destination = neighbor_id;
3446 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3448 GNUNET_i2s (&neighbor));
3449 info->peer = peer_info_get (&neighbor);
3450 GNUNET_assert (NULL != info->peer);
3451 msg = (struct GNUNET_MessageHeader *) mdata->data;
3454 info->mesh_data->data_len,
3461 * Queue a message in a tunnel in multicast, sending a copy to each child node
3462 * down the local one in the tunnel tree.
3464 * @param t Tunnel in which to send the data.
3465 * @param msg Message to be sent.
3468 tunnel_send_multicast (struct MeshTunnel *t,
3469 const struct GNUNET_MessageHeader *msg)
3471 struct MeshData *mdata;
3473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3474 " sending a multicast packet...\n");
3476 mdata = GNUNET_malloc (sizeof (struct MeshData));
3477 mdata->data_len = ntohs (msg->size);
3479 mdata->data = GNUNET_malloc (mdata->data_len);
3480 memcpy (mdata->data, msg, mdata->data_len);
3481 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3483 struct GNUNET_MESH_Multicast *mcast;
3485 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3486 if (t->fwd_queue_n >= t->fwd_queue_max)
3489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3491 " message from %s!\n",
3492 GNUNET_i2s(&mcast->oid));
3493 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3494 " message at %s!\n",
3495 GNUNET_i2s(&my_full_id));
3496 GNUNET_free (mdata->data);
3497 GNUNET_free (mdata);
3501 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3503 ntohl (mcast->ttl));
3507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3510 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3511 if (mdata->reference_counter == 0)
3513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3514 " no one to send data to\n");
3515 GNUNET_free (mdata->data);
3516 GNUNET_free (mdata);
3521 mdata->total_out = mdata->reference_counter;
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3524 " sending a multicast packet done\n");
3530 * Increase the SKIP value of all peers that
3531 * have not received a unicast message.
3533 * @param cls Closure (ID of the peer that HAS received the message).
3534 * @param key ID of the neighbor.
3535 * @param value Information about the neighbor.
3537 * @return GNUNET_YES to keep iterating.
3540 tunnel_add_skip (void *cls,
3541 const struct GNUNET_HashCode * key,
3544 struct GNUNET_PeerIdentity *neighbor = cls;
3545 struct MeshTunnelChildInfo *cinfo = value;
3547 /* TODO compare only pointers? key == neighbor? */
3548 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3558 * @brief Get neighbor's Flow Control information.
3560 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3561 * descendant of the local node in a certain tunnel.
3562 * If the info is not yet there (recently created path), creates the data struct
3563 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3566 * @param t Tunnel related.
3567 * @param peer Neighbor whose Flow Control info is needed.
3569 * @return Neighbor's Flow Control info.
3571 static struct MeshTunnelChildInfo *
3572 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3573 const struct GNUNET_PeerIdentity *peer)
3575 struct MeshTunnelChildInfo *cinfo;
3577 if (NULL == t->children_fc)
3580 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3586 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3587 cinfo->id = GNUNET_PEER_intern (peer);
3588 cinfo->skip = t->fwd_pid;
3591 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3592 cinfo->fwd_ack = t->fwd_pid + delta;
3593 cinfo->bck_ack = delta;
3594 cinfo->bck_pid = -1;
3596 cinfo->send_buffer =
3597 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3599 GNUNET_assert (GNUNET_OK ==
3600 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3603 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3610 * Get the Flow Control info of a client.
3612 * @param t Tunnel on which to look.
3613 * @param c Client whose ACK to get.
3615 * @return ACK value.
3617 static struct MeshTunnelClientInfo *
3618 tunnel_get_client_fc (struct MeshTunnel *t,
3619 struct MeshClient *c)
3623 for (i = 0; i < t->nclients; i++)
3625 if (t->clients[i] != c)
3627 return &t->clients_fc[i];
3630 return NULL; // avoid compiler / coverity complaints
3635 * Iterator to get the appropiate ACK value from all children nodes.
3637 * @param cls Closue (tunnel).
3638 * @param id Id of the child node.
3641 tunnel_get_child_fwd_ack (void *cls,
3644 struct GNUNET_PeerIdentity peer_id;
3645 struct MeshTunnelChildInfo *cinfo;
3646 struct MeshTunnelChildIteratorContext *ctx = cls;
3647 struct MeshTunnel *t = ctx->t;
3650 GNUNET_PEER_resolve (id, &peer_id);
3651 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3652 ack = cinfo->fwd_ack;
3655 if (GNUNET_NO == ctx->init)
3657 ctx->max_child_ack = ack;
3658 ctx->init = GNUNET_YES;
3661 if (GNUNET_YES == t->speed_min)
3663 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3667 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3674 * Get the maximum PID allowed to transmit to any
3675 * tunnel child of the local peer, depending on the tunnel
3676 * buffering/speed settings.
3680 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3683 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3685 struct MeshTunnelChildIteratorContext ctx;
3687 ctx.max_child_ack = 0;
3689 ctx.init = GNUNET_NO;
3690 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3692 if (0 == ctx.nchildren)
3694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3695 " tunnel has no children, no FWD ACK\n");
3699 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3700 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3702 return (int64_t) ctx.max_child_ack;
3707 * Set the FWD ACK value of a client in a particular tunnel.
3709 * @param t Tunnel affected.
3710 * @param c Client whose ACK to set.
3711 * @param ack ACK value.
3714 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3715 struct MeshClient *c,
3720 for (i = 0; i < t->nclients; i++)
3722 if (t->clients[i] != c)
3724 t->clients_fc[i].fwd_ack = ack;
3732 * Get the highest ACK value of all clients in a particular tunnel,
3733 * according to the buffering/speed settings.
3735 * @param t Tunnel on which to look.
3737 * @return Corresponding ACK value (max uint32_t).
3738 * If no clients are suscribed, -1LL.
3741 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3746 if (0 == t->nclients)
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3749 " tunnel has no clients, no FWD ACK\n");
3753 for (ack = -1LL, i = 0; i < t->nclients; i++)
3756 (GNUNET_YES == t->speed_min &&
3757 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3758 (GNUNET_NO == t->speed_min &&
3759 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3761 ack = t->clients_fc[i].fwd_ack;
3765 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3766 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3768 return (uint32_t) ack;
3773 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3774 * mode and the status of all children nodes.
3778 * @return Maximum PID allowed.
3781 tunnel_get_fwd_ack (struct MeshTunnel *t)
3785 uint32_t buffer_free;
3789 count = t->fwd_pid - t->skip;
3790 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3791 child_ack = tunnel_get_children_fwd_ack (t);
3792 client_ack = tunnel_get_clients_fwd_ack (t);
3793 if (GNUNET_YES == t->nobuffer)
3796 if (-1LL == child_ack)
3797 child_ack = client_ack;
3798 if (-1LL == child_ack)
3801 client_ack = child_ack = ack;
3806 ack = count + buffer_free; // Overflow? OK!
3808 if (-1LL == child_ack)
3810 // Node has no children, child_ack AND core buffer are irrelevant.
3811 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3812 return (uint32_t) client_ack;
3814 if (-1LL == client_ack)
3818 if (GNUNET_YES == t->speed_min)
3820 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3821 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3825 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3826 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3829 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3830 count, buffer_free, child_ack, client_ack, ack);
3836 * Build a local ACK message and send it to a local client.
3838 * @param t Tunnel on which to send the ACK.
3839 * @param c Client to whom send the ACK.
3840 * @param ack Value of the ACK.
3843 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3845 struct GNUNET_MESH_LocalAck msg;
3847 msg.header.size = htons (sizeof (msg));
3848 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3849 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3850 msg.max_pid = htonl (ack);
3851 GNUNET_SERVER_notification_context_unicast(nc,
3858 * Build an ACK message and queue it to send to the given peer.
3860 * @param t Tunnel on which to send the ACK.
3861 * @param peer Peer to whom send the ACK.
3862 * @param ack Value of the ACK.
3865 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3867 struct GNUNET_MESH_ACK msg;
3869 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3870 msg.header.size = htons (sizeof (msg));
3871 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3872 msg.pid = htonl (ack);
3873 msg.tid = htonl (t->id.tid);
3875 send_prebuilt_message (&msg.header, peer, t);
3880 * Notify a the owner of a tunnel about how many more
3881 * payload packages will we accept on a given tunnel.
3883 * @param t Tunnel on which to send the ACK.
3886 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3891 "Sending client FWD ACK on tunnel %X\n",
3894 ack = tunnel_get_fwd_ack (t);
3896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3897 if (t->last_fwd_ack == ack)
3899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3904 t->last_fwd_ack = ack;
3905 send_local_ack (t, t->owner, ack);
3910 * Send an ACK informing the predecessor about the available buffer space.
3911 * In case there is no predecessor, inform the owning client.
3912 * If buffering is off, send only on behalf of children or self if endpoint.
3913 * If buffering is on, send when sent to children and buffer space is free.
3914 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3915 * the ACK itself goes "back" (towards root).
3917 * @param t Tunnel on which to send the ACK.
3918 * @param type Type of message that triggered the ACK transmission.
3921 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3923 struct GNUNET_PeerIdentity id;
3926 if (NULL != t->owner)
3928 tunnel_send_client_fwd_ack (t);
3931 /* Is it after unicast / multicast retransmission? */
3934 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3935 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3937 "ACK due to FWD DATA retransmission\n");
3938 if (GNUNET_YES == t->nobuffer)
3940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3944 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3945 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3951 /* Check if we need no retransmit the ACK */
3952 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3953 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3957 " t->qmax: %u, t->qn: %u\n",
3958 t->fwd_queue_max, t->fwd_queue_n);
3959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3960 " t->pid: %u, t->ack: %u\n",
3961 t->fwd_pid, t->last_fwd_ack);
3965 /* Ok, ACK might be necessary, what PID to ACK? */
3966 ack = tunnel_get_fwd_ack (t);
3968 /* If speed_min and not all children have ack'd, dont send yet */
3969 if (ack == t->last_fwd_ack)
3971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3975 t->last_fwd_ack = ack;
3976 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3977 send_ack (t, &id, ack);
3983 * Iterator to send a child node a BCK ACK to allow him to send more
3986 * @param cls Closure (tunnel).
3987 * @param id Id of the child node.
3990 tunnel_send_child_bck_ack (void *cls,
3993 struct MeshTunnel *t = cls;
3994 struct MeshTunnelChildInfo *cinfo;
3995 struct GNUNET_PeerIdentity peer;
3997 GNUNET_PEER_resolve (id, &peer);
3998 cinfo = tunnel_get_neighbor_fc (t, &peer);
4000 if (cinfo->bck_ack != cinfo->fwd_pid &&
4001 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->fwd_pid))
4004 cinfo->bck_ack++; // FIXME window size?
4005 send_ack (t, &peer, cinfo->bck_ack);
4010 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4012 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4014 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4015 * - implement a size 1 buffer in each client_fc AND children_fc
4016 * to hold at least 1 message per "child".
4017 * problem: violates no buffer policy
4018 * - ack 0 and make "children" poll for transmission slots
4019 * problem: big overhead, extra latency even in low traffic
4022 * @param t Tunnel on which to send the BCK ACKs.
4025 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4028 unsigned int tunnel_delta;
4030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4032 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4035 /* Find client whom to allow to send to origin (with lowest buffer space) */
4036 for (i = 0; i < t->nclients; i++)
4038 struct MeshTunnelClientInfo *clinfo;
4041 clinfo = &t->clients_fc[i];
4042 delta = clinfo->bck_ack - clinfo->bck_pid;
4043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4044 t->clients[i]->id, delta);
4046 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4047 (GNUNET_YES == t->nobuffer && 0 == delta))
4051 ack = clinfo->bck_pid;
4052 ack += t->nobuffer ? 1 : tunnel_delta;
4053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4054 " sending ack to client %u: %u\n",
4055 t->clients[i]->id, ack);
4056 send_local_ack (t, t->clients[i], ack);
4057 clinfo->bck_ack = ack;
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4062 " not sending ack to client %u (td %u, d %u)\n",
4063 t->clients[i]->id, tunnel_delta, delta);
4070 * Send an ACK informing the children nodes and destination clients about
4071 * the available buffer space.
4072 * If buffering is off, send only on behalf of root (can be self).
4073 * If buffering is on, send when sent to predecessor and buffer space is free.
4074 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4075 * the ACK itself goes "forward" (towards children/clients).
4077 * @param t Tunnel on which to send the ACK.
4078 * @param type Type of message that triggered the ACK transmission.
4081 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4084 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4085 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4086 /* Is it after data to_origin retransmission? */
4089 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4090 if (GNUNET_YES == t->nobuffer)
4092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4093 " Not sending ACK, nobuffer\n");
4097 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4098 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4099 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4105 tunnel_send_clients_bck_ack (t);
4106 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4111 * @brief Re-initiate traffic to this peer if necessary.
4113 * Check if there is traffic queued towards this peer
4114 * and the core transmit handle is NULL (traffic was stalled).
4115 * If so, call core tmt rdy.
4117 * @param cls Closure (unused)
4118 * @param peer_id Short ID of peer to which initiate traffic.
4121 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4123 struct MeshPeerInfo *peer;
4124 struct GNUNET_PeerIdentity id;
4125 struct MeshPeerQueue *q;
4128 peer = peer_info_get_short(peer_id);
4129 if (NULL != peer->core_transmit)
4132 q = queue_get_next(peer);
4135 /* Might br multicast traffic already sent to this particular peer but
4136 * not to other children in this tunnel.
4137 * This way t->queue_n would be > 0 but the queue of this particular peer
4143 GNUNET_PEER_resolve (peer->id, &id);
4144 peer->core_transmit =
4145 GNUNET_CORE_notify_transmit_ready(core_handle,
4148 GNUNET_TIME_UNIT_FOREVER_REL,
4158 * @brief Allow transmission of FWD traffic on this tunnel
4160 * Check if there is traffic queued towards any children
4161 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4163 * @param t Tunnel on which to unlock FWD traffic.
4166 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4168 if (0 == t->fwd_queue_n)
4171 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4176 * @brief Allow transmission of BCK traffic on this tunnel
4178 * Check if there is traffic queued towards the root of the tree
4179 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4181 * @param t Tunnel on which to unlock BCK traffic.
4184 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4186 if (0 == t->bck_queue_n)
4189 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4194 * Send a message to all peers in this tunnel that the tunnel is no longer
4197 * @param t The tunnel whose peers to notify.
4200 tunnel_send_destroy (struct MeshTunnel *t)
4202 struct GNUNET_MESH_TunnelDestroy msg;
4204 msg.header.size = htons (sizeof (msg));
4205 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4206 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4207 msg.tid = htonl (t->id.tid);
4208 tunnel_send_multicast (t, &msg.header);
4213 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4215 * @param cls Closure (Tunnel which to cancel).
4216 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4219 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4221 struct MeshTunnel *t = cls;
4222 struct MeshPeerInfo *peer_info;
4223 struct MeshPeerQueue *pq;
4224 struct MeshPeerQueue *next;
4226 peer_info = peer_info_get_short (neighbor_id);
4227 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4230 if (pq->tunnel == t)
4232 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4233 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4234 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4236 // Should have been removed on destroy children
4239 queue_destroy (pq, GNUNET_YES);
4242 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4244 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4245 peer_info->core_transmit = NULL;
4250 * Destroy the tunnel and free any allocated resources linked to it.
4252 * @param t the tunnel to destroy
4254 * @return GNUNET_OK on success
4257 tunnel_destroy (struct MeshTunnel *t)
4259 struct MeshClient *c;
4260 struct GNUNET_HashCode hash;
4271 struct GNUNET_PeerIdentity id;
4273 GNUNET_PEER_resolve (t->id.oid, &id);
4274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4275 GNUNET_i2s (&id), t->id.tid);
4277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4281 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4282 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4290 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4292 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4299 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4300 for (i = 0; i < t->nclients; i++)
4304 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4310 for (i = 0; i < t->nignore; i++)
4314 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4321 if (t->nclients > 0)
4324 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4329 GNUNET_free (t->clients);
4330 GNUNET_free (t->clients_fc);
4333 if (NULL != t->peers)
4335 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4337 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4340 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4341 &tunnel_destroy_child,
4343 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4344 t->children_fc = NULL;
4346 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4347 tree_destroy (t->tree);
4349 if (NULL != t->regex_ctx)
4350 regex_cancel_search (t->regex_ctx);
4351 if (NULL != t->dht_get_type)
4352 GNUNET_DHT_get_stop (t->dht_get_type);
4353 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4354 GNUNET_SCHEDULER_cancel (t->timeout_task);
4355 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4356 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4359 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4366 * Create a new tunnel
4368 * @param owner Who is the owner of the tunnel (short ID).
4369 * @param tid Tunnel Number of the tunnel.
4370 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4371 * @param local Tunnel Number for the tunnel, for the client point of view.
4373 * @return A new initialized tunnel. NULL on error.
4375 static struct MeshTunnel *
4376 tunnel_new (GNUNET_PEER_Id owner,
4377 MESH_TunnelNumber tid,
4378 struct MeshClient *client,
4379 MESH_TunnelNumber local)
4381 struct MeshTunnel *t;
4382 struct GNUNET_HashCode hash;
4384 if (n_tunnels >= max_tunnels && NULL == client)
4387 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4390 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4391 t->bck_queue_max = t->fwd_queue_max;
4392 t->tree = tree_new (owner);
4394 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4395 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4396 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4397 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4398 t->local_tid = local;
4399 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4401 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4403 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4405 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4406 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4413 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4420 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4422 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4423 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4427 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4437 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4438 * that are no longer needed, as well as nodes of no longer reachable peers.
4439 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4441 * @param t Tunnel from which to remove the path.
4442 * @param peer Short id of the peer which should be removed.
4445 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4447 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4453 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4454 * client when the client disconnects. If the client is not the owner, the
4455 * owner will get notified if no more clients are in the tunnel and the client
4456 * get removed from the tunnel's list.
4458 * @param cls closure (client that is disconnecting)
4459 * @param key the hash of the local tunnel id (used to access the hashmap)
4460 * @param value the value stored at the key (tunnel to destroy)
4462 * @return GNUNET_OK, keep iterating.
4465 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4467 struct MeshTunnel *t = value;
4468 struct MeshClient *c = cls;
4470 send_client_tunnel_disconnect(t, c);
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4474 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4475 tunnel_delete_client(t, c);
4476 client_delete_tunnel(c, t);
4479 tunnel_send_destroy(t);
4481 t->destroy = GNUNET_YES;
4488 * Timeout function, destroys tunnel if called
4490 * @param cls Closure (tunnel to destroy).
4491 * @param tc TaskContext
4494 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4496 struct MeshTunnel *t = cls;
4497 struct GNUNET_PeerIdentity id;
4499 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4500 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4502 GNUNET_PEER_resolve(t->id.oid, &id);
4503 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4504 "Tunnel %s [%X] timed out. Destroying.\n",
4505 GNUNET_i2s(&id), t->id.tid);
4506 send_clients_tunnel_destroy (t);
4511 * Resets the tunnel timeout. Starts it if no timeout was running.
4513 * @param t Tunnel whose timeout to reset.
4515 * TODO use heap to improve efficiency of scheduler.
4518 tunnel_reset_timeout (struct MeshTunnel *t)
4520 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4521 GNUNET_SCHEDULER_cancel (t->timeout_task);
4523 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4524 (refresh_path_time, 4), &tunnel_timeout, t);
4528 /******************************************************************************/
4529 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4530 /******************************************************************************/
4533 * Function to send a create path packet to a peer.
4535 * @param cls closure
4536 * @param size number of bytes available in buf
4537 * @param buf where the callee should write the message
4538 * @return number of bytes written to buf
4541 send_core_path_create (void *cls, size_t size, void *buf)
4543 struct MeshPathInfo *info = cls;
4544 struct GNUNET_MESH_ManipulatePath *msg;
4545 struct GNUNET_PeerIdentity *peer_ptr;
4546 struct MeshTunnel *t = info->t;
4547 struct MeshPeerPath *p = info->path;
4552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4554 sizeof (struct GNUNET_MESH_ManipulatePath) +
4555 p->length * sizeof (struct GNUNET_PeerIdentity);
4557 if (size < size_needed || NULL == buf)
4562 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4563 msg->header.size = htons (size_needed);
4564 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4565 msg->tid = ntohl (t->id.tid);
4568 if (GNUNET_YES == t->speed_min)
4569 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4570 if (GNUNET_YES == t->nobuffer)
4571 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4572 msg->opt = htonl(opt);
4575 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4576 for (i = 0; i < p->length; i++)
4578 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4585 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4591 * Fill the core buffer
4593 * @param cls closure (data itself)
4594 * @param size number of bytes available in buf
4595 * @param buf where the callee should write the message
4597 * @return number of bytes written to buf
4600 send_core_data_multicast (void *cls, size_t size, void *buf)
4602 struct MeshTransmissionDescriptor *info = cls;
4605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4606 GNUNET_assert (NULL != info);
4607 GNUNET_assert (NULL != info->peer);
4608 total_size = info->mesh_data->data_len;
4609 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4611 if (total_size > size)
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4617 memcpy (buf, info->mesh_data->data, total_size);
4620 struct GNUNET_MESH_Multicast *mc;
4621 struct GNUNET_MessageHeader *mh;
4624 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4626 mc = (struct GNUNET_MESH_Multicast *) mh;
4627 mh = (struct GNUNET_MessageHeader *) &mc[1];
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4629 " multicast, payload type %s\n",
4630 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4632 " multicast, payload size %u\n", ntohs (mh->size));
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4637 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4641 data_descriptor_decrement_rc (info->mesh_data);
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4650 * Creates a path ack message in buf and frees all unused resources.
4652 * @param cls closure (MeshTransmissionDescriptor)
4653 * @param size number of bytes available in buf
4654 * @param buf where the callee should write the message
4655 * @return number of bytes written to buf
4658 send_core_path_ack (void *cls, size_t size, void *buf)
4660 struct MeshTransmissionDescriptor *info = cls;
4661 struct GNUNET_MESH_PathACK *msg = buf;
4663 GNUNET_assert (NULL != info);
4664 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4669 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4670 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4671 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4672 msg->tid = htonl (info->origin->tid);
4673 msg->peer_id = my_full_id;
4676 /* TODO add signature */
4678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4679 return sizeof (struct GNUNET_MESH_PathACK);
4684 * Free a transmission that was already queued with all resources
4685 * associated to the request.
4687 * @param queue Queue handler to cancel.
4688 * @param clear_cls Is it necessary to free associated cls?
4691 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4693 struct MeshTransmissionDescriptor *dd;
4694 struct MeshPathInfo *path_info;
4695 struct MeshTunnelChildInfo *cinfo;
4696 struct GNUNET_PeerIdentity id;
4700 if (GNUNET_YES == clear_cls)
4702 switch (queue->type)
4704 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4705 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4707 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4708 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4709 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4710 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4712 " prebuilt message\n");
4713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4715 GNUNET_MESH_DEBUG_M2S(queue->type));
4717 data_descriptor_decrement_rc (dd->mesh_data);
4719 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4721 path_info = queue->cls;
4722 path_destroy (path_info->path);
4726 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4727 " type %s unknown!\n",
4728 GNUNET_MESH_DEBUG_M2S(queue->type));
4730 GNUNET_free_non_null (queue->cls);
4732 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4733 queue->peer->queue_tail,
4736 /* Delete from child_fc in the appropiate tunnel */
4737 max = queue->tunnel->fwd_queue_max;
4738 GNUNET_PEER_resolve (queue->peer->id, &id);
4739 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4742 for (i = 0; i < cinfo->send_buffer_n; i++)
4745 i2 = (cinfo->send_buffer_start + i) % max;
4746 if (cinfo->send_buffer[i2] == queue)
4748 /* Found corresponding entry in the send_buffer. Move all others back. */
4753 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4755 j2 = (cinfo->send_buffer_start + j) % max;
4756 j3 = (cinfo->send_buffer_start + j + 1) % max;
4757 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4760 cinfo->send_buffer[j3] = NULL;
4761 cinfo->send_buffer_n--;
4766 GNUNET_free (queue);
4771 * @brief Get the next transmittable message from the queue.
4773 * This will be the head, except in the case of being a data packet
4774 * not allowed by the destination peer.
4776 * @param peer Destination peer.
4778 * @return The next viable MeshPeerQueue element to send to that peer.
4779 * NULL when there are no transmittable messages.
4781 struct MeshPeerQueue *
4782 queue_get_next (const struct MeshPeerInfo *peer)
4784 struct MeshPeerQueue *q;
4785 struct MeshTunnel *t;
4786 struct MeshTransmissionDescriptor *info;
4787 struct MeshTunnelChildInfo *cinfo;
4788 struct GNUNET_MESH_Unicast *ucast;
4789 struct GNUNET_MESH_ToOrigin *to_orig;
4790 struct GNUNET_MESH_Multicast *mcast;
4791 struct GNUNET_PeerIdentity id;
4795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4796 for (q = peer->queue_head; NULL != q; q = q->next)
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4802 GNUNET_MESH_DEBUG_M2S(q->type));
4805 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4806 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4807 pid = ntohl (ucast->pid);
4808 GNUNET_PEER_resolve (info->peer->id, &id);
4809 cinfo = tunnel_get_neighbor_fc(t, &id);
4810 ack = cinfo->fwd_ack;
4812 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4813 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4814 pid = ntohl (to_orig->pid);
4817 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4818 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4819 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4821 // Not a multicast payload: multicast control traffic (destroy, etc)
4824 pid = ntohl (mcast->pid);
4825 GNUNET_PEER_resolve (info->peer->id, &id);
4826 cinfo = tunnel_get_neighbor_fc(t, &id);
4827 ack = cinfo->fwd_ack;
4830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4835 "********* ACK: %u, PID: %u\n",
4837 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4846 "********* NEXT!\n");
4849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4850 "********* nothing found\n");
4856 * Core callback to write a queued packet to core buffer
4858 * @param cls Closure (peer info).
4859 * @param size Number of bytes available in buf.
4860 * @param buf Where the to write the message.
4862 * @return number of bytes written to buf
4865 queue_send (void *cls, size_t size, void *buf)
4867 struct MeshPeerInfo *peer = cls;
4868 struct GNUNET_MessageHeader *msg;
4869 struct MeshPeerQueue *queue;
4870 struct MeshTunnel *t;
4871 struct MeshTunnelChildInfo *cinfo;
4872 struct GNUNET_PeerIdentity dst_id;
4875 peer->core_transmit = NULL;
4878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4879 queue = queue_get_next (peer);
4881 /* Queue has no internal mesh traffic nor sendable payload */
4884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4885 if (NULL == peer->queue_head)
4886 GNUNET_break (0); // Should've been canceled
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4891 GNUNET_PEER_resolve (peer->id, &dst_id);
4892 /* Check if buffer size is enough for the message */
4893 if (queue->size > size)
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4896 "********* not enough room, reissue\n");
4897 peer->core_transmit =
4898 GNUNET_CORE_notify_transmit_ready (core_handle,
4901 GNUNET_TIME_UNIT_FOREVER_REL,
4908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4911 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4915 "********* unicast: t->q (%u/%u)\n",
4916 t->fwd_queue_n, t->fwd_queue_max);
4918 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4925 switch (queue->type)
4928 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4929 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4930 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4931 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4933 "********* raw: %s\n",
4934 GNUNET_MESH_DEBUG_M2S (queue->type));
4936 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4937 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4938 data_size = send_core_data_raw (queue->cls, size, buf);
4939 msg = (struct GNUNET_MessageHeader *) buf;
4940 switch (ntohs (msg->type)) // Type of preconstructed message
4942 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4943 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4945 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4946 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4952 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4955 struct MeshTransmissionDescriptor *info = queue->cls;
4957 if ((1 == info->mesh_data->reference_counter
4958 && GNUNET_YES == t->speed_min)
4960 (info->mesh_data->total_out == info->mesh_data->reference_counter
4961 && GNUNET_NO == t->speed_min))
4963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4964 "********* considered sent\n");
4968 data_size = send_core_data_multicast(queue->cls, size, buf);
4969 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4971 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4973 data_size = send_core_path_create (queue->cls, size, buf);
4975 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4977 data_size = send_core_path_ack (queue->cls, size, buf);
4979 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4981 data_size = send_core_data_multicast (queue->cls, size, buf);
4985 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4986 "********* type unknown: %u\n",
4990 switch (queue->type)
4992 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4993 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4994 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4995 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4996 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4999 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5000 "at pos %u (%p) != %p\n",
5001 cinfo->send_buffer_start,
5002 cinfo->send_buffer[cinfo->send_buffer_start],
5005 if (cinfo->send_buffer_n > 0)
5007 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5008 cinfo->send_buffer_n--;
5009 cinfo->send_buffer_start++;
5010 cinfo->send_buffer_start %= t->fwd_queue_max;
5021 /* Free queue, but cls was freed by send_core_* */
5022 queue_destroy (queue, GNUNET_NO);
5024 if (GNUNET_YES == t->destroy)
5026 // FIXME fc tunnel destroy all pending traffic? wait for it?
5027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5031 /* If more data in queue, send next */
5032 queue = queue_get_next(peer);
5035 struct GNUNET_PeerIdentity id;
5037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5038 GNUNET_PEER_resolve (peer->id, &id);
5039 peer->core_transmit =
5040 GNUNET_CORE_notify_transmit_ready(core_handle,
5043 GNUNET_TIME_UNIT_FOREVER_REL,
5051 if (NULL != peer->queue_head)
5053 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5054 "********* %s stalled\n",
5055 GNUNET_i2s(&my_full_id));
5057 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5058 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5059 &tunnel_poll, cinfo);
5062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5068 * @brief Queue and pass message to core when possible.
5070 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5071 * and accounts for it. In case the queue is full, the message is dropped and
5074 * Otherwise, message is treated as internal and allowed to go regardless of
5077 * @param cls Closure (@c type dependant). It will be used by queue_send to
5078 * build the message to be sent if not already prebuilt.
5079 * @param type Type of the message, 0 for a raw message.
5080 * @param size Size of the message.
5081 * @param dst Neighbor to send message to.
5082 * @param t Tunnel this message belongs to.
5085 queue_add (void *cls, uint16_t type, size_t size,
5086 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5088 struct MeshPeerQueue *queue;
5089 struct MeshTunnelChildInfo *cinfo;
5090 struct GNUNET_PeerIdentity id;
5096 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5097 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5099 n = &t->fwd_queue_n;
5100 max = &t->fwd_queue_max;
5102 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5104 n = &t->bck_queue_n;
5105 max = &t->bck_queue_max;
5110 struct MeshTransmissionDescriptor *td = cls;
5111 struct GNUNET_MESH_ToOrigin *to;
5113 to = td->mesh_data->data;
5115 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5116 "bck pid %u, bck ack %u, msg pid %u\n",
5117 t->bck_pid, t->bck_ack, ntohl(to->pid));
5118 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5120 return; // Drop message
5124 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5130 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5131 GNUNET_PEER_resolve (dst->id, &id);
5132 if (NULL == dst->core_transmit)
5134 dst->core_transmit =
5135 GNUNET_CORE_notify_transmit_ready (core_handle,
5138 GNUNET_TIME_UNIT_FOREVER_REL,
5144 if (NULL == n) // Is this internal mesh traffic?
5147 // It's payload, keep track of buffer per peer.
5148 cinfo = tunnel_get_neighbor_fc(t, &id);
5149 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5150 if (NULL != cinfo->send_buffer[i])
5152 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5153 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5154 cinfo->send_buffer_start++;
5155 cinfo->send_buffer_start %= t->fwd_queue_max;
5159 cinfo->send_buffer_n++;
5161 cinfo->send_buffer[i] = queue;
5162 if (cinfo->send_buffer_n > t->fwd_queue_max)
5165 cinfo->send_buffer_n = t->fwd_queue_max;
5170 /******************************************************************************/
5171 /******************** MESH NETWORK HANDLERS **************************/
5172 /******************************************************************************/
5176 * Core handler for path creation
5178 * @param cls closure
5179 * @param message message
5180 * @param peer peer identity this notification is about
5181 * @param atsi performance data
5182 * @param atsi_count number of records in 'atsi'
5184 * @return GNUNET_OK to keep the connection open,
5185 * GNUNET_SYSERR to close it (signal serious error)
5188 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5189 const struct GNUNET_MessageHeader *message,
5190 const struct GNUNET_ATS_Information *atsi,
5191 unsigned int atsi_count)
5193 unsigned int own_pos;
5196 MESH_TunnelNumber tid;
5197 struct GNUNET_MESH_ManipulatePath *msg;
5198 struct GNUNET_PeerIdentity *pi;
5199 struct GNUNET_HashCode hash;
5200 struct MeshPeerPath *path;
5201 struct MeshPeerInfo *dest_peer_info;
5202 struct MeshPeerInfo *orig_peer_info;
5203 struct MeshTunnel *t;
5205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5206 "Received a path create msg [%s]\n",
5207 GNUNET_i2s (&my_full_id));
5208 size = ntohs (message->size);
5209 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5211 GNUNET_break_op (0);
5215 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5216 if (size % sizeof (struct GNUNET_PeerIdentity))
5218 GNUNET_break_op (0);
5221 size /= sizeof (struct GNUNET_PeerIdentity);
5224 GNUNET_break_op (0);
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5228 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5230 tid = ntohl (msg->tid);
5231 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5233 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5234 t = tunnel_get (pi, tid);
5235 if (NULL == t) // FIXME only for INCOMING tunnels?
5239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5240 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5243 // FIXME notify failure
5246 opt = ntohl (msg->opt);
5247 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5248 GNUNET_YES : GNUNET_NO;
5249 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5251 t->nobuffer = GNUNET_YES;
5252 t->last_fwd_ack = t->fwd_pid + 1;
5254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5255 " speed_min: %d, nobuffer:%d\n",
5256 t->speed_min, t->nobuffer);
5258 if (GNUNET_YES == t->nobuffer)
5260 t->bck_queue_max = 1;
5261 t->fwd_queue_max = 1;
5264 // FIXME only assign a local tid if a local client is interested (on demand)
5265 while (NULL != tunnel_get_incoming (next_local_tid))
5266 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5267 t->local_tid_dest = next_local_tid++;
5268 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5271 tunnel_reset_timeout (t);
5272 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5274 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5275 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5283 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5284 if (NULL == dest_peer_info)
5286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5287 " Creating PeerInfo for destination.\n");
5288 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5289 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5290 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5292 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5294 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5295 if (NULL == orig_peer_info)
5297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5298 " Creating PeerInfo for origin.\n");
5299 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5300 orig_peer_info->id = GNUNET_PEER_intern (pi);
5301 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5302 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5305 path = path_new (size);
5307 for (i = 0; i < size; i++)
5309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5310 GNUNET_i2s (&pi[i]));
5311 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5312 if (path->peers[i] == myid)
5315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5318 /* cannot be self, must be 'not found' */
5319 /* create path: self not found in path through self */
5320 GNUNET_break_op (0);
5321 path_destroy (path);
5325 path_add_to_peers (path, GNUNET_NO);
5326 tunnel_add_path (t, path, own_pos);
5327 if (own_pos == size - 1)
5329 /* It is for us! Send ack. */
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5331 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5332 if (NULL == t->peers)
5334 /* New tunnel! Notify clients on first payload message. */
5335 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5337 GNUNET_break (GNUNET_SYSERR !=
5338 GNUNET_CONTAINER_multihashmap_put (t->peers,
5339 &my_full_id.hashPubKey,
5342 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5347 struct MeshPeerPath *path2;
5349 /* It's for somebody else! Retransmit. */
5350 path2 = path_duplicate (path);
5351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5352 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5353 path2 = path_duplicate (path);
5354 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5355 send_create_path (dest_peer_info, path, t);
5362 * Core handler for path destruction
5364 * @param cls closure
5365 * @param message message
5366 * @param peer peer identity this notification is about
5367 * @param atsi performance data
5368 * @param atsi_count number of records in 'atsi'
5370 * @return GNUNET_OK to keep the connection open,
5371 * GNUNET_SYSERR to close it (signal serious error)
5374 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5375 const struct GNUNET_MessageHeader *message,
5376 const struct GNUNET_ATS_Information *atsi,
5377 unsigned int atsi_count)
5379 struct GNUNET_MESH_ManipulatePath *msg;
5380 struct GNUNET_PeerIdentity *pi;
5381 struct MeshPeerPath *path;
5382 struct MeshTunnel *t;
5383 unsigned int own_pos;
5387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5388 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5389 size = ntohs (message->size);
5390 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5392 GNUNET_break_op (0);
5396 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5397 if (size % sizeof (struct GNUNET_PeerIdentity))
5399 GNUNET_break_op (0);
5402 size /= sizeof (struct GNUNET_PeerIdentity);
5405 GNUNET_break_op (0);
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5410 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5411 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5413 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5415 t = tunnel_get (pi, ntohl (msg->tid));
5418 /* TODO notify back: we don't know this tunnel */
5419 GNUNET_break_op (0);
5422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5423 path = path_new (size);
5425 for (i = 0; i < size; i++)
5427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5428 GNUNET_i2s (&pi[i]));
5429 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5430 if (path->peers[i] == myid)
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5434 if (own_pos < path->length - 1)
5435 send_prebuilt_message (message, &pi[own_pos + 1], t);
5437 send_client_tunnel_disconnect(t, NULL);
5439 tunnel_delete_peer (t, path->peers[path->length - 1]);
5440 path_destroy (path);
5446 * Core handler for notifications of broken paths
5448 * @param cls closure
5449 * @param message message
5450 * @param peer peer identity this notification is about
5451 * @param atsi performance data
5452 * @param atsi_count number of records in 'atsi'
5454 * @return GNUNET_OK to keep the connection open,
5455 * GNUNET_SYSERR to close it (signal serious error)
5458 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5459 const struct GNUNET_MessageHeader *message,
5460 const struct GNUNET_ATS_Information *atsi,
5461 unsigned int atsi_count)
5463 struct GNUNET_MESH_PathBroken *msg;
5464 struct MeshTunnel *t;
5466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5467 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5468 msg = (struct GNUNET_MESH_PathBroken *) message;
5469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5470 GNUNET_i2s (&msg->peer1));
5471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5472 GNUNET_i2s (&msg->peer2));
5473 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5476 GNUNET_break_op (0);
5479 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5480 GNUNET_PEER_search (&msg->peer2));
5487 * Core handler for tunnel destruction
5489 * @param cls closure
5490 * @param message message
5491 * @param peer peer identity this notification is about
5492 * @param atsi performance data
5493 * @param atsi_count number of records in 'atsi'
5495 * @return GNUNET_OK to keep the connection open,
5496 * GNUNET_SYSERR to close it (signal serious error)
5499 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5500 const struct GNUNET_MessageHeader *message,
5501 const struct GNUNET_ATS_Information *atsi,
5502 unsigned int atsi_count)
5504 struct GNUNET_MESH_TunnelDestroy *msg;
5505 struct MeshTunnel *t;
5507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5508 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5509 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5511 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5512 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5515 /* Probably already got the message from another path,
5516 * destroyed the tunnel and retransmitted to children.
5519 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5522 if (t->id.oid == myid)
5524 GNUNET_break_op (0);
5527 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5529 /* Tunnel was incoming, notify clients */
5530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5531 t->local_tid, t->local_tid_dest);
5532 send_clients_tunnel_destroy (t);
5534 tunnel_send_destroy (t);
5535 t->destroy = GNUNET_YES;
5536 // TODO: add timeout to destroy the tunnel anyway
5542 * Core handler for mesh network traffic going from the origin to a peer
5544 * @param cls closure
5545 * @param peer peer identity this notification is about
5546 * @param message message
5547 * @param atsi performance data
5548 * @param atsi_count number of records in 'atsi'
5549 * @return GNUNET_OK to keep the connection open,
5550 * GNUNET_SYSERR to close it (signal serious error)
5553 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5554 const struct GNUNET_MessageHeader *message,
5555 const struct GNUNET_ATS_Information *atsi,
5556 unsigned int atsi_count)
5558 struct GNUNET_MESH_Unicast *msg;
5559 struct GNUNET_PeerIdentity *neighbor;
5560 struct MeshTunnelChildInfo *cinfo;
5561 struct MeshTunnel *t;
5562 GNUNET_PEER_Id dest_id;
5567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5570 size = ntohs (message->size);
5572 sizeof (struct GNUNET_MESH_Unicast) +
5573 sizeof (struct GNUNET_MessageHeader))
5578 msg = (struct GNUNET_MESH_Unicast *) message;
5579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5580 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5582 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5585 /* TODO notify back: we don't know this tunnel */
5586 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5587 GNUNET_break_op (0);
5590 pid = ntohl (msg->pid);
5591 if (t->fwd_pid == pid)
5593 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5594 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5595 " Already seen pid %u, DROPPING!\n", pid);
5600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5601 " pid %u not seen yet, forwarding\n", pid);
5604 t->skip += (pid - t->fwd_pid) - 1;
5607 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5609 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5610 GNUNET_break_op (0);
5611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5612 "Received PID %u, ACK %u\n",
5613 pid, t->last_fwd_ack);
5617 tunnel_reset_timeout (t);
5618 dest_id = GNUNET_PEER_search (&msg->destination);
5619 if (dest_id == myid)
5621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5622 " it's for us! sending to clients...\n");
5623 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5624 send_subscribed_clients (message, &msg[1].header, t);
5625 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5628 ttl = ntohl (msg->ttl);
5629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5632 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5633 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5634 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5638 " not for us, retransmitting...\n");
5640 neighbor = tree_get_first_hop (t->tree, dest_id);
5641 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5642 cinfo->fwd_pid = pid;
5643 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5646 if (GNUNET_YES == t->nobuffer &&
5647 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5649 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5650 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5651 GNUNET_break_op (0);
5654 send_prebuilt_message (message, neighbor, t);
5655 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5661 * Core handler for mesh network traffic going from the origin to all peers
5663 * @param cls closure
5664 * @param message message
5665 * @param peer peer identity this notification is about
5666 * @param atsi performance data
5667 * @param atsi_count number of records in 'atsi'
5668 * @return GNUNET_OK to keep the connection open,
5669 * GNUNET_SYSERR to close it (signal serious error)
5671 * TODO: Check who we got this from, to validate route.
5674 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5675 const struct GNUNET_MessageHeader *message,
5676 const struct GNUNET_ATS_Information *atsi,
5677 unsigned int atsi_count)
5679 struct GNUNET_MESH_Multicast *msg;
5680 struct MeshTunnel *t;
5684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5686 size = ntohs (message->size);
5687 if (sizeof (struct GNUNET_MESH_Multicast) +
5688 sizeof (struct GNUNET_MessageHeader) > size)
5690 GNUNET_break_op (0);
5693 msg = (struct GNUNET_MESH_Multicast *) message;
5694 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5698 /* TODO notify that we dont know that tunnel */
5699 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5700 GNUNET_break_op (0);
5703 pid = ntohl (msg->pid);
5704 if (t->fwd_pid == pid)
5706 /* already seen this packet, drop */
5707 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5709 " Already seen pid %u, DROPPING!\n", pid);
5710 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5716 " pid %u not seen yet, forwarding\n", pid);
5718 t->skip += (pid - t->fwd_pid) - 1;
5720 tunnel_reset_timeout (t);
5722 /* Transmit to locally interested clients */
5723 if (NULL != t->peers &&
5724 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5726 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5727 send_subscribed_clients (message, &msg[1].header, t);
5728 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5731 if (ntohl (msg->ttl) == 0)
5733 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5734 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5735 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5738 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5739 tunnel_send_multicast (t, message);
5745 * Core handler for mesh network traffic toward the owner of a tunnel
5747 * @param cls closure
5748 * @param message message
5749 * @param peer peer identity this notification is about
5750 * @param atsi performance data
5751 * @param atsi_count number of records in 'atsi'
5753 * @return GNUNET_OK to keep the connection open,
5754 * GNUNET_SYSERR to close it (signal serious error)
5757 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5758 const struct GNUNET_MessageHeader *message,
5759 const struct GNUNET_ATS_Information *atsi,
5760 unsigned int atsi_count)
5762 struct GNUNET_MESH_ToOrigin *msg;
5763 struct GNUNET_PeerIdentity id;
5764 struct MeshPeerInfo *peer_info;
5765 struct MeshTunnel *t;
5766 struct MeshTunnelChildInfo *cinfo;
5770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5772 size = ntohs (message->size);
5773 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5774 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5776 GNUNET_break_op (0);
5779 msg = (struct GNUNET_MESH_ToOrigin *) message;
5780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5781 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5782 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5783 pid = ntohl (msg->pid);
5787 /* TODO notify that we dont know this tunnel (whom)? */
5788 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5789 GNUNET_break_op (0);
5790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5791 "Received to_origin with PID %u on unknown tunnel\n",
5796 cinfo = tunnel_get_neighbor_fc(t, peer);
5803 if (cinfo->bck_pid == pid)
5805 /* already seen this packet, drop */
5806 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5808 " Already seen pid %u, DROPPING!\n", pid);
5809 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5814 " pid %u not seen yet, forwarding\n", pid);
5815 cinfo->bck_pid = pid;
5817 if (NULL != t->owner)
5820 struct GNUNET_MESH_ToOrigin *copy;
5822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5823 " it's for us! sending to clients...\n");
5824 /* TODO signature verification */
5825 memcpy (cbuf, message, size);
5826 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5827 copy->tid = htonl (t->local_tid);
5829 copy->pid = htonl (t->bck_pid);
5830 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5831 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5832 ©->header, GNUNET_NO);
5833 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5837 " not for us, retransmitting...\n");
5839 peer_info = peer_info_get (&msg->oid);
5840 if (NULL == peer_info)
5842 /* unknown origin of tunnel */
5846 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5847 send_prebuilt_message (message, &id, t);
5848 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5855 * Core handler for mesh network traffic point-to-point acks.
5857 * @param cls closure
5858 * @param message message
5859 * @param peer peer identity this notification is about
5860 * @param atsi performance data
5861 * @param atsi_count number of records in 'atsi'
5863 * @return GNUNET_OK to keep the connection open,
5864 * GNUNET_SYSERR to close it (signal serious error)
5867 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5868 const struct GNUNET_MessageHeader *message,
5869 const struct GNUNET_ATS_Information *atsi,
5870 unsigned int atsi_count)
5872 struct GNUNET_MESH_ACK *msg;
5873 struct MeshTunnel *t;
5876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5878 msg = (struct GNUNET_MESH_ACK *) message;
5880 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5884 /* TODO notify that we dont know this tunnel (whom)? */
5885 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5886 GNUNET_break_op (0);
5889 ack = ntohl (msg->pid);
5891 /* Is this a forward or backward ACK? */
5892 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5894 struct MeshTunnelChildInfo *cinfo;
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5898 cinfo = tunnel_get_neighbor_fc (t, peer);
5899 cinfo->fwd_ack = ack;
5900 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5901 tunnel_unlock_fwd_queues (t);
5905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5907 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5908 tunnel_unlock_bck_queue (t);
5915 * Core handler for mesh network traffic point-to-point ack polls.
5917 * @param cls closure
5918 * @param message message
5919 * @param peer peer identity this notification is about
5920 * @param atsi performance data
5921 * @param atsi_count number of records in 'atsi'
5923 * @return GNUNET_OK to keep the connection open,
5924 * GNUNET_SYSERR to close it (signal serious error)
5927 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5928 const struct GNUNET_MessageHeader *message,
5929 const struct GNUNET_ATS_Information *atsi,
5930 unsigned int atsi_count)
5932 struct GNUNET_MESH_Poll *msg;
5933 struct MeshTunnel *t;
5935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5938 msg = (struct GNUNET_MESH_Poll *) message;
5940 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5944 /* TODO notify that we dont know this tunnel (whom)? */
5945 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5946 GNUNET_break_op (0);
5950 /* Is this a forward or backward ACK? */
5951 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5953 struct MeshTunnelChildInfo *cinfo;
5955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5956 cinfo = tunnel_get_neighbor_fc (t, peer);
5957 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5958 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5963 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5971 * Core handler for path ACKs
5973 * @param cls closure
5974 * @param message message
5975 * @param peer peer identity this notification is about
5976 * @param atsi performance data
5977 * @param atsi_count number of records in 'atsi'
5979 * @return GNUNET_OK to keep the connection open,
5980 * GNUNET_SYSERR to close it (signal serious error)
5983 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5984 const struct GNUNET_MessageHeader *message,
5985 const struct GNUNET_ATS_Information *atsi,
5986 unsigned int atsi_count)
5988 struct GNUNET_MESH_PathACK *msg;
5989 struct GNUNET_PeerIdentity id;
5990 struct MeshPeerInfo *peer_info;
5991 struct MeshPeerPath *p;
5992 struct MeshTunnel *t;
5994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5995 GNUNET_i2s (&my_full_id));
5996 msg = (struct GNUNET_MESH_PathACK *) message;
5997 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6000 /* TODO notify that we don't know the tunnel */
6001 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6003 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6007 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6009 peer_info = peer_info_get (&msg->peer_id);
6010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6011 GNUNET_i2s (&msg->peer_id));
6012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6015 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6018 "connect_by_string completed, stopping search\n");
6019 regex_cancel_search (t->regex_ctx);
6020 t->regex_ctx = NULL;
6023 /* Add paths to peers? */
6024 p = tree_get_path_to_peer (t->tree, peer_info->id);
6027 path_add_to_peers (p, GNUNET_YES);
6035 /* Message for us? */
6036 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6039 if (NULL == t->owner)
6041 GNUNET_break_op (0);
6044 if (NULL != t->dht_get_type)
6046 GNUNET_DHT_get_stop (t->dht_get_type);
6047 t->dht_get_type = NULL;
6049 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6051 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6052 send_client_peer_connected (t, peer_info->id);
6057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6058 " not for us, retransmitting...\n");
6059 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6060 peer_info = peer_info_get (&msg->oid);
6061 if (NULL == peer_info)
6063 /* If we know the tunnel, we should DEFINITELY know the peer */
6067 send_prebuilt_message (message, &id, t);
6073 * Core handler for mesh keepalives.
6075 * @param cls closure
6076 * @param message message
6077 * @param peer peer identity this notification is about
6078 * @param atsi performance data
6079 * @param atsi_count number of records in 'atsi'
6080 * @return GNUNET_OK to keep the connection open,
6081 * GNUNET_SYSERR to close it (signal serious error)
6083 * TODO: Check who we got this from, to validate route.
6086 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6087 const struct GNUNET_MessageHeader *message,
6088 const struct GNUNET_ATS_Information *atsi,
6089 unsigned int atsi_count)
6091 struct GNUNET_MESH_TunnelKeepAlive *msg;
6092 struct MeshTunnel *t;
6094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6097 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6098 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6102 /* TODO notify that we dont know that tunnel */
6103 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6104 GNUNET_break_op (0);
6108 tunnel_reset_timeout (t);
6110 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6111 tunnel_send_multicast (t, message);
6118 * Functions to handle messages from core
6120 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6121 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6122 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6123 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6124 sizeof (struct GNUNET_MESH_PathBroken)},
6125 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6126 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6127 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6128 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6129 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6130 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6131 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6132 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6133 sizeof (struct GNUNET_MESH_ACK)},
6134 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6135 sizeof (struct GNUNET_MESH_Poll)},
6136 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6137 sizeof (struct GNUNET_MESH_PathACK)},
6143 /******************************************************************************/
6144 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6145 /******************************************************************************/
6148 * deregister_app: iterator for removing each application registered by a client
6150 * @param cls closure
6151 * @param key the hash of the application id (used to access the hashmap)
6152 * @param value the value stored at the key (client)
6154 * @return GNUNET_OK on success
6157 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6159 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6160 GNUNET_break (GNUNET_YES ==
6161 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6167 * notify_client_connection_failure: notify a client that the connection to the
6168 * requested remote peer is not possible (for instance, no route found)
6169 * Function called when the socket is ready to queue more data. "buf" will be
6170 * NULL and "size" zero if the socket was closed for writing in the meantime.
6172 * @param cls closure
6173 * @param size number of bytes available in buf
6174 * @param buf where the callee should write the message
6175 * @return number of bytes written to buf
6178 notify_client_connection_failure (void *cls, size_t size, void *buf)
6181 struct MeshPeerInfo *peer_info;
6182 struct GNUNET_MESH_PeerControl *msg;
6183 struct GNUNET_PeerIdentity id;
6185 if (0 == size && NULL == buf)
6187 // TODO retry? cancel?
6191 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6192 peer_info = (struct MeshPeerInfo *) cls;
6193 msg = (struct GNUNET_MESH_PeerControl *) buf;
6194 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6195 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6196 // msg->tunnel_id = htonl(peer_info->t->tid);
6197 GNUNET_PEER_resolve (peer_info->id, &id);
6198 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6206 * Send keepalive packets for a peer
6208 * @param cls Closure (tunnel for which to send the keepalive).
6209 * @param tc Notification context.
6212 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6214 struct MeshTunnel *t = cls;
6215 struct GNUNET_MESH_TunnelKeepAlive *msg;
6216 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6219 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6220 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6226 "sending keepalive for tunnel %d\n", t->id.tid);
6228 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6229 msg->header.size = htons (size);
6230 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6231 msg->oid = my_full_id;
6232 msg->tid = htonl (t->id.tid);
6233 tunnel_send_multicast (t, &msg->header);
6235 t->path_refresh_task =
6236 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6237 tunnel_reset_timeout(t);
6242 * Function to process paths received for a new peer addition. The recorded
6243 * paths form the initial tunnel, which can be optimized later.
6244 * Called on each result obtained for the DHT search.
6246 * @param cls closure
6247 * @param exp when will this value expire
6248 * @param key key of the result
6249 * @param get_path path of the get request
6250 * @param get_path_length lenght of get_path
6251 * @param put_path path of the put request
6252 * @param put_path_length length of the put_path
6253 * @param type type of the result
6254 * @param size number of bytes in data
6255 * @param data pointer to the result data
6257 * TODO: re-issue the request after certain time? cancel after X results?
6260 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6261 const struct GNUNET_HashCode * key,
6262 const struct GNUNET_PeerIdentity *get_path,
6263 unsigned int get_path_length,
6264 const struct GNUNET_PeerIdentity *put_path,
6265 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6266 size_t size, const void *data)
6268 struct MeshPathInfo *path_info = cls;
6269 struct MeshPeerPath *p;
6270 struct GNUNET_PeerIdentity pi;
6273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6274 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6277 p = path_build_from_dht (get_path, get_path_length, put_path,
6279 path_add_to_peers (p, GNUNET_NO);
6281 for (i = 0; i < path_info->peer->ntunnels; i++)
6283 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6284 peer_info_connect (path_info->peer, path_info->t);
6292 * Function to process paths received for a new peer addition. The recorded
6293 * paths form the initial tunnel, which can be optimized later.
6294 * Called on each result obtained for the DHT search.
6296 * @param cls closure
6297 * @param exp when will this value expire
6298 * @param key key of the result
6299 * @param get_path path of the get request
6300 * @param get_path_length lenght of get_path
6301 * @param put_path path of the put request
6302 * @param put_path_length length of the put_path
6303 * @param type type of the result
6304 * @param size number of bytes in data
6305 * @param data pointer to the result data
6308 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6309 const struct GNUNET_HashCode * key,
6310 const struct GNUNET_PeerIdentity *get_path,
6311 unsigned int get_path_length,
6312 const struct GNUNET_PeerIdentity *put_path,
6313 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6314 size_t size, const void *data)
6316 const struct PBlock *pb = data;
6317 const struct GNUNET_PeerIdentity *pi = &pb->id;
6318 struct MeshTunnel *t = cls;
6319 struct MeshPeerInfo *peer_info;
6320 struct MeshPeerPath *p;
6322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6323 if (size != sizeof (struct PBlock))
6325 GNUNET_break_op (0);
6328 if (ntohl(pb->type) != t->type)
6330 GNUNET_break_op (0);
6333 GNUNET_assert (NULL != t->owner);
6334 peer_info = peer_info_get (pi);
6335 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6337 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6339 p = path_build_from_dht (get_path, get_path_length, put_path,
6341 path_add_to_peers (p, GNUNET_NO);
6343 tunnel_add_peer (t, peer_info);
6344 peer_info_connect (peer_info, t);
6349 * Function to process DHT string to regex matching.
6350 * Called on each result obtained for the DHT search.
6352 * @param cls closure (search context)
6353 * @param exp when will this value expire
6354 * @param key key of the result
6355 * @param get_path path of the get request (not used)
6356 * @param get_path_length lenght of get_path (not used)
6357 * @param put_path path of the put request (not used)
6358 * @param put_path_length length of the put_path (not used)
6359 * @param type type of the result
6360 * @param size number of bytes in data
6361 * @param data pointer to the result data
6364 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6365 const struct GNUNET_HashCode * key,
6366 const struct GNUNET_PeerIdentity *get_path,
6367 unsigned int get_path_length,
6368 const struct GNUNET_PeerIdentity *put_path,
6369 unsigned int put_path_length,
6370 enum GNUNET_BLOCK_Type type,
6371 size_t size, const void *data)
6373 const struct MeshRegexAccept *block = data;
6374 struct MeshRegexSearchContext *ctx = cls;
6375 struct MeshRegexSearchInfo *info = ctx->info;
6376 struct MeshPeerPath *p;
6377 struct MeshPeerInfo *peer_info;
6379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6382 peer_info = peer_info_get(&block->id);
6383 p = path_build_from_dht (get_path, get_path_length, put_path,
6385 path_add_to_peers (p, GNUNET_NO);
6388 tunnel_add_peer (info->t, peer_info);
6389 peer_info_connect (peer_info, info->t);
6390 if (0 == info->peer)
6392 info->peer = peer_info->id;
6396 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6399 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6400 ®ex_connect_timeout,
6408 * Function to process DHT string to regex matching.
6409 * Called on each result obtained for the DHT search.
6411 * @param cls closure (search context)
6412 * @param exp when will this value expire
6413 * @param key key of the result
6414 * @param get_path path of the get request (not used)
6415 * @param get_path_length lenght of get_path (not used)
6416 * @param put_path path of the put request (not used)
6417 * @param put_path_length length of the put_path (not used)
6418 * @param type type of the result
6419 * @param size number of bytes in data
6420 * @param data pointer to the result data
6422 * TODO: re-issue the request after certain time? cancel after X results?
6425 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6426 const struct GNUNET_HashCode * key,
6427 const struct GNUNET_PeerIdentity *get_path,
6428 unsigned int get_path_length,
6429 const struct GNUNET_PeerIdentity *put_path,
6430 unsigned int put_path_length,
6431 enum GNUNET_BLOCK_Type type,
6432 size_t size, const void *data)
6434 const struct MeshRegexBlock *block = data;
6435 struct MeshRegexSearchContext *ctx = cls;
6436 struct MeshRegexSearchInfo *info = ctx->info;
6440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6441 "DHT GET STRING RETURNED RESULTS\n");
6442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6443 " key: %s\n", GNUNET_h2s (key));
6445 copy = GNUNET_malloc (size);
6446 memcpy (copy, data, size);
6447 GNUNET_break (GNUNET_OK ==
6448 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6449 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6450 len = ntohl (block->n_proof);
6452 char proof[len + 1];
6454 memcpy (proof, &block[1], len);
6456 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6458 GNUNET_break_op (0);
6462 len = strlen (info->description);
6463 if (len == ctx->position) // String processed
6465 if (GNUNET_YES == ntohl (block->accepting))
6467 regex_find_path(key, ctx);
6471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6472 // FIXME REGEX this block not successful, wait for more? start timeout?
6477 regex_next_edge (block, size, ctx);
6482 /******************************************************************************/
6483 /********************* MESH LOCAL HANDLES **************************/
6484 /******************************************************************************/
6488 * Handler for client disconnection
6490 * @param cls closure
6491 * @param client identification of the client; NULL
6492 * for the last call when the server is destroyed
6495 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6497 struct MeshClient *c;
6498 struct MeshClient *next;
6501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6510 if (c->handle != client)
6512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6518 GNUNET_SERVER_client_drop (c->handle);
6519 c->shutting_down = GNUNET_YES;
6520 GNUNET_assert (NULL != c->own_tunnels);
6521 GNUNET_assert (NULL != c->incoming_tunnels);
6522 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6523 &tunnel_destroy_iterator, c);
6524 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6525 &tunnel_destroy_iterator, c);
6526 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6527 &tunnel_destroy_iterator, c);
6528 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6529 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6530 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6532 /* deregister clients applications */
6533 if (NULL != c->apps)
6535 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6536 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6538 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6539 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6541 GNUNET_SCHEDULER_cancel (announce_applications_task);
6542 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6544 if (NULL != c->types)
6545 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6546 for (i = 0; i < c->n_regex; i++)
6548 GNUNET_free (c->regexes[i]);
6550 GNUNET_free_non_null (c->regexes);
6551 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6552 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6554 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6557 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6566 * Handler for new clients
6568 * @param cls closure
6569 * @param client identification of the client
6570 * @param message the actual message, which includes messages the client wants
6573 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6574 const struct GNUNET_MessageHeader *message)
6576 struct GNUNET_MESH_ClientConnect *cc_msg;
6577 struct MeshClient *c;
6578 GNUNET_MESH_ApplicationType *a;
6585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6586 /* Check data sanity */
6587 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6588 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6589 ntypes = ntohs (cc_msg->types);
6590 napps = ntohs (cc_msg->applications);
6592 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6595 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6599 /* Create new client structure */
6600 c = GNUNET_malloc (sizeof (struct MeshClient));
6601 c->id = next_client_id++;
6602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6604 GNUNET_SERVER_client_keep (client);
6605 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6608 GNUNET_MESH_ApplicationType at;
6609 struct GNUNET_HashCode hc;
6611 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6612 for (i = 0; i < napps; i++)
6615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6616 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6617 /* store in clients hashmap */
6618 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6619 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6620 /* store in global hashmap, for announcements */
6621 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6622 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6624 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6625 announce_applications_task =
6626 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6632 struct GNUNET_HashCode hc;
6634 t = (uint16_t *) & a[napps];
6635 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6636 for (i = 0; i < ntypes; i++)
6639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6640 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6642 /* store in clients hashmap */
6643 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6644 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6645 /* store in global hashmap */
6646 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6647 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6651 " client has %u+%u subscriptions\n", napps, ntypes);
6653 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6654 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6655 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6656 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6657 GNUNET_SERVER_notification_context_add (nc, client);
6658 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6660 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6666 * Handler for clients announcing available services by a regular expression.
6668 * @param cls closure
6669 * @param client identification of the client
6670 * @param message the actual message, which includes messages the client wants
6673 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6674 const struct GNUNET_MessageHeader *message)
6676 struct MeshClient *c;
6680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6682 /* Sanity check for client registration */
6683 if (NULL == (c = client_get (client)))
6686 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6691 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6692 regex = GNUNET_malloc (len + 1);
6693 memcpy (regex, &message[1], len);
6695 GNUNET_array_append (c->regexes, c->n_regex, regex);
6696 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6698 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6704 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6710 * Handler for requests of new tunnels
6712 * @param cls closure
6713 * @param client identification of the client
6714 * @param message the actual message
6717 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6718 const struct GNUNET_MessageHeader *message)
6720 struct GNUNET_MESH_TunnelMessage *t_msg;
6721 struct MeshTunnel *t;
6722 struct MeshClient *c;
6723 MESH_TunnelNumber tid;
6725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6727 /* Sanity check for client registration */
6728 if (NULL == (c = client_get (client)))
6731 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6736 /* Message sanity check */
6737 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6740 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6744 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6745 /* Sanity check for tunnel numbering */
6746 tid = ntohl (t_msg->tunnel_id);
6747 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6753 /* Sanity check for duplicate tunnel IDs */
6754 if (NULL != tunnel_get_by_local_id (c, tid))
6757 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6761 while (NULL != tunnel_get_by_pi (myid, next_tid))
6762 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6763 t = tunnel_new (myid, next_tid++, c, tid);
6766 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6771 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6773 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6774 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6777 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6783 * Handler for requests of deleting tunnels
6785 * @param cls closure
6786 * @param client identification of the client
6787 * @param message the actual message
6790 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6791 const struct GNUNET_MessageHeader *message)
6793 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6794 struct MeshClient *c;
6795 struct MeshTunnel *t;
6796 MESH_TunnelNumber tid;
6798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6799 "Got a DESTROY TUNNEL from client!\n");
6801 /* Sanity check for client registration */
6802 if (NULL == (c = client_get (client)))
6805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6810 /* Message sanity check */
6811 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6814 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6818 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6820 /* Retrieve tunnel */
6821 tid = ntohl (tunnel_msg->tunnel_id);
6822 t = tunnel_get_by_local_id(c, tid);
6825 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6830 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6832 client_ignore_tunnel (c, t);
6834 // TODO: when to destroy incoming tunnel?
6835 if (t->nclients == 0)
6837 GNUNET_assert (GNUNET_YES ==
6838 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6840 GNUNET_assert (GNUNET_YES ==
6841 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6842 &my_full_id.hashPubKey,
6846 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6849 send_client_tunnel_disconnect(t, c);
6850 client_delete_tunnel(c, t);
6852 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6854 tunnel_send_destroy (t);
6855 t->destroy = GNUNET_YES;
6856 // The tunnel will be destroyed when the last message is transmitted.
6857 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6863 * Handler for requests of seeting tunnel's speed.
6865 * @param cls Closure (unused).
6866 * @param client Identification of the client.
6867 * @param message The actual message.
6870 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6871 const struct GNUNET_MessageHeader *message)
6873 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6874 struct MeshClient *c;
6875 struct MeshTunnel *t;
6876 MESH_TunnelNumber tid;
6878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6879 "Got a SPEED request from client!\n");
6881 /* Sanity check for client registration */
6882 if (NULL == (c = client_get (client)))
6885 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6891 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6893 /* Retrieve tunnel */
6894 tid = ntohl (tunnel_msg->tunnel_id);
6895 t = tunnel_get_by_local_id(c, tid);
6898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6900 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6904 switch (ntohs(message->type))
6906 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6907 t->speed_min = GNUNET_YES;
6909 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6910 t->speed_min = GNUNET_NO;
6915 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6920 * Handler for requests of seeting tunnel's buffering policy.
6922 * @param cls Closure (unused).
6923 * @param client Identification of the client.
6924 * @param message The actual message.
6927 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6928 const struct GNUNET_MessageHeader *message)
6930 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6931 struct MeshClient *c;
6932 struct MeshTunnel *t;
6933 MESH_TunnelNumber tid;
6935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6936 "Got a BUFFER request from client!\n");
6938 /* Sanity check for client registration */
6939 if (NULL == (c = client_get (client)))
6942 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6947 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6949 /* Retrieve tunnel */
6950 tid = ntohl (tunnel_msg->tunnel_id);
6951 t = tunnel_get_by_local_id(c, tid);
6954 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6956 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6960 switch (ntohs(message->type))
6962 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6963 t->nobuffer = GNUNET_NO;
6965 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6966 t->nobuffer = GNUNET_YES;
6972 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6977 * Handler for connection requests to new peers
6979 * @param cls closure
6980 * @param client identification of the client
6981 * @param message the actual message (PeerControl)
6984 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6985 const struct GNUNET_MessageHeader *message)
6987 struct GNUNET_MESH_PeerControl *peer_msg;
6988 struct MeshPeerInfo *peer_info;
6989 struct MeshClient *c;
6990 struct MeshTunnel *t;
6991 MESH_TunnelNumber tid;
6993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6994 /* Sanity check for client registration */
6995 if (NULL == (c = client_get (client)))
6998 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7003 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7005 /* Sanity check for message size */
7006 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7013 /* Tunnel exists? */
7014 tid = ntohl (peer_msg->tunnel_id);
7015 t = tunnel_get_by_local_id (c, tid);
7019 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7023 /* Does client own tunnel? */
7024 if (t->owner->handle != client)
7027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7031 GNUNET_i2s (&peer_msg->peer));
7032 peer_info = peer_info_get (&peer_msg->peer);
7034 tunnel_add_peer (t, peer_info);
7035 peer_info_connect (peer_info, t);
7037 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7043 * Handler for disconnection requests of peers in a tunnel
7045 * @param cls closure
7046 * @param client identification of the client
7047 * @param message the actual message (PeerControl)
7050 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7051 const struct GNUNET_MessageHeader *message)
7053 struct GNUNET_MESH_PeerControl *peer_msg;
7054 struct MeshPeerInfo *peer_info;
7055 struct MeshClient *c;
7056 struct MeshTunnel *t;
7057 MESH_TunnelNumber tid;
7059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7060 /* Sanity check for client registration */
7061 if (NULL == (c = client_get (client)))
7064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7069 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7071 /* Sanity check for message size */
7072 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7079 /* Tunnel exists? */
7080 tid = ntohl (peer_msg->tunnel_id);
7081 t = tunnel_get_by_local_id (c, tid);
7085 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7090 /* Does client own tunnel? */
7091 if (t->owner->handle != client)
7094 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7099 GNUNET_i2s (&peer_msg->peer));
7100 /* Is the peer in the tunnel? */
7102 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7103 if (NULL == peer_info)
7106 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7110 /* Ok, delete peer from tunnel */
7111 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7112 &peer_msg->peer.hashPubKey);
7114 send_destroy_path (t, peer_info->id);
7115 tunnel_delete_peer (t, peer_info->id);
7116 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7121 * Handler for blacklist requests of peers in a tunnel
7123 * @param cls closure
7124 * @param client identification of the client
7125 * @param message the actual message (PeerControl)
7127 * FIXME implement DHT block bloomfilter
7130 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7131 const struct GNUNET_MessageHeader *message)
7133 struct GNUNET_MESH_PeerControl *peer_msg;
7134 struct MeshClient *c;
7135 struct MeshTunnel *t;
7136 MESH_TunnelNumber tid;
7138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7139 /* Sanity check for client registration */
7140 if (NULL == (c = client_get (client)))
7143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7148 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7150 /* Sanity check for message size */
7151 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7154 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7158 /* Tunnel exists? */
7159 tid = ntohl (peer_msg->tunnel_id);
7160 t = tunnel_get_by_local_id (c, tid);
7164 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7169 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7170 GNUNET_PEER_intern(&peer_msg->peer));
7175 * Handler for unblacklist requests of peers in a tunnel
7177 * @param cls closure
7178 * @param client identification of the client
7179 * @param message the actual message (PeerControl)
7182 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7183 const struct GNUNET_MessageHeader *message)
7185 struct GNUNET_MESH_PeerControl *peer_msg;
7186 struct MeshClient *c;
7187 struct MeshTunnel *t;
7188 MESH_TunnelNumber tid;
7192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7193 /* Sanity check for client registration */
7194 if (NULL == (c = client_get (client)))
7197 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7202 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7204 /* Sanity check for message size */
7205 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7208 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7212 /* Tunnel exists? */
7213 tid = ntohl (peer_msg->tunnel_id);
7214 t = tunnel_get_by_local_id (c, tid);
7218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7223 /* if peer is not known, complain */
7224 pid = GNUNET_PEER_search (&peer_msg->peer);
7231 /* search and remove from list */
7232 for (i = 0; i < t->nblacklisted; i++)
7234 if (t->blacklisted[i] == pid)
7236 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7237 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7242 /* if peer hasn't been blacklisted, complain */
7248 * Handler for connection requests to new peers by type
7250 * @param cls closure
7251 * @param client identification of the client
7252 * @param message the actual message (ConnectPeerByType)
7255 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7256 const struct GNUNET_MessageHeader *message)
7258 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7259 struct MeshClient *c;
7260 struct MeshTunnel *t;
7261 struct GNUNET_HashCode hash;
7262 MESH_TunnelNumber tid;
7264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7265 /* Sanity check for client registration */
7266 if (NULL == (c = client_get (client)))
7269 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7274 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7276 /* Sanity check for message size */
7277 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7278 ntohs (connect_msg->header.size))
7281 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7285 /* Tunnel exists? */
7286 tid = ntohl (connect_msg->tunnel_id);
7287 t = tunnel_get_by_local_id (c, tid);
7291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7295 /* Does client own tunnel? */
7296 if (t->owner->handle != client)
7299 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7303 /* Do WE have the service? */
7304 t->type = ntohl (connect_msg->type);
7305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7306 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7307 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7310 /* Yes! Fast forward, add ourselves to the tunnel and send the
7311 * good news to the client, and alert the destination client of
7312 * an incoming tunnel.
7314 * FIXME send a path create to self, avoid code duplication
7316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7317 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7318 peer_info_get (&my_full_id),
7319 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7322 send_client_peer_connected (t, myid);
7323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7324 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7326 t->local_tid_dest = next_local_tid++;
7327 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7328 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7329 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7333 /* Ok, lets find a peer offering the service */
7334 if (NULL != t->dht_get_type)
7336 GNUNET_DHT_get_stop (t->dht_get_type);
7338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7339 GNUNET_h2s (&hash));
7341 GNUNET_DHT_get_start (dht_handle,
7342 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7344 dht_replication_level,
7345 GNUNET_DHT_RO_RECORD_ROUTE |
7346 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7348 &dht_get_type_handler, t);
7350 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7356 * Handler for connection requests to new peers by a string service description.
7358 * @param cls closure
7359 * @param client identification of the client
7360 * @param message the actual message, which includes messages the client wants
7363 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7364 const struct GNUNET_MessageHeader *message)
7366 struct GNUNET_MESH_ConnectPeerByString *msg;
7367 struct MeshRegexSearchContext *ctx;
7368 struct MeshRegexSearchInfo *info;
7369 struct GNUNET_DHT_GetHandle *get_h;
7370 struct GNUNET_HashCode key;
7371 struct MeshTunnel *t;
7372 struct MeshClient *c;
7373 MESH_TunnelNumber tid;
7377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7378 "Connect by string started\n");
7379 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7380 size = htons (message->size);
7382 /* Sanity check for client registration */
7383 if (NULL == (c = client_get (client)))
7386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7391 /* Message size sanity check */
7392 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7395 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7399 /* Tunnel exists? */
7400 tid = ntohl (msg->tunnel_id);
7401 t = tunnel_get_by_local_id (c, tid);
7405 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7409 /* Does client own tunnel? */
7410 if (t->owner->handle != client)
7413 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7418 " on tunnel %s [%u]\n",
7419 GNUNET_i2s(&my_full_id),
7422 /* Only one connect_by_string allowed at the same time! */
7423 /* FIXME: allow more, return handle at api level to cancel, document */
7424 if (NULL != t->regex_ctx)
7427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7431 /* Find string itself */
7432 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7433 string = (const char *) &msg[1];
7435 /* Initialize context */
7436 size = GNUNET_REGEX_get_first_key(string, len, &key);
7437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7438 " consumed %u bits out of %u\n", size, len);
7439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7440 " looking for %s\n", GNUNET_h2s (&key));
7442 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7444 info->description = GNUNET_malloc (len + 1);
7445 memcpy (info->description, string, len);
7446 info->description[len] = '\0';
7447 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7448 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7451 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7452 ctx->position = size;
7456 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7458 /* Start search in DHT */
7459 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7460 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7461 &key, /* key to search */
7462 dht_replication_level, /* replication level */
7463 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7464 NULL, /* xquery */ // FIXME BLOOMFILTER
7465 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7466 &dht_get_string_handler, ctx);
7468 GNUNET_break (GNUNET_OK ==
7469 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7472 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7474 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7480 * Handler for client traffic directed to one peer
7482 * @param cls closure
7483 * @param client identification of the client
7484 * @param message the actual message
7487 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7488 const struct GNUNET_MessageHeader *message)
7490 struct MeshClient *c;
7491 struct MeshTunnel *t;
7492 struct MeshPeerInfo *pi;
7493 struct GNUNET_MESH_Unicast *data_msg;
7494 MESH_TunnelNumber tid;
7497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7498 "Got a unicast request from a client!\n");
7500 /* Sanity check for client registration */
7501 if (NULL == (c = client_get (client)))
7504 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7509 data_msg = (struct GNUNET_MESH_Unicast *) message;
7511 /* Sanity check for message size */
7512 size = ntohs (message->size);
7513 if (sizeof (struct GNUNET_MESH_Unicast) +
7514 sizeof (struct GNUNET_MessageHeader) > size)
7517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7521 /* Tunnel exists? */
7522 tid = ntohl (data_msg->tid);
7523 t = tunnel_get_by_local_id (c, tid);
7527 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7531 /* Is it a local tunnel? Then, does client own the tunnel? */
7532 if (t->owner->handle != client)
7535 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7539 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7540 &data_msg->destination.hashPubKey);
7541 /* Is the selected peer in the tunnel? */
7545 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7549 /* PID should be as expected */
7550 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7553 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7554 "Unicast PID, expected %u, got %u\n",
7555 t->fwd_pid + 1, ntohl (data_msg->pid));
7556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7560 /* Ok, everything is correct, send the message
7561 * (pretend we got it from a mesh peer)
7564 /* Work around const limitation */
7565 char buf[ntohs (message->size)] GNUNET_ALIGN;
7566 struct GNUNET_MESH_Unicast *copy;
7568 copy = (struct GNUNET_MESH_Unicast *) buf;
7569 memcpy (buf, data_msg, size);
7570 copy->oid = my_full_id;
7571 copy->tid = htonl (t->id.tid);
7572 copy->ttl = htonl (default_ttl);
7573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7574 " calling generic handler...\n");
7575 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7578 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7585 * Handler for client traffic directed to the origin
7587 * @param cls closure
7588 * @param client identification of the client
7589 * @param message the actual message
7592 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7593 const struct GNUNET_MessageHeader *message)
7595 struct GNUNET_MESH_ToOrigin *data_msg;
7596 struct MeshTunnelClientInfo *clinfo;
7597 struct MeshClient *c;
7598 struct MeshTunnel *t;
7599 MESH_TunnelNumber tid;
7602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7603 "Got a ToOrigin request from a client!\n");
7604 /* Sanity check for client registration */
7605 if (NULL == (c = client_get (client)))
7608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7613 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7615 /* Sanity check for message size */
7616 size = ntohs (message->size);
7617 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7618 sizeof (struct GNUNET_MessageHeader) > size)
7621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7625 /* Tunnel exists? */
7626 tid = ntohl (data_msg->tid);
7627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7628 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7634 t = tunnel_get_by_local_id (c, tid);
7637 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7640 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7644 /* It should be sent by someone who has this as incoming tunnel. */
7645 if (GNUNET_NO == client_knows_tunnel (c, t))
7648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7652 /* PID should be as expected */
7653 clinfo = tunnel_get_client_fc (t, c);
7654 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7657 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7658 "To Origin PID, expected %u, got %u\n",
7659 clinfo->bck_pid + 1,
7660 ntohl (data_msg->pid));
7661 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7665 /* Ok, everything is correct, send the message
7666 * (pretend we got it from a mesh peer)
7670 char buf[ntohs (message->size)] GNUNET_ALIGN;
7671 struct GNUNET_MESH_ToOrigin *copy;
7673 /* Work around const limitation */
7674 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7675 memcpy (buf, data_msg, size);
7676 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7677 copy->tid = htonl (t->id.tid);
7678 copy->ttl = htonl (default_ttl);
7679 if (ntohl (copy->pid) != (t->bck_pid + 1))
7682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7683 "To Origin PID, expected %u, got %u\n",
7688 copy->sender = my_full_id;
7689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7690 " calling generic handler...\n");
7691 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7693 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7700 * Handler for client traffic directed to all peers in a tunnel
7702 * @param cls closure
7703 * @param client identification of the client
7704 * @param message the actual message
7707 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7708 const struct GNUNET_MessageHeader *message)
7710 struct MeshClient *c;
7711 struct MeshTunnel *t;
7712 struct GNUNET_MESH_Multicast *data_msg;
7713 MESH_TunnelNumber tid;
7715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7716 "Got a multicast request from a client!\n");
7718 /* Sanity check for client registration */
7719 if (NULL == (c = client_get (client)))
7722 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7727 data_msg = (struct GNUNET_MESH_Multicast *) message;
7729 /* Sanity check for message size */
7730 if (sizeof (struct GNUNET_MESH_Multicast) +
7731 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7738 /* Tunnel exists? */
7739 tid = ntohl (data_msg->tid);
7740 t = tunnel_get_by_local_id (c, tid);
7744 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7745 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7751 /* Does client own tunnel? */
7752 if (t->owner->handle != client)
7755 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7759 /* PID should be as expected */
7760 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7763 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7764 "Multicast PID, expected %u, got %u\n",
7765 t->fwd_pid + 1, ntohl (data_msg->pid));
7766 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7771 char buf[ntohs (message->size)] GNUNET_ALIGN;
7772 struct GNUNET_MESH_Multicast *copy;
7774 copy = (struct GNUNET_MESH_Multicast *) buf;
7775 memcpy (buf, message, ntohs (message->size));
7776 copy->oid = my_full_id;
7777 copy->tid = htonl (t->id.tid);
7778 copy->ttl = htonl (default_ttl);
7779 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7781 " calling generic handler...\n");
7782 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7785 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7791 * Handler for client's ACKs for payload traffic.
7793 * @param cls Closure (unused).
7794 * @param client Identification of the client.
7795 * @param message The actual message.
7798 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7799 const struct GNUNET_MessageHeader *message)
7801 struct GNUNET_MESH_LocalAck *msg;
7802 struct MeshTunnel *t;
7803 struct MeshClient *c;
7804 MESH_TunnelNumber tid;
7807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7808 /* Sanity check for client registration */
7809 if (NULL == (c = client_get (client)))
7812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7817 msg = (struct GNUNET_MESH_LocalAck *) message;
7819 /* Tunnel exists? */
7820 tid = ntohl (msg->tunnel_id);
7821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7822 t = tunnel_get_by_local_id (c, tid);
7826 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7827 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7832 ack = ntohl (msg->max_pid);
7833 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7835 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7836 if (NULL != t->owner && t->owner->handle == client)
7838 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7840 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7844 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7845 tunnel_set_client_fwd_ack (t, c, ack);
7846 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7849 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7856 * Functions to handle messages from clients
7858 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7859 {&handle_local_new_client, NULL,
7860 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7861 {&handle_local_announce_regex, NULL,
7862 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7863 {&handle_local_tunnel_create, NULL,
7864 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7865 sizeof (struct GNUNET_MESH_TunnelMessage)},
7866 {&handle_local_tunnel_destroy, NULL,
7867 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7868 sizeof (struct GNUNET_MESH_TunnelMessage)},
7869 {&handle_local_tunnel_speed, NULL,
7870 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7871 sizeof (struct GNUNET_MESH_TunnelMessage)},
7872 {&handle_local_tunnel_speed, NULL,
7873 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7874 sizeof (struct GNUNET_MESH_TunnelMessage)},
7875 {&handle_local_tunnel_buffer, NULL,
7876 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7877 sizeof (struct GNUNET_MESH_TunnelMessage)},
7878 {&handle_local_tunnel_buffer, NULL,
7879 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7880 sizeof (struct GNUNET_MESH_TunnelMessage)},
7881 {&handle_local_connect_add, NULL,
7882 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7883 sizeof (struct GNUNET_MESH_PeerControl)},
7884 {&handle_local_connect_del, NULL,
7885 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7886 sizeof (struct GNUNET_MESH_PeerControl)},
7887 {&handle_local_blacklist, NULL,
7888 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7889 sizeof (struct GNUNET_MESH_PeerControl)},
7890 {&handle_local_unblacklist, NULL,
7891 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7892 sizeof (struct GNUNET_MESH_PeerControl)},
7893 {&handle_local_connect_by_type, NULL,
7894 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7895 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7896 {&handle_local_connect_by_string, NULL,
7897 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7898 {&handle_local_unicast, NULL,
7899 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7900 {&handle_local_to_origin, NULL,
7901 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7902 {&handle_local_multicast, NULL,
7903 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7904 {&handle_local_ack, NULL,
7905 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7906 sizeof (struct GNUNET_MESH_LocalAck)},
7912 * To be called on core init/fail.
7914 * @param cls service closure
7915 * @param server handle to the server for this service
7916 * @param identity the public identity of this peer
7919 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7920 const struct GNUNET_PeerIdentity *identity)
7923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7924 core_handle = server;
7925 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7928 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7929 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7931 GNUNET_abort(); // Try harder
7938 * Method called whenever a given peer connects.
7940 * @param cls closure
7941 * @param peer peer identity this notification is about
7942 * @param atsi performance data for the connection
7943 * @param atsi_count number of records in 'atsi'
7946 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7947 const struct GNUNET_ATS_Information *atsi,
7948 unsigned int atsi_count)
7950 struct MeshPeerInfo *peer_info;
7951 struct MeshPeerPath *path;
7953 DEBUG_CONN ("Peer connected\n");
7954 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7955 peer_info = peer_info_get (peer);
7956 if (myid == peer_info->id)
7958 DEBUG_CONN (" (self)\n");
7963 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7965 path = path_new (2);
7966 path->peers[0] = myid;
7967 path->peers[1] = peer_info->id;
7968 GNUNET_PEER_change_rc (myid, 1);
7969 GNUNET_PEER_change_rc (peer_info->id, 1);
7970 peer_info_add_path (peer_info, path, GNUNET_YES);
7971 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7977 * Method called whenever a peer disconnects.
7979 * @param cls closure
7980 * @param peer peer identity this notification is about
7983 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7985 struct MeshPeerInfo *pi;
7986 struct MeshPeerQueue *q;
7987 struct MeshPeerQueue *n;
7989 DEBUG_CONN ("Peer disconnected\n");
7990 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8000 /* TODO try to reroute this traffic instead */
8001 queue_destroy(q, GNUNET_YES);
8004 if (NULL != pi->core_transmit)
8006 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8007 pi->core_transmit = NULL;
8009 peer_info_remove_path (pi, pi->id, myid);
8012 DEBUG_CONN (" (self)\n");
8014 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8019 /******************************************************************************/
8020 /************************ MAIN FUNCTIONS ****************************/
8021 /******************************************************************************/
8024 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8026 * @param cls closure
8027 * @param key current key code
8028 * @param value value in the hash map
8029 * @return GNUNET_YES if we should continue to iterate,
8033 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8035 struct MeshTunnel *t = value;
8042 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8044 * @param cls closure
8045 * @param key current key code
8046 * @param value value in the hash map
8047 * @return GNUNET_YES if we should continue to iterate,
8051 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8053 struct MeshPeerInfo *p = value;
8054 struct MeshPeerQueue *q;
8055 struct MeshPeerQueue *n;
8063 queue_destroy(q, GNUNET_YES);
8067 peer_info_destroy (p);
8073 * Task run during shutdown.
8079 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8083 if (core_handle != NULL)
8085 GNUNET_CORE_disconnect (core_handle);
8090 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8093 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8094 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8095 if (dht_handle != NULL)
8097 GNUNET_DHT_disconnect (dht_handle);
8102 GNUNET_SERVER_notification_context_destroy (nc);
8105 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8107 GNUNET_SCHEDULER_cancel (announce_id_task);
8108 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8115 * Callback for hostkey read/generation
8118 * @param pk the private key
8119 * @param emsg error message
8122 key_generation_cb (void *cls,
8123 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8126 struct MeshPeerInfo *peer;
8127 struct MeshPeerPath *p;
8132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8133 _("Mesh service could not access hostkey. Exiting.\n"));
8134 GNUNET_SCHEDULER_shutdown ();
8137 my_private_key = pk;
8138 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8139 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8140 &my_full_id.hashPubKey);
8141 myid = GNUNET_PEER_intern (&my_full_id);
8142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8143 "Mesh for peer [%s] starting\n",
8144 GNUNET_i2s(&my_full_id));
8146 // transport_handle = GNUNET_TRANSPORT_connect(c,
8156 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8159 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8160 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8161 GNUNET_SERVER_disconnect_notify (server_handle,
8162 &handle_local_client_disconnect, NULL);
8166 clients_tail = NULL;
8169 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8170 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8172 /* Create a peer_info for the local peer */
8173 peer = peer_info_get (&my_full_id);
8176 GNUNET_PEER_change_rc (myid, 1);
8177 peer_info_add_path (peer, p, GNUNET_YES);
8178 GNUNET_SERVER_resume (server_handle);
8179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8184 * Process mesh requests.
8186 * @param cls closure
8187 * @param server the initialized server
8188 * @param c configuration to use
8191 run (void *cls, struct GNUNET_SERVER_Handle *server,
8192 const struct GNUNET_CONFIGURATION_Handle *c)
8196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8197 server_handle = server;
8198 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8199 NULL, /* Closure passed to MESH functions */
8200 &core_init, /* Call core_init once connected */
8201 &core_connect, /* Handle connects */
8202 &core_disconnect, /* remove peers on disconnects */
8203 NULL, /* Don't notify about all incoming messages */
8204 GNUNET_NO, /* For header only in notification */
8205 NULL, /* Don't notify about all outbound messages */
8206 GNUNET_NO, /* For header-only out notification */
8207 core_handlers); /* Register these handlers */
8209 if (core_handle == NULL)
8212 GNUNET_SCHEDULER_shutdown ();
8217 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8222 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8224 GNUNET_SCHEDULER_shutdown ();
8229 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8230 &refresh_path_time))
8232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8234 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8235 "mesh", "refresh path time");
8236 GNUNET_SCHEDULER_shutdown ();
8241 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8242 &app_announce_time))
8244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8246 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8247 "mesh", "app announce time");
8248 GNUNET_SCHEDULER_shutdown ();
8253 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8256 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8258 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8259 "mesh", "id announce time");
8260 GNUNET_SCHEDULER_shutdown ();
8268 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8269 &unacknowledged_wait_time))
8271 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8273 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8274 "mesh", "unacknowledged wait time");
8275 GNUNET_SCHEDULER_shutdown ();
8280 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8285 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8286 "mesh", "connect timeout");
8287 GNUNET_SCHEDULER_shutdown ();
8292 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8297 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8298 "mesh", "max msgs queue");
8299 GNUNET_SCHEDULER_shutdown ();
8304 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8309 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8310 "mesh", "max tunnels");
8311 GNUNET_SCHEDULER_shutdown ();
8316 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8319 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8321 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8322 "mesh", "default ttl", 64);
8327 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8328 &dht_replication_level))
8330 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8332 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8333 "mesh", "dht replication level", 10);
8334 dht_replication_level = 10;
8337 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8338 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8339 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8340 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8341 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8343 dht_handle = GNUNET_DHT_connect (c, 64);
8344 if (NULL == dht_handle)
8348 stats = GNUNET_STATISTICS_create ("mesh", c);
8350 GNUNET_SERVER_suspend (server_handle);
8351 /* Scheduled the task to clean up when shutdown is called */
8352 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8354 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8355 GNUNET_free (keyfile);
8360 * The main function for the mesh service.
8362 * @param argc number of arguments from the command line
8363 * @param argv command line arguments
8364 * @return 0 ok, 1 on error
8367 main (int argc, char *const *argv)
8372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8373 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8375 ret = (GNUNET_OK == r) ? 0 : 1;
8376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8380 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8381 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8382 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);