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 * How many peers do we want to remember?
929 static unsigned long long max_peers;
932 /*************************** Static global variables **************************/
935 * Hostkey generation context
937 static struct GNUNET_CRYPTO_RsaKeyGenerationContext *keygen;
940 * DLL with all the clients, head.
942 static struct MeshClient *clients;
945 * DLL with all the clients, tail.
947 static struct MeshClient *clients_tail;
950 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
952 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
955 * Number of tunnels known.
957 static unsigned long long n_tunnels;
960 * Tunnels incoming, indexed by MESH_TunnelNumber
961 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
963 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
966 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
968 static struct GNUNET_CONTAINER_MultiHashMap *peers;
971 * Handle to communicate with transport
973 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
976 * Handle to communicate with core.
978 static struct GNUNET_CORE_Handle *core_handle;
983 static struct GNUNET_DHT_Handle *dht_handle;
988 static struct GNUNET_SERVER_Handle *server_handle;
991 * Handle to the statistics service.
993 static struct GNUNET_STATISTICS_Handle *stats;
996 * Notification context, to send messages to local clients.
998 static struct GNUNET_SERVER_NotificationContext *nc;
1001 * Local peer own ID (memory efficient handle).
1003 static GNUNET_PEER_Id myid;
1006 * Local peer own ID (full value).
1008 static struct GNUNET_PeerIdentity my_full_id;
1013 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
1018 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
1021 * Tunnel ID for the next created tunnel (global tunnel number).
1023 static MESH_TunnelNumber next_tid;
1026 * Tunnel ID for the next incoming tunnel (local tunnel number).
1028 static MESH_TunnelNumber next_local_tid;
1031 * All application types provided by this peer.
1033 static struct GNUNET_CONTAINER_MultiHashMap *applications;
1036 * All message types clients of this peer are interested in.
1038 static struct GNUNET_CONTAINER_MultiHashMap *types;
1041 * Task to periodically announce provided applications.
1043 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
1046 * Task to periodically announce itself in the network.
1048 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
1051 * Next ID to assign to a client.
1053 unsigned int next_client_id;
1056 /******************************************************************************/
1057 /*********************** DECLARATIONS **************************/
1058 /******************************************************************************/
1061 * Function to process paths received for a new peer addition. The recorded
1062 * paths form the initial tunnel, which can be optimized later.
1063 * Called on each result obtained for the DHT search.
1065 * @param cls closure
1066 * @param exp when will this value expire
1067 * @param key key of the result
1068 * @param type type of the result
1069 * @param size number of bytes in data
1070 * @param data pointer to the result data
1073 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1074 const struct GNUNET_HashCode * key,
1075 const struct GNUNET_PeerIdentity *get_path,
1076 unsigned int get_path_length,
1077 const struct GNUNET_PeerIdentity *put_path,
1078 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1079 size_t size, const void *data);
1083 * Function to process DHT string to regex matching.
1084 * Called on each result obtained for the DHT search.
1086 * @param cls closure (search context)
1087 * @param exp when will this value expire
1088 * @param key key of the result
1089 * @param get_path path of the get request (not used)
1090 * @param get_path_length lenght of get_path (not used)
1091 * @param put_path path of the put request (not used)
1092 * @param put_path_length length of the put_path (not used)
1093 * @param type type of the result
1094 * @param size number of bytes in data
1095 * @param data pointer to the result data
1097 * TODO: re-issue the request after certain time? cancel after X results?
1100 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1101 const struct GNUNET_HashCode * key,
1102 const struct GNUNET_PeerIdentity *get_path,
1103 unsigned int get_path_length,
1104 const struct GNUNET_PeerIdentity *put_path,
1105 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
1106 size_t size, const void *data);
1110 * Function to process DHT string to regex matching.
1111 * Called on each result obtained for the DHT search.
1113 * @param cls closure (search context)
1114 * @param exp when will this value expire
1115 * @param key key of the result
1116 * @param get_path path of the get request (not used)
1117 * @param get_path_length lenght of get_path (not used)
1118 * @param put_path path of the put request (not used)
1119 * @param put_path_length length of the put_path (not used)
1120 * @param type type of the result
1121 * @param size number of bytes in data
1122 * @param data pointer to the result data
1125 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1126 const struct GNUNET_HashCode * key,
1127 const struct GNUNET_PeerIdentity *get_path,
1128 unsigned int get_path_length,
1129 const struct GNUNET_PeerIdentity *put_path,
1130 unsigned int put_path_length,
1131 enum GNUNET_BLOCK_Type type,
1132 size_t size, const void *data);
1136 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1137 * and insert it in the appropiate structures if the peer is not known yet.
1139 * @param peer Short identity of the peer.
1141 * @return Existing or newly created peer info.
1143 static struct MeshPeerInfo *
1144 peer_info_get_short (const GNUNET_PEER_Id peer);
1148 * Try to establish a new connection to this peer.
1149 * Use the best path for the given tunnel.
1150 * If the peer doesn't have any path to it yet, try to get one.
1151 * If the peer already has some path, send a CREATE PATH towards it.
1153 * @param peer PeerInfo of the peer.
1154 * @param t Tunnel for which to create the path, if possible.
1157 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1161 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1162 * needed rescources.
1163 * If peer already exists, reevaluate shortest path and change if different.
1165 * @param t Tunnel we want to add a new peer to
1166 * @param peer PeerInfo of the peer being added
1170 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1174 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1175 * that are no longer needed, as well as nodes of no longer reachable peers.
1176 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1178 * @param t Tunnel from which to remove the path.
1179 * @param peer Short id of the peer which should be removed.
1182 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1186 * Search for a tunnel by global ID using full PeerIdentities.
1188 * @param oid owner of the tunnel.
1189 * @param tid global tunnel number.
1191 * @return tunnel handler, NULL if doesn't exist.
1193 static struct MeshTunnel *
1194 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1198 * Delete an active client from the tunnel.
1204 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1207 * Notify a tunnel that a connection has broken that affects at least
1208 * some of its peers.
1210 * @param t Tunnel affected.
1211 * @param p1 Peer that got disconnected from p2.
1212 * @param p2 Peer that got disconnected from p1.
1214 * @return Short ID of the peer disconnected (either p1 or p2).
1215 * 0 if the tunnel remained unaffected.
1217 static GNUNET_PEER_Id
1218 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1223 * Get the current ack value for a tunnel, for data going from root to leaves,
1224 * taking in account the tunnel mode and the status of all children and clients.
1228 * @return Maximum PID allowed.
1231 tunnel_get_fwd_ack (struct MeshTunnel *t);
1235 * Add a client to a tunnel, initializing all needed data structures.
1237 * @param t Tunnel to which add the client.
1238 * @param c Client which to add to the tunnel.
1241 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1245 * Jump to the next edge, with the longest matching token.
1247 * @param block Block found in the DHT.
1248 * @param size Size of the block.
1249 * @param ctx Context of the search.
1251 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1254 regex_next_edge (const struct MeshRegexBlock *block,
1256 struct MeshRegexSearchContext *ctx);
1260 * Find a path to a peer that offers a regex servcie compatible
1261 * with a given string.
1263 * @param key The key of the accepting state.
1264 * @param ctx Context containing info about the string, tunnel, etc.
1267 regex_find_path (const struct GNUNET_HashCode *key,
1268 struct MeshRegexSearchContext *ctx);
1272 * @brief Queue and pass message to core when possible.
1274 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1275 * and accounts for it. In case the queue is full, the message is dropped and
1278 * Otherwise, message is treated as internal and allowed to go regardless of
1281 * @param cls Closure (@c type dependant). It will be used by queue_send to
1282 * build the message to be sent if not already prebuilt.
1283 * @param type Type of the message, 0 for a raw message.
1284 * @param size Size of the message.
1285 * @param dst Neighbor to send message to.
1286 * @param t Tunnel this message belongs to.
1289 queue_add (void *cls, uint16_t type, size_t size,
1290 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1294 * Free a transmission that was already queued with all resources
1295 * associated to the request.
1297 * @param queue Queue handler to cancel.
1298 * @param clear_cls Is it necessary to free associated cls?
1301 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1305 * @brief Get the next transmittable message from the queue.
1307 * This will be the head, except in the case of being a data packet
1308 * not allowed by the destination peer.
1310 * @param peer Destination peer.
1312 * @return The next viable MeshPeerQueue element to send to that peer.
1313 * NULL when there are no transmittable messages.
1315 struct MeshPeerQueue *
1316 queue_get_next (const struct MeshPeerInfo *peer);
1320 * Core callback to write a queued packet to core buffer
1322 * @param cls Closure (peer info).
1323 * @param size Number of bytes available in buf.
1324 * @param buf Where the to write the message.
1326 * @return number of bytes written to buf
1329 queue_send (void *cls, size_t size, void *buf);
1331 /******************************************************************************/
1332 /************************ ITERATORS ****************************/
1333 /******************************************************************************/
1336 * Iterator over found existing mesh regex blocks that match an ongoing search.
1338 * @param cls closure
1339 * @param key current key code
1340 * @param value value in the hash map
1341 * @return GNUNET_YES if we should continue to iterate,
1345 regex_result_iterator (void *cls,
1346 const struct GNUNET_HashCode * key,
1349 struct MeshRegexBlock *block = value;
1350 struct MeshRegexSearchContext *ctx = cls;
1352 if (GNUNET_YES == ntohl(block->accepting) &&
1353 ctx->position == strlen (ctx->info->description))
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1356 regex_find_path (key, ctx);
1357 return GNUNET_YES; // We found an accept state!
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1362 ctx->position, strlen(ctx->info->description),
1363 ntohl(block->accepting));
1366 regex_next_edge(block, SIZE_MAX, ctx);
1368 GNUNET_STATISTICS_update (stats, "# regex mesh blocks iterated", 1, GNUNET_NO);
1375 * Iterator over edges in a regex block retrieved from the DHT.
1377 * @param cls Closure (context of the search).
1378 * @param token Token that follows to next state.
1379 * @param len Lenght of token.
1380 * @param key Hash of next state.
1382 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1385 regex_edge_iterator (void *cls,
1388 const struct GNUNET_HashCode *key)
1390 struct MeshRegexSearchContext *ctx = cls;
1391 struct MeshRegexSearchInfo *info = ctx->info;
1395 GNUNET_STATISTICS_update (stats, "# regex edges iterated", 1, GNUNET_NO);
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1400 current = &info->description[ctx->position];
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1402 current_len = strlen (info->description) - ctx->position;
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* token : %.*s\n", len, token);
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1407 if (len > current_len)
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1410 return GNUNET_YES; // Token too long, wont match
1412 if (0 != strncmp (current, token, len))
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1415 return GNUNET_YES; // Token doesn't match
1418 if (len > ctx->longest_match)
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is longer, KEEP\n");
1421 ctx->longest_match = len;
1426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token is not longer, IGNORE\n");
1429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1435 * Jump to the next edge, with the longest matching token.
1437 * @param block Block found in the DHT.
1438 * @param size Size of the block.
1439 * @param ctx Context of the search.
1441 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1444 regex_next_edge (const struct MeshRegexBlock *block,
1446 struct MeshRegexSearchContext *ctx)
1448 struct MeshRegexSearchContext *new_ctx;
1449 struct MeshRegexSearchInfo *info = ctx->info;
1450 struct GNUNET_DHT_GetHandle *get_h;
1454 /* Find the longest match for the current string position,
1455 * among tokens in the given block */
1456 ctx->longest_match = 0;
1457 result = GNUNET_MESH_regex_block_iterate (block, size,
1458 ®ex_edge_iterator, ctx);
1459 GNUNET_break (GNUNET_OK == result);
1461 /* Did anything match? */
1462 if (0 == ctx->longest_match)
1465 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1466 new_ctx->info = info;
1467 new_ctx->position = ctx->position + ctx->longest_match;
1468 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1470 /* Check whether we already have a DHT GET running for it */
1472 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, &ctx->hash))
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1475 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results,
1477 ®ex_result_iterator,
1479 // FIXME: "leaks" new_ctx? avoid keeping it around?
1480 return; // We are already looking for it
1483 GNUNET_STATISTICS_update (stats, "# regex nodes traversed", 1, GNUNET_NO);
1485 /* Start search in DHT */
1486 rest = &new_ctx->info->description[new_ctx->position];
1488 GNUNET_DHT_get_start (dht_handle, /* handle */
1489 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1490 &ctx->hash, /* key to search */
1491 dht_replication_level, /* replication level */
1492 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1494 // FIXME add BLOOMFILTER to exclude filtered peers
1495 strlen(rest) + 1, /* xquery bits */
1496 // FIXME add BLOOMFILTER SIZE
1497 &dht_get_string_handler, new_ctx);
1499 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
1502 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1511 * Iterator over hash map entries to cancel DHT GET requests after a
1512 * successful connect_by_string.
1514 * @param cls Closure (unused).
1515 * @param key Current key code (unused).
1516 * @param value Value in the hash map (get handle).
1517 * @return GNUNET_YES if we should continue to iterate,
1521 regex_cancel_dht_get (void *cls,
1522 const struct GNUNET_HashCode * key,
1525 struct GNUNET_DHT_GetHandle *h = value;
1527 GNUNET_DHT_get_stop (h);
1533 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1534 * search for connect_by_string.
1536 * @param cls Closure (unused).
1537 * @param key Current key code (unused).
1538 * @param value MeshRegexBlock in the hash map.
1539 * @return GNUNET_YES if we should continue to iterate,
1543 regex_free_result (void *cls,
1544 const struct GNUNET_HashCode * key,
1548 GNUNET_free (value);
1554 * Find a path to a peer that offers a regex servcie compatible
1555 * with a given string.
1557 * @param key The key of the accepting state.
1558 * @param ctx Context containing info about the string, tunnel, etc.
1561 regex_find_path (const struct GNUNET_HashCode *key,
1562 struct MeshRegexSearchContext *ctx)
1564 struct GNUNET_DHT_GetHandle *get_h;
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1567 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1568 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1569 key, /* key to search */
1570 dht_replication_level, /* replication level */
1571 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE/* |
1572 GNUNET_DHT_RO_RECORD_ROUTE*/,
1574 // FIXME add BLOOMFILTER to exclude filtered peers
1575 0, /* xquery bits */
1576 // FIXME add BLOOMFILTER SIZE
1577 &dht_get_string_accept_handler, ctx);
1578 GNUNET_break (GNUNET_OK ==
1579 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1582 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1587 * Function called if the connect attempt to a peer found via
1588 * connect_by_string times out. Try to connect to another peer, if any.
1589 * Otherwise try to reconnect to the same peer.
1591 * @param cls Closure (info about regex search).
1592 * @param tc TaskContext.
1595 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1597 struct MeshRegexSearchInfo *info = cls;
1598 struct MeshPeerInfo *peer_info;
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1603 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1604 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1612 if (0 < info->n_peers)
1614 // Select next peer, put current in that spot.
1615 id = info->peers[info->i_peer];
1616 info->peers[info->i_peer] = info->peer;
1617 info->i_peer = (info->i_peer + 1) % info->n_peers;
1621 // Try to connect to same peer again.
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1626 peer_info = peer_info_get_short(id);
1627 tunnel_add_peer (info->t, peer_info);
1629 tunnel_delete_peer (info->t, old);
1630 peer_info_connect (peer_info, info->t);
1631 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1632 ®ex_connect_timeout,
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1639 * Cancel an ongoing regex search in the DHT and free all resources.
1641 * @param ctx The search context.
1644 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1646 struct MeshRegexSearchInfo *info = ctx->info;
1649 GNUNET_free (info->description);
1650 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1651 ®ex_cancel_dht_get, NULL);
1652 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1653 ®ex_free_result, NULL);
1654 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1655 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1656 info->t->regex_ctx = NULL;
1657 for (i = 0; i < info->n_contexts; i++)
1659 GNUNET_free (info->contexts[i]);
1661 if (0 < info->n_contexts)
1662 GNUNET_free (info->contexts);
1663 if (0 < info->n_peers)
1664 GNUNET_free (info->peers);
1665 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1667 GNUNET_SCHEDULER_cancel(info->timeout);
1673 /******************************************************************************/
1674 /************************ PERIODIC FUNCTIONS ****************************/
1675 /******************************************************************************/
1678 * Announce iterator over for each application provided by the peer
1680 * @param cls closure
1681 * @param key current key code
1682 * @param value value in the hash map
1683 * @return GNUNET_YES if we should continue to
1688 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1690 struct PBlock block;
1691 struct MeshClient *c;
1693 block.id = my_full_id;
1694 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1695 GNUNET_assert(NULL != c);
1696 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1697 if (0 == block.type)
1702 block.type = htonl (block.type);
1704 GNUNET_break (NULL !=
1705 GNUNET_DHT_put (dht_handle, key,
1706 dht_replication_level,
1707 GNUNET_DHT_RO_RECORD_ROUTE |
1708 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1709 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1711 (const char *) &block,
1712 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1713 app_announce_time, NULL, NULL));
1720 * Function called when the DHT regex put is complete.
1722 * @param the 'struct MeshClient' for which we were PUTting
1723 * @param success GNUNET_OK if the PUT was transmitted,
1724 * GNUNET_NO on timeout,
1725 * GNUNET_SYSERR on disconnect from service
1726 * after the PUT message was transmitted
1727 * (so we don't know if it was received or not)
1730 announce_regex_done (void *cls,
1733 struct MeshClient *c = cls;
1740 * Regex callback iterator to store own service description in the DHT.
1742 * @param cls closure.
1743 * @param key hash for current state.
1744 * @param proof proof for current state.
1745 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1746 * @param num_edges number of edges leaving current state.
1747 * @param edges edges leaving current state.
1750 regex_iterator (void *cls,
1751 const struct GNUNET_HashCode *key,
1754 unsigned int num_edges,
1755 const struct GNUNET_REGEX_Edge *edges)
1757 struct MeshRegexBlock *block;
1758 struct MeshRegexEdge *block_edge;
1759 enum GNUNET_DHT_RouteOption opt;
1763 unsigned int offset;
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 " regex dht put for state %s\n",
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1777 if (GNUNET_YES == accepting)
1779 struct MeshRegexAccept block;
1781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1782 " state %s is accepting, putting own id\n",
1784 size = sizeof (block);
1786 block.id = my_full_id;
1787 GNUNET_STATISTICS_update (stats, "# regex accepting blocks stored",
1789 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes stored",
1790 sizeof (block), GNUNET_NO);
1792 GNUNET_DHT_put(dht_handle, key,
1793 dht_replication_level,
1794 opt/* | GNUNET_DHT_RO_RECORD_ROUTE*/,
1795 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1798 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: expiration time should be option */
1802 len = strlen(proof);
1803 size = sizeof (struct MeshRegexBlock) + len;
1804 block = GNUNET_malloc (size);
1807 block->n_proof = htonl (len);
1808 block->n_edges = htonl (num_edges);
1809 block->accepting = htonl (accepting);
1811 /* Store the proof at the end of the block. */
1812 aux = (char *) &block[1];
1813 memcpy (aux, proof, len);
1816 /* Store each edge in a variable length MeshEdge struct at the
1817 * very end of the MeshRegexBlock structure.
1819 for (i = 0; i < num_edges; i++)
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 " edge %s towards %s\n",
1824 GNUNET_h2s(&edges[i].destination));
1826 /* aux points at the end of the last block */
1827 len = strlen (edges[i].label);
1828 size += sizeof (struct MeshRegexEdge) + len;
1829 // Calculate offset FIXME is this ok? use size instead?
1830 offset = aux - (char *) block;
1831 block = GNUNET_realloc (block, size);
1832 aux = &((char *) block)[offset];
1833 block_edge = (struct MeshRegexEdge *) aux;
1834 block_edge->key = edges[i].destination;
1835 block_edge->n_token = htonl (len);
1836 aux = (char *) &block_edge[1];
1837 memcpy (aux, edges[i].label, len);
1841 GNUNET_DHT_put(dht_handle, key,
1842 dht_replication_level,
1844 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1846 GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS), /* FIXME: this should be an option */
1849 GNUNET_STATISTICS_update (stats, "# regex blocks stored",
1851 GNUNET_STATISTICS_update (stats, "# regex block bytes stored",
1854 GNUNET_free (block);
1859 * Store the regular expression describing a local service into the DHT.
1861 * @param regex The regular expresion.
1864 regex_put (struct MeshRegexDescriptor *regex)
1866 if (NULL == regex->dfa)
1868 regex->dfa = GNUNET_REGEX_construct_dfa (regex->regex,
1869 strlen (regex->regex),
1870 regex->compression);
1873 DEBUG_DHT (" regex_put (%s) start\n", regex->regex);
1874 GNUNET_REGEX_iterate_all_edges (regex->dfa, ®ex_iterator, NULL);
1875 DEBUG_DHT (" regex_put (%s) end\n", regex->regex);
1880 * Periodically announce what applications are provided by local clients
1883 * @param cls closure
1884 * @param tc task context
1887 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1889 struct MeshClient *c = cls;
1892 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1893 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1895 DEBUG_DHT ("Starting PUT for regex\n");
1896 for (i = 0; i < c->n_regex; i++)
1897 regex_put (&c->regexes[i]);
1898 c->regex_announce_task = GNUNET_SCHEDULER_add_delayed (app_announce_time,
1901 DEBUG_DHT ("Finished PUT for regex\n");
1906 * Periodically announce what applications are provided by local clients
1909 * @param cls closure
1910 * @param tc task context
1913 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1915 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1917 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1921 DEBUG_DHT ("Starting PUT for apps\n");
1923 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1925 announce_applications_task =
1926 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1928 DEBUG_DHT ("Finished PUT for apps\n");
1933 * Periodically announce self id in the DHT
1935 * @param cls closure
1936 * @param tc task context
1939 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1941 struct PBlock block;
1943 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1945 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1949 * - Set data expiration in function of X
1950 * - Adapt X to churn
1952 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1954 block.id = my_full_id;
1955 block.type = htonl (0);
1956 GNUNET_DHT_put (dht_handle, /* DHT handle */
1957 &my_full_id.hashPubKey, /* Key to use */
1958 dht_replication_level, /* Replication level */
1959 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1960 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1961 sizeof (block), /* Size of the data */
1962 (const char *) &block, /* Data itself */
1963 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1964 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1965 NULL, /* Continuation */
1966 NULL); /* Continuation closure */
1968 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1972 /******************************************************************************/
1973 /****************** GENERAL HELPER FUNCTIONS ************************/
1974 /******************************************************************************/
1977 * Decrements the reference counter and frees all resources if needed
1979 * @param mesh_data Data Descriptor used in a multicast message.
1980 * Freed no longer needed (last message).
1983 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1985 if (0 == --(mesh_data->reference_counter))
1987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1988 GNUNET_free (mesh_data->data);
1989 GNUNET_free (mesh_data);
1995 * Check if client has registered with the service and has not disconnected
1997 * @param client the client to check
1999 * @return non-NULL if client exists in the global DLL
2001 static struct MeshClient *
2002 client_get (struct GNUNET_SERVER_Client *client)
2004 struct MeshClient *c;
2009 if (c->handle == client)
2018 * Checks if a given client has subscribed to certain message type
2020 * @param message_type Type of message to check
2021 * @param c Client to check
2023 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
2025 * FIXME: use of crypto_hash slows it down
2026 * The hash function alone takes 8-10us out of the ~55us for the whole
2027 * process of retransmitting the message from one local client to another.
2028 * Find faster implementation!
2031 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
2033 struct GNUNET_HashCode hc;
2035 if (NULL == c->types)
2038 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
2039 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
2044 * Check whether client wants traffic from a tunnel.
2046 * @param c Client to check.
2047 * @param t Tunnel to be found.
2049 * @return GNUNET_YES if client knows tunnel.
2051 * TODO look in client hashmap
2054 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2058 for (i = 0; i < t->nclients; i++)
2059 if (t->clients[i] == c)
2066 * Check whether client has been informed about a tunnel.
2068 * @param c Client to check.
2069 * @param t Tunnel to be found.
2071 * @return GNUNET_YES if client knows tunnel.
2073 * TODO look in client hashmap
2076 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2080 for (i = 0; i < t->nignore; i++)
2081 if (t->ignore[i] == c)
2083 return client_wants_tunnel(c, t);
2088 * Marks a client as uninterested in traffic from the tunnel, updating both
2089 * client and tunnel to reflect this.
2091 * @param c Client that doesn't want traffic anymore.
2092 * @param t Tunnel which should be ignored.
2094 * FIXME when to delete an incoming tunnel?
2097 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2099 struct GNUNET_HashCode hash;
2101 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2102 GNUNET_break (GNUNET_YES ==
2103 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2105 GNUNET_break (GNUNET_YES ==
2106 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
2107 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2108 tunnel_delete_active_client (t, c);
2109 GNUNET_array_append (t->ignore, t->nignore, c);
2114 * Deletes a tunnel from a client (either owner or destination). To be used on
2115 * tunnel destroy, otherwise, use client_ignore_tunnel.
2117 * @param c Client whose tunnel to delete.
2118 * @param t Tunnel which should be deleted.
2121 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
2123 struct GNUNET_HashCode hash;
2127 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
2128 GNUNET_assert (GNUNET_YES ==
2129 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
2135 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
2137 GNUNET_assert (GNUNET_YES ==
2138 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
2142 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
2150 * Send the message to all clients that have subscribed to its type
2152 * @param msg Pointer to the message itself
2153 * @param payload Pointer to the payload of the message.
2154 * @param t The tunnel to whose clients this message goes.
2156 * @return number of clients this message was sent to
2159 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
2160 const struct GNUNET_MessageHeader *payload,
2161 struct MeshTunnel *t)
2163 struct MeshClient *c;
2164 MESH_TunnelNumber *tid;
2167 char cbuf[htons (msg->size)];
2169 type = ntohs (payload->type);
2170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2172 GNUNET_MESH_DEBUG_M2S (type));
2174 memcpy (cbuf, msg, sizeof (cbuf));
2175 switch (htons (msg->type))
2177 struct GNUNET_MESH_Unicast *uc;
2178 struct GNUNET_MESH_Multicast *mc;
2179 struct GNUNET_MESH_ToOrigin *to;
2181 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2182 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2185 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2186 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2189 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2190 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2198 for (count = 0, c = clients; c != NULL; c = c->next)
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2201 if (client_is_subscribed (type, c))
2203 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2207 *tid = htonl (t->local_tid);
2211 if (GNUNET_NO == client_knows_tunnel (c, t))
2213 /* This client doesn't know the tunnel */
2214 struct GNUNET_MESH_TunnelNotification tmsg;
2215 struct GNUNET_HashCode hash;
2217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2218 tmsg.header.size = htons (sizeof (tmsg));
2219 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2220 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2221 tmsg.tunnel_id = htonl (t->local_tid_dest);
2223 if (GNUNET_YES == t->speed_min)
2224 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
2225 if (GNUNET_YES == t->nobuffer)
2226 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
2227 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2228 &tmsg.header, GNUNET_NO);
2229 tunnel_add_client (t, c);
2230 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2232 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2233 c->incoming_tunnels, &hash, t,
2234 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2236 *tid = htonl (t->local_tid_dest);
2239 /* Check if the client wants to get traffic from the tunnel */
2240 if (GNUNET_NO == client_wants_tunnel(c, t))
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2244 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2245 (struct GNUNET_MessageHeader
2246 *) cbuf, GNUNET_NO);
2255 * Notify the client that owns the tunnel that a peer has connected to it
2256 * (the requested path to it has been confirmed).
2258 * @param t Tunnel whose owner to notify
2259 * @param id Short id of the peer that has connected
2262 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2264 struct GNUNET_MESH_PeerControl pc;
2266 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2267 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2268 pc.tunnel_id = htonl (t->local_tid);
2269 GNUNET_PEER_resolve (id, &pc.peer);
2270 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2276 * Notify all clients (not depending on registration status) that the incoming
2277 * tunnel is no longer valid.
2279 * @param t Tunnel that was destroyed.
2282 send_clients_tunnel_destroy (struct MeshTunnel *t)
2284 struct GNUNET_MESH_TunnelMessage msg;
2286 msg.header.size = htons (sizeof (msg));
2287 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2288 msg.tunnel_id = htonl (t->local_tid_dest);
2289 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2294 * Notify clients of tunnel disconnections, if needed.
2295 * In case the origin disconnects, the destination clients get a tunnel destroy
2296 * notification. If the last destination disconnects (only one remaining client
2297 * in tunnel), the origin gets a (local ID) peer disconnected.
2298 * Note that the function must be called BEFORE removing the client from
2301 * @param t Tunnel that was destroyed.
2302 * @param c Client that disconnected.
2305 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2311 struct GNUNET_MESH_TunnelMessage msg;
2313 msg.header.size = htons (sizeof (msg));
2314 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2315 msg.tunnel_id = htonl (t->local_tid_dest);
2316 for (i = 0; i < t->nclients; i++)
2317 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2318 &msg.header, GNUNET_NO);
2320 // FIXME when to disconnect an incoming tunnel?
2321 else if (1 == t->nclients && NULL != t->owner)
2323 struct GNUNET_MESH_PeerControl msg;
2325 msg.header.size = htons (sizeof (msg));
2326 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2327 msg.tunnel_id = htonl (t->local_tid);
2328 msg.peer = my_full_id;
2329 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2330 &msg.header, GNUNET_NO);
2336 * Iterator over all the peers to remove the oldest not-used entry.
2338 * @param cls Closure (unsued).
2339 * @param key ID of the peer.
2340 * @param value Peer_Info of the peer.
2345 peer_info_timeout (void *cls,
2346 const struct GNUNET_HashCode *key,
2353 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2354 * and insert it in the appropiate structures if the peer is not known yet.
2356 * @param peer Full identity of the peer.
2358 * @return Existing or newly created peer info.
2360 static struct MeshPeerInfo *
2361 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2363 struct MeshPeerInfo *peer_info;
2365 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2366 if (NULL == peer_info)
2369 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2370 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
2372 GNUNET_CONTAINER_multihashmap_iterate (peers,
2376 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2377 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
2378 peer_info->id = GNUNET_PEER_intern (peer);
2380 peer_info->last_contact = GNUNET_TIME_absolute_get();
2387 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2388 * and insert it in the appropiate structures if the peer is not known yet.
2390 * @param peer Short identity of the peer.
2392 * @return Existing or newly created peer info.
2394 static struct MeshPeerInfo *
2395 peer_info_get_short (const GNUNET_PEER_Id peer)
2397 struct GNUNET_PeerIdentity id;
2399 GNUNET_PEER_resolve (peer, &id);
2400 return peer_info_get (&id);
2405 * Iterator to remove the tunnel from the list of tunnels a peer participates
2408 * @param cls Closure (tunnel info)
2409 * @param key GNUNET_PeerIdentity of the peer (unused)
2410 * @param value PeerInfo of the peer
2412 * @return always GNUNET_YES, to keep iterating
2415 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2417 struct MeshTunnel *t = cls;
2418 struct MeshPeerInfo *peer = value;
2421 for (i = 0; i < peer->ntunnels; i++)
2424 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2427 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2428 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2437 * Core callback to write a pre-constructed data packet to core buffer
2439 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2440 * @param size Number of bytes available in buf.
2441 * @param buf Where the to write the message.
2443 * @return number of bytes written to buf
2446 send_core_data_raw (void *cls, size_t size, void *buf)
2448 struct MeshTransmissionDescriptor *info = cls;
2449 struct GNUNET_MessageHeader *msg;
2452 GNUNET_assert (NULL != info);
2453 GNUNET_assert (NULL != info->mesh_data);
2454 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2455 total_size = ntohs (msg->size);
2457 if (total_size > size)
2462 memcpy (buf, msg, total_size);
2463 data_descriptor_decrement_rc (info->mesh_data);
2470 * Sends an already built non-multicast message to a peer,
2471 * properly registrating all used resources.
2473 * @param message Message to send. Function makes a copy of it.
2474 * @param peer Short ID of the neighbor whom to send the message.
2475 * @param t Tunnel on which this message is transmitted.
2478 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
2479 const struct GNUNET_PeerIdentity *peer,
2480 struct MeshTunnel *t)
2482 struct MeshTransmissionDescriptor *info;
2483 struct MeshPeerInfo *neighbor;
2484 struct MeshPeerPath *p;
2488 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2490 size = ntohs (message->size);
2491 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2492 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2493 info->mesh_data->data = GNUNET_malloc (size);
2494 memcpy (info->mesh_data->data, message, size);
2495 type = ntohs(message->type);
2498 struct GNUNET_MESH_Unicast *m;
2499 struct GNUNET_MESH_ToOrigin *to;
2501 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2502 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2503 m->ttl = htonl (ntohl (m->ttl) - 1);
2505 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2506 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
2508 to->pid = htonl(t->bck_pid);
2510 info->mesh_data->data_len = size;
2511 info->mesh_data->reference_counter = 1;
2512 info->mesh_data->total_out = 1;
2513 neighbor = peer_info_get (peer);
2514 for (p = neighbor->path_head; NULL != p; p = p->next)
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 " %s IS NOT DIRECTLY CONNECTED\n",
2527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 for (p = neighbor->path_head; NULL != p; p = p->next)
2532 struct GNUNET_PeerIdentity debug_id;
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2536 " path with %u hops through:\n",
2538 for (i = 0; i < p->length; i++)
2540 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543 i, GNUNET_i2s(&debug_id));
2547 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2549 " no direct connection to %s\n",
2551 GNUNET_free (info->mesh_data->data);
2552 GNUNET_free (info->mesh_data);
2556 info->peer = neighbor;
2557 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2568 * Sends a CREATE PATH message for a path to a peer, properly registrating
2569 * all used resources.
2571 * @param peer PeerInfo of the final peer for whom this path is being created.
2572 * @param p Path itself.
2573 * @param t Tunnel for which the path is created.
2576 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2577 struct MeshTunnel *t)
2579 struct GNUNET_PeerIdentity id;
2580 struct MeshPathInfo *path_info;
2581 struct MeshPeerInfo *neighbor;
2587 p = tree_get_path_to_peer (t->tree, peer->id);
2594 for (i = 0; i < p->length; i++)
2596 if (p->peers[i] == myid)
2599 if (i >= p->length - 1)
2605 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2607 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2608 path_info->path = p;
2610 neighbor = peer_info_get (&id);
2611 path_info->peer = neighbor;
2612 queue_add (path_info,
2613 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2614 sizeof (struct GNUNET_MESH_ManipulatePath) +
2615 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2622 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2624 * @param t Tunnel whose path to destroy.
2625 * @param destination Short ID of the peer to whom the path to destroy.
2628 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2630 struct MeshPeerPath *p;
2633 p = tree_get_path_to_peer (t->tree, destination);
2639 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2640 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2642 struct GNUNET_MESH_ManipulatePath *msg;
2643 struct GNUNET_PeerIdentity *pi;
2647 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2648 msg->header.size = htons (size);
2649 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2650 msg->tid = htonl (t->id.tid);
2651 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2652 for (i = 0; i < p->length; i++)
2654 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2656 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2663 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
2665 * @param t Tunnel which to confirm.
2668 send_path_ack (struct MeshTunnel *t)
2670 struct MeshTransmissionDescriptor *info;
2671 struct GNUNET_PeerIdentity id;
2672 GNUNET_PEER_Id peer;
2674 peer = tree_get_predecessor (t->tree);
2675 GNUNET_PEER_resolve (peer, &id);
2676 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2677 info->origin = &t->id;
2678 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
2679 GNUNET_assert (NULL != info->peer);
2682 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
2683 sizeof (struct GNUNET_MESH_PathACK),
2690 * Try to establish a new connection to this peer.
2691 * Use the best path for the given tunnel.
2692 * If the peer doesn't have any path to it yet, try to get one.
2693 * If the peer already has some path, send a CREATE PATH towards it.
2695 * @param peer PeerInfo of the peer.
2696 * @param t Tunnel for which to create the path, if possible.
2699 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2701 struct MeshPeerPath *p;
2702 struct MeshPathInfo *path_info;
2704 if (NULL != peer->path_head)
2706 p = tree_get_path_to_peer (t->tree, peer->id);
2713 // FIXME always send create path to self
2716 send_create_path (peer, p, t);
2720 struct GNUNET_HashCode hash;
2723 send_client_peer_connected (t, myid);
2724 t->local_tid_dest = next_local_tid++;
2725 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2728 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2736 else if (NULL == peer->dhtget)
2738 struct GNUNET_PeerIdentity id;
2740 GNUNET_PEER_resolve (peer->id, &id);
2741 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2742 path_info->peer = peer;
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2745 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2746 peer->dhtgetcls = path_info;
2747 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2748 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2749 &id.hashPubKey, /* key to search */
2750 dht_replication_level, /* replication level */
2751 GNUNET_DHT_RO_RECORD_ROUTE |
2752 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2753 NULL, /* xquery */ // FIXME BLOOMFILTER
2754 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2755 &dht_get_id_handler, path_info);
2757 /* Otherwise, there is no path but the DHT get is already started. */
2762 * Task to delay the connection of a peer
2764 * @param cls Closure (path info with tunnel and peer to connect).
2765 * Will be free'd on exection.
2766 * @param tc TaskContext
2769 peer_info_connect_task (void *cls,
2770 const struct GNUNET_SCHEDULER_TaskContext *tc)
2772 struct MeshPathInfo *path_info = cls;
2774 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2776 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2781 peer_info_connect (path_info->peer, path_info->t);
2787 * Destroy the peer_info and free any allocated resources linked to it
2789 * @param pi The peer_info to destroy.
2791 * @return GNUNET_OK on success
2794 peer_info_destroy (struct MeshPeerInfo *pi)
2796 struct GNUNET_PeerIdentity id;
2797 struct MeshPeerPath *p;
2798 struct MeshPeerPath *nextp;
2800 GNUNET_PEER_resolve (pi->id, &id);
2801 GNUNET_PEER_change_rc (pi->id, -1);
2804 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2807 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2808 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2810 if (NULL != pi->dhtget)
2812 GNUNET_DHT_get_stop (pi->dhtget);
2813 GNUNET_free (pi->dhtgetcls);
2819 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2823 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2825 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2833 * Remove all paths that rely on a direct connection between p1 and p2
2834 * from the peer itself and notify all tunnels about it.
2836 * @param peer PeerInfo of affected peer.
2837 * @param p1 GNUNET_PEER_Id of one peer.
2838 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2841 * TODO: optimize (see below)
2844 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2847 struct MeshPeerPath *p;
2848 struct MeshPeerPath *aux;
2849 struct MeshPeerInfo *peer_d;
2851 unsigned int destroyed;
2856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2858 p = peer->path_head;
2862 for (i = 0; i < (p->length - 1); i++)
2864 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2865 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2867 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2878 for (i = 0; i < peer->ntunnels; i++)
2880 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2884 * Problem: one or more peers have been deleted from the tunnel tree.
2885 * We don't know who they are to try to add them again.
2886 * We need to try to find a new path for each of the disconnected peers.
2887 * Some of them might already have a path to reach them that does not
2888 * involve p1 and p2. Adding all anew might render in a better tree than
2889 * the trivial immediate fix.
2891 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2892 * its children will be reachable trough him.
2894 peer_d = peer_info_get_short (d);
2897 for (p = peer_d->path_head; NULL != p; p = p->next)
2899 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2907 /* No callback, as peer will be already disconnected and a connection
2908 * scheduled by tunnel_notify_connection_broken.
2910 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2914 peer_info_connect (peer_d, peer->tunnels[i]);
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2922 * Add the path to the peer and update the path used to reach it in case this
2925 * @param peer_info Destination peer to add the path to.
2926 * @param path New path to add. Last peer must be the peer in arg 1.
2927 * Path will be either used of freed if already known.
2928 * @param trusted Do we trust that this path is real?
2931 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2934 struct MeshPeerPath *aux;
2938 if ((NULL == peer_info) || (NULL == path))
2941 path_destroy (path);
2944 if (path->peers[path->length - 1] != peer_info->id)
2947 path_destroy (path);
2950 if (path->length <= 2 && GNUNET_NO == trusted)
2952 /* Only allow CORE to tell us about direct paths */
2953 path_destroy (path);
2956 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2957 for (l = 1; l < path->length; l++)
2959 if (path->peers[l] == myid)
2961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2962 for (l2 = 0; l2 < path->length - l; l2++)
2964 path->peers[l2] = path->peers[l + l2];
2969 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2974 struct GNUNET_PeerIdentity id;
2976 GNUNET_PEER_resolve (peer_info->id, &id);
2977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2978 path->length, GNUNET_i2s (&id));
2981 l = path_get_length (path);
2988 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2989 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2991 l2 = path_get_length (aux);
2994 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2995 peer_info->path_tail, aux, path);
3000 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
3002 path_destroy (path);
3007 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
3014 * Add the path to the origin peer and update the path used to reach it in case
3015 * this is the shortest.
3016 * The path is given in peer_info -> destination, therefore we turn the path
3017 * upside down first.
3019 * @param peer_info Peer to add the path to, being the origin of the path.
3020 * @param path New path to add after being inversed.
3021 * @param trusted Do we trust that this path is real?
3024 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
3025 struct MeshPeerPath *path, int trusted)
3028 peer_info_add_path (peer_info, path, trusted);
3033 * Function called if the connection to the peer has been stalled for a while,
3034 * possibly due to a missed ACK. Poll the peer about its ACK status.
3036 * @param cls Closure (cinfo).
3037 * @param tc TaskContext.
3040 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3042 struct MeshTunnelChildInfo *cinfo = cls;
3043 struct GNUNET_MESH_Poll msg;
3044 struct GNUNET_PeerIdentity id;
3045 struct MeshTunnel *t;
3047 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3048 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3054 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
3055 msg.header.size = htons (sizeof (msg));
3056 msg.tid = htonl (t->id.tid);
3057 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3058 msg.last_ack = htonl (cinfo->fwd_ack);
3060 GNUNET_PEER_resolve (cinfo->id, &id);
3061 send_prebuilt_message (&msg.header, &id, cinfo->t);
3062 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
3064 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
3065 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
3066 &tunnel_poll, cinfo);
3071 * Build a PeerPath from the paths returned from the DHT, reversing the paths
3072 * to obtain a local peer -> destination path and interning the peer ids.
3074 * @return Newly allocated and created path
3076 static struct MeshPeerPath *
3077 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
3078 unsigned int get_path_length,
3079 const struct GNUNET_PeerIdentity *put_path,
3080 unsigned int put_path_length)
3082 struct MeshPeerPath *p;
3088 GNUNET_PEER_change_rc (myid, 1);
3089 i = get_path_length;
3090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
3091 for (i--; i >= 0; i--)
3093 id = GNUNET_PEER_intern (&get_path[i]);
3094 if (p->length > 0 && id == p->peers[p->length - 1])
3096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3097 GNUNET_PEER_change_rc (id, -1);
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
3102 GNUNET_i2s (&get_path[i]));
3104 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3105 p->peers[p->length - 1] = id;
3108 i = put_path_length;
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
3110 for (i--; i >= 0; i--)
3112 id = GNUNET_PEER_intern (&put_path[i]);
3115 /* PUT path went through us, so discard the path up until now and start
3116 * from here to get a much shorter (and loop-free) path.
3121 if (p->length > 0 && id == p->peers[p->length - 1])
3123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
3124 GNUNET_PEER_change_rc (id, -1);
3128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
3129 GNUNET_i2s (&put_path[i]));
3131 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
3132 p->peers[p->length - 1] = id;
3136 if (get_path_length > 0)
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
3138 GNUNET_i2s (&get_path[0]));
3139 if (put_path_length > 0)
3140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
3141 GNUNET_i2s (&put_path[0]));
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
3144 for (i = 0; i < p->length; i++)
3146 struct GNUNET_PeerIdentity peer_id;
3148 GNUNET_PEER_resolve (p->peers[i], &peer_id);
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
3150 GNUNET_i2s (&peer_id));
3158 * Adds a path to the peer_infos of all the peers in the path
3160 * @param p Path to process.
3161 * @param confirmed Whether we know if the path works or not.
3164 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
3168 /* TODO: invert and add */
3169 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
3170 for (i++; i < p->length; i++)
3172 struct MeshPeerInfo *aux;
3173 struct MeshPeerPath *copy;
3175 aux = peer_info_get_short (p->peers[i]);
3176 copy = path_duplicate (p);
3177 copy->length = i + 1;
3178 peer_info_add_path (aux, copy, GNUNET_NO);
3184 * Send keepalive packets for a peer
3186 * @param cls Closure (tunnel for which to send the keepalive).
3187 * @param tc Notification context.
3189 * TODO: implement explicit multicast keepalive?
3192 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
3196 * Search for a tunnel among the incoming tunnels
3198 * @param tid the local id of the tunnel
3200 * @return tunnel handler, NULL if doesn't exist
3202 static struct MeshTunnel *
3203 tunnel_get_incoming (MESH_TunnelNumber tid)
3205 struct GNUNET_HashCode hash;
3207 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
3208 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3209 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
3214 * Search for a tunnel among the tunnels for a client
3216 * @param c the client whose tunnels to search in
3217 * @param tid the local id of the tunnel
3219 * @return tunnel handler, NULL if doesn't exist
3221 static struct MeshTunnel *
3222 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
3224 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
3226 return tunnel_get_incoming (tid);
3230 struct GNUNET_HashCode hash;
3232 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
3233 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
3239 * Search for a tunnel by global ID using PEER_ID
3241 * @param pi owner of the tunnel
3242 * @param tid global tunnel number
3244 * @return tunnel handler, NULL if doesn't exist
3246 static struct MeshTunnel *
3247 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
3249 struct MESH_TunnelID id;
3250 struct GNUNET_HashCode hash;
3255 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
3256 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
3261 * Search for a tunnel by global ID using full PeerIdentities
3263 * @param oid owner of the tunnel
3264 * @param tid global tunnel number
3266 * @return tunnel handler, NULL if doesn't exist
3268 static struct MeshTunnel *
3269 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3271 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3276 * Delete an active client from the tunnel.
3282 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3286 for (i = 0; i < t->nclients; i++)
3288 if (t->clients[i] == c)
3290 t->clients[i] = t->clients[t->nclients - 1];
3291 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3292 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3294 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3302 * Delete an ignored client from the tunnel.
3308 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3312 for (i = 0; i < t->nignore; i++)
3314 if (t->ignore[i] == c)
3316 t->ignore[i] = t->ignore[t->nignore - 1];
3317 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3325 * Delete a client from the tunnel. It should be only done on
3326 * client disconnection, otherwise use client_ignore_tunnel.
3332 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3334 tunnel_delete_ignored_client (t, c);
3335 tunnel_delete_active_client (t, c);
3340 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
3342 * Destroys queue elements of all waiting transmissions and frees all memory
3343 * used by the struct and its elements.
3345 * @param cls Closure (tunnel info).
3346 * @param key Hash of GNUNET_PEER_Id (unused).
3347 * @param value MeshTunnelChildInfo of the child.
3349 * @return always GNUNET_YES, to keep iterating
3352 tunnel_destroy_child (void *cls,
3353 const struct GNUNET_HashCode * key,
3356 struct MeshTunnelChildInfo *cinfo = value;
3357 struct MeshTunnel *t = cls;
3358 struct MeshPeerQueue *q;
3362 for (c = 0; c < cinfo->send_buffer_n; c++)
3364 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3365 q = cinfo->send_buffer[i];
3366 cinfo->send_buffer[i] = NULL;
3368 queue_destroy (q, GNUNET_YES);
3371 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3373 GNUNET_free_non_null (cinfo->send_buffer);
3374 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
3376 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
3377 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3379 GNUNET_free (cinfo);
3385 * Callback used to notify a client owner of a tunnel that a peer has
3386 * disconnected, most likely because of a path change.
3388 * @param cls Closure (tunnel this notification is about).
3389 * @param peer_id Short ID of disconnected peer.
3392 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3394 struct MeshTunnel *t = cls;
3395 struct MeshPeerInfo *peer;
3396 struct MeshPathInfo *path_info;
3398 if (NULL != t->owner && NULL != nc)
3400 struct GNUNET_MESH_PeerControl msg;
3402 msg.header.size = htons (sizeof (msg));
3403 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3404 msg.tunnel_id = htonl (t->local_tid);
3405 GNUNET_PEER_resolve (peer_id, &msg.peer);
3406 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3407 &msg.header, GNUNET_NO);
3409 peer = peer_info_get_short (peer_id);
3410 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3411 path_info->peer = peer;
3413 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
3419 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3420 * needed rescources.
3421 * If peer already exists, reevaluate shortest path and change if different.
3423 * @param t Tunnel we want to add a new peer to
3424 * @param peer PeerInfo of the peer being added
3428 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3430 struct GNUNET_PeerIdentity id;
3431 struct MeshPeerPath *best_p;
3432 struct MeshPeerPath *p;
3433 unsigned int best_cost;
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3437 GNUNET_PEER_resolve (peer->id, &id);
3439 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3442 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3443 GNUNET_assert (GNUNET_OK ==
3444 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3446 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3449 if (NULL != (p = peer->path_head))
3452 best_cost = tree_get_path_cost (t->tree, p);
3455 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3462 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3463 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3464 t->path_refresh_task =
3465 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3469 /* Start a DHT get */
3470 peer_info_connect (peer, t);
3472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3476 * Add a path to a tunnel which we don't own, just to remember the next hop.
3477 * If destination node was already in the tunnel, the first hop information
3478 * will be replaced with the new path.
3480 * @param t Tunnel we want to add a new peer to
3481 * @param p Path to add
3482 * @param own_pos Position of local node in path.
3486 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3487 unsigned int own_pos)
3489 struct GNUNET_PeerIdentity id;
3491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3492 GNUNET_assert (0 != own_pos);
3493 tree_add_path (t->tree, p, NULL, NULL);
3494 if (own_pos < p->length - 1)
3496 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3497 tree_update_first_hops (t->tree, myid, &id);
3499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3503 * Add a client to a tunnel, initializing all needed data structures.
3505 * @param t Tunnel to which add the client.
3506 * @param c Client which to add to the tunnel.
3509 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3511 struct MeshTunnelClientInfo clinfo;
3513 GNUNET_array_append (t->clients, t->nclients, c);
3514 clinfo.fwd_ack = t->fwd_pid + 1;
3515 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3516 clinfo.fwd_pid = t->fwd_pid;
3517 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3519 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3524 * Notifies a tunnel that a connection has broken that affects at least
3525 * some of its peers. Sends a notification towards the root of the tree.
3526 * In case the peer is the owner of the tree, notifies the client that owns
3527 * the tunnel and tries to reconnect.
3529 * @param t Tunnel affected.
3530 * @param p1 Peer that got disconnected from p2.
3531 * @param p2 Peer that got disconnected from p1.
3533 * @return Short ID of the peer disconnected (either p1 or p2).
3534 * 0 if the tunnel remained unaffected.
3536 static GNUNET_PEER_Id
3537 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3543 tree_notify_connection_broken (t->tree, p1, p2,
3544 &tunnel_notify_client_peer_disconnected,
3546 if (myid != p1 && myid != p2)
3552 if (tree_get_predecessor (t->tree) != 0)
3554 /* We are the peer still connected, notify owner of the disconnection. */
3555 struct GNUNET_MESH_PathBroken msg;
3556 struct GNUNET_PeerIdentity neighbor;
3558 msg.header.size = htons (sizeof (msg));
3559 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3560 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3561 msg.tid = htonl (t->id.tid);
3562 msg.peer1 = my_full_id;
3563 GNUNET_PEER_resolve (pid, &msg.peer2);
3564 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3565 send_prebuilt_message (&msg.header, &neighbor, t);
3573 * Send a multicast packet to a neighbor.
3575 * @param cls Closure (Info about the multicast packet)
3576 * @param neighbor_id Short ID of the neighbor to send the packet to.
3579 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3581 struct MeshData *mdata = cls;
3582 struct MeshTransmissionDescriptor *info;
3583 struct GNUNET_PeerIdentity neighbor;
3584 struct GNUNET_MessageHeader *msg;
3586 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3588 info->mesh_data = mdata;
3589 (mdata->reference_counter) ++;
3590 info->destination = neighbor_id;
3591 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3593 GNUNET_i2s (&neighbor));
3594 info->peer = peer_info_get (&neighbor);
3595 GNUNET_assert (NULL != info->peer);
3596 msg = (struct GNUNET_MessageHeader *) mdata->data;
3599 info->mesh_data->data_len,
3606 * Queue a message in a tunnel in multicast, sending a copy to each child node
3607 * down the local one in the tunnel tree.
3609 * @param t Tunnel in which to send the data.
3610 * @param msg Message to be sent.
3613 tunnel_send_multicast (struct MeshTunnel *t,
3614 const struct GNUNET_MessageHeader *msg)
3616 struct MeshData *mdata;
3618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3619 " sending a multicast packet...\n");
3621 mdata = GNUNET_malloc (sizeof (struct MeshData));
3622 mdata->data_len = ntohs (msg->size);
3624 mdata->data = GNUNET_malloc (mdata->data_len);
3625 memcpy (mdata->data, msg, mdata->data_len);
3626 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3628 struct GNUNET_MESH_Multicast *mcast;
3630 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3631 if (t->fwd_queue_n >= t->fwd_queue_max)
3634 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3636 " message from %s!\n",
3637 GNUNET_i2s(&mcast->oid));
3638 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3639 " message at %s!\n",
3640 GNUNET_i2s(&my_full_id));
3641 GNUNET_free (mdata->data);
3642 GNUNET_free (mdata);
3646 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3648 ntohl (mcast->ttl));
3652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3655 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3656 if (mdata->reference_counter == 0)
3658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3659 " no one to send data to\n");
3660 GNUNET_free (mdata->data);
3661 GNUNET_free (mdata);
3662 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3667 mdata->total_out = mdata->reference_counter;
3669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3670 " sending a multicast packet done\n");
3676 * Increase the SKIP value of all peers that
3677 * have not received a unicast message.
3679 * @param cls Closure (ID of the peer that HAS received the message).
3680 * @param key ID of the neighbor.
3681 * @param value Information about the neighbor.
3683 * @return GNUNET_YES to keep iterating.
3686 tunnel_add_skip (void *cls,
3687 const struct GNUNET_HashCode * key,
3690 struct GNUNET_PeerIdentity *neighbor = cls;
3691 struct MeshTunnelChildInfo *cinfo = value;
3693 /* TODO compare only pointers? key == neighbor? */
3694 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3704 * @brief Get neighbor's Flow Control information.
3706 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3707 * descendant of the local node in a certain tunnel.
3708 * If the info is not yet there (recently created path), creates the data struct
3709 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3712 * @param t Tunnel related.
3713 * @param peer Neighbor whose Flow Control info is needed.
3715 * @return Neighbor's Flow Control info.
3717 static struct MeshTunnelChildInfo *
3718 tunnel_get_neighbor_fc (struct MeshTunnel *t,
3719 const struct GNUNET_PeerIdentity *peer)
3721 struct MeshTunnelChildInfo *cinfo;
3723 if (NULL == t->children_fc)
3726 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3732 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3733 cinfo->id = GNUNET_PEER_intern (peer);
3734 cinfo->skip = t->fwd_pid;
3737 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE;
3738 cinfo->fwd_ack = t->fwd_pid + delta;
3739 cinfo->bck_ack = delta;
3740 cinfo->bck_pid = -1;
3742 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
3743 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
3745 cinfo->send_buffer =
3746 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3748 GNUNET_assert (GNUNET_OK ==
3749 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3752 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3759 * Get the Flow Control info of a client.
3761 * @param t Tunnel on which to look.
3762 * @param c Client whose ACK to get.
3764 * @return ACK value.
3766 static struct MeshTunnelClientInfo *
3767 tunnel_get_client_fc (struct MeshTunnel *t,
3768 struct MeshClient *c)
3772 for (i = 0; i < t->nclients; i++)
3774 if (t->clients[i] != c)
3776 return &t->clients_fc[i];
3779 return NULL; // avoid compiler / coverity complaints
3784 * Iterator to get the appropiate ACK value from all children nodes.
3786 * @param cls Closue (tunnel).
3787 * @param id Id of the child node.
3790 tunnel_get_child_fwd_ack (void *cls,
3793 struct GNUNET_PeerIdentity peer_id;
3794 struct MeshTunnelChildInfo *cinfo;
3795 struct MeshTunnelChildIteratorContext *ctx = cls;
3796 struct MeshTunnel *t = ctx->t;
3799 GNUNET_PEER_resolve (id, &peer_id);
3800 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3801 ack = cinfo->fwd_ack;
3804 if (GNUNET_NO == ctx->init)
3806 ctx->max_child_ack = ack;
3807 ctx->init = GNUNET_YES;
3810 if (GNUNET_YES == t->speed_min)
3812 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3816 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3823 * Get the maximum PID allowed to transmit to any
3824 * tunnel child of the local peer, depending on the tunnel
3825 * buffering/speed settings.
3829 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3832 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3834 struct MeshTunnelChildIteratorContext ctx;
3836 ctx.max_child_ack = 0;
3838 ctx.init = GNUNET_NO;
3839 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3841 if (0 == ctx.nchildren)
3843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3844 " tunnel has no children, no FWD ACK\n");
3848 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3849 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3851 return (int64_t) ctx.max_child_ack;
3856 * Set the FWD ACK value of a client in a particular tunnel.
3858 * @param t Tunnel affected.
3859 * @param c Client whose ACK to set.
3860 * @param ack ACK value.
3863 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3864 struct MeshClient *c,
3869 for (i = 0; i < t->nclients; i++)
3871 if (t->clients[i] != c)
3873 t->clients_fc[i].fwd_ack = ack;
3881 * Get the highest ACK value of all clients in a particular tunnel,
3882 * according to the buffering/speed settings.
3884 * @param t Tunnel on which to look.
3886 * @return Corresponding ACK value (max uint32_t).
3887 * If no clients are suscribed, -1LL.
3890 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3895 if (0 == t->nclients)
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 " tunnel has no clients, no FWD ACK\n");
3902 for (ack = -1LL, i = 0; i < t->nclients; i++)
3905 (GNUNET_YES == t->speed_min &&
3906 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3907 (GNUNET_NO == t->speed_min &&
3908 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3910 ack = t->clients_fc[i].fwd_ack;
3914 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3915 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3917 return (uint32_t) ack;
3922 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3923 * mode and the status of all children nodes.
3927 * @return Maximum PID allowed.
3930 tunnel_get_fwd_ack (struct MeshTunnel *t)
3934 uint32_t buffer_free;
3938 count = t->fwd_pid - t->skip;
3939 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3940 child_ack = tunnel_get_children_fwd_ack (t);
3941 client_ack = tunnel_get_clients_fwd_ack (t);
3942 if (GNUNET_YES == t->nobuffer)
3945 if (-1LL == child_ack)
3946 child_ack = client_ack;
3947 if (-1LL == child_ack)
3950 client_ack = child_ack = ack;
3955 ack = count + buffer_free; // Overflow? OK!
3957 if (-1LL == child_ack)
3959 // Node has no children, child_ack AND core buffer are irrelevant.
3960 if (-1LL == client_ack) // No children AND no clients? Not good!
3962 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3966 return (uint32_t) client_ack;
3968 if (-1LL == client_ack)
3972 if (GNUNET_YES == t->speed_min)
3974 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3975 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3979 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3980 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3983 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3984 count, buffer_free, child_ack, client_ack, ack);
3990 * Build a local ACK message and send it to a local client.
3992 * @param t Tunnel on which to send the ACK.
3993 * @param c Client to whom send the ACK.
3994 * @param ack Value of the ACK.
3997 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3999 struct GNUNET_MESH_LocalAck msg;
4001 msg.header.size = htons (sizeof (msg));
4002 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
4003 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
4004 msg.max_pid = htonl (ack);
4005 GNUNET_SERVER_notification_context_unicast(nc,
4012 * Build an ACK message and queue it to send to the given peer.
4014 * @param t Tunnel on which to send the ACK.
4015 * @param peer Peer to whom send the ACK.
4016 * @param ack Value of the ACK.
4019 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
4021 struct GNUNET_MESH_ACK msg;
4023 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4024 msg.header.size = htons (sizeof (msg));
4025 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
4026 msg.pid = htonl (ack);
4027 msg.tid = htonl (t->id.tid);
4029 send_prebuilt_message (&msg.header, peer, t);
4034 * Notify a the owner of a tunnel about how many more
4035 * payload packages will we accept on a given tunnel.
4037 * @param t Tunnel on which to send the ACK.
4040 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4045 "Sending client FWD ACK on tunnel %X\n",
4048 ack = tunnel_get_fwd_ack (t);
4050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
4051 if (t->last_fwd_ack == ack)
4053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
4057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
4058 t->last_fwd_ack = ack;
4059 send_local_ack (t, t->owner, ack);
4064 * Send an ACK informing the predecessor about the available buffer space.
4065 * In case there is no predecessor, inform the owning client.
4066 * If buffering is off, send only on behalf of children or self if endpoint.
4067 * If buffering is on, send when sent to children and buffer space is free.
4068 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
4069 * the ACK itself goes "back" (towards root).
4071 * @param t Tunnel on which to send the ACK.
4072 * @param type Type of message that triggered the ACK transmission.
4075 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
4077 struct GNUNET_PeerIdentity id;
4080 if (NULL != t->owner)
4082 tunnel_send_client_fwd_ack (t);
4085 /* Is it after unicast / multicast retransmission? */
4088 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4089 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4091 "ACK due to FWD DATA retransmission\n");
4092 if (GNUNET_YES == t->nobuffer)
4094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
4098 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4099 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4101 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4102 t->force_ack = GNUNET_YES;
4108 /* Check if we need to transmit the ACK */
4109 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
4110 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
4111 GNUNET_NO == t->force_ack)
4113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
4114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4115 " t->qmax: %u, t->qn: %u\n",
4116 t->fwd_queue_max, t->fwd_queue_n);
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4118 " t->pid: %u, t->ack: %u\n",
4119 t->fwd_pid, t->last_fwd_ack);
4123 /* Ok, ACK might be necessary, what PID to ACK? */
4124 ack = tunnel_get_fwd_ack (t);
4126 /* If speed_min and not all children have ack'd, dont send yet */
4127 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
4129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
4133 t->last_fwd_ack = ack;
4134 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
4135 send_ack (t, &id, ack);
4137 t->force_ack = GNUNET_NO;
4142 * Iterator to send a child node a BCK ACK to allow him to send more
4145 * @param cls Closure (tunnel).
4146 * @param id Id of the child node.
4149 tunnel_send_child_bck_ack (void *cls,
4152 struct MeshTunnel *t = cls;
4153 struct MeshTunnelChildInfo *cinfo;
4154 struct GNUNET_PeerIdentity peer;
4157 GNUNET_PEER_resolve (id, &peer);
4158 cinfo = tunnel_get_neighbor_fc (t, &peer);
4159 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
4161 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
4163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4164 " Not sending ACK, not needed\n");
4167 cinfo->bck_ack = ack;
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4170 " Sending BCK ACK %u (last sent: %u)\n",
4171 ack, cinfo->bck_ack);
4172 send_ack (t, &peer, ack);
4177 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
4179 * Iterates over all clients and sends BCK ACKs to the ones that need it.
4181 * FIXME fc: what happens if we have 2 clients but q_size is 1?
4182 * - implement a size 1 buffer in each client_fc AND children_fc
4183 * to hold at least 1 message per "child".
4184 * problem: violates no buffer policy
4185 * - ack 0 and make "children" poll for transmission slots
4186 * problem: big overhead, extra latency even in low traffic
4189 * @param t Tunnel on which to send the BCK ACKs.
4192 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
4195 unsigned int tunnel_delta;
4197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
4199 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
4200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
4202 /* Find client whom to allow to send to origin (with lowest buffer space) */
4203 for (i = 0; i < t->nclients; i++)
4205 struct MeshTunnelClientInfo *clinfo;
4208 clinfo = &t->clients_fc[i];
4209 delta = clinfo->bck_ack - clinfo->bck_pid;
4210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
4211 t->clients[i]->id, delta);
4213 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
4214 (GNUNET_YES == t->nobuffer && 0 == delta))
4218 ack = clinfo->bck_pid;
4219 ack += t->nobuffer ? 1 : tunnel_delta;
4220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4221 " sending ack to client %u: %u\n",
4222 t->clients[i]->id, ack);
4223 send_local_ack (t, t->clients[i], ack);
4224 clinfo->bck_ack = ack;
4228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4229 " not sending ack to client %u (td %u, d %u)\n",
4230 t->clients[i]->id, tunnel_delta, delta);
4237 * Send an ACK informing the children nodes and destination clients about
4238 * the available buffer space.
4239 * If buffering is off, send only on behalf of root (can be self).
4240 * If buffering is on, send when sent to predecessor and buffer space is free.
4241 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
4242 * the ACK itself goes "forward" (towards children/clients).
4244 * @param t Tunnel on which to send the ACK.
4245 * @param type Type of message that triggered the ACK transmission.
4248 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
4250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4251 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
4252 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
4253 /* Is it after data to_origin retransmission? */
4256 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4257 if (GNUNET_YES == t->nobuffer)
4259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4260 " Not sending ACK, nobuffer\n");
4264 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4265 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
4267 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4268 t->force_ack = GNUNET_YES;
4274 tunnel_send_clients_bck_ack (t);
4275 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
4276 t->force_ack = GNUNET_NO;
4281 * @brief Re-initiate traffic to this peer if necessary.
4283 * Check if there is traffic queued towards this peer
4284 * and the core transmit handle is NULL (traffic was stalled).
4285 * If so, call core tmt rdy.
4287 * @param cls Closure (unused)
4288 * @param peer_id Short ID of peer to which initiate traffic.
4291 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
4293 struct MeshPeerInfo *peer;
4294 struct GNUNET_PeerIdentity id;
4295 struct MeshPeerQueue *q;
4298 peer = peer_info_get_short(peer_id);
4299 if (NULL != peer->core_transmit)
4302 q = queue_get_next(peer);
4305 /* Might br multicast traffic already sent to this particular peer but
4306 * not to other children in this tunnel.
4307 * This way t->queue_n would be > 0 but the queue of this particular peer
4313 GNUNET_PEER_resolve (peer->id, &id);
4314 peer->core_transmit =
4315 GNUNET_CORE_notify_transmit_ready(core_handle,
4318 GNUNET_TIME_UNIT_FOREVER_REL,
4328 * @brief Allow transmission of FWD traffic on this tunnel
4330 * Check if there is traffic queued towards any children
4331 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4333 * @param t Tunnel on which to unlock FWD traffic.
4336 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4338 if (0 == t->fwd_queue_n)
4341 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4346 * @brief Allow transmission of BCK traffic on this tunnel
4348 * Check if there is traffic queued towards the root of the tree
4349 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4351 * @param t Tunnel on which to unlock BCK traffic.
4354 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4356 if (0 == t->bck_queue_n)
4359 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4364 * Send a message to all peers in this tunnel that the tunnel is no longer
4367 * @param t The tunnel whose peers to notify.
4370 tunnel_send_destroy (struct MeshTunnel *t)
4372 struct GNUNET_MESH_TunnelDestroy msg;
4374 msg.header.size = htons (sizeof (msg));
4375 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4376 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4377 msg.tid = htonl (t->id.tid);
4378 tunnel_send_multicast (t, &msg.header);
4383 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4385 * @param cls Closure (Tunnel which to cancel).
4386 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4389 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4391 struct MeshTunnel *t = cls;
4392 struct MeshPeerInfo *peer_info;
4393 struct MeshPeerQueue *pq;
4394 struct MeshPeerQueue *next;
4396 peer_info = peer_info_get_short (neighbor_id);
4397 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4400 if (pq->tunnel == t)
4402 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4403 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4404 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4406 // Should have been removed on destroy children
4409 queue_destroy (pq, GNUNET_YES);
4412 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4414 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4415 peer_info->core_transmit = NULL;
4420 * Destroy the tunnel and free any allocated resources linked to it.
4422 * @param t the tunnel to destroy
4424 * @return GNUNET_OK on success
4427 tunnel_destroy (struct MeshTunnel *t)
4429 struct MeshClient *c;
4430 struct GNUNET_HashCode hash;
4441 struct GNUNET_PeerIdentity id;
4443 GNUNET_PEER_resolve (t->id.oid, &id);
4444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4445 GNUNET_i2s (&id), t->id.tid);
4447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4451 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4452 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4460 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4462 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4469 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4470 for (i = 0; i < t->nclients; i++)
4474 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4480 for (i = 0; i < t->nignore; i++)
4484 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4491 if (t->nclients > 0)
4494 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4499 GNUNET_free (t->clients);
4500 GNUNET_free (t->clients_fc);
4503 if (NULL != t->peers)
4505 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4507 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4510 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4511 &tunnel_destroy_child,
4513 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4514 t->children_fc = NULL;
4516 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4517 tree_destroy (t->tree);
4519 if (NULL != t->regex_ctx)
4520 regex_cancel_search (t->regex_ctx);
4521 if (NULL != t->dht_get_type)
4522 GNUNET_DHT_get_stop (t->dht_get_type);
4523 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4524 GNUNET_SCHEDULER_cancel (t->timeout_task);
4525 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4526 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4529 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4536 * Create a new tunnel
4538 * @param owner Who is the owner of the tunnel (short ID).
4539 * @param tid Tunnel Number of the tunnel.
4540 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4541 * @param local Tunnel Number for the tunnel, for the client point of view.
4543 * @return A new initialized tunnel. NULL on error.
4545 static struct MeshTunnel *
4546 tunnel_new (GNUNET_PEER_Id owner,
4547 MESH_TunnelNumber tid,
4548 struct MeshClient *client,
4549 MESH_TunnelNumber local)
4551 struct MeshTunnel *t;
4552 struct GNUNET_HashCode hash;
4554 if (n_tunnels >= max_tunnels && NULL == client)
4557 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4560 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4561 t->bck_queue_max = t->fwd_queue_max;
4562 t->tree = tree_new (owner);
4564 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4565 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4566 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4567 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4568 t->local_tid = local;
4569 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
4571 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4573 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4575 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4576 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4583 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4590 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4592 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4593 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4597 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4607 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4608 * that are no longer needed, as well as nodes of no longer reachable peers.
4609 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4611 * @param t Tunnel from which to remove the path.
4612 * @param peer Short id of the peer which should be removed.
4615 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4619 r = tree_del_peer (t->tree, peer, NULL, NULL);
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4623 "Tunnel %u [%u] has no more nodes\n",
4624 t->id.oid, t->id.tid);
4630 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4631 * client when the client disconnects. If the client is not the owner, the
4632 * owner will get notified if no more clients are in the tunnel and the client
4633 * get removed from the tunnel's list.
4635 * @param cls closure (client that is disconnecting)
4636 * @param key the hash of the local tunnel id (used to access the hashmap)
4637 * @param value the value stored at the key (tunnel to destroy)
4639 * @return GNUNET_OK, keep iterating.
4642 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4644 struct MeshTunnel *t = value;
4645 struct MeshClient *c = cls;
4647 send_client_tunnel_disconnect(t, c);
4650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4651 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4652 tunnel_delete_client(t, c);
4653 client_delete_tunnel(c, t);
4656 tunnel_send_destroy(t);
4658 t->destroy = GNUNET_YES;
4665 * Timeout function, destroys tunnel if called
4667 * @param cls Closure (tunnel to destroy).
4668 * @param tc TaskContext
4671 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4673 struct MeshTunnel *t = cls;
4674 struct GNUNET_PeerIdentity id;
4676 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4677 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4679 GNUNET_PEER_resolve(t->id.oid, &id);
4680 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4681 "Tunnel %s [%X] timed out. Destroying.\n",
4682 GNUNET_i2s(&id), t->id.tid);
4683 send_clients_tunnel_destroy (t);
4688 * Resets the tunnel timeout. Starts it if no timeout was running.
4690 * @param t Tunnel whose timeout to reset.
4692 * TODO use heap to improve efficiency of scheduler.
4695 tunnel_reset_timeout (struct MeshTunnel *t)
4697 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4698 GNUNET_SCHEDULER_cancel (t->timeout_task);
4700 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4701 (refresh_path_time, 4), &tunnel_timeout, t);
4705 /******************************************************************************/
4706 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4707 /******************************************************************************/
4710 * Function to send a create path packet to a peer.
4712 * @param cls closure
4713 * @param size number of bytes available in buf
4714 * @param buf where the callee should write the message
4715 * @return number of bytes written to buf
4718 send_core_path_create (void *cls, size_t size, void *buf)
4720 struct MeshPathInfo *info = cls;
4721 struct GNUNET_MESH_ManipulatePath *msg;
4722 struct GNUNET_PeerIdentity *peer_ptr;
4723 struct MeshTunnel *t = info->t;
4724 struct MeshPeerPath *p = info->path;
4729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4731 sizeof (struct GNUNET_MESH_ManipulatePath) +
4732 p->length * sizeof (struct GNUNET_PeerIdentity);
4734 if (size < size_needed || NULL == buf)
4739 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4740 msg->header.size = htons (size_needed);
4741 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4742 msg->tid = ntohl (t->id.tid);
4745 if (GNUNET_YES == t->speed_min)
4746 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4747 if (GNUNET_YES == t->nobuffer)
4748 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4749 msg->opt = htonl(opt);
4752 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4753 for (i = 0; i < p->length; i++)
4755 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4762 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4768 * Fill the core buffer
4770 * @param cls closure (data itself)
4771 * @param size number of bytes available in buf
4772 * @param buf where the callee should write the message
4774 * @return number of bytes written to buf
4777 send_core_data_multicast (void *cls, size_t size, void *buf)
4779 struct MeshTransmissionDescriptor *info = cls;
4782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4783 GNUNET_assert (NULL != info);
4784 GNUNET_assert (NULL != info->peer);
4785 total_size = info->mesh_data->data_len;
4786 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4788 if (total_size > size)
4793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4794 memcpy (buf, info->mesh_data->data, total_size);
4797 struct GNUNET_MESH_Multicast *mc;
4798 struct GNUNET_MessageHeader *mh;
4801 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4803 mc = (struct GNUNET_MESH_Multicast *) mh;
4804 mh = (struct GNUNET_MessageHeader *) &mc[1];
4805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4806 " multicast, payload type %s\n",
4807 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4809 " multicast, payload size %u\n", ntohs (mh->size));
4813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4814 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4818 data_descriptor_decrement_rc (info->mesh_data);
4819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4827 * Creates a path ack message in buf and frees all unused resources.
4829 * @param cls closure (MeshTransmissionDescriptor)
4830 * @param size number of bytes available in buf
4831 * @param buf where the callee should write the message
4832 * @return number of bytes written to buf
4835 send_core_path_ack (void *cls, size_t size, void *buf)
4837 struct MeshTransmissionDescriptor *info = cls;
4838 struct GNUNET_MESH_PathACK *msg = buf;
4840 GNUNET_assert (NULL != info);
4841 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4846 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4847 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4848 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4849 msg->tid = htonl (info->origin->tid);
4850 msg->peer_id = my_full_id;
4853 /* TODO add signature */
4855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4856 return sizeof (struct GNUNET_MESH_PathACK);
4861 * Free a transmission that was already queued with all resources
4862 * associated to the request.
4864 * @param queue Queue handler to cancel.
4865 * @param clear_cls Is it necessary to free associated cls?
4868 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4870 struct MeshTransmissionDescriptor *dd;
4871 struct MeshPathInfo *path_info;
4872 struct MeshTunnelChildInfo *cinfo;
4873 struct GNUNET_PeerIdentity id;
4877 if (GNUNET_YES == clear_cls)
4879 switch (queue->type)
4881 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4882 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4883 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4885 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4886 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4887 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4888 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4889 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4890 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4892 " prebuilt message\n");
4893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4895 GNUNET_MESH_DEBUG_M2S(queue->type));
4897 data_descriptor_decrement_rc (dd->mesh_data);
4899 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4901 path_info = queue->cls;
4902 path_destroy (path_info->path);
4906 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4907 " type %s unknown!\n",
4908 GNUNET_MESH_DEBUG_M2S(queue->type));
4910 GNUNET_free_non_null (queue->cls);
4912 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4913 queue->peer->queue_tail,
4916 /* Delete from child_fc in the appropiate tunnel */
4917 max = queue->tunnel->fwd_queue_max;
4918 GNUNET_PEER_resolve (queue->peer->id, &id);
4919 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4922 for (i = 0; i < cinfo->send_buffer_n; i++)
4925 i2 = (cinfo->send_buffer_start + i) % max;
4926 if (cinfo->send_buffer[i2] == queue)
4928 /* Found corresponding entry in the send_buffer. Move all others back. */
4933 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4935 j2 = (cinfo->send_buffer_start + j) % max;
4936 j3 = (cinfo->send_buffer_start + j + 1) % max;
4937 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4940 cinfo->send_buffer[j3] = NULL;
4941 cinfo->send_buffer_n--;
4946 GNUNET_free (queue);
4951 * @brief Get the next transmittable message from the queue.
4953 * This will be the head, except in the case of being a data packet
4954 * not allowed by the destination peer.
4956 * @param peer Destination peer.
4958 * @return The next viable MeshPeerQueue element to send to that peer.
4959 * NULL when there are no transmittable messages.
4961 struct MeshPeerQueue *
4962 queue_get_next (const struct MeshPeerInfo *peer)
4964 struct MeshPeerQueue *q;
4965 struct MeshTunnel *t;
4966 struct MeshTransmissionDescriptor *info;
4967 struct MeshTunnelChildInfo *cinfo;
4968 struct GNUNET_MESH_Unicast *ucast;
4969 struct GNUNET_MESH_ToOrigin *to_orig;
4970 struct GNUNET_MESH_Multicast *mcast;
4971 struct GNUNET_PeerIdentity id;
4975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4976 for (q = peer->queue_head; NULL != q; q = q->next)
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4982 GNUNET_MESH_DEBUG_M2S(q->type));
4985 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4986 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4987 pid = ntohl (ucast->pid);
4988 GNUNET_PEER_resolve (info->peer->id, &id);
4989 cinfo = tunnel_get_neighbor_fc(t, &id);
4990 ack = cinfo->fwd_ack;
4992 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4993 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4994 pid = ntohl (to_orig->pid);
4997 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4998 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4999 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
5001 // Not a multicast payload: multicast control traffic (destroy, etc)
5004 pid = ntohl (mcast->pid);
5005 GNUNET_PEER_resolve (info->peer->id, &id);
5006 cinfo = tunnel_get_neighbor_fc(t, &id);
5007 ack = cinfo->fwd_ack;
5010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5015 "********* ACK: %u, PID: %u\n",
5017 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
5019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5026 "********* NEXT!\n");
5029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5030 "********* nothing found\n");
5036 * Core callback to write a queued packet to core buffer
5038 * @param cls Closure (peer info).
5039 * @param size Number of bytes available in buf.
5040 * @param buf Where the to write the message.
5042 * @return number of bytes written to buf
5045 queue_send (void *cls, size_t size, void *buf)
5047 struct MeshPeerInfo *peer = cls;
5048 struct GNUNET_MessageHeader *msg;
5049 struct MeshPeerQueue *queue;
5050 struct MeshTunnel *t;
5051 struct MeshTunnelChildInfo *cinfo;
5052 struct GNUNET_PeerIdentity dst_id;
5055 peer->core_transmit = NULL;
5058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
5059 queue = queue_get_next (peer);
5061 /* Queue has no internal mesh traffic nor sendable payload */
5064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
5065 if (NULL == peer->queue_head)
5066 GNUNET_break (0); // Should've been canceled
5069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
5071 GNUNET_PEER_resolve (peer->id, &dst_id);
5072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5073 "********* towards %s\n",
5074 GNUNET_i2s(&dst_id));
5075 /* Check if buffer size is enough for the message */
5076 if (queue->size > size)
5078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5079 "********* not enough room, reissue\n");
5080 peer->core_transmit =
5081 GNUNET_CORE_notify_transmit_ready (core_handle,
5084 GNUNET_TIME_UNIT_FOREVER_REL,
5091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
5094 GNUNET_assert (0 < t->pending_messages);
5095 t->pending_messages--;
5096 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
5099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5100 "********* unicast: t->q (%u/%u)\n",
5101 t->fwd_queue_n, t->fwd_queue_max);
5103 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
5106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
5110 switch (queue->type)
5113 case GNUNET_MESSAGE_TYPE_MESH_ACK:
5114 case GNUNET_MESSAGE_TYPE_MESH_POLL:
5115 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
5116 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
5117 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
5118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5119 "********* raw: %s\n",
5120 GNUNET_MESH_DEBUG_M2S (queue->type));
5122 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5123 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5124 data_size = send_core_data_raw (queue->cls, size, buf);
5125 msg = (struct GNUNET_MessageHeader *) buf;
5126 switch (ntohs (msg->type)) // Type of preconstructed message
5128 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5129 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5131 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5132 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5138 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
5141 struct MeshTransmissionDescriptor *info = queue->cls;
5143 if ((1 == info->mesh_data->reference_counter
5144 && GNUNET_YES == t->speed_min)
5146 (info->mesh_data->total_out == info->mesh_data->reference_counter
5147 && GNUNET_NO == t->speed_min))
5149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5150 "********* considered sent\n");
5155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5156 "********* NOT considered sent yet\n");
5157 t->pending_messages++;
5160 data_size = send_core_data_multicast(queue->cls, size, buf);
5161 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5163 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
5164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
5165 data_size = send_core_path_create (queue->cls, size, buf);
5167 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
5169 data_size = send_core_path_ack (queue->cls, size, buf);
5171 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
5173 data_size = send_core_data_multicast (queue->cls, size, buf);
5177 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5178 "********* type unknown: %u\n",
5182 switch (queue->type)
5184 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
5185 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
5186 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
5187 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5188 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
5191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5192 "at pos %u (%p) != %p\n",
5193 cinfo->send_buffer_start,
5194 cinfo->send_buffer[cinfo->send_buffer_start],
5197 if (cinfo->send_buffer_n > 0)
5199 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
5200 cinfo->send_buffer_n--;
5201 cinfo->send_buffer_start++;
5202 cinfo->send_buffer_start %= t->fwd_queue_max;
5213 /* Free queue, but cls was freed by send_core_* */
5214 queue_destroy (queue, GNUNET_NO);
5216 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
5222 /* If more data in queue, send next */
5223 queue = queue_get_next(peer);
5226 struct GNUNET_PeerIdentity id;
5228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
5229 GNUNET_PEER_resolve (peer->id, &id);
5230 peer->core_transmit =
5231 GNUNET_CORE_notify_transmit_ready(core_handle,
5234 GNUNET_TIME_UNIT_FOREVER_REL,
5242 if (NULL != peer->queue_head)
5244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
5245 "********* %s stalled\n",
5246 GNUNET_i2s(&my_full_id));
5248 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
5249 // FIXME unify bck/fwd structures, bck does not have cinfo right now
5250 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
5252 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
5253 &tunnel_poll, cinfo);
5257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
5263 * @brief Queue and pass message to core when possible.
5265 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
5266 * and accounts for it. In case the queue is full, the message is dropped and
5269 * Otherwise, message is treated as internal and allowed to go regardless of
5272 * @param cls Closure (@c type dependant). It will be used by queue_send to
5273 * build the message to be sent if not already prebuilt.
5274 * @param type Type of the message, 0 for a raw message.
5275 * @param size Size of the message.
5276 * @param dst Neighbor to send message to.
5277 * @param t Tunnel this message belongs to.
5280 queue_add (void *cls, uint16_t type, size_t size,
5281 struct MeshPeerInfo *dst, struct MeshTunnel *t)
5283 struct MeshPeerQueue *queue;
5284 struct MeshTunnelChildInfo *cinfo;
5285 struct GNUNET_PeerIdentity id;
5291 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
5292 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
5294 n = &t->fwd_queue_n;
5295 max = &t->fwd_queue_max;
5297 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
5299 n = &t->bck_queue_n;
5300 max = &t->bck_queue_max;
5307 GNUNET_STATISTICS_update(stats,
5308 "# messages dropped (buffer full)",
5310 return; // Drop message
5314 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
5320 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
5321 GNUNET_PEER_resolve (dst->id, &id);
5322 if (NULL == dst->core_transmit)
5324 dst->core_transmit =
5325 GNUNET_CORE_notify_transmit_ready (core_handle,
5328 GNUNET_TIME_UNIT_FOREVER_REL,
5334 t->pending_messages++;
5335 if (NULL == n) // Is this internal mesh traffic?
5338 // It's payload, keep track of buffer per peer.
5339 cinfo = tunnel_get_neighbor_fc(t, &id);
5340 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
5341 if (NULL != cinfo->send_buffer[i])
5343 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
5344 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
5345 cinfo->send_buffer_start++;
5346 cinfo->send_buffer_start %= t->fwd_queue_max;
5350 cinfo->send_buffer_n++;
5352 cinfo->send_buffer[i] = queue;
5353 if (cinfo->send_buffer_n > t->fwd_queue_max)
5356 cinfo->send_buffer_n = t->fwd_queue_max;
5361 /******************************************************************************/
5362 /******************** MESH NETWORK HANDLERS **************************/
5363 /******************************************************************************/
5367 * Core handler for path creation
5369 * @param cls closure
5370 * @param message message
5371 * @param peer peer identity this notification is about
5372 * @param atsi performance data
5373 * @param atsi_count number of records in 'atsi'
5375 * @return GNUNET_OK to keep the connection open,
5376 * GNUNET_SYSERR to close it (signal serious error)
5379 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
5380 const struct GNUNET_MessageHeader *message,
5381 const struct GNUNET_ATS_Information *atsi,
5382 unsigned int atsi_count)
5384 unsigned int own_pos;
5387 MESH_TunnelNumber tid;
5388 struct GNUNET_MESH_ManipulatePath *msg;
5389 struct GNUNET_PeerIdentity *pi;
5390 struct GNUNET_HashCode hash;
5391 struct MeshPeerPath *path;
5392 struct MeshPeerInfo *dest_peer_info;
5393 struct MeshPeerInfo *orig_peer_info;
5394 struct MeshTunnel *t;
5396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5397 "Received a path create msg [%s]\n",
5398 GNUNET_i2s (&my_full_id));
5399 size = ntohs (message->size);
5400 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5402 GNUNET_break_op (0);
5406 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5407 if (size % sizeof (struct GNUNET_PeerIdentity))
5409 GNUNET_break_op (0);
5412 size /= sizeof (struct GNUNET_PeerIdentity);
5415 GNUNET_break_op (0);
5418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5419 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5421 tid = ntohl (msg->tid);
5422 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5424 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
5425 t = tunnel_get (pi, tid);
5426 if (NULL == t) // FIXME only for INCOMING tunnels?
5430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
5431 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
5434 // FIXME notify failure
5437 opt = ntohl (msg->opt);
5438 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
5439 GNUNET_YES : GNUNET_NO;
5440 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
5442 t->nobuffer = GNUNET_YES;
5443 t->last_fwd_ack = t->fwd_pid + 1;
5445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5446 " speed_min: %d, nobuffer:%d\n",
5447 t->speed_min, t->nobuffer);
5449 if (GNUNET_YES == t->nobuffer)
5451 t->bck_queue_max = 1;
5452 t->fwd_queue_max = 1;
5455 // FIXME only assign a local tid if a local client is interested (on demand)
5456 while (NULL != tunnel_get_incoming (next_local_tid))
5457 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5458 t->local_tid_dest = next_local_tid++;
5459 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5462 tunnel_reset_timeout (t);
5463 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5465 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5466 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5474 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5475 if (NULL == dest_peer_info)
5477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5478 " Creating PeerInfo for destination.\n");
5479 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5480 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5481 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5483 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5485 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5486 if (NULL == orig_peer_info)
5488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5489 " Creating PeerInfo for origin.\n");
5490 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5491 orig_peer_info->id = GNUNET_PEER_intern (pi);
5492 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5493 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5496 path = path_new (size);
5498 for (i = 0; i < size; i++)
5500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5501 GNUNET_i2s (&pi[i]));
5502 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5503 if (path->peers[i] == myid)
5506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5509 /* cannot be self, must be 'not found' */
5510 /* create path: self not found in path through self */
5511 GNUNET_break_op (0);
5512 path_destroy (path);
5516 path_add_to_peers (path, GNUNET_NO);
5517 tunnel_add_path (t, path, own_pos);
5518 if (own_pos == size - 1)
5520 /* It is for us! Send ack. */
5521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5522 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5523 if (NULL == t->peers)
5525 /* New tunnel! Notify clients on first payload message. */
5526 t->peers = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO);
5528 GNUNET_break (GNUNET_SYSERR !=
5529 GNUNET_CONTAINER_multihashmap_put (t->peers,
5530 &my_full_id.hashPubKey,
5533 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5538 struct MeshPeerPath *path2;
5540 /* It's for somebody else! Retransmit. */
5541 path2 = path_duplicate (path);
5542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5543 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5544 path2 = path_duplicate (path);
5545 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5546 send_create_path (dest_peer_info, path, t);
5553 * Core handler for path destruction
5555 * @param cls closure
5556 * @param message message
5557 * @param peer peer identity this notification is about
5558 * @param atsi performance data
5559 * @param atsi_count number of records in 'atsi'
5561 * @return GNUNET_OK to keep the connection open,
5562 * GNUNET_SYSERR to close it (signal serious error)
5565 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5566 const struct GNUNET_MessageHeader *message,
5567 const struct GNUNET_ATS_Information *atsi,
5568 unsigned int atsi_count)
5570 struct GNUNET_MESH_ManipulatePath *msg;
5571 struct GNUNET_PeerIdentity *pi;
5572 struct MeshPeerPath *path;
5573 struct MeshTunnel *t;
5574 unsigned int own_pos;
5578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5579 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5580 size = ntohs (message->size);
5581 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5583 GNUNET_break_op (0);
5587 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5588 if (size % sizeof (struct GNUNET_PeerIdentity))
5590 GNUNET_break_op (0);
5593 size /= sizeof (struct GNUNET_PeerIdentity);
5596 GNUNET_break_op (0);
5599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5601 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5602 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5604 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5606 t = tunnel_get (pi, ntohl (msg->tid));
5609 /* TODO notify back: we don't know this tunnel */
5610 GNUNET_break_op (0);
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5614 path = path_new (size);
5616 for (i = 0; i < size; i++)
5618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5619 GNUNET_i2s (&pi[i]));
5620 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5621 if (path->peers[i] == myid)
5624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5625 if (own_pos < path->length - 1)
5626 send_prebuilt_message (message, &pi[own_pos + 1], t);
5628 send_client_tunnel_disconnect(t, NULL);
5630 tunnel_delete_peer (t, path->peers[path->length - 1]);
5631 path_destroy (path);
5637 * Core handler for notifications of broken paths
5639 * @param cls closure
5640 * @param message message
5641 * @param peer peer identity this notification is about
5642 * @param atsi performance data
5643 * @param atsi_count number of records in 'atsi'
5645 * @return GNUNET_OK to keep the connection open,
5646 * GNUNET_SYSERR to close it (signal serious error)
5649 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5650 const struct GNUNET_MessageHeader *message,
5651 const struct GNUNET_ATS_Information *atsi,
5652 unsigned int atsi_count)
5654 struct GNUNET_MESH_PathBroken *msg;
5655 struct MeshTunnel *t;
5657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5658 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5659 msg = (struct GNUNET_MESH_PathBroken *) message;
5660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5661 GNUNET_i2s (&msg->peer1));
5662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5663 GNUNET_i2s (&msg->peer2));
5664 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5667 GNUNET_break_op (0);
5670 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5671 GNUNET_PEER_search (&msg->peer2));
5678 * Core handler for tunnel destruction
5680 * @param cls closure
5681 * @param message message
5682 * @param peer peer identity this notification is about
5683 * @param atsi performance data
5684 * @param atsi_count number of records in 'atsi'
5686 * @return GNUNET_OK to keep the connection open,
5687 * GNUNET_SYSERR to close it (signal serious error)
5690 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5691 const struct GNUNET_MessageHeader *message,
5692 const struct GNUNET_ATS_Information *atsi,
5693 unsigned int atsi_count)
5695 struct GNUNET_MESH_TunnelDestroy *msg;
5696 struct MeshTunnel *t;
5698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5699 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5700 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5702 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5703 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5706 /* Probably already got the message from another path,
5707 * destroyed the tunnel and retransmitted to children.
5710 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5713 if (t->id.oid == myid)
5715 GNUNET_break_op (0);
5718 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5720 /* Tunnel was incoming, notify clients */
5721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5722 t->local_tid, t->local_tid_dest);
5723 send_clients_tunnel_destroy (t);
5725 tunnel_send_destroy (t);
5726 t->destroy = GNUNET_YES;
5727 // TODO: add timeout to destroy the tunnel anyway
5733 * Core handler for mesh network traffic going from the origin to a peer
5735 * @param cls closure
5736 * @param peer peer identity this notification is about
5737 * @param message message
5738 * @param atsi performance data
5739 * @param atsi_count number of records in 'atsi'
5740 * @return GNUNET_OK to keep the connection open,
5741 * GNUNET_SYSERR to close it (signal serious error)
5744 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5745 const struct GNUNET_MessageHeader *message,
5746 const struct GNUNET_ATS_Information *atsi,
5747 unsigned int atsi_count)
5749 struct GNUNET_MESH_Unicast *msg;
5750 struct GNUNET_PeerIdentity *neighbor;
5751 struct MeshTunnelChildInfo *cinfo;
5752 struct MeshTunnel *t;
5753 GNUNET_PEER_Id dest_id;
5758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5761 size = ntohs (message->size);
5763 sizeof (struct GNUNET_MESH_Unicast) +
5764 sizeof (struct GNUNET_MessageHeader))
5769 msg = (struct GNUNET_MESH_Unicast *) message;
5770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5771 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5773 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5776 /* TODO notify back: we don't know this tunnel */
5777 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5778 GNUNET_break_op (0);
5781 pid = ntohl (msg->pid);
5782 if (t->fwd_pid == pid)
5784 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5785 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5786 " Already seen pid %u, DROPPING!\n", pid);
5791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5792 " pid %u not seen yet, forwarding\n", pid);
5795 t->skip += (pid - t->fwd_pid) - 1;
5798 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5800 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5801 GNUNET_break_op (0);
5802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5803 "Received PID %u, ACK %u\n",
5804 pid, t->last_fwd_ack);
5808 tunnel_reset_timeout (t);
5809 dest_id = GNUNET_PEER_search (&msg->destination);
5810 if (dest_id == myid)
5812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5813 " it's for us! sending to clients...\n");
5814 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5815 send_subscribed_clients (message, &msg[1].header, t);
5816 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5819 ttl = ntohl (msg->ttl);
5820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5823 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5824 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5825 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5829 " not for us, retransmitting...\n");
5831 neighbor = tree_get_first_hop (t->tree, dest_id);
5832 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5833 cinfo->fwd_pid = pid;
5834 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5837 if (GNUNET_YES == t->nobuffer &&
5838 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5840 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5841 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
5842 GNUNET_break_op (0);
5845 send_prebuilt_message (message, neighbor, t);
5846 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5852 * Core handler for mesh network traffic going from the origin to all peers
5854 * @param cls closure
5855 * @param message message
5856 * @param peer peer identity this notification is about
5857 * @param atsi performance data
5858 * @param atsi_count number of records in 'atsi'
5859 * @return GNUNET_OK to keep the connection open,
5860 * GNUNET_SYSERR to close it (signal serious error)
5862 * TODO: Check who we got this from, to validate route.
5865 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5866 const struct GNUNET_MessageHeader *message,
5867 const struct GNUNET_ATS_Information *atsi,
5868 unsigned int atsi_count)
5870 struct GNUNET_MESH_Multicast *msg;
5871 struct MeshTunnel *t;
5875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5877 size = ntohs (message->size);
5878 if (sizeof (struct GNUNET_MESH_Multicast) +
5879 sizeof (struct GNUNET_MessageHeader) > size)
5881 GNUNET_break_op (0);
5884 msg = (struct GNUNET_MESH_Multicast *) message;
5885 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5889 /* TODO notify that we dont know that tunnel */
5890 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5891 GNUNET_break_op (0);
5894 pid = ntohl (msg->pid);
5895 if (t->fwd_pid == pid)
5897 /* already seen this packet, drop */
5898 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5900 " Already seen pid %u, DROPPING!\n", pid);
5901 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5907 " pid %u not seen yet, forwarding\n", pid);
5909 t->skip += (pid - t->fwd_pid) - 1;
5911 tunnel_reset_timeout (t);
5913 /* Transmit to locally interested clients */
5914 if (NULL != t->peers &&
5915 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5917 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5918 send_subscribed_clients (message, &msg[1].header, t);
5919 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5922 if (ntohl (msg->ttl) == 0)
5924 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5925 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5926 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5929 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5930 tunnel_send_multicast (t, message);
5936 * Core handler for mesh network traffic toward the owner of a tunnel
5938 * @param cls closure
5939 * @param message message
5940 * @param peer peer identity this notification is about
5941 * @param atsi performance data
5942 * @param atsi_count number of records in 'atsi'
5944 * @return GNUNET_OK to keep the connection open,
5945 * GNUNET_SYSERR to close it (signal serious error)
5948 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5949 const struct GNUNET_MessageHeader *message,
5950 const struct GNUNET_ATS_Information *atsi,
5951 unsigned int atsi_count)
5953 struct GNUNET_MESH_ToOrigin *msg;
5954 struct GNUNET_PeerIdentity id;
5955 struct MeshPeerInfo *peer_info;
5956 struct MeshTunnel *t;
5957 struct MeshTunnelChildInfo *cinfo;
5961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5963 size = ntohs (message->size);
5964 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5965 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5967 GNUNET_break_op (0);
5970 msg = (struct GNUNET_MESH_ToOrigin *) message;
5971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5972 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5973 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5974 pid = ntohl (msg->pid);
5978 /* TODO notify that we dont know this tunnel (whom)? */
5979 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5980 GNUNET_break_op (0);
5981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5982 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
5983 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5987 cinfo = tunnel_get_neighbor_fc(t, peer);
5994 if (cinfo->bck_pid == pid)
5996 /* already seen this packet, drop */
5997 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5999 " Already seen pid %u, DROPPING!\n", pid);
6000 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6005 " pid %u not seen yet, forwarding\n", pid);
6006 cinfo->bck_pid = pid;
6008 if (NULL != t->owner)
6011 struct GNUNET_MESH_ToOrigin *copy;
6013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6014 " it's for us! sending to clients...\n");
6015 /* TODO signature verification */
6016 memcpy (cbuf, message, size);
6017 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
6018 copy->tid = htonl (t->local_tid);
6020 copy->pid = htonl (t->bck_pid);
6021 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
6022 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
6023 ©->header, GNUNET_NO);
6024 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
6027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6028 " not for us, retransmitting...\n");
6030 peer_info = peer_info_get (&msg->oid);
6031 if (NULL == peer_info)
6033 /* unknown origin of tunnel */
6037 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6038 send_prebuilt_message (message, &id, t);
6039 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6046 * Core handler for mesh network traffic point-to-point acks.
6048 * @param cls closure
6049 * @param message message
6050 * @param peer peer identity this notification is about
6051 * @param atsi performance data
6052 * @param atsi_count number of records in 'atsi'
6054 * @return GNUNET_OK to keep the connection open,
6055 * GNUNET_SYSERR to close it (signal serious error)
6058 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6059 const struct GNUNET_MessageHeader *message,
6060 const struct GNUNET_ATS_Information *atsi,
6061 unsigned int atsi_count)
6063 struct GNUNET_MESH_ACK *msg;
6064 struct MeshTunnel *t;
6067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6069 msg = (struct GNUNET_MESH_ACK *) message;
6071 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6075 /* TODO notify that we dont know this tunnel (whom)? */
6076 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6079 ack = ntohl (msg->pid);
6080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6082 /* Is this a forward or backward ACK? */
6083 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6085 struct MeshTunnelChildInfo *cinfo;
6088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6089 cinfo = tunnel_get_neighbor_fc (t, peer);
6090 cinfo->fwd_ack = ack;
6091 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6092 tunnel_unlock_fwd_queues (t);
6093 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6095 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6096 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6097 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6104 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6105 tunnel_unlock_bck_queue (t);
6112 * Core handler for mesh network traffic point-to-point ack polls.
6114 * @param cls closure
6115 * @param message message
6116 * @param peer peer identity this notification is about
6117 * @param atsi performance data
6118 * @param atsi_count number of records in 'atsi'
6120 * @return GNUNET_OK to keep the connection open,
6121 * GNUNET_SYSERR to close it (signal serious error)
6124 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6125 const struct GNUNET_MessageHeader *message,
6126 const struct GNUNET_ATS_Information *atsi,
6127 unsigned int atsi_count)
6129 struct GNUNET_MESH_Poll *msg;
6130 struct MeshTunnel *t;
6132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6135 msg = (struct GNUNET_MESH_Poll *) message;
6137 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6141 /* TODO notify that we dont know this tunnel (whom)? */
6142 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6143 GNUNET_break_op (0);
6147 /* Is this a forward or backward ACK? */
6148 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6150 struct MeshTunnelChildInfo *cinfo;
6152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6153 cinfo = tunnel_get_neighbor_fc (t, peer);
6154 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6155 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6160 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6168 * Core handler for path ACKs
6170 * @param cls closure
6171 * @param message message
6172 * @param peer peer identity this notification is about
6173 * @param atsi performance data
6174 * @param atsi_count number of records in 'atsi'
6176 * @return GNUNET_OK to keep the connection open,
6177 * GNUNET_SYSERR to close it (signal serious error)
6180 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6181 const struct GNUNET_MessageHeader *message,
6182 const struct GNUNET_ATS_Information *atsi,
6183 unsigned int atsi_count)
6185 struct GNUNET_MESH_PathACK *msg;
6186 struct GNUNET_PeerIdentity id;
6187 struct MeshPeerInfo *peer_info;
6188 struct MeshPeerPath *p;
6189 struct MeshTunnel *t;
6191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6192 GNUNET_i2s (&my_full_id));
6193 msg = (struct GNUNET_MESH_PathACK *) message;
6194 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6197 /* TODO notify that we don't know the tunnel */
6198 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6200 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6204 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6206 peer_info = peer_info_get (&msg->peer_id);
6207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6208 GNUNET_i2s (&msg->peer_id));
6209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6212 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6215 "connect_by_string completed, stopping search\n");
6216 regex_cancel_search (t->regex_ctx);
6217 t->regex_ctx = NULL;
6220 /* Add paths to peers? */
6221 p = tree_get_path_to_peer (t->tree, peer_info->id);
6224 path_add_to_peers (p, GNUNET_YES);
6232 /* Message for us? */
6233 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6236 if (NULL == t->owner)
6238 GNUNET_break_op (0);
6241 if (NULL != t->dht_get_type)
6243 GNUNET_DHT_get_stop (t->dht_get_type);
6244 t->dht_get_type = NULL;
6246 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6248 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6249 send_client_peer_connected (t, peer_info->id);
6254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6255 " not for us, retransmitting...\n");
6256 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6257 peer_info = peer_info_get (&msg->oid);
6258 if (NULL == peer_info)
6260 /* If we know the tunnel, we should DEFINITELY know the peer */
6264 send_prebuilt_message (message, &id, t);
6270 * Core handler for mesh keepalives.
6272 * @param cls closure
6273 * @param message message
6274 * @param peer peer identity this notification is about
6275 * @param atsi performance data
6276 * @param atsi_count number of records in 'atsi'
6277 * @return GNUNET_OK to keep the connection open,
6278 * GNUNET_SYSERR to close it (signal serious error)
6280 * TODO: Check who we got this from, to validate route.
6283 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6284 const struct GNUNET_MessageHeader *message,
6285 const struct GNUNET_ATS_Information *atsi,
6286 unsigned int atsi_count)
6288 struct GNUNET_MESH_TunnelKeepAlive *msg;
6289 struct MeshTunnel *t;
6291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6294 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6295 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6299 /* TODO notify that we dont know that tunnel */
6300 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6301 GNUNET_break_op (0);
6305 tunnel_reset_timeout (t);
6307 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6308 tunnel_send_multicast (t, message);
6315 * Functions to handle messages from core
6317 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6318 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6319 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6320 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6321 sizeof (struct GNUNET_MESH_PathBroken)},
6322 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6323 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6324 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6325 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6326 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6327 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6328 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6329 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6330 sizeof (struct GNUNET_MESH_ACK)},
6331 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6332 sizeof (struct GNUNET_MESH_Poll)},
6333 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6334 sizeof (struct GNUNET_MESH_PathACK)},
6340 /******************************************************************************/
6341 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6342 /******************************************************************************/
6345 * deregister_app: iterator for removing each application registered by a client
6347 * @param cls closure
6348 * @param key the hash of the application id (used to access the hashmap)
6349 * @param value the value stored at the key (client)
6351 * @return GNUNET_OK on success
6354 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6356 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6357 GNUNET_break (GNUNET_YES ==
6358 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6364 * notify_client_connection_failure: notify a client that the connection to the
6365 * requested remote peer is not possible (for instance, no route found)
6366 * Function called when the socket is ready to queue more data. "buf" will be
6367 * NULL and "size" zero if the socket was closed for writing in the meantime.
6369 * @param cls closure
6370 * @param size number of bytes available in buf
6371 * @param buf where the callee should write the message
6372 * @return number of bytes written to buf
6375 notify_client_connection_failure (void *cls, size_t size, void *buf)
6378 struct MeshPeerInfo *peer_info;
6379 struct GNUNET_MESH_PeerControl *msg;
6380 struct GNUNET_PeerIdentity id;
6382 if (0 == size && NULL == buf)
6384 // TODO retry? cancel?
6388 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6389 peer_info = (struct MeshPeerInfo *) cls;
6390 msg = (struct GNUNET_MESH_PeerControl *) buf;
6391 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6392 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6393 // msg->tunnel_id = htonl(peer_info->t->tid);
6394 GNUNET_PEER_resolve (peer_info->id, &id);
6395 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6403 * Send keepalive packets for a peer
6405 * @param cls Closure (tunnel for which to send the keepalive).
6406 * @param tc Notification context.
6409 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6411 struct MeshTunnel *t = cls;
6412 struct GNUNET_MESH_TunnelKeepAlive *msg;
6413 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6416 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6417 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6423 "sending keepalive for tunnel %d\n", t->id.tid);
6425 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6426 msg->header.size = htons (size);
6427 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6428 msg->oid = my_full_id;
6429 msg->tid = htonl (t->id.tid);
6430 tunnel_send_multicast (t, &msg->header);
6432 t->path_refresh_task =
6433 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6434 tunnel_reset_timeout(t);
6439 * Function to process paths received for a new peer addition. The recorded
6440 * paths form the initial tunnel, which can be optimized later.
6441 * Called on each result obtained for the DHT search.
6443 * @param cls closure
6444 * @param exp when will this value expire
6445 * @param key key of the result
6446 * @param get_path path of the get request
6447 * @param get_path_length lenght of get_path
6448 * @param put_path path of the put request
6449 * @param put_path_length length of the put_path
6450 * @param type type of the result
6451 * @param size number of bytes in data
6452 * @param data pointer to the result data
6454 * TODO: re-issue the request after certain time? cancel after X results?
6457 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6458 const struct GNUNET_HashCode * key,
6459 const struct GNUNET_PeerIdentity *get_path,
6460 unsigned int get_path_length,
6461 const struct GNUNET_PeerIdentity *put_path,
6462 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6463 size_t size, const void *data)
6465 struct MeshPathInfo *path_info = cls;
6466 struct MeshPeerPath *p;
6467 struct GNUNET_PeerIdentity pi;
6470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6471 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6474 p = path_build_from_dht (get_path, get_path_length, put_path,
6476 path_add_to_peers (p, GNUNET_NO);
6478 for (i = 0; i < path_info->peer->ntunnels; i++)
6480 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6481 peer_info_connect (path_info->peer, path_info->t);
6489 * Function to process paths received for a new peer addition. The recorded
6490 * paths form the initial tunnel, which can be optimized later.
6491 * Called on each result obtained for the DHT search.
6493 * @param cls closure
6494 * @param exp when will this value expire
6495 * @param key key of the result
6496 * @param get_path path of the get request
6497 * @param get_path_length lenght of get_path
6498 * @param put_path path of the put request
6499 * @param put_path_length length of the put_path
6500 * @param type type of the result
6501 * @param size number of bytes in data
6502 * @param data pointer to the result data
6505 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6506 const struct GNUNET_HashCode * key,
6507 const struct GNUNET_PeerIdentity *get_path,
6508 unsigned int get_path_length,
6509 const struct GNUNET_PeerIdentity *put_path,
6510 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6511 size_t size, const void *data)
6513 const struct PBlock *pb = data;
6514 const struct GNUNET_PeerIdentity *pi = &pb->id;
6515 struct MeshTunnel *t = cls;
6516 struct MeshPeerInfo *peer_info;
6517 struct MeshPeerPath *p;
6519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6520 if (size != sizeof (struct PBlock))
6522 GNUNET_break_op (0);
6525 if (ntohl(pb->type) != t->type)
6527 GNUNET_break_op (0);
6530 GNUNET_assert (NULL != t->owner);
6531 peer_info = peer_info_get (pi);
6532 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6534 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6536 p = path_build_from_dht (get_path, get_path_length, put_path,
6538 path_add_to_peers (p, GNUNET_NO);
6540 tunnel_add_peer (t, peer_info);
6541 peer_info_connect (peer_info, t);
6546 * Function to process DHT string to regex matching.
6547 * Called on each result obtained for the DHT search.
6549 * @param cls closure (search context)
6550 * @param exp when will this value expire
6551 * @param key key of the result
6552 * @param get_path path of the get request (not used)
6553 * @param get_path_length lenght of get_path (not used)
6554 * @param put_path path of the put request (not used)
6555 * @param put_path_length length of the put_path (not used)
6556 * @param type type of the result
6557 * @param size number of bytes in data
6558 * @param data pointer to the result data
6561 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6562 const struct GNUNET_HashCode * key,
6563 const struct GNUNET_PeerIdentity *get_path,
6564 unsigned int get_path_length,
6565 const struct GNUNET_PeerIdentity *put_path,
6566 unsigned int put_path_length,
6567 enum GNUNET_BLOCK_Type type,
6568 size_t size, const void *data)
6570 const struct MeshRegexAccept *block = data;
6571 struct MeshRegexSearchContext *ctx = cls;
6572 struct MeshRegexSearchInfo *info = ctx->info;
6573 // struct MeshPeerPath *p;
6574 struct MeshPeerInfo *peer_info;
6576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6578 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6580 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6583 peer_info = peer_info_get(&block->id);
6584 // p = path_build_from_dht (get_path, get_path_length, put_path,
6585 // put_path_length);
6586 // path_add_to_peers (p, GNUNET_NO);
6589 tunnel_add_peer (info->t, peer_info);
6590 peer_info_connect (peer_info, info->t);
6591 if (0 == info->peer)
6593 info->peer = peer_info->id;
6597 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6600 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6601 ®ex_connect_timeout,
6609 * Function to process DHT string to regex matching.
6610 * Called on each result obtained for the DHT search.
6612 * @param cls closure (search context)
6613 * @param exp when will this value expire
6614 * @param key key of the result
6615 * @param get_path path of the get request (not used)
6616 * @param get_path_length lenght of get_path (not used)
6617 * @param put_path path of the put request (not used)
6618 * @param put_path_length length of the put_path (not used)
6619 * @param type type of the result
6620 * @param size number of bytes in data
6621 * @param data pointer to the result data
6623 * TODO: re-issue the request after certain time? cancel after X results?
6626 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6627 const struct GNUNET_HashCode * key,
6628 const struct GNUNET_PeerIdentity *get_path,
6629 unsigned int get_path_length,
6630 const struct GNUNET_PeerIdentity *put_path,
6631 unsigned int put_path_length,
6632 enum GNUNET_BLOCK_Type type,
6633 size_t size, const void *data)
6635 const struct MeshRegexBlock *block = data;
6636 struct MeshRegexSearchContext *ctx = cls;
6637 struct MeshRegexSearchInfo *info = ctx->info;
6641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6642 "DHT GET STRING RETURNED RESULTS\n");
6643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6644 " key: %s\n", GNUNET_h2s (key));
6646 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6648 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6651 copy = GNUNET_malloc (size);
6652 memcpy (copy, data, size);
6653 GNUNET_break (GNUNET_OK ==
6654 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6655 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6656 len = ntohl (block->n_proof);
6658 char proof[len + 1];
6660 memcpy (proof, &block[1], len);
6662 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6664 GNUNET_break_op (0);
6668 len = strlen (info->description);
6669 if (len == ctx->position) // String processed
6671 if (GNUNET_YES == ntohl (block->accepting))
6673 regex_find_path(key, ctx);
6677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6678 // FIXME REGEX this block not successful, wait for more? start timeout?
6683 regex_next_edge (block, size, ctx);
6688 /******************************************************************************/
6689 /********************* MESH LOCAL HANDLES **************************/
6690 /******************************************************************************/
6694 * Handler for client disconnection
6696 * @param cls closure
6697 * @param client identification of the client; NULL
6698 * for the last call when the server is destroyed
6701 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6703 struct MeshClient *c;
6704 struct MeshClient *next;
6707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6714 // return; uncomment for regex_profiler
6718 if (c->handle != client)
6720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6726 GNUNET_SERVER_client_drop (c->handle);
6727 c->shutting_down = GNUNET_YES;
6728 GNUNET_assert (NULL != c->own_tunnels);
6729 GNUNET_assert (NULL != c->incoming_tunnels);
6730 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6731 &tunnel_destroy_iterator, c);
6732 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6733 &tunnel_destroy_iterator, c);
6734 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6735 &tunnel_destroy_iterator, c);
6736 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6737 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6738 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6740 /* deregister clients applications */
6741 if (NULL != c->apps)
6743 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6744 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6746 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6747 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6749 GNUNET_SCHEDULER_cancel (announce_applications_task);
6750 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6752 if (NULL != c->types)
6753 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6754 for (i = 0; i < c->n_regex; i++)
6756 GNUNET_free (c->regexes[i].regex);
6757 if (NULL != c->regexes[i].dfa)
6758 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6760 GNUNET_free_non_null (c->regexes);
6761 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6762 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6764 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6767 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6776 * Handler for new clients
6778 * @param cls closure
6779 * @param client identification of the client
6780 * @param message the actual message, which includes messages the client wants
6783 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6784 const struct GNUNET_MessageHeader *message)
6786 struct GNUNET_MESH_ClientConnect *cc_msg;
6787 struct MeshClient *c;
6788 GNUNET_MESH_ApplicationType *a;
6795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6796 /* Check data sanity */
6797 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6798 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6799 ntypes = ntohs (cc_msg->types);
6800 napps = ntohs (cc_msg->applications);
6802 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6805 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6809 /* Create new client structure */
6810 c = GNUNET_malloc (sizeof (struct MeshClient));
6811 c->id = next_client_id++;
6812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6814 GNUNET_SERVER_client_keep (client);
6815 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6818 GNUNET_MESH_ApplicationType at;
6819 struct GNUNET_HashCode hc;
6821 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6822 for (i = 0; i < napps; i++)
6825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6826 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6827 /* store in clients hashmap */
6828 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6829 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6830 /* store in global hashmap, for announcements */
6831 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6832 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6834 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6835 announce_applications_task =
6836 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6842 struct GNUNET_HashCode hc;
6844 t = (uint16_t *) & a[napps];
6845 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6846 for (i = 0; i < ntypes; i++)
6849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6850 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6852 /* store in clients hashmap */
6853 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6854 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6855 /* store in global hashmap */
6856 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6857 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6861 " client has %u+%u subscriptions\n", napps, ntypes);
6863 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6864 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6865 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6866 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6867 GNUNET_SERVER_notification_context_add (nc, client);
6868 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6870 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6876 * Handler for clients announcing available services by a regular expression.
6878 * @param cls closure
6879 * @param client identification of the client
6880 * @param message the actual message, which includes messages the client wants
6883 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6884 const struct GNUNET_MessageHeader *message)
6886 const struct GNUNET_MESH_RegexAnnounce *msg;
6887 struct MeshRegexDescriptor rd;
6888 struct MeshClient *c;
6892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6894 /* Sanity check for client registration */
6895 if (NULL == (c = client_get (client)))
6898 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6903 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6904 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6905 regex = GNUNET_malloc (len + 1);
6906 memcpy (regex, &msg[1], len);
6909 rd.compression = ntohs (msg->compression_characters);
6911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6914 GNUNET_array_append (c->regexes, c->n_regex, rd);
6915 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6917 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6923 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6929 * Handler for requests of new tunnels
6931 * @param cls closure
6932 * @param client identification of the client
6933 * @param message the actual message
6936 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6937 const struct GNUNET_MessageHeader *message)
6939 struct GNUNET_MESH_TunnelMessage *t_msg;
6940 struct MeshTunnel *t;
6941 struct MeshClient *c;
6942 MESH_TunnelNumber tid;
6944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6946 /* Sanity check for client registration */
6947 if (NULL == (c = client_get (client)))
6950 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6953 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6955 /* Message sanity check */
6956 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6959 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6963 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6964 /* Sanity check for tunnel numbering */
6965 tid = ntohl (t_msg->tunnel_id);
6966 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6969 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6972 /* Sanity check for duplicate tunnel IDs */
6973 if (NULL != tunnel_get_by_local_id (c, tid))
6976 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6980 while (NULL != tunnel_get_by_pi (myid, next_tid))
6981 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6982 t = tunnel_new (myid, next_tid++, c, tid);
6985 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6987 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6990 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6992 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6993 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6996 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7002 * Handler for requests of deleting tunnels
7004 * @param cls closure
7005 * @param client identification of the client
7006 * @param message the actual message
7009 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
7010 const struct GNUNET_MessageHeader *message)
7012 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7013 struct MeshClient *c;
7014 struct MeshTunnel *t;
7015 MESH_TunnelNumber tid;
7017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7018 "Got a DESTROY TUNNEL from client!\n");
7020 /* Sanity check for client registration */
7021 if (NULL == (c = client_get (client)))
7024 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7029 /* Message sanity check */
7030 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7033 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7037 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7039 /* Retrieve tunnel */
7040 tid = ntohl (tunnel_msg->tunnel_id);
7041 t = tunnel_get_by_local_id(c, tid);
7044 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7046 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7049 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7051 client_ignore_tunnel (c, t);
7053 // TODO: when to destroy incoming tunnel?
7054 if (t->nclients == 0)
7056 GNUNET_assert (GNUNET_YES ==
7057 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
7059 GNUNET_assert (GNUNET_YES ==
7060 GNUNET_CONTAINER_multihashmap_remove (t->peers,
7061 &my_full_id.hashPubKey,
7065 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7068 send_client_tunnel_disconnect(t, c);
7069 client_delete_tunnel(c, t);
7071 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7073 tunnel_send_destroy (t);
7074 t->destroy = GNUNET_YES;
7075 // The tunnel will be destroyed when the last message is transmitted.
7076 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7082 * Handler for requests of seeting tunnel's speed.
7084 * @param cls Closure (unused).
7085 * @param client Identification of the client.
7086 * @param message The actual message.
7089 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7090 const struct GNUNET_MessageHeader *message)
7092 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7093 struct MeshClient *c;
7094 struct MeshTunnel *t;
7095 MESH_TunnelNumber tid;
7097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7098 "Got a SPEED request from client!\n");
7100 /* Sanity check for client registration */
7101 if (NULL == (c = client_get (client)))
7104 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7110 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7112 /* Retrieve tunnel */
7113 tid = ntohl (tunnel_msg->tunnel_id);
7114 t = tunnel_get_by_local_id(c, tid);
7117 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7119 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7123 switch (ntohs(message->type))
7125 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7126 t->speed_min = GNUNET_YES;
7128 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7129 t->speed_min = GNUNET_NO;
7134 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7139 * Handler for requests of seeting tunnel's buffering policy.
7141 * @param cls Closure (unused).
7142 * @param client Identification of the client.
7143 * @param message The actual message.
7146 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7147 const struct GNUNET_MessageHeader *message)
7149 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7150 struct MeshClient *c;
7151 struct MeshTunnel *t;
7152 MESH_TunnelNumber tid;
7154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7155 "Got a BUFFER request from client!\n");
7157 /* Sanity check for client registration */
7158 if (NULL == (c = client_get (client)))
7161 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7166 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7168 /* Retrieve tunnel */
7169 tid = ntohl (tunnel_msg->tunnel_id);
7170 t = tunnel_get_by_local_id(c, tid);
7173 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7179 switch (ntohs(message->type))
7181 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7182 t->nobuffer = GNUNET_NO;
7184 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7185 t->nobuffer = GNUNET_YES;
7191 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7196 * Handler for connection requests to new peers
7198 * @param cls closure
7199 * @param client identification of the client
7200 * @param message the actual message (PeerControl)
7203 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7204 const struct GNUNET_MessageHeader *message)
7206 struct GNUNET_MESH_PeerControl *peer_msg;
7207 struct MeshPeerInfo *peer_info;
7208 struct MeshClient *c;
7209 struct MeshTunnel *t;
7210 MESH_TunnelNumber tid;
7212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7213 /* Sanity check for client registration */
7214 if (NULL == (c = client_get (client)))
7217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7222 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7224 /* Sanity check for message size */
7225 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7232 /* Tunnel exists? */
7233 tid = ntohl (peer_msg->tunnel_id);
7234 t = tunnel_get_by_local_id (c, tid);
7238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7242 /* Does client own tunnel? */
7243 if (t->owner->handle != client)
7246 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7250 GNUNET_i2s (&peer_msg->peer));
7251 peer_info = peer_info_get (&peer_msg->peer);
7253 tunnel_add_peer (t, peer_info);
7254 peer_info_connect (peer_info, t);
7256 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7262 * Handler for disconnection requests of peers in a tunnel
7264 * @param cls closure
7265 * @param client identification of the client
7266 * @param message the actual message (PeerControl)
7269 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7270 const struct GNUNET_MessageHeader *message)
7272 struct GNUNET_MESH_PeerControl *peer_msg;
7273 struct MeshPeerInfo *peer_info;
7274 struct MeshClient *c;
7275 struct MeshTunnel *t;
7276 MESH_TunnelNumber tid;
7278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7279 /* Sanity check for client registration */
7280 if (NULL == (c = client_get (client)))
7283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7288 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7290 /* Sanity check for message size */
7291 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7294 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7298 /* Tunnel exists? */
7299 tid = ntohl (peer_msg->tunnel_id);
7300 t = tunnel_get_by_local_id (c, tid);
7304 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7309 /* Does client own tunnel? */
7310 if (t->owner->handle != client)
7313 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7318 GNUNET_i2s (&peer_msg->peer));
7319 /* Is the peer in the tunnel? */
7321 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7322 if (NULL == peer_info)
7325 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7329 /* Ok, delete peer from tunnel */
7330 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7331 &peer_msg->peer.hashPubKey);
7333 send_destroy_path (t, peer_info->id);
7334 tunnel_delete_peer (t, peer_info->id);
7335 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7340 * Handler for blacklist requests of peers in a tunnel
7342 * @param cls closure
7343 * @param client identification of the client
7344 * @param message the actual message (PeerControl)
7346 * FIXME implement DHT block bloomfilter
7349 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7350 const struct GNUNET_MessageHeader *message)
7352 struct GNUNET_MESH_PeerControl *peer_msg;
7353 struct MeshClient *c;
7354 struct MeshTunnel *t;
7355 MESH_TunnelNumber tid;
7357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7358 /* Sanity check for client registration */
7359 if (NULL == (c = client_get (client)))
7362 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7367 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7369 /* Sanity check for message size */
7370 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7377 /* Tunnel exists? */
7378 tid = ntohl (peer_msg->tunnel_id);
7379 t = tunnel_get_by_local_id (c, tid);
7383 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7388 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7389 GNUNET_PEER_intern(&peer_msg->peer));
7394 * Handler for unblacklist requests of peers in a tunnel
7396 * @param cls closure
7397 * @param client identification of the client
7398 * @param message the actual message (PeerControl)
7401 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7402 const struct GNUNET_MessageHeader *message)
7404 struct GNUNET_MESH_PeerControl *peer_msg;
7405 struct MeshClient *c;
7406 struct MeshTunnel *t;
7407 MESH_TunnelNumber tid;
7411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7412 /* Sanity check for client registration */
7413 if (NULL == (c = client_get (client)))
7416 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7421 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7423 /* Sanity check for message size */
7424 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7431 /* Tunnel exists? */
7432 tid = ntohl (peer_msg->tunnel_id);
7433 t = tunnel_get_by_local_id (c, tid);
7437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7442 /* if peer is not known, complain */
7443 pid = GNUNET_PEER_search (&peer_msg->peer);
7450 /* search and remove from list */
7451 for (i = 0; i < t->nblacklisted; i++)
7453 if (t->blacklisted[i] == pid)
7455 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7456 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7461 /* if peer hasn't been blacklisted, complain */
7467 * Handler for connection requests to new peers by type
7469 * @param cls closure
7470 * @param client identification of the client
7471 * @param message the actual message (ConnectPeerByType)
7474 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7475 const struct GNUNET_MessageHeader *message)
7477 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7478 struct MeshClient *c;
7479 struct MeshTunnel *t;
7480 struct GNUNET_HashCode hash;
7481 MESH_TunnelNumber tid;
7483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7484 /* Sanity check for client registration */
7485 if (NULL == (c = client_get (client)))
7488 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7493 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7495 /* Sanity check for message size */
7496 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7497 ntohs (connect_msg->header.size))
7500 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7504 /* Tunnel exists? */
7505 tid = ntohl (connect_msg->tunnel_id);
7506 t = tunnel_get_by_local_id (c, tid);
7510 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7514 /* Does client own tunnel? */
7515 if (t->owner->handle != client)
7518 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7522 /* Do WE have the service? */
7523 t->type = ntohl (connect_msg->type);
7524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7525 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7526 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7529 /* Yes! Fast forward, add ourselves to the tunnel and send the
7530 * good news to the client, and alert the destination client of
7531 * an incoming tunnel.
7533 * FIXME send a path create to self, avoid code duplication
7535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7536 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7537 peer_info_get (&my_full_id),
7538 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7541 send_client_peer_connected (t, myid);
7542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7543 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7545 t->local_tid_dest = next_local_tid++;
7546 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7547 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7548 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7552 /* Ok, lets find a peer offering the service */
7553 if (NULL != t->dht_get_type)
7555 GNUNET_DHT_get_stop (t->dht_get_type);
7557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7558 GNUNET_h2s (&hash));
7560 GNUNET_DHT_get_start (dht_handle,
7561 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7563 dht_replication_level,
7564 GNUNET_DHT_RO_RECORD_ROUTE |
7565 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7567 &dht_get_type_handler, t);
7569 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7575 * Handler for connection requests to new peers by a string service description.
7577 * @param cls closure
7578 * @param client identification of the client
7579 * @param message the actual message, which includes messages the client wants
7582 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7583 const struct GNUNET_MessageHeader *message)
7585 struct GNUNET_MESH_ConnectPeerByString *msg;
7586 struct MeshRegexSearchContext *ctx;
7587 struct MeshRegexSearchInfo *info;
7588 struct GNUNET_DHT_GetHandle *get_h;
7589 struct GNUNET_HashCode key;
7590 struct MeshTunnel *t;
7591 struct MeshClient *c;
7592 MESH_TunnelNumber tid;
7597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7598 "Connect by string started\n");
7599 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7600 size = htons (message->size);
7602 /* Sanity check for client registration */
7603 if (NULL == (c = client_get (client)))
7606 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7611 /* Message size sanity check */
7612 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7615 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7619 /* Tunnel exists? */
7620 tid = ntohl (msg->tunnel_id);
7621 t = tunnel_get_by_local_id (c, tid);
7625 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7629 /* Does client own tunnel? */
7630 if (t->owner->handle != client)
7633 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7638 " on tunnel %s [%u]\n",
7639 GNUNET_i2s(&my_full_id),
7642 /* Only one connect_by_string allowed at the same time! */
7643 /* FIXME: allow more, return handle at api level to cancel, document */
7644 if (NULL != t->regex_ctx)
7647 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7651 /* Find string itself */
7652 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7653 string = (const char *) &msg[1];
7655 /* Initialize context */
7656 size = GNUNET_REGEX_get_first_key (string, len, &key);
7657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7658 " consumed %u bits out of %u\n", size, len);
7659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7660 " looking for %s\n", GNUNET_h2s (&key));
7662 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7664 info->description = GNUNET_strndup (string, len);
7665 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7666 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7669 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7670 ctx->position = size;
7674 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7676 /* Start search in DHT */
7677 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7678 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7679 &key, /* key to search */
7680 dht_replication_level, /* replication level */
7681 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7682 &info->description[size], /* xquery */
7683 // FIXME add BLOOMFILTER to exclude filtered peers
7684 len + 1 - size, /* xquery bits */
7685 // FIXME add BLOOMFILTER SIZE
7686 &dht_get_string_handler, ctx);
7688 GNUNET_break (GNUNET_OK ==
7689 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7692 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7694 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7700 * Handler for client traffic directed to one peer
7702 * @param cls closure
7703 * @param client identification of the client
7704 * @param message the actual message
7707 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7708 const struct GNUNET_MessageHeader *message)
7710 struct MeshClient *c;
7711 struct MeshTunnel *t;
7712 struct MeshPeerInfo *pi;
7713 struct GNUNET_MESH_Unicast *data_msg;
7714 MESH_TunnelNumber tid;
7717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7718 "Got a unicast request from a client!\n");
7720 /* Sanity check for client registration */
7721 if (NULL == (c = client_get (client)))
7724 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7729 data_msg = (struct GNUNET_MESH_Unicast *) message;
7731 /* Sanity check for message size */
7732 size = ntohs (message->size);
7733 if (sizeof (struct GNUNET_MESH_Unicast) +
7734 sizeof (struct GNUNET_MessageHeader) > size)
7737 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7741 /* Tunnel exists? */
7742 tid = ntohl (data_msg->tid);
7743 t = tunnel_get_by_local_id (c, tid);
7747 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7751 /* Is it a local tunnel? Then, does client own the tunnel? */
7752 if (t->owner->handle != client)
7755 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7759 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7760 &data_msg->destination.hashPubKey);
7761 /* Is the selected peer in the tunnel? */
7765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7769 /* PID should be as expected */
7770 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7773 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7774 "Unicast PID, expected %u, got %u\n",
7775 t->fwd_pid + 1, ntohl (data_msg->pid));
7776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7780 /* Ok, everything is correct, send the message
7781 * (pretend we got it from a mesh peer)
7784 /* Work around const limitation */
7785 char buf[ntohs (message->size)] GNUNET_ALIGN;
7786 struct GNUNET_MESH_Unicast *copy;
7788 copy = (struct GNUNET_MESH_Unicast *) buf;
7789 memcpy (buf, data_msg, size);
7790 copy->oid = my_full_id;
7791 copy->tid = htonl (t->id.tid);
7792 copy->ttl = htonl (default_ttl);
7793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7794 " calling generic handler...\n");
7795 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7798 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7805 * Handler for client traffic directed to the origin
7807 * @param cls closure
7808 * @param client identification of the client
7809 * @param message the actual message
7812 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7813 const struct GNUNET_MessageHeader *message)
7815 struct GNUNET_MESH_ToOrigin *data_msg;
7816 struct MeshTunnelClientInfo *clinfo;
7817 struct MeshClient *c;
7818 struct MeshTunnel *t;
7819 MESH_TunnelNumber tid;
7822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7823 "Got a ToOrigin request from a client!\n");
7824 /* Sanity check for client registration */
7825 if (NULL == (c = client_get (client)))
7828 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7833 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7835 /* Sanity check for message size */
7836 size = ntohs (message->size);
7837 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7838 sizeof (struct GNUNET_MessageHeader) > size)
7841 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7845 /* Tunnel exists? */
7846 tid = ntohl (data_msg->tid);
7847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7848 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7851 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7854 t = tunnel_get_by_local_id (c, tid);
7857 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7858 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7860 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7864 /* It should be sent by someone who has this as incoming tunnel. */
7865 if (GNUNET_NO == client_knows_tunnel (c, t))
7868 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7872 /* PID should be as expected */
7873 clinfo = tunnel_get_client_fc (t, c);
7874 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7878 "To Origin PID, expected %u, got %u\n",
7879 clinfo->bck_pid + 1,
7880 ntohl (data_msg->pid));
7881 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7886 /* Ok, everything is correct, send the message
7887 * (pretend we got it from a mesh peer)
7890 char buf[ntohs (message->size)] GNUNET_ALIGN;
7891 struct GNUNET_MESH_ToOrigin *copy;
7893 /* Work around const limitation */
7894 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7895 memcpy (buf, data_msg, size);
7896 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7897 copy->tid = htonl (t->id.tid);
7898 copy->ttl = htonl (default_ttl);
7899 copy->pid = htonl (t->bck_pid + 1);
7901 copy->sender = my_full_id;
7902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7903 " calling generic handler...\n");
7904 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7906 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7913 * Handler for client traffic directed to all peers in a tunnel
7915 * @param cls closure
7916 * @param client identification of the client
7917 * @param message the actual message
7920 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7921 const struct GNUNET_MessageHeader *message)
7923 struct MeshClient *c;
7924 struct MeshTunnel *t;
7925 struct GNUNET_MESH_Multicast *data_msg;
7926 MESH_TunnelNumber tid;
7928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7929 "Got a multicast request from a client!\n");
7931 /* Sanity check for client registration */
7932 if (NULL == (c = client_get (client)))
7935 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7940 data_msg = (struct GNUNET_MESH_Multicast *) message;
7942 /* Sanity check for message size */
7943 if (sizeof (struct GNUNET_MESH_Multicast) +
7944 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7947 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7951 /* Tunnel exists? */
7952 tid = ntohl (data_msg->tid);
7953 t = tunnel_get_by_local_id (c, tid);
7957 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7958 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7960 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7964 /* Does client own tunnel? */
7965 if (t->owner->handle != client)
7968 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7972 /* PID should be as expected */
7973 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7976 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7977 "Multicast PID, expected %u, got %u\n",
7978 t->fwd_pid + 1, ntohl (data_msg->pid));
7979 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7984 char buf[ntohs (message->size)] GNUNET_ALIGN;
7985 struct GNUNET_MESH_Multicast *copy;
7987 copy = (struct GNUNET_MESH_Multicast *) buf;
7988 memcpy (buf, message, ntohs (message->size));
7989 copy->oid = my_full_id;
7990 copy->tid = htonl (t->id.tid);
7991 copy->ttl = htonl (default_ttl);
7992 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7994 " calling generic handler...\n");
7995 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7998 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
8004 * Handler for client's ACKs for payload traffic.
8006 * @param cls Closure (unused).
8007 * @param client Identification of the client.
8008 * @param message The actual message.
8011 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
8012 const struct GNUNET_MessageHeader *message)
8014 struct GNUNET_MESH_LocalAck *msg;
8015 struct MeshTunnel *t;
8016 struct MeshClient *c;
8017 MESH_TunnelNumber tid;
8020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
8021 /* Sanity check for client registration */
8022 if (NULL == (c = client_get (client)))
8025 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8030 msg = (struct GNUNET_MESH_LocalAck *) message;
8032 /* Tunnel exists? */
8033 tid = ntohl (msg->tunnel_id);
8034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8035 t = tunnel_get_by_local_id (c, tid);
8039 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8040 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8045 ack = ntohl (msg->max_pid);
8046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8048 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8049 if (NULL != t->owner && t->owner->handle == client)
8051 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8053 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8057 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8058 tunnel_set_client_fwd_ack (t, c, ack);
8059 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8062 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8069 * Iterator over all peers to send a monitoring client info about a tunnel.
8071 * @param cls Closure (message being built).
8072 * @param key Key (hashed tunnel ID, unused).
8073 * @param value Peer info.
8075 * @return GNUNET_YES, to keep iterating.
8078 monitor_peers_iterator (void *cls,
8079 const struct GNUNET_HashCode * key,
8082 struct GNUNET_MESH_LocalMonitor *msg = cls;
8083 struct GNUNET_PeerIdentity *id;
8084 struct MeshPeerInfo *info = value;
8086 id = (struct GNUNET_PeerIdentity *) &msg[1];
8087 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8090 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8091 "* sending info about peer %s [%u]\n",
8092 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8100 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8102 * @param cls Closure (client handle).
8103 * @param key Key (hashed tunnel ID, unused).
8104 * @param value Tunnel info.
8106 * @return GNUNET_YES, to keep iterating.
8109 monitor_all_tunnels_iterator (void *cls,
8110 const struct GNUNET_HashCode * key,
8113 struct GNUNET_SERVER_Client *client = cls;
8114 struct MeshTunnel *t = value;
8115 struct GNUNET_MESH_LocalMonitor *msg;
8118 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8119 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8120 npeers * sizeof (struct GNUNET_PeerIdentity));
8121 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8122 msg->tunnel_id = htonl (t->id.tid);
8123 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8124 npeers * sizeof (struct GNUNET_PeerIdentity));
8125 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8127 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8128 monitor_peers_iterator,
8131 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8132 "* sending info about tunnel %s [%u] (%u peers)\n",
8133 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8135 if (msg->npeers != npeers)
8138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8139 "Get tunnels fail: size %u - iter %u\n",
8140 npeers, msg->npeers);
8143 msg->npeers = htonl (npeers);
8144 GNUNET_SERVER_notification_context_unicast (nc, client,
8145 &msg->header, GNUNET_NO);
8151 * Handler for client's MONITOR request.
8153 * @param cls Closure (unused).
8154 * @param client Identification of the client.
8155 * @param message The actual message.
8158 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8159 const struct GNUNET_MessageHeader *message)
8161 struct MeshClient *c;
8163 /* Sanity check for client registration */
8164 if (NULL == (c = client_get (client)))
8167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8171 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8172 "Received get tunnels request from client %u\n",
8174 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8175 monitor_all_tunnels_iterator,
8177 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8178 "Get tunnels request from client %u completed\n",
8180 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8185 * Data needed to build a Monitor_Tunnel message.
8187 struct MeshMonitorTunnelContext
8190 * Partial message, including peer count.
8192 struct GNUNET_MESH_LocalMonitor *msg;
8195 * Hashmap with positions: peer->position.
8197 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8200 * Index of the parent of each peer in the message, realtive to the absolute
8201 * order in the array (can be in a previous message).
8203 uint32_t parents[1024];
8206 * Peers visited so far in the tree, aka position of the current peer.
8208 unsigned int npeers;
8211 * Client requesting the info.
8213 struct MeshClient *c;
8218 * Send a client a message about the structure of a tunnel.
8220 * @param ctx Context of the tunnel iteration, with info regarding the state
8221 * of the execution and the number of peers visited for this message.
8224 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8226 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8227 struct GNUNET_PeerIdentity *pid;
8228 unsigned int *parent;
8231 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8232 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8233 resp->header.size = htons (size);
8234 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8235 parent = (unsigned int *) &pid[resp->npeers];
8236 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8237 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8238 &resp->header, GNUNET_NO);
8242 * Iterator over a tunnel tree to build a message containing all peers
8243 * the in the tunnel, including relay nodes.
8245 * @param cls Closure (pointer to pointer of message being built).
8246 * @param peer Short ID of a peer.
8247 * @param parent Short ID of the @c peer 's parent.
8250 tunnel_tree_iterator (void *cls,
8251 GNUNET_PEER_Id peer,
8252 GNUNET_PEER_Id parent)
8254 struct MeshMonitorTunnelContext *ctx = cls;
8255 struct GNUNET_MESH_LocalMonitor *msg;
8256 struct GNUNET_PeerIdentity *pid;
8257 struct GNUNET_PeerIdentity ppid;
8260 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8261 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8262 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8263 &pid[msg->npeers].hashPubKey,
8264 (void *) (long) ctx->npeers,
8265 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8266 GNUNET_PEER_resolve (parent, &ppid);
8267 ctx->parents[msg->npeers] =
8268 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8274 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8276 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8279 send_client_tunnel_info (ctx);
8286 * Handler for client's MONITOR_TUNNEL request.
8288 * @param cls Closure (unused).
8289 * @param client Identification of the client.
8290 * @param message The actual message.
8293 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8294 const struct GNUNET_MessageHeader *message)
8296 const struct GNUNET_MESH_LocalMonitor *msg;
8297 struct GNUNET_MESH_LocalMonitor *resp;
8298 struct MeshMonitorTunnelContext ctx;
8299 struct MeshClient *c;
8300 struct MeshTunnel *t;
8302 /* Sanity check for client registration */
8303 if (NULL == (c = client_get (client)))
8306 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8310 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8311 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8312 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8315 ntohl (msg->tunnel_id));
8316 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8319 /* We don't know the tunnel */
8320 struct GNUNET_MESH_LocalMonitor warn;
8323 warn.npeers = htonl (UINT_MAX);
8324 GNUNET_SERVER_notification_context_unicast (nc, client,
8327 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8331 /* Initialize context */
8332 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8336 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8340 /* Collect and send information */
8341 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8342 send_client_tunnel_info (&ctx);
8345 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8348 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8349 "Monitor tunnel request from client %u completed\n",
8351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8356 * Functions to handle messages from clients
8358 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8359 {&handle_local_new_client, NULL,
8360 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8361 {&handle_local_announce_regex, NULL,
8362 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8363 {&handle_local_tunnel_create, NULL,
8364 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8365 sizeof (struct GNUNET_MESH_TunnelMessage)},
8366 {&handle_local_tunnel_destroy, NULL,
8367 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8368 sizeof (struct GNUNET_MESH_TunnelMessage)},
8369 {&handle_local_tunnel_speed, NULL,
8370 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8371 sizeof (struct GNUNET_MESH_TunnelMessage)},
8372 {&handle_local_tunnel_speed, NULL,
8373 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8374 sizeof (struct GNUNET_MESH_TunnelMessage)},
8375 {&handle_local_tunnel_buffer, NULL,
8376 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8377 sizeof (struct GNUNET_MESH_TunnelMessage)},
8378 {&handle_local_tunnel_buffer, NULL,
8379 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8380 sizeof (struct GNUNET_MESH_TunnelMessage)},
8381 {&handle_local_connect_add, NULL,
8382 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8383 sizeof (struct GNUNET_MESH_PeerControl)},
8384 {&handle_local_connect_del, NULL,
8385 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8386 sizeof (struct GNUNET_MESH_PeerControl)},
8387 {&handle_local_blacklist, NULL,
8388 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8389 sizeof (struct GNUNET_MESH_PeerControl)},
8390 {&handle_local_unblacklist, NULL,
8391 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8392 sizeof (struct GNUNET_MESH_PeerControl)},
8393 {&handle_local_connect_by_type, NULL,
8394 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8395 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8396 {&handle_local_connect_by_string, NULL,
8397 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8398 {&handle_local_unicast, NULL,
8399 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8400 {&handle_local_to_origin, NULL,
8401 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8402 {&handle_local_multicast, NULL,
8403 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8404 {&handle_local_ack, NULL,
8405 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8406 sizeof (struct GNUNET_MESH_LocalAck)},
8407 {&handle_local_get_tunnels, NULL,
8408 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8409 sizeof (struct GNUNET_MessageHeader)},
8410 {&handle_local_show_tunnel, NULL,
8411 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8412 sizeof (struct GNUNET_MESH_LocalMonitor)},
8418 * To be called on core init/fail.
8420 * @param cls service closure
8421 * @param server handle to the server for this service
8422 * @param identity the public identity of this peer
8425 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8426 const struct GNUNET_PeerIdentity *identity)
8429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8430 core_handle = server;
8431 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8435 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8437 GNUNET_i2s (identity));
8438 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8440 GNUNET_i2s (&my_full_id));
8441 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8443 GNUNET_abort(); // Try harder
8450 * Method called whenever a given peer connects.
8452 * @param cls closure
8453 * @param peer peer identity this notification is about
8454 * @param atsi performance data for the connection
8455 * @param atsi_count number of records in 'atsi'
8458 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8459 const struct GNUNET_ATS_Information *atsi,
8460 unsigned int atsi_count)
8462 struct MeshPeerInfo *peer_info;
8463 struct MeshPeerPath *path;
8465 DEBUG_CONN ("Peer connected\n");
8466 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8467 peer_info = peer_info_get (peer);
8468 if (myid == peer_info->id)
8470 DEBUG_CONN (" (self)\n");
8475 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8477 path = path_new (2);
8478 path->peers[0] = myid;
8479 path->peers[1] = peer_info->id;
8480 GNUNET_PEER_change_rc (myid, 1);
8481 GNUNET_PEER_change_rc (peer_info->id, 1);
8482 peer_info_add_path (peer_info, path, GNUNET_YES);
8483 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8489 * Method called whenever a peer disconnects.
8491 * @param cls closure
8492 * @param peer peer identity this notification is about
8495 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8497 struct MeshPeerInfo *pi;
8498 struct MeshPeerQueue *q;
8499 struct MeshPeerQueue *n;
8501 DEBUG_CONN ("Peer disconnected\n");
8502 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8512 /* TODO try to reroute this traffic instead */
8513 queue_destroy(q, GNUNET_YES);
8516 if (NULL != pi->core_transmit)
8518 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8519 pi->core_transmit = NULL;
8521 peer_info_remove_path (pi, pi->id, myid);
8524 DEBUG_CONN (" (self)\n");
8526 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8531 /******************************************************************************/
8532 /************************ MAIN FUNCTIONS ****************************/
8533 /******************************************************************************/
8536 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8538 * @param cls closure
8539 * @param key current key code
8540 * @param value value in the hash map
8541 * @return GNUNET_YES if we should continue to iterate,
8545 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8547 struct MeshTunnel *t = value;
8554 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8556 * @param cls closure
8557 * @param key current key code
8558 * @param value value in the hash map
8559 * @return GNUNET_YES if we should continue to iterate,
8563 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8565 struct MeshPeerInfo *p = value;
8566 struct MeshPeerQueue *q;
8567 struct MeshPeerQueue *n;
8575 queue_destroy(q, GNUNET_YES);
8579 peer_info_destroy (p);
8585 * Task run during shutdown.
8591 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8595 if (core_handle != NULL)
8597 GNUNET_CORE_disconnect (core_handle);
8602 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8605 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8606 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8607 if (dht_handle != NULL)
8609 GNUNET_DHT_disconnect (dht_handle);
8614 GNUNET_SERVER_notification_context_destroy (nc);
8617 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8619 GNUNET_SCHEDULER_cancel (announce_id_task);
8620 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8622 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8624 GNUNET_SCHEDULER_cancel (announce_applications_task);
8625 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8632 * Callback for hostkey read/generation
8634 * @param cls Closure (Configuration handle).
8635 * @param pk the private key
8636 * @param emsg error message
8639 key_generation_cb (void *cls,
8640 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8643 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8644 struct MeshPeerInfo *peer;
8645 struct MeshPeerPath *p;
8650 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8651 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8653 GNUNET_SCHEDULER_shutdown ();
8656 my_private_key = pk;
8657 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8658 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8659 &my_full_id.hashPubKey);
8660 myid = GNUNET_PEER_intern (&my_full_id);
8661 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8662 "Mesh for peer [%s] starting\n",
8663 GNUNET_i2s(&my_full_id));
8665 // transport_handle = GNUNET_TRANSPORT_connect(c,
8672 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8673 NULL, /* Closure passed to MESH functions */
8674 &core_init, /* Call core_init once connected */
8675 &core_connect, /* Handle connects */
8676 &core_disconnect, /* remove peers on disconnects */
8677 NULL, /* Don't notify about all incoming messages */
8678 GNUNET_NO, /* For header only in notification */
8679 NULL, /* Don't notify about all outbound messages */
8680 GNUNET_NO, /* For header-only out notification */
8681 core_handlers); /* Register these handlers */
8683 if (core_handle == NULL)
8686 GNUNET_SCHEDULER_shutdown ();
8691 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8694 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8695 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8696 GNUNET_SERVER_disconnect_notify (server_handle,
8697 &handle_local_client_disconnect, NULL);
8701 clients_tail = NULL;
8704 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8705 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8707 /* Create a peer_info for the local peer */
8708 peer = peer_info_get (&my_full_id);
8711 GNUNET_PEER_change_rc (myid, 1);
8712 peer_info_add_path (peer, p, GNUNET_YES);
8713 GNUNET_SERVER_resume (server_handle);
8714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8719 * Process mesh requests.
8721 * @param cls closure
8722 * @param server the initialized server
8723 * @param c configuration to use
8726 run (void *cls, struct GNUNET_SERVER_Handle *server,
8727 const struct GNUNET_CONFIGURATION_Handle *c)
8731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8732 server_handle = server;
8735 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8738 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8740 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8742 GNUNET_SCHEDULER_shutdown ();
8747 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8748 &refresh_path_time))
8750 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8752 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8753 "mesh", "refresh path time");
8754 GNUNET_SCHEDULER_shutdown ();
8759 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8760 &app_announce_time))
8762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8764 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8765 "mesh", "app announce time");
8766 GNUNET_SCHEDULER_shutdown ();
8769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8770 "APP_ANNOUNCE_TIME %llu ms\n",
8771 app_announce_time.rel_value);
8773 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8778 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8779 "mesh", "id announce time");
8780 GNUNET_SCHEDULER_shutdown ();
8785 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8788 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8790 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8791 "mesh", "connect timeout");
8792 GNUNET_SCHEDULER_shutdown ();
8797 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8802 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8803 "mesh", "max msgs queue");
8804 GNUNET_SCHEDULER_shutdown ();
8809 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8814 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8815 "mesh", "max tunnels");
8816 GNUNET_SCHEDULER_shutdown ();
8821 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8824 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8826 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8827 "mesh", "default ttl", 64);
8832 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8835 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8836 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8837 "mesh", "max peers", 1000);
8842 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8843 &dht_replication_level))
8845 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8847 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8848 "mesh", "dht replication level", 3);
8849 dht_replication_level = 3;
8852 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8853 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8854 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8855 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8856 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8858 dht_handle = GNUNET_DHT_connect (c, 64);
8859 if (NULL == dht_handle)
8863 stats = GNUNET_STATISTICS_create ("mesh", c);
8865 GNUNET_SERVER_suspend (server_handle);
8866 /* Scheduled the task to clean up when shutdown is called */
8867 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8869 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
8872 GNUNET_free (keyfile);
8877 * The main function for the mesh service.
8879 * @param argc number of arguments from the command line
8880 * @param argv command line arguments
8881 * @return 0 ok, 1 on error
8884 main (int argc, char *const *argv)
8889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8890 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8892 ret = (GNUNET_OK == r) ? 0 : 1;
8893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8897 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8898 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8899 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);