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 DEBUG_DHT (" 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 DEBUG_DHT (" 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 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1773 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
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 = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1787 DEBUG_DHT ("Finished PUT for regex\n");
1794 * Periodically announce what applications are provided by local clients
1797 * @param cls closure
1798 * @param tc task context
1801 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1803 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1805 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1809 DEBUG_DHT ("Starting PUT for apps\n");
1811 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1813 announce_applications_task =
1814 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1816 DEBUG_DHT ("Finished PUT for apps\n");
1823 * Periodically announce self id in the DHT
1825 * @param cls closure
1826 * @param tc task context
1829 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1831 struct PBlock block;
1833 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1835 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1839 * - Set data expiration in function of X
1840 * - Adapt X to churn
1842 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1844 block.id = my_full_id;
1845 block.type = htonl (0);
1846 GNUNET_DHT_put (dht_handle, /* DHT handle */
1847 &my_full_id.hashPubKey, /* Key to use */
1848 dht_replication_level, /* Replication level */
1849 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1850 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1851 sizeof (block), /* Size of the data */
1852 (const char *) &block, /* Data itself */
1853 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1854 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1855 NULL, /* Continuation */
1856 NULL); /* Continuation closure */
1858 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1862 /******************************************************************************/
1863 /****************** GENERAL HELPER FUNCTIONS ************************/
1864 /******************************************************************************/
1867 * Decrements the reference counter and frees all resources if needed
1869 * @param mesh_data Data Descriptor used in a multicast message.
1870 * Freed no longer needed (last message).
1873 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1875 if (0 == --(mesh_data->reference_counter))
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1878 GNUNET_free (mesh_data->data);
1879 GNUNET_free (mesh_data);
1885 * Check if client has registered with the service and has not disconnected
1887 * @param client the client to check
1889 * @return non-NULL if client exists in the global DLL
1891 static struct MeshClient *
1892 client_get (struct GNUNET_SERVER_Client *client)
1894 struct MeshClient *c;
1899 if (c->handle == client)
1908 * Checks if a given client has subscribed to certain message type
1910 * @param message_type Type of message to check
1911 * @param c Client to check
1913 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1915 * FIXME: use of crypto_hash slows it down
1916 * The hash function alone takes 8-10us out of the ~55us for the whole
1917 * process of retransmitting the message from one local client to another.
1918 * Find faster implementation!
1921 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1923 struct GNUNET_HashCode hc;
1925 if (NULL == c->types)
1928 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1929 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1934 * Check whether client wants traffic from a tunnel.
1936 * @param c Client to check.
1937 * @param t Tunnel to be found.
1939 * @return GNUNET_YES if client knows tunnel.
1941 * TODO look in client hashmap
1944 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1948 for (i = 0; i < t->nclients; i++)
1949 if (t->clients[i] == c)
1956 * Check whether client has been informed about a tunnel.
1958 * @param c Client to check.
1959 * @param t Tunnel to be found.
1961 * @return GNUNET_YES if client knows tunnel.
1963 * TODO look in client hashmap
1966 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1970 for (i = 0; i < t->nignore; i++)
1971 if (t->ignore[i] == c)
1973 return client_wants_tunnel(c, t);
1978 * Marks a client as uninterested in traffic from the tunnel, updating both
1979 * client and tunnel to reflect this.
1981 * @param c Client that doesn't want traffic anymore.
1982 * @param t Tunnel which should be ignored.
1984 * FIXME when to delete an incoming tunnel?
1987 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1989 struct GNUNET_HashCode hash;
1991 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1992 GNUNET_break (GNUNET_YES ==
1993 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1995 GNUNET_break (GNUNET_YES ==
1996 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1997 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1998 tunnel_delete_active_client (t, c);
1999 GNUNET_array_append (t->ignore, t->nignore, c);
2004 * Deletes a tunnel from a client (either owner or destination). To be used on
2005 * tunnel destroy, otherwise, use client_ignore_tunnel.
2007 * @param c Client whose tunnel to delete.
2008 * @param t Tunnel which should be deleted.
2011 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2013 struct GNUNET_HashCode hash;
2017 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2018 GNUNET_assert (GNUNET_YES ==
2019 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2025 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2027 GNUNET_assert (GNUNET_YES ==
2028 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2032 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2040 * Send the message to all clients that have subscribed to its type
2042 * @param msg Pointer to the message itself
2043 * @param payload Pointer to the payload of the message.
2044 * @param t The tunnel to whose clients this message goes.
2046 * @return number of clients this message was sent to
2049 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2050 const struct GNUNET_MessageHeader *payload,
2051 struct MeshTunnel *t)
2053 struct MeshClient *c;
2054 MESH_TunnelNumber *tid;
2057 char cbuf[htons (msg->size)];
2059 type = ntohs (payload->type);
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2062 GNUNET_MESH_DEBUG_M2S (type));
2064 memcpy (cbuf, msg, sizeof (cbuf));
2065 switch (htons (msg->type))
2067 struct GNUNET_MESH_Unicast *uc;
2068 struct GNUNET_MESH_Multicast *mc;
2069 struct GNUNET_MESH_ToOrigin *to;
2071 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2072 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2075 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2076 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2079 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2080 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2088 for (count = 0, c = clients; c != NULL; c = c->next)
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2091 if (client_is_subscribed (type, c))
2093 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2097 *tid = htonl (t->local_tid);
2101 if (GNUNET_NO == client_knows_tunnel (c, t))
2103 /* This client doesn't know the tunnel */
2104 struct GNUNET_MESH_TunnelNotification tmsg;
2105 struct GNUNET_HashCode hash;
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2108 tmsg.header.size = htons (sizeof (tmsg));
2109 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2110 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2111 tmsg.tunnel_id = htonl (t->local_tid_dest);
2113 if (GNUNET_YES == t->speed_min)
2114 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2115 if (GNUNET_YES == t->nobuffer)
2116 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2117 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2118 &tmsg.header, GNUNET_NO);
2119 tunnel_add_client (t, c);
2120 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2122 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2123 c->incoming_tunnels, &hash, t,
2124 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2126 *tid = htonl (t->local_tid_dest);
2129 /* Check if the client wants to get traffic from the tunnel */
2130 if (GNUNET_NO == client_wants_tunnel(c, t))
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2134 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2135 (struct GNUNET_MessageHeader
2136 *) cbuf, GNUNET_NO);
2145 * Notify the client that owns the tunnel that a peer has connected to it
2146 * (the requested path to it has been confirmed).
2148 * @param t Tunnel whose owner to notify
2149 * @param id Short id of the peer that has connected
2152 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2154 struct GNUNET_MESH_PeerControl pc;
2156 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2157 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2158 pc.tunnel_id = htonl (t->local_tid);
2159 GNUNET_PEER_resolve (id, &pc.peer);
2160 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2166 * Notify all clients (not depending on registration status) that the incoming
2167 * tunnel is no longer valid.
2169 * @param t Tunnel that was destroyed.
2172 send_clients_tunnel_destroy (struct MeshTunnel *t)
2174 struct GNUNET_MESH_TunnelMessage msg;
2176 msg.header.size = htons (sizeof (msg));
2177 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2178 msg.tunnel_id = htonl (t->local_tid_dest);
2179 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2184 * Notify clients of tunnel disconnections, if needed.
2185 * In case the origin disconnects, the destination clients get a tunnel destroy
2186 * notification. If the last destination disconnects (only one remaining client
2187 * in tunnel), the origin gets a (local ID) peer disconnected.
2188 * Note that the function must be called BEFORE removing the client from
2191 * @param t Tunnel that was destroyed.
2192 * @param c Client that disconnected.
2195 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2201 struct GNUNET_MESH_TunnelMessage msg;
2203 msg.header.size = htons (sizeof (msg));
2204 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2205 msg.tunnel_id = htonl (t->local_tid_dest);
2206 for (i = 0; i < t->nclients; i++)
2207 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2208 &msg.header, GNUNET_NO);
2210 // FIXME when to disconnect an incoming tunnel?
2211 else if (1 == t->nclients && NULL != t->owner)
2213 struct GNUNET_MESH_PeerControl msg;
2215 msg.header.size = htons (sizeof (msg));
2216 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2217 msg.tunnel_id = htonl (t->local_tid);
2218 msg.peer = my_full_id;
2219 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2220 &msg.header, GNUNET_NO);
2226 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2227 * and insert it in the appropiate structures if the peer is not known yet.
2229 * @param peer Full identity of the peer.
2231 * @return Existing or newly created peer info.
2233 static struct MeshPeerInfo *
2234 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2236 struct MeshPeerInfo *peer_info;
2238 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2239 if (NULL == peer_info)
2242 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2243 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2244 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2245 peer_info->id = GNUNET_PEER_intern (peer);
2253 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2254 * and insert it in the appropiate structures if the peer is not known yet.
2256 * @param peer Short identity of the peer.
2258 * @return Existing or newly created peer info.
2260 static struct MeshPeerInfo *
2261 peer_info_get_short (const GNUNET_PEER_Id peer)
2263 struct GNUNET_PeerIdentity id;
2265 GNUNET_PEER_resolve (peer, &id);
2266 return peer_info_get (&id);
2271 * Iterator to remove the tunnel from the list of tunnels a peer participates
2274 * @param cls Closure (tunnel info)
2275 * @param key GNUNET_PeerIdentity of the peer (unused)
2276 * @param value PeerInfo of the peer
2278 * @return always GNUNET_YES, to keep iterating
2281 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2283 struct MeshTunnel *t = cls;
2284 struct MeshPeerInfo *peer = value;
2287 for (i = 0; i < peer->ntunnels; i++)
2290 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2293 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2294 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2303 * Core callback to write a pre-constructed data packet to core buffer
2305 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2306 * @param size Number of bytes available in buf.
2307 * @param buf Where the to write the message.
2309 * @return number of bytes written to buf
2312 send_core_data_raw (void *cls, size_t size, void *buf)
2314 struct MeshTransmissionDescriptor *info = cls;
2315 struct GNUNET_MessageHeader *msg;
2318 GNUNET_assert (NULL != info);
2319 GNUNET_assert (NULL != info->mesh_data);
2320 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2321 total_size = ntohs (msg->size);
2323 if (total_size > size)
2328 memcpy (buf, msg, total_size);
2329 data_descriptor_decrement_rc (info->mesh_data);
2336 * Sends an already built non-multicast message to a peer,
2337 * properly registrating all used resources.
2339 * @param message Message to send. Function makes a copy of it.
2340 * @param peer Short ID of the neighbor whom to send the message.
2341 * @param t Tunnel on which this message is transmitted.
2344 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2345 const struct GNUNET_PeerIdentity *peer,
2346 struct MeshTunnel *t)
2348 struct MeshTransmissionDescriptor *info;
2349 struct MeshPeerInfo *neighbor;
2350 struct MeshPeerPath *p;
2354 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2356 size = ntohs (message->size);
2357 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2358 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2359 info->mesh_data->data = GNUNET_malloc (size);
2360 memcpy (info->mesh_data->data, message, size);
2361 type = ntohs(message->type);
2364 struct GNUNET_MESH_Unicast *m;
2365 struct GNUNET_MESH_ToOrigin *to;
2367 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2368 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2369 m->ttl = htonl (ntohl (m->ttl) - 1);
2371 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2372 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2374 to->pid = htonl(t->bck_pid);
2376 info->mesh_data->data_len = size;
2377 info->mesh_data->reference_counter = 1;
2378 info->mesh_data->total_out = 1;
2379 neighbor = peer_info_get (peer);
2380 for (p = neighbor->path_head; NULL != p; p = p->next)
2390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2391 " %s IS NOT DIRECTLY CONNECTED\n",
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2396 for (p = neighbor->path_head; NULL != p; p = p->next)
2398 struct GNUNET_PeerIdentity debug_id;
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 " path with %u hops through:\n",
2404 for (i = 0; i < p->length; i++)
2406 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2409 i, GNUNET_i2s(&debug_id));
2413 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2414 GNUNET_free (info->mesh_data->data);
2415 GNUNET_free (info->mesh_data);
2419 info->peer = neighbor;
2420 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2431 * Sends a CREATE PATH message for a path to a peer, properly registrating
2432 * all used resources.
2434 * @param peer PeerInfo of the final peer for whom this path is being created.
2435 * @param p Path itself.
2436 * @param t Tunnel for which the path is created.
2439 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2440 struct MeshTunnel *t)
2442 struct GNUNET_PeerIdentity id;
2443 struct MeshPathInfo *path_info;
2444 struct MeshPeerInfo *neighbor;
2450 p = tree_get_path_to_peer (t->tree, peer->id);
2457 for (i = 0; i < p->length; i++)
2459 if (p->peers[i] == myid)
2462 if (i >= p->length - 1)
2468 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2470 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2471 path_info->path = p;
2473 neighbor = peer_info_get (&id);
2474 path_info->peer = neighbor;
2475 queue_add (path_info,
2476 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2477 sizeof (struct GNUNET_MESH_ManipulatePath) +
2478 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2485 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2487 * @param t Tunnel whose path to destroy.
2488 * @param destination Short ID of the peer to whom the path to destroy.
2491 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2493 struct MeshPeerPath *p;
2496 p = tree_get_path_to_peer (t->tree, destination);
2502 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2503 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2505 struct GNUNET_MESH_ManipulatePath *msg;
2506 struct GNUNET_PeerIdentity *pi;
2510 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2511 msg->header.size = htons (size);
2512 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2513 msg->tid = htonl (t->id.tid);
2514 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2515 for (i = 0; i < p->length; i++)
2517 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2519 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2526 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2528 * @param t Tunnel which to confirm.
2531 send_path_ack (struct MeshTunnel *t)
2533 struct MeshTransmissionDescriptor *info;
2534 struct GNUNET_PeerIdentity id;
2535 GNUNET_PEER_Id peer;
2537 peer = tree_get_predecessor (t->tree);
2538 GNUNET_PEER_resolve (peer, &id);
2539 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2540 info->origin = &t->id;
2541 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2542 GNUNET_assert (NULL != info->peer);
2545 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2546 sizeof (struct GNUNET_MESH_PathACK),
2553 * Try to establish a new connection to this peer.
2554 * Use the best path for the given tunnel.
2555 * If the peer doesn't have any path to it yet, try to get one.
2556 * If the peer already has some path, send a CREATE PATH towards it.
2558 * @param peer PeerInfo of the peer.
2559 * @param t Tunnel for which to create the path, if possible.
2562 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2564 struct MeshPeerPath *p;
2565 struct MeshPathInfo *path_info;
2567 if (NULL != peer->path_head)
2569 p = tree_get_path_to_peer (t->tree, peer->id);
2576 // FIXME always send create path to self
2579 send_create_path (peer, p, t);
2583 struct GNUNET_HashCode hash;
2586 send_client_peer_connected (t, myid);
2587 t->local_tid_dest = next_local_tid++;
2588 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2591 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2592 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2599 else if (NULL == peer->dhtget)
2601 struct GNUNET_PeerIdentity id;
2603 GNUNET_PEER_resolve (peer->id, &id);
2604 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2605 path_info->peer = peer;
2607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2608 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2609 peer->dhtgetcls = path_info;
2610 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2611 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2612 &id.hashPubKey, /* key to search */
2613 dht_replication_level, /* replication level */
2614 GNUNET_DHT_RO_RECORD_ROUTE |
2615 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2616 NULL, /* xquery */ // FIXME BLOOMFILTER
2617 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2618 &dht_get_id_handler, path_info);
2620 /* Otherwise, there is no path but the DHT get is already started. */
2625 * Task to delay the connection of a peer
2627 * @param cls Closure (path info with tunnel and peer to connect).
2628 * Will be free'd on exection.
2629 * @param tc TaskContext
2632 peer_info_connect_task (void *cls,
2633 const struct GNUNET_SCHEDULER_TaskContext *tc)
2635 struct MeshPathInfo *path_info = cls;
2637 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2639 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2644 peer_info_connect (path_info->peer, path_info->t);
2650 * Destroy the peer_info and free any allocated resources linked to it
2652 * @param pi The peer_info to destroy.
2654 * @return GNUNET_OK on success
2657 peer_info_destroy (struct MeshPeerInfo *pi)
2659 struct GNUNET_PeerIdentity id;
2660 struct MeshPeerPath *p;
2661 struct MeshPeerPath *nextp;
2663 GNUNET_PEER_resolve (pi->id, &id);
2664 GNUNET_PEER_change_rc (pi->id, -1);
2667 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2670 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2671 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2673 if (NULL != pi->dhtget)
2675 GNUNET_DHT_get_stop (pi->dhtget);
2676 GNUNET_free (pi->dhtgetcls);
2682 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2686 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2688 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2696 * Remove all paths that rely on a direct connection between p1 and p2
2697 * from the peer itself and notify all tunnels about it.
2699 * @param peer PeerInfo of affected peer.
2700 * @param p1 GNUNET_PEER_Id of one peer.
2701 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2704 * TODO: optimize (see below)
2707 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2710 struct MeshPeerPath *p;
2711 struct MeshPeerPath *aux;
2712 struct MeshPeerInfo *peer_d;
2714 unsigned int destroyed;
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2721 p = peer->path_head;
2725 for (i = 0; i < (p->length - 1); i++)
2727 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2728 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2730 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2741 for (i = 0; i < peer->ntunnels; i++)
2743 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2747 * Problem: one or more peers have been deleted from the tunnel tree.
2748 * We don't know who they are to try to add them again.
2749 * We need to try to find a new path for each of the disconnected peers.
2750 * Some of them might already have a path to reach them that does not
2751 * involve p1 and p2. Adding all anew might render in a better tree than
2752 * the trivial immediate fix.
2754 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2755 * its children will be reachable trough him.
2757 peer_d = peer_info_get_short (d);
2760 for (p = peer_d->path_head; NULL != p; p = p->next)
2762 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2770 /* No callback, as peer will be already disconnected and a connection
2771 * scheduled by tunnel_notify_connection_broken.
2773 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2777 peer_info_connect (peer_d, peer->tunnels[i]);
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2785 * Add the path to the peer and update the path used to reach it in case this
2788 * @param peer_info Destination peer to add the path to.
2789 * @param path New path to add. Last peer must be the peer in arg 1.
2790 * Path will be either used of freed if already known.
2791 * @param trusted Do we trust that this path is real?
2794 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2797 struct MeshPeerPath *aux;
2801 if ((NULL == peer_info) || (NULL == path))
2804 path_destroy (path);
2807 if (path->peers[path->length - 1] != peer_info->id)
2810 path_destroy (path);
2813 if (path->length <= 2 && GNUNET_NO == trusted)
2815 /* Only allow CORE to tell us about direct paths */
2816 path_destroy (path);
2819 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2820 for (l = 1; l < path->length; l++)
2822 if (path->peers[l] == myid)
2824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2825 for (l2 = 0; l2 < path->length - l; l2++)
2827 path->peers[l2] = path->peers[l + l2];
2832 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2837 struct GNUNET_PeerIdentity id;
2839 GNUNET_PEER_resolve (peer_info->id, &id);
2840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2841 path->length, GNUNET_i2s (&id));
2844 l = path_get_length (path);
2851 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2852 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2854 l2 = path_get_length (aux);
2857 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2858 peer_info->path_tail, aux, path);
2863 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2865 path_destroy (path);
2870 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2877 * Add the path to the origin peer and update the path used to reach it in case
2878 * this is the shortest.
2879 * The path is given in peer_info -> destination, therefore we turn the path
2880 * upside down first.
2882 * @param peer_info Peer to add the path to, being the origin of the path.
2883 * @param path New path to add after being inversed.
2884 * @param trusted Do we trust that this path is real?
2887 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2888 struct MeshPeerPath *path, int trusted)
2891 peer_info_add_path (peer_info, path, trusted);
2896 * Function called if the connection to the peer has been stalled for a while,
2897 * possibly due to a missed ACK. Poll the peer about its ACK status.
2899 * @param cls Closure (info about regex search).
2900 * @param tc TaskContext.
2903 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2905 struct MeshTunnelChildInfo *cinfo = cls;
2906 struct GNUNET_MESH_Poll msg;
2907 struct GNUNET_PeerIdentity id;
2908 struct MeshTunnel *t;
2910 return; // FIXME fc activate
2911 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2912 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2918 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2919 msg.header.size = htons (sizeof (msg));
2920 msg.tid = htonl (t->id.tid);
2921 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2922 msg.last_ack = htonl (cinfo->fwd_ack);
2924 GNUNET_PEER_resolve (tree_get_predecessor(cinfo->t->tree), &id);
2925 send_prebuilt_message (&msg.header, &id, cinfo->t);
2926 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2927 &tunnel_poll, cinfo);
2932 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2933 * to obtain a local peer -> destination path and interning the peer ids.
2935 * @return Newly allocated and created path
2937 static struct MeshPeerPath *
2938 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2939 unsigned int get_path_length,
2940 const struct GNUNET_PeerIdentity *put_path,
2941 unsigned int put_path_length)
2943 struct MeshPeerPath *p;
2949 GNUNET_PEER_change_rc (myid, 1);
2950 i = get_path_length;
2951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2952 for (i--; i >= 0; i--)
2954 id = GNUNET_PEER_intern (&get_path[i]);
2955 if (p->length > 0 && id == p->peers[p->length - 1])
2957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2958 GNUNET_PEER_change_rc (id, -1);
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2963 GNUNET_i2s (&get_path[i]));
2965 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2966 p->peers[p->length - 1] = id;
2969 i = put_path_length;
2970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2971 for (i--; i >= 0; i--)
2973 id = GNUNET_PEER_intern (&put_path[i]);
2976 /* PUT path went through us, so discard the path up until now and start
2977 * from here to get a much shorter (and loop-free) path.
2982 if (p->length > 0 && id == p->peers[p->length - 1])
2984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2985 GNUNET_PEER_change_rc (id, -1);
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2990 GNUNET_i2s (&put_path[i]));
2992 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2993 p->peers[p->length - 1] = id;
2997 if (get_path_length > 0)
2998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2999 GNUNET_i2s (&get_path[0]));
3000 if (put_path_length > 0)
3001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3002 GNUNET_i2s (&put_path[0]));
3003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3005 for (i = 0; i < p->length; i++)
3007 struct GNUNET_PeerIdentity peer_id;
3009 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3011 GNUNET_i2s (&peer_id));
3019 * Adds a path to the peer_infos of all the peers in the path
3021 * @param p Path to process.
3022 * @param confirmed Whether we know if the path works or not.
3025 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3029 /* TODO: invert and add */
3030 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3031 for (i++; i < p->length; i++)
3033 struct MeshPeerInfo *aux;
3034 struct MeshPeerPath *copy;
3036 aux = peer_info_get_short (p->peers[i]);
3037 copy = path_duplicate (p);
3038 copy->length = i + 1;
3039 peer_info_add_path (aux, copy, GNUNET_NO);
3045 * Send keepalive packets for a peer
3047 * @param cls Closure (tunnel for which to send the keepalive).
3048 * @param tc Notification context.
3050 * TODO: implement explicit multicast keepalive?
3053 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3057 * Search for a tunnel among the incoming tunnels
3059 * @param tid the local id of the tunnel
3061 * @return tunnel handler, NULL if doesn't exist
3063 static struct MeshTunnel *
3064 tunnel_get_incoming (MESH_TunnelNumber tid)
3066 struct GNUNET_HashCode hash;
3068 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3069 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3070 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3075 * Search for a tunnel among the tunnels for a client
3077 * @param c the client whose tunnels to search in
3078 * @param tid the local id of the tunnel
3080 * @return tunnel handler, NULL if doesn't exist
3082 static struct MeshTunnel *
3083 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3085 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3087 return tunnel_get_incoming (tid);
3091 struct GNUNET_HashCode hash;
3093 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3094 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3100 * Search for a tunnel by global ID using PEER_ID
3102 * @param pi owner of the tunnel
3103 * @param tid global tunnel number
3105 * @return tunnel handler, NULL if doesn't exist
3107 static struct MeshTunnel *
3108 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3110 struct MESH_TunnelID id;
3111 struct GNUNET_HashCode hash;
3116 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3117 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3122 * Search for a tunnel by global ID using full PeerIdentities
3124 * @param oid owner of the tunnel
3125 * @param tid global tunnel number
3127 * @return tunnel handler, NULL if doesn't exist
3129 static struct MeshTunnel *
3130 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3132 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3137 * Delete an active client from the tunnel.
3143 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3147 for (i = 0; i < t->nclients; i++)
3149 if (t->clients[i] == c)
3151 t->clients[i] = t->clients[t->nclients - 1];
3152 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3153 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3155 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3163 * Delete an ignored client from the tunnel.
3169 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3173 for (i = 0; i < t->nignore; i++)
3175 if (t->ignore[i] == c)
3177 t->ignore[i] = t->ignore[t->nignore - 1];
3178 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3186 * Delete a client from the tunnel. It should be only done on
3187 * client disconnection, otherwise use client_ignore_tunnel.
3193 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3195 tunnel_delete_ignored_client (t, c);
3196 tunnel_delete_active_client (t, c);
3201 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3203 * Destroys queue elements of all waiting transmissions and frees all memory
3204 * used by the struct and its elements.
3206 * @param cls Closure (tunnel info).
3207 * @param key Hash of GNUNET_PEER_Id (unused).
3208 * @param value MeshTunnelChildInfo of the child.
3210 * @return always GNUNET_YES, to keep iterating
3213 tunnel_destroy_child (void *cls,
3214 const struct GNUNET_HashCode * key,
3217 struct MeshTunnelChildInfo *cinfo = value;
3218 struct MeshTunnel *t = cls;
3219 struct MeshPeerQueue *q;
3223 for (c = 0; c < cinfo->send_buffer_n; c++)
3225 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3226 q = cinfo->send_buffer[i];
3227 cinfo->send_buffer[i] = NULL;
3229 queue_destroy (q, GNUNET_YES);
3232 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3234 GNUNET_free_non_null (cinfo->send_buffer);
3235 GNUNET_free (cinfo);
3241 * Callback used to notify a client owner of a tunnel that a peer has
3242 * disconnected, most likely because of a path change.
3244 * @param cls Closure (tunnel this notification is about).
3245 * @param peer_id Short ID of disconnected peer.
3248 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3250 struct MeshTunnel *t = cls;
3251 struct MeshPeerInfo *peer;
3252 struct MeshPathInfo *path_info;
3254 if (NULL != t->owner && NULL != nc)
3256 struct GNUNET_MESH_PeerControl msg;
3258 msg.header.size = htons (sizeof (msg));
3259 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3260 msg.tunnel_id = htonl (t->local_tid);
3261 GNUNET_PEER_resolve (peer_id, &msg.peer);
3262 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3263 &msg.header, GNUNET_NO);
3265 peer = peer_info_get_short (peer_id);
3266 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3267 path_info->peer = peer;
3269 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3275 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3276 * needed rescources.
3277 * If peer already exists, reevaluate shortest path and change if different.
3279 * @param t Tunnel we want to add a new peer to
3280 * @param peer PeerInfo of the peer being added
3284 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3286 struct GNUNET_PeerIdentity id;
3287 struct MeshPeerPath *best_p;
3288 struct MeshPeerPath *p;
3289 unsigned int best_cost;
3292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3293 GNUNET_PEER_resolve (peer->id, &id);
3295 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3298 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3299 GNUNET_assert (GNUNET_OK ==
3300 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3302 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3305 if (NULL != (p = peer->path_head))
3308 best_cost = tree_get_path_cost (t->tree, p);
3311 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3318 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3319 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3320 t->path_refresh_task =
3321 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3325 /* Start a DHT get */
3326 peer_info_connect (peer, t);
3328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3332 * Add a path to a tunnel which we don't own, just to remember the next hop.
3333 * If destination node was already in the tunnel, the first hop information
3334 * will be replaced with the new path.
3336 * @param t Tunnel we want to add a new peer to
3337 * @param p Path to add
3338 * @param own_pos Position of local node in path.
3342 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3343 unsigned int own_pos)
3345 struct GNUNET_PeerIdentity id;
3347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3348 GNUNET_assert (0 != own_pos);
3349 tree_add_path (t->tree, p, NULL, NULL);
3350 if (own_pos < p->length - 1)
3352 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3353 tree_update_first_hops (t->tree, myid, &id);
3355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3359 * Add a client to a tunnel, initializing all needed data structures.
3361 * @param t Tunnel to which add the client.
3362 * @param c Client which to add to the tunnel.
3365 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3367 struct MeshTunnelClientInfo clinfo;
3369 GNUNET_array_append (t->clients, t->nclients, c);
3370 clinfo.fwd_ack = t->fwd_pid + 1;
3371 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3372 clinfo.fwd_pid = t->fwd_pid;
3373 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3375 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3380 * Notifies a tunnel that a connection has broken that affects at least
3381 * some of its peers. Sends a notification towards the root of the tree.
3382 * In case the peer is the owner of the tree, notifies the client that owns
3383 * the tunnel and tries to reconnect.
3385 * @param t Tunnel affected.
3386 * @param p1 Peer that got disconnected from p2.
3387 * @param p2 Peer that got disconnected from p1.
3389 * @return Short ID of the peer disconnected (either p1 or p2).
3390 * 0 if the tunnel remained unaffected.
3392 static GNUNET_PEER_Id
3393 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3399 tree_notify_connection_broken (t->tree, p1, p2,
3400 &tunnel_notify_client_peer_disconnected,
3402 if (myid != p1 && myid != p2)
3408 if (tree_get_predecessor (t->tree) != 0)
3410 /* We are the peer still connected, notify owner of the disconnection. */
3411 struct GNUNET_MESH_PathBroken msg;
3412 struct GNUNET_PeerIdentity neighbor;
3414 msg.header.size = htons (sizeof (msg));
3415 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3416 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3417 msg.tid = htonl (t->id.tid);
3418 msg.peer1 = my_full_id;
3419 GNUNET_PEER_resolve (pid, &msg.peer2);
3420 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3421 send_prebuilt_message (&msg.header, &neighbor, t);
3429 * Send a multicast packet to a neighbor.
3431 * @param cls Closure (Info about the multicast packet)
3432 * @param neighbor_id Short ID of the neighbor to send the packet to.
3435 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3437 struct MeshData *mdata = cls;
3438 struct MeshTransmissionDescriptor *info;
3439 struct GNUNET_PeerIdentity neighbor;
3440 struct GNUNET_MessageHeader *msg;
3442 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3444 info->mesh_data = mdata;
3445 (mdata->reference_counter) ++;
3446 info->destination = neighbor_id;
3447 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3449 GNUNET_i2s (&neighbor));
3450 info->peer = peer_info_get (&neighbor);
3451 GNUNET_assert (NULL != info->peer);
3452 msg = (struct GNUNET_MessageHeader *) mdata->data;
3455 info->mesh_data->data_len,
3462 * Queue a message in a tunnel in multicast, sending a copy to each child node
3463 * down the local one in the tunnel tree.
3465 * @param t Tunnel in which to send the data.
3466 * @param msg Message to be sent.
3469 tunnel_send_multicast (struct MeshTunnel *t,
3470 const struct GNUNET_MessageHeader *msg)
3472 struct MeshData *mdata;
3474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3475 " sending a multicast packet...\n");
3477 mdata = GNUNET_malloc (sizeof (struct MeshData));
3478 mdata->data_len = ntohs (msg->size);
3480 mdata->data = GNUNET_malloc (mdata->data_len);
3481 memcpy (mdata->data, msg, mdata->data_len);
3482 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3484 struct GNUNET_MESH_Multicast *mcast;
3486 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3487 if (t->fwd_queue_n >= t->fwd_queue_max)
3490 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3492 " message from %s!\n",
3493 GNUNET_i2s(&mcast->oid));
3494 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3495 " message at %s!\n",
3496 GNUNET_i2s(&my_full_id));
3497 GNUNET_free (mdata->data);
3498 GNUNET_free (mdata);
3502 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3504 ntohl (mcast->ttl));
3508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3511 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3512 if (mdata->reference_counter == 0)
3514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3515 " no one to send data to\n");
3516 GNUNET_free (mdata->data);
3517 GNUNET_free (mdata);
3522 mdata->total_out = mdata->reference_counter;
3524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3525 " sending a multicast packet done\n");
3531 * Increase the SKIP value of all peers that
3532 * have not received a unicast message.
3534 * @param cls Closure (ID of the peer that HAS received the message).
3535 * @param key ID of the neighbor.
3536 * @param value Information about the neighbor.
3538 * @return GNUNET_YES to keep iterating.
3541 tunnel_add_skip (void *cls,
3542 const struct GNUNET_HashCode * key,
3545 struct GNUNET_PeerIdentity *neighbor = cls;
3546 struct MeshTunnelChildInfo *cinfo = value;
3548 /* TODO compare only pointers? key == neighbor? */
3549 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3559 * @brief Get neighbor's Flow Control information.
3561 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3562 * descendant of the local node in a certain tunnel.
3563 * If the info is not yet there (recently created path), creates the data struct
3564 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3567 * @param t Tunnel related.
3568 * @param peer Neighbor whose Flow Control info is needed.
3570 * @return Neighbor's Flow Control info.
3572 static struct MeshTunnelChildInfo *
3573 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3574 const struct GNUNET_PeerIdentity *peer)
3576 struct MeshTunnelChildInfo *cinfo;
3578 if (NULL == t->children_fc)
3581 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3587 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3588 cinfo->id = GNUNET_PEER_intern (peer);
3589 cinfo->skip = t->fwd_pid;
3592 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3593 cinfo->fwd_ack = t->fwd_pid + delta;
3594 cinfo->bck_ack = delta;
3595 cinfo->bck_pid = -1;
3597 cinfo->send_buffer =
3598 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3600 GNUNET_assert (GNUNET_OK ==
3601 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3604 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3611 * Get the Flow Control info of a client.
3613 * @param t Tunnel on which to look.
3614 * @param c Client whose ACK to get.
3616 * @return ACK value.
3618 static struct MeshTunnelClientInfo *
3619 tunnel_get_client_fc (struct MeshTunnel *t,
3620 struct MeshClient *c)
3624 for (i = 0; i < t->nclients; i++)
3626 if (t->clients[i] != c)
3628 return &t->clients_fc[i];
3631 return NULL; // avoid compiler / coverity complaints
3636 * Iterator to get the appropiate ACK value from all children nodes.
3638 * @param cls Closue (tunnel).
3639 * @param id Id of the child node.
3642 tunnel_get_child_fwd_ack (void *cls,
3645 struct GNUNET_PeerIdentity peer_id;
3646 struct MeshTunnelChildInfo *cinfo;
3647 struct MeshTunnelChildIteratorContext *ctx = cls;
3648 struct MeshTunnel *t = ctx->t;
3651 GNUNET_PEER_resolve (id, &peer_id);
3652 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3653 ack = cinfo->fwd_ack;
3656 if (GNUNET_NO == ctx->init)
3658 ctx->max_child_ack = ack;
3659 ctx->init = GNUNET_YES;
3662 if (GNUNET_YES == t->speed_min)
3664 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3668 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3675 * Get the maximum PID allowed to transmit to any
3676 * tunnel child of the local peer, depending on the tunnel
3677 * buffering/speed settings.
3681 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3684 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3686 struct MeshTunnelChildIteratorContext ctx;
3688 ctx.max_child_ack = 0;
3690 ctx.init = GNUNET_NO;
3691 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3693 if (0 == ctx.nchildren)
3695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3696 " tunnel has no children, no FWD ACK\n");
3700 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3701 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3703 return (int64_t) ctx.max_child_ack;
3708 * Set the FWD ACK value of a client in a particular tunnel.
3710 * @param t Tunnel affected.
3711 * @param c Client whose ACK to set.
3712 * @param ack ACK value.
3715 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3716 struct MeshClient *c,
3721 for (i = 0; i < t->nclients; i++)
3723 if (t->clients[i] != c)
3725 t->clients_fc[i].fwd_ack = ack;
3733 * Get the highest ACK value of all clients in a particular tunnel,
3734 * according to the buffering/speed settings.
3736 * @param t Tunnel on which to look.
3738 * @return Corresponding ACK value (max uint32_t).
3739 * If no clients are suscribed, -1LL.
3742 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3747 if (0 == t->nclients)
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3750 " tunnel has no clients, no FWD ACK\n");
3754 for (ack = -1LL, i = 0; i < t->nclients; i++)
3757 (GNUNET_YES == t->speed_min &&
3758 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3759 (GNUNET_NO == t->speed_min &&
3760 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3762 ack = t->clients_fc[i].fwd_ack;
3766 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3767 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3769 return (uint32_t) ack;
3774 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3775 * mode and the status of all children nodes.
3779 * @return Maximum PID allowed.
3782 tunnel_get_fwd_ack (struct MeshTunnel *t)
3786 uint32_t buffer_free;
3790 count = t->fwd_pid - t->skip;
3791 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3792 child_ack = tunnel_get_children_fwd_ack (t);
3793 client_ack = tunnel_get_clients_fwd_ack (t);
3794 if (GNUNET_YES == t->nobuffer)
3797 if (-1LL == child_ack)
3798 child_ack = client_ack;
3799 if (-1LL == child_ack)
3802 client_ack = child_ack = ack;
3807 ack = count + buffer_free; // Overflow? OK!
3809 if (-1LL == child_ack)
3811 // Node has no children, child_ack AND core buffer are irrelevant.
3812 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3813 return (uint32_t) client_ack;
3815 if (-1LL == client_ack)
3819 if (GNUNET_YES == t->speed_min)
3821 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3822 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3826 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3827 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3830 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3831 count, buffer_free, child_ack, client_ack, ack);
3837 * Build a local ACK message and send it to a local client.
3839 * @param t Tunnel on which to send the ACK.
3840 * @param c Client to whom send the ACK.
3841 * @param ack Value of the ACK.
3844 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3846 struct GNUNET_MESH_LocalAck msg;
3848 msg.header.size = htons (sizeof (msg));
3849 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3850 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3851 msg.max_pid = htonl (ack);
3852 GNUNET_SERVER_notification_context_unicast(nc,
3859 * Build an ACK message and queue it to send to the given peer.
3861 * @param t Tunnel on which to send the ACK.
3862 * @param peer Peer to whom send the ACK.
3863 * @param ack Value of the ACK.
3866 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3868 struct GNUNET_MESH_ACK msg;
3870 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3871 msg.header.size = htons (sizeof (msg));
3872 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3873 msg.pid = htonl (ack);
3874 msg.tid = htonl (t->id.tid);
3876 send_prebuilt_message (&msg.header, peer, t);
3881 * Notify a the owner of a tunnel about how many more
3882 * payload packages will we accept on a given tunnel.
3884 * @param t Tunnel on which to send the ACK.
3887 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3892 "Sending client FWD ACK on tunnel %X\n",
3895 ack = tunnel_get_fwd_ack (t);
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3898 if (t->last_fwd_ack == ack)
3900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3905 t->last_fwd_ack = ack;
3906 send_local_ack (t, t->owner, ack);
3911 * Send an ACK informing the predecessor about the available buffer space.
3912 * In case there is no predecessor, inform the owning client.
3913 * If buffering is off, send only on behalf of children or self if endpoint.
3914 * If buffering is on, send when sent to children and buffer space is free.
3915 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3916 * the ACK itself goes "back" (towards root).
3918 * @param t Tunnel on which to send the ACK.
3919 * @param type Type of message that triggered the ACK transmission.
3922 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3924 struct GNUNET_PeerIdentity id;
3927 if (NULL != t->owner)
3929 tunnel_send_client_fwd_ack (t);
3932 /* Is it after unicast / multicast retransmission? */
3935 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3936 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3938 "ACK due to FWD DATA retransmission\n");
3939 if (GNUNET_YES == t->nobuffer)
3941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3945 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3946 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3952 /* Check if we need no retransmit the ACK */
3953 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3954 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3958 " t->qmax: %u, t->qn: %u\n",
3959 t->fwd_queue_max, t->fwd_queue_n);
3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3961 " t->pid: %u, t->ack: %u\n",
3962 t->fwd_pid, t->last_fwd_ack);
3966 /* Ok, ACK might be necessary, what PID to ACK? */
3967 ack = tunnel_get_fwd_ack (t);
3969 /* If speed_min and not all children have ack'd, dont send yet */
3970 if (ack == t->last_fwd_ack)
3972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3976 t->last_fwd_ack = ack;
3977 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3978 send_ack (t, &id, ack);
3984 * Iterator to send a child node a BCK ACK to allow him to send more
3987 * @param cls Closure (tunnel).
3988 * @param id Id of the child node.
3991 tunnel_send_child_bck_ack (void *cls,
3994 struct MeshTunnel *t = cls;
3995 struct MeshTunnelChildInfo *cinfo;
3996 struct GNUNET_PeerIdentity peer;
3998 GNUNET_PEER_resolve (id, &peer);
3999 cinfo = tunnel_get_neighbor_fc (t, &peer);
4001 if (cinfo->bck_ack != cinfo->bck_pid &&
4002 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->bck_pid))
4004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4005 " Not sending ACK, not needed\n");
4009 cinfo->bck_ack = t->bck_queue_max - t->bck_queue_n + cinfo->bck_pid;
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4011 " Sending BCK ACK %u\n",
4013 send_ack (t, &peer, cinfo->bck_ack);
4018 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4020 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4022 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4023 * - implement a size 1 buffer in each client_fc AND children_fc
4024 * to hold at least 1 message per "child".
4025 * problem: violates no buffer policy
4026 * - ack 0 and make "children" poll for transmission slots
4027 * problem: big overhead, extra latency even in low traffic
4030 * @param t Tunnel on which to send the BCK ACKs.
4033 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4036 unsigned int tunnel_delta;
4038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4040 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4043 /* Find client whom to allow to send to origin (with lowest buffer space) */
4044 for (i = 0; i < t->nclients; i++)
4046 struct MeshTunnelClientInfo *clinfo;
4049 clinfo = &t->clients_fc[i];
4050 delta = clinfo->bck_ack - clinfo->bck_pid;
4051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4052 t->clients[i]->id, delta);
4054 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4055 (GNUNET_YES == t->nobuffer && 0 == delta))
4059 ack = clinfo->bck_pid;
4060 ack += t->nobuffer ? 1 : tunnel_delta;
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4062 " sending ack to client %u: %u\n",
4063 t->clients[i]->id, ack);
4064 send_local_ack (t, t->clients[i], ack);
4065 clinfo->bck_ack = ack;
4069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4070 " not sending ack to client %u (td %u, d %u)\n",
4071 t->clients[i]->id, tunnel_delta, delta);
4078 * Send an ACK informing the children nodes and destination clients about
4079 * the available buffer space.
4080 * If buffering is off, send only on behalf of root (can be self).
4081 * If buffering is on, send when sent to predecessor and buffer space is free.
4082 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4083 * the ACK itself goes "forward" (towards children/clients).
4085 * @param t Tunnel on which to send the ACK.
4086 * @param type Type of message that triggered the ACK transmission.
4089 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4092 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4093 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4094 /* Is it after data to_origin retransmission? */
4097 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4098 if (GNUNET_YES == t->nobuffer)
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4101 " Not sending ACK, nobuffer\n");
4105 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4106 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4107 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4113 tunnel_send_clients_bck_ack (t);
4114 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4119 * @brief Re-initiate traffic to this peer if necessary.
4121 * Check if there is traffic queued towards this peer
4122 * and the core transmit handle is NULL (traffic was stalled).
4123 * If so, call core tmt rdy.
4125 * @param cls Closure (unused)
4126 * @param peer_id Short ID of peer to which initiate traffic.
4129 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4131 struct MeshPeerInfo *peer;
4132 struct GNUNET_PeerIdentity id;
4133 struct MeshPeerQueue *q;
4136 peer = peer_info_get_short(peer_id);
4137 if (NULL != peer->core_transmit)
4140 q = queue_get_next(peer);
4143 /* Might br multicast traffic already sent to this particular peer but
4144 * not to other children in this tunnel.
4145 * This way t->queue_n would be > 0 but the queue of this particular peer
4151 GNUNET_PEER_resolve (peer->id, &id);
4152 peer->core_transmit =
4153 GNUNET_CORE_notify_transmit_ready(core_handle,
4156 GNUNET_TIME_UNIT_FOREVER_REL,
4166 * @brief Allow transmission of FWD traffic on this tunnel
4168 * Check if there is traffic queued towards any children
4169 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4171 * @param t Tunnel on which to unlock FWD traffic.
4174 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4176 if (0 == t->fwd_queue_n)
4179 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4184 * @brief Allow transmission of BCK traffic on this tunnel
4186 * Check if there is traffic queued towards the root of the tree
4187 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4189 * @param t Tunnel on which to unlock BCK traffic.
4192 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4194 if (0 == t->bck_queue_n)
4197 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4202 * Send a message to all peers in this tunnel that the tunnel is no longer
4205 * @param t The tunnel whose peers to notify.
4208 tunnel_send_destroy (struct MeshTunnel *t)
4210 struct GNUNET_MESH_TunnelDestroy msg;
4212 msg.header.size = htons (sizeof (msg));
4213 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4214 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4215 msg.tid = htonl (t->id.tid);
4216 tunnel_send_multicast (t, &msg.header);
4221 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4223 * @param cls Closure (Tunnel which to cancel).
4224 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4227 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4229 struct MeshTunnel *t = cls;
4230 struct MeshPeerInfo *peer_info;
4231 struct MeshPeerQueue *pq;
4232 struct MeshPeerQueue *next;
4234 peer_info = peer_info_get_short (neighbor_id);
4235 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4238 if (pq->tunnel == t)
4240 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4241 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4242 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4244 // Should have been removed on destroy children
4247 queue_destroy (pq, GNUNET_YES);
4250 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4252 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4253 peer_info->core_transmit = NULL;
4258 * Destroy the tunnel and free any allocated resources linked to it.
4260 * @param t the tunnel to destroy
4262 * @return GNUNET_OK on success
4265 tunnel_destroy (struct MeshTunnel *t)
4267 struct MeshClient *c;
4268 struct GNUNET_HashCode hash;
4279 struct GNUNET_PeerIdentity id;
4281 GNUNET_PEER_resolve (t->id.oid, &id);
4282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4283 GNUNET_i2s (&id), t->id.tid);
4285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4289 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4290 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4298 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4300 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4307 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4308 for (i = 0; i < t->nclients; i++)
4312 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4318 for (i = 0; i < t->nignore; i++)
4322 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4329 if (t->nclients > 0)
4332 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4337 GNUNET_free (t->clients);
4338 GNUNET_free (t->clients_fc);
4341 if (NULL != t->peers)
4343 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4345 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4348 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4349 &tunnel_destroy_child,
4351 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4352 t->children_fc = NULL;
4354 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4355 tree_destroy (t->tree);
4357 if (NULL != t->regex_ctx)
4358 regex_cancel_search (t->regex_ctx);
4359 if (NULL != t->dht_get_type)
4360 GNUNET_DHT_get_stop (t->dht_get_type);
4361 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4362 GNUNET_SCHEDULER_cancel (t->timeout_task);
4363 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4364 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4367 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4374 * Create a new tunnel
4376 * @param owner Who is the owner of the tunnel (short ID).
4377 * @param tid Tunnel Number of the tunnel.
4378 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4379 * @param local Tunnel Number for the tunnel, for the client point of view.
4381 * @return A new initialized tunnel. NULL on error.
4383 static struct MeshTunnel *
4384 tunnel_new (GNUNET_PEER_Id owner,
4385 MESH_TunnelNumber tid,
4386 struct MeshClient *client,
4387 MESH_TunnelNumber local)
4389 struct MeshTunnel *t;
4390 struct GNUNET_HashCode hash;
4392 if (n_tunnels >= max_tunnels && NULL == client)
4395 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4398 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4399 t->bck_queue_max = t->fwd_queue_max;
4400 t->tree = tree_new (owner);
4402 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4403 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4404 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4405 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4406 t->local_tid = local;
4407 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4409 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4411 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4413 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4414 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4421 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4428 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4430 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4431 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4435 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4445 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4446 * that are no longer needed, as well as nodes of no longer reachable peers.
4447 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4449 * @param t Tunnel from which to remove the path.
4450 * @param peer Short id of the peer which should be removed.
4453 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4455 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4461 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4462 * client when the client disconnects. If the client is not the owner, the
4463 * owner will get notified if no more clients are in the tunnel and the client
4464 * get removed from the tunnel's list.
4466 * @param cls closure (client that is disconnecting)
4467 * @param key the hash of the local tunnel id (used to access the hashmap)
4468 * @param value the value stored at the key (tunnel to destroy)
4470 * @return GNUNET_OK, keep iterating.
4473 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4475 struct MeshTunnel *t = value;
4476 struct MeshClient *c = cls;
4478 send_client_tunnel_disconnect(t, c);
4481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4482 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4483 tunnel_delete_client(t, c);
4484 client_delete_tunnel(c, t);
4487 tunnel_send_destroy(t);
4489 t->destroy = GNUNET_YES;
4496 * Timeout function, destroys tunnel if called
4498 * @param cls Closure (tunnel to destroy).
4499 * @param tc TaskContext
4502 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4504 struct MeshTunnel *t = cls;
4505 struct GNUNET_PeerIdentity id;
4507 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4508 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4510 GNUNET_PEER_resolve(t->id.oid, &id);
4511 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4512 "Tunnel %s [%X] timed out. Destroying.\n",
4513 GNUNET_i2s(&id), t->id.tid);
4514 send_clients_tunnel_destroy (t);
4519 * Resets the tunnel timeout. Starts it if no timeout was running.
4521 * @param t Tunnel whose timeout to reset.
4523 * TODO use heap to improve efficiency of scheduler.
4526 tunnel_reset_timeout (struct MeshTunnel *t)
4528 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4529 GNUNET_SCHEDULER_cancel (t->timeout_task);
4531 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4532 (refresh_path_time, 4), &tunnel_timeout, t);
4536 /******************************************************************************/
4537 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4538 /******************************************************************************/
4541 * Function to send a create path packet to a peer.
4543 * @param cls closure
4544 * @param size number of bytes available in buf
4545 * @param buf where the callee should write the message
4546 * @return number of bytes written to buf
4549 send_core_path_create (void *cls, size_t size, void *buf)
4551 struct MeshPathInfo *info = cls;
4552 struct GNUNET_MESH_ManipulatePath *msg;
4553 struct GNUNET_PeerIdentity *peer_ptr;
4554 struct MeshTunnel *t = info->t;
4555 struct MeshPeerPath *p = info->path;
4560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4562 sizeof (struct GNUNET_MESH_ManipulatePath) +
4563 p->length * sizeof (struct GNUNET_PeerIdentity);
4565 if (size < size_needed || NULL == buf)
4570 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4571 msg->header.size = htons (size_needed);
4572 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4573 msg->tid = ntohl (t->id.tid);
4576 if (GNUNET_YES == t->speed_min)
4577 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4578 if (GNUNET_YES == t->nobuffer)
4579 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4580 msg->opt = htonl(opt);
4583 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4584 for (i = 0; i < p->length; i++)
4586 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4593 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4599 * Fill the core buffer
4601 * @param cls closure (data itself)
4602 * @param size number of bytes available in buf
4603 * @param buf where the callee should write the message
4605 * @return number of bytes written to buf
4608 send_core_data_multicast (void *cls, size_t size, void *buf)
4610 struct MeshTransmissionDescriptor *info = cls;
4613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4614 GNUNET_assert (NULL != info);
4615 GNUNET_assert (NULL != info->peer);
4616 total_size = info->mesh_data->data_len;
4617 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4619 if (total_size > size)
4624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4625 memcpy (buf, info->mesh_data->data, total_size);
4628 struct GNUNET_MESH_Multicast *mc;
4629 struct GNUNET_MessageHeader *mh;
4632 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4634 mc = (struct GNUNET_MESH_Multicast *) mh;
4635 mh = (struct GNUNET_MessageHeader *) &mc[1];
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4637 " multicast, payload type %s\n",
4638 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4640 " multicast, payload size %u\n", ntohs (mh->size));
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4645 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4649 data_descriptor_decrement_rc (info->mesh_data);
4650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4658 * Creates a path ack message in buf and frees all unused resources.
4660 * @param cls closure (MeshTransmissionDescriptor)
4661 * @param size number of bytes available in buf
4662 * @param buf where the callee should write the message
4663 * @return number of bytes written to buf
4666 send_core_path_ack (void *cls, size_t size, void *buf)
4668 struct MeshTransmissionDescriptor *info = cls;
4669 struct GNUNET_MESH_PathACK *msg = buf;
4671 GNUNET_assert (NULL != info);
4672 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4677 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4678 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4679 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4680 msg->tid = htonl (info->origin->tid);
4681 msg->peer_id = my_full_id;
4684 /* TODO add signature */
4686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4687 return sizeof (struct GNUNET_MESH_PathACK);
4692 * Free a transmission that was already queued with all resources
4693 * associated to the request.
4695 * @param queue Queue handler to cancel.
4696 * @param clear_cls Is it necessary to free associated cls?
4699 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4701 struct MeshTransmissionDescriptor *dd;
4702 struct MeshPathInfo *path_info;
4703 struct MeshTunnelChildInfo *cinfo;
4704 struct GNUNET_PeerIdentity id;
4708 if (GNUNET_YES == clear_cls)
4710 switch (queue->type)
4712 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4713 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4714 GNUNET_assert (GNUNET_YES == queue->tunnel->destroy);
4715 /* FIXME: don't cancel, send and destroy tunnel in queue_send */
4717 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4718 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4719 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4720 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4721 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4723 " prebuilt message\n");
4724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4726 GNUNET_MESH_DEBUG_M2S(queue->type));
4728 data_descriptor_decrement_rc (dd->mesh_data);
4730 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4732 path_info = queue->cls;
4733 path_destroy (path_info->path);
4737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4738 " type %s unknown!\n",
4739 GNUNET_MESH_DEBUG_M2S(queue->type));
4741 GNUNET_free_non_null (queue->cls);
4743 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4744 queue->peer->queue_tail,
4747 /* Delete from child_fc in the appropiate tunnel */
4748 max = queue->tunnel->fwd_queue_max;
4749 GNUNET_PEER_resolve (queue->peer->id, &id);
4750 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4753 for (i = 0; i < cinfo->send_buffer_n; i++)
4756 i2 = (cinfo->send_buffer_start + i) % max;
4757 if (cinfo->send_buffer[i2] == queue)
4759 /* Found corresponding entry in the send_buffer. Move all others back. */
4764 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4766 j2 = (cinfo->send_buffer_start + j) % max;
4767 j3 = (cinfo->send_buffer_start + j + 1) % max;
4768 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4771 cinfo->send_buffer[j3] = NULL;
4772 cinfo->send_buffer_n--;
4777 GNUNET_free (queue);
4782 * @brief Get the next transmittable message from the queue.
4784 * This will be the head, except in the case of being a data packet
4785 * not allowed by the destination peer.
4787 * @param peer Destination peer.
4789 * @return The next viable MeshPeerQueue element to send to that peer.
4790 * NULL when there are no transmittable messages.
4792 struct MeshPeerQueue *
4793 queue_get_next (const struct MeshPeerInfo *peer)
4795 struct MeshPeerQueue *q;
4796 struct MeshTunnel *t;
4797 struct MeshTransmissionDescriptor *info;
4798 struct MeshTunnelChildInfo *cinfo;
4799 struct GNUNET_MESH_Unicast *ucast;
4800 struct GNUNET_MESH_ToOrigin *to_orig;
4801 struct GNUNET_MESH_Multicast *mcast;
4802 struct GNUNET_PeerIdentity id;
4806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4807 for (q = peer->queue_head; NULL != q; q = q->next)
4811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4813 GNUNET_MESH_DEBUG_M2S(q->type));
4816 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4817 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4818 pid = ntohl (ucast->pid);
4819 GNUNET_PEER_resolve (info->peer->id, &id);
4820 cinfo = tunnel_get_neighbor_fc(t, &id);
4821 ack = cinfo->fwd_ack;
4823 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4824 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4825 pid = ntohl (to_orig->pid);
4828 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4829 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4830 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4832 // Not a multicast payload: multicast control traffic (destroy, etc)
4835 pid = ntohl (mcast->pid);
4836 GNUNET_PEER_resolve (info->peer->id, &id);
4837 cinfo = tunnel_get_neighbor_fc(t, &id);
4838 ack = cinfo->fwd_ack;
4841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4846 "********* ACK: %u, PID: %u\n",
4848 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4857 "********* NEXT!\n");
4860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4861 "********* nothing found\n");
4867 * Core callback to write a queued packet to core buffer
4869 * @param cls Closure (peer info).
4870 * @param size Number of bytes available in buf.
4871 * @param buf Where the to write the message.
4873 * @return number of bytes written to buf
4876 queue_send (void *cls, size_t size, void *buf)
4878 struct MeshPeerInfo *peer = cls;
4879 struct GNUNET_MessageHeader *msg;
4880 struct MeshPeerQueue *queue;
4881 struct MeshTunnel *t;
4882 struct MeshTunnelChildInfo *cinfo;
4883 struct GNUNET_PeerIdentity dst_id;
4886 peer->core_transmit = NULL;
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4890 queue = queue_get_next (peer);
4892 /* Queue has no internal mesh traffic nor sendable payload */
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4896 if (NULL == peer->queue_head)
4897 GNUNET_break (0); // Should've been canceled
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4902 GNUNET_PEER_resolve (peer->id, &dst_id);
4903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4904 "********* towards %s\n",
4905 GNUNET_i2s(&dst_id));
4906 /* Check if buffer size is enough for the message */
4907 if (queue->size > size)
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4910 "********* not enough room, reissue\n");
4911 peer->core_transmit =
4912 GNUNET_CORE_notify_transmit_ready (core_handle,
4915 GNUNET_TIME_UNIT_FOREVER_REL,
4922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4925 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4929 "********* unicast: t->q (%u/%u)\n",
4930 t->fwd_queue_n, t->fwd_queue_max);
4932 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4939 switch (queue->type)
4942 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4943 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4944 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4945 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4947 "********* raw: %s\n",
4948 GNUNET_MESH_DEBUG_M2S (queue->type));
4950 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4951 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4952 data_size = send_core_data_raw (queue->cls, size, buf);
4953 msg = (struct GNUNET_MessageHeader *) buf;
4954 switch (ntohs (msg->type)) // Type of preconstructed message
4956 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4957 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4959 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4960 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4966 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4969 struct MeshTransmissionDescriptor *info = queue->cls;
4971 if ((1 == info->mesh_data->reference_counter
4972 && GNUNET_YES == t->speed_min)
4974 (info->mesh_data->total_out == info->mesh_data->reference_counter
4975 && GNUNET_NO == t->speed_min))
4977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4978 "********* considered sent\n");
4982 data_size = send_core_data_multicast(queue->cls, size, buf);
4983 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4985 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4987 data_size = send_core_path_create (queue->cls, size, buf);
4989 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4991 data_size = send_core_path_ack (queue->cls, size, buf);
4993 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4995 data_size = send_core_data_multicast (queue->cls, size, buf);
4999 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5000 "********* type unknown: %u\n",
5004 switch (queue->type)
5006 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5007 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5008 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5009 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5010 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5014 "at pos %u (%p) != %p\n",
5015 cinfo->send_buffer_start,
5016 cinfo->send_buffer[cinfo->send_buffer_start],
5019 if (cinfo->send_buffer_n > 0)
5021 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5022 cinfo->send_buffer_n--;
5023 cinfo->send_buffer_start++;
5024 cinfo->send_buffer_start %= t->fwd_queue_max;
5035 /* Free queue, but cls was freed by send_core_* */
5036 queue_destroy (queue, GNUNET_NO);
5038 if (GNUNET_YES == t->destroy)
5040 // FIXME fc tunnel destroy all pending traffic? wait for it?
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5045 /* If more data in queue, send next */
5046 queue = queue_get_next(peer);
5049 struct GNUNET_PeerIdentity id;
5051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5052 GNUNET_PEER_resolve (peer->id, &id);
5053 peer->core_transmit =
5054 GNUNET_CORE_notify_transmit_ready(core_handle,
5057 GNUNET_TIME_UNIT_FOREVER_REL,
5065 if (NULL != peer->queue_head)
5067 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5068 "********* %s stalled\n",
5069 GNUNET_i2s(&my_full_id));
5071 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5072 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5073 &tunnel_poll, cinfo);
5076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5082 * @brief Queue and pass message to core when possible.
5084 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5085 * and accounts for it. In case the queue is full, the message is dropped and
5088 * Otherwise, message is treated as internal and allowed to go regardless of
5091 * @param cls Closure (@c type dependant). It will be used by queue_send to
5092 * build the message to be sent if not already prebuilt.
5093 * @param type Type of the message, 0 for a raw message.
5094 * @param size Size of the message.
5095 * @param dst Neighbor to send message to.
5096 * @param t Tunnel this message belongs to.
5099 queue_add (void *cls, uint16_t type, size_t size,
5100 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5102 struct MeshPeerQueue *queue;
5103 struct MeshTunnelChildInfo *cinfo;
5104 struct GNUNET_PeerIdentity id;
5110 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5111 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5113 n = &t->fwd_queue_n;
5114 max = &t->fwd_queue_max;
5116 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5118 n = &t->bck_queue_n;
5119 max = &t->bck_queue_max;
5125 struct MeshTransmissionDescriptor *td = cls;
5126 struct GNUNET_MESH_ToOrigin *to;
5128 to = td->mesh_data->data;
5130 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5131 "bck pid %u, bck ack %u, msg pid %u\n",
5132 t->bck_pid, t->bck_ack, ntohl(to->pid));
5133 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5135 return; // Drop message
5139 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5145 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5146 GNUNET_PEER_resolve (dst->id, &id);
5147 if (NULL == dst->core_transmit)
5149 dst->core_transmit =
5150 GNUNET_CORE_notify_transmit_ready (core_handle,
5153 GNUNET_TIME_UNIT_FOREVER_REL,
5159 if (NULL == n) // Is this internal mesh traffic?
5162 // It's payload, keep track of buffer per peer.
5163 cinfo = tunnel_get_neighbor_fc(t, &id);
5164 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5165 if (NULL != cinfo->send_buffer[i])
5167 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5168 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5169 cinfo->send_buffer_start++;
5170 cinfo->send_buffer_start %= t->fwd_queue_max;
5174 cinfo->send_buffer_n++;
5176 cinfo->send_buffer[i] = queue;
5177 if (cinfo->send_buffer_n > t->fwd_queue_max)
5180 cinfo->send_buffer_n = t->fwd_queue_max;
5185 /******************************************************************************/
5186 /******************** MESH NETWORK HANDLERS **************************/
5187 /******************************************************************************/
5191 * Core handler for path creation
5193 * @param cls closure
5194 * @param message message
5195 * @param peer peer identity this notification is about
5196 * @param atsi performance data
5197 * @param atsi_count number of records in 'atsi'
5199 * @return GNUNET_OK to keep the connection open,
5200 * GNUNET_SYSERR to close it (signal serious error)
5203 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5204 const struct GNUNET_MessageHeader *message,
5205 const struct GNUNET_ATS_Information *atsi,
5206 unsigned int atsi_count)
5208 unsigned int own_pos;
5211 MESH_TunnelNumber tid;
5212 struct GNUNET_MESH_ManipulatePath *msg;
5213 struct GNUNET_PeerIdentity *pi;
5214 struct GNUNET_HashCode hash;
5215 struct MeshPeerPath *path;
5216 struct MeshPeerInfo *dest_peer_info;
5217 struct MeshPeerInfo *orig_peer_info;
5218 struct MeshTunnel *t;
5220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5221 "Received a path create msg [%s]\n",
5222 GNUNET_i2s (&my_full_id));
5223 size = ntohs (message->size);
5224 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5226 GNUNET_break_op (0);
5230 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5231 if (size % sizeof (struct GNUNET_PeerIdentity))
5233 GNUNET_break_op (0);
5236 size /= sizeof (struct GNUNET_PeerIdentity);
5239 GNUNET_break_op (0);
5242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5243 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5245 tid = ntohl (msg->tid);
5246 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5248 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5249 t = tunnel_get (pi, tid);
5250 if (NULL == t) // FIXME only for INCOMING tunnels?
5254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5255 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5258 // FIXME notify failure
5261 opt = ntohl (msg->opt);
5262 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5263 GNUNET_YES : GNUNET_NO;
5264 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5266 t->nobuffer = GNUNET_YES;
5267 t->last_fwd_ack = t->fwd_pid + 1;
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5270 " speed_min: %d, nobuffer:%d\n",
5271 t->speed_min, t->nobuffer);
5273 if (GNUNET_YES == t->nobuffer)
5275 t->bck_queue_max = 1;
5276 t->fwd_queue_max = 1;
5279 // FIXME only assign a local tid if a local client is interested (on demand)
5280 while (NULL != tunnel_get_incoming (next_local_tid))
5281 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5282 t->local_tid_dest = next_local_tid++;
5283 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5286 tunnel_reset_timeout (t);
5287 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5289 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5290 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5298 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5299 if (NULL == dest_peer_info)
5301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5302 " Creating PeerInfo for destination.\n");
5303 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5304 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5305 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5307 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5309 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5310 if (NULL == orig_peer_info)
5312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5313 " Creating PeerInfo for origin.\n");
5314 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5315 orig_peer_info->id = GNUNET_PEER_intern (pi);
5316 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5317 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5320 path = path_new (size);
5322 for (i = 0; i < size; i++)
5324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5325 GNUNET_i2s (&pi[i]));
5326 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5327 if (path->peers[i] == myid)
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5333 /* cannot be self, must be 'not found' */
5334 /* create path: self not found in path through self */
5335 GNUNET_break_op (0);
5336 path_destroy (path);
5340 path_add_to_peers (path, GNUNET_NO);
5341 tunnel_add_path (t, path, own_pos);
5342 if (own_pos == size - 1)
5344 /* It is for us! Send ack. */
5345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5346 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5347 if (NULL == t->peers)
5349 /* New tunnel! Notify clients on first payload message. */
5350 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5352 GNUNET_break (GNUNET_SYSERR !=
5353 GNUNET_CONTAINER_multihashmap_put (t->peers,
5354 &my_full_id.hashPubKey,
5357 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5362 struct MeshPeerPath *path2;
5364 /* It's for somebody else! Retransmit. */
5365 path2 = path_duplicate (path);
5366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5367 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5368 path2 = path_duplicate (path);
5369 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5370 send_create_path (dest_peer_info, path, t);
5377 * Core handler for path destruction
5379 * @param cls closure
5380 * @param message message
5381 * @param peer peer identity this notification is about
5382 * @param atsi performance data
5383 * @param atsi_count number of records in 'atsi'
5385 * @return GNUNET_OK to keep the connection open,
5386 * GNUNET_SYSERR to close it (signal serious error)
5389 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5390 const struct GNUNET_MessageHeader *message,
5391 const struct GNUNET_ATS_Information *atsi,
5392 unsigned int atsi_count)
5394 struct GNUNET_MESH_ManipulatePath *msg;
5395 struct GNUNET_PeerIdentity *pi;
5396 struct MeshPeerPath *path;
5397 struct MeshTunnel *t;
5398 unsigned int own_pos;
5402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5403 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5404 size = ntohs (message->size);
5405 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5407 GNUNET_break_op (0);
5411 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5412 if (size % sizeof (struct GNUNET_PeerIdentity))
5414 GNUNET_break_op (0);
5417 size /= sizeof (struct GNUNET_PeerIdentity);
5420 GNUNET_break_op (0);
5423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5425 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5426 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5428 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5430 t = tunnel_get (pi, ntohl (msg->tid));
5433 /* TODO notify back: we don't know this tunnel */
5434 GNUNET_break_op (0);
5437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5438 path = path_new (size);
5440 for (i = 0; i < size; i++)
5442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5443 GNUNET_i2s (&pi[i]));
5444 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5445 if (path->peers[i] == myid)
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5449 if (own_pos < path->length - 1)
5450 send_prebuilt_message (message, &pi[own_pos + 1], t);
5452 send_client_tunnel_disconnect(t, NULL);
5454 tunnel_delete_peer (t, path->peers[path->length - 1]);
5455 path_destroy (path);
5461 * Core handler for notifications of broken paths
5463 * @param cls closure
5464 * @param message message
5465 * @param peer peer identity this notification is about
5466 * @param atsi performance data
5467 * @param atsi_count number of records in 'atsi'
5469 * @return GNUNET_OK to keep the connection open,
5470 * GNUNET_SYSERR to close it (signal serious error)
5473 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5474 const struct GNUNET_MessageHeader *message,
5475 const struct GNUNET_ATS_Information *atsi,
5476 unsigned int atsi_count)
5478 struct GNUNET_MESH_PathBroken *msg;
5479 struct MeshTunnel *t;
5481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5482 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5483 msg = (struct GNUNET_MESH_PathBroken *) message;
5484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5485 GNUNET_i2s (&msg->peer1));
5486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5487 GNUNET_i2s (&msg->peer2));
5488 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5491 GNUNET_break_op (0);
5494 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5495 GNUNET_PEER_search (&msg->peer2));
5502 * Core handler for tunnel destruction
5504 * @param cls closure
5505 * @param message message
5506 * @param peer peer identity this notification is about
5507 * @param atsi performance data
5508 * @param atsi_count number of records in 'atsi'
5510 * @return GNUNET_OK to keep the connection open,
5511 * GNUNET_SYSERR to close it (signal serious error)
5514 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5515 const struct GNUNET_MessageHeader *message,
5516 const struct GNUNET_ATS_Information *atsi,
5517 unsigned int atsi_count)
5519 struct GNUNET_MESH_TunnelDestroy *msg;
5520 struct MeshTunnel *t;
5522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5523 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5524 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5526 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5527 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5530 /* Probably already got the message from another path,
5531 * destroyed the tunnel and retransmitted to children.
5534 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5537 if (t->id.oid == myid)
5539 GNUNET_break_op (0);
5542 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5544 /* Tunnel was incoming, notify clients */
5545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5546 t->local_tid, t->local_tid_dest);
5547 send_clients_tunnel_destroy (t);
5549 tunnel_send_destroy (t);
5550 t->destroy = GNUNET_YES;
5551 // TODO: add timeout to destroy the tunnel anyway
5557 * Core handler for mesh network traffic going from the origin to a peer
5559 * @param cls closure
5560 * @param peer peer identity this notification is about
5561 * @param message message
5562 * @param atsi performance data
5563 * @param atsi_count number of records in 'atsi'
5564 * @return GNUNET_OK to keep the connection open,
5565 * GNUNET_SYSERR to close it (signal serious error)
5568 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5569 const struct GNUNET_MessageHeader *message,
5570 const struct GNUNET_ATS_Information *atsi,
5571 unsigned int atsi_count)
5573 struct GNUNET_MESH_Unicast *msg;
5574 struct GNUNET_PeerIdentity *neighbor;
5575 struct MeshTunnelChildInfo *cinfo;
5576 struct MeshTunnel *t;
5577 GNUNET_PEER_Id dest_id;
5582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5585 size = ntohs (message->size);
5587 sizeof (struct GNUNET_MESH_Unicast) +
5588 sizeof (struct GNUNET_MessageHeader))
5593 msg = (struct GNUNET_MESH_Unicast *) message;
5594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5595 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5597 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5600 /* TODO notify back: we don't know this tunnel */
5601 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5602 GNUNET_break_op (0);
5605 pid = ntohl (msg->pid);
5606 if (t->fwd_pid == pid)
5608 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5609 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5610 " Already seen pid %u, DROPPING!\n", pid);
5615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5616 " pid %u not seen yet, forwarding\n", pid);
5619 t->skip += (pid - t->fwd_pid) - 1;
5622 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5624 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5625 GNUNET_break_op (0);
5626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5627 "Received PID %u, ACK %u\n",
5628 pid, t->last_fwd_ack);
5632 tunnel_reset_timeout (t);
5633 dest_id = GNUNET_PEER_search (&msg->destination);
5634 if (dest_id == myid)
5636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5637 " it's for us! sending to clients...\n");
5638 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5639 send_subscribed_clients (message, &msg[1].header, t);
5640 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5643 ttl = ntohl (msg->ttl);
5644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5647 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5648 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5649 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5653 " not for us, retransmitting...\n");
5655 neighbor = tree_get_first_hop (t->tree, dest_id);
5656 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5657 cinfo->fwd_pid = pid;
5658 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5661 if (GNUNET_YES == t->nobuffer &&
5662 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5664 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5665 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5666 GNUNET_break_op (0);
5669 send_prebuilt_message (message, neighbor, t);
5670 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5676 * Core handler for mesh network traffic going from the origin to all peers
5678 * @param cls closure
5679 * @param message message
5680 * @param peer peer identity this notification is about
5681 * @param atsi performance data
5682 * @param atsi_count number of records in 'atsi'
5683 * @return GNUNET_OK to keep the connection open,
5684 * GNUNET_SYSERR to close it (signal serious error)
5686 * TODO: Check who we got this from, to validate route.
5689 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5690 const struct GNUNET_MessageHeader *message,
5691 const struct GNUNET_ATS_Information *atsi,
5692 unsigned int atsi_count)
5694 struct GNUNET_MESH_Multicast *msg;
5695 struct MeshTunnel *t;
5699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5701 size = ntohs (message->size);
5702 if (sizeof (struct GNUNET_MESH_Multicast) +
5703 sizeof (struct GNUNET_MessageHeader) > size)
5705 GNUNET_break_op (0);
5708 msg = (struct GNUNET_MESH_Multicast *) message;
5709 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5713 /* TODO notify that we dont know that tunnel */
5714 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5715 GNUNET_break_op (0);
5718 pid = ntohl (msg->pid);
5719 if (t->fwd_pid == pid)
5721 /* already seen this packet, drop */
5722 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5724 " Already seen pid %u, DROPPING!\n", pid);
5725 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5731 " pid %u not seen yet, forwarding\n", pid);
5733 t->skip += (pid - t->fwd_pid) - 1;
5735 tunnel_reset_timeout (t);
5737 /* Transmit to locally interested clients */
5738 if (NULL != t->peers &&
5739 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5741 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5742 send_subscribed_clients (message, &msg[1].header, t);
5743 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5746 if (ntohl (msg->ttl) == 0)
5748 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5749 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5750 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5753 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5754 tunnel_send_multicast (t, message);
5760 * Core handler for mesh network traffic toward the owner of a tunnel
5762 * @param cls closure
5763 * @param message message
5764 * @param peer peer identity this notification is about
5765 * @param atsi performance data
5766 * @param atsi_count number of records in 'atsi'
5768 * @return GNUNET_OK to keep the connection open,
5769 * GNUNET_SYSERR to close it (signal serious error)
5772 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5773 const struct GNUNET_MessageHeader *message,
5774 const struct GNUNET_ATS_Information *atsi,
5775 unsigned int atsi_count)
5777 struct GNUNET_MESH_ToOrigin *msg;
5778 struct GNUNET_PeerIdentity id;
5779 struct MeshPeerInfo *peer_info;
5780 struct MeshTunnel *t;
5781 struct MeshTunnelChildInfo *cinfo;
5785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5787 size = ntohs (message->size);
5788 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5789 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5791 GNUNET_break_op (0);
5794 msg = (struct GNUNET_MESH_ToOrigin *) message;
5795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5796 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5797 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5798 pid = ntohl (msg->pid);
5802 /* TODO notify that we dont know this tunnel (whom)? */
5803 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5804 GNUNET_break_op (0);
5805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5806 "Received to_origin with PID %u on unknown tunnel\n",
5811 cinfo = tunnel_get_neighbor_fc(t, peer);
5818 if (cinfo->bck_pid == pid)
5820 /* already seen this packet, drop */
5821 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5823 " Already seen pid %u, DROPPING!\n", pid);
5824 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5829 " pid %u not seen yet, forwarding\n", pid);
5830 cinfo->bck_pid = pid;
5832 if (NULL != t->owner)
5835 struct GNUNET_MESH_ToOrigin *copy;
5837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5838 " it's for us! sending to clients...\n");
5839 /* TODO signature verification */
5840 memcpy (cbuf, message, size);
5841 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5842 copy->tid = htonl (t->local_tid);
5844 copy->pid = htonl (t->bck_pid);
5845 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5846 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5847 ©->header, GNUNET_NO);
5848 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5852 " not for us, retransmitting...\n");
5854 peer_info = peer_info_get (&msg->oid);
5855 if (NULL == peer_info)
5857 /* unknown origin of tunnel */
5861 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5862 send_prebuilt_message (message, &id, t);
5863 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5870 * Core handler for mesh network traffic point-to-point acks.
5872 * @param cls closure
5873 * @param message message
5874 * @param peer peer identity this notification is about
5875 * @param atsi performance data
5876 * @param atsi_count number of records in 'atsi'
5878 * @return GNUNET_OK to keep the connection open,
5879 * GNUNET_SYSERR to close it (signal serious error)
5882 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5883 const struct GNUNET_MessageHeader *message,
5884 const struct GNUNET_ATS_Information *atsi,
5885 unsigned int atsi_count)
5887 struct GNUNET_MESH_ACK *msg;
5888 struct MeshTunnel *t;
5891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5893 msg = (struct GNUNET_MESH_ACK *) message;
5895 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5899 /* TODO notify that we dont know this tunnel (whom)? */
5900 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5901 GNUNET_break_op (0);
5904 ack = ntohl (msg->pid);
5905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5907 /* Is this a forward or backward ACK? */
5908 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5910 struct MeshTunnelChildInfo *cinfo;
5913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5914 cinfo = tunnel_get_neighbor_fc (t, peer);
5915 cinfo->fwd_ack = ack;
5916 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5917 tunnel_unlock_fwd_queues (t);
5921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5923 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5924 tunnel_unlock_bck_queue (t);
5931 * Core handler for mesh network traffic point-to-point ack polls.
5933 * @param cls closure
5934 * @param message message
5935 * @param peer peer identity this notification is about
5936 * @param atsi performance data
5937 * @param atsi_count number of records in 'atsi'
5939 * @return GNUNET_OK to keep the connection open,
5940 * GNUNET_SYSERR to close it (signal serious error)
5943 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5944 const struct GNUNET_MessageHeader *message,
5945 const struct GNUNET_ATS_Information *atsi,
5946 unsigned int atsi_count)
5948 struct GNUNET_MESH_Poll *msg;
5949 struct MeshTunnel *t;
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5954 msg = (struct GNUNET_MESH_Poll *) message;
5956 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5960 /* TODO notify that we dont know this tunnel (whom)? */
5961 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5962 GNUNET_break_op (0);
5966 /* Is this a forward or backward ACK? */
5967 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5969 struct MeshTunnelChildInfo *cinfo;
5971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5972 cinfo = tunnel_get_neighbor_fc (t, peer);
5973 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5974 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5979 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5987 * Core handler for path ACKs
5989 * @param cls closure
5990 * @param message message
5991 * @param peer peer identity this notification is about
5992 * @param atsi performance data
5993 * @param atsi_count number of records in 'atsi'
5995 * @return GNUNET_OK to keep the connection open,
5996 * GNUNET_SYSERR to close it (signal serious error)
5999 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6000 const struct GNUNET_MessageHeader *message,
6001 const struct GNUNET_ATS_Information *atsi,
6002 unsigned int atsi_count)
6004 struct GNUNET_MESH_PathACK *msg;
6005 struct GNUNET_PeerIdentity id;
6006 struct MeshPeerInfo *peer_info;
6007 struct MeshPeerPath *p;
6008 struct MeshTunnel *t;
6010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6011 GNUNET_i2s (&my_full_id));
6012 msg = (struct GNUNET_MESH_PathACK *) message;
6013 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6016 /* TODO notify that we don't know the tunnel */
6017 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6019 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6023 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6025 peer_info = peer_info_get (&msg->peer_id);
6026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6027 GNUNET_i2s (&msg->peer_id));
6028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6031 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6034 "connect_by_string completed, stopping search\n");
6035 regex_cancel_search (t->regex_ctx);
6036 t->regex_ctx = NULL;
6039 /* Add paths to peers? */
6040 p = tree_get_path_to_peer (t->tree, peer_info->id);
6043 path_add_to_peers (p, GNUNET_YES);
6051 /* Message for us? */
6052 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6055 if (NULL == t->owner)
6057 GNUNET_break_op (0);
6060 if (NULL != t->dht_get_type)
6062 GNUNET_DHT_get_stop (t->dht_get_type);
6063 t->dht_get_type = NULL;
6065 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6067 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6068 send_client_peer_connected (t, peer_info->id);
6073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6074 " not for us, retransmitting...\n");
6075 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6076 peer_info = peer_info_get (&msg->oid);
6077 if (NULL == peer_info)
6079 /* If we know the tunnel, we should DEFINITELY know the peer */
6083 send_prebuilt_message (message, &id, t);
6089 * Core handler for mesh keepalives.
6091 * @param cls closure
6092 * @param message message
6093 * @param peer peer identity this notification is about
6094 * @param atsi performance data
6095 * @param atsi_count number of records in 'atsi'
6096 * @return GNUNET_OK to keep the connection open,
6097 * GNUNET_SYSERR to close it (signal serious error)
6099 * TODO: Check who we got this from, to validate route.
6102 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6103 const struct GNUNET_MessageHeader *message,
6104 const struct GNUNET_ATS_Information *atsi,
6105 unsigned int atsi_count)
6107 struct GNUNET_MESH_TunnelKeepAlive *msg;
6108 struct MeshTunnel *t;
6110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6113 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6114 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6118 /* TODO notify that we dont know that tunnel */
6119 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6120 GNUNET_break_op (0);
6124 tunnel_reset_timeout (t);
6126 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6127 tunnel_send_multicast (t, message);
6134 * Functions to handle messages from core
6136 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6137 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6138 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6139 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6140 sizeof (struct GNUNET_MESH_PathBroken)},
6141 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6142 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6143 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6144 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6145 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6146 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6147 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6148 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6149 sizeof (struct GNUNET_MESH_ACK)},
6150 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6151 sizeof (struct GNUNET_MESH_Poll)},
6152 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6153 sizeof (struct GNUNET_MESH_PathACK)},
6159 /******************************************************************************/
6160 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6161 /******************************************************************************/
6164 * deregister_app: iterator for removing each application registered by a client
6166 * @param cls closure
6167 * @param key the hash of the application id (used to access the hashmap)
6168 * @param value the value stored at the key (client)
6170 * @return GNUNET_OK on success
6173 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6175 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6176 GNUNET_break (GNUNET_YES ==
6177 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6183 * notify_client_connection_failure: notify a client that the connection to the
6184 * requested remote peer is not possible (for instance, no route found)
6185 * Function called when the socket is ready to queue more data. "buf" will be
6186 * NULL and "size" zero if the socket was closed for writing in the meantime.
6188 * @param cls closure
6189 * @param size number of bytes available in buf
6190 * @param buf where the callee should write the message
6191 * @return number of bytes written to buf
6194 notify_client_connection_failure (void *cls, size_t size, void *buf)
6197 struct MeshPeerInfo *peer_info;
6198 struct GNUNET_MESH_PeerControl *msg;
6199 struct GNUNET_PeerIdentity id;
6201 if (0 == size && NULL == buf)
6203 // TODO retry? cancel?
6207 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6208 peer_info = (struct MeshPeerInfo *) cls;
6209 msg = (struct GNUNET_MESH_PeerControl *) buf;
6210 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6211 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6212 // msg->tunnel_id = htonl(peer_info->t->tid);
6213 GNUNET_PEER_resolve (peer_info->id, &id);
6214 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6222 * Send keepalive packets for a peer
6224 * @param cls Closure (tunnel for which to send the keepalive).
6225 * @param tc Notification context.
6228 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6230 struct MeshTunnel *t = cls;
6231 struct GNUNET_MESH_TunnelKeepAlive *msg;
6232 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6235 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6236 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6242 "sending keepalive for tunnel %d\n", t->id.tid);
6244 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6245 msg->header.size = htons (size);
6246 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6247 msg->oid = my_full_id;
6248 msg->tid = htonl (t->id.tid);
6249 tunnel_send_multicast (t, &msg->header);
6251 t->path_refresh_task =
6252 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6253 tunnel_reset_timeout(t);
6258 * Function to process paths received for a new peer addition. The recorded
6259 * paths form the initial tunnel, which can be optimized later.
6260 * Called on each result obtained for the DHT search.
6262 * @param cls closure
6263 * @param exp when will this value expire
6264 * @param key key of the result
6265 * @param get_path path of the get request
6266 * @param get_path_length lenght of get_path
6267 * @param put_path path of the put request
6268 * @param put_path_length length of the put_path
6269 * @param type type of the result
6270 * @param size number of bytes in data
6271 * @param data pointer to the result data
6273 * TODO: re-issue the request after certain time? cancel after X results?
6276 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6277 const struct GNUNET_HashCode * key,
6278 const struct GNUNET_PeerIdentity *get_path,
6279 unsigned int get_path_length,
6280 const struct GNUNET_PeerIdentity *put_path,
6281 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6282 size_t size, const void *data)
6284 struct MeshPathInfo *path_info = cls;
6285 struct MeshPeerPath *p;
6286 struct GNUNET_PeerIdentity pi;
6289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6290 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6293 p = path_build_from_dht (get_path, get_path_length, put_path,
6295 path_add_to_peers (p, GNUNET_NO);
6297 for (i = 0; i < path_info->peer->ntunnels; i++)
6299 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6300 peer_info_connect (path_info->peer, path_info->t);
6308 * Function to process paths received for a new peer addition. The recorded
6309 * paths form the initial tunnel, which can be optimized later.
6310 * Called on each result obtained for the DHT search.
6312 * @param cls closure
6313 * @param exp when will this value expire
6314 * @param key key of the result
6315 * @param get_path path of the get request
6316 * @param get_path_length lenght of get_path
6317 * @param put_path path of the put request
6318 * @param put_path_length length of the put_path
6319 * @param type type of the result
6320 * @param size number of bytes in data
6321 * @param data pointer to the result data
6324 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6325 const struct GNUNET_HashCode * key,
6326 const struct GNUNET_PeerIdentity *get_path,
6327 unsigned int get_path_length,
6328 const struct GNUNET_PeerIdentity *put_path,
6329 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6330 size_t size, const void *data)
6332 const struct PBlock *pb = data;
6333 const struct GNUNET_PeerIdentity *pi = &pb->id;
6334 struct MeshTunnel *t = cls;
6335 struct MeshPeerInfo *peer_info;
6336 struct MeshPeerPath *p;
6338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6339 if (size != sizeof (struct PBlock))
6341 GNUNET_break_op (0);
6344 if (ntohl(pb->type) != t->type)
6346 GNUNET_break_op (0);
6349 GNUNET_assert (NULL != t->owner);
6350 peer_info = peer_info_get (pi);
6351 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6353 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6355 p = path_build_from_dht (get_path, get_path_length, put_path,
6357 path_add_to_peers (p, GNUNET_NO);
6359 tunnel_add_peer (t, peer_info);
6360 peer_info_connect (peer_info, t);
6365 * Function to process DHT string to regex matching.
6366 * Called on each result obtained for the DHT search.
6368 * @param cls closure (search context)
6369 * @param exp when will this value expire
6370 * @param key key of the result
6371 * @param get_path path of the get request (not used)
6372 * @param get_path_length lenght of get_path (not used)
6373 * @param put_path path of the put request (not used)
6374 * @param put_path_length length of the put_path (not used)
6375 * @param type type of the result
6376 * @param size number of bytes in data
6377 * @param data pointer to the result data
6380 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6381 const struct GNUNET_HashCode * key,
6382 const struct GNUNET_PeerIdentity *get_path,
6383 unsigned int get_path_length,
6384 const struct GNUNET_PeerIdentity *put_path,
6385 unsigned int put_path_length,
6386 enum GNUNET_BLOCK_Type type,
6387 size_t size, const void *data)
6389 const struct MeshRegexAccept *block = data;
6390 struct MeshRegexSearchContext *ctx = cls;
6391 struct MeshRegexSearchInfo *info = ctx->info;
6392 struct MeshPeerPath *p;
6393 struct MeshPeerInfo *peer_info;
6395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6398 peer_info = peer_info_get(&block->id);
6399 p = path_build_from_dht (get_path, get_path_length, put_path,
6401 path_add_to_peers (p, GNUNET_NO);
6404 tunnel_add_peer (info->t, peer_info);
6405 peer_info_connect (peer_info, info->t);
6406 if (0 == info->peer)
6408 info->peer = peer_info->id;
6412 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6415 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6416 ®ex_connect_timeout,
6424 * Function to process DHT string to regex matching.
6425 * Called on each result obtained for the DHT search.
6427 * @param cls closure (search context)
6428 * @param exp when will this value expire
6429 * @param key key of the result
6430 * @param get_path path of the get request (not used)
6431 * @param get_path_length lenght of get_path (not used)
6432 * @param put_path path of the put request (not used)
6433 * @param put_path_length length of the put_path (not used)
6434 * @param type type of the result
6435 * @param size number of bytes in data
6436 * @param data pointer to the result data
6438 * TODO: re-issue the request after certain time? cancel after X results?
6441 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6442 const struct GNUNET_HashCode * key,
6443 const struct GNUNET_PeerIdentity *get_path,
6444 unsigned int get_path_length,
6445 const struct GNUNET_PeerIdentity *put_path,
6446 unsigned int put_path_length,
6447 enum GNUNET_BLOCK_Type type,
6448 size_t size, const void *data)
6450 const struct MeshRegexBlock *block = data;
6451 struct MeshRegexSearchContext *ctx = cls;
6452 struct MeshRegexSearchInfo *info = ctx->info;
6456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6457 "DHT GET STRING RETURNED RESULTS\n");
6458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6459 " key: %s\n", GNUNET_h2s (key));
6461 copy = GNUNET_malloc (size);
6462 memcpy (copy, data, size);
6463 GNUNET_break (GNUNET_OK ==
6464 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6465 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6466 len = ntohl (block->n_proof);
6468 char proof[len + 1];
6470 memcpy (proof, &block[1], len);
6472 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6474 GNUNET_break_op (0);
6478 len = strlen (info->description);
6479 if (len == ctx->position) // String processed
6481 if (GNUNET_YES == ntohl (block->accepting))
6483 regex_find_path(key, ctx);
6487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6488 // FIXME REGEX this block not successful, wait for more? start timeout?
6493 regex_next_edge (block, size, ctx);
6498 /******************************************************************************/
6499 /********************* MESH LOCAL HANDLES **************************/
6500 /******************************************************************************/
6504 * Handler for client disconnection
6506 * @param cls closure
6507 * @param client identification of the client; NULL
6508 * for the last call when the server is destroyed
6511 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6513 struct MeshClient *c;
6514 struct MeshClient *next;
6517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6526 if (c->handle != client)
6528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6534 GNUNET_SERVER_client_drop (c->handle);
6535 c->shutting_down = GNUNET_YES;
6536 GNUNET_assert (NULL != c->own_tunnels);
6537 GNUNET_assert (NULL != c->incoming_tunnels);
6538 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6539 &tunnel_destroy_iterator, c);
6540 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6541 &tunnel_destroy_iterator, c);
6542 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6543 &tunnel_destroy_iterator, c);
6544 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6545 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6546 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6548 /* deregister clients applications */
6549 if (NULL != c->apps)
6551 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6552 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6554 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6555 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6557 GNUNET_SCHEDULER_cancel (announce_applications_task);
6558 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6560 if (NULL != c->types)
6561 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6562 for (i = 0; i < c->n_regex; i++)
6564 GNUNET_free (c->regexes[i]);
6566 GNUNET_free_non_null (c->regexes);
6567 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6568 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6570 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6573 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6582 * Handler for new clients
6584 * @param cls closure
6585 * @param client identification of the client
6586 * @param message the actual message, which includes messages the client wants
6589 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6590 const struct GNUNET_MessageHeader *message)
6592 struct GNUNET_MESH_ClientConnect *cc_msg;
6593 struct MeshClient *c;
6594 GNUNET_MESH_ApplicationType *a;
6601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6602 /* Check data sanity */
6603 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6604 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6605 ntypes = ntohs (cc_msg->types);
6606 napps = ntohs (cc_msg->applications);
6608 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6611 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6615 /* Create new client structure */
6616 c = GNUNET_malloc (sizeof (struct MeshClient));
6617 c->id = next_client_id++;
6618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6620 GNUNET_SERVER_client_keep (client);
6621 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6624 GNUNET_MESH_ApplicationType at;
6625 struct GNUNET_HashCode hc;
6627 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6628 for (i = 0; i < napps; i++)
6631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6632 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6633 /* store in clients hashmap */
6634 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6635 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6636 /* store in global hashmap, for announcements */
6637 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6638 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6640 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6641 announce_applications_task =
6642 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6648 struct GNUNET_HashCode hc;
6650 t = (uint16_t *) & a[napps];
6651 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6652 for (i = 0; i < ntypes; i++)
6655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6656 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6658 /* store in clients hashmap */
6659 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6660 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6661 /* store in global hashmap */
6662 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6663 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6667 " client has %u+%u subscriptions\n", napps, ntypes);
6669 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6670 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6671 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6672 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6673 GNUNET_SERVER_notification_context_add (nc, client);
6674 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6676 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6682 * Handler for clients announcing available services by a regular expression.
6684 * @param cls closure
6685 * @param client identification of the client
6686 * @param message the actual message, which includes messages the client wants
6689 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6690 const struct GNUNET_MessageHeader *message)
6692 struct MeshClient *c;
6696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6698 /* Sanity check for client registration */
6699 if (NULL == (c = client_get (client)))
6702 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6707 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6708 regex = GNUNET_malloc (len + 1);
6709 memcpy (regex, &message[1], len);
6711 GNUNET_array_append (c->regexes, c->n_regex, regex);
6712 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6714 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6720 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6726 * Handler for requests of new tunnels
6728 * @param cls closure
6729 * @param client identification of the client
6730 * @param message the actual message
6733 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6734 const struct GNUNET_MessageHeader *message)
6736 struct GNUNET_MESH_TunnelMessage *t_msg;
6737 struct MeshTunnel *t;
6738 struct MeshClient *c;
6739 MESH_TunnelNumber tid;
6741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6743 /* Sanity check for client registration */
6744 if (NULL == (c = client_get (client)))
6747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6752 /* Message sanity check */
6753 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6756 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6760 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6761 /* Sanity check for tunnel numbering */
6762 tid = ntohl (t_msg->tunnel_id);
6763 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6766 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6769 /* Sanity check for duplicate tunnel IDs */
6770 if (NULL != tunnel_get_by_local_id (c, tid))
6773 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6777 while (NULL != tunnel_get_by_pi (myid, next_tid))
6778 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6779 t = tunnel_new (myid, next_tid++, c, tid);
6782 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6784 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6787 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6789 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6790 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6793 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6799 * Handler for requests of deleting tunnels
6801 * @param cls closure
6802 * @param client identification of the client
6803 * @param message the actual message
6806 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6807 const struct GNUNET_MessageHeader *message)
6809 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6810 struct MeshClient *c;
6811 struct MeshTunnel *t;
6812 MESH_TunnelNumber tid;
6814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6815 "Got a DESTROY TUNNEL from client!\n");
6817 /* Sanity check for client registration */
6818 if (NULL == (c = client_get (client)))
6821 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6826 /* Message sanity check */
6827 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6834 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6836 /* Retrieve tunnel */
6837 tid = ntohl (tunnel_msg->tunnel_id);
6838 t = tunnel_get_by_local_id(c, tid);
6841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6846 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6848 client_ignore_tunnel (c, t);
6850 // TODO: when to destroy incoming tunnel?
6851 if (t->nclients == 0)
6853 GNUNET_assert (GNUNET_YES ==
6854 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6856 GNUNET_assert (GNUNET_YES ==
6857 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6858 &my_full_id.hashPubKey,
6862 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6865 send_client_tunnel_disconnect(t, c);
6866 client_delete_tunnel(c, t);
6868 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6870 tunnel_send_destroy (t);
6871 t->destroy = GNUNET_YES;
6872 // The tunnel will be destroyed when the last message is transmitted.
6873 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6879 * Handler for requests of seeting tunnel's speed.
6881 * @param cls Closure (unused).
6882 * @param client Identification of the client.
6883 * @param message The actual message.
6886 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6887 const struct GNUNET_MessageHeader *message)
6889 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6890 struct MeshClient *c;
6891 struct MeshTunnel *t;
6892 MESH_TunnelNumber tid;
6894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6895 "Got a SPEED request from client!\n");
6897 /* Sanity check for client registration */
6898 if (NULL == (c = client_get (client)))
6901 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6907 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6909 /* Retrieve tunnel */
6910 tid = ntohl (tunnel_msg->tunnel_id);
6911 t = tunnel_get_by_local_id(c, tid);
6914 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6916 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6920 switch (ntohs(message->type))
6922 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6923 t->speed_min = GNUNET_YES;
6925 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6926 t->speed_min = GNUNET_NO;
6931 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6936 * Handler for requests of seeting tunnel's buffering policy.
6938 * @param cls Closure (unused).
6939 * @param client Identification of the client.
6940 * @param message The actual message.
6943 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6944 const struct GNUNET_MessageHeader *message)
6946 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6947 struct MeshClient *c;
6948 struct MeshTunnel *t;
6949 MESH_TunnelNumber tid;
6951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6952 "Got a BUFFER request from client!\n");
6954 /* Sanity check for client registration */
6955 if (NULL == (c = client_get (client)))
6958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6963 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6965 /* Retrieve tunnel */
6966 tid = ntohl (tunnel_msg->tunnel_id);
6967 t = tunnel_get_by_local_id(c, tid);
6970 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6976 switch (ntohs(message->type))
6978 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6979 t->nobuffer = GNUNET_NO;
6981 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6982 t->nobuffer = GNUNET_YES;
6988 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6993 * Handler for connection requests to new peers
6995 * @param cls closure
6996 * @param client identification of the client
6997 * @param message the actual message (PeerControl)
7000 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7001 const struct GNUNET_MessageHeader *message)
7003 struct GNUNET_MESH_PeerControl *peer_msg;
7004 struct MeshPeerInfo *peer_info;
7005 struct MeshClient *c;
7006 struct MeshTunnel *t;
7007 MESH_TunnelNumber tid;
7009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7010 /* Sanity check for client registration */
7011 if (NULL == (c = client_get (client)))
7014 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7019 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7021 /* Sanity check for message size */
7022 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7029 /* Tunnel exists? */
7030 tid = ntohl (peer_msg->tunnel_id);
7031 t = tunnel_get_by_local_id (c, tid);
7035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7039 /* Does client own tunnel? */
7040 if (t->owner->handle != client)
7043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7047 GNUNET_i2s (&peer_msg->peer));
7048 peer_info = peer_info_get (&peer_msg->peer);
7050 tunnel_add_peer (t, peer_info);
7051 peer_info_connect (peer_info, t);
7053 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7059 * Handler for disconnection requests of peers in a tunnel
7061 * @param cls closure
7062 * @param client identification of the client
7063 * @param message the actual message (PeerControl)
7066 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7067 const struct GNUNET_MessageHeader *message)
7069 struct GNUNET_MESH_PeerControl *peer_msg;
7070 struct MeshPeerInfo *peer_info;
7071 struct MeshClient *c;
7072 struct MeshTunnel *t;
7073 MESH_TunnelNumber tid;
7075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7076 /* Sanity check for client registration */
7077 if (NULL == (c = client_get (client)))
7080 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7085 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7087 /* Sanity check for message size */
7088 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7091 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7095 /* Tunnel exists? */
7096 tid = ntohl (peer_msg->tunnel_id);
7097 t = tunnel_get_by_local_id (c, tid);
7101 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7106 /* Does client own tunnel? */
7107 if (t->owner->handle != client)
7110 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7115 GNUNET_i2s (&peer_msg->peer));
7116 /* Is the peer in the tunnel? */
7118 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7119 if (NULL == peer_info)
7122 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7126 /* Ok, delete peer from tunnel */
7127 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7128 &peer_msg->peer.hashPubKey);
7130 send_destroy_path (t, peer_info->id);
7131 tunnel_delete_peer (t, peer_info->id);
7132 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7137 * Handler for blacklist requests of peers in a tunnel
7139 * @param cls closure
7140 * @param client identification of the client
7141 * @param message the actual message (PeerControl)
7143 * FIXME implement DHT block bloomfilter
7146 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7147 const struct GNUNET_MessageHeader *message)
7149 struct GNUNET_MESH_PeerControl *peer_msg;
7150 struct MeshClient *c;
7151 struct MeshTunnel *t;
7152 MESH_TunnelNumber tid;
7154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7155 /* Sanity check for client registration */
7156 if (NULL == (c = client_get (client)))
7159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7164 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7166 /* Sanity check for message size */
7167 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7174 /* Tunnel exists? */
7175 tid = ntohl (peer_msg->tunnel_id);
7176 t = tunnel_get_by_local_id (c, tid);
7180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7185 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7186 GNUNET_PEER_intern(&peer_msg->peer));
7191 * Handler for unblacklist requests of peers in a tunnel
7193 * @param cls closure
7194 * @param client identification of the client
7195 * @param message the actual message (PeerControl)
7198 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7199 const struct GNUNET_MessageHeader *message)
7201 struct GNUNET_MESH_PeerControl *peer_msg;
7202 struct MeshClient *c;
7203 struct MeshTunnel *t;
7204 MESH_TunnelNumber tid;
7208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7209 /* Sanity check for client registration */
7210 if (NULL == (c = client_get (client)))
7213 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7218 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7220 /* Sanity check for message size */
7221 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7224 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7228 /* Tunnel exists? */
7229 tid = ntohl (peer_msg->tunnel_id);
7230 t = tunnel_get_by_local_id (c, tid);
7234 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7239 /* if peer is not known, complain */
7240 pid = GNUNET_PEER_search (&peer_msg->peer);
7247 /* search and remove from list */
7248 for (i = 0; i < t->nblacklisted; i++)
7250 if (t->blacklisted[i] == pid)
7252 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7253 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7258 /* if peer hasn't been blacklisted, complain */
7264 * Handler for connection requests to new peers by type
7266 * @param cls closure
7267 * @param client identification of the client
7268 * @param message the actual message (ConnectPeerByType)
7271 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7272 const struct GNUNET_MessageHeader *message)
7274 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7275 struct MeshClient *c;
7276 struct MeshTunnel *t;
7277 struct GNUNET_HashCode hash;
7278 MESH_TunnelNumber tid;
7280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7281 /* Sanity check for client registration */
7282 if (NULL == (c = client_get (client)))
7285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7290 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7292 /* Sanity check for message size */
7293 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7294 ntohs (connect_msg->header.size))
7297 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7301 /* Tunnel exists? */
7302 tid = ntohl (connect_msg->tunnel_id);
7303 t = tunnel_get_by_local_id (c, tid);
7307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7311 /* Does client own tunnel? */
7312 if (t->owner->handle != client)
7315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7319 /* Do WE have the service? */
7320 t->type = ntohl (connect_msg->type);
7321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7322 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7323 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7326 /* Yes! Fast forward, add ourselves to the tunnel and send the
7327 * good news to the client, and alert the destination client of
7328 * an incoming tunnel.
7330 * FIXME send a path create to self, avoid code duplication
7332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7333 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7334 peer_info_get (&my_full_id),
7335 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7338 send_client_peer_connected (t, myid);
7339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7340 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7342 t->local_tid_dest = next_local_tid++;
7343 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7344 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7345 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7349 /* Ok, lets find a peer offering the service */
7350 if (NULL != t->dht_get_type)
7352 GNUNET_DHT_get_stop (t->dht_get_type);
7354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7355 GNUNET_h2s (&hash));
7357 GNUNET_DHT_get_start (dht_handle,
7358 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7360 dht_replication_level,
7361 GNUNET_DHT_RO_RECORD_ROUTE |
7362 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7364 &dht_get_type_handler, t);
7366 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7372 * Handler for connection requests to new peers by a string service description.
7374 * @param cls closure
7375 * @param client identification of the client
7376 * @param message the actual message, which includes messages the client wants
7379 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7380 const struct GNUNET_MessageHeader *message)
7382 struct GNUNET_MESH_ConnectPeerByString *msg;
7383 struct MeshRegexSearchContext *ctx;
7384 struct MeshRegexSearchInfo *info;
7385 struct GNUNET_DHT_GetHandle *get_h;
7386 struct GNUNET_HashCode key;
7387 struct MeshTunnel *t;
7388 struct MeshClient *c;
7389 MESH_TunnelNumber tid;
7393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7394 "Connect by string started\n");
7395 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7396 size = htons (message->size);
7398 /* Sanity check for client registration */
7399 if (NULL == (c = client_get (client)))
7402 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7407 /* Message size sanity check */
7408 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7411 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7415 /* Tunnel exists? */
7416 tid = ntohl (msg->tunnel_id);
7417 t = tunnel_get_by_local_id (c, tid);
7421 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7425 /* Does client own tunnel? */
7426 if (t->owner->handle != client)
7429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7434 " on tunnel %s [%u]\n",
7435 GNUNET_i2s(&my_full_id),
7438 /* Only one connect_by_string allowed at the same time! */
7439 /* FIXME: allow more, return handle at api level to cancel, document */
7440 if (NULL != t->regex_ctx)
7443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7447 /* Find string itself */
7448 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7449 string = (const char *) &msg[1];
7451 /* Initialize context */
7452 size = GNUNET_REGEX_get_first_key(string, len, &key);
7453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7454 " consumed %u bits out of %u\n", size, len);
7455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7456 " looking for %s\n", GNUNET_h2s (&key));
7458 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7460 info->description = GNUNET_malloc (len + 1);
7461 memcpy (info->description, string, len);
7462 info->description[len] = '\0';
7463 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7464 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7467 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7468 ctx->position = size;
7472 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7474 /* Start search in DHT */
7475 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7476 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7477 &key, /* key to search */
7478 dht_replication_level, /* replication level */
7479 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7480 NULL, /* xquery */ // FIXME BLOOMFILTER
7481 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7482 &dht_get_string_handler, ctx);
7484 GNUNET_break (GNUNET_OK ==
7485 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7488 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7490 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7496 * Handler for client traffic directed to one peer
7498 * @param cls closure
7499 * @param client identification of the client
7500 * @param message the actual message
7503 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7504 const struct GNUNET_MessageHeader *message)
7506 struct MeshClient *c;
7507 struct MeshTunnel *t;
7508 struct MeshPeerInfo *pi;
7509 struct GNUNET_MESH_Unicast *data_msg;
7510 MESH_TunnelNumber tid;
7513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7514 "Got a unicast request from a client!\n");
7516 /* Sanity check for client registration */
7517 if (NULL == (c = client_get (client)))
7520 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7525 data_msg = (struct GNUNET_MESH_Unicast *) message;
7527 /* Sanity check for message size */
7528 size = ntohs (message->size);
7529 if (sizeof (struct GNUNET_MESH_Unicast) +
7530 sizeof (struct GNUNET_MessageHeader) > size)
7533 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7537 /* Tunnel exists? */
7538 tid = ntohl (data_msg->tid);
7539 t = tunnel_get_by_local_id (c, tid);
7543 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7547 /* Is it a local tunnel? Then, does client own the tunnel? */
7548 if (t->owner->handle != client)
7551 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7555 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7556 &data_msg->destination.hashPubKey);
7557 /* Is the selected peer in the tunnel? */
7561 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7565 /* PID should be as expected */
7566 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7570 "Unicast PID, expected %u, got %u\n",
7571 t->fwd_pid + 1, ntohl (data_msg->pid));
7572 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7576 /* Ok, everything is correct, send the message
7577 * (pretend we got it from a mesh peer)
7580 /* Work around const limitation */
7581 char buf[ntohs (message->size)] GNUNET_ALIGN;
7582 struct GNUNET_MESH_Unicast *copy;
7584 copy = (struct GNUNET_MESH_Unicast *) buf;
7585 memcpy (buf, data_msg, size);
7586 copy->oid = my_full_id;
7587 copy->tid = htonl (t->id.tid);
7588 copy->ttl = htonl (default_ttl);
7589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7590 " calling generic handler...\n");
7591 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7594 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7601 * Handler for client traffic directed to the origin
7603 * @param cls closure
7604 * @param client identification of the client
7605 * @param message the actual message
7608 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7609 const struct GNUNET_MessageHeader *message)
7611 struct GNUNET_MESH_ToOrigin *data_msg;
7612 struct MeshTunnelClientInfo *clinfo;
7613 struct MeshClient *c;
7614 struct MeshTunnel *t;
7615 MESH_TunnelNumber tid;
7618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7619 "Got a ToOrigin request from a client!\n");
7620 /* Sanity check for client registration */
7621 if (NULL == (c = client_get (client)))
7624 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7629 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7631 /* Sanity check for message size */
7632 size = ntohs (message->size);
7633 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7634 sizeof (struct GNUNET_MessageHeader) > size)
7637 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7641 /* Tunnel exists? */
7642 tid = ntohl (data_msg->tid);
7643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7644 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7650 t = tunnel_get_by_local_id (c, tid);
7653 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7656 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7660 /* It should be sent by someone who has this as incoming tunnel. */
7661 if (GNUNET_NO == client_knows_tunnel (c, t))
7664 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7668 /* PID should be as expected */
7669 clinfo = tunnel_get_client_fc (t, c);
7670 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7673 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7674 "To Origin PID, expected %u, got %u\n",
7675 clinfo->bck_pid + 1,
7676 ntohl (data_msg->pid));
7677 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7681 /* Ok, everything is correct, send the message
7682 * (pretend we got it from a mesh peer)
7686 char buf[ntohs (message->size)] GNUNET_ALIGN;
7687 struct GNUNET_MESH_ToOrigin *copy;
7689 /* Work around const limitation */
7690 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7691 memcpy (buf, data_msg, size);
7692 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7693 copy->tid = htonl (t->id.tid);
7694 copy->ttl = htonl (default_ttl);
7695 if (ntohl (copy->pid) != (t->bck_pid + 1))
7698 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7699 "To Origin PID, expected %u, got %u\n",
7704 copy->sender = my_full_id;
7705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7706 " calling generic handler...\n");
7707 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7709 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7716 * Handler for client traffic directed to all peers in a tunnel
7718 * @param cls closure
7719 * @param client identification of the client
7720 * @param message the actual message
7723 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7724 const struct GNUNET_MessageHeader *message)
7726 struct MeshClient *c;
7727 struct MeshTunnel *t;
7728 struct GNUNET_MESH_Multicast *data_msg;
7729 MESH_TunnelNumber tid;
7731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7732 "Got a multicast request from a client!\n");
7734 /* Sanity check for client registration */
7735 if (NULL == (c = client_get (client)))
7738 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7743 data_msg = (struct GNUNET_MESH_Multicast *) message;
7745 /* Sanity check for message size */
7746 if (sizeof (struct GNUNET_MESH_Multicast) +
7747 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7750 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7754 /* Tunnel exists? */
7755 tid = ntohl (data_msg->tid);
7756 t = tunnel_get_by_local_id (c, tid);
7760 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7761 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7763 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7767 /* Does client own tunnel? */
7768 if (t->owner->handle != client)
7771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7775 /* PID should be as expected */
7776 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7779 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7780 "Multicast PID, expected %u, got %u\n",
7781 t->fwd_pid + 1, ntohl (data_msg->pid));
7782 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7787 char buf[ntohs (message->size)] GNUNET_ALIGN;
7788 struct GNUNET_MESH_Multicast *copy;
7790 copy = (struct GNUNET_MESH_Multicast *) buf;
7791 memcpy (buf, message, ntohs (message->size));
7792 copy->oid = my_full_id;
7793 copy->tid = htonl (t->id.tid);
7794 copy->ttl = htonl (default_ttl);
7795 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7797 " calling generic handler...\n");
7798 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7801 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7807 * Handler for client's ACKs for payload traffic.
7809 * @param cls Closure (unused).
7810 * @param client Identification of the client.
7811 * @param message The actual message.
7814 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7815 const struct GNUNET_MessageHeader *message)
7817 struct GNUNET_MESH_LocalAck *msg;
7818 struct MeshTunnel *t;
7819 struct MeshClient *c;
7820 MESH_TunnelNumber tid;
7823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7824 /* Sanity check for client registration */
7825 if (NULL == (c = client_get (client)))
7828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7833 msg = (struct GNUNET_MESH_LocalAck *) message;
7835 /* Tunnel exists? */
7836 tid = ntohl (msg->tunnel_id);
7837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7838 t = tunnel_get_by_local_id (c, tid);
7842 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7844 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7848 ack = ntohl (msg->max_pid);
7849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7851 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7852 if (NULL != t->owner && t->owner->handle == client)
7854 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7856 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7860 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7861 tunnel_set_client_fwd_ack (t, c, ack);
7862 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7865 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7872 * Functions to handle messages from clients
7874 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7875 {&handle_local_new_client, NULL,
7876 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7877 {&handle_local_announce_regex, NULL,
7878 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7879 {&handle_local_tunnel_create, NULL,
7880 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7881 sizeof (struct GNUNET_MESH_TunnelMessage)},
7882 {&handle_local_tunnel_destroy, NULL,
7883 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7884 sizeof (struct GNUNET_MESH_TunnelMessage)},
7885 {&handle_local_tunnel_speed, NULL,
7886 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7887 sizeof (struct GNUNET_MESH_TunnelMessage)},
7888 {&handle_local_tunnel_speed, NULL,
7889 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7890 sizeof (struct GNUNET_MESH_TunnelMessage)},
7891 {&handle_local_tunnel_buffer, NULL,
7892 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7893 sizeof (struct GNUNET_MESH_TunnelMessage)},
7894 {&handle_local_tunnel_buffer, NULL,
7895 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7896 sizeof (struct GNUNET_MESH_TunnelMessage)},
7897 {&handle_local_connect_add, NULL,
7898 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7899 sizeof (struct GNUNET_MESH_PeerControl)},
7900 {&handle_local_connect_del, NULL,
7901 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7902 sizeof (struct GNUNET_MESH_PeerControl)},
7903 {&handle_local_blacklist, NULL,
7904 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7905 sizeof (struct GNUNET_MESH_PeerControl)},
7906 {&handle_local_unblacklist, NULL,
7907 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7908 sizeof (struct GNUNET_MESH_PeerControl)},
7909 {&handle_local_connect_by_type, NULL,
7910 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7911 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7912 {&handle_local_connect_by_string, NULL,
7913 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7914 {&handle_local_unicast, NULL,
7915 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7916 {&handle_local_to_origin, NULL,
7917 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7918 {&handle_local_multicast, NULL,
7919 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7920 {&handle_local_ack, NULL,
7921 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7922 sizeof (struct GNUNET_MESH_LocalAck)},
7928 * To be called on core init/fail.
7930 * @param cls service closure
7931 * @param server handle to the server for this service
7932 * @param identity the public identity of this peer
7935 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7936 const struct GNUNET_PeerIdentity *identity)
7939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7940 core_handle = server;
7941 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7944 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7945 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7947 GNUNET_abort(); // Try harder
7954 * Method called whenever a given peer connects.
7956 * @param cls closure
7957 * @param peer peer identity this notification is about
7958 * @param atsi performance data for the connection
7959 * @param atsi_count number of records in 'atsi'
7962 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7963 const struct GNUNET_ATS_Information *atsi,
7964 unsigned int atsi_count)
7966 struct MeshPeerInfo *peer_info;
7967 struct MeshPeerPath *path;
7969 DEBUG_CONN ("Peer connected\n");
7970 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7971 peer_info = peer_info_get (peer);
7972 if (myid == peer_info->id)
7974 DEBUG_CONN (" (self)\n");
7979 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7981 path = path_new (2);
7982 path->peers[0] = myid;
7983 path->peers[1] = peer_info->id;
7984 GNUNET_PEER_change_rc (myid, 1);
7985 GNUNET_PEER_change_rc (peer_info->id, 1);
7986 peer_info_add_path (peer_info, path, GNUNET_YES);
7987 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7993 * Method called whenever a peer disconnects.
7995 * @param cls closure
7996 * @param peer peer identity this notification is about
7999 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8001 struct MeshPeerInfo *pi;
8002 struct MeshPeerQueue *q;
8003 struct MeshPeerQueue *n;
8005 DEBUG_CONN ("Peer disconnected\n");
8006 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8016 /* TODO try to reroute this traffic instead */
8017 queue_destroy(q, GNUNET_YES);
8020 if (NULL != pi->core_transmit)
8022 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8023 pi->core_transmit = NULL;
8025 peer_info_remove_path (pi, pi->id, myid);
8028 DEBUG_CONN (" (self)\n");
8030 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8035 /******************************************************************************/
8036 /************************ MAIN FUNCTIONS ****************************/
8037 /******************************************************************************/
8040 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8042 * @param cls closure
8043 * @param key current key code
8044 * @param value value in the hash map
8045 * @return GNUNET_YES if we should continue to iterate,
8049 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8051 struct MeshTunnel *t = value;
8058 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8060 * @param cls closure
8061 * @param key current key code
8062 * @param value value in the hash map
8063 * @return GNUNET_YES if we should continue to iterate,
8067 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8069 struct MeshPeerInfo *p = value;
8070 struct MeshPeerQueue *q;
8071 struct MeshPeerQueue *n;
8079 queue_destroy(q, GNUNET_YES);
8083 peer_info_destroy (p);
8089 * Task run during shutdown.
8095 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8099 if (core_handle != NULL)
8101 GNUNET_CORE_disconnect (core_handle);
8106 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8109 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8110 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8111 if (dht_handle != NULL)
8113 GNUNET_DHT_disconnect (dht_handle);
8118 GNUNET_SERVER_notification_context_destroy (nc);
8121 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8123 GNUNET_SCHEDULER_cancel (announce_id_task);
8124 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8131 * Callback for hostkey read/generation
8134 * @param pk the private key
8135 * @param emsg error message
8138 key_generation_cb (void *cls,
8139 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8142 struct MeshPeerInfo *peer;
8143 struct MeshPeerPath *p;
8148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8149 _("Mesh service could not access hostkey. Exiting.\n"));
8150 GNUNET_SCHEDULER_shutdown ();
8153 my_private_key = pk;
8154 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8155 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8156 &my_full_id.hashPubKey);
8157 myid = GNUNET_PEER_intern (&my_full_id);
8158 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8159 "Mesh for peer [%s] starting\n",
8160 GNUNET_i2s(&my_full_id));
8162 // transport_handle = GNUNET_TRANSPORT_connect(c,
8172 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8175 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8176 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8177 GNUNET_SERVER_disconnect_notify (server_handle,
8178 &handle_local_client_disconnect, NULL);
8182 clients_tail = NULL;
8185 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8186 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8188 /* Create a peer_info for the local peer */
8189 peer = peer_info_get (&my_full_id);
8192 GNUNET_PEER_change_rc (myid, 1);
8193 peer_info_add_path (peer, p, GNUNET_YES);
8194 GNUNET_SERVER_resume (server_handle);
8195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8200 * Process mesh requests.
8202 * @param cls closure
8203 * @param server the initialized server
8204 * @param c configuration to use
8207 run (void *cls, struct GNUNET_SERVER_Handle *server,
8208 const struct GNUNET_CONFIGURATION_Handle *c)
8212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8213 server_handle = server;
8214 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8215 NULL, /* Closure passed to MESH functions */
8216 &core_init, /* Call core_init once connected */
8217 &core_connect, /* Handle connects */
8218 &core_disconnect, /* remove peers on disconnects */
8219 NULL, /* Don't notify about all incoming messages */
8220 GNUNET_NO, /* For header only in notification */
8221 NULL, /* Don't notify about all outbound messages */
8222 GNUNET_NO, /* For header-only out notification */
8223 core_handlers); /* Register these handlers */
8225 if (core_handle == NULL)
8228 GNUNET_SCHEDULER_shutdown ();
8233 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8238 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8240 GNUNET_SCHEDULER_shutdown ();
8245 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8246 &refresh_path_time))
8248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8250 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8251 "mesh", "refresh path time");
8252 GNUNET_SCHEDULER_shutdown ();
8257 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8258 &app_announce_time))
8260 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8262 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8263 "mesh", "app announce time");
8264 GNUNET_SCHEDULER_shutdown ();
8267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8270 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8273 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8275 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8276 "mesh", "id announce time");
8277 GNUNET_SCHEDULER_shutdown ();
8285 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8286 &unacknowledged_wait_time))
8288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8290 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8291 "mesh", "unacknowledged wait time");
8292 GNUNET_SCHEDULER_shutdown ();
8297 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8302 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8303 "mesh", "connect timeout");
8304 GNUNET_SCHEDULER_shutdown ();
8309 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8312 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8314 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8315 "mesh", "max msgs queue");
8316 GNUNET_SCHEDULER_shutdown ();
8321 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8326 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8327 "mesh", "max tunnels");
8328 GNUNET_SCHEDULER_shutdown ();
8333 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8336 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8338 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8339 "mesh", "default ttl", 64);
8344 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8345 &dht_replication_level))
8347 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8349 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8350 "mesh", "dht replication level", 10);
8351 dht_replication_level = 10;
8354 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8355 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8356 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8357 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8358 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8360 dht_handle = GNUNET_DHT_connect (c, 64);
8361 if (NULL == dht_handle)
8365 stats = GNUNET_STATISTICS_create ("mesh", c);
8367 GNUNET_SERVER_suspend (server_handle);
8368 /* Scheduled the task to clean up when shutdown is called */
8369 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8371 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8372 GNUNET_free (keyfile);
8377 * The main function for the mesh service.
8379 * @param argc number of arguments from the command line
8380 * @param argv command line arguments
8381 * @return 0 ok, 1 on error
8384 main (int argc, char *const *argv)
8389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8390 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8392 ret = (GNUNET_OK == r) ? 0 : 1;
8393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8397 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8398 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8399 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);