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, const struct GNUNET_HashCode *key, const char *proof,
1381 int accepting, unsigned int num_edges,
1382 const struct GNUNET_REGEX_Edge *edges)
1384 struct MeshRegexBlock *block;
1385 struct MeshRegexEdge *block_edge;
1386 enum GNUNET_DHT_RouteOption opt;
1390 unsigned int offset;
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394 " regex dht put for state %s\n",
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1404 if (GNUNET_YES == accepting)
1406 struct MeshRegexAccept block;
1408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1409 " state %s is accepting, putting own id\n",
1411 size = sizeof (block);
1413 block.id = my_full_id;
1415 GNUNET_DHT_put(dht_handle, key,
1416 dht_replication_level,
1417 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1418 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1421 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1426 len = strlen(proof);
1427 size = sizeof (struct MeshRegexBlock) + len;
1428 block = GNUNET_malloc (size);
1431 block->n_proof = htonl (len);
1432 block->n_edges = htonl (num_edges);
1433 block->accepting = htonl (accepting);
1435 /* Store the proof at the end of the block. */
1436 aux = (char *) &block[1];
1437 memcpy (aux, proof, len);
1440 /* Store each edge in a variable length MeshEdge struct at the
1441 * very end of the MeshRegexBlock structure.
1443 for (i = 0; i < num_edges; i++)
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1446 " edge %s towards %s\n",
1448 GNUNET_h2s(&edges[i].destination));
1450 /* aux points at the end of the last block */
1451 len = strlen (edges[i].label);
1452 size += sizeof (struct MeshRegexEdge) + len;
1453 // Calculate offset FIXME is this ok? use size instead?
1454 offset = aux - (char *) block;
1455 block = GNUNET_realloc (block, size);
1456 aux = &((char *) block)[offset];
1457 block_edge = (struct MeshRegexEdge *) aux;
1458 block_edge->key = edges[i].destination;
1459 block_edge->n_token = htonl (len);
1460 aux = (char *) &block_edge[1];
1461 memcpy (aux, edges[i].label, len);
1465 GNUNET_DHT_put(dht_handle, key,
1466 dht_replication_level,
1468 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1470 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1474 GNUNET_free (block);
1479 * Store the regular expression describing a local service into the DHT.
1481 * @param regex The regular expresion.
1484 regex_put (const char *regex)
1486 struct GNUNET_REGEX_Automaton *dfa;
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1489 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1490 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1491 GNUNET_REGEX_automaton_destroy (dfa);
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1497 * Find a path to a peer that offers a regex servcie compatible
1498 * with a given string.
1500 * @param key The key of the accepting state.
1501 * @param ctx Context containing info about the string, tunnel, etc.
1504 regex_find_path (const struct GNUNET_HashCode *key,
1505 struct MeshRegexSearchContext *ctx)
1507 struct GNUNET_DHT_GetHandle *get_h;
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1510 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1511 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1512 key, /* key to search */
1513 dht_replication_level, /* replication level */
1514 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1515 GNUNET_DHT_RO_RECORD_ROUTE,
1516 NULL, /* xquery */ // FIXME BLOOMFILTER
1517 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1518 &dht_get_string_accept_handler, ctx);
1519 GNUNET_break (GNUNET_OK ==
1520 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1523 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1528 * Function called if the connect attempt to a peer found via
1529 * connect_by_string times out. Try to connect to another peer, if any.
1530 * Otherwise try to reconnect to the same peer.
1532 * @param cls Closure (info about regex search).
1533 * @param tc TaskContext.
1536 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1538 struct MeshRegexSearchInfo *info = cls;
1539 struct MeshPeerInfo *peer_info;
1543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1544 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1545 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1553 if (0 < info->n_peers)
1555 // Select next peer, put current in that spot.
1556 id = info->peers[info->i_peer];
1557 info->peers[info->i_peer] = info->peer;
1558 info->i_peer = (info->i_peer + 1) % info->n_peers;
1562 // Try to connect to same peer again.
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1567 peer_info = peer_info_get_short(id);
1568 tunnel_add_peer (info->t, peer_info);
1570 tunnel_delete_peer (info->t, old);
1571 peer_info_connect (peer_info, info->t);
1572 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1573 ®ex_connect_timeout,
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1580 * Cancel an ongoing regex search in the DHT and free all resources.
1582 * @param ctx The search context.
1585 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1587 struct MeshRegexSearchInfo *info = ctx->info;
1590 GNUNET_free (info->description);
1591 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1592 ®ex_cancel_dht_get, NULL);
1593 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1594 ®ex_free_result, NULL);
1595 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1596 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1597 info->t->regex_ctx = NULL;
1598 for (i = 0; i < info->n_contexts; i++)
1600 GNUNET_free (info->contexts[i]);
1602 if (0 < info->n_contexts)
1603 GNUNET_free (info->contexts);
1604 if (0 < info->n_peers)
1605 GNUNET_free (info->peers);
1606 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1608 GNUNET_SCHEDULER_cancel(info->timeout);
1614 /******************************************************************************/
1615 /************************ PERIODIC FUNCTIONS ****************************/
1616 /******************************************************************************/
1619 * Announce iterator over for each application provided by the peer
1621 * @param cls closure
1622 * @param key current key code
1623 * @param value value in the hash map
1624 * @return GNUNET_YES if we should continue to
1629 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1631 struct PBlock block;
1632 struct MeshClient *c;
1634 block.id = my_full_id;
1635 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1636 GNUNET_assert(NULL != c);
1637 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1638 if (0 == block.type)
1643 block.type = htonl (block.type);
1645 GNUNET_break (NULL !=
1646 GNUNET_DHT_put (dht_handle, key,
1647 dht_replication_level,
1648 GNUNET_DHT_RO_RECORD_ROUTE |
1649 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1650 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1652 (const char *) &block,
1653 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1655 app_announce_time, NULL, NULL));
1661 * Periodically announce what applications are provided by local clients
1664 * @param cls closure
1665 * @param tc task context
1668 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1670 struct MeshClient *c = cls;
1673 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1675 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1679 DEBUG_DHT ("Starting PUT for regex\n");
1681 for (i = 0; i < c->n_regex; i++)
1683 regex_put (c->regexes[i]);
1685 c->regex_announce_task =
1686 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1687 DEBUG_DHT ("Finished PUT for regex\n");
1694 * Periodically announce what applications are provided by local clients
1697 * @param cls closure
1698 * @param tc task context
1701 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1703 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1705 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1709 DEBUG_DHT ("Starting PUT for apps\n");
1711 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1713 announce_applications_task =
1714 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1716 DEBUG_DHT ("Finished PUT for apps\n");
1723 * Periodically announce self id in the DHT
1725 * @param cls closure
1726 * @param tc task context
1729 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1731 struct PBlock block;
1733 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1735 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1739 * - Set data expiration in function of X
1740 * - Adapt X to churn
1742 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1744 block.id = my_full_id;
1745 block.type = htonl (0);
1746 GNUNET_DHT_put (dht_handle, /* DHT handle */
1747 &my_full_id.hashPubKey, /* Key to use */
1748 dht_replication_level, /* Replication level */
1749 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1750 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1751 sizeof (block), /* Size of the data */
1752 (const char *) &block, /* Data itself */
1753 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1754 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1755 NULL, /* Continuation */
1756 NULL); /* Continuation closure */
1758 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1762 /******************************************************************************/
1763 /****************** GENERAL HELPER FUNCTIONS ************************/
1764 /******************************************************************************/
1767 * Decrements the reference counter and frees all resources if needed
1769 * @param mesh_data Data Descriptor used in a multicast message.
1770 * Freed no longer needed (last message).
1773 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1775 if (0 == --(mesh_data->reference_counter))
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1778 GNUNET_free (mesh_data->data);
1779 GNUNET_free (mesh_data);
1785 * Check if client has registered with the service and has not disconnected
1787 * @param client the client to check
1789 * @return non-NULL if client exists in the global DLL
1791 static struct MeshClient *
1792 client_get (struct GNUNET_SERVER_Client *client)
1794 struct MeshClient *c;
1799 if (c->handle == client)
1808 * Checks if a given client has subscribed to certain message type
1810 * @param message_type Type of message to check
1811 * @param c Client to check
1813 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1815 * FIXME: use of crypto_hash slows it down
1816 * The hash function alone takes 8-10us out of the ~55us for the whole
1817 * process of retransmitting the message from one local client to another.
1818 * Find faster implementation!
1821 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1823 struct GNUNET_HashCode hc;
1825 if (NULL == c->types)
1828 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1829 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1834 * Check whether client wants traffic from a tunnel.
1836 * @param c Client to check.
1837 * @param t Tunnel to be found.
1839 * @return GNUNET_YES if client knows tunnel.
1841 * TODO look in client hashmap
1844 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1848 for (i = 0; i < t->nclients; i++)
1849 if (t->clients[i] == c)
1856 * Check whether client has been informed about a tunnel.
1858 * @param c Client to check.
1859 * @param t Tunnel to be found.
1861 * @return GNUNET_YES if client knows tunnel.
1863 * TODO look in client hashmap
1866 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1870 for (i = 0; i < t->nignore; i++)
1871 if (t->ignore[i] == c)
1873 return client_wants_tunnel(c, t);
1878 * Marks a client as uninterested in traffic from the tunnel, updating both
1879 * client and tunnel to reflect this.
1881 * @param c Client that doesn't want traffic anymore.
1882 * @param t Tunnel which should be ignored.
1884 * FIXME when to delete an incoming tunnel?
1887 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1889 struct GNUNET_HashCode hash;
1891 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1892 GNUNET_break (GNUNET_YES ==
1893 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1895 GNUNET_break (GNUNET_YES ==
1896 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1897 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1898 tunnel_delete_active_client (t, c);
1899 GNUNET_array_append (t->ignore, t->nignore, c);
1904 * Deletes a tunnel from a client (either owner or destination). To be used on
1905 * tunnel destroy, otherwise, use client_ignore_tunnel.
1907 * @param c Client whose tunnel to delete.
1908 * @param t Tunnel which should be deleted.
1911 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1913 struct GNUNET_HashCode hash;
1917 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1918 GNUNET_assert (GNUNET_YES ==
1919 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1925 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1927 GNUNET_assert (GNUNET_YES ==
1928 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1932 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1940 * Send the message to all clients that have subscribed to its type
1942 * @param msg Pointer to the message itself
1943 * @param payload Pointer to the payload of the message.
1944 * @param t The tunnel to whose clients this message goes.
1946 * @return number of clients this message was sent to
1949 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1950 const struct GNUNET_MessageHeader *payload,
1951 struct MeshTunnel *t)
1953 struct MeshClient *c;
1954 MESH_TunnelNumber *tid;
1957 char cbuf[htons (msg->size)];
1959 type = ntohs (payload->type);
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1962 GNUNET_MESH_DEBUG_M2S (type));
1964 memcpy (cbuf, msg, sizeof (cbuf));
1965 switch (htons (msg->type))
1967 struct GNUNET_MESH_Unicast *uc;
1968 struct GNUNET_MESH_Multicast *mc;
1969 struct GNUNET_MESH_ToOrigin *to;
1971 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1972 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1975 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1976 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1979 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1980 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1988 for (count = 0, c = clients; c != NULL; c = c->next)
1990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1991 if (client_is_subscribed (type, c))
1993 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1997 *tid = htonl (t->local_tid);
2001 if (GNUNET_NO == client_knows_tunnel (c, t))
2003 /* This client doesn't know the tunnel */
2004 struct GNUNET_MESH_TunnelNotification tmsg;
2005 struct GNUNET_HashCode hash;
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2008 tmsg.header.size = htons (sizeof (tmsg));
2009 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2010 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2011 tmsg.tunnel_id = htonl (t->local_tid_dest);
2012 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2013 &tmsg.header, GNUNET_NO);
2014 tunnel_add_client (t, c);
2015 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2017 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2018 c->incoming_tunnels, &hash, t,
2019 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2021 *tid = htonl (t->local_tid_dest);
2024 /* Check if the client wants to get traffic from the tunnel */
2025 if (GNUNET_NO == client_wants_tunnel(c, t))
2028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2029 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2030 (struct GNUNET_MessageHeader
2031 *) cbuf, GNUNET_NO);
2040 * Notify the client that owns the tunnel that a peer has connected to it
2041 * (the requested path to it has been confirmed).
2043 * @param t Tunnel whose owner to notify
2044 * @param id Short id of the peer that has connected
2047 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2049 struct GNUNET_MESH_PeerControl pc;
2051 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2052 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2053 pc.tunnel_id = htonl (t->local_tid);
2054 GNUNET_PEER_resolve (id, &pc.peer);
2055 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2061 * Notify all clients (not depending on registration status) that the incoming
2062 * tunnel is no longer valid.
2064 * @param t Tunnel that was destroyed.
2067 send_clients_tunnel_destroy (struct MeshTunnel *t)
2069 struct GNUNET_MESH_TunnelMessage msg;
2071 msg.header.size = htons (sizeof (msg));
2072 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2073 msg.tunnel_id = htonl (t->local_tid_dest);
2074 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2079 * Notify clients of tunnel disconnections, if needed.
2080 * In case the origin disconnects, the destination clients get a tunnel destroy
2081 * notification. If the last destination disconnects (only one remaining client
2082 * in tunnel), the origin gets a (local ID) peer disconnected.
2083 * Note that the function must be called BEFORE removing the client from
2086 * @param t Tunnel that was destroyed.
2087 * @param c Client that disconnected.
2090 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2096 struct GNUNET_MESH_TunnelMessage msg;
2098 msg.header.size = htons (sizeof (msg));
2099 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2100 msg.tunnel_id = htonl (t->local_tid_dest);
2101 for (i = 0; i < t->nclients; i++)
2102 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2103 &msg.header, GNUNET_NO);
2105 // FIXME when to disconnect an incoming tunnel?
2106 else if (1 == t->nclients && NULL != t->owner)
2108 struct GNUNET_MESH_PeerControl msg;
2110 msg.header.size = htons (sizeof (msg));
2111 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2112 msg.tunnel_id = htonl (t->local_tid);
2113 msg.peer = my_full_id;
2114 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2115 &msg.header, GNUNET_NO);
2121 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2122 * and insert it in the appropiate structures if the peer is not known yet.
2124 * @param peer Full identity of the peer.
2126 * @return Existing or newly created peer info.
2128 static struct MeshPeerInfo *
2129 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2131 struct MeshPeerInfo *peer_info;
2133 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2134 if (NULL == peer_info)
2137 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2138 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2139 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2140 peer_info->id = GNUNET_PEER_intern (peer);
2148 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2149 * and insert it in the appropiate structures if the peer is not known yet.
2151 * @param peer Short identity of the peer.
2153 * @return Existing or newly created peer info.
2155 static struct MeshPeerInfo *
2156 peer_info_get_short (const GNUNET_PEER_Id peer)
2158 struct GNUNET_PeerIdentity id;
2160 GNUNET_PEER_resolve (peer, &id);
2161 return peer_info_get (&id);
2166 * Iterator to remove the tunnel from the list of tunnels a peer participates
2169 * @param cls Closure (tunnel info)
2170 * @param key GNUNET_PeerIdentity of the peer (unused)
2171 * @param value PeerInfo of the peer
2173 * @return always GNUNET_YES, to keep iterating
2176 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2178 struct MeshTunnel *t = cls;
2179 struct MeshPeerInfo *peer = value;
2182 for (i = 0; i < peer->ntunnels; i++)
2185 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2188 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2189 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2198 * Core callback to write a pre-constructed data packet to core buffer
2200 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2201 * @param size Number of bytes available in buf.
2202 * @param buf Where the to write the message.
2204 * @return number of bytes written to buf
2207 send_core_data_raw (void *cls, size_t size, void *buf)
2209 struct MeshTransmissionDescriptor *info = cls;
2210 struct GNUNET_MessageHeader *msg;
2213 GNUNET_assert (NULL != info);
2214 GNUNET_assert (NULL != info->mesh_data);
2215 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2216 total_size = ntohs (msg->size);
2218 if (total_size > size)
2223 memcpy (buf, msg, total_size);
2224 data_descriptor_decrement_rc (info->mesh_data);
2231 * Sends an already built non-multicast message to a peer,
2232 * properly registrating all used resources.
2234 * @param message Message to send. Function makes a copy of it.
2235 * @param peer Short ID of the neighbor whom to send the message.
2236 * @param t Tunnel on which this message is transmitted.
2239 send_message (const struct GNUNET_MessageHeader *message,
2240 const struct GNUNET_PeerIdentity *peer,
2241 struct MeshTunnel *t)
2243 struct MeshTransmissionDescriptor *info;
2244 struct MeshPeerInfo *neighbor;
2245 struct MeshPeerPath *p;
2249 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2251 size = ntohs (message->size);
2252 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2253 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2254 info->mesh_data->data = GNUNET_malloc (size);
2255 memcpy (info->mesh_data->data, message, size);
2256 type = ntohs(message->type);
2257 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2259 struct GNUNET_MESH_Unicast *m;
2261 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2262 m->ttl = htonl (ntohl (m->ttl) - 1);
2264 info->mesh_data->data_len = size;
2265 info->mesh_data->reference_counter = 1;
2266 info->mesh_data->total_out = 1;
2267 neighbor = peer_info_get (peer);
2268 for (p = neighbor->path_head; NULL != p; p = p->next)
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279 " %s IS NOT DIRECTLY CONNECTED\n",
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2284 for (p = neighbor->path_head; NULL != p; p = p->next)
2286 struct GNUNET_PeerIdentity debug_id;
2289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2290 " path with %u hops through:\n",
2292 for (i = 0; i < p->length; i++)
2294 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2297 i, GNUNET_i2s(&debug_id));
2301 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2302 GNUNET_free (info->mesh_data->data);
2303 GNUNET_free (info->mesh_data);
2307 info->peer = neighbor;
2308 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2311 * TODO: in this case we only need the service to retransmit
2312 * the message down the path. If we pass the real type to queue_add,
2313 * queue_send will try to build the message from scratch. This can
2314 * probably be done by some other way instead of deleteing the type
2328 * Sends a CREATE PATH message for a path to a peer, properly registrating
2329 * all used resources.
2331 * @param peer PeerInfo of the final peer for whom this path is being created.
2332 * @param p Path itself.
2333 * @param t Tunnel for which the path is created.
2336 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2337 struct MeshTunnel *t)
2339 struct GNUNET_PeerIdentity id;
2340 struct MeshPathInfo *path_info;
2341 struct MeshPeerInfo *neighbor;
2347 p = tree_get_path_to_peer (t->tree, peer->id);
2354 for (i = 0; i < p->length; i++)
2356 if (p->peers[i] == myid)
2359 if (i >= p->length - 1)
2365 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2367 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2368 path_info->path = p;
2370 neighbor = peer_info_get (&id);
2371 path_info->peer = neighbor;
2372 queue_add (path_info,
2373 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2374 sizeof (struct GNUNET_MESH_ManipulatePath) +
2375 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2382 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2384 * @param t Tunnel whose path to destroy.
2385 * @param destination Short ID of the peer to whom the path to destroy.
2388 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2390 struct MeshPeerPath *p;
2393 p = tree_get_path_to_peer (t->tree, destination);
2399 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2400 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2402 struct GNUNET_MESH_ManipulatePath *msg;
2403 struct GNUNET_PeerIdentity *pi;
2407 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2408 msg->header.size = htons (size);
2409 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2410 msg->tid = htonl (t->id.tid);
2411 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2412 for (i = 0; i < p->length; i++)
2414 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2416 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2423 * Try to establish a new connection to this peer.
2424 * Use the best path for the given tunnel.
2425 * If the peer doesn't have any path to it yet, try to get one.
2426 * If the peer already has some path, send a CREATE PATH towards it.
2428 * @param peer PeerInfo of the peer.
2429 * @param t Tunnel for which to create the path, if possible.
2432 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2434 struct MeshPeerPath *p;
2435 struct MeshPathInfo *path_info;
2437 if (NULL != peer->path_head)
2439 p = tree_get_path_to_peer (t->tree, peer->id);
2446 // FIXME always send create path to self
2449 send_create_path (peer, p, t);
2453 struct GNUNET_HashCode hash;
2456 send_client_peer_connected (t, myid);
2457 t->local_tid_dest = next_local_tid++;
2458 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2461 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2462 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2469 else if (NULL == peer->dhtget)
2471 struct GNUNET_PeerIdentity id;
2473 GNUNET_PEER_resolve (peer->id, &id);
2474 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2475 path_info->peer = peer;
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2478 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2479 peer->dhtgetcls = path_info;
2480 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2481 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2482 &id.hashPubKey, /* key to search */
2483 dht_replication_level, /* replication level */
2484 GNUNET_DHT_RO_RECORD_ROUTE |
2485 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2486 NULL, /* xquery */ // FIXME BLOOMFILTER
2487 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2488 &dht_get_id_handler, path_info);
2490 /* Otherwise, there is no path but the DHT get is already started. */
2495 * Task to delay the connection of a peer
2497 * @param cls Closure (path info with tunnel and peer to connect).
2498 * Will be free'd on exection.
2499 * @param tc TaskContext
2502 peer_info_connect_task (void *cls,
2503 const struct GNUNET_SCHEDULER_TaskContext *tc)
2505 struct MeshPathInfo *path_info = cls;
2507 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2512 peer_info_connect (path_info->peer, path_info->t);
2518 * Destroy the peer_info and free any allocated resources linked to it
2520 * @param pi The peer_info to destroy.
2522 * @return GNUNET_OK on success
2525 peer_info_destroy (struct MeshPeerInfo *pi)
2527 struct GNUNET_PeerIdentity id;
2528 struct MeshPeerPath *p;
2529 struct MeshPeerPath *nextp;
2531 GNUNET_PEER_resolve (pi->id, &id);
2532 GNUNET_PEER_change_rc (pi->id, -1);
2535 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2538 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2539 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2541 if (NULL != pi->dhtget)
2543 GNUNET_DHT_get_stop (pi->dhtget);
2544 GNUNET_free (pi->dhtgetcls);
2550 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2560 * Remove all paths that rely on a direct connection between p1 and p2
2561 * from the peer itself and notify all tunnels about it.
2563 * @param peer PeerInfo of affected peer.
2564 * @param p1 GNUNET_PEER_Id of one peer.
2565 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2568 * TODO: optimize (see below)
2571 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2574 struct MeshPeerPath *p;
2575 struct MeshPeerPath *aux;
2576 struct MeshPeerInfo *peer_d;
2578 unsigned int destroyed;
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2585 p = peer->path_head;
2589 for (i = 0; i < (p->length - 1); i++)
2591 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2592 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2594 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2605 for (i = 0; i < peer->ntunnels; i++)
2607 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2611 * Problem: one or more peers have been deleted from the tunnel tree.
2612 * We don't know who they are to try to add them again.
2613 * We need to try to find a new path for each of the disconnected peers.
2614 * Some of them might already have a path to reach them that does not
2615 * involve p1 and p2. Adding all anew might render in a better tree than
2616 * the trivial immediate fix.
2618 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2619 * its children will be reachable trough him.
2621 peer_d = peer_info_get_short (d);
2624 for (p = peer_d->path_head; NULL != p; p = p->next)
2626 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2634 /* No callback, as peer will be already disconnected and a connection
2635 * scheduled by tunnel_notify_connection_broken.
2637 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2641 peer_info_connect (peer_d, peer->tunnels[i]);
2644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2649 * Add the path to the peer and update the path used to reach it in case this
2652 * @param peer_info Destination peer to add the path to.
2653 * @param path New path to add. Last peer must be the peer in arg 1.
2654 * Path will be either used of freed if already known.
2655 * @param trusted Do we trust that this path is real?
2658 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2661 struct MeshPeerPath *aux;
2665 if ((NULL == peer_info) || (NULL == path))
2668 path_destroy (path);
2671 if (path->peers[path->length - 1] != peer_info->id)
2674 path_destroy (path);
2677 if (path->length <= 2 && GNUNET_NO == trusted)
2679 /* Only allow CORE to tell us about direct paths */
2680 path_destroy (path);
2683 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2684 for (l = 1; l < path->length; l++)
2686 if (path->peers[l] == myid)
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2689 for (l2 = 0; l2 < path->length - l; l2++)
2691 path->peers[l2] = path->peers[l + l2];
2696 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2701 struct GNUNET_PeerIdentity id;
2703 GNUNET_PEER_resolve (peer_info->id, &id);
2704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2705 path->length, GNUNET_i2s (&id));
2708 l = path_get_length (path);
2715 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2716 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2718 l2 = path_get_length (aux);
2721 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2722 peer_info->path_tail, aux, path);
2727 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2729 path_destroy (path);
2734 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2741 * Add the path to the origin peer and update the path used to reach it in case
2742 * this is the shortest.
2743 * The path is given in peer_info -> destination, therefore we turn the path
2744 * upside down first.
2746 * @param peer_info Peer to add the path to, being the origin of the path.
2747 * @param path New path to add after being inversed.
2748 * @param trusted Do we trust that this path is real?
2751 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2752 struct MeshPeerPath *path, int trusted)
2755 peer_info_add_path (peer_info, path, trusted);
2760 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2761 * to obtain a local peer -> destination path and interning the peer ids.
2763 * @return Newly allocated and created path
2765 static struct MeshPeerPath *
2766 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2767 unsigned int get_path_length,
2768 const struct GNUNET_PeerIdentity *put_path,
2769 unsigned int put_path_length)
2771 struct MeshPeerPath *p;
2777 GNUNET_PEER_change_rc (myid, 1);
2778 i = get_path_length;
2779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2780 for (i--; i >= 0; i--)
2782 id = GNUNET_PEER_intern (&get_path[i]);
2783 if (p->length > 0 && id == p->peers[p->length - 1])
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2786 GNUNET_PEER_change_rc (id, -1);
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2791 GNUNET_i2s (&get_path[i]));
2793 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2794 p->peers[p->length - 1] = id;
2797 i = put_path_length;
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2799 for (i--; i >= 0; i--)
2801 id = GNUNET_PEER_intern (&put_path[i]);
2804 /* PUT path went through us, so discard the path up until now and start
2805 * from here to get a much shorter (and loop-free) path.
2810 if (p->length > 0 && id == p->peers[p->length - 1])
2812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2813 GNUNET_PEER_change_rc (id, -1);
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2818 GNUNET_i2s (&put_path[i]));
2820 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2821 p->peers[p->length - 1] = id;
2825 if (get_path_length > 0)
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2827 GNUNET_i2s (&get_path[0]));
2828 if (put_path_length > 0)
2829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2830 GNUNET_i2s (&put_path[0]));
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2833 for (i = 0; i < p->length; i++)
2835 struct GNUNET_PeerIdentity peer_id;
2837 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2838 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2839 GNUNET_i2s (&peer_id));
2847 * Adds a path to the peer_infos of all the peers in the path
2849 * @param p Path to process.
2850 * @param confirmed Whether we know if the path works or not.
2853 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2857 /* TODO: invert and add */
2858 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2859 for (i++; i < p->length; i++)
2861 struct MeshPeerInfo *aux;
2862 struct MeshPeerPath *copy;
2864 aux = peer_info_get_short (p->peers[i]);
2865 copy = path_duplicate (p);
2866 copy->length = i + 1;
2867 peer_info_add_path (aux, copy, GNUNET_NO);
2873 * Send keepalive packets for a peer
2875 * @param cls Closure (tunnel for which to send the keepalive).
2876 * @param tc Notification context.
2878 * TODO: implement explicit multicast keepalive?
2881 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2885 * Search for a tunnel among the incoming tunnels
2887 * @param tid the local id of the tunnel
2889 * @return tunnel handler, NULL if doesn't exist
2891 static struct MeshTunnel *
2892 tunnel_get_incoming (MESH_TunnelNumber tid)
2894 struct GNUNET_HashCode hash;
2896 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2897 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2898 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2903 * Search for a tunnel among the tunnels for a client
2905 * @param c the client whose tunnels to search in
2906 * @param tid the local id of the tunnel
2908 * @return tunnel handler, NULL if doesn't exist
2910 static struct MeshTunnel *
2911 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2913 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2915 return tunnel_get_incoming (tid);
2919 struct GNUNET_HashCode hash;
2921 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2922 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2928 * Search for a tunnel by global ID using PEER_ID
2930 * @param pi owner of the tunnel
2931 * @param tid global tunnel number
2933 * @return tunnel handler, NULL if doesn't exist
2935 static struct MeshTunnel *
2936 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2938 struct MESH_TunnelID id;
2939 struct GNUNET_HashCode hash;
2944 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2945 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2950 * Search for a tunnel by global ID using full PeerIdentities
2952 * @param oid owner of the tunnel
2953 * @param tid global tunnel number
2955 * @return tunnel handler, NULL if doesn't exist
2957 static struct MeshTunnel *
2958 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2960 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2965 * Delete an active client from the tunnel.
2971 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2975 for (i = 0; i < t->nclients; i++)
2977 if (t->clients[i] == c)
2979 t->clients[i] = t->clients[t->nclients - 1];
2980 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2981 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2983 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2991 * Delete an ignored client from the tunnel.
2997 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3001 for (i = 0; i < t->nignore; i++)
3003 if (t->ignore[i] == c)
3005 t->ignore[i] = t->ignore[t->nignore - 1];
3006 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3014 * Delete a client from the tunnel. It should be only done on
3015 * client disconnection, otherwise use client_ignore_tunnel.
3021 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3023 tunnel_delete_ignored_client (t, c);
3024 tunnel_delete_active_client (t, c);
3029 * Iterator to free MeshTunnelChildInfo of tunnel children.
3031 * @param cls Closure (tunnel info).
3032 * @param key Hash of GNUNET_PEER_Id (unused).
3033 * @param value MeshTunnelChildInfo of the child.
3035 * @return always GNUNET_YES, to keep iterating
3038 tunnel_destroy_child (void *cls,
3039 const struct GNUNET_HashCode * key,
3042 struct MeshTunnelChildInfo *cinfo = value;
3043 struct MeshTunnel *t = cls;
3047 for (c = 0; c < cinfo->send_buffer_n; c++)
3049 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3050 if (NULL != cinfo->send_buffer[i])
3051 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3054 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3056 GNUNET_free_non_null (cinfo->send_buffer);
3057 GNUNET_free (cinfo);
3063 * Callback used to notify a client owner of a tunnel that a peer has
3064 * disconnected, most likely because of a path change.
3066 * @param cls Closure (tunnel this notification is about).
3067 * @param peer_id Short ID of disconnected peer.
3070 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3072 struct MeshTunnel *t = cls;
3073 struct MeshPeerInfo *peer;
3074 struct MeshPathInfo *path_info;
3076 if (NULL != t->owner && NULL != nc)
3078 struct GNUNET_MESH_PeerControl msg;
3080 msg.header.size = htons (sizeof (msg));
3081 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3082 msg.tunnel_id = htonl (t->local_tid);
3083 GNUNET_PEER_resolve (peer_id, &msg.peer);
3084 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3085 &msg.header, GNUNET_NO);
3087 peer = peer_info_get_short (peer_id);
3088 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3089 path_info->peer = peer;
3091 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3096 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3097 * needed rescources.
3098 * If peer already exists, reevaluate shortest path and change if different.
3100 * @param t Tunnel we want to add a new peer to
3101 * @param peer PeerInfo of the peer being added
3105 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3107 struct GNUNET_PeerIdentity id;
3108 struct MeshPeerPath *best_p;
3109 struct MeshPeerPath *p;
3110 unsigned int best_cost;
3113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3114 GNUNET_PEER_resolve (peer->id, &id);
3116 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3119 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3120 GNUNET_assert (GNUNET_OK ==
3121 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3123 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3126 if (NULL != (p = peer->path_head))
3129 best_cost = tree_get_path_cost (t->tree, p);
3132 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3139 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3140 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3141 t->path_refresh_task =
3142 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3146 /* Start a DHT get */
3147 peer_info_connect (peer, t);
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3153 * Add a path to a tunnel which we don't own, just to remember the next hop.
3154 * If destination node was already in the tunnel, the first hop information
3155 * will be replaced with the new path.
3157 * @param t Tunnel we want to add a new peer to
3158 * @param p Path to add
3159 * @param own_pos Position of local node in path.
3163 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3164 unsigned int own_pos)
3166 struct GNUNET_PeerIdentity id;
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3169 GNUNET_assert (0 != own_pos);
3170 tree_add_path (t->tree, p, NULL, NULL);
3171 if (own_pos < p->length - 1)
3173 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3174 tree_update_first_hops (t->tree, myid, &id);
3176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3180 * Add a client to a tunnel, initializing all needed data structures.
3182 * @param t Tunnel to which add the client.
3183 * @param c Client which to add to the tunnel.
3186 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3188 struct MeshTunnelClientInfo clinfo;
3190 GNUNET_array_append (t->clients, t->nclients, c);
3191 clinfo.fwd_ack = t->fwd_pid + 1;
3192 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3193 clinfo.fwd_pid = t->fwd_pid;
3194 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3196 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3201 * Notifies a tunnel that a connection has broken that affects at least
3202 * some of its peers. Sends a notification towards the root of the tree.
3203 * In case the peer is the owner of the tree, notifies the client that owns
3204 * the tunnel and tries to reconnect.
3206 * @param t Tunnel affected.
3207 * @param p1 Peer that got disconnected from p2.
3208 * @param p2 Peer that got disconnected from p1.
3210 * @return Short ID of the peer disconnected (either p1 or p2).
3211 * 0 if the tunnel remained unaffected.
3213 static GNUNET_PEER_Id
3214 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3220 tree_notify_connection_broken (t->tree, p1, p2,
3221 &tunnel_notify_client_peer_disconnected,
3223 if (myid != p1 && myid != p2)
3229 if (tree_get_predecessor (t->tree) != 0)
3231 /* We are the peer still connected, notify owner of the disconnection. */
3232 struct GNUNET_MESH_PathBroken msg;
3233 struct GNUNET_PeerIdentity neighbor;
3235 msg.header.size = htons (sizeof (msg));
3236 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3237 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3238 msg.tid = htonl (t->id.tid);
3239 msg.peer1 = my_full_id;
3240 GNUNET_PEER_resolve (pid, &msg.peer2);
3241 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3242 send_message (&msg.header, &neighbor, t);
3250 * Send a multicast packet to a neighbor.
3252 * @param cls Closure (Info about the multicast packet)
3253 * @param neighbor_id Short ID of the neighbor to send the packet to.
3256 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3258 struct MeshData *mdata = cls;
3259 struct MeshTransmissionDescriptor *info;
3260 struct GNUNET_PeerIdentity neighbor;
3262 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3264 info->mesh_data = mdata;
3265 (mdata->reference_counter) ++;
3266 info->destination = neighbor_id;
3267 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3269 GNUNET_i2s (&neighbor));
3270 info->peer = peer_info_get (&neighbor);
3271 GNUNET_assert (NULL != info->peer);
3273 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3274 info->mesh_data->data_len,
3281 * Queue a message in a tunnel in multicast, sending a copy to each child node
3282 * down the local one in the tunnel tree.
3284 * @param t Tunnel in which to send the data.
3285 * @param msg Message to be sent.
3286 * @param internal DEPRECATED Has the service generated this message?
3288 * FIXME remove internal if no use comes up
3291 tunnel_send_multicast (struct MeshTunnel *t,
3292 const struct GNUNET_MessageHeader *msg,
3295 struct MeshData *mdata;
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 " sending a multicast packet...\n");
3300 mdata = GNUNET_malloc (sizeof (struct MeshData));
3301 mdata->data_len = ntohs (msg->size);
3303 mdata->data = GNUNET_malloc (mdata->data_len);
3304 memcpy (mdata->data, msg, mdata->data_len);
3305 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3307 struct GNUNET_MESH_Multicast *mcast;
3309 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3310 if (t->fwd_queue_n >= t->fwd_queue_max)
3313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3315 " message from %s!\n",
3316 GNUNET_i2s(&mcast->oid));
3317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3318 " message at %s!\n",
3319 GNUNET_i2s(&my_full_id));
3320 GNUNET_free (mdata->data);
3321 GNUNET_free (mdata);
3325 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3327 ntohl (mcast->ttl));
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3334 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3335 if (mdata->reference_counter == 0)
3337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3338 " no one to send data to\n");
3339 GNUNET_free (mdata->data);
3340 GNUNET_free (mdata);
3345 mdata->total_out = mdata->reference_counter;
3347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3348 " sending a multicast packet done\n");
3354 * Increase the SKIP value of all peers that
3355 * have not received a unicast message.
3357 * @param cls Closure (ID of the peer that HAS received the message).
3358 * @param key ID of the neighbor.
3359 * @param value Information about the neighbor.
3361 * @return GNUNET_YES to keep iterating.
3364 tunnel_add_skip (void *cls,
3365 const struct GNUNET_HashCode * key,
3368 struct GNUNET_PeerIdentity *neighbor = cls;
3369 struct MeshTunnelChildInfo *cinfo = value;
3371 /* TODO compare only pointers? key == neighbor? */
3372 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3382 * @brief Get neighbor's Flow Control information.
3384 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3385 * descendant of the local node in a certain tunnel.
3386 * If the info is not yet there (recently created path), creates the data struct
3387 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3390 * @param t Tunnel related.
3391 * @param peer Neighbor whose Flow Control info is needed.
3393 * @return Neighbor's Flow Control info.
3395 static struct MeshTunnelChildInfo *
3396 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3397 const struct GNUNET_PeerIdentity *peer)
3399 struct MeshTunnelChildInfo *cinfo;
3400 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3406 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3407 cinfo->id = GNUNET_PEER_intern (peer);
3408 cinfo->skip = t->fwd_pid;
3410 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3411 cinfo->fwd_ack = t->fwd_pid + delta;
3412 cinfo->bck_ack = delta;
3414 cinfo->send_buffer =
3415 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3417 GNUNET_assert (GNUNET_OK ==
3418 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3421 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3428 * Get the Flow Control info of a client.
3430 * @param t Tunnel on which to look.
3431 * @param c Client whose ACK to get.
3433 * @return ACK value.
3435 static struct MeshTunnelClientInfo *
3436 tunnel_get_client_fc (struct MeshTunnel *t,
3437 struct MeshClient *c)
3441 for (i = 0; i < t->nclients; i++)
3443 if (t->clients[i] != c)
3445 return &t->clients_fc[i];
3448 return NULL; // avoid compiler / coverity complaints
3453 * Iterator to get the appropiate ACK value from all children nodes.
3455 * @param cls Closue (tunnel).
3456 * @param id Id of the child node.
3459 tunnel_get_child_fwd_ack (void *cls,
3462 struct GNUNET_PeerIdentity peer_id;
3463 struct MeshTunnelChildInfo *cinfo;
3464 struct MeshTunnelChildIteratorContext *ctx = cls;
3465 struct MeshTunnel *t = ctx->t;
3468 GNUNET_PEER_resolve (id, &peer_id);
3469 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3470 ack = cinfo->fwd_ack;
3473 if (GNUNET_NO == ctx->init)
3475 ctx->max_child_ack = ack;
3476 ctx->init = GNUNET_YES;
3479 if (GNUNET_YES == t->speed_min)
3481 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3485 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3492 * Get the maximum PID allowed to transmit to any
3493 * tunnel child of the local peer, depending on the tunnel
3494 * buffering/speed settings.
3498 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3501 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3503 struct MeshTunnelChildIteratorContext ctx;
3505 ctx.max_child_ack = 0;
3507 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3509 if (0 == ctx.nchildren)
3511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3512 " tunnel has no children, no FWD ACK\n");
3516 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3517 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3519 return (int64_t) ctx.max_child_ack;
3524 * Set the FWD ACK value of a client in a particular tunnel.
3526 * @param t Tunnel affected.
3527 * @param c Client whose ACK to set.
3528 * @param ack ACK value.
3531 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3532 struct MeshClient *c,
3537 for (i = 0; i < t->nclients; i++)
3539 if (t->clients[i] != c)
3541 t->clients_fc[i].fwd_ack = ack;
3549 * Get the highest ACK value of all clients in a particular tunnel,
3550 * according to the buffering/speed settings.
3552 * @param t Tunnel on which to look.
3554 * @return Corresponding ACK value (max uint32_t).
3555 * If no clients are suscribed, -1LL.
3558 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3563 if (0 == t->nclients)
3565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3566 " tunnel has no clients, no FWD ACK\n");
3570 for (ack = -1LL, i = 0; i < t->nclients; i++)
3573 (GNUNET_YES == t->speed_min &&
3574 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3575 (GNUNET_NO == t->speed_min &&
3576 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3578 ack = t->clients_fc[i].fwd_ack;
3582 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3583 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3585 return (uint32_t) ack;
3590 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3591 * mode and the status of all children nodes.
3595 * @return Maximum PID allowed.
3598 tunnel_get_fwd_ack (struct MeshTunnel *t)
3602 uint32_t buffer_free;
3606 count = t->fwd_pid - t->skip;
3607 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3608 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3609 child_ack = tunnel_get_children_fwd_ack (t);
3610 client_ack = tunnel_get_clients_fwd_ack (t);
3611 if (-1LL == child_ack)
3613 // Node has no children, child_ack AND core buffer are irrelevant.
3614 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3615 return (uint32_t) client_ack;
3617 if (-1LL == client_ack)
3621 if (GNUNET_YES == t->speed_min)
3623 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3624 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3628 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3629 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3631 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3632 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3634 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3635 count, buffer_free, child_ack, client_ack, ack);
3641 * Build a local ACK message and send it to a local client.
3643 * @param t Tunnel on which to send the ACK.
3644 * @param c Client to whom send the ACK.
3645 * @param ack Value of the ACK.
3648 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3650 struct GNUNET_MESH_LocalAck msg;
3652 msg.header.size = htons (sizeof (msg));
3653 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3654 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3655 msg.max_pid = htonl (ack);
3656 GNUNET_SERVER_notification_context_unicast(nc,
3663 * Build an ACK message and queue it to send to the given peer.
3665 * @param t Tunnel on which to send the ACK.
3666 * @param peer Peer to whom send the ACK.
3667 * @param ack Value of the ACK.
3670 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3672 struct GNUNET_MESH_ACK msg;
3674 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3675 msg.header.size = htons (sizeof (msg));
3676 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3677 msg.pid = htonl (ack);
3678 msg.tid = htonl (t->id.tid);
3680 send_message (&msg.header, peer, t);
3685 * Notify a the owner of a tunnel about how many more
3686 * payload packages will we accept on a given tunnel.
3688 * @param t Tunnel on which to send the ACK.
3691 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3696 "Sending client FWD ACK on tunnel %X\n",
3699 ack = tunnel_get_fwd_ack (t);
3701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3702 if (t->last_fwd_ack == ack)
3705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3706 t->last_fwd_ack = ack;
3707 send_local_ack (t, t->owner, ack);
3712 * Send an ACK informing the predecessor about the available buffer space.
3713 * In case there is no predecessor, inform the owning client.
3714 * If buffering is off, send only on behalf of children or self if endpoint.
3715 * If buffering is on, send when sent to children and buffer space is free.
3716 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3717 * the ACK itself goes "back" (towards root).
3719 * @param t Tunnel on which to send the ACK.
3720 * @param type Type of message that triggered the ACK transmission.
3723 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3725 struct GNUNET_PeerIdentity id;
3728 if (NULL != t->owner)
3730 tunnel_send_client_fwd_ack (t);
3733 /* Is it after unicast / multicast retransmission? */
3736 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3737 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3739 "ACK due to FWD DATA retransmission\n");
3740 if (GNUNET_YES == t->nobuffer)
3742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3746 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3747 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3753 /* Check if we need no retransmit the ACK */
3754 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3755 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3759 " t->qmax: %u, t->qn: %u\n",
3760 t->fwd_queue_max, t->fwd_queue_n);
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3762 " t->pid: %u, t->ack: %u\n",
3763 t->fwd_pid, t->last_fwd_ack);
3767 /* Ok, ACK might be necessary, what PID to ACK? */
3768 ack = tunnel_get_fwd_ack (t);
3770 /* If speed_min and not all children have ack'd, dont send yet */
3771 if (ack == t->last_fwd_ack)
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3777 t->last_fwd_ack = ack;
3778 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3779 send_ack (t, &id, ack);
3785 * Iterator to send a child node a BCK ACK to allow him to send more
3788 * @param cls Closure (tunnel).
3789 * @param id Id of the child node.
3792 tunnel_send_child_bck_ack (void *cls,
3795 struct MeshTunnel *t = cls;
3796 struct MeshTunnelChildInfo *cinfo;
3797 struct GNUNET_PeerIdentity peer;
3799 GNUNET_PEER_resolve (id, &peer);
3800 cinfo = tunnel_get_neighbor_fc (t, &peer);
3802 if (cinfo->bck_ack != cinfo->pid &&
3803 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3807 send_ack (t, &peer, cinfo->bck_ack);
3812 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3814 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3816 * @param t Tunnel on which to send the BCK ACKs.
3819 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3822 unsigned int tunnel_delta;
3824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3826 tunnel_delta = t->bck_ack - t->bck_pid;
3827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3829 /* Find client whom to allow to send to origin (with lowest buffer space) */
3830 for (i = 0; i < t->nclients; i++)
3832 struct MeshTunnelClientInfo *clinfo;
3835 clinfo = &t->clients_fc[i];
3836 delta = clinfo->bck_ack - clinfo->bck_pid;
3837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3838 t->clients[i]->id, delta);
3840 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3841 (GNUNET_YES == t->nobuffer && 0 == delta))
3845 ack = clinfo->bck_pid;
3846 ack += t->nobuffer ? 1 : tunnel_delta;
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 " sending ack to client %u: %u\n",
3849 t->clients[i]->id, ack);
3850 send_local_ack (t, t->clients[i], ack);
3851 clinfo->bck_ack = ack;
3858 * Send an ACK informing the children nodes and destination clients about
3859 * the available buffer space.
3860 * If buffering is off, send only on behalf of root (can be self).
3861 * If buffering is on, send when sent to predecessor and buffer space is free.
3862 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3863 * the ACK itself goes "forward" (towards children/clients).
3865 * @param t Tunnel on which to send the ACK.
3866 * @param type Type of message that triggered the ACK transmission.
3869 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3872 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3873 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3874 /* Is it after data to_origin retransmission? */
3877 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3878 if (GNUNET_YES == t->nobuffer)
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3881 " Not sending ACK, nobuffer\n");
3885 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3886 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3892 tunnel_send_clients_bck_ack (t);
3893 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3898 * @brief Re-initiate traffic to this peer if necessary.
3900 * Check if there is traffic queued towards this peer
3901 * and the core transmit handle is NULL (traffic was stalled).
3902 * If so, call core tmt rdy.
3904 * @param cls Closure (unused)
3905 * @param peer_id Short ID of peer to which initiate traffic.
3908 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3910 struct MeshPeerInfo *peer;
3911 struct GNUNET_PeerIdentity id;
3912 struct MeshPeerQueue *q;
3915 peer = peer_info_get_short(peer_id);
3916 if (NULL != peer->core_transmit)
3919 q = queue_get_next(peer);
3922 /* Might br multicast traffic already sent to this particular peer but
3923 * not to other children in this tunnel.
3924 * This way t->queue_n would be > 0 but the queue of this particular peer
3930 GNUNET_PEER_resolve (peer->id, &id);
3931 peer->core_transmit =
3932 GNUNET_CORE_notify_transmit_ready(core_handle,
3935 GNUNET_TIME_UNIT_FOREVER_REL,
3945 * @brief Allow transmission of FWD traffic on this tunnel
3947 * Check if there is traffic queued towards any children
3948 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3950 * @param t Tunnel on which to unlock FWD traffic.
3953 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3955 if (0 == t->fwd_queue_n)
3958 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3963 * @brief Allow transmission of BCK traffic on this tunnel
3965 * Check if there is traffic queued towards the root of the tree
3966 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3968 * @param t Tunnel on which to unlock BCK traffic.
3971 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3973 if (0 == t->bck_queue_n)
3976 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3981 * Send a message to all peers in this tunnel that the tunnel is no longer
3984 * @param t The tunnel whose peers to notify.
3987 tunnel_send_destroy (struct MeshTunnel *t)
3989 struct GNUNET_MESH_TunnelDestroy msg;
3991 msg.header.size = htons (sizeof (msg));
3992 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3993 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3994 msg.tid = htonl (t->id.tid);
3995 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4000 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4002 * @param cls Closure (Tunnel which to cancel).
4003 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4006 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4008 struct MeshTunnel *t = cls;
4009 struct MeshPeerInfo *peer_info;
4010 struct MeshPeerQueue *pq;
4011 struct MeshPeerQueue *next;
4013 peer_info = peer_info_get_short (neighbor_id);
4014 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4017 if (pq->tunnel == t)
4019 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4020 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4021 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4023 // Should have been removed on destroy children
4026 queue_destroy (pq, GNUNET_YES);
4029 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4031 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4032 peer_info->core_transmit = NULL;
4037 * Destroy the tunnel and free any allocated resources linked to it.
4039 * @param t the tunnel to destroy
4041 * @return GNUNET_OK on success
4044 tunnel_destroy (struct MeshTunnel *t)
4046 struct MeshClient *c;
4047 struct GNUNET_HashCode hash;
4058 struct GNUNET_PeerIdentity id;
4060 GNUNET_PEER_resolve (t->id.oid, &id);
4061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4062 GNUNET_i2s (&id), t->id.tid);
4064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4068 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4069 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4074 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4077 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4081 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4082 for (i = 0; i < t->nclients; i++)
4086 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4091 for (i = 0; i < t->nignore; i++)
4095 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4100 if (t->nclients > 0)
4103 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4107 GNUNET_free (t->clients);
4109 if (NULL != t->peers)
4111 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4113 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4116 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4117 &tunnel_destroy_child,
4119 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4121 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4123 tree_destroy (t->tree);
4125 if (NULL != t->regex_ctx)
4126 regex_cancel_search (t->regex_ctx);
4127 if (NULL != t->dht_get_type)
4128 GNUNET_DHT_get_stop (t->dht_get_type);
4129 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4130 GNUNET_SCHEDULER_cancel (t->timeout_task);
4131 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4132 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4135 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4136 GNUNET_assert (0 <= n_tunnels);
4143 * Create a new tunnel
4145 * @param owner Who is the owner of the tunnel (short ID).
4146 * @param tid Tunnel Number of the tunnel.
4147 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4148 * @param local Tunnel Number for the tunnel, for the client point of view.
4150 * @return A new initialized tunnel. NULL on error.
4152 static struct MeshTunnel *
4153 tunnel_new (GNUNET_PEER_Id owner,
4154 MESH_TunnelNumber tid,
4155 struct MeshClient *client,
4156 MESH_TunnelNumber local)
4158 struct MeshTunnel *t;
4159 struct GNUNET_HashCode hash;
4161 if (n_tunnels >= max_tunnels && NULL == client)
4164 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4167 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4168 t->bck_queue_max = t->fwd_queue_max;
4169 t->tree = tree_new (owner);
4171 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4172 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4173 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4174 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4175 t->local_tid = local;
4176 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4178 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4180 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4182 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4183 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4190 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4197 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4199 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4200 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4204 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4214 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4215 * that are no longer needed, as well as nodes of no longer reachable peers.
4216 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4218 * @param t Tunnel from which to remove the path.
4219 * @param peer Short id of the peer which should be removed.
4222 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4224 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4230 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4231 * client when the client disconnects. If the client is not the owner, the
4232 * owner will get notified if no more clients are in the tunnel and the client
4233 * get removed from the tunnel's list.
4235 * @param cls closure (client that is disconnecting)
4236 * @param key the hash of the local tunnel id (used to access the hashmap)
4237 * @param value the value stored at the key (tunnel to destroy)
4239 * @return GNUNET_OK on success
4242 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4244 struct MeshTunnel *t = value;
4245 struct MeshClient *c = cls;
4248 send_client_tunnel_disconnect(t, c);
4251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4252 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4253 tunnel_delete_client(t, c);
4254 client_delete_tunnel(c, t);
4257 tunnel_send_destroy(t);
4258 r = tunnel_destroy (t);
4264 * Timeout function, destroys tunnel if called
4266 * @param cls Closure (tunnel to destroy).
4267 * @param tc TaskContext
4270 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4272 struct MeshTunnel *t = cls;
4274 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4276 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4281 * Resets the tunnel timeout. Starts it if no timeout was running.
4283 * @param t Tunnel whose timeout to reset.
4285 * TODO use heap to improve efficiency of scheduler.
4288 tunnel_reset_timeout (struct MeshTunnel *t)
4290 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4291 GNUNET_SCHEDULER_cancel (t->timeout_task);
4293 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4294 (refresh_path_time, 4), &tunnel_timeout, t);
4298 /******************************************************************************/
4299 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4300 /******************************************************************************/
4303 * Function to send a create path packet to a peer.
4305 * @param cls closure
4306 * @param size number of bytes available in buf
4307 * @param buf where the callee should write the message
4308 * @return number of bytes written to buf
4311 send_core_path_create (void *cls, size_t size, void *buf)
4313 struct MeshPathInfo *info = cls;
4314 struct GNUNET_MESH_ManipulatePath *msg;
4315 struct GNUNET_PeerIdentity *peer_ptr;
4316 struct MeshTunnel *t = info->t;
4317 struct MeshPeerPath *p = info->path;
4322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4324 sizeof (struct GNUNET_MESH_ManipulatePath) +
4325 p->length * sizeof (struct GNUNET_PeerIdentity);
4327 if (size < size_needed || NULL == buf)
4332 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4333 msg->header.size = htons (size_needed);
4334 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4335 msg->tid = ntohl (t->id.tid);
4338 if (GNUNET_YES == t->speed_min)
4339 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4340 if (GNUNET_YES == t->nobuffer)
4341 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4342 msg->opt = htonl(opt);
4344 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4345 for (i = 0; i < p->length; i++)
4347 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4354 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4360 * Fill the core buffer
4362 * @param cls closure (data itself)
4363 * @param size number of bytes available in buf
4364 * @param buf where the callee should write the message
4366 * @return number of bytes written to buf
4369 send_core_data_multicast (void *cls, size_t size, void *buf)
4371 struct MeshTransmissionDescriptor *info = cls;
4374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4375 GNUNET_assert (NULL != info);
4376 GNUNET_assert (NULL != info->peer);
4377 total_size = info->mesh_data->data_len;
4378 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4380 if (total_size > size)
4385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4386 memcpy (buf, info->mesh_data->data, total_size);
4389 struct GNUNET_MESH_Multicast *mc;
4390 struct GNUNET_MessageHeader *mh;
4393 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4395 mc = (struct GNUNET_MESH_Multicast *) mh;
4396 mh = (struct GNUNET_MessageHeader *) &mc[1];
4397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4398 " multicast, payload type %s\n",
4399 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4401 " multicast, payload size %u\n", ntohs (mh->size));
4405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4406 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4410 data_descriptor_decrement_rc (info->mesh_data);
4411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4419 * Creates a path ack message in buf and frees all unused resources.
4421 * @param cls closure (MeshTransmissionDescriptor)
4422 * @param size number of bytes available in buf
4423 * @param buf where the callee should write the message
4424 * @return number of bytes written to buf
4427 send_core_path_ack (void *cls, size_t size, void *buf)
4429 struct MeshTransmissionDescriptor *info = cls;
4430 struct GNUNET_MESH_PathACK *msg = buf;
4432 GNUNET_assert (NULL != info);
4433 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4438 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4439 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4440 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4441 msg->tid = htonl (info->origin->tid);
4442 msg->peer_id = my_full_id;
4445 /* TODO add signature */
4447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4448 return sizeof (struct GNUNET_MESH_PathACK);
4453 * Free a transmission that was already queued with all resources
4454 * associated to the request.
4456 * @param queue Queue handler to cancel.
4457 * @param clear_cls Is it necessary to free associated cls?
4460 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4462 struct MeshTransmissionDescriptor *dd;
4463 struct MeshPathInfo *path_info;
4465 if (GNUNET_YES == clear_cls)
4467 switch (queue->type)
4469 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4470 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4471 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4474 data_descriptor_decrement_rc (dd->mesh_data);
4476 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4478 path_info = queue->cls;
4479 path_destroy (path_info->path);
4483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4484 " type %s unknown!\n",
4485 GNUNET_MESH_DEBUG_M2S(queue->type));
4487 GNUNET_free_non_null (queue->cls);
4489 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4490 queue->peer->queue_tail,
4492 GNUNET_free (queue);
4497 * @brief Get the next transmittable message from the queue.
4499 * This will be the head, except in the case of being a data packet
4500 * not allowed by the destination peer.
4502 * @param peer Destination peer.
4504 * @return The next viable MeshPeerQueue element to send to that peer.
4505 * NULL when there are no transmittable messages.
4507 struct MeshPeerQueue *
4508 queue_get_next (const struct MeshPeerInfo *peer)
4510 struct MeshPeerQueue *q;
4511 struct MeshTunnel *t;
4512 struct MeshTransmissionDescriptor *info;
4513 struct MeshTunnelChildInfo *cinfo;
4514 struct GNUNET_MESH_Unicast *ucast;
4515 struct GNUNET_MESH_ToOrigin *to_orig;
4516 struct GNUNET_MESH_Multicast *mcast;
4517 struct GNUNET_PeerIdentity id;
4521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4522 for (q = peer->queue_head; NULL != q; q = q->next)
4526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4528 GNUNET_MESH_DEBUG_M2S(q->type));
4531 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4532 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4533 pid = ntohl (ucast->pid);
4534 GNUNET_PEER_resolve (info->peer->id, &id);
4535 cinfo = tunnel_get_neighbor_fc(t, &id);
4536 ack = cinfo->fwd_ack;
4538 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4539 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4540 pid = ntohl (to_orig->pid);
4543 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4544 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4545 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4547 // Not a multicast payload: multicast control traffic (destroy, etc)
4550 pid = ntohl (mcast->pid);
4551 GNUNET_PEER_resolve (info->peer->id, &id);
4552 cinfo = tunnel_get_neighbor_fc(t, &id);
4553 ack = cinfo->fwd_ack;
4556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4561 "********* ACK: %u, PID: %u\n",
4563 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4572 "********* NEXT!\n");
4575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4576 "********* nothing found\n");
4582 * Core callback to write a queued packet to core buffer
4584 * @param cls Closure (peer info).
4585 * @param size Number of bytes available in buf.
4586 * @param buf Where the to write the message.
4588 * @return number of bytes written to buf
4591 queue_send (void *cls, size_t size, void *buf)
4593 struct MeshPeerInfo *peer = cls;
4594 struct GNUNET_MessageHeader *msg;
4595 struct MeshPeerQueue *queue;
4596 struct MeshTunnel *t;
4597 struct MeshTunnelChildInfo *cinfo;
4598 struct GNUNET_PeerIdentity dst_id;
4601 peer->core_transmit = NULL;
4603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4604 queue = queue_get_next(peer);
4606 /* Queue has no internal mesh traffic not sendable payload */
4609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4610 if (NULL == peer->queue_head)
4611 GNUNET_break(0); // Should've been canceled
4614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4616 GNUNET_PEER_resolve (peer->id, &dst_id);
4617 /* Check if buffer size is enough for the message */
4618 if (queue->size > size)
4622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4623 "********* not enough room, reissue\n");
4624 peer->core_transmit =
4625 GNUNET_CORE_notify_transmit_ready(core_handle,
4628 GNUNET_TIME_UNIT_FOREVER_REL,
4635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4638 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4642 "********* unicast: t->q (%u/%u)\n",
4643 t->fwd_queue_n, t->fwd_queue_max);
4645 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4652 switch (queue->type)
4655 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4656 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4657 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4659 "********* raw: %s\n",
4660 GNUNET_MESH_DEBUG_M2S (queue->type));
4662 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4663 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4664 data_size = send_core_data_raw (queue->cls, size, buf);
4665 msg = (struct GNUNET_MessageHeader *) buf;
4666 switch (ntohs (msg->type)) // Type of preconstructed message
4668 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4669 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4671 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4672 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4678 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4681 struct MeshTransmissionDescriptor *info = queue->cls;
4683 if ((1 == info->mesh_data->reference_counter
4684 && GNUNET_YES == t->speed_min)
4686 (info->mesh_data->total_out == info->mesh_data->reference_counter
4687 && GNUNET_NO == t->speed_min))
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4690 "********* considered sent\n");
4694 data_size = send_core_data_multicast(queue->cls, size, buf);
4695 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4697 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4699 data_size = send_core_path_create(queue->cls, size, buf);
4701 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4703 data_size = send_core_path_ack(queue->cls, size, buf);
4707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4708 "********* type unknown: %u\n",
4712 switch (queue->type)
4714 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4715 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4716 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4717 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4718 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4721 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4722 "at pos %u (%p) != %p\n",
4723 cinfo->send_buffer_start,
4724 cinfo->send_buffer[cinfo->send_buffer_start],
4727 GNUNET_break(cinfo->send_buffer_n > 0);
4728 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4729 cinfo->send_buffer_n--;
4730 cinfo->send_buffer_start++;
4731 cinfo->send_buffer_start %= t->fwd_queue_max;
4737 /* Free queue, but cls was freed by send_core_* */
4738 queue_destroy (queue, GNUNET_NO);
4740 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4746 /* If more data in queue, send next */
4747 queue = queue_get_next(peer);
4750 struct GNUNET_PeerIdentity id;
4752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4753 GNUNET_PEER_resolve (peer->id, &id);
4754 peer->core_transmit =
4755 GNUNET_CORE_notify_transmit_ready(core_handle,
4758 GNUNET_TIME_UNIT_FOREVER_REL,
4766 if (NULL != peer->queue_head)
4767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4768 "********* %s stalled\n",
4769 GNUNET_i2s(&my_full_id));
4771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4777 * Queue and pass message to core when possible.
4779 * @param cls Closure (type dependant).
4780 * @param type Type of the message, 0 for a raw message.
4781 * @param size Size of the message.
4782 * @param dst Neighbor to send message to.
4783 * @param t Tunnel this message belongs to.
4786 queue_add (void *cls, uint16_t type, size_t size,
4787 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4789 struct MeshPeerQueue *queue;
4790 struct MeshTunnelChildInfo *cinfo;
4791 struct GNUNET_PeerIdentity id;
4797 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4798 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4800 n = &t->fwd_queue_n;
4801 max = &t->fwd_queue_max;
4803 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4805 n = &t->bck_queue_n;
4806 max = &t->bck_queue_max;
4811 if (NULL == t->owner)
4812 GNUNET_break_op(0); // TODO: kill connection?
4815 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4817 return; // Drop message
4821 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4827 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4828 GNUNET_PEER_resolve (dst->id, &id);
4829 if (NULL == dst->core_transmit)
4831 dst->core_transmit =
4832 GNUNET_CORE_notify_transmit_ready(core_handle,
4835 GNUNET_TIME_UNIT_FOREVER_REL,
4841 if (NULL == n) // Is this internal mesh traffic?
4844 // It's payload, keep track of buffer per peer.
4845 cinfo = tunnel_get_neighbor_fc(t, &id);
4846 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4847 if (NULL != cinfo->send_buffer[i])
4849 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4850 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4851 cinfo->send_buffer_start++;
4852 cinfo->send_buffer_start %= t->fwd_queue_max;
4853 cinfo->send_buffer_n--;
4855 cinfo->send_buffer[i] = queue;
4856 cinfo->send_buffer_n++;
4857 if (cinfo->send_buffer_n > t->fwd_queue_max)
4860 cinfo->send_buffer_n = t->fwd_queue_max;
4865 /******************************************************************************/
4866 /******************** MESH NETWORK HANDLERS **************************/
4867 /******************************************************************************/
4871 * Core handler for path creation
4873 * @param cls closure
4874 * @param message message
4875 * @param peer peer identity this notification is about
4876 * @param atsi performance data
4877 * @param atsi_count number of records in 'atsi'
4879 * @return GNUNET_OK to keep the connection open,
4880 * GNUNET_SYSERR to close it (signal serious error)
4883 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4884 const struct GNUNET_MessageHeader *message,
4885 const struct GNUNET_ATS_Information *atsi,
4886 unsigned int atsi_count)
4888 unsigned int own_pos;
4891 MESH_TunnelNumber tid;
4892 struct GNUNET_MESH_ManipulatePath *msg;
4893 struct GNUNET_PeerIdentity *pi;
4894 struct GNUNET_HashCode hash;
4895 struct MeshPeerPath *path;
4896 struct MeshPeerInfo *dest_peer_info;
4897 struct MeshPeerInfo *orig_peer_info;
4898 struct MeshTunnel *t;
4900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4901 "Received a path create msg [%s]\n",
4902 GNUNET_i2s (&my_full_id));
4903 size = ntohs (message->size);
4904 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4906 GNUNET_break_op (0);
4910 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4911 if (size % sizeof (struct GNUNET_PeerIdentity))
4913 GNUNET_break_op (0);
4916 size /= sizeof (struct GNUNET_PeerIdentity);
4919 GNUNET_break_op (0);
4922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4923 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4925 tid = ntohl (msg->tid);
4926 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4928 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4929 t = tunnel_get (pi, tid);
4930 if (NULL == t) // FIXME only for INCOMING tunnels?
4934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4935 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4938 // FIXME notify failure
4941 opt = ntohl (msg->opt);
4942 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4943 GNUNET_YES : GNUNET_NO;
4944 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4945 GNUNET_YES : GNUNET_NO;
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4947 " speed_min: %d, nobuffer:%d\n",
4948 t->speed_min, t->nobuffer);
4950 if (GNUNET_YES == t->nobuffer)
4952 t->bck_queue_max = 1;
4953 t->fwd_queue_max = 1;
4956 // FIXME only assign a local tid if a local client is interested (on demand)
4957 while (NULL != tunnel_get_incoming (next_local_tid))
4958 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4959 t->local_tid_dest = next_local_tid++;
4960 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4963 tunnel_reset_timeout (t);
4964 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4966 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4967 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4975 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4976 if (NULL == dest_peer_info)
4978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4979 " Creating PeerInfo for destination.\n");
4980 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4981 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4982 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4984 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4986 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4987 if (NULL == orig_peer_info)
4989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4990 " Creating PeerInfo for origin.\n");
4991 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4992 orig_peer_info->id = GNUNET_PEER_intern (pi);
4993 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4994 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4997 path = path_new (size);
4999 for (i = 0; i < size; i++)
5001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5002 GNUNET_i2s (&pi[i]));
5003 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5004 if (path->peers[i] == myid)
5007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5010 /* cannot be self, must be 'not found' */
5011 /* create path: self not found in path through self */
5012 GNUNET_break_op (0);
5013 path_destroy (path);
5017 path_add_to_peers (path, GNUNET_NO);
5018 tunnel_add_path (t, path, own_pos);
5019 if (own_pos == size - 1)
5021 /* It is for us! Send ack. */
5022 struct MeshTransmissionDescriptor *info;
5024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5025 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5026 if (NULL == t->peers)
5028 /* New tunnel! Notify clients on data. */
5029 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5031 GNUNET_break (GNUNET_SYSERR !=
5032 GNUNET_CONTAINER_multihashmap_put (t->peers,
5033 &my_full_id.hashPubKey,
5036 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5037 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5038 info->origin = &t->id;
5039 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5040 GNUNET_assert (NULL != info->peer);
5042 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5043 sizeof (struct GNUNET_MESH_PathACK),
5049 struct MeshPeerPath *path2;
5051 /* It's for somebody else! Retransmit. */
5052 path2 = path_duplicate (path);
5053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5054 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5055 path2 = path_duplicate (path);
5056 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5057 send_create_path (dest_peer_info, path, t);
5064 * Core handler for path destruction
5066 * @param cls closure
5067 * @param message message
5068 * @param peer peer identity this notification is about
5069 * @param atsi performance data
5070 * @param atsi_count number of records in 'atsi'
5072 * @return GNUNET_OK to keep the connection open,
5073 * GNUNET_SYSERR to close it (signal serious error)
5076 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5077 const struct GNUNET_MessageHeader *message,
5078 const struct GNUNET_ATS_Information *atsi,
5079 unsigned int atsi_count)
5081 struct GNUNET_MESH_ManipulatePath *msg;
5082 struct GNUNET_PeerIdentity *pi;
5083 struct MeshPeerPath *path;
5084 struct MeshTunnel *t;
5085 unsigned int own_pos;
5089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5090 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5091 size = ntohs (message->size);
5092 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5094 GNUNET_break_op (0);
5098 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5099 if (size % sizeof (struct GNUNET_PeerIdentity))
5101 GNUNET_break_op (0);
5104 size /= sizeof (struct GNUNET_PeerIdentity);
5107 GNUNET_break_op (0);
5110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5112 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5113 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5115 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5117 t = tunnel_get (pi, ntohl (msg->tid));
5120 /* TODO notify back: we don't know this tunnel */
5121 GNUNET_break_op (0);
5124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5125 path = path_new (size);
5127 for (i = 0; i < size; i++)
5129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5130 GNUNET_i2s (&pi[i]));
5131 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5132 if (path->peers[i] == myid)
5135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5136 if (own_pos < path->length - 1)
5137 send_message (message, &pi[own_pos + 1], t);
5139 send_client_tunnel_disconnect(t, NULL);
5141 tunnel_delete_peer (t, path->peers[path->length - 1]);
5142 path_destroy (path);
5148 * Core handler for notifications of broken paths
5150 * @param cls closure
5151 * @param message message
5152 * @param peer peer identity this notification is about
5153 * @param atsi performance data
5154 * @param atsi_count number of records in 'atsi'
5156 * @return GNUNET_OK to keep the connection open,
5157 * GNUNET_SYSERR to close it (signal serious error)
5160 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5161 const struct GNUNET_MessageHeader *message,
5162 const struct GNUNET_ATS_Information *atsi,
5163 unsigned int atsi_count)
5165 struct GNUNET_MESH_PathBroken *msg;
5166 struct MeshTunnel *t;
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5169 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5170 msg = (struct GNUNET_MESH_PathBroken *) message;
5171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5172 GNUNET_i2s (&msg->peer1));
5173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5174 GNUNET_i2s (&msg->peer2));
5175 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5178 GNUNET_break_op (0);
5181 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5182 GNUNET_PEER_search (&msg->peer2));
5189 * Core handler for tunnel destruction
5191 * @param cls closure
5192 * @param message message
5193 * @param peer peer identity this notification is about
5194 * @param atsi performance data
5195 * @param atsi_count number of records in 'atsi'
5197 * @return GNUNET_OK to keep the connection open,
5198 * GNUNET_SYSERR to close it (signal serious error)
5201 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5202 const struct GNUNET_MessageHeader *message,
5203 const struct GNUNET_ATS_Information *atsi,
5204 unsigned int atsi_count)
5206 struct GNUNET_MESH_TunnelDestroy *msg;
5207 struct MeshTunnel *t;
5209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5210 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5211 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5213 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5214 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5217 /* Probably already got the message from another path,
5218 * destroyed the tunnel and retransmitted to children.
5221 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5224 if (t->id.oid == myid)
5226 GNUNET_break_op (0);
5229 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5231 /* Tunnel was incoming, notify clients */
5232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5233 t->local_tid, t->local_tid_dest);
5234 send_clients_tunnel_destroy (t);
5236 tunnel_send_destroy (t);
5237 t->destroy = GNUNET_YES;
5238 // TODO: add timeout to destroy the tunnel anyway
5244 * Core handler for mesh network traffic going from the origin to a peer
5246 * @param cls closure
5247 * @param peer peer identity this notification is about
5248 * @param message message
5249 * @param atsi performance data
5250 * @param atsi_count number of records in 'atsi'
5251 * @return GNUNET_OK to keep the connection open,
5252 * GNUNET_SYSERR to close it (signal serious error)
5255 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5256 const struct GNUNET_MessageHeader *message,
5257 const struct GNUNET_ATS_Information *atsi,
5258 unsigned int atsi_count)
5260 struct GNUNET_MESH_Unicast *msg;
5261 struct GNUNET_PeerIdentity *neighbor;
5262 struct MeshTunnelChildInfo *cinfo;
5263 struct MeshTunnel *t;
5264 GNUNET_PEER_Id dest_id;
5269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5272 size = ntohs (message->size);
5274 sizeof (struct GNUNET_MESH_Unicast) +
5275 sizeof (struct GNUNET_MessageHeader))
5280 msg = (struct GNUNET_MESH_Unicast *) message;
5281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5282 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5284 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5287 /* TODO notify back: we don't know this tunnel */
5288 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5289 GNUNET_break_op (0);
5292 pid = ntohl (msg->pid);
5293 if (t->fwd_pid == pid)
5295 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5297 " Already seen pid %u, DROPPING!\n", pid);
5302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5303 " pid %u not seen yet, forwarding\n", pid);
5306 t->skip += (pid - t->fwd_pid) - 1;
5309 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5311 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5312 GNUNET_break_op (0);
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5314 "Received PID %u, ACK %u\n",
5315 pid, t->last_fwd_ack);
5319 tunnel_reset_timeout (t);
5320 dest_id = GNUNET_PEER_search (&msg->destination);
5321 if (dest_id == myid)
5323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5324 " it's for us! sending to clients...\n");
5325 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5326 send_subscribed_clients (message, &msg[1].header, t);
5327 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5330 ttl = ntohl (msg->ttl);
5331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5334 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5335 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5336 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5340 " not for us, retransmitting...\n");
5342 neighbor = tree_get_first_hop (t->tree, dest_id);
5343 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5345 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5348 if (GNUNET_YES == t->nobuffer &&
5349 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5351 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5352 GNUNET_break_op (0);
5355 send_message (message, neighbor, t);
5356 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5362 * Core handler for mesh network traffic going from the origin to all peers
5364 * @param cls closure
5365 * @param message message
5366 * @param peer peer identity this notification is about
5367 * @param atsi performance data
5368 * @param atsi_count number of records in 'atsi'
5369 * @return GNUNET_OK to keep the connection open,
5370 * GNUNET_SYSERR to close it (signal serious error)
5372 * TODO: Check who we got this from, to validate route.
5375 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5376 const struct GNUNET_MessageHeader *message,
5377 const struct GNUNET_ATS_Information *atsi,
5378 unsigned int atsi_count)
5380 struct GNUNET_MESH_Multicast *msg;
5381 struct MeshTunnel *t;
5385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5387 size = ntohs (message->size);
5388 if (sizeof (struct GNUNET_MESH_Multicast) +
5389 sizeof (struct GNUNET_MessageHeader) > size)
5391 GNUNET_break_op (0);
5394 msg = (struct GNUNET_MESH_Multicast *) message;
5395 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5399 /* TODO notify that we dont know that tunnel */
5400 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5401 GNUNET_break_op (0);
5404 pid = ntohl (msg->pid);
5405 if (t->fwd_pid == pid)
5407 /* already seen this packet, drop */
5408 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5410 " Already seen pid %u, DROPPING!\n", pid);
5411 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5417 " pid %u not seen yet, forwarding\n", pid);
5419 t->skip += (pid - t->fwd_pid) - 1;
5421 tunnel_reset_timeout (t);
5423 /* Transmit to locally interested clients */
5424 if (NULL != t->peers &&
5425 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5427 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5428 send_subscribed_clients (message, &msg[1].header, t);
5429 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5432 if (ntohl (msg->ttl) == 0)
5434 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5435 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5436 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5439 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5440 tunnel_send_multicast (t, message, GNUNET_NO);
5446 * Core handler for mesh network traffic toward the owner of a tunnel
5448 * @param cls closure
5449 * @param message message
5450 * @param peer peer identity this notification is about
5451 * @param atsi performance data
5452 * @param atsi_count number of records in 'atsi'
5454 * @return GNUNET_OK to keep the connection open,
5455 * GNUNET_SYSERR to close it (signal serious error)
5458 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5459 const struct GNUNET_MessageHeader *message,
5460 const struct GNUNET_ATS_Information *atsi,
5461 unsigned int atsi_count)
5463 struct GNUNET_MESH_ToOrigin *msg;
5464 struct GNUNET_PeerIdentity id;
5465 struct MeshPeerInfo *peer_info;
5466 struct MeshTunnel *t;
5470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5472 size = ntohs (message->size);
5473 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5474 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5476 GNUNET_break_op (0);
5479 msg = (struct GNUNET_MESH_ToOrigin *) message;
5480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5481 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5482 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5486 /* TODO notify that we dont know this tunnel (whom)? */
5487 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5488 GNUNET_break_op (0);
5492 if (NULL != t->owner)
5495 struct GNUNET_MESH_ToOrigin *copy;
5497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5498 " it's for us! sending to clients...\n");
5499 /* TODO signature verification */
5500 memcpy (cbuf, message, size);
5501 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5502 copy->tid = htonl (t->local_tid);
5503 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5504 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5505 ©->header, GNUNET_NO);
5506 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5510 " not for us, retransmitting...\n");
5512 peer_info = peer_info_get (&msg->oid);
5513 if (NULL == peer_info)
5515 /* unknown origin of tunnel */
5519 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5520 send_message (message, &id, t);
5521 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5528 * Core handler for mesh network traffic point-to-point acks.
5530 * @param cls closure
5531 * @param message message
5532 * @param peer peer identity this notification is about
5533 * @param atsi performance data
5534 * @param atsi_count number of records in 'atsi'
5536 * @return GNUNET_OK to keep the connection open,
5537 * GNUNET_SYSERR to close it (signal serious error)
5540 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5541 const struct GNUNET_MessageHeader *message,
5542 const struct GNUNET_ATS_Information *atsi,
5543 unsigned int atsi_count)
5545 struct GNUNET_MESH_ACK *msg;
5546 struct MeshTunnel *t;
5549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5551 msg = (struct GNUNET_MESH_ACK *) message;
5553 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5557 /* TODO notify that we dont know this tunnel (whom)? */
5558 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5559 GNUNET_break_op (0);
5562 ack = ntohl (msg->pid);
5564 /* Is this a forward or backward ACK? */
5565 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5567 struct MeshTunnelChildInfo *cinfo;
5570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5571 cinfo = tunnel_get_neighbor_fc (t, peer);
5572 cinfo->fwd_ack = ack;
5573 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5574 tunnel_unlock_fwd_queues (t);
5578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5580 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5581 tunnel_unlock_bck_queue (t);
5588 * Core handler for path ACKs
5590 * @param cls closure
5591 * @param message message
5592 * @param peer peer identity this notification is about
5593 * @param atsi performance data
5594 * @param atsi_count number of records in 'atsi'
5596 * @return GNUNET_OK to keep the connection open,
5597 * GNUNET_SYSERR to close it (signal serious error)
5600 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5601 const struct GNUNET_MessageHeader *message,
5602 const struct GNUNET_ATS_Information *atsi,
5603 unsigned int atsi_count)
5605 struct GNUNET_MESH_PathACK *msg;
5606 struct GNUNET_PeerIdentity id;
5607 struct MeshPeerInfo *peer_info;
5608 struct MeshPeerPath *p;
5609 struct MeshTunnel *t;
5611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5612 GNUNET_i2s (&my_full_id));
5613 msg = (struct GNUNET_MESH_PathACK *) message;
5614 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5617 /* TODO notify that we don't know the tunnel */
5618 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5620 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5624 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5626 peer_info = peer_info_get (&msg->peer_id);
5627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5628 GNUNET_i2s (&msg->peer_id));
5629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5632 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5635 "connect_by_string completed, stopping search\n");
5636 regex_cancel_search (t->regex_ctx);
5637 t->regex_ctx = NULL;
5640 /* Add paths to peers? */
5641 p = tree_get_path_to_peer (t->tree, peer_info->id);
5644 path_add_to_peers (p, GNUNET_YES);
5652 /* Message for us? */
5653 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5656 if (NULL == t->owner)
5658 GNUNET_break_op (0);
5661 if (NULL != t->dht_get_type)
5663 GNUNET_DHT_get_stop (t->dht_get_type);
5664 t->dht_get_type = NULL;
5666 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5668 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5669 send_client_peer_connected (t, peer_info->id);
5674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5675 " not for us, retransmitting...\n");
5676 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5677 peer_info = peer_info_get (&msg->oid);
5678 if (NULL == peer_info)
5680 /* If we know the tunnel, we should DEFINITELY know the peer */
5684 send_message (message, &id, t);
5690 * Core handler for mesh keepalives.
5692 * @param cls closure
5693 * @param message message
5694 * @param peer peer identity this notification is about
5695 * @param atsi performance data
5696 * @param atsi_count number of records in 'atsi'
5697 * @return GNUNET_OK to keep the connection open,
5698 * GNUNET_SYSERR to close it (signal serious error)
5700 * TODO: Check who we got this from, to validate route.
5703 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5704 const struct GNUNET_MessageHeader *message,
5705 const struct GNUNET_ATS_Information *atsi,
5706 unsigned int atsi_count)
5708 struct GNUNET_MESH_TunnelKeepAlive *msg;
5709 struct MeshTunnel *t;
5711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5714 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5715 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5719 /* TODO notify that we dont know that tunnel */
5720 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1, GNUNET_NO);
5721 GNUNET_break_op (0);
5725 tunnel_reset_timeout (t);
5727 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5728 tunnel_send_multicast (t, message, GNUNET_NO);
5735 * Functions to handle messages from core
5737 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5738 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5739 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5740 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5741 sizeof (struct GNUNET_MESH_PathBroken)},
5742 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5743 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5744 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5745 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5746 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5747 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5748 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5749 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5750 sizeof (struct GNUNET_MESH_ACK)},
5751 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5752 sizeof (struct GNUNET_MESH_PathACK)},
5758 /******************************************************************************/
5759 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5760 /******************************************************************************/
5763 * deregister_app: iterator for removing each application registered by a client
5765 * @param cls closure
5766 * @param key the hash of the application id (used to access the hashmap)
5767 * @param value the value stored at the key (client)
5769 * @return GNUNET_OK on success
5772 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5774 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5775 GNUNET_break (GNUNET_YES ==
5776 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5782 * notify_client_connection_failure: notify a client that the connection to the
5783 * requested remote peer is not possible (for instance, no route found)
5784 * Function called when the socket is ready to queue more data. "buf" will be
5785 * NULL and "size" zero if the socket was closed for writing in the meantime.
5787 * @param cls closure
5788 * @param size number of bytes available in buf
5789 * @param buf where the callee should write the message
5790 * @return number of bytes written to buf
5793 notify_client_connection_failure (void *cls, size_t size, void *buf)
5796 struct MeshPeerInfo *peer_info;
5797 struct GNUNET_MESH_PeerControl *msg;
5798 struct GNUNET_PeerIdentity id;
5800 if (0 == size && NULL == buf)
5802 // TODO retry? cancel?
5806 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5807 peer_info = (struct MeshPeerInfo *) cls;
5808 msg = (struct GNUNET_MESH_PeerControl *) buf;
5809 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5810 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5811 // msg->tunnel_id = htonl(peer_info->t->tid);
5812 GNUNET_PEER_resolve (peer_info->id, &id);
5813 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5821 * Send keepalive packets for a peer
5823 * @param cls Closure (tunnel for which to send the keepalive).
5824 * @param tc Notification context.
5827 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5829 struct MeshTunnel *t = cls;
5830 struct GNUNET_MESH_TunnelKeepAlive *msg;
5831 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5834 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5835 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5841 "sending keepalive for tunnel %d\n", t->id.tid);
5843 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5844 msg->header.size = htons (size);
5845 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5846 msg->oid = my_full_id;
5847 msg->tid = htonl (t->id.tid);
5848 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5850 t->path_refresh_task =
5851 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5857 * Function to process paths received for a new peer addition. The recorded
5858 * paths form the initial tunnel, which can be optimized later.
5859 * Called on each result obtained for the DHT search.
5861 * @param cls closure
5862 * @param exp when will this value expire
5863 * @param key key of the result
5864 * @param get_path path of the get request
5865 * @param get_path_length lenght of get_path
5866 * @param put_path path of the put request
5867 * @param put_path_length length of the put_path
5868 * @param type type of the result
5869 * @param size number of bytes in data
5870 * @param data pointer to the result data
5872 * TODO: re-issue the request after certain time? cancel after X results?
5875 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5876 const struct GNUNET_HashCode * key,
5877 const struct GNUNET_PeerIdentity *get_path,
5878 unsigned int get_path_length,
5879 const struct GNUNET_PeerIdentity *put_path,
5880 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5881 size_t size, const void *data)
5883 struct MeshPathInfo *path_info = cls;
5884 struct MeshPeerPath *p;
5885 struct GNUNET_PeerIdentity pi;
5888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5889 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5892 p = path_build_from_dht (get_path, get_path_length, put_path,
5894 path_add_to_peers (p, GNUNET_NO);
5896 for (i = 0; i < path_info->peer->ntunnels; i++)
5898 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5899 peer_info_connect (path_info->peer, path_info->t);
5907 * Function to process paths received for a new peer addition. The recorded
5908 * paths form the initial tunnel, which can be optimized later.
5909 * Called on each result obtained for the DHT search.
5911 * @param cls closure
5912 * @param exp when will this value expire
5913 * @param key key of the result
5914 * @param get_path path of the get request
5915 * @param get_path_length lenght of get_path
5916 * @param put_path path of the put request
5917 * @param put_path_length length of the put_path
5918 * @param type type of the result
5919 * @param size number of bytes in data
5920 * @param data pointer to the result data
5923 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5924 const struct GNUNET_HashCode * key,
5925 const struct GNUNET_PeerIdentity *get_path,
5926 unsigned int get_path_length,
5927 const struct GNUNET_PeerIdentity *put_path,
5928 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5929 size_t size, const void *data)
5931 const struct PBlock *pb = data;
5932 const struct GNUNET_PeerIdentity *pi = &pb->id;
5933 struct MeshTunnel *t = cls;
5934 struct MeshPeerInfo *peer_info;
5935 struct MeshPeerPath *p;
5937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5938 if (size != sizeof (struct PBlock))
5940 GNUNET_break_op (0);
5943 if (ntohl(pb->type) != t->type)
5945 GNUNET_break_op (0);
5948 GNUNET_assert (NULL != t->owner);
5949 peer_info = peer_info_get (pi);
5950 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5952 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5954 p = path_build_from_dht (get_path, get_path_length, put_path,
5956 path_add_to_peers (p, GNUNET_NO);
5958 tunnel_add_peer (t, peer_info);
5959 peer_info_connect (peer_info, t);
5964 * Function to process DHT string to regex matching.
5965 * Called on each result obtained for the DHT search.
5967 * @param cls closure (search context)
5968 * @param exp when will this value expire
5969 * @param key key of the result
5970 * @param get_path path of the get request (not used)
5971 * @param get_path_length lenght of get_path (not used)
5972 * @param put_path path of the put request (not used)
5973 * @param put_path_length length of the put_path (not used)
5974 * @param type type of the result
5975 * @param size number of bytes in data
5976 * @param data pointer to the result data
5979 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5980 const struct GNUNET_HashCode * key,
5981 const struct GNUNET_PeerIdentity *get_path,
5982 unsigned int get_path_length,
5983 const struct GNUNET_PeerIdentity *put_path,
5984 unsigned int put_path_length,
5985 enum GNUNET_BLOCK_Type type,
5986 size_t size, const void *data)
5988 const struct MeshRegexAccept *block = data;
5989 struct MeshRegexSearchContext *ctx = cls;
5990 struct MeshRegexSearchInfo *info = ctx->info;
5991 struct MeshPeerPath *p;
5992 struct MeshPeerInfo *peer_info;
5994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
5995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
5997 peer_info = peer_info_get(&block->id);
5998 p = path_build_from_dht (get_path, get_path_length, put_path,
6000 path_add_to_peers (p, GNUNET_NO);
6003 tunnel_add_peer (info->t, peer_info);
6004 peer_info_connect (peer_info, info->t);
6005 if (0 == info->peer)
6007 info->peer = peer_info->id;
6011 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6014 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6015 ®ex_connect_timeout,
6023 * Function to process DHT string to regex matching.
6024 * Called on each result obtained for the DHT search.
6026 * @param cls closure (search context)
6027 * @param exp when will this value expire
6028 * @param key key of the result
6029 * @param get_path path of the get request (not used)
6030 * @param get_path_length lenght of get_path (not used)
6031 * @param put_path path of the put request (not used)
6032 * @param put_path_length length of the put_path (not used)
6033 * @param type type of the result
6034 * @param size number of bytes in data
6035 * @param data pointer to the result data
6037 * TODO: re-issue the request after certain time? cancel after X results?
6040 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6041 const struct GNUNET_HashCode * key,
6042 const struct GNUNET_PeerIdentity *get_path,
6043 unsigned int get_path_length,
6044 const struct GNUNET_PeerIdentity *put_path,
6045 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6046 size_t size, const void *data)
6048 const struct MeshRegexBlock *block = data;
6049 struct MeshRegexSearchContext *ctx = cls;
6050 struct MeshRegexSearchInfo *info = ctx->info;
6054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6055 "DHT GET STRING RETURNED RESULTS\n");
6056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6057 " key: %s\n", GNUNET_h2s (key));
6059 copy = GNUNET_malloc (size);
6060 memcpy (copy, data, size);
6061 GNUNET_break (GNUNET_OK ==
6062 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6063 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6064 len = ntohl (block->n_proof);
6066 char proof[len + 1];
6068 memcpy (proof, &block[1], len);
6070 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6072 GNUNET_break_op (0);
6076 len = strlen (info->description);
6077 if (len == ctx->position) // String processed
6079 if (GNUNET_YES == ntohl (block->accepting))
6081 regex_find_path(key, ctx);
6085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6086 // FIXME REGEX this block not successful, wait for more? start timeout?
6090 GNUNET_break (GNUNET_OK ==
6091 GNUNET_MESH_regex_block_iterate (block, size,
6092 ®ex_edge_iterator, ctx));
6096 /******************************************************************************/
6097 /********************* MESH LOCAL HANDLES **************************/
6098 /******************************************************************************/
6102 * Handler for client disconnection
6104 * @param cls closure
6105 * @param client identification of the client; NULL
6106 * for the last call when the server is destroyed
6109 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6111 struct MeshClient *c;
6112 struct MeshClient *next;
6115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6124 if (c->handle != client)
6126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6132 GNUNET_SERVER_client_drop (c->handle);
6133 c->shutting_down = GNUNET_YES;
6134 GNUNET_assert (NULL != c->own_tunnels);
6135 GNUNET_assert (NULL != c->incoming_tunnels);
6136 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6137 &tunnel_destroy_iterator, c);
6138 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6139 &tunnel_destroy_iterator, c);
6140 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6141 &tunnel_destroy_iterator, c);
6142 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6143 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6144 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6146 /* deregister clients applications */
6147 if (NULL != c->apps)
6149 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6150 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6152 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6153 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6155 GNUNET_SCHEDULER_cancel (announce_applications_task);
6156 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6158 if (NULL != c->types)
6159 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6160 for (i = 0; i < c->n_regex; i++)
6162 GNUNET_free (c->regexes[i]);
6164 GNUNET_free_non_null (c->regexes);
6165 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6166 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6168 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6171 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6180 * Handler for new clients
6182 * @param cls closure
6183 * @param client identification of the client
6184 * @param message the actual message, which includes messages the client wants
6187 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6188 const struct GNUNET_MessageHeader *message)
6190 struct GNUNET_MESH_ClientConnect *cc_msg;
6191 struct MeshClient *c;
6192 GNUNET_MESH_ApplicationType *a;
6199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6200 /* Check data sanity */
6201 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6202 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6203 ntypes = ntohs (cc_msg->types);
6204 napps = ntohs (cc_msg->applications);
6206 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6209 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6213 /* Create new client structure */
6214 c = GNUNET_malloc (sizeof (struct MeshClient));
6215 c->id = next_client_id++;
6216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6218 GNUNET_SERVER_client_keep (client);
6219 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6222 GNUNET_MESH_ApplicationType at;
6223 struct GNUNET_HashCode hc;
6225 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6226 for (i = 0; i < napps; i++)
6229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6230 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6231 /* store in clients hashmap */
6232 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6233 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6234 /* store in global hashmap, for announcements */
6235 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6236 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6238 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6239 announce_applications_task =
6240 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6246 struct GNUNET_HashCode hc;
6248 t = (uint16_t *) & a[napps];
6249 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6250 for (i = 0; i < ntypes; i++)
6253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6254 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6256 /* store in clients hashmap */
6257 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6258 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6259 /* store in global hashmap */
6260 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6261 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6265 " client has %u+%u subscriptions\n", napps, ntypes);
6267 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6268 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6269 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6270 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6271 GNUNET_SERVER_notification_context_add (nc, client);
6272 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6274 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6280 * Handler for clients announcing available services by a regular expression.
6282 * @param cls closure
6283 * @param client identification of the client
6284 * @param message the actual message, which includes messages the client wants
6287 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6288 const struct GNUNET_MessageHeader *message)
6290 struct MeshClient *c;
6294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6296 /* Sanity check for client registration */
6297 if (NULL == (c = client_get (client)))
6300 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6305 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6306 regex = GNUNET_malloc (len + 1);
6307 memcpy (regex, &message[1], len);
6309 GNUNET_array_append (c->regexes, c->n_regex, regex);
6310 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6312 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6318 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6324 * Handler for requests of new tunnels
6326 * @param cls closure
6327 * @param client identification of the client
6328 * @param message the actual message
6331 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6332 const struct GNUNET_MessageHeader *message)
6334 struct GNUNET_MESH_TunnelMessage *t_msg;
6335 struct MeshTunnel *t;
6336 struct MeshClient *c;
6337 MESH_TunnelNumber tid;
6339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6341 /* Sanity check for client registration */
6342 if (NULL == (c = client_get (client)))
6345 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6350 /* Message sanity check */
6351 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6358 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6359 /* Sanity check for tunnel numbering */
6360 tid = ntohl (t_msg->tunnel_id);
6361 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6364 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6367 /* Sanity check for duplicate tunnel IDs */
6368 if (NULL != tunnel_get_by_local_id (c, tid))
6371 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6375 while (NULL != tunnel_get_by_pi (myid, next_tid))
6376 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6377 t = tunnel_new (myid, next_tid++, c, tid);
6380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6382 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6385 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6387 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6388 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6391 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6397 * Handler for requests of deleting tunnels
6399 * @param cls closure
6400 * @param client identification of the client
6401 * @param message the actual message
6404 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6405 const struct GNUNET_MessageHeader *message)
6407 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6408 struct MeshClient *c;
6409 struct MeshTunnel *t;
6410 MESH_TunnelNumber tid;
6412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6413 "Got a DESTROY TUNNEL from client!\n");
6415 /* Sanity check for client registration */
6416 if (NULL == (c = client_get (client)))
6419 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6424 /* Message sanity check */
6425 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6432 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6434 /* Retrieve tunnel */
6435 tid = ntohl (tunnel_msg->tunnel_id);
6436 t = tunnel_get_by_local_id(c, tid);
6439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6441 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6444 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6446 client_ignore_tunnel (c, t);
6448 // TODO: when to destroy incoming tunnel?
6449 if (t->nclients == 0)
6451 GNUNET_assert (GNUNET_YES ==
6452 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6454 GNUNET_assert (GNUNET_YES ==
6455 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6456 &my_full_id.hashPubKey,
6460 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6463 send_client_tunnel_disconnect(t, c);
6464 client_delete_tunnel(c, t);
6466 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6468 tunnel_send_destroy (t);
6469 t->destroy = GNUNET_YES;
6470 // The tunnel will be destroyed when the last message is transmitted.
6471 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6477 * Handler for requests of seeting tunnel's speed.
6479 * @param cls Closure (unused).
6480 * @param client Identification of the client.
6481 * @param message The actual message.
6484 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6485 const struct GNUNET_MessageHeader *message)
6487 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6488 struct MeshClient *c;
6489 struct MeshTunnel *t;
6490 MESH_TunnelNumber tid;
6492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6493 "Got a SPEED request from client!\n");
6495 /* Sanity check for client registration */
6496 if (NULL == (c = client_get (client)))
6499 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6505 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6507 /* Retrieve tunnel */
6508 tid = ntohl (tunnel_msg->tunnel_id);
6509 t = tunnel_get_by_local_id(c, tid);
6512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6514 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6518 switch (ntohs(message->type))
6520 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6521 t->speed_min = GNUNET_YES;
6523 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6524 t->speed_min = GNUNET_NO;
6529 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6534 * Handler for requests of seeting tunnel's buffering policy.
6536 * @param cls Closure (unused).
6537 * @param client Identification of the client.
6538 * @param message The actual message.
6541 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6542 const struct GNUNET_MessageHeader *message)
6544 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6545 struct MeshClient *c;
6546 struct MeshTunnel *t;
6547 MESH_TunnelNumber tid;
6549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6550 "Got a BUFFER request from client!\n");
6552 /* Sanity check for client registration */
6553 if (NULL == (c = client_get (client)))
6556 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6561 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6563 /* Retrieve tunnel */
6564 tid = ntohl (tunnel_msg->tunnel_id);
6565 t = tunnel_get_by_local_id(c, tid);
6568 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6570 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6574 switch (ntohs(message->type))
6576 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6577 t->nobuffer = GNUNET_NO;
6579 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6580 t->nobuffer = GNUNET_YES;
6586 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6591 * Handler for connection requests to new peers
6593 * @param cls closure
6594 * @param client identification of the client
6595 * @param message the actual message (PeerControl)
6598 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6599 const struct GNUNET_MessageHeader *message)
6601 struct GNUNET_MESH_PeerControl *peer_msg;
6602 struct MeshPeerInfo *peer_info;
6603 struct MeshClient *c;
6604 struct MeshTunnel *t;
6605 MESH_TunnelNumber tid;
6607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6608 /* Sanity check for client registration */
6609 if (NULL == (c = client_get (client)))
6612 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6617 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6619 /* Sanity check for message size */
6620 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6623 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6627 /* Tunnel exists? */
6628 tid = ntohl (peer_msg->tunnel_id);
6629 t = tunnel_get_by_local_id (c, tid);
6633 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6637 /* Does client own tunnel? */
6638 if (t->owner->handle != client)
6641 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6644 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6645 GNUNET_i2s (&peer_msg->peer));
6646 peer_info = peer_info_get (&peer_msg->peer);
6648 tunnel_add_peer (t, peer_info);
6649 peer_info_connect (peer_info, t);
6651 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6657 * Handler for disconnection requests of peers in a tunnel
6659 * @param cls closure
6660 * @param client identification of the client
6661 * @param message the actual message (PeerControl)
6664 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6665 const struct GNUNET_MessageHeader *message)
6667 struct GNUNET_MESH_PeerControl *peer_msg;
6668 struct MeshPeerInfo *peer_info;
6669 struct MeshClient *c;
6670 struct MeshTunnel *t;
6671 MESH_TunnelNumber tid;
6673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6674 /* Sanity check for client registration */
6675 if (NULL == (c = client_get (client)))
6678 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6683 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6685 /* Sanity check for message size */
6686 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6689 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6693 /* Tunnel exists? */
6694 tid = ntohl (peer_msg->tunnel_id);
6695 t = tunnel_get_by_local_id (c, tid);
6699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6704 /* Does client own tunnel? */
6705 if (t->owner->handle != client)
6708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6713 GNUNET_i2s (&peer_msg->peer));
6714 /* Is the peer in the tunnel? */
6716 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6717 if (NULL == peer_info)
6720 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6724 /* Ok, delete peer from tunnel */
6725 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6726 &peer_msg->peer.hashPubKey);
6728 send_destroy_path (t, peer_info->id);
6729 tunnel_delete_peer (t, peer_info->id);
6730 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6735 * Handler for blacklist requests of peers in a tunnel
6737 * @param cls closure
6738 * @param client identification of the client
6739 * @param message the actual message (PeerControl)
6741 * FIXME implement DHT block bloomfilter
6744 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6745 const struct GNUNET_MessageHeader *message)
6747 struct GNUNET_MESH_PeerControl *peer_msg;
6748 struct MeshClient *c;
6749 struct MeshTunnel *t;
6750 MESH_TunnelNumber tid;
6752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6753 /* Sanity check for client registration */
6754 if (NULL == (c = client_get (client)))
6757 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6762 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6764 /* Sanity check for message size */
6765 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6768 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6772 /* Tunnel exists? */
6773 tid = ntohl (peer_msg->tunnel_id);
6774 t = tunnel_get_by_local_id (c, tid);
6778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6783 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6784 GNUNET_PEER_intern(&peer_msg->peer));
6789 * Handler for unblacklist requests of peers in a tunnel
6791 * @param cls closure
6792 * @param client identification of the client
6793 * @param message the actual message (PeerControl)
6796 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6797 const struct GNUNET_MessageHeader *message)
6799 struct GNUNET_MESH_PeerControl *peer_msg;
6800 struct MeshClient *c;
6801 struct MeshTunnel *t;
6802 MESH_TunnelNumber tid;
6806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6807 /* Sanity check for client registration */
6808 if (NULL == (c = client_get (client)))
6811 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6816 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6818 /* Sanity check for message size */
6819 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6822 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6826 /* Tunnel exists? */
6827 tid = ntohl (peer_msg->tunnel_id);
6828 t = tunnel_get_by_local_id (c, tid);
6832 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6837 /* if peer is not known, complain */
6838 pid = GNUNET_PEER_search (&peer_msg->peer);
6845 /* search and remove from list */
6846 for (i = 0; i < t->nblacklisted; i++)
6848 if (t->blacklisted[i] == pid)
6850 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6851 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6856 /* if peer hasn't been blacklisted, complain */
6862 * Handler for connection requests to new peers by type
6864 * @param cls closure
6865 * @param client identification of the client
6866 * @param message the actual message (ConnectPeerByType)
6869 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6870 const struct GNUNET_MessageHeader *message)
6872 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6873 struct MeshClient *c;
6874 struct MeshTunnel *t;
6875 struct GNUNET_HashCode hash;
6876 MESH_TunnelNumber tid;
6878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6879 /* Sanity check for client registration */
6880 if (NULL == (c = client_get (client)))
6883 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6888 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6890 /* Sanity check for message size */
6891 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6892 ntohs (connect_msg->header.size))
6895 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6899 /* Tunnel exists? */
6900 tid = ntohl (connect_msg->tunnel_id);
6901 t = tunnel_get_by_local_id (c, tid);
6905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6909 /* Does client own tunnel? */
6910 if (t->owner->handle != client)
6913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6917 /* Do WE have the service? */
6918 t->type = ntohl (connect_msg->type);
6919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6920 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6921 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6924 /* Yes! Fast forward, add ourselves to the tunnel and send the
6925 * good news to the client, and alert the destination client of
6926 * an incoming tunnel.
6928 * FIXME send a path create to self, avoid code duplication
6930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6931 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6932 peer_info_get (&my_full_id),
6933 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6936 send_client_peer_connected (t, myid);
6937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6938 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6940 t->local_tid_dest = next_local_tid++;
6941 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6942 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6943 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6947 /* Ok, lets find a peer offering the service */
6948 if (NULL != t->dht_get_type)
6950 GNUNET_DHT_get_stop (t->dht_get_type);
6952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6953 GNUNET_h2s (&hash));
6955 GNUNET_DHT_get_start (dht_handle,
6956 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6958 dht_replication_level,
6959 GNUNET_DHT_RO_RECORD_ROUTE |
6960 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6962 &dht_get_type_handler, t);
6964 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6970 * Handler for connection requests to new peers by a string service description.
6972 * @param cls closure
6973 * @param client identification of the client
6974 * @param message the actual message, which includes messages the client wants
6977 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6978 const struct GNUNET_MessageHeader *message)
6980 struct GNUNET_MESH_ConnectPeerByString *msg;
6981 struct MeshRegexSearchContext *ctx;
6982 struct MeshRegexSearchInfo *info;
6983 struct GNUNET_DHT_GetHandle *get_h;
6984 struct GNUNET_HashCode key;
6985 struct MeshTunnel *t;
6986 struct MeshClient *c;
6987 MESH_TunnelNumber tid;
6991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6992 "Connect by string started\n");
6993 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
6994 size = htons (message->size);
6996 /* Sanity check for client registration */
6997 if (NULL == (c = client_get (client)))
7000 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7005 /* Message size sanity check */
7006 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7013 /* Tunnel exists? */
7014 tid = ntohl (msg->tunnel_id);
7015 t = tunnel_get_by_local_id (c, tid);
7019 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7023 /* Does client own tunnel? */
7024 if (t->owner->handle != client)
7027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7032 " on tunnel %s [%u]\n",
7033 GNUNET_i2s(&my_full_id),
7036 /* Only one connect_by_string allowed at the same time! */
7037 /* FIXME: allow more, return handle at api level to cancel, document */
7038 if (NULL != t->regex_ctx)
7041 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7045 /* Find string itself */
7046 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7047 string = (const char *) &msg[1];
7049 /* Initialize context */
7050 size = GNUNET_REGEX_get_first_key(string, len, &key);
7051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7052 " consumed %u bits out of %u\n", size, len);
7053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7054 " looking for %s\n", GNUNET_h2s (&key));
7056 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7058 info->description = GNUNET_malloc (len + 1);
7059 memcpy (info->description, string, len);
7060 info->description[len] = '\0';
7061 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7062 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7065 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7066 ctx->position = size;
7070 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7072 /* Start search in DHT */
7073 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7074 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7075 &key, /* key to search */
7076 dht_replication_level, /* replication level */
7077 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7078 NULL, /* xquery */ // FIXME BLOOMFILTER
7079 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7080 &dht_get_string_handler, ctx);
7082 GNUNET_break (GNUNET_OK ==
7083 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7086 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7088 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7094 * Handler for client traffic directed to one peer
7096 * @param cls closure
7097 * @param client identification of the client
7098 * @param message the actual message
7101 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7102 const struct GNUNET_MessageHeader *message)
7104 struct MeshClient *c;
7105 struct MeshTunnel *t;
7106 struct MeshPeerInfo *pi;
7107 struct GNUNET_MESH_Unicast *data_msg;
7108 MESH_TunnelNumber tid;
7111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7112 "Got a unicast request from a client!\n");
7114 /* Sanity check for client registration */
7115 if (NULL == (c = client_get (client)))
7118 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7123 data_msg = (struct GNUNET_MESH_Unicast *) message;
7125 /* Sanity check for message size */
7126 size = ntohs (message->size);
7127 if (sizeof (struct GNUNET_MESH_Unicast) +
7128 sizeof (struct GNUNET_MessageHeader) > size)
7131 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7135 /* Tunnel exists? */
7136 tid = ntohl (data_msg->tid);
7137 t = tunnel_get_by_local_id (c, tid);
7141 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7145 /* Is it a local tunnel? Then, does client own the tunnel? */
7146 if (t->owner->handle != client)
7149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7153 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7154 &data_msg->destination.hashPubKey);
7155 /* Is the selected peer in the tunnel? */
7159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7163 /* PID should be as expected */
7164 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7167 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7168 "Unicast PID, expected %u, got %u\n",
7169 t->fwd_pid + 1, ntohl (data_msg->pid));
7170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7174 /* Ok, everything is correct, send the message
7175 * (pretend we got it from a mesh peer)
7178 /* Work around const limitation */
7179 char buf[ntohs (message->size)] GNUNET_ALIGN;
7180 struct GNUNET_MESH_Unicast *copy;
7182 copy = (struct GNUNET_MESH_Unicast *) buf;
7183 memcpy (buf, data_msg, size);
7184 copy->oid = my_full_id;
7185 copy->tid = htonl (t->id.tid);
7186 copy->ttl = htonl (default_ttl);
7187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7188 " calling generic handler...\n");
7189 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7192 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7199 * Handler for client traffic directed to the origin
7201 * @param cls closure
7202 * @param client identification of the client
7203 * @param message the actual message
7206 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7207 const struct GNUNET_MessageHeader *message)
7209 struct GNUNET_MESH_ToOrigin *data_msg;
7210 struct MeshTunnelClientInfo *clinfo;
7211 struct MeshClient *c;
7212 struct MeshTunnel *t;
7213 MESH_TunnelNumber tid;
7216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7217 "Got a ToOrigin request from a client!\n");
7218 /* Sanity check for client registration */
7219 if (NULL == (c = client_get (client)))
7222 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7227 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7229 /* Sanity check for message size */
7230 size = ntohs (message->size);
7231 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7232 sizeof (struct GNUNET_MessageHeader) > size)
7235 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7239 /* Tunnel exists? */
7240 tid = ntohl (data_msg->tid);
7241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7242 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7248 t = tunnel_get_by_local_id (c, tid);
7251 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7252 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7258 /* It should be sent by someone who has this as incoming tunnel. */
7259 if (GNUNET_NO == client_knows_tunnel (c, t))
7262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7266 /* PID should be as expected */
7267 clinfo = tunnel_get_client_fc (t, c);
7268 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7271 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7272 "To Origin PID, expected %u, got %u\n",
7273 clinfo->bck_pid + 1,
7274 ntohl (data_msg->pid));
7275 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7279 /* Ok, everything is correct, send the message
7280 * (pretend we got it from a mesh peer)
7284 char buf[ntohs (message->size)] GNUNET_ALIGN;
7285 struct GNUNET_MESH_ToOrigin *copy;
7287 /* Work around const limitation */
7288 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7289 memcpy (buf, data_msg, size);
7290 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7291 copy->tid = htonl (t->id.tid);
7292 copy->ttl = htonl (default_ttl);
7293 if (ntohl (copy->pid) != (t->bck_pid + 1))
7296 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7297 "To Origin PID, expected %u, got %u\n",
7303 copy->sender = my_full_id;
7304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7305 " calling generic handler...\n");
7306 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7308 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7315 * Handler for client traffic directed to all peers in a tunnel
7317 * @param cls closure
7318 * @param client identification of the client
7319 * @param message the actual message
7322 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7323 const struct GNUNET_MessageHeader *message)
7325 struct MeshClient *c;
7326 struct MeshTunnel *t;
7327 struct GNUNET_MESH_Multicast *data_msg;
7328 MESH_TunnelNumber tid;
7330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7331 "Got a multicast request from a client!\n");
7333 /* Sanity check for client registration */
7334 if (NULL == (c = client_get (client)))
7337 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7342 data_msg = (struct GNUNET_MESH_Multicast *) message;
7344 /* Sanity check for message size */
7345 if (sizeof (struct GNUNET_MESH_Multicast) +
7346 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7349 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7353 /* Tunnel exists? */
7354 tid = ntohl (data_msg->tid);
7355 t = tunnel_get_by_local_id (c, tid);
7359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7360 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7362 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7366 /* Does client own tunnel? */
7367 if (t->owner->handle != client)
7370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7374 /* PID should be as expected */
7375 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7379 "Multicast PID, expected %u, got %u\n",
7380 t->fwd_pid + 1, ntohl (data_msg->pid));
7381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7386 char buf[ntohs (message->size)] GNUNET_ALIGN;
7387 struct GNUNET_MESH_Multicast *copy;
7389 copy = (struct GNUNET_MESH_Multicast *) buf;
7390 memcpy (buf, message, ntohs (message->size));
7391 copy->oid = my_full_id;
7392 copy->tid = htonl (t->id.tid);
7393 copy->ttl = htonl (default_ttl);
7394 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7396 " calling generic handler...\n");
7397 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7400 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
7406 * Handler for client's ACKs for payload traffic.
7408 * @param cls Closure (unused).
7409 * @param client Identification of the client.
7410 * @param message The actual message.
7413 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7414 const struct GNUNET_MessageHeader *message)
7416 struct GNUNET_MESH_LocalAck *msg;
7417 struct MeshTunnel *t;
7418 struct MeshClient *c;
7419 MESH_TunnelNumber tid;
7422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7423 /* Sanity check for client registration */
7424 if (NULL == (c = client_get (client)))
7427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7432 msg = (struct GNUNET_MESH_LocalAck *) message;
7434 /* Tunnel exists? */
7435 tid = ntohl (msg->tunnel_id);
7436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7437 t = tunnel_get_by_local_id (c, tid);
7441 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7443 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7447 ack = ntohl (msg->max_pid);
7448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7450 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7451 if (NULL != t->owner && t->owner->handle == client)
7453 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7455 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7459 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7460 tunnel_set_client_fwd_ack (t, c, ack);
7461 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7464 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7471 * Functions to handle messages from clients
7473 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7474 {&handle_local_new_client, NULL,
7475 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7476 {&handle_local_announce_regex, NULL,
7477 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7478 {&handle_local_tunnel_create, NULL,
7479 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7480 sizeof (struct GNUNET_MESH_TunnelMessage)},
7481 {&handle_local_tunnel_destroy, NULL,
7482 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7483 sizeof (struct GNUNET_MESH_TunnelMessage)},
7484 {&handle_local_tunnel_speed, NULL,
7485 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7486 sizeof (struct GNUNET_MESH_TunnelMessage)},
7487 {&handle_local_tunnel_speed, NULL,
7488 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7489 sizeof (struct GNUNET_MESH_TunnelMessage)},
7490 {&handle_local_tunnel_buffer, NULL,
7491 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7492 sizeof (struct GNUNET_MESH_TunnelMessage)},
7493 {&handle_local_tunnel_buffer, NULL,
7494 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7495 sizeof (struct GNUNET_MESH_TunnelMessage)},
7496 {&handle_local_connect_add, NULL,
7497 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7498 sizeof (struct GNUNET_MESH_PeerControl)},
7499 {&handle_local_connect_del, NULL,
7500 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7501 sizeof (struct GNUNET_MESH_PeerControl)},
7502 {&handle_local_blacklist, NULL,
7503 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7504 sizeof (struct GNUNET_MESH_PeerControl)},
7505 {&handle_local_unblacklist, NULL,
7506 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7507 sizeof (struct GNUNET_MESH_PeerControl)},
7508 {&handle_local_connect_by_type, NULL,
7509 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7510 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7511 {&handle_local_connect_by_string, NULL,
7512 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7513 {&handle_local_unicast, NULL,
7514 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7515 {&handle_local_to_origin, NULL,
7516 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7517 {&handle_local_multicast, NULL,
7518 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7519 {&handle_local_ack, NULL,
7520 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7521 sizeof (struct GNUNET_MESH_LocalAck)},
7527 * To be called on core init/fail.
7529 * @param cls service closure
7530 * @param server handle to the server for this service
7531 * @param identity the public identity of this peer
7534 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7535 const struct GNUNET_PeerIdentity *identity)
7538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7539 core_handle = server;
7540 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7543 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7544 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7546 GNUNET_abort(); // Try harder
7553 * Method called whenever a given peer connects.
7555 * @param cls closure
7556 * @param peer peer identity this notification is about
7557 * @param atsi performance data for the connection
7558 * @param atsi_count number of records in 'atsi'
7561 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7562 const struct GNUNET_ATS_Information *atsi,
7563 unsigned int atsi_count)
7565 struct MeshPeerInfo *peer_info;
7566 struct MeshPeerPath *path;
7568 DEBUG_CONN ("Peer connected\n");
7569 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7570 peer_info = peer_info_get (peer);
7571 if (myid == peer_info->id)
7573 DEBUG_CONN (" (self)\n");
7578 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7580 path = path_new (2);
7581 path->peers[0] = myid;
7582 path->peers[1] = peer_info->id;
7583 GNUNET_PEER_change_rc (myid, 1);
7584 GNUNET_PEER_change_rc (peer_info->id, 1);
7585 peer_info_add_path (peer_info, path, GNUNET_YES);
7586 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7592 * Method called whenever a peer disconnects.
7594 * @param cls closure
7595 * @param peer peer identity this notification is about
7598 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7600 struct MeshPeerInfo *pi;
7601 struct MeshPeerQueue *q;
7602 struct MeshPeerQueue *n;
7604 DEBUG_CONN ("Peer disconnected\n");
7605 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7617 /* try to reroute this traffic instead */
7618 queue_destroy(q, GNUNET_YES);
7622 peer_info_remove_path (pi, pi->id, myid);
7625 DEBUG_CONN (" (self)\n");
7627 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7632 /******************************************************************************/
7633 /************************ MAIN FUNCTIONS ****************************/
7634 /******************************************************************************/
7637 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7639 * @param cls closure
7640 * @param key current key code
7641 * @param value value in the hash map
7642 * @return GNUNET_YES if we should continue to iterate,
7646 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7648 struct MeshTunnel *t = value;
7655 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7657 * @param cls closure
7658 * @param key current key code
7659 * @param value value in the hash map
7660 * @return GNUNET_YES if we should continue to iterate,
7664 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7666 struct MeshPeerInfo *p = value;
7667 struct MeshPeerQueue *q;
7668 struct MeshPeerQueue *n;
7676 queue_destroy(q, GNUNET_YES);
7680 peer_info_destroy (p);
7685 * Task run during shutdown.
7691 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7695 if (core_handle != NULL)
7697 GNUNET_CORE_disconnect (core_handle);
7700 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7701 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7702 if (dht_handle != NULL)
7704 GNUNET_DHT_disconnect (dht_handle);
7709 GNUNET_SERVER_notification_context_destroy (nc);
7712 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7714 GNUNET_SCHEDULER_cancel (announce_id_task);
7715 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7721 * Process mesh requests.
7723 * @param cls closure
7724 * @param server the initialized server
7725 * @param c configuration to use
7728 run (void *cls, struct GNUNET_SERVER_Handle *server,
7729 const struct GNUNET_CONFIGURATION_Handle *c)
7731 struct MeshPeerInfo *peer;
7732 struct MeshPeerPath *p;
7735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7736 server_handle = server;
7737 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7738 NULL, /* Closure passed to MESH functions */
7739 &core_init, /* Call core_init once connected */
7740 &core_connect, /* Handle connects */
7741 &core_disconnect, /* remove peers on disconnects */
7742 NULL, /* Don't notify about all incoming messages */
7743 GNUNET_NO, /* For header only in notification */
7744 NULL, /* Don't notify about all outbound messages */
7745 GNUNET_NO, /* For header-only out notification */
7746 core_handlers); /* Register these handlers */
7748 if (core_handle == NULL)
7751 GNUNET_SCHEDULER_shutdown ();
7756 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7761 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7763 GNUNET_SCHEDULER_shutdown ();
7768 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7769 &refresh_path_time))
7771 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7773 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7774 "refresh path time");
7775 GNUNET_SCHEDULER_shutdown ();
7780 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7781 &app_announce_time))
7783 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7785 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7786 "app announce time");
7787 GNUNET_SCHEDULER_shutdown ();
7792 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7795 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7797 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7798 "id announce time");
7799 GNUNET_SCHEDULER_shutdown ();
7804 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7805 &unacknowledged_wait_time))
7807 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7809 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7810 "unacknowledged wait time");
7811 GNUNET_SCHEDULER_shutdown ();
7816 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7819 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7821 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7823 GNUNET_SCHEDULER_shutdown ();
7828 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7831 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7833 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7835 GNUNET_SCHEDULER_shutdown ();
7840 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7843 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7845 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7847 GNUNET_SCHEDULER_shutdown ();
7852 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7855 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7857 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7863 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7864 &dht_replication_level))
7866 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7868 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7869 "dht replication level", 10);
7870 dht_replication_level = 10;
7874 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7875 GNUNET_free (keyfile);
7876 if (my_private_key == NULL)
7878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7879 _("Mesh service could not access hostkey. Exiting.\n"));
7880 GNUNET_SCHEDULER_shutdown ();
7883 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7884 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7885 &my_full_id.hashPubKey);
7886 myid = GNUNET_PEER_intern (&my_full_id);
7887 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7888 "Mesh for peer [%s] starting\n",
7889 GNUNET_i2s(&my_full_id));
7891 // transport_handle = GNUNET_TRANSPORT_connect(c,
7898 dht_handle = GNUNET_DHT_connect (c, 64);
7899 if (dht_handle == NULL)
7904 stats = GNUNET_STATISTICS_create ("mesh", c);
7908 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7910 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7911 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7912 peers = GNUNET_CONTAINER_multihashmap_create (32);
7913 applications = GNUNET_CONTAINER_multihashmap_create (32);
7914 types = GNUNET_CONTAINER_multihashmap_create (32);
7916 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7917 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7918 GNUNET_SERVER_disconnect_notify (server_handle,
7919 &handle_local_client_disconnect, NULL);
7923 clients_tail = NULL;
7926 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7927 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7929 /* Create a peer_info for the local peer */
7930 peer = peer_info_get (&my_full_id);
7933 GNUNET_PEER_change_rc (myid, 1);
7934 peer_info_add_path (peer, p, GNUNET_YES);
7936 /* Scheduled the task to clean up when shutdown is called */
7937 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7944 * The main function for the mesh service.
7946 * @param argc number of arguments from the command line
7947 * @param argv command line arguments
7948 * @return 0 ok, 1 on error
7951 main (int argc, char *const *argv)
7955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7958 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7964 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7965 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
7966 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);