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 GNUNET_assert(NULL != c);
1639 block.type = (long) GNUNET_CONTAINER_multihashmap_get (c->apps, key);
1640 if (0 == block.type)
1645 block.type = htonl (block.type);
1647 GNUNET_break (NULL !=
1648 GNUNET_DHT_put (dht_handle, key,
1649 dht_replication_level,
1650 GNUNET_DHT_RO_RECORD_ROUTE |
1651 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
1652 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
1654 (const char *) &block,
1655 GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get (),
1657 app_announce_time, NULL, NULL));
1663 * Periodically announce what applications are provided by local clients
1666 * @param cls closure
1667 * @param tc task context
1670 announce_regex (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1672 struct MeshClient *c = cls;
1675 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1677 c->regex_announce_task = GNUNET_SCHEDULER_NO_TASK;
1681 DEBUG_DHT ("Starting PUT for regex\n");
1683 for (i = 0; i < c->n_regex; i++)
1685 regex_put (c->regexes[i]);
1687 c->regex_announce_task =
1688 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_regex, cls);
1689 DEBUG_DHT ("Finished PUT for regex\n");
1696 * Periodically announce what applications are provided by local clients
1699 * @param cls closure
1700 * @param tc task context
1703 announce_applications (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1705 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1707 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
1711 DEBUG_DHT ("Starting PUT for apps\n");
1713 GNUNET_CONTAINER_multihashmap_iterate (applications, &announce_application,
1715 announce_applications_task =
1716 GNUNET_SCHEDULER_add_delayed (app_announce_time, &announce_applications,
1718 DEBUG_DHT ("Finished PUT for apps\n");
1725 * Periodically announce self id in the DHT
1727 * @param cls closure
1728 * @param tc task context
1731 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1733 struct PBlock block;
1735 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1737 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1741 * - Set data expiration in function of X
1742 * - Adapt X to churn
1744 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1746 block.id = my_full_id;
1747 block.type = htonl (0);
1748 GNUNET_DHT_put (dht_handle, /* DHT handle */
1749 &my_full_id.hashPubKey, /* Key to use */
1750 dht_replication_level, /* Replication level */
1751 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1752 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1753 sizeof (block), /* Size of the data */
1754 (const char *) &block, /* Data itself */
1755 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1756 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1757 NULL, /* Continuation */
1758 NULL); /* Continuation closure */
1760 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1764 /******************************************************************************/
1765 /****************** GENERAL HELPER FUNCTIONS ************************/
1766 /******************************************************************************/
1769 * Decrements the reference counter and frees all resources if needed
1771 * @param mesh_data Data Descriptor used in a multicast message.
1772 * Freed no longer needed (last message).
1775 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1777 /* Make sure it's a multicast packet */
1778 GNUNET_assert (NULL != mesh_data->reference_counter);
1780 if (0 == --(*(mesh_data->reference_counter)))
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1783 if (NULL != mesh_data->task)
1785 if (GNUNET_SCHEDULER_NO_TASK != *(mesh_data->task))
1787 GNUNET_SCHEDULER_cancel (*(mesh_data->task));
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client...\n");
1789 GNUNET_SERVER_receive_done (mesh_data->t->owner->handle, GNUNET_OK);
1791 GNUNET_free (mesh_data->task);
1793 GNUNET_free (mesh_data->reference_counter);
1794 GNUNET_free (mesh_data->data);
1795 GNUNET_free (mesh_data);
1801 * Check if client has registered with the service and has not disconnected
1803 * @param client the client to check
1805 * @return non-NULL if client exists in the global DLL
1807 static struct MeshClient *
1808 client_get (struct GNUNET_SERVER_Client *client)
1810 struct MeshClient *c;
1815 if (c->handle == client)
1824 * Checks if a given client has subscribed to certain message type
1826 * @param message_type Type of message to check
1827 * @param c Client to check
1829 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1831 * FIXME: use of crypto_hash slows it down
1832 * The hash function alone takes 8-10us out of the ~55us for the whole
1833 * process of retransmitting the message from one local client to another.
1834 * Find faster implementation!
1837 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1839 struct GNUNET_HashCode hc;
1841 if (NULL == c->types)
1844 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1845 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1850 * Allow a client to send more data after transmitting a multicast message
1851 * which some neighbor has not yet accepted altough a reasonable time has
1854 * @param cls Closure (DataDescriptor containing the task identifier)
1855 * @param tc Task Context
1857 * FIXME reference counter cshould be just int
1860 client_allow_send (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1862 struct MeshData *mdata = cls;
1864 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1866 GNUNET_assert (NULL != mdata->reference_counter);
1867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1868 "CLIENT ALLOW SEND DESPITE %u COPIES PENDING\n",
1869 *(mdata->reference_counter));
1870 *(mdata->task) = GNUNET_SCHEDULER_NO_TASK;
1871 GNUNET_SERVER_receive_done (mdata->t->owner->handle, GNUNET_OK);
1876 * Check whether client wants traffic from a tunnel.
1878 * @param c Client to check.
1879 * @param t Tunnel to be found.
1881 * @return GNUNET_YES if client knows tunnel.
1883 * TODO look in client hashmap
1886 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1890 for (i = 0; i < t->nclients; i++)
1891 if (t->clients[i] == c)
1898 * Check whether client has been informed about a tunnel.
1900 * @param c Client to check.
1901 * @param t Tunnel to be found.
1903 * @return GNUNET_YES if client knows tunnel.
1905 * TODO look in client hashmap
1908 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1912 for (i = 0; i < t->nignore; i++)
1913 if (t->ignore[i] == c)
1915 return client_wants_tunnel(c, t);
1920 * Marks a client as uninterested in traffic from the tunnel, updating both
1921 * client and tunnel to reflect this.
1923 * @param c Client that doesn't want traffic anymore.
1924 * @param t Tunnel which should be ignored.
1926 * FIXME when to delete an incoming tunnel?
1929 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1931 struct GNUNET_HashCode hash;
1933 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1934 GNUNET_break (GNUNET_YES ==
1935 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1937 GNUNET_break (GNUNET_YES ==
1938 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1939 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1940 tunnel_delete_active_client (t, c);
1941 GNUNET_array_append (t->ignore, t->nignore, c);
1946 * Deletes a tunnel from a client (either owner or destination). To be used on
1947 * tunnel destroy, otherwise, use client_ignore_tunnel.
1949 * @param c Client whose tunnel to delete.
1950 * @param t Tunnel which should be deleted.
1953 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1955 struct GNUNET_HashCode hash;
1959 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1960 GNUNET_assert (GNUNET_YES ==
1961 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1967 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1969 GNUNET_assert (GNUNET_YES ==
1970 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1974 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1982 * Send the message to all clients that have subscribed to its type
1984 * @param msg Pointer to the message itself
1985 * @param payload Pointer to the payload of the message.
1986 * @param t The tunnel to whose clients this message goes.
1988 * @return number of clients this message was sent to
1991 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1992 const struct GNUNET_MessageHeader *payload,
1993 struct MeshTunnel *t)
1995 struct MeshClient *c;
1996 MESH_TunnelNumber *tid;
1999 char cbuf[htons (msg->size)];
2001 type = ntohs (payload->type);
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
2003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
2004 GNUNET_MESH_DEBUG_M2S (type));
2006 memcpy (cbuf, msg, sizeof (cbuf));
2007 switch (htons (msg->type))
2009 struct GNUNET_MESH_Unicast *uc;
2010 struct GNUNET_MESH_Multicast *mc;
2011 struct GNUNET_MESH_ToOrigin *to;
2013 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
2014 uc = (struct GNUNET_MESH_Unicast *) cbuf;
2017 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
2018 mc = (struct GNUNET_MESH_Multicast *) cbuf;
2021 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
2022 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
2030 for (count = 0, c = clients; c != NULL; c = c->next)
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
2033 if (client_is_subscribed (type, c))
2035 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
2039 *tid = htonl (t->local_tid);
2043 if (GNUNET_NO == client_knows_tunnel (c, t))
2045 /* This client doesn't know the tunnel */
2046 struct GNUNET_MESH_TunnelNotification tmsg;
2047 struct GNUNET_HashCode hash;
2049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
2050 tmsg.header.size = htons (sizeof (tmsg));
2051 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
2052 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
2053 tmsg.tunnel_id = htonl (t->local_tid_dest);
2054 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2055 &tmsg.header, GNUNET_NO);
2056 tunnel_add_client (t, c);
2057 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2059 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
2060 c->incoming_tunnels, &hash, t,
2061 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
2063 *tid = htonl (t->local_tid_dest);
2066 /* Check if the client wants to get traffic from the tunnel */
2067 if (GNUNET_NO == client_wants_tunnel(c, t))
2070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
2071 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
2072 (struct GNUNET_MessageHeader
2073 *) cbuf, GNUNET_NO);
2082 * Notify the client that owns the tunnel that a peer has connected to it
2083 * (the requested path to it has been confirmed).
2085 * @param t Tunnel whose owner to notify
2086 * @param id Short id of the peer that has connected
2089 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
2091 struct GNUNET_MESH_PeerControl pc;
2093 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
2094 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
2095 pc.tunnel_id = htonl (t->local_tid);
2096 GNUNET_PEER_resolve (id, &pc.peer);
2097 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
2103 * Notify all clients (not depending on registration status) that the incoming
2104 * tunnel is no longer valid.
2106 * @param t Tunnel that was destroyed.
2109 send_clients_tunnel_destroy (struct MeshTunnel *t)
2111 struct GNUNET_MESH_TunnelMessage msg;
2113 msg.header.size = htons (sizeof (msg));
2114 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2115 msg.tunnel_id = htonl (t->local_tid_dest);
2116 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
2121 * Notify clients of tunnel disconnections, if needed.
2122 * In case the origin disconnects, the destination clients get a tunnel destroy
2123 * notification. If the last destination disconnects (only one remaining client
2124 * in tunnel), the origin gets a (local ID) peer disconnected.
2125 * Note that the function must be called BEFORE removing the client from
2128 * @param t Tunnel that was destroyed.
2129 * @param c Client that disconnected.
2132 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
2138 struct GNUNET_MESH_TunnelMessage msg;
2140 msg.header.size = htons (sizeof (msg));
2141 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
2142 msg.tunnel_id = htonl (t->local_tid_dest);
2143 for (i = 0; i < t->nclients; i++)
2144 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
2145 &msg.header, GNUNET_NO);
2147 // FIXME when to disconnect an incoming tunnel?
2148 else if (1 == t->nclients && NULL != t->owner)
2150 struct GNUNET_MESH_PeerControl msg;
2152 msg.header.size = htons (sizeof (msg));
2153 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
2154 msg.tunnel_id = htonl (t->local_tid);
2155 msg.peer = my_full_id;
2156 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
2157 &msg.header, GNUNET_NO);
2163 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2164 * and insert it in the appropiate structures if the peer is not known yet.
2166 * @param peer Full identity of the peer.
2168 * @return Existing or newly created peer info.
2170 static struct MeshPeerInfo *
2171 peer_info_get (const struct GNUNET_PeerIdentity *peer)
2173 struct MeshPeerInfo *peer_info;
2175 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
2176 if (NULL == peer_info)
2179 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
2180 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
2181 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
2182 peer_info->id = GNUNET_PEER_intern (peer);
2190 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
2191 * and insert it in the appropiate structures if the peer is not known yet.
2193 * @param peer Short identity of the peer.
2195 * @return Existing or newly created peer info.
2197 static struct MeshPeerInfo *
2198 peer_info_get_short (const GNUNET_PEER_Id peer)
2200 struct GNUNET_PeerIdentity id;
2202 GNUNET_PEER_resolve (peer, &id);
2203 return peer_info_get (&id);
2208 * Iterator to remove the tunnel from the list of tunnels a peer participates
2211 * @param cls Closure (tunnel info)
2212 * @param key GNUNET_PeerIdentity of the peer (unused)
2213 * @param value PeerInfo of the peer
2215 * @return always GNUNET_YES, to keep iterating
2218 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
2220 struct MeshTunnel *t = cls;
2221 struct MeshPeerInfo *peer = value;
2224 for (i = 0; i < peer->ntunnels; i++)
2227 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
2230 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
2231 peer->tunnels = GNUNET_realloc (peer->tunnels, peer->ntunnels);
2240 * Core callback to write a pre-constructed data packet to core buffer
2242 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
2243 * @param size Number of bytes available in buf.
2244 * @param buf Where the to write the message.
2246 * @return number of bytes written to buf
2249 send_core_data_raw (void *cls, size_t size, void *buf)
2251 struct MeshTransmissionDescriptor *info = cls;
2252 struct GNUNET_MessageHeader *msg;
2255 GNUNET_assert (NULL != info);
2256 GNUNET_assert (NULL != info->mesh_data);
2257 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
2258 total_size = ntohs (msg->size);
2260 if (total_size > size)
2265 memcpy (buf, msg, total_size);
2266 data_descriptor_decrement_rc (info->mesh_data);
2273 * Sends an already built non-multicast message to a peer,
2274 * properly registrating all used resources.
2276 * @param message Message to send. Function makes a copy of it.
2277 * @param peer Short ID of the neighbor whom to send the message.
2278 * @param t Tunnel on which this message is transmitted.
2281 send_message (const struct GNUNET_MessageHeader *message,
2282 const struct GNUNET_PeerIdentity *peer,
2283 struct MeshTunnel *t)
2285 struct MeshTransmissionDescriptor *info;
2286 struct MeshPeerInfo *neighbor;
2287 struct MeshPeerPath *p;
2291 // GNUNET_TRANSPORT_try_connect(); FIXME use?
2293 size = ntohs (message->size);
2294 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
2295 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
2296 info->mesh_data->data = GNUNET_malloc (size);
2297 memcpy (info->mesh_data->data, message, size);
2298 type = ntohs(message->type);
2299 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type)
2301 struct GNUNET_MESH_Unicast *m;
2303 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
2304 m->ttl = htonl (ntohl (m->ttl) - 1);
2306 info->mesh_data->data_len = size;
2307 info->mesh_data->reference_counter = GNUNET_malloc (sizeof (unsigned int));
2308 *info->mesh_data->reference_counter = 1;
2309 neighbor = peer_info_get (peer);
2310 for (p = neighbor->path_head; NULL != p; p = p->next)
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2321 " %s IS NOT DIRECTLY CONNECTED\n",
2323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 for (p = neighbor->path_head; NULL != p; p = p->next)
2328 struct GNUNET_PeerIdentity debug_id;
2331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2332 " path with %u hops through:\n",
2334 for (i = 0; i < p->length; i++)
2336 GNUNET_PEER_resolve(p->peers[i], &debug_id);
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2339 i, GNUNET_i2s(&debug_id));
2343 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
2344 GNUNET_free (info->mesh_data->data);
2345 GNUNET_free (info->mesh_data);
2349 info->peer = neighbor;
2350 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
2353 * TODO: in this case we only need the service to retransmit
2354 * the message down the path. If we pass the real type to queue_add,
2355 * queue_send will try to build the message from scratch. This can
2356 * probably be done by some other way instead of deleteing the type
2370 * Sends a CREATE PATH message for a path to a peer, properly registrating
2371 * all used resources.
2373 * @param peer PeerInfo of the final peer for whom this path is being created.
2374 * @param p Path itself.
2375 * @param t Tunnel for which the path is created.
2378 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
2379 struct MeshTunnel *t)
2381 struct GNUNET_PeerIdentity id;
2382 struct MeshPathInfo *path_info;
2383 struct MeshPeerInfo *neighbor;
2389 p = tree_get_path_to_peer (t->tree, peer->id);
2396 for (i = 0; i < p->length; i++)
2398 if (p->peers[i] == myid)
2401 if (i >= p->length - 1)
2407 GNUNET_PEER_resolve (p->peers[i + 1], &id);
2409 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2410 path_info->path = p;
2412 neighbor = peer_info_get (&id);
2413 path_info->peer = neighbor;
2414 queue_add (path_info,
2415 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
2416 sizeof (struct GNUNET_MESH_ManipulatePath) +
2417 (p->length * sizeof (struct GNUNET_PeerIdentity)),
2424 * Sends a DESTROY PATH message to free resources for a path in a tunnel
2426 * @param t Tunnel whose path to destroy.
2427 * @param destination Short ID of the peer to whom the path to destroy.
2430 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
2432 struct MeshPeerPath *p;
2435 p = tree_get_path_to_peer (t->tree, destination);
2441 size = sizeof (struct GNUNET_MESH_ManipulatePath);
2442 size += p->length * sizeof (struct GNUNET_PeerIdentity);
2444 struct GNUNET_MESH_ManipulatePath *msg;
2445 struct GNUNET_PeerIdentity *pi;
2449 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
2450 msg->header.size = htons (size);
2451 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
2452 msg->tid = htonl (t->id.tid);
2453 pi = (struct GNUNET_PeerIdentity *) &msg[1];
2454 for (i = 0; i < p->length; i++)
2456 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
2458 send_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
2465 * Try to establish a new connection to this peer.
2466 * Use the best path for the given tunnel.
2467 * If the peer doesn't have any path to it yet, try to get one.
2468 * If the peer already has some path, send a CREATE PATH towards it.
2470 * @param peer PeerInfo of the peer.
2471 * @param t Tunnel for which to create the path, if possible.
2474 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
2476 struct MeshPeerPath *p;
2477 struct MeshPathInfo *path_info;
2479 if (NULL != peer->path_head)
2481 p = tree_get_path_to_peer (t->tree, peer->id);
2488 // FIXME always send create path to self
2491 send_create_path (peer, p, t);
2495 struct GNUNET_HashCode hash;
2498 send_client_peer_connected (t, myid);
2499 t->local_tid_dest = next_local_tid++;
2500 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2503 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2504 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2511 else if (NULL == peer->dhtget)
2513 struct GNUNET_PeerIdentity id;
2515 GNUNET_PEER_resolve (peer->id, &id);
2516 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2517 path_info->peer = peer;
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2521 peer->dhtgetcls = path_info;
2522 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2523 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2524 &id.hashPubKey, /* key to search */
2525 dht_replication_level, /* replication level */
2526 GNUNET_DHT_RO_RECORD_ROUTE |
2527 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2528 NULL, /* xquery */ // FIXME BLOOMFILTER
2529 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2530 &dht_get_id_handler, path_info);
2532 /* Otherwise, there is no path but the DHT get is already started. */
2537 * Task to delay the connection of a peer
2539 * @param cls Closure (path info with tunnel and peer to connect).
2540 * Will be free'd on exection.
2541 * @param tc TaskContext
2544 peer_info_connect_task (void *cls,
2545 const struct GNUNET_SCHEDULER_TaskContext *tc)
2547 struct MeshPathInfo *path_info = cls;
2549 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2554 peer_info_connect (path_info->peer, path_info->t);
2560 * Destroy the peer_info and free any allocated resources linked to it
2562 * @param pi The peer_info to destroy.
2564 * @return GNUNET_OK on success
2567 peer_info_destroy (struct MeshPeerInfo *pi)
2569 struct GNUNET_PeerIdentity id;
2570 struct MeshPeerPath *p;
2571 struct MeshPeerPath *nextp;
2573 GNUNET_PEER_resolve (pi->id, &id);
2574 GNUNET_PEER_change_rc (pi->id, -1);
2577 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2580 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2581 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2583 if (NULL != pi->dhtget)
2585 GNUNET_DHT_get_stop (pi->dhtget);
2586 GNUNET_free (pi->dhtgetcls);
2592 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2602 * Remove all paths that rely on a direct connection between p1 and p2
2603 * from the peer itself and notify all tunnels about it.
2605 * @param peer PeerInfo of affected peer.
2606 * @param p1 GNUNET_PEER_Id of one peer.
2607 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2610 * TODO: optimize (see below)
2613 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2616 struct MeshPeerPath *p;
2617 struct MeshPeerPath *aux;
2618 struct MeshPeerInfo *peer_d;
2620 unsigned int destroyed;
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2627 p = peer->path_head;
2631 for (i = 0; i < (p->length - 1); i++)
2633 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2634 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2636 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2647 for (i = 0; i < peer->ntunnels; i++)
2649 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2653 * Problem: one or more peers have been deleted from the tunnel tree.
2654 * We don't know who they are to try to add them again.
2655 * We need to try to find a new path for each of the disconnected peers.
2656 * Some of them might already have a path to reach them that does not
2657 * involve p1 and p2. Adding all anew might render in a better tree than
2658 * the trivial immediate fix.
2660 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2661 * its children will be reachable trough him.
2663 peer_d = peer_info_get_short (d);
2666 for (p = peer_d->path_head; NULL != p; p = p->next)
2668 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2676 /* No callback, as peer will be already disconnected and a connection
2677 * scheduled by tunnel_notify_connection_broken.
2679 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2683 peer_info_connect (peer_d, peer->tunnels[i]);
2686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2691 * Add the path to the peer and update the path used to reach it in case this
2694 * @param peer_info Destination peer to add the path to.
2695 * @param path New path to add. Last peer must be the peer in arg 1.
2696 * Path will be either used of freed if already known.
2697 * @param trusted Do we trust that this path is real?
2700 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2703 struct MeshPeerPath *aux;
2707 if ((NULL == peer_info) || (NULL == path))
2710 path_destroy (path);
2713 if (path->peers[path->length - 1] != peer_info->id)
2716 path_destroy (path);
2719 if (path->length <= 2 && GNUNET_NO == trusted)
2721 /* Only allow CORE to tell us about direct paths */
2722 path_destroy (path);
2725 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2726 for (l = 1; l < path->length; l++)
2728 if (path->peers[l] == myid)
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2731 for (l2 = 0; l2 < path->length - l; l2++)
2733 path->peers[l2] = path->peers[l + l2];
2738 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2743 struct GNUNET_PeerIdentity id;
2745 GNUNET_PEER_resolve (peer_info->id, &id);
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2747 path->length, GNUNET_i2s (&id));
2750 l = path_get_length (path);
2757 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2758 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2760 l2 = path_get_length (aux);
2763 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2764 peer_info->path_tail, aux, path);
2769 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2771 path_destroy (path);
2776 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2783 * Add the path to the origin peer and update the path used to reach it in case
2784 * this is the shortest.
2785 * The path is given in peer_info -> destination, therefore we turn the path
2786 * upside down first.
2788 * @param peer_info Peer to add the path to, being the origin of the path.
2789 * @param path New path to add after being inversed.
2790 * @param trusted Do we trust that this path is real?
2793 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2794 struct MeshPeerPath *path, int trusted)
2797 peer_info_add_path (peer_info, path, trusted);
2802 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2803 * to obtain a local peer -> destination path and interning the peer ids.
2805 * @return Newly allocated and created path
2807 static struct MeshPeerPath *
2808 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2809 unsigned int get_path_length,
2810 const struct GNUNET_PeerIdentity *put_path,
2811 unsigned int put_path_length)
2813 struct MeshPeerPath *p;
2819 GNUNET_PEER_change_rc (myid, 1);
2820 i = get_path_length;
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2822 for (i--; i >= 0; i--)
2824 id = GNUNET_PEER_intern (&get_path[i]);
2825 if (p->length > 0 && id == p->peers[p->length - 1])
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2828 GNUNET_PEER_change_rc (id, -1);
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2833 GNUNET_i2s (&get_path[i]));
2835 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2836 p->peers[p->length - 1] = id;
2839 i = put_path_length;
2840 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2841 for (i--; i >= 0; i--)
2843 id = GNUNET_PEER_intern (&put_path[i]);
2846 /* PUT path went through us, so discard the path up until now and start
2847 * from here to get a much shorter (and loop-free) path.
2852 if (p->length > 0 && id == p->peers[p->length - 1])
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2855 GNUNET_PEER_change_rc (id, -1);
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2860 GNUNET_i2s (&put_path[i]));
2862 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2863 p->peers[p->length - 1] = id;
2867 if (get_path_length > 0)
2868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2869 GNUNET_i2s (&get_path[0]));
2870 if (put_path_length > 0)
2871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2872 GNUNET_i2s (&put_path[0]));
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2875 for (i = 0; i < p->length; i++)
2877 struct GNUNET_PeerIdentity peer_id;
2879 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2881 GNUNET_i2s (&peer_id));
2889 * Adds a path to the peer_infos of all the peers in the path
2891 * @param p Path to process.
2892 * @param confirmed Whether we know if the path works or not.
2895 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2899 /* TODO: invert and add */
2900 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2901 for (i++; i < p->length; i++)
2903 struct MeshPeerInfo *aux;
2904 struct MeshPeerPath *copy;
2906 aux = peer_info_get_short (p->peers[i]);
2907 copy = path_duplicate (p);
2908 copy->length = i + 1;
2909 peer_info_add_path (aux, copy, GNUNET_NO);
2915 * Send keepalive packets for a peer
2917 * @param cls Closure (tunnel for which to send the keepalive).
2918 * @param tc Notification context.
2920 * TODO: implement explicit multicast keepalive?
2923 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2927 * Search for a tunnel among the incoming tunnels
2929 * @param tid the local id of the tunnel
2931 * @return tunnel handler, NULL if doesn't exist
2933 static struct MeshTunnel *
2934 tunnel_get_incoming (MESH_TunnelNumber tid)
2936 struct GNUNET_HashCode hash;
2938 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2939 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2940 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2945 * Search for a tunnel among the tunnels for a client
2947 * @param c the client whose tunnels to search in
2948 * @param tid the local id of the tunnel
2950 * @return tunnel handler, NULL if doesn't exist
2952 static struct MeshTunnel *
2953 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2955 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2957 return tunnel_get_incoming (tid);
2961 struct GNUNET_HashCode hash;
2963 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2964 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2970 * Search for a tunnel by global ID using PEER_ID
2972 * @param pi owner of the tunnel
2973 * @param tid global tunnel number
2975 * @return tunnel handler, NULL if doesn't exist
2977 static struct MeshTunnel *
2978 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2980 struct MESH_TunnelID id;
2981 struct GNUNET_HashCode hash;
2986 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2987 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2992 * Search for a tunnel by global ID using full PeerIdentities
2994 * @param oid owner of the tunnel
2995 * @param tid global tunnel number
2997 * @return tunnel handler, NULL if doesn't exist
2999 static struct MeshTunnel *
3000 tunnel_get (struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
3002 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
3007 * Delete an active client from the tunnel.
3013 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
3017 for (i = 0; i < t->nclients; i++)
3019 if (t->clients[i] == c)
3021 t->clients[i] = t->clients[t->nclients - 1];
3022 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
3023 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
3025 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
3033 * Delete an ignored client from the tunnel.
3039 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
3043 for (i = 0; i < t->nignore; i++)
3045 if (t->ignore[i] == c)
3047 t->ignore[i] = t->ignore[t->nignore - 1];
3048 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
3056 * Delete a client from the tunnel. It should be only done on
3057 * client disconnection, otherwise use client_ignore_tunnel.
3063 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
3065 tunnel_delete_ignored_client (t, c);
3066 tunnel_delete_active_client (t, c);
3071 * Iterator to free MeshTunnelChildInfo of tunnel children.
3073 * @param cls Closure (tunnel info).
3074 * @param key Hash of GNUNET_PEER_Id (unused).
3075 * @param value MeshTunnelChildInfo of the child.
3077 * @return always GNUNET_YES, to keep iterating
3080 tunnel_destroy_child (void *cls,
3081 const struct GNUNET_HashCode * key,
3084 struct MeshTunnelChildInfo *cinfo = value;
3085 struct MeshTunnel *t = cls;
3089 for (c = 0; c < cinfo->send_buffer_n; c++)
3091 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
3092 if (NULL != cinfo->send_buffer[i])
3093 queue_destroy(cinfo->send_buffer[i], GNUNET_YES);
3096 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
3098 GNUNET_free_non_null (cinfo->send_buffer);
3099 GNUNET_free (cinfo);
3105 * Callback used to notify a client owner of a tunnel that a peer has
3106 * disconnected, most likely because of a path change.
3108 * @param cls Closure (tunnel this notification is about).
3109 * @param peer_id Short ID of disconnected peer.
3112 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
3114 struct MeshTunnel *t = cls;
3115 struct MeshPeerInfo *peer;
3116 struct MeshPathInfo *path_info;
3118 if (NULL != t->owner && NULL != nc)
3120 struct GNUNET_MESH_PeerControl msg;
3122 msg.header.size = htons (sizeof (msg));
3123 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
3124 msg.tunnel_id = htonl (t->local_tid);
3125 GNUNET_PEER_resolve (peer_id, &msg.peer);
3126 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
3127 &msg.header, GNUNET_NO);
3129 peer = peer_info_get_short (peer_id);
3130 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
3131 path_info->peer = peer;
3133 GNUNET_SCHEDULER_add_now (&peer_info_connect_task, path_info);
3138 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
3139 * needed rescources.
3140 * If peer already exists, reevaluate shortest path and change if different.
3142 * @param t Tunnel we want to add a new peer to
3143 * @param peer PeerInfo of the peer being added
3147 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
3149 struct GNUNET_PeerIdentity id;
3150 struct MeshPeerPath *best_p;
3151 struct MeshPeerPath *p;
3152 unsigned int best_cost;
3155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
3156 GNUNET_PEER_resolve (peer->id, &id);
3158 GNUNET_CONTAINER_multihashmap_contains (t->peers, &id.hashPubKey))
3161 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
3162 GNUNET_assert (GNUNET_OK ==
3163 GNUNET_CONTAINER_multihashmap_put (t->peers, &id.hashPubKey,
3165 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3168 if (NULL != (p = peer->path_head))
3171 best_cost = tree_get_path_cost (t->tree, p);
3174 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
3181 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
3182 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
3183 t->path_refresh_task =
3184 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
3188 /* Start a DHT get */
3189 peer_info_connect (peer, t);
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
3195 * Add a path to a tunnel which we don't own, just to remember the next hop.
3196 * If destination node was already in the tunnel, the first hop information
3197 * will be replaced with the new path.
3199 * @param t Tunnel we want to add a new peer to
3200 * @param p Path to add
3201 * @param own_pos Position of local node in path.
3205 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
3206 unsigned int own_pos)
3208 struct GNUNET_PeerIdentity id;
3210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
3211 GNUNET_assert (0 != own_pos);
3212 tree_add_path (t->tree, p, NULL, NULL);
3213 if (own_pos < p->length - 1)
3215 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
3216 tree_update_first_hops (t->tree, myid, &id);
3218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
3222 * Add a client to a tunnel, initializing all needed data structures.
3224 * @param t Tunnel to which add the client.
3225 * @param c Client which to add to the tunnel.
3228 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
3230 struct MeshTunnelClientInfo clinfo;
3232 GNUNET_array_append (t->clients, t->nclients, c);
3233 clinfo.fwd_ack = t->fwd_pid + 1;
3234 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3235 clinfo.fwd_pid = t->fwd_pid;
3236 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
3238 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
3243 * Notifies a tunnel that a connection has broken that affects at least
3244 * some of its peers. Sends a notification towards the root of the tree.
3245 * In case the peer is the owner of the tree, notifies the client that owns
3246 * the tunnel and tries to reconnect.
3248 * @param t Tunnel affected.
3249 * @param p1 Peer that got disconnected from p2.
3250 * @param p2 Peer that got disconnected from p1.
3252 * @return Short ID of the peer disconnected (either p1 or p2).
3253 * 0 if the tunnel remained unaffected.
3255 static GNUNET_PEER_Id
3256 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
3262 tree_notify_connection_broken (t->tree, p1, p2,
3263 &tunnel_notify_client_peer_disconnected,
3265 if (myid != p1 && myid != p2)
3271 if (tree_get_predecessor (t->tree) != 0)
3273 /* We are the peer still connected, notify owner of the disconnection. */
3274 struct GNUNET_MESH_PathBroken msg;
3275 struct GNUNET_PeerIdentity neighbor;
3277 msg.header.size = htons (sizeof (msg));
3278 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
3279 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3280 msg.tid = htonl (t->id.tid);
3281 msg.peer1 = my_full_id;
3282 GNUNET_PEER_resolve (pid, &msg.peer2);
3283 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
3284 send_message (&msg.header, &neighbor, t);
3292 * Send a multicast packet to a neighbor.
3294 * @param cls Closure (Info about the multicast packet)
3295 * @param neighbor_id Short ID of the neighbor to send the packet to.
3298 tunnel_send_multicast_iterator (void *cls, GNUNET_PEER_Id neighbor_id)
3300 struct MeshData *mdata = cls;
3301 struct MeshTransmissionDescriptor *info;
3302 struct GNUNET_PeerIdentity neighbor;
3304 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
3306 info->mesh_data = mdata;
3307 (*(mdata->reference_counter)) ++;
3308 info->destination = neighbor_id;
3309 GNUNET_PEER_resolve (neighbor_id, &neighbor);
3310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending to %s...\n",
3311 GNUNET_i2s (&neighbor));
3312 info->peer = peer_info_get (&neighbor);
3313 GNUNET_assert (NULL != info->peer);
3315 GNUNET_MESSAGE_TYPE_MESH_MULTICAST,
3316 info->mesh_data->data_len,
3323 * Queue a message in a tunnel in multicast, sending a copy to each child node
3324 * down the local one in the tunnel tree.
3326 * @param t Tunnel in which to send the data.
3327 * @param msg Message to be sent.
3328 * @param internal Has the service generated this message?
3331 tunnel_send_multicast (struct MeshTunnel *t,
3332 const struct GNUNET_MessageHeader *msg,
3335 struct MeshData *mdata;
3337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3338 " sending a multicast packet...\n");
3340 mdata = GNUNET_malloc (sizeof (struct MeshData));
3341 mdata->data_len = ntohs (msg->size);
3342 mdata->reference_counter = GNUNET_malloc (sizeof (unsigned int));
3344 mdata->data = GNUNET_malloc (mdata->data_len);
3345 memcpy (mdata->data, msg, mdata->data_len);
3346 if (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
3348 struct GNUNET_MESH_Multicast *mcast;
3350 mcast = (struct GNUNET_MESH_Multicast *) mdata->data;
3351 if (t->fwd_queue_n >= t->fwd_queue_max)
3354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " queue full!\n");
3355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3356 " message from %s!\n",
3357 GNUNET_i2s(&mcast->oid));
3358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3359 " message at %s!\n",
3360 GNUNET_i2s(&my_full_id));
3361 GNUNET_free (mdata->data);
3362 GNUNET_free (mdata->reference_counter);
3363 GNUNET_free (mdata);
3367 mcast->ttl = htonl (ntohl (mcast->ttl) - 1);
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " data packet, ttl: %u\n",
3369 ntohl (mcast->ttl));
3373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " not a data packet, no ttl\n");
3375 if (NULL != t->owner &&
3376 GNUNET_YES != t->owner->shutting_down &&
3377 GNUNET_NO == internal)
3379 mdata->task = GNUNET_malloc (sizeof (GNUNET_SCHEDULER_TaskIdentifier));
3381 GNUNET_SCHEDULER_add_delayed (unacknowledged_wait_time, &client_allow_send,
3383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "timeout task %u\n",
3387 tree_iterate_children (t->tree, &tunnel_send_multicast_iterator, mdata);
3388 if (*(mdata->reference_counter) == 0)
3390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3391 " no one to send data to\n");
3392 GNUNET_free (mdata->data);
3393 GNUNET_free (mdata->reference_counter);
3394 if (NULL != mdata->task)
3396 GNUNET_SCHEDULER_cancel(*(mdata->task));
3397 GNUNET_free (mdata->task);
3398 GNUNET_SERVER_receive_done (t->owner->handle, GNUNET_OK);
3400 GNUNET_free (mdata);
3403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3404 " sending a multicast packet done\n");
3410 * Increase the SKIP value of all peers that
3411 * have not received a unicast message.
3413 * @param cls Closure (ID of the peer that HAS received the message).
3414 * @param key ID of the neighbor.
3415 * @param value Information about the neighbor.
3417 * @return GNUNET_YES to keep iterating.
3420 tunnel_add_skip (void *cls,
3421 const struct GNUNET_HashCode * key,
3424 struct GNUNET_PeerIdentity *neighbor = cls;
3425 struct MeshTunnelChildInfo *cinfo = value;
3427 /* TODO compare only pointers? key == neighbor? */
3428 if (0 == memcmp (&neighbor->hashPubKey, key, sizeof (struct GNUNET_HashCode)))
3438 * @brief Get neighbor's Flow Control information.
3440 * Retrieves the MeshTunnelChildInfo containing Flow Control data about a direct
3441 * descendant of the local node in a certain tunnel.
3442 * If the info is not yet there (recently created path), creates the data struct
3443 * and inserts it into the tunnel info, initialized to the current tunnel ACK
3446 * @param t Tunnel related.
3447 * @param peer Neighbor whose Flow Control info is needed.
3449 * @return Neighbor's Flow Control info.
3451 static struct MeshTunnelChildInfo *
3452 tunnel_get_neighbor_fc (const struct MeshTunnel *t,
3453 const struct GNUNET_PeerIdentity *peer)
3455 struct MeshTunnelChildInfo *cinfo;
3456 cinfo = GNUNET_CONTAINER_multihashmap_get (t->children_fc,
3462 cinfo = GNUNET_malloc (sizeof (struct MeshTunnelChildInfo));
3463 cinfo->id = GNUNET_PEER_intern (peer);
3464 cinfo->skip = t->fwd_pid;
3466 delta = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
3467 cinfo->fwd_ack = t->fwd_pid + delta;
3468 cinfo->bck_ack = delta;
3470 cinfo->send_buffer =
3471 GNUNET_malloc (sizeof(struct MeshPeerQueue *) * t->fwd_queue_max);
3473 GNUNET_assert (GNUNET_OK ==
3474 GNUNET_CONTAINER_multihashmap_put (t->children_fc,
3477 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
3484 * Get the Flow Control info of a client.
3486 * @param t Tunnel on which to look.
3487 * @param c Client whose ACK to get.
3489 * @return ACK value.
3491 static struct MeshTunnelClientInfo *
3492 tunnel_get_client_fc (struct MeshTunnel *t,
3493 struct MeshClient *c)
3497 for (i = 0; i < t->nclients; i++)
3499 if (t->clients[i] != c)
3501 return &t->clients_fc[i];
3504 return NULL; // avoid compiler / coverity complaints
3509 * Iterator to get the appropiate ACK value from all children nodes.
3511 * @param cls Closue (tunnel).
3512 * @param id Id of the child node.
3515 tunnel_get_child_fwd_ack (void *cls,
3518 struct GNUNET_PeerIdentity peer_id;
3519 struct MeshTunnelChildInfo *cinfo;
3520 struct MeshTunnelChildIteratorContext *ctx = cls;
3521 struct MeshTunnel *t = ctx->t;
3524 GNUNET_PEER_resolve (id, &peer_id);
3525 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
3526 ack = cinfo->fwd_ack;
3529 if (GNUNET_NO == ctx->init)
3531 ctx->max_child_ack = ack;
3532 ctx->init = GNUNET_YES;
3535 if (GNUNET_YES == t->speed_min)
3537 ctx->max_child_ack = ctx->max_child_ack > ack ? ack : ctx->max_child_ack;
3541 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
3548 * Get the maximum PID allowed to transmit to any
3549 * tunnel child of the local peer, depending on the tunnel
3550 * buffering/speed settings.
3554 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
3557 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
3559 struct MeshTunnelChildIteratorContext ctx;
3561 ctx.max_child_ack = 0;
3563 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
3565 if (0 == ctx.nchildren)
3567 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3568 " tunnel has no children, no FWD ACK\n");
3572 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
3573 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
3575 return (int64_t) ctx.max_child_ack;
3580 * Set the FWD ACK value of a client in a particular tunnel.
3582 * @param t Tunnel affected.
3583 * @param c Client whose ACK to set.
3584 * @param ack ACK value.
3587 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
3588 struct MeshClient *c,
3593 for (i = 0; i < t->nclients; i++)
3595 if (t->clients[i] != c)
3597 t->clients_fc[i].fwd_ack = ack;
3605 * Get the highest ACK value of all clients in a particular tunnel,
3606 * according to the buffering/speed settings.
3608 * @param t Tunnel on which to look.
3610 * @return Corresponding ACK value (max uint32_t).
3611 * If no clients are suscribed, -1LL.
3614 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
3619 if (0 == t->nclients)
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3622 " tunnel has no clients, no FWD ACK\n");
3626 for (ack = -1LL, i = 0; i < t->nclients; i++)
3629 (GNUNET_YES == t->speed_min &&
3630 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack)) ||
3631 (GNUNET_NO == t->speed_min &&
3632 GNUNET_YES == GMC_is_pid_bigger (t->clients_fc[i].fwd_ack, ack)))
3634 ack = t->clients_fc[i].fwd_ack;
3638 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3639 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3641 return (uint32_t) ack;
3646 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3647 * mode and the status of all children nodes.
3651 * @return Maximum PID allowed.
3654 tunnel_get_fwd_ack (struct MeshTunnel *t)
3658 uint32_t buffer_free;
3662 count = t->fwd_pid - t->skip;
3663 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3664 ack = count + buffer_free; // Might overflow 32 bits, it's ok!
3665 child_ack = tunnel_get_children_fwd_ack (t);
3666 client_ack = tunnel_get_clients_fwd_ack (t);
3667 if (-1LL == child_ack)
3669 // Node has no children, child_ack AND core buffer are irrelevant.
3670 GNUNET_break (-1LL != client_ack); // No children AND no clients? Not good!
3671 return (uint32_t) client_ack;
3673 if (-1LL == client_ack)
3677 if (GNUNET_YES == t->speed_min)
3679 ack = GMC_min_pid ((uint32_t) child_ack, ack);
3680 ack = GMC_min_pid ((uint32_t) client_ack, ack);
3684 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3685 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3687 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
3688 ack = t->fwd_pid + 1; // Might overflow 32 bits, it's ok!
3689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3690 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3691 count, buffer_free, child_ack, client_ack, ack);
3697 * Build a local ACK message and send it to a local client.
3699 * @param t Tunnel on which to send the ACK.
3700 * @param c Client to whom send the ACK.
3701 * @param ack Value of the ACK.
3704 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3706 struct GNUNET_MESH_LocalAck msg;
3708 msg.header.size = htons (sizeof (msg));
3709 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3710 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3711 msg.max_pid = htonl (ack);
3712 GNUNET_SERVER_notification_context_unicast(nc,
3719 * Build an ACK message and queue it to send to the given peer.
3721 * @param t Tunnel on which to send the ACK.
3722 * @param peer Peer to whom send the ACK.
3723 * @param ack Value of the ACK.
3726 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3728 struct GNUNET_MESH_ACK msg;
3730 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3731 msg.header.size = htons (sizeof (msg));
3732 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3733 msg.pid = htonl (ack);
3734 msg.tid = htonl (t->id.tid);
3736 send_message (&msg.header, peer, t);
3741 * Notify a the owner of a tunnel about how many more
3742 * payload packages will we accept on a given tunnel.
3744 * @param t Tunnel on which to send the ACK.
3747 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3752 "Sending client FWD ACK on tunnel %X\n",
3755 ack = tunnel_get_fwd_ack (t);
3757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3758 if (t->last_fwd_ack == ack)
3761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3762 t->last_fwd_ack = ack;
3763 send_local_ack (t, t->owner, ack);
3768 * Send an ACK informing the predecessor about the available buffer space.
3769 * In case there is no predecessor, inform the owning client.
3770 * If buffering is off, send only on behalf of children or self if endpoint.
3771 * If buffering is on, send when sent to children and buffer space is free.
3772 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3773 * the ACK itself goes "back" (towards root).
3775 * @param t Tunnel on which to send the ACK.
3776 * @param type Type of message that triggered the ACK transmission.
3779 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3781 struct GNUNET_PeerIdentity id;
3784 if (NULL != t->owner)
3786 tunnel_send_client_fwd_ack (t);
3789 /* Is it after unicast / multicast retransmission? */
3792 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3793 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3795 "ACK due to FWD DATA retransmission\n");
3796 if (GNUNET_YES == t->nobuffer)
3798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3802 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3803 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3809 /* Check if we need no retransmit the ACK */
3810 if (t->fwd_queue_max > t->fwd_queue_n * 2 &&
3811 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid))
3813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3815 " t->qmax: %u, t->qn: %u\n",
3816 t->fwd_queue_max, t->fwd_queue_n);
3817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3818 " t->pid: %u, t->ack: %u\n",
3819 t->fwd_pid, t->last_fwd_ack);
3823 /* Ok, ACK might be necessary, what PID to ACK? */
3824 ack = tunnel_get_fwd_ack (t);
3826 /* If speed_min and not all children have ack'd, dont send yet */
3827 if (ack == t->last_fwd_ack)
3829 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3833 t->last_fwd_ack = ack;
3834 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3835 send_ack (t, &id, ack);
3841 * Iterator to send a child node a BCK ACK to allow him to send more
3844 * @param cls Closure (tunnel).
3845 * @param id Id of the child node.
3848 tunnel_send_child_bck_ack (void *cls,
3851 struct MeshTunnel *t = cls;
3852 struct MeshTunnelChildInfo *cinfo;
3853 struct GNUNET_PeerIdentity peer;
3855 GNUNET_PEER_resolve (id, &peer);
3856 cinfo = tunnel_get_neighbor_fc (t, &peer);
3858 if (cinfo->bck_ack != cinfo->pid &&
3859 GNUNET_NO == GMC_is_pid_bigger (cinfo->bck_ack, cinfo->pid))
3863 send_ack (t, &peer, cinfo->bck_ack);
3868 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3870 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3872 * @param t Tunnel on which to send the BCK ACKs.
3875 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3878 unsigned int tunnel_delta;
3880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3882 tunnel_delta = t->bck_ack - t->bck_pid;
3883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3885 /* Find client whom to allow to send to origin (with lowest buffer space) */
3886 for (i = 0; i < t->nclients; i++)
3888 struct MeshTunnelClientInfo *clinfo;
3891 clinfo = &t->clients_fc[i];
3892 delta = clinfo->bck_ack - clinfo->bck_pid;
3893 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3894 t->clients[i]->id, delta);
3896 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3897 (GNUNET_YES == t->nobuffer && 0 == delta))
3901 ack = clinfo->bck_pid;
3902 ack += t->nobuffer ? 1 : tunnel_delta;
3903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3904 " sending ack to client %u: %u\n",
3905 t->clients[i]->id, ack);
3906 send_local_ack (t, t->clients[i], ack);
3907 clinfo->bck_ack = ack;
3914 * Send an ACK informing the children nodes and destination clients about
3915 * the available buffer space.
3916 * If buffering is off, send only on behalf of root (can be self).
3917 * If buffering is on, send when sent to predecessor and buffer space is free.
3918 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3919 * the ACK itself goes "forward" (towards children/clients).
3921 * @param t Tunnel on which to send the ACK.
3922 * @param type Type of message that triggered the ACK transmission.
3925 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3928 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3929 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3930 /* Is it after data to_origin retransmission? */
3933 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3934 if (GNUNET_YES == t->nobuffer)
3936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3937 " Not sending ACK, nobuffer\n");
3941 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3942 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3948 tunnel_send_clients_bck_ack (t);
3949 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3954 * @brief Re-initiate traffic to this peer if necessary.
3956 * Check if there is traffic queued towards this peer
3957 * and the core transmit handle is NULL (traffic was stalled).
3958 * If so, call core tmt rdy.
3960 * @param cls Closure (unused)
3961 * @param peer_id Short ID of peer to which initiate traffic.
3964 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3966 struct MeshPeerInfo *peer;
3967 struct GNUNET_PeerIdentity id;
3968 struct MeshPeerQueue *q;
3971 peer = peer_info_get_short(peer_id);
3972 if (NULL != peer->core_transmit)
3975 q = queue_get_next(peer);
3978 /* Might br multicast traffic already sent to this particular peer but
3979 * not to other children in this tunnel.
3980 * This way t->queue_n would be > 0 but the queue of this particular peer
3986 GNUNET_PEER_resolve (peer->id, &id);
3987 peer->core_transmit =
3988 GNUNET_CORE_notify_transmit_ready(core_handle,
3991 GNUNET_TIME_UNIT_FOREVER_REL,
4001 * @brief Allow transmission of FWD traffic on this tunnel
4003 * Check if there is traffic queued towards any children
4004 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4006 * @param t Tunnel on which to unlock FWD traffic.
4009 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
4011 if (0 == t->fwd_queue_n)
4014 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
4019 * @brief Allow transmission of BCK traffic on this tunnel
4021 * Check if there is traffic queued towards the root of the tree
4022 * and the core transmit handle is NULL, and if so, call core tmt rdy.
4024 * @param t Tunnel on which to unlock BCK traffic.
4027 tunnel_unlock_bck_queue (struct MeshTunnel *t)
4029 if (0 == t->bck_queue_n)
4032 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
4037 * Send a message to all peers in this tunnel that the tunnel is no longer
4040 * @param t The tunnel whose peers to notify.
4043 tunnel_send_destroy (struct MeshTunnel *t)
4045 struct GNUNET_MESH_TunnelDestroy msg;
4047 msg.header.size = htons (sizeof (msg));
4048 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
4049 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
4050 msg.tid = htonl (t->id.tid);
4051 tunnel_send_multicast (t, &msg.header, GNUNET_NO);
4056 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
4058 * @param cls Closure (Tunnel which to cancel).
4059 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
4062 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
4064 struct MeshTunnel *t = cls;
4065 struct MeshPeerInfo *peer_info;
4066 struct MeshPeerQueue *pq;
4067 struct MeshPeerQueue *next;
4069 peer_info = peer_info_get_short (neighbor_id);
4070 for (pq = peer_info->queue_head; NULL != pq; pq = next)
4073 if (pq->tunnel == t)
4075 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
4076 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
4077 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
4079 // Should have been removed on destroy children
4082 queue_destroy (pq, GNUNET_YES);
4085 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
4087 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
4088 peer_info->core_transmit = NULL;
4093 * Destroy the tunnel and free any allocated resources linked to it.
4095 * @param t the tunnel to destroy
4097 * @return GNUNET_OK on success
4100 tunnel_destroy (struct MeshTunnel *t)
4102 struct MeshClient *c;
4103 struct GNUNET_HashCode hash;
4114 struct GNUNET_PeerIdentity id;
4116 GNUNET_PEER_resolve (t->id.oid, &id);
4117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
4118 GNUNET_i2s (&id), t->id.tid);
4120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
4124 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4125 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
4130 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4133 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
4137 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4138 for (i = 0; i < t->nclients; i++)
4142 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
4147 for (i = 0; i < t->nignore; i++)
4151 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
4156 if (t->nclients > 0)
4159 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t))
4163 GNUNET_free (t->clients);
4165 if (NULL != t->peers)
4167 GNUNET_CONTAINER_multihashmap_iterate (t->peers, &peer_info_delete_tunnel,
4169 GNUNET_CONTAINER_multihashmap_destroy (t->peers);
4172 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4173 &tunnel_destroy_child,
4175 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
4177 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
4179 tree_destroy (t->tree);
4181 if (NULL != t->regex_ctx)
4182 regex_cancel_search (t->regex_ctx);
4183 if (NULL != t->dht_get_type)
4184 GNUNET_DHT_get_stop (t->dht_get_type);
4185 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4186 GNUNET_SCHEDULER_cancel (t->timeout_task);
4187 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
4188 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
4191 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
4192 GNUNET_assert (0 <= n_tunnels);
4199 * Create a new tunnel
4201 * @param owner Who is the owner of the tunnel (short ID).
4202 * @param tid Tunnel Number of the tunnel.
4203 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
4204 * @param local Tunnel Number for the tunnel, for the client point of view.
4206 * @return A new initialized tunnel. NULL on error.
4208 static struct MeshTunnel *
4209 tunnel_new (GNUNET_PEER_Id owner,
4210 MESH_TunnelNumber tid,
4211 struct MeshClient *client,
4212 MESH_TunnelNumber local)
4214 struct MeshTunnel *t;
4215 struct GNUNET_HashCode hash;
4217 if (n_tunnels >= max_tunnels && NULL == client)
4220 t = GNUNET_malloc (sizeof (struct MeshTunnel));
4223 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
4224 t->bck_queue_max = t->fwd_queue_max;
4225 t->tree = tree_new (owner);
4227 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
4228 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
4229 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
4230 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
4231 t->local_tid = local;
4232 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8);
4234 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
4236 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
4238 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
4239 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4246 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4253 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
4255 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
4256 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
4260 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
4270 * Removes an explicit path from a tunnel, freeing all intermediate nodes
4271 * that are no longer needed, as well as nodes of no longer reachable peers.
4272 * The tunnel itself is also destoyed if results in a remote empty tunnel.
4274 * @param t Tunnel from which to remove the path.
4275 * @param peer Short id of the peer which should be removed.
4278 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
4280 if (GNUNET_NO == tree_del_peer (t->tree, peer, NULL, NULL))
4286 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
4287 * client when the client disconnects. If the client is not the owner, the
4288 * owner will get notified if no more clients are in the tunnel and the client
4289 * get removed from the tunnel's list.
4291 * @param cls closure (client that is disconnecting)
4292 * @param key the hash of the local tunnel id (used to access the hashmap)
4293 * @param value the value stored at the key (tunnel to destroy)
4295 * @return GNUNET_OK on success
4298 tunnel_destroy_iterator (void *cls, const struct GNUNET_HashCode * key, void *value)
4300 struct MeshTunnel *t = value;
4301 struct MeshClient *c = cls;
4304 send_client_tunnel_disconnect(t, c);
4307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4308 "Client %u is destination, keeping the tunnel alive.\n", c->id);
4309 tunnel_delete_client(t, c);
4310 client_delete_tunnel(c, t);
4313 tunnel_send_destroy(t);
4314 r = tunnel_destroy (t);
4320 * Timeout function, destroys tunnel if called
4322 * @param cls Closure (tunnel to destroy).
4323 * @param tc TaskContext
4326 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
4328 struct MeshTunnel *t = cls;
4330 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
4332 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
4337 * Resets the tunnel timeout. Starts it if no timeout was running.
4339 * @param t Tunnel whose timeout to reset.
4341 * TODO use heap to improve efficiency of scheduler.
4344 tunnel_reset_timeout (struct MeshTunnel *t)
4346 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
4347 GNUNET_SCHEDULER_cancel (t->timeout_task);
4349 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
4350 (refresh_path_time, 4), &tunnel_timeout, t);
4354 /******************************************************************************/
4355 /**************** MESH NETWORK HANDLER HELPERS ***********************/
4356 /******************************************************************************/
4359 * Function to send a create path packet to a peer.
4361 * @param cls closure
4362 * @param size number of bytes available in buf
4363 * @param buf where the callee should write the message
4364 * @return number of bytes written to buf
4367 send_core_path_create (void *cls, size_t size, void *buf)
4369 struct MeshPathInfo *info = cls;
4370 struct GNUNET_MESH_ManipulatePath *msg;
4371 struct GNUNET_PeerIdentity *peer_ptr;
4372 struct MeshTunnel *t = info->t;
4373 struct MeshPeerPath *p = info->path;
4378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
4380 sizeof (struct GNUNET_MESH_ManipulatePath) +
4381 p->length * sizeof (struct GNUNET_PeerIdentity);
4383 if (size < size_needed || NULL == buf)
4388 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
4389 msg->header.size = htons (size_needed);
4390 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
4391 msg->tid = ntohl (t->id.tid);
4394 if (GNUNET_YES == t->speed_min)
4395 opt |= MESH_TUNNEL_OPT_SPEED_MIN;
4396 if (GNUNET_YES == t->nobuffer)
4397 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4398 msg->opt = htonl(opt);
4400 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4401 for (i = 0; i < p->length; i++)
4403 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4410 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4416 * Fill the core buffer
4418 * @param cls closure (data itself)
4419 * @param size number of bytes available in buf
4420 * @param buf where the callee should write the message
4422 * @return number of bytes written to buf
4425 send_core_data_multicast (void *cls, size_t size, void *buf)
4427 struct MeshTransmissionDescriptor *info = cls;
4430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4431 GNUNET_assert (NULL != info);
4432 GNUNET_assert (NULL != info->peer);
4433 total_size = info->mesh_data->data_len;
4434 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4436 if (total_size > size)
4441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4442 memcpy (buf, info->mesh_data->data, total_size);
4445 struct GNUNET_MESH_Multicast *mc;
4446 struct GNUNET_MessageHeader *mh;
4449 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4451 mc = (struct GNUNET_MESH_Multicast *) mh;
4452 mh = (struct GNUNET_MessageHeader *) &mc[1];
4453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4454 " multicast, payload type %s\n",
4455 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4457 " multicast, payload size %u\n", ntohs (mh->size));
4461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4462 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4466 data_descriptor_decrement_rc (info->mesh_data);
4467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4475 * Creates a path ack message in buf and frees all unused resources.
4477 * @param cls closure (MeshTransmissionDescriptor)
4478 * @param size number of bytes available in buf
4479 * @param buf where the callee should write the message
4480 * @return number of bytes written to buf
4483 send_core_path_ack (void *cls, size_t size, void *buf)
4485 struct MeshTransmissionDescriptor *info = cls;
4486 struct GNUNET_MESH_PathACK *msg = buf;
4488 GNUNET_assert (NULL != info);
4489 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4494 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4495 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4496 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4497 msg->tid = htonl (info->origin->tid);
4498 msg->peer_id = my_full_id;
4501 /* TODO add signature */
4503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4504 return sizeof (struct GNUNET_MESH_PathACK);
4509 * Free a transmission that was already queued with all resources
4510 * associated to the request.
4512 * @param queue Queue handler to cancel.
4513 * @param clear_cls Is it necessary to free associated cls?
4516 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4518 struct MeshTransmissionDescriptor *dd;
4519 struct MeshPathInfo *path_info;
4521 if (GNUNET_YES == clear_cls)
4523 switch (queue->type)
4525 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4526 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4527 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4530 data_descriptor_decrement_rc (dd->mesh_data);
4532 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4534 path_info = queue->cls;
4535 path_destroy (path_info->path);
4539 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4540 " type %u unknown!\n",
4543 GNUNET_free_non_null (queue->cls);
4545 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4546 queue->peer->queue_tail,
4548 GNUNET_free (queue);
4553 * @brief Get the next transmittable message from the queue.
4555 * This will be the head, except in the case of being a data packet
4556 * not allowed by the destination peer.
4558 * @param peer Destination peer.
4560 * @return The next viable MeshPeerQueue element to send to that peer.
4561 * NULL when there are no transmittable messages.
4563 struct MeshPeerQueue *
4564 queue_get_next (const struct MeshPeerInfo *peer)
4566 struct MeshPeerQueue *q;
4567 struct MeshTunnel *t;
4568 struct MeshTransmissionDescriptor *info;
4569 struct MeshTunnelChildInfo *cinfo;
4570 struct GNUNET_MESH_Unicast *ucast;
4571 struct GNUNET_MESH_ToOrigin *to_orig;
4572 struct GNUNET_MESH_Multicast *mcast;
4573 struct GNUNET_PeerIdentity id;
4577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4578 for (q = peer->queue_head; NULL != q; q = q->next)
4582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4584 GNUNET_MESH_DEBUG_M2S(q->type));
4587 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4588 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4589 pid = ntohl (ucast->pid);
4590 GNUNET_PEER_resolve (info->peer->id, &id);
4591 cinfo = tunnel_get_neighbor_fc(t, &id);
4592 ack = cinfo->fwd_ack;
4594 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4595 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4596 pid = ntohl (to_orig->pid);
4599 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4600 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4601 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4603 // Not a multicast payload: multicast control traffic (destroy, etc)
4606 pid = ntohl (mcast->pid);
4607 GNUNET_PEER_resolve (info->peer->id, &id);
4608 cinfo = tunnel_get_neighbor_fc(t, &id);
4609 ack = cinfo->fwd_ack;
4612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4617 "********* ACK: %u, PID: %u\n",
4619 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4628 "********* NEXT!\n");
4631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4632 "********* nothing found\n");
4638 * Core callback to write a queued packet to core buffer
4640 * @param cls Closure (peer info).
4641 * @param size Number of bytes available in buf.
4642 * @param buf Where the to write the message.
4644 * @return number of bytes written to buf
4647 queue_send (void *cls, size_t size, void *buf)
4649 struct MeshPeerInfo *peer = cls;
4650 struct GNUNET_MessageHeader *msg;
4651 struct MeshPeerQueue *queue;
4652 struct MeshTunnel *t;
4653 struct MeshTunnelChildInfo *cinfo;
4654 struct GNUNET_PeerIdentity dst_id;
4657 peer->core_transmit = NULL;
4659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4660 queue = queue_get_next(peer);
4662 /* Queue has no internal mesh traffic not sendable payload */
4665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4666 if (NULL == peer->queue_head)
4667 GNUNET_break(0); // Should've been canceled
4670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4672 GNUNET_PEER_resolve (peer->id, &dst_id);
4673 /* Check if buffer size is enough for the message */
4674 if (queue->size > size)
4678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4679 "********* not enough room, reissue\n");
4680 peer->core_transmit =
4681 GNUNET_CORE_notify_transmit_ready(core_handle,
4684 GNUNET_TIME_UNIT_FOREVER_REL,
4691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4694 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4698 "********* unicast: t->q (%u/%u)\n",
4699 t->fwd_queue_n, t->fwd_queue_max);
4701 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4708 switch (queue->type)
4711 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4712 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4713 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4715 "********* raw: %s\n",
4716 GNUNET_MESH_DEBUG_M2S (queue->type));
4718 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4719 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4720 data_size = send_core_data_raw (queue->cls, size, buf);
4721 msg = (struct GNUNET_MessageHeader *) buf;
4722 switch (ntohs (msg->type)) // Type of preconstructed message
4724 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4725 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4727 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4728 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4734 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4737 struct MeshTransmissionDescriptor *info = queue->cls;
4738 if (*(info->mesh_data->reference_counter) == 1)
4740 // FIXME fc (t->fwd_queue_n--)
4742 data_size = send_core_data_multicast(queue->cls, size, buf);
4743 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4745 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4747 data_size = send_core_path_create(queue->cls, size, buf);
4749 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4751 data_size = send_core_path_ack(queue->cls, size, buf);
4755 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4756 "********* type unknown: %u\n",
4760 switch (queue->type)
4762 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4763 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4764 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4765 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4766 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4769 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4770 "at pos %u (%p) != %p\n",
4771 cinfo->send_buffer_start,
4772 cinfo->send_buffer[cinfo->send_buffer_start],
4775 GNUNET_break(cinfo->send_buffer_n > 0);
4776 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4777 cinfo->send_buffer_n--;
4778 cinfo->send_buffer_start++;
4779 cinfo->send_buffer_start %= t->fwd_queue_max;
4785 /* Free queue, but cls was freed by send_core_* */
4786 queue_destroy (queue, GNUNET_NO);
4788 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4794 /* If more data in queue, send next */
4795 queue = queue_get_next(peer);
4798 struct GNUNET_PeerIdentity id;
4800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4801 GNUNET_PEER_resolve (peer->id, &id);
4802 peer->core_transmit =
4803 GNUNET_CORE_notify_transmit_ready(core_handle,
4806 GNUNET_TIME_UNIT_FOREVER_REL,
4814 if (NULL != peer->queue_head)
4815 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4816 "********* %s stalled\n",
4817 GNUNET_i2s(&my_full_id));
4819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4825 * Queue and pass message to core when possible.
4827 * @param cls Closure (type dependant).
4828 * @param type Type of the message, 0 for a raw message.
4829 * @param size Size of the message.
4830 * @param dst Neighbor to send message to.
4831 * @param t Tunnel this message belongs to.
4834 queue_add (void *cls, uint16_t type, size_t size,
4835 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4837 struct MeshPeerQueue *queue;
4838 struct MeshTunnelChildInfo *cinfo;
4839 struct GNUNET_PeerIdentity id;
4845 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4846 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4848 n = &t->fwd_queue_n;
4849 max = &t->fwd_queue_max;
4851 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4853 n = &t->bck_queue_n;
4854 max = &t->bck_queue_max;
4859 if (NULL == t->owner)
4860 GNUNET_break_op(0); // TODO: kill connection?
4863 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4865 return; // Drop message
4869 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4875 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4876 GNUNET_PEER_resolve (dst->id, &id);
4877 if (NULL == dst->core_transmit)
4879 dst->core_transmit =
4880 GNUNET_CORE_notify_transmit_ready(core_handle,
4883 GNUNET_TIME_UNIT_FOREVER_REL,
4889 if (NULL == n) // Is this internal mesh traffic?
4892 // It's payload, keep track of buffer per peer.
4893 cinfo = tunnel_get_neighbor_fc(t, &id);
4894 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4895 if (NULL != cinfo->send_buffer[i])
4897 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4898 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4899 cinfo->send_buffer_start++;
4900 cinfo->send_buffer_start %= t->fwd_queue_max;
4901 cinfo->send_buffer_n--;
4903 cinfo->send_buffer[i] = queue;
4904 cinfo->send_buffer_n++;
4905 if (cinfo->send_buffer_n > t->fwd_queue_max)
4908 cinfo->send_buffer_n = t->fwd_queue_max;
4913 /******************************************************************************/
4914 /******************** MESH NETWORK HANDLERS **************************/
4915 /******************************************************************************/
4919 * Core handler for path creation
4921 * @param cls closure
4922 * @param message message
4923 * @param peer peer identity this notification is about
4924 * @param atsi performance data
4925 * @param atsi_count number of records in 'atsi'
4927 * @return GNUNET_OK to keep the connection open,
4928 * GNUNET_SYSERR to close it (signal serious error)
4931 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4932 const struct GNUNET_MessageHeader *message,
4933 const struct GNUNET_ATS_Information *atsi,
4934 unsigned int atsi_count)
4936 unsigned int own_pos;
4939 MESH_TunnelNumber tid;
4940 struct GNUNET_MESH_ManipulatePath *msg;
4941 struct GNUNET_PeerIdentity *pi;
4942 struct GNUNET_HashCode hash;
4943 struct MeshPeerPath *path;
4944 struct MeshPeerInfo *dest_peer_info;
4945 struct MeshPeerInfo *orig_peer_info;
4946 struct MeshTunnel *t;
4948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4949 "Received a path create msg [%s]\n",
4950 GNUNET_i2s (&my_full_id));
4951 size = ntohs (message->size);
4952 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4954 GNUNET_break_op (0);
4958 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4959 if (size % sizeof (struct GNUNET_PeerIdentity))
4961 GNUNET_break_op (0);
4964 size /= sizeof (struct GNUNET_PeerIdentity);
4967 GNUNET_break_op (0);
4970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4971 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4973 tid = ntohl (msg->tid);
4974 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4976 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4977 t = tunnel_get (pi, tid);
4978 if (NULL == t) // FIXME only for INCOMING tunnels?
4982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4983 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4986 // FIXME notify failure
4989 opt = ntohl (msg->opt);
4990 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4991 GNUNET_YES : GNUNET_NO;
4992 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4993 GNUNET_YES : GNUNET_NO;
4995 if (GNUNET_YES == t->nobuffer)
4997 t->bck_queue_max = 1;
4998 t->fwd_queue_max = 1;
5001 // FIXME only assign a local tid if a local client is interested (on demand)
5002 while (NULL != tunnel_get_incoming (next_local_tid))
5003 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5004 t->local_tid_dest = next_local_tid++;
5005 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5008 tunnel_reset_timeout (t);
5009 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5011 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5012 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5020 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5021 if (NULL == dest_peer_info)
5023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5024 " Creating PeerInfo for destination.\n");
5025 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5026 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5027 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5029 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5031 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5032 if (NULL == orig_peer_info)
5034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5035 " Creating PeerInfo for origin.\n");
5036 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5037 orig_peer_info->id = GNUNET_PEER_intern (pi);
5038 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5039 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5042 path = path_new (size);
5044 for (i = 0; i < size; i++)
5046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5047 GNUNET_i2s (&pi[i]));
5048 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5049 if (path->peers[i] == myid)
5052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5055 /* cannot be self, must be 'not found' */
5056 /* create path: self not found in path through self */
5057 GNUNET_break_op (0);
5058 path_destroy (path);
5062 path_add_to_peers (path, GNUNET_NO);
5063 tunnel_add_path (t, path, own_pos);
5064 if (own_pos == size - 1)
5066 /* It is for us! Send ack. */
5067 struct MeshTransmissionDescriptor *info;
5069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5070 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5071 if (NULL == t->peers)
5073 /* New tunnel! Notify clients on data. */
5074 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5076 GNUNET_break (GNUNET_SYSERR !=
5077 GNUNET_CONTAINER_multihashmap_put (t->peers,
5078 &my_full_id.hashPubKey,
5081 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5082 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5083 info->origin = &t->id;
5084 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5085 GNUNET_assert (NULL != info->peer);
5087 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5088 sizeof (struct GNUNET_MESH_PathACK),
5094 struct MeshPeerPath *path2;
5096 /* It's for somebody else! Retransmit. */
5097 path2 = path_duplicate (path);
5098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5099 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5100 path2 = path_duplicate (path);
5101 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5102 send_create_path (dest_peer_info, path, t);
5109 * Core handler for path destruction
5111 * @param cls closure
5112 * @param message message
5113 * @param peer peer identity this notification is about
5114 * @param atsi performance data
5115 * @param atsi_count number of records in 'atsi'
5117 * @return GNUNET_OK to keep the connection open,
5118 * GNUNET_SYSERR to close it (signal serious error)
5121 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5122 const struct GNUNET_MessageHeader *message,
5123 const struct GNUNET_ATS_Information *atsi,
5124 unsigned int atsi_count)
5126 struct GNUNET_MESH_ManipulatePath *msg;
5127 struct GNUNET_PeerIdentity *pi;
5128 struct MeshPeerPath *path;
5129 struct MeshTunnel *t;
5130 unsigned int own_pos;
5134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5135 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5136 size = ntohs (message->size);
5137 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5139 GNUNET_break_op (0);
5143 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5144 if (size % sizeof (struct GNUNET_PeerIdentity))
5146 GNUNET_break_op (0);
5149 size /= sizeof (struct GNUNET_PeerIdentity);
5152 GNUNET_break_op (0);
5155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5157 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5158 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5160 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5162 t = tunnel_get (pi, ntohl (msg->tid));
5165 /* TODO notify back: we don't know this tunnel */
5166 GNUNET_break_op (0);
5169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5170 path = path_new (size);
5172 for (i = 0; i < size; i++)
5174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5175 GNUNET_i2s (&pi[i]));
5176 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5177 if (path->peers[i] == myid)
5180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5181 if (own_pos < path->length - 1)
5182 send_message (message, &pi[own_pos + 1], t);
5184 send_client_tunnel_disconnect(t, NULL);
5186 tunnel_delete_peer (t, path->peers[path->length - 1]);
5187 path_destroy (path);
5193 * Core handler for notifications of broken paths
5195 * @param cls closure
5196 * @param message message
5197 * @param peer peer identity this notification is about
5198 * @param atsi performance data
5199 * @param atsi_count number of records in 'atsi'
5201 * @return GNUNET_OK to keep the connection open,
5202 * GNUNET_SYSERR to close it (signal serious error)
5205 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5206 const struct GNUNET_MessageHeader *message,
5207 const struct GNUNET_ATS_Information *atsi,
5208 unsigned int atsi_count)
5210 struct GNUNET_MESH_PathBroken *msg;
5211 struct MeshTunnel *t;
5213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5214 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5215 msg = (struct GNUNET_MESH_PathBroken *) message;
5216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5217 GNUNET_i2s (&msg->peer1));
5218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5219 GNUNET_i2s (&msg->peer2));
5220 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5223 GNUNET_break_op (0);
5226 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5227 GNUNET_PEER_search (&msg->peer2));
5234 * Core handler for tunnel destruction
5236 * @param cls closure
5237 * @param message message
5238 * @param peer peer identity this notification is about
5239 * @param atsi performance data
5240 * @param atsi_count number of records in 'atsi'
5242 * @return GNUNET_OK to keep the connection open,
5243 * GNUNET_SYSERR to close it (signal serious error)
5246 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5247 const struct GNUNET_MessageHeader *message,
5248 const struct GNUNET_ATS_Information *atsi,
5249 unsigned int atsi_count)
5251 struct GNUNET_MESH_TunnelDestroy *msg;
5252 struct MeshTunnel *t;
5254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5255 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5256 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5258 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5259 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5262 /* Probably already got the message from another path,
5263 * destroyed the tunnel and retransmitted to children.
5266 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5269 if (t->id.oid == myid)
5271 GNUNET_break_op (0);
5274 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5276 /* Tunnel was incoming, notify clients */
5277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5278 t->local_tid, t->local_tid_dest);
5279 send_clients_tunnel_destroy (t);
5281 tunnel_send_destroy (t);
5282 t->destroy = GNUNET_YES;
5283 // TODO: add timeout to destroy the tunnel anyway
5289 * Core handler for mesh network traffic going from the origin to a peer
5291 * @param cls closure
5292 * @param peer peer identity this notification is about
5293 * @param message message
5294 * @param atsi performance data
5295 * @param atsi_count number of records in 'atsi'
5296 * @return GNUNET_OK to keep the connection open,
5297 * GNUNET_SYSERR to close it (signal serious error)
5300 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5301 const struct GNUNET_MessageHeader *message,
5302 const struct GNUNET_ATS_Information *atsi,
5303 unsigned int atsi_count)
5305 struct GNUNET_MESH_Unicast *msg;
5306 struct GNUNET_PeerIdentity *neighbor;
5307 struct MeshTunnelChildInfo *cinfo;
5308 struct MeshTunnel *t;
5309 GNUNET_PEER_Id dest_id;
5314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5317 size = ntohs (message->size);
5319 sizeof (struct GNUNET_MESH_Unicast) +
5320 sizeof (struct GNUNET_MessageHeader))
5325 msg = (struct GNUNET_MESH_Unicast *) message;
5326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5327 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5329 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5332 /* TODO notify back: we don't know this tunnel */
5333 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5334 GNUNET_break_op (0);
5337 pid = ntohl (msg->pid);
5338 if (t->fwd_pid == pid)
5340 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5341 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5342 " Already seen pid %u, DROPPING!\n", pid);
5347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5348 " pid %u not seen yet, forwarding\n", pid);
5351 t->skip += (pid - t->fwd_pid) - 1;
5354 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5356 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5357 GNUNET_break_op (0);
5358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5359 "Received PID %u, ACK %u\n",
5360 pid, t->last_fwd_ack);
5364 tunnel_reset_timeout (t);
5365 dest_id = GNUNET_PEER_search (&msg->destination);
5366 if (dest_id == myid)
5368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5369 " it's for us! sending to clients...\n");
5370 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5371 send_subscribed_clients (message, &msg[1].header, t);
5372 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5375 ttl = ntohl (msg->ttl);
5376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5379 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5380 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5381 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5385 " not for us, retransmitting...\n");
5387 neighbor = tree_get_first_hop (t->tree, dest_id);
5388 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5390 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5393 if (GNUNET_YES == t->nobuffer &&
5394 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5396 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5397 GNUNET_break_op (0);
5400 send_message (message, neighbor, t);
5401 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5407 * Core handler for mesh network traffic going from the origin to all peers
5409 * @param cls closure
5410 * @param message message
5411 * @param peer peer identity this notification is about
5412 * @param atsi performance data
5413 * @param atsi_count number of records in 'atsi'
5414 * @return GNUNET_OK to keep the connection open,
5415 * GNUNET_SYSERR to close it (signal serious error)
5417 * TODO: Check who we got this from, to validate route.
5420 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5421 const struct GNUNET_MessageHeader *message,
5422 const struct GNUNET_ATS_Information *atsi,
5423 unsigned int atsi_count)
5425 struct GNUNET_MESH_Multicast *msg;
5426 struct MeshTunnel *t;
5430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5432 size = ntohs (message->size);
5433 if (sizeof (struct GNUNET_MESH_Multicast) +
5434 sizeof (struct GNUNET_MessageHeader) > size)
5436 GNUNET_break_op (0);
5439 msg = (struct GNUNET_MESH_Multicast *) message;
5440 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5444 /* TODO notify that we dont know that tunnel */
5445 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5446 GNUNET_break_op (0);
5449 pid = ntohl (msg->pid);
5450 if (t->fwd_pid == pid)
5452 /* already seen this packet, drop */
5453 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5455 " Already seen pid %u, DROPPING!\n", pid);
5456 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5462 " pid %u not seen yet, forwarding\n", pid);
5464 t->skip += (pid - t->fwd_pid) - 1;
5466 tunnel_reset_timeout (t);
5468 /* Transmit to locally interested clients */
5469 if (NULL != t->peers &&
5470 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5472 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5473 send_subscribed_clients (message, &msg[1].header, t);
5474 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5477 if (ntohl (msg->ttl) == 0)
5479 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5480 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5481 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5484 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5485 tunnel_send_multicast (t, message, GNUNET_NO);
5491 * Core handler for mesh network traffic toward the owner of a tunnel
5493 * @param cls closure
5494 * @param message message
5495 * @param peer peer identity this notification is about
5496 * @param atsi performance data
5497 * @param atsi_count number of records in 'atsi'
5499 * @return GNUNET_OK to keep the connection open,
5500 * GNUNET_SYSERR to close it (signal serious error)
5503 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5504 const struct GNUNET_MessageHeader *message,
5505 const struct GNUNET_ATS_Information *atsi,
5506 unsigned int atsi_count)
5508 struct GNUNET_MESH_ToOrigin *msg;
5509 struct GNUNET_PeerIdentity id;
5510 struct MeshPeerInfo *peer_info;
5511 struct MeshTunnel *t;
5515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5517 size = ntohs (message->size);
5518 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5519 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5521 GNUNET_break_op (0);
5524 msg = (struct GNUNET_MESH_ToOrigin *) message;
5525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5526 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5527 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5531 /* TODO notify that we dont know this tunnel (whom)? */
5532 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5533 GNUNET_break_op (0);
5537 if (NULL != t->owner)
5540 struct GNUNET_MESH_ToOrigin *copy;
5542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5543 " it's for us! sending to clients...\n");
5544 /* TODO signature verification */
5545 memcpy (cbuf, message, size);
5546 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5547 copy->tid = htonl (t->local_tid);
5548 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5549 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5550 ©->header, GNUNET_NO);
5551 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5555 " not for us, retransmitting...\n");
5557 peer_info = peer_info_get (&msg->oid);
5558 if (NULL == peer_info)
5560 /* unknown origin of tunnel */
5564 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5565 send_message (message, &id, t);
5566 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5573 * Core handler for mesh network traffic point-to-point acks.
5575 * @param cls closure
5576 * @param message message
5577 * @param peer peer identity this notification is about
5578 * @param atsi performance data
5579 * @param atsi_count number of records in 'atsi'
5581 * @return GNUNET_OK to keep the connection open,
5582 * GNUNET_SYSERR to close it (signal serious error)
5585 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5586 const struct GNUNET_MessageHeader *message,
5587 const struct GNUNET_ATS_Information *atsi,
5588 unsigned int atsi_count)
5590 struct GNUNET_MESH_ACK *msg;
5591 struct MeshTunnel *t;
5594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5596 msg = (struct GNUNET_MESH_ACK *) message;
5598 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5602 /* TODO notify that we dont know this tunnel (whom)? */
5603 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5604 GNUNET_break_op (0);
5607 ack = ntohl (msg->pid);
5609 /* Is this a forward or backward ACK? */
5610 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5612 struct MeshTunnelChildInfo *cinfo;
5615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5616 cinfo = tunnel_get_neighbor_fc (t, peer);
5617 cinfo->fwd_ack = ack;
5618 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5619 tunnel_unlock_fwd_queues (t);
5623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5625 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5626 tunnel_unlock_bck_queue (t);
5633 * Core handler for path ACKs
5635 * @param cls closure
5636 * @param message message
5637 * @param peer peer identity this notification is about
5638 * @param atsi performance data
5639 * @param atsi_count number of records in 'atsi'
5641 * @return GNUNET_OK to keep the connection open,
5642 * GNUNET_SYSERR to close it (signal serious error)
5645 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5646 const struct GNUNET_MessageHeader *message,
5647 const struct GNUNET_ATS_Information *atsi,
5648 unsigned int atsi_count)
5650 struct GNUNET_MESH_PathACK *msg;
5651 struct GNUNET_PeerIdentity id;
5652 struct MeshPeerInfo *peer_info;
5653 struct MeshPeerPath *p;
5654 struct MeshTunnel *t;
5656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5657 GNUNET_i2s (&my_full_id));
5658 msg = (struct GNUNET_MESH_PathACK *) message;
5659 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5662 /* TODO notify that we don't know the tunnel */
5663 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5665 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5669 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5671 peer_info = peer_info_get (&msg->peer_id);
5672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5673 GNUNET_i2s (&msg->peer_id));
5674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5677 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5680 "connect_by_string completed, stopping search\n");
5681 regex_cancel_search (t->regex_ctx);
5682 t->regex_ctx = NULL;
5685 /* Add paths to peers? */
5686 p = tree_get_path_to_peer (t->tree, peer_info->id);
5689 path_add_to_peers (p, GNUNET_YES);
5697 /* Message for us? */
5698 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5701 if (NULL == t->owner)
5703 GNUNET_break_op (0);
5706 if (NULL != t->dht_get_type)
5708 GNUNET_DHT_get_stop (t->dht_get_type);
5709 t->dht_get_type = NULL;
5711 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5713 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5714 send_client_peer_connected (t, peer_info->id);
5719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5720 " not for us, retransmitting...\n");
5721 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5722 peer_info = peer_info_get (&msg->oid);
5723 if (NULL == peer_info)
5725 /* If we know the tunnel, we should DEFINITELY know the peer */
5729 send_message (message, &id, t);
5735 * Functions to handle messages from core
5737 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5738 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5739 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5740 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5741 sizeof (struct GNUNET_MESH_PathBroken)},
5742 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5743 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5744 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5745 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5746 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5747 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5748 sizeof (struct GNUNET_MESH_ACK)},
5749 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5750 sizeof (struct GNUNET_MESH_PathACK)},
5756 /******************************************************************************/
5757 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5758 /******************************************************************************/
5761 * deregister_app: iterator for removing each application registered by a client
5763 * @param cls closure
5764 * @param key the hash of the application id (used to access the hashmap)
5765 * @param value the value stored at the key (client)
5767 * @return GNUNET_OK on success
5770 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5772 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5773 GNUNET_break (GNUNET_YES ==
5774 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5780 * notify_client_connection_failure: notify a client that the connection to the
5781 * requested remote peer is not possible (for instance, no route found)
5782 * Function called when the socket is ready to queue more data. "buf" will be
5783 * NULL and "size" zero if the socket was closed for writing in the meantime.
5785 * @param cls closure
5786 * @param size number of bytes available in buf
5787 * @param buf where the callee should write the message
5788 * @return number of bytes written to buf
5791 notify_client_connection_failure (void *cls, size_t size, void *buf)
5794 struct MeshPeerInfo *peer_info;
5795 struct GNUNET_MESH_PeerControl *msg;
5796 struct GNUNET_PeerIdentity id;
5798 if (0 == size && NULL == buf)
5800 // TODO retry? cancel?
5804 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5805 peer_info = (struct MeshPeerInfo *) cls;
5806 msg = (struct GNUNET_MESH_PeerControl *) buf;
5807 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5808 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5809 // msg->tunnel_id = htonl(peer_info->t->tid);
5810 GNUNET_PEER_resolve (peer_info->id, &id);
5811 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5819 * Send keepalive packets for a peer
5821 * @param cls Closure (tunnel for which to send the keepalive).
5822 * @param tc Notification context.
5824 * TODO: implement explicit multicast keepalive?
5827 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5829 struct MeshTunnel *t = cls;
5830 struct GNUNET_MessageHeader *payload;
5831 struct GNUNET_MESH_Multicast *msg;
5833 sizeof (struct GNUNET_MESH_Multicast) +
5834 sizeof (struct GNUNET_MessageHeader);
5837 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5841 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5844 "sending keepalive for tunnel %d\n", t->id.tid);
5846 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5847 msg->header.size = htons (size);
5848 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5849 // FIXME: change type to != MULTICAST
5850 msg->oid = my_full_id;
5851 msg->tid = htonl (t->id.tid);
5852 msg->ttl = htonl (default_ttl);
5853 msg->pid = htonl (t->fwd_pid + 1);
5855 payload = (struct GNUNET_MessageHeader *) &msg[1];
5856 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5857 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5858 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5860 t->path_refresh_task =
5861 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5867 * Function to process paths received for a new peer addition. The recorded
5868 * paths form the initial tunnel, which can be optimized later.
5869 * Called on each result obtained for the DHT search.
5871 * @param cls closure
5872 * @param exp when will this value expire
5873 * @param key key of the result
5874 * @param get_path path of the get request
5875 * @param get_path_length lenght of get_path
5876 * @param put_path path of the put request
5877 * @param put_path_length length of the put_path
5878 * @param type type of the result
5879 * @param size number of bytes in data
5880 * @param data pointer to the result data
5882 * TODO: re-issue the request after certain time? cancel after X results?
5885 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5886 const struct GNUNET_HashCode * key,
5887 const struct GNUNET_PeerIdentity *get_path,
5888 unsigned int get_path_length,
5889 const struct GNUNET_PeerIdentity *put_path,
5890 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5891 size_t size, const void *data)
5893 struct MeshPathInfo *path_info = cls;
5894 struct MeshPeerPath *p;
5895 struct GNUNET_PeerIdentity pi;
5898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5899 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5902 p = path_build_from_dht (get_path, get_path_length, put_path,
5904 path_add_to_peers (p, GNUNET_NO);
5906 for (i = 0; i < path_info->peer->ntunnels; i++)
5908 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5909 peer_info_connect (path_info->peer, path_info->t);
5917 * Function to process paths received for a new peer addition. The recorded
5918 * paths form the initial tunnel, which can be optimized later.
5919 * Called on each result obtained for the DHT search.
5921 * @param cls closure
5922 * @param exp when will this value expire
5923 * @param key key of the result
5924 * @param get_path path of the get request
5925 * @param get_path_length lenght of get_path
5926 * @param put_path path of the put request
5927 * @param put_path_length length of the put_path
5928 * @param type type of the result
5929 * @param size number of bytes in data
5930 * @param data pointer to the result data
5933 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5934 const struct GNUNET_HashCode * key,
5935 const struct GNUNET_PeerIdentity *get_path,
5936 unsigned int get_path_length,
5937 const struct GNUNET_PeerIdentity *put_path,
5938 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5939 size_t size, const void *data)
5941 const struct PBlock *pb = data;
5942 const struct GNUNET_PeerIdentity *pi = &pb->id;
5943 struct MeshTunnel *t = cls;
5944 struct MeshPeerInfo *peer_info;
5945 struct MeshPeerPath *p;
5947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5948 if (size != sizeof (struct PBlock))
5950 GNUNET_break_op (0);
5953 if (ntohl(pb->type) != t->type)
5955 GNUNET_break_op (0);
5958 GNUNET_assert (NULL != t->owner);
5959 peer_info = peer_info_get (pi);
5960 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5962 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5964 p = path_build_from_dht (get_path, get_path_length, put_path,
5966 path_add_to_peers (p, GNUNET_NO);
5968 tunnel_add_peer (t, peer_info);
5969 peer_info_connect (peer_info, t);
5974 * Function to process DHT string to regex matching.
5975 * Called on each result obtained for the DHT search.
5977 * @param cls closure (search context)
5978 * @param exp when will this value expire
5979 * @param key key of the result
5980 * @param get_path path of the get request (not used)
5981 * @param get_path_length lenght of get_path (not used)
5982 * @param put_path path of the put request (not used)
5983 * @param put_path_length length of the put_path (not used)
5984 * @param type type of the result
5985 * @param size number of bytes in data
5986 * @param data pointer to the result data
5989 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5990 const struct GNUNET_HashCode * key,
5991 const struct GNUNET_PeerIdentity *get_path,
5992 unsigned int get_path_length,
5993 const struct GNUNET_PeerIdentity *put_path,
5994 unsigned int put_path_length,
5995 enum GNUNET_BLOCK_Type type,
5996 size_t size, const void *data)
5998 const struct MeshRegexAccept *block = data;
5999 struct MeshRegexSearchContext *ctx = cls;
6000 struct MeshRegexSearchInfo *info = ctx->info;
6001 struct MeshPeerPath *p;
6002 struct MeshPeerInfo *peer_info;
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6007 peer_info = peer_info_get(&block->id);
6008 p = path_build_from_dht (get_path, get_path_length, put_path,
6010 path_add_to_peers (p, GNUNET_NO);
6013 tunnel_add_peer (info->t, peer_info);
6014 peer_info_connect (peer_info, info->t);
6015 if (0 == info->peer)
6017 info->peer = peer_info->id;
6021 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6024 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6025 ®ex_connect_timeout,
6033 * Function to process DHT string to regex matching.
6034 * Called on each result obtained for the DHT search.
6036 * @param cls closure (search context)
6037 * @param exp when will this value expire
6038 * @param key key of the result
6039 * @param get_path path of the get request (not used)
6040 * @param get_path_length lenght of get_path (not used)
6041 * @param put_path path of the put request (not used)
6042 * @param put_path_length length of the put_path (not used)
6043 * @param type type of the result
6044 * @param size number of bytes in data
6045 * @param data pointer to the result data
6047 * TODO: re-issue the request after certain time? cancel after X results?
6050 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6051 const struct GNUNET_HashCode * key,
6052 const struct GNUNET_PeerIdentity *get_path,
6053 unsigned int get_path_length,
6054 const struct GNUNET_PeerIdentity *put_path,
6055 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6056 size_t size, const void *data)
6058 const struct MeshRegexBlock *block = data;
6059 struct MeshRegexSearchContext *ctx = cls;
6060 struct MeshRegexSearchInfo *info = ctx->info;
6064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6065 "DHT GET STRING RETURNED RESULTS\n");
6066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6067 " key: %s\n", GNUNET_h2s (key));
6069 copy = GNUNET_malloc (size);
6070 memcpy (copy, data, size);
6071 GNUNET_break (GNUNET_OK ==
6072 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6073 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6074 len = ntohl (block->n_proof);
6076 char proof[len + 1];
6078 memcpy (proof, &block[1], len);
6080 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6082 GNUNET_break_op (0);
6086 len = strlen (info->description);
6087 if (len == ctx->position) // String processed
6089 if (GNUNET_YES == ntohl (block->accepting))
6091 regex_find_path(key, ctx);
6095 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6096 // FIXME REGEX this block not successful, wait for more? start timeout?
6100 GNUNET_break (GNUNET_OK ==
6101 GNUNET_MESH_regex_block_iterate (block, size,
6102 ®ex_edge_iterator, ctx));
6106 /******************************************************************************/
6107 /********************* MESH LOCAL HANDLES **************************/
6108 /******************************************************************************/
6112 * Handler for client disconnection
6114 * @param cls closure
6115 * @param client identification of the client; NULL
6116 * for the last call when the server is destroyed
6119 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6121 struct MeshClient *c;
6122 struct MeshClient *next;
6125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6134 if (c->handle != client)
6136 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6142 GNUNET_SERVER_client_drop (c->handle);
6143 c->shutting_down = GNUNET_YES;
6144 GNUNET_assert (NULL != c->own_tunnels);
6145 GNUNET_assert (NULL != c->incoming_tunnels);
6146 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6147 &tunnel_destroy_iterator, c);
6148 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6149 &tunnel_destroy_iterator, c);
6150 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6151 &tunnel_destroy_iterator, c);
6152 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6153 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6154 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6156 /* deregister clients applications */
6157 if (NULL != c->apps)
6159 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6160 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6162 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6163 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6165 GNUNET_SCHEDULER_cancel (announce_applications_task);
6166 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6168 if (NULL != c->types)
6169 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6170 for (i = 0; i < c->n_regex; i++)
6172 GNUNET_free (c->regexes[i]);
6174 GNUNET_free_non_null (c->regexes);
6175 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6176 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6178 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6181 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6190 * Handler for new clients
6192 * @param cls closure
6193 * @param client identification of the client
6194 * @param message the actual message, which includes messages the client wants
6197 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6198 const struct GNUNET_MessageHeader *message)
6200 struct GNUNET_MESH_ClientConnect *cc_msg;
6201 struct MeshClient *c;
6202 GNUNET_MESH_ApplicationType *a;
6209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6210 /* Check data sanity */
6211 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6212 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6213 ntypes = ntohs (cc_msg->types);
6214 napps = ntohs (cc_msg->applications);
6216 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6219 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6223 /* Create new client structure */
6224 c = GNUNET_malloc (sizeof (struct MeshClient));
6225 c->id = next_client_id++;
6226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6228 GNUNET_SERVER_client_keep (client);
6229 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6232 GNUNET_MESH_ApplicationType at;
6233 struct GNUNET_HashCode hc;
6235 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6236 for (i = 0; i < napps; i++)
6239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6240 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6241 /* store in clients hashmap */
6242 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6243 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6244 /* store in global hashmap, for announcements */
6245 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6246 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6248 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6249 announce_applications_task =
6250 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6256 struct GNUNET_HashCode hc;
6258 t = (uint16_t *) & a[napps];
6259 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6260 for (i = 0; i < ntypes; i++)
6263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6264 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6266 /* store in clients hashmap */
6267 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6268 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6269 /* store in global hashmap */
6270 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6271 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6275 " client has %u+%u subscriptions\n", napps, ntypes);
6277 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6278 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6279 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6280 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6281 GNUNET_SERVER_notification_context_add (nc, client);
6282 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6284 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6290 * Handler for clients announcing available services by a regular expression.
6292 * @param cls closure
6293 * @param client identification of the client
6294 * @param message the actual message, which includes messages the client wants
6297 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6298 const struct GNUNET_MessageHeader *message)
6300 struct MeshClient *c;
6304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6306 /* Sanity check for client registration */
6307 if (NULL == (c = client_get (client)))
6310 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6315 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6316 regex = GNUNET_malloc (len + 1);
6317 memcpy (regex, &message[1], len);
6319 GNUNET_array_append (c->regexes, c->n_regex, regex);
6320 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6322 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6328 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6334 * Handler for requests of new tunnels
6336 * @param cls closure
6337 * @param client identification of the client
6338 * @param message the actual message
6341 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6342 const struct GNUNET_MessageHeader *message)
6344 struct GNUNET_MESH_TunnelMessage *t_msg;
6345 struct MeshTunnel *t;
6346 struct MeshClient *c;
6347 MESH_TunnelNumber tid;
6349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6351 /* Sanity check for client registration */
6352 if (NULL == (c = client_get (client)))
6355 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6360 /* Message sanity check */
6361 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6364 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6368 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6369 /* Sanity check for tunnel numbering */
6370 tid = ntohl (t_msg->tunnel_id);
6371 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6374 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6377 /* Sanity check for duplicate tunnel IDs */
6378 if (NULL != tunnel_get_by_local_id (c, tid))
6381 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6385 while (NULL != tunnel_get_by_pi (myid, next_tid))
6386 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6387 t = tunnel_new (myid, next_tid++, c, tid);
6390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6392 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6395 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6397 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6398 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6401 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6407 * Handler for requests of deleting tunnels
6409 * @param cls closure
6410 * @param client identification of the client
6411 * @param message the actual message
6414 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6415 const struct GNUNET_MessageHeader *message)
6417 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6418 struct MeshClient *c;
6419 struct MeshTunnel *t;
6420 MESH_TunnelNumber tid;
6422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6423 "Got a DESTROY TUNNEL from client!\n");
6425 /* Sanity check for client registration */
6426 if (NULL == (c = client_get (client)))
6429 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6434 /* Message sanity check */
6435 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6438 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6442 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6444 /* Retrieve tunnel */
6445 tid = ntohl (tunnel_msg->tunnel_id);
6446 t = tunnel_get_by_local_id(c, tid);
6449 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6451 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6454 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6456 client_ignore_tunnel (c, t);
6458 // TODO: when to destroy incoming tunnel?
6459 if (t->nclients == 0)
6461 GNUNET_assert (GNUNET_YES ==
6462 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6464 GNUNET_assert (GNUNET_YES ==
6465 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6466 &my_full_id.hashPubKey,
6470 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6473 send_client_tunnel_disconnect(t, c);
6474 client_delete_tunnel(c, t);
6476 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6478 tunnel_send_destroy (t);
6479 t->destroy = GNUNET_YES;
6480 // The tunnel will be destroyed when the last message is transmitted.
6481 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6487 * Handler for requests of seeting tunnel's speed.
6489 * @param cls Closure (unused).
6490 * @param client Identification of the client.
6491 * @param message The actual message.
6494 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6495 const struct GNUNET_MessageHeader *message)
6497 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6498 struct MeshClient *c;
6499 struct MeshTunnel *t;
6500 MESH_TunnelNumber tid;
6502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6503 "Got a SPEED request from client!\n");
6505 /* Sanity check for client registration */
6506 if (NULL == (c = client_get (client)))
6509 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6515 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6517 /* Retrieve tunnel */
6518 tid = ntohl (tunnel_msg->tunnel_id);
6519 t = tunnel_get_by_local_id(c, tid);
6522 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6524 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6528 switch (ntohs(message->type))
6530 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6531 t->speed_min = GNUNET_YES;
6533 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6534 t->speed_min = GNUNET_NO;
6539 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6544 * Handler for requests of seeting tunnel's buffering policy.
6546 * @param cls Closure (unused).
6547 * @param client Identification of the client.
6548 * @param message The actual message.
6551 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6552 const struct GNUNET_MessageHeader *message)
6554 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6555 struct MeshClient *c;
6556 struct MeshTunnel *t;
6557 MESH_TunnelNumber tid;
6559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6560 "Got a BUFFER request from client!\n");
6562 /* Sanity check for client registration */
6563 if (NULL == (c = client_get (client)))
6566 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6571 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6573 /* Retrieve tunnel */
6574 tid = ntohl (tunnel_msg->tunnel_id);
6575 t = tunnel_get_by_local_id(c, tid);
6578 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6580 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6584 switch (ntohs(message->type))
6586 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6587 t->nobuffer = GNUNET_NO;
6589 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6590 t->nobuffer = GNUNET_YES;
6596 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6601 * Handler for connection requests to new peers
6603 * @param cls closure
6604 * @param client identification of the client
6605 * @param message the actual message (PeerControl)
6608 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6609 const struct GNUNET_MessageHeader *message)
6611 struct GNUNET_MESH_PeerControl *peer_msg;
6612 struct MeshPeerInfo *peer_info;
6613 struct MeshClient *c;
6614 struct MeshTunnel *t;
6615 MESH_TunnelNumber tid;
6617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6618 /* Sanity check for client registration */
6619 if (NULL == (c = client_get (client)))
6622 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6627 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6629 /* Sanity check for message size */
6630 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6633 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6637 /* Tunnel exists? */
6638 tid = ntohl (peer_msg->tunnel_id);
6639 t = tunnel_get_by_local_id (c, tid);
6643 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6647 /* Does client own tunnel? */
6648 if (t->owner->handle != client)
6651 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6655 GNUNET_i2s (&peer_msg->peer));
6656 peer_info = peer_info_get (&peer_msg->peer);
6658 tunnel_add_peer (t, peer_info);
6659 peer_info_connect (peer_info, t);
6661 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6667 * Handler for disconnection requests of peers in a tunnel
6669 * @param cls closure
6670 * @param client identification of the client
6671 * @param message the actual message (PeerControl)
6674 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6675 const struct GNUNET_MessageHeader *message)
6677 struct GNUNET_MESH_PeerControl *peer_msg;
6678 struct MeshPeerInfo *peer_info;
6679 struct MeshClient *c;
6680 struct MeshTunnel *t;
6681 MESH_TunnelNumber tid;
6683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6684 /* Sanity check for client registration */
6685 if (NULL == (c = client_get (client)))
6688 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6693 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6695 /* Sanity check for message size */
6696 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6699 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6703 /* Tunnel exists? */
6704 tid = ntohl (peer_msg->tunnel_id);
6705 t = tunnel_get_by_local_id (c, tid);
6709 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6714 /* Does client own tunnel? */
6715 if (t->owner->handle != client)
6718 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6723 GNUNET_i2s (&peer_msg->peer));
6724 /* Is the peer in the tunnel? */
6726 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6727 if (NULL == peer_info)
6730 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6734 /* Ok, delete peer from tunnel */
6735 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6736 &peer_msg->peer.hashPubKey);
6738 send_destroy_path (t, peer_info->id);
6739 tunnel_delete_peer (t, peer_info->id);
6740 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6745 * Handler for blacklist requests of peers in a tunnel
6747 * @param cls closure
6748 * @param client identification of the client
6749 * @param message the actual message (PeerControl)
6751 * FIXME implement DHT block bloomfilter
6754 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6755 const struct GNUNET_MessageHeader *message)
6757 struct GNUNET_MESH_PeerControl *peer_msg;
6758 struct MeshClient *c;
6759 struct MeshTunnel *t;
6760 MESH_TunnelNumber tid;
6762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6763 /* Sanity check for client registration */
6764 if (NULL == (c = client_get (client)))
6767 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6772 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6774 /* Sanity check for message size */
6775 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6778 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6782 /* Tunnel exists? */
6783 tid = ntohl (peer_msg->tunnel_id);
6784 t = tunnel_get_by_local_id (c, tid);
6788 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6793 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6794 GNUNET_PEER_intern(&peer_msg->peer));
6799 * Handler for unblacklist requests of peers in a tunnel
6801 * @param cls closure
6802 * @param client identification of the client
6803 * @param message the actual message (PeerControl)
6806 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6807 const struct GNUNET_MessageHeader *message)
6809 struct GNUNET_MESH_PeerControl *peer_msg;
6810 struct MeshClient *c;
6811 struct MeshTunnel *t;
6812 MESH_TunnelNumber tid;
6816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6817 /* Sanity check for client registration */
6818 if (NULL == (c = client_get (client)))
6821 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6824 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6826 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6828 /* Sanity check for message size */
6829 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6832 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6836 /* Tunnel exists? */
6837 tid = ntohl (peer_msg->tunnel_id);
6838 t = tunnel_get_by_local_id (c, tid);
6842 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6847 /* if peer is not known, complain */
6848 pid = GNUNET_PEER_search (&peer_msg->peer);
6855 /* search and remove from list */
6856 for (i = 0; i < t->nblacklisted; i++)
6858 if (t->blacklisted[i] == pid)
6860 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6861 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6866 /* if peer hasn't been blacklisted, complain */
6872 * Handler for connection requests to new peers by type
6874 * @param cls closure
6875 * @param client identification of the client
6876 * @param message the actual message (ConnectPeerByType)
6879 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6880 const struct GNUNET_MessageHeader *message)
6882 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6883 struct MeshClient *c;
6884 struct MeshTunnel *t;
6885 struct GNUNET_HashCode hash;
6886 MESH_TunnelNumber tid;
6888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6889 /* Sanity check for client registration */
6890 if (NULL == (c = client_get (client)))
6893 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6898 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6900 /* Sanity check for message size */
6901 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6902 ntohs (connect_msg->header.size))
6905 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6909 /* Tunnel exists? */
6910 tid = ntohl (connect_msg->tunnel_id);
6911 t = tunnel_get_by_local_id (c, tid);
6915 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6919 /* Does client own tunnel? */
6920 if (t->owner->handle != client)
6923 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6927 /* Do WE have the service? */
6928 t->type = ntohl (connect_msg->type);
6929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6930 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6931 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6934 /* Yes! Fast forward, add ourselves to the tunnel and send the
6935 * good news to the client, and alert the destination client of
6936 * an incoming tunnel.
6938 * FIXME send a path create to self, avoid code duplication
6940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6941 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6942 peer_info_get (&my_full_id),
6943 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6946 send_client_peer_connected (t, myid);
6947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6948 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6950 t->local_tid_dest = next_local_tid++;
6951 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6952 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6953 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6957 /* Ok, lets find a peer offering the service */
6958 if (NULL != t->dht_get_type)
6960 GNUNET_DHT_get_stop (t->dht_get_type);
6962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6963 GNUNET_h2s (&hash));
6965 GNUNET_DHT_get_start (dht_handle,
6966 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6968 dht_replication_level,
6969 GNUNET_DHT_RO_RECORD_ROUTE |
6970 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6972 &dht_get_type_handler, t);
6974 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6980 * Handler for connection requests to new peers by a string service description.
6982 * @param cls closure
6983 * @param client identification of the client
6984 * @param message the actual message, which includes messages the client wants
6987 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6988 const struct GNUNET_MessageHeader *message)
6990 struct GNUNET_MESH_ConnectPeerByString *msg;
6991 struct MeshRegexSearchContext *ctx;
6992 struct MeshRegexSearchInfo *info;
6993 struct GNUNET_DHT_GetHandle *get_h;
6994 struct GNUNET_HashCode key;
6995 struct MeshTunnel *t;
6996 struct MeshClient *c;
6997 MESH_TunnelNumber tid;
7001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7002 "Connect by string started\n");
7003 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7004 size = htons (message->size);
7006 /* Sanity check for client registration */
7007 if (NULL == (c = client_get (client)))
7010 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7015 /* Message size sanity check */
7016 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7019 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7023 /* Tunnel exists? */
7024 tid = ntohl (msg->tunnel_id);
7025 t = tunnel_get_by_local_id (c, tid);
7029 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7033 /* Does client own tunnel? */
7034 if (t->owner->handle != client)
7037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7042 " on tunnel %s [%u]\n",
7043 GNUNET_i2s(&my_full_id),
7046 /* Only one connect_by_string allowed at the same time! */
7047 /* FIXME: allow more, return handle at api level to cancel, document */
7048 if (NULL != t->regex_ctx)
7051 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7055 /* Find string itself */
7056 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7057 string = (const char *) &msg[1];
7059 /* Initialize context */
7060 size = GNUNET_REGEX_get_first_key(string, len, &key);
7061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7062 " consumed %u bits out of %u\n", size, len);
7063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7064 " looking for %s\n", GNUNET_h2s (&key));
7066 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7068 info->description = GNUNET_malloc (len + 1);
7069 memcpy (info->description, string, len);
7070 info->description[len] = '\0';
7071 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7072 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7075 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7076 ctx->position = size;
7080 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7082 /* Start search in DHT */
7083 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7084 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7085 &key, /* key to search */
7086 dht_replication_level, /* replication level */
7087 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7088 NULL, /* xquery */ // FIXME BLOOMFILTER
7089 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7090 &dht_get_string_handler, ctx);
7092 GNUNET_break (GNUNET_OK ==
7093 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7096 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7098 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7104 * Handler for client traffic directed to one peer
7106 * @param cls closure
7107 * @param client identification of the client
7108 * @param message the actual message
7111 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7112 const struct GNUNET_MessageHeader *message)
7114 struct MeshClient *c;
7115 struct MeshTunnel *t;
7116 struct MeshPeerInfo *pi;
7117 struct GNUNET_MESH_Unicast *data_msg;
7118 MESH_TunnelNumber tid;
7121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7122 "Got a unicast request from a client!\n");
7124 /* Sanity check for client registration */
7125 if (NULL == (c = client_get (client)))
7128 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7133 data_msg = (struct GNUNET_MESH_Unicast *) message;
7135 /* Sanity check for message size */
7136 size = ntohs (message->size);
7137 if (sizeof (struct GNUNET_MESH_Unicast) +
7138 sizeof (struct GNUNET_MessageHeader) > size)
7141 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7145 /* Tunnel exists? */
7146 tid = ntohl (data_msg->tid);
7147 t = tunnel_get_by_local_id (c, tid);
7151 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7155 /* Is it a local tunnel? Then, does client own the tunnel? */
7156 if (t->owner->handle != client)
7159 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7163 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7164 &data_msg->destination.hashPubKey);
7165 /* Is the selected peer in the tunnel? */
7169 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7173 /* PID should be as expected */
7174 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7177 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7178 "Unicast PID, expected %u, got %u\n",
7179 t->fwd_pid + 1, ntohl (data_msg->pid));
7180 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7184 /* Ok, everything is correct, send the message
7185 * (pretend we got it from a mesh peer)
7188 /* Work around const limitation */
7189 char buf[ntohs (message->size)] GNUNET_ALIGN;
7190 struct GNUNET_MESH_Unicast *copy;
7192 copy = (struct GNUNET_MESH_Unicast *) buf;
7193 memcpy (buf, data_msg, size);
7194 copy->oid = my_full_id;
7195 copy->tid = htonl (t->id.tid);
7196 copy->ttl = htonl (default_ttl);
7197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7198 " calling generic handler...\n");
7199 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7202 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7209 * Handler for client traffic directed to the origin
7211 * @param cls closure
7212 * @param client identification of the client
7213 * @param message the actual message
7216 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7217 const struct GNUNET_MessageHeader *message)
7219 struct GNUNET_MESH_ToOrigin *data_msg;
7220 struct MeshTunnelClientInfo *clinfo;
7221 struct MeshClient *c;
7222 struct MeshTunnel *t;
7223 MESH_TunnelNumber tid;
7226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7227 "Got a ToOrigin request from a client!\n");
7228 /* Sanity check for client registration */
7229 if (NULL == (c = client_get (client)))
7232 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7237 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7239 /* Sanity check for message size */
7240 size = ntohs (message->size);
7241 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7242 sizeof (struct GNUNET_MessageHeader) > size)
7245 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7249 /* Tunnel exists? */
7250 tid = ntohl (data_msg->tid);
7251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7252 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7255 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7258 t = tunnel_get_by_local_id (c, tid);
7261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7262 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7264 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7268 /* It should be sent by someone who has this as incoming tunnel. */
7269 if (GNUNET_NO == client_knows_tunnel (c, t))
7272 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7276 /* PID should be as expected */
7277 clinfo = tunnel_get_client_fc (t, c);
7278 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7281 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7282 "To Origin PID, expected %u, got %u\n",
7283 clinfo->bck_pid + 1,
7284 ntohl (data_msg->pid));
7285 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7289 /* Ok, everything is correct, send the message
7290 * (pretend we got it from a mesh peer)
7294 char buf[ntohs (message->size)] GNUNET_ALIGN;
7295 struct GNUNET_MESH_ToOrigin *copy;
7297 /* Work around const limitation */
7298 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7299 memcpy (buf, data_msg, size);
7300 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7301 copy->tid = htonl (t->id.tid);
7302 copy->ttl = htonl (default_ttl);
7303 if (ntohl (copy->pid) != (t->bck_pid + 1))
7306 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7307 "To Origin PID, expected %u, got %u\n",
7313 copy->sender = my_full_id;
7314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7315 " calling generic handler...\n");
7316 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7318 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7325 * Handler for client traffic directed to all peers in a tunnel
7327 * @param cls closure
7328 * @param client identification of the client
7329 * @param message the actual message
7332 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7333 const struct GNUNET_MessageHeader *message)
7335 struct MeshClient *c;
7336 struct MeshTunnel *t;
7337 struct GNUNET_MESH_Multicast *data_msg;
7338 MESH_TunnelNumber tid;
7340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7341 "Got a multicast request from a client!\n");
7343 /* Sanity check for client registration */
7344 if (NULL == (c = client_get (client)))
7347 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7352 data_msg = (struct GNUNET_MESH_Multicast *) message;
7354 /* Sanity check for message size */
7355 if (sizeof (struct GNUNET_MESH_Multicast) +
7356 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7359 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7363 /* Tunnel exists? */
7364 tid = ntohl (data_msg->tid);
7365 t = tunnel_get_by_local_id (c, tid);
7369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7370 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7372 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7376 /* Does client own tunnel? */
7377 if (t->owner->handle != client)
7380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7384 /* PID should be as expected */
7385 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7388 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7389 "Multicast PID, expected %u, got %u\n",
7390 t->fwd_pid + 1, ntohl (data_msg->pid));
7391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7396 char buf[ntohs (message->size)] GNUNET_ALIGN;
7397 struct GNUNET_MESH_Multicast *copy;
7399 copy = (struct GNUNET_MESH_Multicast *) buf;
7400 memcpy (buf, message, ntohs (message->size));
7401 copy->oid = my_full_id;
7402 copy->tid = htonl (t->id.tid);
7403 copy->ttl = htonl (default_ttl);
7404 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7405 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7406 " calling generic handler...\n");
7407 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7410 /* receive done gets called when last copy is sent to a neighbor */
7416 * Handler for client's ACKs for payload traffic.
7418 * @param cls Closure (unused).
7419 * @param client Identification of the client.
7420 * @param message The actual message.
7423 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7424 const struct GNUNET_MessageHeader *message)
7426 struct GNUNET_MESH_LocalAck *msg;
7427 struct MeshTunnel *t;
7428 struct MeshClient *c;
7429 MESH_TunnelNumber tid;
7432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7433 /* Sanity check for client registration */
7434 if (NULL == (c = client_get (client)))
7437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7442 msg = (struct GNUNET_MESH_LocalAck *) message;
7444 /* Tunnel exists? */
7445 tid = ntohl (msg->tunnel_id);
7446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7447 t = tunnel_get_by_local_id (c, tid);
7451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7452 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7453 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7457 ack = ntohl (msg->max_pid);
7458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7460 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7461 if (NULL != t->owner && t->owner->handle == client)
7463 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7465 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7469 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7470 tunnel_set_client_fwd_ack (t, c, ack);
7471 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7474 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7481 * Functions to handle messages from clients
7483 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7484 {&handle_local_new_client, NULL,
7485 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7486 {&handle_local_announce_regex, NULL,
7487 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7488 {&handle_local_tunnel_create, NULL,
7489 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7490 sizeof (struct GNUNET_MESH_TunnelMessage)},
7491 {&handle_local_tunnel_destroy, NULL,
7492 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7493 sizeof (struct GNUNET_MESH_TunnelMessage)},
7494 {&handle_local_tunnel_speed, NULL,
7495 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7496 sizeof (struct GNUNET_MESH_TunnelMessage)},
7497 {&handle_local_tunnel_speed, NULL,
7498 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7499 sizeof (struct GNUNET_MESH_TunnelMessage)},
7500 {&handle_local_tunnel_buffer, NULL,
7501 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7502 sizeof (struct GNUNET_MESH_TunnelMessage)},
7503 {&handle_local_tunnel_buffer, NULL,
7504 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7505 sizeof (struct GNUNET_MESH_TunnelMessage)},
7506 {&handle_local_connect_add, NULL,
7507 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7508 sizeof (struct GNUNET_MESH_PeerControl)},
7509 {&handle_local_connect_del, NULL,
7510 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7511 sizeof (struct GNUNET_MESH_PeerControl)},
7512 {&handle_local_blacklist, NULL,
7513 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7514 sizeof (struct GNUNET_MESH_PeerControl)},
7515 {&handle_local_unblacklist, NULL,
7516 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7517 sizeof (struct GNUNET_MESH_PeerControl)},
7518 {&handle_local_connect_by_type, NULL,
7519 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7520 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7521 {&handle_local_connect_by_string, NULL,
7522 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7523 {&handle_local_unicast, NULL,
7524 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7525 {&handle_local_to_origin, NULL,
7526 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7527 {&handle_local_multicast, NULL,
7528 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7529 {&handle_local_ack, NULL,
7530 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7531 sizeof (struct GNUNET_MESH_LocalAck)},
7537 * To be called on core init/fail.
7539 * @param cls service closure
7540 * @param server handle to the server for this service
7541 * @param identity the public identity of this peer
7544 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7545 const struct GNUNET_PeerIdentity *identity)
7548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7549 core_handle = server;
7550 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7553 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7554 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7556 GNUNET_abort(); // Try harder
7563 * Method called whenever a given peer connects.
7565 * @param cls closure
7566 * @param peer peer identity this notification is about
7567 * @param atsi performance data for the connection
7568 * @param atsi_count number of records in 'atsi'
7571 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7572 const struct GNUNET_ATS_Information *atsi,
7573 unsigned int atsi_count)
7575 struct MeshPeerInfo *peer_info;
7576 struct MeshPeerPath *path;
7578 DEBUG_CONN ("Peer connected\n");
7579 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7580 peer_info = peer_info_get (peer);
7581 if (myid == peer_info->id)
7583 DEBUG_CONN (" (self)\n");
7588 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7590 path = path_new (2);
7591 path->peers[0] = myid;
7592 path->peers[1] = peer_info->id;
7593 GNUNET_PEER_change_rc (myid, 1);
7594 GNUNET_PEER_change_rc (peer_info->id, 1);
7595 peer_info_add_path (peer_info, path, GNUNET_YES);
7596 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7602 * Method called whenever a peer disconnects.
7604 * @param cls closure
7605 * @param peer peer identity this notification is about
7608 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7610 struct MeshPeerInfo *pi;
7611 struct MeshPeerQueue *q;
7612 struct MeshPeerQueue *n;
7614 DEBUG_CONN ("Peer disconnected\n");
7615 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7627 /* try to reroute this traffic instead */
7628 queue_destroy(q, GNUNET_YES);
7632 peer_info_remove_path (pi, pi->id, myid);
7635 DEBUG_CONN (" (self)\n");
7637 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7642 /******************************************************************************/
7643 /************************ MAIN FUNCTIONS ****************************/
7644 /******************************************************************************/
7647 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7649 * @param cls closure
7650 * @param key current key code
7651 * @param value value in the hash map
7652 * @return GNUNET_YES if we should continue to iterate,
7656 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7658 struct MeshTunnel *t = value;
7665 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7667 * @param cls closure
7668 * @param key current key code
7669 * @param value value in the hash map
7670 * @return GNUNET_YES if we should continue to iterate,
7674 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7676 struct MeshPeerInfo *p = value;
7677 struct MeshPeerQueue *q;
7678 struct MeshPeerQueue *n;
7686 queue_destroy(q, GNUNET_YES);
7690 peer_info_destroy (p);
7695 * Task run during shutdown.
7701 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7705 if (core_handle != NULL)
7707 GNUNET_CORE_disconnect (core_handle);
7710 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7711 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7712 if (dht_handle != NULL)
7714 GNUNET_DHT_disconnect (dht_handle);
7719 GNUNET_SERVER_notification_context_destroy (nc);
7722 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7724 GNUNET_SCHEDULER_cancel (announce_id_task);
7725 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7731 * Process mesh requests.
7733 * @param cls closure
7734 * @param server the initialized server
7735 * @param c configuration to use
7738 run (void *cls, struct GNUNET_SERVER_Handle *server,
7739 const struct GNUNET_CONFIGURATION_Handle *c)
7741 struct MeshPeerInfo *peer;
7742 struct MeshPeerPath *p;
7745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7746 server_handle = server;
7747 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7748 NULL, /* Closure passed to MESH functions */
7749 &core_init, /* Call core_init once connected */
7750 &core_connect, /* Handle connects */
7751 &core_disconnect, /* remove peers on disconnects */
7752 NULL, /* Don't notify about all incoming messages */
7753 GNUNET_NO, /* For header only in notification */
7754 NULL, /* Don't notify about all outbound messages */
7755 GNUNET_NO, /* For header-only out notification */
7756 core_handlers); /* Register these handlers */
7758 if (core_handle == NULL)
7761 GNUNET_SCHEDULER_shutdown ();
7766 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7769 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7771 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7773 GNUNET_SCHEDULER_shutdown ();
7778 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7779 &refresh_path_time))
7781 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7783 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7784 "refresh path time");
7785 GNUNET_SCHEDULER_shutdown ();
7790 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7791 &app_announce_time))
7793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7795 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7796 "app announce time");
7797 GNUNET_SCHEDULER_shutdown ();
7802 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7805 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7807 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7808 "id announce time");
7809 GNUNET_SCHEDULER_shutdown ();
7814 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7815 &unacknowledged_wait_time))
7817 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7819 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7820 "unacknowledged wait time");
7821 GNUNET_SCHEDULER_shutdown ();
7826 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7831 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7833 GNUNET_SCHEDULER_shutdown ();
7838 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7843 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7845 GNUNET_SCHEDULER_shutdown ();
7850 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7855 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7857 GNUNET_SCHEDULER_shutdown ();
7862 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7865 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7867 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7873 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7874 &dht_replication_level))
7876 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7878 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7879 "dht replication level", 10);
7880 dht_replication_level = 10;
7884 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7885 GNUNET_free (keyfile);
7886 if (my_private_key == NULL)
7888 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7889 _("Mesh service could not access hostkey. Exiting.\n"));
7890 GNUNET_SCHEDULER_shutdown ();
7893 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7894 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7895 &my_full_id.hashPubKey);
7896 myid = GNUNET_PEER_intern (&my_full_id);
7897 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7898 "Mesh for peer [%s] starting\n",
7899 GNUNET_i2s(&my_full_id));
7901 // transport_handle = GNUNET_TRANSPORT_connect(c,
7908 dht_handle = GNUNET_DHT_connect (c, 64);
7909 if (dht_handle == NULL)
7914 stats = GNUNET_STATISTICS_create ("mesh", c);
7918 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7920 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7921 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7922 peers = GNUNET_CONTAINER_multihashmap_create (32);
7923 applications = GNUNET_CONTAINER_multihashmap_create (32);
7924 types = GNUNET_CONTAINER_multihashmap_create (32);
7926 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7927 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7928 GNUNET_SERVER_disconnect_notify (server_handle,
7929 &handle_local_client_disconnect, NULL);
7933 clients_tail = NULL;
7936 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7937 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7939 /* Create a peer_info for the local peer */
7940 peer = peer_info_get (&my_full_id);
7943 GNUNET_PEER_change_rc (myid, 1);
7944 peer_info_add_path (peer, p, GNUNET_YES);
7946 /* Scheduled the task to clean up when shutdown is called */
7947 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7954 * The main function for the mesh service.
7956 * @param argc number of arguments from the command line
7957 * @param argv command line arguments
7958 * @return 0 ok, 1 on error
7961 main (int argc, char *const *argv)
7965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7968 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7974 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7975 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
7976 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);