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 to store regex information announced by clients.
176 struct MeshRegexDescriptor
179 * Regular expression itself.
184 * How many characters per edge can we squeeze?
186 uint16_t compression;
190 * Struct containing all info possibly needed to build a package when called
193 struct MeshTransmissionDescriptor
195 /** ID of the tunnel this packet travels in */
196 struct MESH_TunnelID *origin;
198 /** Who was this message being sent to */
199 struct MeshPeerInfo *peer;
201 /** Ultimate destination of the packet */
202 GNUNET_PEER_Id destination;
204 /** Data descriptor */
205 struct MeshData* mesh_data;
210 * Struct containing all information regarding a given peer
220 * Last time we heard from this peer
222 struct GNUNET_TIME_Absolute last_contact;
225 * Task handler for delayed connect task;
227 GNUNET_SCHEDULER_TaskIdentifier connect_task;
230 * Number of attempts to reconnect so far
232 int n_reconnect_attempts;
235 * Paths to reach the peer, ordered by ascending hop count
237 struct MeshPeerPath *path_head;
240 * Paths to reach the peer, ordered by ascending hop count
242 struct MeshPeerPath *path_tail;
245 * Handle to stop the DHT search for a path to this peer
247 struct GNUNET_DHT_GetHandle *dhtget;
250 * Closure given to the DHT GET
252 struct MeshPathInfo *dhtgetcls;
255 * Array of tunnels this peer participates in
256 * (most probably a small amount, therefore not a hashmap)
257 * When the path to the peer changes, notify these tunnels to let them
258 * re-adjust their path trees.
260 struct MeshTunnel **tunnels;
263 * Number of tunnels this peers participates in
265 unsigned int ntunnels;
268 * Transmission queue to core DLL head
270 struct MeshPeerQueue *queue_head;
273 * Transmission queue to core DLL tail
275 struct MeshPeerQueue *queue_tail;
278 * How many messages are in the queue to this peer.
280 unsigned int queue_n;
283 * Handle to for queued transmissions
285 struct GNUNET_CORE_TransmitHandle *core_transmit;
290 * Globally unique tunnel identification (owner + number)
291 * DO NOT USE OVER THE NETWORK
296 * Node that owns the tunnel
301 * Tunnel number to differentiate all the tunnels owned by the node oid
302 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
304 MESH_TunnelNumber tid;
309 * Struct containing all information regarding a tunnel
310 * For an intermediate node the improtant info used will be:
311 * - id Tunnel unique identification
312 * - paths[0] To know where to send it next
313 * - metainfo: ready, speeds, accounting
320 struct MESH_TunnelID id;
323 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
325 MESH_TunnelNumber local_tid;
328 * Local tunnel number for local destination clients (incoming number)
329 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
332 MESH_TunnelNumber local_tid_dest;
335 * Is the speed on the tunnel limited to the slowest peer?
340 * Is the tunnel bufferless (minimum latency)?
345 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
350 * Packet ID of the last bck packet sent (unique counter per hop).
355 * SKIP value for this tunnel.
360 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
361 * Contains the Flow Control info: FWD ACK value received,
362 * last BCK ACK sent, PID and SKIP values.
364 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
367 * Last ACK sent towards the origin (for traffic towards leaf node).
369 uint32_t last_fwd_ack;
372 * BCK ACK value received from the hop towards the owner of the tunnel,
373 * (previous node / owner): up to what message PID can we sent back to him.
378 * How many messages are in the forward queue (towards leaves).
380 unsigned int fwd_queue_n;
383 * How many messages do we accept in the forward queue.
385 unsigned int fwd_queue_max;
388 * How many messages are in the backward queue (towards origin).
390 unsigned int bck_queue_n;
393 * How many messages do we accept in the backward queue.
395 unsigned int bck_queue_max;
398 * Task to poll peer in case of a stall.
400 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
403 * Last time the tunnel was used
405 struct GNUNET_TIME_Absolute timestamp;
408 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
409 * containing peers added by id or by type, not intermediate peers.
411 struct GNUNET_CONTAINER_MultiHashMap *peers;
414 * Number of peers that are connected and potentially ready to receive data
416 unsigned int peers_ready;
419 * Number of peers that have been added to the tunnel
421 unsigned int peers_total;
424 * Client owner of the tunnel, if any
426 struct MeshClient *owner;
429 * Clients that have been informed about and want to stay in the tunnel.
431 struct MeshClient **clients;
434 * Flow control info for each client.
436 struct MeshTunnelClientInfo *clients_fc;
439 * Number of elements in clients/clients_fc
441 unsigned int nclients;
444 * Clients that have been informed but requested to leave the tunnel.
446 struct MeshClient **ignore;
449 * Number of elements in clients
451 unsigned int nignore;
456 GNUNET_PEER_Id *blacklisted;
459 * Number of elements in blacklisted
461 unsigned int nblacklisted;
464 * Bloomfilter (for peer identities) to stop circular routes
466 char bloomfilter[MESH_BLOOM_SIZE];
471 struct MeshTunnelTree *tree;
474 * Application type we are looking for in this tunnel
476 GNUNET_MESH_ApplicationType type;
479 * Used to search peers offering a service
481 struct GNUNET_DHT_GetHandle *dht_get_type;
484 * Initial context of the regex search for a connect_by_string
486 struct MeshRegexSearchContext *regex_ctx;
489 * Task to keep the used paths alive
491 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
494 * Task to destroy the tunnel after timeout
496 * FIXME: merge the two? a tunnel will have either
497 * a path refresh OR a timeout, never both!
499 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
502 * Flag to signal the destruction of the tunnel.
503 * If this is set GNUNET_YES the tunnel will be destroyed
504 * when the queue is empty.
511 * Info about a child node in a tunnel, needed to perform flow control.
513 struct MeshTunnelChildInfo
516 * ID of the child node.
536 * Maximum PID allowed (FWD ACK received).
541 * Last ACK sent to that child (BCK ACK).
546 * Circular buffer pointing to MeshPeerQueue elements for all
547 * payload traffic going to this child.
548 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
550 struct MeshPeerQueue **send_buffer;
553 * Index of the oldest element in the send_buffer.
555 unsigned int send_buffer_start;
558 * How many elements are already in the buffer.
560 unsigned int send_buffer_n;
563 * Tunnel this info is about
565 struct MeshTunnel *t;
568 * Task to poll peer in case of a stall.
570 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
575 * Info about a leaf client of a tunnel, needed to perform flow control.
577 struct MeshTunnelClientInfo
580 * PID of the last packet sent to the client (FWD).
585 * PID of the last packet received from the client (BCK).
590 * Maximum PID allowed (FWD ACK received).
595 * Last ACK sent to that child (BCK ACK).
603 * Info collected during iteration of child nodes in order to get the ACK value
606 struct MeshTunnelChildIteratorContext
609 * Tunnel whose info is being collected.
611 struct MeshTunnel *t;
614 * Is this context initialized? Is the value in max_child_ack valid?
619 * Maximum child ACK so far.
621 uint32_t max_child_ack;
624 * Number of children nodes
626 unsigned int nchildren;
631 * Info needed to work with tunnel paths and peers
638 struct MeshTunnel *t;
641 * Neighbouring peer to whom we send the packet to
643 struct MeshPeerInfo *peer;
648 struct MeshPeerPath *path;
653 * Struct containing information about a client of the service
660 struct MeshClient *next;
665 struct MeshClient *prev;
668 * Tunnels that belong to this client, indexed by local id
670 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
673 * Tunnels this client has accepted, indexed by incoming local id
675 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
678 * Tunnels this client has rejected, indexed by incoming local id
680 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
682 * Handle to communicate with the client
684 struct GNUNET_SERVER_Client *handle;
687 * Applications that this client has claimed to provide
689 struct GNUNET_CONTAINER_MultiHashMap *apps;
692 * Messages that this client has declared interest in
694 struct GNUNET_CONTAINER_MultiHashMap *types;
697 * Whether the client is active or shutting down (don't send confirmations
698 * to a client that is shutting down.
703 * ID of the client, mainly for debug messages
708 * Regular expressions describing the services offered by this client.
710 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
713 * Number of regular expressions in regexes.
715 unsigned int n_regex;
718 * Task to refresh all regular expresions in the DHT.
720 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
726 * Struct to keep information of searches of services described by a regex
727 * using a user-provided string service description.
729 struct MeshRegexSearchInfo
732 * Which tunnel is this for
734 struct MeshTunnel *t;
737 * User provided description of the searched service.
742 * Part of the description already consumed by the search.
749 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
752 * Results from running DHT GETs.
754 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
757 * Contexts, for each running DHT GET. Free all on end of search.
759 struct MeshRegexSearchContext **contexts;
762 * Number of contexts (branches/steps in search).
764 unsigned int n_contexts;
767 * Peer that is connecting via connect_by_string. When connected, free ctx.
772 * Other peers that are found but not yet being connected to.
774 GNUNET_PEER_Id *peers;
777 * Number of elements in peers.
779 unsigned int n_peers;
782 * Next peer to try to connect to.
787 * Timeout for a connect attempt.
788 * When reached, try to connect to a different peer, if any. If not,
789 * try the same peer again.
791 GNUNET_SCHEDULER_TaskIdentifier timeout;
796 * Struct to keep state of running searches that have consumed a part of
799 struct MeshRegexSearchContext
802 * Part of the description already consumed by
803 * this particular search branch.
808 * Information about the search.
810 struct MeshRegexSearchInfo *info;
813 * We just want to look for one edge, the longer the better.
816 unsigned int longest_match;
819 * Destination hash of the longest match.
821 struct GNUNET_HashCode hash;
824 /******************************************************************************/
825 /************************ DEBUG FUNCTIONS ****************************/
826 /******************************************************************************/
830 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
831 * @param cls string to print
832 * @param success GNUNET_OK if the PUT was transmitted,
833 * GNUNET_NO on timeout,
834 * GNUNET_SYSERR on disconnect from service
835 * after the PUT message was transmitted
836 * (so we don't know if it was received or not)
841 mesh_debug (void *cls, int success)
845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
849 unsigned int debug_fwd_ack;
850 unsigned int debug_bck_ack;
854 /******************************************************************************/
855 /*********************** GLOBAL VARIABLES ****************************/
856 /******************************************************************************/
859 * Configuration parameters
861 static struct GNUNET_TIME_Relative refresh_path_time;
862 static struct GNUNET_TIME_Relative app_announce_time;
863 static struct GNUNET_TIME_Relative id_announce_time;
864 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
865 static struct GNUNET_TIME_Relative connect_timeout;
866 static unsigned long long default_ttl;
867 static unsigned long long dht_replication_level;
868 static unsigned long long max_tunnels;
869 static unsigned long long max_msgs_queue;
873 * Hostkey generation context
875 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
878 * DLL with all the clients, head.
880 static struct MeshClient *clients;
883 * DLL with all the clients, tail.
885 static struct MeshClient *clients_tail;
888 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
890 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
893 * Number of tunnels known.
895 static unsigned long long n_tunnels;
898 * Tunnels incoming, indexed by MESH_TunnelNumber
899 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
901 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
904 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
906 static struct GNUNET_CONTAINER_MultiHashMap *peers;
909 * Handle to communicate with transport
911 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
914 * Handle to communicate with core.
916 static struct GNUNET_CORE_Handle *core_handle;
921 static struct GNUNET_DHT_Handle *dht_handle;
926 static struct GNUNET_SERVER_Handle *server_handle;
929 * Handle to the statistics service.
931 static struct GNUNET_STATISTICS_Handle *stats;
934 * Notification context, to send messages to local clients.
936 static struct GNUNET_SERVER_NotificationContext *nc;
939 * Local peer own ID (memory efficient handle).
941 static GNUNET_PEER_Id myid;
944 * Local peer own ID (full value).
946 static struct GNUNET_PeerIdentity my_full_id;
951 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
956 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
959 * Tunnel ID for the next created tunnel (global tunnel number).
961 static MESH_TunnelNumber next_tid;
964 * Tunnel ID for the next incoming tunnel (local tunnel number).
966 static MESH_TunnelNumber next_local_tid;
969 * All application types provided by this peer.
971 static struct GNUNET_CONTAINER_MultiHashMap *applications;
974 * All message types clients of this peer are interested in.
976 static struct GNUNET_CONTAINER_MultiHashMap *types;
979 * Task to periodically announce provided applications.
981 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
984 * Task to periodically announce itself in the network.
986 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
989 * Next ID to assign to a client.
991 unsigned int next_client_id;
994 /******************************************************************************/
995 /*********************** DECLARATIONS **************************/
996 /******************************************************************************/
999 * Function to process paths received for a new peer addition. The recorded
1000 * paths form the initial tunnel, which can be optimized later.
1001 * Called on each result obtained for the DHT search.
1003 * @param cls closure
1004 * @param exp when will this value expire
1005 * @param key key of the result
1006 * @param type type of the result
1007 * @param size number of bytes in data
1008 * @param data pointer to the result data
1011 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1012 const struct GNUNET_HashCode * key,
1013 const struct GNUNET_PeerIdentity *get_path,
1014 unsigned int get_path_length,
1015 const struct GNUNET_PeerIdentity *put_path,
1016 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1017 size_t size, const void *data);
1021 * Function to process DHT string to regex matching.
1022 * Called on each result obtained for the DHT search.
1024 * @param cls closure (search context)
1025 * @param exp when will this value expire
1026 * @param key key of the result
1027 * @param get_path path of the get request (not used)
1028 * @param get_path_length lenght of get_path (not used)
1029 * @param put_path path of the put request (not used)
1030 * @param put_path_length length of the put_path (not used)
1031 * @param type type of the result
1032 * @param size number of bytes in data
1033 * @param data pointer to the result data
1035 * TODO: re-issue the request after certain time? cancel after X results?
1038 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1039 const struct GNUNET_HashCode * key,
1040 const struct GNUNET_PeerIdentity *get_path,
1041 unsigned int get_path_length,
1042 const struct GNUNET_PeerIdentity *put_path,
1043 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1044 size_t size, const void *data);
1048 * Function to process DHT string to regex matching.
1049 * Called on each result obtained for the DHT search.
1051 * @param cls closure (search context)
1052 * @param exp when will this value expire
1053 * @param key key of the result
1054 * @param get_path path of the get request (not used)
1055 * @param get_path_length lenght of get_path (not used)
1056 * @param put_path path of the put request (not used)
1057 * @param put_path_length length of the put_path (not used)
1058 * @param type type of the result
1059 * @param size number of bytes in data
1060 * @param data pointer to the result data
1063 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1064 const struct GNUNET_HashCode * key,
1065 const struct GNUNET_PeerIdentity *get_path,
1066 unsigned int get_path_length,
1067 const struct GNUNET_PeerIdentity *put_path,
1068 unsigned int put_path_length,
1069 enum GNUNET_BLOCK_Type type,
1070 size_t size, const void *data);
1074 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1075 * and insert it in the appropiate structures if the peer is not known yet.
1077 * @param peer Short identity of the peer.
1079 * @return Existing or newly created peer info.
1081 static struct MeshPeerInfo *
1082 peer_info_get_short (const GNUNET_PEER_Id peer);
1086 * Try to establish a new connection to this peer.
1087 * Use the best path for the given tunnel.
1088 * If the peer doesn't have any path to it yet, try to get one.
1089 * If the peer already has some path, send a CREATE PATH towards it.
1091 * @param peer PeerInfo of the peer.
1092 * @param t Tunnel for which to create the path, if possible.
1095 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1099 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1100 * needed rescources.
1101 * If peer already exists, reevaluate shortest path and change if different.
1103 * @param t Tunnel we want to add a new peer to
1104 * @param peer PeerInfo of the peer being added
1108 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1112 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1113 * that are no longer needed, as well as nodes of no longer reachable peers.
1114 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1116 * @param t Tunnel from which to remove the path.
1117 * @param peer Short id of the peer which should be removed.
1120 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1124 * Search for a tunnel by global ID using full PeerIdentities.
1126 * @param oid owner of the tunnel.
1127 * @param tid global tunnel number.
1129 * @return tunnel handler, NULL if doesn't exist.
1131 static struct MeshTunnel *
1132 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1136 * Delete an active client from the tunnel.
1142 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1145 * Notify a tunnel that a connection has broken that affects at least
1146 * some of its peers.
1148 * @param t Tunnel affected.
1149 * @param p1 Peer that got disconnected from p2.
1150 * @param p2 Peer that got disconnected from p1.
1152 * @return Short ID of the peer disconnected (either p1 or p2).
1153 * 0 if the tunnel remained unaffected.
1155 static GNUNET_PEER_Id
1156 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1161 * Get the current ack value for a tunnel, for data going from root to leaves,
1162 * taking in account the tunnel mode and the status of all children and clients.
1166 * @return Maximum PID allowed.
1169 tunnel_get_fwd_ack (struct MeshTunnel *t);
1173 * Add a client to a tunnel, initializing all needed data structures.
1175 * @param t Tunnel to which add the client.
1176 * @param c Client which to add to the tunnel.
1179 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1183 * Jump to the next edge, with the longest matching token.
1185 * @param block Block found in the DHT.
1186 * @param size Size of the block.
1187 * @param ctx Context of the search.
1189 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1192 regex_next_edge (const struct MeshRegexBlock *block,
1194 struct MeshRegexSearchContext *ctx);
1198 * Find a path to a peer that offers a regex servcie compatible
1199 * with a given string.
1201 * @param key The key of the accepting state.
1202 * @param ctx Context containing info about the string, tunnel, etc.
1205 regex_find_path (const struct GNUNET_HashCode *key,
1206 struct MeshRegexSearchContext *ctx);
1210 * @brief Queue and pass message to core when possible.
1212 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1213 * and accounts for it. In case the queue is full, the message is dropped and
1216 * Otherwise, message is treated as internal and allowed to go regardless of
1219 * @param cls Closure (@c type dependant). It will be used by queue_send to
1220 * build the message to be sent if not already prebuilt.
1221 * @param type Type of the message, 0 for a raw message.
1222 * @param size Size of the message.
1223 * @param dst Neighbor to send message to.
1224 * @param t Tunnel this message belongs to.
1227 queue_add (void *cls, uint16_t type, size_t size,
1228 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1232 * Free a transmission that was already queued with all resources
1233 * associated to the request.
1235 * @param queue Queue handler to cancel.
1236 * @param clear_cls Is it necessary to free associated cls?
1239 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1243 * @brief Get the next transmittable message from the queue.
1245 * This will be the head, except in the case of being a data packet
1246 * not allowed by the destination peer.
1248 * @param peer Destination peer.
1250 * @return The next viable MeshPeerQueue element to send to that peer.
1251 * NULL when there are no transmittable messages.
1253 struct MeshPeerQueue *
1254 queue_get_next (const struct MeshPeerInfo *peer);
1258 * Core callback to write a queued packet to core buffer
1260 * @param cls Closure (peer info).
1261 * @param size Number of bytes available in buf.
1262 * @param buf Where the to write the message.
1264 * @return number of bytes written to buf
1267 queue_send (void *cls, size_t size, void *buf);
1269 /******************************************************************************/
1270 /************************ ITERATORS ****************************/
1271 /******************************************************************************/
1274 * Iterator over found existing mesh regex blocks that match an ongoing search.
1276 * @param cls closure
1277 * @param key current key code
1278 * @param value value in the hash map
1279 * @return GNUNET_YES if we should continue to iterate,
1283 regex_result_iterator (void *cls,
1284 const struct GNUNET_HashCode * key,
1287 struct MeshRegexBlock *block = value;
1288 struct MeshRegexSearchContext *ctx = cls;
1290 if (GNUNET_YES == ntohl(block->accepting) &&
1291 ctx->position == strlen (ctx->info->description))
1293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1294 regex_find_path (key, ctx);
1295 return GNUNET_YES; // We found an accept state!
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1300 ctx->position, strlen(ctx->info->description),
1301 ntohl(block->accepting));
1304 regex_next_edge(block, SIZE_MAX, ctx);
1306 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1313 * Iterator over edges in a regex block retrieved from the DHT.
1315 * @param cls Closure (context of the search).
1316 * @param token Token that follows to next state.
1317 * @param len Lenght of token.
1318 * @param key Hash of next state.
1320 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1323 regex_edge_iterator (void *cls,
1326 const struct GNUNET_HashCode *key)
1328 struct MeshRegexSearchContext *ctx = cls;
1329 struct MeshRegexSearchInfo *info = ctx->info;
1333 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1338 current = &info->description[ctx->position];
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1340 current_len = strlen (info->description) - ctx->position;
1341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1345 if (len > current_len)
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1348 return GNUNET_YES; // Token too long, wont match
1350 if (0 != strncmp (current, token, len))
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1353 return GNUNET_YES; // Token doesn't match
1356 if (len > ctx->longest_match)
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1359 ctx->longest_match = len;
1364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1373 * Jump to the next edge, with the longest matching token.
1375 * @param block Block found in the DHT.
1376 * @param size Size of the block.
1377 * @param ctx Context of the search.
1379 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1382 regex_next_edge (const struct MeshRegexBlock *block,
1384 struct MeshRegexSearchContext *ctx)
1386 struct MeshRegexSearchContext *new_ctx;
1387 struct MeshRegexSearchInfo *info = ctx->info;
1388 struct GNUNET_DHT_GetHandle *get_h;
1392 /* Find the longest match for the current string position,
1393 * among tokens in the given block */
1394 ctx->longest_match = 0;
1395 result = GNUNET_MESH_regex_block_iterate (block, size,
1396 ®ex_edge_iterator, ctx);
1397 GNUNET_break (GNUNET_OK == result || SIZE_MAX == size);
1399 /* Did anything match? */
1400 if (0 == ctx->longest_match)
1403 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1404 new_ctx->info = info;
1405 new_ctx->position = ctx->position + ctx->longest_match;
1406 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1408 /* Check whether we already have a DHT GET running for it */
1410 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1413 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1415 ®ex_result_iterator,
1417 return; // We are already looking for it
1420 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1422 /* Start search in DHT */
1424 GNUNET_DHT_get_start (dht_handle, /* handle */
1425 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1426 &ctx->hash, /* key to search */
1427 dht_replication_level, /* replication level */
1428 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1429 NULL, /* xquery */ // FIXME BLOOMFILTER
1430 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1431 &dht_get_string_handler, new_ctx);
1433 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1436 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1445 * Iterator over hash map entries to cancel DHT GET requests after a
1446 * successful connect_by_string.
1448 * @param cls Closure (unused).
1449 * @param key Current key code (unused).
1450 * @param value Value in the hash map (get handle).
1451 * @return GNUNET_YES if we should continue to iterate,
1455 regex_cancel_dht_get (void *cls,
1456 const struct GNUNET_HashCode * key,
1459 struct GNUNET_DHT_GetHandle *h = value;
1461 GNUNET_DHT_get_stop (h);
1467 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1468 * search for connect_by_string.
1470 * @param cls Closure (unused).
1471 * @param key Current key code (unused).
1472 * @param value MeshRegexBlock in the hash map.
1473 * @return GNUNET_YES if we should continue to iterate,
1477 regex_free_result (void *cls,
1478 const struct GNUNET_HashCode * key,
1482 GNUNET_free (value);
1488 * Regex callback iterator to store own service description in the DHT.
1490 * @param cls closure.
1491 * @param key hash for current state.
1492 * @param proof proof for current state.
1493 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1494 * @param num_edges number of edges leaving current state.
1495 * @param edges edges leaving current state.
1498 regex_iterator (void *cls,
1499 const struct GNUNET_HashCode *key,
1502 unsigned int num_edges,
1503 const struct GNUNET_REGEX_Edge *edges)
1505 struct MeshRegexBlock *block;
1506 struct MeshRegexEdge *block_edge;
1507 enum GNUNET_DHT_RouteOption opt;
1511 unsigned int offset;
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 " regex dht put for state %s\n",
1517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1525 if (GNUNET_YES == accepting)
1527 struct MeshRegexAccept block;
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 " state %s is accepting, putting own id\n",
1532 size = sizeof (block);
1534 block.id = my_full_id;
1536 GNUNET_DHT_put(dht_handle, key,
1537 dht_replication_level,
1538 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1539 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1542 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1547 len = strlen(proof);
1548 size = sizeof (struct MeshRegexBlock) + len;
1549 block = GNUNET_malloc (size);
1552 block->n_proof = htonl (len);
1553 block->n_edges = htonl (num_edges);
1554 block->accepting = htonl (accepting);
1556 /* Store the proof at the end of the block. */
1557 aux = (char *) &block[1];
1558 memcpy (aux, proof, len);
1561 /* Store each edge in a variable length MeshEdge struct at the
1562 * very end of the MeshRegexBlock structure.
1564 for (i = 0; i < num_edges; i++)
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1567 " edge %s towards %s\n",
1569 GNUNET_h2s(&edges[i].destination));
1571 /* aux points at the end of the last block */
1572 len = strlen (edges[i].label);
1573 size += sizeof (struct MeshRegexEdge) + len;
1574 // Calculate offset FIXME is this ok? use size instead?
1575 offset = aux - (char *) block;
1576 block = GNUNET_realloc (block, size);
1577 aux = &((char *) block)[offset];
1578 block_edge = (struct MeshRegexEdge *) aux;
1579 block_edge->key = edges[i].destination;
1580 block_edge->n_token = htonl (len);
1581 aux = (char *) &block_edge[1];
1582 memcpy (aux, edges[i].label, len);
1586 GNUNET_DHT_put(dht_handle, key,
1587 dht_replication_level,
1589 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1591 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1595 GNUNET_free (block);
1600 * Store the regular expression describing a local service into the DHT.
1602 * @param regex The regular expresion.
1605 regex_put (const struct MeshRegexDescriptor *regex)
1607 struct GNUNET_REGEX_Automaton *dfa;
1609 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1610 dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1611 strlen(regex->regex),
1612 regex->compression);
1613 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1614 GNUNET_REGEX_automaton_destroy (dfa);
1615 DEBUG_DHT (" regex_put (%s) end\n", regex);
1620 * Find a path to a peer that offers a regex servcie compatible
1621 * with a given string.
1623 * @param key The key of the accepting state.
1624 * @param ctx Context containing info about the string, tunnel, etc.
1627 regex_find_path (const struct GNUNET_HashCode *key,
1628 struct MeshRegexSearchContext *ctx)
1630 struct GNUNET_DHT_GetHandle *get_h;
1632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1633 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1634 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1635 key, /* key to search */
1636 dht_replication_level, /* replication level */
1637 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1638 GNUNET_DHT_RO_RECORD_ROUTE,
1639 NULL, /* xquery */ // FIXME BLOOMFILTER
1640 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1641 &dht_get_string_accept_handler, ctx);
1642 GNUNET_break (GNUNET_OK ==
1643 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1646 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1651 * Function called if the connect attempt to a peer found via
1652 * connect_by_string times out. Try to connect to another peer, if any.
1653 * Otherwise try to reconnect to the same peer.
1655 * @param cls Closure (info about regex search).
1656 * @param tc TaskContext.
1659 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1661 struct MeshRegexSearchInfo *info = cls;
1662 struct MeshPeerInfo *peer_info;
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1667 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1668 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1676 if (0 < info->n_peers)
1678 // Select next peer, put current in that spot.
1679 id = info->peers[info->i_peer];
1680 info->peers[info->i_peer] = info->peer;
1681 info->i_peer = (info->i_peer + 1) % info->n_peers;
1685 // Try to connect to same peer again.
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1690 peer_info = peer_info_get_short(id);
1691 tunnel_add_peer (info->t, peer_info);
1693 tunnel_delete_peer (info->t, old);
1694 peer_info_connect (peer_info, info->t);
1695 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1696 ®ex_connect_timeout,
1698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1703 * Cancel an ongoing regex search in the DHT and free all resources.
1705 * @param ctx The search context.
1708 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1710 struct MeshRegexSearchInfo *info = ctx->info;
1713 GNUNET_free (info->description);
1714 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1715 ®ex_cancel_dht_get, NULL);
1716 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1717 ®ex_free_result, NULL);
1718 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1719 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1720 info->t->regex_ctx = NULL;
1721 for (i = 0; i < info->n_contexts; i++)
1723 GNUNET_free (info->contexts[i]);
1725 if (0 < info->n_contexts)
1726 GNUNET_free (info->contexts);
1727 if (0 < info->n_peers)
1728 GNUNET_free (info->peers);
1729 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1731 GNUNET_SCHEDULER_cancel(info->timeout);
1737 /******************************************************************************/
1738 /************************ PERIODIC FUNCTIONS ****************************/
1739 /******************************************************************************/
1742 * Announce iterator over for each application provided by the peer
1744 * @param cls closure
1745 * @param key current key code
1746 * @param value value in the hash map
1747 * @return GNUNET_YES if we should continue to
1752 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1754 struct PBlock block;
1755 struct MeshClient *c;
1757 block.id = my_full_id;
1758 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1759 GNUNET_assert(NULL != c);
1760 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1761 if (0 == block.type)
1766 block.type = htonl (block.type);
1768 GNUNET_break (NULL !=
1769 GNUNET_DHT_put (dht_handle, key,
1770 dht_replication_level,
1771 GNUNET_DHT_RO_RECORD_ROUTE |
1772 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1773 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1775 (const char *) &block,
1776 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1778 app_announce_time, NULL, NULL));
1784 * Periodically announce what applications are provided by local clients
1787 * @param cls closure
1788 * @param tc task context
1791 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1793 struct MeshClient *c = cls;
1796 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1797 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1802 DEBUG_DHT ("Starting PUT for regex\n");
1804 for (i = 0; i < c->n_regex; i++)
1806 regex_put (&c->regexes[i]);
1808 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1811 DEBUG_DHT ("Finished PUT for regex\n");
1818 * Periodically announce what applications are provided by local clients
1821 * @param cls closure
1822 * @param tc task context
1825 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1827 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1829 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1833 DEBUG_DHT ("Starting PUT for apps\n");
1835 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1837 announce_applications_task =
1838 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1840 DEBUG_DHT ("Finished PUT for apps\n");
1847 * Periodically announce self id in the DHT
1849 * @param cls closure
1850 * @param tc task context
1853 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1855 struct PBlock block;
1857 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1859 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1863 * - Set data expiration in function of X
1864 * - Adapt X to churn
1866 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1868 block.id = my_full_id;
1869 block.type = htonl (0);
1870 GNUNET_DHT_put (dht_handle, /* DHT handle */
1871 &my_full_id.hashPubKey, /* Key to use */
1872 dht_replication_level, /* Replication level */
1873 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1874 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1875 sizeof (block), /* Size of the data */
1876 (const char *) &block, /* Data itself */
1877 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1878 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1879 NULL, /* Continuation */
1880 NULL); /* Continuation closure */
1882 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1886 /******************************************************************************/
1887 /****************** GENERAL HELPER FUNCTIONS ************************/
1888 /******************************************************************************/
1891 * Decrements the reference counter and frees all resources if needed
1893 * @param mesh_data Data Descriptor used in a multicast message.
1894 * Freed no longer needed (last message).
1897 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1899 if (0 == --(mesh_data->reference_counter))
1901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1902 GNUNET_free (mesh_data->data);
1903 GNUNET_free (mesh_data);
1909 * Check if client has registered with the service and has not disconnected
1911 * @param client the client to check
1913 * @return non-NULL if client exists in the global DLL
1915 static struct MeshClient *
1916 client_get (struct GNUNET_SERVER_Client *client)
1918 struct MeshClient *c;
1923 if (c->handle == client)
1932 * Checks if a given client has subscribed to certain message type
1934 * @param message_type Type of message to check
1935 * @param c Client to check
1937 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1939 * FIXME: use of crypto_hash slows it down
1940 * The hash function alone takes 8-10us out of the ~55us for the whole
1941 * process of retransmitting the message from one local client to another.
1942 * Find faster implementation!
1945 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1947 struct GNUNET_HashCode hc;
1949 if (NULL == c->types)
1952 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1953 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1958 * Check whether client wants traffic from a tunnel.
1960 * @param c Client to check.
1961 * @param t Tunnel to be found.
1963 * @return GNUNET_YES if client knows tunnel.
1965 * TODO look in client hashmap
1968 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1972 for (i = 0; i < t->nclients; i++)
1973 if (t->clients[i] == c)
1980 * Check whether client has been informed about a tunnel.
1982 * @param c Client to check.
1983 * @param t Tunnel to be found.
1985 * @return GNUNET_YES if client knows tunnel.
1987 * TODO look in client hashmap
1990 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1994 for (i = 0; i < t->nignore; i++)
1995 if (t->ignore[i] == c)
1997 return client_wants_tunnel(c, t);
2002 * Marks a client as uninterested in traffic from the tunnel, updating both
2003 * client and tunnel to reflect this.
2005 * @param c Client that doesn't want traffic anymore.
2006 * @param t Tunnel which should be ignored.
2008 * FIXME when to delete an incoming tunnel?
2011 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2013 struct GNUNET_HashCode hash;
2015 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2016 GNUNET_break (GNUNET_YES ==
2017 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2019 GNUNET_break (GNUNET_YES ==
2020 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2021 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2022 tunnel_delete_active_client (t, c);
2023 GNUNET_array_append (t->ignore, t->nignore, c);
2028 * Deletes a tunnel from a client (either owner or destination). To be used on
2029 * tunnel destroy, otherwise, use client_ignore_tunnel.
2031 * @param c Client whose tunnel to delete.
2032 * @param t Tunnel which should be deleted.
2035 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2037 struct GNUNET_HashCode hash;
2041 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2042 GNUNET_assert (GNUNET_YES ==
2043 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2049 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2051 GNUNET_assert (GNUNET_YES ==
2052 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2056 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2064 * Send the message to all clients that have subscribed to its type
2066 * @param msg Pointer to the message itself
2067 * @param payload Pointer to the payload of the message.
2068 * @param t The tunnel to whose clients this message goes.
2070 * @return number of clients this message was sent to
2073 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2074 const struct GNUNET_MessageHeader *payload,
2075 struct MeshTunnel *t)
2077 struct MeshClient *c;
2078 MESH_TunnelNumber *tid;
2081 char cbuf[htons (msg->size)];
2083 type = ntohs (payload->type);
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2086 GNUNET_MESH_DEBUG_M2S (type));
2088 memcpy (cbuf, msg, sizeof (cbuf));
2089 switch (htons (msg->type))
2091 struct GNUNET_MESH_Unicast *uc;
2092 struct GNUNET_MESH_Multicast *mc;
2093 struct GNUNET_MESH_ToOrigin *to;
2095 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2096 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2099 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2100 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2103 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2104 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2112 for (count = 0, c = clients; c != NULL; c = c->next)
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2115 if (client_is_subscribed (type, c))
2117 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2121 *tid = htonl (t->local_tid);
2125 if (GNUNET_NO == client_knows_tunnel (c, t))
2127 /* This client doesn't know the tunnel */
2128 struct GNUNET_MESH_TunnelNotification tmsg;
2129 struct GNUNET_HashCode hash;
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2132 tmsg.header.size = htons (sizeof (tmsg));
2133 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2134 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2135 tmsg.tunnel_id = htonl (t->local_tid_dest);
2137 if (GNUNET_YES == t->speed_min)
2138 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2139 if (GNUNET_YES == t->nobuffer)
2140 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2141 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2142 &tmsg.header, GNUNET_NO);
2143 tunnel_add_client (t, c);
2144 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2146 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2147 c->incoming_tunnels, &hash, t,
2148 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2150 *tid = htonl (t->local_tid_dest);
2153 /* Check if the client wants to get traffic from the tunnel */
2154 if (GNUNET_NO == client_wants_tunnel(c, t))
2157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2158 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2159 (struct GNUNET_MessageHeader
2160 *) cbuf, GNUNET_NO);
2169 * Notify the client that owns the tunnel that a peer has connected to it
2170 * (the requested path to it has been confirmed).
2172 * @param t Tunnel whose owner to notify
2173 * @param id Short id of the peer that has connected
2176 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2178 struct GNUNET_MESH_PeerControl pc;
2180 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2181 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2182 pc.tunnel_id = htonl (t->local_tid);
2183 GNUNET_PEER_resolve (id, &pc.peer);
2184 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2190 * Notify all clients (not depending on registration status) that the incoming
2191 * tunnel is no longer valid.
2193 * @param t Tunnel that was destroyed.
2196 send_clients_tunnel_destroy (struct MeshTunnel *t)
2198 struct GNUNET_MESH_TunnelMessage msg;
2200 msg.header.size = htons (sizeof (msg));
2201 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2202 msg.tunnel_id = htonl (t->local_tid_dest);
2203 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2208 * Notify clients of tunnel disconnections, if needed.
2209 * In case the origin disconnects, the destination clients get a tunnel destroy
2210 * notification. If the last destination disconnects (only one remaining client
2211 * in tunnel), the origin gets a (local ID) peer disconnected.
2212 * Note that the function must be called BEFORE removing the client from
2215 * @param t Tunnel that was destroyed.
2216 * @param c Client that disconnected.
2219 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2225 struct GNUNET_MESH_TunnelMessage msg;
2227 msg.header.size = htons (sizeof (msg));
2228 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2229 msg.tunnel_id = htonl (t->local_tid_dest);
2230 for (i = 0; i < t->nclients; i++)
2231 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2232 &msg.header, GNUNET_NO);
2234 // FIXME when to disconnect an incoming tunnel?
2235 else if (1 == t->nclients && NULL != t->owner)
2237 struct GNUNET_MESH_PeerControl msg;
2239 msg.header.size = htons (sizeof (msg));
2240 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2241 msg.tunnel_id = htonl (t->local_tid);
2242 msg.peer = my_full_id;
2243 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2244 &msg.header, GNUNET_NO);
2250 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2251 * and insert it in the appropiate structures if the peer is not known yet.
2253 * @param peer Full identity of the peer.
2255 * @return Existing or newly created peer info.
2257 static struct MeshPeerInfo *
2258 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2260 struct MeshPeerInfo *peer_info;
2262 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2263 if (NULL == peer_info)
2266 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2267 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2268 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2269 peer_info->id = GNUNET_PEER_intern (peer);
2277 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2278 * and insert it in the appropiate structures if the peer is not known yet.
2280 * @param peer Short identity of the peer.
2282 * @return Existing or newly created peer info.
2284 static struct MeshPeerInfo *
2285 peer_info_get_short (const GNUNET_PEER_Id peer)
2287 struct GNUNET_PeerIdentity id;
2289 GNUNET_PEER_resolve (peer, &id);
2290 return peer_info_get (&id);
2295 * Iterator to remove the tunnel from the list of tunnels a peer participates
2298 * @param cls Closure (tunnel info)
2299 * @param key GNUNET_PeerIdentity of the peer (unused)
2300 * @param value PeerInfo of the peer
2302 * @return always GNUNET_YES, to keep iterating
2305 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2307 struct MeshTunnel *t = cls;
2308 struct MeshPeerInfo *peer = value;
2311 for (i = 0; i < peer->ntunnels; i++)
2314 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2317 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2318 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2327 * Core callback to write a pre-constructed data packet to core buffer
2329 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2330 * @param size Number of bytes available in buf.
2331 * @param buf Where the to write the message.
2333 * @return number of bytes written to buf
2336 send_core_data_raw (void *cls, size_t size, void *buf)
2338 struct MeshTransmissionDescriptor *info = cls;
2339 struct GNUNET_MessageHeader *msg;
2342 GNUNET_assert (NULL != info);
2343 GNUNET_assert (NULL != info->mesh_data);
2344 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2345 total_size = ntohs (msg->size);
2347 if (total_size > size)
2352 memcpy (buf, msg, total_size);
2353 data_descriptor_decrement_rc (info->mesh_data);
2360 * Sends an already built non-multicast message to a peer,
2361 * properly registrating all used resources.
2363 * @param message Message to send. Function makes a copy of it.
2364 * @param peer Short ID of the neighbor whom to send the message.
2365 * @param t Tunnel on which this message is transmitted.
2368 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2369 const struct GNUNET_PeerIdentity *peer,
2370 struct MeshTunnel *t)
2372 struct MeshTransmissionDescriptor *info;
2373 struct MeshPeerInfo *neighbor;
2374 struct MeshPeerPath *p;
2378 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2380 size = ntohs (message->size);
2381 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2382 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2383 info->mesh_data->data = GNUNET_malloc (size);
2384 memcpy (info->mesh_data->data, message, size);
2385 type = ntohs(message->type);
2388 struct GNUNET_MESH_Unicast *m;
2389 struct GNUNET_MESH_ToOrigin *to;
2391 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2392 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2393 m->ttl = htonl (ntohl (m->ttl) - 1);
2395 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2396 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2398 to->pid = htonl(t->bck_pid);
2400 info->mesh_data->data_len = size;
2401 info->mesh_data->reference_counter = 1;
2402 info->mesh_data->total_out = 1;
2403 neighbor = peer_info_get (peer);
2404 for (p = neighbor->path_head; NULL != p; p = p->next)
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2415 " %s IS NOT DIRECTLY CONNECTED\n",
2417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2420 for (p = neighbor->path_head; NULL != p; p = p->next)
2422 struct GNUNET_PeerIdentity debug_id;
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 " path with %u hops through:\n",
2428 for (i = 0; i < p->length; i++)
2430 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2433 i, GNUNET_i2s(&debug_id));
2437 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2438 GNUNET_free (info->mesh_data->data);
2439 GNUNET_free (info->mesh_data);
2443 info->peer = neighbor;
2444 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2455 * Sends a CREATE PATH message for a path to a peer, properly registrating
2456 * all used resources.
2458 * @param peer PeerInfo of the final peer for whom this path is being created.
2459 * @param p Path itself.
2460 * @param t Tunnel for which the path is created.
2463 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2464 struct MeshTunnel *t)
2466 struct GNUNET_PeerIdentity id;
2467 struct MeshPathInfo *path_info;
2468 struct MeshPeerInfo *neighbor;
2474 p = tree_get_path_to_peer (t->tree, peer->id);
2481 for (i = 0; i < p->length; i++)
2483 if (p->peers[i] == myid)
2486 if (i >= p->length - 1)
2492 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2494 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2495 path_info->path = p;
2497 neighbor = peer_info_get (&id);
2498 path_info->peer = neighbor;
2499 queue_add (path_info,
2500 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2501 sizeof (struct GNUNET_MESH_ManipulatePath) +
2502 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2509 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2511 * @param t Tunnel whose path to destroy.
2512 * @param destination Short ID of the peer to whom the path to destroy.
2515 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2517 struct MeshPeerPath *p;
2520 p = tree_get_path_to_peer (t->tree, destination);
2526 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2527 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2529 struct GNUNET_MESH_ManipulatePath *msg;
2530 struct GNUNET_PeerIdentity *pi;
2534 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2535 msg->header.size = htons (size);
2536 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2537 msg->tid = htonl (t->id.tid);
2538 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2539 for (i = 0; i < p->length; i++)
2541 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2543 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2550 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2552 * @param t Tunnel which to confirm.
2555 send_path_ack (struct MeshTunnel *t)
2557 struct MeshTransmissionDescriptor *info;
2558 struct GNUNET_PeerIdentity id;
2559 GNUNET_PEER_Id peer;
2561 peer = tree_get_predecessor (t->tree);
2562 GNUNET_PEER_resolve (peer, &id);
2563 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2564 info->origin = &t->id;
2565 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2566 GNUNET_assert (NULL != info->peer);
2569 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2570 sizeof (struct GNUNET_MESH_PathACK),
2577 * Try to establish a new connection to this peer.
2578 * Use the best path for the given tunnel.
2579 * If the peer doesn't have any path to it yet, try to get one.
2580 * If the peer already has some path, send a CREATE PATH towards it.
2582 * @param peer PeerInfo of the peer.
2583 * @param t Tunnel for which to create the path, if possible.
2586 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2588 struct MeshPeerPath *p;
2589 struct MeshPathInfo *path_info;
2591 if (NULL != peer->path_head)
2593 p = tree_get_path_to_peer (t->tree, peer->id);
2600 // FIXME always send create path to self
2603 send_create_path (peer, p, t);
2607 struct GNUNET_HashCode hash;
2610 send_client_peer_connected (t, myid);
2611 t->local_tid_dest = next_local_tid++;
2612 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2615 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2616 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2623 else if (NULL == peer->dhtget)
2625 struct GNUNET_PeerIdentity id;
2627 GNUNET_PEER_resolve (peer->id, &id);
2628 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2629 path_info->peer = peer;
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2633 peer->dhtgetcls = path_info;
2634 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2635 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2636 &id.hashPubKey, /* key to search */
2637 dht_replication_level, /* replication level */
2638 GNUNET_DHT_RO_RECORD_ROUTE |
2639 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2640 NULL, /* xquery */ // FIXME BLOOMFILTER
2641 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2642 &dht_get_id_handler, path_info);
2644 /* Otherwise, there is no path but the DHT get is already started. */
2649 * Task to delay the connection of a peer
2651 * @param cls Closure (path info with tunnel and peer to connect).
2652 * Will be free'd on exection.
2653 * @param tc TaskContext
2656 peer_info_connect_task (void *cls,
2657 const struct GNUNET_SCHEDULER_TaskContext *tc)
2659 struct MeshPathInfo *path_info = cls;
2661 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2663 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2668 peer_info_connect (path_info->peer, path_info->t);
2674 * Destroy the peer_info and free any allocated resources linked to it
2676 * @param pi The peer_info to destroy.
2678 * @return GNUNET_OK on success
2681 peer_info_destroy (struct MeshPeerInfo *pi)
2683 struct GNUNET_PeerIdentity id;
2684 struct MeshPeerPath *p;
2685 struct MeshPeerPath *nextp;
2687 GNUNET_PEER_resolve (pi->id, &id);
2688 GNUNET_PEER_change_rc (pi->id, -1);
2691 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2694 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2695 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2697 if (NULL != pi->dhtget)
2699 GNUNET_DHT_get_stop (pi->dhtget);
2700 GNUNET_free (pi->dhtgetcls);
2706 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2710 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2712 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2720 * Remove all paths that rely on a direct connection between p1 and p2
2721 * from the peer itself and notify all tunnels about it.
2723 * @param peer PeerInfo of affected peer.
2724 * @param p1 GNUNET_PEER_Id of one peer.
2725 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2728 * TODO: optimize (see below)
2731 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2734 struct MeshPeerPath *p;
2735 struct MeshPeerPath *aux;
2736 struct MeshPeerInfo *peer_d;
2738 unsigned int destroyed;
2743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2745 p = peer->path_head;
2749 for (i = 0; i < (p->length - 1); i++)
2751 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2752 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2754 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2765 for (i = 0; i < peer->ntunnels; i++)
2767 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2771 * Problem: one or more peers have been deleted from the tunnel tree.
2772 * We don't know who they are to try to add them again.
2773 * We need to try to find a new path for each of the disconnected peers.
2774 * Some of them might already have a path to reach them that does not
2775 * involve p1 and p2. Adding all anew might render in a better tree than
2776 * the trivial immediate fix.
2778 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2779 * its children will be reachable trough him.
2781 peer_d = peer_info_get_short (d);
2784 for (p = peer_d->path_head; NULL != p; p = p->next)
2786 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2794 /* No callback, as peer will be already disconnected and a connection
2795 * scheduled by tunnel_notify_connection_broken.
2797 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2801 peer_info_connect (peer_d, peer->tunnels[i]);
2804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2809 * Add the path to the peer and update the path used to reach it in case this
2812 * @param peer_info Destination peer to add the path to.
2813 * @param path New path to add. Last peer must be the peer in arg 1.
2814 * Path will be either used of freed if already known.
2815 * @param trusted Do we trust that this path is real?
2818 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2821 struct MeshPeerPath *aux;
2825 if ((NULL == peer_info) || (NULL == path))
2828 path_destroy (path);
2831 if (path->peers[path->length - 1] != peer_info->id)
2834 path_destroy (path);
2837 if (path->length <= 2 && GNUNET_NO == trusted)
2839 /* Only allow CORE to tell us about direct paths */
2840 path_destroy (path);
2843 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2844 for (l = 1; l < path->length; l++)
2846 if (path->peers[l] == myid)
2848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2849 for (l2 = 0; l2 < path->length - l; l2++)
2851 path->peers[l2] = path->peers[l + l2];
2856 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2861 struct GNUNET_PeerIdentity id;
2863 GNUNET_PEER_resolve (peer_info->id, &id);
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2865 path->length, GNUNET_i2s (&id));
2868 l = path_get_length (path);
2875 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2876 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2878 l2 = path_get_length (aux);
2881 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2882 peer_info->path_tail, aux, path);
2887 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2889 path_destroy (path);
2894 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2901 * Add the path to the origin peer and update the path used to reach it in case
2902 * this is the shortest.
2903 * The path is given in peer_info -> destination, therefore we turn the path
2904 * upside down first.
2906 * @param peer_info Peer to add the path to, being the origin of the path.
2907 * @param path New path to add after being inversed.
2908 * @param trusted Do we trust that this path is real?
2911 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2912 struct MeshPeerPath *path, int trusted)
2915 peer_info_add_path (peer_info, path, trusted);
2920 * Function called if the connection to the peer has been stalled for a while,
2921 * possibly due to a missed ACK. Poll the peer about its ACK status.
2923 * @param cls Closure (info about regex search).
2924 * @param tc TaskContext.
2927 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2929 struct MeshTunnelChildInfo *cinfo = cls;
2930 struct GNUNET_MESH_Poll msg;
2931 struct GNUNET_PeerIdentity id;
2932 struct MeshTunnel *t;
2934 return; // FIXME fc activate
2935 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2936 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2942 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2943 msg.header.size = htons (sizeof (msg));
2944 msg.tid = htonl (t->id.tid);
2945 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2946 msg.last_ack = htonl (cinfo->fwd_ack);
2948 GNUNET_PEER_resolve (tree_get_predecessor(cinfo->t->tree), &id);
2949 send_prebuilt_message (&msg.header, &id, cinfo->t);
2950 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
2951 &tunnel_poll, cinfo);
2956 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2957 * to obtain a local peer -> destination path and interning the peer ids.
2959 * @return Newly allocated and created path
2961 static struct MeshPeerPath *
2962 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2963 unsigned int get_path_length,
2964 const struct GNUNET_PeerIdentity *put_path,
2965 unsigned int put_path_length)
2967 struct MeshPeerPath *p;
2973 GNUNET_PEER_change_rc (myid, 1);
2974 i = get_path_length;
2975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2976 for (i--; i >= 0; i--)
2978 id = GNUNET_PEER_intern (&get_path[i]);
2979 if (p->length > 0 && id == p->peers[p->length - 1])
2981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2982 GNUNET_PEER_change_rc (id, -1);
2986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2987 GNUNET_i2s (&get_path[i]));
2989 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2990 p->peers[p->length - 1] = id;
2993 i = put_path_length;
2994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2995 for (i--; i >= 0; i--)
2997 id = GNUNET_PEER_intern (&put_path[i]);
3000 /* PUT path went through us, so discard the path up until now and start
3001 * from here to get a much shorter (and loop-free) path.
3006 if (p->length > 0 && id == p->peers[p->length - 1])
3008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3009 GNUNET_PEER_change_rc (id, -1);
3013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3014 GNUNET_i2s (&put_path[i]));
3016 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3017 p->peers[p->length - 1] = id;
3021 if (get_path_length > 0)
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3023 GNUNET_i2s (&get_path[0]));
3024 if (put_path_length > 0)
3025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3026 GNUNET_i2s (&put_path[0]));
3027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3029 for (i = 0; i < p->length; i++)
3031 struct GNUNET_PeerIdentity peer_id;
3033 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3035 GNUNET_i2s (&peer_id));
3043 * Adds a path to the peer_infos of all the peers in the path
3045 * @param p Path to process.
3046 * @param confirmed Whether we know if the path works or not.
3049 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3053 /* TODO: invert and add */
3054 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3055 for (i++; i < p->length; i++)
3057 struct MeshPeerInfo *aux;
3058 struct MeshPeerPath *copy;
3060 aux = peer_info_get_short (p->peers[i]);
3061 copy = path_duplicate (p);
3062 copy->length = i + 1;
3063 peer_info_add_path (aux, copy, GNUNET_NO);
3069 * Send keepalive packets for a peer
3071 * @param cls Closure (tunnel for which to send the keepalive).
3072 * @param tc Notification context.
3074 * TODO: implement explicit multicast keepalive?
3077 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3081 * Search for a tunnel among the incoming tunnels
3083 * @param tid the local id of the tunnel
3085 * @return tunnel handler, NULL if doesn't exist
3087 static struct MeshTunnel *
3088 tunnel_get_incoming (MESH_TunnelNumber tid)
3090 struct GNUNET_HashCode hash;
3092 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3093 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3094 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3099 * Search for a tunnel among the tunnels for a client
3101 * @param c the client whose tunnels to search in
3102 * @param tid the local id of the tunnel
3104 * @return tunnel handler, NULL if doesn't exist
3106 static struct MeshTunnel *
3107 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3109 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3111 return tunnel_get_incoming (tid);
3115 struct GNUNET_HashCode hash;
3117 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3118 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3124 * Search for a tunnel by global ID using PEER_ID
3126 * @param pi owner of the tunnel
3127 * @param tid global tunnel number
3129 * @return tunnel handler, NULL if doesn't exist
3131 static struct MeshTunnel *
3132 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3134 struct MESH_TunnelID id;
3135 struct GNUNET_HashCode hash;
3140 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3141 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3146 * Search for a tunnel by global ID using full PeerIdentities
3148 * @param oid owner of the tunnel
3149 * @param tid global tunnel number
3151 * @return tunnel handler, NULL if doesn't exist
3153 static struct MeshTunnel *
3154 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3156 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3161 * Delete an active client from the tunnel.
3167 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3171 for (i = 0; i < t->nclients; i++)
3173 if (t->clients[i] == c)
3175 t->clients[i] = t->clients[t->nclients - 1];
3176 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3177 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3179 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3187 * Delete an ignored client from the tunnel.
3193 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3197 for (i = 0; i < t->nignore; i++)
3199 if (t->ignore[i] == c)
3201 t->ignore[i] = t->ignore[t->nignore - 1];
3202 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3210 * Delete a client from the tunnel. It should be only done on
3211 * client disconnection, otherwise use client_ignore_tunnel.
3217 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3219 tunnel_delete_ignored_client (t, c);
3220 tunnel_delete_active_client (t, c);
3225 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3227 * Destroys queue elements of all waiting transmissions and frees all memory
3228 * used by the struct and its elements.
3230 * @param cls Closure (tunnel info).
3231 * @param key Hash of GNUNET_PEER_Id (unused).
3232 * @param value MeshTunnelChildInfo of the child.
3234 * @return always GNUNET_YES, to keep iterating
3237 tunnel_destroy_child (void *cls,
3238 const struct GNUNET_HashCode * key,
3241 struct MeshTunnelChildInfo *cinfo = value;
3242 struct MeshTunnel *t = cls;
3243 struct MeshPeerQueue *q;
3247 for (c = 0; c < cinfo->send_buffer_n; c++)
3249 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3250 q = cinfo->send_buffer[i];
3251 cinfo->send_buffer[i] = NULL;
3253 queue_destroy (q, GNUNET_YES);
3256 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3258 GNUNET_free_non_null (cinfo->send_buffer);
3259 GNUNET_free (cinfo);
3265 * Callback used to notify a client owner of a tunnel that a peer has
3266 * disconnected, most likely because of a path change.
3268 * @param cls Closure (tunnel this notification is about).
3269 * @param peer_id Short ID of disconnected peer.
3272 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3274 struct MeshTunnel *t = cls;
3275 struct MeshPeerInfo *peer;
3276 struct MeshPathInfo *path_info;
3278 if (NULL != t->owner && NULL != nc)
3280 struct GNUNET_MESH_PeerControl msg;
3282 msg.header.size = htons (sizeof (msg));
3283 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3284 msg.tunnel_id = htonl (t->local_tid);
3285 GNUNET_PEER_resolve (peer_id, &msg.peer);
3286 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3287 &msg.header, GNUNET_NO);
3289 peer = peer_info_get_short (peer_id);
3290 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3291 path_info->peer = peer;
3293 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3299 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3300 * needed rescources.
3301 * If peer already exists, reevaluate shortest path and change if different.
3303 * @param t Tunnel we want to add a new peer to
3304 * @param peer PeerInfo of the peer being added
3308 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3310 struct GNUNET_PeerIdentity id;
3311 struct MeshPeerPath *best_p;
3312 struct MeshPeerPath *p;
3313 unsigned int best_cost;
3316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3317 GNUNET_PEER_resolve (peer->id, &id);
3319 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3322 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3323 GNUNET_assert (GNUNET_OK ==
3324 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3326 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3329 if (NULL != (p = peer->path_head))
3332 best_cost = tree_get_path_cost (t->tree, p);
3335 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3342 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3343 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3344 t->path_refresh_task =
3345 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3349 /* Start a DHT get */
3350 peer_info_connect (peer, t);
3352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3356 * Add a path to a tunnel which we don't own, just to remember the next hop.
3357 * If destination node was already in the tunnel, the first hop information
3358 * will be replaced with the new path.
3360 * @param t Tunnel we want to add a new peer to
3361 * @param p Path to add
3362 * @param own_pos Position of local node in path.
3366 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3367 unsigned int own_pos)
3369 struct GNUNET_PeerIdentity id;
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3372 GNUNET_assert (0 != own_pos);
3373 tree_add_path (t->tree, p, NULL, NULL);
3374 if (own_pos < p->length - 1)
3376 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3377 tree_update_first_hops (t->tree, myid, &id);
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3383 * Add a client to a tunnel, initializing all needed data structures.
3385 * @param t Tunnel to which add the client.
3386 * @param c Client which to add to the tunnel.
3389 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3391 struct MeshTunnelClientInfo clinfo;
3393 GNUNET_array_append (t->clients, t->nclients, c);
3394 clinfo.fwd_ack = t->fwd_pid + 1;
3395 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3396 clinfo.fwd_pid = t->fwd_pid;
3397 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3399 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3404 * Notifies a tunnel that a connection has broken that affects at least
3405 * some of its peers. Sends a notification towards the root of the tree.
3406 * In case the peer is the owner of the tree, notifies the client that owns
3407 * the tunnel and tries to reconnect.
3409 * @param t Tunnel affected.
3410 * @param p1 Peer that got disconnected from p2.
3411 * @param p2 Peer that got disconnected from p1.
3413 * @return Short ID of the peer disconnected (either p1 or p2).
3414 * 0 if the tunnel remained unaffected.
3416 static GNUNET_PEER_Id
3417 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3423 tree_notify_connection_broken (t->tree, p1, p2,
3424 &tunnel_notify_client_peer_disconnected,
3426 if (myid != p1 && myid != p2)
3432 if (tree_get_predecessor (t->tree) != 0)
3434 /* We are the peer still connected, notify owner of the disconnection. */
3435 struct GNUNET_MESH_PathBroken msg;
3436 struct GNUNET_PeerIdentity neighbor;
3438 msg.header.size = htons (sizeof (msg));
3439 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3440 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3441 msg.tid = htonl (t->id.tid);
3442 msg.peer1 = my_full_id;
3443 GNUNET_PEER_resolve (pid, &msg.peer2);
3444 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3445 send_prebuilt_message (&msg.header, &neighbor, t);
3453 * Send a multicast packet to a neighbor.
3455 * @param cls Closure (Info about the multicast packet)
3456 * @param neighbor_id Short ID of the neighbor to send the packet to.
3459 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3461 struct MeshData *mdata = cls;
3462 struct MeshTransmissionDescriptor *info;
3463 struct GNUNET_PeerIdentity neighbor;
3464 struct GNUNET_MessageHeader *msg;
3466 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3468 info->mesh_data = mdata;
3469 (mdata->reference_counter) ++;
3470 info->destination = neighbor_id;
3471 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3473 GNUNET_i2s (&neighbor));
3474 info->peer = peer_info_get (&neighbor);
3475 GNUNET_assert (NULL != info->peer);
3476 msg = (struct GNUNET_MessageHeader *) mdata->data;
3479 info->mesh_data->data_len,
3486 * Queue a message in a tunnel in multicast, sending a copy to each child node
3487 * down the local one in the tunnel tree.
3489 * @param t Tunnel in which to send the data.
3490 * @param msg Message to be sent.
3493 tunnel_send_multicast (struct MeshTunnel *t,
3494 const struct GNUNET_MessageHeader *msg)
3496 struct MeshData *mdata;
3498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3499 " sending a multicast packet...\n");
3501 mdata = GNUNET_malloc (sizeof (struct MeshData));
3502 mdata->data_len = ntohs (msg->size);
3504 mdata->data = GNUNET_malloc (mdata->data_len);
3505 memcpy (mdata->data, msg, mdata->data_len);
3506 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3508 struct GNUNET_MESH_Multicast *mcast;
3510 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3511 if (t->fwd_queue_n >= t->fwd_queue_max)
3514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3515 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3516 " message from %s!\n",
3517 GNUNET_i2s(&mcast->oid));
3518 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3519 " message at %s!\n",
3520 GNUNET_i2s(&my_full_id));
3521 GNUNET_free (mdata->data);
3522 GNUNET_free (mdata);
3526 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3528 ntohl (mcast->ttl));
3532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3535 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3536 if (mdata->reference_counter == 0)
3538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3539 " no one to send data to\n");
3540 GNUNET_free (mdata->data);
3541 GNUNET_free (mdata);
3546 mdata->total_out = mdata->reference_counter;
3548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3549 " sending a multicast packet done\n");
3555 * Increase the SKIP value of all peers that
3556 * have not received a unicast message.
3558 * @param cls Closure (ID of the peer that HAS received the message).
3559 * @param key ID of the neighbor.
3560 * @param value Information about the neighbor.
3562 * @return GNUNET_YES to keep iterating.
3565 tunnel_add_skip (void *cls,
3566 const struct GNUNET_HashCode * key,
3569 struct GNUNET_PeerIdentity *neighbor = cls;
3570 struct MeshTunnelChildInfo *cinfo = value;
3572 /* TODO compare only pointers? key == neighbor? */
3573 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3583 * @brief Get neighbor's Flow Control information.
3585 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3586 * descendant of the local node in a certain tunnel.
3587 * If the info is not yet there (recently created path), creates the data struct
3588 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3591 * @param t Tunnel related.
3592 * @param peer Neighbor whose Flow Control info is needed.
3594 * @return Neighbor's Flow Control info.
3596 static struct MeshTunnelChildInfo *
3597 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3598 const struct GNUNET_PeerIdentity *peer)
3600 struct MeshTunnelChildInfo *cinfo;
3602 if (NULL == t->children_fc)
3605 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3611 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3612 cinfo->id = GNUNET_PEER_intern (peer);
3613 cinfo->skip = t->fwd_pid;
3616 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3617 cinfo->fwd_ack = t->fwd_pid + delta;
3618 cinfo->bck_ack = delta;
3619 cinfo->bck_pid = -1;
3621 cinfo->send_buffer =
3622 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3624 GNUNET_assert (GNUNET_OK ==
3625 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3628 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3635 * Get the Flow Control info of a client.
3637 * @param t Tunnel on which to look.
3638 * @param c Client whose ACK to get.
3640 * @return ACK value.
3642 static struct MeshTunnelClientInfo *
3643 tunnel_get_client_fc (struct MeshTunnel *t,
3644 struct MeshClient *c)
3648 for (i = 0; i < t->nclients; i++)
3650 if (t->clients[i] != c)
3652 return &t->clients_fc[i];
3655 return NULL; // avoid compiler / coverity complaints
3660 * Iterator to get the appropiate ACK value from all children nodes.
3662 * @param cls Closue (tunnel).
3663 * @param id Id of the child node.
3666 tunnel_get_child_fwd_ack (void *cls,
3669 struct GNUNET_PeerIdentity peer_id;
3670 struct MeshTunnelChildInfo *cinfo;
3671 struct MeshTunnelChildIteratorContext *ctx = cls;
3672 struct MeshTunnel *t = ctx->t;
3675 GNUNET_PEER_resolve (id, &peer_id);
3676 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3677 ack = cinfo->fwd_ack;
3680 if (GNUNET_NO == ctx->init)
3682 ctx->max_child_ack = ack;
3683 ctx->init = GNUNET_YES;
3686 if (GNUNET_YES == t->speed_min)
3688 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3692 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3699 * Get the maximum PID allowed to transmit to any
3700 * tunnel child of the local peer, depending on the tunnel
3701 * buffering/speed settings.
3705 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3708 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3710 struct MeshTunnelChildIteratorContext ctx;
3712 ctx.max_child_ack = 0;
3714 ctx.init = GNUNET_NO;
3715 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3717 if (0 == ctx.nchildren)
3719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3720 " tunnel has no children, no FWD ACK\n");
3724 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3725 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3727 return (int64_t) ctx.max_child_ack;
3732 * Set the FWD ACK value of a client in a particular tunnel.
3734 * @param t Tunnel affected.
3735 * @param c Client whose ACK to set.
3736 * @param ack ACK value.
3739 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3740 struct MeshClient *c,
3745 for (i = 0; i < t->nclients; i++)
3747 if (t->clients[i] != c)
3749 t->clients_fc[i].fwd_ack = ack;
3757 * Get the highest ACK value of all clients in a particular tunnel,
3758 * according to the buffering/speed settings.
3760 * @param t Tunnel on which to look.
3762 * @return Corresponding ACK value (max uint32_t).
3763 * If no clients are suscribed, -1LL.
3766 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3771 if (0 == t->nclients)
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3774 " tunnel has no clients, no FWD ACK\n");
3778 for (ack = -1LL, i = 0; i < t->nclients; i++)
3781 (GNUNET_YES == t->speed_min &&
3782 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3783 (GNUNET_NO == t->speed_min &&
3784 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3786 ack = t->clients_fc[i].fwd_ack;
3790 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3791 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3793 return (uint32_t) ack;
3798 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3799 * mode and the status of all children nodes.
3803 * @return Maximum PID allowed.
3806 tunnel_get_fwd_ack (struct MeshTunnel *t)
3810 uint32_t buffer_free;
3814 count = t->fwd_pid - t->skip;
3815 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3816 child_ack = tunnel_get_children_fwd_ack (t);
3817 client_ack = tunnel_get_clients_fwd_ack (t);
3818 if (GNUNET_YES == t->nobuffer)
3821 if (-1LL == child_ack)
3822 child_ack = client_ack;
3823 if (-1LL == child_ack)
3826 client_ack = child_ack = ack;
3831 ack = count + buffer_free; // Overflow? OK!
3833 if (-1LL == child_ack)
3835 // Node has no children, child_ack AND core buffer are irrelevant.
3836 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3837 return (uint32_t) client_ack;
3839 if (-1LL == client_ack)
3843 if (GNUNET_YES == t->speed_min)
3845 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3846 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3850 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3851 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3854 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3855 count, buffer_free, child_ack, client_ack, ack);
3861 * Build a local ACK message and send it to a local client.
3863 * @param t Tunnel on which to send the ACK.
3864 * @param c Client to whom send the ACK.
3865 * @param ack Value of the ACK.
3868 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3870 struct GNUNET_MESH_LocalAck msg;
3872 msg.header.size = htons (sizeof (msg));
3873 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3874 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3875 msg.max_pid = htonl (ack);
3876 GNUNET_SERVER_notification_context_unicast(nc,
3883 * Build an ACK message and queue it to send to the given peer.
3885 * @param t Tunnel on which to send the ACK.
3886 * @param peer Peer to whom send the ACK.
3887 * @param ack Value of the ACK.
3890 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3892 struct GNUNET_MESH_ACK msg;
3894 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3895 msg.header.size = htons (sizeof (msg));
3896 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3897 msg.pid = htonl (ack);
3898 msg.tid = htonl (t->id.tid);
3900 send_prebuilt_message (&msg.header, peer, t);
3905 * Notify a the owner of a tunnel about how many more
3906 * payload packages will we accept on a given tunnel.
3908 * @param t Tunnel on which to send the ACK.
3911 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3916 "Sending client FWD ACK on tunnel %X\n",
3919 ack = tunnel_get_fwd_ack (t);
3921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3922 if (t->last_fwd_ack == ack)
3924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3929 t->last_fwd_ack = ack;
3930 send_local_ack (t, t->owner, ack);
3935 * Send an ACK informing the predecessor about the available buffer space.
3936 * In case there is no predecessor, inform the owning client.
3937 * If buffering is off, send only on behalf of children or self if endpoint.
3938 * If buffering is on, send when sent to children and buffer space is free.
3939 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3940 * the ACK itself goes "back" (towards root).
3942 * @param t Tunnel on which to send the ACK.
3943 * @param type Type of message that triggered the ACK transmission.
3946 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3948 struct GNUNET_PeerIdentity id;
3951 if (NULL != t->owner)
3953 tunnel_send_client_fwd_ack (t);
3956 /* Is it after unicast / multicast retransmission? */
3959 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3960 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3962 "ACK due to FWD DATA retransmission\n");
3963 if (GNUNET_YES == t->nobuffer)
3965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3969 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3970 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3976 /* Check if we need no retransmit the ACK */
3977 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3978 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3982 " t->qmax: %u, t->qn: %u\n",
3983 t->fwd_queue_max, t->fwd_queue_n);
3984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3985 " t->pid: %u, t->ack: %u\n",
3986 t->fwd_pid, t->last_fwd_ack);
3990 /* Ok, ACK might be necessary, what PID to ACK? */
3991 ack = tunnel_get_fwd_ack (t);
3993 /* If speed_min and not all children have ack'd, dont send yet */
3994 if (ack == t->last_fwd_ack)
3996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4000 t->last_fwd_ack = ack;
4001 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4002 send_ack (t, &id, ack);
4008 * Iterator to send a child node a BCK ACK to allow him to send more
4011 * @param cls Closure (tunnel).
4012 * @param id Id of the child node.
4015 tunnel_send_child_bck_ack (void *cls,
4018 struct MeshTunnel *t = cls;
4019 struct MeshTunnelChildInfo *cinfo;
4020 struct GNUNET_PeerIdentity peer;
4022 GNUNET_PEER_resolve (id, &peer);
4023 cinfo = tunnel_get_neighbor_fc (t, &peer);
4025 if (cinfo->bck_ack != cinfo->bck_pid &&
4026 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->bck_pid))
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4029 " Not sending ACK, not needed\n");
4033 cinfo->bck_ack = t->bck_queue_max - t->bck_queue_n + cinfo->bck_pid;
4034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4035 " Sending BCK ACK %u\n",
4037 send_ack (t, &peer, cinfo->bck_ack);
4042 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4044 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4046 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4047 * - implement a size 1 buffer in each client_fc AND children_fc
4048 * to hold at least 1 message per "child".
4049 * problem: violates no buffer policy
4050 * - ack 0 and make "children" poll for transmission slots
4051 * problem: big overhead, extra latency even in low traffic
4054 * @param t Tunnel on which to send the BCK ACKs.
4057 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4060 unsigned int tunnel_delta;
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4064 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4067 /* Find client whom to allow to send to origin (with lowest buffer space) */
4068 for (i = 0; i < t->nclients; i++)
4070 struct MeshTunnelClientInfo *clinfo;
4073 clinfo = &t->clients_fc[i];
4074 delta = clinfo->bck_ack - clinfo->bck_pid;
4075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4076 t->clients[i]->id, delta);
4078 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4079 (GNUNET_YES == t->nobuffer && 0 == delta))
4083 ack = clinfo->bck_pid;
4084 ack += t->nobuffer ? 1 : tunnel_delta;
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4086 " sending ack to client %u: %u\n",
4087 t->clients[i]->id, ack);
4088 send_local_ack (t, t->clients[i], ack);
4089 clinfo->bck_ack = ack;
4093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4094 " not sending ack to client %u (td %u, d %u)\n",
4095 t->clients[i]->id, tunnel_delta, delta);
4102 * Send an ACK informing the children nodes and destination clients about
4103 * the available buffer space.
4104 * If buffering is off, send only on behalf of root (can be self).
4105 * If buffering is on, send when sent to predecessor and buffer space is free.
4106 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4107 * the ACK itself goes "forward" (towards children/clients).
4109 * @param t Tunnel on which to send the ACK.
4110 * @param type Type of message that triggered the ACK transmission.
4113 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4116 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4117 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4118 /* Is it after data to_origin retransmission? */
4121 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4122 if (GNUNET_YES == t->nobuffer)
4124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4125 " Not sending ACK, nobuffer\n");
4129 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4130 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4131 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4137 tunnel_send_clients_bck_ack (t);
4138 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4143 * @brief Re-initiate traffic to this peer if necessary.
4145 * Check if there is traffic queued towards this peer
4146 * and the core transmit handle is NULL (traffic was stalled).
4147 * If so, call core tmt rdy.
4149 * @param cls Closure (unused)
4150 * @param peer_id Short ID of peer to which initiate traffic.
4153 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4155 struct MeshPeerInfo *peer;
4156 struct GNUNET_PeerIdentity id;
4157 struct MeshPeerQueue *q;
4160 peer = peer_info_get_short(peer_id);
4161 if (NULL != peer->core_transmit)
4164 q = queue_get_next(peer);
4167 /* Might br multicast traffic already sent to this particular peer but
4168 * not to other children in this tunnel.
4169 * This way t->queue_n would be > 0 but the queue of this particular peer
4175 GNUNET_PEER_resolve (peer->id, &id);
4176 peer->core_transmit =
4177 GNUNET_CORE_notify_transmit_ready(core_handle,
4180 GNUNET_TIME_UNIT_FOREVER_REL,
4190 * @brief Allow transmission of FWD traffic on this tunnel
4192 * Check if there is traffic queued towards any children
4193 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4195 * @param t Tunnel on which to unlock FWD traffic.
4198 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4200 if (0 == t->fwd_queue_n)
4203 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4208 * @brief Allow transmission of BCK traffic on this tunnel
4210 * Check if there is traffic queued towards the root of the tree
4211 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4213 * @param t Tunnel on which to unlock BCK traffic.
4216 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4218 if (0 == t->bck_queue_n)
4221 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4226 * Send a message to all peers in this tunnel that the tunnel is no longer
4229 * @param t The tunnel whose peers to notify.
4232 tunnel_send_destroy (struct MeshTunnel *t)
4234 struct GNUNET_MESH_TunnelDestroy msg;
4236 msg.header.size = htons (sizeof (msg));
4237 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4238 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4239 msg.tid = htonl (t->id.tid);
4240 tunnel_send_multicast (t, &msg.header);
4245 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4247 * @param cls Closure (Tunnel which to cancel).
4248 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4251 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4253 struct MeshTunnel *t = cls;
4254 struct MeshPeerInfo *peer_info;
4255 struct MeshPeerQueue *pq;
4256 struct MeshPeerQueue *next;
4258 peer_info = peer_info_get_short (neighbor_id);
4259 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4262 if (pq->tunnel == t)
4264 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4265 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4266 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4268 // Should have been removed on destroy children
4271 queue_destroy (pq, GNUNET_YES);
4274 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4276 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4277 peer_info->core_transmit = NULL;
4282 * Destroy the tunnel and free any allocated resources linked to it.
4284 * @param t the tunnel to destroy
4286 * @return GNUNET_OK on success
4289 tunnel_destroy (struct MeshTunnel *t)
4291 struct MeshClient *c;
4292 struct GNUNET_HashCode hash;
4303 struct GNUNET_PeerIdentity id;
4305 GNUNET_PEER_resolve (t->id.oid, &id);
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4307 GNUNET_i2s (&id), t->id.tid);
4309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4313 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4314 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4322 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4324 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4331 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4332 for (i = 0; i < t->nclients; i++)
4336 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4342 for (i = 0; i < t->nignore; i++)
4346 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4353 if (t->nclients > 0)
4356 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4361 GNUNET_free (t->clients);
4362 GNUNET_free (t->clients_fc);
4365 if (NULL != t->peers)
4367 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4369 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4372 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4373 &tunnel_destroy_child,
4375 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4376 t->children_fc = NULL;
4378 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4379 tree_destroy (t->tree);
4381 if (NULL != t->regex_ctx)
4382 regex_cancel_search (t->regex_ctx);
4383 if (NULL != t->dht_get_type)
4384 GNUNET_DHT_get_stop (t->dht_get_type);
4385 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4386 GNUNET_SCHEDULER_cancel (t->timeout_task);
4387 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4388 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4391 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4398 * Create a new tunnel
4400 * @param owner Who is the owner of the tunnel (short ID).
4401 * @param tid Tunnel Number of the tunnel.
4402 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4403 * @param local Tunnel Number for the tunnel, for the client point of view.
4405 * @return A new initialized tunnel. NULL on error.
4407 static struct MeshTunnel *
4408 tunnel_new (GNUNET_PEER_Id owner,
4409 MESH_TunnelNumber tid,
4410 struct MeshClient *client,
4411 MESH_TunnelNumber local)
4413 struct MeshTunnel *t;
4414 struct GNUNET_HashCode hash;
4416 if (n_tunnels >= max_tunnels && NULL == client)
4419 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4422 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4423 t->bck_queue_max = t->fwd_queue_max;
4424 t->tree = tree_new (owner);
4426 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4427 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4428 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4429 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4430 t->local_tid = local;
4431 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4433 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4435 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4437 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4438 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4445 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4452 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4454 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4455 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4459 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4469 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4470 * that are no longer needed, as well as nodes of no longer reachable peers.
4471 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4473 * @param t Tunnel from which to remove the path.
4474 * @param peer Short id of the peer which should be removed.
4477 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4479 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4485 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4486 * client when the client disconnects. If the client is not the owner, the
4487 * owner will get notified if no more clients are in the tunnel and the client
4488 * get removed from the tunnel's list.
4490 * @param cls closure (client that is disconnecting)
4491 * @param key the hash of the local tunnel id (used to access the hashmap)
4492 * @param value the value stored at the key (tunnel to destroy)
4494 * @return GNUNET_OK, keep iterating.
4497 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4499 struct MeshTunnel *t = value;
4500 struct MeshClient *c = cls;
4502 send_client_tunnel_disconnect(t, c);
4505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4506 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4507 tunnel_delete_client(t, c);
4508 client_delete_tunnel(c, t);
4511 tunnel_send_destroy(t);
4513 t->destroy = GNUNET_YES;
4520 * Timeout function, destroys tunnel if called
4522 * @param cls Closure (tunnel to destroy).
4523 * @param tc TaskContext
4526 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4528 struct MeshTunnel *t = cls;
4529 struct GNUNET_PeerIdentity id;
4531 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4532 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4534 GNUNET_PEER_resolve(t->id.oid, &id);
4535 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4536 "Tunnel %s [%X] timed out. Destroying.\n",
4537 GNUNET_i2s(&id), t->id.tid);
4538 send_clients_tunnel_destroy (t);
4543 * Resets the tunnel timeout. Starts it if no timeout was running.
4545 * @param t Tunnel whose timeout to reset.
4547 * TODO use heap to improve efficiency of scheduler.
4550 tunnel_reset_timeout (struct MeshTunnel *t)
4552 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4553 GNUNET_SCHEDULER_cancel (t->timeout_task);
4555 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4556 (refresh_path_time, 4), &tunnel_timeout, t);
4560 /******************************************************************************/
4561 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4562 /******************************************************************************/
4565 * Function to send a create path packet to a peer.
4567 * @param cls closure
4568 * @param size number of bytes available in buf
4569 * @param buf where the callee should write the message
4570 * @return number of bytes written to buf
4573 send_core_path_create (void *cls, size_t size, void *buf)
4575 struct MeshPathInfo *info = cls;
4576 struct GNUNET_MESH_ManipulatePath *msg;
4577 struct GNUNET_PeerIdentity *peer_ptr;
4578 struct MeshTunnel *t = info->t;
4579 struct MeshPeerPath *p = info->path;
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4586 sizeof (struct GNUNET_MESH_ManipulatePath) +
4587 p->length * sizeof (struct GNUNET_PeerIdentity);
4589 if (size < size_needed || NULL == buf)
4594 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4595 msg->header.size = htons (size_needed);
4596 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4597 msg->tid = ntohl (t->id.tid);
4600 if (GNUNET_YES == t->speed_min)
4601 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4602 if (GNUNET_YES == t->nobuffer)
4603 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4604 msg->opt = htonl(opt);
4607 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4608 for (i = 0; i < p->length; i++)
4610 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4617 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4623 * Fill the core buffer
4625 * @param cls closure (data itself)
4626 * @param size number of bytes available in buf
4627 * @param buf where the callee should write the message
4629 * @return number of bytes written to buf
4632 send_core_data_multicast (void *cls, size_t size, void *buf)
4634 struct MeshTransmissionDescriptor *info = cls;
4637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4638 GNUNET_assert (NULL != info);
4639 GNUNET_assert (NULL != info->peer);
4640 total_size = info->mesh_data->data_len;
4641 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4643 if (total_size > size)
4648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4649 memcpy (buf, info->mesh_data->data, total_size);
4652 struct GNUNET_MESH_Multicast *mc;
4653 struct GNUNET_MessageHeader *mh;
4656 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4658 mc = (struct GNUNET_MESH_Multicast *) mh;
4659 mh = (struct GNUNET_MessageHeader *) &mc[1];
4660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4661 " multicast, payload type %s\n",
4662 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4664 " multicast, payload size %u\n", ntohs (mh->size));
4668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4669 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4673 data_descriptor_decrement_rc (info->mesh_data);
4674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4682 * Creates a path ack message in buf and frees all unused resources.
4684 * @param cls closure (MeshTransmissionDescriptor)
4685 * @param size number of bytes available in buf
4686 * @param buf where the callee should write the message
4687 * @return number of bytes written to buf
4690 send_core_path_ack (void *cls, size_t size, void *buf)
4692 struct MeshTransmissionDescriptor *info = cls;
4693 struct GNUNET_MESH_PathACK *msg = buf;
4695 GNUNET_assert (NULL != info);
4696 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4701 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4702 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4703 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4704 msg->tid = htonl (info->origin->tid);
4705 msg->peer_id = my_full_id;
4708 /* TODO add signature */
4710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4711 return sizeof (struct GNUNET_MESH_PathACK);
4716 * Free a transmission that was already queued with all resources
4717 * associated to the request.
4719 * @param queue Queue handler to cancel.
4720 * @param clear_cls Is it necessary to free associated cls?
4723 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4725 struct MeshTransmissionDescriptor *dd;
4726 struct MeshPathInfo *path_info;
4727 struct MeshTunnelChildInfo *cinfo;
4728 struct GNUNET_PeerIdentity id;
4732 if (GNUNET_YES == clear_cls)
4734 switch (queue->type)
4736 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4738 GNUNET_assert (GNUNET_YES == queue->tunnel->destroy);
4739 /* FIXME: don't cancel, send and destroy tunnel in queue_send */
4741 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4742 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4743 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4744 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4745 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4747 " prebuilt message\n");
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4750 GNUNET_MESH_DEBUG_M2S(queue->type));
4752 data_descriptor_decrement_rc (dd->mesh_data);
4754 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4756 path_info = queue->cls;
4757 path_destroy (path_info->path);
4761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4762 " type %s unknown!\n",
4763 GNUNET_MESH_DEBUG_M2S(queue->type));
4765 GNUNET_free_non_null (queue->cls);
4767 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4768 queue->peer->queue_tail,
4771 /* Delete from child_fc in the appropiate tunnel */
4772 max = queue->tunnel->fwd_queue_max;
4773 GNUNET_PEER_resolve (queue->peer->id, &id);
4774 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4777 for (i = 0; i < cinfo->send_buffer_n; i++)
4780 i2 = (cinfo->send_buffer_start + i) % max;
4781 if (cinfo->send_buffer[i2] == queue)
4783 /* Found corresponding entry in the send_buffer. Move all others back. */
4788 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4790 j2 = (cinfo->send_buffer_start + j) % max;
4791 j3 = (cinfo->send_buffer_start + j + 1) % max;
4792 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4795 cinfo->send_buffer[j3] = NULL;
4796 cinfo->send_buffer_n--;
4801 GNUNET_free (queue);
4806 * @brief Get the next transmittable message from the queue.
4808 * This will be the head, except in the case of being a data packet
4809 * not allowed by the destination peer.
4811 * @param peer Destination peer.
4813 * @return The next viable MeshPeerQueue element to send to that peer.
4814 * NULL when there are no transmittable messages.
4816 struct MeshPeerQueue *
4817 queue_get_next (const struct MeshPeerInfo *peer)
4819 struct MeshPeerQueue *q;
4820 struct MeshTunnel *t;
4821 struct MeshTransmissionDescriptor *info;
4822 struct MeshTunnelChildInfo *cinfo;
4823 struct GNUNET_MESH_Unicast *ucast;
4824 struct GNUNET_MESH_ToOrigin *to_orig;
4825 struct GNUNET_MESH_Multicast *mcast;
4826 struct GNUNET_PeerIdentity id;
4830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4831 for (q = peer->queue_head; NULL != q; q = q->next)
4835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4837 GNUNET_MESH_DEBUG_M2S(q->type));
4840 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4841 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4842 pid = ntohl (ucast->pid);
4843 GNUNET_PEER_resolve (info->peer->id, &id);
4844 cinfo = tunnel_get_neighbor_fc(t, &id);
4845 ack = cinfo->fwd_ack;
4847 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4848 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4849 pid = ntohl (to_orig->pid);
4852 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4853 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4854 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4856 // Not a multicast payload: multicast control traffic (destroy, etc)
4859 pid = ntohl (mcast->pid);
4860 GNUNET_PEER_resolve (info->peer->id, &id);
4861 cinfo = tunnel_get_neighbor_fc(t, &id);
4862 ack = cinfo->fwd_ack;
4865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4870 "********* ACK: %u, PID: %u\n",
4872 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4881 "********* NEXT!\n");
4884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4885 "********* nothing found\n");
4891 * Core callback to write a queued packet to core buffer
4893 * @param cls Closure (peer info).
4894 * @param size Number of bytes available in buf.
4895 * @param buf Where the to write the message.
4897 * @return number of bytes written to buf
4900 queue_send (void *cls, size_t size, void *buf)
4902 struct MeshPeerInfo *peer = cls;
4903 struct GNUNET_MessageHeader *msg;
4904 struct MeshPeerQueue *queue;
4905 struct MeshTunnel *t;
4906 struct MeshTunnelChildInfo *cinfo;
4907 struct GNUNET_PeerIdentity dst_id;
4910 peer->core_transmit = NULL;
4913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4914 queue = queue_get_next (peer);
4916 /* Queue has no internal mesh traffic nor sendable payload */
4919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4920 if (NULL == peer->queue_head)
4921 GNUNET_break (0); // Should've been canceled
4924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4926 GNUNET_PEER_resolve (peer->id, &dst_id);
4927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4928 "********* towards %s\n",
4929 GNUNET_i2s(&dst_id));
4930 /* Check if buffer size is enough for the message */
4931 if (queue->size > size)
4933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4934 "********* not enough room, reissue\n");
4935 peer->core_transmit =
4936 GNUNET_CORE_notify_transmit_ready (core_handle,
4939 GNUNET_TIME_UNIT_FOREVER_REL,
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4949 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4953 "********* unicast: t->q (%u/%u)\n",
4954 t->fwd_queue_n, t->fwd_queue_max);
4956 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4963 switch (queue->type)
4966 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4967 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4968 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4969 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4971 "********* raw: %s\n",
4972 GNUNET_MESH_DEBUG_M2S (queue->type));
4974 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4975 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4976 data_size = send_core_data_raw (queue->cls, size, buf);
4977 msg = (struct GNUNET_MessageHeader *) buf;
4978 switch (ntohs (msg->type)) // Type of preconstructed message
4980 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4981 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4983 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4984 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4990 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4993 struct MeshTransmissionDescriptor *info = queue->cls;
4995 if ((1 == info->mesh_data->reference_counter
4996 && GNUNET_YES == t->speed_min)
4998 (info->mesh_data->total_out == info->mesh_data->reference_counter
4999 && GNUNET_NO == t->speed_min))
5001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5002 "********* considered sent\n");
5006 data_size = send_core_data_multicast(queue->cls, size, buf);
5007 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5009 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5011 data_size = send_core_path_create (queue->cls, size, buf);
5013 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5015 data_size = send_core_path_ack (queue->cls, size, buf);
5017 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5019 data_size = send_core_data_multicast (queue->cls, size, buf);
5023 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5024 "********* type unknown: %u\n",
5028 switch (queue->type)
5030 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5031 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5032 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5033 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5034 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5038 "at pos %u (%p) != %p\n",
5039 cinfo->send_buffer_start,
5040 cinfo->send_buffer[cinfo->send_buffer_start],
5043 if (cinfo->send_buffer_n > 0)
5045 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5046 cinfo->send_buffer_n--;
5047 cinfo->send_buffer_start++;
5048 cinfo->send_buffer_start %= t->fwd_queue_max;
5059 /* Free queue, but cls was freed by send_core_* */
5060 queue_destroy (queue, GNUNET_NO);
5062 if (GNUNET_YES == t->destroy)
5064 // FIXME fc tunnel destroy all pending traffic? wait for it?
5065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5069 /* If more data in queue, send next */
5070 queue = queue_get_next(peer);
5073 struct GNUNET_PeerIdentity id;
5075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5076 GNUNET_PEER_resolve (peer->id, &id);
5077 peer->core_transmit =
5078 GNUNET_CORE_notify_transmit_ready(core_handle,
5081 GNUNET_TIME_UNIT_FOREVER_REL,
5089 if (NULL != peer->queue_head)
5091 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5092 "********* %s stalled\n",
5093 GNUNET_i2s(&my_full_id));
5095 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5096 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5097 &tunnel_poll, cinfo);
5100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5106 * @brief Queue and pass message to core when possible.
5108 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5109 * and accounts for it. In case the queue is full, the message is dropped and
5112 * Otherwise, message is treated as internal and allowed to go regardless of
5115 * @param cls Closure (@c type dependant). It will be used by queue_send to
5116 * build the message to be sent if not already prebuilt.
5117 * @param type Type of the message, 0 for a raw message.
5118 * @param size Size of the message.
5119 * @param dst Neighbor to send message to.
5120 * @param t Tunnel this message belongs to.
5123 queue_add (void *cls, uint16_t type, size_t size,
5124 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5126 struct MeshPeerQueue *queue;
5127 struct MeshTunnelChildInfo *cinfo;
5128 struct GNUNET_PeerIdentity id;
5134 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5135 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5137 n = &t->fwd_queue_n;
5138 max = &t->fwd_queue_max;
5140 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5142 n = &t->bck_queue_n;
5143 max = &t->bck_queue_max;
5149 struct MeshTransmissionDescriptor *td = cls;
5150 struct GNUNET_MESH_ToOrigin *to;
5152 to = td->mesh_data->data;
5154 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5155 "bck pid %u, bck ack %u, msg pid %u\n",
5156 t->bck_pid, t->bck_ack, ntohl(to->pid));
5157 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5159 return; // Drop message
5163 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5169 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5170 GNUNET_PEER_resolve (dst->id, &id);
5171 if (NULL == dst->core_transmit)
5173 dst->core_transmit =
5174 GNUNET_CORE_notify_transmit_ready (core_handle,
5177 GNUNET_TIME_UNIT_FOREVER_REL,
5183 if (NULL == n) // Is this internal mesh traffic?
5186 // It's payload, keep track of buffer per peer.
5187 cinfo = tunnel_get_neighbor_fc(t, &id);
5188 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5189 if (NULL != cinfo->send_buffer[i])
5191 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5192 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5193 cinfo->send_buffer_start++;
5194 cinfo->send_buffer_start %= t->fwd_queue_max;
5198 cinfo->send_buffer_n++;
5200 cinfo->send_buffer[i] = queue;
5201 if (cinfo->send_buffer_n > t->fwd_queue_max)
5204 cinfo->send_buffer_n = t->fwd_queue_max;
5209 /******************************************************************************/
5210 /******************** MESH NETWORK HANDLERS **************************/
5211 /******************************************************************************/
5215 * Core handler for path creation
5217 * @param cls closure
5218 * @param message message
5219 * @param peer peer identity this notification is about
5220 * @param atsi performance data
5221 * @param atsi_count number of records in 'atsi'
5223 * @return GNUNET_OK to keep the connection open,
5224 * GNUNET_SYSERR to close it (signal serious error)
5227 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5228 const struct GNUNET_MessageHeader *message,
5229 const struct GNUNET_ATS_Information *atsi,
5230 unsigned int atsi_count)
5232 unsigned int own_pos;
5235 MESH_TunnelNumber tid;
5236 struct GNUNET_MESH_ManipulatePath *msg;
5237 struct GNUNET_PeerIdentity *pi;
5238 struct GNUNET_HashCode hash;
5239 struct MeshPeerPath *path;
5240 struct MeshPeerInfo *dest_peer_info;
5241 struct MeshPeerInfo *orig_peer_info;
5242 struct MeshTunnel *t;
5244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5245 "Received a path create msg [%s]\n",
5246 GNUNET_i2s (&my_full_id));
5247 size = ntohs (message->size);
5248 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5250 GNUNET_break_op (0);
5254 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5255 if (size % sizeof (struct GNUNET_PeerIdentity))
5257 GNUNET_break_op (0);
5260 size /= sizeof (struct GNUNET_PeerIdentity);
5263 GNUNET_break_op (0);
5266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5267 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5269 tid = ntohl (msg->tid);
5270 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5272 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5273 t = tunnel_get (pi, tid);
5274 if (NULL == t) // FIXME only for INCOMING tunnels?
5278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5279 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5282 // FIXME notify failure
5285 opt = ntohl (msg->opt);
5286 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5287 GNUNET_YES : GNUNET_NO;
5288 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5290 t->nobuffer = GNUNET_YES;
5291 t->last_fwd_ack = t->fwd_pid + 1;
5293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5294 " speed_min: %d, nobuffer:%d\n",
5295 t->speed_min, t->nobuffer);
5297 if (GNUNET_YES == t->nobuffer)
5299 t->bck_queue_max = 1;
5300 t->fwd_queue_max = 1;
5303 // FIXME only assign a local tid if a local client is interested (on demand)
5304 while (NULL != tunnel_get_incoming (next_local_tid))
5305 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5306 t->local_tid_dest = next_local_tid++;
5307 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5310 tunnel_reset_timeout (t);
5311 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5313 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5314 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5322 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5323 if (NULL == dest_peer_info)
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5326 " Creating PeerInfo for destination.\n");
5327 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5328 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5329 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5331 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5333 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5334 if (NULL == orig_peer_info)
5336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5337 " Creating PeerInfo for origin.\n");
5338 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5339 orig_peer_info->id = GNUNET_PEER_intern (pi);
5340 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5341 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5344 path = path_new (size);
5346 for (i = 0; i < size; i++)
5348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5349 GNUNET_i2s (&pi[i]));
5350 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5351 if (path->peers[i] == myid)
5354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5357 /* cannot be self, must be 'not found' */
5358 /* create path: self not found in path through self */
5359 GNUNET_break_op (0);
5360 path_destroy (path);
5364 path_add_to_peers (path, GNUNET_NO);
5365 tunnel_add_path (t, path, own_pos);
5366 if (own_pos == size - 1)
5368 /* It is for us! Send ack. */
5369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5370 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5371 if (NULL == t->peers)
5373 /* New tunnel! Notify clients on first payload message. */
5374 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5376 GNUNET_break (GNUNET_SYSERR !=
5377 GNUNET_CONTAINER_multihashmap_put (t->peers,
5378 &my_full_id.hashPubKey,
5381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5386 struct MeshPeerPath *path2;
5388 /* It's for somebody else! Retransmit. */
5389 path2 = path_duplicate (path);
5390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5391 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5392 path2 = path_duplicate (path);
5393 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5394 send_create_path (dest_peer_info, path, t);
5401 * Core handler for path destruction
5403 * @param cls closure
5404 * @param message message
5405 * @param peer peer identity this notification is about
5406 * @param atsi performance data
5407 * @param atsi_count number of records in 'atsi'
5409 * @return GNUNET_OK to keep the connection open,
5410 * GNUNET_SYSERR to close it (signal serious error)
5413 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5414 const struct GNUNET_MessageHeader *message,
5415 const struct GNUNET_ATS_Information *atsi,
5416 unsigned int atsi_count)
5418 struct GNUNET_MESH_ManipulatePath *msg;
5419 struct GNUNET_PeerIdentity *pi;
5420 struct MeshPeerPath *path;
5421 struct MeshTunnel *t;
5422 unsigned int own_pos;
5426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5427 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5428 size = ntohs (message->size);
5429 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5431 GNUNET_break_op (0);
5435 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5436 if (size % sizeof (struct GNUNET_PeerIdentity))
5438 GNUNET_break_op (0);
5441 size /= sizeof (struct GNUNET_PeerIdentity);
5444 GNUNET_break_op (0);
5447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5449 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5450 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5452 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5454 t = tunnel_get (pi, ntohl (msg->tid));
5457 /* TODO notify back: we don't know this tunnel */
5458 GNUNET_break_op (0);
5461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5462 path = path_new (size);
5464 for (i = 0; i < size; i++)
5466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5467 GNUNET_i2s (&pi[i]));
5468 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5469 if (path->peers[i] == myid)
5472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5473 if (own_pos < path->length - 1)
5474 send_prebuilt_message (message, &pi[own_pos + 1], t);
5476 send_client_tunnel_disconnect(t, NULL);
5478 tunnel_delete_peer (t, path->peers[path->length - 1]);
5479 path_destroy (path);
5485 * Core handler for notifications of broken paths
5487 * @param cls closure
5488 * @param message message
5489 * @param peer peer identity this notification is about
5490 * @param atsi performance data
5491 * @param atsi_count number of records in 'atsi'
5493 * @return GNUNET_OK to keep the connection open,
5494 * GNUNET_SYSERR to close it (signal serious error)
5497 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5498 const struct GNUNET_MessageHeader *message,
5499 const struct GNUNET_ATS_Information *atsi,
5500 unsigned int atsi_count)
5502 struct GNUNET_MESH_PathBroken *msg;
5503 struct MeshTunnel *t;
5505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5506 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5507 msg = (struct GNUNET_MESH_PathBroken *) message;
5508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5509 GNUNET_i2s (&msg->peer1));
5510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5511 GNUNET_i2s (&msg->peer2));
5512 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5515 GNUNET_break_op (0);
5518 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5519 GNUNET_PEER_search (&msg->peer2));
5526 * Core handler for tunnel destruction
5528 * @param cls closure
5529 * @param message message
5530 * @param peer peer identity this notification is about
5531 * @param atsi performance data
5532 * @param atsi_count number of records in 'atsi'
5534 * @return GNUNET_OK to keep the connection open,
5535 * GNUNET_SYSERR to close it (signal serious error)
5538 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5539 const struct GNUNET_MessageHeader *message,
5540 const struct GNUNET_ATS_Information *atsi,
5541 unsigned int atsi_count)
5543 struct GNUNET_MESH_TunnelDestroy *msg;
5544 struct MeshTunnel *t;
5546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5547 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5548 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5550 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5551 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5554 /* Probably already got the message from another path,
5555 * destroyed the tunnel and retransmitted to children.
5558 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5561 if (t->id.oid == myid)
5563 GNUNET_break_op (0);
5566 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5568 /* Tunnel was incoming, notify clients */
5569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5570 t->local_tid, t->local_tid_dest);
5571 send_clients_tunnel_destroy (t);
5573 tunnel_send_destroy (t);
5574 t->destroy = GNUNET_YES;
5575 // TODO: add timeout to destroy the tunnel anyway
5581 * Core handler for mesh network traffic going from the origin to a peer
5583 * @param cls closure
5584 * @param peer peer identity this notification is about
5585 * @param message message
5586 * @param atsi performance data
5587 * @param atsi_count number of records in 'atsi'
5588 * @return GNUNET_OK to keep the connection open,
5589 * GNUNET_SYSERR to close it (signal serious error)
5592 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5593 const struct GNUNET_MessageHeader *message,
5594 const struct GNUNET_ATS_Information *atsi,
5595 unsigned int atsi_count)
5597 struct GNUNET_MESH_Unicast *msg;
5598 struct GNUNET_PeerIdentity *neighbor;
5599 struct MeshTunnelChildInfo *cinfo;
5600 struct MeshTunnel *t;
5601 GNUNET_PEER_Id dest_id;
5606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5609 size = ntohs (message->size);
5611 sizeof (struct GNUNET_MESH_Unicast) +
5612 sizeof (struct GNUNET_MessageHeader))
5617 msg = (struct GNUNET_MESH_Unicast *) message;
5618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5619 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5621 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5624 /* TODO notify back: we don't know this tunnel */
5625 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5626 GNUNET_break_op (0);
5629 pid = ntohl (msg->pid);
5630 if (t->fwd_pid == pid)
5632 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5633 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5634 " Already seen pid %u, DROPPING!\n", pid);
5639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5640 " pid %u not seen yet, forwarding\n", pid);
5643 t->skip += (pid - t->fwd_pid) - 1;
5646 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5648 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5649 GNUNET_break_op (0);
5650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5651 "Received PID %u, ACK %u\n",
5652 pid, t->last_fwd_ack);
5656 tunnel_reset_timeout (t);
5657 dest_id = GNUNET_PEER_search (&msg->destination);
5658 if (dest_id == myid)
5660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5661 " it's for us! sending to clients...\n");
5662 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5663 send_subscribed_clients (message, &msg[1].header, t);
5664 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5667 ttl = ntohl (msg->ttl);
5668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5671 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5673 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5677 " not for us, retransmitting...\n");
5679 neighbor = tree_get_first_hop (t->tree, dest_id);
5680 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5681 cinfo->fwd_pid = pid;
5682 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5685 if (GNUNET_YES == t->nobuffer &&
5686 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5688 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5689 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5690 GNUNET_break_op (0);
5693 send_prebuilt_message (message, neighbor, t);
5694 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5700 * Core handler for mesh network traffic going from the origin to all peers
5702 * @param cls closure
5703 * @param message message
5704 * @param peer peer identity this notification is about
5705 * @param atsi performance data
5706 * @param atsi_count number of records in 'atsi'
5707 * @return GNUNET_OK to keep the connection open,
5708 * GNUNET_SYSERR to close it (signal serious error)
5710 * TODO: Check who we got this from, to validate route.
5713 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5714 const struct GNUNET_MessageHeader *message,
5715 const struct GNUNET_ATS_Information *atsi,
5716 unsigned int atsi_count)
5718 struct GNUNET_MESH_Multicast *msg;
5719 struct MeshTunnel *t;
5723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5725 size = ntohs (message->size);
5726 if (sizeof (struct GNUNET_MESH_Multicast) +
5727 sizeof (struct GNUNET_MessageHeader) > size)
5729 GNUNET_break_op (0);
5732 msg = (struct GNUNET_MESH_Multicast *) message;
5733 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5737 /* TODO notify that we dont know that tunnel */
5738 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5739 GNUNET_break_op (0);
5742 pid = ntohl (msg->pid);
5743 if (t->fwd_pid == pid)
5745 /* already seen this packet, drop */
5746 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5748 " Already seen pid %u, DROPPING!\n", pid);
5749 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5755 " pid %u not seen yet, forwarding\n", pid);
5757 t->skip += (pid - t->fwd_pid) - 1;
5759 tunnel_reset_timeout (t);
5761 /* Transmit to locally interested clients */
5762 if (NULL != t->peers &&
5763 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5765 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5766 send_subscribed_clients (message, &msg[1].header, t);
5767 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5770 if (ntohl (msg->ttl) == 0)
5772 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5773 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5774 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5777 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5778 tunnel_send_multicast (t, message);
5784 * Core handler for mesh network traffic toward the owner of a tunnel
5786 * @param cls closure
5787 * @param message message
5788 * @param peer peer identity this notification is about
5789 * @param atsi performance data
5790 * @param atsi_count number of records in 'atsi'
5792 * @return GNUNET_OK to keep the connection open,
5793 * GNUNET_SYSERR to close it (signal serious error)
5796 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5797 const struct GNUNET_MessageHeader *message,
5798 const struct GNUNET_ATS_Information *atsi,
5799 unsigned int atsi_count)
5801 struct GNUNET_MESH_ToOrigin *msg;
5802 struct GNUNET_PeerIdentity id;
5803 struct MeshPeerInfo *peer_info;
5804 struct MeshTunnel *t;
5805 struct MeshTunnelChildInfo *cinfo;
5809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5811 size = ntohs (message->size);
5812 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5813 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5815 GNUNET_break_op (0);
5818 msg = (struct GNUNET_MESH_ToOrigin *) message;
5819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5820 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5821 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5822 pid = ntohl (msg->pid);
5826 /* TODO notify that we dont know this tunnel (whom)? */
5827 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5828 GNUNET_break_op (0);
5829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5830 "Received to_origin with PID %u on unknown tunnel\n",
5835 cinfo = tunnel_get_neighbor_fc(t, peer);
5842 if (cinfo->bck_pid == pid)
5844 /* already seen this packet, drop */
5845 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5847 " Already seen pid %u, DROPPING!\n", pid);
5848 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5853 " pid %u not seen yet, forwarding\n", pid);
5854 cinfo->bck_pid = pid;
5856 if (NULL != t->owner)
5859 struct GNUNET_MESH_ToOrigin *copy;
5861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5862 " it's for us! sending to clients...\n");
5863 /* TODO signature verification */
5864 memcpy (cbuf, message, size);
5865 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5866 copy->tid = htonl (t->local_tid);
5868 copy->pid = htonl (t->bck_pid);
5869 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5870 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5871 ©->header, GNUNET_NO);
5872 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5876 " not for us, retransmitting...\n");
5878 peer_info = peer_info_get (&msg->oid);
5879 if (NULL == peer_info)
5881 /* unknown origin of tunnel */
5885 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5886 send_prebuilt_message (message, &id, t);
5887 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5894 * Core handler for mesh network traffic point-to-point acks.
5896 * @param cls closure
5897 * @param message message
5898 * @param peer peer identity this notification is about
5899 * @param atsi performance data
5900 * @param atsi_count number of records in 'atsi'
5902 * @return GNUNET_OK to keep the connection open,
5903 * GNUNET_SYSERR to close it (signal serious error)
5906 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5907 const struct GNUNET_MessageHeader *message,
5908 const struct GNUNET_ATS_Information *atsi,
5909 unsigned int atsi_count)
5911 struct GNUNET_MESH_ACK *msg;
5912 struct MeshTunnel *t;
5915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5917 msg = (struct GNUNET_MESH_ACK *) message;
5919 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5923 /* TODO notify that we dont know this tunnel (whom)? */
5924 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5925 GNUNET_break_op (0);
5928 ack = ntohl (msg->pid);
5929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5931 /* Is this a forward or backward ACK? */
5932 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5934 struct MeshTunnelChildInfo *cinfo;
5937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5938 cinfo = tunnel_get_neighbor_fc (t, peer);
5939 cinfo->fwd_ack = ack;
5940 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5941 tunnel_unlock_fwd_queues (t);
5945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5947 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5948 tunnel_unlock_bck_queue (t);
5955 * Core handler for mesh network traffic point-to-point ack polls.
5957 * @param cls closure
5958 * @param message message
5959 * @param peer peer identity this notification is about
5960 * @param atsi performance data
5961 * @param atsi_count number of records in 'atsi'
5963 * @return GNUNET_OK to keep the connection open,
5964 * GNUNET_SYSERR to close it (signal serious error)
5967 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5968 const struct GNUNET_MessageHeader *message,
5969 const struct GNUNET_ATS_Information *atsi,
5970 unsigned int atsi_count)
5972 struct GNUNET_MESH_Poll *msg;
5973 struct MeshTunnel *t;
5975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5978 msg = (struct GNUNET_MESH_Poll *) message;
5980 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5984 /* TODO notify that we dont know this tunnel (whom)? */
5985 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5986 GNUNET_break_op (0);
5990 /* Is this a forward or backward ACK? */
5991 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5993 struct MeshTunnelChildInfo *cinfo;
5995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5996 cinfo = tunnel_get_neighbor_fc (t, peer);
5997 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5998 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6003 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6011 * Core handler for path ACKs
6013 * @param cls closure
6014 * @param message message
6015 * @param peer peer identity this notification is about
6016 * @param atsi performance data
6017 * @param atsi_count number of records in 'atsi'
6019 * @return GNUNET_OK to keep the connection open,
6020 * GNUNET_SYSERR to close it (signal serious error)
6023 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6024 const struct GNUNET_MessageHeader *message,
6025 const struct GNUNET_ATS_Information *atsi,
6026 unsigned int atsi_count)
6028 struct GNUNET_MESH_PathACK *msg;
6029 struct GNUNET_PeerIdentity id;
6030 struct MeshPeerInfo *peer_info;
6031 struct MeshPeerPath *p;
6032 struct MeshTunnel *t;
6034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6035 GNUNET_i2s (&my_full_id));
6036 msg = (struct GNUNET_MESH_PathACK *) message;
6037 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6040 /* TODO notify that we don't know the tunnel */
6041 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6043 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6047 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6049 peer_info = peer_info_get (&msg->peer_id);
6050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6051 GNUNET_i2s (&msg->peer_id));
6052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6055 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6058 "connect_by_string completed, stopping search\n");
6059 regex_cancel_search (t->regex_ctx);
6060 t->regex_ctx = NULL;
6063 /* Add paths to peers? */
6064 p = tree_get_path_to_peer (t->tree, peer_info->id);
6067 path_add_to_peers (p, GNUNET_YES);
6075 /* Message for us? */
6076 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6079 if (NULL == t->owner)
6081 GNUNET_break_op (0);
6084 if (NULL != t->dht_get_type)
6086 GNUNET_DHT_get_stop (t->dht_get_type);
6087 t->dht_get_type = NULL;
6089 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6091 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6092 send_client_peer_connected (t, peer_info->id);
6097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6098 " not for us, retransmitting...\n");
6099 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6100 peer_info = peer_info_get (&msg->oid);
6101 if (NULL == peer_info)
6103 /* If we know the tunnel, we should DEFINITELY know the peer */
6107 send_prebuilt_message (message, &id, t);
6113 * Core handler for mesh keepalives.
6115 * @param cls closure
6116 * @param message message
6117 * @param peer peer identity this notification is about
6118 * @param atsi performance data
6119 * @param atsi_count number of records in 'atsi'
6120 * @return GNUNET_OK to keep the connection open,
6121 * GNUNET_SYSERR to close it (signal serious error)
6123 * TODO: Check who we got this from, to validate route.
6126 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6127 const struct GNUNET_MessageHeader *message,
6128 const struct GNUNET_ATS_Information *atsi,
6129 unsigned int atsi_count)
6131 struct GNUNET_MESH_TunnelKeepAlive *msg;
6132 struct MeshTunnel *t;
6134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6137 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6138 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6142 /* TODO notify that we dont know that tunnel */
6143 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6144 GNUNET_break_op (0);
6148 tunnel_reset_timeout (t);
6150 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6151 tunnel_send_multicast (t, message);
6158 * Functions to handle messages from core
6160 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6161 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6162 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6163 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6164 sizeof (struct GNUNET_MESH_PathBroken)},
6165 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6166 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6167 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6168 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6169 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6170 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6171 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6172 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6173 sizeof (struct GNUNET_MESH_ACK)},
6174 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6175 sizeof (struct GNUNET_MESH_Poll)},
6176 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6177 sizeof (struct GNUNET_MESH_PathACK)},
6183 /******************************************************************************/
6184 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6185 /******************************************************************************/
6188 * deregister_app: iterator for removing each application registered by a client
6190 * @param cls closure
6191 * @param key the hash of the application id (used to access the hashmap)
6192 * @param value the value stored at the key (client)
6194 * @return GNUNET_OK on success
6197 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6199 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6200 GNUNET_break (GNUNET_YES ==
6201 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6207 * notify_client_connection_failure: notify a client that the connection to the
6208 * requested remote peer is not possible (for instance, no route found)
6209 * Function called when the socket is ready to queue more data. "buf" will be
6210 * NULL and "size" zero if the socket was closed for writing in the meantime.
6212 * @param cls closure
6213 * @param size number of bytes available in buf
6214 * @param buf where the callee should write the message
6215 * @return number of bytes written to buf
6218 notify_client_connection_failure (void *cls, size_t size, void *buf)
6221 struct MeshPeerInfo *peer_info;
6222 struct GNUNET_MESH_PeerControl *msg;
6223 struct GNUNET_PeerIdentity id;
6225 if (0 == size && NULL == buf)
6227 // TODO retry? cancel?
6231 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6232 peer_info = (struct MeshPeerInfo *) cls;
6233 msg = (struct GNUNET_MESH_PeerControl *) buf;
6234 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6235 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6236 // msg->tunnel_id = htonl(peer_info->t->tid);
6237 GNUNET_PEER_resolve (peer_info->id, &id);
6238 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6246 * Send keepalive packets for a peer
6248 * @param cls Closure (tunnel for which to send the keepalive).
6249 * @param tc Notification context.
6252 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6254 struct MeshTunnel *t = cls;
6255 struct GNUNET_MESH_TunnelKeepAlive *msg;
6256 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6259 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6260 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6266 "sending keepalive for tunnel %d\n", t->id.tid);
6268 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6269 msg->header.size = htons (size);
6270 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6271 msg->oid = my_full_id;
6272 msg->tid = htonl (t->id.tid);
6273 tunnel_send_multicast (t, &msg->header);
6275 t->path_refresh_task =
6276 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6277 tunnel_reset_timeout(t);
6282 * Function to process paths received for a new peer addition. The recorded
6283 * paths form the initial tunnel, which can be optimized later.
6284 * Called on each result obtained for the DHT search.
6286 * @param cls closure
6287 * @param exp when will this value expire
6288 * @param key key of the result
6289 * @param get_path path of the get request
6290 * @param get_path_length lenght of get_path
6291 * @param put_path path of the put request
6292 * @param put_path_length length of the put_path
6293 * @param type type of the result
6294 * @param size number of bytes in data
6295 * @param data pointer to the result data
6297 * TODO: re-issue the request after certain time? cancel after X results?
6300 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6301 const struct GNUNET_HashCode * key,
6302 const struct GNUNET_PeerIdentity *get_path,
6303 unsigned int get_path_length,
6304 const struct GNUNET_PeerIdentity *put_path,
6305 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6306 size_t size, const void *data)
6308 struct MeshPathInfo *path_info = cls;
6309 struct MeshPeerPath *p;
6310 struct GNUNET_PeerIdentity pi;
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6314 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6317 p = path_build_from_dht (get_path, get_path_length, put_path,
6319 path_add_to_peers (p, GNUNET_NO);
6321 for (i = 0; i < path_info->peer->ntunnels; i++)
6323 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6324 peer_info_connect (path_info->peer, path_info->t);
6332 * Function to process paths received for a new peer addition. The recorded
6333 * paths form the initial tunnel, which can be optimized later.
6334 * Called on each result obtained for the DHT search.
6336 * @param cls closure
6337 * @param exp when will this value expire
6338 * @param key key of the result
6339 * @param get_path path of the get request
6340 * @param get_path_length lenght of get_path
6341 * @param put_path path of the put request
6342 * @param put_path_length length of the put_path
6343 * @param type type of the result
6344 * @param size number of bytes in data
6345 * @param data pointer to the result data
6348 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6349 const struct GNUNET_HashCode * key,
6350 const struct GNUNET_PeerIdentity *get_path,
6351 unsigned int get_path_length,
6352 const struct GNUNET_PeerIdentity *put_path,
6353 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6354 size_t size, const void *data)
6356 const struct PBlock *pb = data;
6357 const struct GNUNET_PeerIdentity *pi = &pb->id;
6358 struct MeshTunnel *t = cls;
6359 struct MeshPeerInfo *peer_info;
6360 struct MeshPeerPath *p;
6362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6363 if (size != sizeof (struct PBlock))
6365 GNUNET_break_op (0);
6368 if (ntohl(pb->type) != t->type)
6370 GNUNET_break_op (0);
6373 GNUNET_assert (NULL != t->owner);
6374 peer_info = peer_info_get (pi);
6375 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6377 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6379 p = path_build_from_dht (get_path, get_path_length, put_path,
6381 path_add_to_peers (p, GNUNET_NO);
6383 tunnel_add_peer (t, peer_info);
6384 peer_info_connect (peer_info, t);
6389 * Function to process DHT string to regex matching.
6390 * Called on each result obtained for the DHT search.
6392 * @param cls closure (search context)
6393 * @param exp when will this value expire
6394 * @param key key of the result
6395 * @param get_path path of the get request (not used)
6396 * @param get_path_length lenght of get_path (not used)
6397 * @param put_path path of the put request (not used)
6398 * @param put_path_length length of the put_path (not used)
6399 * @param type type of the result
6400 * @param size number of bytes in data
6401 * @param data pointer to the result data
6404 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6405 const struct GNUNET_HashCode * key,
6406 const struct GNUNET_PeerIdentity *get_path,
6407 unsigned int get_path_length,
6408 const struct GNUNET_PeerIdentity *put_path,
6409 unsigned int put_path_length,
6410 enum GNUNET_BLOCK_Type type,
6411 size_t size, const void *data)
6413 const struct MeshRegexAccept *block = data;
6414 struct MeshRegexSearchContext *ctx = cls;
6415 struct MeshRegexSearchInfo *info = ctx->info;
6416 struct MeshPeerPath *p;
6417 struct MeshPeerInfo *peer_info;
6419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6422 peer_info = peer_info_get(&block->id);
6423 p = path_build_from_dht (get_path, get_path_length, put_path,
6425 path_add_to_peers (p, GNUNET_NO);
6428 tunnel_add_peer (info->t, peer_info);
6429 peer_info_connect (peer_info, info->t);
6430 if (0 == info->peer)
6432 info->peer = peer_info->id;
6436 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6439 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6440 ®ex_connect_timeout,
6448 * Function to process DHT string to regex matching.
6449 * Called on each result obtained for the DHT search.
6451 * @param cls closure (search context)
6452 * @param exp when will this value expire
6453 * @param key key of the result
6454 * @param get_path path of the get request (not used)
6455 * @param get_path_length lenght of get_path (not used)
6456 * @param put_path path of the put request (not used)
6457 * @param put_path_length length of the put_path (not used)
6458 * @param type type of the result
6459 * @param size number of bytes in data
6460 * @param data pointer to the result data
6462 * TODO: re-issue the request after certain time? cancel after X results?
6465 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6466 const struct GNUNET_HashCode * key,
6467 const struct GNUNET_PeerIdentity *get_path,
6468 unsigned int get_path_length,
6469 const struct GNUNET_PeerIdentity *put_path,
6470 unsigned int put_path_length,
6471 enum GNUNET_BLOCK_Type type,
6472 size_t size, const void *data)
6474 const struct MeshRegexBlock *block = data;
6475 struct MeshRegexSearchContext *ctx = cls;
6476 struct MeshRegexSearchInfo *info = ctx->info;
6480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6481 "DHT GET STRING RETURNED RESULTS\n");
6482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6483 " key: %s\n", GNUNET_h2s (key));
6485 copy = GNUNET_malloc (size);
6486 memcpy (copy, data, size);
6487 GNUNET_break (GNUNET_OK ==
6488 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6489 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6490 len = ntohl (block->n_proof);
6492 char proof[len + 1];
6494 memcpy (proof, &block[1], len);
6496 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6498 GNUNET_break_op (0);
6502 len = strlen (info->description);
6503 if (len == ctx->position) // String processed
6505 if (GNUNET_YES == ntohl (block->accepting))
6507 regex_find_path(key, ctx);
6511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6512 // FIXME REGEX this block not successful, wait for more? start timeout?
6517 regex_next_edge (block, size, ctx);
6522 /******************************************************************************/
6523 /********************* MESH LOCAL HANDLES **************************/
6524 /******************************************************************************/
6528 * Handler for client disconnection
6530 * @param cls closure
6531 * @param client identification of the client; NULL
6532 * for the last call when the server is destroyed
6535 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6537 struct MeshClient *c;
6538 struct MeshClient *next;
6541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6550 if (c->handle != client)
6552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6558 GNUNET_SERVER_client_drop (c->handle);
6559 c->shutting_down = GNUNET_YES;
6560 GNUNET_assert (NULL != c->own_tunnels);
6561 GNUNET_assert (NULL != c->incoming_tunnels);
6562 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6563 &tunnel_destroy_iterator, c);
6564 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6565 &tunnel_destroy_iterator, c);
6566 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6567 &tunnel_destroy_iterator, c);
6568 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6569 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6570 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6572 /* deregister clients applications */
6573 if (NULL != c->apps)
6575 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6576 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6578 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6579 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6581 GNUNET_SCHEDULER_cancel (announce_applications_task);
6582 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6584 if (NULL != c->types)
6585 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6586 for (i = 0; i < c->n_regex; i++)
6588 GNUNET_free (c->regexes[i].regex);
6590 GNUNET_free_non_null (c->regexes);
6591 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6592 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6594 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6597 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6606 * Handler for new clients
6608 * @param cls closure
6609 * @param client identification of the client
6610 * @param message the actual message, which includes messages the client wants
6613 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6614 const struct GNUNET_MessageHeader *message)
6616 struct GNUNET_MESH_ClientConnect *cc_msg;
6617 struct MeshClient *c;
6618 GNUNET_MESH_ApplicationType *a;
6625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6626 /* Check data sanity */
6627 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6628 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6629 ntypes = ntohs (cc_msg->types);
6630 napps = ntohs (cc_msg->applications);
6632 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6635 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6639 /* Create new client structure */
6640 c = GNUNET_malloc (sizeof (struct MeshClient));
6641 c->id = next_client_id++;
6642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6644 GNUNET_SERVER_client_keep (client);
6645 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6648 GNUNET_MESH_ApplicationType at;
6649 struct GNUNET_HashCode hc;
6651 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6652 for (i = 0; i < napps; i++)
6655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6656 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6657 /* store in clients hashmap */
6658 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6659 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6660 /* store in global hashmap, for announcements */
6661 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6662 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6664 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6665 announce_applications_task =
6666 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6672 struct GNUNET_HashCode hc;
6674 t = (uint16_t *) & a[napps];
6675 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6676 for (i = 0; i < ntypes; i++)
6679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6680 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6682 /* store in clients hashmap */
6683 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6685 /* store in global hashmap */
6686 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6687 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6691 " client has %u+%u subscriptions\n", napps, ntypes);
6693 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6694 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6695 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6696 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6697 GNUNET_SERVER_notification_context_add (nc, client);
6698 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6700 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6706 * Handler for clients announcing available services by a regular expression.
6708 * @param cls closure
6709 * @param client identification of the client
6710 * @param message the actual message, which includes messages the client wants
6713 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6714 const struct GNUNET_MessageHeader *message)
6716 struct GNUNET_MESH_RegexAnnounce *msg;
6717 struct MeshRegexDescriptor rd;
6718 struct MeshClient *c;
6722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6724 /* Sanity check for client registration */
6725 if (NULL == (c = client_get (client)))
6728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6733 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6734 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6735 regex = GNUNET_malloc (len + 1);
6736 memcpy (regex, &message[1], len);
6739 rd.compression = ntohs (msg->compression_characters);
6740 GNUNET_array_append (c->regexes, c->n_regex, rd);
6741 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6743 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6749 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6755 * Handler for requests of new tunnels
6757 * @param cls closure
6758 * @param client identification of the client
6759 * @param message the actual message
6762 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6763 const struct GNUNET_MessageHeader *message)
6765 struct GNUNET_MESH_TunnelMessage *t_msg;
6766 struct MeshTunnel *t;
6767 struct MeshClient *c;
6768 MESH_TunnelNumber tid;
6770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6772 /* Sanity check for client registration */
6773 if (NULL == (c = client_get (client)))
6776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6781 /* Message sanity check */
6782 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6789 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6790 /* Sanity check for tunnel numbering */
6791 tid = ntohl (t_msg->tunnel_id);
6792 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6795 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6798 /* Sanity check for duplicate tunnel IDs */
6799 if (NULL != tunnel_get_by_local_id (c, tid))
6802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6806 while (NULL != tunnel_get_by_pi (myid, next_tid))
6807 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6808 t = tunnel_new (myid, next_tid++, c, tid);
6811 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6813 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6816 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6818 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6819 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6822 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6828 * Handler for requests of deleting tunnels
6830 * @param cls closure
6831 * @param client identification of the client
6832 * @param message the actual message
6835 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6836 const struct GNUNET_MessageHeader *message)
6838 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6839 struct MeshClient *c;
6840 struct MeshTunnel *t;
6841 MESH_TunnelNumber tid;
6843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6844 "Got a DESTROY TUNNEL from client!\n");
6846 /* Sanity check for client registration */
6847 if (NULL == (c = client_get (client)))
6850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6855 /* Message sanity check */
6856 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6859 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6863 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6865 /* Retrieve tunnel */
6866 tid = ntohl (tunnel_msg->tunnel_id);
6867 t = tunnel_get_by_local_id(c, tid);
6870 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6872 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6875 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6877 client_ignore_tunnel (c, t);
6879 // TODO: when to destroy incoming tunnel?
6880 if (t->nclients == 0)
6882 GNUNET_assert (GNUNET_YES ==
6883 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6885 GNUNET_assert (GNUNET_YES ==
6886 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6887 &my_full_id.hashPubKey,
6891 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6894 send_client_tunnel_disconnect(t, c);
6895 client_delete_tunnel(c, t);
6897 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6899 tunnel_send_destroy (t);
6900 t->destroy = GNUNET_YES;
6901 // The tunnel will be destroyed when the last message is transmitted.
6902 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6908 * Handler for requests of seeting tunnel's speed.
6910 * @param cls Closure (unused).
6911 * @param client Identification of the client.
6912 * @param message The actual message.
6915 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6916 const struct GNUNET_MessageHeader *message)
6918 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6919 struct MeshClient *c;
6920 struct MeshTunnel *t;
6921 MESH_TunnelNumber tid;
6923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6924 "Got a SPEED request from client!\n");
6926 /* Sanity check for client registration */
6927 if (NULL == (c = client_get (client)))
6930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6936 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6938 /* Retrieve tunnel */
6939 tid = ntohl (tunnel_msg->tunnel_id);
6940 t = tunnel_get_by_local_id(c, tid);
6943 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6949 switch (ntohs(message->type))
6951 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6952 t->speed_min = GNUNET_YES;
6954 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6955 t->speed_min = GNUNET_NO;
6960 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6965 * Handler for requests of seeting tunnel's buffering policy.
6967 * @param cls Closure (unused).
6968 * @param client Identification of the client.
6969 * @param message The actual message.
6972 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6973 const struct GNUNET_MessageHeader *message)
6975 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6976 struct MeshClient *c;
6977 struct MeshTunnel *t;
6978 MESH_TunnelNumber tid;
6980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6981 "Got a BUFFER request from client!\n");
6983 /* Sanity check for client registration */
6984 if (NULL == (c = client_get (client)))
6987 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6992 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6994 /* Retrieve tunnel */
6995 tid = ntohl (tunnel_msg->tunnel_id);
6996 t = tunnel_get_by_local_id(c, tid);
6999 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7001 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7005 switch (ntohs(message->type))
7007 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7008 t->nobuffer = GNUNET_NO;
7010 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7011 t->nobuffer = GNUNET_YES;
7017 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7022 * Handler for connection requests to new peers
7024 * @param cls closure
7025 * @param client identification of the client
7026 * @param message the actual message (PeerControl)
7029 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7030 const struct GNUNET_MessageHeader *message)
7032 struct GNUNET_MESH_PeerControl *peer_msg;
7033 struct MeshPeerInfo *peer_info;
7034 struct MeshClient *c;
7035 struct MeshTunnel *t;
7036 MESH_TunnelNumber tid;
7038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7039 /* Sanity check for client registration */
7040 if (NULL == (c = client_get (client)))
7043 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7048 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7050 /* Sanity check for message size */
7051 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7054 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7058 /* Tunnel exists? */
7059 tid = ntohl (peer_msg->tunnel_id);
7060 t = tunnel_get_by_local_id (c, tid);
7064 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7068 /* Does client own tunnel? */
7069 if (t->owner->handle != client)
7072 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7076 GNUNET_i2s (&peer_msg->peer));
7077 peer_info = peer_info_get (&peer_msg->peer);
7079 tunnel_add_peer (t, peer_info);
7080 peer_info_connect (peer_info, t);
7082 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7088 * Handler for disconnection requests of peers in a tunnel
7090 * @param cls closure
7091 * @param client identification of the client
7092 * @param message the actual message (PeerControl)
7095 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7096 const struct GNUNET_MessageHeader *message)
7098 struct GNUNET_MESH_PeerControl *peer_msg;
7099 struct MeshPeerInfo *peer_info;
7100 struct MeshClient *c;
7101 struct MeshTunnel *t;
7102 MESH_TunnelNumber tid;
7104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7105 /* Sanity check for client registration */
7106 if (NULL == (c = client_get (client)))
7109 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7114 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7116 /* Sanity check for message size */
7117 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7120 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7124 /* Tunnel exists? */
7125 tid = ntohl (peer_msg->tunnel_id);
7126 t = tunnel_get_by_local_id (c, tid);
7130 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7135 /* Does client own tunnel? */
7136 if (t->owner->handle != client)
7139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7144 GNUNET_i2s (&peer_msg->peer));
7145 /* Is the peer in the tunnel? */
7147 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7148 if (NULL == peer_info)
7151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7155 /* Ok, delete peer from tunnel */
7156 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7157 &peer_msg->peer.hashPubKey);
7159 send_destroy_path (t, peer_info->id);
7160 tunnel_delete_peer (t, peer_info->id);
7161 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7166 * Handler for blacklist requests of peers in a tunnel
7168 * @param cls closure
7169 * @param client identification of the client
7170 * @param message the actual message (PeerControl)
7172 * FIXME implement DHT block bloomfilter
7175 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7176 const struct GNUNET_MessageHeader *message)
7178 struct GNUNET_MESH_PeerControl *peer_msg;
7179 struct MeshClient *c;
7180 struct MeshTunnel *t;
7181 MESH_TunnelNumber tid;
7183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7184 /* Sanity check for client registration */
7185 if (NULL == (c = client_get (client)))
7188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7193 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7195 /* Sanity check for message size */
7196 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7203 /* Tunnel exists? */
7204 tid = ntohl (peer_msg->tunnel_id);
7205 t = tunnel_get_by_local_id (c, tid);
7209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7214 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7215 GNUNET_PEER_intern(&peer_msg->peer));
7220 * Handler for unblacklist requests of peers in a tunnel
7222 * @param cls closure
7223 * @param client identification of the client
7224 * @param message the actual message (PeerControl)
7227 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7228 const struct GNUNET_MessageHeader *message)
7230 struct GNUNET_MESH_PeerControl *peer_msg;
7231 struct MeshClient *c;
7232 struct MeshTunnel *t;
7233 MESH_TunnelNumber tid;
7237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7238 /* Sanity check for client registration */
7239 if (NULL == (c = client_get (client)))
7242 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7247 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7249 /* Sanity check for message size */
7250 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7257 /* Tunnel exists? */
7258 tid = ntohl (peer_msg->tunnel_id);
7259 t = tunnel_get_by_local_id (c, tid);
7263 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7268 /* if peer is not known, complain */
7269 pid = GNUNET_PEER_search (&peer_msg->peer);
7276 /* search and remove from list */
7277 for (i = 0; i < t->nblacklisted; i++)
7279 if (t->blacklisted[i] == pid)
7281 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7282 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7287 /* if peer hasn't been blacklisted, complain */
7293 * Handler for connection requests to new peers by type
7295 * @param cls closure
7296 * @param client identification of the client
7297 * @param message the actual message (ConnectPeerByType)
7300 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7301 const struct GNUNET_MessageHeader *message)
7303 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7304 struct MeshClient *c;
7305 struct MeshTunnel *t;
7306 struct GNUNET_HashCode hash;
7307 MESH_TunnelNumber tid;
7309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7310 /* Sanity check for client registration */
7311 if (NULL == (c = client_get (client)))
7314 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7319 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7321 /* Sanity check for message size */
7322 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7323 ntohs (connect_msg->header.size))
7326 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7330 /* Tunnel exists? */
7331 tid = ntohl (connect_msg->tunnel_id);
7332 t = tunnel_get_by_local_id (c, tid);
7336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7340 /* Does client own tunnel? */
7341 if (t->owner->handle != client)
7344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7348 /* Do WE have the service? */
7349 t->type = ntohl (connect_msg->type);
7350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7351 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7352 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7355 /* Yes! Fast forward, add ourselves to the tunnel and send the
7356 * good news to the client, and alert the destination client of
7357 * an incoming tunnel.
7359 * FIXME send a path create to self, avoid code duplication
7361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7362 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7363 peer_info_get (&my_full_id),
7364 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7367 send_client_peer_connected (t, myid);
7368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7369 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7371 t->local_tid_dest = next_local_tid++;
7372 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7373 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7374 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7378 /* Ok, lets find a peer offering the service */
7379 if (NULL != t->dht_get_type)
7381 GNUNET_DHT_get_stop (t->dht_get_type);
7383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7384 GNUNET_h2s (&hash));
7386 GNUNET_DHT_get_start (dht_handle,
7387 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7389 dht_replication_level,
7390 GNUNET_DHT_RO_RECORD_ROUTE |
7391 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7393 &dht_get_type_handler, t);
7395 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7401 * Handler for connection requests to new peers by a string service description.
7403 * @param cls closure
7404 * @param client identification of the client
7405 * @param message the actual message, which includes messages the client wants
7408 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7409 const struct GNUNET_MessageHeader *message)
7411 struct GNUNET_MESH_ConnectPeerByString *msg;
7412 struct MeshRegexSearchContext *ctx;
7413 struct MeshRegexSearchInfo *info;
7414 struct GNUNET_DHT_GetHandle *get_h;
7415 struct GNUNET_HashCode key;
7416 struct MeshTunnel *t;
7417 struct MeshClient *c;
7418 MESH_TunnelNumber tid;
7422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7423 "Connect by string started\n");
7424 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7425 size = htons (message->size);
7427 /* Sanity check for client registration */
7428 if (NULL == (c = client_get (client)))
7431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7436 /* Message size sanity check */
7437 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7444 /* Tunnel exists? */
7445 tid = ntohl (msg->tunnel_id);
7446 t = tunnel_get_by_local_id (c, tid);
7450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7454 /* Does client own tunnel? */
7455 if (t->owner->handle != client)
7458 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7463 " on tunnel %s [%u]\n",
7464 GNUNET_i2s(&my_full_id),
7467 /* Only one connect_by_string allowed at the same time! */
7468 /* FIXME: allow more, return handle at api level to cancel, document */
7469 if (NULL != t->regex_ctx)
7472 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7476 /* Find string itself */
7477 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7478 string = (const char *) &msg[1];
7480 /* Initialize context */
7481 size = GNUNET_REGEX_get_first_key(string, len, &key);
7482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7483 " consumed %u bits out of %u\n", size, len);
7484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7485 " looking for %s\n", GNUNET_h2s (&key));
7487 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7489 info->description = GNUNET_malloc (len + 1);
7490 memcpy (info->description, string, len);
7491 info->description[len] = '\0';
7492 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7493 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7496 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7497 ctx->position = size;
7501 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7503 /* Start search in DHT */
7504 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7505 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7506 &key, /* key to search */
7507 dht_replication_level, /* replication level */
7508 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7509 NULL, /* xquery */ // FIXME BLOOMFILTER
7510 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7511 &dht_get_string_handler, ctx);
7513 GNUNET_break (GNUNET_OK ==
7514 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7517 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7519 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7525 * Handler for client traffic directed to one peer
7527 * @param cls closure
7528 * @param client identification of the client
7529 * @param message the actual message
7532 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7533 const struct GNUNET_MessageHeader *message)
7535 struct MeshClient *c;
7536 struct MeshTunnel *t;
7537 struct MeshPeerInfo *pi;
7538 struct GNUNET_MESH_Unicast *data_msg;
7539 MESH_TunnelNumber tid;
7542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7543 "Got a unicast request from a client!\n");
7545 /* Sanity check for client registration */
7546 if (NULL == (c = client_get (client)))
7549 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7554 data_msg = (struct GNUNET_MESH_Unicast *) message;
7556 /* Sanity check for message size */
7557 size = ntohs (message->size);
7558 if (sizeof (struct GNUNET_MESH_Unicast) +
7559 sizeof (struct GNUNET_MessageHeader) > size)
7562 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7566 /* Tunnel exists? */
7567 tid = ntohl (data_msg->tid);
7568 t = tunnel_get_by_local_id (c, tid);
7572 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7576 /* Is it a local tunnel? Then, does client own the tunnel? */
7577 if (t->owner->handle != client)
7580 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7584 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7585 &data_msg->destination.hashPubKey);
7586 /* Is the selected peer in the tunnel? */
7590 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7594 /* PID should be as expected */
7595 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7599 "Unicast PID, expected %u, got %u\n",
7600 t->fwd_pid + 1, ntohl (data_msg->pid));
7601 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7605 /* Ok, everything is correct, send the message
7606 * (pretend we got it from a mesh peer)
7609 /* Work around const limitation */
7610 char buf[ntohs (message->size)] GNUNET_ALIGN;
7611 struct GNUNET_MESH_Unicast *copy;
7613 copy = (struct GNUNET_MESH_Unicast *) buf;
7614 memcpy (buf, data_msg, size);
7615 copy->oid = my_full_id;
7616 copy->tid = htonl (t->id.tid);
7617 copy->ttl = htonl (default_ttl);
7618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7619 " calling generic handler...\n");
7620 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7623 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7630 * Handler for client traffic directed to the origin
7632 * @param cls closure
7633 * @param client identification of the client
7634 * @param message the actual message
7637 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7638 const struct GNUNET_MessageHeader *message)
7640 struct GNUNET_MESH_ToOrigin *data_msg;
7641 struct MeshTunnelClientInfo *clinfo;
7642 struct MeshClient *c;
7643 struct MeshTunnel *t;
7644 MESH_TunnelNumber tid;
7647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7648 "Got a ToOrigin request from a client!\n");
7649 /* Sanity check for client registration */
7650 if (NULL == (c = client_get (client)))
7653 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7658 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7660 /* Sanity check for message size */
7661 size = ntohs (message->size);
7662 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7663 sizeof (struct GNUNET_MessageHeader) > size)
7666 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7670 /* Tunnel exists? */
7671 tid = ntohl (data_msg->tid);
7672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7673 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7679 t = tunnel_get_by_local_id (c, tid);
7682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7683 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7685 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7689 /* It should be sent by someone who has this as incoming tunnel. */
7690 if (GNUNET_NO == client_knows_tunnel (c, t))
7693 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7697 /* PID should be as expected */
7698 clinfo = tunnel_get_client_fc (t, c);
7699 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7702 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7703 "To Origin PID, expected %u, got %u\n",
7704 clinfo->bck_pid + 1,
7705 ntohl (data_msg->pid));
7706 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7711 /* Ok, everything is correct, send the message
7712 * (pretend we got it from a mesh peer)
7715 char buf[ntohs (message->size)] GNUNET_ALIGN;
7716 struct GNUNET_MESH_ToOrigin *copy;
7718 /* Work around const limitation */
7719 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7720 memcpy (buf, data_msg, size);
7721 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7722 copy->tid = htonl (t->id.tid);
7723 copy->ttl = htonl (default_ttl);
7724 copy->pid = htonl (++(t->bck_pid));
7726 copy->sender = my_full_id;
7727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7728 " calling generic handler...\n");
7729 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7731 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7738 * Handler for client traffic directed to all peers in a tunnel
7740 * @param cls closure
7741 * @param client identification of the client
7742 * @param message the actual message
7745 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7746 const struct GNUNET_MessageHeader *message)
7748 struct MeshClient *c;
7749 struct MeshTunnel *t;
7750 struct GNUNET_MESH_Multicast *data_msg;
7751 MESH_TunnelNumber tid;
7753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7754 "Got a multicast request from a client!\n");
7756 /* Sanity check for client registration */
7757 if (NULL == (c = client_get (client)))
7760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7765 data_msg = (struct GNUNET_MESH_Multicast *) message;
7767 /* Sanity check for message size */
7768 if (sizeof (struct GNUNET_MESH_Multicast) +
7769 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7772 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7776 /* Tunnel exists? */
7777 tid = ntohl (data_msg->tid);
7778 t = tunnel_get_by_local_id (c, tid);
7782 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7785 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7789 /* Does client own tunnel? */
7790 if (t->owner->handle != client)
7793 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7797 /* PID should be as expected */
7798 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7801 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7802 "Multicast PID, expected %u, got %u\n",
7803 t->fwd_pid + 1, ntohl (data_msg->pid));
7804 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7809 char buf[ntohs (message->size)] GNUNET_ALIGN;
7810 struct GNUNET_MESH_Multicast *copy;
7812 copy = (struct GNUNET_MESH_Multicast *) buf;
7813 memcpy (buf, message, ntohs (message->size));
7814 copy->oid = my_full_id;
7815 copy->tid = htonl (t->id.tid);
7816 copy->ttl = htonl (default_ttl);
7817 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7819 " calling generic handler...\n");
7820 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7823 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7829 * Handler for client's ACKs for payload traffic.
7831 * @param cls Closure (unused).
7832 * @param client Identification of the client.
7833 * @param message The actual message.
7836 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7837 const struct GNUNET_MessageHeader *message)
7839 struct GNUNET_MESH_LocalAck *msg;
7840 struct MeshTunnel *t;
7841 struct MeshClient *c;
7842 MESH_TunnelNumber tid;
7845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7846 /* Sanity check for client registration */
7847 if (NULL == (c = client_get (client)))
7850 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7855 msg = (struct GNUNET_MESH_LocalAck *) message;
7857 /* Tunnel exists? */
7858 tid = ntohl (msg->tunnel_id);
7859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7860 t = tunnel_get_by_local_id (c, tid);
7864 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7865 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7870 ack = ntohl (msg->max_pid);
7871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7873 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7874 if (NULL != t->owner && t->owner->handle == client)
7876 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7878 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7882 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7883 tunnel_set_client_fwd_ack (t, c, ack);
7884 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7887 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7894 * Functions to handle messages from clients
7896 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7897 {&handle_local_new_client, NULL,
7898 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7899 {&handle_local_announce_regex, NULL,
7900 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7901 {&handle_local_tunnel_create, NULL,
7902 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7903 sizeof (struct GNUNET_MESH_TunnelMessage)},
7904 {&handle_local_tunnel_destroy, NULL,
7905 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7906 sizeof (struct GNUNET_MESH_TunnelMessage)},
7907 {&handle_local_tunnel_speed, NULL,
7908 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7909 sizeof (struct GNUNET_MESH_TunnelMessage)},
7910 {&handle_local_tunnel_speed, NULL,
7911 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7912 sizeof (struct GNUNET_MESH_TunnelMessage)},
7913 {&handle_local_tunnel_buffer, NULL,
7914 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7915 sizeof (struct GNUNET_MESH_TunnelMessage)},
7916 {&handle_local_tunnel_buffer, NULL,
7917 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7918 sizeof (struct GNUNET_MESH_TunnelMessage)},
7919 {&handle_local_connect_add, NULL,
7920 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7921 sizeof (struct GNUNET_MESH_PeerControl)},
7922 {&handle_local_connect_del, NULL,
7923 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7924 sizeof (struct GNUNET_MESH_PeerControl)},
7925 {&handle_local_blacklist, NULL,
7926 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7927 sizeof (struct GNUNET_MESH_PeerControl)},
7928 {&handle_local_unblacklist, NULL,
7929 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7930 sizeof (struct GNUNET_MESH_PeerControl)},
7931 {&handle_local_connect_by_type, NULL,
7932 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7933 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7934 {&handle_local_connect_by_string, NULL,
7935 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7936 {&handle_local_unicast, NULL,
7937 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7938 {&handle_local_to_origin, NULL,
7939 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7940 {&handle_local_multicast, NULL,
7941 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7942 {&handle_local_ack, NULL,
7943 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7944 sizeof (struct GNUNET_MESH_LocalAck)},
7950 * To be called on core init/fail.
7952 * @param cls service closure
7953 * @param server handle to the server for this service
7954 * @param identity the public identity of this peer
7957 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7958 const struct GNUNET_PeerIdentity *identity)
7961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7962 core_handle = server;
7963 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7967 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7969 GNUNET_abort(); // Try harder
7976 * Method called whenever a given peer connects.
7978 * @param cls closure
7979 * @param peer peer identity this notification is about
7980 * @param atsi performance data for the connection
7981 * @param atsi_count number of records in 'atsi'
7984 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7985 const struct GNUNET_ATS_Information *atsi,
7986 unsigned int atsi_count)
7988 struct MeshPeerInfo *peer_info;
7989 struct MeshPeerPath *path;
7991 DEBUG_CONN ("Peer connected\n");
7992 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7993 peer_info = peer_info_get (peer);
7994 if (myid == peer_info->id)
7996 DEBUG_CONN (" (self)\n");
8001 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8003 path = path_new (2);
8004 path->peers[0] = myid;
8005 path->peers[1] = peer_info->id;
8006 GNUNET_PEER_change_rc (myid, 1);
8007 GNUNET_PEER_change_rc (peer_info->id, 1);
8008 peer_info_add_path (peer_info, path, GNUNET_YES);
8009 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8015 * Method called whenever a peer disconnects.
8017 * @param cls closure
8018 * @param peer peer identity this notification is about
8021 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8023 struct MeshPeerInfo *pi;
8024 struct MeshPeerQueue *q;
8025 struct MeshPeerQueue *n;
8027 DEBUG_CONN ("Peer disconnected\n");
8028 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8038 /* TODO try to reroute this traffic instead */
8039 queue_destroy(q, GNUNET_YES);
8042 if (NULL != pi->core_transmit)
8044 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8045 pi->core_transmit = NULL;
8047 peer_info_remove_path (pi, pi->id, myid);
8050 DEBUG_CONN (" (self)\n");
8052 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8057 /******************************************************************************/
8058 /************************ MAIN FUNCTIONS ****************************/
8059 /******************************************************************************/
8062 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8064 * @param cls closure
8065 * @param key current key code
8066 * @param value value in the hash map
8067 * @return GNUNET_YES if we should continue to iterate,
8071 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8073 struct MeshTunnel *t = value;
8080 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8082 * @param cls closure
8083 * @param key current key code
8084 * @param value value in the hash map
8085 * @return GNUNET_YES if we should continue to iterate,
8089 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8091 struct MeshPeerInfo *p = value;
8092 struct MeshPeerQueue *q;
8093 struct MeshPeerQueue *n;
8101 queue_destroy(q, GNUNET_YES);
8105 peer_info_destroy (p);
8111 * Task run during shutdown.
8117 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8121 if (core_handle != NULL)
8123 GNUNET_CORE_disconnect (core_handle);
8128 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8131 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8132 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8133 if (dht_handle != NULL)
8135 GNUNET_DHT_disconnect (dht_handle);
8140 GNUNET_SERVER_notification_context_destroy (nc);
8143 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8145 GNUNET_SCHEDULER_cancel (announce_id_task);
8146 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8153 * Callback for hostkey read/generation
8156 * @param pk the private key
8157 * @param emsg error message
8160 key_generation_cb (void *cls,
8161 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8164 struct MeshPeerInfo *peer;
8165 struct MeshPeerPath *p;
8170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8171 _("Mesh service could not access hostkey. Exiting.\n"));
8172 GNUNET_SCHEDULER_shutdown ();
8175 my_private_key = pk;
8176 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8177 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8178 &my_full_id.hashPubKey);
8179 myid = GNUNET_PEER_intern (&my_full_id);
8180 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8181 "Mesh for peer [%s] starting\n",
8182 GNUNET_i2s(&my_full_id));
8184 // transport_handle = GNUNET_TRANSPORT_connect(c,
8194 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8197 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8198 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8199 GNUNET_SERVER_disconnect_notify (server_handle,
8200 &handle_local_client_disconnect, NULL);
8204 clients_tail = NULL;
8207 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8208 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8210 /* Create a peer_info for the local peer */
8211 peer = peer_info_get (&my_full_id);
8214 GNUNET_PEER_change_rc (myid, 1);
8215 peer_info_add_path (peer, p, GNUNET_YES);
8216 GNUNET_SERVER_resume (server_handle);
8217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8222 * Process mesh requests.
8224 * @param cls closure
8225 * @param server the initialized server
8226 * @param c configuration to use
8229 run (void *cls, struct GNUNET_SERVER_Handle *server,
8230 const struct GNUNET_CONFIGURATION_Handle *c)
8234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8235 server_handle = server;
8236 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8237 NULL, /* Closure passed to MESH functions */
8238 &core_init, /* Call core_init once connected */
8239 &core_connect, /* Handle connects */
8240 &core_disconnect, /* remove peers on disconnects */
8241 NULL, /* Don't notify about all incoming messages */
8242 GNUNET_NO, /* For header only in notification */
8243 NULL, /* Don't notify about all outbound messages */
8244 GNUNET_NO, /* For header-only out notification */
8245 core_handlers); /* Register these handlers */
8247 if (core_handle == NULL)
8250 GNUNET_SCHEDULER_shutdown ();
8255 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8260 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8262 GNUNET_SCHEDULER_shutdown ();
8267 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8268 &refresh_path_time))
8270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8272 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8273 "mesh", "refresh path time");
8274 GNUNET_SCHEDULER_shutdown ();
8279 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8280 &app_announce_time))
8282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8284 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8285 "mesh", "app announce time");
8286 GNUNET_SCHEDULER_shutdown ();
8289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8292 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8297 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8298 "mesh", "id announce time");
8299 GNUNET_SCHEDULER_shutdown ();
8307 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8308 &unacknowledged_wait_time))
8310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8312 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8313 "mesh", "unacknowledged wait time");
8314 GNUNET_SCHEDULER_shutdown ();
8319 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8324 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8325 "mesh", "connect timeout");
8326 GNUNET_SCHEDULER_shutdown ();
8331 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8336 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8337 "mesh", "max msgs queue");
8338 GNUNET_SCHEDULER_shutdown ();
8343 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8348 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8349 "mesh", "max tunnels");
8350 GNUNET_SCHEDULER_shutdown ();
8355 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8358 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8360 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8361 "mesh", "default ttl", 64);
8366 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8367 &dht_replication_level))
8369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8371 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8372 "mesh", "dht replication level", 10);
8373 dht_replication_level = 10;
8376 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8377 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8378 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8379 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8380 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8382 dht_handle = GNUNET_DHT_connect (c, 64);
8383 if (NULL == dht_handle)
8387 stats = GNUNET_STATISTICS_create ("mesh", c);
8389 GNUNET_SERVER_suspend (server_handle);
8390 /* Scheduled the task to clean up when shutdown is called */
8391 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8393 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8394 GNUNET_free (keyfile);
8399 * The main function for the mesh service.
8401 * @param argc number of arguments from the command line
8402 * @param argv command line arguments
8403 * @return 0 ok, 1 on error
8406 main (int argc, char *const *argv)
8411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8412 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8414 ret = (GNUNET_OK == r) ? 0 : 1;
8415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8419 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8420 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8421 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);