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;
4023 GNUNET_PEER_resolve (id, &peer);
4024 cinfo = tunnel_get_neighbor_fc (t, &peer);
4025 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4027 if (cinfo->bck_ack == ack)
4029 // FIXME fc allow force on poll
4030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4031 " Not sending ACK, not needed\n");
4034 cinfo->bck_ack = ack;
4036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4037 " Sending BCK ACK %u (last sent: %u)\n",
4038 ack, cinfo->bck_ack);
4039 send_ack (t, &peer, ack);
4044 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4046 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4048 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4049 * - implement a size 1 buffer in each client_fc AND children_fc
4050 * to hold at least 1 message per "child".
4051 * problem: violates no buffer policy
4052 * - ack 0 and make "children" poll for transmission slots
4053 * problem: big overhead, extra latency even in low traffic
4056 * @param t Tunnel on which to send the BCK ACKs.
4059 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4062 unsigned int tunnel_delta;
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4066 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4069 /* Find client whom to allow to send to origin (with lowest buffer space) */
4070 for (i = 0; i < t->nclients; i++)
4072 struct MeshTunnelClientInfo *clinfo;
4075 clinfo = &t->clients_fc[i];
4076 delta = clinfo->bck_ack - clinfo->bck_pid;
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4078 t->clients[i]->id, delta);
4080 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4081 (GNUNET_YES == t->nobuffer && 0 == delta))
4085 ack = clinfo->bck_pid;
4086 ack += t->nobuffer ? 1 : tunnel_delta;
4087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4088 " sending ack to client %u: %u\n",
4089 t->clients[i]->id, ack);
4090 send_local_ack (t, t->clients[i], ack);
4091 clinfo->bck_ack = ack;
4095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4096 " not sending ack to client %u (td %u, d %u)\n",
4097 t->clients[i]->id, tunnel_delta, delta);
4104 * Send an ACK informing the children nodes and destination clients about
4105 * the available buffer space.
4106 * If buffering is off, send only on behalf of root (can be self).
4107 * If buffering is on, send when sent to predecessor and buffer space is free.
4108 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4109 * the ACK itself goes "forward" (towards children/clients).
4111 * @param t Tunnel on which to send the ACK.
4112 * @param type Type of message that triggered the ACK transmission.
4115 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4118 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4119 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4120 /* Is it after data to_origin retransmission? */
4123 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4124 if (GNUNET_YES == t->nobuffer)
4126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4127 " Not sending ACK, nobuffer\n");
4131 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4132 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4133 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4139 tunnel_send_clients_bck_ack (t);
4140 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4145 * @brief Re-initiate traffic to this peer if necessary.
4147 * Check if there is traffic queued towards this peer
4148 * and the core transmit handle is NULL (traffic was stalled).
4149 * If so, call core tmt rdy.
4151 * @param cls Closure (unused)
4152 * @param peer_id Short ID of peer to which initiate traffic.
4155 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4157 struct MeshPeerInfo *peer;
4158 struct GNUNET_PeerIdentity id;
4159 struct MeshPeerQueue *q;
4162 peer = peer_info_get_short(peer_id);
4163 if (NULL != peer->core_transmit)
4166 q = queue_get_next(peer);
4169 /* Might br multicast traffic already sent to this particular peer but
4170 * not to other children in this tunnel.
4171 * This way t->queue_n would be > 0 but the queue of this particular peer
4177 GNUNET_PEER_resolve (peer->id, &id);
4178 peer->core_transmit =
4179 GNUNET_CORE_notify_transmit_ready(core_handle,
4182 GNUNET_TIME_UNIT_FOREVER_REL,
4192 * @brief Allow transmission of FWD traffic on this tunnel
4194 * Check if there is traffic queued towards any children
4195 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4197 * @param t Tunnel on which to unlock FWD traffic.
4200 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4202 if (0 == t->fwd_queue_n)
4205 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4210 * @brief Allow transmission of BCK traffic on this tunnel
4212 * Check if there is traffic queued towards the root of the tree
4213 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4215 * @param t Tunnel on which to unlock BCK traffic.
4218 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4220 if (0 == t->bck_queue_n)
4223 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4228 * Send a message to all peers in this tunnel that the tunnel is no longer
4231 * @param t The tunnel whose peers to notify.
4234 tunnel_send_destroy (struct MeshTunnel *t)
4236 struct GNUNET_MESH_TunnelDestroy msg;
4238 msg.header.size = htons (sizeof (msg));
4239 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4240 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4241 msg.tid = htonl (t->id.tid);
4242 tunnel_send_multicast (t, &msg.header);
4247 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4249 * @param cls Closure (Tunnel which to cancel).
4250 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4253 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4255 struct MeshTunnel *t = cls;
4256 struct MeshPeerInfo *peer_info;
4257 struct MeshPeerQueue *pq;
4258 struct MeshPeerQueue *next;
4260 peer_info = peer_info_get_short (neighbor_id);
4261 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4264 if (pq->tunnel == t)
4266 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4267 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4268 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4270 // Should have been removed on destroy children
4273 queue_destroy (pq, GNUNET_YES);
4276 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4278 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4279 peer_info->core_transmit = NULL;
4284 * Destroy the tunnel and free any allocated resources linked to it.
4286 * @param t the tunnel to destroy
4288 * @return GNUNET_OK on success
4291 tunnel_destroy (struct MeshTunnel *t)
4293 struct MeshClient *c;
4294 struct GNUNET_HashCode hash;
4305 struct GNUNET_PeerIdentity id;
4307 GNUNET_PEER_resolve (t->id.oid, &id);
4308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4309 GNUNET_i2s (&id), t->id.tid);
4311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4315 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4316 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4324 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4326 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4333 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4334 for (i = 0; i < t->nclients; i++)
4338 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4344 for (i = 0; i < t->nignore; i++)
4348 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4355 if (t->nclients > 0)
4358 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4363 GNUNET_free (t->clients);
4364 GNUNET_free (t->clients_fc);
4367 if (NULL != t->peers)
4369 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4371 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4374 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4375 &tunnel_destroy_child,
4377 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4378 t->children_fc = NULL;
4380 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4381 tree_destroy (t->tree);
4383 if (NULL != t->regex_ctx)
4384 regex_cancel_search (t->regex_ctx);
4385 if (NULL != t->dht_get_type)
4386 GNUNET_DHT_get_stop (t->dht_get_type);
4387 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4388 GNUNET_SCHEDULER_cancel (t->timeout_task);
4389 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4390 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4393 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4400 * Create a new tunnel
4402 * @param owner Who is the owner of the tunnel (short ID).
4403 * @param tid Tunnel Number of the tunnel.
4404 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4405 * @param local Tunnel Number for the tunnel, for the client point of view.
4407 * @return A new initialized tunnel. NULL on error.
4409 static struct MeshTunnel *
4410 tunnel_new (GNUNET_PEER_Id owner,
4411 MESH_TunnelNumber tid,
4412 struct MeshClient *client,
4413 MESH_TunnelNumber local)
4415 struct MeshTunnel *t;
4416 struct GNUNET_HashCode hash;
4418 if (n_tunnels >= max_tunnels && NULL == client)
4421 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4424 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4425 t->bck_queue_max = t->fwd_queue_max;
4426 t->tree = tree_new (owner);
4428 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4429 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4430 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4431 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4432 t->local_tid = local;
4433 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4435 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4437 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4439 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4440 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4447 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4454 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4456 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4457 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4461 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4471 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4472 * that are no longer needed, as well as nodes of no longer reachable peers.
4473 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4475 * @param t Tunnel from which to remove the path.
4476 * @param peer Short id of the peer which should be removed.
4479 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4481 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4487 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4488 * client when the client disconnects. If the client is not the owner, the
4489 * owner will get notified if no more clients are in the tunnel and the client
4490 * get removed from the tunnel's list.
4492 * @param cls closure (client that is disconnecting)
4493 * @param key the hash of the local tunnel id (used to access the hashmap)
4494 * @param value the value stored at the key (tunnel to destroy)
4496 * @return GNUNET_OK, keep iterating.
4499 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4501 struct MeshTunnel *t = value;
4502 struct MeshClient *c = cls;
4504 send_client_tunnel_disconnect(t, c);
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4508 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4509 tunnel_delete_client(t, c);
4510 client_delete_tunnel(c, t);
4513 tunnel_send_destroy(t);
4515 t->destroy = GNUNET_YES;
4522 * Timeout function, destroys tunnel if called
4524 * @param cls Closure (tunnel to destroy).
4525 * @param tc TaskContext
4528 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4530 struct MeshTunnel *t = cls;
4531 struct GNUNET_PeerIdentity id;
4533 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4534 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4536 GNUNET_PEER_resolve(t->id.oid, &id);
4537 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4538 "Tunnel %s [%X] timed out. Destroying.\n",
4539 GNUNET_i2s(&id), t->id.tid);
4540 send_clients_tunnel_destroy (t);
4545 * Resets the tunnel timeout. Starts it if no timeout was running.
4547 * @param t Tunnel whose timeout to reset.
4549 * TODO use heap to improve efficiency of scheduler.
4552 tunnel_reset_timeout (struct MeshTunnel *t)
4554 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4555 GNUNET_SCHEDULER_cancel (t->timeout_task);
4557 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4558 (refresh_path_time, 4), &tunnel_timeout, t);
4562 /******************************************************************************/
4563 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4564 /******************************************************************************/
4567 * Function to send a create path packet to a peer.
4569 * @param cls closure
4570 * @param size number of bytes available in buf
4571 * @param buf where the callee should write the message
4572 * @return number of bytes written to buf
4575 send_core_path_create (void *cls, size_t size, void *buf)
4577 struct MeshPathInfo *info = cls;
4578 struct GNUNET_MESH_ManipulatePath *msg;
4579 struct GNUNET_PeerIdentity *peer_ptr;
4580 struct MeshTunnel *t = info->t;
4581 struct MeshPeerPath *p = info->path;
4586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4588 sizeof (struct GNUNET_MESH_ManipulatePath) +
4589 p->length * sizeof (struct GNUNET_PeerIdentity);
4591 if (size < size_needed || NULL == buf)
4596 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4597 msg->header.size = htons (size_needed);
4598 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4599 msg->tid = ntohl (t->id.tid);
4602 if (GNUNET_YES == t->speed_min)
4603 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4604 if (GNUNET_YES == t->nobuffer)
4605 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4606 msg->opt = htonl(opt);
4609 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4610 for (i = 0; i < p->length; i++)
4612 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4619 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4625 * Fill the core buffer
4627 * @param cls closure (data itself)
4628 * @param size number of bytes available in buf
4629 * @param buf where the callee should write the message
4631 * @return number of bytes written to buf
4634 send_core_data_multicast (void *cls, size_t size, void *buf)
4636 struct MeshTransmissionDescriptor *info = cls;
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4640 GNUNET_assert (NULL != info);
4641 GNUNET_assert (NULL != info->peer);
4642 total_size = info->mesh_data->data_len;
4643 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4645 if (total_size > size)
4650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4651 memcpy (buf, info->mesh_data->data, total_size);
4654 struct GNUNET_MESH_Multicast *mc;
4655 struct GNUNET_MessageHeader *mh;
4658 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4660 mc = (struct GNUNET_MESH_Multicast *) mh;
4661 mh = (struct GNUNET_MessageHeader *) &mc[1];
4662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4663 " multicast, payload type %s\n",
4664 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4666 " multicast, payload size %u\n", ntohs (mh->size));
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4671 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4675 data_descriptor_decrement_rc (info->mesh_data);
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4684 * Creates a path ack message in buf and frees all unused resources.
4686 * @param cls closure (MeshTransmissionDescriptor)
4687 * @param size number of bytes available in buf
4688 * @param buf where the callee should write the message
4689 * @return number of bytes written to buf
4692 send_core_path_ack (void *cls, size_t size, void *buf)
4694 struct MeshTransmissionDescriptor *info = cls;
4695 struct GNUNET_MESH_PathACK *msg = buf;
4697 GNUNET_assert (NULL != info);
4698 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4703 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4704 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4705 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4706 msg->tid = htonl (info->origin->tid);
4707 msg->peer_id = my_full_id;
4710 /* TODO add signature */
4712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4713 return sizeof (struct GNUNET_MESH_PathACK);
4718 * Free a transmission that was already queued with all resources
4719 * associated to the request.
4721 * @param queue Queue handler to cancel.
4722 * @param clear_cls Is it necessary to free associated cls?
4725 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4727 struct MeshTransmissionDescriptor *dd;
4728 struct MeshPathInfo *path_info;
4729 struct MeshTunnelChildInfo *cinfo;
4730 struct GNUNET_PeerIdentity id;
4734 if (GNUNET_YES == clear_cls)
4736 switch (queue->type)
4738 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4739 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " cancelling TUNNEL_DESTROY\n");
4740 GNUNET_assert (GNUNET_YES == queue->tunnel->destroy);
4741 /* FIXME: don't cancel, send and destroy tunnel in queue_send */
4743 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4744 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4745 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4746 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4747 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4749 " prebuilt message\n");
4750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4752 GNUNET_MESH_DEBUG_M2S(queue->type));
4754 data_descriptor_decrement_rc (dd->mesh_data);
4756 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4758 path_info = queue->cls;
4759 path_destroy (path_info->path);
4763 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4764 " type %s unknown!\n",
4765 GNUNET_MESH_DEBUG_M2S(queue->type));
4767 GNUNET_free_non_null (queue->cls);
4769 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4770 queue->peer->queue_tail,
4773 /* Delete from child_fc in the appropiate tunnel */
4774 max = queue->tunnel->fwd_queue_max;
4775 GNUNET_PEER_resolve (queue->peer->id, &id);
4776 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4779 for (i = 0; i < cinfo->send_buffer_n; i++)
4782 i2 = (cinfo->send_buffer_start + i) % max;
4783 if (cinfo->send_buffer[i2] == queue)
4785 /* Found corresponding entry in the send_buffer. Move all others back. */
4790 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4792 j2 = (cinfo->send_buffer_start + j) % max;
4793 j3 = (cinfo->send_buffer_start + j + 1) % max;
4794 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4797 cinfo->send_buffer[j3] = NULL;
4798 cinfo->send_buffer_n--;
4803 GNUNET_free (queue);
4808 * @brief Get the next transmittable message from the queue.
4810 * This will be the head, except in the case of being a data packet
4811 * not allowed by the destination peer.
4813 * @param peer Destination peer.
4815 * @return The next viable MeshPeerQueue element to send to that peer.
4816 * NULL when there are no transmittable messages.
4818 struct MeshPeerQueue *
4819 queue_get_next (const struct MeshPeerInfo *peer)
4821 struct MeshPeerQueue *q;
4822 struct MeshTunnel *t;
4823 struct MeshTransmissionDescriptor *info;
4824 struct MeshTunnelChildInfo *cinfo;
4825 struct GNUNET_MESH_Unicast *ucast;
4826 struct GNUNET_MESH_ToOrigin *to_orig;
4827 struct GNUNET_MESH_Multicast *mcast;
4828 struct GNUNET_PeerIdentity id;
4832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4833 for (q = peer->queue_head; NULL != q; q = q->next)
4837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4839 GNUNET_MESH_DEBUG_M2S(q->type));
4842 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4843 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4844 pid = ntohl (ucast->pid);
4845 GNUNET_PEER_resolve (info->peer->id, &id);
4846 cinfo = tunnel_get_neighbor_fc(t, &id);
4847 ack = cinfo->fwd_ack;
4849 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4850 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4851 pid = ntohl (to_orig->pid);
4854 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4855 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4856 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4858 // Not a multicast payload: multicast control traffic (destroy, etc)
4861 pid = ntohl (mcast->pid);
4862 GNUNET_PEER_resolve (info->peer->id, &id);
4863 cinfo = tunnel_get_neighbor_fc(t, &id);
4864 ack = cinfo->fwd_ack;
4867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4872 "********* ACK: %u, PID: %u\n",
4874 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4883 "********* NEXT!\n");
4886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4887 "********* nothing found\n");
4893 * Core callback to write a queued packet to core buffer
4895 * @param cls Closure (peer info).
4896 * @param size Number of bytes available in buf.
4897 * @param buf Where the to write the message.
4899 * @return number of bytes written to buf
4902 queue_send (void *cls, size_t size, void *buf)
4904 struct MeshPeerInfo *peer = cls;
4905 struct GNUNET_MessageHeader *msg;
4906 struct MeshPeerQueue *queue;
4907 struct MeshTunnel *t;
4908 struct MeshTunnelChildInfo *cinfo;
4909 struct GNUNET_PeerIdentity dst_id;
4912 peer->core_transmit = NULL;
4915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4916 queue = queue_get_next (peer);
4918 /* Queue has no internal mesh traffic nor sendable payload */
4921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4922 if (NULL == peer->queue_head)
4923 GNUNET_break (0); // Should've been canceled
4926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4928 GNUNET_PEER_resolve (peer->id, &dst_id);
4929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4930 "********* towards %s\n",
4931 GNUNET_i2s(&dst_id));
4932 /* Check if buffer size is enough for the message */
4933 if (queue->size > size)
4935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4936 "********* not enough room, reissue\n");
4937 peer->core_transmit =
4938 GNUNET_CORE_notify_transmit_ready (core_handle,
4941 GNUNET_TIME_UNIT_FOREVER_REL,
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4951 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4955 "********* unicast: t->q (%u/%u)\n",
4956 t->fwd_queue_n, t->fwd_queue_max);
4958 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4965 switch (queue->type)
4968 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4969 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4970 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4971 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4973 "********* raw: %s\n",
4974 GNUNET_MESH_DEBUG_M2S (queue->type));
4976 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4977 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4978 data_size = send_core_data_raw (queue->cls, size, buf);
4979 msg = (struct GNUNET_MessageHeader *) buf;
4980 switch (ntohs (msg->type)) // Type of preconstructed message
4982 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4983 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4985 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4986 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4992 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4995 struct MeshTransmissionDescriptor *info = queue->cls;
4997 if ((1 == info->mesh_data->reference_counter
4998 && GNUNET_YES == t->speed_min)
5000 (info->mesh_data->total_out == info->mesh_data->reference_counter
5001 && GNUNET_NO == t->speed_min))
5003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5004 "********* considered sent\n");
5008 data_size = send_core_data_multicast(queue->cls, size, buf);
5009 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5011 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5013 data_size = send_core_path_create (queue->cls, size, buf);
5015 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5017 data_size = send_core_path_ack (queue->cls, size, buf);
5019 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5021 data_size = send_core_data_multicast (queue->cls, size, buf);
5025 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5026 "********* type unknown: %u\n",
5030 switch (queue->type)
5032 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5033 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5034 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5035 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5036 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5040 "at pos %u (%p) != %p\n",
5041 cinfo->send_buffer_start,
5042 cinfo->send_buffer[cinfo->send_buffer_start],
5045 if (cinfo->send_buffer_n > 0)
5047 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5048 cinfo->send_buffer_n--;
5049 cinfo->send_buffer_start++;
5050 cinfo->send_buffer_start %= t->fwd_queue_max;
5061 /* Free queue, but cls was freed by send_core_* */
5062 queue_destroy (queue, GNUNET_NO);
5064 if (GNUNET_YES == t->destroy)
5066 // FIXME fc tunnel destroy all pending traffic? wait for it?
5067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5071 /* If more data in queue, send next */
5072 queue = queue_get_next(peer);
5075 struct GNUNET_PeerIdentity id;
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5078 GNUNET_PEER_resolve (peer->id, &id);
5079 peer->core_transmit =
5080 GNUNET_CORE_notify_transmit_ready(core_handle,
5083 GNUNET_TIME_UNIT_FOREVER_REL,
5091 if (NULL != peer->queue_head)
5093 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5094 "********* %s stalled\n",
5095 GNUNET_i2s(&my_full_id));
5097 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5098 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS,
5099 &tunnel_poll, cinfo);
5102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5108 * @brief Queue and pass message to core when possible.
5110 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5111 * and accounts for it. In case the queue is full, the message is dropped and
5114 * Otherwise, message is treated as internal and allowed to go regardless of
5117 * @param cls Closure (@c type dependant). It will be used by queue_send to
5118 * build the message to be sent if not already prebuilt.
5119 * @param type Type of the message, 0 for a raw message.
5120 * @param size Size of the message.
5121 * @param dst Neighbor to send message to.
5122 * @param t Tunnel this message belongs to.
5125 queue_add (void *cls, uint16_t type, size_t size,
5126 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5128 struct MeshPeerQueue *queue;
5129 struct MeshTunnelChildInfo *cinfo;
5130 struct GNUNET_PeerIdentity id;
5136 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5137 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5139 n = &t->fwd_queue_n;
5140 max = &t->fwd_queue_max;
5142 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5144 n = &t->bck_queue_n;
5145 max = &t->bck_queue_max;
5151 struct MeshTransmissionDescriptor *td = cls;
5152 struct GNUNET_MESH_ToOrigin *to;
5154 to = td->mesh_data->data;
5156 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5157 "bck pid %u, bck ack %u, msg pid %u\n",
5158 t->bck_pid, t->bck_ack, ntohl(to->pid));
5159 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
5161 return; // Drop message
5165 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5171 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5172 GNUNET_PEER_resolve (dst->id, &id);
5173 if (NULL == dst->core_transmit)
5175 dst->core_transmit =
5176 GNUNET_CORE_notify_transmit_ready (core_handle,
5179 GNUNET_TIME_UNIT_FOREVER_REL,
5185 if (NULL == n) // Is this internal mesh traffic?
5188 // It's payload, keep track of buffer per peer.
5189 cinfo = tunnel_get_neighbor_fc(t, &id);
5190 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5191 if (NULL != cinfo->send_buffer[i])
5193 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5194 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5195 cinfo->send_buffer_start++;
5196 cinfo->send_buffer_start %= t->fwd_queue_max;
5200 cinfo->send_buffer_n++;
5202 cinfo->send_buffer[i] = queue;
5203 if (cinfo->send_buffer_n > t->fwd_queue_max)
5206 cinfo->send_buffer_n = t->fwd_queue_max;
5211 /******************************************************************************/
5212 /******************** MESH NETWORK HANDLERS **************************/
5213 /******************************************************************************/
5217 * Core handler for path creation
5219 * @param cls closure
5220 * @param message message
5221 * @param peer peer identity this notification is about
5222 * @param atsi performance data
5223 * @param atsi_count number of records in 'atsi'
5225 * @return GNUNET_OK to keep the connection open,
5226 * GNUNET_SYSERR to close it (signal serious error)
5229 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5230 const struct GNUNET_MessageHeader *message,
5231 const struct GNUNET_ATS_Information *atsi,
5232 unsigned int atsi_count)
5234 unsigned int own_pos;
5237 MESH_TunnelNumber tid;
5238 struct GNUNET_MESH_ManipulatePath *msg;
5239 struct GNUNET_PeerIdentity *pi;
5240 struct GNUNET_HashCode hash;
5241 struct MeshPeerPath *path;
5242 struct MeshPeerInfo *dest_peer_info;
5243 struct MeshPeerInfo *orig_peer_info;
5244 struct MeshTunnel *t;
5246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5247 "Received a path create msg [%s]\n",
5248 GNUNET_i2s (&my_full_id));
5249 size = ntohs (message->size);
5250 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5252 GNUNET_break_op (0);
5256 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5257 if (size % sizeof (struct GNUNET_PeerIdentity))
5259 GNUNET_break_op (0);
5262 size /= sizeof (struct GNUNET_PeerIdentity);
5265 GNUNET_break_op (0);
5268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5269 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5271 tid = ntohl (msg->tid);
5272 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5274 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5275 t = tunnel_get (pi, tid);
5276 if (NULL == t) // FIXME only for INCOMING tunnels?
5280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5281 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5284 // FIXME notify failure
5287 opt = ntohl (msg->opt);
5288 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5289 GNUNET_YES : GNUNET_NO;
5290 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5292 t->nobuffer = GNUNET_YES;
5293 t->last_fwd_ack = t->fwd_pid + 1;
5295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5296 " speed_min: %d, nobuffer:%d\n",
5297 t->speed_min, t->nobuffer);
5299 if (GNUNET_YES == t->nobuffer)
5301 t->bck_queue_max = 1;
5302 t->fwd_queue_max = 1;
5305 // FIXME only assign a local tid if a local client is interested (on demand)
5306 while (NULL != tunnel_get_incoming (next_local_tid))
5307 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5308 t->local_tid_dest = next_local_tid++;
5309 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5312 tunnel_reset_timeout (t);
5313 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5315 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5316 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5324 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5325 if (NULL == dest_peer_info)
5327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5328 " Creating PeerInfo for destination.\n");
5329 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5330 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5331 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5333 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5335 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5336 if (NULL == orig_peer_info)
5338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5339 " Creating PeerInfo for origin.\n");
5340 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5341 orig_peer_info->id = GNUNET_PEER_intern (pi);
5342 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5343 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5346 path = path_new (size);
5348 for (i = 0; i < size; i++)
5350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5351 GNUNET_i2s (&pi[i]));
5352 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5353 if (path->peers[i] == myid)
5356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5359 /* cannot be self, must be 'not found' */
5360 /* create path: self not found in path through self */
5361 GNUNET_break_op (0);
5362 path_destroy (path);
5366 path_add_to_peers (path, GNUNET_NO);
5367 tunnel_add_path (t, path, own_pos);
5368 if (own_pos == size - 1)
5370 /* It is for us! Send ack. */
5371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5372 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5373 if (NULL == t->peers)
5375 /* New tunnel! Notify clients on first payload message. */
5376 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5378 GNUNET_break (GNUNET_SYSERR !=
5379 GNUNET_CONTAINER_multihashmap_put (t->peers,
5380 &my_full_id.hashPubKey,
5383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5388 struct MeshPeerPath *path2;
5390 /* It's for somebody else! Retransmit. */
5391 path2 = path_duplicate (path);
5392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5393 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5394 path2 = path_duplicate (path);
5395 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5396 send_create_path (dest_peer_info, path, t);
5403 * Core handler for path destruction
5405 * @param cls closure
5406 * @param message message
5407 * @param peer peer identity this notification is about
5408 * @param atsi performance data
5409 * @param atsi_count number of records in 'atsi'
5411 * @return GNUNET_OK to keep the connection open,
5412 * GNUNET_SYSERR to close it (signal serious error)
5415 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5416 const struct GNUNET_MessageHeader *message,
5417 const struct GNUNET_ATS_Information *atsi,
5418 unsigned int atsi_count)
5420 struct GNUNET_MESH_ManipulatePath *msg;
5421 struct GNUNET_PeerIdentity *pi;
5422 struct MeshPeerPath *path;
5423 struct MeshTunnel *t;
5424 unsigned int own_pos;
5428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5429 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5430 size = ntohs (message->size);
5431 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5433 GNUNET_break_op (0);
5437 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5438 if (size % sizeof (struct GNUNET_PeerIdentity))
5440 GNUNET_break_op (0);
5443 size /= sizeof (struct GNUNET_PeerIdentity);
5446 GNUNET_break_op (0);
5449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5451 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5452 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5454 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5456 t = tunnel_get (pi, ntohl (msg->tid));
5459 /* TODO notify back: we don't know this tunnel */
5460 GNUNET_break_op (0);
5463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5464 path = path_new (size);
5466 for (i = 0; i < size; i++)
5468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5469 GNUNET_i2s (&pi[i]));
5470 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5471 if (path->peers[i] == myid)
5474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5475 if (own_pos < path->length - 1)
5476 send_prebuilt_message (message, &pi[own_pos + 1], t);
5478 send_client_tunnel_disconnect(t, NULL);
5480 tunnel_delete_peer (t, path->peers[path->length - 1]);
5481 path_destroy (path);
5487 * Core handler for notifications of broken paths
5489 * @param cls closure
5490 * @param message message
5491 * @param peer peer identity this notification is about
5492 * @param atsi performance data
5493 * @param atsi_count number of records in 'atsi'
5495 * @return GNUNET_OK to keep the connection open,
5496 * GNUNET_SYSERR to close it (signal serious error)
5499 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5500 const struct GNUNET_MessageHeader *message,
5501 const struct GNUNET_ATS_Information *atsi,
5502 unsigned int atsi_count)
5504 struct GNUNET_MESH_PathBroken *msg;
5505 struct MeshTunnel *t;
5507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5508 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5509 msg = (struct GNUNET_MESH_PathBroken *) message;
5510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5511 GNUNET_i2s (&msg->peer1));
5512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5513 GNUNET_i2s (&msg->peer2));
5514 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5517 GNUNET_break_op (0);
5520 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5521 GNUNET_PEER_search (&msg->peer2));
5528 * Core handler for tunnel destruction
5530 * @param cls closure
5531 * @param message message
5532 * @param peer peer identity this notification is about
5533 * @param atsi performance data
5534 * @param atsi_count number of records in 'atsi'
5536 * @return GNUNET_OK to keep the connection open,
5537 * GNUNET_SYSERR to close it (signal serious error)
5540 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5541 const struct GNUNET_MessageHeader *message,
5542 const struct GNUNET_ATS_Information *atsi,
5543 unsigned int atsi_count)
5545 struct GNUNET_MESH_TunnelDestroy *msg;
5546 struct MeshTunnel *t;
5548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5549 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5550 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5552 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5553 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5556 /* Probably already got the message from another path,
5557 * destroyed the tunnel and retransmitted to children.
5560 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5563 if (t->id.oid == myid)
5565 GNUNET_break_op (0);
5568 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5570 /* Tunnel was incoming, notify clients */
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5572 t->local_tid, t->local_tid_dest);
5573 send_clients_tunnel_destroy (t);
5575 tunnel_send_destroy (t);
5576 t->destroy = GNUNET_YES;
5577 // TODO: add timeout to destroy the tunnel anyway
5583 * Core handler for mesh network traffic going from the origin to a peer
5585 * @param cls closure
5586 * @param peer peer identity this notification is about
5587 * @param message message
5588 * @param atsi performance data
5589 * @param atsi_count number of records in 'atsi'
5590 * @return GNUNET_OK to keep the connection open,
5591 * GNUNET_SYSERR to close it (signal serious error)
5594 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5595 const struct GNUNET_MessageHeader *message,
5596 const struct GNUNET_ATS_Information *atsi,
5597 unsigned int atsi_count)
5599 struct GNUNET_MESH_Unicast *msg;
5600 struct GNUNET_PeerIdentity *neighbor;
5601 struct MeshTunnelChildInfo *cinfo;
5602 struct MeshTunnel *t;
5603 GNUNET_PEER_Id dest_id;
5608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5611 size = ntohs (message->size);
5613 sizeof (struct GNUNET_MESH_Unicast) +
5614 sizeof (struct GNUNET_MessageHeader))
5619 msg = (struct GNUNET_MESH_Unicast *) message;
5620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5621 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5623 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5626 /* TODO notify back: we don't know this tunnel */
5627 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5628 GNUNET_break_op (0);
5631 pid = ntohl (msg->pid);
5632 if (t->fwd_pid == pid)
5634 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5635 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5636 " Already seen pid %u, DROPPING!\n", pid);
5641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5642 " pid %u not seen yet, forwarding\n", pid);
5645 t->skip += (pid - t->fwd_pid) - 1;
5648 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5650 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5651 GNUNET_break_op (0);
5652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5653 "Received PID %u, ACK %u\n",
5654 pid, t->last_fwd_ack);
5658 tunnel_reset_timeout (t);
5659 dest_id = GNUNET_PEER_search (&msg->destination);
5660 if (dest_id == myid)
5662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5663 " it's for us! sending to clients...\n");
5664 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5665 send_subscribed_clients (message, &msg[1].header, t);
5666 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5669 ttl = ntohl (msg->ttl);
5670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5673 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5674 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5675 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5679 " not for us, retransmitting...\n");
5681 neighbor = tree_get_first_hop (t->tree, dest_id);
5682 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5683 cinfo->fwd_pid = pid;
5684 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5687 if (GNUNET_YES == t->nobuffer &&
5688 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5690 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5691 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5692 GNUNET_break_op (0);
5695 send_prebuilt_message (message, neighbor, t);
5696 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5702 * Core handler for mesh network traffic going from the origin to all peers
5704 * @param cls closure
5705 * @param message message
5706 * @param peer peer identity this notification is about
5707 * @param atsi performance data
5708 * @param atsi_count number of records in 'atsi'
5709 * @return GNUNET_OK to keep the connection open,
5710 * GNUNET_SYSERR to close it (signal serious error)
5712 * TODO: Check who we got this from, to validate route.
5715 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5716 const struct GNUNET_MessageHeader *message,
5717 const struct GNUNET_ATS_Information *atsi,
5718 unsigned int atsi_count)
5720 struct GNUNET_MESH_Multicast *msg;
5721 struct MeshTunnel *t;
5725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5727 size = ntohs (message->size);
5728 if (sizeof (struct GNUNET_MESH_Multicast) +
5729 sizeof (struct GNUNET_MessageHeader) > size)
5731 GNUNET_break_op (0);
5734 msg = (struct GNUNET_MESH_Multicast *) message;
5735 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5739 /* TODO notify that we dont know that tunnel */
5740 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5741 GNUNET_break_op (0);
5744 pid = ntohl (msg->pid);
5745 if (t->fwd_pid == pid)
5747 /* already seen this packet, drop */
5748 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5750 " Already seen pid %u, DROPPING!\n", pid);
5751 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5757 " pid %u not seen yet, forwarding\n", pid);
5759 t->skip += (pid - t->fwd_pid) - 1;
5761 tunnel_reset_timeout (t);
5763 /* Transmit to locally interested clients */
5764 if (NULL != t->peers &&
5765 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5767 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5768 send_subscribed_clients (message, &msg[1].header, t);
5769 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5772 if (ntohl (msg->ttl) == 0)
5774 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5776 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5779 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5780 tunnel_send_multicast (t, message);
5786 * Core handler for mesh network traffic toward the owner of a tunnel
5788 * @param cls closure
5789 * @param message message
5790 * @param peer peer identity this notification is about
5791 * @param atsi performance data
5792 * @param atsi_count number of records in 'atsi'
5794 * @return GNUNET_OK to keep the connection open,
5795 * GNUNET_SYSERR to close it (signal serious error)
5798 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5799 const struct GNUNET_MessageHeader *message,
5800 const struct GNUNET_ATS_Information *atsi,
5801 unsigned int atsi_count)
5803 struct GNUNET_MESH_ToOrigin *msg;
5804 struct GNUNET_PeerIdentity id;
5805 struct MeshPeerInfo *peer_info;
5806 struct MeshTunnel *t;
5807 struct MeshTunnelChildInfo *cinfo;
5811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5813 size = ntohs (message->size);
5814 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5815 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5817 GNUNET_break_op (0);
5820 msg = (struct GNUNET_MESH_ToOrigin *) message;
5821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5822 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5823 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5824 pid = ntohl (msg->pid);
5828 /* TODO notify that we dont know this tunnel (whom)? */
5829 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5830 GNUNET_break_op (0);
5831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5832 "Received to_origin with PID %u on unknown tunnel\n",
5837 cinfo = tunnel_get_neighbor_fc(t, peer);
5844 if (cinfo->bck_pid == pid)
5846 /* already seen this packet, drop */
5847 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5849 " Already seen pid %u, DROPPING!\n", pid);
5850 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5855 " pid %u not seen yet, forwarding\n", pid);
5856 cinfo->bck_pid = pid;
5858 if (NULL != t->owner)
5861 struct GNUNET_MESH_ToOrigin *copy;
5863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5864 " it's for us! sending to clients...\n");
5865 /* TODO signature verification */
5866 memcpy (cbuf, message, size);
5867 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5868 copy->tid = htonl (t->local_tid);
5870 copy->pid = htonl (t->bck_pid);
5871 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5872 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5873 ©->header, GNUNET_NO);
5874 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5878 " not for us, retransmitting...\n");
5880 peer_info = peer_info_get (&msg->oid);
5881 if (NULL == peer_info)
5883 /* unknown origin of tunnel */
5887 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5888 send_prebuilt_message (message, &id, t);
5889 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5896 * Core handler for mesh network traffic point-to-point acks.
5898 * @param cls closure
5899 * @param message message
5900 * @param peer peer identity this notification is about
5901 * @param atsi performance data
5902 * @param atsi_count number of records in 'atsi'
5904 * @return GNUNET_OK to keep the connection open,
5905 * GNUNET_SYSERR to close it (signal serious error)
5908 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5909 const struct GNUNET_MessageHeader *message,
5910 const struct GNUNET_ATS_Information *atsi,
5911 unsigned int atsi_count)
5913 struct GNUNET_MESH_ACK *msg;
5914 struct MeshTunnel *t;
5917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5919 msg = (struct GNUNET_MESH_ACK *) message;
5921 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5925 /* TODO notify that we dont know this tunnel (whom)? */
5926 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5927 GNUNET_break_op (0);
5930 ack = ntohl (msg->pid);
5931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5933 /* Is this a forward or backward ACK? */
5934 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5936 struct MeshTunnelChildInfo *cinfo;
5939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5940 cinfo = tunnel_get_neighbor_fc (t, peer);
5941 cinfo->fwd_ack = ack;
5942 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5943 tunnel_unlock_fwd_queues (t);
5947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5949 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5950 tunnel_unlock_bck_queue (t);
5957 * Core handler for mesh network traffic point-to-point ack polls.
5959 * @param cls closure
5960 * @param message message
5961 * @param peer peer identity this notification is about
5962 * @param atsi performance data
5963 * @param atsi_count number of records in 'atsi'
5965 * @return GNUNET_OK to keep the connection open,
5966 * GNUNET_SYSERR to close it (signal serious error)
5969 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5970 const struct GNUNET_MessageHeader *message,
5971 const struct GNUNET_ATS_Information *atsi,
5972 unsigned int atsi_count)
5974 struct GNUNET_MESH_Poll *msg;
5975 struct MeshTunnel *t;
5977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5980 msg = (struct GNUNET_MESH_Poll *) message;
5982 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5986 /* TODO notify that we dont know this tunnel (whom)? */
5987 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5988 GNUNET_break_op (0);
5992 /* Is this a forward or backward ACK? */
5993 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5995 struct MeshTunnelChildInfo *cinfo;
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5998 cinfo = tunnel_get_neighbor_fc (t, peer);
5999 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6000 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6005 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6013 * Core handler for path ACKs
6015 * @param cls closure
6016 * @param message message
6017 * @param peer peer identity this notification is about
6018 * @param atsi performance data
6019 * @param atsi_count number of records in 'atsi'
6021 * @return GNUNET_OK to keep the connection open,
6022 * GNUNET_SYSERR to close it (signal serious error)
6025 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6026 const struct GNUNET_MessageHeader *message,
6027 const struct GNUNET_ATS_Information *atsi,
6028 unsigned int atsi_count)
6030 struct GNUNET_MESH_PathACK *msg;
6031 struct GNUNET_PeerIdentity id;
6032 struct MeshPeerInfo *peer_info;
6033 struct MeshPeerPath *p;
6034 struct MeshTunnel *t;
6036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6037 GNUNET_i2s (&my_full_id));
6038 msg = (struct GNUNET_MESH_PathACK *) message;
6039 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6042 /* TODO notify that we don't know the tunnel */
6043 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6045 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6049 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6051 peer_info = peer_info_get (&msg->peer_id);
6052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6053 GNUNET_i2s (&msg->peer_id));
6054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6057 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6060 "connect_by_string completed, stopping search\n");
6061 regex_cancel_search (t->regex_ctx);
6062 t->regex_ctx = NULL;
6065 /* Add paths to peers? */
6066 p = tree_get_path_to_peer (t->tree, peer_info->id);
6069 path_add_to_peers (p, GNUNET_YES);
6077 /* Message for us? */
6078 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6081 if (NULL == t->owner)
6083 GNUNET_break_op (0);
6086 if (NULL != t->dht_get_type)
6088 GNUNET_DHT_get_stop (t->dht_get_type);
6089 t->dht_get_type = NULL;
6091 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6093 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6094 send_client_peer_connected (t, peer_info->id);
6099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6100 " not for us, retransmitting...\n");
6101 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6102 peer_info = peer_info_get (&msg->oid);
6103 if (NULL == peer_info)
6105 /* If we know the tunnel, we should DEFINITELY know the peer */
6109 send_prebuilt_message (message, &id, t);
6115 * Core handler for mesh keepalives.
6117 * @param cls closure
6118 * @param message message
6119 * @param peer peer identity this notification is about
6120 * @param atsi performance data
6121 * @param atsi_count number of records in 'atsi'
6122 * @return GNUNET_OK to keep the connection open,
6123 * GNUNET_SYSERR to close it (signal serious error)
6125 * TODO: Check who we got this from, to validate route.
6128 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6129 const struct GNUNET_MessageHeader *message,
6130 const struct GNUNET_ATS_Information *atsi,
6131 unsigned int atsi_count)
6133 struct GNUNET_MESH_TunnelKeepAlive *msg;
6134 struct MeshTunnel *t;
6136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6139 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6140 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6144 /* TODO notify that we dont know that tunnel */
6145 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6146 GNUNET_break_op (0);
6150 tunnel_reset_timeout (t);
6152 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6153 tunnel_send_multicast (t, message);
6160 * Functions to handle messages from core
6162 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6163 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6164 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6165 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6166 sizeof (struct GNUNET_MESH_PathBroken)},
6167 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6168 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6169 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6170 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6171 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6172 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6173 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6174 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6175 sizeof (struct GNUNET_MESH_ACK)},
6176 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6177 sizeof (struct GNUNET_MESH_Poll)},
6178 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6179 sizeof (struct GNUNET_MESH_PathACK)},
6185 /******************************************************************************/
6186 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6187 /******************************************************************************/
6190 * deregister_app: iterator for removing each application registered by a client
6192 * @param cls closure
6193 * @param key the hash of the application id (used to access the hashmap)
6194 * @param value the value stored at the key (client)
6196 * @return GNUNET_OK on success
6199 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6201 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6202 GNUNET_break (GNUNET_YES ==
6203 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6209 * notify_client_connection_failure: notify a client that the connection to the
6210 * requested remote peer is not possible (for instance, no route found)
6211 * Function called when the socket is ready to queue more data. "buf" will be
6212 * NULL and "size" zero if the socket was closed for writing in the meantime.
6214 * @param cls closure
6215 * @param size number of bytes available in buf
6216 * @param buf where the callee should write the message
6217 * @return number of bytes written to buf
6220 notify_client_connection_failure (void *cls, size_t size, void *buf)
6223 struct MeshPeerInfo *peer_info;
6224 struct GNUNET_MESH_PeerControl *msg;
6225 struct GNUNET_PeerIdentity id;
6227 if (0 == size && NULL == buf)
6229 // TODO retry? cancel?
6233 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6234 peer_info = (struct MeshPeerInfo *) cls;
6235 msg = (struct GNUNET_MESH_PeerControl *) buf;
6236 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6237 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6238 // msg->tunnel_id = htonl(peer_info->t->tid);
6239 GNUNET_PEER_resolve (peer_info->id, &id);
6240 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6248 * Send keepalive packets for a peer
6250 * @param cls Closure (tunnel for which to send the keepalive).
6251 * @param tc Notification context.
6254 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6256 struct MeshTunnel *t = cls;
6257 struct GNUNET_MESH_TunnelKeepAlive *msg;
6258 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6261 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6262 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6268 "sending keepalive for tunnel %d\n", t->id.tid);
6270 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6271 msg->header.size = htons (size);
6272 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6273 msg->oid = my_full_id;
6274 msg->tid = htonl (t->id.tid);
6275 tunnel_send_multicast (t, &msg->header);
6277 t->path_refresh_task =
6278 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6279 tunnel_reset_timeout(t);
6284 * Function to process paths received for a new peer addition. The recorded
6285 * paths form the initial tunnel, which can be optimized later.
6286 * Called on each result obtained for the DHT search.
6288 * @param cls closure
6289 * @param exp when will this value expire
6290 * @param key key of the result
6291 * @param get_path path of the get request
6292 * @param get_path_length lenght of get_path
6293 * @param put_path path of the put request
6294 * @param put_path_length length of the put_path
6295 * @param type type of the result
6296 * @param size number of bytes in data
6297 * @param data pointer to the result data
6299 * TODO: re-issue the request after certain time? cancel after X results?
6302 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6303 const struct GNUNET_HashCode * key,
6304 const struct GNUNET_PeerIdentity *get_path,
6305 unsigned int get_path_length,
6306 const struct GNUNET_PeerIdentity *put_path,
6307 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6308 size_t size, const void *data)
6310 struct MeshPathInfo *path_info = cls;
6311 struct MeshPeerPath *p;
6312 struct GNUNET_PeerIdentity pi;
6315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6316 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6319 p = path_build_from_dht (get_path, get_path_length, put_path,
6321 path_add_to_peers (p, GNUNET_NO);
6323 for (i = 0; i < path_info->peer->ntunnels; i++)
6325 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6326 peer_info_connect (path_info->peer, path_info->t);
6334 * Function to process paths received for a new peer addition. The recorded
6335 * paths form the initial tunnel, which can be optimized later.
6336 * Called on each result obtained for the DHT search.
6338 * @param cls closure
6339 * @param exp when will this value expire
6340 * @param key key of the result
6341 * @param get_path path of the get request
6342 * @param get_path_length lenght of get_path
6343 * @param put_path path of the put request
6344 * @param put_path_length length of the put_path
6345 * @param type type of the result
6346 * @param size number of bytes in data
6347 * @param data pointer to the result data
6350 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6351 const struct GNUNET_HashCode * key,
6352 const struct GNUNET_PeerIdentity *get_path,
6353 unsigned int get_path_length,
6354 const struct GNUNET_PeerIdentity *put_path,
6355 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6356 size_t size, const void *data)
6358 const struct PBlock *pb = data;
6359 const struct GNUNET_PeerIdentity *pi = &pb->id;
6360 struct MeshTunnel *t = cls;
6361 struct MeshPeerInfo *peer_info;
6362 struct MeshPeerPath *p;
6364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6365 if (size != sizeof (struct PBlock))
6367 GNUNET_break_op (0);
6370 if (ntohl(pb->type) != t->type)
6372 GNUNET_break_op (0);
6375 GNUNET_assert (NULL != t->owner);
6376 peer_info = peer_info_get (pi);
6377 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6379 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6381 p = path_build_from_dht (get_path, get_path_length, put_path,
6383 path_add_to_peers (p, GNUNET_NO);
6385 tunnel_add_peer (t, peer_info);
6386 peer_info_connect (peer_info, t);
6391 * Function to process DHT string to regex matching.
6392 * Called on each result obtained for the DHT search.
6394 * @param cls closure (search context)
6395 * @param exp when will this value expire
6396 * @param key key of the result
6397 * @param get_path path of the get request (not used)
6398 * @param get_path_length lenght of get_path (not used)
6399 * @param put_path path of the put request (not used)
6400 * @param put_path_length length of the put_path (not used)
6401 * @param type type of the result
6402 * @param size number of bytes in data
6403 * @param data pointer to the result data
6406 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6407 const struct GNUNET_HashCode * key,
6408 const struct GNUNET_PeerIdentity *get_path,
6409 unsigned int get_path_length,
6410 const struct GNUNET_PeerIdentity *put_path,
6411 unsigned int put_path_length,
6412 enum GNUNET_BLOCK_Type type,
6413 size_t size, const void *data)
6415 const struct MeshRegexAccept *block = data;
6416 struct MeshRegexSearchContext *ctx = cls;
6417 struct MeshRegexSearchInfo *info = ctx->info;
6418 struct MeshPeerPath *p;
6419 struct MeshPeerInfo *peer_info;
6421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6424 peer_info = peer_info_get(&block->id);
6425 p = path_build_from_dht (get_path, get_path_length, put_path,
6427 path_add_to_peers (p, GNUNET_NO);
6430 tunnel_add_peer (info->t, peer_info);
6431 peer_info_connect (peer_info, info->t);
6432 if (0 == info->peer)
6434 info->peer = peer_info->id;
6438 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6441 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6442 ®ex_connect_timeout,
6450 * Function to process DHT string to regex matching.
6451 * Called on each result obtained for the DHT search.
6453 * @param cls closure (search context)
6454 * @param exp when will this value expire
6455 * @param key key of the result
6456 * @param get_path path of the get request (not used)
6457 * @param get_path_length lenght of get_path (not used)
6458 * @param put_path path of the put request (not used)
6459 * @param put_path_length length of the put_path (not used)
6460 * @param type type of the result
6461 * @param size number of bytes in data
6462 * @param data pointer to the result data
6464 * TODO: re-issue the request after certain time? cancel after X results?
6467 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6468 const struct GNUNET_HashCode * key,
6469 const struct GNUNET_PeerIdentity *get_path,
6470 unsigned int get_path_length,
6471 const struct GNUNET_PeerIdentity *put_path,
6472 unsigned int put_path_length,
6473 enum GNUNET_BLOCK_Type type,
6474 size_t size, const void *data)
6476 const struct MeshRegexBlock *block = data;
6477 struct MeshRegexSearchContext *ctx = cls;
6478 struct MeshRegexSearchInfo *info = ctx->info;
6482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6483 "DHT GET STRING RETURNED RESULTS\n");
6484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6485 " key: %s\n", GNUNET_h2s (key));
6487 copy = GNUNET_malloc (size);
6488 memcpy (copy, data, size);
6489 GNUNET_break (GNUNET_OK ==
6490 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6491 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6492 len = ntohl (block->n_proof);
6494 char proof[len + 1];
6496 memcpy (proof, &block[1], len);
6498 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6500 GNUNET_break_op (0);
6504 len = strlen (info->description);
6505 if (len == ctx->position) // String processed
6507 if (GNUNET_YES == ntohl (block->accepting))
6509 regex_find_path(key, ctx);
6513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6514 // FIXME REGEX this block not successful, wait for more? start timeout?
6519 regex_next_edge (block, size, ctx);
6524 /******************************************************************************/
6525 /********************* MESH LOCAL HANDLES **************************/
6526 /******************************************************************************/
6530 * Handler for client disconnection
6532 * @param cls closure
6533 * @param client identification of the client; NULL
6534 * for the last call when the server is destroyed
6537 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6539 struct MeshClient *c;
6540 struct MeshClient *next;
6543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6550 // return; uncomment for regex_profiler
6554 if (c->handle != client)
6556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6562 GNUNET_SERVER_client_drop (c->handle);
6563 c->shutting_down = GNUNET_YES;
6564 GNUNET_assert (NULL != c->own_tunnels);
6565 GNUNET_assert (NULL != c->incoming_tunnels);
6566 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6567 &tunnel_destroy_iterator, c);
6568 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6569 &tunnel_destroy_iterator, c);
6570 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6571 &tunnel_destroy_iterator, c);
6572 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6573 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6574 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6576 /* deregister clients applications */
6577 if (NULL != c->apps)
6579 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6580 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6582 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6583 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6585 GNUNET_SCHEDULER_cancel (announce_applications_task);
6586 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6588 if (NULL != c->types)
6589 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6590 for (i = 0; i < c->n_regex; i++)
6592 GNUNET_free (c->regexes[i].regex);
6594 GNUNET_free_non_null (c->regexes);
6595 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6596 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6598 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6601 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6610 * Handler for new clients
6612 * @param cls closure
6613 * @param client identification of the client
6614 * @param message the actual message, which includes messages the client wants
6617 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6618 const struct GNUNET_MessageHeader *message)
6620 struct GNUNET_MESH_ClientConnect *cc_msg;
6621 struct MeshClient *c;
6622 GNUNET_MESH_ApplicationType *a;
6629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6630 /* Check data sanity */
6631 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6632 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6633 ntypes = ntohs (cc_msg->types);
6634 napps = ntohs (cc_msg->applications);
6636 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6643 /* Create new client structure */
6644 c = GNUNET_malloc (sizeof (struct MeshClient));
6645 c->id = next_client_id++;
6646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6648 GNUNET_SERVER_client_keep (client);
6649 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6652 GNUNET_MESH_ApplicationType at;
6653 struct GNUNET_HashCode hc;
6655 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6656 for (i = 0; i < napps; i++)
6659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6660 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6661 /* store in clients hashmap */
6662 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6663 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6664 /* store in global hashmap, for announcements */
6665 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6666 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6668 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6669 announce_applications_task =
6670 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6676 struct GNUNET_HashCode hc;
6678 t = (uint16_t *) & a[napps];
6679 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6680 for (i = 0; i < ntypes; i++)
6683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6684 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6686 /* store in clients hashmap */
6687 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6688 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6689 /* store in global hashmap */
6690 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6691 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6695 " client has %u+%u subscriptions\n", napps, ntypes);
6697 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6698 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6699 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6700 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6701 GNUNET_SERVER_notification_context_add (nc, client);
6702 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6704 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6710 * Handler for clients announcing available services by a regular expression.
6712 * @param cls closure
6713 * @param client identification of the client
6714 * @param message the actual message, which includes messages the client wants
6717 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6718 const struct GNUNET_MessageHeader *message)
6720 struct GNUNET_MESH_RegexAnnounce *msg;
6721 struct MeshRegexDescriptor rd;
6722 struct MeshClient *c;
6726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6728 /* Sanity check for client registration */
6729 if (NULL == (c = client_get (client)))
6732 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6737 msg = (struct GNUNET_MESH_RegexAnnounce *) message;
6738 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6739 regex = GNUNET_malloc (len + 1);
6740 memcpy (regex, &msg[1], len);
6743 rd.compression = ntohs (msg->compression_characters);
6744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6747 GNUNET_array_append (c->regexes, c->n_regex, rd);
6748 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6750 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6756 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6762 * Handler for requests of new tunnels
6764 * @param cls closure
6765 * @param client identification of the client
6766 * @param message the actual message
6769 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6770 const struct GNUNET_MessageHeader *message)
6772 struct GNUNET_MESH_TunnelMessage *t_msg;
6773 struct MeshTunnel *t;
6774 struct MeshClient *c;
6775 MESH_TunnelNumber tid;
6777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6779 /* Sanity check for client registration */
6780 if (NULL == (c = client_get (client)))
6783 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6788 /* Message sanity check */
6789 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6796 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6797 /* Sanity check for tunnel numbering */
6798 tid = ntohl (t_msg->tunnel_id);
6799 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6802 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6805 /* Sanity check for duplicate tunnel IDs */
6806 if (NULL != tunnel_get_by_local_id (c, tid))
6809 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6813 while (NULL != tunnel_get_by_pi (myid, next_tid))
6814 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6815 t = tunnel_new (myid, next_tid++, c, tid);
6818 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6823 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6825 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6826 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6829 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6835 * Handler for requests of deleting tunnels
6837 * @param cls closure
6838 * @param client identification of the client
6839 * @param message the actual message
6842 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6843 const struct GNUNET_MessageHeader *message)
6845 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6846 struct MeshClient *c;
6847 struct MeshTunnel *t;
6848 MESH_TunnelNumber tid;
6850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6851 "Got a DESTROY TUNNEL from client!\n");
6853 /* Sanity check for client registration */
6854 if (NULL == (c = client_get (client)))
6857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6862 /* Message sanity check */
6863 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6866 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6870 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6872 /* Retrieve tunnel */
6873 tid = ntohl (tunnel_msg->tunnel_id);
6874 t = tunnel_get_by_local_id(c, tid);
6877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6882 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6884 client_ignore_tunnel (c, t);
6886 // TODO: when to destroy incoming tunnel?
6887 if (t->nclients == 0)
6889 GNUNET_assert (GNUNET_YES ==
6890 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6892 GNUNET_assert (GNUNET_YES ==
6893 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6894 &my_full_id.hashPubKey,
6898 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6901 send_client_tunnel_disconnect(t, c);
6902 client_delete_tunnel(c, t);
6904 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6906 tunnel_send_destroy (t);
6907 t->destroy = GNUNET_YES;
6908 // The tunnel will be destroyed when the last message is transmitted.
6909 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6915 * Handler for requests of seeting tunnel's speed.
6917 * @param cls Closure (unused).
6918 * @param client Identification of the client.
6919 * @param message The actual message.
6922 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6923 const struct GNUNET_MessageHeader *message)
6925 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6926 struct MeshClient *c;
6927 struct MeshTunnel *t;
6928 MESH_TunnelNumber tid;
6930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6931 "Got a SPEED request from client!\n");
6933 /* Sanity check for client registration */
6934 if (NULL == (c = client_get (client)))
6937 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6943 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6945 /* Retrieve tunnel */
6946 tid = ntohl (tunnel_msg->tunnel_id);
6947 t = tunnel_get_by_local_id(c, tid);
6950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6952 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6956 switch (ntohs(message->type))
6958 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6959 t->speed_min = GNUNET_YES;
6961 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6962 t->speed_min = GNUNET_NO;
6967 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6972 * Handler for requests of seeting tunnel's buffering policy.
6974 * @param cls Closure (unused).
6975 * @param client Identification of the client.
6976 * @param message The actual message.
6979 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6980 const struct GNUNET_MessageHeader *message)
6982 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6983 struct MeshClient *c;
6984 struct MeshTunnel *t;
6985 MESH_TunnelNumber tid;
6987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6988 "Got a BUFFER request from client!\n");
6990 /* Sanity check for client registration */
6991 if (NULL == (c = client_get (client)))
6994 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6999 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7001 /* Retrieve tunnel */
7002 tid = ntohl (tunnel_msg->tunnel_id);
7003 t = tunnel_get_by_local_id(c, tid);
7006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7008 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7012 switch (ntohs(message->type))
7014 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7015 t->nobuffer = GNUNET_NO;
7017 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7018 t->nobuffer = GNUNET_YES;
7024 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7029 * Handler for connection requests to new peers
7031 * @param cls closure
7032 * @param client identification of the client
7033 * @param message the actual message (PeerControl)
7036 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7037 const struct GNUNET_MessageHeader *message)
7039 struct GNUNET_MESH_PeerControl *peer_msg;
7040 struct MeshPeerInfo *peer_info;
7041 struct MeshClient *c;
7042 struct MeshTunnel *t;
7043 MESH_TunnelNumber tid;
7045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7046 /* Sanity check for client registration */
7047 if (NULL == (c = client_get (client)))
7050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7055 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7057 /* Sanity check for message size */
7058 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7061 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7065 /* Tunnel exists? */
7066 tid = ntohl (peer_msg->tunnel_id);
7067 t = tunnel_get_by_local_id (c, tid);
7071 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7075 /* Does client own tunnel? */
7076 if (t->owner->handle != client)
7079 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7083 GNUNET_i2s (&peer_msg->peer));
7084 peer_info = peer_info_get (&peer_msg->peer);
7086 tunnel_add_peer (t, peer_info);
7087 peer_info_connect (peer_info, t);
7089 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7095 * Handler for disconnection requests of peers in a tunnel
7097 * @param cls closure
7098 * @param client identification of the client
7099 * @param message the actual message (PeerControl)
7102 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7103 const struct GNUNET_MessageHeader *message)
7105 struct GNUNET_MESH_PeerControl *peer_msg;
7106 struct MeshPeerInfo *peer_info;
7107 struct MeshClient *c;
7108 struct MeshTunnel *t;
7109 MESH_TunnelNumber tid;
7111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7112 /* Sanity check for client registration */
7113 if (NULL == (c = client_get (client)))
7116 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7121 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7123 /* Sanity check for message size */
7124 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7131 /* Tunnel exists? */
7132 tid = ntohl (peer_msg->tunnel_id);
7133 t = tunnel_get_by_local_id (c, tid);
7137 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7142 /* Does client own tunnel? */
7143 if (t->owner->handle != client)
7146 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7151 GNUNET_i2s (&peer_msg->peer));
7152 /* Is the peer in the tunnel? */
7154 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7155 if (NULL == peer_info)
7158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7162 /* Ok, delete peer from tunnel */
7163 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7164 &peer_msg->peer.hashPubKey);
7166 send_destroy_path (t, peer_info->id);
7167 tunnel_delete_peer (t, peer_info->id);
7168 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7173 * Handler for blacklist requests of peers in a tunnel
7175 * @param cls closure
7176 * @param client identification of the client
7177 * @param message the actual message (PeerControl)
7179 * FIXME implement DHT block bloomfilter
7182 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7183 const struct GNUNET_MessageHeader *message)
7185 struct GNUNET_MESH_PeerControl *peer_msg;
7186 struct MeshClient *c;
7187 struct MeshTunnel *t;
7188 MESH_TunnelNumber tid;
7190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7191 /* Sanity check for client registration */
7192 if (NULL == (c = client_get (client)))
7195 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7200 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7202 /* Sanity check for message size */
7203 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7206 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7210 /* Tunnel exists? */
7211 tid = ntohl (peer_msg->tunnel_id);
7212 t = tunnel_get_by_local_id (c, tid);
7216 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7221 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7222 GNUNET_PEER_intern(&peer_msg->peer));
7227 * Handler for unblacklist requests of peers in a tunnel
7229 * @param cls closure
7230 * @param client identification of the client
7231 * @param message the actual message (PeerControl)
7234 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7235 const struct GNUNET_MessageHeader *message)
7237 struct GNUNET_MESH_PeerControl *peer_msg;
7238 struct MeshClient *c;
7239 struct MeshTunnel *t;
7240 MESH_TunnelNumber tid;
7244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7245 /* Sanity check for client registration */
7246 if (NULL == (c = client_get (client)))
7249 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7254 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7256 /* Sanity check for message size */
7257 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7260 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7264 /* Tunnel exists? */
7265 tid = ntohl (peer_msg->tunnel_id);
7266 t = tunnel_get_by_local_id (c, tid);
7270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7275 /* if peer is not known, complain */
7276 pid = GNUNET_PEER_search (&peer_msg->peer);
7283 /* search and remove from list */
7284 for (i = 0; i < t->nblacklisted; i++)
7286 if (t->blacklisted[i] == pid)
7288 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7289 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7294 /* if peer hasn't been blacklisted, complain */
7300 * Handler for connection requests to new peers by type
7302 * @param cls closure
7303 * @param client identification of the client
7304 * @param message the actual message (ConnectPeerByType)
7307 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7308 const struct GNUNET_MessageHeader *message)
7310 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7311 struct MeshClient *c;
7312 struct MeshTunnel *t;
7313 struct GNUNET_HashCode hash;
7314 MESH_TunnelNumber tid;
7316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7317 /* Sanity check for client registration */
7318 if (NULL == (c = client_get (client)))
7321 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7326 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7328 /* Sanity check for message size */
7329 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7330 ntohs (connect_msg->header.size))
7333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7337 /* Tunnel exists? */
7338 tid = ntohl (connect_msg->tunnel_id);
7339 t = tunnel_get_by_local_id (c, tid);
7343 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7347 /* Does client own tunnel? */
7348 if (t->owner->handle != client)
7351 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7355 /* Do WE have the service? */
7356 t->type = ntohl (connect_msg->type);
7357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7358 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7359 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7362 /* Yes! Fast forward, add ourselves to the tunnel and send the
7363 * good news to the client, and alert the destination client of
7364 * an incoming tunnel.
7366 * FIXME send a path create to self, avoid code duplication
7368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7369 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7370 peer_info_get (&my_full_id),
7371 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7374 send_client_peer_connected (t, myid);
7375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7376 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7378 t->local_tid_dest = next_local_tid++;
7379 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7380 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7381 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7385 /* Ok, lets find a peer offering the service */
7386 if (NULL != t->dht_get_type)
7388 GNUNET_DHT_get_stop (t->dht_get_type);
7390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7391 GNUNET_h2s (&hash));
7393 GNUNET_DHT_get_start (dht_handle,
7394 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7396 dht_replication_level,
7397 GNUNET_DHT_RO_RECORD_ROUTE |
7398 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7400 &dht_get_type_handler, t);
7402 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7408 * Handler for connection requests to new peers by a string service description.
7410 * @param cls closure
7411 * @param client identification of the client
7412 * @param message the actual message, which includes messages the client wants
7415 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7416 const struct GNUNET_MessageHeader *message)
7418 struct GNUNET_MESH_ConnectPeerByString *msg;
7419 struct MeshRegexSearchContext *ctx;
7420 struct MeshRegexSearchInfo *info;
7421 struct GNUNET_DHT_GetHandle *get_h;
7422 struct GNUNET_HashCode key;
7423 struct MeshTunnel *t;
7424 struct MeshClient *c;
7425 MESH_TunnelNumber tid;
7429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7430 "Connect by string started\n");
7431 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7432 size = htons (message->size);
7434 /* Sanity check for client registration */
7435 if (NULL == (c = client_get (client)))
7438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7443 /* Message size sanity check */
7444 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7447 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7451 /* Tunnel exists? */
7452 tid = ntohl (msg->tunnel_id);
7453 t = tunnel_get_by_local_id (c, tid);
7457 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7461 /* Does client own tunnel? */
7462 if (t->owner->handle != client)
7465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7470 " on tunnel %s [%u]\n",
7471 GNUNET_i2s(&my_full_id),
7474 /* Only one connect_by_string allowed at the same time! */
7475 /* FIXME: allow more, return handle at api level to cancel, document */
7476 if (NULL != t->regex_ctx)
7479 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7483 /* Find string itself */
7484 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7485 string = (const char *) &msg[1];
7487 /* Initialize context */
7488 size = GNUNET_REGEX_get_first_key(string, len, &key);
7489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7490 " consumed %u bits out of %u\n", size, len);
7491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7492 " looking for %s\n", GNUNET_h2s (&key));
7494 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7496 info->description = GNUNET_malloc (len + 1);
7497 memcpy (info->description, string, len);
7498 info->description[len] = '\0';
7499 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7500 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7503 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7504 ctx->position = size;
7508 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7510 /* Start search in DHT */
7511 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7512 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7513 &key, /* key to search */
7514 dht_replication_level, /* replication level */
7515 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7516 NULL, /* xquery */ // FIXME BLOOMFILTER
7517 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7518 &dht_get_string_handler, ctx);
7520 GNUNET_break (GNUNET_OK ==
7521 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7524 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7526 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7532 * Handler for client traffic directed to one peer
7534 * @param cls closure
7535 * @param client identification of the client
7536 * @param message the actual message
7539 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7540 const struct GNUNET_MessageHeader *message)
7542 struct MeshClient *c;
7543 struct MeshTunnel *t;
7544 struct MeshPeerInfo *pi;
7545 struct GNUNET_MESH_Unicast *data_msg;
7546 MESH_TunnelNumber tid;
7549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7550 "Got a unicast request from a client!\n");
7552 /* Sanity check for client registration */
7553 if (NULL == (c = client_get (client)))
7556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7561 data_msg = (struct GNUNET_MESH_Unicast *) message;
7563 /* Sanity check for message size */
7564 size = ntohs (message->size);
7565 if (sizeof (struct GNUNET_MESH_Unicast) +
7566 sizeof (struct GNUNET_MessageHeader) > size)
7569 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7573 /* Tunnel exists? */
7574 tid = ntohl (data_msg->tid);
7575 t = tunnel_get_by_local_id (c, tid);
7579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7583 /* Is it a local tunnel? Then, does client own the tunnel? */
7584 if (t->owner->handle != client)
7587 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7591 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7592 &data_msg->destination.hashPubKey);
7593 /* Is the selected peer in the tunnel? */
7597 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7601 /* PID should be as expected */
7602 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7606 "Unicast PID, expected %u, got %u\n",
7607 t->fwd_pid + 1, ntohl (data_msg->pid));
7608 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7612 /* Ok, everything is correct, send the message
7613 * (pretend we got it from a mesh peer)
7616 /* Work around const limitation */
7617 char buf[ntohs (message->size)] GNUNET_ALIGN;
7618 struct GNUNET_MESH_Unicast *copy;
7620 copy = (struct GNUNET_MESH_Unicast *) buf;
7621 memcpy (buf, data_msg, size);
7622 copy->oid = my_full_id;
7623 copy->tid = htonl (t->id.tid);
7624 copy->ttl = htonl (default_ttl);
7625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7626 " calling generic handler...\n");
7627 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7630 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7637 * Handler for client traffic directed to the origin
7639 * @param cls closure
7640 * @param client identification of the client
7641 * @param message the actual message
7644 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7645 const struct GNUNET_MessageHeader *message)
7647 struct GNUNET_MESH_ToOrigin *data_msg;
7648 struct MeshTunnelClientInfo *clinfo;
7649 struct MeshClient *c;
7650 struct MeshTunnel *t;
7651 MESH_TunnelNumber tid;
7654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7655 "Got a ToOrigin request from a client!\n");
7656 /* Sanity check for client registration */
7657 if (NULL == (c = client_get (client)))
7660 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7665 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7667 /* Sanity check for message size */
7668 size = ntohs (message->size);
7669 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7670 sizeof (struct GNUNET_MessageHeader) > size)
7673 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7677 /* Tunnel exists? */
7678 tid = ntohl (data_msg->tid);
7679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7680 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7683 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7686 t = tunnel_get_by_local_id (c, tid);
7689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7690 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7692 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7696 /* It should be sent by someone who has this as incoming tunnel. */
7697 if (GNUNET_NO == client_knows_tunnel (c, t))
7700 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7704 /* PID should be as expected */
7705 clinfo = tunnel_get_client_fc (t, c);
7706 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7709 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7710 "To Origin PID, expected %u, got %u\n",
7711 clinfo->bck_pid + 1,
7712 ntohl (data_msg->pid));
7713 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7718 /* Ok, everything is correct, send the message
7719 * (pretend we got it from a mesh peer)
7722 char buf[ntohs (message->size)] GNUNET_ALIGN;
7723 struct GNUNET_MESH_ToOrigin *copy;
7725 /* Work around const limitation */
7726 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7727 memcpy (buf, data_msg, size);
7728 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7729 copy->tid = htonl (t->id.tid);
7730 copy->ttl = htonl (default_ttl);
7731 copy->pid = htonl (t->bck_pid + 1);
7733 copy->sender = my_full_id;
7734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7735 " calling generic handler...\n");
7736 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7738 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7745 * Handler for client traffic directed to all peers in a tunnel
7747 * @param cls closure
7748 * @param client identification of the client
7749 * @param message the actual message
7752 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7753 const struct GNUNET_MessageHeader *message)
7755 struct MeshClient *c;
7756 struct MeshTunnel *t;
7757 struct GNUNET_MESH_Multicast *data_msg;
7758 MESH_TunnelNumber tid;
7760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7761 "Got a multicast request from a client!\n");
7763 /* Sanity check for client registration */
7764 if (NULL == (c = client_get (client)))
7767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7772 data_msg = (struct GNUNET_MESH_Multicast *) message;
7774 /* Sanity check for message size */
7775 if (sizeof (struct GNUNET_MESH_Multicast) +
7776 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7783 /* Tunnel exists? */
7784 tid = ntohl (data_msg->tid);
7785 t = tunnel_get_by_local_id (c, tid);
7789 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7790 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7792 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7796 /* Does client own tunnel? */
7797 if (t->owner->handle != client)
7800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7804 /* PID should be as expected */
7805 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7808 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7809 "Multicast PID, expected %u, got %u\n",
7810 t->fwd_pid + 1, ntohl (data_msg->pid));
7811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7816 char buf[ntohs (message->size)] GNUNET_ALIGN;
7817 struct GNUNET_MESH_Multicast *copy;
7819 copy = (struct GNUNET_MESH_Multicast *) buf;
7820 memcpy (buf, message, ntohs (message->size));
7821 copy->oid = my_full_id;
7822 copy->tid = htonl (t->id.tid);
7823 copy->ttl = htonl (default_ttl);
7824 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7826 " calling generic handler...\n");
7827 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7830 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7836 * Handler for client's ACKs for payload traffic.
7838 * @param cls Closure (unused).
7839 * @param client Identification of the client.
7840 * @param message The actual message.
7843 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7844 const struct GNUNET_MessageHeader *message)
7846 struct GNUNET_MESH_LocalAck *msg;
7847 struct MeshTunnel *t;
7848 struct MeshClient *c;
7849 MESH_TunnelNumber tid;
7852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7853 /* Sanity check for client registration */
7854 if (NULL == (c = client_get (client)))
7857 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7862 msg = (struct GNUNET_MESH_LocalAck *) message;
7864 /* Tunnel exists? */
7865 tid = ntohl (msg->tunnel_id);
7866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7867 t = tunnel_get_by_local_id (c, tid);
7871 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7873 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7877 ack = ntohl (msg->max_pid);
7878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7880 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7881 if (NULL != t->owner && t->owner->handle == client)
7883 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7885 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7889 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7890 tunnel_set_client_fwd_ack (t, c, ack);
7891 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7901 * Functions to handle messages from clients
7903 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7904 {&handle_local_new_client, NULL,
7905 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7906 {&handle_local_announce_regex, NULL,
7907 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7908 {&handle_local_tunnel_create, NULL,
7909 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7910 sizeof (struct GNUNET_MESH_TunnelMessage)},
7911 {&handle_local_tunnel_destroy, NULL,
7912 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7913 sizeof (struct GNUNET_MESH_TunnelMessage)},
7914 {&handle_local_tunnel_speed, NULL,
7915 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7916 sizeof (struct GNUNET_MESH_TunnelMessage)},
7917 {&handle_local_tunnel_speed, NULL,
7918 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7919 sizeof (struct GNUNET_MESH_TunnelMessage)},
7920 {&handle_local_tunnel_buffer, NULL,
7921 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7922 sizeof (struct GNUNET_MESH_TunnelMessage)},
7923 {&handle_local_tunnel_buffer, NULL,
7924 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7925 sizeof (struct GNUNET_MESH_TunnelMessage)},
7926 {&handle_local_connect_add, NULL,
7927 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7928 sizeof (struct GNUNET_MESH_PeerControl)},
7929 {&handle_local_connect_del, NULL,
7930 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7931 sizeof (struct GNUNET_MESH_PeerControl)},
7932 {&handle_local_blacklist, NULL,
7933 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7934 sizeof (struct GNUNET_MESH_PeerControl)},
7935 {&handle_local_unblacklist, NULL,
7936 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7937 sizeof (struct GNUNET_MESH_PeerControl)},
7938 {&handle_local_connect_by_type, NULL,
7939 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7940 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7941 {&handle_local_connect_by_string, NULL,
7942 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7943 {&handle_local_unicast, NULL,
7944 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7945 {&handle_local_to_origin, NULL,
7946 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7947 {&handle_local_multicast, NULL,
7948 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7949 {&handle_local_ack, NULL,
7950 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7951 sizeof (struct GNUNET_MESH_LocalAck)},
7957 * To be called on core init/fail.
7959 * @param cls service closure
7960 * @param server handle to the server for this service
7961 * @param identity the public identity of this peer
7964 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7965 const struct GNUNET_PeerIdentity *identity)
7968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7969 core_handle = server;
7970 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7973 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7974 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7976 GNUNET_abort(); // Try harder
7983 * Method called whenever a given peer connects.
7985 * @param cls closure
7986 * @param peer peer identity this notification is about
7987 * @param atsi performance data for the connection
7988 * @param atsi_count number of records in 'atsi'
7991 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7992 const struct GNUNET_ATS_Information *atsi,
7993 unsigned int atsi_count)
7995 struct MeshPeerInfo *peer_info;
7996 struct MeshPeerPath *path;
7998 DEBUG_CONN ("Peer connected\n");
7999 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8000 peer_info = peer_info_get (peer);
8001 if (myid == peer_info->id)
8003 DEBUG_CONN (" (self)\n");
8008 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8010 path = path_new (2);
8011 path->peers[0] = myid;
8012 path->peers[1] = peer_info->id;
8013 GNUNET_PEER_change_rc (myid, 1);
8014 GNUNET_PEER_change_rc (peer_info->id, 1);
8015 peer_info_add_path (peer_info, path, GNUNET_YES);
8016 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8022 * Method called whenever a peer disconnects.
8024 * @param cls closure
8025 * @param peer peer identity this notification is about
8028 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8030 struct MeshPeerInfo *pi;
8031 struct MeshPeerQueue *q;
8032 struct MeshPeerQueue *n;
8034 DEBUG_CONN ("Peer disconnected\n");
8035 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8045 /* TODO try to reroute this traffic instead */
8046 queue_destroy(q, GNUNET_YES);
8049 if (NULL != pi->core_transmit)
8051 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8052 pi->core_transmit = NULL;
8054 peer_info_remove_path (pi, pi->id, myid);
8057 DEBUG_CONN (" (self)\n");
8059 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8064 /******************************************************************************/
8065 /************************ MAIN FUNCTIONS ****************************/
8066 /******************************************************************************/
8069 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8071 * @param cls closure
8072 * @param key current key code
8073 * @param value value in the hash map
8074 * @return GNUNET_YES if we should continue to iterate,
8078 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8080 struct MeshTunnel *t = value;
8087 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8089 * @param cls closure
8090 * @param key current key code
8091 * @param value value in the hash map
8092 * @return GNUNET_YES if we should continue to iterate,
8096 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8098 struct MeshPeerInfo *p = value;
8099 struct MeshPeerQueue *q;
8100 struct MeshPeerQueue *n;
8108 queue_destroy(q, GNUNET_YES);
8112 peer_info_destroy (p);
8118 * Task run during shutdown.
8124 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8128 if (core_handle != NULL)
8130 GNUNET_CORE_disconnect (core_handle);
8135 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8138 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8139 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8140 if (dht_handle != NULL)
8142 GNUNET_DHT_disconnect (dht_handle);
8147 GNUNET_SERVER_notification_context_destroy (nc);
8150 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8152 GNUNET_SCHEDULER_cancel (announce_id_task);
8153 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8160 * Callback for hostkey read/generation
8163 * @param pk the private key
8164 * @param emsg error message
8167 key_generation_cb (void *cls,
8168 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8171 struct MeshPeerInfo *peer;
8172 struct MeshPeerPath *p;
8177 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8178 _("Mesh service could not access hostkey. Exiting.\n"));
8179 GNUNET_SCHEDULER_shutdown ();
8182 my_private_key = pk;
8183 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8184 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8185 &my_full_id.hashPubKey);
8186 myid = GNUNET_PEER_intern (&my_full_id);
8187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8188 "Mesh for peer [%s] starting\n",
8189 GNUNET_i2s(&my_full_id));
8191 // transport_handle = GNUNET_TRANSPORT_connect(c,
8201 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8204 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8205 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8206 GNUNET_SERVER_disconnect_notify (server_handle,
8207 &handle_local_client_disconnect, NULL);
8211 clients_tail = NULL;
8214 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8215 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8217 /* Create a peer_info for the local peer */
8218 peer = peer_info_get (&my_full_id);
8221 GNUNET_PEER_change_rc (myid, 1);
8222 peer_info_add_path (peer, p, GNUNET_YES);
8223 GNUNET_SERVER_resume (server_handle);
8224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8229 * Process mesh requests.
8231 * @param cls closure
8232 * @param server the initialized server
8233 * @param c configuration to use
8236 run (void *cls, struct GNUNET_SERVER_Handle *server,
8237 const struct GNUNET_CONFIGURATION_Handle *c)
8241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8242 server_handle = server;
8243 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8244 NULL, /* Closure passed to MESH functions */
8245 &core_init, /* Call core_init once connected */
8246 &core_connect, /* Handle connects */
8247 &core_disconnect, /* remove peers on disconnects */
8248 NULL, /* Don't notify about all incoming messages */
8249 GNUNET_NO, /* For header only in notification */
8250 NULL, /* Don't notify about all outbound messages */
8251 GNUNET_NO, /* For header-only out notification */
8252 core_handlers); /* Register these handlers */
8254 if (core_handle == NULL)
8257 GNUNET_SCHEDULER_shutdown ();
8262 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8267 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8269 GNUNET_SCHEDULER_shutdown ();
8274 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8275 &refresh_path_time))
8277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8279 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8280 "mesh", "refresh path time");
8281 GNUNET_SCHEDULER_shutdown ();
8286 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8287 &app_announce_time))
8289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8291 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8292 "mesh", "app announce time");
8293 GNUNET_SCHEDULER_shutdown ();
8296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "APP_ANNOUNCE_TIME %llu ms\n", app_announce_time.rel_value);
8299 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8304 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8305 "mesh", "id announce time");
8306 GNUNET_SCHEDULER_shutdown ();
8314 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
8315 &unacknowledged_wait_time))
8317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8319 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8320 "mesh", "unacknowledged wait time");
8321 GNUNET_SCHEDULER_shutdown ();
8326 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8331 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8332 "mesh", "connect timeout");
8333 GNUNET_SCHEDULER_shutdown ();
8338 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8343 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8344 "mesh", "max msgs queue");
8345 GNUNET_SCHEDULER_shutdown ();
8350 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8355 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8356 "mesh", "max tunnels");
8357 GNUNET_SCHEDULER_shutdown ();
8362 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8367 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8368 "mesh", "default ttl", 64);
8373 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8374 &dht_replication_level))
8376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8378 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8379 "mesh", "dht replication level", 10);
8380 dht_replication_level = 10;
8383 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8384 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8385 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8386 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8387 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8389 dht_handle = GNUNET_DHT_connect (c, 64);
8390 if (NULL == dht_handle)
8394 stats = GNUNET_STATISTICS_create ("mesh", c);
8396 GNUNET_SERVER_suspend (server_handle);
8397 /* Scheduled the task to clean up when shutdown is called */
8398 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8400 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8401 GNUNET_free (keyfile);
8406 * The main function for the mesh service.
8408 * @param argc number of arguments from the command line
8409 * @param argv command line arguments
8410 * @return 0 ok, 1 on error
8413 main (int argc, char *const *argv)
8418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8419 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8421 ret = (GNUNET_OK == r) ? 0 : 1;
8422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8426 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8427 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8428 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);