2 This file is part of GNUnet.
3 (C) 2001 - 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
40 * - add vs create? change vs. keep-alive? same msg or different ones? -- thinking...
42 * - relay corking down to core
43 * - set ttl relative to tree depth
44 * - Add data ACK count in path ACK
45 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
51 #include "mesh_protocol.h"
52 #include "mesh_tunnel_tree.h"
53 #include "block_mesh.h"
54 #include "mesh_block_lib.h"
55 #include "gnunet_dht_service.h"
56 #include "gnunet_statistics_service.h"
57 #include "gnunet_regex_lib.h"
59 #define MESH_BLOOM_SIZE 128
61 #define MESH_DEBUG_DHT GNUNET_NO
62 #define MESH_DEBUG_CONNECTION GNUNET_NO
63 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_YES
65 #if MESH_DEBUG_CONNECTION
66 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
68 #define DEBUG_CONN(...)
72 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
74 #define DEBUG_DHT(...)
81 struct timespec __mesh_start;
82 struct timespec __mesh_end;
83 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
84 #define INTERVAL_END \
86 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
87 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
88 if (__diff < 0) __diff += 1000000000;\
92 #define INTERVAL_SHOW \
94 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
96 #define INTERVAL_START
101 /******************************************************************************/
102 /************************ DATA STRUCTURES ****************************/
103 /******************************************************************************/
105 /** FWD declaration */
111 * Struct representing a piece of data being sent to other peers
115 /** Tunnel it belongs to. */
116 struct MeshTunnel *t;
118 /** In case of a multicast, task to allow a client to send more data if
119 * some neighbor is too slow. */
120 GNUNET_SCHEDULER_TaskIdentifier *task;
122 /** How many remaining neighbors we need to send this to. */
123 unsigned int *reference_counter;
125 /** Size of the data. */
134 * Struct containing info about a queued transmission to this peer
141 struct MeshPeerQueue *next;
146 struct MeshPeerQueue *prev;
149 * Peer this transmission is directed to.
151 struct MeshPeerInfo *peer;
154 * Tunnel this message belongs to.
156 struct MeshTunnel *tunnel;
159 * Pointer to info stucture used as cls.
169 * Size of the message
176 * Struct containing all info possibly needed to build a package when called
179 struct MeshTransmissionDescriptor
181 /** ID of the tunnel this packet travels in */
182 struct MESH_TunnelID *origin;
184 /** Who was this message being sent to */
185 struct MeshPeerInfo *peer;
187 /** Ultimate destination of the packet */
188 GNUNET_PEER_Id destination;
190 /** Data descriptor */
191 struct MeshData* mesh_data;
196 * Struct containing all information regarding a given peer
206 * Last time we heard from this peer
208 struct GNUNET_TIME_Absolute last_contact;
211 * Number of attempts to reconnect so far
213 int n_reconnect_attempts;
216 * Paths to reach the peer, ordered by ascending hop count
218 struct MeshPeerPath *path_head;
221 * Paths to reach the peer, ordered by ascending hop count
223 struct MeshPeerPath *path_tail;
226 * Handle to stop the DHT search for a path to this peer
228 struct GNUNET_DHT_GetHandle *dhtget;
231 * Closure given to the DHT GET
233 struct MeshPathInfo *dhtgetcls;
236 * Array of tunnels this peer participates in
237 * (most probably a small amount, therefore not a hashmap)
238 * When the path to the peer changes, notify these tunnels to let them
239 * re-adjust their path trees.
241 struct MeshTunnel **tunnels;
244 * Number of tunnels this peers participates in
246 unsigned int ntunnels;
249 * Transmission queue to core DLL head
251 struct MeshPeerQueue *queue_head;
254 * Transmission queue to core DLL tail
256 struct MeshPeerQueue *queue_tail;
259 * How many messages are in the queue to this peer.
261 unsigned int queue_n;
264 * Handle to for queued transmissions
266 struct GNUNET_CORE_TransmitHandle *core_transmit;
271 * Globally unique tunnel identification (owner + number)
272 * DO NOT USE OVER THE NETWORK
277 * Node that owns the tunnel
282 * Tunnel number to differentiate all the tunnels owned by the node oid
283 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
285 MESH_TunnelNumber tid;
290 * Struct containing all information regarding a tunnel
291 * For an intermediate node the improtant info used will be:
292 * - id Tunnel unique identification
293 * - paths[0] To know where to send it next
294 * - metainfo: ready, speeds, accounting
301 struct MESH_TunnelID id;
304 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
306 MESH_TunnelNumber local_tid;
309 * Local tunnel number for local destination clients (incoming number)
310 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
313 MESH_TunnelNumber local_tid_dest;
316 * Is the speed on the tunnel limited to the slowest peer?
321 * Is the tunnel bufferless (minimum latency)?
326 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
331 * Packet ID of the last bck packet sent (unique counter per hop).
336 * SKIP value for this tunnel.
341 * MeshTunnelChildInfo of all children, indexed by GNUNET_PEER_Id.
342 * Contains the Flow Control info: FWD ACK value received,
343 * last BCK ACK sent, PID and SKIP values.
345 struct GNUNET_CONTAINER_MultiHashMap *children_fc;
348 * Last ACK sent towards the origin (for traffic towards leaf node).
350 uint32_t last_fwd_ack;
353 * BCK ACK value received from the hop towards the owner of the tunnel,
354 * (previous node / owner): up to what message PID can we sent back to him.
359 * How many messages are in the forward queue (towards leaves).
361 unsigned int fwd_queue_n;
364 * How many messages do we accept in the forward queue.
366 unsigned int fwd_queue_max;
369 * How many messages are in the backward queue (towards origin).
371 unsigned int bck_queue_n;
374 * How many messages do we accept in the backward queue.
376 unsigned int bck_queue_max;
379 * Last time the tunnel was used
381 struct GNUNET_TIME_Absolute timestamp;
384 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
385 * containing peers added by id or by type, not intermediate peers.
387 struct GNUNET_CONTAINER_MultiHashMap *peers;
390 * Number of peers that are connected and potentially ready to receive data
392 unsigned int peers_ready;
395 * Number of peers that have been added to the tunnel
397 unsigned int peers_total;
400 * Client owner of the tunnel, if any
402 struct MeshClient *owner;
405 * Clients that have been informed about and want to stay in the tunnel.
407 struct MeshClient **clients;
410 * Flow control info for each client.
412 struct MeshTunnelClientInfo *clients_fc;
415 * Number of elements in clients/clients_fc
417 unsigned int nclients;
420 * Clients that have been informed but requested to leave the tunnel.
422 struct MeshClient **ignore;
425 * Number of elements in clients
427 unsigned int nignore;
432 GNUNET_PEER_Id *blacklisted;
435 * Number of elements in blacklisted
437 unsigned int nblacklisted;
440 * Bloomfilter (for peer identities) to stop circular routes
442 char bloomfilter[MESH_BLOOM_SIZE];
447 struct MeshTunnelTree *tree;
450 * Application type we are looking for in this tunnel
452 GNUNET_MESH_ApplicationType type;
455 * Used to search peers offering a service
457 struct GNUNET_DHT_GetHandle *dht_get_type;
460 * Initial context of the regex search for a connect_by_string
462 struct MeshRegexSearchContext *regex_ctx;
465 * Task to keep the used paths alive
467 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
470 * Task to destroy the tunnel after timeout
472 * FIXME: merge the two? a tunnel will have either
473 * a path refresh OR a timeout, never both!
475 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
478 * Flag to signal the destruction of the tunnel.
479 * If this is set GNUNET_YES the tunnel will be destroyed
480 * when the queue is empty.
487 * Info about a child node in a tunnel, needed to perform flow control.
489 struct MeshTunnelChildInfo
492 * ID of the child node.
507 * Maximum PID allowed (FWD ACK received).
512 * Last ACK sent to that child (BCK ACK).
517 * Circular buffer pointing to MeshPeerQueue elements.
518 * Size determined by the tunnel queue size.
520 struct MeshPeerQueue **send_buffer;
523 * Index of the oldest element in the send_buffer.
525 unsigned int send_buffer_start;
528 * How many elements are already in the buffer.
530 unsigned int send_buffer_n;
535 * Info about a leaf client of a tunnel, needed to perform flow control.
537 struct MeshTunnelClientInfo
540 * PID of the last packet sent to the client (FWD).
545 * PID of the last packet received from the client (BCK).
550 * Maximum PID allowed (FWD ACK received).
555 * Last ACK sent to that child (BCK ACK).
563 * Info collected during iteration of child nodes in order to get the ACK value
566 struct MeshTunnelChildIteratorContext
569 * Tunnel whose info is being collected.
571 struct MeshTunnel *t;
574 * Is this context initialized? Is the value in max_child_ack valid?
579 * Maximum child ACK so far.
581 uint32_t max_child_ack;
584 * Number of children nodes
586 unsigned int nchildren;
591 * Info needed to work with tunnel paths and peers
598 struct MeshTunnel *t;
601 * Neighbouring peer to whom we send the packet to
603 struct MeshPeerInfo *peer;
608 struct MeshPeerPath *path;
613 * Struct containing information about a client of the service
620 struct MeshClient *next;
625 struct MeshClient *prev;
628 * Tunnels that belong to this client, indexed by local id
630 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
633 * Tunnels this client has accepted, indexed by incoming local id
635 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
638 * Tunnels this client has rejected, indexed by incoming local id
640 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
642 * Handle to communicate with the client
644 struct GNUNET_SERVER_Client *handle;
647 * Applications that this client has claimed to provide
649 struct GNUNET_CONTAINER_MultiHashMap *apps;
652 * Messages that this client has declared interest in
654 struct GNUNET_CONTAINER_MultiHashMap *types;
657 * Whether the client is active or shutting down (don't send confirmations
658 * to a client that is shutting down.
663 * ID of the client, mainly for debug messages
668 * Regular expressions describing the services offered by this client.
670 char **regexes; // FIXME add timeout? API to remove a regex?
673 * Number of regular expressions in regexes.
675 unsigned int n_regex;
678 * Task to refresh all regular expresions in the DHT.
680 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
686 * Struct to keep information of searches of services described by a regex
687 * using a user-provided string service description.
689 struct MeshRegexSearchInfo
692 * Which tunnel is this for
694 struct MeshTunnel *t;
697 * User provided description of the searched service.
702 * Part of the description already consumed by the search.
709 struct GNUNET_CONTAINER_MultiHashMap *dht_get_handles;
712 * Results from running DHT GETs.
714 struct GNUNET_CONTAINER_MultiHashMap *dht_get_results;
717 * Contexts, for each running DHT GET. Free all on end of search.
719 struct MeshRegexSearchContext **contexts;
722 * Number of contexts (branches/steps in search).
724 unsigned int n_contexts;
727 * Peer that is connecting via connect_by_string. When connected, free ctx.
732 * Other peers that are found but not yet being connected to.
734 GNUNET_PEER_Id *peers;
737 * Number of elements in peers.
739 unsigned int n_peers;
742 * Next peer to try to connect to.
747 * Timeout for a connect attempt.
748 * When reached, try to connect to a different peer, if any. If not,
749 * try the same peer again.
751 GNUNET_SCHEDULER_TaskIdentifier timeout;
756 * Struct to keep state of running searches that have consumed a part of
759 struct MeshRegexSearchContext
762 * Part of the description already consumed by
763 * this particular search branch.
768 * Information about the search.
770 struct MeshRegexSearchInfo *info;
774 /******************************************************************************/
775 /************************ DEBUG FUNCTIONS ****************************/
776 /******************************************************************************/
780 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
781 * @param cls string to print
782 * @param success GNUNET_OK if the PUT was transmitted,
783 * GNUNET_NO on timeout,
784 * GNUNET_SYSERR on disconnect from service
785 * after the PUT message was transmitted
786 * (so we don't know if it was received or not)
791 mesh_debug (void *cls, int success)
795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
799 unsigned int debug_fwd_ack;
800 unsigned int debug_bck_ack;
804 /******************************************************************************/
805 /*********************** GLOBAL VARIABLES ****************************/
806 /******************************************************************************/
809 * Configuration parameters
811 static struct GNUNET_TIME_Relative refresh_path_time;
812 static struct GNUNET_TIME_Relative app_announce_time;
813 static struct GNUNET_TIME_Relative id_announce_time;
814 static struct GNUNET_TIME_Relative unacknowledged_wait_time;
815 static struct GNUNET_TIME_Relative connect_timeout;
816 static long long unsigned int default_ttl;
817 static long long unsigned int dht_replication_level;
818 static long long unsigned int max_tunnels;
819 static long long unsigned int max_msgs_queue;
822 * DLL with all the clients, head.
824 static struct MeshClient *clients;
827 * DLL with all the clients, tail.
829 static struct MeshClient *clients_tail;
832 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
834 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
837 * Number of tunnels known.
839 static unsigned long long n_tunnels;
842 * Tunnels incoming, indexed by MESH_TunnelNumber
843 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
845 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
848 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
850 static struct GNUNET_CONTAINER_MultiHashMap *peers;
853 * Handle to communicate with transport
855 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
858 * Handle to communicate with core.
860 static struct GNUNET_CORE_Handle *core_handle;
865 static struct GNUNET_DHT_Handle *dht_handle;
870 static struct GNUNET_SERVER_Handle *server_handle;
873 * Handle to the statistics service.
875 static struct GNUNET_STATISTICS_Handle *stats;
878 * Notification context, to send messages to local clients.
880 static struct GNUNET_SERVER_NotificationContext *nc;
883 * Local peer own ID (memory efficient handle).
885 static GNUNET_PEER_Id myid;
888 * Local peer own ID (full value).
890 static struct GNUNET_PeerIdentity my_full_id;
895 static struct GNUNET_CRYPTO_RsaPrivateKey *my_private_key;
900 static struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded my_public_key;
903 * Tunnel ID for the next created tunnel (global tunnel number).
905 static MESH_TunnelNumber next_tid;
908 * Tunnel ID for the next incoming tunnel (local tunnel number).
910 static MESH_TunnelNumber next_local_tid;
913 * All application types provided by this peer.
915 static struct GNUNET_CONTAINER_MultiHashMap *applications;
918 * All message types clients of this peer are interested in.
920 static struct GNUNET_CONTAINER_MultiHashMap *types;
923 * Task to periodically announce provided applications.
925 GNUNET_SCHEDULER_TaskIdentifier announce_applications_task;
928 * Task to periodically announce itself in the network.
930 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
933 * Next ID to assign to a client.
935 unsigned int next_client_id;
938 /******************************************************************************/
939 /*********************** DECLARATIONS **************************/
940 /******************************************************************************/
942 /* FIXME move declarations here */
945 * Function to process paths received for a new peer addition. The recorded
946 * paths form the initial tunnel, which can be optimized later.
947 * Called on each result obtained for the DHT search.
950 * @param exp when will this value expire
951 * @param key key of the result
952 * @param type type of the result
953 * @param size number of bytes in data
954 * @param data pointer to the result data
957 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
958 const struct GNUNET_HashCode * key,
959 const struct GNUNET_PeerIdentity *get_path,
960 unsigned int get_path_length,
961 const struct GNUNET_PeerIdentity *put_path,
962 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
963 size_t size, const void *data);
967 * Function to process DHT string to regex matching.
968 * Called on each result obtained for the DHT search.
970 * @param cls closure (search context)
971 * @param exp when will this value expire
972 * @param key key of the result
973 * @param get_path path of the get request (not used)
974 * @param get_path_length lenght of get_path (not used)
975 * @param put_path path of the put request (not used)
976 * @param put_path_length length of the put_path (not used)
977 * @param type type of the result
978 * @param size number of bytes in data
979 * @param data pointer to the result data
981 * TODO: re-issue the request after certain time? cancel after X results?
984 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
985 const struct GNUNET_HashCode * key,
986 const struct GNUNET_PeerIdentity *get_path,
987 unsigned int get_path_length,
988 const struct GNUNET_PeerIdentity *put_path,
989 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
990 size_t size, const void *data);
994 * Function to process DHT string to regex matching.
995 * Called on each result obtained for the DHT search.
997 * @param cls closure (search context)
998 * @param exp when will this value expire
999 * @param key key of the result
1000 * @param get_path path of the get request (not used)
1001 * @param get_path_length lenght of get_path (not used)
1002 * @param put_path path of the put request (not used)
1003 * @param put_path_length length of the put_path (not used)
1004 * @param type type of the result
1005 * @param size number of bytes in data
1006 * @param data pointer to the result data
1009 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
1010 const struct GNUNET_HashCode * key,
1011 const struct GNUNET_PeerIdentity *get_path,
1012 unsigned int get_path_length,
1013 const struct GNUNET_PeerIdentity *put_path,
1014 unsigned int put_path_length,
1015 enum GNUNET_BLOCK_Type type,
1016 size_t size, const void *data);
1020 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1021 * and insert it in the appropiate structures if the peer is not known yet.
1023 * @param peer Short identity of the peer.
1025 * @return Existing or newly created peer info.
1027 static struct MeshPeerInfo *
1028 peer_info_get_short (const GNUNET_PEER_Id peer);
1032 * Try to establish a new connection to this peer.
1033 * Use the best path for the given tunnel.
1034 * If the peer doesn't have any path to it yet, try to get one.
1035 * If the peer already has some path, send a CREATE PATH towards it.
1037 * @param peer PeerInfo of the peer.
1038 * @param t Tunnel for which to create the path, if possible.
1041 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1045 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1046 * needed rescources.
1047 * If peer already exists, reevaluate shortest path and change if different.
1049 * @param t Tunnel we want to add a new peer to
1050 * @param peer PeerInfo of the peer being added
1054 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1058 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1059 * that are no longer needed, as well as nodes of no longer reachable peers.
1060 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1062 * @param t Tunnel from which to remove the path.
1063 * @param peer Short id of the peer which should be removed.
1066 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1070 * Search for a tunnel by global ID using full PeerIdentities.
1072 * @param oid owner of the tunnel.
1073 * @param tid global tunnel number.
1075 * @return tunnel handler, NULL if doesn't exist.
1077 static struct MeshTunnel *
1078 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1082 * Delete an active client from the tunnel.
1088 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1091 * Notify a tunnel that a connection has broken that affects at least
1092 * some of its peers.
1094 * @param t Tunnel affected.
1095 * @param p1 Peer that got disconnected from p2.
1096 * @param p2 Peer that got disconnected from p1.
1098 * @return Short ID of the peer disconnected (either p1 or p2).
1099 * 0 if the tunnel remained unaffected.
1101 static GNUNET_PEER_Id
1102 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1107 * Get the current ack value for a tunnel, for data going from root to leaves,
1108 * taking in account the tunnel mode and the status of all children and clients.
1112 * @return Maximum PID allowed.
1115 tunnel_get_fwd_ack (struct MeshTunnel *t);
1119 * Add a client to a tunnel, initializing all needed data structures.
1121 * @param t Tunnel to which add the client.
1122 * @param c Client which to add to the tunnel.
1125 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1129 * Iterator over edges in a regex block retrieved from the DHT.
1131 * @param cls Closure.
1132 * @param token Token that follows to next state.
1133 * @param len Lenght of token.
1134 * @param key Hash of next state.
1136 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1139 regex_edge_iterator (void *cls,
1142 const struct GNUNET_HashCode *key);
1146 * Find a path to a peer that offers a regex servcie compatible
1147 * with a given string.
1149 * @param key The key of the accepting state.
1150 * @param ctx Context containing info about the string, tunnel, etc.
1153 regex_find_path (const struct GNUNET_HashCode *key,
1154 struct MeshRegexSearchContext *ctx);
1158 * Queue and pass message to core when possible.
1160 * @param cls Closure (type dependant).
1161 * @param type Type of the message, 0 for a raw message.
1162 * @param size Size of the message.
1163 * @param dst Neighbor to send message to.
1164 * @param t Tunnel this message belongs to.
1167 queue_add (void *cls, uint16_t type, size_t size,
1168 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1172 * Free a transmission that was already queued with all resources
1173 * associated to the request.
1175 * @param queue Queue handler to cancel.
1176 * @param clear_cls Is it necessary to free associated cls?
1179 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1183 * @brief Get the next transmittable message from the queue.
1185 * This will be the head, except in the case of being a data packet
1186 * not allowed by the destination peer.
1188 * @param peer Destination peer.
1190 * @return The next viable MeshPeerQueue element to send to that peer.
1191 * NULL when there are no transmittable messages.
1193 struct MeshPeerQueue *
1194 queue_get_next (const struct MeshPeerInfo *peer);
1198 * Core callback to write a queued packet to core buffer
1200 * @param cls Closure (peer info).
1201 * @param size Number of bytes available in buf.
1202 * @param buf Where the to write the message.
1204 * @return number of bytes written to buf
1207 queue_send (void *cls, size_t size, void *buf);
1209 /******************************************************************************/
1210 /************************ ITERATORS ****************************/
1211 /******************************************************************************/
1214 * Iterator over found existing mesh regex blocks that match an ongoing search.
1216 * @param cls closure
1217 * @param key current key code
1218 * @param value value in the hash map
1219 * @return GNUNET_YES if we should continue to iterate,
1223 regex_result_iterator (void *cls,
1224 const struct GNUNET_HashCode * key,
1227 struct MeshRegexBlock *block = value;
1228 struct MeshRegexSearchContext *ctx = cls;
1230 if (GNUNET_YES == ntohl(block->accepting) &&
1231 ctx->position == strlen (ctx->info->description))
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Found accepting known block\n");
1234 regex_find_path (key, ctx);
1235 return GNUNET_YES; // We found an accept state!
1239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* %u, %u, [%u]\n",
1240 ctx->position, strlen(ctx->info->description),
1241 ntohl(block->accepting));
1244 (void) GNUNET_MESH_regex_block_iterate (block, SIZE_MAX,
1245 ®ex_edge_iterator, ctx);
1252 * Iterator over edges in a regex block retrieved from the DHT.
1254 * @param cls Closure (context of the search).
1255 * @param token Token that follows to next state.
1256 * @param len Lenght of token.
1257 * @param key Hash of next state.
1259 * @return GNUNET_YES if should keep iterating, GNUNET_NO otherwise.
1262 regex_edge_iterator (void *cls,
1265 const struct GNUNET_HashCode *key)
1267 struct MeshRegexSearchContext *ctx = cls;
1268 struct MeshRegexSearchContext *new_ctx;
1269 struct MeshRegexSearchInfo *info = ctx->info;
1270 struct GNUNET_DHT_GetHandle *get_h;
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Start of regex edge iterator\n");
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* descr : %s\n", info->description);
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* posit : %u\n", ctx->position);
1277 current = &info->description[ctx->position];
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* currt : %s\n", current);
1279 current_len = strlen (info->description) - ctx->position;
1280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* ctlen : %u\n", current_len);
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tklen : %u\n", len);
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* tk[0] : %c\n", token[0]);
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* nextk : %s\n", GNUNET_h2s(key));
1284 if (len > current_len)
1286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token too long, END\n");
1287 return GNUNET_YES; // Token too long, wont match
1289 if (0 != strncmp (current, token, len))
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* Token doesn't match, END\n");
1292 return GNUNET_YES; // Token doesn't match
1294 new_ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
1295 new_ctx->info = info;
1296 new_ctx->position = ctx->position + len;
1297 GNUNET_array_append (info->contexts, info->n_contexts, new_ctx);
1299 GNUNET_CONTAINER_multihashmap_contains(info->dht_get_handles, key))
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* GET running, END\n");
1302 GNUNET_CONTAINER_multihashmap_get_multiple (info->dht_get_results, key,
1303 ®ex_result_iterator,
1305 return GNUNET_YES; // We are already looking for it
1307 /* Start search in DHT */
1309 GNUNET_DHT_get_start (dht_handle, /* handle */
1310 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
1311 key, /* key to search */
1312 dht_replication_level, /* replication level */
1313 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1314 NULL, /* xquery */ // FIXME BLOOMFILTER
1315 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1316 &dht_get_string_handler, new_ctx);
1318 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles, key, get_h,
1319 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "* End of regex edge iterator\n");
1329 * Iterator over hash map entries to cancel DHT GET requests after a
1330 * successful connect_by_string.
1332 * @param cls Closure (unused).
1333 * @param key Current key code (unused).
1334 * @param value Value in the hash map (get handle).
1335 * @return GNUNET_YES if we should continue to iterate,
1339 regex_cancel_dht_get (void *cls,
1340 const struct GNUNET_HashCode * key,
1343 struct GNUNET_DHT_GetHandle *h = value;
1345 GNUNET_DHT_get_stop (h);
1351 * Iterator over hash map entries to free MeshRegexBlocks stored during the
1352 * search for connect_by_string.
1354 * @param cls Closure (unused).
1355 * @param key Current key code (unused).
1356 * @param value MeshRegexBlock in the hash map.
1357 * @return GNUNET_YES if we should continue to iterate,
1361 regex_free_result (void *cls,
1362 const struct GNUNET_HashCode * key,
1366 GNUNET_free (value);
1372 * Regex callback iterator to store own service description in the DHT.
1374 * @param cls closure.
1375 * @param key hash for current state.
1376 * @param proof proof for current state.
1377 * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
1378 * @param num_edges number of edges leaving current state.
1379 * @param edges edges leaving current state.
1382 regex_iterator (void *cls, const struct GNUNET_HashCode *key, const char *proof,
1383 int accepting, unsigned int num_edges,
1384 const struct GNUNET_REGEX_Edge *edges)
1386 struct MeshRegexBlock *block;
1387 struct MeshRegexEdge *block_edge;
1388 enum GNUNET_DHT_RouteOption opt;
1392 unsigned int offset;
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 " regex dht put for state %s\n",
1398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 opt = GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE;
1406 if (GNUNET_YES == accepting)
1408 struct MeshRegexAccept block;
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 " state %s is accepting, putting own id\n",
1413 size = sizeof (block);
1415 block.id = my_full_id;
1417 GNUNET_DHT_put(dht_handle, key,
1418 dht_replication_level,
1419 opt | GNUNET_DHT_RO_RECORD_ROUTE,
1420 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT,
1423 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1428 len = strlen(proof);
1429 size = sizeof (struct MeshRegexBlock) + len;
1430 block = GNUNET_malloc (size);
1433 block->n_proof = htonl (len);
1434 block->n_edges = htonl (num_edges);
1435 block->accepting = htonl (accepting);
1437 /* Store the proof at the end of the block. */
1438 aux = (char *) &block[1];
1439 memcpy (aux, proof, len);
1442 /* Store each edge in a variable length MeshEdge struct at the
1443 * very end of the MeshRegexBlock structure.
1445 for (i = 0; i < num_edges; i++)
1447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1448 " edge %s towards %s\n",
1450 GNUNET_h2s(&edges[i].destination));
1452 /* aux points at the end of the last block */
1453 len = strlen (edges[i].label);
1454 size += sizeof (struct MeshRegexEdge) + len;
1455 // Calculate offset FIXME is this ok? use size instead?
1456 offset = aux - (char *) block;
1457 block = GNUNET_realloc (block, size);
1458 aux = &((char *) block)[offset];
1459 block_edge = (struct MeshRegexEdge *) aux;
1460 block_edge->key = edges[i].destination;
1461 block_edge->n_token = htonl (len);
1462 aux = (char *) &block_edge[1];
1463 memcpy (aux, edges[i].label, len);
1467 GNUNET_DHT_put(dht_handle, key,
1468 dht_replication_level,
1470 GNUNET_BLOCK_TYPE_MESH_REGEX, size,
1472 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1476 GNUNET_free (block);
1481 * Store the regular expression describing a local service into the DHT.
1483 * @param regex The regular expresion.
1486 regex_put (const char *regex)
1488 struct GNUNET_REGEX_Automaton *dfa;
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) start\n", regex);
1491 dfa = GNUNET_REGEX_construct_dfa (regex, strlen(regex));
1492 GNUNET_REGEX_iterate_all_edges (dfa, ®ex_iterator, NULL);
1493 GNUNET_REGEX_automaton_destroy (dfa);
1494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "regex_put (%s) end\n", regex);
1499 * Find a path to a peer that offers a regex servcie compatible
1500 * with a given string.
1502 * @param key The key of the accepting state.
1503 * @param ctx Context containing info about the string, tunnel, etc.
1506 regex_find_path (const struct GNUNET_HashCode *key,
1507 struct MeshRegexSearchContext *ctx)
1509 struct GNUNET_DHT_GetHandle *get_h;
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Found peer by service\n");
1512 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
1513 GNUNET_BLOCK_TYPE_MESH_REGEX_ACCEPT, /* type */
1514 key, /* key to search */
1515 dht_replication_level, /* replication level */
1516 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE |
1517 GNUNET_DHT_RO_RECORD_ROUTE,
1518 NULL, /* xquery */ // FIXME BLOOMFILTER
1519 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
1520 &dht_get_string_accept_handler, ctx);
1521 GNUNET_break (GNUNET_OK ==
1522 GNUNET_CONTAINER_multihashmap_put(ctx->info->dht_get_handles,
1525 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1530 * Function called if the connect attempt to a peer found via
1531 * connect_by_string times out. Try to connect to another peer, if any.
1532 * Otherwise try to reconnect to the same peer.
1534 * @param cls Closure (info about regex search).
1535 * @param tc TaskContext.
1538 regex_connect_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1540 struct MeshRegexSearchInfo *info = cls;
1541 struct MeshPeerInfo *peer_info;
1545 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout\n");
1546 info->timeout = GNUNET_SCHEDULER_NO_TASK;
1547 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " timed out: %u\n", old);
1555 if (0 < info->n_peers)
1557 // Select next peer, put current in that spot.
1558 id = info->peers[info->i_peer];
1559 info->peers[info->i_peer] = info->peer;
1560 info->i_peer = (info->i_peer + 1) % info->n_peers;
1564 // Try to connect to same peer again.
1567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " trying: %u\n", id);
1569 peer_info = peer_info_get_short(id);
1570 tunnel_add_peer (info->t, peer_info);
1572 tunnel_delete_peer (info->t, old);
1573 peer_info_connect (peer_info, info->t);
1574 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
1575 ®ex_connect_timeout,
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Regex connect timeout END\n");
1582 * Cancel an ongoing regex search in the DHT and free all resources.
1584 * @param ctx The search context.
1587 regex_cancel_search(struct MeshRegexSearchContext *ctx)
1589 struct MeshRegexSearchInfo *info = ctx->info;
1592 GNUNET_free (info->description);
1593 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_handles,
1594 ®ex_cancel_dht_get, NULL);
1595 GNUNET_CONTAINER_multihashmap_iterate (info->dht_get_results,
1596 ®ex_free_result, NULL);
1597 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_results);
1598 GNUNET_CONTAINER_multihashmap_destroy (info->dht_get_handles);
1599 info->t->regex_ctx = NULL;
1600 for (i = 0; i < info->n_contexts; i++)
1602 GNUNET_free (info->contexts[i]);
1604 if (0 < info->n_contexts)
1605 GNUNET_free (info->contexts);
1606 if (0 < info->n_peers)
1607 GNUNET_free (info->peers);
1608 if (GNUNET_SCHEDULER_NO_TASK != info->timeout)
1610 GNUNET_SCHEDULER_cancel(info->timeout);
1616 /******************************************************************************/
1617 /************************ PERIODIC FUNCTIONS ****************************/
1618 /******************************************************************************/
1621 * Announce iterator over for each application provided by the peer
1623 * @param cls closure
1624 * @param key current key code
1625 * @param value value in the hash map
1626 * @return GNUNET_YES if we should continue to
1631 announce_application (void *cls, const struct GNUNET_HashCode * key, void *value)
1633 struct PBlock block;
1634 struct MeshClient *c;
1636 block.id = my_full_id;
1637 c = GNUNET_CONTAINER_multihashmap_get (applications, key);
1638 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1639 if (0 == block.type)
1644 block.type = htonl (block.type);
1646 GNUNET_break (NULL !=
1647 GNUNET_DHT_put (dht_handle, key,
1648 dht_replication_level,
1649 GNUNET_DHT_RO_RECORD_ROUTE |
1650 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1651 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1653 (const char *) &block,
1654 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1656 app_announce_time, NULL, NULL));
1662 * Periodically announce what applications are provided by local clients
1665 * @param cls closure
1666 * @param tc task context
1669 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1671 struct MeshClient *c = cls;
1674 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1676 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1680 DEBUG_DHT ("Starting PUT for regex\n");
1682 for (i = 0; i < c->n_regex; i++)
1684 regex_put (c->regexes[i]);
1686 c->regex_announce_task =
1687 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1688 DEBUG_DHT ("Finished PUT for regex\n");
1695 * Periodically announce what applications are provided by local clients
1698 * @param cls closure
1699 * @param tc task context
1702 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1704 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1706 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1710 DEBUG_DHT ("Starting PUT for apps\n");
1712 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1714 announce_applications_task =
1715 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1717 DEBUG_DHT ("Finished PUT for apps\n");
1724 * Periodically announce self id in the DHT
1726 * @param cls closure
1727 * @param tc task context
1730 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1732 struct PBlock block;
1734 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1736 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1740 * - Set data expiration in function of X
1741 * - Adapt X to churn
1743 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1745 block.id = my_full_id;
1746 block.type = htonl (0);
1747 GNUNET_DHT_put (dht_handle, /* DHT handle */
1748 &my_full_id.hashPubKey, /* Key to use */
1749 dht_replication_level, /* Replication level */
1750 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1751 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1752 sizeof (block), /* Size of the data */
1753 (const char *) &block, /* Data itself */
1754 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1755 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1756 NULL, /* Continuation */
1757 NULL); /* Continuation closure */
1759 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1763 /******************************************************************************/
1764 /****************** GENERAL HELPER FUNCTIONS ************************/
1765 /******************************************************************************/
1768 * Decrements the reference counter and frees all resources if needed
1770 * @param mesh_data Data Descriptor used in a multicast message.
1771 * Freed no longer needed (last message).
1774 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1776 /* Make sure it's a multicast packet */
1777 GNUNET_assert (NULL != mesh_data->reference_counter);
1779 if (0 == --(*(mesh_data->reference_counter)))
1781 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1782 if (NULL != mesh_data->task)
1784 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1786 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1788 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1790 GNUNET_free (mesh_data->task);
1792 GNUNET_free (mesh_data->reference_counter);
1793 GNUNET_free (mesh_data->data);
1794 GNUNET_free (mesh_data);
1800 * Check if client has registered with the service and has not disconnected
1802 * @param client the client to check
1804 * @return non-NULL if client exists in the global DLL
1806 static struct MeshClient *
1807 client_get (struct GNUNET_SERVER_Client *client)
1809 struct MeshClient *c;
1814 if (c->handle == client)
1823 * Checks if a given client has subscribed to certain message type
1825 * @param message_type Type of message to check
1826 * @param c Client to check
1828 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1830 * FIXME: use of crypto_hash slows it down
1831 * The hash function alone takes 8-10us out of the ~55us for the whole
1832 * process of retransmitting the message from one local client to another.
1833 * Find faster implementation!
1836 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1838 struct GNUNET_HashCode hc;
1840 if (NULL == c->types)
1843 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1844 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1849 * Allow a client to send more data after transmitting a multicast message
1850 * which some neighbor has not yet accepted altough a reasonable time has
1853 * @param cls Closure (DataDescriptor containing the task identifier)
1854 * @param tc Task Context
1856 * FIXME reference counter cshould be just int
1859 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1861 struct MeshData *mdata = cls;
1863 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1865 GNUNET_assert (NULL != mdata->reference_counter);
1866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1867 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1868 *(mdata->reference_counter));
1869 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1870 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1875 * Check whether client wants traffic from a tunnel.
1877 * @param c Client to check.
1878 * @param t Tunnel to be found.
1880 * @return GNUNET_YES if client knows tunnel.
1882 * TODO look in client hashmap
1885 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1889 for (i = 0; i < t->nclients; i++)
1890 if (t->clients[i] == c)
1897 * Check whether client has been informed about a tunnel.
1899 * @param c Client to check.
1900 * @param t Tunnel to be found.
1902 * @return GNUNET_YES if client knows tunnel.
1904 * TODO look in client hashmap
1907 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1911 for (i = 0; i < t->nignore; i++)
1912 if (t->ignore[i] == c)
1914 return client_wants_tunnel(c, t);
1919 * Marks a client as uninterested in traffic from the tunnel, updating both
1920 * client and tunnel to reflect this.
1922 * @param c Client that doesn't want traffic anymore.
1923 * @param t Tunnel which should be ignored.
1925 * FIXME when to delete an incoming tunnel?
1928 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1930 struct GNUNET_HashCode hash;
1932 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1933 GNUNET_break (GNUNET_YES ==
1934 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1936 GNUNET_break (GNUNET_YES ==
1937 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1938 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1939 tunnel_delete_active_client (t, c);
1940 GNUNET_array_append (t->ignore, t->nignore, c);
1945 * Deletes a tunnel from a client (either owner or destination). To be used on
1946 * tunnel destroy, otherwise, use client_ignore_tunnel.
1948 * @param c Client whose tunnel to delete.
1949 * @param t Tunnel which should be deleted.
1952 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1954 struct GNUNET_HashCode hash;
1958 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1959 GNUNET_assert (GNUNET_YES ==
1960 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1966 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1968 GNUNET_assert (GNUNET_YES ==
1969 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1973 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1981 * Send the message to all clients that have subscribed to its type
1983 * @param msg Pointer to the message itself
1984 * @param payload Pointer to the payload of the message.
1985 * @param t The tunnel to whose clients this message goes.
1987 * @return number of clients this message was sent to
1990 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1991 const struct GNUNET_MessageHeader *payload,
1992 struct MeshTunnel *t)
1994 struct MeshClient *c;
1995 MESH_TunnelNumber *tid;
1998 char cbuf[htons (msg->size)];
2000 type = ntohs (payload->type);
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2003 GNUNET_MESH_DEBUG_M2S (type));
2005 memcpy (cbuf, msg, sizeof (cbuf));
2006 switch (htons (msg->type))
2008 struct GNUNET_MESH_Unicast *uc;
2009 struct GNUNET_MESH_Multicast *mc;
2010 struct GNUNET_MESH_ToOrigin *to;
2012 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2013 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2016 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2017 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2020 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2021 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2029 for (count = 0, c = clients; c != NULL; c = c->next)
2031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2032 if (client_is_subscribed (type, c))
2034 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2038 *tid = htonl (t->local_tid);
2042 if (GNUNET_NO == client_knows_tunnel (c, t))
2044 /* This client doesn't know the tunnel */
2045 struct GNUNET_MESH_TunnelNotification tmsg;
2046 struct GNUNET_HashCode hash;
2048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2049 tmsg.header.size = htons (sizeof (tmsg));
2050 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2051 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2052 tmsg.tunnel_id = htonl (t->local_tid_dest);
2053 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2054 &tmsg.header, GNUNET_NO);
2055 tunnel_add_client (t, c);
2056 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2058 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2059 c->incoming_tunnels, &hash, t,
2060 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2062 *tid = htonl (t->local_tid_dest);
2065 /* Check if the client wants to get traffic from the tunnel */
2066 if (GNUNET_NO == client_wants_tunnel(c, t))
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2070 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2071 (struct GNUNET_MessageHeader
2072 *) cbuf, GNUNET_NO);
2081 * Notify the client that owns the tunnel that a peer has connected to it
2082 * (the requested path to it has been confirmed).
2084 * @param t Tunnel whose owner to notify
2085 * @param id Short id of the peer that has connected
2088 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2090 struct GNUNET_MESH_PeerControl pc;
2092 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2093 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2094 pc.tunnel_id = htonl (t->local_tid);
2095 GNUNET_PEER_resolve (id, &pc.peer);
2096 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2102 * Notify all clients (not depending on registration status) that the incoming
2103 * tunnel is no longer valid.
2105 * @param t Tunnel that was destroyed.
2108 send_clients_tunnel_destroy (struct MeshTunnel *t)
2110 struct GNUNET_MESH_TunnelMessage msg;
2112 msg.header.size = htons (sizeof (msg));
2113 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2114 msg.tunnel_id = htonl (t->local_tid_dest);
2115 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2120 * Notify clients of tunnel disconnections, if needed.
2121 * In case the origin disconnects, the destination clients get a tunnel destroy
2122 * notification. If the last destination disconnects (only one remaining client
2123 * in tunnel), the origin gets a (local ID) peer disconnected.
2124 * Note that the function must be called BEFORE removing the client from
2127 * @param t Tunnel that was destroyed.
2128 * @param c Client that disconnected.
2131 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2137 struct GNUNET_MESH_TunnelMessage msg;
2139 msg.header.size = htons (sizeof (msg));
2140 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2141 msg.tunnel_id = htonl (t->local_tid_dest);
2142 for (i = 0; i < t->nclients; i++)
2143 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2144 &msg.header, GNUNET_NO);
2146 // FIXME when to disconnect an incoming tunnel?
2147 else if (1 == t->nclients && NULL != t->owner)
2149 struct GNUNET_MESH_PeerControl msg;
2151 msg.header.size = htons (sizeof (msg));
2152 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2153 msg.tunnel_id = htonl (t->local_tid);
2154 msg.peer = my_full_id;
2155 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2156 &msg.header, GNUNET_NO);
2162 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2163 * and insert it in the appropiate structures if the peer is not known yet.
2165 * @param peer Full identity of the peer.
2167 * @return Existing or newly created peer info.
2169 static struct MeshPeerInfo *
2170 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2172 struct MeshPeerInfo *peer_info;
2174 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2175 if (NULL == peer_info)
2178 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2179 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2180 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2181 peer_info->id = GNUNET_PEER_intern (peer);
2189 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2190 * and insert it in the appropiate structures if the peer is not known yet.
2192 * @param peer Short identity of the peer.
2194 * @return Existing or newly created peer info.
2196 static struct MeshPeerInfo *
2197 peer_info_get_short (const GNUNET_PEER_Id peer)
2199 struct GNUNET_PeerIdentity id;
2201 GNUNET_PEER_resolve (peer, &id);
2202 return peer_info_get (&id);
2207 * Iterator to remove the tunnel from the list of tunnels a peer participates
2210 * @param cls Closure (tunnel info)
2211 * @param key GNUNET_PeerIdentity of the peer (unused)
2212 * @param value PeerInfo of the peer
2214 * @return always GNUNET_YES, to keep iterating
2217 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2219 struct MeshTunnel *t = cls;
2220 struct MeshPeerInfo *peer = value;
2223 for (i = 0; i < peer->ntunnels; i++)
2226 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2229 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2230 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2239 * Core callback to write a pre-constructed data packet to core buffer
2241 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2242 * @param size Number of bytes available in buf.
2243 * @param buf Where the to write the message.
2245 * @return number of bytes written to buf
2248 send_core_data_raw (void *cls, size_t size, void *buf)
2250 struct MeshTransmissionDescriptor *info = cls;
2251 struct GNUNET_MessageHeader *msg;
2254 GNUNET_assert (NULL != info);
2255 GNUNET_assert (NULL != info->mesh_data);
2256 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2257 total_size = ntohs (msg->size);
2259 if (total_size > size)
2264 memcpy (buf, msg, total_size);
2265 data_descriptor_decrement_rc (info->mesh_data);
2272 * Sends an already built non-multicast message to a peer,
2273 * properly registrating all used resources.
2275 * @param message Message to send. Function makes a copy of it.
2276 * @param peer Short ID of the neighbor whom to send the message.
2277 * @param t Tunnel on which this message is transmitted.
2280 send_message (const struct GNUNET_MessageHeader *message,
2281 const struct GNUNET_PeerIdentity *peer,
2282 struct MeshTunnel *t)
2284 struct MeshTransmissionDescriptor *info;
2285 struct MeshPeerInfo *neighbor;
2286 struct MeshPeerPath *p;
2290 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2292 size = ntohs (message->size);
2293 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2294 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2295 info->mesh_data->data = GNUNET_malloc (size);
2296 memcpy (info->mesh_data->data, message, size);
2297 type = ntohs(message->type);
2298 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2300 struct GNUNET_MESH_Unicast *m;
2302 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2303 m->ttl = htonl (ntohl (m->ttl) - 1);
2305 info->mesh_data->data_len = size;
2306 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2307 *info->mesh_data->reference_counter = 1;
2308 neighbor = peer_info_get (peer);
2309 for (p = neighbor->path_head; NULL != p; p = p->next)
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320 " %s IS NOT DIRECTLY CONNECTED\n",
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 for (p = neighbor->path_head; NULL != p; p = p->next)
2327 struct GNUNET_PeerIdentity debug_id;
2330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 " path with %u hops through:\n",
2333 for (i = 0; i < p->length; i++)
2335 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2338 i, GNUNET_i2s(&debug_id));
2342 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2343 GNUNET_free (info->mesh_data->data);
2344 GNUNET_free (info->mesh_data);
2348 info->peer = neighbor;
2349 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2352 * TODO: in this case we only need the service to retransmit
2353 * the message down the path. If we pass the real type to queue_add,
2354 * queue_send will try to build the message from scratch. This can
2355 * probably be done by some other way instead of deleteing the type
2369 * Sends a CREATE PATH message for a path to a peer, properly registrating
2370 * all used resources.
2372 * @param peer PeerInfo of the final peer for whom this path is being created.
2373 * @param p Path itself.
2374 * @param t Tunnel for which the path is created.
2377 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2378 struct MeshTunnel *t)
2380 struct GNUNET_PeerIdentity id;
2381 struct MeshPathInfo *path_info;
2382 struct MeshPeerInfo *neighbor;
2388 p = tree_get_path_to_peer (t->tree, peer->id);
2395 for (i = 0; i < p->length; i++)
2397 if (p->peers[i] == myid)
2400 if (i >= p->length - 1)
2406 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2408 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2409 path_info->path = p;
2411 neighbor = peer_info_get (&id);
2412 path_info->peer = neighbor;
2413 queue_add (path_info,
2414 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2415 sizeof (struct GNUNET_MESH_ManipulatePath) +
2416 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2423 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2425 * @param t Tunnel whose path to destroy.
2426 * @param destination Short ID of the peer to whom the path to destroy.
2429 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2431 struct MeshPeerPath *p;
2434 p = tree_get_path_to_peer (t->tree, destination);
2440 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2441 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2443 struct GNUNET_MESH_ManipulatePath *msg;
2444 struct GNUNET_PeerIdentity *pi;
2448 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2449 msg->header.size = htons (size);
2450 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2451 msg->tid = htonl (t->id.tid);
2452 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2453 for (i = 0; i < p->length; i++)
2455 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2457 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2464 * Try to establish a new connection to this peer.
2465 * Use the best path for the given tunnel.
2466 * If the peer doesn't have any path to it yet, try to get one.
2467 * If the peer already has some path, send a CREATE PATH towards it.
2469 * @param peer PeerInfo of the peer.
2470 * @param t Tunnel for which to create the path, if possible.
2473 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2475 struct MeshPeerPath *p;
2476 struct MeshPathInfo *path_info;
2478 if (NULL != peer->path_head)
2480 p = tree_get_path_to_peer (t->tree, peer->id);
2487 // FIXME always send create path to self
2490 send_create_path (peer, p, t);
2494 struct GNUNET_HashCode hash;
2497 send_client_peer_connected (t, myid);
2498 t->local_tid_dest = next_local_tid++;
2499 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2502 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2503 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2510 else if (NULL == peer->dhtget)
2512 struct GNUNET_PeerIdentity id;
2514 GNUNET_PEER_resolve (peer->id, &id);
2515 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2516 path_info->peer = peer;
2518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2519 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2520 peer->dhtgetcls = path_info;
2521 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2522 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2523 &id.hashPubKey, /* key to search */
2524 dht_replication_level, /* replication level */
2525 GNUNET_DHT_RO_RECORD_ROUTE |
2526 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2527 NULL, /* xquery */ // FIXME BLOOMFILTER
2528 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2529 &dht_get_id_handler, path_info);
2531 /* Otherwise, there is no path but the DHT get is already started. */
2536 * Task to delay the connection of a peer
2538 * @param cls Closure (path info with tunnel and peer to connect).
2539 * Will be free'd on exection.
2540 * @param tc TaskContext
2543 peer_info_connect_task (void *cls,
2544 const struct GNUNET_SCHEDULER_TaskContext *tc)
2546 struct MeshPathInfo *path_info = cls;
2548 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2553 peer_info_connect (path_info->peer, path_info->t);
2559 * Destroy the peer_info and free any allocated resources linked to it
2561 * @param pi The peer_info to destroy.
2563 * @return GNUNET_OK on success
2566 peer_info_destroy (struct MeshPeerInfo *pi)
2568 struct GNUNET_PeerIdentity id;
2569 struct MeshPeerPath *p;
2570 struct MeshPeerPath *nextp;
2572 GNUNET_PEER_resolve (pi->id, &id);
2573 GNUNET_PEER_change_rc (pi->id, -1);
2576 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2579 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2580 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2582 if (NULL != pi->dhtget)
2584 GNUNET_DHT_get_stop (pi->dhtget);
2585 GNUNET_free (pi->dhtgetcls);
2591 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2601 * Remove all paths that rely on a direct connection between p1 and p2
2602 * from the peer itself and notify all tunnels about it.
2604 * @param peer PeerInfo of affected peer.
2605 * @param p1 GNUNET_PEER_Id of one peer.
2606 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2609 * TODO: optimize (see below)
2612 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2615 struct MeshPeerPath *p;
2616 struct MeshPeerPath *aux;
2617 struct MeshPeerInfo *peer_d;
2619 unsigned int destroyed;
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2626 p = peer->path_head;
2630 for (i = 0; i < (p->length - 1); i++)
2632 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2633 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2635 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2646 for (i = 0; i < peer->ntunnels; i++)
2648 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2652 * Problem: one or more peers have been deleted from the tunnel tree.
2653 * We don't know who they are to try to add them again.
2654 * We need to try to find a new path for each of the disconnected peers.
2655 * Some of them might already have a path to reach them that does not
2656 * involve p1 and p2. Adding all anew might render in a better tree than
2657 * the trivial immediate fix.
2659 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2660 * its children will be reachable trough him.
2662 peer_d = peer_info_get_short (d);
2665 for (p = peer_d->path_head; NULL != p; p = p->next)
2667 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2675 /* No callback, as peer will be already disconnected and a connection
2676 * scheduled by tunnel_notify_connection_broken.
2678 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2682 peer_info_connect (peer_d, peer->tunnels[i]);
2685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2690 * Add the path to the peer and update the path used to reach it in case this
2693 * @param peer_info Destination peer to add the path to.
2694 * @param path New path to add. Last peer must be the peer in arg 1.
2695 * Path will be either used of freed if already known.
2696 * @param trusted Do we trust that this path is real?
2699 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2702 struct MeshPeerPath *aux;
2706 if ((NULL == peer_info) || (NULL == path))
2709 path_destroy (path);
2712 if (path->peers[path->length - 1] != peer_info->id)
2715 path_destroy (path);
2718 if (path->length <= 2 && GNUNET_NO == trusted)
2720 /* Only allow CORE to tell us about direct paths */
2721 path_destroy (path);
2724 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2725 for (l = 1; l < path->length; l++)
2727 if (path->peers[l] == myid)
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2730 for (l2 = 0; l2 < path->length - l; l2++)
2732 path->peers[l2] = path->peers[l + l2];
2737 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2742 struct GNUNET_PeerIdentity id;
2744 GNUNET_PEER_resolve (peer_info->id, &id);
2745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2746 path->length, GNUNET_i2s (&id));
2749 l = path_get_length (path);
2756 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2757 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2759 l2 = path_get_length (aux);
2762 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2763 peer_info->path_tail, aux, path);
2768 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2770 path_destroy (path);
2775 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2782 * Add the path to the origin peer and update the path used to reach it in case
2783 * this is the shortest.
2784 * The path is given in peer_info -> destination, therefore we turn the path
2785 * upside down first.
2787 * @param peer_info Peer to add the path to, being the origin of the path.
2788 * @param path New path to add after being inversed.
2789 * @param trusted Do we trust that this path is real?
2792 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2793 struct MeshPeerPath *path, int trusted)
2796 peer_info_add_path (peer_info, path, trusted);
2801 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2802 * to obtain a local peer -> destination path and interning the peer ids.
2804 * @return Newly allocated and created path
2806 static struct MeshPeerPath *
2807 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2808 unsigned int get_path_length,
2809 const struct GNUNET_PeerIdentity *put_path,
2810 unsigned int put_path_length)
2812 struct MeshPeerPath *p;
2818 GNUNET_PEER_change_rc (myid, 1);
2819 i = get_path_length;
2820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2821 for (i--; i >= 0; i--)
2823 id = GNUNET_PEER_intern (&get_path[i]);
2824 if (p->length > 0 && id == p->peers[p->length - 1])
2826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2827 GNUNET_PEER_change_rc (id, -1);
2831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2832 GNUNET_i2s (&get_path[i]));
2834 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2835 p->peers[p->length - 1] = id;
2838 i = put_path_length;
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2840 for (i--; i >= 0; i--)
2842 id = GNUNET_PEER_intern (&put_path[i]);
2845 /* PUT path went through us, so discard the path up until now and start
2846 * from here to get a much shorter (and loop-free) path.
2851 if (p->length > 0 && id == p->peers[p->length - 1])
2853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2854 GNUNET_PEER_change_rc (id, -1);
2858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2859 GNUNET_i2s (&put_path[i]));
2861 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2862 p->peers[p->length - 1] = id;
2866 if (get_path_length > 0)
2867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2868 GNUNET_i2s (&get_path[0]));
2869 if (put_path_length > 0)
2870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2871 GNUNET_i2s (&put_path[0]));
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2874 for (i = 0; i < p->length; i++)
2876 struct GNUNET_PeerIdentity peer_id;
2878 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2880 GNUNET_i2s (&peer_id));
2888 * Adds a path to the peer_infos of all the peers in the path
2890 * @param p Path to process.
2891 * @param confirmed Whether we know if the path works or not.
2894 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2898 /* TODO: invert and add */
2899 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2900 for (i++; i < p->length; i++)
2902 struct MeshPeerInfo *aux;
2903 struct MeshPeerPath *copy;
2905 aux = peer_info_get_short (p->peers[i]);
2906 copy = path_duplicate (p);
2907 copy->length = i + 1;
2908 peer_info_add_path (aux, copy, GNUNET_NO);
2914 * Send keepalive packets for a peer
2916 * @param cls Closure (tunnel for which to send the keepalive).
2917 * @param tc Notification context.
2919 * TODO: implement explicit multicast keepalive?
2922 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2926 * Search for a tunnel among the incoming tunnels
2928 * @param tid the local id of the tunnel
2930 * @return tunnel handler, NULL if doesn't exist
2932 static struct MeshTunnel *
2933 tunnel_get_incoming (MESH_TunnelNumber tid)
2935 struct GNUNET_HashCode hash;
2937 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2938 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2939 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2944 * Search for a tunnel among the tunnels for a client
2946 * @param c the client whose tunnels to search in
2947 * @param tid the local id of the tunnel
2949 * @return tunnel handler, NULL if doesn't exist
2951 static struct MeshTunnel *
2952 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2954 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2956 return tunnel_get_incoming (tid);
2960 struct GNUNET_HashCode hash;
2962 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2963 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2969 * Search for a tunnel by global ID using PEER_ID
2971 * @param pi owner of the tunnel
2972 * @param tid global tunnel number
2974 * @return tunnel handler, NULL if doesn't exist
2976 static struct MeshTunnel *
2977 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2979 struct MESH_TunnelID id;
2980 struct GNUNET_HashCode hash;
2985 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2986 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2991 * Search for a tunnel by global ID using full PeerIdentities
2993 * @param oid owner of the tunnel
2994 * @param tid global tunnel number
2996 * @return tunnel handler, NULL if doesn't exist
2998 static struct MeshTunnel *
2999 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3001 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3006 * Delete an active client from the tunnel.
3012 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3016 for (i = 0; i < t->nclients; i++)
3018 if (t->clients[i] == c)
3020 t->clients[i] = t->clients[t->nclients - 1];
3021 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3022 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3024 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3032 * Delete an ignored client from the tunnel.
3038 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3042 for (i = 0; i < t->nignore; i++)
3044 if (t->ignore[i] == c)
3046 t->ignore[i] = t->ignore[t->nignore - 1];
3047 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3055 * Delete a client from the tunnel. It should be only done on
3056 * client disconnection, otherwise use client_ignore_tunnel.
3062 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3064 tunnel_delete_ignored_client (t, c);
3065 tunnel_delete_active_client (t, c);
3070 * Iterator to free MeshTunnelChildInfo of tunnel children.
3072 * @param cls Closure (tunnel info).
3073 * @param key Hash of GNUNET_PEER_Id (unused).
3074 * @param value MeshTunnelChildInfo of the child.
3076 * @return always GNUNET_YES, to keep iterating
3079 tunnel_destroy_child (void *cls,
3080 const struct GNUNET_HashCode * key,
3083 struct MeshTunnelChildInfo *cinfo = value;
3084 struct MeshTunnel *t = cls;
3088 for (c = 0; c < cinfo->send_buffer_n; c++)
3090 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3091 if (NULL != cinfo->send_buffer[i])
3092 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3095 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3097 GNUNET_free_non_null (cinfo->send_buffer);
3098 GNUNET_free (cinfo);
3104 * Callback used to notify a client owner of a tunnel that a peer has
3105 * disconnected, most likely because of a path change.
3107 * @param cls Closure (tunnel this notification is about).
3108 * @param peer_id Short ID of disconnected peer.
3111 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3113 struct MeshTunnel *t = cls;
3114 struct MeshPeerInfo *peer;
3115 struct MeshPathInfo *path_info;
3117 if (NULL != t->owner && NULL != nc)
3119 struct GNUNET_MESH_PeerControl msg;
3121 msg.header.size = htons (sizeof (msg));
3122 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3123 msg.tunnel_id = htonl (t->local_tid);
3124 GNUNET_PEER_resolve (peer_id, &msg.peer);
3125 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3126 &msg.header, GNUNET_NO);
3128 peer = peer_info_get_short (peer_id);
3129 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3130 path_info->peer = peer;
3132 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3137 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3138 * needed rescources.
3139 * If peer already exists, reevaluate shortest path and change if different.
3141 * @param t Tunnel we want to add a new peer to
3142 * @param peer PeerInfo of the peer being added
3146 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3148 struct GNUNET_PeerIdentity id;
3149 struct MeshPeerPath *best_p;
3150 struct MeshPeerPath *p;
3151 unsigned int best_cost;
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3155 GNUNET_PEER_resolve (peer->id, &id);
3157 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3160 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3161 GNUNET_assert (GNUNET_OK ==
3162 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3167 if (NULL != (p = peer->path_head))
3170 best_cost = tree_get_path_cost (t->tree, p);
3173 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3180 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3181 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3182 t->path_refresh_task =
3183 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3187 /* Start a DHT get */
3188 peer_info_connect (peer, t);
3190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3194 * Add a path to a tunnel which we don't own, just to remember the next hop.
3195 * If destination node was already in the tunnel, the first hop information
3196 * will be replaced with the new path.
3198 * @param t Tunnel we want to add a new peer to
3199 * @param p Path to add
3200 * @param own_pos Position of local node in path.
3204 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3205 unsigned int own_pos)
3207 struct GNUNET_PeerIdentity id;
3209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3210 GNUNET_assert (0 != own_pos);
3211 tree_add_path (t->tree, p, NULL, NULL);
3212 if (own_pos < p->length - 1)
3214 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3215 tree_update_first_hops (t->tree, myid, &id);
3217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3221 * Add a client to a tunnel, initializing all needed data structures.
3223 * @param t Tunnel to which add the client.
3224 * @param c Client which to add to the tunnel.
3227 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3229 struct MeshTunnelClientInfo clinfo;
3231 GNUNET_array_append (t->clients, t->nclients, c);
3232 clinfo.fwd_ack = t->fwd_pid + 1;
3233 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3234 clinfo.fwd_pid = t->fwd_pid;
3235 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3237 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3242 * Notifies a tunnel that a connection has broken that affects at least
3243 * some of its peers. Sends a notification towards the root of the tree.
3244 * In case the peer is the owner of the tree, notifies the client that owns
3245 * the tunnel and tries to reconnect.
3247 * @param t Tunnel affected.
3248 * @param p1 Peer that got disconnected from p2.
3249 * @param p2 Peer that got disconnected from p1.
3251 * @return Short ID of the peer disconnected (either p1 or p2).
3252 * 0 if the tunnel remained unaffected.
3254 static GNUNET_PEER_Id
3255 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3261 tree_notify_connection_broken (t->tree, p1, p2,
3262 &tunnel_notify_client_peer_disconnected,
3264 if (myid != p1 && myid != p2)
3270 if (tree_get_predecessor (t->tree) != 0)
3272 /* We are the peer still connected, notify owner of the disconnection. */
3273 struct GNUNET_MESH_PathBroken msg;
3274 struct GNUNET_PeerIdentity neighbor;
3276 msg.header.size = htons (sizeof (msg));
3277 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3278 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3279 msg.tid = htonl (t->id.tid);
3280 msg.peer1 = my_full_id;
3281 GNUNET_PEER_resolve (pid, &msg.peer2);
3282 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3283 send_message (&msg.header, &neighbor, t);
3291 * Send a multicast packet to a neighbor.
3293 * @param cls Closure (Info about the multicast packet)
3294 * @param neighbor_id Short ID of the neighbor to send the packet to.
3297 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3299 struct MeshData *mdata = cls;
3300 struct MeshTransmissionDescriptor *info;
3301 struct GNUNET_PeerIdentity neighbor;
3303 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3305 info->mesh_data = mdata;
3306 (*(mdata->reference_counter)) ++;
3307 info->destination = neighbor_id;
3308 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3310 GNUNET_i2s (&neighbor));
3311 info->peer = peer_info_get (&neighbor);
3312 GNUNET_assert (NULL != info->peer);
3314 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3315 info->mesh_data->data_len,
3322 * Queue a message in a tunnel in multicast, sending a copy to each child node
3323 * down the local one in the tunnel tree.
3325 * @param t Tunnel in which to send the data.
3326 * @param msg Message to be sent.
3327 * @param internal Has the service generated this message?
3330 tunnel_send_multicast (struct MeshTunnel *t,
3331 const struct GNUNET_MessageHeader *msg,
3334 struct MeshData *mdata;
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3337 " sending a multicast packet...\n");
3339 mdata = GNUNET_malloc (sizeof (struct MeshData));
3340 mdata->data_len = ntohs (msg->size);
3341 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3343 mdata->data = GNUNET_malloc (mdata->data_len);
3344 memcpy (mdata->data, msg, mdata->data_len);
3345 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3347 struct GNUNET_MESH_Multicast *mcast;
3349 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3350 if (t->fwd_queue_n >= t->fwd_queue_max)
3353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3355 " message from %s!\n",
3356 GNUNET_i2s(&mcast->oid));
3357 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3358 " message at %s!\n",
3359 GNUNET_i2s(&my_full_id));
3360 GNUNET_free (mdata->data);
3361 GNUNET_free (mdata->reference_counter);
3362 GNUNET_free (mdata);
3366 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3368 ntohl (mcast->ttl));
3372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3374 if (NULL != t->owner &&
3375 GNUNET_YES != t->owner->shutting_down &&
3376 GNUNET_NO == internal)
3378 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3380 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3386 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3387 if (*(mdata->reference_counter) == 0)
3389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3390 " no one to send data to\n");
3391 GNUNET_free (mdata->data);
3392 GNUNET_free (mdata->reference_counter);
3393 if (NULL != mdata->task)
3395 GNUNET_SCHEDULER_cancel(*(mdata->task));
3396 GNUNET_free (mdata->task);
3397 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3399 GNUNET_free (mdata);
3402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3403 " sending a multicast packet done\n");
3409 * Increase the SKIP value of all peers that
3410 * have not received a unicast message.
3412 * @param cls Closure (ID of the peer that HAS received the message).
3413 * @param key ID of the neighbor.
3414 * @param value Information about the neighbor.
3416 * @return GNUNET_YES to keep iterating.
3419 tunnel_add_skip (void *cls,
3420 const struct GNUNET_HashCode * key,
3423 struct GNUNET_PeerIdentity *neighbor = cls;
3424 struct MeshTunnelChildInfo *cinfo = value;
3426 /* TODO compare only pointers? key == neighbor? */
3427 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3437 * @brief Get neighbor's Flow Control information.
3439 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3440 * descendant of the local node in a certain tunnel.
3441 * If the info is not yet there (recently created path), creates the data struct
3442 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3445 * @param t Tunnel related.
3446 * @param peer Neighbor whose Flow Control info is needed.
3448 * @return Neighbor's Flow Control info.
3450 static struct MeshTunnelChildInfo *
3451 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3452 const struct GNUNET_PeerIdentity *peer)
3454 struct MeshTunnelChildInfo *cinfo;
3455 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3461 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3462 cinfo->id = GNUNET_PEER_intern (peer);
3463 cinfo->skip = t->fwd_pid;
3465 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3466 cinfo->fwd_ack = t->fwd_pid + delta;
3467 cinfo->bck_ack = delta;
3469 cinfo->send_buffer =
3470 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3472 GNUNET_assert (GNUNET_OK ==
3473 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3476 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3483 * Get the Flow Control info of a client.
3485 * @param t Tunnel on which to look.
3486 * @param c Client whose ACK to get.
3488 * @return ACK value.
3490 static struct MeshTunnelClientInfo *
3491 tunnel_get_client_fc (struct MeshTunnel *t,
3492 struct MeshClient *c)
3496 for (i = 0; i < t->nclients; i++)
3498 if (t->clients[i] != c)
3500 return &t->clients_fc[i];
3503 return NULL; // avoid compiler / coverity complaints
3508 * Iterator to get the appropiate ACK value from all children nodes.
3510 * @param cls Closue (tunnel).
3511 * @param id Id of the child node.
3514 tunnel_get_child_fwd_ack (void *cls,
3517 struct GNUNET_PeerIdentity peer_id;
3518 struct MeshTunnelChildInfo *cinfo;
3519 struct MeshTunnelChildIteratorContext *ctx = cls;
3520 struct MeshTunnel *t = ctx->t;
3523 GNUNET_PEER_resolve (id, &peer_id);
3524 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3525 ack = cinfo->fwd_ack;
3528 if (GNUNET_NO == ctx->init)
3530 ctx->max_child_ack = ack;
3531 ctx->init = GNUNET_YES;
3534 if (GNUNET_YES == t->speed_min)
3536 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3540 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3547 * Get the maximum PID allowed to transmit to any
3548 * tunnel child of the local peer, depending on the tunnel
3549 * buffering/speed settings.
3553 * @return Maximum PID allowed (uint32 MAX), -1 if node has no children.
3556 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3558 struct MeshTunnelChildIteratorContext ctx;
3560 ctx.max_child_ack = 0;
3562 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3564 if (0 == ctx.nchildren)
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 " tunnel has no children, no FWD ACK\n");
3571 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3572 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3574 return (int64_t) ctx.max_child_ack;
3579 * Set the FWD ACK value of a client in a particular tunnel.
3581 * @param t Tunnel affected.
3582 * @param c Client whose ACK to set.
3583 * @param ack ACK value.
3586 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3587 struct MeshClient *c,
3592 for (i = 0; i < t->nclients; i++)
3594 if (t->clients[i] != c)
3596 t->clients_fc[i].fwd_ack = ack;
3604 * Get the highest ACK value of all clients in a particular tunnel,
3605 * according to the buffering/speed settings.
3607 * @param t Tunnel on which to look.
3609 * @return Corresponding ACK value (max uint32_t).
3610 * If no clients are suscribed, -1.
3613 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3618 if (0 == t->nclients)
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3621 " tunnel has no clients, no FWD ACK\n");
3625 for (ack = -1, i = 0; i < t->nclients; i++)
3628 (GNUNET_YES == t->speed_min &&
3629 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3630 (GNUNET_NO == t->speed_min &&
3631 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3633 ack = t->clients_fc[i].fwd_ack;
3637 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3638 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3640 return (uint32_t) ack;
3645 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3646 * mode and the status of all children nodes.
3650 * @return Maximum PID allowed.
3653 tunnel_get_fwd_ack (struct MeshTunnel *t)
3657 uint32_t buffer_free;
3661 count = t->fwd_pid - t->skip;
3662 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3663 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3664 child_ack = tunnel_get_children_fwd_ack (t);
3665 client_ack = tunnel_get_clients_fwd_ack (t);
3666 if (-1LL == child_ack)
3668 // Node has no children, child_ack AND core buffer are irrelevant.
3669 GNUNET_break (-1 != client_ack); // No children AND no clients? Not good!
3670 return (uint32_t) client_ack;
3672 if (-1LL == client_ack)
3676 if (GNUNET_YES == t->speed_min)
3678 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3679 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3683 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3684 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3686 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3687 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3689 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3690 count, buffer_free, child_ack, client_ack, ack);
3696 * Build a local ACK message and send it to a local client.
3698 * @param t Tunnel on which to send the ACK.
3699 * @param c Client to whom send the ACK.
3700 * @param ack Value of the ACK.
3703 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3705 struct GNUNET_MESH_LocalAck msg;
3707 msg.header.size = htons (sizeof (msg));
3708 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3709 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3710 msg.max_pid = htonl (ack);
3711 GNUNET_SERVER_notification_context_unicast(nc,
3718 * Build an ACK message and queue it to send to the given peer.
3720 * @param t Tunnel on which to send the ACK.
3721 * @param peer Peer to whom send the ACK.
3722 * @param ack Value of the ACK.
3725 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3727 struct GNUNET_MESH_ACK msg;
3729 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3730 msg.header.size = htons (sizeof (msg));
3731 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3732 msg.pid = htonl (ack);
3733 msg.tid = htonl (t->id.tid);
3735 send_message (&msg.header, peer, t);
3740 * Notify a the owner of a tunnel about how many more
3741 * payload packages will we accept on a given tunnel.
3743 * @param t Tunnel on which to send the ACK.
3746 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3751 "Sending client FWD ACK on tunnel %X\n",
3754 ack = tunnel_get_fwd_ack (t);
3756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3757 if (t->last_fwd_ack == ack)
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3761 t->last_fwd_ack = ack;
3762 send_local_ack (t, t->owner, ack);
3767 * Send an ACK informing the predecessor about the available buffer space.
3768 * In case there is no predecessor, inform the owning client.
3769 * If buffering is off, send only on behalf of children or self if endpoint.
3770 * If buffering is on, send when sent to children and buffer space is free.
3771 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3772 * the ACK itself goes "back" (towards root).
3774 * @param t Tunnel on which to send the ACK.
3775 * @param type Type of message that triggered the ACK transmission.
3778 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3780 struct GNUNET_PeerIdentity id;
3783 if (NULL != t->owner)
3785 tunnel_send_client_fwd_ack (t);
3788 /* Is it after unicast / multicast retransmission? */
3791 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3792 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3794 "ACK due to FWD DATA retransmission\n");
3795 if (GNUNET_YES == t->nobuffer)
3797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3801 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3802 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3808 /* Check if we need no retransmit the ACK */
3809 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3810 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3814 " t->qmax: %u, t->qn: %u\n",
3815 t->fwd_queue_max, t->fwd_queue_n);
3816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3817 " t->pid: %u, t->ack: %u\n",
3818 t->fwd_pid, t->last_fwd_ack);
3822 /* Ok, ACK might be necessary, what PID to ACK? */
3823 ack = tunnel_get_fwd_ack (t);
3825 /* If speed_min and not all children have ack'd, dont send yet */
3826 if (ack == t->last_fwd_ack)
3828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3832 t->last_fwd_ack = ack;
3833 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3834 send_ack (t, &id, ack);
3840 * Iterator to send a child node a BCK ACK to allow him to send more
3843 * @param cls Closure (tunnel).
3844 * @param id Id of the child node.
3847 tunnel_send_child_bck_ack (void *cls,
3850 struct MeshTunnel *t = cls;
3851 struct MeshTunnelChildInfo *cinfo;
3852 struct GNUNET_PeerIdentity peer;
3854 GNUNET_PEER_resolve (id, &peer);
3855 cinfo = tunnel_get_neighbor_fc (t, &peer);
3857 if (cinfo->bck_ack != cinfo->pid &&
3858 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3862 send_ack (t, &peer, cinfo->bck_ack);
3867 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3869 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3871 * @param t Tunnel on which to send the BCK ACKs.
3874 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3877 unsigned int tunnel_delta;
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3881 tunnel_delta = t->bck_ack - t->bck_pid;
3882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3884 /* Find client whom to allow to send to origin (with lowest buffer space) */
3885 for (i = 0; i < t->nclients; i++)
3887 struct MeshTunnelClientInfo *clinfo;
3890 clinfo = &t->clients_fc[i];
3891 delta = clinfo->bck_ack - clinfo->bck_pid;
3892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3893 t->clients[i]->id, delta);
3895 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3896 (GNUNET_YES == t->nobuffer && 0 == delta))
3900 ack = clinfo->bck_pid;
3901 ack += t->nobuffer ? 1 : tunnel_delta;
3902 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3903 " sending ack to client %u: %u\n",
3904 t->clients[i]->id, ack);
3905 send_local_ack (t, t->clients[i], ack);
3906 clinfo->bck_ack = ack;
3913 * Send an ACK informing the children nodes and destination clients about
3914 * the available buffer space.
3915 * If buffering is off, send only on behalf of root (can be self).
3916 * If buffering is on, send when sent to predecessor and buffer space is free.
3917 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3918 * the ACK itself goes "forward" (towards children/clients).
3920 * @param t Tunnel on which to send the ACK.
3921 * @param type Type of message that triggered the ACK transmission.
3924 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3927 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3928 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3929 /* Is it after data to_origin retransmission? */
3932 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3933 if (GNUNET_YES == t->nobuffer)
3935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3936 " Not sending ACK, nobuffer\n");
3940 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3941 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3947 tunnel_send_clients_bck_ack (t);
3948 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3953 * @brief Re-initiate traffic to this peer if necessary.
3955 * Check if there is traffic queued towards this peer
3956 * and the core transmit handle is NULL (traffic was stalled).
3957 * If so, call core tmt rdy.
3959 * @param cls Closure (unused)
3960 * @param peer_id Short ID of peer to which initiate traffic.
3963 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3965 struct MeshPeerInfo *peer;
3966 struct GNUNET_PeerIdentity id;
3967 struct MeshPeerQueue *q;
3970 peer = peer_info_get_short(peer_id);
3971 if (NULL != peer->core_transmit)
3974 q = queue_get_next(peer);
3977 /* Might br multicast traffic already sent to this particular peer but
3978 * not to other children in this tunnel.
3979 * This way t->queue_n would be > 0 but the queue of this particular peer
3985 GNUNET_PEER_resolve (peer->id, &id);
3986 peer->core_transmit =
3987 GNUNET_CORE_notify_transmit_ready(core_handle,
3990 GNUNET_TIME_UNIT_FOREVER_REL,
4000 * @brief Allow transmission of FWD traffic on this tunnel
4002 * Check if there is traffic queued towards any children
4003 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4005 * @param t Tunnel on which to unlock FWD traffic.
4008 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4010 if (0 == t->fwd_queue_n)
4013 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4018 * @brief Allow transmission of BCK traffic on this tunnel
4020 * Check if there is traffic queued towards the root of the tree
4021 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4023 * @param t Tunnel on which to unlock BCK traffic.
4026 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4028 if (0 == t->bck_queue_n)
4031 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4036 * Send a message to all peers in this tunnel that the tunnel is no longer
4039 * @param t The tunnel whose peers to notify.
4042 tunnel_send_destroy (struct MeshTunnel *t)
4044 struct GNUNET_MESH_TunnelDestroy msg;
4046 msg.header.size = htons (sizeof (msg));
4047 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4048 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4049 msg.tid = htonl (t->id.tid);
4050 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4055 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4057 * @param cls Closure (Tunnel which to cancel).
4058 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4061 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4063 struct MeshTunnel *t = cls;
4064 struct MeshPeerInfo *peer_info;
4065 struct MeshPeerQueue *pq;
4066 struct MeshPeerQueue *next;
4068 peer_info = peer_info_get_short (neighbor_id);
4069 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4072 if (pq->tunnel == t)
4074 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4075 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4076 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4078 // Should have been removed on destroy children
4081 queue_destroy (pq, GNUNET_YES);
4084 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4086 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4087 peer_info->core_transmit = NULL;
4092 * Destroy the tunnel and free any allocated resources linked to it.
4094 * @param t the tunnel to destroy
4096 * @return GNUNET_OK on success
4099 tunnel_destroy (struct MeshTunnel *t)
4101 struct MeshClient *c;
4102 struct GNUNET_HashCode hash;
4113 struct GNUNET_PeerIdentity id;
4115 GNUNET_PEER_resolve (t->id.oid, &id);
4116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4117 GNUNET_i2s (&id), t->id.tid);
4119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4123 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4124 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4129 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4132 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4136 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4137 for (i = 0; i < t->nclients; i++)
4141 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4146 for (i = 0; i < t->nignore; i++)
4150 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4155 if (t->nclients > 0)
4158 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4162 GNUNET_free (t->clients);
4164 if (NULL != t->peers)
4166 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4168 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4171 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4172 &tunnel_destroy_child,
4174 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4176 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4178 tree_destroy (t->tree);
4180 if (NULL != t->regex_ctx)
4181 regex_cancel_search (t->regex_ctx);
4182 if (NULL != t->dht_get_type)
4183 GNUNET_DHT_get_stop (t->dht_get_type);
4184 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4185 GNUNET_SCHEDULER_cancel (t->timeout_task);
4186 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4187 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4190 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4191 GNUNET_assert (0 <= n_tunnels);
4198 * Create a new tunnel
4200 * @param owner Who is the owner of the tunnel (short ID).
4201 * @param tid Tunnel Number of the tunnel.
4202 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4203 * @param local Tunnel Number for the tunnel, for the client point of view.
4205 * @return A new initialized tunnel. NULL on error.
4207 static struct MeshTunnel *
4208 tunnel_new (GNUNET_PEER_Id owner,
4209 MESH_TunnelNumber tid,
4210 struct MeshClient *client,
4211 MESH_TunnelNumber local)
4213 struct MeshTunnel *t;
4214 struct GNUNET_HashCode hash;
4216 if (n_tunnels >= max_tunnels && NULL == client)
4219 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4222 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4223 t->bck_queue_max = t->fwd_queue_max;
4224 t->tree = tree_new (owner);
4226 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4227 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4228 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4229 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4230 t->local_tid = local;
4231 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4233 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4235 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4237 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4238 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4245 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4252 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4254 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4255 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4259 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4269 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4270 * that are no longer needed, as well as nodes of no longer reachable peers.
4271 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4273 * @param t Tunnel from which to remove the path.
4274 * @param peer Short id of the peer which should be removed.
4277 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4279 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4285 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4286 * client when the client disconnects. If the client is not the owner, the
4287 * owner will get notified if no more clients are in the tunnel and the client
4288 * get removed from the tunnel's list.
4290 * @param cls closure (client that is disconnecting)
4291 * @param key the hash of the local tunnel id (used to access the hashmap)
4292 * @param value the value stored at the key (tunnel to destroy)
4294 * @return GNUNET_OK on success
4297 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4299 struct MeshTunnel *t = value;
4300 struct MeshClient *c = cls;
4303 send_client_tunnel_disconnect(t, c);
4306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4307 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4308 tunnel_delete_client(t, c);
4309 client_delete_tunnel(c, t);
4312 tunnel_send_destroy(t);
4313 r = tunnel_destroy (t);
4319 * Timeout function, destroys tunnel if called
4321 * @param cls Closure (tunnel to destroy).
4322 * @param tc TaskContext
4325 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4327 struct MeshTunnel *t = cls;
4329 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4331 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4336 * Resets the tunnel timeout. Starts it if no timeout was running.
4338 * @param t Tunnel whose timeout to reset.
4340 * TODO use heap to improve efficiency of scheduler.
4343 tunnel_reset_timeout (struct MeshTunnel *t)
4345 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4346 GNUNET_SCHEDULER_cancel (t->timeout_task);
4348 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4349 (refresh_path_time, 4), &tunnel_timeout, t);
4353 /******************************************************************************/
4354 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4355 /******************************************************************************/
4358 * Function to send a create path packet to a peer.
4360 * @param cls closure
4361 * @param size number of bytes available in buf
4362 * @param buf where the callee should write the message
4363 * @return number of bytes written to buf
4366 send_core_path_create (void *cls, size_t size, void *buf)
4368 struct MeshPathInfo *info = cls;
4369 struct GNUNET_MESH_ManipulatePath *msg;
4370 struct GNUNET_PeerIdentity *peer_ptr;
4371 struct MeshTunnel *t = info->t;
4372 struct MeshPeerPath *p = info->path;
4377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4379 sizeof (struct GNUNET_MESH_ManipulatePath) +
4380 p->length * sizeof (struct GNUNET_PeerIdentity);
4382 if (size < size_needed || NULL == buf)
4387 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4388 msg->header.size = htons (size_needed);
4389 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4390 msg->tid = ntohl (t->id.tid);
4392 if (GNUNET_YES == t->speed_min)
4393 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4394 if (GNUNET_YES == t->nobuffer)
4395 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4396 msg->opt = htonl(opt);
4398 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4399 for (i = 0; i < p->length; i++)
4401 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4408 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4414 * Fill the core buffer
4416 * @param cls closure (data itself)
4417 * @param size number of bytes available in buf
4418 * @param buf where the callee should write the message
4420 * @return number of bytes written to buf
4423 send_core_data_multicast (void *cls, size_t size, void *buf)
4425 struct MeshTransmissionDescriptor *info = cls;
4428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4429 GNUNET_assert (NULL != info);
4430 GNUNET_assert (NULL != info->peer);
4431 total_size = info->mesh_data->data_len;
4432 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4434 if (total_size > size)
4439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4440 memcpy (buf, info->mesh_data->data, total_size);
4443 struct GNUNET_MESH_Multicast *mc;
4444 struct GNUNET_MessageHeader *mh;
4447 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4449 mc = (struct GNUNET_MESH_Multicast *) mh;
4450 mh = (struct GNUNET_MessageHeader *) &mc[1];
4451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4452 " multicast, payload type %s\n",
4453 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4455 " multicast, payload size %u\n", ntohs (mh->size));
4459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4460 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4464 data_descriptor_decrement_rc (info->mesh_data);
4465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4473 * Creates a path ack message in buf and frees all unused resources.
4475 * @param cls closure (MeshTransmissionDescriptor)
4476 * @param size number of bytes available in buf
4477 * @param buf where the callee should write the message
4478 * @return number of bytes written to buf
4481 send_core_path_ack (void *cls, size_t size, void *buf)
4483 struct MeshTransmissionDescriptor *info = cls;
4484 struct GNUNET_MESH_PathACK *msg = buf;
4486 GNUNET_assert (NULL != info);
4487 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4492 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4493 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4494 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4495 msg->tid = htonl (info->origin->tid);
4496 msg->peer_id = my_full_id;
4499 /* TODO add signature */
4501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4502 return sizeof (struct GNUNET_MESH_PathACK);
4507 * Free a transmission that was already queued with all resources
4508 * associated to the request.
4510 * @param queue Queue handler to cancel.
4511 * @param clear_cls Is it necessary to free associated cls?
4514 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4516 struct MeshTransmissionDescriptor *dd;
4517 struct MeshPathInfo *path_info;
4519 if (GNUNET_YES == clear_cls)
4521 switch (queue->type)
4523 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4524 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4525 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4528 data_descriptor_decrement_rc (dd->mesh_data);
4530 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4532 path_info = queue->cls;
4533 path_destroy (path_info->path);
4537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4538 " type %u unknown!\n",
4541 GNUNET_free_non_null (queue->cls);
4543 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4544 queue->peer->queue_tail,
4546 GNUNET_free (queue);
4551 * @brief Get the next transmittable message from the queue.
4553 * This will be the head, except in the case of being a data packet
4554 * not allowed by the destination peer.
4556 * @param peer Destination peer.
4558 * @return The next viable MeshPeerQueue element to send to that peer.
4559 * NULL when there are no transmittable messages.
4561 struct MeshPeerQueue *
4562 queue_get_next (const struct MeshPeerInfo *peer)
4564 struct MeshPeerQueue *q;
4565 struct MeshTunnel *t;
4566 struct MeshTransmissionDescriptor *info;
4567 struct MeshTunnelChildInfo *cinfo;
4568 struct GNUNET_MESH_Unicast *ucast;
4569 struct GNUNET_MESH_ToOrigin *to_orig;
4570 struct GNUNET_MESH_Multicast *mcast;
4571 struct GNUNET_PeerIdentity id;
4575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4576 for (q = peer->queue_head; NULL != q; q = q->next)
4580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4582 GNUNET_MESH_DEBUG_M2S(q->type));
4585 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4586 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4587 pid = ntohl (ucast->pid);
4588 GNUNET_PEER_resolve (info->peer->id, &id);
4589 cinfo = tunnel_get_neighbor_fc(t, &id);
4590 ack = cinfo->fwd_ack;
4592 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4593 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4594 pid = ntohl (to_orig->pid);
4597 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4598 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4599 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4601 // Not a multicast payload: multicast control traffic (destroy, etc)
4604 pid = ntohl (mcast->pid);
4605 GNUNET_PEER_resolve (info->peer->id, &id);
4606 cinfo = tunnel_get_neighbor_fc(t, &id);
4607 ack = cinfo->fwd_ack;
4610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4615 "********* ACK: %u, PID: %u\n",
4617 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4626 "********* NEXT!\n");
4629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4630 "********* nothing found\n");
4636 * Core callback to write a queued packet to core buffer
4638 * @param cls Closure (peer info).
4639 * @param size Number of bytes available in buf.
4640 * @param buf Where the to write the message.
4642 * @return number of bytes written to buf
4645 queue_send (void *cls, size_t size, void *buf)
4647 struct MeshPeerInfo *peer = cls;
4648 struct GNUNET_MessageHeader *msg;
4649 struct MeshPeerQueue *queue;
4650 struct MeshTunnel *t;
4651 struct MeshTunnelChildInfo *cinfo;
4652 struct GNUNET_PeerIdentity dst_id;
4655 peer->core_transmit = NULL;
4657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4658 queue = queue_get_next(peer);
4660 /* Queue has no internal mesh traffic not sendable payload */
4663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4664 if (NULL == peer->queue_head)
4665 GNUNET_break(0); // Should've been canceled
4668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4670 GNUNET_PEER_resolve (peer->id, &dst_id);
4671 /* Check if buffer size is enough for the message */
4672 if (queue->size > size)
4676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4677 "********* not enough room, reissue\n");
4678 peer->core_transmit =
4679 GNUNET_CORE_notify_transmit_ready(core_handle,
4682 GNUNET_TIME_UNIT_FOREVER_REL,
4689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4692 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4696 "********* unicast: t->q (%u/%u)\n",
4697 t->fwd_queue_n, t->fwd_queue_max);
4699 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4706 switch (queue->type)
4709 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4710 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4711 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4713 "********* raw: %s\n",
4714 GNUNET_MESH_DEBUG_M2S (queue->type));
4716 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4717 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4718 data_size = send_core_data_raw (queue->cls, size, buf);
4719 msg = (struct GNUNET_MessageHeader *) buf;
4720 switch (ntohs (msg->type)) // Type of preconstructed message
4722 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4723 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4725 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4726 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4732 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4735 struct MeshTransmissionDescriptor *info = queue->cls;
4736 if (*(info->mesh_data->reference_counter) == 1)
4738 // FIXME fc (t->fwd_queue_n--)
4740 data_size = send_core_data_multicast(queue->cls, size, buf);
4741 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4743 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4745 data_size = send_core_path_create(queue->cls, size, buf);
4747 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4749 data_size = send_core_path_ack(queue->cls, size, buf);
4753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4754 "********* type unknown: %u\n",
4758 switch (queue->type)
4760 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4761 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4762 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4763 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4764 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4767 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4768 "at pos %u (%p) != %p\n",
4769 cinfo->send_buffer_start,
4770 cinfo->send_buffer[cinfo->send_buffer_start],
4773 GNUNET_break(cinfo->send_buffer_n > 0);
4774 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4775 cinfo->send_buffer_n--;
4776 cinfo->send_buffer_start++;
4777 cinfo->send_buffer_start %= t->fwd_queue_max;
4783 /* Free queue, but cls was freed by send_core_* */
4784 queue_destroy (queue, GNUNET_NO);
4786 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4792 /* If more data in queue, send next */
4793 queue = queue_get_next(peer);
4796 struct GNUNET_PeerIdentity id;
4798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4799 GNUNET_PEER_resolve (peer->id, &id);
4800 peer->core_transmit =
4801 GNUNET_CORE_notify_transmit_ready(core_handle,
4804 GNUNET_TIME_UNIT_FOREVER_REL,
4812 if (NULL != peer->queue_head)
4813 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4814 "********* %s stalled\n",
4815 GNUNET_i2s(&my_full_id));
4817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4823 * Queue and pass message to core when possible.
4825 * @param cls Closure (type dependant).
4826 * @param type Type of the message, 0 for a raw message.
4827 * @param size Size of the message.
4828 * @param dst Neighbor to send message to.
4829 * @param t Tunnel this message belongs to.
4832 queue_add (void *cls, uint16_t type, size_t size,
4833 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4835 struct MeshPeerQueue *queue;
4836 struct MeshTunnelChildInfo *cinfo;
4837 struct GNUNET_PeerIdentity id;
4843 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4844 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4846 n = &t->fwd_queue_n;
4847 max = &t->fwd_queue_max;
4849 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4851 n = &t->bck_queue_n;
4852 max = &t->bck_queue_max;
4857 if (NULL == t->owner)
4858 GNUNET_break_op(0); // TODO: kill connection?
4861 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4863 return; // Drop message
4867 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4873 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4874 GNUNET_PEER_resolve (dst->id, &id);
4875 if (NULL == dst->core_transmit)
4877 dst->core_transmit =
4878 GNUNET_CORE_notify_transmit_ready(core_handle,
4881 GNUNET_TIME_UNIT_FOREVER_REL,
4887 if (NULL == n) // Is this internal mesh traffic?
4890 // It's payload, keep track of buffer per peer.
4891 cinfo = tunnel_get_neighbor_fc(t, &id);
4892 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4893 if (NULL != cinfo->send_buffer[i])
4895 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4896 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4897 cinfo->send_buffer_start++;
4898 cinfo->send_buffer_start %= t->fwd_queue_max;
4899 cinfo->send_buffer_n--;
4901 cinfo->send_buffer[i] = queue;
4902 cinfo->send_buffer_n++;
4903 if (cinfo->send_buffer_n > t->fwd_queue_max)
4906 cinfo->send_buffer_n = t->fwd_queue_max;
4911 /******************************************************************************/
4912 /******************** MESH NETWORK HANDLERS **************************/
4913 /******************************************************************************/
4917 * Core handler for path creation
4919 * @param cls closure
4920 * @param message message
4921 * @param peer peer identity this notification is about
4922 * @param atsi performance data
4923 * @param atsi_count number of records in 'atsi'
4925 * @return GNUNET_OK to keep the connection open,
4926 * GNUNET_SYSERR to close it (signal serious error)
4929 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4930 const struct GNUNET_MessageHeader *message,
4931 const struct GNUNET_ATS_Information *atsi,
4932 unsigned int atsi_count)
4934 unsigned int own_pos;
4937 MESH_TunnelNumber tid;
4938 struct GNUNET_MESH_ManipulatePath *msg;
4939 struct GNUNET_PeerIdentity *pi;
4940 struct GNUNET_HashCode hash;
4941 struct MeshPeerPath *path;
4942 struct MeshPeerInfo *dest_peer_info;
4943 struct MeshPeerInfo *orig_peer_info;
4944 struct MeshTunnel *t;
4946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4947 "Received a path create msg [%s]\n",
4948 GNUNET_i2s (&my_full_id));
4949 size = ntohs (message->size);
4950 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4952 GNUNET_break_op (0);
4956 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4957 if (size % sizeof (struct GNUNET_PeerIdentity))
4959 GNUNET_break_op (0);
4962 size /= sizeof (struct GNUNET_PeerIdentity);
4965 GNUNET_break_op (0);
4968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4969 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4971 tid = ntohl (msg->tid);
4972 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4974 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4975 t = tunnel_get (pi, tid);
4976 if (NULL == t) // FIXME only for INCOMING tunnels?
4980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4981 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4984 // FIXME notify failure
4987 opt = ntohl (msg->opt);
4988 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4989 GNUNET_YES : GNUNET_NO;
4990 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4991 GNUNET_YES : GNUNET_NO;
4993 if (GNUNET_YES == t->nobuffer)
4995 t->bck_queue_max = 1;
4996 t->fwd_queue_max = 1;
4999 // FIXME only assign a local tid if a local client is interested (on demand)
5000 while (NULL != tunnel_get_incoming (next_local_tid))
5001 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5002 t->local_tid_dest = next_local_tid++;
5003 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5006 tunnel_reset_timeout (t);
5007 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5009 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5010 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5018 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5019 if (NULL == dest_peer_info)
5021 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5022 " Creating PeerInfo for destination.\n");
5023 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5024 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5025 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5027 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5029 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5030 if (NULL == orig_peer_info)
5032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5033 " Creating PeerInfo for origin.\n");
5034 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5035 orig_peer_info->id = GNUNET_PEER_intern (pi);
5036 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5037 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5040 path = path_new (size);
5042 for (i = 0; i < size; i++)
5044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5045 GNUNET_i2s (&pi[i]));
5046 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5047 if (path->peers[i] == myid)
5050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5053 /* cannot be self, must be 'not found' */
5054 /* create path: self not found in path through self */
5055 GNUNET_break_op (0);
5056 path_destroy (path);
5060 path_add_to_peers (path, GNUNET_NO);
5061 tunnel_add_path (t, path, own_pos);
5062 if (own_pos == size - 1)
5064 /* It is for us! Send ack. */
5065 struct MeshTransmissionDescriptor *info;
5067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5068 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5069 if (NULL == t->peers)
5071 /* New tunnel! Notify clients on data. */
5072 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5074 GNUNET_break (GNUNET_SYSERR !=
5075 GNUNET_CONTAINER_multihashmap_put (t->peers,
5076 &my_full_id.hashPubKey,
5079 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5080 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5081 info->origin = &t->id;
5082 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5083 GNUNET_assert (NULL != info->peer);
5085 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5086 sizeof (struct GNUNET_MESH_PathACK),
5092 struct MeshPeerPath *path2;
5094 /* It's for somebody else! Retransmit. */
5095 path2 = path_duplicate (path);
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5097 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5098 path2 = path_duplicate (path);
5099 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5100 send_create_path (dest_peer_info, path, t);
5107 * Core handler for path destruction
5109 * @param cls closure
5110 * @param message message
5111 * @param peer peer identity this notification is about
5112 * @param atsi performance data
5113 * @param atsi_count number of records in 'atsi'
5115 * @return GNUNET_OK to keep the connection open,
5116 * GNUNET_SYSERR to close it (signal serious error)
5119 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5120 const struct GNUNET_MessageHeader *message,
5121 const struct GNUNET_ATS_Information *atsi,
5122 unsigned int atsi_count)
5124 struct GNUNET_MESH_ManipulatePath *msg;
5125 struct GNUNET_PeerIdentity *pi;
5126 struct MeshPeerPath *path;
5127 struct MeshTunnel *t;
5128 unsigned int own_pos;
5132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5133 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5134 size = ntohs (message->size);
5135 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5137 GNUNET_break_op (0);
5141 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5142 if (size % sizeof (struct GNUNET_PeerIdentity))
5144 GNUNET_break_op (0);
5147 size /= sizeof (struct GNUNET_PeerIdentity);
5150 GNUNET_break_op (0);
5153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5155 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5156 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5158 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5160 t = tunnel_get (pi, ntohl (msg->tid));
5163 /* TODO notify back: we don't know this tunnel */
5164 GNUNET_break_op (0);
5167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5168 path = path_new (size);
5170 for (i = 0; i < size; i++)
5172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5173 GNUNET_i2s (&pi[i]));
5174 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5175 if (path->peers[i] == myid)
5178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5179 if (own_pos < path->length - 1)
5180 send_message (message, &pi[own_pos + 1], t);
5182 send_client_tunnel_disconnect(t, NULL);
5184 tunnel_delete_peer (t, path->peers[path->length - 1]);
5185 path_destroy (path);
5191 * Core handler for notifications of broken paths
5193 * @param cls closure
5194 * @param message message
5195 * @param peer peer identity this notification is about
5196 * @param atsi performance data
5197 * @param atsi_count number of records in 'atsi'
5199 * @return GNUNET_OK to keep the connection open,
5200 * GNUNET_SYSERR to close it (signal serious error)
5203 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5204 const struct GNUNET_MessageHeader *message,
5205 const struct GNUNET_ATS_Information *atsi,
5206 unsigned int atsi_count)
5208 struct GNUNET_MESH_PathBroken *msg;
5209 struct MeshTunnel *t;
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5212 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5213 msg = (struct GNUNET_MESH_PathBroken *) message;
5214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5215 GNUNET_i2s (&msg->peer1));
5216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5217 GNUNET_i2s (&msg->peer2));
5218 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5221 GNUNET_break_op (0);
5224 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5225 GNUNET_PEER_search (&msg->peer2));
5232 * Core handler for tunnel destruction
5234 * @param cls closure
5235 * @param message message
5236 * @param peer peer identity this notification is about
5237 * @param atsi performance data
5238 * @param atsi_count number of records in 'atsi'
5240 * @return GNUNET_OK to keep the connection open,
5241 * GNUNET_SYSERR to close it (signal serious error)
5244 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5245 const struct GNUNET_MessageHeader *message,
5246 const struct GNUNET_ATS_Information *atsi,
5247 unsigned int atsi_count)
5249 struct GNUNET_MESH_TunnelDestroy *msg;
5250 struct MeshTunnel *t;
5252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5253 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5254 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5256 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5257 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5260 /* Probably already got the message from another path,
5261 * destroyed the tunnel and retransmitted to children.
5264 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5267 if (t->id.oid == myid)
5269 GNUNET_break_op (0);
5272 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5274 /* Tunnel was incoming, notify clients */
5275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5276 t->local_tid, t->local_tid_dest);
5277 send_clients_tunnel_destroy (t);
5279 tunnel_send_destroy (t);
5280 t->destroy = GNUNET_YES;
5281 // TODO: add timeout to destroy the tunnel anyway
5287 * Core handler for mesh network traffic going from the origin to a peer
5289 * @param cls closure
5290 * @param peer peer identity this notification is about
5291 * @param message message
5292 * @param atsi performance data
5293 * @param atsi_count number of records in 'atsi'
5294 * @return GNUNET_OK to keep the connection open,
5295 * GNUNET_SYSERR to close it (signal serious error)
5298 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5299 const struct GNUNET_MessageHeader *message,
5300 const struct GNUNET_ATS_Information *atsi,
5301 unsigned int atsi_count)
5303 struct GNUNET_MESH_Unicast *msg;
5304 struct GNUNET_PeerIdentity *neighbor;
5305 struct MeshTunnelChildInfo *cinfo;
5306 struct MeshTunnel *t;
5307 GNUNET_PEER_Id dest_id;
5312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5315 size = ntohs (message->size);
5317 sizeof (struct GNUNET_MESH_Unicast) +
5318 sizeof (struct GNUNET_MessageHeader))
5323 msg = (struct GNUNET_MESH_Unicast *) message;
5324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5325 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5327 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5330 /* TODO notify back: we don't know this tunnel */
5331 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5332 GNUNET_break_op (0);
5335 pid = ntohl (msg->pid);
5336 if (t->fwd_pid == pid)
5338 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5339 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5340 " Already seen pid %u, DROPPING!\n", pid);
5345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5346 " pid %u not seen yet, forwarding\n", pid);
5349 t->skip += (pid - t->fwd_pid) - 1;
5352 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5354 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5355 GNUNET_break_op (0);
5356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5357 "Received PID %u, ACK %u\n",
5358 pid, t->last_fwd_ack);
5362 tunnel_reset_timeout (t);
5363 dest_id = GNUNET_PEER_search (&msg->destination);
5364 if (dest_id == myid)
5366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5367 " it's for us! sending to clients...\n");
5368 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5369 send_subscribed_clients (message, &msg[1].header, t);
5370 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5373 ttl = ntohl (msg->ttl);
5374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5377 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5378 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5379 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5383 " not for us, retransmitting...\n");
5385 neighbor = tree_get_first_hop (t->tree, dest_id);
5386 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5388 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5391 if (GNUNET_YES == t->nobuffer &&
5392 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5394 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5395 GNUNET_break_op (0);
5398 send_message (message, neighbor, t);
5399 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5405 * Core handler for mesh network traffic going from the origin to all peers
5407 * @param cls closure
5408 * @param message message
5409 * @param peer peer identity this notification is about
5410 * @param atsi performance data
5411 * @param atsi_count number of records in 'atsi'
5412 * @return GNUNET_OK to keep the connection open,
5413 * GNUNET_SYSERR to close it (signal serious error)
5415 * TODO: Check who we got this from, to validate route.
5418 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5419 const struct GNUNET_MessageHeader *message,
5420 const struct GNUNET_ATS_Information *atsi,
5421 unsigned int atsi_count)
5423 struct GNUNET_MESH_Multicast *msg;
5424 struct MeshTunnel *t;
5428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5430 size = ntohs (message->size);
5431 if (sizeof (struct GNUNET_MESH_Multicast) +
5432 sizeof (struct GNUNET_MessageHeader) > size)
5434 GNUNET_break_op (0);
5437 msg = (struct GNUNET_MESH_Multicast *) message;
5438 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5442 /* TODO notify that we dont know that tunnel */
5443 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5444 GNUNET_break_op (0);
5447 pid = ntohl (msg->pid);
5448 if (t->fwd_pid == pid)
5450 /* already seen this packet, drop */
5451 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5453 " Already seen pid %u, DROPPING!\n", pid);
5454 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5460 " pid %u not seen yet, forwarding\n", pid);
5462 t->skip += (pid - t->fwd_pid) - 1;
5464 tunnel_reset_timeout (t);
5466 /* Transmit to locally interested clients */
5467 if (NULL != t->peers &&
5468 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5470 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5471 send_subscribed_clients (message, &msg[1].header, t);
5472 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5475 if (ntohl (msg->ttl) == 0)
5477 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5478 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5479 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5482 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5483 tunnel_send_multicast (t, message, GNUNET_NO);
5489 * Core handler for mesh network traffic toward the owner of a tunnel
5491 * @param cls closure
5492 * @param message message
5493 * @param peer peer identity this notification is about
5494 * @param atsi performance data
5495 * @param atsi_count number of records in 'atsi'
5497 * @return GNUNET_OK to keep the connection open,
5498 * GNUNET_SYSERR to close it (signal serious error)
5501 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5502 const struct GNUNET_MessageHeader *message,
5503 const struct GNUNET_ATS_Information *atsi,
5504 unsigned int atsi_count)
5506 struct GNUNET_MESH_ToOrigin *msg;
5507 struct GNUNET_PeerIdentity id;
5508 struct MeshPeerInfo *peer_info;
5509 struct MeshTunnel *t;
5513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5515 size = ntohs (message->size);
5516 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5517 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5519 GNUNET_break_op (0);
5522 msg = (struct GNUNET_MESH_ToOrigin *) message;
5523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5524 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5525 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5529 /* TODO notify that we dont know this tunnel (whom)? */
5530 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5531 GNUNET_break_op (0);
5535 if (NULL != t->owner)
5538 struct GNUNET_MESH_ToOrigin *copy;
5540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5541 " it's for us! sending to clients...\n");
5542 /* TODO signature verification */
5543 memcpy (cbuf, message, size);
5544 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5545 copy->tid = htonl (t->local_tid);
5546 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5547 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5548 ©->header, GNUNET_NO);
5549 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5553 " not for us, retransmitting...\n");
5555 peer_info = peer_info_get (&msg->oid);
5556 if (NULL == peer_info)
5558 /* unknown origin of tunnel */
5562 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5563 send_message (message, &id, t);
5564 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5571 * Core handler for mesh network traffic point-to-point acks.
5573 * @param cls closure
5574 * @param message message
5575 * @param peer peer identity this notification is about
5576 * @param atsi performance data
5577 * @param atsi_count number of records in 'atsi'
5579 * @return GNUNET_OK to keep the connection open,
5580 * GNUNET_SYSERR to close it (signal serious error)
5583 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5584 const struct GNUNET_MessageHeader *message,
5585 const struct GNUNET_ATS_Information *atsi,
5586 unsigned int atsi_count)
5588 struct GNUNET_MESH_ACK *msg;
5589 struct MeshTunnel *t;
5592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5594 msg = (struct GNUNET_MESH_ACK *) message;
5596 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5600 /* TODO notify that we dont know this tunnel (whom)? */
5601 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5602 GNUNET_break_op (0);
5605 ack = ntohl (msg->pid);
5607 /* Is this a forward or backward ACK? */
5608 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5610 struct MeshTunnelChildInfo *cinfo;
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5614 cinfo = tunnel_get_neighbor_fc (t, peer);
5615 cinfo->fwd_ack = ack;
5616 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5617 tunnel_unlock_fwd_queues (t);
5621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5623 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5624 tunnel_unlock_bck_queue (t);
5631 * Core handler for path ACKs
5633 * @param cls closure
5634 * @param message message
5635 * @param peer peer identity this notification is about
5636 * @param atsi performance data
5637 * @param atsi_count number of records in 'atsi'
5639 * @return GNUNET_OK to keep the connection open,
5640 * GNUNET_SYSERR to close it (signal serious error)
5643 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5644 const struct GNUNET_MessageHeader *message,
5645 const struct GNUNET_ATS_Information *atsi,
5646 unsigned int atsi_count)
5648 struct GNUNET_MESH_PathACK *msg;
5649 struct GNUNET_PeerIdentity id;
5650 struct MeshPeerInfo *peer_info;
5651 struct MeshPeerPath *p;
5652 struct MeshTunnel *t;
5654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5655 GNUNET_i2s (&my_full_id));
5656 msg = (struct GNUNET_MESH_PathACK *) message;
5657 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5660 /* TODO notify that we don't know the tunnel */
5661 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5663 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5667 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5669 peer_info = peer_info_get (&msg->peer_id);
5670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5671 GNUNET_i2s (&msg->peer_id));
5672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5675 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5678 "connect_by_string completed, stopping search\n");
5679 regex_cancel_search (t->regex_ctx);
5680 t->regex_ctx = NULL;
5683 /* Add paths to peers? */
5684 p = tree_get_path_to_peer (t->tree, peer_info->id);
5687 path_add_to_peers (p, GNUNET_YES);
5695 /* Message for us? */
5696 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5699 if (NULL == t->owner)
5701 GNUNET_break_op (0);
5704 if (NULL != t->dht_get_type)
5706 GNUNET_DHT_get_stop (t->dht_get_type);
5707 t->dht_get_type = NULL;
5709 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5711 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5712 send_client_peer_connected (t, peer_info->id);
5717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5718 " not for us, retransmitting...\n");
5719 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5720 peer_info = peer_info_get (&msg->oid);
5721 if (NULL == peer_info)
5723 /* If we know the tunnel, we should DEFINITELY know the peer */
5727 send_message (message, &id, t);
5733 * Functions to handle messages from core
5735 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5736 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5737 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5738 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5739 sizeof (struct GNUNET_MESH_PathBroken)},
5740 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5741 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5742 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5743 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5744 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5745 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5746 sizeof (struct GNUNET_MESH_ACK)},
5747 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5748 sizeof (struct GNUNET_MESH_PathACK)},
5754 /******************************************************************************/
5755 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5756 /******************************************************************************/
5759 * deregister_app: iterator for removing each application registered by a client
5761 * @param cls closure
5762 * @param key the hash of the application id (used to access the hashmap)
5763 * @param value the value stored at the key (client)
5765 * @return GNUNET_OK on success
5768 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5770 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5771 GNUNET_break (GNUNET_YES ==
5772 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5778 * notify_client_connection_failure: notify a client that the connection to the
5779 * requested remote peer is not possible (for instance, no route found)
5780 * Function called when the socket is ready to queue more data. "buf" will be
5781 * NULL and "size" zero if the socket was closed for writing in the meantime.
5783 * @param cls closure
5784 * @param size number of bytes available in buf
5785 * @param buf where the callee should write the message
5786 * @return number of bytes written to buf
5789 notify_client_connection_failure (void *cls, size_t size, void *buf)
5792 struct MeshPeerInfo *peer_info;
5793 struct GNUNET_MESH_PeerControl *msg;
5794 struct GNUNET_PeerIdentity id;
5796 if (0 == size && NULL == buf)
5798 // TODO retry? cancel?
5802 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5803 peer_info = (struct MeshPeerInfo *) cls;
5804 msg = (struct GNUNET_MESH_PeerControl *) buf;
5805 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5806 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5807 // msg->tunnel_id = htonl(peer_info->t->tid);
5808 GNUNET_PEER_resolve (peer_info->id, &id);
5809 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5817 * Send keepalive packets for a peer
5819 * @param cls Closure (tunnel for which to send the keepalive).
5820 * @param tc Notification context.
5822 * TODO: implement explicit multicast keepalive?
5825 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5827 struct MeshTunnel *t = cls;
5828 struct GNUNET_MessageHeader *payload;
5829 struct GNUNET_MESH_Multicast *msg;
5831 sizeof (struct GNUNET_MESH_Multicast) +
5832 sizeof (struct GNUNET_MessageHeader);
5835 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5839 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5842 "sending keepalive for tunnel %d\n", t->id.tid);
5844 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5845 msg->header.size = htons (size);
5846 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5847 // FIXME: change type to != MULTICAST
5848 msg->oid = my_full_id;
5849 msg->tid = htonl (t->id.tid);
5850 msg->ttl = htonl (default_ttl);
5851 msg->pid = htonl (t->fwd_pid + 1);
5853 payload = (struct GNUNET_MessageHeader *) &msg[1];
5854 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5855 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5856 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5858 t->path_refresh_task =
5859 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5865 * Function to process paths received for a new peer addition. The recorded
5866 * paths form the initial tunnel, which can be optimized later.
5867 * Called on each result obtained for the DHT search.
5869 * @param cls closure
5870 * @param exp when will this value expire
5871 * @param key key of the result
5872 * @param get_path path of the get request
5873 * @param get_path_length lenght of get_path
5874 * @param put_path path of the put request
5875 * @param put_path_length length of the put_path
5876 * @param type type of the result
5877 * @param size number of bytes in data
5878 * @param data pointer to the result data
5880 * TODO: re-issue the request after certain time? cancel after X results?
5883 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5884 const struct GNUNET_HashCode * key,
5885 const struct GNUNET_PeerIdentity *get_path,
5886 unsigned int get_path_length,
5887 const struct GNUNET_PeerIdentity *put_path,
5888 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5889 size_t size, const void *data)
5891 struct MeshPathInfo *path_info = cls;
5892 struct MeshPeerPath *p;
5893 struct GNUNET_PeerIdentity pi;
5896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5897 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5900 p = path_build_from_dht (get_path, get_path_length, put_path,
5902 path_add_to_peers (p, GNUNET_NO);
5904 for (i = 0; i < path_info->peer->ntunnels; i++)
5906 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5907 peer_info_connect (path_info->peer, path_info->t);
5915 * Function to process paths received for a new peer addition. The recorded
5916 * paths form the initial tunnel, which can be optimized later.
5917 * Called on each result obtained for the DHT search.
5919 * @param cls closure
5920 * @param exp when will this value expire
5921 * @param key key of the result
5922 * @param get_path path of the get request
5923 * @param get_path_length lenght of get_path
5924 * @param put_path path of the put request
5925 * @param put_path_length length of the put_path
5926 * @param type type of the result
5927 * @param size number of bytes in data
5928 * @param data pointer to the result data
5931 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5932 const struct GNUNET_HashCode * key,
5933 const struct GNUNET_PeerIdentity *get_path,
5934 unsigned int get_path_length,
5935 const struct GNUNET_PeerIdentity *put_path,
5936 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5937 size_t size, const void *data)
5939 const struct PBlock *pb = data;
5940 const struct GNUNET_PeerIdentity *pi = &pb->id;
5941 struct MeshTunnel *t = cls;
5942 struct MeshPeerInfo *peer_info;
5943 struct MeshPeerPath *p;
5945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5946 if (size != sizeof (struct PBlock))
5948 GNUNET_break_op (0);
5951 if (ntohl(pb->type) != t->type)
5953 GNUNET_break_op (0);
5956 GNUNET_assert (NULL != t->owner);
5957 peer_info = peer_info_get (pi);
5958 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5960 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5962 p = path_build_from_dht (get_path, get_path_length, put_path,
5964 path_add_to_peers (p, GNUNET_NO);
5966 tunnel_add_peer (t, peer_info);
5967 peer_info_connect (peer_info, t);
5972 * Function to process DHT string to regex matching.
5973 * Called on each result obtained for the DHT search.
5975 * @param cls closure (search context)
5976 * @param exp when will this value expire
5977 * @param key key of the result
5978 * @param get_path path of the get request (not used)
5979 * @param get_path_length lenght of get_path (not used)
5980 * @param put_path path of the put request (not used)
5981 * @param put_path_length length of the put_path (not used)
5982 * @param type type of the result
5983 * @param size number of bytes in data
5984 * @param data pointer to the result data
5987 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5988 const struct GNUNET_HashCode * key,
5989 const struct GNUNET_PeerIdentity *get_path,
5990 unsigned int get_path_length,
5991 const struct GNUNET_PeerIdentity *put_path,
5992 unsigned int put_path_length,
5993 enum GNUNET_BLOCK_Type type,
5994 size_t size, const void *data)
5996 const struct MeshRegexAccept *block = data;
5997 struct MeshRegexSearchContext *ctx = cls;
5998 struct MeshRegexSearchInfo *info = ctx->info;
5999 struct MeshPeerPath *p;
6000 struct MeshPeerInfo *peer_info;
6002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6005 peer_info = peer_info_get(&block->id);
6006 p = path_build_from_dht (get_path, get_path_length, put_path,
6008 path_add_to_peers (p, GNUNET_NO);
6011 tunnel_add_peer (info->t, peer_info);
6012 peer_info_connect (peer_info, info->t);
6013 if (0 == info->peer)
6015 info->peer = peer_info->id;
6019 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6022 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6023 ®ex_connect_timeout,
6031 * Function to process DHT string to regex matching.
6032 * Called on each result obtained for the DHT search.
6034 * @param cls closure (search context)
6035 * @param exp when will this value expire
6036 * @param key key of the result
6037 * @param get_path path of the get request (not used)
6038 * @param get_path_length lenght of get_path (not used)
6039 * @param put_path path of the put request (not used)
6040 * @param put_path_length length of the put_path (not used)
6041 * @param type type of the result
6042 * @param size number of bytes in data
6043 * @param data pointer to the result data
6045 * TODO: re-issue the request after certain time? cancel after X results?
6048 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6049 const struct GNUNET_HashCode * key,
6050 const struct GNUNET_PeerIdentity *get_path,
6051 unsigned int get_path_length,
6052 const struct GNUNET_PeerIdentity *put_path,
6053 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6054 size_t size, const void *data)
6056 const struct MeshRegexBlock *block = data;
6057 struct MeshRegexSearchContext *ctx = cls;
6058 struct MeshRegexSearchInfo *info = ctx->info;
6062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6063 "DHT GET STRING RETURNED RESULTS\n");
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6065 " key: %s\n", GNUNET_h2s (key));
6067 copy = GNUNET_malloc (size);
6068 memcpy (copy, data, size);
6069 GNUNET_break (GNUNET_OK ==
6070 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6071 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6072 len = ntohl (block->n_proof);
6074 char proof[len + 1];
6076 memcpy (proof, &block[1], len);
6078 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6080 GNUNET_break_op (0);
6084 len = strlen (info->description);
6085 if (len == ctx->position) // String processed
6087 if (GNUNET_YES == ntohl (block->accepting))
6089 regex_find_path(key, ctx);
6093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6094 // FIXME REGEX this block not successful, wait for more? start timeout?
6098 GNUNET_break (GNUNET_OK ==
6099 GNUNET_MESH_regex_block_iterate (block, size,
6100 ®ex_edge_iterator, ctx));
6104 /******************************************************************************/
6105 /********************* MESH LOCAL HANDLES **************************/
6106 /******************************************************************************/
6110 * Handler for client disconnection
6112 * @param cls closure
6113 * @param client identification of the client; NULL
6114 * for the last call when the server is destroyed
6117 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6119 struct MeshClient *c;
6120 struct MeshClient *next;
6123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6132 if (c->handle != client)
6134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6140 GNUNET_SERVER_client_drop (c->handle);
6141 c->shutting_down = GNUNET_YES;
6142 GNUNET_assert (NULL != c->own_tunnels);
6143 GNUNET_assert (NULL != c->incoming_tunnels);
6144 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6145 &tunnel_destroy_iterator, c);
6146 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6147 &tunnel_destroy_iterator, c);
6148 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6149 &tunnel_destroy_iterator, c);
6150 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6151 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6152 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6154 /* deregister clients applications */
6155 if (NULL != c->apps)
6157 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6158 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6160 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6161 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6163 GNUNET_SCHEDULER_cancel (announce_applications_task);
6164 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6166 if (NULL != c->types)
6167 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6168 for (i = 0; i < c->n_regex; i++)
6170 GNUNET_free (c->regexes[i]);
6172 GNUNET_free_non_null (c->regexes);
6173 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6174 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6176 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6179 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6188 * Handler for new clients
6190 * @param cls closure
6191 * @param client identification of the client
6192 * @param message the actual message, which includes messages the client wants
6195 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6196 const struct GNUNET_MessageHeader *message)
6198 struct GNUNET_MESH_ClientConnect *cc_msg;
6199 struct MeshClient *c;
6200 GNUNET_MESH_ApplicationType *a;
6207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6208 /* Check data sanity */
6209 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6210 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6211 ntypes = ntohs (cc_msg->types);
6212 napps = ntohs (cc_msg->applications);
6214 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6217 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6221 /* Create new client structure */
6222 c = GNUNET_malloc (sizeof (struct MeshClient));
6223 c->id = next_client_id++;
6224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6226 GNUNET_SERVER_client_keep (client);
6227 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6230 GNUNET_MESH_ApplicationType at;
6231 struct GNUNET_HashCode hc;
6233 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6234 for (i = 0; i < napps; i++)
6237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6238 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6239 /* store in clients hashmap */
6240 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6241 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6242 /* store in global hashmap, for announcements */
6243 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6244 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6246 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6247 announce_applications_task =
6248 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6254 struct GNUNET_HashCode hc;
6256 t = (uint16_t *) & a[napps];
6257 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6258 for (i = 0; i < ntypes; i++)
6261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6262 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6264 /* store in clients hashmap */
6265 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6266 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6267 /* store in global hashmap */
6268 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6269 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6273 " client has %u+%u subscriptions\n", napps, ntypes);
6275 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6276 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6277 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6278 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6279 GNUNET_SERVER_notification_context_add (nc, client);
6280 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6282 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6288 * Handler for clients announcing available services by a regular expression.
6290 * @param cls closure
6291 * @param client identification of the client
6292 * @param message the actual message, which includes messages the client wants
6295 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6296 const struct GNUNET_MessageHeader *message)
6298 struct MeshClient *c;
6302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6304 /* Sanity check for client registration */
6305 if (NULL == (c = client_get (client)))
6308 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6313 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6314 regex = GNUNET_malloc (len + 1);
6315 memcpy (regex, &message[1], len);
6317 GNUNET_array_append (c->regexes, c->n_regex, regex);
6318 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6320 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6326 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6332 * Handler for requests of new tunnels
6334 * @param cls closure
6335 * @param client identification of the client
6336 * @param message the actual message
6339 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6340 const struct GNUNET_MessageHeader *message)
6342 struct GNUNET_MESH_TunnelMessage *t_msg;
6343 struct MeshTunnel *t;
6344 struct MeshClient *c;
6345 MESH_TunnelNumber tid;
6347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6349 /* Sanity check for client registration */
6350 if (NULL == (c = client_get (client)))
6353 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6358 /* Message sanity check */
6359 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6362 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6366 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6367 /* Sanity check for tunnel numbering */
6368 tid = ntohl (t_msg->tunnel_id);
6369 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6375 /* Sanity check for duplicate tunnel IDs */
6376 if (NULL != tunnel_get_by_local_id (c, tid))
6379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6383 while (NULL != tunnel_get_by_pi (myid, next_tid))
6384 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6385 t = tunnel_new (myid, next_tid++, c, tid);
6388 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6393 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6395 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6396 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6399 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6405 * Handler for requests of deleting tunnels
6407 * @param cls closure
6408 * @param client identification of the client
6409 * @param message the actual message
6412 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6413 const struct GNUNET_MessageHeader *message)
6415 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6416 struct MeshClient *c;
6417 struct MeshTunnel *t;
6418 MESH_TunnelNumber tid;
6420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6421 "Got a DESTROY TUNNEL from client!\n");
6423 /* Sanity check for client registration */
6424 if (NULL == (c = client_get (client)))
6427 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6432 /* Message sanity check */
6433 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6440 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6442 /* Retrieve tunnel */
6443 tid = ntohl (tunnel_msg->tunnel_id);
6444 t = tunnel_get_by_local_id(c, tid);
6447 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6449 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6452 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6454 client_ignore_tunnel (c, t);
6456 // TODO: when to destroy incoming tunnel?
6457 if (t->nclients == 0)
6459 GNUNET_assert (GNUNET_YES ==
6460 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6462 GNUNET_assert (GNUNET_YES ==
6463 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6464 &my_full_id.hashPubKey,
6468 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6471 send_client_tunnel_disconnect(t, c);
6472 client_delete_tunnel(c, t);
6474 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6476 tunnel_send_destroy (t);
6477 t->destroy = GNUNET_YES;
6478 // The tunnel will be destroyed when the last message is transmitted.
6479 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6485 * Handler for requests of seeting tunnel's speed.
6487 * @param cls Closure (unused).
6488 * @param client Identification of the client.
6489 * @param message The actual message.
6492 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6493 const struct GNUNET_MessageHeader *message)
6495 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6496 struct MeshClient *c;
6497 struct MeshTunnel *t;
6498 MESH_TunnelNumber tid;
6500 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6501 "Got a SPEED request from client!\n");
6503 /* Sanity check for client registration */
6504 if (NULL == (c = client_get (client)))
6507 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6513 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6515 /* Retrieve tunnel */
6516 tid = ntohl (tunnel_msg->tunnel_id);
6517 t = tunnel_get_by_local_id(c, tid);
6520 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6522 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6526 switch (ntohs(message->type))
6528 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6529 t->speed_min = GNUNET_YES;
6531 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6532 t->speed_min = GNUNET_NO;
6537 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6542 * Handler for requests of seeting tunnel's buffering policy.
6544 * @param cls Closure (unused).
6545 * @param client Identification of the client.
6546 * @param message The actual message.
6549 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6550 const struct GNUNET_MessageHeader *message)
6552 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6553 struct MeshClient *c;
6554 struct MeshTunnel *t;
6555 MESH_TunnelNumber tid;
6557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6558 "Got a BUFFER request from client!\n");
6560 /* Sanity check for client registration */
6561 if (NULL == (c = client_get (client)))
6564 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6569 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6571 /* Retrieve tunnel */
6572 tid = ntohl (tunnel_msg->tunnel_id);
6573 t = tunnel_get_by_local_id(c, tid);
6576 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6578 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6582 switch (ntohs(message->type))
6584 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6585 t->nobuffer = GNUNET_NO;
6587 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6588 t->nobuffer = GNUNET_YES;
6594 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6599 * Handler for connection requests to new peers
6601 * @param cls closure
6602 * @param client identification of the client
6603 * @param message the actual message (PeerControl)
6606 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6607 const struct GNUNET_MessageHeader *message)
6609 struct GNUNET_MESH_PeerControl *peer_msg;
6610 struct MeshPeerInfo *peer_info;
6611 struct MeshClient *c;
6612 struct MeshTunnel *t;
6613 MESH_TunnelNumber tid;
6615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6616 /* Sanity check for client registration */
6617 if (NULL == (c = client_get (client)))
6620 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6625 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6627 /* Sanity check for message size */
6628 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6631 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6635 /* Tunnel exists? */
6636 tid = ntohl (peer_msg->tunnel_id);
6637 t = tunnel_get_by_local_id (c, tid);
6641 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6645 /* Does client own tunnel? */
6646 if (t->owner->handle != client)
6649 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6653 GNUNET_i2s (&peer_msg->peer));
6654 peer_info = peer_info_get (&peer_msg->peer);
6656 tunnel_add_peer (t, peer_info);
6657 peer_info_connect (peer_info, t);
6659 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6665 * Handler for disconnection requests of peers in a tunnel
6667 * @param cls closure
6668 * @param client identification of the client
6669 * @param message the actual message (PeerControl)
6672 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6673 const struct GNUNET_MessageHeader *message)
6675 struct GNUNET_MESH_PeerControl *peer_msg;
6676 struct MeshPeerInfo *peer_info;
6677 struct MeshClient *c;
6678 struct MeshTunnel *t;
6679 MESH_TunnelNumber tid;
6681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6682 /* Sanity check for client registration */
6683 if (NULL == (c = client_get (client)))
6686 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6691 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6693 /* Sanity check for message size */
6694 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6697 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6701 /* Tunnel exists? */
6702 tid = ntohl (peer_msg->tunnel_id);
6703 t = tunnel_get_by_local_id (c, tid);
6707 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6712 /* Does client own tunnel? */
6713 if (t->owner->handle != client)
6716 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6721 GNUNET_i2s (&peer_msg->peer));
6722 /* Is the peer in the tunnel? */
6724 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6725 if (NULL == peer_info)
6728 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6732 /* Ok, delete peer from tunnel */
6733 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6734 &peer_msg->peer.hashPubKey);
6736 send_destroy_path (t, peer_info->id);
6737 tunnel_delete_peer (t, peer_info->id);
6738 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6743 * Handler for blacklist requests of peers in a tunnel
6745 * @param cls closure
6746 * @param client identification of the client
6747 * @param message the actual message (PeerControl)
6749 * FIXME implement DHT block bloomfilter
6752 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6753 const struct GNUNET_MessageHeader *message)
6755 struct GNUNET_MESH_PeerControl *peer_msg;
6756 struct MeshClient *c;
6757 struct MeshTunnel *t;
6758 MESH_TunnelNumber tid;
6760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6761 /* Sanity check for client registration */
6762 if (NULL == (c = client_get (client)))
6765 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6770 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6772 /* Sanity check for message size */
6773 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6776 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6780 /* Tunnel exists? */
6781 tid = ntohl (peer_msg->tunnel_id);
6782 t = tunnel_get_by_local_id (c, tid);
6786 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6791 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6792 GNUNET_PEER_intern(&peer_msg->peer));
6797 * Handler for unblacklist requests of peers in a tunnel
6799 * @param cls closure
6800 * @param client identification of the client
6801 * @param message the actual message (PeerControl)
6804 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6805 const struct GNUNET_MessageHeader *message)
6807 struct GNUNET_MESH_PeerControl *peer_msg;
6808 struct MeshClient *c;
6809 struct MeshTunnel *t;
6810 MESH_TunnelNumber tid;
6814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6815 /* Sanity check for client registration */
6816 if (NULL == (c = client_get (client)))
6819 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6824 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6826 /* Sanity check for message size */
6827 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6830 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6834 /* Tunnel exists? */
6835 tid = ntohl (peer_msg->tunnel_id);
6836 t = tunnel_get_by_local_id (c, tid);
6840 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6845 /* if peer is not known, complain */
6846 pid = GNUNET_PEER_search (&peer_msg->peer);
6853 /* search and remove from list */
6854 for (i = 0; i < t->nblacklisted; i++)
6856 if (t->blacklisted[i] == pid)
6858 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6859 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6864 /* if peer hasn't been blacklisted, complain */
6870 * Handler for connection requests to new peers by type
6872 * @param cls closure
6873 * @param client identification of the client
6874 * @param message the actual message (ConnectPeerByType)
6877 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6878 const struct GNUNET_MessageHeader *message)
6880 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6881 struct MeshClient *c;
6882 struct MeshTunnel *t;
6883 struct GNUNET_HashCode hash;
6884 MESH_TunnelNumber tid;
6886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6887 /* Sanity check for client registration */
6888 if (NULL == (c = client_get (client)))
6891 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6896 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6898 /* Sanity check for message size */
6899 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6900 ntohs (connect_msg->header.size))
6903 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6907 /* Tunnel exists? */
6908 tid = ntohl (connect_msg->tunnel_id);
6909 t = tunnel_get_by_local_id (c, tid);
6913 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6917 /* Does client own tunnel? */
6918 if (t->owner->handle != client)
6921 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6925 /* Do WE have the service? */
6926 t->type = ntohl (connect_msg->type);
6927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6928 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6929 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6932 /* Yes! Fast forward, add ourselves to the tunnel and send the
6933 * good news to the client, and alert the destination client of
6934 * an incoming tunnel.
6936 * FIXME send a path create to self, avoid code duplication
6938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6939 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6940 peer_info_get (&my_full_id),
6941 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6944 send_client_peer_connected (t, myid);
6945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6946 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6948 t->local_tid_dest = next_local_tid++;
6949 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6950 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6951 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6955 /* Ok, lets find a peer offering the service */
6956 if (NULL != t->dht_get_type)
6958 GNUNET_DHT_get_stop (t->dht_get_type);
6960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6961 GNUNET_h2s (&hash));
6963 GNUNET_DHT_get_start (dht_handle,
6964 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6966 dht_replication_level,
6967 GNUNET_DHT_RO_RECORD_ROUTE |
6968 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6970 &dht_get_type_handler, t);
6972 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6978 * Handler for connection requests to new peers by a string service description.
6980 * @param cls closure
6981 * @param client identification of the client
6982 * @param message the actual message, which includes messages the client wants
6985 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6986 const struct GNUNET_MessageHeader *message)
6988 struct GNUNET_MESH_ConnectPeerByString *msg;
6989 struct MeshRegexSearchContext *ctx;
6990 struct MeshRegexSearchInfo *info;
6991 struct GNUNET_DHT_GetHandle *get_h;
6992 struct GNUNET_HashCode key;
6993 struct MeshTunnel *t;
6994 struct MeshClient *c;
6995 MESH_TunnelNumber tid;
6999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7000 "Connect by string started\n");
7001 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7002 size = htons (message->size);
7004 /* Sanity check for client registration */
7005 if (NULL == (c = client_get (client)))
7008 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7013 /* Message size sanity check */
7014 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7017 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7021 /* Tunnel exists? */
7022 tid = ntohl (msg->tunnel_id);
7023 t = tunnel_get_by_local_id (c, tid);
7027 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7031 /* Does client own tunnel? */
7032 if (t->owner->handle != client)
7035 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7040 " on tunnel %s [%u]\n",
7041 GNUNET_i2s(&my_full_id),
7044 /* Only one connect_by_string allowed at the same time! */
7045 /* FIXME: allow more, return handle at api level to cancel, document */
7046 if (NULL != t->regex_ctx)
7049 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7053 /* Find string itself */
7054 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7055 string = (const char *) &msg[1];
7057 /* Initialize context */
7058 size = GNUNET_REGEX_get_first_key(string, len, &key);
7059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7060 " consumed %u bits out of %u\n", size, len);
7061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7062 " looking for %s\n", GNUNET_h2s (&key));
7064 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7066 info->description = GNUNET_malloc (len + 1);
7067 memcpy (info->description, string, len);
7068 info->description[len] = '\0';
7069 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7070 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7073 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7074 ctx->position = size;
7078 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7080 /* Start search in DHT */
7081 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7082 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7083 &key, /* key to search */
7084 dht_replication_level, /* replication level */
7085 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7086 NULL, /* xquery */ // FIXME BLOOMFILTER
7087 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7088 &dht_get_string_handler, ctx);
7090 GNUNET_break (GNUNET_OK ==
7091 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7094 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7096 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7102 * Handler for client traffic directed to one peer
7104 * @param cls closure
7105 * @param client identification of the client
7106 * @param message the actual message
7109 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7110 const struct GNUNET_MessageHeader *message)
7112 struct MeshClient *c;
7113 struct MeshTunnel *t;
7114 struct MeshPeerInfo *pi;
7115 struct GNUNET_MESH_Unicast *data_msg;
7116 MESH_TunnelNumber tid;
7119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7120 "Got a unicast request from a client!\n");
7122 /* Sanity check for client registration */
7123 if (NULL == (c = client_get (client)))
7126 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7131 data_msg = (struct GNUNET_MESH_Unicast *) message;
7133 /* Sanity check for message size */
7134 size = ntohs (message->size);
7135 if (sizeof (struct GNUNET_MESH_Unicast) +
7136 sizeof (struct GNUNET_MessageHeader) > size)
7139 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7143 /* Tunnel exists? */
7144 tid = ntohl (data_msg->tid);
7145 t = tunnel_get_by_local_id (c, tid);
7149 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7153 /* Is it a local tunnel? Then, does client own the tunnel? */
7154 if (t->owner->handle != client)
7157 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7161 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7162 &data_msg->destination.hashPubKey);
7163 /* Is the selected peer in the tunnel? */
7167 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7171 /* PID should be as expected */
7172 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7175 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7176 "Unicast PID, expected %u, got %u\n",
7177 t->fwd_pid + 1, ntohl (data_msg->pid));
7178 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7182 /* Ok, everything is correct, send the message
7183 * (pretend we got it from a mesh peer)
7186 /* Work around const limitation */
7187 char buf[ntohs (message->size)] GNUNET_ALIGN;
7188 struct GNUNET_MESH_Unicast *copy;
7190 copy = (struct GNUNET_MESH_Unicast *) buf;
7191 memcpy (buf, data_msg, size);
7192 copy->oid = my_full_id;
7193 copy->tid = htonl (t->id.tid);
7194 copy->ttl = htonl (default_ttl);
7195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7196 " calling generic handler...\n");
7197 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7200 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7207 * Handler for client traffic directed to the origin
7209 * @param cls closure
7210 * @param client identification of the client
7211 * @param message the actual message
7214 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7215 const struct GNUNET_MessageHeader *message)
7217 struct GNUNET_MESH_ToOrigin *data_msg;
7218 struct MeshTunnelClientInfo *clinfo;
7219 struct MeshClient *c;
7220 struct MeshTunnel *t;
7221 MESH_TunnelNumber tid;
7224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7225 "Got a ToOrigin request from a client!\n");
7226 /* Sanity check for client registration */
7227 if (NULL == (c = client_get (client)))
7230 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7235 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7237 /* Sanity check for message size */
7238 size = ntohs (message->size);
7239 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7240 sizeof (struct GNUNET_MessageHeader) > size)
7243 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7247 /* Tunnel exists? */
7248 tid = ntohl (data_msg->tid);
7249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7250 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7253 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7256 t = tunnel_get_by_local_id (c, tid);
7259 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7262 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7266 /* It should be sent by someone who has this as incoming tunnel. */
7267 if (GNUNET_NO == client_knows_tunnel (c, t))
7270 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7274 /* PID should be as expected */
7275 clinfo = tunnel_get_client_fc (t, c);
7276 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7279 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7280 "To Origin PID, expected %u, got %u\n",
7281 clinfo->bck_pid + 1,
7282 ntohl (data_msg->pid));
7283 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7287 /* Ok, everything is correct, send the message
7288 * (pretend we got it from a mesh peer)
7292 char buf[ntohs (message->size)] GNUNET_ALIGN;
7293 struct GNUNET_MESH_ToOrigin *copy;
7295 /* Work around const limitation */
7296 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7297 memcpy (buf, data_msg, size);
7298 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7299 copy->tid = htonl (t->id.tid);
7300 copy->ttl = htonl (default_ttl);
7301 if (ntohl (copy->pid) != (t->bck_pid + 1))
7304 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7305 "To Origin PID, expected %u, got %u\n",
7311 copy->sender = my_full_id;
7312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7313 " calling generic handler...\n");
7314 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7316 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7323 * Handler for client traffic directed to all peers in a tunnel
7325 * @param cls closure
7326 * @param client identification of the client
7327 * @param message the actual message
7330 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7331 const struct GNUNET_MessageHeader *message)
7333 struct MeshClient *c;
7334 struct MeshTunnel *t;
7335 struct GNUNET_MESH_Multicast *data_msg;
7336 MESH_TunnelNumber tid;
7338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7339 "Got a multicast request from a client!\n");
7341 /* Sanity check for client registration */
7342 if (NULL == (c = client_get (client)))
7345 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7350 data_msg = (struct GNUNET_MESH_Multicast *) message;
7352 /* Sanity check for message size */
7353 if (sizeof (struct GNUNET_MESH_Multicast) +
7354 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7357 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7361 /* Tunnel exists? */
7362 tid = ntohl (data_msg->tid);
7363 t = tunnel_get_by_local_id (c, tid);
7367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7370 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7374 /* Does client own tunnel? */
7375 if (t->owner->handle != client)
7378 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7382 /* PID should be as expected */
7383 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7386 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7387 "Multicast PID, expected %u, got %u\n",
7388 t->fwd_pid + 1, ntohl (data_msg->pid));
7389 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7394 char buf[ntohs (message->size)] GNUNET_ALIGN;
7395 struct GNUNET_MESH_Multicast *copy;
7397 copy = (struct GNUNET_MESH_Multicast *) buf;
7398 memcpy (buf, message, ntohs (message->size));
7399 copy->oid = my_full_id;
7400 copy->tid = htonl (t->id.tid);
7401 copy->ttl = htonl (default_ttl);
7402 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7404 " calling generic handler...\n");
7405 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7408 /* receive done gets called when last copy is sent to a neighbor */
7414 * Handler for client's ACKs for payload traffic.
7416 * @param cls Closure (unused).
7417 * @param client Identification of the client.
7418 * @param message The actual message.
7421 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7422 const struct GNUNET_MessageHeader *message)
7424 struct GNUNET_MESH_LocalAck *msg;
7425 struct MeshTunnel *t;
7426 struct MeshClient *c;
7427 MESH_TunnelNumber tid;
7430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7431 /* Sanity check for client registration */
7432 if (NULL == (c = client_get (client)))
7435 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7440 msg = (struct GNUNET_MESH_LocalAck *) message;
7442 /* Tunnel exists? */
7443 tid = ntohl (msg->tunnel_id);
7444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7445 t = tunnel_get_by_local_id (c, tid);
7449 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7450 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7455 ack = ntohl (msg->max_pid);
7456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7458 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7459 if (NULL != t->owner && t->owner->handle == client)
7461 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7463 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7467 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7468 tunnel_set_client_fwd_ack (t, c, ack);
7469 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7472 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7479 * Functions to handle messages from clients
7481 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7482 {&handle_local_new_client, NULL,
7483 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7484 {&handle_local_announce_regex, NULL,
7485 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7486 {&handle_local_tunnel_create, NULL,
7487 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7488 sizeof (struct GNUNET_MESH_TunnelMessage)},
7489 {&handle_local_tunnel_destroy, NULL,
7490 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7491 sizeof (struct GNUNET_MESH_TunnelMessage)},
7492 {&handle_local_tunnel_speed, NULL,
7493 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7494 sizeof (struct GNUNET_MESH_TunnelMessage)},
7495 {&handle_local_tunnel_speed, NULL,
7496 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7497 sizeof (struct GNUNET_MESH_TunnelMessage)},
7498 {&handle_local_tunnel_buffer, NULL,
7499 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7500 sizeof (struct GNUNET_MESH_TunnelMessage)},
7501 {&handle_local_tunnel_buffer, NULL,
7502 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7503 sizeof (struct GNUNET_MESH_TunnelMessage)},
7504 {&handle_local_connect_add, NULL,
7505 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7506 sizeof (struct GNUNET_MESH_PeerControl)},
7507 {&handle_local_connect_del, NULL,
7508 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7509 sizeof (struct GNUNET_MESH_PeerControl)},
7510 {&handle_local_blacklist, NULL,
7511 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7512 sizeof (struct GNUNET_MESH_PeerControl)},
7513 {&handle_local_unblacklist, NULL,
7514 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7515 sizeof (struct GNUNET_MESH_PeerControl)},
7516 {&handle_local_connect_by_type, NULL,
7517 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7518 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7519 {&handle_local_connect_by_string, NULL,
7520 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7521 {&handle_local_unicast, NULL,
7522 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7523 {&handle_local_to_origin, NULL,
7524 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7525 {&handle_local_multicast, NULL,
7526 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7527 {&handle_local_ack, NULL,
7528 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7529 sizeof (struct GNUNET_MESH_LocalAck)},
7535 * To be called on core init/fail.
7537 * @param cls service closure
7538 * @param server handle to the server for this service
7539 * @param identity the public identity of this peer
7542 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7543 const struct GNUNET_PeerIdentity *identity)
7546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7547 core_handle = server;
7548 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7552 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7554 GNUNET_abort(); // Try harder
7561 * Method called whenever a given peer connects.
7563 * @param cls closure
7564 * @param peer peer identity this notification is about
7565 * @param atsi performance data for the connection
7566 * @param atsi_count number of records in 'atsi'
7569 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7570 const struct GNUNET_ATS_Information *atsi,
7571 unsigned int atsi_count)
7573 struct MeshPeerInfo *peer_info;
7574 struct MeshPeerPath *path;
7576 DEBUG_CONN ("Peer connected\n");
7577 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7578 peer_info = peer_info_get (peer);
7579 if (myid == peer_info->id)
7581 DEBUG_CONN (" (self)\n");
7586 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7588 path = path_new (2);
7589 path->peers[0] = myid;
7590 path->peers[1] = peer_info->id;
7591 GNUNET_PEER_change_rc (myid, 1);
7592 GNUNET_PEER_change_rc (peer_info->id, 1);
7593 peer_info_add_path (peer_info, path, GNUNET_YES);
7594 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7600 * Method called whenever a peer disconnects.
7602 * @param cls closure
7603 * @param peer peer identity this notification is about
7606 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7608 struct MeshPeerInfo *pi;
7609 struct MeshPeerQueue *q;
7610 struct MeshPeerQueue *n;
7612 DEBUG_CONN ("Peer disconnected\n");
7613 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7625 /* try to reroute this traffic instead */
7626 queue_destroy(q, GNUNET_YES);
7630 peer_info_remove_path (pi, pi->id, myid);
7633 DEBUG_CONN (" (self)\n");
7635 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7640 /******************************************************************************/
7641 /************************ MAIN FUNCTIONS ****************************/
7642 /******************************************************************************/
7645 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7647 * @param cls closure
7648 * @param key current key code
7649 * @param value value in the hash map
7650 * @return GNUNET_YES if we should continue to iterate,
7654 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7656 struct MeshTunnel *t = value;
7663 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7665 * @param cls closure
7666 * @param key current key code
7667 * @param value value in the hash map
7668 * @return GNUNET_YES if we should continue to iterate,
7672 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7674 struct MeshPeerInfo *p = value;
7675 struct MeshPeerQueue *q;
7676 struct MeshPeerQueue *n;
7684 queue_destroy(q, GNUNET_YES);
7688 peer_info_destroy (p);
7693 * Task run during shutdown.
7699 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7703 if (core_handle != NULL)
7705 GNUNET_CORE_disconnect (core_handle);
7708 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7709 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7710 if (dht_handle != NULL)
7712 GNUNET_DHT_disconnect (dht_handle);
7717 GNUNET_SERVER_notification_context_destroy (nc);
7720 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7722 GNUNET_SCHEDULER_cancel (announce_id_task);
7723 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7729 * Process mesh requests.
7731 * @param cls closure
7732 * @param server the initialized server
7733 * @param c configuration to use
7736 run (void *cls, struct GNUNET_SERVER_Handle *server,
7737 const struct GNUNET_CONFIGURATION_Handle *c)
7739 struct MeshPeerInfo *peer;
7740 struct MeshPeerPath *p;
7743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7744 server_handle = server;
7745 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7746 NULL, /* Closure passed to MESH functions */
7747 &core_init, /* Call core_init once connected */
7748 &core_connect, /* Handle connects */
7749 &core_disconnect, /* remove peers on disconnects */
7750 NULL, /* Don't notify about all incoming messages */
7751 GNUNET_NO, /* For header only in notification */
7752 NULL, /* Don't notify about all outbound messages */
7753 GNUNET_NO, /* For header-only out notification */
7754 core_handlers); /* Register these handlers */
7756 if (core_handle == NULL)
7759 GNUNET_SCHEDULER_shutdown ();
7764 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7767 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7769 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7771 GNUNET_SCHEDULER_shutdown ();
7776 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7777 &refresh_path_time))
7779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7781 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7782 "refresh path time");
7783 GNUNET_SCHEDULER_shutdown ();
7788 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7789 &app_announce_time))
7791 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7793 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7794 "app announce time");
7795 GNUNET_SCHEDULER_shutdown ();
7800 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7805 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7806 "id announce time");
7807 GNUNET_SCHEDULER_shutdown ();
7812 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7813 &unacknowledged_wait_time))
7815 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7817 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7818 "unacknowledged wait time");
7819 GNUNET_SCHEDULER_shutdown ();
7824 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7829 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7831 GNUNET_SCHEDULER_shutdown ();
7836 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7839 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7841 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7843 GNUNET_SCHEDULER_shutdown ();
7848 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7851 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7853 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7855 GNUNET_SCHEDULER_shutdown ();
7860 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7863 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7865 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7871 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7872 &dht_replication_level))
7874 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7876 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7877 "dht replication level", 10);
7878 dht_replication_level = 10;
7882 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7883 GNUNET_free (keyfile);
7884 if (my_private_key == NULL)
7886 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7887 _("Mesh service could not access hostkey. Exiting.\n"));
7888 GNUNET_SCHEDULER_shutdown ();
7891 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7892 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7893 &my_full_id.hashPubKey);
7894 myid = GNUNET_PEER_intern (&my_full_id);
7895 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7896 "Mesh for peer [%s] starting\n",
7897 GNUNET_i2s(&my_full_id));
7899 // transport_handle = GNUNET_TRANSPORT_connect(c,
7906 dht_handle = GNUNET_DHT_connect (c, 64);
7907 if (dht_handle == NULL)
7912 stats = GNUNET_STATISTICS_create ("mesh", c);
7916 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7918 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7919 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7920 peers = GNUNET_CONTAINER_multihashmap_create (32);
7921 applications = GNUNET_CONTAINER_multihashmap_create (32);
7922 types = GNUNET_CONTAINER_multihashmap_create (32);
7924 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7925 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7926 GNUNET_SERVER_disconnect_notify (server_handle,
7927 &handle_local_client_disconnect, NULL);
7931 clients_tail = NULL;
7934 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7935 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7937 /* Create a peer_info for the local peer */
7938 peer = peer_info_get (&my_full_id);
7941 GNUNET_PEER_change_rc (myid, 1);
7942 peer_info_add_path (peer, p, GNUNET_YES);
7944 /* Scheduled the task to clean up when shutdown is called */
7945 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7952 * The main function for the mesh service.
7954 * @param argc number of arguments from the command line
7955 * @param argv command line arguments
7956 * @return 0 ok, 1 on error
7959 main (int argc, char *const *argv)
7963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7966 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7973 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
7974 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);