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 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
65 GNUNET_TIME_UNIT_MINUTES,\
68 #if MESH_DEBUG_CONNECTION
69 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
71 #define DEBUG_CONN(...)
75 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
77 #define DEBUG_DHT(...)
84 struct timespec __mesh_start;
85 struct timespec __mesh_end;
86 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
87 #define INTERVAL_END \
89 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
90 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
91 if (__diff < 0) __diff += 1000000000;\
95 #define INTERVAL_SHOW \
97 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
99 #define INTERVAL_START
101 #define INTERVAL_SHOW
104 /******************************************************************************/
105 /************************ DATA STRUCTURES ****************************/
106 /******************************************************************************/
108 /** FWD declaration */
114 * Struct representing a piece of data being sent to other peers
118 /** Tunnel it belongs to. */
119 struct MeshTunnel *t;
121 /** How many remaining neighbors still hav't got it. */
122 unsigned int reference_counter;
124 /** How many remaining neighbors we need to send this to. */
125 unsigned int total_out;
127 /** Size of the data. */
136 * Struct containing info about a queued transmission to this peer
143 struct MeshPeerQueue *next;
148 struct MeshPeerQueue *prev;
151 * Peer this transmission is directed to.
153 struct MeshPeerInfo *peer;
156 * Tunnel this message belongs to.
158 struct MeshTunnel *tunnel;
161 * Pointer to info stucture used as cls.
171 * Size of the message
178 * Struct to store regex information announced by clients.
180 struct MeshRegexDescriptor
183 * Regular expression itself.
188 * Cached DFA for regular expression 'regex'.
190 struct GNUNET_REGEX_Automaton *dfa;
193 * How many characters per edge can we squeeze?
195 uint16_t compression;
199 * Struct containing all info possibly needed to build a package when called
202 struct MeshTransmissionDescriptor
204 /** ID of the tunnel this packet travels in */
205 struct MESH_TunnelID *origin;
207 /** Who was this message being sent to */
208 struct MeshPeerInfo *peer;
210 /** Ultimate destination of the packet */
211 GNUNET_PEER_Id destination;
213 /** Data descriptor */
214 struct MeshData* mesh_data;
219 * Struct containing all information regarding a given peer
229 * Last time we heard from this peer
231 struct GNUNET_TIME_Absolute last_contact;
234 * Task handler for delayed connect task;
236 GNUNET_SCHEDULER_TaskIdentifier connect_task;
239 * Number of attempts to reconnect so far
241 int n_reconnect_attempts;
244 * Paths to reach the peer, ordered by ascending hop count
246 struct MeshPeerPath *path_head;
249 * Paths to reach the peer, ordered by ascending hop count
251 struct MeshPeerPath *path_tail;
254 * Handle to stop the DHT search for a path to this peer
256 struct GNUNET_DHT_GetHandle *dhtget;
259 * Closure given to the DHT GET
261 struct MeshPathInfo *dhtgetcls;
264 * Array of tunnels this peer participates in
265 * (most probably a small amount, therefore not a hashmap)
266 * When the path to the peer changes, notify these tunnels to let them
267 * re-adjust their path trees.
269 struct MeshTunnel **tunnels;
272 * Number of tunnels this peers participates in
274 unsigned int ntunnels;
277 * Transmission queue to core DLL head
279 struct MeshPeerQueue *queue_head;
282 * Transmission queue to core DLL tail
284 struct MeshPeerQueue *queue_tail;
287 * How many messages are in the queue to this peer.
289 unsigned int queue_n;
292 * Handle to for queued transmissions
294 struct GNUNET_CORE_TransmitHandle *core_transmit;
299 * Globally unique tunnel identification (owner + number)
300 * DO NOT USE OVER THE NETWORK
305 * Node that owns the tunnel
310 * Tunnel number to differentiate all the tunnels owned by the node oid
311 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
313 MESH_TunnelNumber tid;
318 * Struct containing all information regarding a tunnel
319 * For an intermediate node the improtant info used will be:
320 * - id Tunnel unique identification
321 * - paths[0] To know where to send it next
322 * - metainfo: ready, speeds, accounting
329 struct MESH_TunnelID id;
332 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
334 MESH_TunnelNumber local_tid;
337 * Local tunnel number for local destination clients (incoming number)
338 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
341 MESH_TunnelNumber local_tid_dest;
344 * Is the speed on the tunnel limited to the slowest peer?
349 * Is the tunnel bufferless (minimum latency)?
354 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
359 * Packet ID of the last bck packet sent (unique counter per hop).
364 * SKIP value for this tunnel.
369 * Force sending ACK? Flag to allow duplicate ACK on POLL.
374 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
375 * Contains the Flow Control info: FWD ACK value received,
376 * last BCK ACK sent, PID and SKIP values.
378 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
381 * Last ACK sent towards the origin (for traffic towards leaf node).
383 uint32_t last_fwd_ack;
386 * BCK ACK value received from the hop towards the owner of the tunnel,
387 * (previous node / owner): up to what message PID can we sent back to him.
392 * How many messages are in the forward queue (towards leaves).
394 unsigned int fwd_queue_n;
397 * How many messages do we accept in the forward queue.
399 unsigned int fwd_queue_max;
402 * How many messages are in the backward queue (towards origin).
404 unsigned int bck_queue_n;
407 * How many messages do we accept in the backward queue.
409 unsigned int bck_queue_max;
412 * Task to poll peer in case of a stall.
414 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
417 * Last time the tunnel was used
419 struct GNUNET_TIME_Absolute timestamp;
422 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
423 * containing peers added by id or by type, not intermediate peers.
425 struct GNUNET_CONTAINER_MultiHashMap *peers;
428 * Number of peers that are connected and potentially ready to receive data
430 unsigned int peers_ready;
433 * Number of peers that have been added to the tunnel
435 unsigned int peers_total;
438 * Client owner of the tunnel, if any
440 struct MeshClient *owner;
443 * Clients that have been informed about and want to stay in the tunnel.
445 struct MeshClient **clients;
448 * Flow control info for each client.
450 struct MeshTunnelClientInfo *clients_fc;
453 * Number of elements in clients/clients_fc
455 unsigned int nclients;
458 * Clients that have been informed but requested to leave the tunnel.
460 struct MeshClient **ignore;
463 * Number of elements in clients
465 unsigned int nignore;
470 GNUNET_PEER_Id *blacklisted;
473 * Number of elements in blacklisted
475 unsigned int nblacklisted;
478 * Bloomfilter (for peer identities) to stop circular routes
480 char bloomfilter[MESH_BLOOM_SIZE];
485 struct MeshTunnelTree *tree;
488 * Application type we are looking for in this tunnel
490 GNUNET_MESH_ApplicationType type;
493 * Used to search peers offering a service
495 struct GNUNET_DHT_GetHandle *dht_get_type;
498 * Initial context of the regex search for a connect_by_string
500 struct MeshRegexSearchContext *regex_ctx;
503 * Task to keep the used paths alive
505 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
508 * Task to destroy the tunnel after timeout
510 * FIXME: merge the two? a tunnel will have either
511 * a path refresh OR a timeout, never both!
513 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
516 * Flag to signal the destruction of the tunnel.
517 * If this is set GNUNET_YES the tunnel will be destroyed
518 * when the queue is empty.
523 * Total messages pending for this tunnels, payload or not.
525 unsigned int pending_messages;
530 * Info about a child node in a tunnel, needed to perform flow control.
532 struct MeshTunnelChildInfo
535 * ID of the child node.
555 * Maximum PID allowed (FWD ACK received).
560 * Last ACK sent to that child (BCK ACK).
565 * Circular buffer pointing to MeshPeerQueue elements for all
566 * payload traffic going to this child.
567 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
569 struct MeshPeerQueue **send_buffer;
572 * Index of the oldest element in the send_buffer.
574 unsigned int send_buffer_start;
577 * How many elements are already in the buffer.
579 unsigned int send_buffer_n;
582 * Tunnel this info is about
584 struct MeshTunnel *t;
587 * Task to poll peer in case of a stall.
589 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
592 * Time to use for next polling call.
594 struct GNUNET_TIME_Relative fc_poll_time;
599 * Info about a leaf client of a tunnel, needed to perform flow control.
601 struct MeshTunnelClientInfo
604 * PID of the last packet sent to the client (FWD).
609 * PID of the last packet received from the client (BCK).
614 * Maximum PID allowed (FWD ACK received).
619 * Last ACK sent to that child (BCK ACK).
627 * Info collected during iteration of child nodes in order to get the ACK value
630 struct MeshTunnelChildIteratorContext
633 * Tunnel whose info is being collected.
635 struct MeshTunnel *t;
638 * Is this context initialized? Is the value in max_child_ack valid?
643 * Maximum child ACK so far.
645 uint32_t max_child_ack;
648 * Number of children nodes
650 unsigned int nchildren;
655 * Info needed to work with tunnel paths and peers
662 struct MeshTunnel *t;
665 * Neighbouring peer to whom we send the packet to
667 struct MeshPeerInfo *peer;
672 struct MeshPeerPath *path;
677 * Struct containing information about a client of the service
684 struct MeshClient *next;
689 struct MeshClient *prev;
692 * Tunnels that belong to this client, indexed by local id
694 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
697 * Tunnels this client has accepted, indexed by incoming local id
699 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
702 * Tunnels this client has rejected, indexed by incoming local id
704 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
706 * Handle to communicate with the client
708 struct GNUNET_SERVER_Client *handle;
711 * Applications that this client has claimed to provide
713 struct GNUNET_CONTAINER_MultiHashMap *apps;
716 * Messages that this client has declared interest in
718 struct GNUNET_CONTAINER_MultiHashMap *types;
721 * Whether the client is active or shutting down (don't send confirmations
722 * to a client that is shutting down.
727 * ID of the client, mainly for debug messages
732 * Regular expressions describing the services offered by this client.
734 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
737 * Number of regular expressions in regexes.
739 unsigned int n_regex;
742 * Task to refresh all regular expresions in the DHT.
744 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
750 * Struct to keep information of searches of services described by a regex
751 * using a user-provided string service description.
753 struct MeshRegexSearchInfo
756 * Which tunnel is this for
758 struct MeshTunnel *t;
761 * User provided description of the searched service.
766 * Part of the description already consumed by the search.
773 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
776 * Results from running DHT GETs.
778 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
781 * Contexts, for each running DHT GET. Free all on end of search.
783 struct MeshRegexSearchContext **contexts;
786 * Number of contexts (branches/steps in search).
788 unsigned int n_contexts;
791 * Peer that is connecting via connect_by_string. When connected, free ctx.
796 * Other peers that are found but not yet being connected to.
798 GNUNET_PEER_Id *peers;
801 * Number of elements in peers.
803 unsigned int n_peers;
806 * Next peer to try to connect to.
811 * Timeout for a connect attempt.
812 * When reached, try to connect to a different peer, if any. If not,
813 * try the same peer again.
815 GNUNET_SCHEDULER_TaskIdentifier timeout;
820 * Struct to keep state of running searches that have consumed a part of
823 struct MeshRegexSearchContext
826 * Part of the description already consumed by
827 * this particular search branch.
832 * Information about the search.
834 struct MeshRegexSearchInfo *info;
837 * We just want to look for one edge, the longer the better.
840 unsigned int longest_match;
843 * Destination hash of the longest match.
845 struct GNUNET_HashCode hash;
848 /******************************************************************************/
849 /************************ DEBUG FUNCTIONS ****************************/
850 /******************************************************************************/
854 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
855 * @param cls string to print
856 * @param success GNUNET_OK if the PUT was transmitted,
857 * GNUNET_NO on timeout,
858 * GNUNET_SYSERR on disconnect from service
859 * after the PUT message was transmitted
860 * (so we don't know if it was received or not)
865 mesh_debug (void *cls, int success)
869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
873 unsigned int debug_fwd_ack;
874 unsigned int debug_bck_ack;
878 /******************************************************************************/
879 /*********************** GLOBAL VARIABLES ****************************/
880 /******************************************************************************/
882 /************************** Configuration parameters **************************/
885 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
887 static struct GNUNET_TIME_Relative refresh_path_time;
890 * How often to PUT local application numbers in the DHT.
892 static struct GNUNET_TIME_Relative app_announce_time;
895 * How often to PUT own ID in the DHT.
897 static struct GNUNET_TIME_Relative id_announce_time;
900 * Maximum time allowed to connect to a peer found by string.
902 static struct GNUNET_TIME_Relative connect_timeout;
905 * Default TTL for payload packets.
907 static unsigned long long default_ttl;
910 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
912 static unsigned long long dht_replication_level;
915 * How many tunnels are we willing to maintain.
916 * Local tunnels are always allowed, even if there are more tunnels than max.
918 static unsigned long long max_tunnels;
921 * How many messages *in total* are we willing to queue, divided by number of
922 * tunnels to get tunnel queue size.
924 static unsigned long long max_msgs_queue;
927 /*************************** Static global variables **************************/
930 * Hostkey generation context
932 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
935 * DLL with all the clients, head.
937 static struct MeshClient *clients;
940 * DLL with all the clients, tail.
942 static struct MeshClient *clients_tail;
945 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
947 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
950 * Number of tunnels known.
952 static unsigned long long n_tunnels;
955 * Tunnels incoming, indexed by MESH_TunnelNumber
956 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
958 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
961 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
963 static struct GNUNET_CONTAINER_MultiHashMap *peers;
966 * Handle to communicate with transport
968 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
971 * Handle to communicate with core.
973 static struct GNUNET_CORE_Handle *core_handle;
978 static struct GNUNET_DHT_Handle *dht_handle;
983 static struct GNUNET_SERVER_Handle *server_handle;
986 * Handle to the statistics service.
988 static struct GNUNET_STATISTICS_Handle *stats;
991 * Notification context, to send messages to local clients.
993 static struct GNUNET_SERVER_NotificationContext *nc;
996 * Local peer own ID (memory efficient handle).
998 static GNUNET_PEER_Id myid;
1001 * Local peer own ID (full value).
1003 static struct GNUNET_PeerIdentity my_full_id;
1008 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1013 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1016 * Tunnel ID for the next created tunnel (global tunnel number).
1018 static MESH_TunnelNumber next_tid;
1021 * Tunnel ID for the next incoming tunnel (local tunnel number).
1023 static MESH_TunnelNumber next_local_tid;
1026 * All application types provided by this peer.
1028 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1031 * All message types clients of this peer are interested in.
1033 static struct GNUNET_CONTAINER_MultiHashMap *types;
1036 * Task to periodically announce provided applications.
1038 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1041 * Task to periodically announce itself in the network.
1043 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1046 * Next ID to assign to a client.
1048 unsigned int next_client_id;
1051 /******************************************************************************/
1052 /*********************** DECLARATIONS **************************/
1053 /******************************************************************************/
1056 * Function to process paths received for a new peer addition. The recorded
1057 * paths form the initial tunnel, which can be optimized later.
1058 * Called on each result obtained for the DHT search.
1060 * @param cls closure
1061 * @param exp when will this value expire
1062 * @param key key of the result
1063 * @param type type of the result
1064 * @param size number of bytes in data
1065 * @param data pointer to the result data
1068 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1069 const struct GNUNET_HashCode * key,
1070 const struct GNUNET_PeerIdentity *get_path,
1071 unsigned int get_path_length,
1072 const struct GNUNET_PeerIdentity *put_path,
1073 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1074 size_t size, const void *data);
1078 * Function to process DHT string to regex matching.
1079 * Called on each result obtained for the DHT search.
1081 * @param cls closure (search context)
1082 * @param exp when will this value expire
1083 * @param key key of the result
1084 * @param get_path path of the get request (not used)
1085 * @param get_path_length lenght of get_path (not used)
1086 * @param put_path path of the put request (not used)
1087 * @param put_path_length length of the put_path (not used)
1088 * @param type type of the result
1089 * @param size number of bytes in data
1090 * @param data pointer to the result data
1092 * TODO: re-issue the request after certain time? cancel after X results?
1095 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1096 const struct GNUNET_HashCode * key,
1097 const struct GNUNET_PeerIdentity *get_path,
1098 unsigned int get_path_length,
1099 const struct GNUNET_PeerIdentity *put_path,
1100 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1101 size_t size, const void *data);
1105 * Function to process DHT string to regex matching.
1106 * Called on each result obtained for the DHT search.
1108 * @param cls closure (search context)
1109 * @param exp when will this value expire
1110 * @param key key of the result
1111 * @param get_path path of the get request (not used)
1112 * @param get_path_length lenght of get_path (not used)
1113 * @param put_path path of the put request (not used)
1114 * @param put_path_length length of the put_path (not used)
1115 * @param type type of the result
1116 * @param size number of bytes in data
1117 * @param data pointer to the result data
1120 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1121 const struct GNUNET_HashCode * key,
1122 const struct GNUNET_PeerIdentity *get_path,
1123 unsigned int get_path_length,
1124 const struct GNUNET_PeerIdentity *put_path,
1125 unsigned int put_path_length,
1126 enum GNUNET_BLOCK_Type type,
1127 size_t size, const void *data);
1131 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1132 * and insert it in the appropiate structures if the peer is not known yet.
1134 * @param peer Short identity of the peer.
1136 * @return Existing or newly created peer info.
1138 static struct MeshPeerInfo *
1139 peer_info_get_short (const GNUNET_PEER_Id peer);
1143 * Try to establish a new connection to this peer.
1144 * Use the best path for the given tunnel.
1145 * If the peer doesn't have any path to it yet, try to get one.
1146 * If the peer already has some path, send a CREATE PATH towards it.
1148 * @param peer PeerInfo of the peer.
1149 * @param t Tunnel for which to create the path, if possible.
1152 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1156 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1157 * needed rescources.
1158 * If peer already exists, reevaluate shortest path and change if different.
1160 * @param t Tunnel we want to add a new peer to
1161 * @param peer PeerInfo of the peer being added
1165 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1169 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1170 * that are no longer needed, as well as nodes of no longer reachable peers.
1171 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1173 * @param t Tunnel from which to remove the path.
1174 * @param peer Short id of the peer which should be removed.
1177 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1181 * Search for a tunnel by global ID using full PeerIdentities.
1183 * @param oid owner of the tunnel.
1184 * @param tid global tunnel number.
1186 * @return tunnel handler, NULL if doesn't exist.
1188 static struct MeshTunnel *
1189 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1193 * Delete an active client from the tunnel.
1199 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1202 * Notify a tunnel that a connection has broken that affects at least
1203 * some of its peers.
1205 * @param t Tunnel affected.
1206 * @param p1 Peer that got disconnected from p2.
1207 * @param p2 Peer that got disconnected from p1.
1209 * @return Short ID of the peer disconnected (either p1 or p2).
1210 * 0 if the tunnel remained unaffected.
1212 static GNUNET_PEER_Id
1213 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1218 * Get the current ack value for a tunnel, for data going from root to leaves,
1219 * taking in account the tunnel mode and the status of all children and clients.
1223 * @return Maximum PID allowed.
1226 tunnel_get_fwd_ack (struct MeshTunnel *t);
1230 * Add a client to a tunnel, initializing all needed data structures.
1232 * @param t Tunnel to which add the client.
1233 * @param c Client which to add to the tunnel.
1236 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1240 * Jump to the next edge, with the longest matching token.
1242 * @param block Block found in the DHT.
1243 * @param size Size of the block.
1244 * @param ctx Context of the search.
1246 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1249 regex_next_edge (const struct MeshRegexBlock *block,
1251 struct MeshRegexSearchContext *ctx);
1255 * Find a path to a peer that offers a regex servcie compatible
1256 * with a given string.
1258 * @param key The key of the accepting state.
1259 * @param ctx Context containing info about the string, tunnel, etc.
1262 regex_find_path (const struct GNUNET_HashCode *key,
1263 struct MeshRegexSearchContext *ctx);
1267 * @brief Queue and pass message to core when possible.
1269 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1270 * and accounts for it. In case the queue is full, the message is dropped and
1273 * Otherwise, message is treated as internal and allowed to go regardless of
1276 * @param cls Closure (@c type dependant). It will be used by queue_send to
1277 * build the message to be sent if not already prebuilt.
1278 * @param type Type of the message, 0 for a raw message.
1279 * @param size Size of the message.
1280 * @param dst Neighbor to send message to.
1281 * @param t Tunnel this message belongs to.
1284 queue_add (void *cls, uint16_t type, size_t size,
1285 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1289 * Free a transmission that was already queued with all resources
1290 * associated to the request.
1292 * @param queue Queue handler to cancel.
1293 * @param clear_cls Is it necessary to free associated cls?
1296 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1300 * @brief Get the next transmittable message from the queue.
1302 * This will be the head, except in the case of being a data packet
1303 * not allowed by the destination peer.
1305 * @param peer Destination peer.
1307 * @return The next viable MeshPeerQueue element to send to that peer.
1308 * NULL when there are no transmittable messages.
1310 struct MeshPeerQueue *
1311 queue_get_next (const struct MeshPeerInfo *peer);
1315 * Core callback to write a queued packet to core buffer
1317 * @param cls Closure (peer info).
1318 * @param size Number of bytes available in buf.
1319 * @param buf Where the to write the message.
1321 * @return number of bytes written to buf
1324 queue_send (void *cls, size_t size, void *buf);
1326 /******************************************************************************/
1327 /************************ ITERATORS ****************************/
1328 /******************************************************************************/
1331 * Iterator over found existing mesh regex blocks that match an ongoing search.
1333 * @param cls closure
1334 * @param key current key code
1335 * @param value value in the hash map
1336 * @return GNUNET_YES if we should continue to iterate,
1340 regex_result_iterator (void *cls,
1341 const struct GNUNET_HashCode * key,
1344 struct MeshRegexBlock *block = value;
1345 struct MeshRegexSearchContext *ctx = cls;
1347 if (GNUNET_YES == ntohl(block->accepting) &&
1348 ctx->position == strlen (ctx->info->description))
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1351 regex_find_path (key, ctx);
1352 return GNUNET_YES; // We found an accept state!
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1357 ctx->position, strlen(ctx->info->description),
1358 ntohl(block->accepting));
1361 regex_next_edge(block, SIZE_MAX, ctx);
1363 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1370 * Iterator over edges in a regex block retrieved from the DHT.
1372 * @param cls Closure (context of the search).
1373 * @param token Token that follows to next state.
1374 * @param len Lenght of token.
1375 * @param key Hash of next state.
1377 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1380 regex_edge_iterator (void *cls,
1383 const struct GNUNET_HashCode *key)
1385 struct MeshRegexSearchContext *ctx = cls;
1386 struct MeshRegexSearchInfo *info = ctx->info;
1390 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1395 current = &info->description[ctx->position];
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1397 current_len = strlen (info->description) - ctx->position;
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* token : %.*s\n", len, token);
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1402 if (len > current_len)
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1405 return GNUNET_YES; // Token too long, wont match
1407 if (0 != strncmp (current, token, len))
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1410 return GNUNET_YES; // Token doesn't match
1413 if (len > ctx->longest_match)
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1416 ctx->longest_match = len;
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1430 * Jump to the next edge, with the longest matching token.
1432 * @param block Block found in the DHT.
1433 * @param size Size of the block.
1434 * @param ctx Context of the search.
1436 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1439 regex_next_edge (const struct MeshRegexBlock *block,
1441 struct MeshRegexSearchContext *ctx)
1443 struct MeshRegexSearchContext *new_ctx;
1444 struct MeshRegexSearchInfo *info = ctx->info;
1445 struct GNUNET_DHT_GetHandle *get_h;
1449 /* Find the longest match for the current string position,
1450 * among tokens in the given block */
1451 ctx->longest_match = 0;
1452 result = GNUNET_MESH_regex_block_iterate (block, size,
1453 ®ex_edge_iterator, ctx);
1454 GNUNET_break (GNUNET_OK == result);
1456 /* Did anything match? */
1457 if (0 == ctx->longest_match)
1460 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1461 new_ctx->info = info;
1462 new_ctx->position = ctx->position + ctx->longest_match;
1463 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1465 /* Check whether we already have a DHT GET running for it */
1467 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1470 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1472 ®ex_result_iterator,
1474 // FIXME: "leaks" new_ctx? avoid keeping it around?
1475 return; // We are already looking for it
1478 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1480 /* Start search in DHT */
1481 rest = &new_ctx->info->description[new_ctx->position];
1483 GNUNET_DHT_get_start (dht_handle, /* handle */
1484 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1485 &ctx->hash, /* key to search */
1486 dht_replication_level, /* replication level */
1487 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1489 // FIXME add BLOOMFILTER to exclude filtered peers
1490 strlen(rest) + 1, /* xquery bits */
1491 // FIXME add BLOOMFILTER SIZE
1492 &dht_get_string_handler, new_ctx);
1494 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1497 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1506 * Iterator over hash map entries to cancel DHT GET requests after a
1507 * successful connect_by_string.
1509 * @param cls Closure (unused).
1510 * @param key Current key code (unused).
1511 * @param value Value in the hash map (get handle).
1512 * @return GNUNET_YES if we should continue to iterate,
1516 regex_cancel_dht_get (void *cls,
1517 const struct GNUNET_HashCode * key,
1520 struct GNUNET_DHT_GetHandle *h = value;
1522 GNUNET_DHT_get_stop (h);
1528 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1529 * search for connect_by_string.
1531 * @param cls Closure (unused).
1532 * @param key Current key code (unused).
1533 * @param value MeshRegexBlock in the hash map.
1534 * @return GNUNET_YES if we should continue to iterate,
1538 regex_free_result (void *cls,
1539 const struct GNUNET_HashCode * key,
1543 GNUNET_free (value);
1549 * Find a path to a peer that offers a regex servcie compatible
1550 * with a given string.
1552 * @param key The key of the accepting state.
1553 * @param ctx Context containing info about the string, tunnel, etc.
1556 regex_find_path (const struct GNUNET_HashCode *key,
1557 struct MeshRegexSearchContext *ctx)
1559 struct GNUNET_DHT_GetHandle *get_h;
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1562 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1563 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1564 key, /* key to search */
1565 dht_replication_level, /* replication level */
1566 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE/* |
1567 GNUNET_DHT_RO_RECORD_ROUTE*/,
1569 // FIXME add BLOOMFILTER to exclude filtered peers
1570 0, /* xquery bits */
1571 // FIXME add BLOOMFILTER SIZE
1572 &dht_get_string_accept_handler, ctx);
1573 GNUNET_break (GNUNET_OK ==
1574 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1577 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1582 * Function called if the connect attempt to a peer found via
1583 * connect_by_string times out. Try to connect to another peer, if any.
1584 * Otherwise try to reconnect to the same peer.
1586 * @param cls Closure (info about regex search).
1587 * @param tc TaskContext.
1590 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1592 struct MeshRegexSearchInfo *info = cls;
1593 struct MeshPeerInfo *peer_info;
1597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1598 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1599 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1607 if (0 < info->n_peers)
1609 // Select next peer, put current in that spot.
1610 id = info->peers[info->i_peer];
1611 info->peers[info->i_peer] = info->peer;
1612 info->i_peer = (info->i_peer + 1) % info->n_peers;
1616 // Try to connect to same peer again.
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1621 peer_info = peer_info_get_short(id);
1622 tunnel_add_peer (info->t, peer_info);
1624 tunnel_delete_peer (info->t, old);
1625 peer_info_connect (peer_info, info->t);
1626 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1627 ®ex_connect_timeout,
1629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1634 * Cancel an ongoing regex search in the DHT and free all resources.
1636 * @param ctx The search context.
1639 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1641 struct MeshRegexSearchInfo *info = ctx->info;
1644 GNUNET_free (info->description);
1645 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1646 ®ex_cancel_dht_get, NULL);
1647 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1648 ®ex_free_result, NULL);
1649 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1650 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1651 info->t->regex_ctx = NULL;
1652 for (i = 0; i < info->n_contexts; i++)
1654 GNUNET_free (info->contexts[i]);
1656 if (0 < info->n_contexts)
1657 GNUNET_free (info->contexts);
1658 if (0 < info->n_peers)
1659 GNUNET_free (info->peers);
1660 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1662 GNUNET_SCHEDULER_cancel(info->timeout);
1668 /******************************************************************************/
1669 /************************ PERIODIC FUNCTIONS ****************************/
1670 /******************************************************************************/
1673 * Announce iterator over for each application provided by the peer
1675 * @param cls closure
1676 * @param key current key code
1677 * @param value value in the hash map
1678 * @return GNUNET_YES if we should continue to
1683 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1685 struct PBlock block;
1686 struct MeshClient *c;
1688 block.id = my_full_id;
1689 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1690 GNUNET_assert(NULL != c);
1691 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1692 if (0 == block.type)
1697 block.type = htonl (block.type);
1699 GNUNET_break (NULL !=
1700 GNUNET_DHT_put (dht_handle, key,
1701 dht_replication_level,
1702 GNUNET_DHT_RO_RECORD_ROUTE |
1703 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1704 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1706 (const char *) &block,
1707 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1708 app_announce_time, NULL, NULL));
1715 * Function called when the DHT regex put is complete.
1717 * @param the 'struct MeshClient' for which we were PUTting
1718 * @param success GNUNET_OK if the PUT was transmitted,
1719 * GNUNET_NO on timeout,
1720 * GNUNET_SYSERR on disconnect from service
1721 * after the PUT message was transmitted
1722 * (so we don't know if it was received or not)
1725 announce_regex_done (void *cls,
1728 struct MeshClient *c = cls;
1735 * Regex callback iterator to store own service description in the DHT.
1737 * @param cls closure.
1738 * @param key hash for current state.
1739 * @param proof proof for current state.
1740 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1741 * @param num_edges number of edges leaving current state.
1742 * @param edges edges leaving current state.
1745 regex_iterator (void *cls,
1746 const struct GNUNET_HashCode *key,
1749 unsigned int num_edges,
1750 const struct GNUNET_REGEX_Edge *edges)
1752 struct MeshRegexBlock *block;
1753 struct MeshRegexEdge *block_edge;
1754 enum GNUNET_DHT_RouteOption opt;
1758 unsigned int offset;
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 " regex dht put for state %s\n",
1764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1772 if (GNUNET_YES == accepting)
1774 struct MeshRegexAccept block;
1776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1777 " state %s is accepting, putting own id\n",
1779 size = sizeof (block);
1781 block.id = my_full_id;
1782 GNUNET_STATISTICS_update (stats, "# regex accepting blocks stored",
1784 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes stored",
1785 sizeof (block), GNUNET_NO);
1787 GNUNET_DHT_put(dht_handle, key,
1788 dht_replication_level,
1789 opt/* | GNUNET_DHT_RO_RECORD_ROUTE*/,
1790 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1793 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: expiration time should be option */
1797 len = strlen(proof);
1798 size = sizeof (struct MeshRegexBlock) + len;
1799 block = GNUNET_malloc (size);
1802 block->n_proof = htonl (len);
1803 block->n_edges = htonl (num_edges);
1804 block->accepting = htonl (accepting);
1806 /* Store the proof at the end of the block. */
1807 aux = (char *) &block[1];
1808 memcpy (aux, proof, len);
1811 /* Store each edge in a variable length MeshEdge struct at the
1812 * very end of the MeshRegexBlock structure.
1814 for (i = 0; i < num_edges; i++)
1816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1817 " edge %s towards %s\n",
1819 GNUNET_h2s(&edges[i].destination));
1821 /* aux points at the end of the last block */
1822 len = strlen (edges[i].label);
1823 size += sizeof (struct MeshRegexEdge) + len;
1824 // Calculate offset FIXME is this ok? use size instead?
1825 offset = aux - (char *) block;
1826 block = GNUNET_realloc (block, size);
1827 aux = &((char *) block)[offset];
1828 block_edge = (struct MeshRegexEdge *) aux;
1829 block_edge->key = edges[i].destination;
1830 block_edge->n_token = htonl (len);
1831 aux = (char *) &block_edge[1];
1832 memcpy (aux, edges[i].label, len);
1836 GNUNET_DHT_put(dht_handle, key,
1837 dht_replication_level,
1839 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1841 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1844 GNUNET_STATISTICS_update (stats, "# regex blocks stored",
1846 GNUNET_STATISTICS_update (stats, "# regex block bytes stored",
1849 GNUNET_free (block);
1854 * Store the regular expression describing a local service into the DHT.
1856 * @param regex The regular expresion.
1859 regex_put (struct MeshRegexDescriptor *regex)
1861 if (NULL == regex->dfa)
1863 regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1864 strlen (regex->regex),
1865 regex->compression);
1868 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1869 GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL);
1870 DEBUG_DHT (" regex_put (%s) end\n", regex->regex);
1875 * Periodically announce what applications are provided by local clients
1878 * @param cls closure
1879 * @param tc task context
1882 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1884 struct MeshClient *c = cls;
1887 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1888 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1890 DEBUG_DHT ("Starting PUT for regex\n");
1891 for (i = 0; i < c->n_regex; i++)
1892 regex_put (&c->regexes[i]);
1893 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1896 DEBUG_DHT ("Finished PUT for regex\n");
1901 * Periodically announce what applications are provided by local clients
1904 * @param cls closure
1905 * @param tc task context
1908 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1910 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1912 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1916 DEBUG_DHT ("Starting PUT for apps\n");
1918 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1920 announce_applications_task =
1921 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1923 DEBUG_DHT ("Finished PUT for apps\n");
1928 * Periodically announce self id in the DHT
1930 * @param cls closure
1931 * @param tc task context
1934 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1936 struct PBlock block;
1938 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1940 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1944 * - Set data expiration in function of X
1945 * - Adapt X to churn
1947 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1949 block.id = my_full_id;
1950 block.type = htonl (0);
1951 GNUNET_DHT_put (dht_handle, /* DHT handle */
1952 &my_full_id.hashPubKey, /* Key to use */
1953 dht_replication_level, /* Replication level */
1954 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1955 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1956 sizeof (block), /* Size of the data */
1957 (const char *) &block, /* Data itself */
1958 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1959 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1960 NULL, /* Continuation */
1961 NULL); /* Continuation closure */
1963 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1967 /******************************************************************************/
1968 /****************** GENERAL HELPER FUNCTIONS ************************/
1969 /******************************************************************************/
1972 * Decrements the reference counter and frees all resources if needed
1974 * @param mesh_data Data Descriptor used in a multicast message.
1975 * Freed no longer needed (last message).
1978 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1980 if (0 == --(mesh_data->reference_counter))
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1983 GNUNET_free (mesh_data->data);
1984 GNUNET_free (mesh_data);
1990 * Check if client has registered with the service and has not disconnected
1992 * @param client the client to check
1994 * @return non-NULL if client exists in the global DLL
1996 static struct MeshClient *
1997 client_get (struct GNUNET_SERVER_Client *client)
1999 struct MeshClient *c;
2004 if (c->handle == client)
2013 * Checks if a given client has subscribed to certain message type
2015 * @param message_type Type of message to check
2016 * @param c Client to check
2018 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
2020 * FIXME: use of crypto_hash slows it down
2021 * The hash function alone takes 8-10us out of the ~55us for the whole
2022 * process of retransmitting the message from one local client to another.
2023 * Find faster implementation!
2026 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
2028 struct GNUNET_HashCode hc;
2030 if (NULL == c->types)
2033 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
2034 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2039 * Check whether client wants traffic from a tunnel.
2041 * @param c Client to check.
2042 * @param t Tunnel to be found.
2044 * @return GNUNET_YES if client knows tunnel.
2046 * TODO look in client hashmap
2049 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2053 for (i = 0; i < t->nclients; i++)
2054 if (t->clients[i] == c)
2061 * Check whether client has been informed about a tunnel.
2063 * @param c Client to check.
2064 * @param t Tunnel to be found.
2066 * @return GNUNET_YES if client knows tunnel.
2068 * TODO look in client hashmap
2071 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2075 for (i = 0; i < t->nignore; i++)
2076 if (t->ignore[i] == c)
2078 return client_wants_tunnel(c, t);
2083 * Marks a client as uninterested in traffic from the tunnel, updating both
2084 * client and tunnel to reflect this.
2086 * @param c Client that doesn't want traffic anymore.
2087 * @param t Tunnel which should be ignored.
2089 * FIXME when to delete an incoming tunnel?
2092 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2094 struct GNUNET_HashCode hash;
2096 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2097 GNUNET_break (GNUNET_YES ==
2098 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2100 GNUNET_break (GNUNET_YES ==
2101 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2102 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2103 tunnel_delete_active_client (t, c);
2104 GNUNET_array_append (t->ignore, t->nignore, c);
2109 * Deletes a tunnel from a client (either owner or destination). To be used on
2110 * tunnel destroy, otherwise, use client_ignore_tunnel.
2112 * @param c Client whose tunnel to delete.
2113 * @param t Tunnel which should be deleted.
2116 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2118 struct GNUNET_HashCode hash;
2122 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2123 GNUNET_assert (GNUNET_YES ==
2124 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2130 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2132 GNUNET_assert (GNUNET_YES ==
2133 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2137 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2145 * Send the message to all clients that have subscribed to its type
2147 * @param msg Pointer to the message itself
2148 * @param payload Pointer to the payload of the message.
2149 * @param t The tunnel to whose clients this message goes.
2151 * @return number of clients this message was sent to
2154 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2155 const struct GNUNET_MessageHeader *payload,
2156 struct MeshTunnel *t)
2158 struct MeshClient *c;
2159 MESH_TunnelNumber *tid;
2162 char cbuf[htons (msg->size)];
2164 type = ntohs (payload->type);
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2167 GNUNET_MESH_DEBUG_M2S (type));
2169 memcpy (cbuf, msg, sizeof (cbuf));
2170 switch (htons (msg->type))
2172 struct GNUNET_MESH_Unicast *uc;
2173 struct GNUNET_MESH_Multicast *mc;
2174 struct GNUNET_MESH_ToOrigin *to;
2176 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2177 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2180 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2181 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2184 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2185 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2193 for (count = 0, c = clients; c != NULL; c = c->next)
2195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2196 if (client_is_subscribed (type, c))
2198 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2202 *tid = htonl (t->local_tid);
2206 if (GNUNET_NO == client_knows_tunnel (c, t))
2208 /* This client doesn't know the tunnel */
2209 struct GNUNET_MESH_TunnelNotification tmsg;
2210 struct GNUNET_HashCode hash;
2212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2213 tmsg.header.size = htons (sizeof (tmsg));
2214 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2215 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2216 tmsg.tunnel_id = htonl (t->local_tid_dest);
2218 if (GNUNET_YES == t->speed_min)
2219 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2220 if (GNUNET_YES == t->nobuffer)
2221 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2222 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2223 &tmsg.header, GNUNET_NO);
2224 tunnel_add_client (t, c);
2225 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2227 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2228 c->incoming_tunnels, &hash, t,
2229 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2231 *tid = htonl (t->local_tid_dest);
2234 /* Check if the client wants to get traffic from the tunnel */
2235 if (GNUNET_NO == client_wants_tunnel(c, t))
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2239 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2240 (struct GNUNET_MessageHeader
2241 *) cbuf, GNUNET_NO);
2250 * Notify the client that owns the tunnel that a peer has connected to it
2251 * (the requested path to it has been confirmed).
2253 * @param t Tunnel whose owner to notify
2254 * @param id Short id of the peer that has connected
2257 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2259 struct GNUNET_MESH_PeerControl pc;
2261 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2262 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2263 pc.tunnel_id = htonl (t->local_tid);
2264 GNUNET_PEER_resolve (id, &pc.peer);
2265 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2271 * Notify all clients (not depending on registration status) that the incoming
2272 * tunnel is no longer valid.
2274 * @param t Tunnel that was destroyed.
2277 send_clients_tunnel_destroy (struct MeshTunnel *t)
2279 struct GNUNET_MESH_TunnelMessage msg;
2281 msg.header.size = htons (sizeof (msg));
2282 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2283 msg.tunnel_id = htonl (t->local_tid_dest);
2284 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2289 * Notify clients of tunnel disconnections, if needed.
2290 * In case the origin disconnects, the destination clients get a tunnel destroy
2291 * notification. If the last destination disconnects (only one remaining client
2292 * in tunnel), the origin gets a (local ID) peer disconnected.
2293 * Note that the function must be called BEFORE removing the client from
2296 * @param t Tunnel that was destroyed.
2297 * @param c Client that disconnected.
2300 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2306 struct GNUNET_MESH_TunnelMessage msg;
2308 msg.header.size = htons (sizeof (msg));
2309 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2310 msg.tunnel_id = htonl (t->local_tid_dest);
2311 for (i = 0; i < t->nclients; i++)
2312 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2313 &msg.header, GNUNET_NO);
2315 // FIXME when to disconnect an incoming tunnel?
2316 else if (1 == t->nclients && NULL != t->owner)
2318 struct GNUNET_MESH_PeerControl msg;
2320 msg.header.size = htons (sizeof (msg));
2321 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2322 msg.tunnel_id = htonl (t->local_tid);
2323 msg.peer = my_full_id;
2324 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2325 &msg.header, GNUNET_NO);
2331 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2332 * and insert it in the appropiate structures if the peer is not known yet.
2334 * @param peer Full identity of the peer.
2336 * @return Existing or newly created peer info.
2338 static struct MeshPeerInfo *
2339 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2341 struct MeshPeerInfo *peer_info;
2343 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2344 if (NULL == peer_info)
2347 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2348 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2349 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2350 peer_info->id = GNUNET_PEER_intern (peer);
2358 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2359 * and insert it in the appropiate structures if the peer is not known yet.
2361 * @param peer Short identity of the peer.
2363 * @return Existing or newly created peer info.
2365 static struct MeshPeerInfo *
2366 peer_info_get_short (const GNUNET_PEER_Id peer)
2368 struct GNUNET_PeerIdentity id;
2370 GNUNET_PEER_resolve (peer, &id);
2371 return peer_info_get (&id);
2376 * Iterator to remove the tunnel from the list of tunnels a peer participates
2379 * @param cls Closure (tunnel info)
2380 * @param key GNUNET_PeerIdentity of the peer (unused)
2381 * @param value PeerInfo of the peer
2383 * @return always GNUNET_YES, to keep iterating
2386 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2388 struct MeshTunnel *t = cls;
2389 struct MeshPeerInfo *peer = value;
2392 for (i = 0; i < peer->ntunnels; i++)
2395 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2398 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2399 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2408 * Core callback to write a pre-constructed data packet to core buffer
2410 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2411 * @param size Number of bytes available in buf.
2412 * @param buf Where the to write the message.
2414 * @return number of bytes written to buf
2417 send_core_data_raw (void *cls, size_t size, void *buf)
2419 struct MeshTransmissionDescriptor *info = cls;
2420 struct GNUNET_MessageHeader *msg;
2423 GNUNET_assert (NULL != info);
2424 GNUNET_assert (NULL != info->mesh_data);
2425 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2426 total_size = ntohs (msg->size);
2428 if (total_size > size)
2433 memcpy (buf, msg, total_size);
2434 data_descriptor_decrement_rc (info->mesh_data);
2441 * Sends an already built non-multicast message to a peer,
2442 * properly registrating all used resources.
2444 * @param message Message to send. Function makes a copy of it.
2445 * @param peer Short ID of the neighbor whom to send the message.
2446 * @param t Tunnel on which this message is transmitted.
2449 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2450 const struct GNUNET_PeerIdentity *peer,
2451 struct MeshTunnel *t)
2453 struct MeshTransmissionDescriptor *info;
2454 struct MeshPeerInfo *neighbor;
2455 struct MeshPeerPath *p;
2459 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2461 size = ntohs (message->size);
2462 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2463 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2464 info->mesh_data->data = GNUNET_malloc (size);
2465 memcpy (info->mesh_data->data, message, size);
2466 type = ntohs(message->type);
2469 struct GNUNET_MESH_Unicast *m;
2470 struct GNUNET_MESH_ToOrigin *to;
2472 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2473 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2474 m->ttl = htonl (ntohl (m->ttl) - 1);
2476 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2477 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2479 to->pid = htonl(t->bck_pid);
2481 info->mesh_data->data_len = size;
2482 info->mesh_data->reference_counter = 1;
2483 info->mesh_data->total_out = 1;
2484 neighbor = peer_info_get (peer);
2485 for (p = neighbor->path_head; NULL != p; p = p->next)
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2496 " %s IS NOT DIRECTLY CONNECTED\n",
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2501 for (p = neighbor->path_head; NULL != p; p = p->next)
2503 struct GNUNET_PeerIdentity debug_id;
2506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2507 " path with %u hops through:\n",
2509 for (i = 0; i < p->length; i++)
2511 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2514 i, GNUNET_i2s(&debug_id));
2518 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2520 " no direct connection to %s\n",
2522 GNUNET_free (info->mesh_data->data);
2523 GNUNET_free (info->mesh_data);
2527 info->peer = neighbor;
2528 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2539 * Sends a CREATE PATH message for a path to a peer, properly registrating
2540 * all used resources.
2542 * @param peer PeerInfo of the final peer for whom this path is being created.
2543 * @param p Path itself.
2544 * @param t Tunnel for which the path is created.
2547 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2548 struct MeshTunnel *t)
2550 struct GNUNET_PeerIdentity id;
2551 struct MeshPathInfo *path_info;
2552 struct MeshPeerInfo *neighbor;
2558 p = tree_get_path_to_peer (t->tree, peer->id);
2565 for (i = 0; i < p->length; i++)
2567 if (p->peers[i] == myid)
2570 if (i >= p->length - 1)
2576 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2578 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2579 path_info->path = p;
2581 neighbor = peer_info_get (&id);
2582 path_info->peer = neighbor;
2583 queue_add (path_info,
2584 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2585 sizeof (struct GNUNET_MESH_ManipulatePath) +
2586 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2593 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2595 * @param t Tunnel whose path to destroy.
2596 * @param destination Short ID of the peer to whom the path to destroy.
2599 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2601 struct MeshPeerPath *p;
2604 p = tree_get_path_to_peer (t->tree, destination);
2610 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2611 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2613 struct GNUNET_MESH_ManipulatePath *msg;
2614 struct GNUNET_PeerIdentity *pi;
2618 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2619 msg->header.size = htons (size);
2620 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2621 msg->tid = htonl (t->id.tid);
2622 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2623 for (i = 0; i < p->length; i++)
2625 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2627 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2634 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2636 * @param t Tunnel which to confirm.
2639 send_path_ack (struct MeshTunnel *t)
2641 struct MeshTransmissionDescriptor *info;
2642 struct GNUNET_PeerIdentity id;
2643 GNUNET_PEER_Id peer;
2645 peer = tree_get_predecessor (t->tree);
2646 GNUNET_PEER_resolve (peer, &id);
2647 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2648 info->origin = &t->id;
2649 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2650 GNUNET_assert (NULL != info->peer);
2653 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2654 sizeof (struct GNUNET_MESH_PathACK),
2661 * Try to establish a new connection to this peer.
2662 * Use the best path for the given tunnel.
2663 * If the peer doesn't have any path to it yet, try to get one.
2664 * If the peer already has some path, send a CREATE PATH towards it.
2666 * @param peer PeerInfo of the peer.
2667 * @param t Tunnel for which to create the path, if possible.
2670 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2672 struct MeshPeerPath *p;
2673 struct MeshPathInfo *path_info;
2675 if (NULL != peer->path_head)
2677 p = tree_get_path_to_peer (t->tree, peer->id);
2684 // FIXME always send create path to self
2687 send_create_path (peer, p, t);
2691 struct GNUNET_HashCode hash;
2694 send_client_peer_connected (t, myid);
2695 t->local_tid_dest = next_local_tid++;
2696 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2699 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2700 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2707 else if (NULL == peer->dhtget)
2709 struct GNUNET_PeerIdentity id;
2711 GNUNET_PEER_resolve (peer->id, &id);
2712 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2713 path_info->peer = peer;
2715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2716 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2717 peer->dhtgetcls = path_info;
2718 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2719 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2720 &id.hashPubKey, /* key to search */
2721 dht_replication_level, /* replication level */
2722 GNUNET_DHT_RO_RECORD_ROUTE |
2723 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2724 NULL, /* xquery */ // FIXME BLOOMFILTER
2725 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2726 &dht_get_id_handler, path_info);
2728 /* Otherwise, there is no path but the DHT get is already started. */
2733 * Task to delay the connection of a peer
2735 * @param cls Closure (path info with tunnel and peer to connect).
2736 * Will be free'd on exection.
2737 * @param tc TaskContext
2740 peer_info_connect_task (void *cls,
2741 const struct GNUNET_SCHEDULER_TaskContext *tc)
2743 struct MeshPathInfo *path_info = cls;
2745 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2747 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2752 peer_info_connect (path_info->peer, path_info->t);
2758 * Destroy the peer_info and free any allocated resources linked to it
2760 * @param pi The peer_info to destroy.
2762 * @return GNUNET_OK on success
2765 peer_info_destroy (struct MeshPeerInfo *pi)
2767 struct GNUNET_PeerIdentity id;
2768 struct MeshPeerPath *p;
2769 struct MeshPeerPath *nextp;
2771 GNUNET_PEER_resolve (pi->id, &id);
2772 GNUNET_PEER_change_rc (pi->id, -1);
2775 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2778 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2779 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2781 if (NULL != pi->dhtget)
2783 GNUNET_DHT_get_stop (pi->dhtget);
2784 GNUNET_free (pi->dhtgetcls);
2790 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2794 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2796 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2804 * Remove all paths that rely on a direct connection between p1 and p2
2805 * from the peer itself and notify all tunnels about it.
2807 * @param peer PeerInfo of affected peer.
2808 * @param p1 GNUNET_PEER_Id of one peer.
2809 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2812 * TODO: optimize (see below)
2815 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2818 struct MeshPeerPath *p;
2819 struct MeshPeerPath *aux;
2820 struct MeshPeerInfo *peer_d;
2822 unsigned int destroyed;
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2829 p = peer->path_head;
2833 for (i = 0; i < (p->length - 1); i++)
2835 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2836 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2838 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2849 for (i = 0; i < peer->ntunnels; i++)
2851 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2855 * Problem: one or more peers have been deleted from the tunnel tree.
2856 * We don't know who they are to try to add them again.
2857 * We need to try to find a new path for each of the disconnected peers.
2858 * Some of them might already have a path to reach them that does not
2859 * involve p1 and p2. Adding all anew might render in a better tree than
2860 * the trivial immediate fix.
2862 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2863 * its children will be reachable trough him.
2865 peer_d = peer_info_get_short (d);
2868 for (p = peer_d->path_head; NULL != p; p = p->next)
2870 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2878 /* No callback, as peer will be already disconnected and a connection
2879 * scheduled by tunnel_notify_connection_broken.
2881 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2885 peer_info_connect (peer_d, peer->tunnels[i]);
2888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2893 * Add the path to the peer and update the path used to reach it in case this
2896 * @param peer_info Destination peer to add the path to.
2897 * @param path New path to add. Last peer must be the peer in arg 1.
2898 * Path will be either used of freed if already known.
2899 * @param trusted Do we trust that this path is real?
2902 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2905 struct MeshPeerPath *aux;
2909 if ((NULL == peer_info) || (NULL == path))
2912 path_destroy (path);
2915 if (path->peers[path->length - 1] != peer_info->id)
2918 path_destroy (path);
2921 if (path->length <= 2 && GNUNET_NO == trusted)
2923 /* Only allow CORE to tell us about direct paths */
2924 path_destroy (path);
2927 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2928 for (l = 1; l < path->length; l++)
2930 if (path->peers[l] == myid)
2932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2933 for (l2 = 0; l2 < path->length - l; l2++)
2935 path->peers[l2] = path->peers[l + l2];
2940 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2945 struct GNUNET_PeerIdentity id;
2947 GNUNET_PEER_resolve (peer_info->id, &id);
2948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2949 path->length, GNUNET_i2s (&id));
2952 l = path_get_length (path);
2959 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2960 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2962 l2 = path_get_length (aux);
2965 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2966 peer_info->path_tail, aux, path);
2971 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2973 path_destroy (path);
2978 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2985 * Add the path to the origin peer and update the path used to reach it in case
2986 * this is the shortest.
2987 * The path is given in peer_info -> destination, therefore we turn the path
2988 * upside down first.
2990 * @param peer_info Peer to add the path to, being the origin of the path.
2991 * @param path New path to add after being inversed.
2992 * @param trusted Do we trust that this path is real?
2995 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2996 struct MeshPeerPath *path, int trusted)
2999 peer_info_add_path (peer_info, path, trusted);
3004 * Function called if the connection to the peer has been stalled for a while,
3005 * possibly due to a missed ACK. Poll the peer about its ACK status.
3007 * @param cls Closure (cinfo).
3008 * @param tc TaskContext.
3011 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3013 struct MeshTunnelChildInfo *cinfo = cls;
3014 struct GNUNET_MESH_Poll msg;
3015 struct GNUNET_PeerIdentity id;
3016 struct MeshTunnel *t;
3018 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3019 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3025 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3026 msg.header.size = htons (sizeof (msg));
3027 msg.tid = htonl (t->id.tid);
3028 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3029 msg.last_ack = htonl (cinfo->fwd_ack);
3031 GNUNET_PEER_resolve (cinfo->id, &id);
3032 send_prebuilt_message (&msg.header, &id, cinfo->t);
3033 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
3035 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
3036 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3037 &tunnel_poll, cinfo);
3042 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3043 * to obtain a local peer -> destination path and interning the peer ids.
3045 * @return Newly allocated and created path
3047 static struct MeshPeerPath *
3048 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3049 unsigned int get_path_length,
3050 const struct GNUNET_PeerIdentity *put_path,
3051 unsigned int put_path_length)
3053 struct MeshPeerPath *p;
3059 GNUNET_PEER_change_rc (myid, 1);
3060 i = get_path_length;
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3062 for (i--; i >= 0; i--)
3064 id = GNUNET_PEER_intern (&get_path[i]);
3065 if (p->length > 0 && id == p->peers[p->length - 1])
3067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3068 GNUNET_PEER_change_rc (id, -1);
3072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3073 GNUNET_i2s (&get_path[i]));
3075 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3076 p->peers[p->length - 1] = id;
3079 i = put_path_length;
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3081 for (i--; i >= 0; i--)
3083 id = GNUNET_PEER_intern (&put_path[i]);
3086 /* PUT path went through us, so discard the path up until now and start
3087 * from here to get a much shorter (and loop-free) path.
3092 if (p->length > 0 && id == p->peers[p->length - 1])
3094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3095 GNUNET_PEER_change_rc (id, -1);
3099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3100 GNUNET_i2s (&put_path[i]));
3102 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3103 p->peers[p->length - 1] = id;
3107 if (get_path_length > 0)
3108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3109 GNUNET_i2s (&get_path[0]));
3110 if (put_path_length > 0)
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3112 GNUNET_i2s (&put_path[0]));
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3115 for (i = 0; i < p->length; i++)
3117 struct GNUNET_PeerIdentity peer_id;
3119 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3121 GNUNET_i2s (&peer_id));
3129 * Adds a path to the peer_infos of all the peers in the path
3131 * @param p Path to process.
3132 * @param confirmed Whether we know if the path works or not.
3135 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3139 /* TODO: invert and add */
3140 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3141 for (i++; i < p->length; i++)
3143 struct MeshPeerInfo *aux;
3144 struct MeshPeerPath *copy;
3146 aux = peer_info_get_short (p->peers[i]);
3147 copy = path_duplicate (p);
3148 copy->length = i + 1;
3149 peer_info_add_path (aux, copy, GNUNET_NO);
3155 * Send keepalive packets for a peer
3157 * @param cls Closure (tunnel for which to send the keepalive).
3158 * @param tc Notification context.
3160 * TODO: implement explicit multicast keepalive?
3163 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3167 * Search for a tunnel among the incoming tunnels
3169 * @param tid the local id of the tunnel
3171 * @return tunnel handler, NULL if doesn't exist
3173 static struct MeshTunnel *
3174 tunnel_get_incoming (MESH_TunnelNumber tid)
3176 struct GNUNET_HashCode hash;
3178 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3179 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3180 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3185 * Search for a tunnel among the tunnels for a client
3187 * @param c the client whose tunnels to search in
3188 * @param tid the local id of the tunnel
3190 * @return tunnel handler, NULL if doesn't exist
3192 static struct MeshTunnel *
3193 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3195 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3197 return tunnel_get_incoming (tid);
3201 struct GNUNET_HashCode hash;
3203 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3204 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3210 * Search for a tunnel by global ID using PEER_ID
3212 * @param pi owner of the tunnel
3213 * @param tid global tunnel number
3215 * @return tunnel handler, NULL if doesn't exist
3217 static struct MeshTunnel *
3218 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3220 struct MESH_TunnelID id;
3221 struct GNUNET_HashCode hash;
3226 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3227 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3232 * Search for a tunnel by global ID using full PeerIdentities
3234 * @param oid owner of the tunnel
3235 * @param tid global tunnel number
3237 * @return tunnel handler, NULL if doesn't exist
3239 static struct MeshTunnel *
3240 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3242 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3247 * Delete an active client from the tunnel.
3253 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3257 for (i = 0; i < t->nclients; i++)
3259 if (t->clients[i] == c)
3261 t->clients[i] = t->clients[t->nclients - 1];
3262 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3263 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3265 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3273 * Delete an ignored client from the tunnel.
3279 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3283 for (i = 0; i < t->nignore; i++)
3285 if (t->ignore[i] == c)
3287 t->ignore[i] = t->ignore[t->nignore - 1];
3288 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3296 * Delete a client from the tunnel. It should be only done on
3297 * client disconnection, otherwise use client_ignore_tunnel.
3303 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3305 tunnel_delete_ignored_client (t, c);
3306 tunnel_delete_active_client (t, c);
3311 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3313 * Destroys queue elements of all waiting transmissions and frees all memory
3314 * used by the struct and its elements.
3316 * @param cls Closure (tunnel info).
3317 * @param key Hash of GNUNET_PEER_Id (unused).
3318 * @param value MeshTunnelChildInfo of the child.
3320 * @return always GNUNET_YES, to keep iterating
3323 tunnel_destroy_child (void *cls,
3324 const struct GNUNET_HashCode * key,
3327 struct MeshTunnelChildInfo *cinfo = value;
3328 struct MeshTunnel *t = cls;
3329 struct MeshPeerQueue *q;
3333 for (c = 0; c < cinfo->send_buffer_n; c++)
3335 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3336 q = cinfo->send_buffer[i];
3337 cinfo->send_buffer[i] = NULL;
3339 queue_destroy (q, GNUNET_YES);
3342 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3344 GNUNET_free_non_null (cinfo->send_buffer);
3345 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
3347 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
3348 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3350 GNUNET_free (cinfo);
3356 * Callback used to notify a client owner of a tunnel that a peer has
3357 * disconnected, most likely because of a path change.
3359 * @param cls Closure (tunnel this notification is about).
3360 * @param peer_id Short ID of disconnected peer.
3363 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3365 struct MeshTunnel *t = cls;
3366 struct MeshPeerInfo *peer;
3367 struct MeshPathInfo *path_info;
3369 if (NULL != t->owner && NULL != nc)
3371 struct GNUNET_MESH_PeerControl msg;
3373 msg.header.size = htons (sizeof (msg));
3374 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3375 msg.tunnel_id = htonl (t->local_tid);
3376 GNUNET_PEER_resolve (peer_id, &msg.peer);
3377 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3378 &msg.header, GNUNET_NO);
3380 peer = peer_info_get_short (peer_id);
3381 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3382 path_info->peer = peer;
3384 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3390 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3391 * needed rescources.
3392 * If peer already exists, reevaluate shortest path and change if different.
3394 * @param t Tunnel we want to add a new peer to
3395 * @param peer PeerInfo of the peer being added
3399 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3401 struct GNUNET_PeerIdentity id;
3402 struct MeshPeerPath *best_p;
3403 struct MeshPeerPath *p;
3404 unsigned int best_cost;
3407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3408 GNUNET_PEER_resolve (peer->id, &id);
3410 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3413 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3414 GNUNET_assert (GNUNET_OK ==
3415 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3417 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3420 if (NULL != (p = peer->path_head))
3423 best_cost = tree_get_path_cost (t->tree, p);
3426 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3433 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3434 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3435 t->path_refresh_task =
3436 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3440 /* Start a DHT get */
3441 peer_info_connect (peer, t);
3443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3447 * Add a path to a tunnel which we don't own, just to remember the next hop.
3448 * If destination node was already in the tunnel, the first hop information
3449 * will be replaced with the new path.
3451 * @param t Tunnel we want to add a new peer to
3452 * @param p Path to add
3453 * @param own_pos Position of local node in path.
3457 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3458 unsigned int own_pos)
3460 struct GNUNET_PeerIdentity id;
3462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3463 GNUNET_assert (0 != own_pos);
3464 tree_add_path (t->tree, p, NULL, NULL);
3465 if (own_pos < p->length - 1)
3467 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3468 tree_update_first_hops (t->tree, myid, &id);
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3474 * Add a client to a tunnel, initializing all needed data structures.
3476 * @param t Tunnel to which add the client.
3477 * @param c Client which to add to the tunnel.
3480 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3482 struct MeshTunnelClientInfo clinfo;
3484 GNUNET_array_append (t->clients, t->nclients, c);
3485 clinfo.fwd_ack = t->fwd_pid + 1;
3486 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3487 clinfo.fwd_pid = t->fwd_pid;
3488 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3490 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3495 * Notifies a tunnel that a connection has broken that affects at least
3496 * some of its peers. Sends a notification towards the root of the tree.
3497 * In case the peer is the owner of the tree, notifies the client that owns
3498 * the tunnel and tries to reconnect.
3500 * @param t Tunnel affected.
3501 * @param p1 Peer that got disconnected from p2.
3502 * @param p2 Peer that got disconnected from p1.
3504 * @return Short ID of the peer disconnected (either p1 or p2).
3505 * 0 if the tunnel remained unaffected.
3507 static GNUNET_PEER_Id
3508 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3514 tree_notify_connection_broken (t->tree, p1, p2,
3515 &tunnel_notify_client_peer_disconnected,
3517 if (myid != p1 && myid != p2)
3523 if (tree_get_predecessor (t->tree) != 0)
3525 /* We are the peer still connected, notify owner of the disconnection. */
3526 struct GNUNET_MESH_PathBroken msg;
3527 struct GNUNET_PeerIdentity neighbor;
3529 msg.header.size = htons (sizeof (msg));
3530 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3531 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3532 msg.tid = htonl (t->id.tid);
3533 msg.peer1 = my_full_id;
3534 GNUNET_PEER_resolve (pid, &msg.peer2);
3535 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3536 send_prebuilt_message (&msg.header, &neighbor, t);
3544 * Send a multicast packet to a neighbor.
3546 * @param cls Closure (Info about the multicast packet)
3547 * @param neighbor_id Short ID of the neighbor to send the packet to.
3550 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3552 struct MeshData *mdata = cls;
3553 struct MeshTransmissionDescriptor *info;
3554 struct GNUNET_PeerIdentity neighbor;
3555 struct GNUNET_MessageHeader *msg;
3557 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3559 info->mesh_data = mdata;
3560 (mdata->reference_counter) ++;
3561 info->destination = neighbor_id;
3562 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3564 GNUNET_i2s (&neighbor));
3565 info->peer = peer_info_get (&neighbor);
3566 GNUNET_assert (NULL != info->peer);
3567 msg = (struct GNUNET_MessageHeader *) mdata->data;
3570 info->mesh_data->data_len,
3577 * Queue a message in a tunnel in multicast, sending a copy to each child node
3578 * down the local one in the tunnel tree.
3580 * @param t Tunnel in which to send the data.
3581 * @param msg Message to be sent.
3584 tunnel_send_multicast (struct MeshTunnel *t,
3585 const struct GNUNET_MessageHeader *msg)
3587 struct MeshData *mdata;
3589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3590 " sending a multicast packet...\n");
3592 mdata = GNUNET_malloc (sizeof (struct MeshData));
3593 mdata->data_len = ntohs (msg->size);
3595 mdata->data = GNUNET_malloc (mdata->data_len);
3596 memcpy (mdata->data, msg, mdata->data_len);
3597 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3599 struct GNUNET_MESH_Multicast *mcast;
3601 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3602 if (t->fwd_queue_n >= t->fwd_queue_max)
3605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3606 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3607 " message from %s!\n",
3608 GNUNET_i2s(&mcast->oid));
3609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3610 " message at %s!\n",
3611 GNUNET_i2s(&my_full_id));
3612 GNUNET_free (mdata->data);
3613 GNUNET_free (mdata);
3617 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3619 ntohl (mcast->ttl));
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3626 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3627 if (mdata->reference_counter == 0)
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3630 " no one to send data to\n");
3631 GNUNET_free (mdata->data);
3632 GNUNET_free (mdata);
3633 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3638 mdata->total_out = mdata->reference_counter;
3640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3641 " sending a multicast packet done\n");
3647 * Increase the SKIP value of all peers that
3648 * have not received a unicast message.
3650 * @param cls Closure (ID of the peer that HAS received the message).
3651 * @param key ID of the neighbor.
3652 * @param value Information about the neighbor.
3654 * @return GNUNET_YES to keep iterating.
3657 tunnel_add_skip (void *cls,
3658 const struct GNUNET_HashCode * key,
3661 struct GNUNET_PeerIdentity *neighbor = cls;
3662 struct MeshTunnelChildInfo *cinfo = value;
3664 /* TODO compare only pointers? key == neighbor? */
3665 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3675 * @brief Get neighbor's Flow Control information.
3677 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3678 * descendant of the local node in a certain tunnel.
3679 * If the info is not yet there (recently created path), creates the data struct
3680 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3683 * @param t Tunnel related.
3684 * @param peer Neighbor whose Flow Control info is needed.
3686 * @return Neighbor's Flow Control info.
3688 static struct MeshTunnelChildInfo *
3689 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3690 const struct GNUNET_PeerIdentity *peer)
3692 struct MeshTunnelChildInfo *cinfo;
3694 if (NULL == t->children_fc)
3697 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3703 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3704 cinfo->id = GNUNET_PEER_intern (peer);
3705 cinfo->skip = t->fwd_pid;
3708 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3709 cinfo->fwd_ack = t->fwd_pid + delta;
3710 cinfo->bck_ack = delta;
3711 cinfo->bck_pid = -1;
3713 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3714 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3716 cinfo->send_buffer =
3717 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3719 GNUNET_assert (GNUNET_OK ==
3720 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3723 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3730 * Get the Flow Control info of a client.
3732 * @param t Tunnel on which to look.
3733 * @param c Client whose ACK to get.
3735 * @return ACK value.
3737 static struct MeshTunnelClientInfo *
3738 tunnel_get_client_fc (struct MeshTunnel *t,
3739 struct MeshClient *c)
3743 for (i = 0; i < t->nclients; i++)
3745 if (t->clients[i] != c)
3747 return &t->clients_fc[i];
3750 return NULL; // avoid compiler / coverity complaints
3755 * Iterator to get the appropiate ACK value from all children nodes.
3757 * @param cls Closue (tunnel).
3758 * @param id Id of the child node.
3761 tunnel_get_child_fwd_ack (void *cls,
3764 struct GNUNET_PeerIdentity peer_id;
3765 struct MeshTunnelChildInfo *cinfo;
3766 struct MeshTunnelChildIteratorContext *ctx = cls;
3767 struct MeshTunnel *t = ctx->t;
3770 GNUNET_PEER_resolve (id, &peer_id);
3771 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3772 ack = cinfo->fwd_ack;
3775 if (GNUNET_NO == ctx->init)
3777 ctx->max_child_ack = ack;
3778 ctx->init = GNUNET_YES;
3781 if (GNUNET_YES == t->speed_min)
3783 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3787 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3794 * Get the maximum PID allowed to transmit to any
3795 * tunnel child of the local peer, depending on the tunnel
3796 * buffering/speed settings.
3800 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3803 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3805 struct MeshTunnelChildIteratorContext ctx;
3807 ctx.max_child_ack = 0;
3809 ctx.init = GNUNET_NO;
3810 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3812 if (0 == ctx.nchildren)
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3815 " tunnel has no children, no FWD ACK\n");
3819 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3820 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3822 return (int64_t) ctx.max_child_ack;
3827 * Set the FWD ACK value of a client in a particular tunnel.
3829 * @param t Tunnel affected.
3830 * @param c Client whose ACK to set.
3831 * @param ack ACK value.
3834 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3835 struct MeshClient *c,
3840 for (i = 0; i < t->nclients; i++)
3842 if (t->clients[i] != c)
3844 t->clients_fc[i].fwd_ack = ack;
3852 * Get the highest ACK value of all clients in a particular tunnel,
3853 * according to the buffering/speed settings.
3855 * @param t Tunnel on which to look.
3857 * @return Corresponding ACK value (max uint32_t).
3858 * If no clients are suscribed, -1LL.
3861 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3866 if (0 == t->nclients)
3868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3869 " tunnel has no clients, no FWD ACK\n");
3873 for (ack = -1LL, i = 0; i < t->nclients; i++)
3876 (GNUNET_YES == t->speed_min &&
3877 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3878 (GNUNET_NO == t->speed_min &&
3879 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3881 ack = t->clients_fc[i].fwd_ack;
3885 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3886 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3888 return (uint32_t) ack;
3893 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3894 * mode and the status of all children nodes.
3898 * @return Maximum PID allowed.
3901 tunnel_get_fwd_ack (struct MeshTunnel *t)
3905 uint32_t buffer_free;
3909 count = t->fwd_pid - t->skip;
3910 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3911 child_ack = tunnel_get_children_fwd_ack (t);
3912 client_ack = tunnel_get_clients_fwd_ack (t);
3913 if (GNUNET_YES == t->nobuffer)
3916 if (-1LL == child_ack)
3917 child_ack = client_ack;
3918 if (-1LL == child_ack)
3921 client_ack = child_ack = ack;
3926 ack = count + buffer_free; // Overflow? OK!
3928 if (-1LL == child_ack)
3930 // Node has no children, child_ack AND core buffer are irrelevant.
3931 if (-1LL == client_ack) // No children AND no clients? Not good!
3933 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3937 return (uint32_t) client_ack;
3939 if (-1LL == client_ack)
3943 if (GNUNET_YES == t->speed_min)
3945 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3946 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3950 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3951 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3954 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3955 count, buffer_free, child_ack, client_ack, ack);
3961 * Build a local ACK message and send it to a local client.
3963 * @param t Tunnel on which to send the ACK.
3964 * @param c Client to whom send the ACK.
3965 * @param ack Value of the ACK.
3968 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3970 struct GNUNET_MESH_LocalAck msg;
3972 msg.header.size = htons (sizeof (msg));
3973 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3974 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3975 msg.max_pid = htonl (ack);
3976 GNUNET_SERVER_notification_context_unicast(nc,
3983 * Build an ACK message and queue it to send to the given peer.
3985 * @param t Tunnel on which to send the ACK.
3986 * @param peer Peer to whom send the ACK.
3987 * @param ack Value of the ACK.
3990 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3992 struct GNUNET_MESH_ACK msg;
3994 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3995 msg.header.size = htons (sizeof (msg));
3996 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3997 msg.pid = htonl (ack);
3998 msg.tid = htonl (t->id.tid);
4000 send_prebuilt_message (&msg.header, peer, t);
4005 * Notify a the owner of a tunnel about how many more
4006 * payload packages will we accept on a given tunnel.
4008 * @param t Tunnel on which to send the ACK.
4011 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
4015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4016 "Sending client FWD ACK on tunnel %X\n",
4019 ack = tunnel_get_fwd_ack (t);
4021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4022 if (t->last_fwd_ack == ack)
4024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
4028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
4029 t->last_fwd_ack = ack;
4030 send_local_ack (t, t->owner, ack);
4035 * Send an ACK informing the predecessor about the available buffer space.
4036 * In case there is no predecessor, inform the owning client.
4037 * If buffering is off, send only on behalf of children or self if endpoint.
4038 * If buffering is on, send when sent to children and buffer space is free.
4039 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4040 * the ACK itself goes "back" (towards root).
4042 * @param t Tunnel on which to send the ACK.
4043 * @param type Type of message that triggered the ACK transmission.
4046 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4048 struct GNUNET_PeerIdentity id;
4051 if (NULL != t->owner)
4053 tunnel_send_client_fwd_ack (t);
4056 /* Is it after unicast / multicast retransmission? */
4059 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4060 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4062 "ACK due to FWD DATA retransmission\n");
4063 if (GNUNET_YES == t->nobuffer)
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4069 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4070 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4072 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4073 t->force_ack = GNUNET_YES;
4079 /* Check if we need to transmit the ACK */
4080 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4081 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4082 GNUNET_NO == t->force_ack)
4084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4086 " t->qmax: %u, t->qn: %u\n",
4087 t->fwd_queue_max, t->fwd_queue_n);
4088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4089 " t->pid: %u, t->ack: %u\n",
4090 t->fwd_pid, t->last_fwd_ack);
4094 /* Ok, ACK might be necessary, what PID to ACK? */
4095 ack = tunnel_get_fwd_ack (t);
4097 /* If speed_min and not all children have ack'd, dont send yet */
4098 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4104 t->last_fwd_ack = ack;
4105 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4106 send_ack (t, &id, ack);
4108 t->force_ack = GNUNET_NO;
4113 * Iterator to send a child node a BCK ACK to allow him to send more
4116 * @param cls Closure (tunnel).
4117 * @param id Id of the child node.
4120 tunnel_send_child_bck_ack (void *cls,
4123 struct MeshTunnel *t = cls;
4124 struct MeshTunnelChildInfo *cinfo;
4125 struct GNUNET_PeerIdentity peer;
4128 GNUNET_PEER_resolve (id, &peer);
4129 cinfo = tunnel_get_neighbor_fc (t, &peer);
4130 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4132 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4135 " Not sending ACK, not needed\n");
4138 cinfo->bck_ack = ack;
4140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4141 " Sending BCK ACK %u (last sent: %u)\n",
4142 ack, cinfo->bck_ack);
4143 send_ack (t, &peer, ack);
4148 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4150 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4152 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4153 * - implement a size 1 buffer in each client_fc AND children_fc
4154 * to hold at least 1 message per "child".
4155 * problem: violates no buffer policy
4156 * - ack 0 and make "children" poll for transmission slots
4157 * problem: big overhead, extra latency even in low traffic
4160 * @param t Tunnel on which to send the BCK ACKs.
4163 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4166 unsigned int tunnel_delta;
4168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4170 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4173 /* Find client whom to allow to send to origin (with lowest buffer space) */
4174 for (i = 0; i < t->nclients; i++)
4176 struct MeshTunnelClientInfo *clinfo;
4179 clinfo = &t->clients_fc[i];
4180 delta = clinfo->bck_ack - clinfo->bck_pid;
4181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4182 t->clients[i]->id, delta);
4184 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4185 (GNUNET_YES == t->nobuffer && 0 == delta))
4189 ack = clinfo->bck_pid;
4190 ack += t->nobuffer ? 1 : tunnel_delta;
4191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4192 " sending ack to client %u: %u\n",
4193 t->clients[i]->id, ack);
4194 send_local_ack (t, t->clients[i], ack);
4195 clinfo->bck_ack = ack;
4199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4200 " not sending ack to client %u (td %u, d %u)\n",
4201 t->clients[i]->id, tunnel_delta, delta);
4208 * Send an ACK informing the children nodes and destination clients about
4209 * the available buffer space.
4210 * If buffering is off, send only on behalf of root (can be self).
4211 * If buffering is on, send when sent to predecessor and buffer space is free.
4212 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4213 * the ACK itself goes "forward" (towards children/clients).
4215 * @param t Tunnel on which to send the ACK.
4216 * @param type Type of message that triggered the ACK transmission.
4219 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4222 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4223 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4224 /* Is it after data to_origin retransmission? */
4227 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4228 if (GNUNET_YES == t->nobuffer)
4230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4231 " Not sending ACK, nobuffer\n");
4235 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4236 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4238 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4239 t->force_ack = GNUNET_YES;
4245 tunnel_send_clients_bck_ack (t);
4246 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4247 t->force_ack = GNUNET_NO;
4252 * @brief Re-initiate traffic to this peer if necessary.
4254 * Check if there is traffic queued towards this peer
4255 * and the core transmit handle is NULL (traffic was stalled).
4256 * If so, call core tmt rdy.
4258 * @param cls Closure (unused)
4259 * @param peer_id Short ID of peer to which initiate traffic.
4262 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4264 struct MeshPeerInfo *peer;
4265 struct GNUNET_PeerIdentity id;
4266 struct MeshPeerQueue *q;
4269 peer = peer_info_get_short(peer_id);
4270 if (NULL != peer->core_transmit)
4273 q = queue_get_next(peer);
4276 /* Might br multicast traffic already sent to this particular peer but
4277 * not to other children in this tunnel.
4278 * This way t->queue_n would be > 0 but the queue of this particular peer
4284 GNUNET_PEER_resolve (peer->id, &id);
4285 peer->core_transmit =
4286 GNUNET_CORE_notify_transmit_ready(core_handle,
4289 GNUNET_TIME_UNIT_FOREVER_REL,
4299 * @brief Allow transmission of FWD traffic on this tunnel
4301 * Check if there is traffic queued towards any children
4302 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4304 * @param t Tunnel on which to unlock FWD traffic.
4307 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4309 if (0 == t->fwd_queue_n)
4312 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4317 * @brief Allow transmission of BCK traffic on this tunnel
4319 * Check if there is traffic queued towards the root of the tree
4320 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4322 * @param t Tunnel on which to unlock BCK traffic.
4325 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4327 if (0 == t->bck_queue_n)
4330 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4335 * Send a message to all peers in this tunnel that the tunnel is no longer
4338 * @param t The tunnel whose peers to notify.
4341 tunnel_send_destroy (struct MeshTunnel *t)
4343 struct GNUNET_MESH_TunnelDestroy msg;
4345 msg.header.size = htons (sizeof (msg));
4346 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4347 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4348 msg.tid = htonl (t->id.tid);
4349 tunnel_send_multicast (t, &msg.header);
4354 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4356 * @param cls Closure (Tunnel which to cancel).
4357 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4360 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4362 struct MeshTunnel *t = cls;
4363 struct MeshPeerInfo *peer_info;
4364 struct MeshPeerQueue *pq;
4365 struct MeshPeerQueue *next;
4367 peer_info = peer_info_get_short (neighbor_id);
4368 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4371 if (pq->tunnel == t)
4373 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4374 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4375 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4377 // Should have been removed on destroy children
4380 queue_destroy (pq, GNUNET_YES);
4383 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4385 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4386 peer_info->core_transmit = NULL;
4391 * Destroy the tunnel and free any allocated resources linked to it.
4393 * @param t the tunnel to destroy
4395 * @return GNUNET_OK on success
4398 tunnel_destroy (struct MeshTunnel *t)
4400 struct MeshClient *c;
4401 struct GNUNET_HashCode hash;
4412 struct GNUNET_PeerIdentity id;
4414 GNUNET_PEER_resolve (t->id.oid, &id);
4415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4416 GNUNET_i2s (&id), t->id.tid);
4418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4422 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4423 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4431 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4433 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4440 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4441 for (i = 0; i < t->nclients; i++)
4445 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4451 for (i = 0; i < t->nignore; i++)
4455 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4462 if (t->nclients > 0)
4465 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4470 GNUNET_free (t->clients);
4471 GNUNET_free (t->clients_fc);
4474 if (NULL != t->peers)
4476 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4478 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4481 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4482 &tunnel_destroy_child,
4484 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4485 t->children_fc = NULL;
4487 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4488 tree_destroy (t->tree);
4490 if (NULL != t->regex_ctx)
4491 regex_cancel_search (t->regex_ctx);
4492 if (NULL != t->dht_get_type)
4493 GNUNET_DHT_get_stop (t->dht_get_type);
4494 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4495 GNUNET_SCHEDULER_cancel (t->timeout_task);
4496 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4497 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4500 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4507 * Create a new tunnel
4509 * @param owner Who is the owner of the tunnel (short ID).
4510 * @param tid Tunnel Number of the tunnel.
4511 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4512 * @param local Tunnel Number for the tunnel, for the client point of view.
4514 * @return A new initialized tunnel. NULL on error.
4516 static struct MeshTunnel *
4517 tunnel_new (GNUNET_PEER_Id owner,
4518 MESH_TunnelNumber tid,
4519 struct MeshClient *client,
4520 MESH_TunnelNumber local)
4522 struct MeshTunnel *t;
4523 struct GNUNET_HashCode hash;
4525 if (n_tunnels >= max_tunnels && NULL == client)
4528 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4531 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4532 t->bck_queue_max = t->fwd_queue_max;
4533 t->tree = tree_new (owner);
4535 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4536 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4537 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4538 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4539 t->local_tid = local;
4540 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4542 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4544 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4546 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4547 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4554 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4561 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4563 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4564 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4568 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4578 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4579 * that are no longer needed, as well as nodes of no longer reachable peers.
4580 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4582 * @param t Tunnel from which to remove the path.
4583 * @param peer Short id of the peer which should be removed.
4586 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4590 r = tree_del_peer (t->tree, peer, NULL, NULL);
4593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4594 "Tunnel %u [%u] has no more nodes\n",
4595 t->id.oid, t->id.tid);
4601 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4602 * client when the client disconnects. If the client is not the owner, the
4603 * owner will get notified if no more clients are in the tunnel and the client
4604 * get removed from the tunnel's list.
4606 * @param cls closure (client that is disconnecting)
4607 * @param key the hash of the local tunnel id (used to access the hashmap)
4608 * @param value the value stored at the key (tunnel to destroy)
4610 * @return GNUNET_OK, keep iterating.
4613 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4615 struct MeshTunnel *t = value;
4616 struct MeshClient *c = cls;
4618 send_client_tunnel_disconnect(t, c);
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4622 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4623 tunnel_delete_client(t, c);
4624 client_delete_tunnel(c, t);
4627 tunnel_send_destroy(t);
4629 t->destroy = GNUNET_YES;
4636 * Timeout function, destroys tunnel if called
4638 * @param cls Closure (tunnel to destroy).
4639 * @param tc TaskContext
4642 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4644 struct MeshTunnel *t = cls;
4645 struct GNUNET_PeerIdentity id;
4647 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4648 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4650 GNUNET_PEER_resolve(t->id.oid, &id);
4651 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4652 "Tunnel %s [%X] timed out. Destroying.\n",
4653 GNUNET_i2s(&id), t->id.tid);
4654 send_clients_tunnel_destroy (t);
4659 * Resets the tunnel timeout. Starts it if no timeout was running.
4661 * @param t Tunnel whose timeout to reset.
4663 * TODO use heap to improve efficiency of scheduler.
4666 tunnel_reset_timeout (struct MeshTunnel *t)
4668 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4669 GNUNET_SCHEDULER_cancel (t->timeout_task);
4671 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4672 (refresh_path_time, 4), &tunnel_timeout, t);
4676 /******************************************************************************/
4677 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4678 /******************************************************************************/
4681 * Function to send a create path packet to a peer.
4683 * @param cls closure
4684 * @param size number of bytes available in buf
4685 * @param buf where the callee should write the message
4686 * @return number of bytes written to buf
4689 send_core_path_create (void *cls, size_t size, void *buf)
4691 struct MeshPathInfo *info = cls;
4692 struct GNUNET_MESH_ManipulatePath *msg;
4693 struct GNUNET_PeerIdentity *peer_ptr;
4694 struct MeshTunnel *t = info->t;
4695 struct MeshPeerPath *p = info->path;
4700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4702 sizeof (struct GNUNET_MESH_ManipulatePath) +
4703 p->length * sizeof (struct GNUNET_PeerIdentity);
4705 if (size < size_needed || NULL == buf)
4710 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4711 msg->header.size = htons (size_needed);
4712 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4713 msg->tid = ntohl (t->id.tid);
4716 if (GNUNET_YES == t->speed_min)
4717 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4718 if (GNUNET_YES == t->nobuffer)
4719 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4720 msg->opt = htonl(opt);
4723 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4724 for (i = 0; i < p->length; i++)
4726 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4733 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4739 * Fill the core buffer
4741 * @param cls closure (data itself)
4742 * @param size number of bytes available in buf
4743 * @param buf where the callee should write the message
4745 * @return number of bytes written to buf
4748 send_core_data_multicast (void *cls, size_t size, void *buf)
4750 struct MeshTransmissionDescriptor *info = cls;
4753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4754 GNUNET_assert (NULL != info);
4755 GNUNET_assert (NULL != info->peer);
4756 total_size = info->mesh_data->data_len;
4757 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4759 if (total_size > size)
4764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4765 memcpy (buf, info->mesh_data->data, total_size);
4768 struct GNUNET_MESH_Multicast *mc;
4769 struct GNUNET_MessageHeader *mh;
4772 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4774 mc = (struct GNUNET_MESH_Multicast *) mh;
4775 mh = (struct GNUNET_MessageHeader *) &mc[1];
4776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4777 " multicast, payload type %s\n",
4778 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4780 " multicast, payload size %u\n", ntohs (mh->size));
4784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4785 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4789 data_descriptor_decrement_rc (info->mesh_data);
4790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4798 * Creates a path ack message in buf and frees all unused resources.
4800 * @param cls closure (MeshTransmissionDescriptor)
4801 * @param size number of bytes available in buf
4802 * @param buf where the callee should write the message
4803 * @return number of bytes written to buf
4806 send_core_path_ack (void *cls, size_t size, void *buf)
4808 struct MeshTransmissionDescriptor *info = cls;
4809 struct GNUNET_MESH_PathACK *msg = buf;
4811 GNUNET_assert (NULL != info);
4812 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4817 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4818 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4819 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4820 msg->tid = htonl (info->origin->tid);
4821 msg->peer_id = my_full_id;
4824 /* TODO add signature */
4826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4827 return sizeof (struct GNUNET_MESH_PathACK);
4832 * Free a transmission that was already queued with all resources
4833 * associated to the request.
4835 * @param queue Queue handler to cancel.
4836 * @param clear_cls Is it necessary to free associated cls?
4839 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4841 struct MeshTransmissionDescriptor *dd;
4842 struct MeshPathInfo *path_info;
4843 struct MeshTunnelChildInfo *cinfo;
4844 struct GNUNET_PeerIdentity id;
4848 if (GNUNET_YES == clear_cls)
4850 switch (queue->type)
4852 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4853 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4854 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4856 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4857 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4858 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4859 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4860 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4861 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4863 " prebuilt message\n");
4864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4866 GNUNET_MESH_DEBUG_M2S(queue->type));
4868 data_descriptor_decrement_rc (dd->mesh_data);
4870 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4872 path_info = queue->cls;
4873 path_destroy (path_info->path);
4877 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4878 " type %s unknown!\n",
4879 GNUNET_MESH_DEBUG_M2S(queue->type));
4881 GNUNET_free_non_null (queue->cls);
4883 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4884 queue->peer->queue_tail,
4887 /* Delete from child_fc in the appropiate tunnel */
4888 max = queue->tunnel->fwd_queue_max;
4889 GNUNET_PEER_resolve (queue->peer->id, &id);
4890 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4893 for (i = 0; i < cinfo->send_buffer_n; i++)
4896 i2 = (cinfo->send_buffer_start + i) % max;
4897 if (cinfo->send_buffer[i2] == queue)
4899 /* Found corresponding entry in the send_buffer. Move all others back. */
4904 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4906 j2 = (cinfo->send_buffer_start + j) % max;
4907 j3 = (cinfo->send_buffer_start + j + 1) % max;
4908 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4911 cinfo->send_buffer[j3] = NULL;
4912 cinfo->send_buffer_n--;
4917 GNUNET_free (queue);
4922 * @brief Get the next transmittable message from the queue.
4924 * This will be the head, except in the case of being a data packet
4925 * not allowed by the destination peer.
4927 * @param peer Destination peer.
4929 * @return The next viable MeshPeerQueue element to send to that peer.
4930 * NULL when there are no transmittable messages.
4932 struct MeshPeerQueue *
4933 queue_get_next (const struct MeshPeerInfo *peer)
4935 struct MeshPeerQueue *q;
4936 struct MeshTunnel *t;
4937 struct MeshTransmissionDescriptor *info;
4938 struct MeshTunnelChildInfo *cinfo;
4939 struct GNUNET_MESH_Unicast *ucast;
4940 struct GNUNET_MESH_ToOrigin *to_orig;
4941 struct GNUNET_MESH_Multicast *mcast;
4942 struct GNUNET_PeerIdentity id;
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4947 for (q = peer->queue_head; NULL != q; q = q->next)
4951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4953 GNUNET_MESH_DEBUG_M2S(q->type));
4956 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4957 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4958 pid = ntohl (ucast->pid);
4959 GNUNET_PEER_resolve (info->peer->id, &id);
4960 cinfo = tunnel_get_neighbor_fc(t, &id);
4961 ack = cinfo->fwd_ack;
4963 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4964 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4965 pid = ntohl (to_orig->pid);
4968 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4969 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4970 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4972 // Not a multicast payload: multicast control traffic (destroy, etc)
4975 pid = ntohl (mcast->pid);
4976 GNUNET_PEER_resolve (info->peer->id, &id);
4977 cinfo = tunnel_get_neighbor_fc(t, &id);
4978 ack = cinfo->fwd_ack;
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4986 "********* ACK: %u, PID: %u\n",
4988 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4997 "********* NEXT!\n");
5000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5001 "********* nothing found\n");
5007 * Core callback to write a queued packet to core buffer
5009 * @param cls Closure (peer info).
5010 * @param size Number of bytes available in buf.
5011 * @param buf Where the to write the message.
5013 * @return number of bytes written to buf
5016 queue_send (void *cls, size_t size, void *buf)
5018 struct MeshPeerInfo *peer = cls;
5019 struct GNUNET_MessageHeader *msg;
5020 struct MeshPeerQueue *queue;
5021 struct MeshTunnel *t;
5022 struct MeshTunnelChildInfo *cinfo;
5023 struct GNUNET_PeerIdentity dst_id;
5026 peer->core_transmit = NULL;
5029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5030 queue = queue_get_next (peer);
5032 /* Queue has no internal mesh traffic nor sendable payload */
5035 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5036 if (NULL == peer->queue_head)
5037 GNUNET_break (0); // Should've been canceled
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5042 GNUNET_PEER_resolve (peer->id, &dst_id);
5043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5044 "********* towards %s\n",
5045 GNUNET_i2s(&dst_id));
5046 /* Check if buffer size is enough for the message */
5047 if (queue->size > size)
5049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5050 "********* not enough room, reissue\n");
5051 peer->core_transmit =
5052 GNUNET_CORE_notify_transmit_ready (core_handle,
5055 GNUNET_TIME_UNIT_FOREVER_REL,
5062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5065 GNUNET_assert (0 < t->pending_messages);
5066 t->pending_messages--;
5067 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5071 "********* unicast: t->q (%u/%u)\n",
5072 t->fwd_queue_n, t->fwd_queue_max);
5074 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5081 switch (queue->type)
5084 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5085 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5086 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5087 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5088 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5090 "********* raw: %s\n",
5091 GNUNET_MESH_DEBUG_M2S (queue->type));
5093 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5094 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5095 data_size = send_core_data_raw (queue->cls, size, buf);
5096 msg = (struct GNUNET_MessageHeader *) buf;
5097 switch (ntohs (msg->type)) // Type of preconstructed message
5099 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5100 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5102 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5103 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5109 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5112 struct MeshTransmissionDescriptor *info = queue->cls;
5114 if ((1 == info->mesh_data->reference_counter
5115 && GNUNET_YES == t->speed_min)
5117 (info->mesh_data->total_out == info->mesh_data->reference_counter
5118 && GNUNET_NO == t->speed_min))
5120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5121 "********* considered sent\n");
5126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5127 "********* NOT considered sent yet\n");
5128 t->pending_messages++;
5131 data_size = send_core_data_multicast(queue->cls, size, buf);
5132 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5134 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5136 data_size = send_core_path_create (queue->cls, size, buf);
5138 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5140 data_size = send_core_path_ack (queue->cls, size, buf);
5142 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5144 data_size = send_core_data_multicast (queue->cls, size, buf);
5148 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5149 "********* type unknown: %u\n",
5153 switch (queue->type)
5155 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5156 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5157 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5158 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5159 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5163 "at pos %u (%p) != %p\n",
5164 cinfo->send_buffer_start,
5165 cinfo->send_buffer[cinfo->send_buffer_start],
5168 if (cinfo->send_buffer_n > 0)
5170 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5171 cinfo->send_buffer_n--;
5172 cinfo->send_buffer_start++;
5173 cinfo->send_buffer_start %= t->fwd_queue_max;
5184 /* Free queue, but cls was freed by send_core_* */
5185 queue_destroy (queue, GNUNET_NO);
5187 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5193 /* If more data in queue, send next */
5194 queue = queue_get_next(peer);
5197 struct GNUNET_PeerIdentity id;
5199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5200 GNUNET_PEER_resolve (peer->id, &id);
5201 peer->core_transmit =
5202 GNUNET_CORE_notify_transmit_ready(core_handle,
5205 GNUNET_TIME_UNIT_FOREVER_REL,
5213 if (NULL != peer->queue_head)
5215 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5216 "********* %s stalled\n",
5217 GNUNET_i2s(&my_full_id));
5219 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5220 // FIXME unify bck/fwd structures, bck does not have cinfo right now
5221 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5223 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
5224 &tunnel_poll, cinfo);
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5234 * @brief Queue and pass message to core when possible.
5236 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5237 * and accounts for it. In case the queue is full, the message is dropped and
5240 * Otherwise, message is treated as internal and allowed to go regardless of
5243 * @param cls Closure (@c type dependant). It will be used by queue_send to
5244 * build the message to be sent if not already prebuilt.
5245 * @param type Type of the message, 0 for a raw message.
5246 * @param size Size of the message.
5247 * @param dst Neighbor to send message to.
5248 * @param t Tunnel this message belongs to.
5251 queue_add (void *cls, uint16_t type, size_t size,
5252 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5254 struct MeshPeerQueue *queue;
5255 struct MeshTunnelChildInfo *cinfo;
5256 struct GNUNET_PeerIdentity id;
5262 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5263 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5265 n = &t->fwd_queue_n;
5266 max = &t->fwd_queue_max;
5268 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5270 n = &t->bck_queue_n;
5271 max = &t->bck_queue_max;
5278 GNUNET_STATISTICS_update(stats,
5279 "# messages dropped (buffer full)",
5281 return; // Drop message
5285 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5291 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5292 GNUNET_PEER_resolve (dst->id, &id);
5293 if (NULL == dst->core_transmit)
5295 dst->core_transmit =
5296 GNUNET_CORE_notify_transmit_ready (core_handle,
5299 GNUNET_TIME_UNIT_FOREVER_REL,
5305 t->pending_messages++;
5306 if (NULL == n) // Is this internal mesh traffic?
5309 // It's payload, keep track of buffer per peer.
5310 cinfo = tunnel_get_neighbor_fc(t, &id);
5311 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5312 if (NULL != cinfo->send_buffer[i])
5314 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5315 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5316 cinfo->send_buffer_start++;
5317 cinfo->send_buffer_start %= t->fwd_queue_max;
5321 cinfo->send_buffer_n++;
5323 cinfo->send_buffer[i] = queue;
5324 if (cinfo->send_buffer_n > t->fwd_queue_max)
5327 cinfo->send_buffer_n = t->fwd_queue_max;
5332 /******************************************************************************/
5333 /******************** MESH NETWORK HANDLERS **************************/
5334 /******************************************************************************/
5338 * Core handler for path creation
5340 * @param cls closure
5341 * @param message message
5342 * @param peer peer identity this notification is about
5343 * @param atsi performance data
5344 * @param atsi_count number of records in 'atsi'
5346 * @return GNUNET_OK to keep the connection open,
5347 * GNUNET_SYSERR to close it (signal serious error)
5350 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5351 const struct GNUNET_MessageHeader *message,
5352 const struct GNUNET_ATS_Information *atsi,
5353 unsigned int atsi_count)
5355 unsigned int own_pos;
5358 MESH_TunnelNumber tid;
5359 struct GNUNET_MESH_ManipulatePath *msg;
5360 struct GNUNET_PeerIdentity *pi;
5361 struct GNUNET_HashCode hash;
5362 struct MeshPeerPath *path;
5363 struct MeshPeerInfo *dest_peer_info;
5364 struct MeshPeerInfo *orig_peer_info;
5365 struct MeshTunnel *t;
5367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5368 "Received a path create msg [%s]\n",
5369 GNUNET_i2s (&my_full_id));
5370 size = ntohs (message->size);
5371 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5373 GNUNET_break_op (0);
5377 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5378 if (size % sizeof (struct GNUNET_PeerIdentity))
5380 GNUNET_break_op (0);
5383 size /= sizeof (struct GNUNET_PeerIdentity);
5386 GNUNET_break_op (0);
5389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5390 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5392 tid = ntohl (msg->tid);
5393 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5395 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5396 t = tunnel_get (pi, tid);
5397 if (NULL == t) // FIXME only for INCOMING tunnels?
5401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5402 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5405 // FIXME notify failure
5408 opt = ntohl (msg->opt);
5409 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5410 GNUNET_YES : GNUNET_NO;
5411 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5413 t->nobuffer = GNUNET_YES;
5414 t->last_fwd_ack = t->fwd_pid + 1;
5416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5417 " speed_min: %d, nobuffer:%d\n",
5418 t->speed_min, t->nobuffer);
5420 if (GNUNET_YES == t->nobuffer)
5422 t->bck_queue_max = 1;
5423 t->fwd_queue_max = 1;
5426 // FIXME only assign a local tid if a local client is interested (on demand)
5427 while (NULL != tunnel_get_incoming (next_local_tid))
5428 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5429 t->local_tid_dest = next_local_tid++;
5430 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5433 tunnel_reset_timeout (t);
5434 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5436 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5445 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5446 if (NULL == dest_peer_info)
5448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5449 " Creating PeerInfo for destination.\n");
5450 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5451 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5452 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5454 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5456 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5457 if (NULL == orig_peer_info)
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5460 " Creating PeerInfo for origin.\n");
5461 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5462 orig_peer_info->id = GNUNET_PEER_intern (pi);
5463 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5464 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5467 path = path_new (size);
5469 for (i = 0; i < size; i++)
5471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5472 GNUNET_i2s (&pi[i]));
5473 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5474 if (path->peers[i] == myid)
5477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5480 /* cannot be self, must be 'not found' */
5481 /* create path: self not found in path through self */
5482 GNUNET_break_op (0);
5483 path_destroy (path);
5487 path_add_to_peers (path, GNUNET_NO);
5488 tunnel_add_path (t, path, own_pos);
5489 if (own_pos == size - 1)
5491 /* It is for us! Send ack. */
5492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5493 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5494 if (NULL == t->peers)
5496 /* New tunnel! Notify clients on first payload message. */
5497 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5499 GNUNET_break (GNUNET_SYSERR !=
5500 GNUNET_CONTAINER_multihashmap_put (t->peers,
5501 &my_full_id.hashPubKey,
5504 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5509 struct MeshPeerPath *path2;
5511 /* It's for somebody else! Retransmit. */
5512 path2 = path_duplicate (path);
5513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5514 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5515 path2 = path_duplicate (path);
5516 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5517 send_create_path (dest_peer_info, path, t);
5524 * Core handler for path destruction
5526 * @param cls closure
5527 * @param message message
5528 * @param peer peer identity this notification is about
5529 * @param atsi performance data
5530 * @param atsi_count number of records in 'atsi'
5532 * @return GNUNET_OK to keep the connection open,
5533 * GNUNET_SYSERR to close it (signal serious error)
5536 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5537 const struct GNUNET_MessageHeader *message,
5538 const struct GNUNET_ATS_Information *atsi,
5539 unsigned int atsi_count)
5541 struct GNUNET_MESH_ManipulatePath *msg;
5542 struct GNUNET_PeerIdentity *pi;
5543 struct MeshPeerPath *path;
5544 struct MeshTunnel *t;
5545 unsigned int own_pos;
5549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5550 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5551 size = ntohs (message->size);
5552 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5554 GNUNET_break_op (0);
5558 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5559 if (size % sizeof (struct GNUNET_PeerIdentity))
5561 GNUNET_break_op (0);
5564 size /= sizeof (struct GNUNET_PeerIdentity);
5567 GNUNET_break_op (0);
5570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5572 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5573 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5575 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5577 t = tunnel_get (pi, ntohl (msg->tid));
5580 /* TODO notify back: we don't know this tunnel */
5581 GNUNET_break_op (0);
5584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5585 path = path_new (size);
5587 for (i = 0; i < size; i++)
5589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5590 GNUNET_i2s (&pi[i]));
5591 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5592 if (path->peers[i] == myid)
5595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5596 if (own_pos < path->length - 1)
5597 send_prebuilt_message (message, &pi[own_pos + 1], t);
5599 send_client_tunnel_disconnect(t, NULL);
5601 tunnel_delete_peer (t, path->peers[path->length - 1]);
5602 path_destroy (path);
5608 * Core handler for notifications of broken paths
5610 * @param cls closure
5611 * @param message message
5612 * @param peer peer identity this notification is about
5613 * @param atsi performance data
5614 * @param atsi_count number of records in 'atsi'
5616 * @return GNUNET_OK to keep the connection open,
5617 * GNUNET_SYSERR to close it (signal serious error)
5620 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5621 const struct GNUNET_MessageHeader *message,
5622 const struct GNUNET_ATS_Information *atsi,
5623 unsigned int atsi_count)
5625 struct GNUNET_MESH_PathBroken *msg;
5626 struct MeshTunnel *t;
5628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5629 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5630 msg = (struct GNUNET_MESH_PathBroken *) message;
5631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5632 GNUNET_i2s (&msg->peer1));
5633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5634 GNUNET_i2s (&msg->peer2));
5635 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5638 GNUNET_break_op (0);
5641 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5642 GNUNET_PEER_search (&msg->peer2));
5649 * Core handler for tunnel destruction
5651 * @param cls closure
5652 * @param message message
5653 * @param peer peer identity this notification is about
5654 * @param atsi performance data
5655 * @param atsi_count number of records in 'atsi'
5657 * @return GNUNET_OK to keep the connection open,
5658 * GNUNET_SYSERR to close it (signal serious error)
5661 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5662 const struct GNUNET_MessageHeader *message,
5663 const struct GNUNET_ATS_Information *atsi,
5664 unsigned int atsi_count)
5666 struct GNUNET_MESH_TunnelDestroy *msg;
5667 struct MeshTunnel *t;
5669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5670 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5671 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5673 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5674 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5677 /* Probably already got the message from another path,
5678 * destroyed the tunnel and retransmitted to children.
5681 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5684 if (t->id.oid == myid)
5686 GNUNET_break_op (0);
5689 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5691 /* Tunnel was incoming, notify clients */
5692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5693 t->local_tid, t->local_tid_dest);
5694 send_clients_tunnel_destroy (t);
5696 tunnel_send_destroy (t);
5697 t->destroy = GNUNET_YES;
5698 // TODO: add timeout to destroy the tunnel anyway
5704 * Core handler for mesh network traffic going from the origin to a peer
5706 * @param cls closure
5707 * @param peer peer identity this notification is about
5708 * @param message message
5709 * @param atsi performance data
5710 * @param atsi_count number of records in 'atsi'
5711 * @return GNUNET_OK to keep the connection open,
5712 * GNUNET_SYSERR to close it (signal serious error)
5715 handle_mesh_data_unicast (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_Unicast *msg;
5721 struct GNUNET_PeerIdentity *neighbor;
5722 struct MeshTunnelChildInfo *cinfo;
5723 struct MeshTunnel *t;
5724 GNUNET_PEER_Id dest_id;
5729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5732 size = ntohs (message->size);
5734 sizeof (struct GNUNET_MESH_Unicast) +
5735 sizeof (struct GNUNET_MessageHeader))
5740 msg = (struct GNUNET_MESH_Unicast *) message;
5741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5742 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5744 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5747 /* TODO notify back: we don't know this tunnel */
5748 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5749 GNUNET_break_op (0);
5752 pid = ntohl (msg->pid);
5753 if (t->fwd_pid == pid)
5755 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5756 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5757 " Already seen pid %u, DROPPING!\n", pid);
5762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5763 " pid %u not seen yet, forwarding\n", pid);
5766 t->skip += (pid - t->fwd_pid) - 1;
5769 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5771 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5772 GNUNET_break_op (0);
5773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5774 "Received PID %u, ACK %u\n",
5775 pid, t->last_fwd_ack);
5779 tunnel_reset_timeout (t);
5780 dest_id = GNUNET_PEER_search (&msg->destination);
5781 if (dest_id == myid)
5783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5784 " it's for us! sending to clients...\n");
5785 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5786 send_subscribed_clients (message, &msg[1].header, t);
5787 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5790 ttl = ntohl (msg->ttl);
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5794 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5795 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5796 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5800 " not for us, retransmitting...\n");
5802 neighbor = tree_get_first_hop (t->tree, dest_id);
5803 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5804 cinfo->fwd_pid = pid;
5805 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5808 if (GNUNET_YES == t->nobuffer &&
5809 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5811 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5812 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5813 GNUNET_break_op (0);
5816 send_prebuilt_message (message, neighbor, t);
5817 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5823 * Core handler for mesh network traffic going from the origin to all peers
5825 * @param cls closure
5826 * @param message message
5827 * @param peer peer identity this notification is about
5828 * @param atsi performance data
5829 * @param atsi_count number of records in 'atsi'
5830 * @return GNUNET_OK to keep the connection open,
5831 * GNUNET_SYSERR to close it (signal serious error)
5833 * TODO: Check who we got this from, to validate route.
5836 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5837 const struct GNUNET_MessageHeader *message,
5838 const struct GNUNET_ATS_Information *atsi,
5839 unsigned int atsi_count)
5841 struct GNUNET_MESH_Multicast *msg;
5842 struct MeshTunnel *t;
5846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5848 size = ntohs (message->size);
5849 if (sizeof (struct GNUNET_MESH_Multicast) +
5850 sizeof (struct GNUNET_MessageHeader) > size)
5852 GNUNET_break_op (0);
5855 msg = (struct GNUNET_MESH_Multicast *) message;
5856 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5860 /* TODO notify that we dont know that tunnel */
5861 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5862 GNUNET_break_op (0);
5865 pid = ntohl (msg->pid);
5866 if (t->fwd_pid == pid)
5868 /* already seen this packet, drop */
5869 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5871 " Already seen pid %u, DROPPING!\n", pid);
5872 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5878 " pid %u not seen yet, forwarding\n", pid);
5880 t->skip += (pid - t->fwd_pid) - 1;
5882 tunnel_reset_timeout (t);
5884 /* Transmit to locally interested clients */
5885 if (NULL != t->peers &&
5886 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5888 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5889 send_subscribed_clients (message, &msg[1].header, t);
5890 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5893 if (ntohl (msg->ttl) == 0)
5895 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5896 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5897 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5900 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5901 tunnel_send_multicast (t, message);
5907 * Core handler for mesh network traffic toward the owner of a tunnel
5909 * @param cls closure
5910 * @param message message
5911 * @param peer peer identity this notification is about
5912 * @param atsi performance data
5913 * @param atsi_count number of records in 'atsi'
5915 * @return GNUNET_OK to keep the connection open,
5916 * GNUNET_SYSERR to close it (signal serious error)
5919 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5920 const struct GNUNET_MessageHeader *message,
5921 const struct GNUNET_ATS_Information *atsi,
5922 unsigned int atsi_count)
5924 struct GNUNET_MESH_ToOrigin *msg;
5925 struct GNUNET_PeerIdentity id;
5926 struct MeshPeerInfo *peer_info;
5927 struct MeshTunnel *t;
5928 struct MeshTunnelChildInfo *cinfo;
5932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5934 size = ntohs (message->size);
5935 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5936 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5938 GNUNET_break_op (0);
5941 msg = (struct GNUNET_MESH_ToOrigin *) message;
5942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5943 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5944 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5945 pid = ntohl (msg->pid);
5949 /* TODO notify that we dont know this tunnel (whom)? */
5950 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5951 GNUNET_break_op (0);
5952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5953 "Received to_origin with PID %u on unknown tunnel\n",
5958 cinfo = tunnel_get_neighbor_fc(t, peer);
5965 if (cinfo->bck_pid == pid)
5967 /* already seen this packet, drop */
5968 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5970 " Already seen pid %u, DROPPING!\n", pid);
5971 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5976 " pid %u not seen yet, forwarding\n", pid);
5977 cinfo->bck_pid = pid;
5979 if (NULL != t->owner)
5982 struct GNUNET_MESH_ToOrigin *copy;
5984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5985 " it's for us! sending to clients...\n");
5986 /* TODO signature verification */
5987 memcpy (cbuf, message, size);
5988 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5989 copy->tid = htonl (t->local_tid);
5991 copy->pid = htonl (t->bck_pid);
5992 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5993 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5994 ©->header, GNUNET_NO);
5995 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5999 " not for us, retransmitting...\n");
6001 peer_info = peer_info_get (&msg->oid);
6002 if (NULL == peer_info)
6004 /* unknown origin of tunnel */
6008 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6009 send_prebuilt_message (message, &id, t);
6010 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6017 * Core handler for mesh network traffic point-to-point acks.
6019 * @param cls closure
6020 * @param message message
6021 * @param peer peer identity this notification is about
6022 * @param atsi performance data
6023 * @param atsi_count number of records in 'atsi'
6025 * @return GNUNET_OK to keep the connection open,
6026 * GNUNET_SYSERR to close it (signal serious error)
6029 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6030 const struct GNUNET_MessageHeader *message,
6031 const struct GNUNET_ATS_Information *atsi,
6032 unsigned int atsi_count)
6034 struct GNUNET_MESH_ACK *msg;
6035 struct MeshTunnel *t;
6038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6040 msg = (struct GNUNET_MESH_ACK *) message;
6042 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6046 /* TODO notify that we dont know this tunnel (whom)? */
6047 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6050 ack = ntohl (msg->pid);
6051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6053 /* Is this a forward or backward ACK? */
6054 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6056 struct MeshTunnelChildInfo *cinfo;
6059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6060 cinfo = tunnel_get_neighbor_fc (t, peer);
6061 cinfo->fwd_ack = ack;
6062 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6063 tunnel_unlock_fwd_queues (t);
6064 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6066 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6067 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6068 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6075 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6076 tunnel_unlock_bck_queue (t);
6083 * Core handler for mesh network traffic point-to-point ack polls.
6085 * @param cls closure
6086 * @param message message
6087 * @param peer peer identity this notification is about
6088 * @param atsi performance data
6089 * @param atsi_count number of records in 'atsi'
6091 * @return GNUNET_OK to keep the connection open,
6092 * GNUNET_SYSERR to close it (signal serious error)
6095 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6096 const struct GNUNET_MessageHeader *message,
6097 const struct GNUNET_ATS_Information *atsi,
6098 unsigned int atsi_count)
6100 struct GNUNET_MESH_Poll *msg;
6101 struct MeshTunnel *t;
6103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6106 msg = (struct GNUNET_MESH_Poll *) message;
6108 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6112 /* TODO notify that we dont know this tunnel (whom)? */
6113 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6114 GNUNET_break_op (0);
6118 /* Is this a forward or backward ACK? */
6119 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6121 struct MeshTunnelChildInfo *cinfo;
6123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6124 cinfo = tunnel_get_neighbor_fc (t, peer);
6125 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6126 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6131 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6139 * Core handler for path ACKs
6141 * @param cls closure
6142 * @param message message
6143 * @param peer peer identity this notification is about
6144 * @param atsi performance data
6145 * @param atsi_count number of records in 'atsi'
6147 * @return GNUNET_OK to keep the connection open,
6148 * GNUNET_SYSERR to close it (signal serious error)
6151 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6152 const struct GNUNET_MessageHeader *message,
6153 const struct GNUNET_ATS_Information *atsi,
6154 unsigned int atsi_count)
6156 struct GNUNET_MESH_PathACK *msg;
6157 struct GNUNET_PeerIdentity id;
6158 struct MeshPeerInfo *peer_info;
6159 struct MeshPeerPath *p;
6160 struct MeshTunnel *t;
6162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6163 GNUNET_i2s (&my_full_id));
6164 msg = (struct GNUNET_MESH_PathACK *) message;
6165 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6168 /* TODO notify that we don't know the tunnel */
6169 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6171 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6175 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6177 peer_info = peer_info_get (&msg->peer_id);
6178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6179 GNUNET_i2s (&msg->peer_id));
6180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6183 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6186 "connect_by_string completed, stopping search\n");
6187 regex_cancel_search (t->regex_ctx);
6188 t->regex_ctx = NULL;
6191 /* Add paths to peers? */
6192 p = tree_get_path_to_peer (t->tree, peer_info->id);
6195 path_add_to_peers (p, GNUNET_YES);
6203 /* Message for us? */
6204 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6207 if (NULL == t->owner)
6209 GNUNET_break_op (0);
6212 if (NULL != t->dht_get_type)
6214 GNUNET_DHT_get_stop (t->dht_get_type);
6215 t->dht_get_type = NULL;
6217 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6219 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6220 send_client_peer_connected (t, peer_info->id);
6225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6226 " not for us, retransmitting...\n");
6227 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6228 peer_info = peer_info_get (&msg->oid);
6229 if (NULL == peer_info)
6231 /* If we know the tunnel, we should DEFINITELY know the peer */
6235 send_prebuilt_message (message, &id, t);
6241 * Core handler for mesh keepalives.
6243 * @param cls closure
6244 * @param message message
6245 * @param peer peer identity this notification is about
6246 * @param atsi performance data
6247 * @param atsi_count number of records in 'atsi'
6248 * @return GNUNET_OK to keep the connection open,
6249 * GNUNET_SYSERR to close it (signal serious error)
6251 * TODO: Check who we got this from, to validate route.
6254 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6255 const struct GNUNET_MessageHeader *message,
6256 const struct GNUNET_ATS_Information *atsi,
6257 unsigned int atsi_count)
6259 struct GNUNET_MESH_TunnelKeepAlive *msg;
6260 struct MeshTunnel *t;
6262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6265 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6266 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6270 /* TODO notify that we dont know that tunnel */
6271 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6272 GNUNET_break_op (0);
6276 tunnel_reset_timeout (t);
6278 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6279 tunnel_send_multicast (t, message);
6286 * Functions to handle messages from core
6288 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6289 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6290 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6291 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6292 sizeof (struct GNUNET_MESH_PathBroken)},
6293 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6294 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6295 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6296 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6297 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6298 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6299 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6300 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6301 sizeof (struct GNUNET_MESH_ACK)},
6302 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6303 sizeof (struct GNUNET_MESH_Poll)},
6304 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6305 sizeof (struct GNUNET_MESH_PathACK)},
6311 /******************************************************************************/
6312 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6313 /******************************************************************************/
6316 * deregister_app: iterator for removing each application registered by a client
6318 * @param cls closure
6319 * @param key the hash of the application id (used to access the hashmap)
6320 * @param value the value stored at the key (client)
6322 * @return GNUNET_OK on success
6325 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6327 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6328 GNUNET_break (GNUNET_YES ==
6329 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6335 * notify_client_connection_failure: notify a client that the connection to the
6336 * requested remote peer is not possible (for instance, no route found)
6337 * Function called when the socket is ready to queue more data. "buf" will be
6338 * NULL and "size" zero if the socket was closed for writing in the meantime.
6340 * @param cls closure
6341 * @param size number of bytes available in buf
6342 * @param buf where the callee should write the message
6343 * @return number of bytes written to buf
6346 notify_client_connection_failure (void *cls, size_t size, void *buf)
6349 struct MeshPeerInfo *peer_info;
6350 struct GNUNET_MESH_PeerControl *msg;
6351 struct GNUNET_PeerIdentity id;
6353 if (0 == size && NULL == buf)
6355 // TODO retry? cancel?
6359 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6360 peer_info = (struct MeshPeerInfo *) cls;
6361 msg = (struct GNUNET_MESH_PeerControl *) buf;
6362 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6363 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6364 // msg->tunnel_id = htonl(peer_info->t->tid);
6365 GNUNET_PEER_resolve (peer_info->id, &id);
6366 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6374 * Send keepalive packets for a peer
6376 * @param cls Closure (tunnel for which to send the keepalive).
6377 * @param tc Notification context.
6380 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6382 struct MeshTunnel *t = cls;
6383 struct GNUNET_MESH_TunnelKeepAlive *msg;
6384 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6387 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6388 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6394 "sending keepalive for tunnel %d\n", t->id.tid);
6396 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6397 msg->header.size = htons (size);
6398 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6399 msg->oid = my_full_id;
6400 msg->tid = htonl (t->id.tid);
6401 tunnel_send_multicast (t, &msg->header);
6403 t->path_refresh_task =
6404 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6405 tunnel_reset_timeout(t);
6410 * Function to process paths received for a new peer addition. The recorded
6411 * paths form the initial tunnel, which can be optimized later.
6412 * Called on each result obtained for the DHT search.
6414 * @param cls closure
6415 * @param exp when will this value expire
6416 * @param key key of the result
6417 * @param get_path path of the get request
6418 * @param get_path_length lenght of get_path
6419 * @param put_path path of the put request
6420 * @param put_path_length length of the put_path
6421 * @param type type of the result
6422 * @param size number of bytes in data
6423 * @param data pointer to the result data
6425 * TODO: re-issue the request after certain time? cancel after X results?
6428 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6429 const struct GNUNET_HashCode * key,
6430 const struct GNUNET_PeerIdentity *get_path,
6431 unsigned int get_path_length,
6432 const struct GNUNET_PeerIdentity *put_path,
6433 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6434 size_t size, const void *data)
6436 struct MeshPathInfo *path_info = cls;
6437 struct MeshPeerPath *p;
6438 struct GNUNET_PeerIdentity pi;
6441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6442 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6445 p = path_build_from_dht (get_path, get_path_length, put_path,
6447 path_add_to_peers (p, GNUNET_NO);
6449 for (i = 0; i < path_info->peer->ntunnels; i++)
6451 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6452 peer_info_connect (path_info->peer, path_info->t);
6460 * Function to process paths received for a new peer addition. The recorded
6461 * paths form the initial tunnel, which can be optimized later.
6462 * Called on each result obtained for the DHT search.
6464 * @param cls closure
6465 * @param exp when will this value expire
6466 * @param key key of the result
6467 * @param get_path path of the get request
6468 * @param get_path_length lenght of get_path
6469 * @param put_path path of the put request
6470 * @param put_path_length length of the put_path
6471 * @param type type of the result
6472 * @param size number of bytes in data
6473 * @param data pointer to the result data
6476 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6477 const struct GNUNET_HashCode * key,
6478 const struct GNUNET_PeerIdentity *get_path,
6479 unsigned int get_path_length,
6480 const struct GNUNET_PeerIdentity *put_path,
6481 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6482 size_t size, const void *data)
6484 const struct PBlock *pb = data;
6485 const struct GNUNET_PeerIdentity *pi = &pb->id;
6486 struct MeshTunnel *t = cls;
6487 struct MeshPeerInfo *peer_info;
6488 struct MeshPeerPath *p;
6490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6491 if (size != sizeof (struct PBlock))
6493 GNUNET_break_op (0);
6496 if (ntohl(pb->type) != t->type)
6498 GNUNET_break_op (0);
6501 GNUNET_assert (NULL != t->owner);
6502 peer_info = peer_info_get (pi);
6503 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6505 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6507 p = path_build_from_dht (get_path, get_path_length, put_path,
6509 path_add_to_peers (p, GNUNET_NO);
6511 tunnel_add_peer (t, peer_info);
6512 peer_info_connect (peer_info, t);
6517 * Function to process DHT string to regex matching.
6518 * Called on each result obtained for the DHT search.
6520 * @param cls closure (search context)
6521 * @param exp when will this value expire
6522 * @param key key of the result
6523 * @param get_path path of the get request (not used)
6524 * @param get_path_length lenght of get_path (not used)
6525 * @param put_path path of the put request (not used)
6526 * @param put_path_length length of the put_path (not used)
6527 * @param type type of the result
6528 * @param size number of bytes in data
6529 * @param data pointer to the result data
6532 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6533 const struct GNUNET_HashCode * key,
6534 const struct GNUNET_PeerIdentity *get_path,
6535 unsigned int get_path_length,
6536 const struct GNUNET_PeerIdentity *put_path,
6537 unsigned int put_path_length,
6538 enum GNUNET_BLOCK_Type type,
6539 size_t size, const void *data)
6541 const struct MeshRegexAccept *block = data;
6542 struct MeshRegexSearchContext *ctx = cls;
6543 struct MeshRegexSearchInfo *info = ctx->info;
6544 // struct MeshPeerPath *p;
6545 struct MeshPeerInfo *peer_info;
6547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6549 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6551 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6554 peer_info = peer_info_get(&block->id);
6555 // p = path_build_from_dht (get_path, get_path_length, put_path,
6556 // put_path_length);
6557 // path_add_to_peers (p, GNUNET_NO);
6560 tunnel_add_peer (info->t, peer_info);
6561 peer_info_connect (peer_info, info->t);
6562 if (0 == info->peer)
6564 info->peer = peer_info->id;
6568 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6571 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6572 ®ex_connect_timeout,
6580 * Function to process DHT string to regex matching.
6581 * Called on each result obtained for the DHT search.
6583 * @param cls closure (search context)
6584 * @param exp when will this value expire
6585 * @param key key of the result
6586 * @param get_path path of the get request (not used)
6587 * @param get_path_length lenght of get_path (not used)
6588 * @param put_path path of the put request (not used)
6589 * @param put_path_length length of the put_path (not used)
6590 * @param type type of the result
6591 * @param size number of bytes in data
6592 * @param data pointer to the result data
6594 * TODO: re-issue the request after certain time? cancel after X results?
6597 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6598 const struct GNUNET_HashCode * key,
6599 const struct GNUNET_PeerIdentity *get_path,
6600 unsigned int get_path_length,
6601 const struct GNUNET_PeerIdentity *put_path,
6602 unsigned int put_path_length,
6603 enum GNUNET_BLOCK_Type type,
6604 size_t size, const void *data)
6606 const struct MeshRegexBlock *block = data;
6607 struct MeshRegexSearchContext *ctx = cls;
6608 struct MeshRegexSearchInfo *info = ctx->info;
6612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6613 "DHT GET STRING RETURNED RESULTS\n");
6614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6615 " key: %s\n", GNUNET_h2s (key));
6617 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6619 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6622 copy = GNUNET_malloc (size);
6623 memcpy (copy, data, size);
6624 GNUNET_break (GNUNET_OK ==
6625 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6626 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6627 len = ntohl (block->n_proof);
6629 char proof[len + 1];
6631 memcpy (proof, &block[1], len);
6633 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6635 GNUNET_break_op (0);
6639 len = strlen (info->description);
6640 if (len == ctx->position) // String processed
6642 if (GNUNET_YES == ntohl (block->accepting))
6644 regex_find_path(key, ctx);
6648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6649 // FIXME REGEX this block not successful, wait for more? start timeout?
6654 regex_next_edge (block, size, ctx);
6659 /******************************************************************************/
6660 /********************* MESH LOCAL HANDLES **************************/
6661 /******************************************************************************/
6665 * Handler for client disconnection
6667 * @param cls closure
6668 * @param client identification of the client; NULL
6669 * for the last call when the server is destroyed
6672 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6674 struct MeshClient *c;
6675 struct MeshClient *next;
6678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6685 // return; uncomment for regex_profiler
6689 if (c->handle != client)
6691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6697 GNUNET_SERVER_client_drop (c->handle);
6698 c->shutting_down = GNUNET_YES;
6699 GNUNET_assert (NULL != c->own_tunnels);
6700 GNUNET_assert (NULL != c->incoming_tunnels);
6701 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6702 &tunnel_destroy_iterator, c);
6703 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6704 &tunnel_destroy_iterator, c);
6705 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6706 &tunnel_destroy_iterator, c);
6707 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6708 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6709 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6711 /* deregister clients applications */
6712 if (NULL != c->apps)
6714 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6715 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6717 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6718 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6720 GNUNET_SCHEDULER_cancel (announce_applications_task);
6721 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6723 if (NULL != c->types)
6724 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6725 for (i = 0; i < c->n_regex; i++)
6727 GNUNET_free (c->regexes[i].regex);
6728 if (NULL != c->regexes[i].dfa)
6729 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6731 GNUNET_free_non_null (c->regexes);
6732 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6733 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6735 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6738 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6747 * Handler for new clients
6749 * @param cls closure
6750 * @param client identification of the client
6751 * @param message the actual message, which includes messages the client wants
6754 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6755 const struct GNUNET_MessageHeader *message)
6757 struct GNUNET_MESH_ClientConnect *cc_msg;
6758 struct MeshClient *c;
6759 GNUNET_MESH_ApplicationType *a;
6766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6767 /* Check data sanity */
6768 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6769 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6770 ntypes = ntohs (cc_msg->types);
6771 napps = ntohs (cc_msg->applications);
6773 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6780 /* Create new client structure */
6781 c = GNUNET_malloc (sizeof (struct MeshClient));
6782 c->id = next_client_id++;
6783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6785 GNUNET_SERVER_client_keep (client);
6786 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6789 GNUNET_MESH_ApplicationType at;
6790 struct GNUNET_HashCode hc;
6792 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6793 for (i = 0; i < napps; i++)
6796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6797 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6798 /* store in clients hashmap */
6799 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6800 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6801 /* store in global hashmap, for announcements */
6802 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6803 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6805 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6806 announce_applications_task =
6807 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6813 struct GNUNET_HashCode hc;
6815 t = (uint16_t *) & a[napps];
6816 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6817 for (i = 0; i < ntypes; i++)
6820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6821 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6823 /* store in clients hashmap */
6824 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6825 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6826 /* store in global hashmap */
6827 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6828 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6832 " client has %u+%u subscriptions\n", napps, ntypes);
6834 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6835 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6836 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6837 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6838 GNUNET_SERVER_notification_context_add (nc, client);
6839 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6841 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6847 * Handler for clients announcing available services by a regular expression.
6849 * @param cls closure
6850 * @param client identification of the client
6851 * @param message the actual message, which includes messages the client wants
6854 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6855 const struct GNUNET_MessageHeader *message)
6857 const struct GNUNET_MESH_RegexAnnounce *msg;
6858 struct MeshRegexDescriptor rd;
6859 struct MeshClient *c;
6863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6865 /* Sanity check for client registration */
6866 if (NULL == (c = client_get (client)))
6869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6874 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6875 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6876 regex = GNUNET_malloc (len + 1);
6877 memcpy (regex, &msg[1], len);
6880 rd.compression = ntohs (msg->compression_characters);
6882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6885 GNUNET_array_append (c->regexes, c->n_regex, rd);
6886 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6888 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6900 * Handler for requests of new tunnels
6902 * @param cls closure
6903 * @param client identification of the client
6904 * @param message the actual message
6907 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6908 const struct GNUNET_MessageHeader *message)
6910 struct GNUNET_MESH_TunnelMessage *t_msg;
6911 struct MeshTunnel *t;
6912 struct MeshClient *c;
6913 MESH_TunnelNumber tid;
6915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6917 /* Sanity check for client registration */
6918 if (NULL == (c = client_get (client)))
6921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6926 /* Message sanity check */
6927 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6930 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6934 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6935 /* Sanity check for tunnel numbering */
6936 tid = ntohl (t_msg->tunnel_id);
6937 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6940 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6943 /* Sanity check for duplicate tunnel IDs */
6944 if (NULL != tunnel_get_by_local_id (c, tid))
6947 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6951 while (NULL != tunnel_get_by_pi (myid, next_tid))
6952 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6953 t = tunnel_new (myid, next_tid++, c, tid);
6956 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6958 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6961 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6963 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6964 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6967 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6973 * Handler for requests of deleting tunnels
6975 * @param cls closure
6976 * @param client identification of the client
6977 * @param message the actual message
6980 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6981 const struct GNUNET_MessageHeader *message)
6983 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6984 struct MeshClient *c;
6985 struct MeshTunnel *t;
6986 MESH_TunnelNumber tid;
6988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6989 "Got a DESTROY TUNNEL from client!\n");
6991 /* Sanity check for client registration */
6992 if (NULL == (c = client_get (client)))
6995 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7000 /* Message sanity check */
7001 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7004 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7008 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7010 /* Retrieve tunnel */
7011 tid = ntohl (tunnel_msg->tunnel_id);
7012 t = tunnel_get_by_local_id(c, tid);
7015 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7017 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7020 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7022 client_ignore_tunnel (c, t);
7024 // TODO: when to destroy incoming tunnel?
7025 if (t->nclients == 0)
7027 GNUNET_assert (GNUNET_YES ==
7028 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
7030 GNUNET_assert (GNUNET_YES ==
7031 GNUNET_CONTAINER_multihashmap_remove (t->peers,
7032 &my_full_id.hashPubKey,
7036 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7039 send_client_tunnel_disconnect(t, c);
7040 client_delete_tunnel(c, t);
7042 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7044 tunnel_send_destroy (t);
7045 t->destroy = GNUNET_YES;
7046 // The tunnel will be destroyed when the last message is transmitted.
7047 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7053 * Handler for requests of seeting tunnel's speed.
7055 * @param cls Closure (unused).
7056 * @param client Identification of the client.
7057 * @param message The actual message.
7060 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7061 const struct GNUNET_MessageHeader *message)
7063 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7064 struct MeshClient *c;
7065 struct MeshTunnel *t;
7066 MESH_TunnelNumber tid;
7068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7069 "Got a SPEED request from client!\n");
7071 /* Sanity check for client registration */
7072 if (NULL == (c = client_get (client)))
7075 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7081 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7083 /* Retrieve tunnel */
7084 tid = ntohl (tunnel_msg->tunnel_id);
7085 t = tunnel_get_by_local_id(c, tid);
7088 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7090 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7094 switch (ntohs(message->type))
7096 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7097 t->speed_min = GNUNET_YES;
7099 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7100 t->speed_min = GNUNET_NO;
7105 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7110 * Handler for requests of seeting tunnel's buffering policy.
7112 * @param cls Closure (unused).
7113 * @param client Identification of the client.
7114 * @param message The actual message.
7117 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7118 const struct GNUNET_MessageHeader *message)
7120 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7121 struct MeshClient *c;
7122 struct MeshTunnel *t;
7123 MESH_TunnelNumber tid;
7125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7126 "Got a BUFFER request from client!\n");
7128 /* Sanity check for client registration */
7129 if (NULL == (c = client_get (client)))
7132 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7137 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7139 /* Retrieve tunnel */
7140 tid = ntohl (tunnel_msg->tunnel_id);
7141 t = tunnel_get_by_local_id(c, tid);
7144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7146 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7150 switch (ntohs(message->type))
7152 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7153 t->nobuffer = GNUNET_NO;
7155 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7156 t->nobuffer = GNUNET_YES;
7162 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7167 * Handler for connection requests to new peers
7169 * @param cls closure
7170 * @param client identification of the client
7171 * @param message the actual message (PeerControl)
7174 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7175 const struct GNUNET_MessageHeader *message)
7177 struct GNUNET_MESH_PeerControl *peer_msg;
7178 struct MeshPeerInfo *peer_info;
7179 struct MeshClient *c;
7180 struct MeshTunnel *t;
7181 MESH_TunnelNumber tid;
7183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7184 /* Sanity check for client registration */
7185 if (NULL == (c = client_get (client)))
7188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7193 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7195 /* Sanity check for message size */
7196 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7203 /* Tunnel exists? */
7204 tid = ntohl (peer_msg->tunnel_id);
7205 t = tunnel_get_by_local_id (c, tid);
7209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7213 /* Does client own tunnel? */
7214 if (t->owner->handle != client)
7217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7221 GNUNET_i2s (&peer_msg->peer));
7222 peer_info = peer_info_get (&peer_msg->peer);
7224 tunnel_add_peer (t, peer_info);
7225 peer_info_connect (peer_info, t);
7227 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7233 * Handler for disconnection requests of peers in a tunnel
7235 * @param cls closure
7236 * @param client identification of the client
7237 * @param message the actual message (PeerControl)
7240 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7241 const struct GNUNET_MessageHeader *message)
7243 struct GNUNET_MESH_PeerControl *peer_msg;
7244 struct MeshPeerInfo *peer_info;
7245 struct MeshClient *c;
7246 struct MeshTunnel *t;
7247 MESH_TunnelNumber tid;
7249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7250 /* Sanity check for client registration */
7251 if (NULL == (c = client_get (client)))
7254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7259 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7261 /* Sanity check for message size */
7262 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7269 /* Tunnel exists? */
7270 tid = ntohl (peer_msg->tunnel_id);
7271 t = tunnel_get_by_local_id (c, tid);
7275 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7280 /* Does client own tunnel? */
7281 if (t->owner->handle != client)
7284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7289 GNUNET_i2s (&peer_msg->peer));
7290 /* Is the peer in the tunnel? */
7292 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7293 if (NULL == peer_info)
7296 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7300 /* Ok, delete peer from tunnel */
7301 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7302 &peer_msg->peer.hashPubKey);
7304 send_destroy_path (t, peer_info->id);
7305 tunnel_delete_peer (t, peer_info->id);
7306 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7311 * Handler for blacklist requests of peers in a tunnel
7313 * @param cls closure
7314 * @param client identification of the client
7315 * @param message the actual message (PeerControl)
7317 * FIXME implement DHT block bloomfilter
7320 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7321 const struct GNUNET_MessageHeader *message)
7323 struct GNUNET_MESH_PeerControl *peer_msg;
7324 struct MeshClient *c;
7325 struct MeshTunnel *t;
7326 MESH_TunnelNumber tid;
7328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7329 /* Sanity check for client registration */
7330 if (NULL == (c = client_get (client)))
7333 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7338 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7340 /* Sanity check for message size */
7341 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7344 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7348 /* Tunnel exists? */
7349 tid = ntohl (peer_msg->tunnel_id);
7350 t = tunnel_get_by_local_id (c, tid);
7354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7359 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7360 GNUNET_PEER_intern(&peer_msg->peer));
7365 * Handler for unblacklist requests of peers in a tunnel
7367 * @param cls closure
7368 * @param client identification of the client
7369 * @param message the actual message (PeerControl)
7372 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7373 const struct GNUNET_MessageHeader *message)
7375 struct GNUNET_MESH_PeerControl *peer_msg;
7376 struct MeshClient *c;
7377 struct MeshTunnel *t;
7378 MESH_TunnelNumber tid;
7382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7383 /* Sanity check for client registration */
7384 if (NULL == (c = client_get (client)))
7387 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7392 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7394 /* Sanity check for message size */
7395 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7398 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7402 /* Tunnel exists? */
7403 tid = ntohl (peer_msg->tunnel_id);
7404 t = tunnel_get_by_local_id (c, tid);
7408 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7413 /* if peer is not known, complain */
7414 pid = GNUNET_PEER_search (&peer_msg->peer);
7421 /* search and remove from list */
7422 for (i = 0; i < t->nblacklisted; i++)
7424 if (t->blacklisted[i] == pid)
7426 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7427 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7432 /* if peer hasn't been blacklisted, complain */
7438 * Handler for connection requests to new peers by type
7440 * @param cls closure
7441 * @param client identification of the client
7442 * @param message the actual message (ConnectPeerByType)
7445 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7446 const struct GNUNET_MessageHeader *message)
7448 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7449 struct MeshClient *c;
7450 struct MeshTunnel *t;
7451 struct GNUNET_HashCode hash;
7452 MESH_TunnelNumber tid;
7454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7455 /* Sanity check for client registration */
7456 if (NULL == (c = client_get (client)))
7459 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7464 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7466 /* Sanity check for message size */
7467 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7468 ntohs (connect_msg->header.size))
7471 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7475 /* Tunnel exists? */
7476 tid = ntohl (connect_msg->tunnel_id);
7477 t = tunnel_get_by_local_id (c, tid);
7481 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7485 /* Does client own tunnel? */
7486 if (t->owner->handle != client)
7489 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7493 /* Do WE have the service? */
7494 t->type = ntohl (connect_msg->type);
7495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7496 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7497 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7500 /* Yes! Fast forward, add ourselves to the tunnel and send the
7501 * good news to the client, and alert the destination client of
7502 * an incoming tunnel.
7504 * FIXME send a path create to self, avoid code duplication
7506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7507 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7508 peer_info_get (&my_full_id),
7509 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7512 send_client_peer_connected (t, myid);
7513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7514 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7516 t->local_tid_dest = next_local_tid++;
7517 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7518 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7519 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7523 /* Ok, lets find a peer offering the service */
7524 if (NULL != t->dht_get_type)
7526 GNUNET_DHT_get_stop (t->dht_get_type);
7528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7529 GNUNET_h2s (&hash));
7531 GNUNET_DHT_get_start (dht_handle,
7532 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7534 dht_replication_level,
7535 GNUNET_DHT_RO_RECORD_ROUTE |
7536 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7538 &dht_get_type_handler, t);
7540 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7546 * Handler for connection requests to new peers by a string service description.
7548 * @param cls closure
7549 * @param client identification of the client
7550 * @param message the actual message, which includes messages the client wants
7553 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7554 const struct GNUNET_MessageHeader *message)
7556 struct GNUNET_MESH_ConnectPeerByString *msg;
7557 struct MeshRegexSearchContext *ctx;
7558 struct MeshRegexSearchInfo *info;
7559 struct GNUNET_DHT_GetHandle *get_h;
7560 struct GNUNET_HashCode key;
7561 struct MeshTunnel *t;
7562 struct MeshClient *c;
7563 MESH_TunnelNumber tid;
7568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7569 "Connect by string started\n");
7570 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7571 size = htons (message->size);
7573 /* Sanity check for client registration */
7574 if (NULL == (c = client_get (client)))
7577 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7582 /* Message size sanity check */
7583 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7586 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7590 /* Tunnel exists? */
7591 tid = ntohl (msg->tunnel_id);
7592 t = tunnel_get_by_local_id (c, tid);
7596 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7600 /* Does client own tunnel? */
7601 if (t->owner->handle != client)
7604 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7609 " on tunnel %s [%u]\n",
7610 GNUNET_i2s(&my_full_id),
7613 /* Only one connect_by_string allowed at the same time! */
7614 /* FIXME: allow more, return handle at api level to cancel, document */
7615 if (NULL != t->regex_ctx)
7618 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7622 /* Find string itself */
7623 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7624 string = (const char *) &msg[1];
7626 /* Initialize context */
7627 size = GNUNET_REGEX_get_first_key (string, len, &key);
7628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7629 " consumed %u bits out of %u\n", size, len);
7630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7631 " looking for %s\n", GNUNET_h2s (&key));
7633 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7635 info->description = GNUNET_strndup (string, len);
7636 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7637 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7640 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7641 ctx->position = size;
7645 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7647 /* Start search in DHT */
7648 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7649 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7650 &key, /* key to search */
7651 dht_replication_level, /* replication level */
7652 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7653 &info->description[size], /* xquery */
7654 // FIXME add BLOOMFILTER to exclude filtered peers
7655 len + 1 - size, /* xquery bits */
7656 // FIXME add BLOOMFILTER SIZE
7657 &dht_get_string_handler, ctx);
7659 GNUNET_break (GNUNET_OK ==
7660 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7663 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7665 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7671 * Handler for client traffic directed to one peer
7673 * @param cls closure
7674 * @param client identification of the client
7675 * @param message the actual message
7678 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7679 const struct GNUNET_MessageHeader *message)
7681 struct MeshClient *c;
7682 struct MeshTunnel *t;
7683 struct MeshPeerInfo *pi;
7684 struct GNUNET_MESH_Unicast *data_msg;
7685 MESH_TunnelNumber tid;
7688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7689 "Got a unicast request from a client!\n");
7691 /* Sanity check for client registration */
7692 if (NULL == (c = client_get (client)))
7695 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7700 data_msg = (struct GNUNET_MESH_Unicast *) message;
7702 /* Sanity check for message size */
7703 size = ntohs (message->size);
7704 if (sizeof (struct GNUNET_MESH_Unicast) +
7705 sizeof (struct GNUNET_MessageHeader) > size)
7708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7712 /* Tunnel exists? */
7713 tid = ntohl (data_msg->tid);
7714 t = tunnel_get_by_local_id (c, tid);
7718 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7722 /* Is it a local tunnel? Then, does client own the tunnel? */
7723 if (t->owner->handle != client)
7726 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7730 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7731 &data_msg->destination.hashPubKey);
7732 /* Is the selected peer in the tunnel? */
7736 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7740 /* PID should be as expected */
7741 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7744 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7745 "Unicast PID, expected %u, got %u\n",
7746 t->fwd_pid + 1, ntohl (data_msg->pid));
7747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7751 /* Ok, everything is correct, send the message
7752 * (pretend we got it from a mesh peer)
7755 /* Work around const limitation */
7756 char buf[ntohs (message->size)] GNUNET_ALIGN;
7757 struct GNUNET_MESH_Unicast *copy;
7759 copy = (struct GNUNET_MESH_Unicast *) buf;
7760 memcpy (buf, data_msg, size);
7761 copy->oid = my_full_id;
7762 copy->tid = htonl (t->id.tid);
7763 copy->ttl = htonl (default_ttl);
7764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7765 " calling generic handler...\n");
7766 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7769 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7776 * Handler for client traffic directed to the origin
7778 * @param cls closure
7779 * @param client identification of the client
7780 * @param message the actual message
7783 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7784 const struct GNUNET_MessageHeader *message)
7786 struct GNUNET_MESH_ToOrigin *data_msg;
7787 struct MeshTunnelClientInfo *clinfo;
7788 struct MeshClient *c;
7789 struct MeshTunnel *t;
7790 MESH_TunnelNumber tid;
7793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7794 "Got a ToOrigin request from a client!\n");
7795 /* Sanity check for client registration */
7796 if (NULL == (c = client_get (client)))
7799 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7804 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7806 /* Sanity check for message size */
7807 size = ntohs (message->size);
7808 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7809 sizeof (struct GNUNET_MessageHeader) > size)
7812 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7816 /* Tunnel exists? */
7817 tid = ntohl (data_msg->tid);
7818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7819 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7825 t = tunnel_get_by_local_id (c, tid);
7828 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7829 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7835 /* It should be sent by someone who has this as incoming tunnel. */
7836 if (GNUNET_NO == client_knows_tunnel (c, t))
7839 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7843 /* PID should be as expected */
7844 clinfo = tunnel_get_client_fc (t, c);
7845 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7849 "To Origin PID, expected %u, got %u\n",
7850 clinfo->bck_pid + 1,
7851 ntohl (data_msg->pid));
7852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7857 /* Ok, everything is correct, send the message
7858 * (pretend we got it from a mesh peer)
7861 char buf[ntohs (message->size)] GNUNET_ALIGN;
7862 struct GNUNET_MESH_ToOrigin *copy;
7864 /* Work around const limitation */
7865 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7866 memcpy (buf, data_msg, size);
7867 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7868 copy->tid = htonl (t->id.tid);
7869 copy->ttl = htonl (default_ttl);
7870 copy->pid = htonl (t->bck_pid + 1);
7872 copy->sender = my_full_id;
7873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7874 " calling generic handler...\n");
7875 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7877 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7884 * Handler for client traffic directed to all peers in a tunnel
7886 * @param cls closure
7887 * @param client identification of the client
7888 * @param message the actual message
7891 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7892 const struct GNUNET_MessageHeader *message)
7894 struct MeshClient *c;
7895 struct MeshTunnel *t;
7896 struct GNUNET_MESH_Multicast *data_msg;
7897 MESH_TunnelNumber tid;
7899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7900 "Got a multicast request from a client!\n");
7902 /* Sanity check for client registration */
7903 if (NULL == (c = client_get (client)))
7906 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7911 data_msg = (struct GNUNET_MESH_Multicast *) message;
7913 /* Sanity check for message size */
7914 if (sizeof (struct GNUNET_MESH_Multicast) +
7915 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7918 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7922 /* Tunnel exists? */
7923 tid = ntohl (data_msg->tid);
7924 t = tunnel_get_by_local_id (c, tid);
7928 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7929 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7931 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7935 /* Does client own tunnel? */
7936 if (t->owner->handle != client)
7939 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7943 /* PID should be as expected */
7944 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7947 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7948 "Multicast PID, expected %u, got %u\n",
7949 t->fwd_pid + 1, ntohl (data_msg->pid));
7950 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7955 char buf[ntohs (message->size)] GNUNET_ALIGN;
7956 struct GNUNET_MESH_Multicast *copy;
7958 copy = (struct GNUNET_MESH_Multicast *) buf;
7959 memcpy (buf, message, ntohs (message->size));
7960 copy->oid = my_full_id;
7961 copy->tid = htonl (t->id.tid);
7962 copy->ttl = htonl (default_ttl);
7963 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7965 " calling generic handler...\n");
7966 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7969 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7975 * Handler for client's ACKs for payload traffic.
7977 * @param cls Closure (unused).
7978 * @param client Identification of the client.
7979 * @param message The actual message.
7982 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7983 const struct GNUNET_MessageHeader *message)
7985 struct GNUNET_MESH_LocalAck *msg;
7986 struct MeshTunnel *t;
7987 struct MeshClient *c;
7988 MESH_TunnelNumber tid;
7991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7992 /* Sanity check for client registration */
7993 if (NULL == (c = client_get (client)))
7996 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8001 msg = (struct GNUNET_MESH_LocalAck *) message;
8003 /* Tunnel exists? */
8004 tid = ntohl (msg->tunnel_id);
8005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8006 t = tunnel_get_by_local_id (c, tid);
8010 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8011 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8016 ack = ntohl (msg->max_pid);
8017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8019 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8020 if (NULL != t->owner && t->owner->handle == client)
8022 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8024 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8028 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8029 tunnel_set_client_fwd_ack (t, c, ack);
8030 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8033 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8040 * Iterator over all peers to send a monitoring client info about a tunnel.
8042 * @param cls Closure (message being built).
8043 * @param key Key (hashed tunnel ID, unused).
8044 * @param value Peer info.
8046 * @return GNUNET_YES, to keep iterating.
8049 monitor_peers_iterator (void *cls,
8050 const struct GNUNET_HashCode * key,
8053 struct GNUNET_MESH_LocalMonitor *msg = cls;
8054 struct GNUNET_PeerIdentity *id;
8055 struct MeshPeerInfo *info = value;
8057 id = (struct GNUNET_PeerIdentity *) &msg[1];
8058 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8061 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8062 "* sending info about peer %s [%u]\n",
8063 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8071 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8073 * @param cls Closure (client handle).
8074 * @param key Key (hashed tunnel ID, unused).
8075 * @param value Tunnel info.
8077 * @return GNUNET_YES, to keep iterating.
8080 monitor_all_tunnels_iterator (void *cls,
8081 const struct GNUNET_HashCode * key,
8084 struct GNUNET_SERVER_Client *client = cls;
8085 struct MeshTunnel *t = value;
8086 struct GNUNET_MESH_LocalMonitor *msg;
8089 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8090 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8091 npeers * sizeof (struct GNUNET_PeerIdentity));
8092 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8093 msg->tunnel_id = htonl (t->id.tid);
8094 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8095 npeers * sizeof (struct GNUNET_PeerIdentity));
8096 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8098 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8099 monitor_peers_iterator,
8102 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8103 "* sending info about tunnel %s [%u] (%u peers)\n",
8104 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8106 if (msg->npeers != npeers)
8109 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8110 "Get tunnels fail: size %u - iter %u\n",
8111 npeers, msg->npeers);
8114 msg->npeers = htonl (npeers);
8115 GNUNET_SERVER_notification_context_unicast (nc, client,
8116 &msg->header, GNUNET_NO);
8122 * Handler for client's MONITOR request.
8124 * @param cls Closure (unused).
8125 * @param client Identification of the client.
8126 * @param message The actual message.
8129 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8130 const struct GNUNET_MessageHeader *message)
8132 struct MeshClient *c;
8134 /* Sanity check for client registration */
8135 if (NULL == (c = client_get (client)))
8138 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8143 "Received get tunnels request from client %u\n",
8145 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8146 monitor_all_tunnels_iterator,
8148 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8149 "Get tunnels request from client %u completed\n",
8151 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8156 * Data needed to build a Monitor_Tunnel message.
8158 struct MeshMonitorTunnelContext
8161 * Partial message, including peer count.
8163 struct GNUNET_MESH_LocalMonitor *msg;
8166 * Hashmap with positions: peer->position.
8168 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8171 * Index of the parent of each peer in the message, realtive to the absolute
8172 * order in the array (can be in a previous message).
8174 uint32_t parents[1024];
8177 * Peers visited so far in the tree, aka position of the current peer.
8179 unsigned int npeers;
8182 * Client requesting the info.
8184 struct MeshClient *c;
8189 * Send a client a message about the structure of a tunnel.
8191 * @param ctx Context of the tunnel iteration, with info regarding the state
8192 * of the execution and the number of peers visited for this message.
8195 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8197 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8198 struct GNUNET_PeerIdentity *pid;
8199 unsigned int *parent;
8202 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8203 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8204 resp->header.size = htons (size);
8205 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8206 parent = (unsigned int *) &pid[resp->npeers];
8207 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8208 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8209 &resp->header, GNUNET_NO);
8213 * Iterator over a tunnel tree to build a message containing all peers
8214 * the in the tunnel, including relay nodes.
8216 * @param cls Closure (pointer to pointer of message being built).
8217 * @param peer Short ID of a peer.
8218 * @param parent Short ID of the @c peer 's parent.
8221 tunnel_tree_iterator (void *cls,
8222 GNUNET_PEER_Id peer,
8223 GNUNET_PEER_Id parent)
8225 struct MeshMonitorTunnelContext *ctx = cls;
8226 struct GNUNET_MESH_LocalMonitor *msg;
8227 struct GNUNET_PeerIdentity *pid;
8228 struct GNUNET_PeerIdentity ppid;
8231 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8232 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8233 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8234 &pid[msg->npeers].hashPubKey,
8235 (void *) (long) ctx->npeers,
8236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8237 GNUNET_PEER_resolve (parent, &ppid);
8238 ctx->parents[msg->npeers] =
8239 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8245 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8247 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8250 send_client_tunnel_info (ctx);
8257 * Handler for client's MONITOR_TUNNEL request.
8259 * @param cls Closure (unused).
8260 * @param client Identification of the client.
8261 * @param message The actual message.
8264 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8265 const struct GNUNET_MessageHeader *message)
8267 const struct GNUNET_MESH_LocalMonitor *msg;
8268 struct GNUNET_MESH_LocalMonitor *resp;
8269 struct MeshMonitorTunnelContext ctx;
8270 struct MeshClient *c;
8271 struct MeshTunnel *t;
8273 /* Sanity check for client registration */
8274 if (NULL == (c = client_get (client)))
8277 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8281 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8282 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8283 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8286 ntohl (msg->tunnel_id));
8287 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8290 /* We don't know the tunnel */
8291 struct GNUNET_MESH_LocalMonitor warn;
8294 warn.npeers = htonl (UINT_MAX);
8295 GNUNET_SERVER_notification_context_unicast (nc, client,
8298 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8302 /* Initialize context */
8303 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8307 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8311 /* Collect and send information */
8312 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8313 send_client_tunnel_info (&ctx);
8316 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8319 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8320 "Monitor tunnel request from client %u completed\n",
8322 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8327 * Functions to handle messages from clients
8329 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8330 {&handle_local_new_client, NULL,
8331 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8332 {&handle_local_announce_regex, NULL,
8333 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8334 {&handle_local_tunnel_create, NULL,
8335 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8336 sizeof (struct GNUNET_MESH_TunnelMessage)},
8337 {&handle_local_tunnel_destroy, NULL,
8338 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8339 sizeof (struct GNUNET_MESH_TunnelMessage)},
8340 {&handle_local_tunnel_speed, NULL,
8341 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8342 sizeof (struct GNUNET_MESH_TunnelMessage)},
8343 {&handle_local_tunnel_speed, NULL,
8344 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8345 sizeof (struct GNUNET_MESH_TunnelMessage)},
8346 {&handle_local_tunnel_buffer, NULL,
8347 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8348 sizeof (struct GNUNET_MESH_TunnelMessage)},
8349 {&handle_local_tunnel_buffer, NULL,
8350 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8351 sizeof (struct GNUNET_MESH_TunnelMessage)},
8352 {&handle_local_connect_add, NULL,
8353 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8354 sizeof (struct GNUNET_MESH_PeerControl)},
8355 {&handle_local_connect_del, NULL,
8356 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8357 sizeof (struct GNUNET_MESH_PeerControl)},
8358 {&handle_local_blacklist, NULL,
8359 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8360 sizeof (struct GNUNET_MESH_PeerControl)},
8361 {&handle_local_unblacklist, NULL,
8362 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8363 sizeof (struct GNUNET_MESH_PeerControl)},
8364 {&handle_local_connect_by_type, NULL,
8365 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8366 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8367 {&handle_local_connect_by_string, NULL,
8368 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8369 {&handle_local_unicast, NULL,
8370 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8371 {&handle_local_to_origin, NULL,
8372 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8373 {&handle_local_multicast, NULL,
8374 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8375 {&handle_local_ack, NULL,
8376 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8377 sizeof (struct GNUNET_MESH_LocalAck)},
8378 {&handle_local_get_tunnels, NULL,
8379 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8380 sizeof (struct GNUNET_MessageHeader)},
8381 {&handle_local_show_tunnel, NULL,
8382 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8383 sizeof (struct GNUNET_MESH_LocalMonitor)},
8389 * To be called on core init/fail.
8391 * @param cls service closure
8392 * @param server handle to the server for this service
8393 * @param identity the public identity of this peer
8396 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8397 const struct GNUNET_PeerIdentity *identity)
8400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8401 core_handle = server;
8402 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8406 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8408 GNUNET_abort(); // Try harder
8415 * Method called whenever a given peer connects.
8417 * @param cls closure
8418 * @param peer peer identity this notification is about
8419 * @param atsi performance data for the connection
8420 * @param atsi_count number of records in 'atsi'
8423 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8424 const struct GNUNET_ATS_Information *atsi,
8425 unsigned int atsi_count)
8427 struct MeshPeerInfo *peer_info;
8428 struct MeshPeerPath *path;
8430 DEBUG_CONN ("Peer connected\n");
8431 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8432 peer_info = peer_info_get (peer);
8433 if (myid == peer_info->id)
8435 DEBUG_CONN (" (self)\n");
8440 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8442 path = path_new (2);
8443 path->peers[0] = myid;
8444 path->peers[1] = peer_info->id;
8445 GNUNET_PEER_change_rc (myid, 1);
8446 GNUNET_PEER_change_rc (peer_info->id, 1);
8447 peer_info_add_path (peer_info, path, GNUNET_YES);
8448 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8454 * Method called whenever a peer disconnects.
8456 * @param cls closure
8457 * @param peer peer identity this notification is about
8460 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8462 struct MeshPeerInfo *pi;
8463 struct MeshPeerQueue *q;
8464 struct MeshPeerQueue *n;
8466 DEBUG_CONN ("Peer disconnected\n");
8467 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8477 /* TODO try to reroute this traffic instead */
8478 queue_destroy(q, GNUNET_YES);
8481 if (NULL != pi->core_transmit)
8483 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8484 pi->core_transmit = NULL;
8486 peer_info_remove_path (pi, pi->id, myid);
8489 DEBUG_CONN (" (self)\n");
8491 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8496 /******************************************************************************/
8497 /************************ MAIN FUNCTIONS ****************************/
8498 /******************************************************************************/
8501 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8503 * @param cls closure
8504 * @param key current key code
8505 * @param value value in the hash map
8506 * @return GNUNET_YES if we should continue to iterate,
8510 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8512 struct MeshTunnel *t = value;
8519 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8521 * @param cls closure
8522 * @param key current key code
8523 * @param value value in the hash map
8524 * @return GNUNET_YES if we should continue to iterate,
8528 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8530 struct MeshPeerInfo *p = value;
8531 struct MeshPeerQueue *q;
8532 struct MeshPeerQueue *n;
8540 queue_destroy(q, GNUNET_YES);
8544 peer_info_destroy (p);
8550 * Task run during shutdown.
8556 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8560 if (core_handle != NULL)
8562 GNUNET_CORE_disconnect (core_handle);
8567 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8570 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8571 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8572 if (dht_handle != NULL)
8574 GNUNET_DHT_disconnect (dht_handle);
8579 GNUNET_SERVER_notification_context_destroy (nc);
8582 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8584 GNUNET_SCHEDULER_cancel (announce_id_task);
8585 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8592 * Callback for hostkey read/generation
8595 * @param pk the private key
8596 * @param emsg error message
8599 key_generation_cb (void *cls,
8600 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8603 struct MeshPeerInfo *peer;
8604 struct MeshPeerPath *p;
8609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8610 _("Mesh service could not access hostkey. Exiting.\n"));
8611 GNUNET_SCHEDULER_shutdown ();
8614 my_private_key = pk;
8615 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8616 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8617 &my_full_id.hashPubKey);
8618 myid = GNUNET_PEER_intern (&my_full_id);
8619 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8620 "Mesh for peer [%s] starting\n",
8621 GNUNET_i2s(&my_full_id));
8623 // transport_handle = GNUNET_TRANSPORT_connect(c,
8633 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8636 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8637 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8638 GNUNET_SERVER_disconnect_notify (server_handle,
8639 &handle_local_client_disconnect, NULL);
8643 clients_tail = NULL;
8646 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8647 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8649 /* Create a peer_info for the local peer */
8650 peer = peer_info_get (&my_full_id);
8653 GNUNET_PEER_change_rc (myid, 1);
8654 peer_info_add_path (peer, p, GNUNET_YES);
8655 GNUNET_SERVER_resume (server_handle);
8656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8661 * Process mesh requests.
8663 * @param cls closure
8664 * @param server the initialized server
8665 * @param c configuration to use
8668 run (void *cls, struct GNUNET_SERVER_Handle *server,
8669 const struct GNUNET_CONFIGURATION_Handle *c)
8673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8674 server_handle = server;
8675 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8676 NULL, /* Closure passed to MESH functions */
8677 &core_init, /* Call core_init once connected */
8678 &core_connect, /* Handle connects */
8679 &core_disconnect, /* remove peers on disconnects */
8680 NULL, /* Don't notify about all incoming messages */
8681 GNUNET_NO, /* For header only in notification */
8682 NULL, /* Don't notify about all outbound messages */
8683 GNUNET_NO, /* For header-only out notification */
8684 core_handlers); /* Register these handlers */
8686 if (core_handle == NULL)
8689 GNUNET_SCHEDULER_shutdown ();
8694 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8697 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8699 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8701 GNUNET_SCHEDULER_shutdown ();
8706 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8707 &refresh_path_time))
8709 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8711 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8712 "mesh", "refresh path time");
8713 GNUNET_SCHEDULER_shutdown ();
8718 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8719 &app_announce_time))
8721 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8723 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8724 "mesh", "app announce time");
8725 GNUNET_SCHEDULER_shutdown ();
8728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8729 "APP_ANNOUNCE_TIME %llu ms\n",
8730 app_announce_time.rel_value);
8732 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8737 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8738 "mesh", "id announce time");
8739 GNUNET_SCHEDULER_shutdown ();
8744 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8747 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8749 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8750 "mesh", "connect timeout");
8751 GNUNET_SCHEDULER_shutdown ();
8756 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8761 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8762 "mesh", "max msgs queue");
8763 GNUNET_SCHEDULER_shutdown ();
8768 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8773 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8774 "mesh", "max tunnels");
8775 GNUNET_SCHEDULER_shutdown ();
8780 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8783 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8785 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8786 "mesh", "default ttl", 64);
8791 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8792 &dht_replication_level))
8794 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8796 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8797 "mesh", "dht replication level", 3);
8798 dht_replication_level = 3;
8801 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8802 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8803 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8804 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8805 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8807 dht_handle = GNUNET_DHT_connect (c, 64);
8808 if (NULL == dht_handle)
8812 stats = GNUNET_STATISTICS_create ("mesh", c);
8814 GNUNET_SERVER_suspend (server_handle);
8815 /* Scheduled the task to clean up when shutdown is called */
8816 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8818 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile, &key_generation_cb, NULL);
8819 GNUNET_free (keyfile);
8824 * The main function for the mesh service.
8826 * @param argc number of arguments from the command line
8827 * @param argv command line arguments
8828 * @return 0 ok, 1 on error
8831 main (int argc, char *const *argv)
8836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8837 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8839 ret = (GNUNET_OK == r) ? 0 : 1;
8840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8844 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8845 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8846 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);