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);
4393 if (GNUNET_YES == t->speed_min)
4394 opt = MESH_TUNNEL_OPT_SPEED_MIN;
4395 if (GNUNET_YES == t->nobuffer)
4396 opt |= MESH_TUNNEL_OPT_NOBUFFER;
4397 msg->opt = htonl(opt);
4399 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
4400 for (i = 0; i < p->length; i++)
4402 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
4408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4409 "CREATE PATH (%u bytes long) sent!\n", size_needed);
4415 * Fill the core buffer
4417 * @param cls closure (data itself)
4418 * @param size number of bytes available in buf
4419 * @param buf where the callee should write the message
4421 * @return number of bytes written to buf
4424 send_core_data_multicast (void *cls, size_t size, void *buf)
4426 struct MeshTransmissionDescriptor *info = cls;
4429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
4430 GNUNET_assert (NULL != info);
4431 GNUNET_assert (NULL != info->peer);
4432 total_size = info->mesh_data->data_len;
4433 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
4435 if (total_size > size)
4440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
4441 memcpy (buf, info->mesh_data->data, total_size);
4444 struct GNUNET_MESH_Multicast *mc;
4445 struct GNUNET_MessageHeader *mh;
4448 if (ntohs (mh->type) == GNUNET_MESSAGE_TYPE_MESH_MULTICAST)
4450 mc = (struct GNUNET_MESH_Multicast *) mh;
4451 mh = (struct GNUNET_MessageHeader *) &mc[1];
4452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4453 " multicast, payload type %s\n",
4454 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4456 " multicast, payload size %u\n", ntohs (mh->size));
4460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
4461 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
4465 data_descriptor_decrement_rc (info->mesh_data);
4466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
4468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
4474 * Creates a path ack message in buf and frees all unused resources.
4476 * @param cls closure (MeshTransmissionDescriptor)
4477 * @param size number of bytes available in buf
4478 * @param buf where the callee should write the message
4479 * @return number of bytes written to buf
4482 send_core_path_ack (void *cls, size_t size, void *buf)
4484 struct MeshTransmissionDescriptor *info = cls;
4485 struct GNUNET_MESH_PathACK *msg = buf;
4487 GNUNET_assert (NULL != info);
4488 if (sizeof (struct GNUNET_MESH_PathACK) > size)
4493 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4494 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4495 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4496 msg->tid = htonl (info->origin->tid);
4497 msg->peer_id = my_full_id;
4500 /* TODO add signature */
4502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4503 return sizeof (struct GNUNET_MESH_PathACK);
4508 * Free a transmission that was already queued with all resources
4509 * associated to the request.
4511 * @param queue Queue handler to cancel.
4512 * @param clear_cls Is it necessary to free associated cls?
4515 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4517 struct MeshTransmissionDescriptor *dd;
4518 struct MeshPathInfo *path_info;
4520 if (GNUNET_YES == clear_cls)
4522 switch (queue->type)
4524 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4525 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4526 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type payload\n");
4529 data_descriptor_decrement_rc (dd->mesh_data);
4531 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4533 path_info = queue->cls;
4534 path_destroy (path_info->path);
4538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4539 " type %u unknown!\n",
4542 GNUNET_free_non_null (queue->cls);
4544 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4545 queue->peer->queue_tail,
4547 GNUNET_free (queue);
4552 * @brief Get the next transmittable message from the queue.
4554 * This will be the head, except in the case of being a data packet
4555 * not allowed by the destination peer.
4557 * @param peer Destination peer.
4559 * @return The next viable MeshPeerQueue element to send to that peer.
4560 * NULL when there are no transmittable messages.
4562 struct MeshPeerQueue *
4563 queue_get_next (const struct MeshPeerInfo *peer)
4565 struct MeshPeerQueue *q;
4566 struct MeshTunnel *t;
4567 struct MeshTransmissionDescriptor *info;
4568 struct MeshTunnelChildInfo *cinfo;
4569 struct GNUNET_MESH_Unicast *ucast;
4570 struct GNUNET_MESH_ToOrigin *to_orig;
4571 struct GNUNET_MESH_Multicast *mcast;
4572 struct GNUNET_PeerIdentity id;
4576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4577 for (q = peer->queue_head; NULL != q; q = q->next)
4581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4583 GNUNET_MESH_DEBUG_M2S(q->type));
4586 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4587 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4588 pid = ntohl (ucast->pid);
4589 GNUNET_PEER_resolve (info->peer->id, &id);
4590 cinfo = tunnel_get_neighbor_fc(t, &id);
4591 ack = cinfo->fwd_ack;
4593 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4594 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4595 pid = ntohl (to_orig->pid);
4598 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4599 mcast = (struct GNUNET_MESH_Multicast *) info->mesh_data->data;
4600 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST != ntohs(mcast->header.type))
4602 // Not a multicast payload: multicast control traffic (destroy, etc)
4605 pid = ntohl (mcast->pid);
4606 GNUNET_PEER_resolve (info->peer->id, &id);
4607 cinfo = tunnel_get_neighbor_fc(t, &id);
4608 ack = cinfo->fwd_ack;
4611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4616 "********* ACK: %u, PID: %u\n",
4618 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4627 "********* NEXT!\n");
4630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4631 "********* nothing found\n");
4637 * Core callback to write a queued packet to core buffer
4639 * @param cls Closure (peer info).
4640 * @param size Number of bytes available in buf.
4641 * @param buf Where the to write the message.
4643 * @return number of bytes written to buf
4646 queue_send (void *cls, size_t size, void *buf)
4648 struct MeshPeerInfo *peer = cls;
4649 struct GNUNET_MessageHeader *msg;
4650 struct MeshPeerQueue *queue;
4651 struct MeshTunnel *t;
4652 struct MeshTunnelChildInfo *cinfo;
4653 struct GNUNET_PeerIdentity dst_id;
4656 peer->core_transmit = NULL;
4658 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4659 queue = queue_get_next(peer);
4661 /* Queue has no internal mesh traffic not sendable payload */
4664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4665 if (NULL == peer->queue_head)
4666 GNUNET_break(0); // Should've been canceled
4669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4671 GNUNET_PEER_resolve (peer->id, &dst_id);
4672 /* Check if buffer size is enough for the message */
4673 if (queue->size > size)
4677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4678 "********* not enough room, reissue\n");
4679 peer->core_transmit =
4680 GNUNET_CORE_notify_transmit_ready(core_handle,
4683 GNUNET_TIME_UNIT_FOREVER_REL,
4690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4693 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4697 "********* unicast: t->q (%u/%u)\n",
4698 t->fwd_queue_n, t->fwd_queue_max);
4700 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4707 switch (queue->type)
4710 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4711 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4712 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4714 "********* raw: %s\n",
4715 GNUNET_MESH_DEBUG_M2S (queue->type));
4717 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4718 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4719 data_size = send_core_data_raw (queue->cls, size, buf);
4720 msg = (struct GNUNET_MessageHeader *) buf;
4721 switch (ntohs (msg->type)) // Type of preconstructed message
4723 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4724 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4726 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4727 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4733 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* multicast\n");
4736 struct MeshTransmissionDescriptor *info = queue->cls;
4737 if (*(info->mesh_data->reference_counter) == 1)
4739 // FIXME fc (t->fwd_queue_n--)
4741 data_size = send_core_data_multicast(queue->cls, size, buf);
4742 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
4744 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4745 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4746 data_size = send_core_path_create(queue->cls, size, buf);
4748 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4750 data_size = send_core_path_ack(queue->cls, size, buf);
4754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4755 "********* type unknown: %u\n",
4759 switch (queue->type)
4761 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4762 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4763 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4764 cinfo = tunnel_get_neighbor_fc(t, &dst_id);
4765 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4768 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4769 "at pos %u (%p) != %p\n",
4770 cinfo->send_buffer_start,
4771 cinfo->send_buffer[cinfo->send_buffer_start],
4774 GNUNET_break(cinfo->send_buffer_n > 0);
4775 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4776 cinfo->send_buffer_n--;
4777 cinfo->send_buffer_start++;
4778 cinfo->send_buffer_start %= t->fwd_queue_max;
4784 /* Free queue, but cls was freed by send_core_* */
4785 queue_destroy (queue, GNUNET_NO);
4787 if (GNUNET_YES == t->destroy && 0 == t->fwd_queue_n)
4789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4793 /* If more data in queue, send next */
4794 queue = queue_get_next(peer);
4797 struct GNUNET_PeerIdentity id;
4799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4800 GNUNET_PEER_resolve (peer->id, &id);
4801 peer->core_transmit =
4802 GNUNET_CORE_notify_transmit_ready(core_handle,
4805 GNUNET_TIME_UNIT_FOREVER_REL,
4813 if (NULL != peer->queue_head)
4814 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4815 "********* %s stalled\n",
4816 GNUNET_i2s(&my_full_id));
4818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4824 * Queue and pass message to core when possible.
4826 * @param cls Closure (type dependant).
4827 * @param type Type of the message, 0 for a raw message.
4828 * @param size Size of the message.
4829 * @param dst Neighbor to send message to.
4830 * @param t Tunnel this message belongs to.
4833 queue_add (void *cls, uint16_t type, size_t size,
4834 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4836 struct MeshPeerQueue *queue;
4837 struct MeshTunnelChildInfo *cinfo;
4838 struct GNUNET_PeerIdentity id;
4844 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4845 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4847 n = &t->fwd_queue_n;
4848 max = &t->fwd_queue_max;
4850 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4852 n = &t->bck_queue_n;
4853 max = &t->bck_queue_max;
4858 if (NULL == t->owner)
4859 GNUNET_break_op(0); // TODO: kill connection?
4862 GNUNET_STATISTICS_update(stats, "# messages dropped (buffer full)",
4864 return; // Drop message
4868 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4874 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4875 GNUNET_PEER_resolve (dst->id, &id);
4876 if (NULL == dst->core_transmit)
4878 dst->core_transmit =
4879 GNUNET_CORE_notify_transmit_ready(core_handle,
4882 GNUNET_TIME_UNIT_FOREVER_REL,
4888 if (NULL == n) // Is this internal mesh traffic?
4891 // It's payload, keep track of buffer per peer.
4892 cinfo = tunnel_get_neighbor_fc(t, &id);
4893 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4894 if (NULL != cinfo->send_buffer[i])
4896 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4897 queue_destroy(cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4898 cinfo->send_buffer_start++;
4899 cinfo->send_buffer_start %= t->fwd_queue_max;
4900 cinfo->send_buffer_n--;
4902 cinfo->send_buffer[i] = queue;
4903 cinfo->send_buffer_n++;
4904 if (cinfo->send_buffer_n > t->fwd_queue_max)
4907 cinfo->send_buffer_n = t->fwd_queue_max;
4912 /******************************************************************************/
4913 /******************** MESH NETWORK HANDLERS **************************/
4914 /******************************************************************************/
4918 * Core handler for path creation
4920 * @param cls closure
4921 * @param message message
4922 * @param peer peer identity this notification is about
4923 * @param atsi performance data
4924 * @param atsi_count number of records in 'atsi'
4926 * @return GNUNET_OK to keep the connection open,
4927 * GNUNET_SYSERR to close it (signal serious error)
4930 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4931 const struct GNUNET_MessageHeader *message,
4932 const struct GNUNET_ATS_Information *atsi,
4933 unsigned int atsi_count)
4935 unsigned int own_pos;
4938 MESH_TunnelNumber tid;
4939 struct GNUNET_MESH_ManipulatePath *msg;
4940 struct GNUNET_PeerIdentity *pi;
4941 struct GNUNET_HashCode hash;
4942 struct MeshPeerPath *path;
4943 struct MeshPeerInfo *dest_peer_info;
4944 struct MeshPeerInfo *orig_peer_info;
4945 struct MeshTunnel *t;
4947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4948 "Received a path create msg [%s]\n",
4949 GNUNET_i2s (&my_full_id));
4950 size = ntohs (message->size);
4951 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4953 GNUNET_break_op (0);
4957 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4958 if (size % sizeof (struct GNUNET_PeerIdentity))
4960 GNUNET_break_op (0);
4963 size /= sizeof (struct GNUNET_PeerIdentity);
4966 GNUNET_break_op (0);
4969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4970 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4972 tid = ntohl (msg->tid);
4973 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4975 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4976 t = tunnel_get (pi, tid);
4977 if (NULL == t) // FIXME only for INCOMING tunnels?
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4982 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4985 // FIXME notify failure
4988 opt = ntohl (msg->opt);
4989 t->speed_min = (0 != (opt & MESH_TUNNEL_OPT_SPEED_MIN)) ?
4990 GNUNET_YES : GNUNET_NO;
4991 t->nobuffer = (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER)) ?
4992 GNUNET_YES : GNUNET_NO;
4994 if (GNUNET_YES == t->nobuffer)
4996 t->bck_queue_max = 1;
4997 t->fwd_queue_max = 1;
5000 // FIXME only assign a local tid if a local client is interested (on demand)
5001 while (NULL != tunnel_get_incoming (next_local_tid))
5002 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5003 t->local_tid_dest = next_local_tid++;
5004 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
5007 tunnel_reset_timeout (t);
5008 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
5010 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
5011 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
5019 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
5020 if (NULL == dest_peer_info)
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5023 " Creating PeerInfo for destination.\n");
5024 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5025 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
5026 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
5028 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5030 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
5031 if (NULL == orig_peer_info)
5033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5034 " Creating PeerInfo for origin.\n");
5035 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
5036 orig_peer_info->id = GNUNET_PEER_intern (pi);
5037 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
5038 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
5040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5041 path = path_new (size);
5043 for (i = 0; i < size; i++)
5045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5046 GNUNET_i2s (&pi[i]));
5047 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5048 if (path->peers[i] == myid)
5051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5054 /* cannot be self, must be 'not found' */
5055 /* create path: self not found in path through self */
5056 GNUNET_break_op (0);
5057 path_destroy (path);
5061 path_add_to_peers (path, GNUNET_NO);
5062 tunnel_add_path (t, path, own_pos);
5063 if (own_pos == size - 1)
5065 /* It is for us! Send ack. */
5066 struct MeshTransmissionDescriptor *info;
5068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5069 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
5070 if (NULL == t->peers)
5072 /* New tunnel! Notify clients on data. */
5073 t->peers = GNUNET_CONTAINER_multihashmap_create (4);
5075 GNUNET_break (GNUNET_SYSERR !=
5076 GNUNET_CONTAINER_multihashmap_put (t->peers,
5077 &my_full_id.hashPubKey,
5080 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE));
5081 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
5082 info->origin = &t->id;
5083 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
5084 GNUNET_assert (NULL != info->peer);
5086 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5087 sizeof (struct GNUNET_MESH_PathACK),
5093 struct MeshPeerPath *path2;
5095 /* It's for somebody else! Retransmit. */
5096 path2 = path_duplicate (path);
5097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
5098 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
5099 path2 = path_duplicate (path);
5100 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
5101 send_create_path (dest_peer_info, path, t);
5108 * Core handler for path destruction
5110 * @param cls closure
5111 * @param message message
5112 * @param peer peer identity this notification is about
5113 * @param atsi performance data
5114 * @param atsi_count number of records in 'atsi'
5116 * @return GNUNET_OK to keep the connection open,
5117 * GNUNET_SYSERR to close it (signal serious error)
5120 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5121 const struct GNUNET_MessageHeader *message,
5122 const struct GNUNET_ATS_Information *atsi,
5123 unsigned int atsi_count)
5125 struct GNUNET_MESH_ManipulatePath *msg;
5126 struct GNUNET_PeerIdentity *pi;
5127 struct MeshPeerPath *path;
5128 struct MeshTunnel *t;
5129 unsigned int own_pos;
5133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5134 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
5135 size = ntohs (message->size);
5136 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
5138 GNUNET_break_op (0);
5142 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
5143 if (size % sizeof (struct GNUNET_PeerIdentity))
5145 GNUNET_break_op (0);
5148 size /= sizeof (struct GNUNET_PeerIdentity);
5151 GNUNET_break_op (0);
5154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
5156 msg = (struct GNUNET_MESH_ManipulatePath *) message;
5157 pi = (struct GNUNET_PeerIdentity *) &msg[1];
5158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5159 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
5161 t = tunnel_get (pi, ntohl (msg->tid));
5164 /* TODO notify back: we don't know this tunnel */
5165 GNUNET_break_op (0);
5168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
5169 path = path_new (size);
5171 for (i = 0; i < size; i++)
5173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
5174 GNUNET_i2s (&pi[i]));
5175 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
5176 if (path->peers[i] == myid)
5179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
5180 if (own_pos < path->length - 1)
5181 send_message (message, &pi[own_pos + 1], t);
5183 send_client_tunnel_disconnect(t, NULL);
5185 tunnel_delete_peer (t, path->peers[path->length - 1]);
5186 path_destroy (path);
5192 * Core handler for notifications of broken paths
5194 * @param cls closure
5195 * @param message message
5196 * @param peer peer identity this notification is about
5197 * @param atsi performance data
5198 * @param atsi_count number of records in 'atsi'
5200 * @return GNUNET_OK to keep the connection open,
5201 * GNUNET_SYSERR to close it (signal serious error)
5204 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
5205 const struct GNUNET_MessageHeader *message,
5206 const struct GNUNET_ATS_Information *atsi,
5207 unsigned int atsi_count)
5209 struct GNUNET_MESH_PathBroken *msg;
5210 struct MeshTunnel *t;
5212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5213 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
5214 msg = (struct GNUNET_MESH_PathBroken *) message;
5215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5216 GNUNET_i2s (&msg->peer1));
5217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
5218 GNUNET_i2s (&msg->peer2));
5219 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5222 GNUNET_break_op (0);
5225 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
5226 GNUNET_PEER_search (&msg->peer2));
5233 * Core handler for tunnel destruction
5235 * @param cls closure
5236 * @param message message
5237 * @param peer peer identity this notification is about
5238 * @param atsi performance data
5239 * @param atsi_count number of records in 'atsi'
5241 * @return GNUNET_OK to keep the connection open,
5242 * GNUNET_SYSERR to close it (signal serious error)
5245 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
5246 const struct GNUNET_MessageHeader *message,
5247 const struct GNUNET_ATS_Information *atsi,
5248 unsigned int atsi_count)
5250 struct GNUNET_MESH_TunnelDestroy *msg;
5251 struct MeshTunnel *t;
5253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5254 "Got a TUNNEL DESTROY packet from %s\n", GNUNET_i2s (peer));
5255 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
5256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for tunnel %s [%u]\n",
5257 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
5258 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5261 /* Probably already got the message from another path,
5262 * destroyed the tunnel and retransmitted to children.
5265 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5268 if (t->id.oid == myid)
5270 GNUNET_break_op (0);
5273 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5275 /* Tunnel was incoming, notify clients */
5276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
5277 t->local_tid, t->local_tid_dest);
5278 send_clients_tunnel_destroy (t);
5280 tunnel_send_destroy (t);
5281 t->destroy = GNUNET_YES;
5282 // TODO: add timeout to destroy the tunnel anyway
5288 * Core handler for mesh network traffic going from the origin to a peer
5290 * @param cls closure
5291 * @param peer peer identity this notification is about
5292 * @param message message
5293 * @param atsi performance data
5294 * @param atsi_count number of records in 'atsi'
5295 * @return GNUNET_OK to keep the connection open,
5296 * GNUNET_SYSERR to close it (signal serious error)
5299 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5300 const struct GNUNET_MessageHeader *message,
5301 const struct GNUNET_ATS_Information *atsi,
5302 unsigned int atsi_count)
5304 struct GNUNET_MESH_Unicast *msg;
5305 struct GNUNET_PeerIdentity *neighbor;
5306 struct MeshTunnelChildInfo *cinfo;
5307 struct MeshTunnel *t;
5308 GNUNET_PEER_Id dest_id;
5313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
5316 size = ntohs (message->size);
5318 sizeof (struct GNUNET_MESH_Unicast) +
5319 sizeof (struct GNUNET_MessageHeader))
5324 msg = (struct GNUNET_MESH_Unicast *) message;
5325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5326 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5328 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5331 /* TODO notify back: we don't know this tunnel */
5332 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5333 GNUNET_break_op (0);
5336 pid = ntohl (msg->pid);
5337 if (t->fwd_pid == pid)
5339 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5340 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5341 " Already seen pid %u, DROPPING!\n", pid);
5346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5347 " pid %u not seen yet, forwarding\n", pid);
5350 t->skip += (pid - t->fwd_pid) - 1;
5353 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
5355 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5356 GNUNET_break_op (0);
5357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5358 "Received PID %u, ACK %u\n",
5359 pid, t->last_fwd_ack);
5363 tunnel_reset_timeout (t);
5364 dest_id = GNUNET_PEER_search (&msg->destination);
5365 if (dest_id == myid)
5367 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5368 " it's for us! sending to clients...\n");
5369 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
5370 send_subscribed_clients (message, &msg[1].header, t);
5371 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
5374 ttl = ntohl (msg->ttl);
5375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
5378 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5379 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5380 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5384 " not for us, retransmitting...\n");
5386 neighbor = tree_get_first_hop (t->tree, dest_id);
5387 cinfo = tunnel_get_neighbor_fc (t, neighbor);
5389 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
5392 if (GNUNET_YES == t->nobuffer &&
5393 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
5395 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
5396 GNUNET_break_op (0);
5399 send_message (message, neighbor, t);
5400 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
5406 * Core handler for mesh network traffic going from the origin to all peers
5408 * @param cls closure
5409 * @param message message
5410 * @param peer peer identity this notification is about
5411 * @param atsi performance data
5412 * @param atsi_count number of records in 'atsi'
5413 * @return GNUNET_OK to keep the connection open,
5414 * GNUNET_SYSERR to close it (signal serious error)
5416 * TODO: Check who we got this from, to validate route.
5419 handle_mesh_data_multicast (void *cls, const struct GNUNET_PeerIdentity *peer,
5420 const struct GNUNET_MessageHeader *message,
5421 const struct GNUNET_ATS_Information *atsi,
5422 unsigned int atsi_count)
5424 struct GNUNET_MESH_Multicast *msg;
5425 struct MeshTunnel *t;
5429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a multicast packet from %s\n",
5431 size = ntohs (message->size);
5432 if (sizeof (struct GNUNET_MESH_Multicast) +
5433 sizeof (struct GNUNET_MessageHeader) > size)
5435 GNUNET_break_op (0);
5438 msg = (struct GNUNET_MESH_Multicast *) message;
5439 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5443 /* TODO notify that we dont know that tunnel */
5444 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5445 GNUNET_break_op (0);
5448 pid = ntohl (msg->pid);
5449 if (t->fwd_pid == pid)
5451 /* already seen this packet, drop */
5452 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
5453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5454 " Already seen pid %u, DROPPING!\n", pid);
5455 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5461 " pid %u not seen yet, forwarding\n", pid);
5463 t->skip += (pid - t->fwd_pid) - 1;
5465 tunnel_reset_timeout (t);
5467 /* Transmit to locally interested clients */
5468 if (NULL != t->peers &&
5469 GNUNET_CONTAINER_multihashmap_contains (t->peers, &my_full_id.hashPubKey))
5471 GNUNET_STATISTICS_update (stats, "# multicast received", 1, GNUNET_NO);
5472 send_subscribed_clients (message, &msg[1].header, t);
5473 tunnel_send_fwd_ack(t, GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ntohl (msg->ttl));
5476 if (ntohl (msg->ttl) == 0)
5478 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
5479 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
5480 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5483 GNUNET_STATISTICS_update (stats, "# multicast forwarded", 1, GNUNET_NO);
5484 tunnel_send_multicast (t, message, GNUNET_NO);
5490 * Core handler for mesh network traffic toward the owner of a tunnel
5492 * @param cls closure
5493 * @param message message
5494 * @param peer peer identity this notification is about
5495 * @param atsi performance data
5496 * @param atsi_count number of records in 'atsi'
5498 * @return GNUNET_OK to keep the connection open,
5499 * GNUNET_SYSERR to close it (signal serious error)
5502 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
5503 const struct GNUNET_MessageHeader *message,
5504 const struct GNUNET_ATS_Information *atsi,
5505 unsigned int atsi_count)
5507 struct GNUNET_MESH_ToOrigin *msg;
5508 struct GNUNET_PeerIdentity id;
5509 struct MeshPeerInfo *peer_info;
5510 struct MeshTunnel *t;
5514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
5516 size = ntohs (message->size);
5517 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
5518 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
5520 GNUNET_break_op (0);
5523 msg = (struct GNUNET_MESH_ToOrigin *) message;
5524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
5525 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
5526 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5530 /* TODO notify that we dont know this tunnel (whom)? */
5531 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
5532 GNUNET_break_op (0);
5536 if (NULL != t->owner)
5539 struct GNUNET_MESH_ToOrigin *copy;
5541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5542 " it's for us! sending to clients...\n");
5543 /* TODO signature verification */
5544 memcpy (cbuf, message, size);
5545 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5546 copy->tid = htonl (t->local_tid);
5547 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5548 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5549 ©->header, GNUNET_NO);
5550 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5554 " not for us, retransmitting...\n");
5556 peer_info = peer_info_get (&msg->oid);
5557 if (NULL == peer_info)
5559 /* unknown origin of tunnel */
5563 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5564 send_message (message, &id, t);
5565 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5572 * Core handler for mesh network traffic point-to-point acks.
5574 * @param cls closure
5575 * @param message message
5576 * @param peer peer identity this notification is about
5577 * @param atsi performance data
5578 * @param atsi_count number of records in 'atsi'
5580 * @return GNUNET_OK to keep the connection open,
5581 * GNUNET_SYSERR to close it (signal serious error)
5584 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5585 const struct GNUNET_MessageHeader *message,
5586 const struct GNUNET_ATS_Information *atsi,
5587 unsigned int atsi_count)
5589 struct GNUNET_MESH_ACK *msg;
5590 struct MeshTunnel *t;
5593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5595 msg = (struct GNUNET_MESH_ACK *) message;
5597 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5601 /* TODO notify that we dont know this tunnel (whom)? */
5602 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5603 GNUNET_break_op (0);
5606 ack = ntohl (msg->pid);
5608 /* Is this a forward or backward ACK? */
5609 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5611 struct MeshTunnelChildInfo *cinfo;
5614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5615 cinfo = tunnel_get_neighbor_fc (t, peer);
5616 cinfo->fwd_ack = ack;
5617 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5618 tunnel_unlock_fwd_queues (t);
5622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5624 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5625 tunnel_unlock_bck_queue (t);
5632 * Core handler for path ACKs
5634 * @param cls closure
5635 * @param message message
5636 * @param peer peer identity this notification is about
5637 * @param atsi performance data
5638 * @param atsi_count number of records in 'atsi'
5640 * @return GNUNET_OK to keep the connection open,
5641 * GNUNET_SYSERR to close it (signal serious error)
5644 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5645 const struct GNUNET_MessageHeader *message,
5646 const struct GNUNET_ATS_Information *atsi,
5647 unsigned int atsi_count)
5649 struct GNUNET_MESH_PathACK *msg;
5650 struct GNUNET_PeerIdentity id;
5651 struct MeshPeerInfo *peer_info;
5652 struct MeshPeerPath *p;
5653 struct MeshTunnel *t;
5655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5656 GNUNET_i2s (&my_full_id));
5657 msg = (struct GNUNET_MESH_PathACK *) message;
5658 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5661 /* TODO notify that we don't know the tunnel */
5662 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5664 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5667 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5668 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5670 peer_info = peer_info_get (&msg->peer_id);
5671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5672 GNUNET_i2s (&msg->peer_id));
5673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5676 if (NULL != t->regex_ctx && t->regex_ctx->info->peer == peer_info->id)
5678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5679 "connect_by_string completed, stopping search\n");
5680 regex_cancel_search (t->regex_ctx);
5681 t->regex_ctx = NULL;
5684 /* Add paths to peers? */
5685 p = tree_get_path_to_peer (t->tree, peer_info->id);
5688 path_add_to_peers (p, GNUNET_YES);
5696 /* Message for us? */
5697 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5700 if (NULL == t->owner)
5702 GNUNET_break_op (0);
5705 if (NULL != t->dht_get_type)
5707 GNUNET_DHT_get_stop (t->dht_get_type);
5708 t->dht_get_type = NULL;
5710 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5712 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5713 send_client_peer_connected (t, peer_info->id);
5718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5719 " not for us, retransmitting...\n");
5720 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5721 peer_info = peer_info_get (&msg->oid);
5722 if (NULL == peer_info)
5724 /* If we know the tunnel, we should DEFINITELY know the peer */
5728 send_message (message, &id, t);
5734 * Functions to handle messages from core
5736 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5737 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5738 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5739 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5740 sizeof (struct GNUNET_MESH_PathBroken)},
5741 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5742 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5743 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5744 {&handle_mesh_data_multicast, GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
5745 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5746 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5747 sizeof (struct GNUNET_MESH_ACK)},
5748 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5749 sizeof (struct GNUNET_MESH_PathACK)},
5755 /******************************************************************************/
5756 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5757 /******************************************************************************/
5760 * deregister_app: iterator for removing each application registered by a client
5762 * @param cls closure
5763 * @param key the hash of the application id (used to access the hashmap)
5764 * @param value the value stored at the key (client)
5766 * @return GNUNET_OK on success
5769 deregister_app (void *cls, const struct GNUNET_HashCode * key, void *value)
5771 struct GNUNET_CONTAINER_MultiHashMap *h = cls;
5772 GNUNET_break (GNUNET_YES ==
5773 GNUNET_CONTAINER_multihashmap_remove (h, key, value));
5779 * notify_client_connection_failure: notify a client that the connection to the
5780 * requested remote peer is not possible (for instance, no route found)
5781 * Function called when the socket is ready to queue more data. "buf" will be
5782 * NULL and "size" zero if the socket was closed for writing in the meantime.
5784 * @param cls closure
5785 * @param size number of bytes available in buf
5786 * @param buf where the callee should write the message
5787 * @return number of bytes written to buf
5790 notify_client_connection_failure (void *cls, size_t size, void *buf)
5793 struct MeshPeerInfo *peer_info;
5794 struct GNUNET_MESH_PeerControl *msg;
5795 struct GNUNET_PeerIdentity id;
5797 if (0 == size && NULL == buf)
5799 // TODO retry? cancel?
5803 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5804 peer_info = (struct MeshPeerInfo *) cls;
5805 msg = (struct GNUNET_MESH_PeerControl *) buf;
5806 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5807 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5808 // msg->tunnel_id = htonl(peer_info->t->tid);
5809 GNUNET_PEER_resolve (peer_info->id, &id);
5810 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5818 * Send keepalive packets for a peer
5820 * @param cls Closure (tunnel for which to send the keepalive).
5821 * @param tc Notification context.
5823 * TODO: implement explicit multicast keepalive?
5826 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5828 struct MeshTunnel *t = cls;
5829 struct GNUNET_MessageHeader *payload;
5830 struct GNUNET_MESH_Multicast *msg;
5832 sizeof (struct GNUNET_MESH_Multicast) +
5833 sizeof (struct GNUNET_MessageHeader);
5836 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5840 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5843 "sending keepalive for tunnel %d\n", t->id.tid);
5845 msg = (struct GNUNET_MESH_Multicast *) cbuf;
5846 msg->header.size = htons (size);
5847 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_MULTICAST);
5848 // FIXME: change type to != MULTICAST
5849 msg->oid = my_full_id;
5850 msg->tid = htonl (t->id.tid);
5851 msg->ttl = htonl (default_ttl);
5852 msg->pid = htonl (t->fwd_pid + 1);
5854 payload = (struct GNUNET_MessageHeader *) &msg[1];
5855 payload->size = htons (sizeof (struct GNUNET_MessageHeader));
5856 payload->type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5857 tunnel_send_multicast (t, &msg->header, GNUNET_YES);
5859 t->path_refresh_task =
5860 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5866 * Function to process paths received for a new peer addition. The recorded
5867 * paths form the initial tunnel, which can be optimized later.
5868 * Called on each result obtained for the DHT search.
5870 * @param cls closure
5871 * @param exp when will this value expire
5872 * @param key key of the result
5873 * @param get_path path of the get request
5874 * @param get_path_length lenght of get_path
5875 * @param put_path path of the put request
5876 * @param put_path_length length of the put_path
5877 * @param type type of the result
5878 * @param size number of bytes in data
5879 * @param data pointer to the result data
5881 * TODO: re-issue the request after certain time? cancel after X results?
5884 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5885 const struct GNUNET_HashCode * key,
5886 const struct GNUNET_PeerIdentity *get_path,
5887 unsigned int get_path_length,
5888 const struct GNUNET_PeerIdentity *put_path,
5889 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5890 size_t size, const void *data)
5892 struct MeshPathInfo *path_info = cls;
5893 struct MeshPeerPath *p;
5894 struct GNUNET_PeerIdentity pi;
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5898 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5901 p = path_build_from_dht (get_path, get_path_length, put_path,
5903 path_add_to_peers (p, GNUNET_NO);
5905 for (i = 0; i < path_info->peer->ntunnels; i++)
5907 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5908 peer_info_connect (path_info->peer, path_info->t);
5916 * Function to process paths received for a new peer addition. The recorded
5917 * paths form the initial tunnel, which can be optimized later.
5918 * Called on each result obtained for the DHT search.
5920 * @param cls closure
5921 * @param exp when will this value expire
5922 * @param key key of the result
5923 * @param get_path path of the get request
5924 * @param get_path_length lenght of get_path
5925 * @param put_path path of the put request
5926 * @param put_path_length length of the put_path
5927 * @param type type of the result
5928 * @param size number of bytes in data
5929 * @param data pointer to the result data
5932 dht_get_type_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5933 const struct GNUNET_HashCode * key,
5934 const struct GNUNET_PeerIdentity *get_path,
5935 unsigned int get_path_length,
5936 const struct GNUNET_PeerIdentity *put_path,
5937 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5938 size_t size, const void *data)
5940 const struct PBlock *pb = data;
5941 const struct GNUNET_PeerIdentity *pi = &pb->id;
5942 struct MeshTunnel *t = cls;
5943 struct MeshPeerInfo *peer_info;
5944 struct MeshPeerPath *p;
5946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got type DHT result!\n");
5947 if (size != sizeof (struct PBlock))
5949 GNUNET_break_op (0);
5952 if (ntohl(pb->type) != t->type)
5954 GNUNET_break_op (0);
5957 GNUNET_assert (NULL != t->owner);
5958 peer_info = peer_info_get (pi);
5959 (void) GNUNET_CONTAINER_multihashmap_put (t->peers, &pi->hashPubKey,
5961 GNUNET_CONTAINER_MULTIHASHMAPOPTION_REPLACE);
5963 p = path_build_from_dht (get_path, get_path_length, put_path,
5965 path_add_to_peers (p, GNUNET_NO);
5967 tunnel_add_peer (t, peer_info);
5968 peer_info_connect (peer_info, t);
5973 * Function to process DHT string to regex matching.
5974 * Called on each result obtained for the DHT search.
5976 * @param cls closure (search context)
5977 * @param exp when will this value expire
5978 * @param key key of the result
5979 * @param get_path path of the get request (not used)
5980 * @param get_path_length lenght of get_path (not used)
5981 * @param put_path path of the put request (not used)
5982 * @param put_path_length length of the put_path (not used)
5983 * @param type type of the result
5984 * @param size number of bytes in data
5985 * @param data pointer to the result data
5988 dht_get_string_accept_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5989 const struct GNUNET_HashCode * key,
5990 const struct GNUNET_PeerIdentity *get_path,
5991 unsigned int get_path_length,
5992 const struct GNUNET_PeerIdentity *put_path,
5993 unsigned int put_path_length,
5994 enum GNUNET_BLOCK_Type type,
5995 size_t size, const void *data)
5997 const struct MeshRegexAccept *block = data;
5998 struct MeshRegexSearchContext *ctx = cls;
5999 struct MeshRegexSearchInfo *info = ctx->info;
6000 struct MeshPeerPath *p;
6001 struct MeshPeerInfo *peer_info;
6003 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got regex results from DHT!\n");
6004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", info->description);
6006 peer_info = peer_info_get(&block->id);
6007 p = path_build_from_dht (get_path, get_path_length, put_path,
6009 path_add_to_peers (p, GNUNET_NO);
6012 tunnel_add_peer (info->t, peer_info);
6013 peer_info_connect (peer_info, info->t);
6014 if (0 == info->peer)
6016 info->peer = peer_info->id;
6020 GNUNET_array_append (info->peers, info->n_peers, peer_info->id);
6023 info->timeout = GNUNET_SCHEDULER_add_delayed (connect_timeout,
6024 ®ex_connect_timeout,
6032 * Function to process DHT string to regex matching.
6033 * Called on each result obtained for the DHT search.
6035 * @param cls closure (search context)
6036 * @param exp when will this value expire
6037 * @param key key of the result
6038 * @param get_path path of the get request (not used)
6039 * @param get_path_length lenght of get_path (not used)
6040 * @param put_path path of the put request (not used)
6041 * @param put_path_length length of the put_path (not used)
6042 * @param type type of the result
6043 * @param size number of bytes in data
6044 * @param data pointer to the result data
6046 * TODO: re-issue the request after certain time? cancel after X results?
6049 dht_get_string_handler (void *cls, struct GNUNET_TIME_Absolute exp,
6050 const struct GNUNET_HashCode * key,
6051 const struct GNUNET_PeerIdentity *get_path,
6052 unsigned int get_path_length,
6053 const struct GNUNET_PeerIdentity *put_path,
6054 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
6055 size_t size, const void *data)
6057 const struct MeshRegexBlock *block = data;
6058 struct MeshRegexSearchContext *ctx = cls;
6059 struct MeshRegexSearchInfo *info = ctx->info;
6063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6064 "DHT GET STRING RETURNED RESULTS\n");
6065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6066 " key: %s\n", GNUNET_h2s (key));
6068 copy = GNUNET_malloc (size);
6069 memcpy (copy, data, size);
6070 GNUNET_break (GNUNET_OK ==
6071 GNUNET_CONTAINER_multihashmap_put(info->dht_get_results, key, copy,
6072 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
6073 len = ntohl (block->n_proof);
6075 char proof[len + 1];
6077 memcpy (proof, &block[1], len);
6079 if (GNUNET_OK != GNUNET_REGEX_check_proof (proof, key))
6081 GNUNET_break_op (0);
6085 len = strlen (info->description);
6086 if (len == ctx->position) // String processed
6088 if (GNUNET_YES == ntohl (block->accepting))
6090 regex_find_path(key, ctx);
6094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " block not accepting!\n");
6095 // FIXME REGEX this block not successful, wait for more? start timeout?
6099 GNUNET_break (GNUNET_OK ==
6100 GNUNET_MESH_regex_block_iterate (block, size,
6101 ®ex_edge_iterator, ctx));
6105 /******************************************************************************/
6106 /********************* MESH LOCAL HANDLES **************************/
6107 /******************************************************************************/
6111 * Handler for client disconnection
6113 * @param cls closure
6114 * @param client identification of the client; NULL
6115 * for the last call when the server is destroyed
6118 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
6120 struct MeshClient *c;
6121 struct MeshClient *next;
6124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
6127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
6133 if (c->handle != client)
6135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
6139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
6141 GNUNET_SERVER_client_drop (c->handle);
6142 c->shutting_down = GNUNET_YES;
6143 GNUNET_assert (NULL != c->own_tunnels);
6144 GNUNET_assert (NULL != c->incoming_tunnels);
6145 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
6146 &tunnel_destroy_iterator, c);
6147 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
6148 &tunnel_destroy_iterator, c);
6149 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
6150 &tunnel_destroy_iterator, c);
6151 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
6152 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
6153 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
6155 /* deregister clients applications */
6156 if (NULL != c->apps)
6158 GNUNET_CONTAINER_multihashmap_iterate (c->apps, &deregister_app, c->apps);
6159 GNUNET_CONTAINER_multihashmap_destroy (c->apps);
6161 if (0 == GNUNET_CONTAINER_multihashmap_size (applications) &&
6162 GNUNET_SCHEDULER_NO_TASK != announce_applications_task)
6164 GNUNET_SCHEDULER_cancel (announce_applications_task);
6165 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
6167 if (NULL != c->types)
6168 GNUNET_CONTAINER_multihashmap_destroy (c->types);
6169 for (i = 0; i < c->n_regex; i++)
6171 GNUNET_free (c->regexes[i]);
6173 GNUNET_free_non_null (c->regexes);
6174 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
6175 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
6177 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
6178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
6180 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
6183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
6189 * Handler for new clients
6191 * @param cls closure
6192 * @param client identification of the client
6193 * @param message the actual message, which includes messages the client wants
6196 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
6197 const struct GNUNET_MessageHeader *message)
6199 struct GNUNET_MESH_ClientConnect *cc_msg;
6200 struct MeshClient *c;
6201 GNUNET_MESH_ApplicationType *a;
6208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
6209 /* Check data sanity */
6210 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
6211 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
6212 ntypes = ntohs (cc_msg->types);
6213 napps = ntohs (cc_msg->applications);
6215 ntypes * sizeof (uint16_t) + napps * sizeof (GNUNET_MESH_ApplicationType))
6218 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6222 /* Create new client structure */
6223 c = GNUNET_malloc (sizeof (struct MeshClient));
6224 c->id = next_client_id++;
6225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
6227 GNUNET_SERVER_client_keep (client);
6228 a = (GNUNET_MESH_ApplicationType *) &cc_msg[1];
6231 GNUNET_MESH_ApplicationType at;
6232 struct GNUNET_HashCode hc;
6234 c->apps = GNUNET_CONTAINER_multihashmap_create (napps);
6235 for (i = 0; i < napps; i++)
6238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " app type: %u\n", at);
6239 GNUNET_CRYPTO_hash (&at, sizeof (at), &hc);
6240 /* store in clients hashmap */
6241 GNUNET_CONTAINER_multihashmap_put (c->apps, &hc, (void *) (long) at,
6242 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6243 /* store in global hashmap, for announcements */
6244 GNUNET_CONTAINER_multihashmap_put (applications, &hc, c,
6245 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6247 if (GNUNET_SCHEDULER_NO_TASK == announce_applications_task)
6248 announce_applications_task =
6249 GNUNET_SCHEDULER_add_now (&announce_applications, NULL);
6255 struct GNUNET_HashCode hc;
6257 t = (uint16_t *) & a[napps];
6258 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes);
6259 for (i = 0; i < ntypes; i++)
6262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
6263 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
6265 /* store in clients hashmap */
6266 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
6267 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6268 /* store in global hashmap */
6269 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
6270 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
6273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6274 " client has %u+%u subscriptions\n", napps, ntypes);
6276 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
6277 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6278 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6279 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
6280 GNUNET_SERVER_notification_context_add (nc, client);
6281 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
6283 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
6289 * Handler for clients announcing available services by a regular expression.
6291 * @param cls closure
6292 * @param client identification of the client
6293 * @param message the actual message, which includes messages the client wants
6296 handle_local_announce_regex (void *cls, struct GNUNET_SERVER_Client *client,
6297 const struct GNUNET_MessageHeader *message)
6299 struct MeshClient *c;
6303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex started\n");
6305 /* Sanity check for client registration */
6306 if (NULL == (c = client_get (client)))
6309 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6314 len = ntohs (message->size) - sizeof(struct GNUNET_MessageHeader);
6315 regex = GNUNET_malloc (len + 1);
6316 memcpy (regex, &message[1], len);
6318 GNUNET_array_append (c->regexes, c->n_regex, regex);
6319 if (GNUNET_SCHEDULER_NO_TASK == c->regex_announce_task)
6321 c->regex_announce_task = GNUNET_SCHEDULER_add_now(&announce_regex, c);
6327 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "announce regex processed\n");
6333 * Handler for requests of new tunnels
6335 * @param cls closure
6336 * @param client identification of the client
6337 * @param message the actual message
6340 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
6341 const struct GNUNET_MessageHeader *message)
6343 struct GNUNET_MESH_TunnelMessage *t_msg;
6344 struct MeshTunnel *t;
6345 struct MeshClient *c;
6346 MESH_TunnelNumber tid;
6348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
6350 /* Sanity check for client registration */
6351 if (NULL == (c = client_get (client)))
6354 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6359 /* Message sanity check */
6360 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6363 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6367 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6368 /* Sanity check for tunnel numbering */
6369 tid = ntohl (t_msg->tunnel_id);
6370 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
6373 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6376 /* Sanity check for duplicate tunnel IDs */
6377 if (NULL != tunnel_get_by_local_id (c, tid))
6380 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6384 while (NULL != tunnel_get_by_pi (myid, next_tid))
6385 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6386 t = tunnel_new (myid, next_tid++, c, tid);
6389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
6391 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6394 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
6395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
6396 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
6397 t->peers = GNUNET_CONTAINER_multihashmap_create (32);
6399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
6400 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6406 * Handler for requests of deleting tunnels
6408 * @param cls closure
6409 * @param client identification of the client
6410 * @param message the actual message
6413 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
6414 const struct GNUNET_MessageHeader *message)
6416 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6417 struct MeshClient *c;
6418 struct MeshTunnel *t;
6419 MESH_TunnelNumber tid;
6421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6422 "Got a DESTROY TUNNEL from client!\n");
6424 /* Sanity check for client registration */
6425 if (NULL == (c = client_get (client)))
6428 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6433 /* Message sanity check */
6434 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
6437 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6441 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6443 /* Retrieve tunnel */
6444 tid = ntohl (tunnel_msg->tunnel_id);
6445 t = tunnel_get_by_local_id(c, tid);
6448 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6450 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6453 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
6455 client_ignore_tunnel (c, t);
6457 // TODO: when to destroy incoming tunnel?
6458 if (t->nclients == 0)
6460 GNUNET_assert (GNUNET_YES ==
6461 GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels,
6463 GNUNET_assert (GNUNET_YES ==
6464 GNUNET_CONTAINER_multihashmap_remove (t->peers,
6465 &my_full_id.hashPubKey,
6469 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6472 send_client_tunnel_disconnect(t, c);
6473 client_delete_tunnel(c, t);
6475 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
6477 tunnel_send_destroy (t);
6478 t->destroy = GNUNET_YES;
6479 // The tunnel will be destroyed when the last message is transmitted.
6480 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6486 * Handler for requests of seeting tunnel's speed.
6488 * @param cls Closure (unused).
6489 * @param client Identification of the client.
6490 * @param message The actual message.
6493 handle_local_tunnel_speed (void *cls, struct GNUNET_SERVER_Client *client,
6494 const struct GNUNET_MessageHeader *message)
6496 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6497 struct MeshClient *c;
6498 struct MeshTunnel *t;
6499 MESH_TunnelNumber tid;
6501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6502 "Got a SPEED request from client!\n");
6504 /* Sanity check for client registration */
6505 if (NULL == (c = client_get (client)))
6508 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6514 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6516 /* Retrieve tunnel */
6517 tid = ntohl (tunnel_msg->tunnel_id);
6518 t = tunnel_get_by_local_id(c, tid);
6521 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " tunnel %X not found\n", tid);
6523 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6527 switch (ntohs(message->type))
6529 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN:
6530 t->speed_min = GNUNET_YES;
6532 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX:
6533 t->speed_min = GNUNET_NO;
6538 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6543 * Handler for requests of seeting tunnel's buffering policy.
6545 * @param cls Closure (unused).
6546 * @param client Identification of the client.
6547 * @param message The actual message.
6550 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
6551 const struct GNUNET_MessageHeader *message)
6553 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
6554 struct MeshClient *c;
6555 struct MeshTunnel *t;
6556 MESH_TunnelNumber tid;
6558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6559 "Got a BUFFER request from client!\n");
6561 /* Sanity check for client registration */
6562 if (NULL == (c = client_get (client)))
6565 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6570 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
6572 /* Retrieve tunnel */
6573 tid = ntohl (tunnel_msg->tunnel_id);
6574 t = tunnel_get_by_local_id(c, tid);
6577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
6579 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6583 switch (ntohs(message->type))
6585 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
6586 t->nobuffer = GNUNET_NO;
6588 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
6589 t->nobuffer = GNUNET_YES;
6595 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6600 * Handler for connection requests to new peers
6602 * @param cls closure
6603 * @param client identification of the client
6604 * @param message the actual message (PeerControl)
6607 handle_local_connect_add (void *cls, struct GNUNET_SERVER_Client *client,
6608 const struct GNUNET_MessageHeader *message)
6610 struct GNUNET_MESH_PeerControl *peer_msg;
6611 struct MeshPeerInfo *peer_info;
6612 struct MeshClient *c;
6613 struct MeshTunnel *t;
6614 MESH_TunnelNumber tid;
6616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got connection request\n");
6617 /* Sanity check for client registration */
6618 if (NULL == (c = client_get (client)))
6621 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6626 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6628 /* Sanity check for message size */
6629 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6632 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6636 /* Tunnel exists? */
6637 tid = ntohl (peer_msg->tunnel_id);
6638 t = tunnel_get_by_local_id (c, tid);
6642 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6646 /* Does client own tunnel? */
6647 if (t->owner->handle != client)
6650 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n",
6654 GNUNET_i2s (&peer_msg->peer));
6655 peer_info = peer_info_get (&peer_msg->peer);
6657 tunnel_add_peer (t, peer_info);
6658 peer_info_connect (peer_info, t);
6660 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6666 * Handler for disconnection requests of peers in a tunnel
6668 * @param cls closure
6669 * @param client identification of the client
6670 * @param message the actual message (PeerControl)
6673 handle_local_connect_del (void *cls, struct GNUNET_SERVER_Client *client,
6674 const struct GNUNET_MessageHeader *message)
6676 struct GNUNET_MESH_PeerControl *peer_msg;
6677 struct MeshPeerInfo *peer_info;
6678 struct MeshClient *c;
6679 struct MeshTunnel *t;
6680 MESH_TunnelNumber tid;
6682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER DEL request\n");
6683 /* Sanity check for client registration */
6684 if (NULL == (c = client_get (client)))
6687 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6692 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6694 /* Sanity check for message size */
6695 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6698 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6702 /* Tunnel exists? */
6703 tid = ntohl (peer_msg->tunnel_id);
6704 t = tunnel_get_by_local_id (c, tid);
6708 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6713 /* Does client own tunnel? */
6714 if (t->owner->handle != client)
6717 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for peer %s\n",
6722 GNUNET_i2s (&peer_msg->peer));
6723 /* Is the peer in the tunnel? */
6725 GNUNET_CONTAINER_multihashmap_get (t->peers, &peer_msg->peer.hashPubKey);
6726 if (NULL == peer_info)
6729 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6733 /* Ok, delete peer from tunnel */
6734 GNUNET_CONTAINER_multihashmap_remove_all (t->peers,
6735 &peer_msg->peer.hashPubKey);
6737 send_destroy_path (t, peer_info->id);
6738 tunnel_delete_peer (t, peer_info->id);
6739 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6744 * Handler for blacklist requests of peers in a tunnel
6746 * @param cls closure
6747 * @param client identification of the client
6748 * @param message the actual message (PeerControl)
6750 * FIXME implement DHT block bloomfilter
6753 handle_local_blacklist (void *cls, struct GNUNET_SERVER_Client *client,
6754 const struct GNUNET_MessageHeader *message)
6756 struct GNUNET_MESH_PeerControl *peer_msg;
6757 struct MeshClient *c;
6758 struct MeshTunnel *t;
6759 MESH_TunnelNumber tid;
6761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER BLACKLIST request\n");
6762 /* Sanity check for client registration */
6763 if (NULL == (c = client_get (client)))
6766 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6771 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6773 /* Sanity check for message size */
6774 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6777 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6781 /* Tunnel exists? */
6782 tid = ntohl (peer_msg->tunnel_id);
6783 t = tunnel_get_by_local_id (c, tid);
6787 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6792 GNUNET_array_append(t->blacklisted, t->nblacklisted,
6793 GNUNET_PEER_intern(&peer_msg->peer));
6798 * Handler for unblacklist requests of peers in a tunnel
6800 * @param cls closure
6801 * @param client identification of the client
6802 * @param message the actual message (PeerControl)
6805 handle_local_unblacklist (void *cls, struct GNUNET_SERVER_Client *client,
6806 const struct GNUNET_MessageHeader *message)
6808 struct GNUNET_MESH_PeerControl *peer_msg;
6809 struct MeshClient *c;
6810 struct MeshTunnel *t;
6811 MESH_TunnelNumber tid;
6815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a PEER UNBLACKLIST request\n");
6816 /* Sanity check for client registration */
6817 if (NULL == (c = client_get (client)))
6820 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6825 peer_msg = (struct GNUNET_MESH_PeerControl *) message;
6827 /* Sanity check for message size */
6828 if (sizeof (struct GNUNET_MESH_PeerControl) != ntohs (peer_msg->header.size))
6831 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6835 /* Tunnel exists? */
6836 tid = ntohl (peer_msg->tunnel_id);
6837 t = tunnel_get_by_local_id (c, tid);
6841 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", t->id.tid);
6846 /* if peer is not known, complain */
6847 pid = GNUNET_PEER_search (&peer_msg->peer);
6854 /* search and remove from list */
6855 for (i = 0; i < t->nblacklisted; i++)
6857 if (t->blacklisted[i] == pid)
6859 t->blacklisted[i] = t->blacklisted[t->nblacklisted - 1];
6860 GNUNET_array_grow (t->blacklisted, t->nblacklisted, t->nblacklisted - 1);
6865 /* if peer hasn't been blacklisted, complain */
6871 * Handler for connection requests to new peers by type
6873 * @param cls closure
6874 * @param client identification of the client
6875 * @param message the actual message (ConnectPeerByType)
6878 handle_local_connect_by_type (void *cls, struct GNUNET_SERVER_Client *client,
6879 const struct GNUNET_MessageHeader *message)
6881 struct GNUNET_MESH_ConnectPeerByType *connect_msg;
6882 struct MeshClient *c;
6883 struct MeshTunnel *t;
6884 struct GNUNET_HashCode hash;
6885 MESH_TunnelNumber tid;
6887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got connect by type request\n");
6888 /* Sanity check for client registration */
6889 if (NULL == (c = client_get (client)))
6892 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6897 connect_msg = (struct GNUNET_MESH_ConnectPeerByType *) message;
6899 /* Sanity check for message size */
6900 if (sizeof (struct GNUNET_MESH_ConnectPeerByType) !=
6901 ntohs (connect_msg->header.size))
6904 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6908 /* Tunnel exists? */
6909 tid = ntohl (connect_msg->tunnel_id);
6910 t = tunnel_get_by_local_id (c, tid);
6914 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6918 /* Does client own tunnel? */
6919 if (t->owner->handle != client)
6922 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6926 /* Do WE have the service? */
6927 t->type = ntohl (connect_msg->type);
6928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type requested: %u\n", t->type);
6929 GNUNET_CRYPTO_hash (&t->type, sizeof (GNUNET_MESH_ApplicationType), &hash);
6930 if (GNUNET_CONTAINER_multihashmap_contains (applications, &hash) ==
6933 /* Yes! Fast forward, add ourselves to the tunnel and send the
6934 * good news to the client, and alert the destination client of
6935 * an incoming tunnel.
6937 * FIXME send a path create to self, avoid code duplication
6939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " available locally\n");
6940 GNUNET_CONTAINER_multihashmap_put (t->peers, &my_full_id.hashPubKey,
6941 peer_info_get (&my_full_id),
6942 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " notifying client\n");
6945 send_client_peer_connected (t, myid);
6946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Done\n");
6947 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6949 t->local_tid_dest = next_local_tid++;
6950 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
6951 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
6952 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
6956 /* Ok, lets find a peer offering the service */
6957 if (NULL != t->dht_get_type)
6959 GNUNET_DHT_get_stop (t->dht_get_type);
6961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " looking in DHT for %s\n",
6962 GNUNET_h2s (&hash));
6964 GNUNET_DHT_get_start (dht_handle,
6965 GNUNET_BLOCK_TYPE_MESH_PEER_BY_TYPE,
6967 dht_replication_level,
6968 GNUNET_DHT_RO_RECORD_ROUTE |
6969 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
6971 &dht_get_type_handler, t);
6973 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6979 * Handler for connection requests to new peers by a string service description.
6981 * @param cls closure
6982 * @param client identification of the client
6983 * @param message the actual message, which includes messages the client wants
6986 handle_local_connect_by_string (void *cls, struct GNUNET_SERVER_Client *client,
6987 const struct GNUNET_MessageHeader *message)
6989 struct GNUNET_MESH_ConnectPeerByString *msg;
6990 struct MeshRegexSearchContext *ctx;
6991 struct MeshRegexSearchInfo *info;
6992 struct GNUNET_DHT_GetHandle *get_h;
6993 struct GNUNET_HashCode key;
6994 struct MeshTunnel *t;
6995 struct MeshClient *c;
6996 MESH_TunnelNumber tid;
7000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7001 "Connect by string started\n");
7002 msg = (struct GNUNET_MESH_ConnectPeerByString *) message;
7003 size = htons (message->size);
7005 /* Sanity check for client registration */
7006 if (NULL == (c = client_get (client)))
7009 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7014 /* Message size sanity check */
7015 if (sizeof(struct GNUNET_MESH_ConnectPeerByString) >= size)
7018 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7022 /* Tunnel exists? */
7023 tid = ntohl (msg->tunnel_id);
7024 t = tunnel_get_by_local_id (c, tid);
7028 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7032 /* Does client own tunnel? */
7033 if (t->owner->handle != client)
7036 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7041 " on tunnel %s [%u]\n",
7042 GNUNET_i2s(&my_full_id),
7045 /* Only one connect_by_string allowed at the same time! */
7046 /* FIXME: allow more, return handle at api level to cancel, document */
7047 if (NULL != t->regex_ctx)
7050 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7054 /* Find string itself */
7055 len = size - sizeof(struct GNUNET_MESH_ConnectPeerByString);
7056 string = (const char *) &msg[1];
7058 /* Initialize context */
7059 size = GNUNET_REGEX_get_first_key(string, len, &key);
7060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7061 " consumed %u bits out of %u\n", size, len);
7062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7063 " looking for %s\n", GNUNET_h2s (&key));
7065 info = GNUNET_malloc (sizeof (struct MeshRegexSearchInfo));
7067 info->description = GNUNET_malloc (len + 1);
7068 memcpy (info->description, string, len);
7069 info->description[len] = '\0';
7070 info->dht_get_handles = GNUNET_CONTAINER_multihashmap_create(32);
7071 info->dht_get_results = GNUNET_CONTAINER_multihashmap_create(32);
7072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " string: %s\n", info->description);
7074 ctx = GNUNET_malloc (sizeof (struct MeshRegexSearchContext));
7075 ctx->position = size;
7079 GNUNET_array_append (info->contexts, info->n_contexts, ctx);
7081 /* Start search in DHT */
7082 get_h = GNUNET_DHT_get_start (dht_handle, /* handle */
7083 GNUNET_BLOCK_TYPE_MESH_REGEX, /* type */
7084 &key, /* key to search */
7085 dht_replication_level, /* replication level */
7086 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
7087 NULL, /* xquery */ // FIXME BLOOMFILTER
7088 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
7089 &dht_get_string_handler, ctx);
7091 GNUNET_break (GNUNET_OK ==
7092 GNUNET_CONTAINER_multihashmap_put(info->dht_get_handles,
7095 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
7097 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "connect by string processed\n");
7103 * Handler for client traffic directed to one peer
7105 * @param cls closure
7106 * @param client identification of the client
7107 * @param message the actual message
7110 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
7111 const struct GNUNET_MessageHeader *message)
7113 struct MeshClient *c;
7114 struct MeshTunnel *t;
7115 struct MeshPeerInfo *pi;
7116 struct GNUNET_MESH_Unicast *data_msg;
7117 MESH_TunnelNumber tid;
7120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7121 "Got a unicast request from a client!\n");
7123 /* Sanity check for client registration */
7124 if (NULL == (c = client_get (client)))
7127 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7132 data_msg = (struct GNUNET_MESH_Unicast *) message;
7134 /* Sanity check for message size */
7135 size = ntohs (message->size);
7136 if (sizeof (struct GNUNET_MESH_Unicast) +
7137 sizeof (struct GNUNET_MessageHeader) > size)
7140 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7144 /* Tunnel exists? */
7145 tid = ntohl (data_msg->tid);
7146 t = tunnel_get_by_local_id (c, tid);
7150 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7154 /* Is it a local tunnel? Then, does client own the tunnel? */
7155 if (t->owner->handle != client)
7158 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7162 pi = GNUNET_CONTAINER_multihashmap_get (t->peers,
7163 &data_msg->destination.hashPubKey);
7164 /* Is the selected peer in the tunnel? */
7168 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7172 /* PID should be as expected */
7173 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7176 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7177 "Unicast PID, expected %u, got %u\n",
7178 t->fwd_pid + 1, ntohl (data_msg->pid));
7179 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7183 /* Ok, everything is correct, send the message
7184 * (pretend we got it from a mesh peer)
7187 /* Work around const limitation */
7188 char buf[ntohs (message->size)] GNUNET_ALIGN;
7189 struct GNUNET_MESH_Unicast *copy;
7191 copy = (struct GNUNET_MESH_Unicast *) buf;
7192 memcpy (buf, data_msg, size);
7193 copy->oid = my_full_id;
7194 copy->tid = htonl (t->id.tid);
7195 copy->ttl = htonl (default_ttl);
7196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7197 " calling generic handler...\n");
7198 handle_mesh_data_unicast (NULL, &my_full_id, ©->header, NULL, 0);
7200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
7201 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7208 * Handler for client traffic directed to the origin
7210 * @param cls closure
7211 * @param client identification of the client
7212 * @param message the actual message
7215 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
7216 const struct GNUNET_MessageHeader *message)
7218 struct GNUNET_MESH_ToOrigin *data_msg;
7219 struct MeshTunnelClientInfo *clinfo;
7220 struct MeshClient *c;
7221 struct MeshTunnel *t;
7222 MESH_TunnelNumber tid;
7225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7226 "Got a ToOrigin request from a client!\n");
7227 /* Sanity check for client registration */
7228 if (NULL == (c = client_get (client)))
7231 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7236 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
7238 /* Sanity check for message size */
7239 size = ntohs (message->size);
7240 if (sizeof (struct GNUNET_MESH_ToOrigin) +
7241 sizeof (struct GNUNET_MessageHeader) > size)
7244 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7248 /* Tunnel exists? */
7249 tid = ntohl (data_msg->tid);
7250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7251 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
7254 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7257 t = tunnel_get_by_local_id (c, tid);
7260 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7263 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7267 /* It should be sent by someone who has this as incoming tunnel. */
7268 if (GNUNET_NO == client_knows_tunnel (c, t))
7271 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7275 /* PID should be as expected */
7276 clinfo = tunnel_get_client_fc (t, c);
7277 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
7280 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7281 "To Origin PID, expected %u, got %u\n",
7282 clinfo->bck_pid + 1,
7283 ntohl (data_msg->pid));
7284 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7288 /* Ok, everything is correct, send the message
7289 * (pretend we got it from a mesh peer)
7293 char buf[ntohs (message->size)] GNUNET_ALIGN;
7294 struct GNUNET_MESH_ToOrigin *copy;
7296 /* Work around const limitation */
7297 copy = (struct GNUNET_MESH_ToOrigin *) buf;
7298 memcpy (buf, data_msg, size);
7299 GNUNET_PEER_resolve (t->id.oid, ©->oid);
7300 copy->tid = htonl (t->id.tid);
7301 copy->ttl = htonl (default_ttl);
7302 if (ntohl (copy->pid) != (t->bck_pid + 1))
7305 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7306 "To Origin PID, expected %u, got %u\n",
7312 copy->sender = my_full_id;
7313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7314 " calling generic handler...\n");
7315 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header, NULL, 0);
7317 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7324 * Handler for client traffic directed to all peers in a tunnel
7326 * @param cls closure
7327 * @param client identification of the client
7328 * @param message the actual message
7331 handle_local_multicast (void *cls, struct GNUNET_SERVER_Client *client,
7332 const struct GNUNET_MessageHeader *message)
7334 struct MeshClient *c;
7335 struct MeshTunnel *t;
7336 struct GNUNET_MESH_Multicast *data_msg;
7337 MESH_TunnelNumber tid;
7339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7340 "Got a multicast request from a client!\n");
7342 /* Sanity check for client registration */
7343 if (NULL == (c = client_get (client)))
7346 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7351 data_msg = (struct GNUNET_MESH_Multicast *) message;
7353 /* Sanity check for message size */
7354 if (sizeof (struct GNUNET_MESH_Multicast) +
7355 sizeof (struct GNUNET_MessageHeader) > ntohs (data_msg->header.size))
7358 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7362 /* Tunnel exists? */
7363 tid = ntohl (data_msg->tid);
7364 t = tunnel_get_by_local_id (c, tid);
7368 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7369 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7371 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7375 /* Does client own tunnel? */
7376 if (t->owner->handle != client)
7379 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7383 /* PID should be as expected */
7384 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
7387 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7388 "Multicast PID, expected %u, got %u\n",
7389 t->fwd_pid + 1, ntohl (data_msg->pid));
7390 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7395 char buf[ntohs (message->size)] GNUNET_ALIGN;
7396 struct GNUNET_MESH_Multicast *copy;
7398 copy = (struct GNUNET_MESH_Multicast *) buf;
7399 memcpy (buf, message, ntohs (message->size));
7400 copy->oid = my_full_id;
7401 copy->tid = htonl (t->id.tid);
7402 copy->ttl = htonl (default_ttl);
7403 GNUNET_assert (ntohl (copy->pid) == (t->fwd_pid + 1));
7404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
7405 " calling generic handler...\n");
7406 handle_mesh_data_multicast (client, &my_full_id, ©->header, NULL, 0);
7409 /* receive done gets called when last copy is sent to a neighbor */
7415 * Handler for client's ACKs for payload traffic.
7417 * @param cls Closure (unused).
7418 * @param client Identification of the client.
7419 * @param message The actual message.
7422 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
7423 const struct GNUNET_MessageHeader *message)
7425 struct GNUNET_MESH_LocalAck *msg;
7426 struct MeshTunnel *t;
7427 struct MeshClient *c;
7428 MESH_TunnelNumber tid;
7431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
7432 /* Sanity check for client registration */
7433 if (NULL == (c = client_get (client)))
7436 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
7441 msg = (struct GNUNET_MESH_LocalAck *) message;
7443 /* Tunnel exists? */
7444 tid = ntohl (msg->tunnel_id);
7445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
7446 t = tunnel_get_by_local_id (c, tid);
7450 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
7451 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
7452 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
7456 ack = ntohl (msg->max_pid);
7457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
7459 /* Does client own tunnel? I.E: Is this and ACK for BCK traffic? */
7460 if (NULL != t->owner && t->owner->handle == client)
7462 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
7464 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7468 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
7469 tunnel_set_client_fwd_ack (t, c, ack);
7470 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
7473 GNUNET_SERVER_receive_done (client, GNUNET_OK);
7480 * Functions to handle messages from clients
7482 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
7483 {&handle_local_new_client, NULL,
7484 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
7485 {&handle_local_announce_regex, NULL,
7486 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ANNOUNCE_REGEX, 0},
7487 {&handle_local_tunnel_create, NULL,
7488 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
7489 sizeof (struct GNUNET_MESH_TunnelMessage)},
7490 {&handle_local_tunnel_destroy, NULL,
7491 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
7492 sizeof (struct GNUNET_MESH_TunnelMessage)},
7493 {&handle_local_tunnel_speed, NULL,
7494 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MIN,
7495 sizeof (struct GNUNET_MESH_TunnelMessage)},
7496 {&handle_local_tunnel_speed, NULL,
7497 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_MAX,
7498 sizeof (struct GNUNET_MESH_TunnelMessage)},
7499 {&handle_local_tunnel_buffer, NULL,
7500 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
7501 sizeof (struct GNUNET_MESH_TunnelMessage)},
7502 {&handle_local_tunnel_buffer, NULL,
7503 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
7504 sizeof (struct GNUNET_MESH_TunnelMessage)},
7505 {&handle_local_connect_add, NULL,
7506 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD,
7507 sizeof (struct GNUNET_MESH_PeerControl)},
7508 {&handle_local_connect_del, NULL,
7509 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL,
7510 sizeof (struct GNUNET_MESH_PeerControl)},
7511 {&handle_local_blacklist, NULL,
7512 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_BLACKLIST,
7513 sizeof (struct GNUNET_MESH_PeerControl)},
7514 {&handle_local_unblacklist, NULL,
7515 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_UNBLACKLIST,
7516 sizeof (struct GNUNET_MESH_PeerControl)},
7517 {&handle_local_connect_by_type, NULL,
7518 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_TYPE,
7519 sizeof (struct GNUNET_MESH_ConnectPeerByType)},
7520 {&handle_local_connect_by_string, NULL,
7521 GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD_BY_STRING, 0},
7522 {&handle_local_unicast, NULL,
7523 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
7524 {&handle_local_to_origin, NULL,
7525 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
7526 {&handle_local_multicast, NULL,
7527 GNUNET_MESSAGE_TYPE_MESH_MULTICAST, 0},
7528 {&handle_local_ack, NULL,
7529 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
7530 sizeof (struct GNUNET_MESH_LocalAck)},
7536 * To be called on core init/fail.
7538 * @param cls service closure
7539 * @param server handle to the server for this service
7540 * @param identity the public identity of this peer
7543 core_init (void *cls, struct GNUNET_CORE_Handle *server,
7544 const struct GNUNET_PeerIdentity *identity)
7547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
7548 core_handle = server;
7549 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
7552 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
7553 GNUNET_SCHEDULER_shutdown (); // Try gracefully
7555 GNUNET_abort(); // Try harder
7562 * Method called whenever a given peer connects.
7564 * @param cls closure
7565 * @param peer peer identity this notification is about
7566 * @param atsi performance data for the connection
7567 * @param atsi_count number of records in 'atsi'
7570 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
7571 const struct GNUNET_ATS_Information *atsi,
7572 unsigned int atsi_count)
7574 struct MeshPeerInfo *peer_info;
7575 struct MeshPeerPath *path;
7577 DEBUG_CONN ("Peer connected\n");
7578 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
7579 peer_info = peer_info_get (peer);
7580 if (myid == peer_info->id)
7582 DEBUG_CONN (" (self)\n");
7587 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
7589 path = path_new (2);
7590 path->peers[0] = myid;
7591 path->peers[1] = peer_info->id;
7592 GNUNET_PEER_change_rc (myid, 1);
7593 GNUNET_PEER_change_rc (peer_info->id, 1);
7594 peer_info_add_path (peer_info, path, GNUNET_YES);
7595 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
7601 * Method called whenever a peer disconnects.
7603 * @param cls closure
7604 * @param peer peer identity this notification is about
7607 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
7609 struct MeshPeerInfo *pi;
7610 struct MeshPeerQueue *q;
7611 struct MeshPeerQueue *n;
7613 DEBUG_CONN ("Peer disconnected\n");
7614 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
7626 /* try to reroute this traffic instead */
7627 queue_destroy(q, GNUNET_YES);
7631 peer_info_remove_path (pi, pi->id, myid);
7634 DEBUG_CONN (" (self)\n");
7636 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
7641 /******************************************************************************/
7642 /************************ MAIN FUNCTIONS ****************************/
7643 /******************************************************************************/
7646 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
7648 * @param cls closure
7649 * @param key current key code
7650 * @param value value in the hash map
7651 * @return GNUNET_YES if we should continue to iterate,
7655 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
7657 struct MeshTunnel *t = value;
7664 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
7666 * @param cls closure
7667 * @param key current key code
7668 * @param value value in the hash map
7669 * @return GNUNET_YES if we should continue to iterate,
7673 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
7675 struct MeshPeerInfo *p = value;
7676 struct MeshPeerQueue *q;
7677 struct MeshPeerQueue *n;
7685 queue_destroy(q, GNUNET_YES);
7689 peer_info_destroy (p);
7694 * Task run during shutdown.
7700 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
7702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
7704 if (core_handle != NULL)
7706 GNUNET_CORE_disconnect (core_handle);
7709 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
7710 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
7711 if (dht_handle != NULL)
7713 GNUNET_DHT_disconnect (dht_handle);
7718 GNUNET_SERVER_notification_context_destroy (nc);
7721 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
7723 GNUNET_SCHEDULER_cancel (announce_id_task);
7724 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
7726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
7730 * Process mesh requests.
7732 * @param cls closure
7733 * @param server the initialized server
7734 * @param c configuration to use
7737 run (void *cls, struct GNUNET_SERVER_Handle *server,
7738 const struct GNUNET_CONFIGURATION_Handle *c)
7740 struct MeshPeerInfo *peer;
7741 struct MeshPeerPath *p;
7744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
7745 server_handle = server;
7746 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
7747 NULL, /* Closure passed to MESH functions */
7748 &core_init, /* Call core_init once connected */
7749 &core_connect, /* Handle connects */
7750 &core_disconnect, /* remove peers on disconnects */
7751 NULL, /* Don't notify about all incoming messages */
7752 GNUNET_NO, /* For header only in notification */
7753 NULL, /* Don't notify about all outbound messages */
7754 GNUNET_NO, /* For header-only out notification */
7755 core_handlers); /* Register these handlers */
7757 if (core_handle == NULL)
7760 GNUNET_SCHEDULER_shutdown ();
7765 GNUNET_CONFIGURATION_get_value_filename (c, "GNUNETD", "HOSTKEY",
7768 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7770 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7772 GNUNET_SCHEDULER_shutdown ();
7777 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
7778 &refresh_path_time))
7780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7782 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7783 "refresh path time");
7784 GNUNET_SCHEDULER_shutdown ();
7789 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
7790 &app_announce_time))
7792 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7794 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7795 "app announce time");
7796 GNUNET_SCHEDULER_shutdown ();
7801 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
7804 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7806 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7807 "id announce time");
7808 GNUNET_SCHEDULER_shutdown ();
7813 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "UNACKNOWLEDGED_WAIT",
7814 &unacknowledged_wait_time))
7816 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7818 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7819 "unacknowledged wait time");
7820 GNUNET_SCHEDULER_shutdown ();
7825 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
7828 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7830 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7832 GNUNET_SCHEDULER_shutdown ();
7837 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
7840 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7842 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7844 GNUNET_SCHEDULER_shutdown ();
7849 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
7852 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7854 ("Mesh service is lacking key configuration settings (%s). Exiting.\n"),
7856 GNUNET_SCHEDULER_shutdown ();
7861 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
7864 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7866 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7872 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
7873 &dht_replication_level))
7875 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
7877 ("Mesh service is lacking key configuration settings (%s). Using default (%u).\n"),
7878 "dht replication level", 10);
7879 dht_replication_level = 10;
7883 my_private_key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
7884 GNUNET_free (keyfile);
7885 if (my_private_key == NULL)
7887 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
7888 _("Mesh service could not access hostkey. Exiting.\n"));
7889 GNUNET_SCHEDULER_shutdown ();
7892 GNUNET_CRYPTO_rsa_key_get_public (my_private_key, &my_public_key);
7893 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
7894 &my_full_id.hashPubKey);
7895 myid = GNUNET_PEER_intern (&my_full_id);
7896 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7897 "Mesh for peer [%s] starting\n",
7898 GNUNET_i2s(&my_full_id));
7900 // transport_handle = GNUNET_TRANSPORT_connect(c,
7907 dht_handle = GNUNET_DHT_connect (c, 64);
7908 if (dht_handle == NULL)
7913 stats = GNUNET_STATISTICS_create ("mesh", c);
7917 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
7919 tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7920 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32);
7921 peers = GNUNET_CONTAINER_multihashmap_create (32);
7922 applications = GNUNET_CONTAINER_multihashmap_create (32);
7923 types = GNUNET_CONTAINER_multihashmap_create (32);
7925 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
7926 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
7927 GNUNET_SERVER_disconnect_notify (server_handle,
7928 &handle_local_client_disconnect, NULL);
7932 clients_tail = NULL;
7935 announce_applications_task = GNUNET_SCHEDULER_NO_TASK;
7936 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
7938 /* Create a peer_info for the local peer */
7939 peer = peer_info_get (&my_full_id);
7942 GNUNET_PEER_change_rc (myid, 1);
7943 peer_info_add_path (peer, p, GNUNET_YES);
7945 /* Scheduled the task to clean up when shutdown is called */
7946 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
7949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "end of run()\n");
7953 * The main function for the mesh service.
7955 * @param argc number of arguments from the command line
7956 * @param argv command line arguments
7957 * @return 0 ok, 1 on error
7960 main (int argc, char *const *argv)
7964 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
7967 GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
7969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
7973 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
7974 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
7975 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);