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?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "mesh_block_lib.h"
54 #include "gnunet_dht_service.h"
55 #include "gnunet_statistics_service.h"
56 #include "gnunet_regex_lib.h"
58 #define MESH_BLOOM_SIZE 128
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_YES
64 #if MESH_DEBUG_CONNECTION
65 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
67 #define DEBUG_CONN(...)
71 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
73 #define DEBUG_DHT(...)
80 struct timespec __mesh_start;
81 struct timespec __mesh_end;
82 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
83 #define INTERVAL_END \
85 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
86 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
87 if (__diff < 0) __diff += 1000000000;\
91 #define INTERVAL_SHOW \
93 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
95 #define INTERVAL_START
100 /******************************************************************************/
101 /************************ DATA STRUCTURES ****************************/
102 /******************************************************************************/
104 /** FWD declaration */
110 * Struct representing a piece of data being sent to other peers
114 /** Tunnel it belongs to. */
115 struct MeshTunnel *t;
117 /** How many remaining neighbors still hav't got it. */
118 unsigned int reference_counter;
120 /** How many remaining neighbors we need to send this to. */
121 unsigned int total_out;
123 /** Size of the data. */
132 * Struct containing info about a queued transmission to this peer
139 struct MeshPeerQueue *next;
144 struct MeshPeerQueue *prev;
147 * Peer this transmission is directed to.
149 struct MeshPeerInfo *peer;
152 * Tunnel this message belongs to.
154 struct MeshTunnel *tunnel;
157 * Pointer to info stucture used as cls.
167 * Size of the message
174 * Struct containing all info possibly needed to build a package when called
177 struct MeshTransmissionDescriptor
179 /** ID of the tunnel this packet travels in */
180 struct MESH_TunnelID *origin;
182 /** Who was this message being sent to */
183 struct MeshPeerInfo *peer;
185 /** Ultimate destination of the packet */
186 GNUNET_PEER_Id destination;
188 /** Data descriptor */
189 struct MeshData* mesh_data;
194 * Struct containing all information regarding a given peer
204 * Last time we heard from this peer
206 struct GNUNET_TIME_Absolute last_contact;
209 * Number of attempts to reconnect so far
211 int n_reconnect_attempts;
214 * Paths to reach the peer, ordered by ascending hop count
216 struct MeshPeerPath *path_head;
219 * Paths to reach the peer, ordered by ascending hop count
221 struct MeshPeerPath *path_tail;
224 * Handle to stop the DHT search for a path to this peer
226 struct GNUNET_DHT_GetHandle *dhtget;
229 * Closure given to the DHT GET
231 struct MeshPathInfo *dhtgetcls;
234 * Array of tunnels this peer participates in
235 * (most probably a small amount, therefore not a hashmap)
236 * When the path to the peer changes, notify these tunnels to let them
237 * re-adjust their path trees.
239 struct MeshTunnel **tunnels;
242 * Number of tunnels this peers participates in
244 unsigned int ntunnels;
247 * Transmission queue to core DLL head
249 struct MeshPeerQueue *queue_head;
252 * Transmission queue to core DLL tail
254 struct MeshPeerQueue *queue_tail;
257 * How many messages are in the queue to this peer.
259 unsigned int queue_n;
262 * Handle to for queued transmissions
264 struct GNUNET_CORE_TransmitHandle *core_transmit;
269 * Globally unique tunnel identification (owner + number)
270 * DO NOT USE OVER THE NETWORK
275 * Node that owns the tunnel
280 * Tunnel number to differentiate all the tunnels owned by the node oid
281 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
283 MESH_TunnelNumber tid;
288 * Struct containing all information regarding a tunnel
289 * For an intermediate node the improtant info used will be:
290 * - id Tunnel unique identification
291 * - paths[0] To know where to send it next
292 * - metainfo: ready, speeds, accounting
299 struct MESH_TunnelID id;
302 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
304 MESH_TunnelNumber local_tid;
307 * Local tunnel number for local destination clients (incoming number)
308 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
311 MESH_TunnelNumber local_tid_dest;
314 * Is the speed on the tunnel limited to the slowest peer?
319 * Is the tunnel bufferless (minimum latency)?
324 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
329 * Packet ID of the last bck packet sent (unique counter per hop).
334 * SKIP value for this tunnel.
339 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
340 * Contains the Flow Control info: FWD ACK value received,
341 * last BCK ACK sent, PID and SKIP values.
343 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
346 * Last ACK sent towards the origin (for traffic towards leaf node).
348 uint32_t last_fwd_ack;
351 * BCK ACK value received from the hop towards the owner of the tunnel,
352 * (previous node / owner): up to what message PID can we sent back to him.
357 * How many messages are in the forward queue (towards leaves).
359 unsigned int fwd_queue_n;
362 * How many messages do we accept in the forward queue.
364 unsigned int fwd_queue_max;
367 * How many messages are in the backward queue (towards origin).
369 unsigned int bck_queue_n;
372 * How many messages do we accept in the backward queue.
374 unsigned int bck_queue_max;
377 * Last time the tunnel was used
379 struct GNUNET_TIME_Absolute timestamp;
382 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
383 * containing peers added by id or by type, not intermediate peers.
385 struct GNUNET_CONTAINER_MultiHashMap *peers;
388 * Number of peers that are connected and potentially ready to receive data
390 unsigned int peers_ready;
393 * Number of peers that have been added to the tunnel
395 unsigned int peers_total;
398 * Client owner of the tunnel, if any
400 struct MeshClient *owner;
403 * Clients that have been informed about and want to stay in the tunnel.
405 struct MeshClient **clients;
408 * Flow control info for each client.
410 struct MeshTunnelClientInfo *clients_fc;
413 * Number of elements in clients/clients_fc
415 unsigned int nclients;
418 * Clients that have been informed but requested to leave the tunnel.
420 struct MeshClient **ignore;
423 * Number of elements in clients
425 unsigned int nignore;
430 GNUNET_PEER_Id *blacklisted;
433 * Number of elements in blacklisted
435 unsigned int nblacklisted;
438 * Bloomfilter (for peer identities) to stop circular routes
440 char bloomfilter[MESH_BLOOM_SIZE];
445 struct MeshTunnelTree *tree;
448 * Application type we are looking for in this tunnel
450 GNUNET_MESH_ApplicationType type;
453 * Used to search peers offering a service
455 struct GNUNET_DHT_GetHandle *dht_get_type;
458 * Initial context of the regex search for a connect_by_string
460 struct MeshRegexSearchContext *regex_ctx;
463 * Task to keep the used paths alive
465 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
468 * Task to destroy the tunnel after timeout
470 * FIXME: merge the two? a tunnel will have either
471 * a path refresh OR a timeout, never both!
473 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
476 * Flag to signal the destruction of the tunnel.
477 * If this is set GNUNET_YES the tunnel will be destroyed
478 * when the queue is empty.
485 * Info about a child node in a tunnel, needed to perform flow control.
487 struct MeshTunnelChildInfo
490 * ID of the child node.
505 * Maximum PID allowed (FWD ACK received).
510 * Last ACK sent to that child (BCK ACK).
515 * Circular buffer pointing to MeshPeerQueue elements.
516 * Size determined by the tunnel queue size.
518 struct MeshPeerQueue **send_buffer;
521 * Index of the oldest element in the send_buffer.
523 unsigned int send_buffer_start;
526 * How many elements are already in the buffer.
528 unsigned int send_buffer_n;
533 * Info about a leaf client of a tunnel, needed to perform flow control.
535 struct MeshTunnelClientInfo
538 * PID of the last packet sent to the client (FWD).
543 * PID of the last packet received from the client (BCK).
548 * Maximum PID allowed (FWD ACK received).
553 * Last ACK sent to that child (BCK ACK).
561 * Info collected during iteration of child nodes in order to get the ACK value
564 struct MeshTunnelChildIteratorContext
567 * Tunnel whose info is being collected.
569 struct MeshTunnel *t;
572 * Is this context initialized? Is the value in max_child_ack valid?
577 * Maximum child ACK so far.
579 uint32_t max_child_ack;
582 * Number of children nodes
584 unsigned int nchildren;
589 * Info needed to work with tunnel paths and peers
596 struct MeshTunnel *t;
599 * Neighbouring peer to whom we send the packet to
601 struct MeshPeerInfo *peer;
606 struct MeshPeerPath *path;
611 * Struct containing information about a client of the service
618 struct MeshClient *next;
623 struct MeshClient *prev;
626 * Tunnels that belong to this client, indexed by local id
628 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
631 * Tunnels this client has accepted, indexed by incoming local id
633 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
636 * Tunnels this client has rejected, indexed by incoming local id
638 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
640 * Handle to communicate with the client
642 struct GNUNET_SERVER_Client *handle;
645 * Applications that this client has claimed to provide
647 struct GNUNET_CONTAINER_MultiHashMap *apps;
650 * Messages that this client has declared interest in
652 struct GNUNET_CONTAINER_MultiHashMap *types;
655 * Whether the client is active or shutting down (don't send confirmations
656 * to a client that is shutting down.
661 * ID of the client, mainly for debug messages
666 * Regular expressions describing the services offered by this client.
668 char **regexes; // FIXME add timeout? API to remove a regex?
671 * Number of regular expressions in regexes.
673 unsigned int n_regex;
676 * Task to refresh all regular expresions in the DHT.
678 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
684 * Struct to keep information of searches of services described by a regex
685 * using a user-provided string service description.
687 struct MeshRegexSearchInfo
690 * Which tunnel is this for
692 struct MeshTunnel *t;
695 * User provided description of the searched service.
700 * Part of the description already consumed by the search.
707 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
710 * Results from running DHT GETs.
712 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
715 * Contexts, for each running DHT GET. Free all on end of search.
717 struct MeshRegexSearchContext **contexts;
720 * Number of contexts (branches/steps in search).
722 unsigned int n_contexts;
725 * Peer that is connecting via connect_by_string. When connected, free ctx.
730 * Other peers that are found but not yet being connected to.
732 GNUNET_PEER_Id *peers;
735 * Number of elements in peers.
737 unsigned int n_peers;
740 * Next peer to try to connect to.
745 * Timeout for a connect attempt.
746 * When reached, try to connect to a different peer, if any. If not,
747 * try the same peer again.
749 GNUNET_SCHEDULER_TaskIdentifier timeout;
754 * Struct to keep state of running searches that have consumed a part of
757 struct MeshRegexSearchContext
760 * Part of the description already consumed by
761 * this particular search branch.
766 * Information about the search.
768 struct MeshRegexSearchInfo *info;
772 /******************************************************************************/
773 /************************ DEBUG FUNCTIONS ****************************/
774 /******************************************************************************/
778 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
779 * @param cls string to print
780 * @param success GNUNET_OK if the PUT was transmitted,
781 * GNUNET_NO on timeout,
782 * GNUNET_SYSERR on disconnect from service
783 * after the PUT message was transmitted
784 * (so we don't know if it was received or not)
789 mesh_debug (void *cls, int success)
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
797 unsigned int debug_fwd_ack;
798 unsigned int debug_bck_ack;
802 /******************************************************************************/
803 /*********************** GLOBAL VARIABLES ****************************/
804 /******************************************************************************/
807 * Configuration parameters
809 static struct GNUNET_TIME_Relative refresh_path_time;
810 static struct GNUNET_TIME_Relative app_announce_time;
811 static struct GNUNET_TIME_Relative id_announce_time;
812 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
813 static struct GNUNET_TIME_Relative connect_timeout;
814 static long long unsigned int default_ttl;
815 static long long unsigned int dht_replication_level;
816 static long long unsigned int max_tunnels;
817 static long long unsigned int max_msgs_queue;
820 * DLL with all the clients, head.
822 static struct MeshClient *clients;
825 * DLL with all the clients, tail.
827 static struct MeshClient *clients_tail;
830 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
832 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
835 * Number of tunnels known.
837 static unsigned long long n_tunnels;
840 * Tunnels incoming, indexed by MESH_TunnelNumber
841 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
843 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
846 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
848 static struct GNUNET_CONTAINER_MultiHashMap *peers;
851 * Handle to communicate with transport
853 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
856 * Handle to communicate with core.
858 static struct GNUNET_CORE_Handle *core_handle;
863 static struct GNUNET_DHT_Handle *dht_handle;
868 static struct GNUNET_SERVER_Handle *server_handle;
871 * Handle to the statistics service.
873 static struct GNUNET_STATISTICS_Handle *stats;
876 * Notification context, to send messages to local clients.
878 static struct GNUNET_SERVER_NotificationContext *nc;
881 * Local peer own ID (memory efficient handle).
883 static GNUNET_PEER_Id myid;
886 * Local peer own ID (full value).
888 static struct GNUNET_PeerIdentity my_full_id;
893 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
898 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
901 * Tunnel ID for the next created tunnel (global tunnel number).
903 static MESH_TunnelNumber next_tid;
906 * Tunnel ID for the next incoming tunnel (local tunnel number).
908 static MESH_TunnelNumber next_local_tid;
911 * All application types provided by this peer.
913 static struct GNUNET_CONTAINER_MultiHashMap *applications;
916 * All message types clients of this peer are interested in.
918 static struct GNUNET_CONTAINER_MultiHashMap *types;
921 * Task to periodically announce provided applications.
923 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
926 * Task to periodically announce itself in the network.
928 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
931 * Next ID to assign to a client.
933 unsigned int next_client_id;
936 /******************************************************************************/
937 /*********************** DECLARATIONS **************************/
938 /******************************************************************************/
940 /* FIXME move declarations here */
943 * Function to process paths received for a new peer addition. The recorded
944 * paths form the initial tunnel, which can be optimized later.
945 * Called on each result obtained for the DHT search.
948 * @param exp when will this value expire
949 * @param key key of the result
950 * @param type type of the result
951 * @param size number of bytes in data
952 * @param data pointer to the result data
955 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
956 const struct GNUNET_HashCode * key,
957 const struct GNUNET_PeerIdentity *get_path,
958 unsigned int get_path_length,
959 const struct GNUNET_PeerIdentity *put_path,
960 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
961 size_t size, const void *data);
965 * Function to process DHT string to regex matching.
966 * Called on each result obtained for the DHT search.
968 * @param cls closure (search context)
969 * @param exp when will this value expire
970 * @param key key of the result
971 * @param get_path path of the get request (not used)
972 * @param get_path_length lenght of get_path (not used)
973 * @param put_path path of the put request (not used)
974 * @param put_path_length length of the put_path (not used)
975 * @param type type of the result
976 * @param size number of bytes in data
977 * @param data pointer to the result data
979 * TODO: re-issue the request after certain time? cancel after X results?
982 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
983 const struct GNUNET_HashCode * key,
984 const struct GNUNET_PeerIdentity *get_path,
985 unsigned int get_path_length,
986 const struct GNUNET_PeerIdentity *put_path,
987 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
988 size_t size, const void *data);
992 * Function to process DHT string to regex matching.
993 * Called on each result obtained for the DHT search.
995 * @param cls closure (search context)
996 * @param exp when will this value expire
997 * @param key key of the result
998 * @param get_path path of the get request (not used)
999 * @param get_path_length lenght of get_path (not used)
1000 * @param put_path path of the put request (not used)
1001 * @param put_path_length length of the put_path (not used)
1002 * @param type type of the result
1003 * @param size number of bytes in data
1004 * @param data pointer to the result data
1007 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1008 const struct GNUNET_HashCode * key,
1009 const struct GNUNET_PeerIdentity *get_path,
1010 unsigned int get_path_length,
1011 const struct GNUNET_PeerIdentity *put_path,
1012 unsigned int put_path_length,
1013 enum GNUNET_BLOCK_Type type,
1014 size_t size, const void *data);
1018 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1019 * and insert it in the appropiate structures if the peer is not known yet.
1021 * @param peer Short identity of the peer.
1023 * @return Existing or newly created peer info.
1025 static struct MeshPeerInfo *
1026 peer_info_get_short (const GNUNET_PEER_Id peer);
1030 * Try to establish a new connection to this peer.
1031 * Use the best path for the given tunnel.
1032 * If the peer doesn't have any path to it yet, try to get one.
1033 * If the peer already has some path, send a CREATE PATH towards it.
1035 * @param peer PeerInfo of the peer.
1036 * @param t Tunnel for which to create the path, if possible.
1039 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1043 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1044 * needed rescources.
1045 * If peer already exists, reevaluate shortest path and change if different.
1047 * @param t Tunnel we want to add a new peer to
1048 * @param peer PeerInfo of the peer being added
1052 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1056 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1057 * that are no longer needed, as well as nodes of no longer reachable peers.
1058 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1060 * @param t Tunnel from which to remove the path.
1061 * @param peer Short id of the peer which should be removed.
1064 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1068 * Search for a tunnel by global ID using full PeerIdentities.
1070 * @param oid owner of the tunnel.
1071 * @param tid global tunnel number.
1073 * @return tunnel handler, NULL if doesn't exist.
1075 static struct MeshTunnel *
1076 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1080 * Delete an active client from the tunnel.
1086 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1089 * Notify a tunnel that a connection has broken that affects at least
1090 * some of its peers.
1092 * @param t Tunnel affected.
1093 * @param p1 Peer that got disconnected from p2.
1094 * @param p2 Peer that got disconnected from p1.
1096 * @return Short ID of the peer disconnected (either p1 or p2).
1097 * 0 if the tunnel remained unaffected.
1099 static GNUNET_PEER_Id
1100 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1105 * Get the current ack value for a tunnel, for data going from root to leaves,
1106 * taking in account the tunnel mode and the status of all children and clients.
1110 * @return Maximum PID allowed.
1113 tunnel_get_fwd_ack (struct MeshTunnel *t);
1117 * Add a client to a tunnel, initializing all needed data structures.
1119 * @param t Tunnel to which add the client.
1120 * @param c Client which to add to the tunnel.
1123 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1127 * Iterator over edges in a regex block retrieved from the DHT.
1129 * @param cls Closure.
1130 * @param token Token that follows to next state.
1131 * @param len Lenght of token.
1132 * @param key Hash of next state.
1134 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1137 regex_edge_iterator (void *cls,
1140 const struct GNUNET_HashCode *key);
1144 * Find a path to a peer that offers a regex servcie compatible
1145 * with a given string.
1147 * @param key The key of the accepting state.
1148 * @param ctx Context containing info about the string, tunnel, etc.
1151 regex_find_path (const struct GNUNET_HashCode *key,
1152 struct MeshRegexSearchContext *ctx);
1156 * Queue and pass message to core when possible.
1158 * @param cls Closure (type dependant).
1159 * @param type Type of the message, 0 for a raw message.
1160 * @param size Size of the message.
1161 * @param dst Neighbor to send message to.
1162 * @param t Tunnel this message belongs to.
1165 queue_add (void *cls, uint16_t type, size_t size,
1166 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1170 * Free a transmission that was already queued with all resources
1171 * associated to the request.
1173 * @param queue Queue handler to cancel.
1174 * @param clear_cls Is it necessary to free associated cls?
1177 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1181 * @brief Get the next transmittable message from the queue.
1183 * This will be the head, except in the case of being a data packet
1184 * not allowed by the destination peer.
1186 * @param peer Destination peer.
1188 * @return The next viable MeshPeerQueue element to send to that peer.
1189 * NULL when there are no transmittable messages.
1191 struct MeshPeerQueue *
1192 queue_get_next (const struct MeshPeerInfo *peer);
1196 * Core callback to write a queued packet to core buffer
1198 * @param cls Closure (peer info).
1199 * @param size Number of bytes available in buf.
1200 * @param buf Where the to write the message.
1202 * @return number of bytes written to buf
1205 queue_send (void *cls, size_t size, void *buf);
1207 /******************************************************************************/
1208 /************************ ITERATORS ****************************/
1209 /******************************************************************************/
1212 * Iterator over found existing mesh regex blocks that match an ongoing search.
1214 * @param cls closure
1215 * @param key current key code
1216 * @param value value in the hash map
1217 * @return GNUNET_YES if we should continue to iterate,
1221 regex_result_iterator (void *cls,
1222 const struct GNUNET_HashCode * key,
1225 struct MeshRegexBlock *block = value;
1226 struct MeshRegexSearchContext *ctx = cls;
1228 if (GNUNET_YES == ntohl(block->accepting) &&
1229 ctx->position == strlen (ctx->info->description))
1231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1232 regex_find_path (key, ctx);
1233 return GNUNET_YES; // We found an accept state!
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1238 ctx->position, strlen(ctx->info->description),
1239 ntohl(block->accepting));
1242 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1243 ®ex_edge_iterator, ctx);
1250 * Iterator over edges in a regex block retrieved from the DHT.
1252 * @param cls Closure (context of the search).
1253 * @param token Token that follows to next state.
1254 * @param len Lenght of token.
1255 * @param key Hash of next state.
1257 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1260 regex_edge_iterator (void *cls,
1263 const struct GNUNET_HashCode *key)
1265 struct MeshRegexSearchContext *ctx = cls;
1266 struct MeshRegexSearchContext *new_ctx;
1267 struct MeshRegexSearchInfo *info = ctx->info;
1268 struct GNUNET_DHT_GetHandle *get_h;
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1275 current = &info->description[ctx->position];
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1277 current_len = strlen (info->description) - ctx->position;
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1282 if (len > current_len)
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1285 return GNUNET_YES; // Token too long, wont match
1287 if (0 != strncmp (current, token, len))
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1290 return GNUNET_YES; // Token doesn't match
1292 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1293 new_ctx->info = info;
1294 new_ctx->position = ctx->position + len;
1295 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1297 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1300 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1301 ®ex_result_iterator,
1303 return GNUNET_YES; // We are already looking for it
1305 /* Start search in DHT */
1307 GNUNET_DHT_get_start (dht_handle, /* handle */
1308 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1309 key, /* key to search */
1310 dht_replication_level, /* replication level */
1311 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1312 NULL, /* xquery */ // FIXME BLOOMFILTER
1313 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1314 &dht_get_string_handler, new_ctx);
1316 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1317 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1327 * Iterator over hash map entries to cancel DHT GET requests after a
1328 * successful connect_by_string.
1330 * @param cls Closure (unused).
1331 * @param key Current key code (unused).
1332 * @param value Value in the hash map (get handle).
1333 * @return GNUNET_YES if we should continue to iterate,
1337 regex_cancel_dht_get (void *cls,
1338 const struct GNUNET_HashCode * key,
1341 struct GNUNET_DHT_GetHandle *h = value;
1343 GNUNET_DHT_get_stop (h);
1349 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1350 * search for connect_by_string.
1352 * @param cls Closure (unused).
1353 * @param key Current key code (unused).
1354 * @param value MeshRegexBlock in the hash map.
1355 * @return GNUNET_YES if we should continue to iterate,
1359 regex_free_result (void *cls,
1360 const struct GNUNET_HashCode * key,
1364 GNUNET_free (value);
1370 * Regex callback iterator to store own service description in the DHT.
1372 * @param cls closure.
1373 * @param key hash for current state.
1374 * @param proof proof for current state.
1375 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1376 * @param num_edges number of edges leaving current state.
1377 * @param edges edges leaving current state.
1380 regex_iterator (void *cls,
1381 const struct GNUNET_HashCode *key,
1384 unsigned int num_edges,
1385 const struct GNUNET_REGEX_Edge *edges)
1387 struct MeshRegexBlock *block;
1388 struct MeshRegexEdge *block_edge;
1389 enum GNUNET_DHT_RouteOption opt;
1393 unsigned int offset;
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 " regex dht put for state %s\n",
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1406 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1407 if (GNUNET_YES == accepting)
1409 struct MeshRegexAccept block;
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 " state %s is accepting, putting own id\n",
1414 size = sizeof (block);
1416 block.id = my_full_id;
1418 GNUNET_DHT_put(dht_handle, key,
1419 dht_replication_level,
1420 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1421 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1424 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1429 len = strlen(proof);
1430 size = sizeof (struct MeshRegexBlock) + len;
1431 block = GNUNET_malloc (size);
1434 block->n_proof = htonl (len);
1435 block->n_edges = htonl (num_edges);
1436 block->accepting = htonl (accepting);
1438 /* Store the proof at the end of the block. */
1439 aux = (char *) &block[1];
1440 memcpy (aux, proof, len);
1443 /* Store each edge in a variable length MeshEdge struct at the
1444 * very end of the MeshRegexBlock structure.
1446 for (i = 0; i < num_edges; i++)
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449 " edge %s towards %s\n",
1451 GNUNET_h2s(&edges[i].destination));
1453 /* aux points at the end of the last block */
1454 len = strlen (edges[i].label);
1455 size += sizeof (struct MeshRegexEdge) + len;
1456 // Calculate offset FIXME is this ok? use size instead?
1457 offset = aux - (char *) block;
1458 block = GNUNET_realloc (block, size);
1459 aux = &((char *) block)[offset];
1460 block_edge = (struct MeshRegexEdge *) aux;
1461 block_edge->key = edges[i].destination;
1462 block_edge->n_token = htonl (len);
1463 aux = (char *) &block_edge[1];
1464 memcpy (aux, edges[i].label, len);
1468 GNUNET_DHT_put(dht_handle, key,
1469 dht_replication_level,
1471 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1473 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1477 GNUNET_free (block);
1482 * Store the regular expression describing a local service into the DHT.
1484 * @param regex The regular expresion.
1487 regex_put (const char *regex)
1489 struct GNUNET_REGEX_Automaton *dfa;
1491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1492 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1493 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1494 GNUNET_REGEX_automaton_destroy (dfa);
1495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1500 * Find a path to a peer that offers a regex servcie compatible
1501 * with a given string.
1503 * @param key The key of the accepting state.
1504 * @param ctx Context containing info about the string, tunnel, etc.
1507 regex_find_path (const struct GNUNET_HashCode *key,
1508 struct MeshRegexSearchContext *ctx)
1510 struct GNUNET_DHT_GetHandle *get_h;
1512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1513 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1514 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1515 key, /* key to search */
1516 dht_replication_level, /* replication level */
1517 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1518 GNUNET_DHT_RO_RECORD_ROUTE,
1519 NULL, /* xquery */ // FIXME BLOOMFILTER
1520 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1521 &dht_get_string_accept_handler, ctx);
1522 GNUNET_break (GNUNET_OK ==
1523 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1526 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1531 * Function called if the connect attempt to a peer found via
1532 * connect_by_string times out. Try to connect to another peer, if any.
1533 * Otherwise try to reconnect to the same peer.
1535 * @param cls Closure (info about regex search).
1536 * @param tc TaskContext.
1539 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1541 struct MeshRegexSearchInfo *info = cls;
1542 struct MeshPeerInfo *peer_info;
1546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1547 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1548 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1556 if (0 < info->n_peers)
1558 // Select next peer, put current in that spot.
1559 id = info->peers[info->i_peer];
1560 info->peers[info->i_peer] = info->peer;
1561 info->i_peer = (info->i_peer + 1) % info->n_peers;
1565 // Try to connect to same peer again.
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1570 peer_info = peer_info_get_short(id);
1571 tunnel_add_peer (info->t, peer_info);
1573 tunnel_delete_peer (info->t, old);
1574 peer_info_connect (peer_info, info->t);
1575 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1576 ®ex_connect_timeout,
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1583 * Cancel an ongoing regex search in the DHT and free all resources.
1585 * @param ctx The search context.
1588 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1590 struct MeshRegexSearchInfo *info = ctx->info;
1593 GNUNET_free (info->description);
1594 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1595 ®ex_cancel_dht_get, NULL);
1596 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1597 ®ex_free_result, NULL);
1598 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1599 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1600 info->t->regex_ctx = NULL;
1601 for (i = 0; i < info->n_contexts; i++)
1603 GNUNET_free (info->contexts[i]);
1605 if (0 < info->n_contexts)
1606 GNUNET_free (info->contexts);
1607 if (0 < info->n_peers)
1608 GNUNET_free (info->peers);
1609 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1611 GNUNET_SCHEDULER_cancel(info->timeout);
1617 /******************************************************************************/
1618 /************************ PERIODIC FUNCTIONS ****************************/
1619 /******************************************************************************/
1622 * Announce iterator over for each application provided by the peer
1624 * @param cls closure
1625 * @param key current key code
1626 * @param value value in the hash map
1627 * @return GNUNET_YES if we should continue to
1632 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1634 struct PBlock block;
1635 struct MeshClient *c;
1637 block.id = my_full_id;
1638 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1639 GNUNET_assert(NULL != c);
1640 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1641 if (0 == block.type)
1646 block.type = htonl (block.type);
1648 GNUNET_break (NULL !=
1649 GNUNET_DHT_put (dht_handle, key,
1650 dht_replication_level,
1651 GNUNET_DHT_RO_RECORD_ROUTE |
1652 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1653 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1655 (const char *) &block,
1656 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1658 app_announce_time, NULL, NULL));
1664 * Periodically announce what applications are provided by local clients
1667 * @param cls closure
1668 * @param tc task context
1671 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1673 struct MeshClient *c = cls;
1676 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1678 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1682 DEBUG_DHT ("Starting PUT for regex\n");
1684 for (i = 0; i < c->n_regex; i++)
1686 regex_put (c->regexes[i]);
1688 c->regex_announce_task =
1689 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1690 DEBUG_DHT ("Finished PUT for regex\n");
1697 * Periodically announce what applications are provided by local clients
1700 * @param cls closure
1701 * @param tc task context
1704 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1706 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1708 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1712 DEBUG_DHT ("Starting PUT for apps\n");
1714 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1716 announce_applications_task =
1717 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1719 DEBUG_DHT ("Finished PUT for apps\n");
1726 * Periodically announce self id in the DHT
1728 * @param cls closure
1729 * @param tc task context
1732 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1734 struct PBlock block;
1736 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1738 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1742 * - Set data expiration in function of X
1743 * - Adapt X to churn
1745 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1747 block.id = my_full_id;
1748 block.type = htonl (0);
1749 GNUNET_DHT_put (dht_handle, /* DHT handle */
1750 &my_full_id.hashPubKey, /* Key to use */
1751 dht_replication_level, /* Replication level */
1752 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1753 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1754 sizeof (block), /* Size of the data */
1755 (const char *) &block, /* Data itself */
1756 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1757 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1758 NULL, /* Continuation */
1759 NULL); /* Continuation closure */
1761 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1765 /******************************************************************************/
1766 /****************** GENERAL HELPER FUNCTIONS ************************/
1767 /******************************************************************************/
1770 * Decrements the reference counter and frees all resources if needed
1772 * @param mesh_data Data Descriptor used in a multicast message.
1773 * Freed no longer needed (last message).
1776 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1778 if (0 == --(mesh_data->reference_counter))
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1781 GNUNET_free (mesh_data->data);
1782 GNUNET_free (mesh_data);
1788 * Check if client has registered with the service and has not disconnected
1790 * @param client the client to check
1792 * @return non-NULL if client exists in the global DLL
1794 static struct MeshClient *
1795 client_get (struct GNUNET_SERVER_Client *client)
1797 struct MeshClient *c;
1802 if (c->handle == client)
1811 * Checks if a given client has subscribed to certain message type
1813 * @param message_type Type of message to check
1814 * @param c Client to check
1816 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1818 * FIXME: use of crypto_hash slows it down
1819 * The hash function alone takes 8-10us out of the ~55us for the whole
1820 * process of retransmitting the message from one local client to another.
1821 * Find faster implementation!
1824 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1826 struct GNUNET_HashCode hc;
1828 if (NULL == c->types)
1831 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1832 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1837 * Check whether client wants traffic from a tunnel.
1839 * @param c Client to check.
1840 * @param t Tunnel to be found.
1842 * @return GNUNET_YES if client knows tunnel.
1844 * TODO look in client hashmap
1847 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1851 for (i = 0; i < t->nclients; i++)
1852 if (t->clients[i] == c)
1859 * Check whether client has been informed about a tunnel.
1861 * @param c Client to check.
1862 * @param t Tunnel to be found.
1864 * @return GNUNET_YES if client knows tunnel.
1866 * TODO look in client hashmap
1869 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1873 for (i = 0; i < t->nignore; i++)
1874 if (t->ignore[i] == c)
1876 return client_wants_tunnel(c, t);
1881 * Marks a client as uninterested in traffic from the tunnel, updating both
1882 * client and tunnel to reflect this.
1884 * @param c Client that doesn't want traffic anymore.
1885 * @param t Tunnel which should be ignored.
1887 * FIXME when to delete an incoming tunnel?
1890 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1892 struct GNUNET_HashCode hash;
1894 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1895 GNUNET_break (GNUNET_YES ==
1896 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1898 GNUNET_break (GNUNET_YES ==
1899 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1900 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1901 tunnel_delete_active_client (t, c);
1902 GNUNET_array_append (t->ignore, t->nignore, c);
1907 * Deletes a tunnel from a client (either owner or destination). To be used on
1908 * tunnel destroy, otherwise, use client_ignore_tunnel.
1910 * @param c Client whose tunnel to delete.
1911 * @param t Tunnel which should be deleted.
1914 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1916 struct GNUNET_HashCode hash;
1920 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1921 GNUNET_assert (GNUNET_YES ==
1922 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1928 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1930 GNUNET_assert (GNUNET_YES ==
1931 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1935 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1943 * Send the message to all clients that have subscribed to its type
1945 * @param msg Pointer to the message itself
1946 * @param payload Pointer to the payload of the message.
1947 * @param t The tunnel to whose clients this message goes.
1949 * @return number of clients this message was sent to
1952 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1953 const struct GNUNET_MessageHeader *payload,
1954 struct MeshTunnel *t)
1956 struct MeshClient *c;
1957 MESH_TunnelNumber *tid;
1960 char cbuf[htons (msg->size)];
1962 type = ntohs (payload->type);
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1965 GNUNET_MESH_DEBUG_M2S (type));
1967 memcpy (cbuf, msg, sizeof (cbuf));
1968 switch (htons (msg->type))
1970 struct GNUNET_MESH_Unicast *uc;
1971 struct GNUNET_MESH_Multicast *mc;
1972 struct GNUNET_MESH_ToOrigin *to;
1974 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1975 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1978 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1979 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1982 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1983 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1991 for (count = 0, c = clients; c != NULL; c = c->next)
1993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1994 if (client_is_subscribed (type, c))
1996 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2000 *tid = htonl (t->local_tid);
2004 if (GNUNET_NO == client_knows_tunnel (c, t))
2006 /* This client doesn't know the tunnel */
2007 struct GNUNET_MESH_TunnelNotification tmsg;
2008 struct GNUNET_HashCode hash;
2010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2011 tmsg.header.size = htons (sizeof (tmsg));
2012 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2013 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2014 tmsg.tunnel_id = htonl (t->local_tid_dest);
2015 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2016 &tmsg.header, GNUNET_NO);
2017 tunnel_add_client (t, c);
2018 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2020 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2021 c->incoming_tunnels, &hash, t,
2022 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2024 *tid = htonl (t->local_tid_dest);
2027 /* Check if the client wants to get traffic from the tunnel */
2028 if (GNUNET_NO == client_wants_tunnel(c, t))
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2032 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2033 (struct GNUNET_MessageHeader
2034 *) cbuf, GNUNET_NO);
2043 * Notify the client that owns the tunnel that a peer has connected to it
2044 * (the requested path to it has been confirmed).
2046 * @param t Tunnel whose owner to notify
2047 * @param id Short id of the peer that has connected
2050 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2052 struct GNUNET_MESH_PeerControl pc;
2054 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2055 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2056 pc.tunnel_id = htonl (t->local_tid);
2057 GNUNET_PEER_resolve (id, &pc.peer);
2058 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2064 * Notify all clients (not depending on registration status) that the incoming
2065 * tunnel is no longer valid.
2067 * @param t Tunnel that was destroyed.
2070 send_clients_tunnel_destroy (struct MeshTunnel *t)
2072 struct GNUNET_MESH_TunnelMessage msg;
2074 msg.header.size = htons (sizeof (msg));
2075 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2076 msg.tunnel_id = htonl (t->local_tid_dest);
2077 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2082 * Notify clients of tunnel disconnections, if needed.
2083 * In case the origin disconnects, the destination clients get a tunnel destroy
2084 * notification. If the last destination disconnects (only one remaining client
2085 * in tunnel), the origin gets a (local ID) peer disconnected.
2086 * Note that the function must be called BEFORE removing the client from
2089 * @param t Tunnel that was destroyed.
2090 * @param c Client that disconnected.
2093 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2099 struct GNUNET_MESH_TunnelMessage msg;
2101 msg.header.size = htons (sizeof (msg));
2102 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2103 msg.tunnel_id = htonl (t->local_tid_dest);
2104 for (i = 0; i < t->nclients; i++)
2105 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2106 &msg.header, GNUNET_NO);
2108 // FIXME when to disconnect an incoming tunnel?
2109 else if (1 == t->nclients && NULL != t->owner)
2111 struct GNUNET_MESH_PeerControl msg;
2113 msg.header.size = htons (sizeof (msg));
2114 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2115 msg.tunnel_id = htonl (t->local_tid);
2116 msg.peer = my_full_id;
2117 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2118 &msg.header, GNUNET_NO);
2124 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2125 * and insert it in the appropiate structures if the peer is not known yet.
2127 * @param peer Full identity of the peer.
2129 * @return Existing or newly created peer info.
2131 static struct MeshPeerInfo *
2132 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2134 struct MeshPeerInfo *peer_info;
2136 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2137 if (NULL == peer_info)
2140 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2141 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2143 peer_info->id = GNUNET_PEER_intern (peer);
2151 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2152 * and insert it in the appropiate structures if the peer is not known yet.
2154 * @param peer Short identity of the peer.
2156 * @return Existing or newly created peer info.
2158 static struct MeshPeerInfo *
2159 peer_info_get_short (const GNUNET_PEER_Id peer)
2161 struct GNUNET_PeerIdentity id;
2163 GNUNET_PEER_resolve (peer, &id);
2164 return peer_info_get (&id);
2169 * Iterator to remove the tunnel from the list of tunnels a peer participates
2172 * @param cls Closure (tunnel info)
2173 * @param key GNUNET_PeerIdentity of the peer (unused)
2174 * @param value PeerInfo of the peer
2176 * @return always GNUNET_YES, to keep iterating
2179 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2181 struct MeshTunnel *t = cls;
2182 struct MeshPeerInfo *peer = value;
2185 for (i = 0; i < peer->ntunnels; i++)
2188 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2191 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2192 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2201 * Core callback to write a pre-constructed data packet to core buffer
2203 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2204 * @param size Number of bytes available in buf.
2205 * @param buf Where the to write the message.
2207 * @return number of bytes written to buf
2210 send_core_data_raw (void *cls, size_t size, void *buf)
2212 struct MeshTransmissionDescriptor *info = cls;
2213 struct GNUNET_MessageHeader *msg;
2216 GNUNET_assert (NULL != info);
2217 GNUNET_assert (NULL != info->mesh_data);
2218 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2219 total_size = ntohs (msg->size);
2221 if (total_size > size)
2226 memcpy (buf, msg, total_size);
2227 data_descriptor_decrement_rc (info->mesh_data);
2234 * Sends an already built non-multicast message to a peer,
2235 * properly registrating all used resources.
2237 * @param message Message to send. Function makes a copy of it.
2238 * @param peer Short ID of the neighbor whom to send the message.
2239 * @param t Tunnel on which this message is transmitted.
2242 send_message (const struct GNUNET_MessageHeader *message,
2243 const struct GNUNET_PeerIdentity *peer,
2244 struct MeshTunnel *t)
2246 struct MeshTransmissionDescriptor *info;
2247 struct MeshPeerInfo *neighbor;
2248 struct MeshPeerPath *p;
2252 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2254 size = ntohs (message->size);
2255 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2256 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2257 info->mesh_data->data = GNUNET_malloc (size);
2258 memcpy (info->mesh_data->data, message, size);
2259 type = ntohs(message->type);
2260 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2262 struct GNUNET_MESH_Unicast *m;
2264 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2265 m->ttl = htonl (ntohl (m->ttl) - 1);
2267 info->mesh_data->data_len = size;
2268 info->mesh_data->reference_counter = 1;
2269 info->mesh_data->total_out = 1;
2270 neighbor = peer_info_get (peer);
2271 for (p = neighbor->path_head; NULL != p; p = p->next)
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 " %s IS NOT DIRECTLY CONNECTED\n",
2284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2287 for (p = neighbor->path_head; NULL != p; p = p->next)
2289 struct GNUNET_PeerIdentity debug_id;
2292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2293 " path with %u hops through:\n",
2295 for (i = 0; i < p->length; i++)
2297 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2300 i, GNUNET_i2s(&debug_id));
2304 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2305 GNUNET_free (info->mesh_data->data);
2306 GNUNET_free (info->mesh_data);
2310 info->peer = neighbor;
2311 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2314 * TODO: in this case we only need the service to retransmit
2315 * the message down the path. If we pass the real type to queue_add,
2316 * queue_send will try to build the message from scratch. This can
2317 * probably be done by some other way instead of deleteing the type
2331 * Sends a CREATE PATH message for a path to a peer, properly registrating
2332 * all used resources.
2334 * @param peer PeerInfo of the final peer for whom this path is being created.
2335 * @param p Path itself.
2336 * @param t Tunnel for which the path is created.
2339 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2340 struct MeshTunnel *t)
2342 struct GNUNET_PeerIdentity id;
2343 struct MeshPathInfo *path_info;
2344 struct MeshPeerInfo *neighbor;
2350 p = tree_get_path_to_peer (t->tree, peer->id);
2357 for (i = 0; i < p->length; i++)
2359 if (p->peers[i] == myid)
2362 if (i >= p->length - 1)
2368 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2370 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2371 path_info->path = p;
2373 neighbor = peer_info_get (&id);
2374 path_info->peer = neighbor;
2375 queue_add (path_info,
2376 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2377 sizeof (struct GNUNET_MESH_ManipulatePath) +
2378 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2385 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2387 * @param t Tunnel whose path to destroy.
2388 * @param destination Short ID of the peer to whom the path to destroy.
2391 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2393 struct MeshPeerPath *p;
2396 p = tree_get_path_to_peer (t->tree, destination);
2402 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2403 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2405 struct GNUNET_MESH_ManipulatePath *msg;
2406 struct GNUNET_PeerIdentity *pi;
2410 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2411 msg->header.size = htons (size);
2412 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2413 msg->tid = htonl (t->id.tid);
2414 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2415 for (i = 0; i < p->length; i++)
2417 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2419 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2426 * Try to establish a new connection to this peer.
2427 * Use the best path for the given tunnel.
2428 * If the peer doesn't have any path to it yet, try to get one.
2429 * If the peer already has some path, send a CREATE PATH towards it.
2431 * @param peer PeerInfo of the peer.
2432 * @param t Tunnel for which to create the path, if possible.
2435 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2437 struct MeshPeerPath *p;
2438 struct MeshPathInfo *path_info;
2440 if (NULL != peer->path_head)
2442 p = tree_get_path_to_peer (t->tree, peer->id);
2449 // FIXME always send create path to self
2452 send_create_path (peer, p, t);
2456 struct GNUNET_HashCode hash;
2459 send_client_peer_connected (t, myid);
2460 t->local_tid_dest = next_local_tid++;
2461 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2464 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2465 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2472 else if (NULL == peer->dhtget)
2474 struct GNUNET_PeerIdentity id;
2476 GNUNET_PEER_resolve (peer->id, &id);
2477 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2478 path_info->peer = peer;
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2482 peer->dhtgetcls = path_info;
2483 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2484 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2485 &id.hashPubKey, /* key to search */
2486 dht_replication_level, /* replication level */
2487 GNUNET_DHT_RO_RECORD_ROUTE |
2488 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2489 NULL, /* xquery */ // FIXME BLOOMFILTER
2490 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2491 &dht_get_id_handler, path_info);
2493 /* Otherwise, there is no path but the DHT get is already started. */
2498 * Task to delay the connection of a peer
2500 * @param cls Closure (path info with tunnel and peer to connect).
2501 * Will be free'd on exection.
2502 * @param tc TaskContext
2505 peer_info_connect_task (void *cls,
2506 const struct GNUNET_SCHEDULER_TaskContext *tc)
2508 struct MeshPathInfo *path_info = cls;
2510 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2515 peer_info_connect (path_info->peer, path_info->t);
2521 * Destroy the peer_info and free any allocated resources linked to it
2523 * @param pi The peer_info to destroy.
2525 * @return GNUNET_OK on success
2528 peer_info_destroy (struct MeshPeerInfo *pi)
2530 struct GNUNET_PeerIdentity id;
2531 struct MeshPeerPath *p;
2532 struct MeshPeerPath *nextp;
2534 GNUNET_PEER_resolve (pi->id, &id);
2535 GNUNET_PEER_change_rc (pi->id, -1);
2538 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2541 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2542 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2544 if (NULL != pi->dhtget)
2546 GNUNET_DHT_get_stop (pi->dhtget);
2547 GNUNET_free (pi->dhtgetcls);
2553 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2563 * Remove all paths that rely on a direct connection between p1 and p2
2564 * from the peer itself and notify all tunnels about it.
2566 * @param peer PeerInfo of affected peer.
2567 * @param p1 GNUNET_PEER_Id of one peer.
2568 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2571 * TODO: optimize (see below)
2574 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2577 struct MeshPeerPath *p;
2578 struct MeshPeerPath *aux;
2579 struct MeshPeerInfo *peer_d;
2581 unsigned int destroyed;
2586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2588 p = peer->path_head;
2592 for (i = 0; i < (p->length - 1); i++)
2594 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2595 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2597 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2608 for (i = 0; i < peer->ntunnels; i++)
2610 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2614 * Problem: one or more peers have been deleted from the tunnel tree.
2615 * We don't know who they are to try to add them again.
2616 * We need to try to find a new path for each of the disconnected peers.
2617 * Some of them might already have a path to reach them that does not
2618 * involve p1 and p2. Adding all anew might render in a better tree than
2619 * the trivial immediate fix.
2621 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2622 * its children will be reachable trough him.
2624 peer_d = peer_info_get_short (d);
2627 for (p = peer_d->path_head; NULL != p; p = p->next)
2629 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2637 /* No callback, as peer will be already disconnected and a connection
2638 * scheduled by tunnel_notify_connection_broken.
2640 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2644 peer_info_connect (peer_d, peer->tunnels[i]);
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2652 * Add the path to the peer and update the path used to reach it in case this
2655 * @param peer_info Destination peer to add the path to.
2656 * @param path New path to add. Last peer must be the peer in arg 1.
2657 * Path will be either used of freed if already known.
2658 * @param trusted Do we trust that this path is real?
2661 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2664 struct MeshPeerPath *aux;
2668 if ((NULL == peer_info) || (NULL == path))
2671 path_destroy (path);
2674 if (path->peers[path->length - 1] != peer_info->id)
2677 path_destroy (path);
2680 if (path->length <= 2 && GNUNET_NO == trusted)
2682 /* Only allow CORE to tell us about direct paths */
2683 path_destroy (path);
2686 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2687 for (l = 1; l < path->length; l++)
2689 if (path->peers[l] == myid)
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2692 for (l2 = 0; l2 < path->length - l; l2++)
2694 path->peers[l2] = path->peers[l + l2];
2699 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2704 struct GNUNET_PeerIdentity id;
2706 GNUNET_PEER_resolve (peer_info->id, &id);
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2708 path->length, GNUNET_i2s (&id));
2711 l = path_get_length (path);
2718 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2719 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2721 l2 = path_get_length (aux);
2724 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2725 peer_info->path_tail, aux, path);
2730 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2732 path_destroy (path);
2737 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2744 * Add the path to the origin peer and update the path used to reach it in case
2745 * this is the shortest.
2746 * The path is given in peer_info -> destination, therefore we turn the path
2747 * upside down first.
2749 * @param peer_info Peer to add the path to, being the origin of the path.
2750 * @param path New path to add after being inversed.
2751 * @param trusted Do we trust that this path is real?
2754 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2755 struct MeshPeerPath *path, int trusted)
2758 peer_info_add_path (peer_info, path, trusted);
2763 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2764 * to obtain a local peer -> destination path and interning the peer ids.
2766 * @return Newly allocated and created path
2768 static struct MeshPeerPath *
2769 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2770 unsigned int get_path_length,
2771 const struct GNUNET_PeerIdentity *put_path,
2772 unsigned int put_path_length)
2774 struct MeshPeerPath *p;
2780 GNUNET_PEER_change_rc (myid, 1);
2781 i = get_path_length;
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2783 for (i--; i >= 0; i--)
2785 id = GNUNET_PEER_intern (&get_path[i]);
2786 if (p->length > 0 && id == p->peers[p->length - 1])
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2789 GNUNET_PEER_change_rc (id, -1);
2793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2794 GNUNET_i2s (&get_path[i]));
2796 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2797 p->peers[p->length - 1] = id;
2800 i = put_path_length;
2801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2802 for (i--; i >= 0; i--)
2804 id = GNUNET_PEER_intern (&put_path[i]);
2807 /* PUT path went through us, so discard the path up until now and start
2808 * from here to get a much shorter (and loop-free) path.
2813 if (p->length > 0 && id == p->peers[p->length - 1])
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2816 GNUNET_PEER_change_rc (id, -1);
2820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2821 GNUNET_i2s (&put_path[i]));
2823 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2824 p->peers[p->length - 1] = id;
2828 if (get_path_length > 0)
2829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2830 GNUNET_i2s (&get_path[0]));
2831 if (put_path_length > 0)
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2833 GNUNET_i2s (&put_path[0]));
2834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2836 for (i = 0; i < p->length; i++)
2838 struct GNUNET_PeerIdentity peer_id;
2840 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2842 GNUNET_i2s (&peer_id));
2850 * Adds a path to the peer_infos of all the peers in the path
2852 * @param p Path to process.
2853 * @param confirmed Whether we know if the path works or not.
2856 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2860 /* TODO: invert and add */
2861 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2862 for (i++; i < p->length; i++)
2864 struct MeshPeerInfo *aux;
2865 struct MeshPeerPath *copy;
2867 aux = peer_info_get_short (p->peers[i]);
2868 copy = path_duplicate (p);
2869 copy->length = i + 1;
2870 peer_info_add_path (aux, copy, GNUNET_NO);
2876 * Send keepalive packets for a peer
2878 * @param cls Closure (tunnel for which to send the keepalive).
2879 * @param tc Notification context.
2881 * TODO: implement explicit multicast keepalive?
2884 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2888 * Search for a tunnel among the incoming tunnels
2890 * @param tid the local id of the tunnel
2892 * @return tunnel handler, NULL if doesn't exist
2894 static struct MeshTunnel *
2895 tunnel_get_incoming (MESH_TunnelNumber tid)
2897 struct GNUNET_HashCode hash;
2899 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2900 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2901 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2906 * Search for a tunnel among the tunnels for a client
2908 * @param c the client whose tunnels to search in
2909 * @param tid the local id of the tunnel
2911 * @return tunnel handler, NULL if doesn't exist
2913 static struct MeshTunnel *
2914 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2916 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2918 return tunnel_get_incoming (tid);
2922 struct GNUNET_HashCode hash;
2924 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2925 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2931 * Search for a tunnel by global ID using PEER_ID
2933 * @param pi owner of the tunnel
2934 * @param tid global tunnel number
2936 * @return tunnel handler, NULL if doesn't exist
2938 static struct MeshTunnel *
2939 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2941 struct MESH_TunnelID id;
2942 struct GNUNET_HashCode hash;
2947 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2948 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2953 * Search for a tunnel by global ID using full PeerIdentities
2955 * @param oid owner of the tunnel
2956 * @param tid global tunnel number
2958 * @return tunnel handler, NULL if doesn't exist
2960 static struct MeshTunnel *
2961 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2963 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2968 * Delete an active client from the tunnel.
2974 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2978 for (i = 0; i < t->nclients; i++)
2980 if (t->clients[i] == c)
2982 t->clients[i] = t->clients[t->nclients - 1];
2983 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2984 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2986 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2994 * Delete an ignored client from the tunnel.
3000 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3004 for (i = 0; i < t->nignore; i++)
3006 if (t->ignore[i] == c)
3008 t->ignore[i] = t->ignore[t->nignore - 1];
3009 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3017 * Delete a client from the tunnel. It should be only done on
3018 * client disconnection, otherwise use client_ignore_tunnel.
3024 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3026 tunnel_delete_ignored_client (t, c);
3027 tunnel_delete_active_client (t, c);
3032 * Iterator to free MeshTunnelChildInfo of tunnel children.
3034 * @param cls Closure (tunnel info).
3035 * @param key Hash of GNUNET_PEER_Id (unused).
3036 * @param value MeshTunnelChildInfo of the child.
3038 * @return always GNUNET_YES, to keep iterating
3041 tunnel_destroy_child (void *cls,
3042 const struct GNUNET_HashCode * key,
3045 struct MeshTunnelChildInfo *cinfo = value;
3046 struct MeshTunnel *t = cls;
3050 for (c = 0; c < cinfo->send_buffer_n; c++)
3052 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3053 if (NULL != cinfo->send_buffer[i])
3054 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3057 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3059 GNUNET_free_non_null (cinfo->send_buffer);
3060 GNUNET_free (cinfo);
3066 * Callback used to notify a client owner of a tunnel that a peer has
3067 * disconnected, most likely because of a path change.
3069 * @param cls Closure (tunnel this notification is about).
3070 * @param peer_id Short ID of disconnected peer.
3073 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3075 struct MeshTunnel *t = cls;
3076 struct MeshPeerInfo *peer;
3077 struct MeshPathInfo *path_info;
3079 if (NULL != t->owner && NULL != nc)
3081 struct GNUNET_MESH_PeerControl msg;
3083 msg.header.size = htons (sizeof (msg));
3084 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3085 msg.tunnel_id = htonl (t->local_tid);
3086 GNUNET_PEER_resolve (peer_id, &msg.peer);
3087 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3088 &msg.header, GNUNET_NO);
3090 peer = peer_info_get_short (peer_id);
3091 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3092 path_info->peer = peer;
3094 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3099 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3100 * needed rescources.
3101 * If peer already exists, reevaluate shortest path and change if different.
3103 * @param t Tunnel we want to add a new peer to
3104 * @param peer PeerInfo of the peer being added
3108 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3110 struct GNUNET_PeerIdentity id;
3111 struct MeshPeerPath *best_p;
3112 struct MeshPeerPath *p;
3113 unsigned int best_cost;
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3117 GNUNET_PEER_resolve (peer->id, &id);
3119 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3122 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3123 GNUNET_assert (GNUNET_OK ==
3124 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3126 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3129 if (NULL != (p = peer->path_head))
3132 best_cost = tree_get_path_cost (t->tree, p);
3135 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3142 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3143 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3144 t->path_refresh_task =
3145 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3149 /* Start a DHT get */
3150 peer_info_connect (peer, t);
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3156 * Add a path to a tunnel which we don't own, just to remember the next hop.
3157 * If destination node was already in the tunnel, the first hop information
3158 * will be replaced with the new path.
3160 * @param t Tunnel we want to add a new peer to
3161 * @param p Path to add
3162 * @param own_pos Position of local node in path.
3166 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3167 unsigned int own_pos)
3169 struct GNUNET_PeerIdentity id;
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3172 GNUNET_assert (0 != own_pos);
3173 tree_add_path (t->tree, p, NULL, NULL);
3174 if (own_pos < p->length - 1)
3176 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3177 tree_update_first_hops (t->tree, myid, &id);
3179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3183 * Add a client to a tunnel, initializing all needed data structures.
3185 * @param t Tunnel to which add the client.
3186 * @param c Client which to add to the tunnel.
3189 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3191 struct MeshTunnelClientInfo clinfo;
3193 GNUNET_array_append (t->clients, t->nclients, c);
3194 clinfo.fwd_ack = t->fwd_pid + 1;
3195 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3196 clinfo.fwd_pid = t->fwd_pid;
3197 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3199 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3204 * Notifies a tunnel that a connection has broken that affects at least
3205 * some of its peers. Sends a notification towards the root of the tree.
3206 * In case the peer is the owner of the tree, notifies the client that owns
3207 * the tunnel and tries to reconnect.
3209 * @param t Tunnel affected.
3210 * @param p1 Peer that got disconnected from p2.
3211 * @param p2 Peer that got disconnected from p1.
3213 * @return Short ID of the peer disconnected (either p1 or p2).
3214 * 0 if the tunnel remained unaffected.
3216 static GNUNET_PEER_Id
3217 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3223 tree_notify_connection_broken (t->tree, p1, p2,
3224 &tunnel_notify_client_peer_disconnected,
3226 if (myid != p1 && myid != p2)
3232 if (tree_get_predecessor (t->tree) != 0)
3234 /* We are the peer still connected, notify owner of the disconnection. */
3235 struct GNUNET_MESH_PathBroken msg;
3236 struct GNUNET_PeerIdentity neighbor;
3238 msg.header.size = htons (sizeof (msg));
3239 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3240 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3241 msg.tid = htonl (t->id.tid);
3242 msg.peer1 = my_full_id;
3243 GNUNET_PEER_resolve (pid, &msg.peer2);
3244 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3245 send_message (&msg.header, &neighbor, t);
3253 * Send a multicast packet to a neighbor.
3255 * @param cls Closure (Info about the multicast packet)
3256 * @param neighbor_id Short ID of the neighbor to send the packet to.
3259 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3261 struct MeshData *mdata = cls;
3262 struct MeshTransmissionDescriptor *info;
3263 struct GNUNET_PeerIdentity neighbor;
3265 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3267 info->mesh_data = mdata;
3268 (mdata->reference_counter) ++;
3269 info->destination = neighbor_id;
3270 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3272 GNUNET_i2s (&neighbor));
3273 info->peer = peer_info_get (&neighbor);
3274 GNUNET_assert (NULL != info->peer);
3276 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3277 info->mesh_data->data_len,
3284 * Queue a message in a tunnel in multicast, sending a copy to each child node
3285 * down the local one in the tunnel tree.
3287 * @param t Tunnel in which to send the data.
3288 * @param msg Message to be sent.
3289 * @param internal DEPRECATED Has the service generated this message?
3291 * FIXME remove internal if no use comes up
3294 tunnel_send_multicast (struct MeshTunnel *t,
3295 const struct GNUNET_MessageHeader *msg,
3298 struct MeshData *mdata;
3300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3301 " sending a multicast packet...\n");
3303 mdata = GNUNET_malloc (sizeof (struct MeshData));
3304 mdata->data_len = ntohs (msg->size);
3306 mdata->data = GNUNET_malloc (mdata->data_len);
3307 memcpy (mdata->data, msg, mdata->data_len);
3308 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3310 struct GNUNET_MESH_Multicast *mcast;
3312 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3313 if (t->fwd_queue_n >= t->fwd_queue_max)
3316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3318 " message from %s!\n",
3319 GNUNET_i2s(&mcast->oid));
3320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3321 " message at %s!\n",
3322 GNUNET_i2s(&my_full_id));
3323 GNUNET_free (mdata->data);
3324 GNUNET_free (mdata);
3328 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3330 ntohl (mcast->ttl));
3334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3337 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3338 if (mdata->reference_counter == 0)
3340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3341 " no one to send data to\n");
3342 GNUNET_free (mdata->data);
3343 GNUNET_free (mdata);
3348 mdata->total_out = mdata->reference_counter;
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3351 " sending a multicast packet done\n");
3357 * Increase the SKIP value of all peers that
3358 * have not received a unicast message.
3360 * @param cls Closure (ID of the peer that HAS received the message).
3361 * @param key ID of the neighbor.
3362 * @param value Information about the neighbor.
3364 * @return GNUNET_YES to keep iterating.
3367 tunnel_add_skip (void *cls,
3368 const struct GNUNET_HashCode * key,
3371 struct GNUNET_PeerIdentity *neighbor = cls;
3372 struct MeshTunnelChildInfo *cinfo = value;
3374 /* TODO compare only pointers? key == neighbor? */
3375 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3385 * @brief Get neighbor's Flow Control information.
3387 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3388 * descendant of the local node in a certain tunnel.
3389 * If the info is not yet there (recently created path), creates the data struct
3390 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3393 * @param t Tunnel related.
3394 * @param peer Neighbor whose Flow Control info is needed.
3396 * @return Neighbor's Flow Control info.
3398 static struct MeshTunnelChildInfo *
3399 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3400 const struct GNUNET_PeerIdentity *peer)
3402 struct MeshTunnelChildInfo *cinfo;
3403 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3409 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3410 cinfo->id = GNUNET_PEER_intern (peer);
3411 cinfo->skip = t->fwd_pid;
3413 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3414 cinfo->fwd_ack = t->fwd_pid + delta;
3415 cinfo->bck_ack = delta;
3417 cinfo->send_buffer =
3418 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3420 GNUNET_assert (GNUNET_OK ==
3421 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3424 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3431 * Get the Flow Control info of a client.
3433 * @param t Tunnel on which to look.
3434 * @param c Client whose ACK to get.
3436 * @return ACK value.
3438 static struct MeshTunnelClientInfo *
3439 tunnel_get_client_fc (struct MeshTunnel *t,
3440 struct MeshClient *c)
3444 for (i = 0; i < t->nclients; i++)
3446 if (t->clients[i] != c)
3448 return &t->clients_fc[i];
3451 return NULL; // avoid compiler / coverity complaints
3456 * Iterator to get the appropiate ACK value from all children nodes.
3458 * @param cls Closue (tunnel).
3459 * @param id Id of the child node.
3462 tunnel_get_child_fwd_ack (void *cls,
3465 struct GNUNET_PeerIdentity peer_id;
3466 struct MeshTunnelChildInfo *cinfo;
3467 struct MeshTunnelChildIteratorContext *ctx = cls;
3468 struct MeshTunnel *t = ctx->t;
3471 GNUNET_PEER_resolve (id, &peer_id);
3472 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3473 ack = cinfo->fwd_ack;
3476 if (GNUNET_NO == ctx->init)
3478 ctx->max_child_ack = ack;
3479 ctx->init = GNUNET_YES;
3482 if (GNUNET_YES == t->speed_min)
3484 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3488 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3495 * Get the maximum PID allowed to transmit to any
3496 * tunnel child of the local peer, depending on the tunnel
3497 * buffering/speed settings.
3501 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3504 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3506 struct MeshTunnelChildIteratorContext ctx;
3508 ctx.max_child_ack = 0;
3510 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3512 if (0 == ctx.nchildren)
3514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3515 " tunnel has no children, no FWD ACK\n");
3519 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3520 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3522 return (int64_t) ctx.max_child_ack;
3527 * Set the FWD ACK value of a client in a particular tunnel.
3529 * @param t Tunnel affected.
3530 * @param c Client whose ACK to set.
3531 * @param ack ACK value.
3534 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3535 struct MeshClient *c,
3540 for (i = 0; i < t->nclients; i++)
3542 if (t->clients[i] != c)
3544 t->clients_fc[i].fwd_ack = ack;
3552 * Get the highest ACK value of all clients in a particular tunnel,
3553 * according to the buffering/speed settings.
3555 * @param t Tunnel on which to look.
3557 * @return Corresponding ACK value (max uint32_t).
3558 * If no clients are suscribed, -1LL.
3561 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3566 if (0 == t->nclients)
3568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3569 " tunnel has no clients, no FWD ACK\n");
3573 for (ack = -1LL, i = 0; i < t->nclients; i++)
3576 (GNUNET_YES == t->speed_min &&
3577 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3578 (GNUNET_NO == t->speed_min &&
3579 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3581 ack = t->clients_fc[i].fwd_ack;
3585 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3586 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3588 return (uint32_t) ack;
3593 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3594 * mode and the status of all children nodes.
3598 * @return Maximum PID allowed.
3601 tunnel_get_fwd_ack (struct MeshTunnel *t)
3605 uint32_t buffer_free;
3609 count = t->fwd_pid - t->skip;
3610 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3611 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3612 child_ack = tunnel_get_children_fwd_ack (t);
3613 client_ack = tunnel_get_clients_fwd_ack (t);
3614 if (-1LL == child_ack)
3616 // Node has no children, child_ack AND core buffer are irrelevant.
3617 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3618 return (uint32_t) client_ack;
3620 if (-1LL == client_ack)
3624 if (GNUNET_YES == t->speed_min)
3626 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3627 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3631 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3632 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3634 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3635 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3637 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3638 count, buffer_free, child_ack, client_ack, ack);
3644 * Build a local ACK message and send it to a local client.
3646 * @param t Tunnel on which to send the ACK.
3647 * @param c Client to whom send the ACK.
3648 * @param ack Value of the ACK.
3651 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3653 struct GNUNET_MESH_LocalAck msg;
3655 msg.header.size = htons (sizeof (msg));
3656 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3657 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3658 msg.max_pid = htonl (ack);
3659 GNUNET_SERVER_notification_context_unicast(nc,
3666 * Build an ACK message and queue it to send to the given peer.
3668 * @param t Tunnel on which to send the ACK.
3669 * @param peer Peer to whom send the ACK.
3670 * @param ack Value of the ACK.
3673 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3675 struct GNUNET_MESH_ACK msg;
3677 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3678 msg.header.size = htons (sizeof (msg));
3679 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3680 msg.pid = htonl (ack);
3681 msg.tid = htonl (t->id.tid);
3683 send_message (&msg.header, peer, t);
3688 * Notify a the owner of a tunnel about how many more
3689 * payload packages will we accept on a given tunnel.
3691 * @param t Tunnel on which to send the ACK.
3694 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3699 "Sending client FWD ACK on tunnel %X\n",
3702 ack = tunnel_get_fwd_ack (t);
3704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3705 if (t->last_fwd_ack == ack)
3708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3709 t->last_fwd_ack = ack;
3710 send_local_ack (t, t->owner, ack);
3715 * Send an ACK informing the predecessor about the available buffer space.
3716 * In case there is no predecessor, inform the owning client.
3717 * If buffering is off, send only on behalf of children or self if endpoint.
3718 * If buffering is on, send when sent to children and buffer space is free.
3719 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3720 * the ACK itself goes "back" (towards root).
3722 * @param t Tunnel on which to send the ACK.
3723 * @param type Type of message that triggered the ACK transmission.
3726 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3728 struct GNUNET_PeerIdentity id;
3731 if (NULL != t->owner)
3733 tunnel_send_client_fwd_ack (t);
3736 /* Is it after unicast / multicast retransmission? */
3739 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3740 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3742 "ACK due to FWD DATA retransmission\n");
3743 if (GNUNET_YES == t->nobuffer)
3745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3749 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3750 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3756 /* Check if we need no retransmit the ACK */
3757 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3758 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3762 " t->qmax: %u, t->qn: %u\n",
3763 t->fwd_queue_max, t->fwd_queue_n);
3764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3765 " t->pid: %u, t->ack: %u\n",
3766 t->fwd_pid, t->last_fwd_ack);
3770 /* Ok, ACK might be necessary, what PID to ACK? */
3771 ack = tunnel_get_fwd_ack (t);
3773 /* If speed_min and not all children have ack'd, dont send yet */
3774 if (ack == t->last_fwd_ack)
3776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3780 t->last_fwd_ack = ack;
3781 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3782 send_ack (t, &id, ack);
3788 * Iterator to send a child node a BCK ACK to allow him to send more
3791 * @param cls Closure (tunnel).
3792 * @param id Id of the child node.
3795 tunnel_send_child_bck_ack (void *cls,
3798 struct MeshTunnel *t = cls;
3799 struct MeshTunnelChildInfo *cinfo;
3800 struct GNUNET_PeerIdentity peer;
3802 GNUNET_PEER_resolve (id, &peer);
3803 cinfo = tunnel_get_neighbor_fc (t, &peer);
3805 if (cinfo->bck_ack != cinfo->pid &&
3806 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3810 send_ack (t, &peer, cinfo->bck_ack);
3815 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3817 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3819 * @param t Tunnel on which to send the BCK ACKs.
3822 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3825 unsigned int tunnel_delta;
3827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3829 tunnel_delta = t->bck_ack - t->bck_pid;
3830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3832 /* Find client whom to allow to send to origin (with lowest buffer space) */
3833 for (i = 0; i < t->nclients; i++)
3835 struct MeshTunnelClientInfo *clinfo;
3838 clinfo = &t->clients_fc[i];
3839 delta = clinfo->bck_ack - clinfo->bck_pid;
3840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3841 t->clients[i]->id, delta);
3843 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3844 (GNUNET_YES == t->nobuffer && 0 == delta))
3848 ack = clinfo->bck_pid;
3849 ack += t->nobuffer ? 1 : tunnel_delta;
3850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3851 " sending ack to client %u: %u\n",
3852 t->clients[i]->id, ack);
3853 send_local_ack (t, t->clients[i], ack);
3854 clinfo->bck_ack = ack;
3861 * Send an ACK informing the children nodes and destination clients about
3862 * the available buffer space.
3863 * If buffering is off, send only on behalf of root (can be self).
3864 * If buffering is on, send when sent to predecessor and buffer space is free.
3865 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3866 * the ACK itself goes "forward" (towards children/clients).
3868 * @param t Tunnel on which to send the ACK.
3869 * @param type Type of message that triggered the ACK transmission.
3872 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3874 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3875 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3876 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3877 /* Is it after data to_origin retransmission? */
3880 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3881 if (GNUNET_YES == t->nobuffer)
3883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3884 " Not sending ACK, nobuffer\n");
3888 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3889 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3895 tunnel_send_clients_bck_ack (t);
3896 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3901 * @brief Re-initiate traffic to this peer if necessary.
3903 * Check if there is traffic queued towards this peer
3904 * and the core transmit handle is NULL (traffic was stalled).
3905 * If so, call core tmt rdy.
3907 * @param cls Closure (unused)
3908 * @param peer_id Short ID of peer to which initiate traffic.
3911 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3913 struct MeshPeerInfo *peer;
3914 struct GNUNET_PeerIdentity id;
3915 struct MeshPeerQueue *q;
3918 peer = peer_info_get_short(peer_id);
3919 if (NULL != peer->core_transmit)
3922 q = queue_get_next(peer);
3925 /* Might br multicast traffic already sent to this particular peer but
3926 * not to other children in this tunnel.
3927 * This way t->queue_n would be > 0 but the queue of this particular peer
3933 GNUNET_PEER_resolve (peer->id, &id);
3934 peer->core_transmit =
3935 GNUNET_CORE_notify_transmit_ready(core_handle,
3938 GNUNET_TIME_UNIT_FOREVER_REL,
3948 * @brief Allow transmission of FWD traffic on this tunnel
3950 * Check if there is traffic queued towards any children
3951 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3953 * @param t Tunnel on which to unlock FWD traffic.
3956 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3958 if (0 == t->fwd_queue_n)
3961 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3966 * @brief Allow transmission of BCK traffic on this tunnel
3968 * Check if there is traffic queued towards the root of the tree
3969 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3971 * @param t Tunnel on which to unlock BCK traffic.
3974 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3976 if (0 == t->bck_queue_n)
3979 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3984 * Send a message to all peers in this tunnel that the tunnel is no longer
3987 * @param t The tunnel whose peers to notify.
3990 tunnel_send_destroy (struct MeshTunnel *t)
3992 struct GNUNET_MESH_TunnelDestroy msg;
3994 msg.header.size = htons (sizeof (msg));
3995 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3996 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3997 msg.tid = htonl (t->id.tid);
3998 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4003 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4005 * @param cls Closure (Tunnel which to cancel).
4006 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4009 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4011 struct MeshTunnel *t = cls;
4012 struct MeshPeerInfo *peer_info;
4013 struct MeshPeerQueue *pq;
4014 struct MeshPeerQueue *next;
4016 peer_info = peer_info_get_short (neighbor_id);
4017 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4020 if (pq->tunnel == t)
4022 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4023 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4024 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4026 // Should have been removed on destroy children
4029 queue_destroy (pq, GNUNET_YES);
4032 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4034 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4035 peer_info->core_transmit = NULL;
4040 * Destroy the tunnel and free any allocated resources linked to it.
4042 * @param t the tunnel to destroy
4044 * @return GNUNET_OK on success
4047 tunnel_destroy (struct MeshTunnel *t)
4049 struct MeshClient *c;
4050 struct GNUNET_HashCode hash;
4061 struct GNUNET_PeerIdentity id;
4063 GNUNET_PEER_resolve (t->id.oid, &id);
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4065 GNUNET_i2s (&id), t->id.tid);
4067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4071 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4072 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4077 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4080 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4084 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4085 for (i = 0; i < t->nclients; i++)
4089 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4094 for (i = 0; i < t->nignore; i++)
4098 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4103 if (t->nclients > 0)
4106 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4110 GNUNET_free (t->clients);
4112 if (NULL != t->peers)
4114 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4116 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4119 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4120 &tunnel_destroy_child,
4122 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4124 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4126 tree_destroy (t->tree);
4128 if (NULL != t->regex_ctx)
4129 regex_cancel_search (t->regex_ctx);
4130 if (NULL != t->dht_get_type)
4131 GNUNET_DHT_get_stop (t->dht_get_type);
4132 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4133 GNUNET_SCHEDULER_cancel (t->timeout_task);
4134 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4135 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4138 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4139 GNUNET_assert (0 <= n_tunnels);
4146 * Create a new tunnel
4148 * @param owner Who is the owner of the tunnel (short ID).
4149 * @param tid Tunnel Number of the tunnel.
4150 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4151 * @param local Tunnel Number for the tunnel, for the client point of view.
4153 * @return A new initialized tunnel. NULL on error.
4155 static struct MeshTunnel *
4156 tunnel_new (GNUNET_PEER_Id owner,
4157 MESH_TunnelNumber tid,
4158 struct MeshClient *client,
4159 MESH_TunnelNumber local)
4161 struct MeshTunnel *t;
4162 struct GNUNET_HashCode hash;
4164 if (n_tunnels >= max_tunnels && NULL == client)
4167 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4170 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4171 t->bck_queue_max = t->fwd_queue_max;
4172 t->tree = tree_new (owner);
4174 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4175 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4176 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4177 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4178 t->local_tid = local;
4179 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4181 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4183 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4185 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4186 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4193 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4200 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4202 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4203 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4207 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4217 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4218 * that are no longer needed, as well as nodes of no longer reachable peers.
4219 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4221 * @param t Tunnel from which to remove the path.
4222 * @param peer Short id of the peer which should be removed.
4225 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4227 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4233 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4234 * client when the client disconnects. If the client is not the owner, the
4235 * owner will get notified if no more clients are in the tunnel and the client
4236 * get removed from the tunnel's list.
4238 * @param cls closure (client that is disconnecting)
4239 * @param key the hash of the local tunnel id (used to access the hashmap)
4240 * @param value the value stored at the key (tunnel to destroy)
4242 * @return GNUNET_OK on success
4245 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4247 struct MeshTunnel *t = value;
4248 struct MeshClient *c = cls;
4251 send_client_tunnel_disconnect(t, c);
4254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4255 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4256 tunnel_delete_client(t, c);
4257 client_delete_tunnel(c, t);
4260 tunnel_send_destroy(t);
4261 r = tunnel_destroy (t);
4267 * Timeout function, destroys tunnel if called
4269 * @param cls Closure (tunnel to destroy).
4270 * @param tc TaskContext
4273 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4275 struct MeshTunnel *t = cls;
4277 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4279 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4284 * Resets the tunnel timeout. Starts it if no timeout was running.
4286 * @param t Tunnel whose timeout to reset.
4288 * TODO use heap to improve efficiency of scheduler.
4291 tunnel_reset_timeout (struct MeshTunnel *t)
4293 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4294 GNUNET_SCHEDULER_cancel (t->timeout_task);
4296 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4297 (refresh_path_time, 4), &tunnel_timeout, t);
4301 /******************************************************************************/
4302 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4303 /******************************************************************************/
4306 * Function to send a create path packet to a peer.
4308 * @param cls closure
4309 * @param size number of bytes available in buf
4310 * @param buf where the callee should write the message
4311 * @return number of bytes written to buf
4314 send_core_path_create (void *cls, size_t size, void *buf)
4316 struct MeshPathInfo *info = cls;
4317 struct GNUNET_MESH_ManipulatePath *msg;
4318 struct GNUNET_PeerIdentity *peer_ptr;
4319 struct MeshTunnel *t = info->t;
4320 struct MeshPeerPath *p = info->path;
4325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4327 sizeof (struct GNUNET_MESH_ManipulatePath) +
4328 p->length * sizeof (struct GNUNET_PeerIdentity);
4330 if (size < size_needed || NULL == buf)
4335 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4336 msg->header.size = htons (size_needed);
4337 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4338 msg->tid = ntohl (t->id.tid);
4341 if (GNUNET_YES == t->speed_min)
4342 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4343 if (GNUNET_YES == t->nobuffer)
4344 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4345 msg->opt = htonl(opt);
4347 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4348 for (i = 0; i < p->length; i++)
4350 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4357 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4363 * Fill the core buffer
4365 * @param cls closure (data itself)
4366 * @param size number of bytes available in buf
4367 * @param buf where the callee should write the message
4369 * @return number of bytes written to buf
4372 send_core_data_multicast (void *cls, size_t size, void *buf)
4374 struct MeshTransmissionDescriptor *info = cls;
4377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4378 GNUNET_assert (NULL != info);
4379 GNUNET_assert (NULL != info->peer);
4380 total_size = info->mesh_data->data_len;
4381 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4383 if (total_size > size)
4388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4389 memcpy (buf, info->mesh_data->data, total_size);
4392 struct GNUNET_MESH_Multicast *mc;
4393 struct GNUNET_MessageHeader *mh;
4396 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4398 mc = (struct GNUNET_MESH_Multicast *) mh;
4399 mh = (struct GNUNET_MessageHeader *) &mc[1];
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4401 " multicast, payload type %s\n",
4402 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4404 " multicast, payload size %u\n", ntohs (mh->size));
4408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4409 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4413 data_descriptor_decrement_rc (info->mesh_data);
4414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4422 * Creates a path ack message in buf and frees all unused resources.
4424 * @param cls closure (MeshTransmissionDescriptor)
4425 * @param size number of bytes available in buf
4426 * @param buf where the callee should write the message
4427 * @return number of bytes written to buf
4430 send_core_path_ack (void *cls, size_t size, void *buf)
4432 struct MeshTransmissionDescriptor *info = cls;
4433 struct GNUNET_MESH_PathACK *msg = buf;
4435 GNUNET_assert (NULL != info);
4436 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4441 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4442 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4443 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4444 msg->tid = htonl (info->origin->tid);
4445 msg->peer_id = my_full_id;
4448 /* TODO add signature */
4450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4451 return sizeof (struct GNUNET_MESH_PathACK);
4456 * Free a transmission that was already queued with all resources
4457 * associated to the request.
4459 * @param queue Queue handler to cancel.
4460 * @param clear_cls Is it necessary to free associated cls?
4463 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4465 struct MeshTransmissionDescriptor *dd;
4466 struct MeshPathInfo *path_info;
4468 if (GNUNET_YES == clear_cls)
4470 switch (queue->type)
4472 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4473 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4474 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4477 data_descriptor_decrement_rc (dd->mesh_data);
4479 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4481 path_info = queue->cls;
4482 path_destroy (path_info->path);
4486 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4487 " type %s unknown!\n",
4488 GNUNET_MESH_DEBUG_M2S(queue->type));
4490 GNUNET_free_non_null (queue->cls);
4492 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4493 queue->peer->queue_tail,
4495 GNUNET_free (queue);
4500 * @brief Get the next transmittable message from the queue.
4502 * This will be the head, except in the case of being a data packet
4503 * not allowed by the destination peer.
4505 * @param peer Destination peer.
4507 * @return The next viable MeshPeerQueue element to send to that peer.
4508 * NULL when there are no transmittable messages.
4510 struct MeshPeerQueue *
4511 queue_get_next (const struct MeshPeerInfo *peer)
4513 struct MeshPeerQueue *q;
4514 struct MeshTunnel *t;
4515 struct MeshTransmissionDescriptor *info;
4516 struct MeshTunnelChildInfo *cinfo;
4517 struct GNUNET_MESH_Unicast *ucast;
4518 struct GNUNET_MESH_ToOrigin *to_orig;
4519 struct GNUNET_MESH_Multicast *mcast;
4520 struct GNUNET_PeerIdentity id;
4524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4525 for (q = peer->queue_head; NULL != q; q = q->next)
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4531 GNUNET_MESH_DEBUG_M2S(q->type));
4534 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4535 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4536 pid = ntohl (ucast->pid);
4537 GNUNET_PEER_resolve (info->peer->id, &id);
4538 cinfo = tunnel_get_neighbor_fc(t, &id);
4539 ack = cinfo->fwd_ack;
4541 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4542 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4543 pid = ntohl (to_orig->pid);
4546 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4547 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4548 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4550 // Not a multicast payload: multicast control traffic (destroy, etc)
4553 pid = ntohl (mcast->pid);
4554 GNUNET_PEER_resolve (info->peer->id, &id);
4555 cinfo = tunnel_get_neighbor_fc(t, &id);
4556 ack = cinfo->fwd_ack;
4559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4564 "********* ACK: %u, PID: %u\n",
4566 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4575 "********* NEXT!\n");
4578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4579 "********* nothing found\n");
4585 * Core callback to write a queued packet to core buffer
4587 * @param cls Closure (peer info).
4588 * @param size Number of bytes available in buf.
4589 * @param buf Where the to write the message.
4591 * @return number of bytes written to buf
4594 queue_send (void *cls, size_t size, void *buf)
4596 struct MeshPeerInfo *peer = cls;
4597 struct GNUNET_MessageHeader *msg;
4598 struct MeshPeerQueue *queue;
4599 struct MeshTunnel *t;
4600 struct MeshTunnelChildInfo *cinfo;
4601 struct GNUNET_PeerIdentity dst_id;
4604 peer->core_transmit = NULL;
4606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4607 queue = queue_get_next(peer);
4609 /* Queue has no internal mesh traffic not sendable payload */
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4613 if (NULL == peer->queue_head)
4614 GNUNET_break(0); // Should've been canceled
4617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4619 GNUNET_PEER_resolve (peer->id, &dst_id);
4620 /* Check if buffer size is enough for the message */
4621 if (queue->size > size)
4625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4626 "********* not enough room, reissue\n");
4627 peer->core_transmit =
4628 GNUNET_CORE_notify_transmit_ready(core_handle,
4631 GNUNET_TIME_UNIT_FOREVER_REL,
4638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4641 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4645 "********* unicast: t->q (%u/%u)\n",
4646 t->fwd_queue_n, t->fwd_queue_max);
4648 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4655 switch (queue->type)
4658 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4659 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4660 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4662 "********* raw: %s\n",
4663 GNUNET_MESH_DEBUG_M2S (queue->type));
4665 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4666 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4667 data_size = send_core_data_raw (queue->cls, size, buf);
4668 msg = (struct GNUNET_MessageHeader *) buf;
4669 switch (ntohs (msg->type)) // Type of preconstructed message
4671 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4672 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4674 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4675 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4681 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4684 struct MeshTransmissionDescriptor *info = queue->cls;
4686 if ((1 == info->mesh_data->reference_counter
4687 && GNUNET_YES == t->speed_min)
4689 (info->mesh_data->total_out == info->mesh_data->reference_counter
4690 && GNUNET_NO == t->speed_min))
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4693 "********* considered sent\n");
4697 data_size = send_core_data_multicast(queue->cls, size, buf);
4698 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4700 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4702 data_size = send_core_path_create(queue->cls, size, buf);
4704 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4706 data_size = send_core_path_ack(queue->cls, size, buf);
4710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4711 "********* type unknown: %u\n",
4715 switch (queue->type)
4717 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4718 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4719 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4720 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4721 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4725 "at pos %u (%p) != %p\n",
4726 cinfo->send_buffer_start,
4727 cinfo->send_buffer[cinfo->send_buffer_start],
4730 GNUNET_break(cinfo->send_buffer_n > 0);
4731 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4732 cinfo->send_buffer_n--;
4733 cinfo->send_buffer_start++;
4734 cinfo->send_buffer_start %= t->fwd_queue_max;
4740 /* Free queue, but cls was freed by send_core_* */
4741 queue_destroy (queue, GNUNET_NO);
4743 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4749 /* If more data in queue, send next */
4750 queue = queue_get_next(peer);
4753 struct GNUNET_PeerIdentity id;
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4756 GNUNET_PEER_resolve (peer->id, &id);
4757 peer->core_transmit =
4758 GNUNET_CORE_notify_transmit_ready(core_handle,
4761 GNUNET_TIME_UNIT_FOREVER_REL,
4769 if (NULL != peer->queue_head)
4770 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4771 "********* %s stalled\n",
4772 GNUNET_i2s(&my_full_id));
4774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4780 * Queue and pass message to core when possible.
4782 * @param cls Closure (type dependant).
4783 * @param type Type of the message, 0 for a raw message.
4784 * @param size Size of the message.
4785 * @param dst Neighbor to send message to.
4786 * @param t Tunnel this message belongs to.
4789 queue_add (void *cls, uint16_t type, size_t size,
4790 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4792 struct MeshPeerQueue *queue;
4793 struct MeshTunnelChildInfo *cinfo;
4794 struct GNUNET_PeerIdentity id;
4800 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4801 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4803 n = &t->fwd_queue_n;
4804 max = &t->fwd_queue_max;
4806 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4808 n = &t->bck_queue_n;
4809 max = &t->bck_queue_max;
4814 if (NULL == t->owner)
4815 GNUNET_break_op(0); // TODO: kill connection?
4818 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4820 return; // Drop message
4824 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4830 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4831 GNUNET_PEER_resolve (dst->id, &id);
4832 if (NULL == dst->core_transmit)
4834 dst->core_transmit =
4835 GNUNET_CORE_notify_transmit_ready(core_handle,
4838 GNUNET_TIME_UNIT_FOREVER_REL,
4844 if (NULL == n) // Is this internal mesh traffic?
4847 // It's payload, keep track of buffer per peer.
4848 cinfo = tunnel_get_neighbor_fc(t, &id);
4849 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4850 if (NULL != cinfo->send_buffer[i])
4852 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4853 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4854 cinfo->send_buffer_start++;
4855 cinfo->send_buffer_start %= t->fwd_queue_max;
4856 cinfo->send_buffer_n--;
4858 cinfo->send_buffer[i] = queue;
4859 cinfo->send_buffer_n++;
4860 if (cinfo->send_buffer_n > t->fwd_queue_max)
4863 cinfo->send_buffer_n = t->fwd_queue_max;
4868 /******************************************************************************/
4869 /******************** MESH NETWORK HANDLERS **************************/
4870 /******************************************************************************/
4874 * Core handler for path creation
4876 * @param cls closure
4877 * @param message message
4878 * @param peer peer identity this notification is about
4879 * @param atsi performance data
4880 * @param atsi_count number of records in 'atsi'
4882 * @return GNUNET_OK to keep the connection open,
4883 * GNUNET_SYSERR to close it (signal serious error)
4886 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4887 const struct GNUNET_MessageHeader *message,
4888 const struct GNUNET_ATS_Information *atsi,
4889 unsigned int atsi_count)
4891 unsigned int own_pos;
4894 MESH_TunnelNumber tid;
4895 struct GNUNET_MESH_ManipulatePath *msg;
4896 struct GNUNET_PeerIdentity *pi;
4897 struct GNUNET_HashCode hash;
4898 struct MeshPeerPath *path;
4899 struct MeshPeerInfo *dest_peer_info;
4900 struct MeshPeerInfo *orig_peer_info;
4901 struct MeshTunnel *t;
4903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4904 "Received a path create msg [%s]\n",
4905 GNUNET_i2s (&my_full_id));
4906 size = ntohs (message->size);
4907 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4909 GNUNET_break_op (0);
4913 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4914 if (size % sizeof (struct GNUNET_PeerIdentity))
4916 GNUNET_break_op (0);
4919 size /= sizeof (struct GNUNET_PeerIdentity);
4922 GNUNET_break_op (0);
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4926 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4928 tid = ntohl (msg->tid);
4929 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4931 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4932 t = tunnel_get (pi, tid);
4933 if (NULL == t) // FIXME only for INCOMING tunnels?
4937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4938 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4941 // FIXME notify failure
4944 opt = ntohl (msg->opt);
4945 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4946 GNUNET_YES : GNUNET_NO;
4947 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4948 GNUNET_YES : GNUNET_NO;
4949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4950 " speed_min: %d, nobuffer:%d\n",
4951 t->speed_min, t->nobuffer);
4953 if (GNUNET_YES == t->nobuffer)
4955 t->bck_queue_max = 1;
4956 t->fwd_queue_max = 1;
4959 // FIXME only assign a local tid if a local client is interested (on demand)
4960 while (NULL != tunnel_get_incoming (next_local_tid))
4961 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4962 t->local_tid_dest = next_local_tid++;
4963 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4966 tunnel_reset_timeout (t);
4967 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4969 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4970 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4978 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4979 if (NULL == dest_peer_info)
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4982 " Creating PeerInfo for destination.\n");
4983 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4984 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4985 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4987 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4989 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4990 if (NULL == orig_peer_info)
4992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4993 " Creating PeerInfo for origin.\n");
4994 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4995 orig_peer_info->id = GNUNET_PEER_intern (pi);
4996 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4997 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5000 path = path_new (size);
5002 for (i = 0; i < size; i++)
5004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5005 GNUNET_i2s (&pi[i]));
5006 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5007 if (path->peers[i] == myid)
5010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5013 /* cannot be self, must be 'not found' */
5014 /* create path: self not found in path through self */
5015 GNUNET_break_op (0);
5016 path_destroy (path);
5020 path_add_to_peers (path, GNUNET_NO);
5021 tunnel_add_path (t, path, own_pos);
5022 if (own_pos == size - 1)
5024 /* It is for us! Send ack. */
5025 struct MeshTransmissionDescriptor *info;
5027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5028 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5029 if (NULL == t->peers)
5031 /* New tunnel! Notify clients on data. */
5032 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5034 GNUNET_break (GNUNET_SYSERR !=
5035 GNUNET_CONTAINER_multihashmap_put (t->peers,
5036 &my_full_id.hashPubKey,
5039 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5040 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5041 info->origin = &t->id;
5042 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5043 GNUNET_assert (NULL != info->peer);
5045 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5046 sizeof (struct GNUNET_MESH_PathACK),
5052 struct MeshPeerPath *path2;
5054 /* It's for somebody else! Retransmit. */
5055 path2 = path_duplicate (path);
5056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5057 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5058 path2 = path_duplicate (path);
5059 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5060 send_create_path (dest_peer_info, path, t);
5067 * Core handler for path destruction
5069 * @param cls closure
5070 * @param message message
5071 * @param peer peer identity this notification is about
5072 * @param atsi performance data
5073 * @param atsi_count number of records in 'atsi'
5075 * @return GNUNET_OK to keep the connection open,
5076 * GNUNET_SYSERR to close it (signal serious error)
5079 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5080 const struct GNUNET_MessageHeader *message,
5081 const struct GNUNET_ATS_Information *atsi,
5082 unsigned int atsi_count)
5084 struct GNUNET_MESH_ManipulatePath *msg;
5085 struct GNUNET_PeerIdentity *pi;
5086 struct MeshPeerPath *path;
5087 struct MeshTunnel *t;
5088 unsigned int own_pos;
5092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5093 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5094 size = ntohs (message->size);
5095 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5097 GNUNET_break_op (0);
5101 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5102 if (size % sizeof (struct GNUNET_PeerIdentity))
5104 GNUNET_break_op (0);
5107 size /= sizeof (struct GNUNET_PeerIdentity);
5110 GNUNET_break_op (0);
5113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5115 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5116 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5118 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5120 t = tunnel_get (pi, ntohl (msg->tid));
5123 /* TODO notify back: we don't know this tunnel */
5124 GNUNET_break_op (0);
5127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5128 path = path_new (size);
5130 for (i = 0; i < size; i++)
5132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5133 GNUNET_i2s (&pi[i]));
5134 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5135 if (path->peers[i] == myid)
5138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5139 if (own_pos < path->length - 1)
5140 send_message (message, &pi[own_pos + 1], t);
5142 send_client_tunnel_disconnect(t, NULL);
5144 tunnel_delete_peer (t, path->peers[path->length - 1]);
5145 path_destroy (path);
5151 * Core handler for notifications of broken paths
5153 * @param cls closure
5154 * @param message message
5155 * @param peer peer identity this notification is about
5156 * @param atsi performance data
5157 * @param atsi_count number of records in 'atsi'
5159 * @return GNUNET_OK to keep the connection open,
5160 * GNUNET_SYSERR to close it (signal serious error)
5163 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5164 const struct GNUNET_MessageHeader *message,
5165 const struct GNUNET_ATS_Information *atsi,
5166 unsigned int atsi_count)
5168 struct GNUNET_MESH_PathBroken *msg;
5169 struct MeshTunnel *t;
5171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5172 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5173 msg = (struct GNUNET_MESH_PathBroken *) message;
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5175 GNUNET_i2s (&msg->peer1));
5176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5177 GNUNET_i2s (&msg->peer2));
5178 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5181 GNUNET_break_op (0);
5184 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5185 GNUNET_PEER_search (&msg->peer2));
5192 * Core handler for tunnel destruction
5194 * @param cls closure
5195 * @param message message
5196 * @param peer peer identity this notification is about
5197 * @param atsi performance data
5198 * @param atsi_count number of records in 'atsi'
5200 * @return GNUNET_OK to keep the connection open,
5201 * GNUNET_SYSERR to close it (signal serious error)
5204 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5205 const struct GNUNET_MessageHeader *message,
5206 const struct GNUNET_ATS_Information *atsi,
5207 unsigned int atsi_count)
5209 struct GNUNET_MESH_TunnelDestroy *msg;
5210 struct MeshTunnel *t;
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5213 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5214 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5216 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5217 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5220 /* Probably already got the message from another path,
5221 * destroyed the tunnel and retransmitted to children.
5224 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5227 if (t->id.oid == myid)
5229 GNUNET_break_op (0);
5232 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5234 /* Tunnel was incoming, notify clients */
5235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5236 t->local_tid, t->local_tid_dest);
5237 send_clients_tunnel_destroy (t);
5239 tunnel_send_destroy (t);
5240 t->destroy = GNUNET_YES;
5241 // TODO: add timeout to destroy the tunnel anyway
5247 * Core handler for mesh network traffic going from the origin to a peer
5249 * @param cls closure
5250 * @param peer peer identity this notification is about
5251 * @param message message
5252 * @param atsi performance data
5253 * @param atsi_count number of records in 'atsi'
5254 * @return GNUNET_OK to keep the connection open,
5255 * GNUNET_SYSERR to close it (signal serious error)
5258 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5259 const struct GNUNET_MessageHeader *message,
5260 const struct GNUNET_ATS_Information *atsi,
5261 unsigned int atsi_count)
5263 struct GNUNET_MESH_Unicast *msg;
5264 struct GNUNET_PeerIdentity *neighbor;
5265 struct MeshTunnelChildInfo *cinfo;
5266 struct MeshTunnel *t;
5267 GNUNET_PEER_Id dest_id;
5272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5275 size = ntohs (message->size);
5277 sizeof (struct GNUNET_MESH_Unicast) +
5278 sizeof (struct GNUNET_MessageHeader))
5283 msg = (struct GNUNET_MESH_Unicast *) message;
5284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5285 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5287 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5290 /* TODO notify back: we don't know this tunnel */
5291 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5292 GNUNET_break_op (0);
5295 pid = ntohl (msg->pid);
5296 if (t->fwd_pid == pid)
5298 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5300 " Already seen pid %u, DROPPING!\n", pid);
5305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5306 " pid %u not seen yet, forwarding\n", pid);
5309 t->skip += (pid - t->fwd_pid) - 1;
5312 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5314 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5315 GNUNET_break_op (0);
5316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5317 "Received PID %u, ACK %u\n",
5318 pid, t->last_fwd_ack);
5322 tunnel_reset_timeout (t);
5323 dest_id = GNUNET_PEER_search (&msg->destination);
5324 if (dest_id == myid)
5326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5327 " it's for us! sending to clients...\n");
5328 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5329 send_subscribed_clients (message, &msg[1].header, t);
5330 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5333 ttl = ntohl (msg->ttl);
5334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5337 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5338 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5339 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5343 " not for us, retransmitting...\n");
5345 neighbor = tree_get_first_hop (t->tree, dest_id);
5346 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5348 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5351 if (GNUNET_YES == t->nobuffer &&
5352 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5354 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5355 GNUNET_break_op (0);
5358 send_message (message, neighbor, t);
5359 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5365 * Core handler for mesh network traffic going from the origin to all peers
5367 * @param cls closure
5368 * @param message message
5369 * @param peer peer identity this notification is about
5370 * @param atsi performance data
5371 * @param atsi_count number of records in 'atsi'
5372 * @return GNUNET_OK to keep the connection open,
5373 * GNUNET_SYSERR to close it (signal serious error)
5375 * TODO: Check who we got this from, to validate route.
5378 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5379 const struct GNUNET_MessageHeader *message,
5380 const struct GNUNET_ATS_Information *atsi,
5381 unsigned int atsi_count)
5383 struct GNUNET_MESH_Multicast *msg;
5384 struct MeshTunnel *t;
5388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5390 size = ntohs (message->size);
5391 if (sizeof (struct GNUNET_MESH_Multicast) +
5392 sizeof (struct GNUNET_MessageHeader) > size)
5394 GNUNET_break_op (0);
5397 msg = (struct GNUNET_MESH_Multicast *) message;
5398 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5402 /* TODO notify that we dont know that tunnel */
5403 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5404 GNUNET_break_op (0);
5407 pid = ntohl (msg->pid);
5408 if (t->fwd_pid == pid)
5410 /* already seen this packet, drop */
5411 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5413 " Already seen pid %u, DROPPING!\n", pid);
5414 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5420 " pid %u not seen yet, forwarding\n", pid);
5422 t->skip += (pid - t->fwd_pid) - 1;
5424 tunnel_reset_timeout (t);
5426 /* Transmit to locally interested clients */
5427 if (NULL != t->peers &&
5428 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5430 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5431 send_subscribed_clients (message, &msg[1].header, t);
5432 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5435 if (ntohl (msg->ttl) == 0)
5437 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5439 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5442 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5443 tunnel_send_multicast (t, message, GNUNET_NO);
5449 * Core handler for mesh network traffic toward the owner of a tunnel
5451 * @param cls closure
5452 * @param message message
5453 * @param peer peer identity this notification is about
5454 * @param atsi performance data
5455 * @param atsi_count number of records in 'atsi'
5457 * @return GNUNET_OK to keep the connection open,
5458 * GNUNET_SYSERR to close it (signal serious error)
5461 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5462 const struct GNUNET_MessageHeader *message,
5463 const struct GNUNET_ATS_Information *atsi,
5464 unsigned int atsi_count)
5466 struct GNUNET_MESH_ToOrigin *msg;
5467 struct GNUNET_PeerIdentity id;
5468 struct MeshPeerInfo *peer_info;
5469 struct MeshTunnel *t;
5473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5475 size = ntohs (message->size);
5476 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5477 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5479 GNUNET_break_op (0);
5482 msg = (struct GNUNET_MESH_ToOrigin *) message;
5483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5484 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5485 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5489 /* TODO notify that we dont know this tunnel (whom)? */
5490 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5491 GNUNET_break_op (0);
5495 if (NULL != t->owner)
5498 struct GNUNET_MESH_ToOrigin *copy;
5500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5501 " it's for us! sending to clients...\n");
5502 /* TODO signature verification */
5503 memcpy (cbuf, message, size);
5504 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5505 copy->tid = htonl (t->local_tid);
5506 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5507 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5508 ©->header, GNUNET_NO);
5509 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5513 " not for us, retransmitting...\n");
5515 peer_info = peer_info_get (&msg->oid);
5516 if (NULL == peer_info)
5518 /* unknown origin of tunnel */
5522 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5523 send_message (message, &id, t);
5524 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5531 * Core handler for mesh network traffic point-to-point acks.
5533 * @param cls closure
5534 * @param message message
5535 * @param peer peer identity this notification is about
5536 * @param atsi performance data
5537 * @param atsi_count number of records in 'atsi'
5539 * @return GNUNET_OK to keep the connection open,
5540 * GNUNET_SYSERR to close it (signal serious error)
5543 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5544 const struct GNUNET_MessageHeader *message,
5545 const struct GNUNET_ATS_Information *atsi,
5546 unsigned int atsi_count)
5548 struct GNUNET_MESH_ACK *msg;
5549 struct MeshTunnel *t;
5552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5554 msg = (struct GNUNET_MESH_ACK *) message;
5556 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5560 /* TODO notify that we dont know this tunnel (whom)? */
5561 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5562 GNUNET_break_op (0);
5565 ack = ntohl (msg->pid);
5567 /* Is this a forward or backward ACK? */
5568 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5570 struct MeshTunnelChildInfo *cinfo;
5573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5574 cinfo = tunnel_get_neighbor_fc (t, peer);
5575 cinfo->fwd_ack = ack;
5576 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5577 tunnel_unlock_fwd_queues (t);
5581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5583 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5584 tunnel_unlock_bck_queue (t);
5591 * Core handler for path ACKs
5593 * @param cls closure
5594 * @param message message
5595 * @param peer peer identity this notification is about
5596 * @param atsi performance data
5597 * @param atsi_count number of records in 'atsi'
5599 * @return GNUNET_OK to keep the connection open,
5600 * GNUNET_SYSERR to close it (signal serious error)
5603 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5604 const struct GNUNET_MessageHeader *message,
5605 const struct GNUNET_ATS_Information *atsi,
5606 unsigned int atsi_count)
5608 struct GNUNET_MESH_PathACK *msg;
5609 struct GNUNET_PeerIdentity id;
5610 struct MeshPeerInfo *peer_info;
5611 struct MeshPeerPath *p;
5612 struct MeshTunnel *t;
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5615 GNUNET_i2s (&my_full_id));
5616 msg = (struct GNUNET_MESH_PathACK *) message;
5617 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5620 /* TODO notify that we don't know the tunnel */
5621 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5623 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5627 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5629 peer_info = peer_info_get (&msg->peer_id);
5630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5631 GNUNET_i2s (&msg->peer_id));
5632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5635 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5638 "connect_by_string completed, stopping search\n");
5639 regex_cancel_search (t->regex_ctx);
5640 t->regex_ctx = NULL;
5643 /* Add paths to peers? */
5644 p = tree_get_path_to_peer (t->tree, peer_info->id);
5647 path_add_to_peers (p, GNUNET_YES);
5655 /* Message for us? */
5656 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5659 if (NULL == t->owner)
5661 GNUNET_break_op (0);
5664 if (NULL != t->dht_get_type)
5666 GNUNET_DHT_get_stop (t->dht_get_type);
5667 t->dht_get_type = NULL;
5669 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5671 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5672 send_client_peer_connected (t, peer_info->id);
5677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5678 " not for us, retransmitting...\n");
5679 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5680 peer_info = peer_info_get (&msg->oid);
5681 if (NULL == peer_info)
5683 /* If we know the tunnel, we should DEFINITELY know the peer */
5687 send_message (message, &id, t);
5693 * Core handler for mesh keepalives.
5695 * @param cls closure
5696 * @param message message
5697 * @param peer peer identity this notification is about
5698 * @param atsi performance data
5699 * @param atsi_count number of records in 'atsi'
5700 * @return GNUNET_OK to keep the connection open,
5701 * GNUNET_SYSERR to close it (signal serious error)
5703 * TODO: Check who we got this from, to validate route.
5706 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5707 const struct GNUNET_MessageHeader *message,
5708 const struct GNUNET_ATS_Information *atsi,
5709 unsigned int atsi_count)
5711 struct GNUNET_MESH_TunnelKeepAlive *msg;
5712 struct MeshTunnel *t;
5714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5717 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5718 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5722 /* TODO notify that we dont know that tunnel */
5723 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5724 GNUNET_break_op (0);
5728 tunnel_reset_timeout (t);
5730 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5731 tunnel_send_multicast (t, message, GNUNET_NO);
5738 * Functions to handle messages from core
5740 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5741 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5742 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5743 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5744 sizeof (struct GNUNET_MESH_PathBroken)},
5745 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5746 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5747 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5748 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5749 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5750 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5751 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5752 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5753 sizeof (struct GNUNET_MESH_ACK)},
5754 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5755 sizeof (struct GNUNET_MESH_PathACK)},
5761 /******************************************************************************/
5762 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5763 /******************************************************************************/
5766 * deregister_app: iterator for removing each application registered by a client
5768 * @param cls closure
5769 * @param key the hash of the application id (used to access the hashmap)
5770 * @param value the value stored at the key (client)
5772 * @return GNUNET_OK on success
5775 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5777 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5778 GNUNET_break (GNUNET_YES ==
5779 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5785 * notify_client_connection_failure: notify a client that the connection to the
5786 * requested remote peer is not possible (for instance, no route found)
5787 * Function called when the socket is ready to queue more data. "buf" will be
5788 * NULL and "size" zero if the socket was closed for writing in the meantime.
5790 * @param cls closure
5791 * @param size number of bytes available in buf
5792 * @param buf where the callee should write the message
5793 * @return number of bytes written to buf
5796 notify_client_connection_failure (void *cls, size_t size, void *buf)
5799 struct MeshPeerInfo *peer_info;
5800 struct GNUNET_MESH_PeerControl *msg;
5801 struct GNUNET_PeerIdentity id;
5803 if (0 == size && NULL == buf)
5805 // TODO retry? cancel?
5809 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5810 peer_info = (struct MeshPeerInfo *) cls;
5811 msg = (struct GNUNET_MESH_PeerControl *) buf;
5812 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5813 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5814 // msg->tunnel_id = htonl(peer_info->t->tid);
5815 GNUNET_PEER_resolve (peer_info->id, &id);
5816 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5824 * Send keepalive packets for a peer
5826 * @param cls Closure (tunnel for which to send the keepalive).
5827 * @param tc Notification context.
5830 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5832 struct MeshTunnel *t = cls;
5833 struct GNUNET_MESH_TunnelKeepAlive *msg;
5834 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5837 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5838 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5844 "sending keepalive for tunnel %d\n", t->id.tid);
5846 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5847 msg->header.size = htons (size);
5848 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5849 msg->oid = my_full_id;
5850 msg->tid = htonl (t->id.tid);
5851 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5853 t->path_refresh_task =
5854 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5860 * Function to process paths received for a new peer addition. The recorded
5861 * paths form the initial tunnel, which can be optimized later.
5862 * Called on each result obtained for the DHT search.
5864 * @param cls closure
5865 * @param exp when will this value expire
5866 * @param key key of the result
5867 * @param get_path path of the get request
5868 * @param get_path_length lenght of get_path
5869 * @param put_path path of the put request
5870 * @param put_path_length length of the put_path
5871 * @param type type of the result
5872 * @param size number of bytes in data
5873 * @param data pointer to the result data
5875 * TODO: re-issue the request after certain time? cancel after X results?
5878 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5879 const struct GNUNET_HashCode * key,
5880 const struct GNUNET_PeerIdentity *get_path,
5881 unsigned int get_path_length,
5882 const struct GNUNET_PeerIdentity *put_path,
5883 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5884 size_t size, const void *data)
5886 struct MeshPathInfo *path_info = cls;
5887 struct MeshPeerPath *p;
5888 struct GNUNET_PeerIdentity pi;
5891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5892 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5895 p = path_build_from_dht (get_path, get_path_length, put_path,
5897 path_add_to_peers (p, GNUNET_NO);
5899 for (i = 0; i < path_info->peer->ntunnels; i++)
5901 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5902 peer_info_connect (path_info->peer, path_info->t);
5910 * Function to process paths received for a new peer addition. The recorded
5911 * paths form the initial tunnel, which can be optimized later.
5912 * Called on each result obtained for the DHT search.
5914 * @param cls closure
5915 * @param exp when will this value expire
5916 * @param key key of the result
5917 * @param get_path path of the get request
5918 * @param get_path_length lenght of get_path
5919 * @param put_path path of the put request
5920 * @param put_path_length length of the put_path
5921 * @param type type of the result
5922 * @param size number of bytes in data
5923 * @param data pointer to the result data
5926 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5927 const struct GNUNET_HashCode * key,
5928 const struct GNUNET_PeerIdentity *get_path,
5929 unsigned int get_path_length,
5930 const struct GNUNET_PeerIdentity *put_path,
5931 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5932 size_t size, const void *data)
5934 const struct PBlock *pb = data;
5935 const struct GNUNET_PeerIdentity *pi = &pb->id;
5936 struct MeshTunnel *t = cls;
5937 struct MeshPeerInfo *peer_info;
5938 struct MeshPeerPath *p;
5940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5941 if (size != sizeof (struct PBlock))
5943 GNUNET_break_op (0);
5946 if (ntohl(pb->type) != t->type)
5948 GNUNET_break_op (0);
5951 GNUNET_assert (NULL != t->owner);
5952 peer_info = peer_info_get (pi);
5953 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5955 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5957 p = path_build_from_dht (get_path, get_path_length, put_path,
5959 path_add_to_peers (p, GNUNET_NO);
5961 tunnel_add_peer (t, peer_info);
5962 peer_info_connect (peer_info, t);
5967 * Function to process DHT string to regex matching.
5968 * Called on each result obtained for the DHT search.
5970 * @param cls closure (search context)
5971 * @param exp when will this value expire
5972 * @param key key of the result
5973 * @param get_path path of the get request (not used)
5974 * @param get_path_length lenght of get_path (not used)
5975 * @param put_path path of the put request (not used)
5976 * @param put_path_length length of the put_path (not used)
5977 * @param type type of the result
5978 * @param size number of bytes in data
5979 * @param data pointer to the result data
5982 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5983 const struct GNUNET_HashCode * key,
5984 const struct GNUNET_PeerIdentity *get_path,
5985 unsigned int get_path_length,
5986 const struct GNUNET_PeerIdentity *put_path,
5987 unsigned int put_path_length,
5988 enum GNUNET_BLOCK_Type type,
5989 size_t size, const void *data)
5991 const struct MeshRegexAccept *block = data;
5992 struct MeshRegexSearchContext *ctx = cls;
5993 struct MeshRegexSearchInfo *info = ctx->info;
5994 struct MeshPeerPath *p;
5995 struct MeshPeerInfo *peer_info;
5997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6000 peer_info = peer_info_get(&block->id);
6001 p = path_build_from_dht (get_path, get_path_length, put_path,
6003 path_add_to_peers (p, GNUNET_NO);
6006 tunnel_add_peer (info->t, peer_info);
6007 peer_info_connect (peer_info, info->t);
6008 if (0 == info->peer)
6010 info->peer = peer_info->id;
6014 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6017 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6018 ®ex_connect_timeout,
6026 * Function to process DHT string to regex matching.
6027 * Called on each result obtained for the DHT search.
6029 * @param cls closure (search context)
6030 * @param exp when will this value expire
6031 * @param key key of the result
6032 * @param get_path path of the get request (not used)
6033 * @param get_path_length lenght of get_path (not used)
6034 * @param put_path path of the put request (not used)
6035 * @param put_path_length length of the put_path (not used)
6036 * @param type type of the result
6037 * @param size number of bytes in data
6038 * @param data pointer to the result data
6040 * TODO: re-issue the request after certain time? cancel after X results?
6043 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6044 const struct GNUNET_HashCode * key,
6045 const struct GNUNET_PeerIdentity *get_path,
6046 unsigned int get_path_length,
6047 const struct GNUNET_PeerIdentity *put_path,
6048 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6049 size_t size, const void *data)
6051 const struct MeshRegexBlock *block = data;
6052 struct MeshRegexSearchContext *ctx = cls;
6053 struct MeshRegexSearchInfo *info = ctx->info;
6057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6058 "DHT GET STRING RETURNED RESULTS\n");
6059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6060 " key: %s\n", GNUNET_h2s (key));
6062 copy = GNUNET_malloc (size);
6063 memcpy (copy, data, size);
6064 GNUNET_break (GNUNET_OK ==
6065 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6066 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6067 len = ntohl (block->n_proof);
6069 char proof[len + 1];
6071 memcpy (proof, &block[1], len);
6073 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6075 GNUNET_break_op (0);
6079 len = strlen (info->description);
6080 if (len == ctx->position) // String processed
6082 if (GNUNET_YES == ntohl (block->accepting))
6084 regex_find_path(key, ctx);
6088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6089 // FIXME REGEX this block not successful, wait for more? start timeout?
6093 GNUNET_break (GNUNET_OK ==
6094 GNUNET_MESH_regex_block_iterate (block, size,
6095 ®ex_edge_iterator, ctx));
6099 /******************************************************************************/
6100 /********************* MESH LOCAL HANDLES **************************/
6101 /******************************************************************************/
6105 * Handler for client disconnection
6107 * @param cls closure
6108 * @param client identification of the client; NULL
6109 * for the last call when the server is destroyed
6112 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6114 struct MeshClient *c;
6115 struct MeshClient *next;
6118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6127 if (c->handle != client)
6129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6135 GNUNET_SERVER_client_drop (c->handle);
6136 c->shutting_down = GNUNET_YES;
6137 GNUNET_assert (NULL != c->own_tunnels);
6138 GNUNET_assert (NULL != c->incoming_tunnels);
6139 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6140 &tunnel_destroy_iterator, c);
6141 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6142 &tunnel_destroy_iterator, c);
6143 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6144 &tunnel_destroy_iterator, c);
6145 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6146 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6147 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6149 /* deregister clients applications */
6150 if (NULL != c->apps)
6152 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6153 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6155 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6156 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6158 GNUNET_SCHEDULER_cancel (announce_applications_task);
6159 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6161 if (NULL != c->types)
6162 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6163 for (i = 0; i < c->n_regex; i++)
6165 GNUNET_free (c->regexes[i]);
6167 GNUNET_free_non_null (c->regexes);
6168 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6169 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6171 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6174 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6183 * Handler for new clients
6185 * @param cls closure
6186 * @param client identification of the client
6187 * @param message the actual message, which includes messages the client wants
6190 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6191 const struct GNUNET_MessageHeader *message)
6193 struct GNUNET_MESH_ClientConnect *cc_msg;
6194 struct MeshClient *c;
6195 GNUNET_MESH_ApplicationType *a;
6202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6203 /* Check data sanity */
6204 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6205 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6206 ntypes = ntohs (cc_msg->types);
6207 napps = ntohs (cc_msg->applications);
6209 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6212 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6216 /* Create new client structure */
6217 c = GNUNET_malloc (sizeof (struct MeshClient));
6218 c->id = next_client_id++;
6219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6221 GNUNET_SERVER_client_keep (client);
6222 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6225 GNUNET_MESH_ApplicationType at;
6226 struct GNUNET_HashCode hc;
6228 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6229 for (i = 0; i < napps; i++)
6232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6233 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6234 /* store in clients hashmap */
6235 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6237 /* store in global hashmap, for announcements */
6238 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6241 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6242 announce_applications_task =
6243 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6249 struct GNUNET_HashCode hc;
6251 t = (uint16_t *) & a[napps];
6252 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6253 for (i = 0; i < ntypes; i++)
6256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6257 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6259 /* store in clients hashmap */
6260 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6261 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6262 /* store in global hashmap */
6263 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6264 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6268 " client has %u+%u subscriptions\n", napps, ntypes);
6270 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6271 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6272 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6273 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6274 GNUNET_SERVER_notification_context_add (nc, client);
6275 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6277 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6283 * Handler for clients announcing available services by a regular expression.
6285 * @param cls closure
6286 * @param client identification of the client
6287 * @param message the actual message, which includes messages the client wants
6290 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6291 const struct GNUNET_MessageHeader *message)
6293 struct MeshClient *c;
6297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6299 /* Sanity check for client registration */
6300 if (NULL == (c = client_get (client)))
6303 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6308 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6309 regex = GNUNET_malloc (len + 1);
6310 memcpy (regex, &message[1], len);
6312 GNUNET_array_append (c->regexes, c->n_regex, regex);
6313 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6315 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6321 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6327 * Handler for requests of new tunnels
6329 * @param cls closure
6330 * @param client identification of the client
6331 * @param message the actual message
6334 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6335 const struct GNUNET_MessageHeader *message)
6337 struct GNUNET_MESH_TunnelMessage *t_msg;
6338 struct MeshTunnel *t;
6339 struct MeshClient *c;
6340 MESH_TunnelNumber tid;
6342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6344 /* Sanity check for client registration */
6345 if (NULL == (c = client_get (client)))
6348 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6353 /* Message sanity check */
6354 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6361 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6362 /* Sanity check for tunnel numbering */
6363 tid = ntohl (t_msg->tunnel_id);
6364 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6367 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6370 /* Sanity check for duplicate tunnel IDs */
6371 if (NULL != tunnel_get_by_local_id (c, tid))
6374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6378 while (NULL != tunnel_get_by_pi (myid, next_tid))
6379 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6380 t = tunnel_new (myid, next_tid++, c, tid);
6383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6385 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6388 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6390 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6391 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6394 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6400 * Handler for requests of deleting tunnels
6402 * @param cls closure
6403 * @param client identification of the client
6404 * @param message the actual message
6407 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6408 const struct GNUNET_MessageHeader *message)
6410 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6411 struct MeshClient *c;
6412 struct MeshTunnel *t;
6413 MESH_TunnelNumber tid;
6415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6416 "Got a DESTROY TUNNEL from client!\n");
6418 /* Sanity check for client registration */
6419 if (NULL == (c = client_get (client)))
6422 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6427 /* Message sanity check */
6428 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6431 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6435 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6437 /* Retrieve tunnel */
6438 tid = ntohl (tunnel_msg->tunnel_id);
6439 t = tunnel_get_by_local_id(c, tid);
6442 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6444 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6447 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6449 client_ignore_tunnel (c, t);
6451 // TODO: when to destroy incoming tunnel?
6452 if (t->nclients == 0)
6454 GNUNET_assert (GNUNET_YES ==
6455 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6457 GNUNET_assert (GNUNET_YES ==
6458 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6459 &my_full_id.hashPubKey,
6463 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6466 send_client_tunnel_disconnect(t, c);
6467 client_delete_tunnel(c, t);
6469 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6471 tunnel_send_destroy (t);
6472 t->destroy = GNUNET_YES;
6473 // The tunnel will be destroyed when the last message is transmitted.
6474 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6480 * Handler for requests of seeting tunnel's speed.
6482 * @param cls Closure (unused).
6483 * @param client Identification of the client.
6484 * @param message The actual message.
6487 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6488 const struct GNUNET_MessageHeader *message)
6490 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6491 struct MeshClient *c;
6492 struct MeshTunnel *t;
6493 MESH_TunnelNumber tid;
6495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6496 "Got a SPEED request from client!\n");
6498 /* Sanity check for client registration */
6499 if (NULL == (c = client_get (client)))
6502 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6508 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6510 /* Retrieve tunnel */
6511 tid = ntohl (tunnel_msg->tunnel_id);
6512 t = tunnel_get_by_local_id(c, tid);
6515 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6517 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6521 switch (ntohs(message->type))
6523 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6524 t->speed_min = GNUNET_YES;
6526 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6527 t->speed_min = GNUNET_NO;
6532 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6537 * Handler for requests of seeting tunnel's buffering policy.
6539 * @param cls Closure (unused).
6540 * @param client Identification of the client.
6541 * @param message The actual message.
6544 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6545 const struct GNUNET_MessageHeader *message)
6547 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6548 struct MeshClient *c;
6549 struct MeshTunnel *t;
6550 MESH_TunnelNumber tid;
6552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6553 "Got a BUFFER request from client!\n");
6555 /* Sanity check for client registration */
6556 if (NULL == (c = client_get (client)))
6559 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6564 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6566 /* Retrieve tunnel */
6567 tid = ntohl (tunnel_msg->tunnel_id);
6568 t = tunnel_get_by_local_id(c, tid);
6571 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6573 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6577 switch (ntohs(message->type))
6579 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6580 t->nobuffer = GNUNET_NO;
6582 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6583 t->nobuffer = GNUNET_YES;
6589 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6594 * Handler for connection requests to new peers
6596 * @param cls closure
6597 * @param client identification of the client
6598 * @param message the actual message (PeerControl)
6601 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6602 const struct GNUNET_MessageHeader *message)
6604 struct GNUNET_MESH_PeerControl *peer_msg;
6605 struct MeshPeerInfo *peer_info;
6606 struct MeshClient *c;
6607 struct MeshTunnel *t;
6608 MESH_TunnelNumber tid;
6610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6611 /* Sanity check for client registration */
6612 if (NULL == (c = client_get (client)))
6615 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6620 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6622 /* Sanity check for message size */
6623 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6626 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6630 /* Tunnel exists? */
6631 tid = ntohl (peer_msg->tunnel_id);
6632 t = tunnel_get_by_local_id (c, tid);
6636 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6640 /* Does client own tunnel? */
6641 if (t->owner->handle != client)
6644 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6648 GNUNET_i2s (&peer_msg->peer));
6649 peer_info = peer_info_get (&peer_msg->peer);
6651 tunnel_add_peer (t, peer_info);
6652 peer_info_connect (peer_info, t);
6654 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6660 * Handler for disconnection requests of peers in a tunnel
6662 * @param cls closure
6663 * @param client identification of the client
6664 * @param message the actual message (PeerControl)
6667 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6668 const struct GNUNET_MessageHeader *message)
6670 struct GNUNET_MESH_PeerControl *peer_msg;
6671 struct MeshPeerInfo *peer_info;
6672 struct MeshClient *c;
6673 struct MeshTunnel *t;
6674 MESH_TunnelNumber tid;
6676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6677 /* Sanity check for client registration */
6678 if (NULL == (c = client_get (client)))
6681 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6686 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6688 /* Sanity check for message size */
6689 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6692 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6696 /* Tunnel exists? */
6697 tid = ntohl (peer_msg->tunnel_id);
6698 t = tunnel_get_by_local_id (c, tid);
6702 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6707 /* Does client own tunnel? */
6708 if (t->owner->handle != client)
6711 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6716 GNUNET_i2s (&peer_msg->peer));
6717 /* Is the peer in the tunnel? */
6719 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6720 if (NULL == peer_info)
6723 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6727 /* Ok, delete peer from tunnel */
6728 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6729 &peer_msg->peer.hashPubKey);
6731 send_destroy_path (t, peer_info->id);
6732 tunnel_delete_peer (t, peer_info->id);
6733 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6738 * Handler for blacklist requests of peers in a tunnel
6740 * @param cls closure
6741 * @param client identification of the client
6742 * @param message the actual message (PeerControl)
6744 * FIXME implement DHT block bloomfilter
6747 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6748 const struct GNUNET_MessageHeader *message)
6750 struct GNUNET_MESH_PeerControl *peer_msg;
6751 struct MeshClient *c;
6752 struct MeshTunnel *t;
6753 MESH_TunnelNumber tid;
6755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6756 /* Sanity check for client registration */
6757 if (NULL == (c = client_get (client)))
6760 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6765 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6767 /* Sanity check for message size */
6768 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6771 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6775 /* Tunnel exists? */
6776 tid = ntohl (peer_msg->tunnel_id);
6777 t = tunnel_get_by_local_id (c, tid);
6781 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6786 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6787 GNUNET_PEER_intern(&peer_msg->peer));
6792 * Handler for unblacklist requests of peers in a tunnel
6794 * @param cls closure
6795 * @param client identification of the client
6796 * @param message the actual message (PeerControl)
6799 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6800 const struct GNUNET_MessageHeader *message)
6802 struct GNUNET_MESH_PeerControl *peer_msg;
6803 struct MeshClient *c;
6804 struct MeshTunnel *t;
6805 MESH_TunnelNumber tid;
6809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6810 /* Sanity check for client registration */
6811 if (NULL == (c = client_get (client)))
6814 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6819 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6821 /* Sanity check for message size */
6822 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6825 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6829 /* Tunnel exists? */
6830 tid = ntohl (peer_msg->tunnel_id);
6831 t = tunnel_get_by_local_id (c, tid);
6835 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6840 /* if peer is not known, complain */
6841 pid = GNUNET_PEER_search (&peer_msg->peer);
6848 /* search and remove from list */
6849 for (i = 0; i < t->nblacklisted; i++)
6851 if (t->blacklisted[i] == pid)
6853 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6854 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6859 /* if peer hasn't been blacklisted, complain */
6865 * Handler for connection requests to new peers by type
6867 * @param cls closure
6868 * @param client identification of the client
6869 * @param message the actual message (ConnectPeerByType)
6872 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6873 const struct GNUNET_MessageHeader *message)
6875 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6876 struct MeshClient *c;
6877 struct MeshTunnel *t;
6878 struct GNUNET_HashCode hash;
6879 MESH_TunnelNumber tid;
6881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6882 /* Sanity check for client registration */
6883 if (NULL == (c = client_get (client)))
6886 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6891 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6893 /* Sanity check for message size */
6894 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6895 ntohs (connect_msg->header.size))
6898 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6902 /* Tunnel exists? */
6903 tid = ntohl (connect_msg->tunnel_id);
6904 t = tunnel_get_by_local_id (c, tid);
6908 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6912 /* Does client own tunnel? */
6913 if (t->owner->handle != client)
6916 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6920 /* Do WE have the service? */
6921 t->type = ntohl (connect_msg->type);
6922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6923 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6924 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6927 /* Yes! Fast forward, add ourselves to the tunnel and send the
6928 * good news to the client, and alert the destination client of
6929 * an incoming tunnel.
6931 * FIXME send a path create to self, avoid code duplication
6933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6934 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6935 peer_info_get (&my_full_id),
6936 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6939 send_client_peer_connected (t, myid);
6940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6941 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6943 t->local_tid_dest = next_local_tid++;
6944 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6945 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6946 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6950 /* Ok, lets find a peer offering the service */
6951 if (NULL != t->dht_get_type)
6953 GNUNET_DHT_get_stop (t->dht_get_type);
6955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6956 GNUNET_h2s (&hash));
6958 GNUNET_DHT_get_start (dht_handle,
6959 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6961 dht_replication_level,
6962 GNUNET_DHT_RO_RECORD_ROUTE |
6963 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6965 &dht_get_type_handler, t);
6967 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6973 * Handler for connection requests to new peers by a string service description.
6975 * @param cls closure
6976 * @param client identification of the client
6977 * @param message the actual message, which includes messages the client wants
6980 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6981 const struct GNUNET_MessageHeader *message)
6983 struct GNUNET_MESH_ConnectPeerByString *msg;
6984 struct MeshRegexSearchContext *ctx;
6985 struct MeshRegexSearchInfo *info;
6986 struct GNUNET_DHT_GetHandle *get_h;
6987 struct GNUNET_HashCode key;
6988 struct MeshTunnel *t;
6989 struct MeshClient *c;
6990 MESH_TunnelNumber tid;
6994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6995 "Connect by string started\n");
6996 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6997 size = htons (message->size);
6999 /* Sanity check for client registration */
7000 if (NULL == (c = client_get (client)))
7003 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7008 /* Message size sanity check */
7009 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7012 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7016 /* Tunnel exists? */
7017 tid = ntohl (msg->tunnel_id);
7018 t = tunnel_get_by_local_id (c, tid);
7022 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7026 /* Does client own tunnel? */
7027 if (t->owner->handle != client)
7030 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7035 " on tunnel %s [%u]\n",
7036 GNUNET_i2s(&my_full_id),
7039 /* Only one connect_by_string allowed at the same time! */
7040 /* FIXME: allow more, return handle at api level to cancel, document */
7041 if (NULL != t->regex_ctx)
7044 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7048 /* Find string itself */
7049 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7050 string = (const char *) &msg[1];
7052 /* Initialize context */
7053 size = GNUNET_REGEX_get_first_key(string, len, &key);
7054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7055 " consumed %u bits out of %u\n", size, len);
7056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7057 " looking for %s\n", GNUNET_h2s (&key));
7059 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7061 info->description = GNUNET_malloc (len + 1);
7062 memcpy (info->description, string, len);
7063 info->description[len] = '\0';
7064 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7065 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7068 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7069 ctx->position = size;
7073 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7075 /* Start search in DHT */
7076 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7077 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7078 &key, /* key to search */
7079 dht_replication_level, /* replication level */
7080 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7081 NULL, /* xquery */ // FIXME BLOOMFILTER
7082 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7083 &dht_get_string_handler, ctx);
7085 GNUNET_break (GNUNET_OK ==
7086 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7089 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7091 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7097 * Handler for client traffic directed to one peer
7099 * @param cls closure
7100 * @param client identification of the client
7101 * @param message the actual message
7104 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7105 const struct GNUNET_MessageHeader *message)
7107 struct MeshClient *c;
7108 struct MeshTunnel *t;
7109 struct MeshPeerInfo *pi;
7110 struct GNUNET_MESH_Unicast *data_msg;
7111 MESH_TunnelNumber tid;
7114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7115 "Got a unicast request from a client!\n");
7117 /* Sanity check for client registration */
7118 if (NULL == (c = client_get (client)))
7121 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7126 data_msg = (struct GNUNET_MESH_Unicast *) message;
7128 /* Sanity check for message size */
7129 size = ntohs (message->size);
7130 if (sizeof (struct GNUNET_MESH_Unicast) +
7131 sizeof (struct GNUNET_MessageHeader) > size)
7134 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7138 /* Tunnel exists? */
7139 tid = ntohl (data_msg->tid);
7140 t = tunnel_get_by_local_id (c, tid);
7144 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7148 /* Is it a local tunnel? Then, does client own the tunnel? */
7149 if (t->owner->handle != client)
7152 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7156 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7157 &data_msg->destination.hashPubKey);
7158 /* Is the selected peer in the tunnel? */
7162 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7166 /* PID should be as expected */
7167 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7170 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7171 "Unicast PID, expected %u, got %u\n",
7172 t->fwd_pid + 1, ntohl (data_msg->pid));
7173 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7177 /* Ok, everything is correct, send the message
7178 * (pretend we got it from a mesh peer)
7181 /* Work around const limitation */
7182 char buf[ntohs (message->size)] GNUNET_ALIGN;
7183 struct GNUNET_MESH_Unicast *copy;
7185 copy = (struct GNUNET_MESH_Unicast *) buf;
7186 memcpy (buf, data_msg, size);
7187 copy->oid = my_full_id;
7188 copy->tid = htonl (t->id.tid);
7189 copy->ttl = htonl (default_ttl);
7190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7191 " calling generic handler...\n");
7192 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7195 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7202 * Handler for client traffic directed to the origin
7204 * @param cls closure
7205 * @param client identification of the client
7206 * @param message the actual message
7209 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7210 const struct GNUNET_MessageHeader *message)
7212 struct GNUNET_MESH_ToOrigin *data_msg;
7213 struct MeshTunnelClientInfo *clinfo;
7214 struct MeshClient *c;
7215 struct MeshTunnel *t;
7216 MESH_TunnelNumber tid;
7219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7220 "Got a ToOrigin request from a client!\n");
7221 /* Sanity check for client registration */
7222 if (NULL == (c = client_get (client)))
7225 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7230 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7232 /* Sanity check for message size */
7233 size = ntohs (message->size);
7234 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7235 sizeof (struct GNUNET_MessageHeader) > size)
7238 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7242 /* Tunnel exists? */
7243 tid = ntohl (data_msg->tid);
7244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7245 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7248 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7251 t = tunnel_get_by_local_id (c, tid);
7254 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7255 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7257 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7261 /* It should be sent by someone who has this as incoming tunnel. */
7262 if (GNUNET_NO == client_knows_tunnel (c, t))
7265 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7269 /* PID should be as expected */
7270 clinfo = tunnel_get_client_fc (t, c);
7271 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7274 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7275 "To Origin PID, expected %u, got %u\n",
7276 clinfo->bck_pid + 1,
7277 ntohl (data_msg->pid));
7278 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7282 /* Ok, everything is correct, send the message
7283 * (pretend we got it from a mesh peer)
7287 char buf[ntohs (message->size)] GNUNET_ALIGN;
7288 struct GNUNET_MESH_ToOrigin *copy;
7290 /* Work around const limitation */
7291 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7292 memcpy (buf, data_msg, size);
7293 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7294 copy->tid = htonl (t->id.tid);
7295 copy->ttl = htonl (default_ttl);
7296 if (ntohl (copy->pid) != (t->bck_pid + 1))
7299 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7300 "To Origin PID, expected %u, got %u\n",
7306 copy->sender = my_full_id;
7307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7308 " calling generic handler...\n");
7309 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7311 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7318 * Handler for client traffic directed to all peers in a tunnel
7320 * @param cls closure
7321 * @param client identification of the client
7322 * @param message the actual message
7325 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7326 const struct GNUNET_MessageHeader *message)
7328 struct MeshClient *c;
7329 struct MeshTunnel *t;
7330 struct GNUNET_MESH_Multicast *data_msg;
7331 MESH_TunnelNumber tid;
7333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7334 "Got a multicast request from a client!\n");
7336 /* Sanity check for client registration */
7337 if (NULL == (c = client_get (client)))
7340 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7345 data_msg = (struct GNUNET_MESH_Multicast *) message;
7347 /* Sanity check for message size */
7348 if (sizeof (struct GNUNET_MESH_Multicast) +
7349 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7352 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7356 /* Tunnel exists? */
7357 tid = ntohl (data_msg->tid);
7358 t = tunnel_get_by_local_id (c, tid);
7362 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7365 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7369 /* Does client own tunnel? */
7370 if (t->owner->handle != client)
7373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7377 /* PID should be as expected */
7378 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7381 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7382 "Multicast PID, expected %u, got %u\n",
7383 t->fwd_pid + 1, ntohl (data_msg->pid));
7384 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7389 char buf[ntohs (message->size)] GNUNET_ALIGN;
7390 struct GNUNET_MESH_Multicast *copy;
7392 copy = (struct GNUNET_MESH_Multicast *) buf;
7393 memcpy (buf, message, ntohs (message->size));
7394 copy->oid = my_full_id;
7395 copy->tid = htonl (t->id.tid);
7396 copy->ttl = htonl (default_ttl);
7397 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7399 " calling generic handler...\n");
7400 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7403 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7409 * Handler for client's ACKs for payload traffic.
7411 * @param cls Closure (unused).
7412 * @param client Identification of the client.
7413 * @param message The actual message.
7416 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7417 const struct GNUNET_MessageHeader *message)
7419 struct GNUNET_MESH_LocalAck *msg;
7420 struct MeshTunnel *t;
7421 struct MeshClient *c;
7422 MESH_TunnelNumber tid;
7425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7426 /* Sanity check for client registration */
7427 if (NULL == (c = client_get (client)))
7430 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7435 msg = (struct GNUNET_MESH_LocalAck *) message;
7437 /* Tunnel exists? */
7438 tid = ntohl (msg->tunnel_id);
7439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7440 t = tunnel_get_by_local_id (c, tid);
7444 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7445 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7446 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7450 ack = ntohl (msg->max_pid);
7451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7453 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7454 if (NULL != t->owner && t->owner->handle == client)
7456 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7458 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7462 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7463 tunnel_set_client_fwd_ack (t, c, ack);
7464 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7467 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7474 * Functions to handle messages from clients
7476 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7477 {&handle_local_new_client, NULL,
7478 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7479 {&handle_local_announce_regex, NULL,
7480 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7481 {&handle_local_tunnel_create, NULL,
7482 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7483 sizeof (struct GNUNET_MESH_TunnelMessage)},
7484 {&handle_local_tunnel_destroy, NULL,
7485 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7486 sizeof (struct GNUNET_MESH_TunnelMessage)},
7487 {&handle_local_tunnel_speed, NULL,
7488 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7489 sizeof (struct GNUNET_MESH_TunnelMessage)},
7490 {&handle_local_tunnel_speed, NULL,
7491 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7492 sizeof (struct GNUNET_MESH_TunnelMessage)},
7493 {&handle_local_tunnel_buffer, NULL,
7494 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7495 sizeof (struct GNUNET_MESH_TunnelMessage)},
7496 {&handle_local_tunnel_buffer, NULL,
7497 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7498 sizeof (struct GNUNET_MESH_TunnelMessage)},
7499 {&handle_local_connect_add, NULL,
7500 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7501 sizeof (struct GNUNET_MESH_PeerControl)},
7502 {&handle_local_connect_del, NULL,
7503 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7504 sizeof (struct GNUNET_MESH_PeerControl)},
7505 {&handle_local_blacklist, NULL,
7506 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7507 sizeof (struct GNUNET_MESH_PeerControl)},
7508 {&handle_local_unblacklist, NULL,
7509 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7510 sizeof (struct GNUNET_MESH_PeerControl)},
7511 {&handle_local_connect_by_type, NULL,
7512 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7513 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7514 {&handle_local_connect_by_string, NULL,
7515 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7516 {&handle_local_unicast, NULL,
7517 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7518 {&handle_local_to_origin, NULL,
7519 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7520 {&handle_local_multicast, NULL,
7521 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7522 {&handle_local_ack, NULL,
7523 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7524 sizeof (struct GNUNET_MESH_LocalAck)},
7530 * To be called on core init/fail.
7532 * @param cls service closure
7533 * @param server handle to the server for this service
7534 * @param identity the public identity of this peer
7537 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7538 const struct GNUNET_PeerIdentity *identity)
7541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7542 core_handle = server;
7543 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7546 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7547 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7549 GNUNET_abort(); // Try harder
7556 * Method called whenever a given peer connects.
7558 * @param cls closure
7559 * @param peer peer identity this notification is about
7560 * @param atsi performance data for the connection
7561 * @param atsi_count number of records in 'atsi'
7564 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7565 const struct GNUNET_ATS_Information *atsi,
7566 unsigned int atsi_count)
7568 struct MeshPeerInfo *peer_info;
7569 struct MeshPeerPath *path;
7571 DEBUG_CONN ("Peer connected\n");
7572 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7573 peer_info = peer_info_get (peer);
7574 if (myid == peer_info->id)
7576 DEBUG_CONN (" (self)\n");
7581 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7583 path = path_new (2);
7584 path->peers[0] = myid;
7585 path->peers[1] = peer_info->id;
7586 GNUNET_PEER_change_rc (myid, 1);
7587 GNUNET_PEER_change_rc (peer_info->id, 1);
7588 peer_info_add_path (peer_info, path, GNUNET_YES);
7589 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7595 * Method called whenever a peer disconnects.
7597 * @param cls closure
7598 * @param peer peer identity this notification is about
7601 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7603 struct MeshPeerInfo *pi;
7604 struct MeshPeerQueue *q;
7605 struct MeshPeerQueue *n;
7607 DEBUG_CONN ("Peer disconnected\n");
7608 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7620 /* try to reroute this traffic instead */
7621 queue_destroy(q, GNUNET_YES);
7625 peer_info_remove_path (pi, pi->id, myid);
7628 DEBUG_CONN (" (self)\n");
7630 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7635 /******************************************************************************/
7636 /************************ MAIN FUNCTIONS ****************************/
7637 /******************************************************************************/
7640 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7642 * @param cls closure
7643 * @param key current key code
7644 * @param value value in the hash map
7645 * @return GNUNET_YES if we should continue to iterate,
7649 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7651 struct MeshTunnel *t = value;
7658 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7660 * @param cls closure
7661 * @param key current key code
7662 * @param value value in the hash map
7663 * @return GNUNET_YES if we should continue to iterate,
7667 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7669 struct MeshPeerInfo *p = value;
7670 struct MeshPeerQueue *q;
7671 struct MeshPeerQueue *n;
7679 queue_destroy(q, GNUNET_YES);
7683 peer_info_destroy (p);
7688 * Task run during shutdown.
7694 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7698 if (core_handle != NULL)
7700 GNUNET_CORE_disconnect (core_handle);
7703 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7704 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7705 if (dht_handle != NULL)
7707 GNUNET_DHT_disconnect (dht_handle);
7712 GNUNET_SERVER_notification_context_destroy (nc);
7715 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7717 GNUNET_SCHEDULER_cancel (announce_id_task);
7718 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7724 * Process mesh requests.
7726 * @param cls closure
7727 * @param server the initialized server
7728 * @param c configuration to use
7731 run (void *cls, struct GNUNET_SERVER_Handle *server,
7732 const struct GNUNET_CONFIGURATION_Handle *c)
7734 struct MeshPeerInfo *peer;
7735 struct MeshPeerPath *p;
7738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7739 server_handle = server;
7740 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7741 NULL, /* Closure passed to MESH functions */
7742 &core_init, /* Call core_init once connected */
7743 &core_connect, /* Handle connects */
7744 &core_disconnect, /* remove peers on disconnects */
7745 NULL, /* Don't notify about all incoming messages */
7746 GNUNET_NO, /* For header only in notification */
7747 NULL, /* Don't notify about all outbound messages */
7748 GNUNET_NO, /* For header-only out notification */
7749 core_handlers); /* Register these handlers */
7751 if (core_handle == NULL)
7754 GNUNET_SCHEDULER_shutdown ();
7759 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7762 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7764 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7766 GNUNET_SCHEDULER_shutdown ();
7771 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7772 &refresh_path_time))
7774 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7776 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7777 "refresh path time");
7778 GNUNET_SCHEDULER_shutdown ();
7783 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7784 &app_announce_time))
7786 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7788 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7789 "app announce time");
7790 GNUNET_SCHEDULER_shutdown ();
7795 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7798 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7800 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7801 "id announce time");
7802 GNUNET_SCHEDULER_shutdown ();
7807 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7808 &unacknowledged_wait_time))
7810 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7812 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7813 "unacknowledged wait time");
7814 GNUNET_SCHEDULER_shutdown ();
7819 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7822 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7824 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7826 GNUNET_SCHEDULER_shutdown ();
7831 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7834 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7836 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7838 GNUNET_SCHEDULER_shutdown ();
7843 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7846 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7848 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7850 GNUNET_SCHEDULER_shutdown ();
7855 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7858 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7860 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7866 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7867 &dht_replication_level))
7869 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7871 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7872 "dht replication level", 10);
7873 dht_replication_level = 10;
7877 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7878 GNUNET_free (keyfile);
7879 if (my_private_key == NULL)
7881 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7882 _("Mesh service could not access hostkey. Exiting.\n"));
7883 GNUNET_SCHEDULER_shutdown ();
7886 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7887 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7888 &my_full_id.hashPubKey);
7889 myid = GNUNET_PEER_intern (&my_full_id);
7890 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7891 "Mesh for peer [%s] starting\n",
7892 GNUNET_i2s(&my_full_id));
7894 // transport_handle = GNUNET_TRANSPORT_connect(c,
7901 dht_handle = GNUNET_DHT_connect (c, 64);
7902 if (dht_handle == NULL)
7907 stats = GNUNET_STATISTICS_create ("mesh", c);
7911 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7913 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7914 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7915 peers = GNUNET_CONTAINER_multihashmap_create (32);
7916 applications = GNUNET_CONTAINER_multihashmap_create (32);
7917 types = GNUNET_CONTAINER_multihashmap_create (32);
7919 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7920 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7921 GNUNET_SERVER_disconnect_notify (server_handle,
7922 &handle_local_client_disconnect, NULL);
7926 clients_tail = NULL;
7929 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7930 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7932 /* Create a peer_info for the local peer */
7933 peer = peer_info_get (&my_full_id);
7936 GNUNET_PEER_change_rc (myid, 1);
7937 peer_info_add_path (peer, p, GNUNET_YES);
7939 /* Scheduled the task to clean up when shutdown is called */
7940 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7947 * The main function for the mesh service.
7949 * @param argc number of arguments from the command line
7950 * @param argv command line arguments
7951 * @return 0 ok, 1 on error
7954 main (int argc, char *const *argv)
7958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7961 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7967 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7968 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
7969 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);