2 This file is part of GNUnet.
3 (C) 2001 - 2011 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?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
42 * - relay corking down to core
43 * - set ttl relative to tree depth
44 * - Add data ACK count in path ACK
45 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
51 #include "mesh_protocol.h"
52 #include "mesh_tunnel_tree.h"
53 #include "block_mesh.h"
54 #include "mesh_block_lib.h"
55 #include "gnunet_dht_service.h"
56 #include "gnunet_statistics_service.h"
57 #include "gnunet_regex_lib.h"
59 #define MESH_BLOOM_SIZE 128
61 #define MESH_DEBUG_DHT GNUNET_NO
62 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_YES
65 #if MESH_DEBUG_CONNECTION
66 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
68 #define DEBUG_CONN(...)
72 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
74 #define DEBUG_DHT(...)
81 struct timespec __mesh_start;
82 struct timespec __mesh_end;
83 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
84 #define INTERVAL_END \
86 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
87 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
88 if (__diff < 0) __diff += 1000000000;\
92 #define INTERVAL_SHOW \
94 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
96 #define INTERVAL_START
101 /******************************************************************************/
102 /************************ DATA STRUCTURES ****************************/
103 /******************************************************************************/
105 /** FWD declaration */
111 * Struct representing a piece of data being sent to other peers
115 /** Tunnel it belongs to. */
116 struct MeshTunnel *t;
118 /** How many remaining neighbors still hav't got it. */
119 unsigned int reference_counter;
121 /** How many remaining neighbors we need to send this to. */
122 unsigned int total_out;
124 /** Size of the data. */
133 * Struct containing info about a queued transmission to this peer
140 struct MeshPeerQueue *next;
145 struct MeshPeerQueue *prev;
148 * Peer this transmission is directed to.
150 struct MeshPeerInfo *peer;
153 * Tunnel this message belongs to.
155 struct MeshTunnel *tunnel;
158 * Pointer to info stucture used as cls.
168 * Size of the message
175 * Struct containing all info possibly needed to build a package when called
178 struct MeshTransmissionDescriptor
180 /** ID of the tunnel this packet travels in */
181 struct MESH_TunnelID *origin;
183 /** Who was this message being sent to */
184 struct MeshPeerInfo *peer;
186 /** Ultimate destination of the packet */
187 GNUNET_PEER_Id destination;
189 /** Data descriptor */
190 struct MeshData* mesh_data;
195 * Struct containing all information regarding a given peer
205 * Last time we heard from this peer
207 struct GNUNET_TIME_Absolute last_contact;
210 * Number of attempts to reconnect so far
212 int n_reconnect_attempts;
215 * Paths to reach the peer, ordered by ascending hop count
217 struct MeshPeerPath *path_head;
220 * Paths to reach the peer, ordered by ascending hop count
222 struct MeshPeerPath *path_tail;
225 * Handle to stop the DHT search for a path to this peer
227 struct GNUNET_DHT_GetHandle *dhtget;
230 * Closure given to the DHT GET
232 struct MeshPathInfo *dhtgetcls;
235 * Array of tunnels this peer participates in
236 * (most probably a small amount, therefore not a hashmap)
237 * When the path to the peer changes, notify these tunnels to let them
238 * re-adjust their path trees.
240 struct MeshTunnel **tunnels;
243 * Number of tunnels this peers participates in
245 unsigned int ntunnels;
248 * Transmission queue to core DLL head
250 struct MeshPeerQueue *queue_head;
253 * Transmission queue to core DLL tail
255 struct MeshPeerQueue *queue_tail;
258 * How many messages are in the queue to this peer.
260 unsigned int queue_n;
263 * Handle to for queued transmissions
265 struct GNUNET_CORE_TransmitHandle *core_transmit;
270 * Globally unique tunnel identification (owner + number)
271 * DO NOT USE OVER THE NETWORK
276 * Node that owns the tunnel
281 * Tunnel number to differentiate all the tunnels owned by the node oid
282 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
284 MESH_TunnelNumber tid;
289 * Struct containing all information regarding a tunnel
290 * For an intermediate node the improtant info used will be:
291 * - id Tunnel unique identification
292 * - paths[0] To know where to send it next
293 * - metainfo: ready, speeds, accounting
300 struct MESH_TunnelID id;
303 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
305 MESH_TunnelNumber local_tid;
308 * Local tunnel number for local destination clients (incoming number)
309 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
312 MESH_TunnelNumber local_tid_dest;
315 * Is the speed on the tunnel limited to the slowest peer?
320 * Is the tunnel bufferless (minimum latency)?
325 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
330 * Packet ID of the last bck packet sent (unique counter per hop).
335 * SKIP value for this tunnel.
340 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
341 * Contains the Flow Control info: FWD ACK value received,
342 * last BCK ACK sent, PID and SKIP values.
344 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
347 * Last ACK sent towards the origin (for traffic towards leaf node).
349 uint32_t last_fwd_ack;
352 * BCK ACK value received from the hop towards the owner of the tunnel,
353 * (previous node / owner): up to what message PID can we sent back to him.
358 * How many messages are in the forward queue (towards leaves).
360 unsigned int fwd_queue_n;
363 * How many messages do we accept in the forward queue.
365 unsigned int fwd_queue_max;
368 * How many messages are in the backward queue (towards origin).
370 unsigned int bck_queue_n;
373 * How many messages do we accept in the backward queue.
375 unsigned int bck_queue_max;
378 * Last time the tunnel was used
380 struct GNUNET_TIME_Absolute timestamp;
383 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
384 * containing peers added by id or by type, not intermediate peers.
386 struct GNUNET_CONTAINER_MultiHashMap *peers;
389 * Number of peers that are connected and potentially ready to receive data
391 unsigned int peers_ready;
394 * Number of peers that have been added to the tunnel
396 unsigned int peers_total;
399 * Client owner of the tunnel, if any
401 struct MeshClient *owner;
404 * Clients that have been informed about and want to stay in the tunnel.
406 struct MeshClient **clients;
409 * Flow control info for each client.
411 struct MeshTunnelClientInfo *clients_fc;
414 * Number of elements in clients/clients_fc
416 unsigned int nclients;
419 * Clients that have been informed but requested to leave the tunnel.
421 struct MeshClient **ignore;
424 * Number of elements in clients
426 unsigned int nignore;
431 GNUNET_PEER_Id *blacklisted;
434 * Number of elements in blacklisted
436 unsigned int nblacklisted;
439 * Bloomfilter (for peer identities) to stop circular routes
441 char bloomfilter[MESH_BLOOM_SIZE];
446 struct MeshTunnelTree *tree;
449 * Application type we are looking for in this tunnel
451 GNUNET_MESH_ApplicationType type;
454 * Used to search peers offering a service
456 struct GNUNET_DHT_GetHandle *dht_get_type;
459 * Initial context of the regex search for a connect_by_string
461 struct MeshRegexSearchContext *regex_ctx;
464 * Task to keep the used paths alive
466 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
469 * Task to destroy the tunnel after timeout
471 * FIXME: merge the two? a tunnel will have either
472 * a path refresh OR a timeout, never both!
474 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
477 * Flag to signal the destruction of the tunnel.
478 * If this is set GNUNET_YES the tunnel will be destroyed
479 * when the queue is empty.
486 * Info about a child node in a tunnel, needed to perform flow control.
488 struct MeshTunnelChildInfo
491 * ID of the child node.
506 * Maximum PID allowed (FWD ACK received).
511 * Last ACK sent to that child (BCK ACK).
516 * Circular buffer pointing to MeshPeerQueue elements.
517 * Size determined by the tunnel queue size.
519 struct MeshPeerQueue **send_buffer;
522 * Index of the oldest element in the send_buffer.
524 unsigned int send_buffer_start;
527 * How many elements are already in the buffer.
529 unsigned int send_buffer_n;
534 * Info about a leaf client of a tunnel, needed to perform flow control.
536 struct MeshTunnelClientInfo
539 * PID of the last packet sent to the client (FWD).
544 * PID of the last packet received from the client (BCK).
549 * Maximum PID allowed (FWD ACK received).
554 * Last ACK sent to that child (BCK ACK).
562 * Info collected during iteration of child nodes in order to get the ACK value
565 struct MeshTunnelChildIteratorContext
568 * Tunnel whose info is being collected.
570 struct MeshTunnel *t;
573 * Is this context initialized? Is the value in max_child_ack valid?
578 * Maximum child ACK so far.
580 uint32_t max_child_ack;
583 * Number of children nodes
585 unsigned int nchildren;
590 * Info needed to work with tunnel paths and peers
597 struct MeshTunnel *t;
600 * Neighbouring peer to whom we send the packet to
602 struct MeshPeerInfo *peer;
607 struct MeshPeerPath *path;
612 * Struct containing information about a client of the service
619 struct MeshClient *next;
624 struct MeshClient *prev;
627 * Tunnels that belong to this client, indexed by local id
629 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
632 * Tunnels this client has accepted, indexed by incoming local id
634 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
637 * Tunnels this client has rejected, indexed by incoming local id
639 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
641 * Handle to communicate with the client
643 struct GNUNET_SERVER_Client *handle;
646 * Applications that this client has claimed to provide
648 struct GNUNET_CONTAINER_MultiHashMap *apps;
651 * Messages that this client has declared interest in
653 struct GNUNET_CONTAINER_MultiHashMap *types;
656 * Whether the client is active or shutting down (don't send confirmations
657 * to a client that is shutting down.
662 * ID of the client, mainly for debug messages
667 * Regular expressions describing the services offered by this client.
669 char **regexes; // FIXME add timeout? API to remove a regex?
672 * Number of regular expressions in regexes.
674 unsigned int n_regex;
677 * Task to refresh all regular expresions in the DHT.
679 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
685 * Struct to keep information of searches of services described by a regex
686 * using a user-provided string service description.
688 struct MeshRegexSearchInfo
691 * Which tunnel is this for
693 struct MeshTunnel *t;
696 * User provided description of the searched service.
701 * Part of the description already consumed by the search.
708 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
711 * Results from running DHT GETs.
713 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
716 * Contexts, for each running DHT GET. Free all on end of search.
718 struct MeshRegexSearchContext **contexts;
721 * Number of contexts (branches/steps in search).
723 unsigned int n_contexts;
726 * Peer that is connecting via connect_by_string. When connected, free ctx.
731 * Other peers that are found but not yet being connected to.
733 GNUNET_PEER_Id *peers;
736 * Number of elements in peers.
738 unsigned int n_peers;
741 * Next peer to try to connect to.
746 * Timeout for a connect attempt.
747 * When reached, try to connect to a different peer, if any. If not,
748 * try the same peer again.
750 GNUNET_SCHEDULER_TaskIdentifier timeout;
755 * Struct to keep state of running searches that have consumed a part of
758 struct MeshRegexSearchContext
761 * Part of the description already consumed by
762 * this particular search branch.
767 * Information about the search.
769 struct MeshRegexSearchInfo *info;
773 /******************************************************************************/
774 /************************ DEBUG FUNCTIONS ****************************/
775 /******************************************************************************/
779 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
780 * @param cls string to print
781 * @param success GNUNET_OK if the PUT was transmitted,
782 * GNUNET_NO on timeout,
783 * GNUNET_SYSERR on disconnect from service
784 * after the PUT message was transmitted
785 * (so we don't know if it was received or not)
790 mesh_debug (void *cls, int success)
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
798 unsigned int debug_fwd_ack;
799 unsigned int debug_bck_ack;
803 /******************************************************************************/
804 /*********************** GLOBAL VARIABLES ****************************/
805 /******************************************************************************/
808 * Configuration parameters
810 static struct GNUNET_TIME_Relative refresh_path_time;
811 static struct GNUNET_TIME_Relative app_announce_time;
812 static struct GNUNET_TIME_Relative id_announce_time;
813 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
814 static struct GNUNET_TIME_Relative connect_timeout;
815 static long long unsigned int default_ttl;
816 static long long unsigned int dht_replication_level;
817 static long long unsigned int max_tunnels;
818 static long long unsigned int max_msgs_queue;
821 * DLL with all the clients, head.
823 static struct MeshClient *clients;
826 * DLL with all the clients, tail.
828 static struct MeshClient *clients_tail;
831 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
833 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
836 * Number of tunnels known.
838 static unsigned long long n_tunnels;
841 * Tunnels incoming, indexed by MESH_TunnelNumber
842 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
844 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
847 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
849 static struct GNUNET_CONTAINER_MultiHashMap *peers;
852 * Handle to communicate with transport
854 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
857 * Handle to communicate with core.
859 static struct GNUNET_CORE_Handle *core_handle;
864 static struct GNUNET_DHT_Handle *dht_handle;
869 static struct GNUNET_SERVER_Handle *server_handle;
872 * Handle to the statistics service.
874 static struct GNUNET_STATISTICS_Handle *stats;
877 * Notification context, to send messages to local clients.
879 static struct GNUNET_SERVER_NotificationContext *nc;
882 * Local peer own ID (memory efficient handle).
884 static GNUNET_PEER_Id myid;
887 * Local peer own ID (full value).
889 static struct GNUNET_PeerIdentity my_full_id;
894 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
899 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
902 * Tunnel ID for the next created tunnel (global tunnel number).
904 static MESH_TunnelNumber next_tid;
907 * Tunnel ID for the next incoming tunnel (local tunnel number).
909 static MESH_TunnelNumber next_local_tid;
912 * All application types provided by this peer.
914 static struct GNUNET_CONTAINER_MultiHashMap *applications;
917 * All message types clients of this peer are interested in.
919 static struct GNUNET_CONTAINER_MultiHashMap *types;
922 * Task to periodically announce provided applications.
924 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
927 * Task to periodically announce itself in the network.
929 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
932 * Next ID to assign to a client.
934 unsigned int next_client_id;
937 /******************************************************************************/
938 /*********************** DECLARATIONS **************************/
939 /******************************************************************************/
941 /* FIXME move declarations here */
944 * Function to process paths received for a new peer addition. The recorded
945 * paths form the initial tunnel, which can be optimized later.
946 * Called on each result obtained for the DHT search.
949 * @param exp when will this value expire
950 * @param key key of the result
951 * @param type type of the result
952 * @param size number of bytes in data
953 * @param data pointer to the result data
956 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
957 const struct GNUNET_HashCode * key,
958 const struct GNUNET_PeerIdentity *get_path,
959 unsigned int get_path_length,
960 const struct GNUNET_PeerIdentity *put_path,
961 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
962 size_t size, const void *data);
966 * Function to process DHT string to regex matching.
967 * Called on each result obtained for the DHT search.
969 * @param cls closure (search context)
970 * @param exp when will this value expire
971 * @param key key of the result
972 * @param get_path path of the get request (not used)
973 * @param get_path_length lenght of get_path (not used)
974 * @param put_path path of the put request (not used)
975 * @param put_path_length length of the put_path (not used)
976 * @param type type of the result
977 * @param size number of bytes in data
978 * @param data pointer to the result data
980 * TODO: re-issue the request after certain time? cancel after X results?
983 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
984 const struct GNUNET_HashCode * key,
985 const struct GNUNET_PeerIdentity *get_path,
986 unsigned int get_path_length,
987 const struct GNUNET_PeerIdentity *put_path,
988 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
989 size_t size, const void *data);
993 * Function to process DHT string to regex matching.
994 * Called on each result obtained for the DHT search.
996 * @param cls closure (search context)
997 * @param exp when will this value expire
998 * @param key key of the result
999 * @param get_path path of the get request (not used)
1000 * @param get_path_length lenght of get_path (not used)
1001 * @param put_path path of the put request (not used)
1002 * @param put_path_length length of the put_path (not used)
1003 * @param type type of the result
1004 * @param size number of bytes in data
1005 * @param data pointer to the result data
1008 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1009 const struct GNUNET_HashCode * key,
1010 const struct GNUNET_PeerIdentity *get_path,
1011 unsigned int get_path_length,
1012 const struct GNUNET_PeerIdentity *put_path,
1013 unsigned int put_path_length,
1014 enum GNUNET_BLOCK_Type type,
1015 size_t size, const void *data);
1019 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1020 * and insert it in the appropiate structures if the peer is not known yet.
1022 * @param peer Short identity of the peer.
1024 * @return Existing or newly created peer info.
1026 static struct MeshPeerInfo *
1027 peer_info_get_short (const GNUNET_PEER_Id peer);
1031 * Try to establish a new connection to this peer.
1032 * Use the best path for the given tunnel.
1033 * If the peer doesn't have any path to it yet, try to get one.
1034 * If the peer already has some path, send a CREATE PATH towards it.
1036 * @param peer PeerInfo of the peer.
1037 * @param t Tunnel for which to create the path, if possible.
1040 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1044 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1045 * needed rescources.
1046 * If peer already exists, reevaluate shortest path and change if different.
1048 * @param t Tunnel we want to add a new peer to
1049 * @param peer PeerInfo of the peer being added
1053 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1057 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1058 * that are no longer needed, as well as nodes of no longer reachable peers.
1059 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1061 * @param t Tunnel from which to remove the path.
1062 * @param peer Short id of the peer which should be removed.
1065 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1069 * Search for a tunnel by global ID using full PeerIdentities.
1071 * @param oid owner of the tunnel.
1072 * @param tid global tunnel number.
1074 * @return tunnel handler, NULL if doesn't exist.
1076 static struct MeshTunnel *
1077 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1081 * Delete an active client from the tunnel.
1087 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1090 * Notify a tunnel that a connection has broken that affects at least
1091 * some of its peers.
1093 * @param t Tunnel affected.
1094 * @param p1 Peer that got disconnected from p2.
1095 * @param p2 Peer that got disconnected from p1.
1097 * @return Short ID of the peer disconnected (either p1 or p2).
1098 * 0 if the tunnel remained unaffected.
1100 static GNUNET_PEER_Id
1101 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1106 * Get the current ack value for a tunnel, for data going from root to leaves,
1107 * taking in account the tunnel mode and the status of all children and clients.
1111 * @return Maximum PID allowed.
1114 tunnel_get_fwd_ack (struct MeshTunnel *t);
1118 * Add a client to a tunnel, initializing all needed data structures.
1120 * @param t Tunnel to which add the client.
1121 * @param c Client which to add to the tunnel.
1124 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1128 * Iterator over edges in a regex block retrieved from the DHT.
1130 * @param cls Closure.
1131 * @param token Token that follows to next state.
1132 * @param len Lenght of token.
1133 * @param key Hash of next state.
1135 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1138 regex_edge_iterator (void *cls,
1141 const struct GNUNET_HashCode *key);
1145 * Find a path to a peer that offers a regex servcie compatible
1146 * with a given string.
1148 * @param key The key of the accepting state.
1149 * @param ctx Context containing info about the string, tunnel, etc.
1152 regex_find_path (const struct GNUNET_HashCode *key,
1153 struct MeshRegexSearchContext *ctx);
1157 * Queue and pass message to core when possible.
1159 * @param cls Closure (type dependant).
1160 * @param type Type of the message, 0 for a raw message.
1161 * @param size Size of the message.
1162 * @param dst Neighbor to send message to.
1163 * @param t Tunnel this message belongs to.
1166 queue_add (void *cls, uint16_t type, size_t size,
1167 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1171 * Free a transmission that was already queued with all resources
1172 * associated to the request.
1174 * @param queue Queue handler to cancel.
1175 * @param clear_cls Is it necessary to free associated cls?
1178 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1182 * @brief Get the next transmittable message from the queue.
1184 * This will be the head, except in the case of being a data packet
1185 * not allowed by the destination peer.
1187 * @param peer Destination peer.
1189 * @return The next viable MeshPeerQueue element to send to that peer.
1190 * NULL when there are no transmittable messages.
1192 struct MeshPeerQueue *
1193 queue_get_next (const struct MeshPeerInfo *peer);
1197 * Core callback to write a queued packet to core buffer
1199 * @param cls Closure (peer info).
1200 * @param size Number of bytes available in buf.
1201 * @param buf Where the to write the message.
1203 * @return number of bytes written to buf
1206 queue_send (void *cls, size_t size, void *buf);
1208 /******************************************************************************/
1209 /************************ ITERATORS ****************************/
1210 /******************************************************************************/
1213 * Iterator over found existing mesh regex blocks that match an ongoing search.
1215 * @param cls closure
1216 * @param key current key code
1217 * @param value value in the hash map
1218 * @return GNUNET_YES if we should continue to iterate,
1222 regex_result_iterator (void *cls,
1223 const struct GNUNET_HashCode * key,
1226 struct MeshRegexBlock *block = value;
1227 struct MeshRegexSearchContext *ctx = cls;
1229 if (GNUNET_YES == ntohl(block->accepting) &&
1230 ctx->position == strlen (ctx->info->description))
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1233 regex_find_path (key, ctx);
1234 return GNUNET_YES; // We found an accept state!
1238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1239 ctx->position, strlen(ctx->info->description),
1240 ntohl(block->accepting));
1243 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1244 ®ex_edge_iterator, ctx);
1251 * Iterator over edges in a regex block retrieved from the DHT.
1253 * @param cls Closure (context of the search).
1254 * @param token Token that follows to next state.
1255 * @param len Lenght of token.
1256 * @param key Hash of next state.
1258 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1261 regex_edge_iterator (void *cls,
1264 const struct GNUNET_HashCode *key)
1266 struct MeshRegexSearchContext *ctx = cls;
1267 struct MeshRegexSearchContext *new_ctx;
1268 struct MeshRegexSearchInfo *info = ctx->info;
1269 struct GNUNET_DHT_GetHandle *get_h;
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1276 current = &info->description[ctx->position];
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1278 current_len = strlen (info->description) - ctx->position;
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1283 if (len > current_len)
1285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1286 return GNUNET_YES; // Token too long, wont match
1288 if (0 != strncmp (current, token, len))
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1291 return GNUNET_YES; // Token doesn't match
1293 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1294 new_ctx->info = info;
1295 new_ctx->position = ctx->position + len;
1296 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1298 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1301 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1302 ®ex_result_iterator,
1304 return GNUNET_YES; // We are already looking for it
1306 /* Start search in DHT */
1308 GNUNET_DHT_get_start (dht_handle, /* handle */
1309 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1310 key, /* key to search */
1311 dht_replication_level, /* replication level */
1312 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1313 NULL, /* xquery */ // FIXME BLOOMFILTER
1314 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1315 &dht_get_string_handler, new_ctx);
1317 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1318 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1328 * Iterator over hash map entries to cancel DHT GET requests after a
1329 * successful connect_by_string.
1331 * @param cls Closure (unused).
1332 * @param key Current key code (unused).
1333 * @param value Value in the hash map (get handle).
1334 * @return GNUNET_YES if we should continue to iterate,
1338 regex_cancel_dht_get (void *cls,
1339 const struct GNUNET_HashCode * key,
1342 struct GNUNET_DHT_GetHandle *h = value;
1344 GNUNET_DHT_get_stop (h);
1350 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1351 * search for connect_by_string.
1353 * @param cls Closure (unused).
1354 * @param key Current key code (unused).
1355 * @param value MeshRegexBlock in the hash map.
1356 * @return GNUNET_YES if we should continue to iterate,
1360 regex_free_result (void *cls,
1361 const struct GNUNET_HashCode * key,
1365 GNUNET_free (value);
1371 * Regex callback iterator to store own service description in the DHT.
1373 * @param cls closure.
1374 * @param key hash for current state.
1375 * @param proof proof for current state.
1376 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1377 * @param num_edges number of edges leaving current state.
1378 * @param edges edges leaving current state.
1381 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1382 int accepting, unsigned int num_edges,
1383 const struct GNUNET_REGEX_Edge *edges)
1385 struct MeshRegexBlock *block;
1386 struct MeshRegexEdge *block_edge;
1387 enum GNUNET_DHT_RouteOption opt;
1391 unsigned int offset;
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 " regex dht put for state %s\n",
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1404 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1405 if (GNUNET_YES == accepting)
1407 struct MeshRegexAccept block;
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1410 " state %s is accepting, putting own id\n",
1412 size = sizeof (block);
1414 block.id = my_full_id;
1416 GNUNET_DHT_put(dht_handle, key,
1417 dht_replication_level,
1418 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1419 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1422 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1427 len = strlen(proof);
1428 size = sizeof (struct MeshRegexBlock) + len;
1429 block = GNUNET_malloc (size);
1432 block->n_proof = htonl (len);
1433 block->n_edges = htonl (num_edges);
1434 block->accepting = htonl (accepting);
1436 /* Store the proof at the end of the block. */
1437 aux = (char *) &block[1];
1438 memcpy (aux, proof, len);
1441 /* Store each edge in a variable length MeshEdge struct at the
1442 * very end of the MeshRegexBlock structure.
1444 for (i = 0; i < num_edges; i++)
1446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1447 " edge %s towards %s\n",
1449 GNUNET_h2s(&edges[i].destination));
1451 /* aux points at the end of the last block */
1452 len = strlen (edges[i].label);
1453 size += sizeof (struct MeshRegexEdge) + len;
1454 // Calculate offset FIXME is this ok? use size instead?
1455 offset = aux - (char *) block;
1456 block = GNUNET_realloc (block, size);
1457 aux = &((char *) block)[offset];
1458 block_edge = (struct MeshRegexEdge *) aux;
1459 block_edge->key = edges[i].destination;
1460 block_edge->n_token = htonl (len);
1461 aux = (char *) &block_edge[1];
1462 memcpy (aux, edges[i].label, len);
1466 GNUNET_DHT_put(dht_handle, key,
1467 dht_replication_level,
1469 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1471 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1475 GNUNET_free (block);
1480 * Store the regular expression describing a local service into the DHT.
1482 * @param regex The regular expresion.
1485 regex_put (const char *regex)
1487 struct GNUNET_REGEX_Automaton *dfa;
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1490 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1491 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1492 GNUNET_REGEX_automaton_destroy (dfa);
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1498 * Find a path to a peer that offers a regex servcie compatible
1499 * with a given string.
1501 * @param key The key of the accepting state.
1502 * @param ctx Context containing info about the string, tunnel, etc.
1505 regex_find_path (const struct GNUNET_HashCode *key,
1506 struct MeshRegexSearchContext *ctx)
1508 struct GNUNET_DHT_GetHandle *get_h;
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1511 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1512 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1513 key, /* key to search */
1514 dht_replication_level, /* replication level */
1515 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1516 GNUNET_DHT_RO_RECORD_ROUTE,
1517 NULL, /* xquery */ // FIXME BLOOMFILTER
1518 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1519 &dht_get_string_accept_handler, ctx);
1520 GNUNET_break (GNUNET_OK ==
1521 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1524 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1529 * Function called if the connect attempt to a peer found via
1530 * connect_by_string times out. Try to connect to another peer, if any.
1531 * Otherwise try to reconnect to the same peer.
1533 * @param cls Closure (info about regex search).
1534 * @param tc TaskContext.
1537 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1539 struct MeshRegexSearchInfo *info = cls;
1540 struct MeshPeerInfo *peer_info;
1544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1545 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1546 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1554 if (0 < info->n_peers)
1556 // Select next peer, put current in that spot.
1557 id = info->peers[info->i_peer];
1558 info->peers[info->i_peer] = info->peer;
1559 info->i_peer = (info->i_peer + 1) % info->n_peers;
1563 // Try to connect to same peer again.
1566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1568 peer_info = peer_info_get_short(id);
1569 tunnel_add_peer (info->t, peer_info);
1571 tunnel_delete_peer (info->t, old);
1572 peer_info_connect (peer_info, info->t);
1573 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1574 ®ex_connect_timeout,
1576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1581 * Cancel an ongoing regex search in the DHT and free all resources.
1583 * @param ctx The search context.
1586 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1588 struct MeshRegexSearchInfo *info = ctx->info;
1591 GNUNET_free (info->description);
1592 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1593 ®ex_cancel_dht_get, NULL);
1594 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1595 ®ex_free_result, NULL);
1596 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1597 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1598 info->t->regex_ctx = NULL;
1599 for (i = 0; i < info->n_contexts; i++)
1601 GNUNET_free (info->contexts[i]);
1603 if (0 < info->n_contexts)
1604 GNUNET_free (info->contexts);
1605 if (0 < info->n_peers)
1606 GNUNET_free (info->peers);
1607 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1609 GNUNET_SCHEDULER_cancel(info->timeout);
1615 /******************************************************************************/
1616 /************************ PERIODIC FUNCTIONS ****************************/
1617 /******************************************************************************/
1620 * Announce iterator over for each application provided by the peer
1622 * @param cls closure
1623 * @param key current key code
1624 * @param value value in the hash map
1625 * @return GNUNET_YES if we should continue to
1630 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1632 struct PBlock block;
1633 struct MeshClient *c;
1635 block.id = my_full_id;
1636 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1637 GNUNET_assert(NULL != c);
1638 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1639 if (0 == block.type)
1644 block.type = htonl (block.type);
1646 GNUNET_break (NULL !=
1647 GNUNET_DHT_put (dht_handle, key,
1648 dht_replication_level,
1649 GNUNET_DHT_RO_RECORD_ROUTE |
1650 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1651 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1653 (const char *) &block,
1654 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1656 app_announce_time, NULL, NULL));
1662 * Periodically announce what applications are provided by local clients
1665 * @param cls closure
1666 * @param tc task context
1669 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1671 struct MeshClient *c = cls;
1674 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1676 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1680 DEBUG_DHT ("Starting PUT for regex\n");
1682 for (i = 0; i < c->n_regex; i++)
1684 regex_put (c->regexes[i]);
1686 c->regex_announce_task =
1687 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1688 DEBUG_DHT ("Finished PUT for regex\n");
1695 * Periodically announce what applications are provided by local clients
1698 * @param cls closure
1699 * @param tc task context
1702 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1704 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1706 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1710 DEBUG_DHT ("Starting PUT for apps\n");
1712 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1714 announce_applications_task =
1715 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1717 DEBUG_DHT ("Finished PUT for apps\n");
1724 * Periodically announce self id in the DHT
1726 * @param cls closure
1727 * @param tc task context
1730 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1732 struct PBlock block;
1734 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1736 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1740 * - Set data expiration in function of X
1741 * - Adapt X to churn
1743 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1745 block.id = my_full_id;
1746 block.type = htonl (0);
1747 GNUNET_DHT_put (dht_handle, /* DHT handle */
1748 &my_full_id.hashPubKey, /* Key to use */
1749 dht_replication_level, /* Replication level */
1750 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1751 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1752 sizeof (block), /* Size of the data */
1753 (const char *) &block, /* Data itself */
1754 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1755 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1756 NULL, /* Continuation */
1757 NULL); /* Continuation closure */
1759 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1763 /******************************************************************************/
1764 /****************** GENERAL HELPER FUNCTIONS ************************/
1765 /******************************************************************************/
1768 * Decrements the reference counter and frees all resources if needed
1770 * @param mesh_data Data Descriptor used in a multicast message.
1771 * Freed no longer needed (last message).
1774 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1776 if (0 == --(mesh_data->reference_counter))
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1779 GNUNET_free (mesh_data->data);
1780 GNUNET_free (mesh_data);
1786 * Check if client has registered with the service and has not disconnected
1788 * @param client the client to check
1790 * @return non-NULL if client exists in the global DLL
1792 static struct MeshClient *
1793 client_get (struct GNUNET_SERVER_Client *client)
1795 struct MeshClient *c;
1800 if (c->handle == client)
1809 * Checks if a given client has subscribed to certain message type
1811 * @param message_type Type of message to check
1812 * @param c Client to check
1814 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1816 * FIXME: use of crypto_hash slows it down
1817 * The hash function alone takes 8-10us out of the ~55us for the whole
1818 * process of retransmitting the message from one local client to another.
1819 * Find faster implementation!
1822 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1824 struct GNUNET_HashCode hc;
1826 if (NULL == c->types)
1829 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1830 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1835 * Check whether client wants traffic from a tunnel.
1837 * @param c Client to check.
1838 * @param t Tunnel to be found.
1840 * @return GNUNET_YES if client knows tunnel.
1842 * TODO look in client hashmap
1845 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1849 for (i = 0; i < t->nclients; i++)
1850 if (t->clients[i] == c)
1857 * Check whether client has been informed about a tunnel.
1859 * @param c Client to check.
1860 * @param t Tunnel to be found.
1862 * @return GNUNET_YES if client knows tunnel.
1864 * TODO look in client hashmap
1867 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1871 for (i = 0; i < t->nignore; i++)
1872 if (t->ignore[i] == c)
1874 return client_wants_tunnel(c, t);
1879 * Marks a client as uninterested in traffic from the tunnel, updating both
1880 * client and tunnel to reflect this.
1882 * @param c Client that doesn't want traffic anymore.
1883 * @param t Tunnel which should be ignored.
1885 * FIXME when to delete an incoming tunnel?
1888 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1890 struct GNUNET_HashCode hash;
1892 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1893 GNUNET_break (GNUNET_YES ==
1894 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1896 GNUNET_break (GNUNET_YES ==
1897 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1898 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1899 tunnel_delete_active_client (t, c);
1900 GNUNET_array_append (t->ignore, t->nignore, c);
1905 * Deletes a tunnel from a client (either owner or destination). To be used on
1906 * tunnel destroy, otherwise, use client_ignore_tunnel.
1908 * @param c Client whose tunnel to delete.
1909 * @param t Tunnel which should be deleted.
1912 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1914 struct GNUNET_HashCode hash;
1918 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1919 GNUNET_assert (GNUNET_YES ==
1920 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1926 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1928 GNUNET_assert (GNUNET_YES ==
1929 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1933 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1941 * Send the message to all clients that have subscribed to its type
1943 * @param msg Pointer to the message itself
1944 * @param payload Pointer to the payload of the message.
1945 * @param t The tunnel to whose clients this message goes.
1947 * @return number of clients this message was sent to
1950 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1951 const struct GNUNET_MessageHeader *payload,
1952 struct MeshTunnel *t)
1954 struct MeshClient *c;
1955 MESH_TunnelNumber *tid;
1958 char cbuf[htons (msg->size)];
1960 type = ntohs (payload->type);
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1963 GNUNET_MESH_DEBUG_M2S (type));
1965 memcpy (cbuf, msg, sizeof (cbuf));
1966 switch (htons (msg->type))
1968 struct GNUNET_MESH_Unicast *uc;
1969 struct GNUNET_MESH_Multicast *mc;
1970 struct GNUNET_MESH_ToOrigin *to;
1972 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1973 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1976 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1977 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1980 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1981 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1989 for (count = 0, c = clients; c != NULL; c = c->next)
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1992 if (client_is_subscribed (type, c))
1994 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1998 *tid = htonl (t->local_tid);
2002 if (GNUNET_NO == client_knows_tunnel (c, t))
2004 /* This client doesn't know the tunnel */
2005 struct GNUNET_MESH_TunnelNotification tmsg;
2006 struct GNUNET_HashCode hash;
2008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2009 tmsg.header.size = htons (sizeof (tmsg));
2010 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2011 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2012 tmsg.tunnel_id = htonl (t->local_tid_dest);
2013 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2014 &tmsg.header, GNUNET_NO);
2015 tunnel_add_client (t, c);
2016 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2018 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2019 c->incoming_tunnels, &hash, t,
2020 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2022 *tid = htonl (t->local_tid_dest);
2025 /* Check if the client wants to get traffic from the tunnel */
2026 if (GNUNET_NO == client_wants_tunnel(c, t))
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2030 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2031 (struct GNUNET_MessageHeader
2032 *) cbuf, GNUNET_NO);
2041 * Notify the client that owns the tunnel that a peer has connected to it
2042 * (the requested path to it has been confirmed).
2044 * @param t Tunnel whose owner to notify
2045 * @param id Short id of the peer that has connected
2048 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2050 struct GNUNET_MESH_PeerControl pc;
2052 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2053 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2054 pc.tunnel_id = htonl (t->local_tid);
2055 GNUNET_PEER_resolve (id, &pc.peer);
2056 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2062 * Notify all clients (not depending on registration status) that the incoming
2063 * tunnel is no longer valid.
2065 * @param t Tunnel that was destroyed.
2068 send_clients_tunnel_destroy (struct MeshTunnel *t)
2070 struct GNUNET_MESH_TunnelMessage msg;
2072 msg.header.size = htons (sizeof (msg));
2073 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2074 msg.tunnel_id = htonl (t->local_tid_dest);
2075 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2080 * Notify clients of tunnel disconnections, if needed.
2081 * In case the origin disconnects, the destination clients get a tunnel destroy
2082 * notification. If the last destination disconnects (only one remaining client
2083 * in tunnel), the origin gets a (local ID) peer disconnected.
2084 * Note that the function must be called BEFORE removing the client from
2087 * @param t Tunnel that was destroyed.
2088 * @param c Client that disconnected.
2091 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2097 struct GNUNET_MESH_TunnelMessage msg;
2099 msg.header.size = htons (sizeof (msg));
2100 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2101 msg.tunnel_id = htonl (t->local_tid_dest);
2102 for (i = 0; i < t->nclients; i++)
2103 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2104 &msg.header, GNUNET_NO);
2106 // FIXME when to disconnect an incoming tunnel?
2107 else if (1 == t->nclients && NULL != t->owner)
2109 struct GNUNET_MESH_PeerControl msg;
2111 msg.header.size = htons (sizeof (msg));
2112 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2113 msg.tunnel_id = htonl (t->local_tid);
2114 msg.peer = my_full_id;
2115 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2116 &msg.header, GNUNET_NO);
2122 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2123 * and insert it in the appropiate structures if the peer is not known yet.
2125 * @param peer Full identity of the peer.
2127 * @return Existing or newly created peer info.
2129 static struct MeshPeerInfo *
2130 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2132 struct MeshPeerInfo *peer_info;
2134 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2135 if (NULL == peer_info)
2138 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2139 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2140 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2141 peer_info->id = GNUNET_PEER_intern (peer);
2149 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2150 * and insert it in the appropiate structures if the peer is not known yet.
2152 * @param peer Short identity of the peer.
2154 * @return Existing or newly created peer info.
2156 static struct MeshPeerInfo *
2157 peer_info_get_short (const GNUNET_PEER_Id peer)
2159 struct GNUNET_PeerIdentity id;
2161 GNUNET_PEER_resolve (peer, &id);
2162 return peer_info_get (&id);
2167 * Iterator to remove the tunnel from the list of tunnels a peer participates
2170 * @param cls Closure (tunnel info)
2171 * @param key GNUNET_PeerIdentity of the peer (unused)
2172 * @param value PeerInfo of the peer
2174 * @return always GNUNET_YES, to keep iterating
2177 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2179 struct MeshTunnel *t = cls;
2180 struct MeshPeerInfo *peer = value;
2183 for (i = 0; i < peer->ntunnels; i++)
2186 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2189 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2190 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2199 * Core callback to write a pre-constructed data packet to core buffer
2201 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2202 * @param size Number of bytes available in buf.
2203 * @param buf Where the to write the message.
2205 * @return number of bytes written to buf
2208 send_core_data_raw (void *cls, size_t size, void *buf)
2210 struct MeshTransmissionDescriptor *info = cls;
2211 struct GNUNET_MessageHeader *msg;
2214 GNUNET_assert (NULL != info);
2215 GNUNET_assert (NULL != info->mesh_data);
2216 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2217 total_size = ntohs (msg->size);
2219 if (total_size > size)
2224 memcpy (buf, msg, total_size);
2225 data_descriptor_decrement_rc (info->mesh_data);
2232 * Sends an already built non-multicast message to a peer,
2233 * properly registrating all used resources.
2235 * @param message Message to send. Function makes a copy of it.
2236 * @param peer Short ID of the neighbor whom to send the message.
2237 * @param t Tunnel on which this message is transmitted.
2240 send_message (const struct GNUNET_MessageHeader *message,
2241 const struct GNUNET_PeerIdentity *peer,
2242 struct MeshTunnel *t)
2244 struct MeshTransmissionDescriptor *info;
2245 struct MeshPeerInfo *neighbor;
2246 struct MeshPeerPath *p;
2250 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2252 size = ntohs (message->size);
2253 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2254 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2255 info->mesh_data->data = GNUNET_malloc (size);
2256 memcpy (info->mesh_data->data, message, size);
2257 type = ntohs(message->type);
2258 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2260 struct GNUNET_MESH_Unicast *m;
2262 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2263 m->ttl = htonl (ntohl (m->ttl) - 1);
2265 info->mesh_data->data_len = size;
2266 info->mesh_data->reference_counter = 1;
2267 info->mesh_data->total_out = 1;
2268 neighbor = peer_info_get (peer);
2269 for (p = neighbor->path_head; NULL != p; p = p->next)
2279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2280 " %s IS NOT DIRECTLY CONNECTED\n",
2282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2285 for (p = neighbor->path_head; NULL != p; p = p->next)
2287 struct GNUNET_PeerIdentity debug_id;
2290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2291 " path with %u hops through:\n",
2293 for (i = 0; i < p->length; i++)
2295 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2298 i, GNUNET_i2s(&debug_id));
2302 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2303 GNUNET_free (info->mesh_data->data);
2304 GNUNET_free (info->mesh_data);
2308 info->peer = neighbor;
2309 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2312 * TODO: in this case we only need the service to retransmit
2313 * the message down the path. If we pass the real type to queue_add,
2314 * queue_send will try to build the message from scratch. This can
2315 * probably be done by some other way instead of deleteing the type
2329 * Sends a CREATE PATH message for a path to a peer, properly registrating
2330 * all used resources.
2332 * @param peer PeerInfo of the final peer for whom this path is being created.
2333 * @param p Path itself.
2334 * @param t Tunnel for which the path is created.
2337 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2338 struct MeshTunnel *t)
2340 struct GNUNET_PeerIdentity id;
2341 struct MeshPathInfo *path_info;
2342 struct MeshPeerInfo *neighbor;
2348 p = tree_get_path_to_peer (t->tree, peer->id);
2355 for (i = 0; i < p->length; i++)
2357 if (p->peers[i] == myid)
2360 if (i >= p->length - 1)
2366 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2368 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2369 path_info->path = p;
2371 neighbor = peer_info_get (&id);
2372 path_info->peer = neighbor;
2373 queue_add (path_info,
2374 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2375 sizeof (struct GNUNET_MESH_ManipulatePath) +
2376 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2383 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2385 * @param t Tunnel whose path to destroy.
2386 * @param destination Short ID of the peer to whom the path to destroy.
2389 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2391 struct MeshPeerPath *p;
2394 p = tree_get_path_to_peer (t->tree, destination);
2400 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2401 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2403 struct GNUNET_MESH_ManipulatePath *msg;
2404 struct GNUNET_PeerIdentity *pi;
2408 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2409 msg->header.size = htons (size);
2410 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2411 msg->tid = htonl (t->id.tid);
2412 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2413 for (i = 0; i < p->length; i++)
2415 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2417 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2424 * Try to establish a new connection to this peer.
2425 * Use the best path for the given tunnel.
2426 * If the peer doesn't have any path to it yet, try to get one.
2427 * If the peer already has some path, send a CREATE PATH towards it.
2429 * @param peer PeerInfo of the peer.
2430 * @param t Tunnel for which to create the path, if possible.
2433 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2435 struct MeshPeerPath *p;
2436 struct MeshPathInfo *path_info;
2438 if (NULL != peer->path_head)
2440 p = tree_get_path_to_peer (t->tree, peer->id);
2447 // FIXME always send create path to self
2450 send_create_path (peer, p, t);
2454 struct GNUNET_HashCode hash;
2457 send_client_peer_connected (t, myid);
2458 t->local_tid_dest = next_local_tid++;
2459 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2462 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2463 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2470 else if (NULL == peer->dhtget)
2472 struct GNUNET_PeerIdentity id;
2474 GNUNET_PEER_resolve (peer->id, &id);
2475 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2476 path_info->peer = peer;
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2480 peer->dhtgetcls = path_info;
2481 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2482 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2483 &id.hashPubKey, /* key to search */
2484 dht_replication_level, /* replication level */
2485 GNUNET_DHT_RO_RECORD_ROUTE |
2486 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2487 NULL, /* xquery */ // FIXME BLOOMFILTER
2488 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2489 &dht_get_id_handler, path_info);
2491 /* Otherwise, there is no path but the DHT get is already started. */
2496 * Task to delay the connection of a peer
2498 * @param cls Closure (path info with tunnel and peer to connect).
2499 * Will be free'd on exection.
2500 * @param tc TaskContext
2503 peer_info_connect_task (void *cls,
2504 const struct GNUNET_SCHEDULER_TaskContext *tc)
2506 struct MeshPathInfo *path_info = cls;
2508 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2513 peer_info_connect (path_info->peer, path_info->t);
2519 * Destroy the peer_info and free any allocated resources linked to it
2521 * @param pi The peer_info to destroy.
2523 * @return GNUNET_OK on success
2526 peer_info_destroy (struct MeshPeerInfo *pi)
2528 struct GNUNET_PeerIdentity id;
2529 struct MeshPeerPath *p;
2530 struct MeshPeerPath *nextp;
2532 GNUNET_PEER_resolve (pi->id, &id);
2533 GNUNET_PEER_change_rc (pi->id, -1);
2536 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2540 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2542 if (NULL != pi->dhtget)
2544 GNUNET_DHT_get_stop (pi->dhtget);
2545 GNUNET_free (pi->dhtgetcls);
2551 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2561 * Remove all paths that rely on a direct connection between p1 and p2
2562 * from the peer itself and notify all tunnels about it.
2564 * @param peer PeerInfo of affected peer.
2565 * @param p1 GNUNET_PEER_Id of one peer.
2566 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2569 * TODO: optimize (see below)
2572 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2575 struct MeshPeerPath *p;
2576 struct MeshPeerPath *aux;
2577 struct MeshPeerInfo *peer_d;
2579 unsigned int destroyed;
2584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2586 p = peer->path_head;
2590 for (i = 0; i < (p->length - 1); i++)
2592 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2593 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2595 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2606 for (i = 0; i < peer->ntunnels; i++)
2608 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2612 * Problem: one or more peers have been deleted from the tunnel tree.
2613 * We don't know who they are to try to add them again.
2614 * We need to try to find a new path for each of the disconnected peers.
2615 * Some of them might already have a path to reach them that does not
2616 * involve p1 and p2. Adding all anew might render in a better tree than
2617 * the trivial immediate fix.
2619 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2620 * its children will be reachable trough him.
2622 peer_d = peer_info_get_short (d);
2625 for (p = peer_d->path_head; NULL != p; p = p->next)
2627 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2635 /* No callback, as peer will be already disconnected and a connection
2636 * scheduled by tunnel_notify_connection_broken.
2638 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2642 peer_info_connect (peer_d, peer->tunnels[i]);
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2650 * Add the path to the peer and update the path used to reach it in case this
2653 * @param peer_info Destination peer to add the path to.
2654 * @param path New path to add. Last peer must be the peer in arg 1.
2655 * Path will be either used of freed if already known.
2656 * @param trusted Do we trust that this path is real?
2659 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2662 struct MeshPeerPath *aux;
2666 if ((NULL == peer_info) || (NULL == path))
2669 path_destroy (path);
2672 if (path->peers[path->length - 1] != peer_info->id)
2675 path_destroy (path);
2678 if (path->length <= 2 && GNUNET_NO == trusted)
2680 /* Only allow CORE to tell us about direct paths */
2681 path_destroy (path);
2684 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2685 for (l = 1; l < path->length; l++)
2687 if (path->peers[l] == myid)
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2690 for (l2 = 0; l2 < path->length - l; l2++)
2692 path->peers[l2] = path->peers[l + l2];
2697 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2702 struct GNUNET_PeerIdentity id;
2704 GNUNET_PEER_resolve (peer_info->id, &id);
2705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2706 path->length, GNUNET_i2s (&id));
2709 l = path_get_length (path);
2716 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2717 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2719 l2 = path_get_length (aux);
2722 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2723 peer_info->path_tail, aux, path);
2728 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2730 path_destroy (path);
2735 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2742 * Add the path to the origin peer and update the path used to reach it in case
2743 * this is the shortest.
2744 * The path is given in peer_info -> destination, therefore we turn the path
2745 * upside down first.
2747 * @param peer_info Peer to add the path to, being the origin of the path.
2748 * @param path New path to add after being inversed.
2749 * @param trusted Do we trust that this path is real?
2752 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2753 struct MeshPeerPath *path, int trusted)
2756 peer_info_add_path (peer_info, path, trusted);
2761 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2762 * to obtain a local peer -> destination path and interning the peer ids.
2764 * @return Newly allocated and created path
2766 static struct MeshPeerPath *
2767 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2768 unsigned int get_path_length,
2769 const struct GNUNET_PeerIdentity *put_path,
2770 unsigned int put_path_length)
2772 struct MeshPeerPath *p;
2778 GNUNET_PEER_change_rc (myid, 1);
2779 i = get_path_length;
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2781 for (i--; i >= 0; i--)
2783 id = GNUNET_PEER_intern (&get_path[i]);
2784 if (p->length > 0 && id == p->peers[p->length - 1])
2786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2787 GNUNET_PEER_change_rc (id, -1);
2791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2792 GNUNET_i2s (&get_path[i]));
2794 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2795 p->peers[p->length - 1] = id;
2798 i = put_path_length;
2799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2800 for (i--; i >= 0; i--)
2802 id = GNUNET_PEER_intern (&put_path[i]);
2805 /* PUT path went through us, so discard the path up until now and start
2806 * from here to get a much shorter (and loop-free) path.
2811 if (p->length > 0 && id == p->peers[p->length - 1])
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2814 GNUNET_PEER_change_rc (id, -1);
2818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2819 GNUNET_i2s (&put_path[i]));
2821 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2822 p->peers[p->length - 1] = id;
2826 if (get_path_length > 0)
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2828 GNUNET_i2s (&get_path[0]));
2829 if (put_path_length > 0)
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2831 GNUNET_i2s (&put_path[0]));
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2834 for (i = 0; i < p->length; i++)
2836 struct GNUNET_PeerIdentity peer_id;
2838 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2840 GNUNET_i2s (&peer_id));
2848 * Adds a path to the peer_infos of all the peers in the path
2850 * @param p Path to process.
2851 * @param confirmed Whether we know if the path works or not.
2854 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2858 /* TODO: invert and add */
2859 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2860 for (i++; i < p->length; i++)
2862 struct MeshPeerInfo *aux;
2863 struct MeshPeerPath *copy;
2865 aux = peer_info_get_short (p->peers[i]);
2866 copy = path_duplicate (p);
2867 copy->length = i + 1;
2868 peer_info_add_path (aux, copy, GNUNET_NO);
2874 * Send keepalive packets for a peer
2876 * @param cls Closure (tunnel for which to send the keepalive).
2877 * @param tc Notification context.
2879 * TODO: implement explicit multicast keepalive?
2882 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2886 * Search for a tunnel among the incoming tunnels
2888 * @param tid the local id of the tunnel
2890 * @return tunnel handler, NULL if doesn't exist
2892 static struct MeshTunnel *
2893 tunnel_get_incoming (MESH_TunnelNumber tid)
2895 struct GNUNET_HashCode hash;
2897 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2898 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2899 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2904 * Search for a tunnel among the tunnels for a client
2906 * @param c the client whose tunnels to search in
2907 * @param tid the local id of the tunnel
2909 * @return tunnel handler, NULL if doesn't exist
2911 static struct MeshTunnel *
2912 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2914 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2916 return tunnel_get_incoming (tid);
2920 struct GNUNET_HashCode hash;
2922 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2923 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2929 * Search for a tunnel by global ID using PEER_ID
2931 * @param pi owner of the tunnel
2932 * @param tid global tunnel number
2934 * @return tunnel handler, NULL if doesn't exist
2936 static struct MeshTunnel *
2937 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2939 struct MESH_TunnelID id;
2940 struct GNUNET_HashCode hash;
2945 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2946 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2951 * Search for a tunnel by global ID using full PeerIdentities
2953 * @param oid owner of the tunnel
2954 * @param tid global tunnel number
2956 * @return tunnel handler, NULL if doesn't exist
2958 static struct MeshTunnel *
2959 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2961 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2966 * Delete an active client from the tunnel.
2972 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2976 for (i = 0; i < t->nclients; i++)
2978 if (t->clients[i] == c)
2980 t->clients[i] = t->clients[t->nclients - 1];
2981 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2982 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2984 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2992 * Delete an ignored client from the tunnel.
2998 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3002 for (i = 0; i < t->nignore; i++)
3004 if (t->ignore[i] == c)
3006 t->ignore[i] = t->ignore[t->nignore - 1];
3007 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3015 * Delete a client from the tunnel. It should be only done on
3016 * client disconnection, otherwise use client_ignore_tunnel.
3022 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3024 tunnel_delete_ignored_client (t, c);
3025 tunnel_delete_active_client (t, c);
3030 * Iterator to free MeshTunnelChildInfo of tunnel children.
3032 * @param cls Closure (tunnel info).
3033 * @param key Hash of GNUNET_PEER_Id (unused).
3034 * @param value MeshTunnelChildInfo of the child.
3036 * @return always GNUNET_YES, to keep iterating
3039 tunnel_destroy_child (void *cls,
3040 const struct GNUNET_HashCode * key,
3043 struct MeshTunnelChildInfo *cinfo = value;
3044 struct MeshTunnel *t = cls;
3048 for (c = 0; c < cinfo->send_buffer_n; c++)
3050 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3051 if (NULL != cinfo->send_buffer[i])
3052 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3055 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3057 GNUNET_free_non_null (cinfo->send_buffer);
3058 GNUNET_free (cinfo);
3064 * Callback used to notify a client owner of a tunnel that a peer has
3065 * disconnected, most likely because of a path change.
3067 * @param cls Closure (tunnel this notification is about).
3068 * @param peer_id Short ID of disconnected peer.
3071 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3073 struct MeshTunnel *t = cls;
3074 struct MeshPeerInfo *peer;
3075 struct MeshPathInfo *path_info;
3077 if (NULL != t->owner && NULL != nc)
3079 struct GNUNET_MESH_PeerControl msg;
3081 msg.header.size = htons (sizeof (msg));
3082 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3083 msg.tunnel_id = htonl (t->local_tid);
3084 GNUNET_PEER_resolve (peer_id, &msg.peer);
3085 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3086 &msg.header, GNUNET_NO);
3088 peer = peer_info_get_short (peer_id);
3089 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3090 path_info->peer = peer;
3092 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3097 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3098 * needed rescources.
3099 * If peer already exists, reevaluate shortest path and change if different.
3101 * @param t Tunnel we want to add a new peer to
3102 * @param peer PeerInfo of the peer being added
3106 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3108 struct GNUNET_PeerIdentity id;
3109 struct MeshPeerPath *best_p;
3110 struct MeshPeerPath *p;
3111 unsigned int best_cost;
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3115 GNUNET_PEER_resolve (peer->id, &id);
3117 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3120 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3121 GNUNET_assert (GNUNET_OK ==
3122 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3124 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3127 if (NULL != (p = peer->path_head))
3130 best_cost = tree_get_path_cost (t->tree, p);
3133 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3140 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3141 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3142 t->path_refresh_task =
3143 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3147 /* Start a DHT get */
3148 peer_info_connect (peer, t);
3150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3154 * Add a path to a tunnel which we don't own, just to remember the next hop.
3155 * If destination node was already in the tunnel, the first hop information
3156 * will be replaced with the new path.
3158 * @param t Tunnel we want to add a new peer to
3159 * @param p Path to add
3160 * @param own_pos Position of local node in path.
3164 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3165 unsigned int own_pos)
3167 struct GNUNET_PeerIdentity id;
3169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3170 GNUNET_assert (0 != own_pos);
3171 tree_add_path (t->tree, p, NULL, NULL);
3172 if (own_pos < p->length - 1)
3174 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3175 tree_update_first_hops (t->tree, myid, &id);
3177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3181 * Add a client to a tunnel, initializing all needed data structures.
3183 * @param t Tunnel to which add the client.
3184 * @param c Client which to add to the tunnel.
3187 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3189 struct MeshTunnelClientInfo clinfo;
3191 GNUNET_array_append (t->clients, t->nclients, c);
3192 clinfo.fwd_ack = t->fwd_pid + 1;
3193 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3194 clinfo.fwd_pid = t->fwd_pid;
3195 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3197 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3202 * Notifies a tunnel that a connection has broken that affects at least
3203 * some of its peers. Sends a notification towards the root of the tree.
3204 * In case the peer is the owner of the tree, notifies the client that owns
3205 * the tunnel and tries to reconnect.
3207 * @param t Tunnel affected.
3208 * @param p1 Peer that got disconnected from p2.
3209 * @param p2 Peer that got disconnected from p1.
3211 * @return Short ID of the peer disconnected (either p1 or p2).
3212 * 0 if the tunnel remained unaffected.
3214 static GNUNET_PEER_Id
3215 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3221 tree_notify_connection_broken (t->tree, p1, p2,
3222 &tunnel_notify_client_peer_disconnected,
3224 if (myid != p1 && myid != p2)
3230 if (tree_get_predecessor (t->tree) != 0)
3232 /* We are the peer still connected, notify owner of the disconnection. */
3233 struct GNUNET_MESH_PathBroken msg;
3234 struct GNUNET_PeerIdentity neighbor;
3236 msg.header.size = htons (sizeof (msg));
3237 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3238 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3239 msg.tid = htonl (t->id.tid);
3240 msg.peer1 = my_full_id;
3241 GNUNET_PEER_resolve (pid, &msg.peer2);
3242 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3243 send_message (&msg.header, &neighbor, t);
3251 * Send a multicast packet to a neighbor.
3253 * @param cls Closure (Info about the multicast packet)
3254 * @param neighbor_id Short ID of the neighbor to send the packet to.
3257 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3259 struct MeshData *mdata = cls;
3260 struct MeshTransmissionDescriptor *info;
3261 struct GNUNET_PeerIdentity neighbor;
3263 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3265 info->mesh_data = mdata;
3266 (mdata->reference_counter) ++;
3267 info->destination = neighbor_id;
3268 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3270 GNUNET_i2s (&neighbor));
3271 info->peer = peer_info_get (&neighbor);
3272 GNUNET_assert (NULL != info->peer);
3274 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3275 info->mesh_data->data_len,
3282 * Queue a message in a tunnel in multicast, sending a copy to each child node
3283 * down the local one in the tunnel tree.
3285 * @param t Tunnel in which to send the data.
3286 * @param msg Message to be sent.
3287 * @param internal DEPRECATED Has the service generated this message?
3289 * FIXME remove internal if no use comes up
3292 tunnel_send_multicast (struct MeshTunnel *t,
3293 const struct GNUNET_MessageHeader *msg,
3296 struct MeshData *mdata;
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299 " sending a multicast packet...\n");
3301 mdata = GNUNET_malloc (sizeof (struct MeshData));
3302 mdata->data_len = ntohs (msg->size);
3304 mdata->data = GNUNET_malloc (mdata->data_len);
3305 memcpy (mdata->data, msg, mdata->data_len);
3306 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3308 struct GNUNET_MESH_Multicast *mcast;
3310 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3311 if (t->fwd_queue_n >= t->fwd_queue_max)
3314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3315 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3316 " message from %s!\n",
3317 GNUNET_i2s(&mcast->oid));
3318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3319 " message at %s!\n",
3320 GNUNET_i2s(&my_full_id));
3321 GNUNET_free (mdata->data);
3322 GNUNET_free (mdata);
3326 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3328 ntohl (mcast->ttl));
3332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3335 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3336 if (mdata->reference_counter == 0)
3338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3339 " no one to send data to\n");
3340 GNUNET_free (mdata->data);
3341 GNUNET_free (mdata);
3346 mdata->total_out = mdata->reference_counter;
3348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3349 " sending a multicast packet done\n");
3355 * Increase the SKIP value of all peers that
3356 * have not received a unicast message.
3358 * @param cls Closure (ID of the peer that HAS received the message).
3359 * @param key ID of the neighbor.
3360 * @param value Information about the neighbor.
3362 * @return GNUNET_YES to keep iterating.
3365 tunnel_add_skip (void *cls,
3366 const struct GNUNET_HashCode * key,
3369 struct GNUNET_PeerIdentity *neighbor = cls;
3370 struct MeshTunnelChildInfo *cinfo = value;
3372 /* TODO compare only pointers? key == neighbor? */
3373 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3383 * @brief Get neighbor's Flow Control information.
3385 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3386 * descendant of the local node in a certain tunnel.
3387 * If the info is not yet there (recently created path), creates the data struct
3388 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3391 * @param t Tunnel related.
3392 * @param peer Neighbor whose Flow Control info is needed.
3394 * @return Neighbor's Flow Control info.
3396 static struct MeshTunnelChildInfo *
3397 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3398 const struct GNUNET_PeerIdentity *peer)
3400 struct MeshTunnelChildInfo *cinfo;
3401 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3407 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3408 cinfo->id = GNUNET_PEER_intern (peer);
3409 cinfo->skip = t->fwd_pid;
3411 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3412 cinfo->fwd_ack = t->fwd_pid + delta;
3413 cinfo->bck_ack = delta;
3415 cinfo->send_buffer =
3416 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3418 GNUNET_assert (GNUNET_OK ==
3419 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3422 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3429 * Get the Flow Control info of a client.
3431 * @param t Tunnel on which to look.
3432 * @param c Client whose ACK to get.
3434 * @return ACK value.
3436 static struct MeshTunnelClientInfo *
3437 tunnel_get_client_fc (struct MeshTunnel *t,
3438 struct MeshClient *c)
3442 for (i = 0; i < t->nclients; i++)
3444 if (t->clients[i] != c)
3446 return &t->clients_fc[i];
3449 return NULL; // avoid compiler / coverity complaints
3454 * Iterator to get the appropiate ACK value from all children nodes.
3456 * @param cls Closue (tunnel).
3457 * @param id Id of the child node.
3460 tunnel_get_child_fwd_ack (void *cls,
3463 struct GNUNET_PeerIdentity peer_id;
3464 struct MeshTunnelChildInfo *cinfo;
3465 struct MeshTunnelChildIteratorContext *ctx = cls;
3466 struct MeshTunnel *t = ctx->t;
3469 GNUNET_PEER_resolve (id, &peer_id);
3470 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3471 ack = cinfo->fwd_ack;
3474 if (GNUNET_NO == ctx->init)
3476 ctx->max_child_ack = ack;
3477 ctx->init = GNUNET_YES;
3480 if (GNUNET_YES == t->speed_min)
3482 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3486 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3493 * Get the maximum PID allowed to transmit to any
3494 * tunnel child of the local peer, depending on the tunnel
3495 * buffering/speed settings.
3499 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3502 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3504 struct MeshTunnelChildIteratorContext ctx;
3506 ctx.max_child_ack = 0;
3508 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3510 if (0 == ctx.nchildren)
3512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3513 " tunnel has no children, no FWD ACK\n");
3517 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3518 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3520 return (int64_t) ctx.max_child_ack;
3525 * Set the FWD ACK value of a client in a particular tunnel.
3527 * @param t Tunnel affected.
3528 * @param c Client whose ACK to set.
3529 * @param ack ACK value.
3532 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3533 struct MeshClient *c,
3538 for (i = 0; i < t->nclients; i++)
3540 if (t->clients[i] != c)
3542 t->clients_fc[i].fwd_ack = ack;
3550 * Get the highest ACK value of all clients in a particular tunnel,
3551 * according to the buffering/speed settings.
3553 * @param t Tunnel on which to look.
3555 * @return Corresponding ACK value (max uint32_t).
3556 * If no clients are suscribed, -1LL.
3559 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3564 if (0 == t->nclients)
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 " tunnel has no clients, no FWD ACK\n");
3571 for (ack = -1LL, i = 0; i < t->nclients; i++)
3574 (GNUNET_YES == t->speed_min &&
3575 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3576 (GNUNET_NO == t->speed_min &&
3577 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3579 ack = t->clients_fc[i].fwd_ack;
3583 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3584 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3586 return (uint32_t) ack;
3591 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3592 * mode and the status of all children nodes.
3596 * @return Maximum PID allowed.
3599 tunnel_get_fwd_ack (struct MeshTunnel *t)
3603 uint32_t buffer_free;
3607 count = t->fwd_pid - t->skip;
3608 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3609 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3610 child_ack = tunnel_get_children_fwd_ack (t);
3611 client_ack = tunnel_get_clients_fwd_ack (t);
3612 if (-1LL == child_ack)
3614 // Node has no children, child_ack AND core buffer are irrelevant.
3615 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3616 return (uint32_t) client_ack;
3618 if (-1LL == client_ack)
3622 if (GNUNET_YES == t->speed_min)
3624 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3625 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3629 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3630 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3632 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3633 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3635 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3636 count, buffer_free, child_ack, client_ack, ack);
3642 * Build a local ACK message and send it to a local client.
3644 * @param t Tunnel on which to send the ACK.
3645 * @param c Client to whom send the ACK.
3646 * @param ack Value of the ACK.
3649 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3651 struct GNUNET_MESH_LocalAck msg;
3653 msg.header.size = htons (sizeof (msg));
3654 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3655 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3656 msg.max_pid = htonl (ack);
3657 GNUNET_SERVER_notification_context_unicast(nc,
3664 * Build an ACK message and queue it to send to the given peer.
3666 * @param t Tunnel on which to send the ACK.
3667 * @param peer Peer to whom send the ACK.
3668 * @param ack Value of the ACK.
3671 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3673 struct GNUNET_MESH_ACK msg;
3675 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3676 msg.header.size = htons (sizeof (msg));
3677 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3678 msg.pid = htonl (ack);
3679 msg.tid = htonl (t->id.tid);
3681 send_message (&msg.header, peer, t);
3686 * Notify a the owner of a tunnel about how many more
3687 * payload packages will we accept on a given tunnel.
3689 * @param t Tunnel on which to send the ACK.
3692 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3697 "Sending client FWD ACK on tunnel %X\n",
3700 ack = tunnel_get_fwd_ack (t);
3702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3703 if (t->last_fwd_ack == ack)
3706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3707 t->last_fwd_ack = ack;
3708 send_local_ack (t, t->owner, ack);
3713 * Send an ACK informing the predecessor about the available buffer space.
3714 * In case there is no predecessor, inform the owning client.
3715 * If buffering is off, send only on behalf of children or self if endpoint.
3716 * If buffering is on, send when sent to children and buffer space is free.
3717 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3718 * the ACK itself goes "back" (towards root).
3720 * @param t Tunnel on which to send the ACK.
3721 * @param type Type of message that triggered the ACK transmission.
3724 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3726 struct GNUNET_PeerIdentity id;
3729 if (NULL != t->owner)
3731 tunnel_send_client_fwd_ack (t);
3734 /* Is it after unicast / multicast retransmission? */
3737 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3738 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3740 "ACK due to FWD DATA retransmission\n");
3741 if (GNUNET_YES == t->nobuffer)
3743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3747 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3748 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3754 /* Check if we need no retransmit the ACK */
3755 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3756 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3760 " t->qmax: %u, t->qn: %u\n",
3761 t->fwd_queue_max, t->fwd_queue_n);
3762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3763 " t->pid: %u, t->ack: %u\n",
3764 t->fwd_pid, t->last_fwd_ack);
3768 /* Ok, ACK might be necessary, what PID to ACK? */
3769 ack = tunnel_get_fwd_ack (t);
3771 /* If speed_min and not all children have ack'd, dont send yet */
3772 if (ack == t->last_fwd_ack)
3774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3778 t->last_fwd_ack = ack;
3779 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3780 send_ack (t, &id, ack);
3786 * Iterator to send a child node a BCK ACK to allow him to send more
3789 * @param cls Closure (tunnel).
3790 * @param id Id of the child node.
3793 tunnel_send_child_bck_ack (void *cls,
3796 struct MeshTunnel *t = cls;
3797 struct MeshTunnelChildInfo *cinfo;
3798 struct GNUNET_PeerIdentity peer;
3800 GNUNET_PEER_resolve (id, &peer);
3801 cinfo = tunnel_get_neighbor_fc (t, &peer);
3803 if (cinfo->bck_ack != cinfo->pid &&
3804 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3808 send_ack (t, &peer, cinfo->bck_ack);
3813 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3815 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3817 * @param t Tunnel on which to send the BCK ACKs.
3820 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3823 unsigned int tunnel_delta;
3825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3827 tunnel_delta = t->bck_ack - t->bck_pid;
3828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3830 /* Find client whom to allow to send to origin (with lowest buffer space) */
3831 for (i = 0; i < t->nclients; i++)
3833 struct MeshTunnelClientInfo *clinfo;
3836 clinfo = &t->clients_fc[i];
3837 delta = clinfo->bck_ack - clinfo->bck_pid;
3838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3839 t->clients[i]->id, delta);
3841 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3842 (GNUNET_YES == t->nobuffer && 0 == delta))
3846 ack = clinfo->bck_pid;
3847 ack += t->nobuffer ? 1 : tunnel_delta;
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3849 " sending ack to client %u: %u\n",
3850 t->clients[i]->id, ack);
3851 send_local_ack (t, t->clients[i], ack);
3852 clinfo->bck_ack = ack;
3859 * Send an ACK informing the children nodes and destination clients about
3860 * the available buffer space.
3861 * If buffering is off, send only on behalf of root (can be self).
3862 * If buffering is on, send when sent to predecessor and buffer space is free.
3863 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3864 * the ACK itself goes "forward" (towards children/clients).
3866 * @param t Tunnel on which to send the ACK.
3867 * @param type Type of message that triggered the ACK transmission.
3870 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3873 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3874 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3875 /* Is it after data to_origin retransmission? */
3878 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3879 if (GNUNET_YES == t->nobuffer)
3881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3882 " Not sending ACK, nobuffer\n");
3886 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3887 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3893 tunnel_send_clients_bck_ack (t);
3894 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3899 * @brief Re-initiate traffic to this peer if necessary.
3901 * Check if there is traffic queued towards this peer
3902 * and the core transmit handle is NULL (traffic was stalled).
3903 * If so, call core tmt rdy.
3905 * @param cls Closure (unused)
3906 * @param peer_id Short ID of peer to which initiate traffic.
3909 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3911 struct MeshPeerInfo *peer;
3912 struct GNUNET_PeerIdentity id;
3913 struct MeshPeerQueue *q;
3916 peer = peer_info_get_short(peer_id);
3917 if (NULL != peer->core_transmit)
3920 q = queue_get_next(peer);
3923 /* Might br multicast traffic already sent to this particular peer but
3924 * not to other children in this tunnel.
3925 * This way t->queue_n would be > 0 but the queue of this particular peer
3931 GNUNET_PEER_resolve (peer->id, &id);
3932 peer->core_transmit =
3933 GNUNET_CORE_notify_transmit_ready(core_handle,
3936 GNUNET_TIME_UNIT_FOREVER_REL,
3946 * @brief Allow transmission of FWD traffic on this tunnel
3948 * Check if there is traffic queued towards any children
3949 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3951 * @param t Tunnel on which to unlock FWD traffic.
3954 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3956 if (0 == t->fwd_queue_n)
3959 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3964 * @brief Allow transmission of BCK traffic on this tunnel
3966 * Check if there is traffic queued towards the root of the tree
3967 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3969 * @param t Tunnel on which to unlock BCK traffic.
3972 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3974 if (0 == t->bck_queue_n)
3977 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3982 * Send a message to all peers in this tunnel that the tunnel is no longer
3985 * @param t The tunnel whose peers to notify.
3988 tunnel_send_destroy (struct MeshTunnel *t)
3990 struct GNUNET_MESH_TunnelDestroy msg;
3992 msg.header.size = htons (sizeof (msg));
3993 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3994 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3995 msg.tid = htonl (t->id.tid);
3996 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4001 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4003 * @param cls Closure (Tunnel which to cancel).
4004 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4007 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4009 struct MeshTunnel *t = cls;
4010 struct MeshPeerInfo *peer_info;
4011 struct MeshPeerQueue *pq;
4012 struct MeshPeerQueue *next;
4014 peer_info = peer_info_get_short (neighbor_id);
4015 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4018 if (pq->tunnel == t)
4020 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4021 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4022 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4024 // Should have been removed on destroy children
4027 queue_destroy (pq, GNUNET_YES);
4030 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4032 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4033 peer_info->core_transmit = NULL;
4038 * Destroy the tunnel and free any allocated resources linked to it.
4040 * @param t the tunnel to destroy
4042 * @return GNUNET_OK on success
4045 tunnel_destroy (struct MeshTunnel *t)
4047 struct MeshClient *c;
4048 struct GNUNET_HashCode hash;
4059 struct GNUNET_PeerIdentity id;
4061 GNUNET_PEER_resolve (t->id.oid, &id);
4062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4063 GNUNET_i2s (&id), t->id.tid);
4065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4069 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4070 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4075 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4078 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4082 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4083 for (i = 0; i < t->nclients; i++)
4087 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4092 for (i = 0; i < t->nignore; i++)
4096 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4101 if (t->nclients > 0)
4104 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4108 GNUNET_free (t->clients);
4110 if (NULL != t->peers)
4112 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4114 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4117 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4118 &tunnel_destroy_child,
4120 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4122 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4124 tree_destroy (t->tree);
4126 if (NULL != t->regex_ctx)
4127 regex_cancel_search (t->regex_ctx);
4128 if (NULL != t->dht_get_type)
4129 GNUNET_DHT_get_stop (t->dht_get_type);
4130 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4131 GNUNET_SCHEDULER_cancel (t->timeout_task);
4132 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4133 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4136 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4137 GNUNET_assert (0 <= n_tunnels);
4144 * Create a new tunnel
4146 * @param owner Who is the owner of the tunnel (short ID).
4147 * @param tid Tunnel Number of the tunnel.
4148 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4149 * @param local Tunnel Number for the tunnel, for the client point of view.
4151 * @return A new initialized tunnel. NULL on error.
4153 static struct MeshTunnel *
4154 tunnel_new (GNUNET_PEER_Id owner,
4155 MESH_TunnelNumber tid,
4156 struct MeshClient *client,
4157 MESH_TunnelNumber local)
4159 struct MeshTunnel *t;
4160 struct GNUNET_HashCode hash;
4162 if (n_tunnels >= max_tunnels && NULL == client)
4165 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4168 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4169 t->bck_queue_max = t->fwd_queue_max;
4170 t->tree = tree_new (owner);
4172 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4173 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4174 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4175 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4176 t->local_tid = local;
4177 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4179 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4181 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4183 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4184 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4191 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4198 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4200 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4201 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4205 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4215 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4216 * that are no longer needed, as well as nodes of no longer reachable peers.
4217 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4219 * @param t Tunnel from which to remove the path.
4220 * @param peer Short id of the peer which should be removed.
4223 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4225 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4231 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4232 * client when the client disconnects. If the client is not the owner, the
4233 * owner will get notified if no more clients are in the tunnel and the client
4234 * get removed from the tunnel's list.
4236 * @param cls closure (client that is disconnecting)
4237 * @param key the hash of the local tunnel id (used to access the hashmap)
4238 * @param value the value stored at the key (tunnel to destroy)
4240 * @return GNUNET_OK on success
4243 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4245 struct MeshTunnel *t = value;
4246 struct MeshClient *c = cls;
4249 send_client_tunnel_disconnect(t, c);
4252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4253 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4254 tunnel_delete_client(t, c);
4255 client_delete_tunnel(c, t);
4258 tunnel_send_destroy(t);
4259 r = tunnel_destroy (t);
4265 * Timeout function, destroys tunnel if called
4267 * @param cls Closure (tunnel to destroy).
4268 * @param tc TaskContext
4271 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4273 struct MeshTunnel *t = cls;
4275 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4277 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4282 * Resets the tunnel timeout. Starts it if no timeout was running.
4284 * @param t Tunnel whose timeout to reset.
4286 * TODO use heap to improve efficiency of scheduler.
4289 tunnel_reset_timeout (struct MeshTunnel *t)
4291 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4292 GNUNET_SCHEDULER_cancel (t->timeout_task);
4294 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4295 (refresh_path_time, 4), &tunnel_timeout, t);
4299 /******************************************************************************/
4300 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4301 /******************************************************************************/
4304 * Function to send a create path packet to a peer.
4306 * @param cls closure
4307 * @param size number of bytes available in buf
4308 * @param buf where the callee should write the message
4309 * @return number of bytes written to buf
4312 send_core_path_create (void *cls, size_t size, void *buf)
4314 struct MeshPathInfo *info = cls;
4315 struct GNUNET_MESH_ManipulatePath *msg;
4316 struct GNUNET_PeerIdentity *peer_ptr;
4317 struct MeshTunnel *t = info->t;
4318 struct MeshPeerPath *p = info->path;
4323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4325 sizeof (struct GNUNET_MESH_ManipulatePath) +
4326 p->length * sizeof (struct GNUNET_PeerIdentity);
4328 if (size < size_needed || NULL == buf)
4333 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4334 msg->header.size = htons (size_needed);
4335 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4336 msg->tid = ntohl (t->id.tid);
4339 if (GNUNET_YES == t->speed_min)
4340 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4341 if (GNUNET_YES == t->nobuffer)
4342 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4343 msg->opt = htonl(opt);
4345 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4346 for (i = 0; i < p->length; i++)
4348 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4355 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4361 * Fill the core buffer
4363 * @param cls closure (data itself)
4364 * @param size number of bytes available in buf
4365 * @param buf where the callee should write the message
4367 * @return number of bytes written to buf
4370 send_core_data_multicast (void *cls, size_t size, void *buf)
4372 struct MeshTransmissionDescriptor *info = cls;
4375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4376 GNUNET_assert (NULL != info);
4377 GNUNET_assert (NULL != info->peer);
4378 total_size = info->mesh_data->data_len;
4379 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4381 if (total_size > size)
4386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4387 memcpy (buf, info->mesh_data->data, total_size);
4390 struct GNUNET_MESH_Multicast *mc;
4391 struct GNUNET_MessageHeader *mh;
4394 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4396 mc = (struct GNUNET_MESH_Multicast *) mh;
4397 mh = (struct GNUNET_MessageHeader *) &mc[1];
4398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4399 " multicast, payload type %s\n",
4400 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4402 " multicast, payload size %u\n", ntohs (mh->size));
4406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4407 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4411 data_descriptor_decrement_rc (info->mesh_data);
4412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4420 * Creates a path ack message in buf and frees all unused resources.
4422 * @param cls closure (MeshTransmissionDescriptor)
4423 * @param size number of bytes available in buf
4424 * @param buf where the callee should write the message
4425 * @return number of bytes written to buf
4428 send_core_path_ack (void *cls, size_t size, void *buf)
4430 struct MeshTransmissionDescriptor *info = cls;
4431 struct GNUNET_MESH_PathACK *msg = buf;
4433 GNUNET_assert (NULL != info);
4434 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4439 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4440 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4441 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4442 msg->tid = htonl (info->origin->tid);
4443 msg->peer_id = my_full_id;
4446 /* TODO add signature */
4448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4449 return sizeof (struct GNUNET_MESH_PathACK);
4454 * Free a transmission that was already queued with all resources
4455 * associated to the request.
4457 * @param queue Queue handler to cancel.
4458 * @param clear_cls Is it necessary to free associated cls?
4461 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4463 struct MeshTransmissionDescriptor *dd;
4464 struct MeshPathInfo *path_info;
4466 if (GNUNET_YES == clear_cls)
4468 switch (queue->type)
4470 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4471 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4472 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4475 data_descriptor_decrement_rc (dd->mesh_data);
4477 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4479 path_info = queue->cls;
4480 path_destroy (path_info->path);
4484 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4485 " type %u unknown!\n",
4488 GNUNET_free_non_null (queue->cls);
4490 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4491 queue->peer->queue_tail,
4493 GNUNET_free (queue);
4498 * @brief Get the next transmittable message from the queue.
4500 * This will be the head, except in the case of being a data packet
4501 * not allowed by the destination peer.
4503 * @param peer Destination peer.
4505 * @return The next viable MeshPeerQueue element to send to that peer.
4506 * NULL when there are no transmittable messages.
4508 struct MeshPeerQueue *
4509 queue_get_next (const struct MeshPeerInfo *peer)
4511 struct MeshPeerQueue *q;
4512 struct MeshTunnel *t;
4513 struct MeshTransmissionDescriptor *info;
4514 struct MeshTunnelChildInfo *cinfo;
4515 struct GNUNET_MESH_Unicast *ucast;
4516 struct GNUNET_MESH_ToOrigin *to_orig;
4517 struct GNUNET_MESH_Multicast *mcast;
4518 struct GNUNET_PeerIdentity id;
4522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4523 for (q = peer->queue_head; NULL != q; q = q->next)
4527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4529 GNUNET_MESH_DEBUG_M2S(q->type));
4532 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4533 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4534 pid = ntohl (ucast->pid);
4535 GNUNET_PEER_resolve (info->peer->id, &id);
4536 cinfo = tunnel_get_neighbor_fc(t, &id);
4537 ack = cinfo->fwd_ack;
4539 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4540 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4541 pid = ntohl (to_orig->pid);
4544 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4545 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4546 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4548 // Not a multicast payload: multicast control traffic (destroy, etc)
4551 pid = ntohl (mcast->pid);
4552 GNUNET_PEER_resolve (info->peer->id, &id);
4553 cinfo = tunnel_get_neighbor_fc(t, &id);
4554 ack = cinfo->fwd_ack;
4557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4562 "********* ACK: %u, PID: %u\n",
4564 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4573 "********* NEXT!\n");
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4577 "********* nothing found\n");
4583 * Core callback to write a queued packet to core buffer
4585 * @param cls Closure (peer info).
4586 * @param size Number of bytes available in buf.
4587 * @param buf Where the to write the message.
4589 * @return number of bytes written to buf
4592 queue_send (void *cls, size_t size, void *buf)
4594 struct MeshPeerInfo *peer = cls;
4595 struct GNUNET_MessageHeader *msg;
4596 struct MeshPeerQueue *queue;
4597 struct MeshTunnel *t;
4598 struct MeshTunnelChildInfo *cinfo;
4599 struct GNUNET_PeerIdentity dst_id;
4602 peer->core_transmit = NULL;
4604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4605 queue = queue_get_next(peer);
4607 /* Queue has no internal mesh traffic not sendable payload */
4610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4611 if (NULL == peer->queue_head)
4612 GNUNET_break(0); // Should've been canceled
4615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4617 GNUNET_PEER_resolve (peer->id, &dst_id);
4618 /* Check if buffer size is enough for the message */
4619 if (queue->size > size)
4623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4624 "********* not enough room, reissue\n");
4625 peer->core_transmit =
4626 GNUNET_CORE_notify_transmit_ready(core_handle,
4629 GNUNET_TIME_UNIT_FOREVER_REL,
4636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4639 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4643 "********* unicast: t->q (%u/%u)\n",
4644 t->fwd_queue_n, t->fwd_queue_max);
4646 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4653 switch (queue->type)
4656 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4657 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4658 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4660 "********* raw: %s\n",
4661 GNUNET_MESH_DEBUG_M2S (queue->type));
4663 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4664 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4665 data_size = send_core_data_raw (queue->cls, size, buf);
4666 msg = (struct GNUNET_MessageHeader *) buf;
4667 switch (ntohs (msg->type)) // Type of preconstructed message
4669 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4670 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4672 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4673 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4679 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4682 struct MeshTransmissionDescriptor *info = queue->cls;
4684 if ((1 == info->mesh_data->reference_counter
4685 && GNUNET_YES == t->speed_min)
4687 (info->mesh_data->total_out == info->mesh_data->reference_counter
4688 && GNUNET_NO == t->speed_min))
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4691 "********* considered sent\n");
4695 data_size = send_core_data_multicast(queue->cls, size, buf);
4696 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4698 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4700 data_size = send_core_path_create(queue->cls, size, buf);
4702 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4704 data_size = send_core_path_ack(queue->cls, size, buf);
4708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4709 "********* type unknown: %u\n",
4713 switch (queue->type)
4715 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4716 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4717 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4718 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4719 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4723 "at pos %u (%p) != %p\n",
4724 cinfo->send_buffer_start,
4725 cinfo->send_buffer[cinfo->send_buffer_start],
4728 GNUNET_break(cinfo->send_buffer_n > 0);
4729 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4730 cinfo->send_buffer_n--;
4731 cinfo->send_buffer_start++;
4732 cinfo->send_buffer_start %= t->fwd_queue_max;
4738 /* Free queue, but cls was freed by send_core_* */
4739 queue_destroy (queue, GNUNET_NO);
4741 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4747 /* If more data in queue, send next */
4748 queue = queue_get_next(peer);
4751 struct GNUNET_PeerIdentity id;
4753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4754 GNUNET_PEER_resolve (peer->id, &id);
4755 peer->core_transmit =
4756 GNUNET_CORE_notify_transmit_ready(core_handle,
4759 GNUNET_TIME_UNIT_FOREVER_REL,
4767 if (NULL != peer->queue_head)
4768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4769 "********* %s stalled\n",
4770 GNUNET_i2s(&my_full_id));
4772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4778 * Queue and pass message to core when possible.
4780 * @param cls Closure (type dependant).
4781 * @param type Type of the message, 0 for a raw message.
4782 * @param size Size of the message.
4783 * @param dst Neighbor to send message to.
4784 * @param t Tunnel this message belongs to.
4787 queue_add (void *cls, uint16_t type, size_t size,
4788 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4790 struct MeshPeerQueue *queue;
4791 struct MeshTunnelChildInfo *cinfo;
4792 struct GNUNET_PeerIdentity id;
4798 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4799 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4801 n = &t->fwd_queue_n;
4802 max = &t->fwd_queue_max;
4804 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4806 n = &t->bck_queue_n;
4807 max = &t->bck_queue_max;
4812 if (NULL == t->owner)
4813 GNUNET_break_op(0); // TODO: kill connection?
4816 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4818 return; // Drop message
4822 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4828 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4829 GNUNET_PEER_resolve (dst->id, &id);
4830 if (NULL == dst->core_transmit)
4832 dst->core_transmit =
4833 GNUNET_CORE_notify_transmit_ready(core_handle,
4836 GNUNET_TIME_UNIT_FOREVER_REL,
4842 if (NULL == n) // Is this internal mesh traffic?
4845 // It's payload, keep track of buffer per peer.
4846 cinfo = tunnel_get_neighbor_fc(t, &id);
4847 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4848 if (NULL != cinfo->send_buffer[i])
4850 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4851 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4852 cinfo->send_buffer_start++;
4853 cinfo->send_buffer_start %= t->fwd_queue_max;
4854 cinfo->send_buffer_n--;
4856 cinfo->send_buffer[i] = queue;
4857 cinfo->send_buffer_n++;
4858 if (cinfo->send_buffer_n > t->fwd_queue_max)
4861 cinfo->send_buffer_n = t->fwd_queue_max;
4866 /******************************************************************************/
4867 /******************** MESH NETWORK HANDLERS **************************/
4868 /******************************************************************************/
4872 * Core handler for path creation
4874 * @param cls closure
4875 * @param message message
4876 * @param peer peer identity this notification is about
4877 * @param atsi performance data
4878 * @param atsi_count number of records in 'atsi'
4880 * @return GNUNET_OK to keep the connection open,
4881 * GNUNET_SYSERR to close it (signal serious error)
4884 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4885 const struct GNUNET_MessageHeader *message,
4886 const struct GNUNET_ATS_Information *atsi,
4887 unsigned int atsi_count)
4889 unsigned int own_pos;
4892 MESH_TunnelNumber tid;
4893 struct GNUNET_MESH_ManipulatePath *msg;
4894 struct GNUNET_PeerIdentity *pi;
4895 struct GNUNET_HashCode hash;
4896 struct MeshPeerPath *path;
4897 struct MeshPeerInfo *dest_peer_info;
4898 struct MeshPeerInfo *orig_peer_info;
4899 struct MeshTunnel *t;
4901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4902 "Received a path create msg [%s]\n",
4903 GNUNET_i2s (&my_full_id));
4904 size = ntohs (message->size);
4905 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4907 GNUNET_break_op (0);
4911 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4912 if (size % sizeof (struct GNUNET_PeerIdentity))
4914 GNUNET_break_op (0);
4917 size /= sizeof (struct GNUNET_PeerIdentity);
4920 GNUNET_break_op (0);
4923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4924 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4926 tid = ntohl (msg->tid);
4927 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4929 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4930 t = tunnel_get (pi, tid);
4931 if (NULL == t) // FIXME only for INCOMING tunnels?
4935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4936 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4939 // FIXME notify failure
4942 opt = ntohl (msg->opt);
4943 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4944 GNUNET_YES : GNUNET_NO;
4945 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4946 GNUNET_YES : GNUNET_NO;
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4948 " speed_min: %d, nobuffer:%d\n",
4949 t->speed_min, t->nobuffer);
4951 if (GNUNET_YES == t->nobuffer)
4953 t->bck_queue_max = 1;
4954 t->fwd_queue_max = 1;
4957 // FIXME only assign a local tid if a local client is interested (on demand)
4958 while (NULL != tunnel_get_incoming (next_local_tid))
4959 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4960 t->local_tid_dest = next_local_tid++;
4961 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4964 tunnel_reset_timeout (t);
4965 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4967 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4968 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4976 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4977 if (NULL == dest_peer_info)
4979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4980 " Creating PeerInfo for destination.\n");
4981 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4982 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4983 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4985 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4987 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4988 if (NULL == orig_peer_info)
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4991 " Creating PeerInfo for origin.\n");
4992 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4993 orig_peer_info->id = GNUNET_PEER_intern (pi);
4994 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4995 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4998 path = path_new (size);
5000 for (i = 0; i < size; i++)
5002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5003 GNUNET_i2s (&pi[i]));
5004 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5005 if (path->peers[i] == myid)
5008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5011 /* cannot be self, must be 'not found' */
5012 /* create path: self not found in path through self */
5013 GNUNET_break_op (0);
5014 path_destroy (path);
5018 path_add_to_peers (path, GNUNET_NO);
5019 tunnel_add_path (t, path, own_pos);
5020 if (own_pos == size - 1)
5022 /* It is for us! Send ack. */
5023 struct MeshTransmissionDescriptor *info;
5025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5026 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5027 if (NULL == t->peers)
5029 /* New tunnel! Notify clients on data. */
5030 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5032 GNUNET_break (GNUNET_SYSERR !=
5033 GNUNET_CONTAINER_multihashmap_put (t->peers,
5034 &my_full_id.hashPubKey,
5037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5038 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5039 info->origin = &t->id;
5040 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5041 GNUNET_assert (NULL != info->peer);
5043 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5044 sizeof (struct GNUNET_MESH_PathACK),
5050 struct MeshPeerPath *path2;
5052 /* It's for somebody else! Retransmit. */
5053 path2 = path_duplicate (path);
5054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5055 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5056 path2 = path_duplicate (path);
5057 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5058 send_create_path (dest_peer_info, path, t);
5065 * Core handler for path destruction
5067 * @param cls closure
5068 * @param message message
5069 * @param peer peer identity this notification is about
5070 * @param atsi performance data
5071 * @param atsi_count number of records in 'atsi'
5073 * @return GNUNET_OK to keep the connection open,
5074 * GNUNET_SYSERR to close it (signal serious error)
5077 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5078 const struct GNUNET_MessageHeader *message,
5079 const struct GNUNET_ATS_Information *atsi,
5080 unsigned int atsi_count)
5082 struct GNUNET_MESH_ManipulatePath *msg;
5083 struct GNUNET_PeerIdentity *pi;
5084 struct MeshPeerPath *path;
5085 struct MeshTunnel *t;
5086 unsigned int own_pos;
5090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5091 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5092 size = ntohs (message->size);
5093 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5095 GNUNET_break_op (0);
5099 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5100 if (size % sizeof (struct GNUNET_PeerIdentity))
5102 GNUNET_break_op (0);
5105 size /= sizeof (struct GNUNET_PeerIdentity);
5108 GNUNET_break_op (0);
5111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5113 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5114 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5116 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5118 t = tunnel_get (pi, ntohl (msg->tid));
5121 /* TODO notify back: we don't know this tunnel */
5122 GNUNET_break_op (0);
5125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5126 path = path_new (size);
5128 for (i = 0; i < size; i++)
5130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5131 GNUNET_i2s (&pi[i]));
5132 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5133 if (path->peers[i] == myid)
5136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5137 if (own_pos < path->length - 1)
5138 send_message (message, &pi[own_pos + 1], t);
5140 send_client_tunnel_disconnect(t, NULL);
5142 tunnel_delete_peer (t, path->peers[path->length - 1]);
5143 path_destroy (path);
5149 * Core handler for notifications of broken paths
5151 * @param cls closure
5152 * @param message message
5153 * @param peer peer identity this notification is about
5154 * @param atsi performance data
5155 * @param atsi_count number of records in 'atsi'
5157 * @return GNUNET_OK to keep the connection open,
5158 * GNUNET_SYSERR to close it (signal serious error)
5161 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5162 const struct GNUNET_MessageHeader *message,
5163 const struct GNUNET_ATS_Information *atsi,
5164 unsigned int atsi_count)
5166 struct GNUNET_MESH_PathBroken *msg;
5167 struct MeshTunnel *t;
5169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5170 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5171 msg = (struct GNUNET_MESH_PathBroken *) message;
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5173 GNUNET_i2s (&msg->peer1));
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5175 GNUNET_i2s (&msg->peer2));
5176 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5179 GNUNET_break_op (0);
5182 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5183 GNUNET_PEER_search (&msg->peer2));
5190 * Core handler for tunnel destruction
5192 * @param cls closure
5193 * @param message message
5194 * @param peer peer identity this notification is about
5195 * @param atsi performance data
5196 * @param atsi_count number of records in 'atsi'
5198 * @return GNUNET_OK to keep the connection open,
5199 * GNUNET_SYSERR to close it (signal serious error)
5202 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5203 const struct GNUNET_MessageHeader *message,
5204 const struct GNUNET_ATS_Information *atsi,
5205 unsigned int atsi_count)
5207 struct GNUNET_MESH_TunnelDestroy *msg;
5208 struct MeshTunnel *t;
5210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5211 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5212 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5214 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5215 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5218 /* Probably already got the message from another path,
5219 * destroyed the tunnel and retransmitted to children.
5222 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5225 if (t->id.oid == myid)
5227 GNUNET_break_op (0);
5230 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5232 /* Tunnel was incoming, notify clients */
5233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5234 t->local_tid, t->local_tid_dest);
5235 send_clients_tunnel_destroy (t);
5237 tunnel_send_destroy (t);
5238 t->destroy = GNUNET_YES;
5239 // TODO: add timeout to destroy the tunnel anyway
5245 * Core handler for mesh network traffic going from the origin to a peer
5247 * @param cls closure
5248 * @param peer peer identity this notification is about
5249 * @param message message
5250 * @param atsi performance data
5251 * @param atsi_count number of records in 'atsi'
5252 * @return GNUNET_OK to keep the connection open,
5253 * GNUNET_SYSERR to close it (signal serious error)
5256 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5257 const struct GNUNET_MessageHeader *message,
5258 const struct GNUNET_ATS_Information *atsi,
5259 unsigned int atsi_count)
5261 struct GNUNET_MESH_Unicast *msg;
5262 struct GNUNET_PeerIdentity *neighbor;
5263 struct MeshTunnelChildInfo *cinfo;
5264 struct MeshTunnel *t;
5265 GNUNET_PEER_Id dest_id;
5270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5273 size = ntohs (message->size);
5275 sizeof (struct GNUNET_MESH_Unicast) +
5276 sizeof (struct GNUNET_MessageHeader))
5281 msg = (struct GNUNET_MESH_Unicast *) message;
5282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5283 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5285 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5288 /* TODO notify back: we don't know this tunnel */
5289 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5290 GNUNET_break_op (0);
5293 pid = ntohl (msg->pid);
5294 if (t->fwd_pid == pid)
5296 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5297 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5298 " Already seen pid %u, DROPPING!\n", pid);
5303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5304 " pid %u not seen yet, forwarding\n", pid);
5307 t->skip += (pid - t->fwd_pid) - 1;
5310 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5312 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5313 GNUNET_break_op (0);
5314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5315 "Received PID %u, ACK %u\n",
5316 pid, t->last_fwd_ack);
5320 tunnel_reset_timeout (t);
5321 dest_id = GNUNET_PEER_search (&msg->destination);
5322 if (dest_id == myid)
5324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5325 " it's for us! sending to clients...\n");
5326 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5327 send_subscribed_clients (message, &msg[1].header, t);
5328 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5331 ttl = ntohl (msg->ttl);
5332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5335 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5336 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5337 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5341 " not for us, retransmitting...\n");
5343 neighbor = tree_get_first_hop (t->tree, dest_id);
5344 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5346 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5349 if (GNUNET_YES == t->nobuffer &&
5350 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5352 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5353 GNUNET_break_op (0);
5356 send_message (message, neighbor, t);
5357 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5363 * Core handler for mesh network traffic going from the origin to all peers
5365 * @param cls closure
5366 * @param message message
5367 * @param peer peer identity this notification is about
5368 * @param atsi performance data
5369 * @param atsi_count number of records in 'atsi'
5370 * @return GNUNET_OK to keep the connection open,
5371 * GNUNET_SYSERR to close it (signal serious error)
5373 * TODO: Check who we got this from, to validate route.
5376 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5377 const struct GNUNET_MessageHeader *message,
5378 const struct GNUNET_ATS_Information *atsi,
5379 unsigned int atsi_count)
5381 struct GNUNET_MESH_Multicast *msg;
5382 struct MeshTunnel *t;
5386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5388 size = ntohs (message->size);
5389 if (sizeof (struct GNUNET_MESH_Multicast) +
5390 sizeof (struct GNUNET_MessageHeader) > size)
5392 GNUNET_break_op (0);
5395 msg = (struct GNUNET_MESH_Multicast *) message;
5396 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5400 /* TODO notify that we dont know that tunnel */
5401 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5402 GNUNET_break_op (0);
5405 pid = ntohl (msg->pid);
5406 if (t->fwd_pid == pid)
5408 /* already seen this packet, drop */
5409 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5411 " Already seen pid %u, DROPPING!\n", pid);
5412 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5418 " pid %u not seen yet, forwarding\n", pid);
5420 t->skip += (pid - t->fwd_pid) - 1;
5422 tunnel_reset_timeout (t);
5424 /* Transmit to locally interested clients */
5425 if (NULL != t->peers &&
5426 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5428 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5429 send_subscribed_clients (message, &msg[1].header, t);
5430 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5433 if (ntohl (msg->ttl) == 0)
5435 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5436 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5437 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5440 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5441 tunnel_send_multicast (t, message, GNUNET_NO);
5447 * Core handler for mesh network traffic toward the owner of a tunnel
5449 * @param cls closure
5450 * @param message message
5451 * @param peer peer identity this notification is about
5452 * @param atsi performance data
5453 * @param atsi_count number of records in 'atsi'
5455 * @return GNUNET_OK to keep the connection open,
5456 * GNUNET_SYSERR to close it (signal serious error)
5459 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5460 const struct GNUNET_MessageHeader *message,
5461 const struct GNUNET_ATS_Information *atsi,
5462 unsigned int atsi_count)
5464 struct GNUNET_MESH_ToOrigin *msg;
5465 struct GNUNET_PeerIdentity id;
5466 struct MeshPeerInfo *peer_info;
5467 struct MeshTunnel *t;
5471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5473 size = ntohs (message->size);
5474 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5475 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5477 GNUNET_break_op (0);
5480 msg = (struct GNUNET_MESH_ToOrigin *) message;
5481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5482 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5483 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5487 /* TODO notify that we dont know this tunnel (whom)? */
5488 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5489 GNUNET_break_op (0);
5493 if (NULL != t->owner)
5496 struct GNUNET_MESH_ToOrigin *copy;
5498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5499 " it's for us! sending to clients...\n");
5500 /* TODO signature verification */
5501 memcpy (cbuf, message, size);
5502 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5503 copy->tid = htonl (t->local_tid);
5504 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5505 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5506 ©->header, GNUNET_NO);
5507 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5511 " not for us, retransmitting...\n");
5513 peer_info = peer_info_get (&msg->oid);
5514 if (NULL == peer_info)
5516 /* unknown origin of tunnel */
5520 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5521 send_message (message, &id, t);
5522 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5529 * Core handler for mesh network traffic point-to-point acks.
5531 * @param cls closure
5532 * @param message message
5533 * @param peer peer identity this notification is about
5534 * @param atsi performance data
5535 * @param atsi_count number of records in 'atsi'
5537 * @return GNUNET_OK to keep the connection open,
5538 * GNUNET_SYSERR to close it (signal serious error)
5541 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5542 const struct GNUNET_MessageHeader *message,
5543 const struct GNUNET_ATS_Information *atsi,
5544 unsigned int atsi_count)
5546 struct GNUNET_MESH_ACK *msg;
5547 struct MeshTunnel *t;
5550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5552 msg = (struct GNUNET_MESH_ACK *) message;
5554 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5558 /* TODO notify that we dont know this tunnel (whom)? */
5559 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5560 GNUNET_break_op (0);
5563 ack = ntohl (msg->pid);
5565 /* Is this a forward or backward ACK? */
5566 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5568 struct MeshTunnelChildInfo *cinfo;
5571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5572 cinfo = tunnel_get_neighbor_fc (t, peer);
5573 cinfo->fwd_ack = ack;
5574 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5575 tunnel_unlock_fwd_queues (t);
5579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5581 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5582 tunnel_unlock_bck_queue (t);
5589 * Core handler for path ACKs
5591 * @param cls closure
5592 * @param message message
5593 * @param peer peer identity this notification is about
5594 * @param atsi performance data
5595 * @param atsi_count number of records in 'atsi'
5597 * @return GNUNET_OK to keep the connection open,
5598 * GNUNET_SYSERR to close it (signal serious error)
5601 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5602 const struct GNUNET_MessageHeader *message,
5603 const struct GNUNET_ATS_Information *atsi,
5604 unsigned int atsi_count)
5606 struct GNUNET_MESH_PathACK *msg;
5607 struct GNUNET_PeerIdentity id;
5608 struct MeshPeerInfo *peer_info;
5609 struct MeshPeerPath *p;
5610 struct MeshTunnel *t;
5612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5613 GNUNET_i2s (&my_full_id));
5614 msg = (struct GNUNET_MESH_PathACK *) message;
5615 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5618 /* TODO notify that we don't know the tunnel */
5619 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5621 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5625 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5627 peer_info = peer_info_get (&msg->peer_id);
5628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5629 GNUNET_i2s (&msg->peer_id));
5630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5633 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5636 "connect_by_string completed, stopping search\n");
5637 regex_cancel_search (t->regex_ctx);
5638 t->regex_ctx = NULL;
5641 /* Add paths to peers? */
5642 p = tree_get_path_to_peer (t->tree, peer_info->id);
5645 path_add_to_peers (p, GNUNET_YES);
5653 /* Message for us? */
5654 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5657 if (NULL == t->owner)
5659 GNUNET_break_op (0);
5662 if (NULL != t->dht_get_type)
5664 GNUNET_DHT_get_stop (t->dht_get_type);
5665 t->dht_get_type = NULL;
5667 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5669 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5670 send_client_peer_connected (t, peer_info->id);
5675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5676 " not for us, retransmitting...\n");
5677 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5678 peer_info = peer_info_get (&msg->oid);
5679 if (NULL == peer_info)
5681 /* If we know the tunnel, we should DEFINITELY know the peer */
5685 send_message (message, &id, t);
5691 * Functions to handle messages from core
5693 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5694 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5695 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5696 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5697 sizeof (struct GNUNET_MESH_PathBroken)},
5698 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5699 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5700 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5701 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5702 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5703 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5704 sizeof (struct GNUNET_MESH_ACK)},
5705 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5706 sizeof (struct GNUNET_MESH_PathACK)},
5712 /******************************************************************************/
5713 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5714 /******************************************************************************/
5717 * deregister_app: iterator for removing each application registered by a client
5719 * @param cls closure
5720 * @param key the hash of the application id (used to access the hashmap)
5721 * @param value the value stored at the key (client)
5723 * @return GNUNET_OK on success
5726 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5728 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5729 GNUNET_break (GNUNET_YES ==
5730 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5736 * notify_client_connection_failure: notify a client that the connection to the
5737 * requested remote peer is not possible (for instance, no route found)
5738 * Function called when the socket is ready to queue more data. "buf" will be
5739 * NULL and "size" zero if the socket was closed for writing in the meantime.
5741 * @param cls closure
5742 * @param size number of bytes available in buf
5743 * @param buf where the callee should write the message
5744 * @return number of bytes written to buf
5747 notify_client_connection_failure (void *cls, size_t size, void *buf)
5750 struct MeshPeerInfo *peer_info;
5751 struct GNUNET_MESH_PeerControl *msg;
5752 struct GNUNET_PeerIdentity id;
5754 if (0 == size && NULL == buf)
5756 // TODO retry? cancel?
5760 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5761 peer_info = (struct MeshPeerInfo *) cls;
5762 msg = (struct GNUNET_MESH_PeerControl *) buf;
5763 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5764 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5765 // msg->tunnel_id = htonl(peer_info->t->tid);
5766 GNUNET_PEER_resolve (peer_info->id, &id);
5767 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5775 * Send keepalive packets for a peer
5777 * @param cls Closure (tunnel for which to send the keepalive).
5778 * @param tc Notification context.
5780 * TODO: implement explicit multicast keepalive?
5783 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5785 struct MeshTunnel *t = cls;
5786 struct GNUNET_MessageHeader *payload;
5787 struct GNUNET_MESH_Multicast *msg;
5789 sizeof (struct GNUNET_MESH_Multicast) +
5790 sizeof (struct GNUNET_MessageHeader);
5793 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5797 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5800 "sending keepalive for tunnel %d\n", t->id.tid);
5802 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5803 msg->header.size = htons (size);
5804 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5805 // FIXME: change type to != MULTICAST
5806 msg->oid = my_full_id;
5807 msg->tid = htonl (t->id.tid);
5808 msg->ttl = htonl (default_ttl);
5809 msg->pid = htonl (t->fwd_pid + 1);
5811 payload = (struct GNUNET_MessageHeader *) &msg[1];
5812 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5813 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5814 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5816 t->path_refresh_task =
5817 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5823 * Function to process paths received for a new peer addition. The recorded
5824 * paths form the initial tunnel, which can be optimized later.
5825 * Called on each result obtained for the DHT search.
5827 * @param cls closure
5828 * @param exp when will this value expire
5829 * @param key key of the result
5830 * @param get_path path of the get request
5831 * @param get_path_length lenght of get_path
5832 * @param put_path path of the put request
5833 * @param put_path_length length of the put_path
5834 * @param type type of the result
5835 * @param size number of bytes in data
5836 * @param data pointer to the result data
5838 * TODO: re-issue the request after certain time? cancel after X results?
5841 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5842 const struct GNUNET_HashCode * key,
5843 const struct GNUNET_PeerIdentity *get_path,
5844 unsigned int get_path_length,
5845 const struct GNUNET_PeerIdentity *put_path,
5846 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5847 size_t size, const void *data)
5849 struct MeshPathInfo *path_info = cls;
5850 struct MeshPeerPath *p;
5851 struct GNUNET_PeerIdentity pi;
5854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5855 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5858 p = path_build_from_dht (get_path, get_path_length, put_path,
5860 path_add_to_peers (p, GNUNET_NO);
5862 for (i = 0; i < path_info->peer->ntunnels; i++)
5864 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5865 peer_info_connect (path_info->peer, path_info->t);
5873 * Function to process paths received for a new peer addition. The recorded
5874 * paths form the initial tunnel, which can be optimized later.
5875 * Called on each result obtained for the DHT search.
5877 * @param cls closure
5878 * @param exp when will this value expire
5879 * @param key key of the result
5880 * @param get_path path of the get request
5881 * @param get_path_length lenght of get_path
5882 * @param put_path path of the put request
5883 * @param put_path_length length of the put_path
5884 * @param type type of the result
5885 * @param size number of bytes in data
5886 * @param data pointer to the result data
5889 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5890 const struct GNUNET_HashCode * key,
5891 const struct GNUNET_PeerIdentity *get_path,
5892 unsigned int get_path_length,
5893 const struct GNUNET_PeerIdentity *put_path,
5894 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5895 size_t size, const void *data)
5897 const struct PBlock *pb = data;
5898 const struct GNUNET_PeerIdentity *pi = &pb->id;
5899 struct MeshTunnel *t = cls;
5900 struct MeshPeerInfo *peer_info;
5901 struct MeshPeerPath *p;
5903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5904 if (size != sizeof (struct PBlock))
5906 GNUNET_break_op (0);
5909 if (ntohl(pb->type) != t->type)
5911 GNUNET_break_op (0);
5914 GNUNET_assert (NULL != t->owner);
5915 peer_info = peer_info_get (pi);
5916 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5918 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5920 p = path_build_from_dht (get_path, get_path_length, put_path,
5922 path_add_to_peers (p, GNUNET_NO);
5924 tunnel_add_peer (t, peer_info);
5925 peer_info_connect (peer_info, t);
5930 * Function to process DHT string to regex matching.
5931 * Called on each result obtained for the DHT search.
5933 * @param cls closure (search context)
5934 * @param exp when will this value expire
5935 * @param key key of the result
5936 * @param get_path path of the get request (not used)
5937 * @param get_path_length lenght of get_path (not used)
5938 * @param put_path path of the put request (not used)
5939 * @param put_path_length length of the put_path (not used)
5940 * @param type type of the result
5941 * @param size number of bytes in data
5942 * @param data pointer to the result data
5945 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5946 const struct GNUNET_HashCode * key,
5947 const struct GNUNET_PeerIdentity *get_path,
5948 unsigned int get_path_length,
5949 const struct GNUNET_PeerIdentity *put_path,
5950 unsigned int put_path_length,
5951 enum GNUNET_BLOCK_Type type,
5952 size_t size, const void *data)
5954 const struct MeshRegexAccept *block = data;
5955 struct MeshRegexSearchContext *ctx = cls;
5956 struct MeshRegexSearchInfo *info = ctx->info;
5957 struct MeshPeerPath *p;
5958 struct MeshPeerInfo *peer_info;
5960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5963 peer_info = peer_info_get(&block->id);
5964 p = path_build_from_dht (get_path, get_path_length, put_path,
5966 path_add_to_peers (p, GNUNET_NO);
5969 tunnel_add_peer (info->t, peer_info);
5970 peer_info_connect (peer_info, info->t);
5971 if (0 == info->peer)
5973 info->peer = peer_info->id;
5977 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
5980 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
5981 ®ex_connect_timeout,
5989 * Function to process DHT string to regex matching.
5990 * Called on each result obtained for the DHT search.
5992 * @param cls closure (search context)
5993 * @param exp when will this value expire
5994 * @param key key of the result
5995 * @param get_path path of the get request (not used)
5996 * @param get_path_length lenght of get_path (not used)
5997 * @param put_path path of the put request (not used)
5998 * @param put_path_length length of the put_path (not used)
5999 * @param type type of the result
6000 * @param size number of bytes in data
6001 * @param data pointer to the result data
6003 * TODO: re-issue the request after certain time? cancel after X results?
6006 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6007 const struct GNUNET_HashCode * key,
6008 const struct GNUNET_PeerIdentity *get_path,
6009 unsigned int get_path_length,
6010 const struct GNUNET_PeerIdentity *put_path,
6011 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6012 size_t size, const void *data)
6014 const struct MeshRegexBlock *block = data;
6015 struct MeshRegexSearchContext *ctx = cls;
6016 struct MeshRegexSearchInfo *info = ctx->info;
6020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6021 "DHT GET STRING RETURNED RESULTS\n");
6022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6023 " key: %s\n", GNUNET_h2s (key));
6025 copy = GNUNET_malloc (size);
6026 memcpy (copy, data, size);
6027 GNUNET_break (GNUNET_OK ==
6028 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6030 len = ntohl (block->n_proof);
6032 char proof[len + 1];
6034 memcpy (proof, &block[1], len);
6036 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6038 GNUNET_break_op (0);
6042 len = strlen (info->description);
6043 if (len == ctx->position) // String processed
6045 if (GNUNET_YES == ntohl (block->accepting))
6047 regex_find_path(key, ctx);
6051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6052 // FIXME REGEX this block not successful, wait for more? start timeout?
6056 GNUNET_break (GNUNET_OK ==
6057 GNUNET_MESH_regex_block_iterate (block, size,
6058 ®ex_edge_iterator, ctx));
6062 /******************************************************************************/
6063 /********************* MESH LOCAL HANDLES **************************/
6064 /******************************************************************************/
6068 * Handler for client disconnection
6070 * @param cls closure
6071 * @param client identification of the client; NULL
6072 * for the last call when the server is destroyed
6075 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6077 struct MeshClient *c;
6078 struct MeshClient *next;
6081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6090 if (c->handle != client)
6092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6098 GNUNET_SERVER_client_drop (c->handle);
6099 c->shutting_down = GNUNET_YES;
6100 GNUNET_assert (NULL != c->own_tunnels);
6101 GNUNET_assert (NULL != c->incoming_tunnels);
6102 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6103 &tunnel_destroy_iterator, c);
6104 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6105 &tunnel_destroy_iterator, c);
6106 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6107 &tunnel_destroy_iterator, c);
6108 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6109 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6110 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6112 /* deregister clients applications */
6113 if (NULL != c->apps)
6115 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6116 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6118 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6119 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6121 GNUNET_SCHEDULER_cancel (announce_applications_task);
6122 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6124 if (NULL != c->types)
6125 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6126 for (i = 0; i < c->n_regex; i++)
6128 GNUNET_free (c->regexes[i]);
6130 GNUNET_free_non_null (c->regexes);
6131 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6132 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6134 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6137 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6146 * Handler for new clients
6148 * @param cls closure
6149 * @param client identification of the client
6150 * @param message the actual message, which includes messages the client wants
6153 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6154 const struct GNUNET_MessageHeader *message)
6156 struct GNUNET_MESH_ClientConnect *cc_msg;
6157 struct MeshClient *c;
6158 GNUNET_MESH_ApplicationType *a;
6165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6166 /* Check data sanity */
6167 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6168 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6169 ntypes = ntohs (cc_msg->types);
6170 napps = ntohs (cc_msg->applications);
6172 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6175 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6179 /* Create new client structure */
6180 c = GNUNET_malloc (sizeof (struct MeshClient));
6181 c->id = next_client_id++;
6182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6184 GNUNET_SERVER_client_keep (client);
6185 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6188 GNUNET_MESH_ApplicationType at;
6189 struct GNUNET_HashCode hc;
6191 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6192 for (i = 0; i < napps; i++)
6195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6196 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6197 /* store in clients hashmap */
6198 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6199 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6200 /* store in global hashmap, for announcements */
6201 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6202 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6204 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6205 announce_applications_task =
6206 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6212 struct GNUNET_HashCode hc;
6214 t = (uint16_t *) & a[napps];
6215 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6216 for (i = 0; i < ntypes; i++)
6219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6220 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6222 /* store in clients hashmap */
6223 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6224 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6225 /* store in global hashmap */
6226 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6227 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6231 " client has %u+%u subscriptions\n", napps, ntypes);
6233 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6234 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6235 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6236 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6237 GNUNET_SERVER_notification_context_add (nc, client);
6238 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6240 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6246 * Handler for clients announcing available services by a regular expression.
6248 * @param cls closure
6249 * @param client identification of the client
6250 * @param message the actual message, which includes messages the client wants
6253 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6254 const struct GNUNET_MessageHeader *message)
6256 struct MeshClient *c;
6260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6262 /* Sanity check for client registration */
6263 if (NULL == (c = client_get (client)))
6266 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6271 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6272 regex = GNUNET_malloc (len + 1);
6273 memcpy (regex, &message[1], len);
6275 GNUNET_array_append (c->regexes, c->n_regex, regex);
6276 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6278 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6284 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6290 * Handler for requests of new tunnels
6292 * @param cls closure
6293 * @param client identification of the client
6294 * @param message the actual message
6297 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6298 const struct GNUNET_MessageHeader *message)
6300 struct GNUNET_MESH_TunnelMessage *t_msg;
6301 struct MeshTunnel *t;
6302 struct MeshClient *c;
6303 MESH_TunnelNumber tid;
6305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6307 /* Sanity check for client registration */
6308 if (NULL == (c = client_get (client)))
6311 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6316 /* Message sanity check */
6317 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6320 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6324 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6325 /* Sanity check for tunnel numbering */
6326 tid = ntohl (t_msg->tunnel_id);
6327 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6330 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6333 /* Sanity check for duplicate tunnel IDs */
6334 if (NULL != tunnel_get_by_local_id (c, tid))
6337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6341 while (NULL != tunnel_get_by_pi (myid, next_tid))
6342 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6343 t = tunnel_new (myid, next_tid++, c, tid);
6346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6351 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6353 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6354 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6357 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6363 * Handler for requests of deleting tunnels
6365 * @param cls closure
6366 * @param client identification of the client
6367 * @param message the actual message
6370 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6371 const struct GNUNET_MessageHeader *message)
6373 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6374 struct MeshClient *c;
6375 struct MeshTunnel *t;
6376 MESH_TunnelNumber tid;
6378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6379 "Got a DESTROY TUNNEL from client!\n");
6381 /* Sanity check for client registration */
6382 if (NULL == (c = client_get (client)))
6385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6390 /* Message sanity check */
6391 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6394 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6398 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6400 /* Retrieve tunnel */
6401 tid = ntohl (tunnel_msg->tunnel_id);
6402 t = tunnel_get_by_local_id(c, tid);
6405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6407 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6410 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6412 client_ignore_tunnel (c, t);
6414 // TODO: when to destroy incoming tunnel?
6415 if (t->nclients == 0)
6417 GNUNET_assert (GNUNET_YES ==
6418 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6420 GNUNET_assert (GNUNET_YES ==
6421 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6422 &my_full_id.hashPubKey,
6426 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6429 send_client_tunnel_disconnect(t, c);
6430 client_delete_tunnel(c, t);
6432 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6434 tunnel_send_destroy (t);
6435 t->destroy = GNUNET_YES;
6436 // The tunnel will be destroyed when the last message is transmitted.
6437 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6443 * Handler for requests of seeting tunnel's speed.
6445 * @param cls Closure (unused).
6446 * @param client Identification of the client.
6447 * @param message The actual message.
6450 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6451 const struct GNUNET_MessageHeader *message)
6453 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6454 struct MeshClient *c;
6455 struct MeshTunnel *t;
6456 MESH_TunnelNumber tid;
6458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6459 "Got a SPEED request from client!\n");
6461 /* Sanity check for client registration */
6462 if (NULL == (c = client_get (client)))
6465 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6471 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6473 /* Retrieve tunnel */
6474 tid = ntohl (tunnel_msg->tunnel_id);
6475 t = tunnel_get_by_local_id(c, tid);
6478 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6480 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6484 switch (ntohs(message->type))
6486 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6487 t->speed_min = GNUNET_YES;
6489 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6490 t->speed_min = GNUNET_NO;
6495 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6500 * Handler for requests of seeting tunnel's buffering policy.
6502 * @param cls Closure (unused).
6503 * @param client Identification of the client.
6504 * @param message The actual message.
6507 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6508 const struct GNUNET_MessageHeader *message)
6510 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6511 struct MeshClient *c;
6512 struct MeshTunnel *t;
6513 MESH_TunnelNumber tid;
6515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6516 "Got a BUFFER request from client!\n");
6518 /* Sanity check for client registration */
6519 if (NULL == (c = client_get (client)))
6522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6527 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6529 /* Retrieve tunnel */
6530 tid = ntohl (tunnel_msg->tunnel_id);
6531 t = tunnel_get_by_local_id(c, tid);
6534 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6536 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6540 switch (ntohs(message->type))
6542 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6543 t->nobuffer = GNUNET_NO;
6545 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6546 t->nobuffer = GNUNET_YES;
6552 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6557 * Handler for connection requests to new peers
6559 * @param cls closure
6560 * @param client identification of the client
6561 * @param message the actual message (PeerControl)
6564 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6565 const struct GNUNET_MessageHeader *message)
6567 struct GNUNET_MESH_PeerControl *peer_msg;
6568 struct MeshPeerInfo *peer_info;
6569 struct MeshClient *c;
6570 struct MeshTunnel *t;
6571 MESH_TunnelNumber tid;
6573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6574 /* Sanity check for client registration */
6575 if (NULL == (c = client_get (client)))
6578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6583 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6585 /* Sanity check for message size */
6586 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6589 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6593 /* Tunnel exists? */
6594 tid = ntohl (peer_msg->tunnel_id);
6595 t = tunnel_get_by_local_id (c, tid);
6599 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6603 /* Does client own tunnel? */
6604 if (t->owner->handle != client)
6607 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6611 GNUNET_i2s (&peer_msg->peer));
6612 peer_info = peer_info_get (&peer_msg->peer);
6614 tunnel_add_peer (t, peer_info);
6615 peer_info_connect (peer_info, t);
6617 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6623 * Handler for disconnection requests of peers in a tunnel
6625 * @param cls closure
6626 * @param client identification of the client
6627 * @param message the actual message (PeerControl)
6630 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6631 const struct GNUNET_MessageHeader *message)
6633 struct GNUNET_MESH_PeerControl *peer_msg;
6634 struct MeshPeerInfo *peer_info;
6635 struct MeshClient *c;
6636 struct MeshTunnel *t;
6637 MESH_TunnelNumber tid;
6639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6640 /* Sanity check for client registration */
6641 if (NULL == (c = client_get (client)))
6644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6649 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6651 /* Sanity check for message size */
6652 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6655 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6659 /* Tunnel exists? */
6660 tid = ntohl (peer_msg->tunnel_id);
6661 t = tunnel_get_by_local_id (c, tid);
6665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6670 /* Does client own tunnel? */
6671 if (t->owner->handle != client)
6674 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6679 GNUNET_i2s (&peer_msg->peer));
6680 /* Is the peer in the tunnel? */
6682 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6683 if (NULL == peer_info)
6686 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6690 /* Ok, delete peer from tunnel */
6691 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6692 &peer_msg->peer.hashPubKey);
6694 send_destroy_path (t, peer_info->id);
6695 tunnel_delete_peer (t, peer_info->id);
6696 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6701 * Handler for blacklist requests of peers in a tunnel
6703 * @param cls closure
6704 * @param client identification of the client
6705 * @param message the actual message (PeerControl)
6707 * FIXME implement DHT block bloomfilter
6710 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6711 const struct GNUNET_MessageHeader *message)
6713 struct GNUNET_MESH_PeerControl *peer_msg;
6714 struct MeshClient *c;
6715 struct MeshTunnel *t;
6716 MESH_TunnelNumber tid;
6718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6719 /* Sanity check for client registration */
6720 if (NULL == (c = client_get (client)))
6723 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6728 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6730 /* Sanity check for message size */
6731 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6738 /* Tunnel exists? */
6739 tid = ntohl (peer_msg->tunnel_id);
6740 t = tunnel_get_by_local_id (c, tid);
6744 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6749 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6750 GNUNET_PEER_intern(&peer_msg->peer));
6755 * Handler for unblacklist requests of peers in a tunnel
6757 * @param cls closure
6758 * @param client identification of the client
6759 * @param message the actual message (PeerControl)
6762 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6763 const struct GNUNET_MessageHeader *message)
6765 struct GNUNET_MESH_PeerControl *peer_msg;
6766 struct MeshClient *c;
6767 struct MeshTunnel *t;
6768 MESH_TunnelNumber tid;
6772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6773 /* Sanity check for client registration */
6774 if (NULL == (c = client_get (client)))
6777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6782 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6784 /* Sanity check for message size */
6785 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6788 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6792 /* Tunnel exists? */
6793 tid = ntohl (peer_msg->tunnel_id);
6794 t = tunnel_get_by_local_id (c, tid);
6798 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6803 /* if peer is not known, complain */
6804 pid = GNUNET_PEER_search (&peer_msg->peer);
6811 /* search and remove from list */
6812 for (i = 0; i < t->nblacklisted; i++)
6814 if (t->blacklisted[i] == pid)
6816 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6817 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6822 /* if peer hasn't been blacklisted, complain */
6828 * Handler for connection requests to new peers by type
6830 * @param cls closure
6831 * @param client identification of the client
6832 * @param message the actual message (ConnectPeerByType)
6835 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6836 const struct GNUNET_MessageHeader *message)
6838 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6839 struct MeshClient *c;
6840 struct MeshTunnel *t;
6841 struct GNUNET_HashCode hash;
6842 MESH_TunnelNumber tid;
6844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6845 /* Sanity check for client registration */
6846 if (NULL == (c = client_get (client)))
6849 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6854 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6856 /* Sanity check for message size */
6857 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6858 ntohs (connect_msg->header.size))
6861 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6865 /* Tunnel exists? */
6866 tid = ntohl (connect_msg->tunnel_id);
6867 t = tunnel_get_by_local_id (c, tid);
6871 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6875 /* Does client own tunnel? */
6876 if (t->owner->handle != client)
6879 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6883 /* Do WE have the service? */
6884 t->type = ntohl (connect_msg->type);
6885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6886 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6887 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6890 /* Yes! Fast forward, add ourselves to the tunnel and send the
6891 * good news to the client, and alert the destination client of
6892 * an incoming tunnel.
6894 * FIXME send a path create to self, avoid code duplication
6896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6897 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6898 peer_info_get (&my_full_id),
6899 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6902 send_client_peer_connected (t, myid);
6903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6904 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6906 t->local_tid_dest = next_local_tid++;
6907 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6908 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6909 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6913 /* Ok, lets find a peer offering the service */
6914 if (NULL != t->dht_get_type)
6916 GNUNET_DHT_get_stop (t->dht_get_type);
6918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6919 GNUNET_h2s (&hash));
6921 GNUNET_DHT_get_start (dht_handle,
6922 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6924 dht_replication_level,
6925 GNUNET_DHT_RO_RECORD_ROUTE |
6926 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6928 &dht_get_type_handler, t);
6930 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6936 * Handler for connection requests to new peers by a string service description.
6938 * @param cls closure
6939 * @param client identification of the client
6940 * @param message the actual message, which includes messages the client wants
6943 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6944 const struct GNUNET_MessageHeader *message)
6946 struct GNUNET_MESH_ConnectPeerByString *msg;
6947 struct MeshRegexSearchContext *ctx;
6948 struct MeshRegexSearchInfo *info;
6949 struct GNUNET_DHT_GetHandle *get_h;
6950 struct GNUNET_HashCode key;
6951 struct MeshTunnel *t;
6952 struct MeshClient *c;
6953 MESH_TunnelNumber tid;
6957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6958 "Connect by string started\n");
6959 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6960 size = htons (message->size);
6962 /* Sanity check for client registration */
6963 if (NULL == (c = client_get (client)))
6966 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6971 /* Message size sanity check */
6972 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
6975 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6979 /* Tunnel exists? */
6980 tid = ntohl (msg->tunnel_id);
6981 t = tunnel_get_by_local_id (c, tid);
6985 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6989 /* Does client own tunnel? */
6990 if (t->owner->handle != client)
6993 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6998 " on tunnel %s [%u]\n",
6999 GNUNET_i2s(&my_full_id),
7002 /* Only one connect_by_string allowed at the same time! */
7003 /* FIXME: allow more, return handle at api level to cancel, document */
7004 if (NULL != t->regex_ctx)
7007 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7011 /* Find string itself */
7012 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7013 string = (const char *) &msg[1];
7015 /* Initialize context */
7016 size = GNUNET_REGEX_get_first_key(string, len, &key);
7017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7018 " consumed %u bits out of %u\n", size, len);
7019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7020 " looking for %s\n", GNUNET_h2s (&key));
7022 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7024 info->description = GNUNET_malloc (len + 1);
7025 memcpy (info->description, string, len);
7026 info->description[len] = '\0';
7027 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7028 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7031 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7032 ctx->position = size;
7036 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7038 /* Start search in DHT */
7039 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7040 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7041 &key, /* key to search */
7042 dht_replication_level, /* replication level */
7043 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7044 NULL, /* xquery */ // FIXME BLOOMFILTER
7045 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7046 &dht_get_string_handler, ctx);
7048 GNUNET_break (GNUNET_OK ==
7049 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7052 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7054 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7060 * Handler for client traffic directed to one peer
7062 * @param cls closure
7063 * @param client identification of the client
7064 * @param message the actual message
7067 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7068 const struct GNUNET_MessageHeader *message)
7070 struct MeshClient *c;
7071 struct MeshTunnel *t;
7072 struct MeshPeerInfo *pi;
7073 struct GNUNET_MESH_Unicast *data_msg;
7074 MESH_TunnelNumber tid;
7077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7078 "Got a unicast request from a client!\n");
7080 /* Sanity check for client registration */
7081 if (NULL == (c = client_get (client)))
7084 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7089 data_msg = (struct GNUNET_MESH_Unicast *) message;
7091 /* Sanity check for message size */
7092 size = ntohs (message->size);
7093 if (sizeof (struct GNUNET_MESH_Unicast) +
7094 sizeof (struct GNUNET_MessageHeader) > size)
7097 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7101 /* Tunnel exists? */
7102 tid = ntohl (data_msg->tid);
7103 t = tunnel_get_by_local_id (c, tid);
7107 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7111 /* Is it a local tunnel? Then, does client own the tunnel? */
7112 if (t->owner->handle != client)
7115 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7119 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7120 &data_msg->destination.hashPubKey);
7121 /* Is the selected peer in the tunnel? */
7125 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7129 /* PID should be as expected */
7130 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7134 "Unicast PID, expected %u, got %u\n",
7135 t->fwd_pid + 1, ntohl (data_msg->pid));
7136 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7140 /* Ok, everything is correct, send the message
7141 * (pretend we got it from a mesh peer)
7144 /* Work around const limitation */
7145 char buf[ntohs (message->size)] GNUNET_ALIGN;
7146 struct GNUNET_MESH_Unicast *copy;
7148 copy = (struct GNUNET_MESH_Unicast *) buf;
7149 memcpy (buf, data_msg, size);
7150 copy->oid = my_full_id;
7151 copy->tid = htonl (t->id.tid);
7152 copy->ttl = htonl (default_ttl);
7153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7154 " calling generic handler...\n");
7155 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7158 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7165 * Handler for client traffic directed to the origin
7167 * @param cls closure
7168 * @param client identification of the client
7169 * @param message the actual message
7172 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7173 const struct GNUNET_MessageHeader *message)
7175 struct GNUNET_MESH_ToOrigin *data_msg;
7176 struct MeshTunnelClientInfo *clinfo;
7177 struct MeshClient *c;
7178 struct MeshTunnel *t;
7179 MESH_TunnelNumber tid;
7182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7183 "Got a ToOrigin request from a client!\n");
7184 /* Sanity check for client registration */
7185 if (NULL == (c = client_get (client)))
7188 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7193 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7195 /* Sanity check for message size */
7196 size = ntohs (message->size);
7197 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7198 sizeof (struct GNUNET_MessageHeader) > size)
7201 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7205 /* Tunnel exists? */
7206 tid = ntohl (data_msg->tid);
7207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7208 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7211 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7214 t = tunnel_get_by_local_id (c, tid);
7217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7220 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7224 /* It should be sent by someone who has this as incoming tunnel. */
7225 if (GNUNET_NO == client_knows_tunnel (c, t))
7228 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7232 /* PID should be as expected */
7233 clinfo = tunnel_get_client_fc (t, c);
7234 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7238 "To Origin PID, expected %u, got %u\n",
7239 clinfo->bck_pid + 1,
7240 ntohl (data_msg->pid));
7241 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7245 /* Ok, everything is correct, send the message
7246 * (pretend we got it from a mesh peer)
7250 char buf[ntohs (message->size)] GNUNET_ALIGN;
7251 struct GNUNET_MESH_ToOrigin *copy;
7253 /* Work around const limitation */
7254 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7255 memcpy (buf, data_msg, size);
7256 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7257 copy->tid = htonl (t->id.tid);
7258 copy->ttl = htonl (default_ttl);
7259 if (ntohl (copy->pid) != (t->bck_pid + 1))
7262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7263 "To Origin PID, expected %u, got %u\n",
7269 copy->sender = my_full_id;
7270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7271 " calling generic handler...\n");
7272 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7274 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7281 * Handler for client traffic directed to all peers in a tunnel
7283 * @param cls closure
7284 * @param client identification of the client
7285 * @param message the actual message
7288 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7289 const struct GNUNET_MessageHeader *message)
7291 struct MeshClient *c;
7292 struct MeshTunnel *t;
7293 struct GNUNET_MESH_Multicast *data_msg;
7294 MESH_TunnelNumber tid;
7296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7297 "Got a multicast request from a client!\n");
7299 /* Sanity check for client registration */
7300 if (NULL == (c = client_get (client)))
7303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7308 data_msg = (struct GNUNET_MESH_Multicast *) message;
7310 /* Sanity check for message size */
7311 if (sizeof (struct GNUNET_MESH_Multicast) +
7312 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7315 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7319 /* Tunnel exists? */
7320 tid = ntohl (data_msg->tid);
7321 t = tunnel_get_by_local_id (c, tid);
7325 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7326 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7328 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7332 /* Does client own tunnel? */
7333 if (t->owner->handle != client)
7336 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7340 /* PID should be as expected */
7341 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7344 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7345 "Multicast PID, expected %u, got %u\n",
7346 t->fwd_pid + 1, ntohl (data_msg->pid));
7347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7352 char buf[ntohs (message->size)] GNUNET_ALIGN;
7353 struct GNUNET_MESH_Multicast *copy;
7355 copy = (struct GNUNET_MESH_Multicast *) buf;
7356 memcpy (buf, message, ntohs (message->size));
7357 copy->oid = my_full_id;
7358 copy->tid = htonl (t->id.tid);
7359 copy->ttl = htonl (default_ttl);
7360 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7362 " calling generic handler...\n");
7363 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7366 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7372 * Handler for client's ACKs for payload traffic.
7374 * @param cls Closure (unused).
7375 * @param client Identification of the client.
7376 * @param message The actual message.
7379 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7380 const struct GNUNET_MessageHeader *message)
7382 struct GNUNET_MESH_LocalAck *msg;
7383 struct MeshTunnel *t;
7384 struct MeshClient *c;
7385 MESH_TunnelNumber tid;
7388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7389 /* Sanity check for client registration */
7390 if (NULL == (c = client_get (client)))
7393 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7398 msg = (struct GNUNET_MESH_LocalAck *) message;
7400 /* Tunnel exists? */
7401 tid = ntohl (msg->tunnel_id);
7402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7403 t = tunnel_get_by_local_id (c, tid);
7407 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7408 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7409 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7413 ack = ntohl (msg->max_pid);
7414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7416 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7417 if (NULL != t->owner && t->owner->handle == client)
7419 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7421 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7425 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7426 tunnel_set_client_fwd_ack (t, c, ack);
7427 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7430 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7437 * Functions to handle messages from clients
7439 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7440 {&handle_local_new_client, NULL,
7441 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7442 {&handle_local_announce_regex, NULL,
7443 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7444 {&handle_local_tunnel_create, NULL,
7445 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7446 sizeof (struct GNUNET_MESH_TunnelMessage)},
7447 {&handle_local_tunnel_destroy, NULL,
7448 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7449 sizeof (struct GNUNET_MESH_TunnelMessage)},
7450 {&handle_local_tunnel_speed, NULL,
7451 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7452 sizeof (struct GNUNET_MESH_TunnelMessage)},
7453 {&handle_local_tunnel_speed, NULL,
7454 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7455 sizeof (struct GNUNET_MESH_TunnelMessage)},
7456 {&handle_local_tunnel_buffer, NULL,
7457 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7458 sizeof (struct GNUNET_MESH_TunnelMessage)},
7459 {&handle_local_tunnel_buffer, NULL,
7460 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7461 sizeof (struct GNUNET_MESH_TunnelMessage)},
7462 {&handle_local_connect_add, NULL,
7463 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7464 sizeof (struct GNUNET_MESH_PeerControl)},
7465 {&handle_local_connect_del, NULL,
7466 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7467 sizeof (struct GNUNET_MESH_PeerControl)},
7468 {&handle_local_blacklist, NULL,
7469 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7470 sizeof (struct GNUNET_MESH_PeerControl)},
7471 {&handle_local_unblacklist, NULL,
7472 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7473 sizeof (struct GNUNET_MESH_PeerControl)},
7474 {&handle_local_connect_by_type, NULL,
7475 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7476 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7477 {&handle_local_connect_by_string, NULL,
7478 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7479 {&handle_local_unicast, NULL,
7480 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7481 {&handle_local_to_origin, NULL,
7482 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7483 {&handle_local_multicast, NULL,
7484 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7485 {&handle_local_ack, NULL,
7486 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7487 sizeof (struct GNUNET_MESH_LocalAck)},
7493 * To be called on core init/fail.
7495 * @param cls service closure
7496 * @param server handle to the server for this service
7497 * @param identity the public identity of this peer
7500 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7501 const struct GNUNET_PeerIdentity *identity)
7504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7505 core_handle = server;
7506 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7510 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7512 GNUNET_abort(); // Try harder
7519 * Method called whenever a given peer connects.
7521 * @param cls closure
7522 * @param peer peer identity this notification is about
7523 * @param atsi performance data for the connection
7524 * @param atsi_count number of records in 'atsi'
7527 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7528 const struct GNUNET_ATS_Information *atsi,
7529 unsigned int atsi_count)
7531 struct MeshPeerInfo *peer_info;
7532 struct MeshPeerPath *path;
7534 DEBUG_CONN ("Peer connected\n");
7535 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7536 peer_info = peer_info_get (peer);
7537 if (myid == peer_info->id)
7539 DEBUG_CONN (" (self)\n");
7544 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7546 path = path_new (2);
7547 path->peers[0] = myid;
7548 path->peers[1] = peer_info->id;
7549 GNUNET_PEER_change_rc (myid, 1);
7550 GNUNET_PEER_change_rc (peer_info->id, 1);
7551 peer_info_add_path (peer_info, path, GNUNET_YES);
7552 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7558 * Method called whenever a peer disconnects.
7560 * @param cls closure
7561 * @param peer peer identity this notification is about
7564 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7566 struct MeshPeerInfo *pi;
7567 struct MeshPeerQueue *q;
7568 struct MeshPeerQueue *n;
7570 DEBUG_CONN ("Peer disconnected\n");
7571 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7583 /* try to reroute this traffic instead */
7584 queue_destroy(q, GNUNET_YES);
7588 peer_info_remove_path (pi, pi->id, myid);
7591 DEBUG_CONN (" (self)\n");
7593 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7598 /******************************************************************************/
7599 /************************ MAIN FUNCTIONS ****************************/
7600 /******************************************************************************/
7603 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7605 * @param cls closure
7606 * @param key current key code
7607 * @param value value in the hash map
7608 * @return GNUNET_YES if we should continue to iterate,
7612 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7614 struct MeshTunnel *t = value;
7621 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7623 * @param cls closure
7624 * @param key current key code
7625 * @param value value in the hash map
7626 * @return GNUNET_YES if we should continue to iterate,
7630 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7632 struct MeshPeerInfo *p = value;
7633 struct MeshPeerQueue *q;
7634 struct MeshPeerQueue *n;
7642 queue_destroy(q, GNUNET_YES);
7646 peer_info_destroy (p);
7651 * Task run during shutdown.
7657 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7661 if (core_handle != NULL)
7663 GNUNET_CORE_disconnect (core_handle);
7666 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7667 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7668 if (dht_handle != NULL)
7670 GNUNET_DHT_disconnect (dht_handle);
7675 GNUNET_SERVER_notification_context_destroy (nc);
7678 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7680 GNUNET_SCHEDULER_cancel (announce_id_task);
7681 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7687 * Process mesh requests.
7689 * @param cls closure
7690 * @param server the initialized server
7691 * @param c configuration to use
7694 run (void *cls, struct GNUNET_SERVER_Handle *server,
7695 const struct GNUNET_CONFIGURATION_Handle *c)
7697 struct MeshPeerInfo *peer;
7698 struct MeshPeerPath *p;
7701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7702 server_handle = server;
7703 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7704 NULL, /* Closure passed to MESH functions */
7705 &core_init, /* Call core_init once connected */
7706 &core_connect, /* Handle connects */
7707 &core_disconnect, /* remove peers on disconnects */
7708 NULL, /* Don't notify about all incoming messages */
7709 GNUNET_NO, /* For header only in notification */
7710 NULL, /* Don't notify about all outbound messages */
7711 GNUNET_NO, /* For header-only out notification */
7712 core_handlers); /* Register these handlers */
7714 if (core_handle == NULL)
7717 GNUNET_SCHEDULER_shutdown ();
7722 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7725 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7727 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7729 GNUNET_SCHEDULER_shutdown ();
7734 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7735 &refresh_path_time))
7737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7739 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7740 "refresh path time");
7741 GNUNET_SCHEDULER_shutdown ();
7746 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7747 &app_announce_time))
7749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7751 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7752 "app announce time");
7753 GNUNET_SCHEDULER_shutdown ();
7758 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7763 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7764 "id announce time");
7765 GNUNET_SCHEDULER_shutdown ();
7770 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7771 &unacknowledged_wait_time))
7773 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7775 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7776 "unacknowledged wait time");
7777 GNUNET_SCHEDULER_shutdown ();
7782 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7785 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7787 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7789 GNUNET_SCHEDULER_shutdown ();
7794 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7797 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7799 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7801 GNUNET_SCHEDULER_shutdown ();
7806 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7809 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7811 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7813 GNUNET_SCHEDULER_shutdown ();
7818 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7821 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7823 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7829 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7830 &dht_replication_level))
7832 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7834 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7835 "dht replication level", 10);
7836 dht_replication_level = 10;
7840 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7841 GNUNET_free (keyfile);
7842 if (my_private_key == NULL)
7844 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7845 _("Mesh service could not access hostkey. Exiting.\n"));
7846 GNUNET_SCHEDULER_shutdown ();
7849 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7850 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7851 &my_full_id.hashPubKey);
7852 myid = GNUNET_PEER_intern (&my_full_id);
7853 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7854 "Mesh for peer [%s] starting\n",
7855 GNUNET_i2s(&my_full_id));
7857 // transport_handle = GNUNET_TRANSPORT_connect(c,
7864 dht_handle = GNUNET_DHT_connect (c, 64);
7865 if (dht_handle == NULL)
7870 stats = GNUNET_STATISTICS_create ("mesh", c);
7874 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7876 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7877 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7878 peers = GNUNET_CONTAINER_multihashmap_create (32);
7879 applications = GNUNET_CONTAINER_multihashmap_create (32);
7880 types = GNUNET_CONTAINER_multihashmap_create (32);
7882 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7883 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7884 GNUNET_SERVER_disconnect_notify (server_handle,
7885 &handle_local_client_disconnect, NULL);
7889 clients_tail = NULL;
7892 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7893 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7895 /* Create a peer_info for the local peer */
7896 peer = peer_info_get (&my_full_id);
7899 GNUNET_PEER_change_rc (myid, 1);
7900 peer_info_add_path (peer, p, GNUNET_YES);
7902 /* Scheduled the task to clean up when shutdown is called */
7903 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7910 * The main function for the mesh service.
7912 * @param argc number of arguments from the command line
7913 * @param argv command line arguments
7914 * @return 0 ok, 1 on error
7917 main (int argc, char *const *argv)
7921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7924 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7930 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7931 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
7932 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);