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);
1290 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1297 * Iterator over edges in a regex block retrieved from the DHT.
1299 * @param cls Closure (context of the search).
1300 * @param token Token that follows to next state.
1301 * @param len Lenght of token.
1302 * @param key Hash of next state.
1304 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1307 regex_edge_iterator (void *cls,
1310 const struct GNUNET_HashCode *key)
1312 struct MeshRegexSearchContext *ctx = cls;
1313 struct MeshRegexSearchInfo *info = ctx->info;
1317 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1322 current = &info->description[ctx->position];
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1324 current_len = strlen (info->description) - ctx->position;
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1329 if (len > current_len)
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1332 return GNUNET_YES; // Token too long, wont match
1334 if (0 != strncmp (current, token, len))
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1337 return GNUNET_YES; // Token doesn't match
1340 if (len > ctx->longest_match)
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1343 ctx->longest_match = len;
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1357 * Jump to the next edge, with the longest matching token.
1359 * @param block Block found in the DHT.
1360 * @param size Size of the block.
1361 * @param ctx Context of the search.
1363 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1366 regex_next_edge (const struct MeshRegexBlock *block,
1368 struct MeshRegexSearchContext *ctx)
1370 struct MeshRegexSearchContext *new_ctx;
1371 struct MeshRegexSearchInfo *info = ctx->info;
1372 struct GNUNET_DHT_GetHandle *get_h;
1376 /* Find the longest match for the current string position,
1377 * among tokens in the given block */
1378 ctx->longest_match = 0;
1379 result = GNUNET_MESH_regex_block_iterate (block, size,
1380 ®ex_edge_iterator, ctx);
1381 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1383 /* Did anything match? */
1384 if (0 == ctx->longest_match)
1387 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1388 new_ctx->info = info;
1389 new_ctx->position = ctx->position + ctx->longest_match;
1390 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1392 /* Check whether we already have a DHT GET running for it */
1394 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1397 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1399 ®ex_result_iterator,
1401 return; // We are already looking for it
1404 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1406 /* Start search in DHT */
1408 GNUNET_DHT_get_start (dht_handle, /* handle */
1409 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1410 &ctx->hash, /* key to search */
1411 dht_replication_level, /* replication level */
1412 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1413 NULL, /* xquery */ // FIXME BLOOMFILTER
1414 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1415 &dht_get_string_handler, new_ctx);
1417 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1420 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1429 * Iterator over hash map entries to cancel DHT GET requests after a
1430 * successful connect_by_string.
1432 * @param cls Closure (unused).
1433 * @param key Current key code (unused).
1434 * @param value Value in the hash map (get handle).
1435 * @return GNUNET_YES if we should continue to iterate,
1439 regex_cancel_dht_get (void *cls,
1440 const struct GNUNET_HashCode * key,
1443 struct GNUNET_DHT_GetHandle *h = value;
1445 GNUNET_DHT_get_stop (h);
1451 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1452 * search for connect_by_string.
1454 * @param cls Closure (unused).
1455 * @param key Current key code (unused).
1456 * @param value MeshRegexBlock in the hash map.
1457 * @return GNUNET_YES if we should continue to iterate,
1461 regex_free_result (void *cls,
1462 const struct GNUNET_HashCode * key,
1466 GNUNET_free (value);
1472 * Regex callback iterator to store own service description in the DHT.
1474 * @param cls closure.
1475 * @param key hash for current state.
1476 * @param proof proof for current state.
1477 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1478 * @param num_edges number of edges leaving current state.
1479 * @param edges edges leaving current state.
1482 regex_iterator (void *cls,
1483 const struct GNUNET_HashCode *key,
1486 unsigned int num_edges,
1487 const struct GNUNET_REGEX_Edge *edges)
1489 struct MeshRegexBlock *block;
1490 struct MeshRegexEdge *block_edge;
1491 enum GNUNET_DHT_RouteOption opt;
1495 unsigned int offset;
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1499 " regex dht put for state %s\n",
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1509 if (GNUNET_YES == accepting)
1511 struct MeshRegexAccept block;
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 " state %s is accepting, putting own id\n",
1516 size = sizeof (block);
1518 block.id = my_full_id;
1520 GNUNET_DHT_put(dht_handle, key,
1521 dht_replication_level,
1522 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1523 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1526 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1531 len = strlen(proof);
1532 size = sizeof (struct MeshRegexBlock) + len;
1533 block = GNUNET_malloc (size);
1536 block->n_proof = htonl (len);
1537 block->n_edges = htonl (num_edges);
1538 block->accepting = htonl (accepting);
1540 /* Store the proof at the end of the block. */
1541 aux = (char *) &block[1];
1542 memcpy (aux, proof, len);
1545 /* Store each edge in a variable length MeshEdge struct at the
1546 * very end of the MeshRegexBlock structure.
1548 for (i = 0; i < num_edges; i++)
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 " edge %s towards %s\n",
1553 GNUNET_h2s(&edges[i].destination));
1555 /* aux points at the end of the last block */
1556 len = strlen (edges[i].label);
1557 size += sizeof (struct MeshRegexEdge) + len;
1558 // Calculate offset FIXME is this ok? use size instead?
1559 offset = aux - (char *) block;
1560 block = GNUNET_realloc (block, size);
1561 aux = &((char *) block)[offset];
1562 block_edge = (struct MeshRegexEdge *) aux;
1563 block_edge->key = edges[i].destination;
1564 block_edge->n_token = htonl (len);
1565 aux = (char *) &block_edge[1];
1566 memcpy (aux, edges[i].label, len);
1570 GNUNET_DHT_put(dht_handle, key,
1571 dht_replication_level,
1573 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1575 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1579 GNUNET_free (block);
1584 * Store the regular expression describing a local service into the DHT.
1586 * @param regex The regular expresion.
1589 regex_put (const char *regex)
1591 struct GNUNET_REGEX_Automaton *dfa;
1593 DEBUG_DHT (" regex_put (%s) start\n", regex);
1594 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1595 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1596 GNUNET_REGEX_automaton_destroy (dfa);
1597 DEBUG_DHT (" regex_put (%s) end\n", regex);
1602 * Find a path to a peer that offers a regex servcie compatible
1603 * with a given string.
1605 * @param key The key of the accepting state.
1606 * @param ctx Context containing info about the string, tunnel, etc.
1609 regex_find_path (const struct GNUNET_HashCode *key,
1610 struct MeshRegexSearchContext *ctx)
1612 struct GNUNET_DHT_GetHandle *get_h;
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1615 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1616 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1617 key, /* key to search */
1618 dht_replication_level, /* replication level */
1619 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1620 GNUNET_DHT_RO_RECORD_ROUTE,
1621 NULL, /* xquery */ // FIXME BLOOMFILTER
1622 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1623 &dht_get_string_accept_handler, ctx);
1624 GNUNET_break (GNUNET_OK ==
1625 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1628 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1633 * Function called if the connect attempt to a peer found via
1634 * connect_by_string times out. Try to connect to another peer, if any.
1635 * Otherwise try to reconnect to the same peer.
1637 * @param cls Closure (info about regex search).
1638 * @param tc TaskContext.
1641 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1643 struct MeshRegexSearchInfo *info = cls;
1644 struct MeshPeerInfo *peer_info;
1648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1649 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1650 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1658 if (0 < info->n_peers)
1660 // Select next peer, put current in that spot.
1661 id = info->peers[info->i_peer];
1662 info->peers[info->i_peer] = info->peer;
1663 info->i_peer = (info->i_peer + 1) % info->n_peers;
1667 // Try to connect to same peer again.
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1672 peer_info = peer_info_get_short(id);
1673 tunnel_add_peer (info->t, peer_info);
1675 tunnel_delete_peer (info->t, old);
1676 peer_info_connect (peer_info, info->t);
1677 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1678 ®ex_connect_timeout,
1680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1685 * Cancel an ongoing regex search in the DHT and free all resources.
1687 * @param ctx The search context.
1690 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1692 struct MeshRegexSearchInfo *info = ctx->info;
1695 GNUNET_free (info->description);
1696 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1697 ®ex_cancel_dht_get, NULL);
1698 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1699 ®ex_free_result, NULL);
1700 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1701 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1702 info->t->regex_ctx = NULL;
1703 for (i = 0; i < info->n_contexts; i++)
1705 GNUNET_free (info->contexts[i]);
1707 if (0 < info->n_contexts)
1708 GNUNET_free (info->contexts);
1709 if (0 < info->n_peers)
1710 GNUNET_free (info->peers);
1711 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1713 GNUNET_SCHEDULER_cancel(info->timeout);
1719 /******************************************************************************/
1720 /************************ PERIODIC FUNCTIONS ****************************/
1721 /******************************************************************************/
1724 * Announce iterator over for each application provided by the peer
1726 * @param cls closure
1727 * @param key current key code
1728 * @param value value in the hash map
1729 * @return GNUNET_YES if we should continue to
1734 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1736 struct PBlock block;
1737 struct MeshClient *c;
1739 block.id = my_full_id;
1740 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1741 GNUNET_assert(NULL != c);
1742 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1743 if (0 == block.type)
1748 block.type = htonl (block.type);
1750 GNUNET_break (NULL !=
1751 GNUNET_DHT_put (dht_handle, key,
1752 dht_replication_level,
1753 GNUNET_DHT_RO_RECORD_ROUTE |
1754 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1755 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1757 (const char *) &block,
1758 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1760 app_announce_time, NULL, NULL));
1766 * Periodically announce what applications are provided by local clients
1769 * @param cls closure
1770 * @param tc task context
1773 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1775 struct MeshClient *c = cls;
1778 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1779 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1784 DEBUG_DHT ("Starting PUT for regex\n");
1786 for (i = 0; i < c->n_regex; i++)
1788 regex_put (c->regexes[i]);
1790 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1793 DEBUG_DHT ("Finished PUT for regex\n");
1800 * Periodically announce what applications are provided by local clients
1803 * @param cls closure
1804 * @param tc task context
1807 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1809 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1811 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1815 DEBUG_DHT ("Starting PUT for apps\n");
1817 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1819 announce_applications_task =
1820 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1822 DEBUG_DHT ("Finished PUT for apps\n");
1829 * Periodically announce self id in the DHT
1831 * @param cls closure
1832 * @param tc task context
1835 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1837 struct PBlock block;
1839 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1841 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1845 * - Set data expiration in function of X
1846 * - Adapt X to churn
1848 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1850 block.id = my_full_id;
1851 block.type = htonl (0);
1852 GNUNET_DHT_put (dht_handle, /* DHT handle */
1853 &my_full_id.hashPubKey, /* Key to use */
1854 dht_replication_level, /* Replication level */
1855 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1856 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1857 sizeof (block), /* Size of the data */
1858 (const char *) &block, /* Data itself */
1859 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1860 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1861 NULL, /* Continuation */
1862 NULL); /* Continuation closure */
1864 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1868 /******************************************************************************/
1869 /****************** GENERAL HELPER FUNCTIONS ************************/
1870 /******************************************************************************/
1873 * Decrements the reference counter and frees all resources if needed
1875 * @param mesh_data Data Descriptor used in a multicast message.
1876 * Freed no longer needed (last message).
1879 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1881 if (0 == --(mesh_data->reference_counter))
1883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1884 GNUNET_free (mesh_data->data);
1885 GNUNET_free (mesh_data);
1891 * Check if client has registered with the service and has not disconnected
1893 * @param client the client to check
1895 * @return non-NULL if client exists in the global DLL
1897 static struct MeshClient *
1898 client_get (struct GNUNET_SERVER_Client *client)
1900 struct MeshClient *c;
1905 if (c->handle == client)
1914 * Checks if a given client has subscribed to certain message type
1916 * @param message_type Type of message to check
1917 * @param c Client to check
1919 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1921 * FIXME: use of crypto_hash slows it down
1922 * The hash function alone takes 8-10us out of the ~55us for the whole
1923 * process of retransmitting the message from one local client to another.
1924 * Find faster implementation!
1927 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1929 struct GNUNET_HashCode hc;
1931 if (NULL == c->types)
1934 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1935 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1940 * Check whether client wants traffic from a tunnel.
1942 * @param c Client to check.
1943 * @param t Tunnel to be found.
1945 * @return GNUNET_YES if client knows tunnel.
1947 * TODO look in client hashmap
1950 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1954 for (i = 0; i < t->nclients; i++)
1955 if (t->clients[i] == c)
1962 * Check whether client has been informed about a tunnel.
1964 * @param c Client to check.
1965 * @param t Tunnel to be found.
1967 * @return GNUNET_YES if client knows tunnel.
1969 * TODO look in client hashmap
1972 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1976 for (i = 0; i < t->nignore; i++)
1977 if (t->ignore[i] == c)
1979 return client_wants_tunnel(c, t);
1984 * Marks a client as uninterested in traffic from the tunnel, updating both
1985 * client and tunnel to reflect this.
1987 * @param c Client that doesn't want traffic anymore.
1988 * @param t Tunnel which should be ignored.
1990 * FIXME when to delete an incoming tunnel?
1993 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1995 struct GNUNET_HashCode hash;
1997 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1998 GNUNET_break (GNUNET_YES ==
1999 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2001 GNUNET_break (GNUNET_YES ==
2002 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2003 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2004 tunnel_delete_active_client (t, c);
2005 GNUNET_array_append (t->ignore, t->nignore, c);
2010 * Deletes a tunnel from a client (either owner or destination). To be used on
2011 * tunnel destroy, otherwise, use client_ignore_tunnel.
2013 * @param c Client whose tunnel to delete.
2014 * @param t Tunnel which should be deleted.
2017 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2019 struct GNUNET_HashCode hash;
2023 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2024 GNUNET_assert (GNUNET_YES ==
2025 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2031 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2033 GNUNET_assert (GNUNET_YES ==
2034 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2038 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2046 * Send the message to all clients that have subscribed to its type
2048 * @param msg Pointer to the message itself
2049 * @param payload Pointer to the payload of the message.
2050 * @param t The tunnel to whose clients this message goes.
2052 * @return number of clients this message was sent to
2055 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2056 const struct GNUNET_MessageHeader *payload,
2057 struct MeshTunnel *t)
2059 struct MeshClient *c;
2060 MESH_TunnelNumber *tid;
2063 char cbuf[htons (msg->size)];
2065 type = ntohs (payload->type);
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2068 GNUNET_MESH_DEBUG_M2S (type));
2070 memcpy (cbuf, msg, sizeof (cbuf));
2071 switch (htons (msg->type))
2073 struct GNUNET_MESH_Unicast *uc;
2074 struct GNUNET_MESH_Multicast *mc;
2075 struct GNUNET_MESH_ToOrigin *to;
2077 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2078 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2081 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2082 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2085 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2086 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2094 for (count = 0, c = clients; c != NULL; c = c->next)
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2097 if (client_is_subscribed (type, c))
2099 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2103 *tid = htonl (t->local_tid);
2107 if (GNUNET_NO == client_knows_tunnel (c, t))
2109 /* This client doesn't know the tunnel */
2110 struct GNUNET_MESH_TunnelNotification tmsg;
2111 struct GNUNET_HashCode hash;
2113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2114 tmsg.header.size = htons (sizeof (tmsg));
2115 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2116 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2117 tmsg.tunnel_id = htonl (t->local_tid_dest);
2119 if (GNUNET_YES == t->speed_min)
2120 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2121 if (GNUNET_YES == t->nobuffer)
2122 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2123 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2124 &tmsg.header, GNUNET_NO);
2125 tunnel_add_client (t, c);
2126 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2128 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2129 c->incoming_tunnels, &hash, t,
2130 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2132 *tid = htonl (t->local_tid_dest);
2135 /* Check if the client wants to get traffic from the tunnel */
2136 if (GNUNET_NO == client_wants_tunnel(c, t))
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2140 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2141 (struct GNUNET_MessageHeader
2142 *) cbuf, GNUNET_NO);
2151 * Notify the client that owns the tunnel that a peer has connected to it
2152 * (the requested path to it has been confirmed).
2154 * @param t Tunnel whose owner to notify
2155 * @param id Short id of the peer that has connected
2158 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2160 struct GNUNET_MESH_PeerControl pc;
2162 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2163 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2164 pc.tunnel_id = htonl (t->local_tid);
2165 GNUNET_PEER_resolve (id, &pc.peer);
2166 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2172 * Notify all clients (not depending on registration status) that the incoming
2173 * tunnel is no longer valid.
2175 * @param t Tunnel that was destroyed.
2178 send_clients_tunnel_destroy (struct MeshTunnel *t)
2180 struct GNUNET_MESH_TunnelMessage msg;
2182 msg.header.size = htons (sizeof (msg));
2183 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2184 msg.tunnel_id = htonl (t->local_tid_dest);
2185 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2190 * Notify clients of tunnel disconnections, if needed.
2191 * In case the origin disconnects, the destination clients get a tunnel destroy
2192 * notification. If the last destination disconnects (only one remaining client
2193 * in tunnel), the origin gets a (local ID) peer disconnected.
2194 * Note that the function must be called BEFORE removing the client from
2197 * @param t Tunnel that was destroyed.
2198 * @param c Client that disconnected.
2201 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2207 struct GNUNET_MESH_TunnelMessage msg;
2209 msg.header.size = htons (sizeof (msg));
2210 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2211 msg.tunnel_id = htonl (t->local_tid_dest);
2212 for (i = 0; i < t->nclients; i++)
2213 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2214 &msg.header, GNUNET_NO);
2216 // FIXME when to disconnect an incoming tunnel?
2217 else if (1 == t->nclients && NULL != t->owner)
2219 struct GNUNET_MESH_PeerControl msg;
2221 msg.header.size = htons (sizeof (msg));
2222 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2223 msg.tunnel_id = htonl (t->local_tid);
2224 msg.peer = my_full_id;
2225 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2226 &msg.header, GNUNET_NO);
2232 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2233 * and insert it in the appropiate structures if the peer is not known yet.
2235 * @param peer Full identity of the peer.
2237 * @return Existing or newly created peer info.
2239 static struct MeshPeerInfo *
2240 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2242 struct MeshPeerInfo *peer_info;
2244 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2245 if (NULL == peer_info)
2248 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2249 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2250 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2251 peer_info->id = GNUNET_PEER_intern (peer);
2259 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2260 * and insert it in the appropiate structures if the peer is not known yet.
2262 * @param peer Short identity of the peer.
2264 * @return Existing or newly created peer info.
2266 static struct MeshPeerInfo *
2267 peer_info_get_short (const GNUNET_PEER_Id peer)
2269 struct GNUNET_PeerIdentity id;
2271 GNUNET_PEER_resolve (peer, &id);
2272 return peer_info_get (&id);
2277 * Iterator to remove the tunnel from the list of tunnels a peer participates
2280 * @param cls Closure (tunnel info)
2281 * @param key GNUNET_PeerIdentity of the peer (unused)
2282 * @param value PeerInfo of the peer
2284 * @return always GNUNET_YES, to keep iterating
2287 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2289 struct MeshTunnel *t = cls;
2290 struct MeshPeerInfo *peer = value;
2293 for (i = 0; i < peer->ntunnels; i++)
2296 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2299 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2300 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2309 * Core callback to write a pre-constructed data packet to core buffer
2311 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2312 * @param size Number of bytes available in buf.
2313 * @param buf Where the to write the message.
2315 * @return number of bytes written to buf
2318 send_core_data_raw (void *cls, size_t size, void *buf)
2320 struct MeshTransmissionDescriptor *info = cls;
2321 struct GNUNET_MessageHeader *msg;
2324 GNUNET_assert (NULL != info);
2325 GNUNET_assert (NULL != info->mesh_data);
2326 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2327 total_size = ntohs (msg->size);
2329 if (total_size > size)
2334 memcpy (buf, msg, total_size);
2335 data_descriptor_decrement_rc (info->mesh_data);
2342 * Sends an already built non-multicast message to a peer,
2343 * properly registrating all used resources.
2345 * @param message Message to send. Function makes a copy of it.
2346 * @param peer Short ID of the neighbor whom to send the message.
2347 * @param t Tunnel on which this message is transmitted.
2350 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2351 const struct GNUNET_PeerIdentity *peer,
2352 struct MeshTunnel *t)
2354 struct MeshTransmissionDescriptor *info;
2355 struct MeshPeerInfo *neighbor;
2356 struct MeshPeerPath *p;
2360 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2362 size = ntohs (message->size);
2363 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2364 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2365 info->mesh_data->data = GNUNET_malloc (size);
2366 memcpy (info->mesh_data->data, message, size);
2367 type = ntohs(message->type);
2370 struct GNUNET_MESH_Unicast *m;
2371 struct GNUNET_MESH_ToOrigin *to;
2373 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2374 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2375 m->ttl = htonl (ntohl (m->ttl) - 1);
2377 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2378 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2380 to->pid = htonl(t->bck_pid);
2382 info->mesh_data->data_len = size;
2383 info->mesh_data->reference_counter = 1;
2384 info->mesh_data->total_out = 1;
2385 neighbor = peer_info_get (peer);
2386 for (p = neighbor->path_head; NULL != p; p = p->next)
2396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2397 " %s IS NOT DIRECTLY CONNECTED\n",
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 for (p = neighbor->path_head; NULL != p; p = p->next)
2404 struct GNUNET_PeerIdentity debug_id;
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 " path with %u hops through:\n",
2410 for (i = 0; i < p->length; i++)
2412 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2415 i, GNUNET_i2s(&debug_id));
2419 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2420 GNUNET_free (info->mesh_data->data);
2421 GNUNET_free (info->mesh_data);
2425 info->peer = neighbor;
2426 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2437 * Sends a CREATE PATH message for a path to a peer, properly registrating
2438 * all used resources.
2440 * @param peer PeerInfo of the final peer for whom this path is being created.
2441 * @param p Path itself.
2442 * @param t Tunnel for which the path is created.
2445 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2446 struct MeshTunnel *t)
2448 struct GNUNET_PeerIdentity id;
2449 struct MeshPathInfo *path_info;
2450 struct MeshPeerInfo *neighbor;
2456 p = tree_get_path_to_peer (t->tree, peer->id);
2463 for (i = 0; i < p->length; i++)
2465 if (p->peers[i] == myid)
2468 if (i >= p->length - 1)
2474 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2476 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2477 path_info->path = p;
2479 neighbor = peer_info_get (&id);
2480 path_info->peer = neighbor;
2481 queue_add (path_info,
2482 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2483 sizeof (struct GNUNET_MESH_ManipulatePath) +
2484 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2491 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2493 * @param t Tunnel whose path to destroy.
2494 * @param destination Short ID of the peer to whom the path to destroy.
2497 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2499 struct MeshPeerPath *p;
2502 p = tree_get_path_to_peer (t->tree, destination);
2508 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2509 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2511 struct GNUNET_MESH_ManipulatePath *msg;
2512 struct GNUNET_PeerIdentity *pi;
2516 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2517 msg->header.size = htons (size);
2518 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2519 msg->tid = htonl (t->id.tid);
2520 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2521 for (i = 0; i < p->length; i++)
2523 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2525 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2532 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2534 * @param t Tunnel which to confirm.
2537 send_path_ack (struct MeshTunnel *t)
2539 struct MeshTransmissionDescriptor *info;
2540 struct GNUNET_PeerIdentity id;
2541 GNUNET_PEER_Id peer;
2543 peer = tree_get_predecessor (t->tree);
2544 GNUNET_PEER_resolve (peer, &id);
2545 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2546 info->origin = &t->id;
2547 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2548 GNUNET_assert (NULL != info->peer);
2551 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2552 sizeof (struct GNUNET_MESH_PathACK),
2559 * Try to establish a new connection to this peer.
2560 * Use the best path for the given tunnel.
2561 * If the peer doesn't have any path to it yet, try to get one.
2562 * If the peer already has some path, send a CREATE PATH towards it.
2564 * @param peer PeerInfo of the peer.
2565 * @param t Tunnel for which to create the path, if possible.
2568 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2570 struct MeshPeerPath *p;
2571 struct MeshPathInfo *path_info;
2573 if (NULL != peer->path_head)
2575 p = tree_get_path_to_peer (t->tree, peer->id);
2582 // FIXME always send create path to self
2585 send_create_path (peer, p, t);
2589 struct GNUNET_HashCode hash;
2592 send_client_peer_connected (t, myid);
2593 t->local_tid_dest = next_local_tid++;
2594 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2597 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2598 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2605 else if (NULL == peer->dhtget)
2607 struct GNUNET_PeerIdentity id;
2609 GNUNET_PEER_resolve (peer->id, &id);
2610 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2611 path_info->peer = peer;
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2614 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2615 peer->dhtgetcls = path_info;
2616 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2617 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2618 &id.hashPubKey, /* key to search */
2619 dht_replication_level, /* replication level */
2620 GNUNET_DHT_RO_RECORD_ROUTE |
2621 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2622 NULL, /* xquery */ // FIXME BLOOMFILTER
2623 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2624 &dht_get_id_handler, path_info);
2626 /* Otherwise, there is no path but the DHT get is already started. */
2631 * Task to delay the connection of a peer
2633 * @param cls Closure (path info with tunnel and peer to connect).
2634 * Will be free'd on exection.
2635 * @param tc TaskContext
2638 peer_info_connect_task (void *cls,
2639 const struct GNUNET_SCHEDULER_TaskContext *tc)
2641 struct MeshPathInfo *path_info = cls;
2643 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2645 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2650 peer_info_connect (path_info->peer, path_info->t);
2656 * Destroy the peer_info and free any allocated resources linked to it
2658 * @param pi The peer_info to destroy.
2660 * @return GNUNET_OK on success
2663 peer_info_destroy (struct MeshPeerInfo *pi)
2665 struct GNUNET_PeerIdentity id;
2666 struct MeshPeerPath *p;
2667 struct MeshPeerPath *nextp;
2669 GNUNET_PEER_resolve (pi->id, &id);
2670 GNUNET_PEER_change_rc (pi->id, -1);
2673 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2676 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2677 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2679 if (NULL != pi->dhtget)
2681 GNUNET_DHT_get_stop (pi->dhtget);
2682 GNUNET_free (pi->dhtgetcls);
2688 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2692 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2694 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2702 * Remove all paths that rely on a direct connection between p1 and p2
2703 * from the peer itself and notify all tunnels about it.
2705 * @param peer PeerInfo of affected peer.
2706 * @param p1 GNUNET_PEER_Id of one peer.
2707 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2710 * TODO: optimize (see below)
2713 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2716 struct MeshPeerPath *p;
2717 struct MeshPeerPath *aux;
2718 struct MeshPeerInfo *peer_d;
2720 unsigned int destroyed;
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2727 p = peer->path_head;
2731 for (i = 0; i < (p->length - 1); i++)
2733 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2734 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2736 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2747 for (i = 0; i < peer->ntunnels; i++)
2749 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2753 * Problem: one or more peers have been deleted from the tunnel tree.
2754 * We don't know who they are to try to add them again.
2755 * We need to try to find a new path for each of the disconnected peers.
2756 * Some of them might already have a path to reach them that does not
2757 * involve p1 and p2. Adding all anew might render in a better tree than
2758 * the trivial immediate fix.
2760 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2761 * its children will be reachable trough him.
2763 peer_d = peer_info_get_short (d);
2766 for (p = peer_d->path_head; NULL != p; p = p->next)
2768 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2776 /* No callback, as peer will be already disconnected and a connection
2777 * scheduled by tunnel_notify_connection_broken.
2779 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2783 peer_info_connect (peer_d, peer->tunnels[i]);
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2791 * Add the path to the peer and update the path used to reach it in case this
2794 * @param peer_info Destination peer to add the path to.
2795 * @param path New path to add. Last peer must be the peer in arg 1.
2796 * Path will be either used of freed if already known.
2797 * @param trusted Do we trust that this path is real?
2800 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2803 struct MeshPeerPath *aux;
2807 if ((NULL == peer_info) || (NULL == path))
2810 path_destroy (path);
2813 if (path->peers[path->length - 1] != peer_info->id)
2816 path_destroy (path);
2819 if (path->length <= 2 && GNUNET_NO == trusted)
2821 /* Only allow CORE to tell us about direct paths */
2822 path_destroy (path);
2825 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2826 for (l = 1; l < path->length; l++)
2828 if (path->peers[l] == myid)
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2831 for (l2 = 0; l2 < path->length - l; l2++)
2833 path->peers[l2] = path->peers[l + l2];
2838 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2843 struct GNUNET_PeerIdentity id;
2845 GNUNET_PEER_resolve (peer_info->id, &id);
2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2847 path->length, GNUNET_i2s (&id));
2850 l = path_get_length (path);
2857 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2858 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2860 l2 = path_get_length (aux);
2863 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2864 peer_info->path_tail, aux, path);
2869 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2871 path_destroy (path);
2876 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2883 * Add the path to the origin peer and update the path used to reach it in case
2884 * this is the shortest.
2885 * The path is given in peer_info -> destination, therefore we turn the path
2886 * upside down first.
2888 * @param peer_info Peer to add the path to, being the origin of the path.
2889 * @param path New path to add after being inversed.
2890 * @param trusted Do we trust that this path is real?
2893 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2894 struct MeshPeerPath *path, int trusted)
2897 peer_info_add_path (peer_info, path, trusted);
2902 * Function called if the connection to the peer has been stalled for a while,
2903 * possibly due to a missed ACK. Poll the peer about its ACK status.
2905 * @param cls Closure (info about regex search).
2906 * @param tc TaskContext.
2909 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2911 struct MeshTunnelChildInfo *cinfo = cls;
2912 struct GNUNET_MESH_Poll msg;
2913 struct GNUNET_PeerIdentity id;
2914 struct MeshTunnel *t;
2916 return; // FIXME fc activate
2917 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2918 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2924 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2925 msg.header.size = htons (sizeof (msg));
2926 msg.tid = htonl (t->id.tid);
2927 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2928 msg.last_ack = htonl (cinfo->fwd_ack);
2930 GNUNET_PEER_resolve (tree_get_predecessor(cinfo->t->tree), &id);
2931 send_prebuilt_message (&msg.header, &id, cinfo->t);
2932 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2933 &tunnel_poll, cinfo);
2938 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2939 * to obtain a local peer -> destination path and interning the peer ids.
2941 * @return Newly allocated and created path
2943 static struct MeshPeerPath *
2944 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2945 unsigned int get_path_length,
2946 const struct GNUNET_PeerIdentity *put_path,
2947 unsigned int put_path_length)
2949 struct MeshPeerPath *p;
2955 GNUNET_PEER_change_rc (myid, 1);
2956 i = get_path_length;
2957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2958 for (i--; i >= 0; i--)
2960 id = GNUNET_PEER_intern (&get_path[i]);
2961 if (p->length > 0 && id == p->peers[p->length - 1])
2963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2964 GNUNET_PEER_change_rc (id, -1);
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2969 GNUNET_i2s (&get_path[i]));
2971 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2972 p->peers[p->length - 1] = id;
2975 i = put_path_length;
2976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2977 for (i--; i >= 0; i--)
2979 id = GNUNET_PEER_intern (&put_path[i]);
2982 /* PUT path went through us, so discard the path up until now and start
2983 * from here to get a much shorter (and loop-free) path.
2988 if (p->length > 0 && id == p->peers[p->length - 1])
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2991 GNUNET_PEER_change_rc (id, -1);
2995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2996 GNUNET_i2s (&put_path[i]));
2998 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2999 p->peers[p->length - 1] = id;
3003 if (get_path_length > 0)
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3005 GNUNET_i2s (&get_path[0]));
3006 if (put_path_length > 0)
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3008 GNUNET_i2s (&put_path[0]));
3009 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3011 for (i = 0; i < p->length; i++)
3013 struct GNUNET_PeerIdentity peer_id;
3015 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3017 GNUNET_i2s (&peer_id));
3025 * Adds a path to the peer_infos of all the peers in the path
3027 * @param p Path to process.
3028 * @param confirmed Whether we know if the path works or not.
3031 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3035 /* TODO: invert and add */
3036 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3037 for (i++; i < p->length; i++)
3039 struct MeshPeerInfo *aux;
3040 struct MeshPeerPath *copy;
3042 aux = peer_info_get_short (p->peers[i]);
3043 copy = path_duplicate (p);
3044 copy->length = i + 1;
3045 peer_info_add_path (aux, copy, GNUNET_NO);
3051 * Send keepalive packets for a peer
3053 * @param cls Closure (tunnel for which to send the keepalive).
3054 * @param tc Notification context.
3056 * TODO: implement explicit multicast keepalive?
3059 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3063 * Search for a tunnel among the incoming tunnels
3065 * @param tid the local id of the tunnel
3067 * @return tunnel handler, NULL if doesn't exist
3069 static struct MeshTunnel *
3070 tunnel_get_incoming (MESH_TunnelNumber tid)
3072 struct GNUNET_HashCode hash;
3074 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3075 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3076 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3081 * Search for a tunnel among the tunnels for a client
3083 * @param c the client whose tunnels to search in
3084 * @param tid the local id of the tunnel
3086 * @return tunnel handler, NULL if doesn't exist
3088 static struct MeshTunnel *
3089 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3091 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3093 return tunnel_get_incoming (tid);
3097 struct GNUNET_HashCode hash;
3099 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3100 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3106 * Search for a tunnel by global ID using PEER_ID
3108 * @param pi owner of the tunnel
3109 * @param tid global tunnel number
3111 * @return tunnel handler, NULL if doesn't exist
3113 static struct MeshTunnel *
3114 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3116 struct MESH_TunnelID id;
3117 struct GNUNET_HashCode hash;
3122 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3123 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3128 * Search for a tunnel by global ID using full PeerIdentities
3130 * @param oid owner of the tunnel
3131 * @param tid global tunnel number
3133 * @return tunnel handler, NULL if doesn't exist
3135 static struct MeshTunnel *
3136 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3138 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3143 * Delete an active client from the tunnel.
3149 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3153 for (i = 0; i < t->nclients; i++)
3155 if (t->clients[i] == c)
3157 t->clients[i] = t->clients[t->nclients - 1];
3158 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3159 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3161 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3169 * Delete an ignored client from the tunnel.
3175 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3179 for (i = 0; i < t->nignore; i++)
3181 if (t->ignore[i] == c)
3183 t->ignore[i] = t->ignore[t->nignore - 1];
3184 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3192 * Delete a client from the tunnel. It should be only done on
3193 * client disconnection, otherwise use client_ignore_tunnel.
3199 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3201 tunnel_delete_ignored_client (t, c);
3202 tunnel_delete_active_client (t, c);
3207 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3209 * Destroys queue elements of all waiting transmissions and frees all memory
3210 * used by the struct and its elements.
3212 * @param cls Closure (tunnel info).
3213 * @param key Hash of GNUNET_PEER_Id (unused).
3214 * @param value MeshTunnelChildInfo of the child.
3216 * @return always GNUNET_YES, to keep iterating
3219 tunnel_destroy_child (void *cls,
3220 const struct GNUNET_HashCode * key,
3223 struct MeshTunnelChildInfo *cinfo = value;
3224 struct MeshTunnel *t = cls;
3225 struct MeshPeerQueue *q;
3229 for (c = 0; c < cinfo->send_buffer_n; c++)
3231 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3232 q = cinfo->send_buffer[i];
3233 cinfo->send_buffer[i] = NULL;
3235 queue_destroy (q, GNUNET_YES);
3238 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3240 GNUNET_free_non_null (cinfo->send_buffer);
3241 GNUNET_free (cinfo);
3247 * Callback used to notify a client owner of a tunnel that a peer has
3248 * disconnected, most likely because of a path change.
3250 * @param cls Closure (tunnel this notification is about).
3251 * @param peer_id Short ID of disconnected peer.
3254 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3256 struct MeshTunnel *t = cls;
3257 struct MeshPeerInfo *peer;
3258 struct MeshPathInfo *path_info;
3260 if (NULL != t->owner && NULL != nc)
3262 struct GNUNET_MESH_PeerControl msg;
3264 msg.header.size = htons (sizeof (msg));
3265 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3266 msg.tunnel_id = htonl (t->local_tid);
3267 GNUNET_PEER_resolve (peer_id, &msg.peer);
3268 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3269 &msg.header, GNUNET_NO);
3271 peer = peer_info_get_short (peer_id);
3272 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3273 path_info->peer = peer;
3275 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3281 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3282 * needed rescources.
3283 * If peer already exists, reevaluate shortest path and change if different.
3285 * @param t Tunnel we want to add a new peer to
3286 * @param peer PeerInfo of the peer being added
3290 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3292 struct GNUNET_PeerIdentity id;
3293 struct MeshPeerPath *best_p;
3294 struct MeshPeerPath *p;
3295 unsigned int best_cost;
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3299 GNUNET_PEER_resolve (peer->id, &id);
3301 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3304 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3305 GNUNET_assert (GNUNET_OK ==
3306 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3308 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3311 if (NULL != (p = peer->path_head))
3314 best_cost = tree_get_path_cost (t->tree, p);
3317 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3324 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3325 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3326 t->path_refresh_task =
3327 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3331 /* Start a DHT get */
3332 peer_info_connect (peer, t);
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3338 * Add a path to a tunnel which we don't own, just to remember the next hop.
3339 * If destination node was already in the tunnel, the first hop information
3340 * will be replaced with the new path.
3342 * @param t Tunnel we want to add a new peer to
3343 * @param p Path to add
3344 * @param own_pos Position of local node in path.
3348 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3349 unsigned int own_pos)
3351 struct GNUNET_PeerIdentity id;
3353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3354 GNUNET_assert (0 != own_pos);
3355 tree_add_path (t->tree, p, NULL, NULL);
3356 if (own_pos < p->length - 1)
3358 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3359 tree_update_first_hops (t->tree, myid, &id);
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3365 * Add a client to a tunnel, initializing all needed data structures.
3367 * @param t Tunnel to which add the client.
3368 * @param c Client which to add to the tunnel.
3371 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3373 struct MeshTunnelClientInfo clinfo;
3375 GNUNET_array_append (t->clients, t->nclients, c);
3376 clinfo.fwd_ack = t->fwd_pid + 1;
3377 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3378 clinfo.fwd_pid = t->fwd_pid;
3379 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3381 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3386 * Notifies a tunnel that a connection has broken that affects at least
3387 * some of its peers. Sends a notification towards the root of the tree.
3388 * In case the peer is the owner of the tree, notifies the client that owns
3389 * the tunnel and tries to reconnect.
3391 * @param t Tunnel affected.
3392 * @param p1 Peer that got disconnected from p2.
3393 * @param p2 Peer that got disconnected from p1.
3395 * @return Short ID of the peer disconnected (either p1 or p2).
3396 * 0 if the tunnel remained unaffected.
3398 static GNUNET_PEER_Id
3399 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3405 tree_notify_connection_broken (t->tree, p1, p2,
3406 &tunnel_notify_client_peer_disconnected,
3408 if (myid != p1 && myid != p2)
3414 if (tree_get_predecessor (t->tree) != 0)
3416 /* We are the peer still connected, notify owner of the disconnection. */
3417 struct GNUNET_MESH_PathBroken msg;
3418 struct GNUNET_PeerIdentity neighbor;
3420 msg.header.size = htons (sizeof (msg));
3421 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3422 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3423 msg.tid = htonl (t->id.tid);
3424 msg.peer1 = my_full_id;
3425 GNUNET_PEER_resolve (pid, &msg.peer2);
3426 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3427 send_prebuilt_message (&msg.header, &neighbor, t);
3435 * Send a multicast packet to a neighbor.
3437 * @param cls Closure (Info about the multicast packet)
3438 * @param neighbor_id Short ID of the neighbor to send the packet to.
3441 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3443 struct MeshData *mdata = cls;
3444 struct MeshTransmissionDescriptor *info;
3445 struct GNUNET_PeerIdentity neighbor;
3446 struct GNUNET_MessageHeader *msg;
3448 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3450 info->mesh_data = mdata;
3451 (mdata->reference_counter) ++;
3452 info->destination = neighbor_id;
3453 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3455 GNUNET_i2s (&neighbor));
3456 info->peer = peer_info_get (&neighbor);
3457 GNUNET_assert (NULL != info->peer);
3458 msg = (struct GNUNET_MessageHeader *) mdata->data;
3461 info->mesh_data->data_len,
3468 * Queue a message in a tunnel in multicast, sending a copy to each child node
3469 * down the local one in the tunnel tree.
3471 * @param t Tunnel in which to send the data.
3472 * @param msg Message to be sent.
3475 tunnel_send_multicast (struct MeshTunnel *t,
3476 const struct GNUNET_MessageHeader *msg)
3478 struct MeshData *mdata;
3480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3481 " sending a multicast packet...\n");
3483 mdata = GNUNET_malloc (sizeof (struct MeshData));
3484 mdata->data_len = ntohs (msg->size);
3486 mdata->data = GNUNET_malloc (mdata->data_len);
3487 memcpy (mdata->data, msg, mdata->data_len);
3488 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3490 struct GNUNET_MESH_Multicast *mcast;
3492 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3493 if (t->fwd_queue_n >= t->fwd_queue_max)
3496 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3498 " message from %s!\n",
3499 GNUNET_i2s(&mcast->oid));
3500 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3501 " message at %s!\n",
3502 GNUNET_i2s(&my_full_id));
3503 GNUNET_free (mdata->data);
3504 GNUNET_free (mdata);
3508 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3510 ntohl (mcast->ttl));
3514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3517 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3518 if (mdata->reference_counter == 0)
3520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3521 " no one to send data to\n");
3522 GNUNET_free (mdata->data);
3523 GNUNET_free (mdata);
3528 mdata->total_out = mdata->reference_counter;
3530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3531 " sending a multicast packet done\n");
3537 * Increase the SKIP value of all peers that
3538 * have not received a unicast message.
3540 * @param cls Closure (ID of the peer that HAS received the message).
3541 * @param key ID of the neighbor.
3542 * @param value Information about the neighbor.
3544 * @return GNUNET_YES to keep iterating.
3547 tunnel_add_skip (void *cls,
3548 const struct GNUNET_HashCode * key,
3551 struct GNUNET_PeerIdentity *neighbor = cls;
3552 struct MeshTunnelChildInfo *cinfo = value;
3554 /* TODO compare only pointers? key == neighbor? */
3555 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3565 * @brief Get neighbor's Flow Control information.
3567 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3568 * descendant of the local node in a certain tunnel.
3569 * If the info is not yet there (recently created path), creates the data struct
3570 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3573 * @param t Tunnel related.
3574 * @param peer Neighbor whose Flow Control info is needed.
3576 * @return Neighbor's Flow Control info.
3578 static struct MeshTunnelChildInfo *
3579 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3580 const struct GNUNET_PeerIdentity *peer)
3582 struct MeshTunnelChildInfo *cinfo;
3584 if (NULL == t->children_fc)
3587 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3593 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3594 cinfo->id = GNUNET_PEER_intern (peer);
3595 cinfo->skip = t->fwd_pid;
3598 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3599 cinfo->fwd_ack = t->fwd_pid + delta;
3600 cinfo->bck_ack = delta;
3601 cinfo->bck_pid = -1;
3603 cinfo->send_buffer =
3604 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3606 GNUNET_assert (GNUNET_OK ==
3607 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3610 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3617 * Get the Flow Control info of a client.
3619 * @param t Tunnel on which to look.
3620 * @param c Client whose ACK to get.
3622 * @return ACK value.
3624 static struct MeshTunnelClientInfo *
3625 tunnel_get_client_fc (struct MeshTunnel *t,
3626 struct MeshClient *c)
3630 for (i = 0; i < t->nclients; i++)
3632 if (t->clients[i] != c)
3634 return &t->clients_fc[i];
3637 return NULL; // avoid compiler / coverity complaints
3642 * Iterator to get the appropiate ACK value from all children nodes.
3644 * @param cls Closue (tunnel).
3645 * @param id Id of the child node.
3648 tunnel_get_child_fwd_ack (void *cls,
3651 struct GNUNET_PeerIdentity peer_id;
3652 struct MeshTunnelChildInfo *cinfo;
3653 struct MeshTunnelChildIteratorContext *ctx = cls;
3654 struct MeshTunnel *t = ctx->t;
3657 GNUNET_PEER_resolve (id, &peer_id);
3658 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3659 ack = cinfo->fwd_ack;
3662 if (GNUNET_NO == ctx->init)
3664 ctx->max_child_ack = ack;
3665 ctx->init = GNUNET_YES;
3668 if (GNUNET_YES == t->speed_min)
3670 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3674 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3681 * Get the maximum PID allowed to transmit to any
3682 * tunnel child of the local peer, depending on the tunnel
3683 * buffering/speed settings.
3687 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3690 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3692 struct MeshTunnelChildIteratorContext ctx;
3694 ctx.max_child_ack = 0;
3696 ctx.init = GNUNET_NO;
3697 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3699 if (0 == ctx.nchildren)
3701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3702 " tunnel has no children, no FWD ACK\n");
3706 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3707 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3709 return (int64_t) ctx.max_child_ack;
3714 * Set the FWD ACK value of a client in a particular tunnel.
3716 * @param t Tunnel affected.
3717 * @param c Client whose ACK to set.
3718 * @param ack ACK value.
3721 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3722 struct MeshClient *c,
3727 for (i = 0; i < t->nclients; i++)
3729 if (t->clients[i] != c)
3731 t->clients_fc[i].fwd_ack = ack;
3739 * Get the highest ACK value of all clients in a particular tunnel,
3740 * according to the buffering/speed settings.
3742 * @param t Tunnel on which to look.
3744 * @return Corresponding ACK value (max uint32_t).
3745 * If no clients are suscribed, -1LL.
3748 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3753 if (0 == t->nclients)
3755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3756 " tunnel has no clients, no FWD ACK\n");
3760 for (ack = -1LL, i = 0; i < t->nclients; i++)
3763 (GNUNET_YES == t->speed_min &&
3764 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3765 (GNUNET_NO == t->speed_min &&
3766 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3768 ack = t->clients_fc[i].fwd_ack;
3772 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3773 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3775 return (uint32_t) ack;
3780 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3781 * mode and the status of all children nodes.
3785 * @return Maximum PID allowed.
3788 tunnel_get_fwd_ack (struct MeshTunnel *t)
3792 uint32_t buffer_free;
3796 count = t->fwd_pid - t->skip;
3797 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3798 child_ack = tunnel_get_children_fwd_ack (t);
3799 client_ack = tunnel_get_clients_fwd_ack (t);
3800 if (GNUNET_YES == t->nobuffer)
3803 if (-1LL == child_ack)
3804 child_ack = client_ack;
3805 if (-1LL == child_ack)
3808 client_ack = child_ack = ack;
3813 ack = count + buffer_free; // Overflow? OK!
3815 if (-1LL == child_ack)
3817 // Node has no children, child_ack AND core buffer are irrelevant.
3818 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3819 return (uint32_t) client_ack;
3821 if (-1LL == client_ack)
3825 if (GNUNET_YES == t->speed_min)
3827 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3828 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3832 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3833 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3836 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3837 count, buffer_free, child_ack, client_ack, ack);
3843 * Build a local ACK message and send it to a local client.
3845 * @param t Tunnel on which to send the ACK.
3846 * @param c Client to whom send the ACK.
3847 * @param ack Value of the ACK.
3850 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3852 struct GNUNET_MESH_LocalAck msg;
3854 msg.header.size = htons (sizeof (msg));
3855 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3856 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3857 msg.max_pid = htonl (ack);
3858 GNUNET_SERVER_notification_context_unicast(nc,
3865 * Build an ACK message and queue it to send to the given peer.
3867 * @param t Tunnel on which to send the ACK.
3868 * @param peer Peer to whom send the ACK.
3869 * @param ack Value of the ACK.
3872 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3874 struct GNUNET_MESH_ACK msg;
3876 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3877 msg.header.size = htons (sizeof (msg));
3878 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3879 msg.pid = htonl (ack);
3880 msg.tid = htonl (t->id.tid);
3882 send_prebuilt_message (&msg.header, peer, t);
3887 * Notify a the owner of a tunnel about how many more
3888 * payload packages will we accept on a given tunnel.
3890 * @param t Tunnel on which to send the ACK.
3893 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 "Sending client FWD ACK on tunnel %X\n",
3901 ack = tunnel_get_fwd_ack (t);
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3904 if (t->last_fwd_ack == ack)
3906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3911 t->last_fwd_ack = ack;
3912 send_local_ack (t, t->owner, ack);
3917 * Send an ACK informing the predecessor about the available buffer space.
3918 * In case there is no predecessor, inform the owning client.
3919 * If buffering is off, send only on behalf of children or self if endpoint.
3920 * If buffering is on, send when sent to children and buffer space is free.
3921 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3922 * the ACK itself goes "back" (towards root).
3924 * @param t Tunnel on which to send the ACK.
3925 * @param type Type of message that triggered the ACK transmission.
3928 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3930 struct GNUNET_PeerIdentity id;
3933 if (NULL != t->owner)
3935 tunnel_send_client_fwd_ack (t);
3938 /* Is it after unicast / multicast retransmission? */
3941 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3942 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3944 "ACK due to FWD DATA retransmission\n");
3945 if (GNUNET_YES == t->nobuffer)
3947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3951 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3952 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3958 /* Check if we need no retransmit the ACK */
3959 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3960 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3964 " t->qmax: %u, t->qn: %u\n",
3965 t->fwd_queue_max, t->fwd_queue_n);
3966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3967 " t->pid: %u, t->ack: %u\n",
3968 t->fwd_pid, t->last_fwd_ack);
3972 /* Ok, ACK might be necessary, what PID to ACK? */
3973 ack = tunnel_get_fwd_ack (t);
3975 /* If speed_min and not all children have ack'd, dont send yet */
3976 if (ack == t->last_fwd_ack)
3978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3982 t->last_fwd_ack = ack;
3983 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3984 send_ack (t, &id, ack);
3990 * Iterator to send a child node a BCK ACK to allow him to send more
3993 * @param cls Closure (tunnel).
3994 * @param id Id of the child node.
3997 tunnel_send_child_bck_ack (void *cls,
4000 struct MeshTunnel *t = cls;
4001 struct MeshTunnelChildInfo *cinfo;
4002 struct GNUNET_PeerIdentity peer;
4004 GNUNET_PEER_resolve (id, &peer);
4005 cinfo = tunnel_get_neighbor_fc (t, &peer);
4007 if (cinfo->bck_ack != cinfo->bck_pid &&
4008 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->bck_pid))
4010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4011 " Not sending ACK, not needed\n");
4015 cinfo->bck_ack = t->bck_queue_max - t->bck_queue_n + cinfo->bck_pid;
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4017 " Sending BCK ACK %u\n",
4019 send_ack (t, &peer, cinfo->bck_ack);
4024 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4026 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4028 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4029 * - implement a size 1 buffer in each client_fc AND children_fc
4030 * to hold at least 1 message per "child".
4031 * problem: violates no buffer policy
4032 * - ack 0 and make "children" poll for transmission slots
4033 * problem: big overhead, extra latency even in low traffic
4036 * @param t Tunnel on which to send the BCK ACKs.
4039 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4042 unsigned int tunnel_delta;
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4046 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4049 /* Find client whom to allow to send to origin (with lowest buffer space) */
4050 for (i = 0; i < t->nclients; i++)
4052 struct MeshTunnelClientInfo *clinfo;
4055 clinfo = &t->clients_fc[i];
4056 delta = clinfo->bck_ack - clinfo->bck_pid;
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4058 t->clients[i]->id, delta);
4060 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4061 (GNUNET_YES == t->nobuffer && 0 == delta))
4065 ack = clinfo->bck_pid;
4066 ack += t->nobuffer ? 1 : tunnel_delta;
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4068 " sending ack to client %u: %u\n",
4069 t->clients[i]->id, ack);
4070 send_local_ack (t, t->clients[i], ack);
4071 clinfo->bck_ack = ack;
4075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4076 " not sending ack to client %u (td %u, d %u)\n",
4077 t->clients[i]->id, tunnel_delta, delta);
4084 * Send an ACK informing the children nodes and destination clients about
4085 * the available buffer space.
4086 * If buffering is off, send only on behalf of root (can be self).
4087 * If buffering is on, send when sent to predecessor and buffer space is free.
4088 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4089 * the ACK itself goes "forward" (towards children/clients).
4091 * @param t Tunnel on which to send the ACK.
4092 * @param type Type of message that triggered the ACK transmission.
4095 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4098 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4099 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4100 /* Is it after data to_origin retransmission? */
4103 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4104 if (GNUNET_YES == t->nobuffer)
4106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4107 " Not sending ACK, nobuffer\n");
4111 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4112 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4113 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4119 tunnel_send_clients_bck_ack (t);
4120 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4125 * @brief Re-initiate traffic to this peer if necessary.
4127 * Check if there is traffic queued towards this peer
4128 * and the core transmit handle is NULL (traffic was stalled).
4129 * If so, call core tmt rdy.
4131 * @param cls Closure (unused)
4132 * @param peer_id Short ID of peer to which initiate traffic.
4135 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4137 struct MeshPeerInfo *peer;
4138 struct GNUNET_PeerIdentity id;
4139 struct MeshPeerQueue *q;
4142 peer = peer_info_get_short(peer_id);
4143 if (NULL != peer->core_transmit)
4146 q = queue_get_next(peer);
4149 /* Might br multicast traffic already sent to this particular peer but
4150 * not to other children in this tunnel.
4151 * This way t->queue_n would be > 0 but the queue of this particular peer
4157 GNUNET_PEER_resolve (peer->id, &id);
4158 peer->core_transmit =
4159 GNUNET_CORE_notify_transmit_ready(core_handle,
4162 GNUNET_TIME_UNIT_FOREVER_REL,
4172 * @brief Allow transmission of FWD traffic on this tunnel
4174 * Check if there is traffic queued towards any children
4175 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4177 * @param t Tunnel on which to unlock FWD traffic.
4180 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4182 if (0 == t->fwd_queue_n)
4185 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4190 * @brief Allow transmission of BCK traffic on this tunnel
4192 * Check if there is traffic queued towards the root of the tree
4193 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4195 * @param t Tunnel on which to unlock BCK traffic.
4198 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4200 if (0 == t->bck_queue_n)
4203 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4208 * Send a message to all peers in this tunnel that the tunnel is no longer
4211 * @param t The tunnel whose peers to notify.
4214 tunnel_send_destroy (struct MeshTunnel *t)
4216 struct GNUNET_MESH_TunnelDestroy msg;
4218 msg.header.size = htons (sizeof (msg));
4219 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4220 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4221 msg.tid = htonl (t->id.tid);
4222 tunnel_send_multicast (t, &msg.header);
4227 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4229 * @param cls Closure (Tunnel which to cancel).
4230 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4233 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4235 struct MeshTunnel *t = cls;
4236 struct MeshPeerInfo *peer_info;
4237 struct MeshPeerQueue *pq;
4238 struct MeshPeerQueue *next;
4240 peer_info = peer_info_get_short (neighbor_id);
4241 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4244 if (pq->tunnel == t)
4246 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4247 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4248 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4250 // Should have been removed on destroy children
4253 queue_destroy (pq, GNUNET_YES);
4256 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4258 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4259 peer_info->core_transmit = NULL;
4264 * Destroy the tunnel and free any allocated resources linked to it.
4266 * @param t the tunnel to destroy
4268 * @return GNUNET_OK on success
4271 tunnel_destroy (struct MeshTunnel *t)
4273 struct MeshClient *c;
4274 struct GNUNET_HashCode hash;
4285 struct GNUNET_PeerIdentity id;
4287 GNUNET_PEER_resolve (t->id.oid, &id);
4288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4289 GNUNET_i2s (&id), t->id.tid);
4291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4295 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4296 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4304 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4306 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4313 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4314 for (i = 0; i < t->nclients; i++)
4318 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4324 for (i = 0; i < t->nignore; i++)
4328 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4335 if (t->nclients > 0)
4338 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4343 GNUNET_free (t->clients);
4344 GNUNET_free (t->clients_fc);
4347 if (NULL != t->peers)
4349 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4351 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4354 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4355 &tunnel_destroy_child,
4357 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4358 t->children_fc = NULL;
4360 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4361 tree_destroy (t->tree);
4363 if (NULL != t->regex_ctx)
4364 regex_cancel_search (t->regex_ctx);
4365 if (NULL != t->dht_get_type)
4366 GNUNET_DHT_get_stop (t->dht_get_type);
4367 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4368 GNUNET_SCHEDULER_cancel (t->timeout_task);
4369 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4370 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4373 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4380 * Create a new tunnel
4382 * @param owner Who is the owner of the tunnel (short ID).
4383 * @param tid Tunnel Number of the tunnel.
4384 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4385 * @param local Tunnel Number for the tunnel, for the client point of view.
4387 * @return A new initialized tunnel. NULL on error.
4389 static struct MeshTunnel *
4390 tunnel_new (GNUNET_PEER_Id owner,
4391 MESH_TunnelNumber tid,
4392 struct MeshClient *client,
4393 MESH_TunnelNumber local)
4395 struct MeshTunnel *t;
4396 struct GNUNET_HashCode hash;
4398 if (n_tunnels >= max_tunnels && NULL == client)
4401 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4404 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4405 t->bck_queue_max = t->fwd_queue_max;
4406 t->tree = tree_new (owner);
4408 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4409 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4410 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4411 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4412 t->local_tid = local;
4413 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4415 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4417 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4419 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4420 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4427 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4434 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4436 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4441 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4451 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4452 * that are no longer needed, as well as nodes of no longer reachable peers.
4453 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4455 * @param t Tunnel from which to remove the path.
4456 * @param peer Short id of the peer which should be removed.
4459 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4461 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4467 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4468 * client when the client disconnects. If the client is not the owner, the
4469 * owner will get notified if no more clients are in the tunnel and the client
4470 * get removed from the tunnel's list.
4472 * @param cls closure (client that is disconnecting)
4473 * @param key the hash of the local tunnel id (used to access the hashmap)
4474 * @param value the value stored at the key (tunnel to destroy)
4476 * @return GNUNET_OK, keep iterating.
4479 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4481 struct MeshTunnel *t = value;
4482 struct MeshClient *c = cls;
4484 send_client_tunnel_disconnect(t, c);
4487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4488 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4489 tunnel_delete_client(t, c);
4490 client_delete_tunnel(c, t);
4493 tunnel_send_destroy(t);
4495 t->destroy = GNUNET_YES;
4502 * Timeout function, destroys tunnel if called
4504 * @param cls Closure (tunnel to destroy).
4505 * @param tc TaskContext
4508 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4510 struct MeshTunnel *t = cls;
4511 struct GNUNET_PeerIdentity id;
4513 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4514 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4516 GNUNET_PEER_resolve(t->id.oid, &id);
4517 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4518 "Tunnel %s [%X] timed out. Destroying.\n",
4519 GNUNET_i2s(&id), t->id.tid);
4520 send_clients_tunnel_destroy (t);
4525 * Resets the tunnel timeout. Starts it if no timeout was running.
4527 * @param t Tunnel whose timeout to reset.
4529 * TODO use heap to improve efficiency of scheduler.
4532 tunnel_reset_timeout (struct MeshTunnel *t)
4534 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4535 GNUNET_SCHEDULER_cancel (t->timeout_task);
4537 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4538 (refresh_path_time, 4), &tunnel_timeout, t);
4542 /******************************************************************************/
4543 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4544 /******************************************************************************/
4547 * Function to send a create path packet to a peer.
4549 * @param cls closure
4550 * @param size number of bytes available in buf
4551 * @param buf where the callee should write the message
4552 * @return number of bytes written to buf
4555 send_core_path_create (void *cls, size_t size, void *buf)
4557 struct MeshPathInfo *info = cls;
4558 struct GNUNET_MESH_ManipulatePath *msg;
4559 struct GNUNET_PeerIdentity *peer_ptr;
4560 struct MeshTunnel *t = info->t;
4561 struct MeshPeerPath *p = info->path;
4566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4568 sizeof (struct GNUNET_MESH_ManipulatePath) +
4569 p->length * sizeof (struct GNUNET_PeerIdentity);
4571 if (size < size_needed || NULL == buf)
4576 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4577 msg->header.size = htons (size_needed);
4578 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4579 msg->tid = ntohl (t->id.tid);
4582 if (GNUNET_YES == t->speed_min)
4583 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4584 if (GNUNET_YES == t->nobuffer)
4585 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4586 msg->opt = htonl(opt);
4589 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4590 for (i = 0; i < p->length; i++)
4592 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4599 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4605 * Fill the core buffer
4607 * @param cls closure (data itself)
4608 * @param size number of bytes available in buf
4609 * @param buf where the callee should write the message
4611 * @return number of bytes written to buf
4614 send_core_data_multicast (void *cls, size_t size, void *buf)
4616 struct MeshTransmissionDescriptor *info = cls;
4619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4620 GNUNET_assert (NULL != info);
4621 GNUNET_assert (NULL != info->peer);
4622 total_size = info->mesh_data->data_len;
4623 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4625 if (total_size > size)
4630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4631 memcpy (buf, info->mesh_data->data, total_size);
4634 struct GNUNET_MESH_Multicast *mc;
4635 struct GNUNET_MessageHeader *mh;
4638 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4640 mc = (struct GNUNET_MESH_Multicast *) mh;
4641 mh = (struct GNUNET_MessageHeader *) &mc[1];
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 " multicast, payload type %s\n",
4644 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4646 " multicast, payload size %u\n", ntohs (mh->size));
4650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4651 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4655 data_descriptor_decrement_rc (info->mesh_data);
4656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4664 * Creates a path ack message in buf and frees all unused resources.
4666 * @param cls closure (MeshTransmissionDescriptor)
4667 * @param size number of bytes available in buf
4668 * @param buf where the callee should write the message
4669 * @return number of bytes written to buf
4672 send_core_path_ack (void *cls, size_t size, void *buf)
4674 struct MeshTransmissionDescriptor *info = cls;
4675 struct GNUNET_MESH_PathACK *msg = buf;
4677 GNUNET_assert (NULL != info);
4678 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4683 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4684 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4685 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4686 msg->tid = htonl (info->origin->tid);
4687 msg->peer_id = my_full_id;
4690 /* TODO add signature */
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4693 return sizeof (struct GNUNET_MESH_PathACK);
4698 * Free a transmission that was already queued with all resources
4699 * associated to the request.
4701 * @param queue Queue handler to cancel.
4702 * @param clear_cls Is it necessary to free associated cls?
4705 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4707 struct MeshTransmissionDescriptor *dd;
4708 struct MeshPathInfo *path_info;
4709 struct MeshTunnelChildInfo *cinfo;
4710 struct GNUNET_PeerIdentity id;
4714 if (GNUNET_YES == clear_cls)
4716 switch (queue->type)
4718 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4720 GNUNET_assert (GNUNET_YES == queue->tunnel->destroy);
4721 /* FIXME: don't cancel, send and destroy tunnel in queue_send */
4723 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4724 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4725 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4726 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4727 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4729 " prebuilt message\n");
4730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4732 GNUNET_MESH_DEBUG_M2S(queue->type));
4734 data_descriptor_decrement_rc (dd->mesh_data);
4736 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4738 path_info = queue->cls;
4739 path_destroy (path_info->path);
4743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4744 " type %s unknown!\n",
4745 GNUNET_MESH_DEBUG_M2S(queue->type));
4747 GNUNET_free_non_null (queue->cls);
4749 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4750 queue->peer->queue_tail,
4753 /* Delete from child_fc in the appropiate tunnel */
4754 max = queue->tunnel->fwd_queue_max;
4755 GNUNET_PEER_resolve (queue->peer->id, &id);
4756 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4759 for (i = 0; i < cinfo->send_buffer_n; i++)
4762 i2 = (cinfo->send_buffer_start + i) % max;
4763 if (cinfo->send_buffer[i2] == queue)
4765 /* Found corresponding entry in the send_buffer. Move all others back. */
4770 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4772 j2 = (cinfo->send_buffer_start + j) % max;
4773 j3 = (cinfo->send_buffer_start + j + 1) % max;
4774 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4777 cinfo->send_buffer[j3] = NULL;
4778 cinfo->send_buffer_n--;
4783 GNUNET_free (queue);
4788 * @brief Get the next transmittable message from the queue.
4790 * This will be the head, except in the case of being a data packet
4791 * not allowed by the destination peer.
4793 * @param peer Destination peer.
4795 * @return The next viable MeshPeerQueue element to send to that peer.
4796 * NULL when there are no transmittable messages.
4798 struct MeshPeerQueue *
4799 queue_get_next (const struct MeshPeerInfo *peer)
4801 struct MeshPeerQueue *q;
4802 struct MeshTunnel *t;
4803 struct MeshTransmissionDescriptor *info;
4804 struct MeshTunnelChildInfo *cinfo;
4805 struct GNUNET_MESH_Unicast *ucast;
4806 struct GNUNET_MESH_ToOrigin *to_orig;
4807 struct GNUNET_MESH_Multicast *mcast;
4808 struct GNUNET_PeerIdentity id;
4812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4813 for (q = peer->queue_head; NULL != q; q = q->next)
4817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4819 GNUNET_MESH_DEBUG_M2S(q->type));
4822 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4823 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4824 pid = ntohl (ucast->pid);
4825 GNUNET_PEER_resolve (info->peer->id, &id);
4826 cinfo = tunnel_get_neighbor_fc(t, &id);
4827 ack = cinfo->fwd_ack;
4829 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4830 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4831 pid = ntohl (to_orig->pid);
4834 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4835 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4836 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4838 // Not a multicast payload: multicast control traffic (destroy, etc)
4841 pid = ntohl (mcast->pid);
4842 GNUNET_PEER_resolve (info->peer->id, &id);
4843 cinfo = tunnel_get_neighbor_fc(t, &id);
4844 ack = cinfo->fwd_ack;
4847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4852 "********* ACK: %u, PID: %u\n",
4854 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4863 "********* NEXT!\n");
4866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4867 "********* nothing found\n");
4873 * Core callback to write a queued packet to core buffer
4875 * @param cls Closure (peer info).
4876 * @param size Number of bytes available in buf.
4877 * @param buf Where the to write the message.
4879 * @return number of bytes written to buf
4882 queue_send (void *cls, size_t size, void *buf)
4884 struct MeshPeerInfo *peer = cls;
4885 struct GNUNET_MessageHeader *msg;
4886 struct MeshPeerQueue *queue;
4887 struct MeshTunnel *t;
4888 struct MeshTunnelChildInfo *cinfo;
4889 struct GNUNET_PeerIdentity dst_id;
4892 peer->core_transmit = NULL;
4895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4896 queue = queue_get_next (peer);
4898 /* Queue has no internal mesh traffic nor sendable payload */
4901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4902 if (NULL == peer->queue_head)
4903 GNUNET_break (0); // Should've been canceled
4906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4908 GNUNET_PEER_resolve (peer->id, &dst_id);
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4910 "********* towards %s\n",
4911 GNUNET_i2s(&dst_id));
4912 /* Check if buffer size is enough for the message */
4913 if (queue->size > size)
4915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4916 "********* not enough room, reissue\n");
4917 peer->core_transmit =
4918 GNUNET_CORE_notify_transmit_ready (core_handle,
4921 GNUNET_TIME_UNIT_FOREVER_REL,
4928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4931 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4935 "********* unicast: t->q (%u/%u)\n",
4936 t->fwd_queue_n, t->fwd_queue_max);
4938 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4945 switch (queue->type)
4948 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4949 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4950 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4951 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4953 "********* raw: %s\n",
4954 GNUNET_MESH_DEBUG_M2S (queue->type));
4956 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4957 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4958 data_size = send_core_data_raw (queue->cls, size, buf);
4959 msg = (struct GNUNET_MessageHeader *) buf;
4960 switch (ntohs (msg->type)) // Type of preconstructed message
4962 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4963 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4965 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4966 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4972 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4975 struct MeshTransmissionDescriptor *info = queue->cls;
4977 if ((1 == info->mesh_data->reference_counter
4978 && GNUNET_YES == t->speed_min)
4980 (info->mesh_data->total_out == info->mesh_data->reference_counter
4981 && GNUNET_NO == t->speed_min))
4983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4984 "********* considered sent\n");
4988 data_size = send_core_data_multicast(queue->cls, size, buf);
4989 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4991 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4993 data_size = send_core_path_create (queue->cls, size, buf);
4995 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4997 data_size = send_core_path_ack (queue->cls, size, buf);
4999 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5001 data_size = send_core_data_multicast (queue->cls, size, buf);
5005 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5006 "********* type unknown: %u\n",
5010 switch (queue->type)
5012 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5013 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5014 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5015 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5016 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5019 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5020 "at pos %u (%p) != %p\n",
5021 cinfo->send_buffer_start,
5022 cinfo->send_buffer[cinfo->send_buffer_start],
5025 if (cinfo->send_buffer_n > 0)
5027 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5028 cinfo->send_buffer_n--;
5029 cinfo->send_buffer_start++;
5030 cinfo->send_buffer_start %= t->fwd_queue_max;
5041 /* Free queue, but cls was freed by send_core_* */
5042 queue_destroy (queue, GNUNET_NO);
5044 if (GNUNET_YES == t->destroy)
5046 // FIXME fc tunnel destroy all pending traffic? wait for it?
5047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5051 /* If more data in queue, send next */
5052 queue = queue_get_next(peer);
5055 struct GNUNET_PeerIdentity id;
5057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5058 GNUNET_PEER_resolve (peer->id, &id);
5059 peer->core_transmit =
5060 GNUNET_CORE_notify_transmit_ready(core_handle,
5063 GNUNET_TIME_UNIT_FOREVER_REL,
5071 if (NULL != peer->queue_head)
5073 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5074 "********* %s stalled\n",
5075 GNUNET_i2s(&my_full_id));
5077 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5078 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5079 &tunnel_poll, cinfo);
5082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5088 * @brief Queue and pass message to core when possible.
5090 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5091 * and accounts for it. In case the queue is full, the message is dropped and
5094 * Otherwise, message is treated as internal and allowed to go regardless of
5097 * @param cls Closure (@c type dependant). It will be used by queue_send to
5098 * build the message to be sent if not already prebuilt.
5099 * @param type Type of the message, 0 for a raw message.
5100 * @param size Size of the message.
5101 * @param dst Neighbor to send message to.
5102 * @param t Tunnel this message belongs to.
5105 queue_add (void *cls, uint16_t type, size_t size,
5106 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5108 struct MeshPeerQueue *queue;
5109 struct MeshTunnelChildInfo *cinfo;
5110 struct GNUNET_PeerIdentity id;
5116 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5117 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5119 n = &t->fwd_queue_n;
5120 max = &t->fwd_queue_max;
5122 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5124 n = &t->bck_queue_n;
5125 max = &t->bck_queue_max;
5131 struct MeshTransmissionDescriptor *td = cls;
5132 struct GNUNET_MESH_ToOrigin *to;
5134 to = td->mesh_data->data;
5136 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5137 "bck pid %u, bck ack %u, msg pid %u\n",
5138 t->bck_pid, t->bck_ack, ntohl(to->pid));
5139 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5141 return; // Drop message
5145 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5151 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5152 GNUNET_PEER_resolve (dst->id, &id);
5153 if (NULL == dst->core_transmit)
5155 dst->core_transmit =
5156 GNUNET_CORE_notify_transmit_ready (core_handle,
5159 GNUNET_TIME_UNIT_FOREVER_REL,
5165 if (NULL == n) // Is this internal mesh traffic?
5168 // It's payload, keep track of buffer per peer.
5169 cinfo = tunnel_get_neighbor_fc(t, &id);
5170 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5171 if (NULL != cinfo->send_buffer[i])
5173 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5174 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5175 cinfo->send_buffer_start++;
5176 cinfo->send_buffer_start %= t->fwd_queue_max;
5180 cinfo->send_buffer_n++;
5182 cinfo->send_buffer[i] = queue;
5183 if (cinfo->send_buffer_n > t->fwd_queue_max)
5186 cinfo->send_buffer_n = t->fwd_queue_max;
5191 /******************************************************************************/
5192 /******************** MESH NETWORK HANDLERS **************************/
5193 /******************************************************************************/
5197 * Core handler for path creation
5199 * @param cls closure
5200 * @param message message
5201 * @param peer peer identity this notification is about
5202 * @param atsi performance data
5203 * @param atsi_count number of records in 'atsi'
5205 * @return GNUNET_OK to keep the connection open,
5206 * GNUNET_SYSERR to close it (signal serious error)
5209 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5210 const struct GNUNET_MessageHeader *message,
5211 const struct GNUNET_ATS_Information *atsi,
5212 unsigned int atsi_count)
5214 unsigned int own_pos;
5217 MESH_TunnelNumber tid;
5218 struct GNUNET_MESH_ManipulatePath *msg;
5219 struct GNUNET_PeerIdentity *pi;
5220 struct GNUNET_HashCode hash;
5221 struct MeshPeerPath *path;
5222 struct MeshPeerInfo *dest_peer_info;
5223 struct MeshPeerInfo *orig_peer_info;
5224 struct MeshTunnel *t;
5226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5227 "Received a path create msg [%s]\n",
5228 GNUNET_i2s (&my_full_id));
5229 size = ntohs (message->size);
5230 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5232 GNUNET_break_op (0);
5236 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5237 if (size % sizeof (struct GNUNET_PeerIdentity))
5239 GNUNET_break_op (0);
5242 size /= sizeof (struct GNUNET_PeerIdentity);
5245 GNUNET_break_op (0);
5248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5249 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5251 tid = ntohl (msg->tid);
5252 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5254 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5255 t = tunnel_get (pi, tid);
5256 if (NULL == t) // FIXME only for INCOMING tunnels?
5260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5261 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5264 // FIXME notify failure
5267 opt = ntohl (msg->opt);
5268 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5269 GNUNET_YES : GNUNET_NO;
5270 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5272 t->nobuffer = GNUNET_YES;
5273 t->last_fwd_ack = t->fwd_pid + 1;
5275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5276 " speed_min: %d, nobuffer:%d\n",
5277 t->speed_min, t->nobuffer);
5279 if (GNUNET_YES == t->nobuffer)
5281 t->bck_queue_max = 1;
5282 t->fwd_queue_max = 1;
5285 // FIXME only assign a local tid if a local client is interested (on demand)
5286 while (NULL != tunnel_get_incoming (next_local_tid))
5287 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5288 t->local_tid_dest = next_local_tid++;
5289 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5292 tunnel_reset_timeout (t);
5293 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5295 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5296 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5304 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5305 if (NULL == dest_peer_info)
5307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5308 " Creating PeerInfo for destination.\n");
5309 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5310 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5311 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5313 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5315 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5316 if (NULL == orig_peer_info)
5318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5319 " Creating PeerInfo for origin.\n");
5320 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5321 orig_peer_info->id = GNUNET_PEER_intern (pi);
5322 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5323 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5326 path = path_new (size);
5328 for (i = 0; i < size; i++)
5330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5331 GNUNET_i2s (&pi[i]));
5332 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5333 if (path->peers[i] == myid)
5336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5339 /* cannot be self, must be 'not found' */
5340 /* create path: self not found in path through self */
5341 GNUNET_break_op (0);
5342 path_destroy (path);
5346 path_add_to_peers (path, GNUNET_NO);
5347 tunnel_add_path (t, path, own_pos);
5348 if (own_pos == size - 1)
5350 /* It is for us! Send ack. */
5351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5352 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5353 if (NULL == t->peers)
5355 /* New tunnel! Notify clients on first payload message. */
5356 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5358 GNUNET_break (GNUNET_SYSERR !=
5359 GNUNET_CONTAINER_multihashmap_put (t->peers,
5360 &my_full_id.hashPubKey,
5363 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5368 struct MeshPeerPath *path2;
5370 /* It's for somebody else! Retransmit. */
5371 path2 = path_duplicate (path);
5372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5373 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5374 path2 = path_duplicate (path);
5375 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5376 send_create_path (dest_peer_info, path, t);
5383 * Core handler for path destruction
5385 * @param cls closure
5386 * @param message message
5387 * @param peer peer identity this notification is about
5388 * @param atsi performance data
5389 * @param atsi_count number of records in 'atsi'
5391 * @return GNUNET_OK to keep the connection open,
5392 * GNUNET_SYSERR to close it (signal serious error)
5395 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5396 const struct GNUNET_MessageHeader *message,
5397 const struct GNUNET_ATS_Information *atsi,
5398 unsigned int atsi_count)
5400 struct GNUNET_MESH_ManipulatePath *msg;
5401 struct GNUNET_PeerIdentity *pi;
5402 struct MeshPeerPath *path;
5403 struct MeshTunnel *t;
5404 unsigned int own_pos;
5408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5409 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5410 size = ntohs (message->size);
5411 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5413 GNUNET_break_op (0);
5417 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5418 if (size % sizeof (struct GNUNET_PeerIdentity))
5420 GNUNET_break_op (0);
5423 size /= sizeof (struct GNUNET_PeerIdentity);
5426 GNUNET_break_op (0);
5429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5431 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5432 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5434 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5436 t = tunnel_get (pi, ntohl (msg->tid));
5439 /* TODO notify back: we don't know this tunnel */
5440 GNUNET_break_op (0);
5443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5444 path = path_new (size);
5446 for (i = 0; i < size; i++)
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5449 GNUNET_i2s (&pi[i]));
5450 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5451 if (path->peers[i] == myid)
5454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5455 if (own_pos < path->length - 1)
5456 send_prebuilt_message (message, &pi[own_pos + 1], t);
5458 send_client_tunnel_disconnect(t, NULL);
5460 tunnel_delete_peer (t, path->peers[path->length - 1]);
5461 path_destroy (path);
5467 * Core handler for notifications of broken paths
5469 * @param cls closure
5470 * @param message message
5471 * @param peer peer identity this notification is about
5472 * @param atsi performance data
5473 * @param atsi_count number of records in 'atsi'
5475 * @return GNUNET_OK to keep the connection open,
5476 * GNUNET_SYSERR to close it (signal serious error)
5479 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5480 const struct GNUNET_MessageHeader *message,
5481 const struct GNUNET_ATS_Information *atsi,
5482 unsigned int atsi_count)
5484 struct GNUNET_MESH_PathBroken *msg;
5485 struct MeshTunnel *t;
5487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5488 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5489 msg = (struct GNUNET_MESH_PathBroken *) message;
5490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5491 GNUNET_i2s (&msg->peer1));
5492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5493 GNUNET_i2s (&msg->peer2));
5494 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5497 GNUNET_break_op (0);
5500 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5501 GNUNET_PEER_search (&msg->peer2));
5508 * Core handler for tunnel destruction
5510 * @param cls closure
5511 * @param message message
5512 * @param peer peer identity this notification is about
5513 * @param atsi performance data
5514 * @param atsi_count number of records in 'atsi'
5516 * @return GNUNET_OK to keep the connection open,
5517 * GNUNET_SYSERR to close it (signal serious error)
5520 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5521 const struct GNUNET_MessageHeader *message,
5522 const struct GNUNET_ATS_Information *atsi,
5523 unsigned int atsi_count)
5525 struct GNUNET_MESH_TunnelDestroy *msg;
5526 struct MeshTunnel *t;
5528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5529 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5530 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5532 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5533 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5536 /* Probably already got the message from another path,
5537 * destroyed the tunnel and retransmitted to children.
5540 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5543 if (t->id.oid == myid)
5545 GNUNET_break_op (0);
5548 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5550 /* Tunnel was incoming, notify clients */
5551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5552 t->local_tid, t->local_tid_dest);
5553 send_clients_tunnel_destroy (t);
5555 tunnel_send_destroy (t);
5556 t->destroy = GNUNET_YES;
5557 // TODO: add timeout to destroy the tunnel anyway
5563 * Core handler for mesh network traffic going from the origin to a peer
5565 * @param cls closure
5566 * @param peer peer identity this notification is about
5567 * @param message message
5568 * @param atsi performance data
5569 * @param atsi_count number of records in 'atsi'
5570 * @return GNUNET_OK to keep the connection open,
5571 * GNUNET_SYSERR to close it (signal serious error)
5574 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5575 const struct GNUNET_MessageHeader *message,
5576 const struct GNUNET_ATS_Information *atsi,
5577 unsigned int atsi_count)
5579 struct GNUNET_MESH_Unicast *msg;
5580 struct GNUNET_PeerIdentity *neighbor;
5581 struct MeshTunnelChildInfo *cinfo;
5582 struct MeshTunnel *t;
5583 GNUNET_PEER_Id dest_id;
5588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5591 size = ntohs (message->size);
5593 sizeof (struct GNUNET_MESH_Unicast) +
5594 sizeof (struct GNUNET_MessageHeader))
5599 msg = (struct GNUNET_MESH_Unicast *) message;
5600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5601 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5603 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5606 /* TODO notify back: we don't know this tunnel */
5607 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5608 GNUNET_break_op (0);
5611 pid = ntohl (msg->pid);
5612 if (t->fwd_pid == pid)
5614 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5615 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5616 " Already seen pid %u, DROPPING!\n", pid);
5621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5622 " pid %u not seen yet, forwarding\n", pid);
5625 t->skip += (pid - t->fwd_pid) - 1;
5628 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5630 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5631 GNUNET_break_op (0);
5632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5633 "Received PID %u, ACK %u\n",
5634 pid, t->last_fwd_ack);
5638 tunnel_reset_timeout (t);
5639 dest_id = GNUNET_PEER_search (&msg->destination);
5640 if (dest_id == myid)
5642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5643 " it's for us! sending to clients...\n");
5644 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5645 send_subscribed_clients (message, &msg[1].header, t);
5646 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5649 ttl = ntohl (msg->ttl);
5650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5653 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5655 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5659 " not for us, retransmitting...\n");
5661 neighbor = tree_get_first_hop (t->tree, dest_id);
5662 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5663 cinfo->fwd_pid = pid;
5664 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5667 if (GNUNET_YES == t->nobuffer &&
5668 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5670 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5672 GNUNET_break_op (0);
5675 send_prebuilt_message (message, neighbor, t);
5676 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5682 * Core handler for mesh network traffic going from the origin to all peers
5684 * @param cls closure
5685 * @param message message
5686 * @param peer peer identity this notification is about
5687 * @param atsi performance data
5688 * @param atsi_count number of records in 'atsi'
5689 * @return GNUNET_OK to keep the connection open,
5690 * GNUNET_SYSERR to close it (signal serious error)
5692 * TODO: Check who we got this from, to validate route.
5695 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5696 const struct GNUNET_MessageHeader *message,
5697 const struct GNUNET_ATS_Information *atsi,
5698 unsigned int atsi_count)
5700 struct GNUNET_MESH_Multicast *msg;
5701 struct MeshTunnel *t;
5705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5707 size = ntohs (message->size);
5708 if (sizeof (struct GNUNET_MESH_Multicast) +
5709 sizeof (struct GNUNET_MessageHeader) > size)
5711 GNUNET_break_op (0);
5714 msg = (struct GNUNET_MESH_Multicast *) message;
5715 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5719 /* TODO notify that we dont know that tunnel */
5720 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5721 GNUNET_break_op (0);
5724 pid = ntohl (msg->pid);
5725 if (t->fwd_pid == pid)
5727 /* already seen this packet, drop */
5728 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5730 " Already seen pid %u, DROPPING!\n", pid);
5731 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5737 " pid %u not seen yet, forwarding\n", pid);
5739 t->skip += (pid - t->fwd_pid) - 1;
5741 tunnel_reset_timeout (t);
5743 /* Transmit to locally interested clients */
5744 if (NULL != t->peers &&
5745 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5747 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5748 send_subscribed_clients (message, &msg[1].header, t);
5749 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5752 if (ntohl (msg->ttl) == 0)
5754 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5755 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5756 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5759 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5760 tunnel_send_multicast (t, message);
5766 * Core handler for mesh network traffic toward the owner of a tunnel
5768 * @param cls closure
5769 * @param message message
5770 * @param peer peer identity this notification is about
5771 * @param atsi performance data
5772 * @param atsi_count number of records in 'atsi'
5774 * @return GNUNET_OK to keep the connection open,
5775 * GNUNET_SYSERR to close it (signal serious error)
5778 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5779 const struct GNUNET_MessageHeader *message,
5780 const struct GNUNET_ATS_Information *atsi,
5781 unsigned int atsi_count)
5783 struct GNUNET_MESH_ToOrigin *msg;
5784 struct GNUNET_PeerIdentity id;
5785 struct MeshPeerInfo *peer_info;
5786 struct MeshTunnel *t;
5787 struct MeshTunnelChildInfo *cinfo;
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5793 size = ntohs (message->size);
5794 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5795 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5797 GNUNET_break_op (0);
5800 msg = (struct GNUNET_MESH_ToOrigin *) message;
5801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5802 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5803 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5804 pid = ntohl (msg->pid);
5808 /* TODO notify that we dont know this tunnel (whom)? */
5809 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5810 GNUNET_break_op (0);
5811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5812 "Received to_origin with PID %u on unknown tunnel\n",
5817 cinfo = tunnel_get_neighbor_fc(t, peer);
5824 if (cinfo->bck_pid == pid)
5826 /* already seen this packet, drop */
5827 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5829 " Already seen pid %u, DROPPING!\n", pid);
5830 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5835 " pid %u not seen yet, forwarding\n", pid);
5836 cinfo->bck_pid = pid;
5838 if (NULL != t->owner)
5841 struct GNUNET_MESH_ToOrigin *copy;
5843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5844 " it's for us! sending to clients...\n");
5845 /* TODO signature verification */
5846 memcpy (cbuf, message, size);
5847 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5848 copy->tid = htonl (t->local_tid);
5850 copy->pid = htonl (t->bck_pid);
5851 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5852 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5853 ©->header, GNUNET_NO);
5854 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5858 " not for us, retransmitting...\n");
5860 peer_info = peer_info_get (&msg->oid);
5861 if (NULL == peer_info)
5863 /* unknown origin of tunnel */
5867 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5868 send_prebuilt_message (message, &id, t);
5869 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5876 * Core handler for mesh network traffic point-to-point acks.
5878 * @param cls closure
5879 * @param message message
5880 * @param peer peer identity this notification is about
5881 * @param atsi performance data
5882 * @param atsi_count number of records in 'atsi'
5884 * @return GNUNET_OK to keep the connection open,
5885 * GNUNET_SYSERR to close it (signal serious error)
5888 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5889 const struct GNUNET_MessageHeader *message,
5890 const struct GNUNET_ATS_Information *atsi,
5891 unsigned int atsi_count)
5893 struct GNUNET_MESH_ACK *msg;
5894 struct MeshTunnel *t;
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5899 msg = (struct GNUNET_MESH_ACK *) message;
5901 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5905 /* TODO notify that we dont know this tunnel (whom)? */
5906 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5907 GNUNET_break_op (0);
5910 ack = ntohl (msg->pid);
5911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5913 /* Is this a forward or backward ACK? */
5914 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5916 struct MeshTunnelChildInfo *cinfo;
5919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5920 cinfo = tunnel_get_neighbor_fc (t, peer);
5921 cinfo->fwd_ack = ack;
5922 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5923 tunnel_unlock_fwd_queues (t);
5927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5929 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5930 tunnel_unlock_bck_queue (t);
5937 * Core handler for mesh network traffic point-to-point ack polls.
5939 * @param cls closure
5940 * @param message message
5941 * @param peer peer identity this notification is about
5942 * @param atsi performance data
5943 * @param atsi_count number of records in 'atsi'
5945 * @return GNUNET_OK to keep the connection open,
5946 * GNUNET_SYSERR to close it (signal serious error)
5949 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5950 const struct GNUNET_MessageHeader *message,
5951 const struct GNUNET_ATS_Information *atsi,
5952 unsigned int atsi_count)
5954 struct GNUNET_MESH_Poll *msg;
5955 struct MeshTunnel *t;
5957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5960 msg = (struct GNUNET_MESH_Poll *) message;
5962 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5966 /* TODO notify that we dont know this tunnel (whom)? */
5967 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5968 GNUNET_break_op (0);
5972 /* Is this a forward or backward ACK? */
5973 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5975 struct MeshTunnelChildInfo *cinfo;
5977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5978 cinfo = tunnel_get_neighbor_fc (t, peer);
5979 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5980 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5985 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5993 * Core handler for path ACKs
5995 * @param cls closure
5996 * @param message message
5997 * @param peer peer identity this notification is about
5998 * @param atsi performance data
5999 * @param atsi_count number of records in 'atsi'
6001 * @return GNUNET_OK to keep the connection open,
6002 * GNUNET_SYSERR to close it (signal serious error)
6005 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6006 const struct GNUNET_MessageHeader *message,
6007 const struct GNUNET_ATS_Information *atsi,
6008 unsigned int atsi_count)
6010 struct GNUNET_MESH_PathACK *msg;
6011 struct GNUNET_PeerIdentity id;
6012 struct MeshPeerInfo *peer_info;
6013 struct MeshPeerPath *p;
6014 struct MeshTunnel *t;
6016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6017 GNUNET_i2s (&my_full_id));
6018 msg = (struct GNUNET_MESH_PathACK *) message;
6019 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6022 /* TODO notify that we don't know the tunnel */
6023 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6025 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6029 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6031 peer_info = peer_info_get (&msg->peer_id);
6032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6033 GNUNET_i2s (&msg->peer_id));
6034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6037 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6040 "connect_by_string completed, stopping search\n");
6041 regex_cancel_search (t->regex_ctx);
6042 t->regex_ctx = NULL;
6045 /* Add paths to peers? */
6046 p = tree_get_path_to_peer (t->tree, peer_info->id);
6049 path_add_to_peers (p, GNUNET_YES);
6057 /* Message for us? */
6058 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6061 if (NULL == t->owner)
6063 GNUNET_break_op (0);
6066 if (NULL != t->dht_get_type)
6068 GNUNET_DHT_get_stop (t->dht_get_type);
6069 t->dht_get_type = NULL;
6071 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6073 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6074 send_client_peer_connected (t, peer_info->id);
6079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6080 " not for us, retransmitting...\n");
6081 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6082 peer_info = peer_info_get (&msg->oid);
6083 if (NULL == peer_info)
6085 /* If we know the tunnel, we should DEFINITELY know the peer */
6089 send_prebuilt_message (message, &id, t);
6095 * Core handler for mesh keepalives.
6097 * @param cls closure
6098 * @param message message
6099 * @param peer peer identity this notification is about
6100 * @param atsi performance data
6101 * @param atsi_count number of records in 'atsi'
6102 * @return GNUNET_OK to keep the connection open,
6103 * GNUNET_SYSERR to close it (signal serious error)
6105 * TODO: Check who we got this from, to validate route.
6108 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6109 const struct GNUNET_MessageHeader *message,
6110 const struct GNUNET_ATS_Information *atsi,
6111 unsigned int atsi_count)
6113 struct GNUNET_MESH_TunnelKeepAlive *msg;
6114 struct MeshTunnel *t;
6116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6119 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6120 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6124 /* TODO notify that we dont know that tunnel */
6125 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6126 GNUNET_break_op (0);
6130 tunnel_reset_timeout (t);
6132 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6133 tunnel_send_multicast (t, message);
6140 * Functions to handle messages from core
6142 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6143 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6144 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6145 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6146 sizeof (struct GNUNET_MESH_PathBroken)},
6147 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6148 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6149 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6150 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6151 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6152 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6153 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6154 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6155 sizeof (struct GNUNET_MESH_ACK)},
6156 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6157 sizeof (struct GNUNET_MESH_Poll)},
6158 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6159 sizeof (struct GNUNET_MESH_PathACK)},
6165 /******************************************************************************/
6166 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6167 /******************************************************************************/
6170 * deregister_app: iterator for removing each application registered by a client
6172 * @param cls closure
6173 * @param key the hash of the application id (used to access the hashmap)
6174 * @param value the value stored at the key (client)
6176 * @return GNUNET_OK on success
6179 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6181 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6182 GNUNET_break (GNUNET_YES ==
6183 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6189 * notify_client_connection_failure: notify a client that the connection to the
6190 * requested remote peer is not possible (for instance, no route found)
6191 * Function called when the socket is ready to queue more data. "buf" will be
6192 * NULL and "size" zero if the socket was closed for writing in the meantime.
6194 * @param cls closure
6195 * @param size number of bytes available in buf
6196 * @param buf where the callee should write the message
6197 * @return number of bytes written to buf
6200 notify_client_connection_failure (void *cls, size_t size, void *buf)
6203 struct MeshPeerInfo *peer_info;
6204 struct GNUNET_MESH_PeerControl *msg;
6205 struct GNUNET_PeerIdentity id;
6207 if (0 == size && NULL == buf)
6209 // TODO retry? cancel?
6213 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6214 peer_info = (struct MeshPeerInfo *) cls;
6215 msg = (struct GNUNET_MESH_PeerControl *) buf;
6216 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6217 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6218 // msg->tunnel_id = htonl(peer_info->t->tid);
6219 GNUNET_PEER_resolve (peer_info->id, &id);
6220 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6228 * Send keepalive packets for a peer
6230 * @param cls Closure (tunnel for which to send the keepalive).
6231 * @param tc Notification context.
6234 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6236 struct MeshTunnel *t = cls;
6237 struct GNUNET_MESH_TunnelKeepAlive *msg;
6238 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6241 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6242 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6248 "sending keepalive for tunnel %d\n", t->id.tid);
6250 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6251 msg->header.size = htons (size);
6252 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6253 msg->oid = my_full_id;
6254 msg->tid = htonl (t->id.tid);
6255 tunnel_send_multicast (t, &msg->header);
6257 t->path_refresh_task =
6258 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6259 tunnel_reset_timeout(t);
6264 * Function to process paths received for a new peer addition. The recorded
6265 * paths form the initial tunnel, which can be optimized later.
6266 * Called on each result obtained for the DHT search.
6268 * @param cls closure
6269 * @param exp when will this value expire
6270 * @param key key of the result
6271 * @param get_path path of the get request
6272 * @param get_path_length lenght of get_path
6273 * @param put_path path of the put request
6274 * @param put_path_length length of the put_path
6275 * @param type type of the result
6276 * @param size number of bytes in data
6277 * @param data pointer to the result data
6279 * TODO: re-issue the request after certain time? cancel after X results?
6282 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6283 const struct GNUNET_HashCode * key,
6284 const struct GNUNET_PeerIdentity *get_path,
6285 unsigned int get_path_length,
6286 const struct GNUNET_PeerIdentity *put_path,
6287 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6288 size_t size, const void *data)
6290 struct MeshPathInfo *path_info = cls;
6291 struct MeshPeerPath *p;
6292 struct GNUNET_PeerIdentity pi;
6295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6296 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6299 p = path_build_from_dht (get_path, get_path_length, put_path,
6301 path_add_to_peers (p, GNUNET_NO);
6303 for (i = 0; i < path_info->peer->ntunnels; i++)
6305 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6306 peer_info_connect (path_info->peer, path_info->t);
6314 * Function to process paths received for a new peer addition. The recorded
6315 * paths form the initial tunnel, which can be optimized later.
6316 * Called on each result obtained for the DHT search.
6318 * @param cls closure
6319 * @param exp when will this value expire
6320 * @param key key of the result
6321 * @param get_path path of the get request
6322 * @param get_path_length lenght of get_path
6323 * @param put_path path of the put request
6324 * @param put_path_length length of the put_path
6325 * @param type type of the result
6326 * @param size number of bytes in data
6327 * @param data pointer to the result data
6330 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6331 const struct GNUNET_HashCode * key,
6332 const struct GNUNET_PeerIdentity *get_path,
6333 unsigned int get_path_length,
6334 const struct GNUNET_PeerIdentity *put_path,
6335 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6336 size_t size, const void *data)
6338 const struct PBlock *pb = data;
6339 const struct GNUNET_PeerIdentity *pi = &pb->id;
6340 struct MeshTunnel *t = cls;
6341 struct MeshPeerInfo *peer_info;
6342 struct MeshPeerPath *p;
6344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6345 if (size != sizeof (struct PBlock))
6347 GNUNET_break_op (0);
6350 if (ntohl(pb->type) != t->type)
6352 GNUNET_break_op (0);
6355 GNUNET_assert (NULL != t->owner);
6356 peer_info = peer_info_get (pi);
6357 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6359 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6361 p = path_build_from_dht (get_path, get_path_length, put_path,
6363 path_add_to_peers (p, GNUNET_NO);
6365 tunnel_add_peer (t, peer_info);
6366 peer_info_connect (peer_info, t);
6371 * Function to process DHT string to regex matching.
6372 * Called on each result obtained for the DHT search.
6374 * @param cls closure (search context)
6375 * @param exp when will this value expire
6376 * @param key key of the result
6377 * @param get_path path of the get request (not used)
6378 * @param get_path_length lenght of get_path (not used)
6379 * @param put_path path of the put request (not used)
6380 * @param put_path_length length of the put_path (not used)
6381 * @param type type of the result
6382 * @param size number of bytes in data
6383 * @param data pointer to the result data
6386 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6387 const struct GNUNET_HashCode * key,
6388 const struct GNUNET_PeerIdentity *get_path,
6389 unsigned int get_path_length,
6390 const struct GNUNET_PeerIdentity *put_path,
6391 unsigned int put_path_length,
6392 enum GNUNET_BLOCK_Type type,
6393 size_t size, const void *data)
6395 const struct MeshRegexAccept *block = data;
6396 struct MeshRegexSearchContext *ctx = cls;
6397 struct MeshRegexSearchInfo *info = ctx->info;
6398 struct MeshPeerPath *p;
6399 struct MeshPeerInfo *peer_info;
6401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6404 peer_info = peer_info_get(&block->id);
6405 p = path_build_from_dht (get_path, get_path_length, put_path,
6407 path_add_to_peers (p, GNUNET_NO);
6410 tunnel_add_peer (info->t, peer_info);
6411 peer_info_connect (peer_info, info->t);
6412 if (0 == info->peer)
6414 info->peer = peer_info->id;
6418 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6421 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6422 ®ex_connect_timeout,
6430 * Function to process DHT string to regex matching.
6431 * Called on each result obtained for the DHT search.
6433 * @param cls closure (search context)
6434 * @param exp when will this value expire
6435 * @param key key of the result
6436 * @param get_path path of the get request (not used)
6437 * @param get_path_length lenght of get_path (not used)
6438 * @param put_path path of the put request (not used)
6439 * @param put_path_length length of the put_path (not used)
6440 * @param type type of the result
6441 * @param size number of bytes in data
6442 * @param data pointer to the result data
6444 * TODO: re-issue the request after certain time? cancel after X results?
6447 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6448 const struct GNUNET_HashCode * key,
6449 const struct GNUNET_PeerIdentity *get_path,
6450 unsigned int get_path_length,
6451 const struct GNUNET_PeerIdentity *put_path,
6452 unsigned int put_path_length,
6453 enum GNUNET_BLOCK_Type type,
6454 size_t size, const void *data)
6456 const struct MeshRegexBlock *block = data;
6457 struct MeshRegexSearchContext *ctx = cls;
6458 struct MeshRegexSearchInfo *info = ctx->info;
6462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6463 "DHT GET STRING RETURNED RESULTS\n");
6464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6465 " key: %s\n", GNUNET_h2s (key));
6467 copy = GNUNET_malloc (size);
6468 memcpy (copy, data, size);
6469 GNUNET_break (GNUNET_OK ==
6470 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6471 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6472 len = ntohl (block->n_proof);
6474 char proof[len + 1];
6476 memcpy (proof, &block[1], len);
6478 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6480 GNUNET_break_op (0);
6484 len = strlen (info->description);
6485 if (len == ctx->position) // String processed
6487 if (GNUNET_YES == ntohl (block->accepting))
6489 regex_find_path(key, ctx);
6493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6494 // FIXME REGEX this block not successful, wait for more? start timeout?
6499 regex_next_edge (block, size, ctx);
6504 /******************************************************************************/
6505 /********************* MESH LOCAL HANDLES **************************/
6506 /******************************************************************************/
6510 * Handler for client disconnection
6512 * @param cls closure
6513 * @param client identification of the client; NULL
6514 * for the last call when the server is destroyed
6517 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6519 struct MeshClient *c;
6520 struct MeshClient *next;
6523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6532 if (c->handle != client)
6534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6540 GNUNET_SERVER_client_drop (c->handle);
6541 c->shutting_down = GNUNET_YES;
6542 GNUNET_assert (NULL != c->own_tunnels);
6543 GNUNET_assert (NULL != c->incoming_tunnels);
6544 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6545 &tunnel_destroy_iterator, c);
6546 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6547 &tunnel_destroy_iterator, c);
6548 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6549 &tunnel_destroy_iterator, c);
6550 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6551 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6552 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6554 /* deregister clients applications */
6555 if (NULL != c->apps)
6557 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6558 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6560 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6561 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6563 GNUNET_SCHEDULER_cancel (announce_applications_task);
6564 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6566 if (NULL != c->types)
6567 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6568 for (i = 0; i < c->n_regex; i++)
6570 GNUNET_free (c->regexes[i]);
6572 GNUNET_free_non_null (c->regexes);
6573 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6574 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6576 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6579 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6588 * Handler for new clients
6590 * @param cls closure
6591 * @param client identification of the client
6592 * @param message the actual message, which includes messages the client wants
6595 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6596 const struct GNUNET_MessageHeader *message)
6598 struct GNUNET_MESH_ClientConnect *cc_msg;
6599 struct MeshClient *c;
6600 GNUNET_MESH_ApplicationType *a;
6607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6608 /* Check data sanity */
6609 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6610 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6611 ntypes = ntohs (cc_msg->types);
6612 napps = ntohs (cc_msg->applications);
6614 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6617 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6621 /* Create new client structure */
6622 c = GNUNET_malloc (sizeof (struct MeshClient));
6623 c->id = next_client_id++;
6624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6626 GNUNET_SERVER_client_keep (client);
6627 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6630 GNUNET_MESH_ApplicationType at;
6631 struct GNUNET_HashCode hc;
6633 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6634 for (i = 0; i < napps; i++)
6637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6638 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6639 /* store in clients hashmap */
6640 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6641 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6642 /* store in global hashmap, for announcements */
6643 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6644 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6646 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6647 announce_applications_task =
6648 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6654 struct GNUNET_HashCode hc;
6656 t = (uint16_t *) & a[napps];
6657 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6658 for (i = 0; i < ntypes; i++)
6661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6662 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6664 /* store in clients hashmap */
6665 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6666 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6667 /* store in global hashmap */
6668 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6669 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6673 " client has %u+%u subscriptions\n", napps, ntypes);
6675 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6676 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6677 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6678 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6679 GNUNET_SERVER_notification_context_add (nc, client);
6680 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6682 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6688 * Handler for clients announcing available services by a regular expression.
6690 * @param cls closure
6691 * @param client identification of the client
6692 * @param message the actual message, which includes messages the client wants
6695 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6696 const struct GNUNET_MessageHeader *message)
6698 struct MeshClient *c;
6702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6704 /* Sanity check for client registration */
6705 if (NULL == (c = client_get (client)))
6708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6713 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6714 regex = GNUNET_malloc (len + 1);
6715 memcpy (regex, &message[1], len);
6717 GNUNET_array_append (c->regexes, c->n_regex, regex);
6718 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6720 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6726 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6732 * Handler for requests of new tunnels
6734 * @param cls closure
6735 * @param client identification of the client
6736 * @param message the actual message
6739 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6740 const struct GNUNET_MessageHeader *message)
6742 struct GNUNET_MESH_TunnelMessage *t_msg;
6743 struct MeshTunnel *t;
6744 struct MeshClient *c;
6745 MESH_TunnelNumber tid;
6747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6749 /* Sanity check for client registration */
6750 if (NULL == (c = client_get (client)))
6753 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6758 /* Message sanity check */
6759 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6762 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6766 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6767 /* Sanity check for tunnel numbering */
6768 tid = ntohl (t_msg->tunnel_id);
6769 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6775 /* Sanity check for duplicate tunnel IDs */
6776 if (NULL != tunnel_get_by_local_id (c, tid))
6779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6783 while (NULL != tunnel_get_by_pi (myid, next_tid))
6784 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6785 t = tunnel_new (myid, next_tid++, c, tid);
6788 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6790 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6793 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6795 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6796 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6799 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6805 * Handler for requests of deleting tunnels
6807 * @param cls closure
6808 * @param client identification of the client
6809 * @param message the actual message
6812 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6813 const struct GNUNET_MessageHeader *message)
6815 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6816 struct MeshClient *c;
6817 struct MeshTunnel *t;
6818 MESH_TunnelNumber tid;
6820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6821 "Got a DESTROY TUNNEL from client!\n");
6823 /* Sanity check for client registration */
6824 if (NULL == (c = client_get (client)))
6827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6832 /* Message sanity check */
6833 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6836 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6840 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6842 /* Retrieve tunnel */
6843 tid = ntohl (tunnel_msg->tunnel_id);
6844 t = tunnel_get_by_local_id(c, tid);
6847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6849 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6852 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6854 client_ignore_tunnel (c, t);
6856 // TODO: when to destroy incoming tunnel?
6857 if (t->nclients == 0)
6859 GNUNET_assert (GNUNET_YES ==
6860 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6862 GNUNET_assert (GNUNET_YES ==
6863 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6864 &my_full_id.hashPubKey,
6868 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6871 send_client_tunnel_disconnect(t, c);
6872 client_delete_tunnel(c, t);
6874 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6876 tunnel_send_destroy (t);
6877 t->destroy = GNUNET_YES;
6878 // The tunnel will be destroyed when the last message is transmitted.
6879 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6885 * Handler for requests of seeting tunnel's speed.
6887 * @param cls Closure (unused).
6888 * @param client Identification of the client.
6889 * @param message The actual message.
6892 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6893 const struct GNUNET_MessageHeader *message)
6895 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6896 struct MeshClient *c;
6897 struct MeshTunnel *t;
6898 MESH_TunnelNumber tid;
6900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6901 "Got a SPEED request from client!\n");
6903 /* Sanity check for client registration */
6904 if (NULL == (c = client_get (client)))
6907 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6913 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6915 /* Retrieve tunnel */
6916 tid = ntohl (tunnel_msg->tunnel_id);
6917 t = tunnel_get_by_local_id(c, tid);
6920 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6926 switch (ntohs(message->type))
6928 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6929 t->speed_min = GNUNET_YES;
6931 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6932 t->speed_min = GNUNET_NO;
6937 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6942 * Handler for requests of seeting tunnel's buffering policy.
6944 * @param cls Closure (unused).
6945 * @param client Identification of the client.
6946 * @param message The actual message.
6949 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6950 const struct GNUNET_MessageHeader *message)
6952 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6953 struct MeshClient *c;
6954 struct MeshTunnel *t;
6955 MESH_TunnelNumber tid;
6957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6958 "Got a BUFFER request from client!\n");
6960 /* Sanity check for client registration */
6961 if (NULL == (c = client_get (client)))
6964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6969 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6971 /* Retrieve tunnel */
6972 tid = ntohl (tunnel_msg->tunnel_id);
6973 t = tunnel_get_by_local_id(c, tid);
6976 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6978 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6982 switch (ntohs(message->type))
6984 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6985 t->nobuffer = GNUNET_NO;
6987 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6988 t->nobuffer = GNUNET_YES;
6994 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6999 * Handler for connection requests to new peers
7001 * @param cls closure
7002 * @param client identification of the client
7003 * @param message the actual message (PeerControl)
7006 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7007 const struct GNUNET_MessageHeader *message)
7009 struct GNUNET_MESH_PeerControl *peer_msg;
7010 struct MeshPeerInfo *peer_info;
7011 struct MeshClient *c;
7012 struct MeshTunnel *t;
7013 MESH_TunnelNumber tid;
7015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7016 /* Sanity check for client registration */
7017 if (NULL == (c = client_get (client)))
7020 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7025 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7027 /* Sanity check for message size */
7028 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7031 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7035 /* Tunnel exists? */
7036 tid = ntohl (peer_msg->tunnel_id);
7037 t = tunnel_get_by_local_id (c, tid);
7041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7045 /* Does client own tunnel? */
7046 if (t->owner->handle != client)
7049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7053 GNUNET_i2s (&peer_msg->peer));
7054 peer_info = peer_info_get (&peer_msg->peer);
7056 tunnel_add_peer (t, peer_info);
7057 peer_info_connect (peer_info, t);
7059 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7065 * Handler for disconnection requests of peers in a tunnel
7067 * @param cls closure
7068 * @param client identification of the client
7069 * @param message the actual message (PeerControl)
7072 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7073 const struct GNUNET_MessageHeader *message)
7075 struct GNUNET_MESH_PeerControl *peer_msg;
7076 struct MeshPeerInfo *peer_info;
7077 struct MeshClient *c;
7078 struct MeshTunnel *t;
7079 MESH_TunnelNumber tid;
7081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7082 /* Sanity check for client registration */
7083 if (NULL == (c = client_get (client)))
7086 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7091 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7093 /* Sanity check for message size */
7094 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7101 /* Tunnel exists? */
7102 tid = ntohl (peer_msg->tunnel_id);
7103 t = tunnel_get_by_local_id (c, tid);
7107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7112 /* Does client own tunnel? */
7113 if (t->owner->handle != client)
7116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7121 GNUNET_i2s (&peer_msg->peer));
7122 /* Is the peer in the tunnel? */
7124 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7125 if (NULL == peer_info)
7128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7132 /* Ok, delete peer from tunnel */
7133 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7134 &peer_msg->peer.hashPubKey);
7136 send_destroy_path (t, peer_info->id);
7137 tunnel_delete_peer (t, peer_info->id);
7138 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7143 * Handler for blacklist requests of peers in a tunnel
7145 * @param cls closure
7146 * @param client identification of the client
7147 * @param message the actual message (PeerControl)
7149 * FIXME implement DHT block bloomfilter
7152 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7153 const struct GNUNET_MessageHeader *message)
7155 struct GNUNET_MESH_PeerControl *peer_msg;
7156 struct MeshClient *c;
7157 struct MeshTunnel *t;
7158 MESH_TunnelNumber tid;
7160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7161 /* Sanity check for client registration */
7162 if (NULL == (c = client_get (client)))
7165 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7170 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7172 /* Sanity check for message size */
7173 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7176 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7180 /* Tunnel exists? */
7181 tid = ntohl (peer_msg->tunnel_id);
7182 t = tunnel_get_by_local_id (c, tid);
7186 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7191 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7192 GNUNET_PEER_intern(&peer_msg->peer));
7197 * Handler for unblacklist requests of peers in a tunnel
7199 * @param cls closure
7200 * @param client identification of the client
7201 * @param message the actual message (PeerControl)
7204 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7205 const struct GNUNET_MessageHeader *message)
7207 struct GNUNET_MESH_PeerControl *peer_msg;
7208 struct MeshClient *c;
7209 struct MeshTunnel *t;
7210 MESH_TunnelNumber tid;
7214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7215 /* Sanity check for client registration */
7216 if (NULL == (c = client_get (client)))
7219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7224 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7226 /* Sanity check for message size */
7227 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7230 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7234 /* Tunnel exists? */
7235 tid = ntohl (peer_msg->tunnel_id);
7236 t = tunnel_get_by_local_id (c, tid);
7240 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7245 /* if peer is not known, complain */
7246 pid = GNUNET_PEER_search (&peer_msg->peer);
7253 /* search and remove from list */
7254 for (i = 0; i < t->nblacklisted; i++)
7256 if (t->blacklisted[i] == pid)
7258 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7259 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7264 /* if peer hasn't been blacklisted, complain */
7270 * Handler for connection requests to new peers by type
7272 * @param cls closure
7273 * @param client identification of the client
7274 * @param message the actual message (ConnectPeerByType)
7277 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7278 const struct GNUNET_MessageHeader *message)
7280 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7281 struct MeshClient *c;
7282 struct MeshTunnel *t;
7283 struct GNUNET_HashCode hash;
7284 MESH_TunnelNumber tid;
7286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7287 /* Sanity check for client registration */
7288 if (NULL == (c = client_get (client)))
7291 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7296 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7298 /* Sanity check for message size */
7299 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7300 ntohs (connect_msg->header.size))
7303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7307 /* Tunnel exists? */
7308 tid = ntohl (connect_msg->tunnel_id);
7309 t = tunnel_get_by_local_id (c, tid);
7313 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7317 /* Does client own tunnel? */
7318 if (t->owner->handle != client)
7321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7325 /* Do WE have the service? */
7326 t->type = ntohl (connect_msg->type);
7327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7328 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7329 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7332 /* Yes! Fast forward, add ourselves to the tunnel and send the
7333 * good news to the client, and alert the destination client of
7334 * an incoming tunnel.
7336 * FIXME send a path create to self, avoid code duplication
7338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7339 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7340 peer_info_get (&my_full_id),
7341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7344 send_client_peer_connected (t, myid);
7345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7346 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7348 t->local_tid_dest = next_local_tid++;
7349 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7350 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7351 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7355 /* Ok, lets find a peer offering the service */
7356 if (NULL != t->dht_get_type)
7358 GNUNET_DHT_get_stop (t->dht_get_type);
7360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7361 GNUNET_h2s (&hash));
7363 GNUNET_DHT_get_start (dht_handle,
7364 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7366 dht_replication_level,
7367 GNUNET_DHT_RO_RECORD_ROUTE |
7368 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7370 &dht_get_type_handler, t);
7372 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7378 * Handler for connection requests to new peers by a string service description.
7380 * @param cls closure
7381 * @param client identification of the client
7382 * @param message the actual message, which includes messages the client wants
7385 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7386 const struct GNUNET_MessageHeader *message)
7388 struct GNUNET_MESH_ConnectPeerByString *msg;
7389 struct MeshRegexSearchContext *ctx;
7390 struct MeshRegexSearchInfo *info;
7391 struct GNUNET_DHT_GetHandle *get_h;
7392 struct GNUNET_HashCode key;
7393 struct MeshTunnel *t;
7394 struct MeshClient *c;
7395 MESH_TunnelNumber tid;
7399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7400 "Connect by string started\n");
7401 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7402 size = htons (message->size);
7404 /* Sanity check for client registration */
7405 if (NULL == (c = client_get (client)))
7408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7413 /* Message size sanity check */
7414 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7417 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7421 /* Tunnel exists? */
7422 tid = ntohl (msg->tunnel_id);
7423 t = tunnel_get_by_local_id (c, tid);
7427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7431 /* Does client own tunnel? */
7432 if (t->owner->handle != client)
7435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7440 " on tunnel %s [%u]\n",
7441 GNUNET_i2s(&my_full_id),
7444 /* Only one connect_by_string allowed at the same time! */
7445 /* FIXME: allow more, return handle at api level to cancel, document */
7446 if (NULL != t->regex_ctx)
7449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7453 /* Find string itself */
7454 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7455 string = (const char *) &msg[1];
7457 /* Initialize context */
7458 size = GNUNET_REGEX_get_first_key(string, len, &key);
7459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7460 " consumed %u bits out of %u\n", size, len);
7461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7462 " looking for %s\n", GNUNET_h2s (&key));
7464 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7466 info->description = GNUNET_malloc (len + 1);
7467 memcpy (info->description, string, len);
7468 info->description[len] = '\0';
7469 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7470 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7473 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7474 ctx->position = size;
7478 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7480 /* Start search in DHT */
7481 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7482 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7483 &key, /* key to search */
7484 dht_replication_level, /* replication level */
7485 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7486 NULL, /* xquery */ // FIXME BLOOMFILTER
7487 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7488 &dht_get_string_handler, ctx);
7490 GNUNET_break (GNUNET_OK ==
7491 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7494 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7496 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7502 * Handler for client traffic directed to one peer
7504 * @param cls closure
7505 * @param client identification of the client
7506 * @param message the actual message
7509 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7510 const struct GNUNET_MessageHeader *message)
7512 struct MeshClient *c;
7513 struct MeshTunnel *t;
7514 struct MeshPeerInfo *pi;
7515 struct GNUNET_MESH_Unicast *data_msg;
7516 MESH_TunnelNumber tid;
7519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7520 "Got a unicast request from a client!\n");
7522 /* Sanity check for client registration */
7523 if (NULL == (c = client_get (client)))
7526 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7531 data_msg = (struct GNUNET_MESH_Unicast *) message;
7533 /* Sanity check for message size */
7534 size = ntohs (message->size);
7535 if (sizeof (struct GNUNET_MESH_Unicast) +
7536 sizeof (struct GNUNET_MessageHeader) > size)
7539 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7543 /* Tunnel exists? */
7544 tid = ntohl (data_msg->tid);
7545 t = tunnel_get_by_local_id (c, tid);
7549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7553 /* Is it a local tunnel? Then, does client own the tunnel? */
7554 if (t->owner->handle != client)
7557 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7561 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7562 &data_msg->destination.hashPubKey);
7563 /* Is the selected peer in the tunnel? */
7567 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7571 /* PID should be as expected */
7572 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7575 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7576 "Unicast PID, expected %u, got %u\n",
7577 t->fwd_pid + 1, ntohl (data_msg->pid));
7578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7582 /* Ok, everything is correct, send the message
7583 * (pretend we got it from a mesh peer)
7586 /* Work around const limitation */
7587 char buf[ntohs (message->size)] GNUNET_ALIGN;
7588 struct GNUNET_MESH_Unicast *copy;
7590 copy = (struct GNUNET_MESH_Unicast *) buf;
7591 memcpy (buf, data_msg, size);
7592 copy->oid = my_full_id;
7593 copy->tid = htonl (t->id.tid);
7594 copy->ttl = htonl (default_ttl);
7595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7596 " calling generic handler...\n");
7597 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7600 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7607 * Handler for client traffic directed to the origin
7609 * @param cls closure
7610 * @param client identification of the client
7611 * @param message the actual message
7614 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7615 const struct GNUNET_MessageHeader *message)
7617 struct GNUNET_MESH_ToOrigin *data_msg;
7618 struct MeshTunnelClientInfo *clinfo;
7619 struct MeshClient *c;
7620 struct MeshTunnel *t;
7621 MESH_TunnelNumber tid;
7624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7625 "Got a ToOrigin request from a client!\n");
7626 /* Sanity check for client registration */
7627 if (NULL == (c = client_get (client)))
7630 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7635 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7637 /* Sanity check for message size */
7638 size = ntohs (message->size);
7639 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7640 sizeof (struct GNUNET_MessageHeader) > size)
7643 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7647 /* Tunnel exists? */
7648 tid = ntohl (data_msg->tid);
7649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7650 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7656 t = tunnel_get_by_local_id (c, tid);
7659 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7662 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7666 /* It should be sent by someone who has this as incoming tunnel. */
7667 if (GNUNET_NO == client_knows_tunnel (c, t))
7670 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7674 /* PID should be as expected */
7675 clinfo = tunnel_get_client_fc (t, c);
7676 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7679 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7680 "To Origin PID, expected %u, got %u\n",
7681 clinfo->bck_pid + 1,
7682 ntohl (data_msg->pid));
7683 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7688 /* Ok, everything is correct, send the message
7689 * (pretend we got it from a mesh peer)
7692 char buf[ntohs (message->size)] GNUNET_ALIGN;
7693 struct GNUNET_MESH_ToOrigin *copy;
7695 /* Work around const limitation */
7696 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7697 memcpy (buf, data_msg, size);
7698 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7699 copy->tid = htonl (t->id.tid);
7700 copy->ttl = htonl (default_ttl);
7701 copy->pid = htonl (++(t->bck_pid));
7703 copy->sender = my_full_id;
7704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7705 " calling generic handler...\n");
7706 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7708 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7715 * Handler for client traffic directed to all peers in a tunnel
7717 * @param cls closure
7718 * @param client identification of the client
7719 * @param message the actual message
7722 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7723 const struct GNUNET_MessageHeader *message)
7725 struct MeshClient *c;
7726 struct MeshTunnel *t;
7727 struct GNUNET_MESH_Multicast *data_msg;
7728 MESH_TunnelNumber tid;
7730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7731 "Got a multicast request from a client!\n");
7733 /* Sanity check for client registration */
7734 if (NULL == (c = client_get (client)))
7737 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7742 data_msg = (struct GNUNET_MESH_Multicast *) message;
7744 /* Sanity check for message size */
7745 if (sizeof (struct GNUNET_MESH_Multicast) +
7746 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7749 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7753 /* Tunnel exists? */
7754 tid = ntohl (data_msg->tid);
7755 t = tunnel_get_by_local_id (c, tid);
7759 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7760 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7762 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7766 /* Does client own tunnel? */
7767 if (t->owner->handle != client)
7770 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7774 /* PID should be as expected */
7775 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7779 "Multicast PID, expected %u, got %u\n",
7780 t->fwd_pid + 1, ntohl (data_msg->pid));
7781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7786 char buf[ntohs (message->size)] GNUNET_ALIGN;
7787 struct GNUNET_MESH_Multicast *copy;
7789 copy = (struct GNUNET_MESH_Multicast *) buf;
7790 memcpy (buf, message, ntohs (message->size));
7791 copy->oid = my_full_id;
7792 copy->tid = htonl (t->id.tid);
7793 copy->ttl = htonl (default_ttl);
7794 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7796 " calling generic handler...\n");
7797 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7800 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7806 * Handler for client's ACKs for payload traffic.
7808 * @param cls Closure (unused).
7809 * @param client Identification of the client.
7810 * @param message The actual message.
7813 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7814 const struct GNUNET_MessageHeader *message)
7816 struct GNUNET_MESH_LocalAck *msg;
7817 struct MeshTunnel *t;
7818 struct MeshClient *c;
7819 MESH_TunnelNumber tid;
7822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7823 /* Sanity check for client registration */
7824 if (NULL == (c = client_get (client)))
7827 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7832 msg = (struct GNUNET_MESH_LocalAck *) message;
7834 /* Tunnel exists? */
7835 tid = ntohl (msg->tunnel_id);
7836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7837 t = tunnel_get_by_local_id (c, tid);
7841 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7842 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7843 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7847 ack = ntohl (msg->max_pid);
7848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7850 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7851 if (NULL != t->owner && t->owner->handle == client)
7853 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7855 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7859 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7860 tunnel_set_client_fwd_ack (t, c, ack);
7861 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7864 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7871 * Functions to handle messages from clients
7873 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7874 {&handle_local_new_client, NULL,
7875 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7876 {&handle_local_announce_regex, NULL,
7877 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7878 {&handle_local_tunnel_create, NULL,
7879 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7880 sizeof (struct GNUNET_MESH_TunnelMessage)},
7881 {&handle_local_tunnel_destroy, NULL,
7882 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7883 sizeof (struct GNUNET_MESH_TunnelMessage)},
7884 {&handle_local_tunnel_speed, NULL,
7885 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7886 sizeof (struct GNUNET_MESH_TunnelMessage)},
7887 {&handle_local_tunnel_speed, NULL,
7888 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7889 sizeof (struct GNUNET_MESH_TunnelMessage)},
7890 {&handle_local_tunnel_buffer, NULL,
7891 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7892 sizeof (struct GNUNET_MESH_TunnelMessage)},
7893 {&handle_local_tunnel_buffer, NULL,
7894 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7895 sizeof (struct GNUNET_MESH_TunnelMessage)},
7896 {&handle_local_connect_add, NULL,
7897 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7898 sizeof (struct GNUNET_MESH_PeerControl)},
7899 {&handle_local_connect_del, NULL,
7900 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7901 sizeof (struct GNUNET_MESH_PeerControl)},
7902 {&handle_local_blacklist, NULL,
7903 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7904 sizeof (struct GNUNET_MESH_PeerControl)},
7905 {&handle_local_unblacklist, NULL,
7906 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7907 sizeof (struct GNUNET_MESH_PeerControl)},
7908 {&handle_local_connect_by_type, NULL,
7909 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7910 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7911 {&handle_local_connect_by_string, NULL,
7912 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7913 {&handle_local_unicast, NULL,
7914 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7915 {&handle_local_to_origin, NULL,
7916 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7917 {&handle_local_multicast, NULL,
7918 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7919 {&handle_local_ack, NULL,
7920 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7921 sizeof (struct GNUNET_MESH_LocalAck)},
7927 * To be called on core init/fail.
7929 * @param cls service closure
7930 * @param server handle to the server for this service
7931 * @param identity the public identity of this peer
7934 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7935 const struct GNUNET_PeerIdentity *identity)
7938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7939 core_handle = server;
7940 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7943 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7944 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7946 GNUNET_abort(); // Try harder
7953 * Method called whenever a given peer connects.
7955 * @param cls closure
7956 * @param peer peer identity this notification is about
7957 * @param atsi performance data for the connection
7958 * @param atsi_count number of records in 'atsi'
7961 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7962 const struct GNUNET_ATS_Information *atsi,
7963 unsigned int atsi_count)
7965 struct MeshPeerInfo *peer_info;
7966 struct MeshPeerPath *path;
7968 DEBUG_CONN ("Peer connected\n");
7969 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7970 peer_info = peer_info_get (peer);
7971 if (myid == peer_info->id)
7973 DEBUG_CONN (" (self)\n");
7978 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7980 path = path_new (2);
7981 path->peers[0] = myid;
7982 path->peers[1] = peer_info->id;
7983 GNUNET_PEER_change_rc (myid, 1);
7984 GNUNET_PEER_change_rc (peer_info->id, 1);
7985 peer_info_add_path (peer_info, path, GNUNET_YES);
7986 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7992 * Method called whenever a peer disconnects.
7994 * @param cls closure
7995 * @param peer peer identity this notification is about
7998 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8000 struct MeshPeerInfo *pi;
8001 struct MeshPeerQueue *q;
8002 struct MeshPeerQueue *n;
8004 DEBUG_CONN ("Peer disconnected\n");
8005 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8015 /* TODO try to reroute this traffic instead */
8016 queue_destroy(q, GNUNET_YES);
8019 if (NULL != pi->core_transmit)
8021 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8022 pi->core_transmit = NULL;
8024 peer_info_remove_path (pi, pi->id, myid);
8027 DEBUG_CONN (" (self)\n");
8029 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8034 /******************************************************************************/
8035 /************************ MAIN FUNCTIONS ****************************/
8036 /******************************************************************************/
8039 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8041 * @param cls closure
8042 * @param key current key code
8043 * @param value value in the hash map
8044 * @return GNUNET_YES if we should continue to iterate,
8048 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8050 struct MeshTunnel *t = value;
8057 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8059 * @param cls closure
8060 * @param key current key code
8061 * @param value value in the hash map
8062 * @return GNUNET_YES if we should continue to iterate,
8066 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8068 struct MeshPeerInfo *p = value;
8069 struct MeshPeerQueue *q;
8070 struct MeshPeerQueue *n;
8078 queue_destroy(q, GNUNET_YES);
8082 peer_info_destroy (p);
8088 * Task run during shutdown.
8094 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8098 if (core_handle != NULL)
8100 GNUNET_CORE_disconnect (core_handle);
8105 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8108 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8109 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8110 if (dht_handle != NULL)
8112 GNUNET_DHT_disconnect (dht_handle);
8117 GNUNET_SERVER_notification_context_destroy (nc);
8120 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8122 GNUNET_SCHEDULER_cancel (announce_id_task);
8123 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8130 * Callback for hostkey read/generation
8133 * @param pk the private key
8134 * @param emsg error message
8137 key_generation_cb (void *cls,
8138 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8141 struct MeshPeerInfo *peer;
8142 struct MeshPeerPath *p;
8147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8148 _("Mesh service could not access hostkey. Exiting.\n"));
8149 GNUNET_SCHEDULER_shutdown ();
8152 my_private_key = pk;
8153 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8154 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8155 &my_full_id.hashPubKey);
8156 myid = GNUNET_PEER_intern (&my_full_id);
8157 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8158 "Mesh for peer [%s] starting\n",
8159 GNUNET_i2s(&my_full_id));
8161 // transport_handle = GNUNET_TRANSPORT_connect(c,
8171 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8174 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8175 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8176 GNUNET_SERVER_disconnect_notify (server_handle,
8177 &handle_local_client_disconnect, NULL);
8181 clients_tail = NULL;
8184 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8185 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8187 /* Create a peer_info for the local peer */
8188 peer = peer_info_get (&my_full_id);
8191 GNUNET_PEER_change_rc (myid, 1);
8192 peer_info_add_path (peer, p, GNUNET_YES);
8193 GNUNET_SERVER_resume (server_handle);
8194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8199 * Process mesh requests.
8201 * @param cls closure
8202 * @param server the initialized server
8203 * @param c configuration to use
8206 run (void *cls, struct GNUNET_SERVER_Handle *server,
8207 const struct GNUNET_CONFIGURATION_Handle *c)
8211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8212 server_handle = server;
8213 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8214 NULL, /* Closure passed to MESH functions */
8215 &core_init, /* Call core_init once connected */
8216 &core_connect, /* Handle connects */
8217 &core_disconnect, /* remove peers on disconnects */
8218 NULL, /* Don't notify about all incoming messages */
8219 GNUNET_NO, /* For header only in notification */
8220 NULL, /* Don't notify about all outbound messages */
8221 GNUNET_NO, /* For header-only out notification */
8222 core_handlers); /* Register these handlers */
8224 if (core_handle == NULL)
8227 GNUNET_SCHEDULER_shutdown ();
8232 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8237 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8239 GNUNET_SCHEDULER_shutdown ();
8244 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8245 &refresh_path_time))
8247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8249 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8250 "mesh", "refresh path time");
8251 GNUNET_SCHEDULER_shutdown ();
8256 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8257 &app_announce_time))
8259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8261 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8262 "mesh", "app announce time");
8263 GNUNET_SCHEDULER_shutdown ();
8266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8269 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8274 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8275 "mesh", "id announce time");
8276 GNUNET_SCHEDULER_shutdown ();
8284 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8285 &unacknowledged_wait_time))
8287 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8289 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8290 "mesh", "unacknowledged wait time");
8291 GNUNET_SCHEDULER_shutdown ();
8296 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8301 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8302 "mesh", "connect timeout");
8303 GNUNET_SCHEDULER_shutdown ();
8308 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8311 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8313 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8314 "mesh", "max msgs queue");
8315 GNUNET_SCHEDULER_shutdown ();
8320 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8325 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8326 "mesh", "max tunnels");
8327 GNUNET_SCHEDULER_shutdown ();
8332 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8337 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8338 "mesh", "default ttl", 64);
8343 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8344 &dht_replication_level))
8346 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8348 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8349 "mesh", "dht replication level", 10);
8350 dht_replication_level = 10;
8353 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8354 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8355 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8356 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8357 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8359 dht_handle = GNUNET_DHT_connect (c, 64);
8360 if (NULL == dht_handle)
8364 stats = GNUNET_STATISTICS_create ("mesh", c);
8366 GNUNET_SERVER_suspend (server_handle);
8367 /* Scheduled the task to clean up when shutdown is called */
8368 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8370 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8371 GNUNET_free (keyfile);
8376 * The main function for the mesh service.
8378 * @param argc number of arguments from the command line
8379 * @param argv command line arguments
8380 * @return 0 ok, 1 on error
8383 main (int argc, char *const *argv)
8388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8389 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8391 ret = (GNUNET_OK == r) ? 0 : 1;
8392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8396 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8397 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8398 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);