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;
5958 GNUNET_PEER_Id predecessor;
5962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5964 size = ntohs (message->size);
5965 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5966 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5968 GNUNET_break_op (0);
5971 msg = (struct GNUNET_MESH_ToOrigin *) message;
5972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5973 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5974 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5975 pid = ntohl (msg->pid);
5979 /* TODO notify that we dont know this tunnel (whom)? */
5980 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
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 predecessor = tree_get_predecessor (t->tree);
6038 if (0 == predecessor)
6040 if (GNUNET_YES == t->destroy)
6042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6043 "to orig received on a dying tunnel %s [%X]\n",
6044 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6048 "unknown to origin at %s\n",
6049 GNUNET_i2s (&my_full_id));
6050 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6053 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6054 "for tunnel %s [%X]\n",
6055 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6058 tree_debug (t->tree);
6061 GNUNET_PEER_resolve (predecessor, &id);
6062 send_prebuilt_message (message, &id, t);
6063 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
6070 * Core handler for mesh network traffic point-to-point acks.
6072 * @param cls closure
6073 * @param message message
6074 * @param peer peer identity this notification is about
6075 * @param atsi performance data
6076 * @param atsi_count number of records in 'atsi'
6078 * @return GNUNET_OK to keep the connection open,
6079 * GNUNET_SYSERR to close it (signal serious error)
6082 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6083 const struct GNUNET_MessageHeader *message,
6084 const struct GNUNET_ATS_Information *atsi,
6085 unsigned int atsi_count)
6087 struct GNUNET_MESH_ACK *msg;
6088 struct MeshTunnel *t;
6091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
6093 msg = (struct GNUNET_MESH_ACK *) message;
6095 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6099 /* TODO notify that we dont know this tunnel (whom)? */
6100 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
6103 ack = ntohl (msg->pid);
6104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
6106 /* Is this a forward or backward ACK? */
6107 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6109 struct MeshTunnelChildInfo *cinfo;
6112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
6113 cinfo = tunnel_get_neighbor_fc (t, peer);
6114 cinfo->fwd_ack = ack;
6115 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6116 tunnel_unlock_fwd_queues (t);
6117 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
6119 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
6120 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
6121 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
6126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
6128 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
6129 tunnel_unlock_bck_queue (t);
6136 * Core handler for mesh network traffic point-to-point ack polls.
6138 * @param cls closure
6139 * @param message message
6140 * @param peer peer identity this notification is about
6141 * @param atsi performance data
6142 * @param atsi_count number of records in 'atsi'
6144 * @return GNUNET_OK to keep the connection open,
6145 * GNUNET_SYSERR to close it (signal serious error)
6148 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
6149 const struct GNUNET_MessageHeader *message,
6150 const struct GNUNET_ATS_Information *atsi,
6151 unsigned int atsi_count)
6153 struct GNUNET_MESH_Poll *msg;
6154 struct MeshTunnel *t;
6156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
6159 msg = (struct GNUNET_MESH_Poll *) message;
6161 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6165 /* TODO notify that we dont know this tunnel (whom)? */
6166 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
6167 GNUNET_break_op (0);
6171 /* Is this a forward or backward ACK? */
6172 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
6174 struct MeshTunnelChildInfo *cinfo;
6176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
6177 cinfo = tunnel_get_neighbor_fc (t, peer);
6178 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
6179 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
6184 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
6192 * Core handler for path ACKs
6194 * @param cls closure
6195 * @param message message
6196 * @param peer peer identity this notification is about
6197 * @param atsi performance data
6198 * @param atsi_count number of records in 'atsi'
6200 * @return GNUNET_OK to keep the connection open,
6201 * GNUNET_SYSERR to close it (signal serious error)
6204 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
6205 const struct GNUNET_MessageHeader *message,
6206 const struct GNUNET_ATS_Information *atsi,
6207 unsigned int atsi_count)
6209 struct GNUNET_MESH_PathACK *msg;
6210 struct GNUNET_PeerIdentity id;
6211 struct MeshPeerInfo *peer_info;
6212 struct MeshPeerPath *p;
6213 struct MeshTunnel *t;
6215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
6216 GNUNET_i2s (&my_full_id));
6217 msg = (struct GNUNET_MESH_PathACK *) message;
6218 t = tunnel_get (&msg->oid, ntohl(msg->tid));
6221 /* TODO notify that we don't know the tunnel */
6222 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
6223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
6224 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
6228 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
6230 peer_info = peer_info_get (&msg->peer_id);
6231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
6232 GNUNET_i2s (&msg->peer_id));
6233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
6236 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
6238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6239 "connect_by_string completed, stopping search\n");
6240 regex_cancel_search (t->regex_ctx);
6241 t->regex_ctx = NULL;
6244 /* Add paths to peers? */
6245 p = tree_get_path_to_peer (t->tree, peer_info->id);
6248 path_add_to_peers (p, GNUNET_YES);
6256 /* Message for us? */
6257 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
6259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
6260 if (NULL == t->owner)
6262 GNUNET_break_op (0);
6265 if (NULL != t->dht_get_type)
6267 GNUNET_DHT_get_stop (t->dht_get_type);
6268 t->dht_get_type = NULL;
6270 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
6272 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
6273 send_client_peer_connected (t, peer_info->id);
6278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6279 " not for us, retransmitting...\n");
6280 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
6281 peer_info = peer_info_get (&msg->oid);
6282 if (NULL == peer_info)
6284 /* If we know the tunnel, we should DEFINITELY know the peer */
6288 send_prebuilt_message (message, &id, t);
6294 * Core handler for mesh keepalives.
6296 * @param cls closure
6297 * @param message message
6298 * @param peer peer identity this notification is about
6299 * @param atsi performance data
6300 * @param atsi_count number of records in 'atsi'
6301 * @return GNUNET_OK to keep the connection open,
6302 * GNUNET_SYSERR to close it (signal serious error)
6304 * TODO: Check who we got this from, to validate route.
6307 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
6308 const struct GNUNET_MessageHeader *message,
6309 const struct GNUNET_ATS_Information *atsi,
6310 unsigned int atsi_count)
6312 struct GNUNET_MESH_TunnelKeepAlive *msg;
6313 struct MeshTunnel *t;
6315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
6318 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
6319 t = tunnel_get (&msg->oid, ntohl (msg->tid));
6323 /* TODO notify that we dont know that tunnel */
6324 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
6325 GNUNET_break_op (0);
6329 tunnel_reset_timeout (t);
6331 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
6332 tunnel_send_multicast (t, message);
6339 * Functions to handle messages from core
6341 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
6342 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
6343 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
6344 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
6345 sizeof (struct GNUNET_MESH_PathBroken)},
6346 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
6347 sizeof (struct GNUNET_MESH_TunnelDestroy)},
6348 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6349 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
6350 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
6351 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
6352 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6353 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
6354 sizeof (struct GNUNET_MESH_ACK)},
6355 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
6356 sizeof (struct GNUNET_MESH_Poll)},
6357 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
6358 sizeof (struct GNUNET_MESH_PathACK)},
6364 /******************************************************************************/
6365 /**************** MESH LOCAL HANDLER HELPERS ***********************/
6366 /******************************************************************************/
6369 * deregister_app: iterator for removing each application registered by a client
6371 * @param cls closure
6372 * @param key the hash of the application id (used to access the hashmap)
6373 * @param value the value stored at the key (client)
6375 * @return GNUNET_OK on success
6378 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
6380 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
6381 GNUNET_break (GNUNET_YES ==
6382 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
6388 * notify_client_connection_failure: notify a client that the connection to the
6389 * requested remote peer is not possible (for instance, no route found)
6390 * Function called when the socket is ready to queue more data. "buf" will be
6391 * NULL and "size" zero if the socket was closed for writing in the meantime.
6393 * @param cls closure
6394 * @param size number of bytes available in buf
6395 * @param buf where the callee should write the message
6396 * @return number of bytes written to buf
6399 notify_client_connection_failure (void *cls, size_t size, void *buf)
6402 struct MeshPeerInfo *peer_info;
6403 struct GNUNET_MESH_PeerControl *msg;
6404 struct GNUNET_PeerIdentity id;
6406 if (0 == size && NULL == buf)
6408 // TODO retry? cancel?
6412 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
6413 peer_info = (struct MeshPeerInfo *) cls;
6414 msg = (struct GNUNET_MESH_PeerControl *) buf;
6415 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
6416 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
6417 // msg->tunnel_id = htonl(peer_info->t->tid);
6418 GNUNET_PEER_resolve (peer_info->id, &id);
6419 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
6427 * Send keepalive packets for a peer
6429 * @param cls Closure (tunnel for which to send the keepalive).
6430 * @param tc Notification context.
6433 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6435 struct MeshTunnel *t = cls;
6436 struct GNUNET_MESH_TunnelKeepAlive *msg;
6437 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
6440 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
6441 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
6446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6447 "sending keepalive for tunnel %d\n", t->id.tid);
6449 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
6450 msg->header.size = htons (size);
6451 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
6452 msg->oid = my_full_id;
6453 msg->tid = htonl (t->id.tid);
6454 tunnel_send_multicast (t, &msg->header);
6456 t->path_refresh_task =
6457 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
6458 tunnel_reset_timeout(t);
6463 * Function to process paths received for a new peer addition. The recorded
6464 * paths form the initial tunnel, which can be optimized later.
6465 * Called on each result obtained for the DHT search.
6467 * @param cls closure
6468 * @param exp when will this value expire
6469 * @param key key of the result
6470 * @param get_path path of the get request
6471 * @param get_path_length lenght of get_path
6472 * @param put_path path of the put request
6473 * @param put_path_length length of the put_path
6474 * @param type type of the result
6475 * @param size number of bytes in data
6476 * @param data pointer to the result data
6478 * TODO: re-issue the request after certain time? cancel after X results?
6481 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6482 const struct GNUNET_HashCode * key,
6483 const struct GNUNET_PeerIdentity *get_path,
6484 unsigned int get_path_length,
6485 const struct GNUNET_PeerIdentity *put_path,
6486 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6487 size_t size, const void *data)
6489 struct MeshPathInfo *path_info = cls;
6490 struct MeshPeerPath *p;
6491 struct GNUNET_PeerIdentity pi;
6494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
6495 GNUNET_PEER_resolve (path_info->peer->id, &pi);
6496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
6498 p = path_build_from_dht (get_path, get_path_length, put_path,
6500 path_add_to_peers (p, GNUNET_NO);
6502 for (i = 0; i < path_info->peer->ntunnels; i++)
6504 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
6505 peer_info_connect (path_info->peer, path_info->t);
6513 * Function to process paths received for a new peer addition. The recorded
6514 * paths form the initial tunnel, which can be optimized later.
6515 * Called on each result obtained for the DHT search.
6517 * @param cls closure
6518 * @param exp when will this value expire
6519 * @param key key of the result
6520 * @param get_path path of the get request
6521 * @param get_path_length lenght of get_path
6522 * @param put_path path of the put request
6523 * @param put_path_length length of the put_path
6524 * @param type type of the result
6525 * @param size number of bytes in data
6526 * @param data pointer to the result data
6529 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6530 const struct GNUNET_HashCode * key,
6531 const struct GNUNET_PeerIdentity *get_path,
6532 unsigned int get_path_length,
6533 const struct GNUNET_PeerIdentity *put_path,
6534 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6535 size_t size, const void *data)
6537 const struct PBlock *pb = data;
6538 const struct GNUNET_PeerIdentity *pi = &pb->id;
6539 struct MeshTunnel *t = cls;
6540 struct MeshPeerInfo *peer_info;
6541 struct MeshPeerPath *p;
6543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
6544 if (size != sizeof (struct PBlock))
6546 GNUNET_break_op (0);
6549 if (ntohl(pb->type) != t->type)
6551 GNUNET_break_op (0);
6554 GNUNET_assert (NULL != t->owner);
6555 peer_info = peer_info_get (pi);
6556 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
6558 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
6560 p = path_build_from_dht (get_path, get_path_length, put_path,
6562 path_add_to_peers (p, GNUNET_NO);
6564 tunnel_add_peer (t, peer_info);
6565 peer_info_connect (peer_info, t);
6570 * Function to process DHT string to regex matching.
6571 * Called on each result obtained for the DHT search.
6573 * @param cls closure (search context)
6574 * @param exp when will this value expire
6575 * @param key key of the result
6576 * @param get_path path of the get request (not used)
6577 * @param get_path_length lenght of get_path (not used)
6578 * @param put_path path of the put request (not used)
6579 * @param put_path_length length of the put_path (not used)
6580 * @param type type of the result
6581 * @param size number of bytes in data
6582 * @param data pointer to the result data
6585 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6586 const struct GNUNET_HashCode * key,
6587 const struct GNUNET_PeerIdentity *get_path,
6588 unsigned int get_path_length,
6589 const struct GNUNET_PeerIdentity *put_path,
6590 unsigned int put_path_length,
6591 enum GNUNET_BLOCK_Type type,
6592 size_t size, const void *data)
6594 const struct MeshRegexAccept *block = data;
6595 struct MeshRegexSearchContext *ctx = cls;
6596 struct MeshRegexSearchInfo *info = ctx->info;
6597 // struct MeshPeerPath *p;
6598 struct MeshPeerInfo *peer_info;
6600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6602 GNUNET_STATISTICS_update (stats, "# regex accepting blocks found",
6604 GNUNET_STATISTICS_update (stats, "# regex accepting block bytes found",
6607 peer_info = peer_info_get(&block->id);
6608 // p = path_build_from_dht (get_path, get_path_length, put_path,
6609 // put_path_length);
6610 // path_add_to_peers (p, GNUNET_NO);
6613 tunnel_add_peer (info->t, peer_info);
6614 peer_info_connect (peer_info, info->t);
6615 if (0 == info->peer)
6617 info->peer = peer_info->id;
6621 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6624 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6625 ®ex_connect_timeout,
6633 * Function to process DHT string to regex matching.
6634 * Called on each result obtained for the DHT search.
6636 * @param cls closure (search context)
6637 * @param exp when will this value expire
6638 * @param key key of the result
6639 * @param get_path path of the get request (not used)
6640 * @param get_path_length lenght of get_path (not used)
6641 * @param put_path path of the put request (not used)
6642 * @param put_path_length length of the put_path (not used)
6643 * @param type type of the result
6644 * @param size number of bytes in data
6645 * @param data pointer to the result data
6647 * TODO: re-issue the request after certain time? cancel after X results?
6650 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6651 const struct GNUNET_HashCode * key,
6652 const struct GNUNET_PeerIdentity *get_path,
6653 unsigned int get_path_length,
6654 const struct GNUNET_PeerIdentity *put_path,
6655 unsigned int put_path_length,
6656 enum GNUNET_BLOCK_Type type,
6657 size_t size, const void *data)
6659 const struct MeshRegexBlock *block = data;
6660 struct MeshRegexSearchContext *ctx = cls;
6661 struct MeshRegexSearchInfo *info = ctx->info;
6665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6666 "DHT GET STRING RETURNED RESULTS\n");
6667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6668 " key: %s\n", GNUNET_h2s (key));
6670 GNUNET_STATISTICS_update (stats, "# regex blocks found",
6672 GNUNET_STATISTICS_update (stats, "# regex block bytes found",
6675 copy = GNUNET_malloc (size);
6676 memcpy (copy, data, size);
6677 GNUNET_break (GNUNET_OK ==
6678 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6679 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6680 len = ntohl (block->n_proof);
6682 char proof[len + 1];
6684 memcpy (proof, &block[1], len);
6686 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6688 GNUNET_break_op (0);
6692 len = strlen (info->description);
6693 if (len == ctx->position) // String processed
6695 if (GNUNET_YES == ntohl (block->accepting))
6697 regex_find_path(key, ctx);
6701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6702 // FIXME REGEX this block not successful, wait for more? start timeout?
6707 regex_next_edge (block, size, ctx);
6712 /******************************************************************************/
6713 /********************* MESH LOCAL HANDLES **************************/
6714 /******************************************************************************/
6718 * Handler for client disconnection
6720 * @param cls closure
6721 * @param client identification of the client; NULL
6722 * for the last call when the server is destroyed
6725 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6727 struct MeshClient *c;
6728 struct MeshClient *next;
6731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6738 // return; uncomment for regex_profiler
6742 if (c->handle != client)
6744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6750 GNUNET_SERVER_client_drop (c->handle);
6751 c->shutting_down = GNUNET_YES;
6752 GNUNET_assert (NULL != c->own_tunnels);
6753 GNUNET_assert (NULL != c->incoming_tunnels);
6754 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6755 &tunnel_destroy_iterator, c);
6756 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6757 &tunnel_destroy_iterator, c);
6758 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6759 &tunnel_destroy_iterator, c);
6760 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6761 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6762 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6764 /* deregister clients applications */
6765 if (NULL != c->apps)
6767 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6768 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6770 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6771 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6773 GNUNET_SCHEDULER_cancel (announce_applications_task);
6774 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6776 if (NULL != c->types)
6777 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6778 for (i = 0; i < c->n_regex; i++)
6780 GNUNET_free (c->regexes[i].regex);
6781 if (NULL != c->regexes[i].dfa)
6782 GNUNET_REGEX_automaton_destroy (c->regexes[i].dfa);
6784 GNUNET_free_non_null (c->regexes);
6785 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6786 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6788 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6791 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6800 * Handler for new clients
6802 * @param cls closure
6803 * @param client identification of the client
6804 * @param message the actual message, which includes messages the client wants
6807 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6808 const struct GNUNET_MessageHeader *message)
6810 struct GNUNET_MESH_ClientConnect *cc_msg;
6811 struct MeshClient *c;
6812 GNUNET_MESH_ApplicationType *a;
6819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6820 /* Check data sanity */
6821 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6822 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6823 ntypes = ntohs (cc_msg->types);
6824 napps = ntohs (cc_msg->applications);
6826 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6829 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6833 /* Create new client structure */
6834 c = GNUNET_malloc (sizeof (struct MeshClient));
6835 c->id = next_client_id++;
6836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6838 GNUNET_SERVER_client_keep (client);
6839 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6842 GNUNET_MESH_ApplicationType at;
6843 struct GNUNET_HashCode hc;
6845 c->apps = GNUNET_CONTAINER_multihashmap_create (napps, GNUNET_NO);
6846 for (i = 0; i < napps; i++)
6849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6850 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6851 /* store in clients hashmap */
6852 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6853 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6854 /* store in global hashmap, for announcements */
6855 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6856 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6858 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6859 announce_applications_task =
6860 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6866 struct GNUNET_HashCode hc;
6868 t = (uint16_t *) & a[napps];
6869 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
6870 for (i = 0; i < ntypes; i++)
6873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6874 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6876 /* store in clients hashmap */
6877 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6878 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6879 /* store in global hashmap */
6880 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6881 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6885 " client has %u+%u subscriptions\n", napps, ntypes);
6887 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6888 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6889 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6890 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6891 GNUNET_SERVER_notification_context_add (nc, client);
6892 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6894 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6900 * Handler for clients announcing available services by a regular expression.
6902 * @param cls closure
6903 * @param client identification of the client
6904 * @param message the actual message, which includes messages the client wants
6907 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6908 const struct GNUNET_MessageHeader *message)
6910 const struct GNUNET_MESH_RegexAnnounce *msg;
6911 struct MeshRegexDescriptor rd;
6912 struct MeshClient *c;
6916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6918 /* Sanity check for client registration */
6919 if (NULL == (c = client_get (client)))
6922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6927 msg = (const struct GNUNET_MESH_RegexAnnounce *) message;
6928 len = ntohs (message->size) - sizeof(struct GNUNET_MESH_RegexAnnounce);
6929 regex = GNUNET_malloc (len + 1);
6930 memcpy (regex, &msg[1], len);
6933 rd.compression = ntohs (msg->compression_characters);
6935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " length %u\n", len);
6936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regex %s\n", regex);
6937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " cm %u\n", ntohs(rd.compression));
6938 GNUNET_array_append (c->regexes, c->n_regex, rd);
6939 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6941 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6953 * Handler for requests of new tunnels
6955 * @param cls closure
6956 * @param client identification of the client
6957 * @param message the actual message
6960 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6961 const struct GNUNET_MessageHeader *message)
6963 struct GNUNET_MESH_TunnelMessage *t_msg;
6964 struct MeshTunnel *t;
6965 struct MeshClient *c;
6966 MESH_TunnelNumber tid;
6968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6970 /* Sanity check for client registration */
6971 if (NULL == (c = client_get (client)))
6974 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6979 /* Message sanity check */
6980 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6983 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6987 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6988 /* Sanity check for tunnel numbering */
6989 tid = ntohl (t_msg->tunnel_id);
6990 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6993 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6996 /* Sanity check for duplicate tunnel IDs */
6997 if (NULL != tunnel_get_by_local_id (c, tid))
7000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7004 while (NULL != tunnel_get_by_pi (myid, next_tid))
7005 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7006 t = tunnel_new (myid, next_tid++, c, tid);
7009 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
7011 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7014 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
7015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
7016 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
7017 t->peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
7019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
7020 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7026 * Handler for requests of deleting tunnels
7028 * @param cls closure
7029 * @param client identification of the client
7030 * @param message the actual message
7033 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
7034 const struct GNUNET_MessageHeader *message)
7036 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7037 struct MeshClient *c;
7038 struct MeshTunnel *t;
7039 MESH_TunnelNumber tid;
7041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7042 "Got a DESTROY TUNNEL from client!\n");
7044 /* Sanity check for client registration */
7045 if (NULL == (c = client_get (client)))
7048 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7053 /* Message sanity check */
7054 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
7057 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7061 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7063 /* Retrieve tunnel */
7064 tid = ntohl (tunnel_msg->tunnel_id);
7065 t = tunnel_get_by_local_id(c, tid);
7068 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7070 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7073 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7075 client_ignore_tunnel (c, t);
7077 // TODO: when to destroy incoming tunnel?
7078 if (t->nclients == 0)
7080 GNUNET_assert (GNUNET_YES ==
7081 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
7083 GNUNET_assert (GNUNET_YES ==
7084 GNUNET_CONTAINER_multihashmap_remove (t->peers,
7085 &my_full_id.hashPubKey,
7089 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7092 send_client_tunnel_disconnect(t, c);
7093 client_delete_tunnel(c, t);
7095 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
7097 tunnel_send_destroy (t);
7098 t->destroy = GNUNET_YES;
7099 // The tunnel will be destroyed when the last message is transmitted.
7100 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7106 * Handler for requests of seeting tunnel's speed.
7108 * @param cls Closure (unused).
7109 * @param client Identification of the client.
7110 * @param message The actual message.
7113 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
7114 const struct GNUNET_MessageHeader *message)
7116 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7117 struct MeshClient *c;
7118 struct MeshTunnel *t;
7119 MESH_TunnelNumber tid;
7121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7122 "Got a SPEED request from client!\n");
7124 /* Sanity check for client registration */
7125 if (NULL == (c = client_get (client)))
7128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7134 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7136 /* Retrieve tunnel */
7137 tid = ntohl (tunnel_msg->tunnel_id);
7138 t = tunnel_get_by_local_id(c, tid);
7141 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
7143 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7147 switch (ntohs(message->type))
7149 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
7150 t->speed_min = GNUNET_YES;
7152 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
7153 t->speed_min = GNUNET_NO;
7158 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7163 * Handler for requests of seeting tunnel's buffering policy.
7165 * @param cls Closure (unused).
7166 * @param client Identification of the client.
7167 * @param message The actual message.
7170 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
7171 const struct GNUNET_MessageHeader *message)
7173 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
7174 struct MeshClient *c;
7175 struct MeshTunnel *t;
7176 MESH_TunnelNumber tid;
7178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7179 "Got a BUFFER request from client!\n");
7181 /* Sanity check for client registration */
7182 if (NULL == (c = client_get (client)))
7185 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7190 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
7192 /* Retrieve tunnel */
7193 tid = ntohl (tunnel_msg->tunnel_id);
7194 t = tunnel_get_by_local_id(c, tid);
7197 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
7199 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7203 switch (ntohs(message->type))
7205 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
7206 t->nobuffer = GNUNET_NO;
7208 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
7209 t->nobuffer = GNUNET_YES;
7215 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7220 * Handler for connection requests to new peers
7222 * @param cls closure
7223 * @param client identification of the client
7224 * @param message the actual message (PeerControl)
7227 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
7228 const struct GNUNET_MessageHeader *message)
7230 struct GNUNET_MESH_PeerControl *peer_msg;
7231 struct MeshPeerInfo *peer_info;
7232 struct MeshClient *c;
7233 struct MeshTunnel *t;
7234 MESH_TunnelNumber tid;
7236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
7237 /* Sanity check for client registration */
7238 if (NULL == (c = client_get (client)))
7241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7246 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7248 /* Sanity check for message size */
7249 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7252 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7256 /* Tunnel exists? */
7257 tid = ntohl (peer_msg->tunnel_id);
7258 t = tunnel_get_by_local_id (c, tid);
7262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7266 /* Does client own tunnel? */
7267 if (t->owner->handle != client)
7270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
7274 GNUNET_i2s (&peer_msg->peer));
7275 peer_info = peer_info_get (&peer_msg->peer);
7277 tunnel_add_peer (t, peer_info);
7278 peer_info_connect (peer_info, t);
7280 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7286 * Handler for disconnection requests of peers in a tunnel
7288 * @param cls closure
7289 * @param client identification of the client
7290 * @param message the actual message (PeerControl)
7293 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
7294 const struct GNUNET_MessageHeader *message)
7296 struct GNUNET_MESH_PeerControl *peer_msg;
7297 struct MeshPeerInfo *peer_info;
7298 struct MeshClient *c;
7299 struct MeshTunnel *t;
7300 MESH_TunnelNumber tid;
7302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
7303 /* Sanity check for client registration */
7304 if (NULL == (c = client_get (client)))
7307 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7312 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7314 /* Sanity check for message size */
7315 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7318 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7322 /* Tunnel exists? */
7323 tid = ntohl (peer_msg->tunnel_id);
7324 t = tunnel_get_by_local_id (c, tid);
7328 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7333 /* Does client own tunnel? */
7334 if (t->owner->handle != client)
7337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
7342 GNUNET_i2s (&peer_msg->peer));
7343 /* Is the peer in the tunnel? */
7345 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
7346 if (NULL == peer_info)
7349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7353 /* Ok, delete peer from tunnel */
7354 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
7355 &peer_msg->peer.hashPubKey);
7357 send_destroy_path (t, peer_info->id);
7358 tunnel_delete_peer (t, peer_info->id);
7359 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7364 * Handler for blacklist requests of peers in a tunnel
7366 * @param cls closure
7367 * @param client identification of the client
7368 * @param message the actual message (PeerControl)
7370 * FIXME implement DHT block bloomfilter
7373 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
7374 const struct GNUNET_MessageHeader *message)
7376 struct GNUNET_MESH_PeerControl *peer_msg;
7377 struct MeshClient *c;
7378 struct MeshTunnel *t;
7379 MESH_TunnelNumber tid;
7381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
7382 /* Sanity check for client registration */
7383 if (NULL == (c = client_get (client)))
7386 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7391 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7393 /* Sanity check for message size */
7394 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7397 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7401 /* Tunnel exists? */
7402 tid = ntohl (peer_msg->tunnel_id);
7403 t = tunnel_get_by_local_id (c, tid);
7407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7412 GNUNET_array_append(t->blacklisted, t->nblacklisted,
7413 GNUNET_PEER_intern(&peer_msg->peer));
7418 * Handler for unblacklist requests of peers in a tunnel
7420 * @param cls closure
7421 * @param client identification of the client
7422 * @param message the actual message (PeerControl)
7425 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
7426 const struct GNUNET_MessageHeader *message)
7428 struct GNUNET_MESH_PeerControl *peer_msg;
7429 struct MeshClient *c;
7430 struct MeshTunnel *t;
7431 MESH_TunnelNumber tid;
7435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
7436 /* Sanity check for client registration */
7437 if (NULL == (c = client_get (client)))
7440 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7445 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
7447 /* Sanity check for message size */
7448 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
7451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7455 /* Tunnel exists? */
7456 tid = ntohl (peer_msg->tunnel_id);
7457 t = tunnel_get_by_local_id (c, tid);
7461 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
7466 /* if peer is not known, complain */
7467 pid = GNUNET_PEER_search (&peer_msg->peer);
7474 /* search and remove from list */
7475 for (i = 0; i < t->nblacklisted; i++)
7477 if (t->blacklisted[i] == pid)
7479 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
7480 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
7485 /* if peer hasn't been blacklisted, complain */
7491 * Handler for connection requests to new peers by type
7493 * @param cls closure
7494 * @param client identification of the client
7495 * @param message the actual message (ConnectPeerByType)
7498 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
7499 const struct GNUNET_MessageHeader *message)
7501 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
7502 struct MeshClient *c;
7503 struct MeshTunnel *t;
7504 struct GNUNET_HashCode hash;
7505 MESH_TunnelNumber tid;
7507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
7508 /* Sanity check for client registration */
7509 if (NULL == (c = client_get (client)))
7512 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7517 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
7519 /* Sanity check for message size */
7520 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
7521 ntohs (connect_msg->header.size))
7524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7528 /* Tunnel exists? */
7529 tid = ntohl (connect_msg->tunnel_id);
7530 t = tunnel_get_by_local_id (c, tid);
7534 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7538 /* Does client own tunnel? */
7539 if (t->owner->handle != client)
7542 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7546 /* Do WE have the service? */
7547 t->type = ntohl (connect_msg->type);
7548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
7549 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
7550 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
7553 /* Yes! Fast forward, add ourselves to the tunnel and send the
7554 * good news to the client, and alert the destination client of
7555 * an incoming tunnel.
7557 * FIXME send a path create to self, avoid code duplication
7559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
7560 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
7561 peer_info_get (&my_full_id),
7562 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
7565 send_client_peer_connected (t, myid);
7566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
7567 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7569 t->local_tid_dest = next_local_tid++;
7570 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
7571 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
7572 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
7576 /* Ok, lets find a peer offering the service */
7577 if (NULL != t->dht_get_type)
7579 GNUNET_DHT_get_stop (t->dht_get_type);
7581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
7582 GNUNET_h2s (&hash));
7584 GNUNET_DHT_get_start (dht_handle,
7585 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
7587 dht_replication_level,
7588 GNUNET_DHT_RO_RECORD_ROUTE |
7589 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7591 &dht_get_type_handler, t);
7593 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7599 * Handler for connection requests to new peers by a string service description.
7601 * @param cls closure
7602 * @param client identification of the client
7603 * @param message the actual message, which includes messages the client wants
7606 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
7607 const struct GNUNET_MessageHeader *message)
7609 struct GNUNET_MESH_ConnectPeerByString *msg;
7610 struct MeshRegexSearchContext *ctx;
7611 struct MeshRegexSearchInfo *info;
7612 struct GNUNET_DHT_GetHandle *get_h;
7613 struct GNUNET_HashCode key;
7614 struct MeshTunnel *t;
7615 struct MeshClient *c;
7616 MESH_TunnelNumber tid;
7621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7622 "Connect by string started\n");
7623 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7624 size = htons (message->size);
7626 /* Sanity check for client registration */
7627 if (NULL == (c = client_get (client)))
7630 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7635 /* Message size sanity check */
7636 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7643 /* Tunnel exists? */
7644 tid = ntohl (msg->tunnel_id);
7645 t = tunnel_get_by_local_id (c, tid);
7649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7653 /* Does client own tunnel? */
7654 if (t->owner->handle != client)
7657 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7662 " on tunnel %s [%u]\n",
7663 GNUNET_i2s(&my_full_id),
7666 /* Only one connect_by_string allowed at the same time! */
7667 /* FIXME: allow more, return handle at api level to cancel, document */
7668 if (NULL != t->regex_ctx)
7671 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7675 /* Find string itself */
7676 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7677 string = (const char *) &msg[1];
7679 /* Initialize context */
7680 size = GNUNET_REGEX_get_first_key (string, len, &key);
7681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7682 " consumed %u bits out of %u\n", size, len);
7683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7684 " looking for %s\n", GNUNET_h2s (&key));
7686 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7688 info->description = GNUNET_strndup (string, len);
7689 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7690 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32, GNUNET_NO);
7691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7693 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7694 ctx->position = size;
7698 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7700 /* Start search in DHT */
7701 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7702 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7703 &key, /* key to search */
7704 dht_replication_level, /* replication level */
7705 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7706 &info->description[size], /* xquery */
7707 // FIXME add BLOOMFILTER to exclude filtered peers
7708 len + 1 - size, /* xquery bits */
7709 // FIXME add BLOOMFILTER SIZE
7710 &dht_get_string_handler, ctx);
7712 GNUNET_break (GNUNET_OK ==
7713 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7716 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7718 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7724 * Handler for client traffic directed to one peer
7726 * @param cls closure
7727 * @param client identification of the client
7728 * @param message the actual message
7731 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7732 const struct GNUNET_MessageHeader *message)
7734 struct MeshClient *c;
7735 struct MeshTunnel *t;
7736 struct MeshPeerInfo *pi;
7737 struct GNUNET_MESH_Unicast *data_msg;
7738 MESH_TunnelNumber tid;
7741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7742 "Got a unicast request from a client!\n");
7744 /* Sanity check for client registration */
7745 if (NULL == (c = client_get (client)))
7748 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7753 data_msg = (struct GNUNET_MESH_Unicast *) message;
7755 /* Sanity check for message size */
7756 size = ntohs (message->size);
7757 if (sizeof (struct GNUNET_MESH_Unicast) +
7758 sizeof (struct GNUNET_MessageHeader) > size)
7761 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7765 /* Tunnel exists? */
7766 tid = ntohl (data_msg->tid);
7767 t = tunnel_get_by_local_id (c, tid);
7771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7775 /* Is it a local tunnel? Then, does client own the tunnel? */
7776 if (t->owner->handle != client)
7779 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7783 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7784 &data_msg->destination.hashPubKey);
7785 /* Is the selected peer in the tunnel? */
7789 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7793 /* PID should be as expected */
7794 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7797 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7798 "Unicast PID, expected %u, got %u\n",
7799 t->fwd_pid + 1, ntohl (data_msg->pid));
7800 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7804 /* Ok, everything is correct, send the message
7805 * (pretend we got it from a mesh peer)
7808 /* Work around const limitation */
7809 char buf[ntohs (message->size)] GNUNET_ALIGN;
7810 struct GNUNET_MESH_Unicast *copy;
7812 copy = (struct GNUNET_MESH_Unicast *) buf;
7813 memcpy (buf, data_msg, size);
7814 copy->oid = my_full_id;
7815 copy->tid = htonl (t->id.tid);
7816 copy->ttl = htonl (default_ttl);
7817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7818 " calling generic handler...\n");
7819 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7822 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7829 * Handler for client traffic directed to the origin
7831 * @param cls closure
7832 * @param client identification of the client
7833 * @param message the actual message
7836 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7837 const struct GNUNET_MessageHeader *message)
7839 struct GNUNET_MESH_ToOrigin *data_msg;
7840 struct MeshTunnelClientInfo *clinfo;
7841 struct MeshClient *c;
7842 struct MeshTunnel *t;
7843 MESH_TunnelNumber tid;
7846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7847 "Got a ToOrigin request from a client!\n");
7848 /* Sanity check for client registration */
7849 if (NULL == (c = client_get (client)))
7852 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7857 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7859 /* Sanity check for message size */
7860 size = ntohs (message->size);
7861 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7862 sizeof (struct GNUNET_MessageHeader) > size)
7865 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7869 /* Tunnel exists? */
7870 tid = ntohl (data_msg->tid);
7871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7872 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7875 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7878 t = tunnel_get_by_local_id (c, tid);
7881 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7882 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7884 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7888 /* It should be sent by someone who has this as incoming tunnel. */
7889 if (GNUNET_NO == client_knows_tunnel (c, t))
7892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7896 /* PID should be as expected */
7897 clinfo = tunnel_get_client_fc (t, c);
7898 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7901 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7902 "To Origin PID, expected %u, got %u\n",
7903 clinfo->bck_pid + 1,
7904 ntohl (data_msg->pid));
7905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7910 /* Ok, everything is correct, send the message
7911 * (pretend we got it from a mesh peer)
7914 char buf[ntohs (message->size)] GNUNET_ALIGN;
7915 struct GNUNET_MESH_ToOrigin *copy;
7917 /* Work around const limitation */
7918 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7919 memcpy (buf, data_msg, size);
7920 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7921 copy->tid = htonl (t->id.tid);
7922 copy->ttl = htonl (default_ttl);
7923 copy->pid = htonl (t->bck_pid + 1);
7925 copy->sender = my_full_id;
7926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7927 " calling generic handler...\n");
7928 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7930 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7937 * Handler for client traffic directed to all peers in a tunnel
7939 * @param cls closure
7940 * @param client identification of the client
7941 * @param message the actual message
7944 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7945 const struct GNUNET_MessageHeader *message)
7947 struct MeshClient *c;
7948 struct MeshTunnel *t;
7949 struct GNUNET_MESH_Multicast *data_msg;
7950 MESH_TunnelNumber tid;
7952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7953 "Got a multicast request from a client!\n");
7955 /* Sanity check for client registration */
7956 if (NULL == (c = client_get (client)))
7959 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7964 data_msg = (struct GNUNET_MESH_Multicast *) message;
7966 /* Sanity check for message size */
7967 if (sizeof (struct GNUNET_MESH_Multicast) +
7968 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7971 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7975 /* Tunnel exists? */
7976 tid = ntohl (data_msg->tid);
7977 t = tunnel_get_by_local_id (c, tid);
7981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7982 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7984 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7988 /* Does client own tunnel? */
7989 if (t->owner->handle != client)
7992 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7996 /* PID should be as expected */
7997 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
8000 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8001 "Multicast PID, expected %u, got %u\n",
8002 t->fwd_pid + 1, ntohl (data_msg->pid));
8003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8008 char buf[ntohs (message->size)] GNUNET_ALIGN;
8009 struct GNUNET_MESH_Multicast *copy;
8011 copy = (struct GNUNET_MESH_Multicast *) buf;
8012 memcpy (buf, message, ntohs (message->size));
8013 copy->oid = my_full_id;
8014 copy->tid = htonl (t->id.tid);
8015 copy->ttl = htonl (default_ttl);
8016 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
8017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8018 " calling generic handler...\n");
8019 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
8022 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
8028 * Handler for client's ACKs for payload traffic.
8030 * @param cls Closure (unused).
8031 * @param client Identification of the client.
8032 * @param message The actual message.
8035 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
8036 const struct GNUNET_MessageHeader *message)
8038 struct GNUNET_MESH_LocalAck *msg;
8039 struct MeshTunnel *t;
8040 struct MeshClient *c;
8041 MESH_TunnelNumber tid;
8044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
8045 /* Sanity check for client registration */
8046 if (NULL == (c = client_get (client)))
8049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
8054 msg = (struct GNUNET_MESH_LocalAck *) message;
8056 /* Tunnel exists? */
8057 tid = ntohl (msg->tunnel_id);
8058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
8059 t = tunnel_get_by_local_id (c, tid);
8063 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
8064 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
8065 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8069 ack = ntohl (msg->max_pid);
8070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
8072 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
8073 if (NULL != t->owner && t->owner->handle == client)
8075 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
8077 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8081 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
8082 tunnel_set_client_fwd_ack (t, c, ack);
8083 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
8086 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8093 * Iterator over all peers to send a monitoring client info about a tunnel.
8095 * @param cls Closure (message being built).
8096 * @param key Key (hashed tunnel ID, unused).
8097 * @param value Peer info.
8099 * @return GNUNET_YES, to keep iterating.
8102 monitor_peers_iterator (void *cls,
8103 const struct GNUNET_HashCode * key,
8106 struct GNUNET_MESH_LocalMonitor *msg = cls;
8107 struct GNUNET_PeerIdentity *id;
8108 struct MeshPeerInfo *info = value;
8110 id = (struct GNUNET_PeerIdentity *) &msg[1];
8111 GNUNET_PEER_resolve (info->id, &id[msg->npeers]);
8114 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8115 "* sending info about peer %s [%u]\n",
8116 GNUNET_i2s (&id[msg->npeers - 1]), msg->npeers);
8124 * Iterator over all tunnels to send a monitoring client info about each tunnel.
8126 * @param cls Closure (client handle).
8127 * @param key Key (hashed tunnel ID, unused).
8128 * @param value Tunnel info.
8130 * @return GNUNET_YES, to keep iterating.
8133 monitor_all_tunnels_iterator (void *cls,
8134 const struct GNUNET_HashCode * key,
8137 struct GNUNET_SERVER_Client *client = cls;
8138 struct MeshTunnel *t = value;
8139 struct GNUNET_MESH_LocalMonitor *msg;
8142 npeers = GNUNET_CONTAINER_multihashmap_size (t->peers);
8143 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor) +
8144 npeers * sizeof (struct GNUNET_PeerIdentity));
8145 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
8146 msg->tunnel_id = htonl (t->id.tid);
8147 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor) +
8148 npeers * sizeof (struct GNUNET_PeerIdentity));
8149 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
8151 (void) GNUNET_CONTAINER_multihashmap_iterate (t->peers,
8152 monitor_peers_iterator,
8155 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8156 "* sending info about tunnel %s [%u] (%u peers)\n",
8157 GNUNET_i2s (&msg->owner), t->id.tid, npeers);
8159 if (msg->npeers != npeers)
8162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8163 "Get tunnels fail: size %u - iter %u\n",
8164 npeers, msg->npeers);
8167 msg->npeers = htonl (npeers);
8168 GNUNET_SERVER_notification_context_unicast (nc, client,
8169 &msg->header, GNUNET_NO);
8175 * Handler for client's MONITOR request.
8177 * @param cls Closure (unused).
8178 * @param client Identification of the client.
8179 * @param message The actual message.
8182 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
8183 const struct GNUNET_MessageHeader *message)
8185 struct MeshClient *c;
8187 /* Sanity check for client registration */
8188 if (NULL == (c = client_get (client)))
8191 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8195 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8196 "Received get tunnels request from client %u\n",
8198 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
8199 monitor_all_tunnels_iterator,
8201 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8202 "Get tunnels request from client %u completed\n",
8204 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8209 * Data needed to build a Monitor_Tunnel message.
8211 struct MeshMonitorTunnelContext
8214 * Partial message, including peer count.
8216 struct GNUNET_MESH_LocalMonitor *msg;
8219 * Hashmap with positions: peer->position.
8221 struct GNUNET_CONTAINER_MultiHashMap *lookup;
8224 * Index of the parent of each peer in the message, realtive to the absolute
8225 * order in the array (can be in a previous message).
8227 uint32_t parents[1024];
8230 * Peers visited so far in the tree, aka position of the current peer.
8232 unsigned int npeers;
8235 * Client requesting the info.
8237 struct MeshClient *c;
8242 * Send a client a message about the structure of a tunnel.
8244 * @param ctx Context of the tunnel iteration, with info regarding the state
8245 * of the execution and the number of peers visited for this message.
8248 send_client_tunnel_info (struct MeshMonitorTunnelContext *ctx)
8250 struct GNUNET_MESH_LocalMonitor *resp = ctx->msg;
8251 struct GNUNET_PeerIdentity *pid;
8252 unsigned int *parent;
8255 size = sizeof (struct GNUNET_MESH_LocalMonitor);
8256 size += (sizeof (struct GNUNET_PeerIdentity) + sizeof (int)) * resp->npeers;
8257 resp->header.size = htons (size);
8258 pid = (struct GNUNET_PeerIdentity *) &resp[1];
8259 parent = (unsigned int *) &pid[resp->npeers];
8260 memcpy (parent, ctx->parents, sizeof(uint32_t) * resp->npeers);
8261 GNUNET_SERVER_notification_context_unicast (nc, ctx->c->handle,
8262 &resp->header, GNUNET_NO);
8266 * Iterator over a tunnel tree to build a message containing all peers
8267 * the in the tunnel, including relay nodes.
8269 * @param cls Closure (pointer to pointer of message being built).
8270 * @param peer Short ID of a peer.
8271 * @param parent Short ID of the @c peer 's parent.
8274 tunnel_tree_iterator (void *cls,
8275 GNUNET_PEER_Id peer,
8276 GNUNET_PEER_Id parent)
8278 struct MeshMonitorTunnelContext *ctx = cls;
8279 struct GNUNET_MESH_LocalMonitor *msg;
8280 struct GNUNET_PeerIdentity *pid;
8281 struct GNUNET_PeerIdentity ppid;
8284 pid = (struct GNUNET_PeerIdentity *) &msg[1];
8285 GNUNET_PEER_resolve (peer, &pid[msg->npeers]);
8286 GNUNET_CONTAINER_multihashmap_put (ctx->lookup,
8287 &pid[msg->npeers].hashPubKey,
8288 (void *) (long) ctx->npeers,
8289 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
8290 GNUNET_PEER_resolve (parent, &ppid);
8291 ctx->parents[msg->npeers] =
8292 htonl ((long) GNUNET_CONTAINER_multihashmap_get (ctx->lookup,
8298 if (sizeof (struct GNUNET_MESH_LocalMonitor) +
8300 (sizeof (struct GNUNET_PeerIdentity) + sizeof (uint32_t))
8303 send_client_tunnel_info (ctx);
8310 * Handler for client's MONITOR_TUNNEL request.
8312 * @param cls Closure (unused).
8313 * @param client Identification of the client.
8314 * @param message The actual message.
8317 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
8318 const struct GNUNET_MessageHeader *message)
8320 const struct GNUNET_MESH_LocalMonitor *msg;
8321 struct GNUNET_MESH_LocalMonitor *resp;
8322 struct MeshMonitorTunnelContext ctx;
8323 struct MeshClient *c;
8324 struct MeshTunnel *t;
8326 /* Sanity check for client registration */
8327 if (NULL == (c = client_get (client)))
8330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
8334 msg = (struct GNUNET_MESH_LocalMonitor *) message;
8335 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8336 "Received tunnel info request from client %u for tunnel %s[%X]\n",
8339 ntohl (msg->tunnel_id));
8340 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
8343 /* We don't know the tunnel */
8344 struct GNUNET_MESH_LocalMonitor warn;
8347 warn.npeers = htonl (UINT_MAX);
8348 GNUNET_SERVER_notification_context_unicast (nc, client,
8351 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8355 /* Initialize context */
8356 resp = GNUNET_malloc (USHRT_MAX); /* avoid realloc'ing on each step */
8360 ctx.lookup = GNUNET_CONTAINER_multihashmap_create (4 * t->peers_total,
8364 /* Collect and send information */
8365 tree_iterate_all (t->tree, &tunnel_tree_iterator, &ctx);
8366 send_client_tunnel_info (&ctx);
8369 GNUNET_CONTAINER_multihashmap_destroy (ctx.lookup);
8372 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8373 "Monitor tunnel request from client %u completed\n",
8375 GNUNET_SERVER_receive_done (client, GNUNET_OK);
8380 * Functions to handle messages from clients
8382 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
8383 {&handle_local_new_client, NULL,
8384 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
8385 {&handle_local_announce_regex, NULL,
8386 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
8387 {&handle_local_tunnel_create, NULL,
8388 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
8389 sizeof (struct GNUNET_MESH_TunnelMessage)},
8390 {&handle_local_tunnel_destroy, NULL,
8391 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
8392 sizeof (struct GNUNET_MESH_TunnelMessage)},
8393 {&handle_local_tunnel_speed, NULL,
8394 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
8395 sizeof (struct GNUNET_MESH_TunnelMessage)},
8396 {&handle_local_tunnel_speed, NULL,
8397 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
8398 sizeof (struct GNUNET_MESH_TunnelMessage)},
8399 {&handle_local_tunnel_buffer, NULL,
8400 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
8401 sizeof (struct GNUNET_MESH_TunnelMessage)},
8402 {&handle_local_tunnel_buffer, NULL,
8403 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
8404 sizeof (struct GNUNET_MESH_TunnelMessage)},
8405 {&handle_local_connect_add, NULL,
8406 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
8407 sizeof (struct GNUNET_MESH_PeerControl)},
8408 {&handle_local_connect_del, NULL,
8409 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
8410 sizeof (struct GNUNET_MESH_PeerControl)},
8411 {&handle_local_blacklist, NULL,
8412 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
8413 sizeof (struct GNUNET_MESH_PeerControl)},
8414 {&handle_local_unblacklist, NULL,
8415 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
8416 sizeof (struct GNUNET_MESH_PeerControl)},
8417 {&handle_local_connect_by_type, NULL,
8418 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
8419 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
8420 {&handle_local_connect_by_string, NULL,
8421 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
8422 {&handle_local_unicast, NULL,
8423 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
8424 {&handle_local_to_origin, NULL,
8425 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
8426 {&handle_local_multicast, NULL,
8427 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
8428 {&handle_local_ack, NULL,
8429 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
8430 sizeof (struct GNUNET_MESH_LocalAck)},
8431 {&handle_local_get_tunnels, NULL,
8432 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
8433 sizeof (struct GNUNET_MessageHeader)},
8434 {&handle_local_show_tunnel, NULL,
8435 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
8436 sizeof (struct GNUNET_MESH_LocalMonitor)},
8442 * To be called on core init/fail.
8444 * @param cls service closure
8445 * @param server handle to the server for this service
8446 * @param identity the public identity of this peer
8449 core_init (void *cls, struct GNUNET_CORE_Handle *server,
8450 const struct GNUNET_PeerIdentity *identity)
8453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
8454 core_handle = server;
8455 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
8458 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
8459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8461 GNUNET_i2s (identity));
8462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8464 GNUNET_i2s (&my_full_id));
8465 GNUNET_SCHEDULER_shutdown (); // Try gracefully
8467 GNUNET_abort(); // Try harder
8474 * Method called whenever a given peer connects.
8476 * @param cls closure
8477 * @param peer peer identity this notification is about
8478 * @param atsi performance data for the connection
8479 * @param atsi_count number of records in 'atsi'
8482 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
8483 const struct GNUNET_ATS_Information *atsi,
8484 unsigned int atsi_count)
8486 struct MeshPeerInfo *peer_info;
8487 struct MeshPeerPath *path;
8489 DEBUG_CONN ("Peer connected\n");
8490 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
8491 peer_info = peer_info_get (peer);
8492 if (myid == peer_info->id)
8494 DEBUG_CONN (" (self)\n");
8499 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
8501 path = path_new (2);
8502 path->peers[0] = myid;
8503 path->peers[1] = peer_info->id;
8504 GNUNET_PEER_change_rc (myid, 1);
8505 GNUNET_PEER_change_rc (peer_info->id, 1);
8506 peer_info_add_path (peer_info, path, GNUNET_YES);
8507 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
8513 * Method called whenever a peer disconnects.
8515 * @param cls closure
8516 * @param peer peer identity this notification is about
8519 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
8521 struct MeshPeerInfo *pi;
8522 struct MeshPeerQueue *q;
8523 struct MeshPeerQueue *n;
8525 DEBUG_CONN ("Peer disconnected\n");
8526 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
8536 /* TODO try to reroute this traffic instead */
8537 queue_destroy(q, GNUNET_YES);
8540 if (NULL != pi->core_transmit)
8542 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
8543 pi->core_transmit = NULL;
8545 peer_info_remove_path (pi, pi->id, myid);
8548 DEBUG_CONN (" (self)\n");
8550 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
8555 /******************************************************************************/
8556 /************************ MAIN FUNCTIONS ****************************/
8557 /******************************************************************************/
8560 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
8562 * @param cls closure
8563 * @param key current key code
8564 * @param value value in the hash map
8565 * @return GNUNET_YES if we should continue to iterate,
8569 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
8571 struct MeshTunnel *t = value;
8578 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
8580 * @param cls closure
8581 * @param key current key code
8582 * @param value value in the hash map
8583 * @return GNUNET_YES if we should continue to iterate,
8587 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
8589 struct MeshPeerInfo *p = value;
8590 struct MeshPeerQueue *q;
8591 struct MeshPeerQueue *n;
8599 queue_destroy(q, GNUNET_YES);
8603 peer_info_destroy (p);
8609 * Task run during shutdown.
8615 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
8617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
8619 if (core_handle != NULL)
8621 GNUNET_CORE_disconnect (core_handle);
8626 GNUNET_CRYPTO_rsa_key_create_stop (keygen);
8629 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
8630 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
8631 if (dht_handle != NULL)
8633 GNUNET_DHT_disconnect (dht_handle);
8638 GNUNET_SERVER_notification_context_destroy (nc);
8641 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
8643 GNUNET_SCHEDULER_cancel (announce_id_task);
8644 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
8646 if (GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
8648 GNUNET_SCHEDULER_cancel (announce_applications_task);
8649 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
8656 * Callback for hostkey read/generation
8658 * @param cls Closure (Configuration handle).
8659 * @param pk the private key
8660 * @param emsg error message
8663 key_generation_cb (void *cls,
8664 struct GNUNET_CRYPTO_RsaPrivateKey *pk,
8667 const struct GNUNET_CONFIGURATION_Handle *c = cls;
8668 struct MeshPeerInfo *peer;
8669 struct MeshPeerPath *p;
8674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8675 _("Mesh service could not access hostkey: %s. Exiting.\n"),
8677 GNUNET_SCHEDULER_shutdown ();
8680 my_private_key = pk;
8681 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
8682 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
8683 &my_full_id.hashPubKey);
8684 myid = GNUNET_PEER_intern (&my_full_id);
8685 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8686 "Mesh for peer [%s] starting\n",
8687 GNUNET_i2s(&my_full_id));
8689 // transport_handle = GNUNET_TRANSPORT_connect(c,
8696 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
8697 NULL, /* Closure passed to MESH functions */
8698 &core_init, /* Call core_init once connected */
8699 &core_connect, /* Handle connects */
8700 &core_disconnect, /* remove peers on disconnects */
8701 NULL, /* Don't notify about all incoming messages */
8702 GNUNET_NO, /* For header only in notification */
8703 NULL, /* Don't notify about all outbound messages */
8704 GNUNET_NO, /* For header-only out notification */
8705 core_handlers); /* Register these handlers */
8707 if (core_handle == NULL)
8710 GNUNET_SCHEDULER_shutdown ();
8715 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
8718 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
8719 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
8720 GNUNET_SERVER_disconnect_notify (server_handle,
8721 &handle_local_client_disconnect, NULL);
8725 clients_tail = NULL;
8728 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
8729 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
8731 /* Create a peer_info for the local peer */
8732 peer = peer_info_get (&my_full_id);
8735 GNUNET_PEER_change_rc (myid, 1);
8736 peer_info_add_path (peer, p, GNUNET_YES);
8737 GNUNET_SERVER_resume (server_handle);
8738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
8743 * Process mesh requests.
8745 * @param cls closure
8746 * @param server the initialized server
8747 * @param c configuration to use
8750 run (void *cls, struct GNUNET_SERVER_Handle *server,
8751 const struct GNUNET_CONFIGURATION_Handle *c)
8755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
8756 server_handle = server;
8759 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
8762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8764 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8766 GNUNET_SCHEDULER_shutdown ();
8771 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
8772 &refresh_path_time))
8774 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8776 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8777 "mesh", "refresh path time");
8778 GNUNET_SCHEDULER_shutdown ();
8783 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
8784 &app_announce_time))
8786 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8788 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8789 "mesh", "app announce time");
8790 GNUNET_SCHEDULER_shutdown ();
8793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
8794 "APP_ANNOUNCE_TIME %llu ms\n",
8795 app_announce_time.rel_value);
8797 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
8800 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8802 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8803 "mesh", "id announce time");
8804 GNUNET_SCHEDULER_shutdown ();
8809 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
8812 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8814 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8815 "mesh", "connect timeout");
8816 GNUNET_SCHEDULER_shutdown ();
8821 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
8824 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8826 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8827 "mesh", "max msgs queue");
8828 GNUNET_SCHEDULER_shutdown ();
8833 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
8836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
8838 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
8839 "mesh", "max tunnels");
8840 GNUNET_SCHEDULER_shutdown ();
8845 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
8848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8850 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8851 "mesh", "default ttl", 64);
8856 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
8859 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8860 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8861 "mesh", "max peers", 1000);
8866 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
8867 &dht_replication_level))
8869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
8871 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
8872 "mesh", "dht replication level", 3);
8873 dht_replication_level = 3;
8876 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8877 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8878 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8879 applications = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8880 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
8882 dht_handle = GNUNET_DHT_connect (c, 64);
8883 if (NULL == dht_handle)
8887 stats = GNUNET_STATISTICS_create ("mesh", c);
8889 GNUNET_SERVER_suspend (server_handle);
8890 /* Scheduled the task to clean up when shutdown is called */
8891 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
8893 keygen = GNUNET_CRYPTO_rsa_key_create_start (keyfile,
8896 GNUNET_free (keyfile);
8901 * The main function for the mesh service.
8903 * @param argc number of arguments from the command line
8904 * @param argv command line arguments
8905 * @return 0 ok, 1 on error
8908 main (int argc, char *const *argv)
8913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
8914 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
8916 ret = (GNUNET_OK == r) ? 0 : 1;
8917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
8921 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
8922 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
8923 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);