2 This file is part of GNUnet.
3 (C) 2001-2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file mesh/gnunet-service-mesh.c
23 * @brief GNUnet MESH service
24 * @author Bartlomiej Polot
30 * - PERIODIC FUNCTIONS
31 * - MESH NETWORK HANDLER HELPERS
32 * - MESH NETWORK HANDLES
33 * - MESH LOCAL HANDLER HELPERS
34 * - MESH LOCAL HANDLES
35 * - MAIN FUNCTIONS (main & run)
38 * - error reporting (CREATE/CHANGE/ADD/DEL?) -- new message!
39 * - partial disconnect reporting -- same as error reporting?
41 * - relay corking down to core
42 * - set ttl relative to tree depth
43 * - Add data ACK count in path ACK
44 * - Make common GNUNET_MESH_Data header for unicast, to_orig, multicast
50 #include "mesh2_protocol.h"
51 #include "mesh_tunnel_tree.h"
52 #include "block_mesh.h"
53 #include "gnunet_dht_service.h"
54 #include "gnunet_statistics_service.h"
55 #include "gnunet_regex_lib.h"
57 #define MESH_BLOOM_SIZE 128
59 #define MESH_DEBUG_REGEX GNUNET_YES
60 #define MESH_DEBUG_DHT GNUNET_NO
61 #define MESH_DEBUG_CONNECTION GNUNET_NO
62 #define MESH_DEBUG_TIMING __LINUX__ && GNUNET_NO
64 #define MESH_MAX_POLL_TIME GNUNET_TIME_relative_multiply (\
65 GNUNET_TIME_UNIT_MINUTES,\
68 #if MESH_DEBUG_CONNECTION
69 #define DEBUG_CONN(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
71 #define DEBUG_CONN(...)
75 #define DEBUG_DHT(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
77 #define DEBUG_DHT(...)
81 #define DEBUG_REGEX(...) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, __VA_ARGS__)
83 #define DEBUG_REGEX(...)
90 struct timespec __mesh_start;
91 struct timespec __mesh_end;
92 #define INTERVAL_START clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_start))
93 #define INTERVAL_END \
95 clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &(__mesh_end));\
96 double __diff = __mesh_end.tv_nsec - __mesh_start.tv_nsec;\
97 if (__diff < 0) __diff += 1000000000;\
101 #define INTERVAL_SHOW \
103 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "AVG process time: %f ns\n", __sum/__count)
105 #define INTERVAL_START
107 #define INTERVAL_SHOW
110 /******************************************************************************/
111 /************************ DATA STRUCTURES ****************************/
112 /******************************************************************************/
114 /** FWD declaration */
120 * Struct representing a piece of data being sent to other peers
124 /** Tunnel it belongs to. */
125 struct MeshTunnel *t;
127 /** How many remaining neighbors still hav't got it. */
128 unsigned int reference_counter;
130 /** How many remaining neighbors we need to send this to. */
131 unsigned int total_out;
133 /** Size of the data. */
142 * Struct containing info about a queued transmission to this peer
149 struct MeshPeerQueue *next;
154 struct MeshPeerQueue *prev;
157 * Peer this transmission is directed to.
159 struct MeshPeerInfo *peer;
162 * Tunnel this message belongs to.
164 struct MeshTunnel *tunnel;
167 * Pointer to info stucture used as cls.
177 * Size of the message
184 * Struct to store regex information announced by clients.
186 struct MeshRegexDescriptor
189 * Regular expression itself.
194 * How many characters per edge can we squeeze?
196 uint16_t compression;
199 * Handle to announce the regex.
201 struct GNUNET_REGEX_announce_handle *h;
206 * Struct to keep information of searches of services described by a regex
207 * using a user-provided string service description.
209 struct MeshRegexSearchInfo
212 * Which tunnel is this for
214 struct MeshTunnel *t;
217 * User provided description of the searched service.
222 * Regex search handle.
224 struct GNUNET_REGEX_search_handle *search_handle;
227 * Peer that is connecting via connect_by_string. When connected, free ctx.
232 * Other peers that are found but not yet being connected to.
234 GNUNET_PEER_Id *peers;
237 * Number of elements in peers.
239 unsigned int n_peers;
242 * Next peer to try to connect to.
247 * Timeout for a connect attempt.
248 * When reached, try to connect to a different peer, if any. If not,
249 * try the same peer again.
251 GNUNET_SCHEDULER_TaskIdentifier timeout;
257 * Struct containing all info possibly needed to build a package when called
260 struct MeshTransmissionDescriptor
262 /** ID of the tunnel this packet travels in */
263 struct MESH_TunnelID *origin;
265 /** Who was this message being sent to */
266 struct MeshPeerInfo *peer;
268 /** Ultimate destination of the packet */
269 GNUNET_PEER_Id destination;
271 /** Data descriptor */
272 struct MeshData* mesh_data;
277 * Struct containing all information regarding a given peer
287 * Last time we heard from this peer
289 struct GNUNET_TIME_Absolute last_contact;
292 * Task handler for delayed connect task;
294 GNUNET_SCHEDULER_TaskIdentifier connect_task;
297 * Number of attempts to reconnect so far
299 int n_reconnect_attempts;
302 * Paths to reach the peer, ordered by ascending hop count
304 struct MeshPeerPath *path_head;
307 * Paths to reach the peer, ordered by ascending hop count
309 struct MeshPeerPath *path_tail;
312 * Handle to stop the DHT search for a path to this peer
314 struct GNUNET_DHT_GetHandle *dhtget;
317 * Closure given to the DHT GET
319 struct MeshPathInfo *dhtgetcls;
322 * Array of tunnels this peer participates in
323 * (most probably a small amount, therefore not a hashmap)
324 * When the path to the peer changes, notify these tunnels to let them
325 * re-adjust their path trees.
327 struct MeshTunnel **tunnels;
330 * Number of tunnels this peers participates in
332 unsigned int ntunnels;
335 * Transmission queue to core DLL head
337 struct MeshPeerQueue *queue_head;
340 * Transmission queue to core DLL tail
342 struct MeshPeerQueue *queue_tail;
345 * How many messages are in the queue to this peer.
347 unsigned int queue_n;
350 * Handle to for queued transmissions
352 struct GNUNET_CORE_TransmitHandle *core_transmit;
357 * Globally unique tunnel identification (owner + number)
358 * DO NOT USE OVER THE NETWORK
363 * Node that owns the tunnel
368 * Tunnel number to differentiate all the tunnels owned by the node oid
369 * ( tid < GNUNET_MESH_LOCAL_TUNNEL_ID_CLI )
371 MESH_TunnelNumber tid;
376 * Struct containing all information regarding a tunnel
377 * For an intermediate node the improtant info used will be:
378 * - id Tunnel unique identification
379 * - paths[0] To know where to send it next
380 * - metainfo: ready, speeds, accounting
387 struct MESH_TunnelID id;
390 * Local tunnel number ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_CLI or 0 )
392 MESH_TunnelNumber local_tid;
395 * Local tunnel number for local destination clients (incoming number)
396 * ( >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV or 0). All clients share the same
399 MESH_TunnelNumber local_tid_dest;
402 * Is the tunnel bufferless (minimum latency)?
407 * Packet ID of the last fwd packet seen (sent/retransmitted/received).
412 * Packet ID of the last bck packet sent (unique counter per hop).
417 * Force sending ACK? Flag to allow duplicate ACK on POLL.
422 * Last ACK sent towards the origin (for traffic towards leaf node).
424 uint32_t last_fwd_ack;
427 * BCK ACK value received from the hop towards the owner of the tunnel,
428 * (previous node / owner): up to what message PID can we sent back to him.
433 * How many messages are in the forward queue (towards leaves).
435 unsigned int fwd_queue_n;
438 * How many messages do we accept in the forward queue.
440 unsigned int fwd_queue_max;
443 * How many messages are in the backward queue (towards origin).
445 unsigned int bck_queue_n;
448 * How many messages do we accept in the backward queue.
450 unsigned int bck_queue_max;
453 * Task to poll peer in case of a stall.
455 GNUNET_SCHEDULER_TaskIdentifier fc_poll_bck;
458 * Last time the tunnel was used
460 struct GNUNET_TIME_Absolute timestamp;
463 * Peers in the tunnel, indexed by PeerIdentity -> (MeshPeerInfo)
464 * containing peers added by id or by type, not intermediate peers.
466 struct GNUNET_PEER_Id peer;
469 * Client owner of the tunnel, if any
471 struct MeshClient *owner;
474 * Clients that have been informed about and want to stay in the tunnel.
476 struct MeshClient **clients;
479 * Flow control info for each client.
481 struct MeshTunnelClientInfo *clients_fc;
484 * Number of elements in clients/clients_fc
486 unsigned int nclients;
489 * Clients that have been informed but requested to leave the tunnel.
491 struct MeshClient **ignore;
494 * Number of elements in clients
496 unsigned int nignore;
499 * Bloomfilter (for peer identities) to stop circular routes
501 char bloomfilter[MESH_BLOOM_SIZE];
507 struct MeshTunnelTree *tree;
510 * Task to keep the used paths alive
512 GNUNET_SCHEDULER_TaskIdentifier path_refresh_task;
515 * Task to destroy the tunnel after timeout
517 * FIXME: merge the two? a tunnel will have either
518 * a path refresh OR a timeout, never both!
520 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
523 * Flag to signal the destruction of the tunnel.
524 * If this is set GNUNET_YES the tunnel will be destroyed
525 * when the queue is empty.
530 * Total messages pending for this tunnels, payload or not.
532 unsigned int pending_messages;
535 * If the tunnel is empty, destoy it.
537 GNUNET_SCHEDULER_TaskIdentifier delayed_destroy;
542 * Info about a child node in a tunnel, needed to perform flow control.
544 struct MeshTunnelChildInfo
547 * ID of the child node.
567 * Maximum PID allowed (FWD ACK received).
572 * Last ACK sent to that child (BCK ACK).
577 * Circular buffer pointing to MeshPeerQueue elements for all
578 * payload traffic going to this child.
579 * Size determined by the tunnel queue size (@c t->fwd_queue_max).
581 struct MeshPeerQueue **send_buffer;
584 * Index of the oldest element in the send_buffer.
586 unsigned int send_buffer_start;
589 * How many elements are already in the buffer.
591 unsigned int send_buffer_n;
594 * Tunnel this info is about
596 struct MeshTunnel *t;
599 * Task to poll peer in case of a stall.
601 GNUNET_SCHEDULER_TaskIdentifier fc_poll;
604 * Time to use for next polling call.
606 struct GNUNET_TIME_Relative fc_poll_time;
611 * Info about a leaf client of a tunnel, needed to perform flow control.
613 struct MeshTunnelClientInfo
616 * PID of the last packet sent to the client (FWD).
621 * PID of the last packet received from the client (BCK).
626 * Maximum PID allowed (FWD ACK received).
631 * Last ACK sent to that child (BCK ACK).
639 * Info collected during iteration of child nodes in order to get the ACK value
642 struct MeshTunnelChildIteratorContext
645 * Tunnel whose info is being collected.
647 struct MeshTunnel *t;
650 * Is this context initialized? Is the value in max_child_ack valid?
655 * Maximum child ACK so far.
657 uint32_t max_child_ack;
660 * Number of children nodes
662 unsigned int nchildren;
667 * Info needed to work with tunnel paths and peers
674 struct MeshTunnel *t;
677 * Neighbouring peer to whom we send the packet to
679 struct MeshPeerInfo *peer;
684 struct MeshPeerPath *path;
689 * Struct containing information about a client of the service
696 struct MeshClient *next;
701 struct MeshClient *prev;
704 * Tunnels that belong to this client, indexed by local id
706 struct GNUNET_CONTAINER_MultiHashMap *own_tunnels;
709 * Tunnels this client has accepted, indexed by incoming local id
711 struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
714 * Tunnels this client has rejected, indexed by incoming local id
716 struct GNUNET_CONTAINER_MultiHashMap *ignore_tunnels;
718 * Handle to communicate with the client
720 struct GNUNET_SERVER_Client *handle;
723 * Messages that this client has declared interest in
725 struct GNUNET_CONTAINER_MultiHashMap *types;
728 * Whether the client is active or shutting down (don't send confirmations
729 * to a client that is shutting down.
734 * ID of the client, mainly for debug messages
739 * Regular expressions describing the services offered by this client.
741 struct MeshRegexDescriptor *regexes; // FIXME regex add timeout? API to remove a regex?
744 * Number of regular expressions in regexes.
746 unsigned int n_regex;
749 * Task to refresh all regular expresions in the DHT.
751 GNUNET_SCHEDULER_TaskIdentifier regex_announce_task;
754 * Tmp store for partially retrieved regex.
761 /******************************************************************************/
762 /************************ DEBUG FUNCTIONS ****************************/
763 /******************************************************************************/
767 * GNUNET_SCHEDULER_Task for printing a message after some operation is done
768 * @param cls string to print
769 * @param success GNUNET_OK if the PUT was transmitted,
770 * GNUNET_NO on timeout,
771 * GNUNET_SYSERR on disconnect from service
772 * after the PUT message was transmitted
773 * (so we don't know if it was received or not)
778 mesh_debug (void *cls, int success)
782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "%s (%d)\n", s, success);
786 unsigned int debug_fwd_ack;
787 unsigned int debug_bck_ack;
791 /******************************************************************************/
792 /*********************** GLOBAL VARIABLES ****************************/
793 /******************************************************************************/
795 /************************** Configuration parameters **************************/
798 * How often to send tunnel keepalives. Tunnels timeout after 4 missed.
800 static struct GNUNET_TIME_Relative refresh_path_time;
803 * How often to PUT local application numbers in the DHT.
805 static struct GNUNET_TIME_Relative app_announce_time;
808 * How often to PUT own ID in the DHT.
810 static struct GNUNET_TIME_Relative id_announce_time;
813 * Maximum time allowed to connect to a peer found by string.
815 static struct GNUNET_TIME_Relative connect_timeout;
818 * Default TTL for payload packets.
820 static unsigned long long default_ttl;
823 * DHT replication level, see DHT API: GNUNET_DHT_get_start, GNUNET_DHT_put.
825 static unsigned long long dht_replication_level;
828 * How many tunnels are we willing to maintain.
829 * Local tunnels are always allowed, even if there are more tunnels than max.
831 static unsigned long long max_tunnels;
834 * How many messages *in total* are we willing to queue, divided by number of
835 * tunnels to get tunnel queue size.
837 static unsigned long long max_msgs_queue;
840 * How many peers do we want to remember?
842 static unsigned long long max_peers;
845 /*************************** Static global variables **************************/
848 * Hostkey generation context
850 static struct GNUNET_CRYPTO_EccKeyGenerationContext *keygen;
853 * DLL with all the clients, head.
855 static struct MeshClient *clients;
858 * DLL with all the clients, tail.
860 static struct MeshClient *clients_tail;
863 * Tunnels known, indexed by MESH_TunnelID (MeshTunnel).
865 static struct GNUNET_CONTAINER_MultiHashMap *tunnels;
868 * Number of tunnels known.
870 static unsigned long long n_tunnels;
873 * Tunnels incoming, indexed by MESH_TunnelNumber
874 * (which is greater than GNUNET_MESH_LOCAL_TUNNEL_ID_SERV).
876 static struct GNUNET_CONTAINER_MultiHashMap *incoming_tunnels;
879 * Peers known, indexed by PeerIdentity (MeshPeerInfo).
881 static struct GNUNET_CONTAINER_MultiHashMap *peers;
884 * Handle to communicate with transport
886 // static struct GNUNET_TRANSPORT_Handle *transport_handle;
889 * Handle to communicate with core.
891 static struct GNUNET_CORE_Handle *core_handle;
896 static struct GNUNET_DHT_Handle *dht_handle;
901 static struct GNUNET_SERVER_Handle *server_handle;
904 * Handle to the statistics service.
906 static struct GNUNET_STATISTICS_Handle *stats;
909 * Notification context, to send messages to local clients.
911 static struct GNUNET_SERVER_NotificationContext *nc;
914 * Local peer own ID (memory efficient handle).
916 static GNUNET_PEER_Id myid;
919 * Local peer own ID (full value).
921 static struct GNUNET_PeerIdentity my_full_id;
926 static struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
931 static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
934 * Tunnel ID for the next created tunnel (global tunnel number).
936 static MESH_TunnelNumber next_tid;
939 * Tunnel ID for the next incoming tunnel (local tunnel number).
941 static MESH_TunnelNumber next_local_tid;
944 * All message types clients of this peer are interested in.
946 static struct GNUNET_CONTAINER_MultiHashMap *types;
949 * Task to periodically announce itself in the network.
951 GNUNET_SCHEDULER_TaskIdentifier announce_id_task;
954 * Next ID to assign to a client.
956 unsigned int next_client_id;
959 /******************************************************************************/
960 /*********************** DECLARATIONS **************************/
961 /******************************************************************************/
964 * Function to process paths received for a new peer addition. The recorded
965 * paths form the initial tunnel, which can be optimized later.
966 * Called on each result obtained for the DHT search.
969 * @param exp when will this value expire
970 * @param key key of the result
971 * @param type type of the result
972 * @param size number of bytes in data
973 * @param data pointer to the result data
976 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
977 const struct GNUNET_HashCode * key,
978 const struct GNUNET_PeerIdentity *get_path,
979 unsigned int get_path_length,
980 const struct GNUNET_PeerIdentity *put_path,
981 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
982 size_t size, const void *data);
986 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
987 * and insert it in the appropiate structures if the peer is not known yet.
989 * @param peer Full identity of the peer.
991 * @return Existing or newly created peer info.
993 static struct MeshPeerInfo *
994 peer_info_get (const struct GNUNET_PeerIdentity *peer);
998 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
999 * and insert it in the appropiate structures if the peer is not known yet.
1001 * @param peer Short identity of the peer.
1003 * @return Existing or newly created peer info.
1005 static struct MeshPeerInfo *
1006 peer_info_get_short (const GNUNET_PEER_Id peer);
1010 * Try to establish a new connection to this peer.
1011 * Use the best path for the given tunnel.
1012 * If the peer doesn't have any path to it yet, try to get one.
1013 * If the peer already has some path, send a CREATE PATH towards it.
1015 * @param peer PeerInfo of the peer.
1016 * @param t Tunnel for which to create the path, if possible.
1019 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t);
1023 * Build a PeerPath from the paths returned from the DHT, reversing the paths
1024 * to obtain a local peer -> destination path and interning the peer ids.
1026 * @return Newly allocated and created path
1028 static struct MeshPeerPath *
1029 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
1030 unsigned int get_path_length,
1031 const struct GNUNET_PeerIdentity *put_path,
1032 unsigned int put_path_length);
1036 * Adds a path to the peer_infos of all the peers in the path
1038 * @param p Path to process.
1039 * @param confirmed Whether we know if the path works or not.
1042 path_add_to_peers (struct MeshPeerPath *p, int confirmed);
1046 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
1047 * needed rescources.
1048 * If peer already exists, reevaluate shortest path and change if different.
1050 * @param t Tunnel we want to add a new peer to
1051 * @param peer PeerInfo of the peer being added
1055 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer);
1059 * Removes an explicit path from a tunnel, freeing all intermediate nodes
1060 * that are no longer needed, as well as nodes of no longer reachable peers.
1061 * The tunnel itself is also destoyed if results in a remote empty tunnel.
1063 * @param t Tunnel from which to remove the path.
1064 * @param peer Short id of the peer which should be removed.
1067 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer);
1071 * Search for a tunnel by global ID using full PeerIdentities.
1073 * @param oid owner of the tunnel.
1074 * @param tid global tunnel number.
1076 * @return tunnel handler, NULL if doesn't exist.
1078 static struct MeshTunnel *
1079 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid);
1083 * Delete an active client from the tunnel.
1089 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c);
1092 * Notify a tunnel that a connection has broken that affects at least
1093 * some of its peers.
1095 * @param t Tunnel affected.
1096 * @param p1 Peer that got disconnected from p2.
1097 * @param p2 Peer that got disconnected from p1.
1099 * @return Short ID of the peer disconnected (either p1 or p2).
1100 * 0 if the tunnel remained unaffected.
1102 static GNUNET_PEER_Id
1103 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
1108 * Get the current ack value for a tunnel, for data going from root to leaves,
1109 * taking in account the tunnel mode and the status of all children and clients.
1113 * @return Maximum PID allowed.
1116 tunnel_get_fwd_ack (struct MeshTunnel *t);
1120 * Add a client to a tunnel, initializing all needed data structures.
1122 * @param t Tunnel to which add the client.
1123 * @param c Client which to add to the tunnel.
1126 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c);
1130 * @brief Queue and pass message to core when possible.
1132 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
1133 * and accounts for it. In case the queue is full, the message is dropped and
1136 * Otherwise, message is treated as internal and allowed to go regardless of
1139 * @param cls Closure (@c type dependant). It will be used by queue_send to
1140 * build the message to be sent if not already prebuilt.
1141 * @param type Type of the message, 0 for a raw message.
1142 * @param size Size of the message.
1143 * @param dst Neighbor to send message to.
1144 * @param t Tunnel this message belongs to.
1147 queue_add (void *cls, uint16_t type, size_t size,
1148 struct MeshPeerInfo *dst, struct MeshTunnel *t);
1152 * Free a transmission that was already queued with all resources
1153 * associated to the request.
1155 * @param queue Queue handler to cancel.
1156 * @param clear_cls Is it necessary to free associated cls?
1159 queue_destroy (struct MeshPeerQueue *queue, int clear_cls);
1163 * @brief Get the next transmittable message from the queue.
1165 * This will be the head, except in the case of being a data packet
1166 * not allowed by the destination peer.
1168 * @param peer Destination peer.
1170 * @return The next viable MeshPeerQueue element to send to that peer.
1171 * NULL when there are no transmittable messages.
1173 struct MeshPeerQueue *
1174 queue_get_next (const struct MeshPeerInfo *peer);
1178 * Core callback to write a queued packet to core buffer
1180 * @param cls Closure (peer info).
1181 * @param size Number of bytes available in buf.
1182 * @param buf Where the to write the message.
1184 * @return number of bytes written to buf
1187 queue_send (void *cls, size_t size, void *buf);
1190 /******************************************************************************/
1191 /************************ PERIODIC FUNCTIONS ****************************/
1192 /******************************************************************************/
1195 * Periodically announce self id in the DHT
1197 * @param cls closure
1198 * @param tc task context
1201 announce_id (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1203 struct PBlock block;
1205 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
1207 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
1211 * - Set data expiration in function of X
1212 * - Adapt X to churn
1214 DEBUG_DHT ("DHT_put for ID %s started.\n", GNUNET_i2s (&my_full_id));
1216 block.id = my_full_id;
1217 block.type = htonl (0);
1218 GNUNET_DHT_put (dht_handle, /* DHT handle */
1219 &my_full_id.hashPubKey, /* Key to use */
1220 dht_replication_level, /* Replication level */
1221 GNUNET_DHT_RO_RECORD_ROUTE | GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE, /* DHT options */
1222 GNUNET_BLOCK_TYPE_MESH_PEER, /* Block type */
1223 sizeof (block), /* Size of the data */
1224 (const char *) &block, /* Data itself */
1225 GNUNET_TIME_UNIT_FOREVER_ABS, /* Data expiration */
1226 GNUNET_TIME_UNIT_FOREVER_REL, /* Retry time */
1227 NULL, /* Continuation */
1228 NULL); /* Continuation closure */
1230 GNUNET_SCHEDULER_add_delayed (id_announce_time, &announce_id, cls);
1234 /******************************************************************************/
1235 /****************** GENERAL HELPER FUNCTIONS ************************/
1236 /******************************************************************************/
1239 * Decrements the reference counter and frees all resources if needed
1241 * @param mesh_data Data Descriptor used in a multicast message.
1242 * Freed no longer needed (last message).
1245 data_descriptor_decrement_rc (struct MeshData *mesh_data)
1247 if (0 == --(mesh_data->reference_counter))
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Last copy!\n");
1250 GNUNET_free (mesh_data->data);
1251 GNUNET_free (mesh_data);
1257 * Check if client has registered with the service and has not disconnected
1259 * @param client the client to check
1261 * @return non-NULL if client exists in the global DLL
1263 static struct MeshClient *
1264 client_get (struct GNUNET_SERVER_Client *client)
1266 struct MeshClient *c;
1271 if (c->handle == client)
1280 * Checks if a given client has subscribed to certain message type
1282 * @param message_type Type of message to check
1283 * @param c Client to check
1285 * @return GNUNET_YES or GNUNET_NO, depending on subscription status
1287 * FIXME: use of crypto_hash slows it down
1288 * The hash function alone takes 8-10us out of the ~55us for the whole
1289 * process of retransmitting the message from one local client to another.
1290 * Find faster implementation!
1293 client_is_subscribed (uint16_t message_type, struct MeshClient *c)
1295 struct GNUNET_HashCode hc;
1297 if (NULL == c->types)
1300 GNUNET_CRYPTO_hash (&message_type, sizeof (uint16_t), &hc);
1301 return GNUNET_CONTAINER_multihashmap_contains (c->types, &hc);
1306 * Check whether client wants traffic from a tunnel.
1308 * @param c Client to check.
1309 * @param t Tunnel to be found.
1311 * @return GNUNET_YES if client knows tunnel.
1313 * TODO look in client hashmap
1316 client_wants_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1320 for (i = 0; i < t->nclients; i++)
1321 if (t->clients[i] == c)
1328 * Check whether client has been informed about a tunnel.
1330 * @param c Client to check.
1331 * @param t Tunnel to be found.
1333 * @return GNUNET_YES if client knows tunnel.
1335 * TODO look in client hashmap
1338 client_knows_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1342 for (i = 0; i < t->nignore; i++)
1343 if (t->ignore[i] == c)
1345 return client_wants_tunnel(c, t);
1350 * Marks a client as uninterested in traffic from the tunnel, updating both
1351 * client and tunnel to reflect this.
1353 * @param c Client that doesn't want traffic anymore.
1354 * @param t Tunnel which should be ignored.
1356 * FIXME when to delete an incoming tunnel?
1359 client_ignore_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1361 struct GNUNET_HashCode hash;
1363 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1364 GNUNET_break (GNUNET_YES ==
1365 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1367 GNUNET_break (GNUNET_YES ==
1368 GNUNET_CONTAINER_multihashmap_put (c->ignore_tunnels, &hash, t,
1369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1370 tunnel_delete_active_client (t, c);
1371 GNUNET_array_append (t->ignore, t->nignore, c);
1376 * Deletes a tunnel from a client (either owner or destination). To be used on
1377 * tunnel destroy, otherwise, use client_ignore_tunnel.
1379 * @param c Client whose tunnel to delete.
1380 * @param t Tunnel which should be deleted.
1383 client_delete_tunnel (struct MeshClient *c, struct MeshTunnel *t)
1385 struct GNUNET_HashCode hash;
1389 GNUNET_CRYPTO_hash(&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
1390 GNUNET_assert (GNUNET_YES ==
1391 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels,
1397 GNUNET_CRYPTO_hash(&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
1399 GNUNET_assert (GNUNET_YES ==
1400 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels,
1404 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels,
1412 * Notify the owner of a tunnel that a peer has disconnected.
1414 * @param c Client (owner of tunnel).
1415 * @param t Tunnel this message is about.
1416 * @param peer_id Short ID of the disconnected peer.
1419 client_notify_peer_disconnected (struct MeshClient *c,
1420 struct MeshTunnel *t,
1421 GNUNET_PEER_Id peer_id)
1423 struct GNUNET_MESH_PeerControl msg;
1425 if (NULL == t->owner || NULL == nc)
1428 msg.header.size = htons (sizeof (msg));
1429 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1430 msg.tunnel_id = htonl (t->local_tid);
1431 GNUNET_PEER_resolve (peer_id, &msg.peer);
1432 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1433 &msg.header, GNUNET_NO);
1438 * Send the message to all clients that have subscribed to its type
1440 * @param msg Pointer to the message itself
1441 * @param payload Pointer to the payload of the message.
1442 * @param t The tunnel to whose clients this message goes.
1444 * @return number of clients this message was sent to
1447 send_subscribed_clients (const struct GNUNET_MessageHeader *msg,
1448 const struct GNUNET_MessageHeader *payload,
1449 struct MeshTunnel *t)
1451 struct MeshClient *c;
1452 MESH_TunnelNumber *tid;
1455 char cbuf[htons (msg->size)];
1457 type = ntohs (payload->type);
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending to clients...\n");
1459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "message of type %s\n",
1460 GNUNET_MESH_DEBUG_M2S (type));
1462 memcpy (cbuf, msg, sizeof (cbuf));
1463 switch (htons (msg->type))
1465 struct GNUNET_MESH_Unicast *uc;
1466 struct GNUNET_MESH_Multicast *mc;
1467 struct GNUNET_MESH_ToOrigin *to;
1469 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1470 uc = (struct GNUNET_MESH_Unicast *) cbuf;
1473 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
1474 mc = (struct GNUNET_MESH_Multicast *) cbuf;
1477 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1478 to = (struct GNUNET_MESH_ToOrigin *) cbuf;
1486 for (count = 0, c = clients; c != NULL; c = c->next)
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u\n", c->id);
1489 if (client_is_subscribed (type, c))
1491 if (htons (msg->type) == GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN)
1495 *tid = htonl (t->local_tid);
1499 if (GNUNET_NO == client_knows_tunnel (c, t))
1501 /* This client doesn't know the tunnel */
1502 struct GNUNET_MESH_TunnelNotification tmsg;
1503 struct GNUNET_HashCode hash;
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending tunnel create\n");
1506 tmsg.header.size = htons (sizeof (tmsg));
1507 tmsg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
1508 GNUNET_PEER_resolve (t->id.oid, &tmsg.peer);
1509 tmsg.tunnel_id = htonl (t->local_tid_dest);
1511 if (GNUNET_YES == t->speed_min)
1512 tmsg.opt |= MESH_TUNNEL_OPT_SPEED_MIN;
1513 if (GNUNET_YES == t->nobuffer)
1514 tmsg.opt |= MESH_TUNNEL_OPT_NOBUFFER;
1515 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1516 &tmsg.header, GNUNET_NO);
1517 tunnel_add_client (t, c);
1518 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
1520 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multihashmap_put (
1521 c->incoming_tunnels, &hash, t,
1522 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST));
1524 *tid = htonl (t->local_tid_dest);
1527 /* Check if the client wants to get traffic from the tunnel */
1528 if (GNUNET_NO == client_wants_tunnel(c, t))
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending\n");
1532 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
1533 (struct GNUNET_MessageHeader
1534 *) cbuf, GNUNET_NO);
1543 * Notify the client that owns the tunnel that a peer has connected to it
1544 * (the requested path to it has been confirmed).
1546 * @param t Tunnel whose owner to notify
1547 * @param id Short id of the peer that has connected
1550 send_client_peer_connected (const struct MeshTunnel *t, const GNUNET_PEER_Id id)
1552 struct GNUNET_MESH_PeerControl pc;
1554 if (NULL == t->owner || GNUNET_YES == t->destroy)
1557 pc.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_ADD);
1558 pc.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
1559 pc.tunnel_id = htonl (t->local_tid);
1560 GNUNET_PEER_resolve (id, &pc.peer);
1561 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle, &pc.header,
1567 * Notify all clients (not depending on registration status) that the incoming
1568 * tunnel is no longer valid.
1570 * @param t Tunnel that was destroyed.
1573 send_clients_tunnel_destroy (struct MeshTunnel *t)
1575 struct GNUNET_MESH_TunnelMessage msg;
1577 msg.header.size = htons (sizeof (msg));
1578 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1579 msg.tunnel_id = htonl (t->local_tid_dest);
1580 GNUNET_SERVER_notification_context_broadcast (nc, &msg.header, GNUNET_NO);
1585 * Notify clients of tunnel disconnections, if needed.
1586 * In case the origin disconnects, the destination clients get a tunnel destroy
1587 * notification. If the last destination disconnects (only one remaining client
1588 * in tunnel), the origin gets a (local ID) peer disconnected.
1589 * Note that the function must be called BEFORE removing the client from
1592 * @param t Tunnel that was destroyed.
1593 * @param c Client that disconnected.
1596 send_client_tunnel_disconnect (struct MeshTunnel *t, struct MeshClient *c)
1602 struct GNUNET_MESH_TunnelMessage msg;
1604 msg.header.size = htons (sizeof (msg));
1605 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1606 msg.tunnel_id = htonl (t->local_tid_dest);
1607 for (i = 0; i < t->nclients; i++)
1608 GNUNET_SERVER_notification_context_unicast (nc, t->clients[i]->handle,
1609 &msg.header, GNUNET_NO);
1611 // FIXME when to disconnect an incoming tunnel?
1612 else if (1 == t->nclients && NULL != t->owner)
1614 struct GNUNET_MESH_PeerControl msg;
1616 msg.header.size = htons (sizeof (msg));
1617 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DEL);
1618 msg.tunnel_id = htonl (t->local_tid);
1619 msg.peer = my_full_id;
1620 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
1621 &msg.header, GNUNET_NO);
1627 * Iterator over all the peers to remove the oldest not-used entry.
1629 * @param cls Closure (unsued).
1630 * @param key ID of the peer.
1631 * @param value Peer_Info of the peer.
1636 peer_info_timeout (void *cls,
1637 const struct GNUNET_HashCode *key,
1644 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1645 * and insert it in the appropiate structures if the peer is not known yet.
1647 * @param peer Full identity of the peer.
1649 * @return Existing or newly created peer info.
1651 static struct MeshPeerInfo *
1652 peer_info_get (const struct GNUNET_PeerIdentity *peer)
1654 struct MeshPeerInfo *peer_info;
1656 peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
1657 if (NULL == peer_info)
1660 (struct MeshPeerInfo *) GNUNET_malloc (sizeof (struct MeshPeerInfo));
1661 if (GNUNET_CONTAINER_multihashmap_size (peers) > max_peers)
1663 GNUNET_CONTAINER_multihashmap_iterate (peers,
1667 GNUNET_CONTAINER_multihashmap_put (peers, &peer->hashPubKey, peer_info,
1668 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1669 peer_info->id = GNUNET_PEER_intern (peer);
1671 peer_info->last_contact = GNUNET_TIME_absolute_get();
1678 * Retrieve the MeshPeerInfo stucture associated with the peer, create one
1679 * and insert it in the appropiate structures if the peer is not known yet.
1681 * @param peer Short identity of the peer.
1683 * @return Existing or newly created peer info.
1685 static struct MeshPeerInfo *
1686 peer_info_get_short (const GNUNET_PEER_Id peer)
1688 struct GNUNET_PeerIdentity id;
1690 GNUNET_PEER_resolve (peer, &id);
1691 return peer_info_get (&id);
1696 * Iterator to remove the tunnel from the list of tunnels a peer participates
1699 * @param cls Closure (tunnel info)
1700 * @param key GNUNET_PeerIdentity of the peer (unused)
1701 * @param value PeerInfo of the peer
1703 * @return always GNUNET_YES, to keep iterating
1706 peer_info_delete_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
1708 struct MeshTunnel *t = cls;
1709 struct MeshPeerInfo *peer = value;
1712 for (i = 0; i < peer->ntunnels; i++)
1715 memcmp (&peer->tunnels[i]->id, &t->id, sizeof (struct MESH_TunnelID)))
1718 peer->tunnels[i] = peer->tunnels[peer->ntunnels];
1720 GNUNET_realloc (peer->tunnels,
1721 peer->ntunnels * sizeof(struct MeshTunnel *));
1730 * Core callback to write a pre-constructed data packet to core buffer
1732 * @param cls Closure (MeshTransmissionDescriptor with data in "data" member).
1733 * @param size Number of bytes available in buf.
1734 * @param buf Where the to write the message.
1736 * @return number of bytes written to buf
1739 send_core_data_raw (void *cls, size_t size, void *buf)
1741 struct MeshTransmissionDescriptor *info = cls;
1742 struct GNUNET_MessageHeader *msg;
1745 GNUNET_assert (NULL != info);
1746 GNUNET_assert (NULL != info->mesh_data);
1747 msg = (struct GNUNET_MessageHeader *) info->mesh_data->data;
1748 total_size = ntohs (msg->size);
1750 if (total_size > size)
1755 memcpy (buf, msg, total_size);
1756 data_descriptor_decrement_rc (info->mesh_data);
1763 * Sends an already built non-multicast message to a peer,
1764 * properly registrating all used resources.
1766 * @param message Message to send. Function makes a copy of it.
1767 * @param peer Short ID of the neighbor whom to send the message.
1768 * @param t Tunnel on which this message is transmitted.
1771 send_prebuilt_message (const struct GNUNET_MessageHeader *message,
1772 const struct GNUNET_PeerIdentity *peer,
1773 struct MeshTunnel *t)
1775 struct MeshTransmissionDescriptor *info;
1776 struct MeshPeerInfo *neighbor;
1777 struct MeshPeerPath *p;
1781 // GNUNET_TRANSPORT_try_connect(); FIXME use?
1783 size = ntohs (message->size);
1784 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1785 info->mesh_data = GNUNET_malloc (sizeof (struct MeshData));
1786 info->mesh_data->data = GNUNET_malloc (size);
1787 memcpy (info->mesh_data->data, message, size);
1788 type = ntohs(message->type);
1791 struct GNUNET_MESH_Unicast *m;
1792 struct GNUNET_MESH_ToOrigin *to;
1794 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
1795 m = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
1796 m->ttl = htonl (ntohl (m->ttl) - 1);
1798 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
1799 to = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
1801 to->pid = htonl(t->bck_pid);
1803 info->mesh_data->data_len = size;
1804 info->mesh_data->reference_counter = 1;
1805 info->mesh_data->total_out = 1;
1806 neighbor = peer_info_get (peer);
1807 for (p = neighbor->path_head; NULL != p; p = p->next)
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818 " %s IS NOT DIRECTLY CONNECTED\n",
1820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1823 for (p = neighbor->path_head; NULL != p; p = p->next)
1825 struct GNUNET_PeerIdentity debug_id;
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 " path with %u hops through:\n",
1831 for (i = 0; i < p->length; i++)
1833 GNUNET_PEER_resolve(p->peers[i], &debug_id);
1834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1836 i, GNUNET_i2s(&debug_id));
1840 GNUNET_break (0); // FIXME sometimes fails (testing disconnect?)
1841 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1842 " no direct connection to %s\n",
1844 GNUNET_free (info->mesh_data->data);
1845 GNUNET_free (info->mesh_data);
1849 info->peer = neighbor;
1850 if (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK == type)
1861 * Sends a CREATE PATH message for a path to a peer, properly registrating
1862 * all used resources.
1864 * @param peer PeerInfo of the final peer for whom this path is being created.
1865 * @param p Path itself.
1866 * @param t Tunnel for which the path is created.
1869 send_create_path (struct MeshPeerInfo *peer, struct MeshPeerPath *p,
1870 struct MeshTunnel *t)
1872 struct GNUNET_PeerIdentity id;
1873 struct MeshPathInfo *path_info;
1874 struct MeshPeerInfo *neighbor;
1880 p = tree_get_path_to_peer (t->tree, peer->id);
1887 for (i = 0; i < p->length; i++)
1889 if (p->peers[i] == myid)
1892 if (i >= p->length - 1)
1898 GNUNET_PEER_resolve (p->peers[i + 1], &id);
1900 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
1901 path_info->path = p;
1903 neighbor = peer_info_get (&id);
1904 path_info->peer = neighbor;
1905 queue_add (path_info,
1906 GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE,
1907 sizeof (struct GNUNET_MESH_ManipulatePath) +
1908 (p->length * sizeof (struct GNUNET_PeerIdentity)),
1915 * Sends a DESTROY PATH message to free resources for a path in a tunnel
1917 * @param t Tunnel whose path to destroy.
1918 * @param destination Short ID of the peer to whom the path to destroy.
1921 send_destroy_path (struct MeshTunnel *t, GNUNET_PEER_Id destination)
1923 struct MeshPeerPath *p;
1926 p = tree_get_path_to_peer (t->tree, destination);
1932 size = sizeof (struct GNUNET_MESH_ManipulatePath);
1933 size += p->length * sizeof (struct GNUNET_PeerIdentity);
1935 struct GNUNET_MESH_ManipulatePath *msg;
1936 struct GNUNET_PeerIdentity *pi;
1940 msg = (struct GNUNET_MESH_ManipulatePath *) cbuf;
1941 msg->header.size = htons (size);
1942 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY);
1943 msg->tid = htonl (t->id.tid);
1944 pi = (struct GNUNET_PeerIdentity *) &msg[1];
1945 for (i = 0; i < p->length; i++)
1947 GNUNET_PEER_resolve (p->peers[i], &pi[i]);
1949 send_prebuilt_message (&msg->header, tree_get_first_hop (t->tree, destination), t);
1956 * Sends a PATH ACK message in reponse to a received PATH_CREATE directed to us.
1958 * @param t Tunnel which to confirm.
1961 send_path_ack (struct MeshTunnel *t)
1963 struct MeshTransmissionDescriptor *info;
1964 struct GNUNET_PeerIdentity id;
1965 GNUNET_PEER_Id peer;
1967 peer = tree_get_predecessor (t->tree);
1968 GNUNET_PEER_resolve (peer, &id);
1969 info = GNUNET_malloc (sizeof (struct MeshTransmissionDescriptor));
1970 info->origin = &t->id;
1971 info->peer = GNUNET_CONTAINER_multihashmap_get (peers, &id.hashPubKey);
1972 GNUNET_assert (NULL != info->peer);
1975 GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
1976 sizeof (struct GNUNET_MESH_PathACK),
1983 * Try to establish a new connection to this peer.
1984 * Use the best path for the given tunnel.
1985 * If the peer doesn't have any path to it yet, try to get one.
1986 * If the peer already has some path, send a CREATE PATH towards it.
1988 * @param peer PeerInfo of the peer.
1989 * @param t Tunnel for which to create the path, if possible.
1992 peer_info_connect (struct MeshPeerInfo *peer, struct MeshTunnel *t)
1994 struct MeshPeerPath *p;
1995 struct MeshPathInfo *path_info;
1997 if (NULL != peer->path_head)
1999 p = tree_get_path_to_peer (t->tree, peer->id);
2006 // FIXME always send create path to self
2009 send_create_path (peer, p, t);
2013 struct GNUNET_HashCode hash;
2016 send_client_peer_connected (t, myid);
2017 t->local_tid_dest = next_local_tid++;
2018 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber),
2021 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
2022 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
2029 else if (NULL == peer->dhtget)
2031 struct GNUNET_PeerIdentity id;
2033 GNUNET_PEER_resolve (peer->id, &id);
2034 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2035 path_info->peer = peer;
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 " Starting DHT GET for peer %s\n", GNUNET_i2s (&id));
2039 peer->dhtgetcls = path_info;
2040 peer->dhtget = GNUNET_DHT_get_start (dht_handle, /* handle */
2041 GNUNET_BLOCK_TYPE_MESH_PEER, /* type */
2042 &id.hashPubKey, /* key to search */
2043 dht_replication_level, /* replication level */
2044 GNUNET_DHT_RO_RECORD_ROUTE |
2045 GNUNET_DHT_RO_DEMULTIPLEX_EVERYWHERE,
2046 NULL, /* xquery */ // FIXME BLOOMFILTER
2047 0, /* xquery bits */ // FIXME BLOOMFILTER SIZE
2048 &dht_get_id_handler, path_info);
2050 /* Otherwise, there is no path but the DHT get is already started. */
2055 * Task to delay the connection of a peer
2057 * @param cls Closure (path info with tunnel and peer to connect).
2058 * Will be free'd on exection.
2059 * @param tc TaskContext
2062 peer_info_connect_task (void *cls,
2063 const struct GNUNET_SCHEDULER_TaskContext *tc)
2065 struct MeshPathInfo *path_info = cls;
2067 path_info->peer->connect_task = GNUNET_SCHEDULER_NO_TASK;
2069 if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
2074 peer_info_connect (path_info->peer, path_info->t);
2080 * Destroy the peer_info and free any allocated resources linked to it
2082 * @param pi The peer_info to destroy.
2084 * @return GNUNET_OK on success
2087 peer_info_destroy (struct MeshPeerInfo *pi)
2089 struct GNUNET_PeerIdentity id;
2090 struct MeshPeerPath *p;
2091 struct MeshPeerPath *nextp;
2093 GNUNET_PEER_resolve (pi->id, &id);
2094 GNUNET_PEER_change_rc (pi->id, -1);
2097 GNUNET_CONTAINER_multihashmap_remove (peers, &id.hashPubKey, pi))
2100 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2101 "removing peer %s, not in hashmap\n", GNUNET_i2s (&id));
2103 if (NULL != pi->dhtget)
2105 GNUNET_DHT_get_stop (pi->dhtget);
2106 GNUNET_free (pi->dhtgetcls);
2112 GNUNET_CONTAINER_DLL_remove (pi->path_head, pi->path_tail, p);
2116 if (GNUNET_SCHEDULER_NO_TASK != pi->connect_task)
2118 GNUNET_free (GNUNET_SCHEDULER_cancel (pi->connect_task));
2126 * Remove all paths that rely on a direct connection between p1 and p2
2127 * from the peer itself and notify all tunnels about it.
2129 * @param peer PeerInfo of affected peer.
2130 * @param p1 GNUNET_PEER_Id of one peer.
2131 * @param p2 GNUNET_PEER_Id of another peer that was connected to the first and
2134 * TODO: optimize (see below)
2137 peer_info_remove_path (struct MeshPeerInfo *peer, GNUNET_PEER_Id p1,
2140 struct MeshPeerPath *p;
2141 struct MeshPeerPath *aux;
2142 struct MeshPeerInfo *peer_d;
2144 unsigned int destroyed;
2149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path\n");
2151 p = peer->path_head;
2155 for (i = 0; i < (p->length - 1); i++)
2157 if ((p->peers[i] == p1 && p->peers[i + 1] == p2) ||
2158 (p->peers[i] == p2 && p->peers[i + 1] == p1))
2160 GNUNET_CONTAINER_DLL_remove (peer->path_head, peer->path_tail, p);
2171 for (i = 0; i < peer->ntunnels; i++)
2173 d = tunnel_notify_connection_broken (peer->tunnels[i], p1, p2);
2177 * Problem: one or more peers have been deleted from the tunnel tree.
2178 * We don't know who they are to try to add them again.
2179 * We need to try to find a new path for each of the disconnected peers.
2180 * Some of them might already have a path to reach them that does not
2181 * involve p1 and p2. Adding all anew might render in a better tree than
2182 * the trivial immediate fix.
2184 * Trivial immiediate fix: try to reconnect to the disconnected node. All
2185 * its children will be reachable trough him.
2187 peer_d = peer_info_get_short (d);
2190 for (p = peer_d->path_head; NULL != p; p = p->next)
2192 if ((cost = tree_get_path_cost (peer->tunnels[i]->tree, p)) < best)
2200 /* No callback, as peer will be already disconnected and a connection
2201 * scheduled by tunnel_notify_connection_broken.
2203 tree_add_path (peer->tunnels[i]->tree, aux, NULL, NULL);
2207 peer_info_connect (peer_d, peer->tunnels[i]);
2210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "peer_info_remove_path END\n");
2215 * Add the path to the peer and update the path used to reach it in case this
2218 * @param peer_info Destination peer to add the path to.
2219 * @param path New path to add. Last peer must be the peer in arg 1.
2220 * Path will be either used of freed if already known.
2221 * @param trusted Do we trust that this path is real?
2224 peer_info_add_path (struct MeshPeerInfo *peer_info, struct MeshPeerPath *path,
2227 struct MeshPeerPath *aux;
2231 if ((NULL == peer_info) || (NULL == path))
2234 path_destroy (path);
2237 if (path->peers[path->length - 1] != peer_info->id)
2240 path_destroy (path);
2243 if (path->length <= 2 && GNUNET_NO == trusted)
2245 /* Only allow CORE to tell us about direct paths */
2246 path_destroy (path);
2249 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2250 for (l = 1; l < path->length; l++)
2252 if (path->peers[l] == myid)
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shortening path by %u\n", l);
2255 for (l2 = 0; l2 < path->length - l; l2++)
2257 path->peers[l2] = path->peers[l + l2];
2262 GNUNET_realloc (path->peers, path->length * sizeof (GNUNET_PEER_Id));
2267 struct GNUNET_PeerIdentity id;
2269 GNUNET_PEER_resolve (peer_info->id, &id);
2270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "adding path [%u] to peer %s\n",
2271 path->length, GNUNET_i2s (&id));
2274 l = path_get_length (path);
2281 GNUNET_assert (peer_info->id == path->peers[path->length - 1]);
2282 for (aux = peer_info->path_head; aux != NULL; aux = aux->next)
2284 l2 = path_get_length (aux);
2287 GNUNET_CONTAINER_DLL_insert_before (peer_info->path_head,
2288 peer_info->path_tail, aux, path);
2293 if (l2 == l && memcmp (path->peers, aux->peers, l) == 0)
2295 path_destroy (path);
2300 GNUNET_CONTAINER_DLL_insert_tail (peer_info->path_head, peer_info->path_tail,
2307 * Add the path to the origin peer and update the path used to reach it in case
2308 * this is the shortest.
2309 * The path is given in peer_info -> destination, therefore we turn the path
2310 * upside down first.
2312 * @param peer_info Peer to add the path to, being the origin of the path.
2313 * @param path New path to add after being inversed.
2314 * @param trusted Do we trust that this path is real?
2317 peer_info_add_path_to_origin (struct MeshPeerInfo *peer_info,
2318 struct MeshPeerPath *path, int trusted)
2321 peer_info_add_path (peer_info, path, trusted);
2326 * Function called if the connection to the peer has been stalled for a while,
2327 * possibly due to a missed ACK. Poll the peer about its ACK status.
2329 * @param cls Closure (cinfo).
2330 * @param tc TaskContext.
2333 tunnel_poll (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2335 struct MeshTunnelChildInfo *cinfo = cls;
2336 struct GNUNET_MESH_Poll msg;
2337 struct GNUNET_PeerIdentity id;
2338 struct MeshTunnel *t;
2340 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2341 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
2347 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_POLL);
2348 msg.header.size = htons (sizeof (msg));
2349 msg.tid = htonl (t->id.tid);
2350 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2351 msg.last_ack = htonl (cinfo->fwd_ack);
2353 GNUNET_PEER_resolve (cinfo->id, &id);
2354 send_prebuilt_message (&msg.header, &id, cinfo->t);
2355 cinfo->fc_poll_time = GNUNET_TIME_relative_min (
2357 GNUNET_TIME_relative_multiply (cinfo->fc_poll_time, 2));
2358 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
2359 &tunnel_poll, cinfo);
2364 * Build a PeerPath from the paths returned from the DHT, reversing the paths
2365 * to obtain a local peer -> destination path and interning the peer ids.
2367 * @return Newly allocated and created path
2369 static struct MeshPeerPath *
2370 path_build_from_dht (const struct GNUNET_PeerIdentity *get_path,
2371 unsigned int get_path_length,
2372 const struct GNUNET_PeerIdentity *put_path,
2373 unsigned int put_path_length)
2375 struct MeshPeerPath *p;
2381 GNUNET_PEER_change_rc (myid, 1);
2382 i = get_path_length;
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " GET has %d hops.\n", i);
2384 for (i--; i >= 0; i--)
2386 id = GNUNET_PEER_intern (&get_path[i]);
2387 if (p->length > 0 && id == p->peers[p->length - 1])
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2390 GNUNET_PEER_change_rc (id, -1);
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from GET: %s.\n",
2395 GNUNET_i2s (&get_path[i]));
2397 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2398 p->peers[p->length - 1] = id;
2401 i = put_path_length;
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " PUT has %d hops.\n", i);
2403 for (i--; i >= 0; i--)
2405 id = GNUNET_PEER_intern (&put_path[i]);
2408 /* PUT path went through us, so discard the path up until now and start
2409 * from here to get a much shorter (and loop-free) path.
2414 if (p->length > 0 && id == p->peers[p->length - 1])
2416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Optimizing 1 hop out.\n");
2417 GNUNET_PEER_change_rc (id, -1);
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Adding from PUT: %s.\n",
2422 GNUNET_i2s (&put_path[i]));
2424 p->peers = GNUNET_realloc (p->peers, sizeof (GNUNET_PEER_Id) * p->length);
2425 p->peers[p->length - 1] = id;
2429 if (get_path_length > 0)
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of GET: %s)\n",
2431 GNUNET_i2s (&get_path[0]));
2432 if (put_path_length > 0)
2433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (first of PUT: %s)\n",
2434 GNUNET_i2s (&put_path[0]));
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " In total: %d hops\n",
2437 for (i = 0; i < p->length; i++)
2439 struct GNUNET_PeerIdentity peer_id;
2441 GNUNET_PEER_resolve (p->peers[i], &peer_id);
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " %u: %s\n", p->peers[i],
2443 GNUNET_i2s (&peer_id));
2451 * Adds a path to the peer_infos of all the peers in the path
2453 * @param p Path to process.
2454 * @param confirmed Whether we know if the path works or not.
2457 path_add_to_peers (struct MeshPeerPath *p, int confirmed)
2461 /* TODO: invert and add */
2462 for (i = 0; i < p->length && p->peers[i] != myid; i++) /* skip'em */ ;
2463 for (i++; i < p->length; i++)
2465 struct MeshPeerInfo *aux;
2466 struct MeshPeerPath *copy;
2468 aux = peer_info_get_short (p->peers[i]);
2469 copy = path_duplicate (p);
2470 copy->length = i + 1;
2471 peer_info_add_path (aux, copy, GNUNET_NO);
2477 * Send keepalive packets for a peer
2479 * @param cls Closure (tunnel for which to send the keepalive).
2480 * @param tc Notification context.
2482 * TODO: implement explicit multicast keepalive?
2485 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
2489 * Search for a tunnel among the incoming tunnels
2491 * @param tid the local id of the tunnel
2493 * @return tunnel handler, NULL if doesn't exist
2495 static struct MeshTunnel *
2496 tunnel_get_incoming (MESH_TunnelNumber tid)
2498 struct GNUNET_HashCode hash;
2500 GNUNET_assert (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV);
2501 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2502 return GNUNET_CONTAINER_multihashmap_get (incoming_tunnels, &hash);
2507 * Search for a tunnel among the tunnels for a client
2509 * @param c the client whose tunnels to search in
2510 * @param tid the local id of the tunnel
2512 * @return tunnel handler, NULL if doesn't exist
2514 static struct MeshTunnel *
2515 tunnel_get_by_local_id (struct MeshClient *c, MESH_TunnelNumber tid)
2517 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
2519 return tunnel_get_incoming (tid);
2523 struct GNUNET_HashCode hash;
2525 GNUNET_CRYPTO_hash (&tid, sizeof (MESH_TunnelNumber), &hash);
2526 return GNUNET_CONTAINER_multihashmap_get (c->own_tunnels, &hash);
2532 * Search for a tunnel by global ID using PEER_ID
2534 * @param pi owner of the tunnel
2535 * @param tid global tunnel number
2537 * @return tunnel handler, NULL if doesn't exist
2539 static struct MeshTunnel *
2540 tunnel_get_by_pi (GNUNET_PEER_Id pi, MESH_TunnelNumber tid)
2542 struct MESH_TunnelID id;
2543 struct GNUNET_HashCode hash;
2548 GNUNET_CRYPTO_hash (&id, sizeof (struct MESH_TunnelID), &hash);
2549 return GNUNET_CONTAINER_multihashmap_get (tunnels, &hash);
2554 * Search for a tunnel by global ID using full PeerIdentities
2556 * @param oid owner of the tunnel
2557 * @param tid global tunnel number
2559 * @return tunnel handler, NULL if doesn't exist
2561 static struct MeshTunnel *
2562 tunnel_get (const struct GNUNET_PeerIdentity *oid, MESH_TunnelNumber tid)
2564 return tunnel_get_by_pi (GNUNET_PEER_search (oid), tid);
2569 * Delete an active client from the tunnel.
2575 tunnel_delete_active_client (struct MeshTunnel *t, const struct MeshClient *c)
2579 for (i = 0; i < t->nclients; i++)
2581 if (t->clients[i] == c)
2583 t->clients[i] = t->clients[t->nclients - 1];
2584 t->clients_fc[i] = t->clients_fc[t->nclients - 1];
2585 GNUNET_array_grow (t->clients, t->nclients, t->nclients - 1);
2587 GNUNET_array_grow (t->clients_fc, t->nclients, t->nclients - 1);
2595 * Delete an ignored client from the tunnel.
2601 tunnel_delete_ignored_client (struct MeshTunnel *t, const struct MeshClient *c)
2605 for (i = 0; i < t->nignore; i++)
2607 if (t->ignore[i] == c)
2609 t->ignore[i] = t->ignore[t->nignore - 1];
2610 GNUNET_array_grow (t->ignore, t->nignore, t->nignore - 1);
2618 * Delete a client from the tunnel. It should be only done on
2619 * client disconnection, otherwise use client_ignore_tunnel.
2625 tunnel_delete_client (struct MeshTunnel *t, const struct MeshClient *c)
2627 tunnel_delete_ignored_client (t, c);
2628 tunnel_delete_active_client (t, c);
2633 * @brief Iterator to destroy MeshTunnelChildInfo of tunnel children.
2635 * Destroys queue elements of all waiting transmissions and frees all memory
2636 * used by the struct and its elements.
2638 * @param cls Closure (tunnel info).
2639 * @param key Hash of GNUNET_PEER_Id (unused).
2640 * @param value MeshTunnelChildInfo of the child.
2642 * @return always GNUNET_YES, to keep iterating
2645 tunnel_destroy_child (void *cls,
2646 const struct GNUNET_HashCode * key,
2649 struct MeshTunnelChildInfo *cinfo = value;
2650 struct MeshTunnel *t = cls;
2651 struct MeshPeerQueue *q;
2655 for (c = 0; c < cinfo->send_buffer_n; c++)
2657 i = (cinfo->send_buffer_start + c) % t->fwd_queue_max;
2658 q = cinfo->send_buffer[i];
2659 cinfo->send_buffer[i] = NULL;
2661 queue_destroy (q, GNUNET_YES);
2664 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "%u %u\n", c, cinfo->send_buffer_n);
2666 GNUNET_free_non_null (cinfo->send_buffer);
2667 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
2669 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
2670 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
2672 GNUNET_free (cinfo);
2678 * Callback used to notify a client owner of a tunnel that a peer has
2679 * disconnected, most likely because of a path change.
2681 * @param cls Closure (tunnel this notification is about).
2682 * @param peer_id Short ID of disconnected peer.
2685 tunnel_notify_client_peer_disconnected (void *cls, GNUNET_PEER_Id peer_id)
2687 struct MeshTunnel *t = cls;
2688 struct MeshPeerInfo *peer;
2689 struct MeshPathInfo *path_info;
2691 client_notify_peer_disconnected (t->owner, t, peer_id);
2693 peer = peer_info_get_short (peer_id);
2694 path_info = GNUNET_malloc (sizeof (struct MeshPathInfo));
2695 path_info->peer = peer;
2697 peer->connect_task = GNUNET_SCHEDULER_add_now (&peer_info_connect_task,
2703 * Add a peer to a tunnel, accomodating paths accordingly and initializing all
2704 * needed rescources.
2705 * If peer already exists, reevaluate shortest path and change if different.
2707 * @param t Tunnel we want to add a new peer to
2708 * @param peer PeerInfo of the peer being added
2712 tunnel_add_peer (struct MeshTunnel *t, struct MeshPeerInfo *peer)
2714 struct MeshPeerPath *best_p;
2715 struct MeshPeerPath *p;
2716 unsigned int best_cost;
2719 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer\n");
2721 if (t->peer != peer->id)
2723 GNUNET_array_append (peer->tunnels, peer->ntunnels, t);
2727 if (NULL != (p = peer->path_head))
2730 best_cost = tree_get_path_cost (t->tree, p);
2733 if ((cost = tree_get_path_cost (t->tree, p)) < best_cost)
2740 tree_add_path (t->tree, best_p, &tunnel_notify_client_peer_disconnected, t);
2741 if (GNUNET_SCHEDULER_NO_TASK == t->path_refresh_task)
2742 t->path_refresh_task =
2743 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
2747 /* Start a DHT get */
2748 peer_info_connect (peer, t);
2750 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_peer END\n");
2754 * Add a path to a tunnel which we don't own, just to remember the next hop.
2755 * If destination node was already in the tunnel, the first hop information
2756 * will be replaced with the new path.
2758 * @param t Tunnel we want to add a new peer to
2759 * @param p Path to add
2760 * @param own_pos Position of local node in path.
2764 tunnel_add_path (struct MeshTunnel *t, struct MeshPeerPath *p,
2765 unsigned int own_pos)
2767 struct GNUNET_PeerIdentity id;
2769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path\n");
2770 GNUNET_assert (0 != own_pos);
2771 tree_add_path (t->tree, p, NULL, NULL);
2772 if (own_pos < p->length - 1)
2774 GNUNET_PEER_resolve (p->peers[own_pos + 1], &id);
2775 tree_update_first_hops (t->tree, myid, &id);
2777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "tunnel_add_path END\n");
2781 * Add a client to a tunnel, initializing all needed data structures.
2783 * @param t Tunnel to which add the client.
2784 * @param c Client which to add to the tunnel.
2787 tunnel_add_client (struct MeshTunnel *t, struct MeshClient *c)
2789 struct MeshTunnelClientInfo clinfo;
2791 GNUNET_array_append (t->clients, t->nclients, c);
2792 clinfo.fwd_ack = t->fwd_pid + 1;
2793 clinfo.bck_ack = t->nobuffer ? 1 : INITIAL_WINDOW_SIZE - 1;
2794 clinfo.fwd_pid = t->fwd_pid;
2795 clinfo.bck_pid = (uint32_t) -1; // Expected next: 0
2797 GNUNET_array_append (t->clients_fc, t->nclients, clinfo);
2802 * Notifies a tunnel that a connection has broken that affects at least
2803 * some of its peers. Sends a notification towards the root of the tree.
2804 * In case the peer is the owner of the tree, notifies the client that owns
2805 * the tunnel and tries to reconnect.
2807 * @param t Tunnel affected.
2808 * @param p1 Peer that got disconnected from p2.
2809 * @param p2 Peer that got disconnected from p1.
2811 * @return Short ID of the peer disconnected (either p1 or p2).
2812 * 0 if the tunnel remained unaffected.
2814 static GNUNET_PEER_Id
2815 tunnel_notify_connection_broken (struct MeshTunnel *t, GNUNET_PEER_Id p1,
2821 tree_notify_connection_broken (t->tree, p1, p2,
2822 &tunnel_notify_client_peer_disconnected,
2824 if (myid != p1 && myid != p2)
2830 if (tree_get_predecessor (t->tree) != 0)
2832 /* We are the peer still connected, notify owner of the disconnection. */
2833 struct GNUNET_MESH_PathBroken msg;
2834 struct GNUNET_PeerIdentity neighbor;
2836 msg.header.size = htons (sizeof (msg));
2837 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN);
2838 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
2839 msg.tid = htonl (t->id.tid);
2840 msg.peer1 = my_full_id;
2841 GNUNET_PEER_resolve (pid, &msg.peer2);
2842 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &neighbor);
2843 send_prebuilt_message (&msg.header, &neighbor, t);
2851 * Get the Flow Control info of a client.
2853 * @param t Tunnel on which to look.
2854 * @param c Client whose ACK to get.
2856 * @return ACK value.
2858 static struct MeshTunnelClientInfo *
2859 tunnel_get_client_fc (struct MeshTunnel *t,
2860 struct MeshClient *c)
2864 for (i = 0; i < t->nclients; i++)
2866 if (t->clients[i] != c)
2868 return &t->clients_fc[i];
2871 return NULL; // avoid compiler / coverity complaints
2876 * Iterator to get the appropiate ACK value from all children nodes.
2878 * @param cls Closue (tunnel).
2879 * @param id Id of the child node.
2882 tunnel_get_child_fwd_ack (void *cls,
2885 struct GNUNET_PeerIdentity peer_id;
2886 struct MeshTunnelChildInfo *cinfo;
2887 struct MeshTunnelChildIteratorContext *ctx = cls;
2888 struct MeshTunnel *t = ctx->t;
2891 GNUNET_PEER_resolve (id, &peer_id);
2892 cinfo = tunnel_get_neighbor_fc (t, &peer_id);
2893 ack = cinfo->fwd_ack;
2896 if (GNUNET_NO == ctx->init)
2898 ctx->max_child_ack = ack;
2899 ctx->init = GNUNET_YES;
2903 ctx->max_child_ack = ctx->max_child_ack > ack ? ctx->max_child_ack : ack;
2909 * Get the maximum PID allowed to transmit to any
2910 * tunnel child of the local peer, depending on the tunnel
2911 * buffering/speed settings.
2915 * @return Maximum PID allowed (uint32 MAX), -1LL if node has no children.
2918 tunnel_get_children_fwd_ack (struct MeshTunnel *t)
2920 struct MeshTunnelChildIteratorContext ctx;
2922 ctx.max_child_ack = 0;
2924 ctx.init = GNUNET_NO;
2925 tree_iterate_children (t->tree, tunnel_get_child_fwd_ack, &ctx);
2927 if (0 == ctx.nchildren)
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2930 " tunnel has no children, no FWD ACK\n");
2934 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ctx.max_child_ack, t->fwd_pid))
2935 ctx.max_child_ack = t->fwd_pid + 1; // Might overflow, it's ok.
2937 return (int64_t) ctx.max_child_ack;
2942 * Set the FWD ACK value of a client in a particular tunnel.
2944 * @param t Tunnel affected.
2945 * @param c Client whose ACK to set.
2946 * @param ack ACK value.
2949 tunnel_set_client_fwd_ack (struct MeshTunnel *t,
2950 struct MeshClient *c,
2955 for (i = 0; i < t->nclients; i++)
2957 if (t->clients[i] != c)
2959 t->clients_fc[i].fwd_ack = ack;
2967 * Get the highest ACK value of all clients in a particular tunnel,
2968 * according to the buffering/speed settings.
2970 * @param t Tunnel on which to look.
2972 * @return Corresponding ACK value (max uint32_t).
2973 * If no clients are suscribed, -1LL.
2976 tunnel_get_clients_fwd_ack (struct MeshTunnel *t)
2981 if (0 == t->nclients)
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2984 " tunnel has no clients, no FWD ACK\n");
2988 for (ack = -1LL, i = 0; i < t->nclients; i++)
2991 GNUNET_YES == GMC_is_pid_bigger (ack, t->clients_fc[i].fwd_ack))
2993 ack = t->clients_fc[i].fwd_ack;
2997 if (GNUNET_YES == t->nobuffer && GMC_is_pid_bigger(ack, t->fwd_pid))
2998 ack = (uint32_t) t->fwd_pid + 1; // Might overflow, it's ok.
3000 return (uint32_t) ack;
3005 * Get the current fwd ack value for a tunnel, taking in account the tunnel
3006 * mode and the status of all children nodes.
3010 * @return Maximum PID allowed.
3013 tunnel_get_fwd_ack (struct MeshTunnel *t)
3017 uint32_t buffer_free;
3022 buffer_free = t->fwd_queue_max - t->fwd_queue_n;
3023 child_ack = tunnel_get_children_fwd_ack (t);
3024 client_ack = tunnel_get_clients_fwd_ack (t);
3025 if (GNUNET_YES == t->nobuffer)
3028 if (-1LL == child_ack)
3029 child_ack = client_ack;
3030 if (-1LL == child_ack)
3033 client_ack = child_ack = ack;
3038 ack = count + buffer_free; // Overflow? OK!
3040 if (-1LL == child_ack)
3042 // Node has no children, child_ack AND core buffer are irrelevant.
3043 if (-1LL == client_ack) // No children AND no clients? Not good!
3045 GNUNET_STATISTICS_update (stats, "# mesh acks with no target",
3049 return (uint32_t) client_ack;
3051 if (-1LL == client_ack)
3056 ack = GMC_max_pid ((uint32_t) child_ack, ack);
3057 ack = GMC_max_pid ((uint32_t) client_ack, ack);
3059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3060 "c %u, bf %u, ch %lld, cl %lld, ACK: %u\n",
3061 count, buffer_free, child_ack, client_ack, ack);
3067 * Build a local ACK message and send it to a local client.
3069 * @param t Tunnel on which to send the ACK.
3070 * @param c Client to whom send the ACK.
3071 * @param ack Value of the ACK.
3074 send_local_ack (struct MeshTunnel *t, struct MeshClient *c, uint32_t ack)
3076 struct GNUNET_MESH_LocalAck msg;
3078 msg.header.size = htons (sizeof (msg));
3079 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
3080 msg.tunnel_id = htonl (t->owner == c ? t->local_tid : t->local_tid_dest);
3081 msg.max_pid = htonl (ack);
3082 GNUNET_SERVER_notification_context_unicast(nc,
3089 * Build an ACK message and queue it to send to the given peer.
3091 * @param t Tunnel on which to send the ACK.
3092 * @param peer Peer to whom send the ACK.
3093 * @param ack Value of the ACK.
3096 send_ack (struct MeshTunnel *t, struct GNUNET_PeerIdentity *peer, uint32_t ack)
3098 struct GNUNET_MESH_ACK msg;
3100 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3101 msg.header.size = htons (sizeof (msg));
3102 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_ACK);
3103 msg.pid = htonl (ack);
3104 msg.tid = htonl (t->id.tid);
3106 send_prebuilt_message (&msg.header, peer, t);
3111 * Notify a the owner of a tunnel about how many more
3112 * payload packages will we accept on a given tunnel.
3114 * @param t Tunnel on which to send the ACK.
3117 tunnel_send_client_fwd_ack (struct MeshTunnel *t)
3121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3122 "Sending client FWD ACK on tunnel %X\n",
3125 ack = tunnel_get_fwd_ack (t);
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
3128 if (t->last_fwd_ack == ack)
3130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " same as last, not sending!\n");
3134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending!\n");
3135 t->last_fwd_ack = ack;
3136 send_local_ack (t, t->owner, ack);
3141 * Send an ACK informing the predecessor about the available buffer space.
3142 * In case there is no predecessor, inform the owning client.
3143 * If buffering is off, send only on behalf of children or self if endpoint.
3144 * If buffering is on, send when sent to children and buffer space is free.
3145 * Note that although the name is fwd_ack, the FWD mean forward *traffic*,
3146 * the ACK itself goes "back" (towards root).
3148 * @param t Tunnel on which to send the ACK.
3149 * @param type Type of message that triggered the ACK transmission.
3152 tunnel_send_fwd_ack (struct MeshTunnel *t, uint16_t type)
3154 struct GNUNET_PeerIdentity id;
3157 if (NULL != t->owner)
3159 tunnel_send_client_fwd_ack (t);
3162 /* Is it after unicast / multicast retransmission? */
3165 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
3166 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3168 "ACK due to FWD DATA retransmission\n");
3169 if (GNUNET_YES == t->nobuffer)
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, nobuffer\n");
3175 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3176 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3178 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3179 t->force_ack = GNUNET_YES;
3185 /* Check if we need to transmit the ACK */
3186 if (t->fwd_queue_max > t->fwd_queue_n * 4 &&
3187 GMC_is_pid_bigger(t->last_fwd_ack, t->fwd_pid) &&
3188 GNUNET_NO == t->force_ack)
3190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending ACK, buffer free\n");
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192 " t->qmax: %u, t->qn: %u\n",
3193 t->fwd_queue_max, t->fwd_queue_n);
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 " t->pid: %u, t->ack: %u\n",
3196 t->fwd_pid, t->last_fwd_ack);
3200 /* Ok, ACK might be necessary, what PID to ACK? */
3201 ack = tunnel_get_fwd_ack (t);
3203 /* If speed_min and not all children have ack'd, dont send yet */
3204 if (ack == t->last_fwd_ack && GNUNET_NO == t->force_ack)
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Not sending FWD ACK, not ready\n");
3210 t->last_fwd_ack = ack;
3211 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
3212 send_ack (t, &id, ack);
3214 t->force_ack = GNUNET_NO;
3219 * Iterator to send a child node a BCK ACK to allow him to send more
3222 * @param cls Closure (tunnel).
3223 * @param id Id of the child node.
3226 tunnel_send_child_bck_ack (void *cls,
3229 struct MeshTunnel *t = cls;
3230 struct MeshTunnelChildInfo *cinfo;
3231 struct GNUNET_PeerIdentity peer;
3234 GNUNET_PEER_resolve (id, &peer);
3235 cinfo = tunnel_get_neighbor_fc (t, &peer);
3236 ack = cinfo->bck_pid + t->bck_queue_max - t->bck_queue_n;
3238 if (cinfo->bck_ack == ack && GNUNET_NO == t->force_ack)
3240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3241 " Not sending ACK, not needed\n");
3244 cinfo->bck_ack = ack;
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3247 " Sending BCK ACK %u (last sent: %u)\n",
3248 ack, cinfo->bck_ack);
3249 send_ack (t, &peer, ack);
3254 * @brief Send BCK ACKs to clients to allow them more to_origin traffic
3256 * Iterates over all clients and sends BCK ACKs to the ones that need it.
3258 * FIXME fc: what happens if we have 2 clients but q_size is 1?
3259 * - implement a size 1 buffer in each client_fc AND children_fc
3260 * to hold at least 1 message per "child".
3261 * problem: violates no buffer policy
3262 * - ack 0 and make "children" poll for transmission slots
3263 * problem: big overhead, extra latency even in low traffic
3266 * @param t Tunnel on which to send the BCK ACKs.
3269 tunnel_send_clients_bck_ack (struct MeshTunnel *t)
3272 unsigned int tunnel_delta;
3274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Sending BCK ACK to clients\n");
3276 tunnel_delta = t->bck_queue_max - t->bck_queue_n;
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " tunnel delta: %u\n", tunnel_delta);
3279 /* Find client whom to allow to send to origin (with lowest buffer space) */
3280 for (i = 0; i < t->nclients; i++)
3282 struct MeshTunnelClientInfo *clinfo;
3285 clinfo = &t->clients_fc[i];
3286 delta = clinfo->bck_ack - clinfo->bck_pid;
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client %u delta: %u\n",
3288 t->clients[i]->id, delta);
3290 if ((GNUNET_NO == t->nobuffer && tunnel_delta > delta) ||
3291 (GNUNET_YES == t->nobuffer && 0 == delta))
3295 ack = clinfo->bck_pid;
3296 ack += t->nobuffer ? 1 : tunnel_delta;
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 " sending ack to client %u: %u\n",
3299 t->clients[i]->id, ack);
3300 send_local_ack (t, t->clients[i], ack);
3301 clinfo->bck_ack = ack;
3305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3306 " not sending ack to client %u (td %u, d %u)\n",
3307 t->clients[i]->id, tunnel_delta, delta);
3314 * Send an ACK informing the children nodes and destination clients about
3315 * the available buffer space.
3316 * If buffering is off, send only on behalf of root (can be self).
3317 * If buffering is on, send when sent to predecessor and buffer space is free.
3318 * Note that although the name is bck_ack, the BCK mean backwards *traffic*,
3319 * the ACK itself goes "forward" (towards children/clients).
3321 * @param t Tunnel on which to send the ACK.
3322 * @param type Type of message that triggered the ACK transmission.
3325 tunnel_send_bck_ack (struct MeshTunnel *t, uint16_t type)
3327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3328 "Sending BCK ACK on tunnel %u [%u] due to %s\n",
3329 t->id.oid, t->id.tid, GNUNET_MESH_DEBUG_M2S(type));
3330 /* Is it after data to_origin retransmission? */
3333 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
3334 if (GNUNET_YES == t->nobuffer)
3336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3337 " Not sending ACK, nobuffer\n");
3341 case GNUNET_MESSAGE_TYPE_MESH_ACK:
3342 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK:
3344 case GNUNET_MESSAGE_TYPE_MESH_POLL:
3345 t->force_ack = GNUNET_YES;
3351 tunnel_send_clients_bck_ack (t);
3352 tree_iterate_children (t->tree, &tunnel_send_child_bck_ack, t);
3353 t->force_ack = GNUNET_NO;
3358 * @brief Re-initiate traffic to this peer if necessary.
3360 * Check if there is traffic queued towards this peer
3361 * and the core transmit handle is NULL (traffic was stalled).
3362 * If so, call core tmt rdy.
3364 * @param cls Closure (unused)
3365 * @param peer_id Short ID of peer to which initiate traffic.
3368 peer_unlock_queue(void *cls, GNUNET_PEER_Id peer_id)
3370 struct MeshPeerInfo *peer;
3371 struct GNUNET_PeerIdentity id;
3372 struct MeshPeerQueue *q;
3375 peer = peer_info_get_short(peer_id);
3376 if (NULL != peer->core_transmit)
3379 q = queue_get_next(peer);
3382 /* Might br multicast traffic already sent to this particular peer but
3383 * not to other children in this tunnel.
3384 * This way t->queue_n would be > 0 but the queue of this particular peer
3390 GNUNET_PEER_resolve (peer->id, &id);
3391 peer->core_transmit =
3392 GNUNET_CORE_notify_transmit_ready(core_handle,
3395 GNUNET_TIME_UNIT_FOREVER_REL,
3405 * @brief Allow transmission of FWD traffic on this tunnel
3407 * Check if there is traffic queued towards any children
3408 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3410 * @param t Tunnel on which to unlock FWD traffic.
3413 tunnel_unlock_fwd_queues (struct MeshTunnel *t)
3415 if (0 == t->fwd_queue_n)
3418 tree_iterate_children (t->tree, &peer_unlock_queue, NULL);
3423 * @brief Allow transmission of BCK traffic on this tunnel
3425 * Check if there is traffic queued towards the root of the tree
3426 * and the core transmit handle is NULL, and if so, call core tmt rdy.
3428 * @param t Tunnel on which to unlock BCK traffic.
3431 tunnel_unlock_bck_queue (struct MeshTunnel *t)
3433 if (0 == t->bck_queue_n)
3436 peer_unlock_queue(NULL, tree_get_predecessor(t->tree));
3441 * Send a message to all peers in this tunnel that the tunnel is no longer
3444 * @param t The tunnel whose peers to notify.
3445 * @param parent ID of the parent, in case the tree is already destroyed.
3448 tunnel_send_destroy (struct MeshTunnel *t, GNUNET_PEER_Id parent)
3450 struct GNUNET_MESH_TunnelDestroy msg;
3451 struct GNUNET_PeerIdentity id;
3453 msg.header.size = htons (sizeof (msg));
3454 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY);
3455 GNUNET_PEER_resolve (t->id.oid, &msg.oid);
3456 msg.tid = htonl (t->id.tid);
3457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3458 " sending tunnel destroy for tunnel: %s [%X]\n",
3459 GNUNET_i2s (&msg.oid), t->id.tid);
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " sending multicast to child\n");
3462 /* FIXME tunnel_send_multicast (t, &msg.header); */
3465 parent = tree_get_predecessor (t->tree);
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " parent: %u\n", parent);
3470 GNUNET_PEER_resolve (parent, &id);
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3472 " sending back to %s\n",
3474 send_prebuilt_message (&msg.header, &id, t);
3479 * Cancel all transmissions towards a neighbor that belong to a certain tunnel.
3481 * @param cls Closure (Tunnel which to cancel).
3482 * @param neighbor_id Short ID of the neighbor to whom cancel the transmissions.
3485 tunnel_cancel_queues (void *cls, GNUNET_PEER_Id neighbor_id)
3487 struct MeshTunnel *t = cls;
3488 struct MeshPeerInfo *peer_info;
3489 struct MeshPeerQueue *pq;
3490 struct MeshPeerQueue *next;
3492 peer_info = peer_info_get_short (neighbor_id);
3493 for (pq = peer_info->queue_head; NULL != pq; pq = next)
3496 if (pq->tunnel == t)
3498 if (GNUNET_MESSAGE_TYPE_MESH_MULTICAST == pq->type ||
3499 GNUNET_MESSAGE_TYPE_MESH_UNICAST == pq->type ||
3500 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == pq->type)
3502 // Should have been removed on destroy children
3505 queue_destroy (pq, GNUNET_YES);
3508 if (NULL == peer_info->queue_head && NULL != peer_info->core_transmit)
3510 GNUNET_CORE_notify_transmit_ready_cancel(peer_info->core_transmit);
3511 peer_info->core_transmit = NULL;
3516 * Destroy the tunnel and free any allocated resources linked to it.
3518 * @param t the tunnel to destroy
3520 * @return GNUNET_OK on success
3523 tunnel_destroy (struct MeshTunnel *t)
3525 struct MeshClient *c;
3526 struct GNUNET_HashCode hash;
3537 struct GNUNET_PeerIdentity id;
3539 GNUNET_PEER_resolve (t->id.oid, &id);
3540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "destroying tunnel %s [%x]\n",
3541 GNUNET_i2s (&id), t->id.tid);
3543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
3547 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3548 if (GNUNET_YES != GNUNET_CONTAINER_multihashmap_remove (tunnels, &hash, t))
3556 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3558 GNUNET_CONTAINER_multihashmap_remove (c->own_tunnels, &hash, t))
3565 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
3566 for (i = 0; i < t->nclients; i++)
3570 GNUNET_CONTAINER_multihashmap_remove (c->incoming_tunnels, &hash, t))
3576 for (i = 0; i < t->nignore; i++)
3580 GNUNET_CONTAINER_multihashmap_remove (c->ignore_tunnels, &hash, t))
3587 (void) GNUNET_CONTAINER_multihashmap_remove (incoming_tunnels, &hash, t);
3588 GNUNET_free_non_null (t->clients);
3589 GNUNET_free_non_null (t->ignore);
3590 GNUNET_free_non_null (t->clients_fc);
3592 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
3593 &tunnel_destroy_child,
3595 GNUNET_CONTAINER_multihashmap_destroy (t->children_fc);
3596 t->children_fc = NULL;
3598 tree_iterate_children (t->tree, &tunnel_cancel_queues, t);
3599 tree_destroy (t->tree);
3601 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3602 GNUNET_SCHEDULER_cancel (t->timeout_task);
3603 if (GNUNET_SCHEDULER_NO_TASK != t->path_refresh_task)
3604 GNUNET_SCHEDULER_cancel (t->path_refresh_task);
3607 GNUNET_STATISTICS_update (stats, "# tunnels", -1, GNUNET_NO);
3612 #define TUNNEL_DESTROY_EMPTY_TIME GNUNET_TIME_UNIT_MILLISECONDS
3615 * Tunnel is empty: destroy it.
3617 * @param cls Closure (Tunnel).
3618 * @param tc TaskContext.
3621 tunnel_destroy_empty_delayed (void *cls,
3622 const struct GNUNET_SCHEDULER_TaskContext *tc)
3624 struct MeshTunnel *t = cls;
3626 t->delayed_destroy = GNUNET_SCHEDULER_NO_TASK;
3627 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3630 if (0 != t->nclients ||
3631 0 != tree_count_children (t->tree))
3636 struct GNUNET_PeerIdentity id;
3638 GNUNET_PEER_resolve (t->id.oid, &id);
3639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3640 "executing destruction of empty tunnel %s [%X]\n",
3641 GNUNET_i2s (&id), t->id.tid);
3645 tunnel_send_destroy (t, 0);
3646 if (0 == t->pending_messages)
3649 t->destroy = GNUNET_YES;
3654 * Schedule tunnel destruction if is empty and no new traffic comes in a time.
3656 * @param t Tunnel to destroy if empty.
3659 tunnel_destroy_empty (struct MeshTunnel *t)
3661 if (GNUNET_SCHEDULER_NO_TASK != t->delayed_destroy ||
3663 0 != tree_count_children (t->tree))
3665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3667 t->delayed_destroy, t->nclients, tree_count_children(t->tree));
3673 struct GNUNET_PeerIdentity id;
3675 GNUNET_PEER_resolve (t->id.oid, &id);
3676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3677 "scheduling destruction of empty tunnel %s [%X]\n",
3678 GNUNET_i2s (&id), t->id.tid);
3682 t->delayed_destroy =
3683 GNUNET_SCHEDULER_add_delayed (TUNNEL_DESTROY_EMPTY_TIME,
3684 &tunnel_destroy_empty_delayed,
3690 * Create a new tunnel
3692 * @param owner Who is the owner of the tunnel (short ID).
3693 * @param tid Tunnel Number of the tunnel.
3694 * @param client Clients that owns the tunnel, NULL for foreign tunnels.
3695 * @param local Tunnel Number for the tunnel, for the client point of view.
3697 * @return A new initialized tunnel. NULL on error.
3699 static struct MeshTunnel *
3700 tunnel_new (GNUNET_PEER_Id owner,
3701 MESH_TunnelNumber tid,
3702 struct MeshClient *client,
3703 MESH_TunnelNumber local)
3705 struct MeshTunnel *t;
3706 struct GNUNET_HashCode hash;
3708 if (n_tunnels >= max_tunnels && NULL == client)
3711 t = GNUNET_malloc (sizeof (struct MeshTunnel));
3714 t->fwd_queue_max = (max_msgs_queue / max_tunnels) + 1;
3715 t->bck_queue_max = t->fwd_queue_max;
3716 t->tree = tree_new (owner);
3718 t->fwd_pid = (uint32_t) -1; // Next (expected) = 0
3719 t->bck_pid = (uint32_t) -1; // Next (expected) = 0
3720 t->bck_ack = INITIAL_WINDOW_SIZE - 1;
3721 t->last_fwd_ack = INITIAL_WINDOW_SIZE - 1;
3722 t->local_tid = local;
3723 t->children_fc = GNUNET_CONTAINER_multihashmap_create (8, GNUNET_NO);
3725 GNUNET_STATISTICS_update (stats, "# tunnels", 1, GNUNET_NO);
3727 GNUNET_CRYPTO_hash (&t->id, sizeof (struct MESH_TunnelID), &hash);
3729 GNUNET_CONTAINER_multihashmap_put (tunnels, &hash, t,
3730 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3737 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3744 GNUNET_CRYPTO_hash (&t->local_tid, sizeof (MESH_TunnelNumber), &hash);
3746 GNUNET_CONTAINER_multihashmap_put (client->own_tunnels, &hash, t,
3747 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
3751 GNUNET_SERVER_receive_done (client->handle, GNUNET_SYSERR);
3760 * Callback when removing children from a tunnel tree. Notify owner.
3762 * @param cls Closure (tunnel).
3763 * @param peer_id Short ID of the peer deleted.
3766 tunnel_child_removed (void *cls, GNUNET_PEER_Id peer_id)
3768 struct MeshTunnel *t = cls;
3770 client_notify_peer_disconnected (t->owner, t, peer_id);
3774 * Removes an explicit path from a tunnel, freeing all intermediate nodes
3775 * that are no longer needed, as well as nodes of no longer reachable peers.
3776 * The tunnel itself is also destoyed if results in a remote empty tunnel.
3778 * @param t Tunnel from which to remove the path.
3779 * @param peer Short id of the peer which should be removed.
3782 tunnel_delete_peer (struct MeshTunnel *t, GNUNET_PEER_Id peer)
3786 r = tree_del_peer (t->tree, peer, &tunnel_child_removed, t);
3789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3790 "Tunnel %u [%u] has no more nodes\n",
3791 t->id.oid, t->id.tid);
3797 * tunnel_destroy_iterator: iterator for deleting each tunnel that belongs to a
3798 * client when the client disconnects. If the client is not the owner, the
3799 * owner will get notified if no more clients are in the tunnel and the client
3800 * get removed from the tunnel's list.
3802 * @param cls closure (client that is disconnecting)
3803 * @param key the hash of the local tunnel id (used to access the hashmap)
3804 * @param value the value stored at the key (tunnel to destroy)
3806 * @return GNUNET_OK, keep iterating.
3809 tunnel_destroy_iterator (void *cls,
3810 const struct GNUNET_HashCode * key,
3813 struct MeshTunnel *t = value;
3814 struct MeshClient *c = cls;
3816 send_client_tunnel_disconnect (t, c);
3819 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %u is destination.\n", c->id);
3820 tunnel_delete_client (t, c);
3821 client_delete_tunnel (c, t);
3822 tunnel_destroy_empty (t);
3825 tunnel_send_destroy (t, 0);
3827 t->destroy = GNUNET_YES;
3834 * Timeout function, destroys tunnel if called
3836 * @param cls Closure (tunnel to destroy).
3837 * @param tc TaskContext
3840 tunnel_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
3842 struct MeshTunnel *t = cls;
3843 struct GNUNET_PeerIdentity id;
3845 t->timeout_task = GNUNET_SCHEDULER_NO_TASK;
3846 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
3848 GNUNET_PEER_resolve(t->id.oid, &id);
3849 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3850 "Tunnel %s [%X] timed out. Destroying.\n",
3851 GNUNET_i2s(&id), t->id.tid);
3852 send_clients_tunnel_destroy (t);
3857 * Resets the tunnel timeout. Starts it if no timeout was running.
3859 * @param t Tunnel whose timeout to reset.
3861 * TODO use heap to improve efficiency of scheduler.
3864 tunnel_reset_timeout (struct MeshTunnel *t)
3866 if (GNUNET_SCHEDULER_NO_TASK != t->timeout_task)
3867 GNUNET_SCHEDULER_cancel (t->timeout_task);
3869 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
3870 (refresh_path_time, 4), &tunnel_timeout, t);
3874 /******************************************************************************/
3875 /**************** MESH NETWORK HANDLER HELPERS ***********************/
3876 /******************************************************************************/
3879 * Function to send a create path packet to a peer.
3881 * @param cls closure
3882 * @param size number of bytes available in buf
3883 * @param buf where the callee should write the message
3884 * @return number of bytes written to buf
3887 send_core_path_create (void *cls, size_t size, void *buf)
3889 struct MeshPathInfo *info = cls;
3890 struct GNUNET_MESH_ManipulatePath *msg;
3891 struct GNUNET_PeerIdentity *peer_ptr;
3892 struct MeshTunnel *t = info->t;
3893 struct MeshPeerPath *p = info->path;
3898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATE PATH sending...\n");
3900 sizeof (struct GNUNET_MESH_ManipulatePath) +
3901 p->length * sizeof (struct GNUNET_PeerIdentity);
3903 if (size < size_needed || NULL == buf)
3908 msg = (struct GNUNET_MESH_ManipulatePath *) buf;
3909 msg->header.size = htons (size_needed);
3910 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE);
3911 msg->tid = ntohl (t->id.tid);
3914 if (GNUNET_YES == t->nobuffer)
3915 opt |= MESH_TUNNEL_OPT_NOBUFFER;
3916 msg->opt = htonl(opt);
3919 peer_ptr = (struct GNUNET_PeerIdentity *) &msg[1];
3920 for (i = 0; i < p->length; i++)
3922 GNUNET_PEER_resolve (p->peers[i], peer_ptr++);
3928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3929 "CREATE PATH (%u bytes long) sent!\n", size_needed);
3935 * Fill the core buffer
3937 * @param cls closure (data itself)
3938 * @param size number of bytes available in buf
3939 * @param buf where the callee should write the message
3941 * @return number of bytes written to buf
3944 send_core_data_multicast (void *cls, size_t size, void *buf)
3946 struct MeshTransmissionDescriptor *info = cls;
3949 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Multicast callback.\n");
3950 GNUNET_assert (NULL != info);
3951 GNUNET_assert (NULL != info->peer);
3952 total_size = info->mesh_data->data_len;
3953 GNUNET_assert (total_size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
3955 if (total_size > size)
3960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " copying data...\n");
3961 memcpy (buf, info->mesh_data->data, total_size);
3964 struct GNUNET_MessageHeader *mh;
3967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type %s\n",
3968 GNUNET_MESH_DEBUG_M2S (ntohs (mh->type)));
3971 data_descriptor_decrement_rc (info->mesh_data);
3972 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "freeing info...\n");
3974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "return %u\n", total_size);
3980 * Creates a path ack message in buf and frees all unused resources.
3982 * @param cls closure (MeshTransmissionDescriptor)
3983 * @param size number of bytes available in buf
3984 * @param buf where the callee should write the message
3985 * @return number of bytes written to buf
3988 send_core_path_ack (void *cls, size_t size, void *buf)
3990 struct MeshTransmissionDescriptor *info = cls;
3991 struct GNUNET_MESH_PathACK *msg = buf;
3993 GNUNET_assert (NULL != info);
3994 if (sizeof (struct GNUNET_MESH_PathACK) > size)
3999 msg->header.size = htons (sizeof (struct GNUNET_MESH_PathACK));
4000 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_ACK);
4001 GNUNET_PEER_resolve (info->origin->oid, &msg->oid);
4002 msg->tid = htonl (info->origin->tid);
4003 msg->peer_id = my_full_id;
4006 /* TODO add signature */
4008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "PATH ACK sent!\n");
4009 return sizeof (struct GNUNET_MESH_PathACK);
4014 * Free a transmission that was already queued with all resources
4015 * associated to the request.
4017 * @param queue Queue handler to cancel.
4018 * @param clear_cls Is it necessary to free associated cls?
4021 queue_destroy (struct MeshPeerQueue *queue, int clear_cls)
4023 struct MeshTransmissionDescriptor *dd;
4024 struct MeshPathInfo *path_info;
4025 struct MeshTunnelChildInfo *cinfo;
4026 struct GNUNET_PeerIdentity id;
4030 if (GNUNET_YES == clear_cls)
4032 switch (queue->type)
4034 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4035 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " cancelling TUNNEL_DESTROY\n");
4036 GNUNET_break (GNUNET_YES == queue->tunnel->destroy);
4038 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4039 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4040 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4041 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4042 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4043 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4045 " prebuilt message\n");
4046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4048 GNUNET_MESH_DEBUG_M2S(queue->type));
4050 data_descriptor_decrement_rc (dd->mesh_data);
4052 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " type create path\n");
4054 path_info = queue->cls;
4055 path_destroy (path_info->path);
4059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4060 " type %s unknown!\n",
4061 GNUNET_MESH_DEBUG_M2S(queue->type));
4063 GNUNET_free_non_null (queue->cls);
4065 GNUNET_CONTAINER_DLL_remove (queue->peer->queue_head,
4066 queue->peer->queue_tail,
4069 /* Delete from child_fc in the appropiate tunnel */
4070 max = queue->tunnel->fwd_queue_max;
4071 GNUNET_PEER_resolve (queue->peer->id, &id);
4072 cinfo = tunnel_get_neighbor_fc (queue->tunnel, &id);
4075 for (i = 0; i < cinfo->send_buffer_n; i++)
4078 i2 = (cinfo->send_buffer_start + i) % max;
4079 if (cinfo->send_buffer[i2] == queue)
4081 /* Found corresponding entry in the send_buffer. Move all others back. */
4086 for (j = i, j2 = 0, j3 = 0; j < cinfo->send_buffer_n - 1; j++)
4088 j2 = (cinfo->send_buffer_start + j) % max;
4089 j3 = (cinfo->send_buffer_start + j + 1) % max;
4090 cinfo->send_buffer[j2] = cinfo->send_buffer[j3];
4093 cinfo->send_buffer[j3] = NULL;
4094 cinfo->send_buffer_n--;
4099 GNUNET_free (queue);
4104 * @brief Get the next transmittable message from the queue.
4106 * This will be the head, except in the case of being a data packet
4107 * not allowed by the destination peer.
4109 * @param peer Destination peer.
4111 * @return The next viable MeshPeerQueue element to send to that peer.
4112 * NULL when there are no transmittable messages.
4114 struct MeshPeerQueue *
4115 queue_get_next (const struct MeshPeerInfo *peer)
4117 struct MeshPeerQueue *q;
4118 struct MeshTunnel *t;
4119 struct MeshTransmissionDescriptor *info;
4120 struct MeshTunnelChildInfo *cinfo;
4121 struct GNUNET_MESH_Unicast *ucast;
4122 struct GNUNET_MESH_ToOrigin *to_orig;
4123 struct GNUNET_PeerIdentity id;
4127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* selecting message\n");
4128 for (q = peer->queue_head; NULL != q; q = q->next)
4132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4134 GNUNET_MESH_DEBUG_M2S(q->type));
4137 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4138 ucast = (struct GNUNET_MESH_Unicast *) info->mesh_data->data;
4139 pid = ntohl (ucast->pid);
4140 GNUNET_PEER_resolve (info->peer->id, &id);
4141 cinfo = tunnel_get_neighbor_fc(t, &id);
4142 ack = cinfo->fwd_ack;
4144 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4145 to_orig = (struct GNUNET_MESH_ToOrigin *) info->mesh_data->data;
4146 pid = ntohl (to_orig->pid);
4150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4155 "********* ACK: %u, PID: %u\n",
4157 if (GNUNET_NO == GMC_is_pid_bigger(pid, ack))
4159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4166 "********* NEXT!\n");
4169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4170 "********* nothing found\n");
4176 * Core callback to write a queued packet to core buffer
4178 * @param cls Closure (peer info).
4179 * @param size Number of bytes available in buf.
4180 * @param buf Where the to write the message.
4182 * @return number of bytes written to buf
4185 queue_send (void *cls, size_t size, void *buf)
4187 struct MeshPeerInfo *peer = cls;
4188 struct GNUNET_MessageHeader *msg;
4189 struct MeshPeerQueue *queue;
4190 struct MeshTunnel *t;
4191 struct MeshTunnelChildInfo *cinfo;
4192 struct GNUNET_PeerIdentity dst_id;
4195 peer->core_transmit = NULL;
4198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* Queue send\n");
4199 queue = queue_get_next (peer);
4201 /* Queue has no internal mesh traffic nor sendable payload */
4204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not ready, return\n");
4205 if (NULL == peer->queue_head)
4206 GNUNET_break (0); // Should've been canceled
4209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* not empty\n");
4211 GNUNET_PEER_resolve (peer->id, &dst_id);
4212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4213 "********* towards %s\n",
4214 GNUNET_i2s(&dst_id));
4215 /* Check if buffer size is enough for the message */
4216 if (queue->size > size)
4218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4219 "********* not enough room, reissue\n");
4220 peer->core_transmit =
4221 GNUNET_CORE_notify_transmit_ready (core_handle,
4224 GNUNET_TIME_UNIT_FOREVER_REL,
4231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* size ok\n");
4234 GNUNET_assert (0 < t->pending_messages);
4235 t->pending_messages--;
4236 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == queue->type)
4239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4240 "********* unicast: t->q (%u/%u)\n",
4241 t->fwd_queue_n, t->fwd_queue_max);
4243 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == queue->type)
4246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* to origin\n");
4250 switch (queue->type)
4253 case GNUNET_MESSAGE_TYPE_MESH_ACK:
4254 case GNUNET_MESSAGE_TYPE_MESH_POLL:
4255 case GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN:
4256 case GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY:
4257 case GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY:
4258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4259 "********* raw: %s\n",
4260 GNUNET_MESH_DEBUG_M2S (queue->type));
4262 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4263 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4264 data_size = send_core_data_raw (queue->cls, size, buf);
4265 msg = (struct GNUNET_MessageHeader *) buf;
4266 switch (ntohs (msg->type)) // Type of preconstructed message
4268 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4269 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4271 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4272 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
4278 case GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE:
4279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path create\n");
4280 data_size = send_core_path_create (queue->cls, size, buf);
4282 case GNUNET_MESSAGE_TYPE_MESH_PATH_ACK:
4283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path ack\n");
4284 data_size = send_core_path_ack (queue->cls, size, buf);
4286 case GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE:
4287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* path keepalive\n");
4288 data_size = send_core_data_multicast (queue->cls, size, buf);
4292 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4293 "********* type unknown: %u\n",
4297 switch (queue->type)
4299 case GNUNET_MESSAGE_TYPE_MESH_UNICAST:
4300 case GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN:
4301 case GNUNET_MESSAGE_TYPE_MESH_MULTICAST:
4302 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4303 if (cinfo->send_buffer[cinfo->send_buffer_start] != queue)
4306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4307 "at pos %u (%p) != %p\n",
4308 cinfo->send_buffer_start,
4309 cinfo->send_buffer[cinfo->send_buffer_start],
4312 if (cinfo->send_buffer_n > 0)
4314 cinfo->send_buffer[cinfo->send_buffer_start] = NULL;
4315 cinfo->send_buffer_n--;
4316 cinfo->send_buffer_start++;
4317 cinfo->send_buffer_start %= t->fwd_queue_max;
4328 /* Free queue, but cls was freed by send_core_* */
4329 queue_destroy (queue, GNUNET_NO);
4331 if (GNUNET_YES == t->destroy && 0 == t->pending_messages)
4333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* destroying tunnel!\n");
4337 /* If more data in queue, send next */
4338 queue = queue_get_next(peer);
4341 struct GNUNET_PeerIdentity id;
4343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* more data!\n");
4344 GNUNET_PEER_resolve (peer->id, &id);
4345 peer->core_transmit =
4346 GNUNET_CORE_notify_transmit_ready(core_handle,
4349 GNUNET_TIME_UNIT_FOREVER_REL,
4357 if (NULL != peer->queue_head)
4359 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
4360 "********* %s stalled\n",
4361 GNUNET_i2s(&my_full_id));
4363 cinfo = tunnel_get_neighbor_fc (t, &dst_id);
4364 // FIXME unify bck/fwd structures, bck does not have cinfo right now
4365 if (NULL != cinfo && GNUNET_SCHEDULER_NO_TASK == cinfo->fc_poll)
4367 cinfo->fc_poll = GNUNET_SCHEDULER_add_delayed (cinfo->fc_poll_time,
4368 &tunnel_poll, cinfo);
4372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "********* return %d\n", data_size);
4378 * @brief Queue and pass message to core when possible.
4380 * If type is payload (UNICAST, TO_ORIGIN, MULTICAST) checks for queue status
4381 * and accounts for it. In case the queue is full, the message is dropped and
4384 * Otherwise, message is treated as internal and allowed to go regardless of
4387 * @param cls Closure (@c type dependant). It will be used by queue_send to
4388 * build the message to be sent if not already prebuilt.
4389 * @param type Type of the message, 0 for a raw message.
4390 * @param size Size of the message.
4391 * @param dst Neighbor to send message to.
4392 * @param t Tunnel this message belongs to.
4395 queue_add (void *cls, uint16_t type, size_t size,
4396 struct MeshPeerInfo *dst, struct MeshTunnel *t)
4398 struct MeshPeerQueue *queue;
4399 struct MeshTunnelChildInfo *cinfo;
4400 struct GNUNET_PeerIdentity id;
4406 if (GNUNET_MESSAGE_TYPE_MESH_UNICAST == type ||
4407 GNUNET_MESSAGE_TYPE_MESH_MULTICAST == type)
4409 n = &t->fwd_queue_n;
4410 max = &t->fwd_queue_max;
4412 else if (GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN == type)
4414 n = &t->bck_queue_n;
4415 max = &t->bck_queue_max;
4422 GNUNET_STATISTICS_update(stats,
4423 "# messages dropped (buffer full)",
4425 return; // Drop message
4429 queue = GNUNET_malloc (sizeof (struct MeshPeerQueue));
4435 GNUNET_CONTAINER_DLL_insert_tail (dst->queue_head, dst->queue_tail, queue);
4436 GNUNET_PEER_resolve (dst->id, &id);
4437 if (NULL == dst->core_transmit)
4439 dst->core_transmit =
4440 GNUNET_CORE_notify_transmit_ready (core_handle,
4443 GNUNET_TIME_UNIT_FOREVER_REL,
4449 t->pending_messages++;
4450 if (NULL == n) // Is this internal mesh traffic?
4453 // It's payload, keep track of buffer per peer.
4454 cinfo = tunnel_get_neighbor_fc(t, &id);
4455 i = (cinfo->send_buffer_start + cinfo->send_buffer_n) % t->fwd_queue_max;
4456 if (NULL != cinfo->send_buffer[i])
4458 GNUNET_break (cinfo->send_buffer_n == t->fwd_queue_max); // aka i == start
4459 queue_destroy (cinfo->send_buffer[cinfo->send_buffer_start], GNUNET_YES);
4460 cinfo->send_buffer_start++;
4461 cinfo->send_buffer_start %= t->fwd_queue_max;
4465 cinfo->send_buffer_n++;
4467 cinfo->send_buffer[i] = queue;
4468 if (cinfo->send_buffer_n > t->fwd_queue_max)
4471 cinfo->send_buffer_n = t->fwd_queue_max;
4476 /******************************************************************************/
4477 /******************** MESH NETWORK HANDLERS **************************/
4478 /******************************************************************************/
4482 * Core handler for path creation
4484 * @param cls closure
4485 * @param message message
4486 * @param peer peer identity this notification is about
4488 * @return GNUNET_OK to keep the connection open,
4489 * GNUNET_SYSERR to close it (signal serious error)
4492 handle_mesh_path_create (void *cls, const struct GNUNET_PeerIdentity *peer,
4493 const struct GNUNET_MessageHeader *message)
4495 unsigned int own_pos;
4498 MESH_TunnelNumber tid;
4499 struct GNUNET_MESH_ManipulatePath *msg;
4500 struct GNUNET_PeerIdentity *pi;
4501 struct GNUNET_HashCode hash;
4502 struct MeshPeerPath *path;
4503 struct MeshPeerInfo *dest_peer_info;
4504 struct MeshPeerInfo *orig_peer_info;
4505 struct MeshTunnel *t;
4507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4508 "Received a path create msg [%s]\n",
4509 GNUNET_i2s (&my_full_id));
4510 size = ntohs (message->size);
4511 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4513 GNUNET_break_op (0);
4517 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4518 if (size % sizeof (struct GNUNET_PeerIdentity))
4520 GNUNET_break_op (0);
4523 size /= sizeof (struct GNUNET_PeerIdentity);
4526 GNUNET_break_op (0);
4529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4530 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4532 tid = ntohl (msg->tid);
4533 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4535 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi), tid);
4536 t = tunnel_get (pi, tid);
4537 if (NULL == t) // FIXME only for INCOMING tunnels?
4541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating tunnel\n");
4542 t = tunnel_new (GNUNET_PEER_intern (pi), tid, NULL, 0);
4545 // FIXME notify failure
4548 opt = ntohl (msg->opt);
4549 if (0 != (opt & MESH_TUNNEL_OPT_NOBUFFER))
4551 t->nobuffer = GNUNET_YES;
4552 t->last_fwd_ack = t->fwd_pid + 1;
4554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " nobuffer:%d\n", t->nobuffer);
4556 if (GNUNET_YES == t->nobuffer)
4558 t->bck_queue_max = 1;
4559 t->fwd_queue_max = 1;
4562 // FIXME only assign a local tid if a local client is interested (on demand)
4563 while (NULL != tunnel_get_incoming (next_local_tid))
4564 next_local_tid = (next_local_tid + 1) | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4565 t->local_tid_dest = next_local_tid++;
4566 next_local_tid = next_local_tid | GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
4569 tunnel_reset_timeout (t);
4570 GNUNET_CRYPTO_hash (&t->local_tid_dest, sizeof (MESH_TunnelNumber), &hash);
4572 GNUNET_CONTAINER_multihashmap_put (incoming_tunnels, &hash, t,
4573 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST))
4581 GNUNET_CONTAINER_multihashmap_get (peers, &pi[size - 1].hashPubKey);
4582 if (NULL == dest_peer_info)
4584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4585 " Creating PeerInfo for destination.\n");
4586 dest_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4587 dest_peer_info->id = GNUNET_PEER_intern (&pi[size - 1]);
4588 GNUNET_CONTAINER_multihashmap_put (peers, &pi[size - 1].hashPubKey,
4590 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4592 orig_peer_info = GNUNET_CONTAINER_multihashmap_get (peers, &pi->hashPubKey);
4593 if (NULL == orig_peer_info)
4595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4596 " Creating PeerInfo for origin.\n");
4597 orig_peer_info = GNUNET_malloc (sizeof (struct MeshPeerInfo));
4598 orig_peer_info->id = GNUNET_PEER_intern (pi);
4599 GNUNET_CONTAINER_multihashmap_put (peers, &pi->hashPubKey, orig_peer_info,
4600 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
4602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4603 path = path_new (size);
4605 for (i = 0; i < size; i++)
4607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4608 GNUNET_i2s (&pi[i]));
4609 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4610 if (path->peers[i] == myid)
4613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4616 /* cannot be self, must be 'not found' */
4617 /* create path: self not found in path through self */
4618 GNUNET_break_op (0);
4619 path_destroy (path);
4623 path_add_to_peers (path, GNUNET_NO);
4624 tunnel_add_path (t, path, own_pos);
4625 if (own_pos == size - 1)
4627 /* It is for us! Send ack. */
4628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
4629 peer_info_add_path_to_origin (orig_peer_info, path, GNUNET_NO);
4635 struct MeshPeerPath *path2;
4637 /* It's for somebody else! Retransmit. */
4638 path2 = path_duplicate (path);
4639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Retransmitting.\n");
4640 peer_info_add_path (dest_peer_info, path2, GNUNET_NO);
4641 path2 = path_duplicate (path);
4642 peer_info_add_path_to_origin (orig_peer_info, path2, GNUNET_NO);
4643 send_create_path (dest_peer_info, path, t);
4650 * Core handler for path destruction
4652 * @param cls closure
4653 * @param message message
4654 * @param peer peer identity this notification is about
4656 * @return GNUNET_OK to keep the connection open,
4657 * GNUNET_SYSERR to close it (signal serious error)
4660 handle_mesh_path_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4661 const struct GNUNET_MessageHeader *message)
4663 struct GNUNET_MESH_ManipulatePath *msg;
4664 struct GNUNET_PeerIdentity *pi;
4665 struct MeshPeerPath *path;
4666 struct MeshTunnel *t;
4667 unsigned int own_pos;
4671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4672 "Received a PATH DESTROY msg from %s\n", GNUNET_i2s (peer));
4673 size = ntohs (message->size);
4674 if (size < sizeof (struct GNUNET_MESH_ManipulatePath))
4676 GNUNET_break_op (0);
4680 size -= sizeof (struct GNUNET_MESH_ManipulatePath);
4681 if (size % sizeof (struct GNUNET_PeerIdentity))
4683 GNUNET_break_op (0);
4686 size /= sizeof (struct GNUNET_PeerIdentity);
4689 GNUNET_break_op (0);
4692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " path has %u hops.\n", size);
4694 msg = (struct GNUNET_MESH_ManipulatePath *) message;
4695 pi = (struct GNUNET_PeerIdentity *) &msg[1];
4696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4697 " path is for tunnel %s [%X].\n", GNUNET_i2s (pi),
4699 t = tunnel_get (pi, ntohl (msg->tid));
4702 /* TODO notify back: we don't know this tunnel */
4703 GNUNET_break_op (0);
4706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Creating path...\n");
4707 path = path_new (size);
4709 for (i = 0; i < size; i++)
4711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... adding %s\n",
4712 GNUNET_i2s (&pi[i]));
4713 path->peers[i] = GNUNET_PEER_intern (&pi[i]);
4714 if (path->peers[i] == myid)
4717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " Own position: %u\n", own_pos);
4718 if (own_pos < path->length - 1)
4719 send_prebuilt_message (message, &pi[own_pos + 1], t);
4721 send_client_tunnel_disconnect(t, NULL);
4723 tunnel_delete_peer (t, path->peers[path->length - 1]);
4724 path_destroy (path);
4730 * Core handler for notifications of broken paths
4732 * @param cls closure
4733 * @param message message
4734 * @param peer peer identity this notification is about
4736 * @return GNUNET_OK to keep the connection open,
4737 * GNUNET_SYSERR to close it (signal serious error)
4740 handle_mesh_path_broken (void *cls, const struct GNUNET_PeerIdentity *peer,
4741 const struct GNUNET_MessageHeader *message)
4743 struct GNUNET_MESH_PathBroken *msg;
4744 struct MeshTunnel *t;
4746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4747 "Received a PATH BROKEN msg from %s\n", GNUNET_i2s (peer));
4748 msg = (struct GNUNET_MESH_PathBroken *) message;
4749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4750 GNUNET_i2s (&msg->peer1));
4751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " regarding %s\n",
4752 GNUNET_i2s (&msg->peer2));
4753 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4756 GNUNET_break_op (0);
4759 tunnel_notify_connection_broken (t, GNUNET_PEER_search (&msg->peer1),
4760 GNUNET_PEER_search (&msg->peer2));
4767 * Core handler for tunnel destruction
4769 * @param cls closure
4770 * @param message message
4771 * @param peer peer identity this notification is about
4773 * @return GNUNET_OK to keep the connection open,
4774 * GNUNET_SYSERR to close it (signal serious error)
4777 handle_mesh_tunnel_destroy (void *cls, const struct GNUNET_PeerIdentity *peer,
4778 const struct GNUNET_MessageHeader *message)
4780 struct GNUNET_MESH_TunnelDestroy *msg;
4781 struct MeshTunnel *t;
4782 GNUNET_PEER_Id parent;
4785 msg = (struct GNUNET_MESH_TunnelDestroy *) message;
4786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4787 "Got a TUNNEL DESTROY packet from %s\n",
4789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4790 " for tunnel %s [%u]\n",
4791 GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4792 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4793 /* Check signature */
4796 /* Probably already got the message from another path,
4797 * destroyed the tunnel and retransmitted to children.
4800 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel",
4804 parent = tree_get_predecessor (t->tree);
4805 pid = GNUNET_PEER_search (peer);
4810 tree_del_peer (t->tree, pid, &tunnel_child_removed, t);
4811 nc = tree_count_children (t->tree);
4812 if (nc > 0 || NULL != t->owner || t->nclients > 0)
4814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4815 "still in use: %u cl, %u ch\n",
4820 if (t->local_tid_dest >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
4822 /* Tunnel was incoming, notify clients */
4823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "INCOMING TUNNEL %X %X\n",
4824 t->local_tid, t->local_tid_dest);
4825 send_clients_tunnel_destroy (t);
4827 tunnel_send_destroy (t, parent);
4828 t->destroy = GNUNET_YES;
4829 // TODO: add timeout to destroy the tunnel anyway
4835 * Core handler for mesh network traffic going from the origin to a peer
4837 * @param cls closure
4838 * @param peer peer identity this notification is about
4839 * @param message message
4840 * @return GNUNET_OK to keep the connection open,
4841 * GNUNET_SYSERR to close it (signal serious error)
4844 handle_mesh_data_unicast (void *cls, const struct GNUNET_PeerIdentity *peer,
4845 const struct GNUNET_MessageHeader *message)
4847 struct GNUNET_MESH_Unicast *msg;
4848 struct GNUNET_PeerIdentity *neighbor;
4849 struct MeshTunnelChildInfo *cinfo;
4850 struct MeshTunnel *t;
4851 GNUNET_PEER_Id dest_id;
4856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a unicast packet from %s\n",
4859 size = ntohs (message->size);
4861 sizeof (struct GNUNET_MESH_Unicast) +
4862 sizeof (struct GNUNET_MessageHeader))
4867 msg = (struct GNUNET_MESH_Unicast *) message;
4868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4869 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4871 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4874 /* TODO notify back: we don't know this tunnel */
4875 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4876 GNUNET_break_op (0);
4879 pid = ntohl (msg->pid);
4880 if (t->fwd_pid == pid)
4882 GNUNET_STATISTICS_update (stats, "# duplicate PID drops", 1, GNUNET_NO);
4883 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
4884 " Already seen pid %u, DROPPING!\n", pid);
4889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4890 " pid %u not seen yet, forwarding\n", pid);
4895 if (GMC_is_pid_bigger (pid, t->last_fwd_ack))
4897 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4898 GNUNET_break_op (0);
4899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4900 "Received PID %u, ACK %u\n",
4901 pid, t->last_fwd_ack);
4905 tunnel_reset_timeout (t);
4907 if (dest_id == myid)
4909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4910 " it's for us! sending to clients...\n");
4911 GNUNET_STATISTICS_update (stats, "# unicast received", 1, GNUNET_NO);
4912 send_subscribed_clients (message, &msg[1].header, t);
4913 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_UNICAST);
4916 ttl = ntohl (msg->ttl);
4917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ttl: %u\n", ttl);
4920 GNUNET_STATISTICS_update (stats, "# TTL drops", 1, GNUNET_NO);
4921 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " TTL is 0, DROPPING!\n");
4922 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
4925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4926 " not for us, retransmitting...\n");
4928 neighbor = tree_get_first_hop (t->tree, dest_id);
4929 cinfo = tunnel_get_neighbor_fc (t, neighbor);
4930 cinfo->fwd_pid = pid;
4931 GNUNET_CONTAINER_multihashmap_iterate (t->children_fc,
4934 if (GNUNET_YES == t->nobuffer &&
4935 GNUNET_YES == GMC_is_pid_bigger (pid, cinfo->fwd_ack))
4937 GNUNET_STATISTICS_update (stats, "# unsolicited unicast", 1, GNUNET_NO);
4938 GNUNET_log (GNUNET_ERROR_TYPE_INFO, " %u > %u\n", pid, cinfo->fwd_ack);
4939 GNUNET_break_op (0);
4942 send_prebuilt_message (message, neighbor, t);
4943 GNUNET_STATISTICS_update (stats, "# unicast forwarded", 1, GNUNET_NO);
4949 * Core handler for mesh network traffic toward the owner of a tunnel
4951 * @param cls closure
4952 * @param message message
4953 * @param peer peer identity this notification is about
4955 * @return GNUNET_OK to keep the connection open,
4956 * GNUNET_SYSERR to close it (signal serious error)
4959 handle_mesh_data_to_orig (void *cls, const struct GNUNET_PeerIdentity *peer,
4960 const struct GNUNET_MessageHeader *message)
4962 struct GNUNET_MESH_ToOrigin *msg;
4963 struct GNUNET_PeerIdentity id;
4964 struct MeshPeerInfo *peer_info;
4965 struct MeshTunnel *t;
4966 struct MeshTunnelChildInfo *cinfo;
4967 GNUNET_PEER_Id predecessor;
4971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a ToOrigin packet from %s\n",
4973 size = ntohs (message->size);
4974 if (size < sizeof (struct GNUNET_MESH_ToOrigin) + /* Payload must be */
4975 sizeof (struct GNUNET_MessageHeader)) /* at least a header */
4977 GNUNET_break_op (0);
4980 msg = (struct GNUNET_MESH_ToOrigin *) message;
4981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " of type %s\n",
4982 GNUNET_MESH_DEBUG_M2S (ntohs (msg[1].header.type)));
4983 t = tunnel_get (&msg->oid, ntohl (msg->tid));
4984 pid = ntohl (msg->pid);
4988 /* TODO notify that we dont know this tunnel (whom)? */
4989 GNUNET_STATISTICS_update (stats, "# data on unknown tunnel", 1, GNUNET_NO);
4990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4991 "Received to_origin with PID %u on unknown tunnel %s [%u]\n",
4992 pid, GNUNET_i2s (&msg->oid), ntohl (msg->tid));
4996 cinfo = tunnel_get_neighbor_fc(t, peer);
5003 if (cinfo->bck_pid == pid)
5005 /* already seen this packet, drop */
5006 GNUNET_STATISTICS_update (stats, "# duplicate PID drops BCK", 1, GNUNET_NO);
5007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5008 " Already seen pid %u, DROPPING!\n", pid);
5009 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5014 " pid %u not seen yet, forwarding\n", pid);
5015 cinfo->bck_pid = pid;
5017 if (NULL != t->owner)
5020 struct GNUNET_MESH_ToOrigin *copy;
5022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5023 " it's for us! sending to clients...\n");
5024 /* TODO signature verification */
5025 memcpy (cbuf, message, size);
5026 copy = (struct GNUNET_MESH_ToOrigin *) cbuf;
5027 copy->tid = htonl (t->local_tid);
5029 copy->pid = htonl (t->bck_pid);
5030 GNUNET_STATISTICS_update (stats, "# to origin received", 1, GNUNET_NO);
5031 GNUNET_SERVER_notification_context_unicast (nc, t->owner->handle,
5032 ©->header, GNUNET_NO);
5033 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN);
5036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5037 " not for us, retransmitting...\n");
5039 peer_info = peer_info_get (&msg->oid);
5040 if (NULL == peer_info)
5042 /* unknown origin of tunnel */
5046 predecessor = tree_get_predecessor (t->tree);
5047 if (0 == predecessor)
5049 if (GNUNET_YES == t->destroy)
5051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5052 "to orig received on a dying tunnel %s [%X]\n",
5053 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5056 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5057 "unknown to origin at %s\n",
5058 GNUNET_i2s (&my_full_id));
5059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5062 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5063 "for tunnel %s [%X]\n",
5064 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5065 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
5067 tree_debug (t->tree);
5070 GNUNET_PEER_resolve (predecessor, &id);
5071 send_prebuilt_message (message, &id, t);
5072 GNUNET_STATISTICS_update (stats, "# to origin forwarded", 1, GNUNET_NO);
5079 * Core handler for mesh network traffic point-to-point acks.
5081 * @param cls closure
5082 * @param message message
5083 * @param peer peer identity this notification is about
5085 * @return GNUNET_OK to keep the connection open,
5086 * GNUNET_SYSERR to close it (signal serious error)
5089 handle_mesh_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5090 const struct GNUNET_MessageHeader *message)
5092 struct GNUNET_MESH_ACK *msg;
5093 struct MeshTunnel *t;
5096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an ACK packet from %s!\n",
5098 msg = (struct GNUNET_MESH_ACK *) message;
5100 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5104 /* TODO notify that we dont know this tunnel (whom)? */
5105 GNUNET_STATISTICS_update (stats, "# ack on unknown tunnel", 1, GNUNET_NO);
5108 ack = ntohl (msg->pid);
5109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ACK %u\n", ack);
5111 /* Is this a forward or backward ACK? */
5112 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5114 struct MeshTunnelChildInfo *cinfo;
5117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " FWD ACK\n");
5118 cinfo = tunnel_get_neighbor_fc (t, peer);
5119 cinfo->fwd_ack = ack;
5120 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5121 tunnel_unlock_fwd_queues (t);
5122 if (GNUNET_SCHEDULER_NO_TASK != cinfo->fc_poll)
5124 GNUNET_SCHEDULER_cancel (cinfo->fc_poll);
5125 cinfo->fc_poll = GNUNET_SCHEDULER_NO_TASK;
5126 cinfo->fc_poll_time = GNUNET_TIME_UNIT_SECONDS;
5131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " BCK ACK\n");
5133 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_ACK);
5134 tunnel_unlock_bck_queue (t);
5141 * Core handler for mesh network traffic point-to-point ack polls.
5143 * @param cls closure
5144 * @param message message
5145 * @param peer peer identity this notification is about
5147 * @return GNUNET_OK to keep the connection open,
5148 * GNUNET_SYSERR to close it (signal serious error)
5151 handle_mesh_poll (void *cls, const struct GNUNET_PeerIdentity *peer,
5152 const struct GNUNET_MessageHeader *message)
5154 struct GNUNET_MESH_Poll *msg;
5155 struct MeshTunnel *t;
5157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got an POLL packet from %s!\n",
5160 msg = (struct GNUNET_MESH_Poll *) message;
5162 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5166 /* TODO notify that we dont know this tunnel (whom)? */
5167 GNUNET_STATISTICS_update (stats, "# poll on unknown tunnel", 1, GNUNET_NO);
5168 GNUNET_break_op (0);
5172 /* Is this a forward or backward ACK? */
5173 if (tree_get_predecessor(t->tree) != GNUNET_PEER_search(peer))
5175 struct MeshTunnelChildInfo *cinfo;
5177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from FWD\n");
5178 cinfo = tunnel_get_neighbor_fc (t, peer);
5179 cinfo->bck_ack = cinfo->fwd_pid; // mark as ready to send
5180 tunnel_send_bck_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " from BCK\n");
5185 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_POLL);
5192 * Core handler for path ACKs
5194 * @param cls closure
5195 * @param message message
5196 * @param peer peer identity this notification is about
5198 * @return GNUNET_OK to keep the connection open,
5199 * GNUNET_SYSERR to close it (signal serious error)
5202 handle_mesh_path_ack (void *cls, const struct GNUNET_PeerIdentity *peer,
5203 const struct GNUNET_MessageHeader *message)
5205 struct GNUNET_MESH_PathACK *msg;
5206 struct GNUNET_PeerIdentity id;
5207 struct MeshPeerInfo *peer_info;
5208 struct MeshPeerPath *p;
5209 struct MeshTunnel *t;
5211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received a path ACK msg [%s]\n",
5212 GNUNET_i2s (&my_full_id));
5213 msg = (struct GNUNET_MESH_PathACK *) message;
5214 t = tunnel_get (&msg->oid, ntohl(msg->tid));
5217 /* TODO notify that we don't know the tunnel */
5218 GNUNET_STATISTICS_update (stats, "# control on unknown tunnel", 1, GNUNET_NO);
5219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " don't know the tunnel %s [%X]!\n",
5220 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %s [%X]\n",
5224 GNUNET_i2s (&msg->oid), ntohl(msg->tid));
5226 peer_info = peer_info_get (&msg->peer_id);
5227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by peer %s\n",
5228 GNUNET_i2s (&msg->peer_id));
5229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " via peer %s\n",
5232 /* Add paths to peers? */
5233 p = tree_get_path_to_peer (t->tree, peer_info->id);
5236 path_add_to_peers (p, GNUNET_YES);
5244 /* Message for us? */
5245 if (0 == memcmp (&msg->oid, &my_full_id, sizeof (struct GNUNET_PeerIdentity)))
5247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " It's for us!\n");
5248 if (NULL == t->owner)
5250 GNUNET_break_op (0);
5253 if (tree_get_status (t->tree, peer_info->id) != MESH_PEER_READY)
5255 tree_set_status (t->tree, peer_info->id, MESH_PEER_READY);
5256 send_client_peer_connected (t, peer_info->id);
5258 if (NULL != peer_info->dhtget)
5260 GNUNET_DHT_get_stop (peer_info->dhtget);
5261 peer_info->dhtget = NULL;
5266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5267 " not for us, retransmitting...\n");
5268 GNUNET_PEER_resolve (tree_get_predecessor (t->tree), &id);
5269 peer_info = peer_info_get (&msg->oid);
5270 send_prebuilt_message (message, &id, t);
5276 * Core handler for mesh keepalives.
5278 * @param cls closure
5279 * @param message message
5280 * @param peer peer identity this notification is about
5281 * @return GNUNET_OK to keep the connection open,
5282 * GNUNET_SYSERR to close it (signal serious error)
5284 * TODO: Check who we got this from, to validate route.
5287 handle_mesh_keepalive (void *cls, const struct GNUNET_PeerIdentity *peer,
5288 const struct GNUNET_MessageHeader *message)
5290 struct GNUNET_MESH_TunnelKeepAlive *msg;
5291 struct MeshTunnel *t;
5293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "got a keepalive packet from %s\n",
5296 msg = (struct GNUNET_MESH_TunnelKeepAlive *) message;
5297 t = tunnel_get (&msg->oid, ntohl (msg->tid));
5301 /* TODO notify that we dont know that tunnel */
5302 GNUNET_STATISTICS_update (stats, "# keepalive on unknown tunnel", 1,
5307 tunnel_reset_timeout (t);
5309 GNUNET_STATISTICS_update (stats, "# keepalives forwarded", 1, GNUNET_NO);
5310 /* FIXME tunnel_send_multicast (t, message); */
5317 * Functions to handle messages from core
5319 static struct GNUNET_CORE_MessageHandler core_handlers[] = {
5320 {&handle_mesh_path_create, GNUNET_MESSAGE_TYPE_MESH_PATH_CREATE, 0},
5321 {&handle_mesh_path_destroy, GNUNET_MESSAGE_TYPE_MESH_PATH_DESTROY, 0},
5322 {&handle_mesh_path_broken, GNUNET_MESSAGE_TYPE_MESH_PATH_BROKEN,
5323 sizeof (struct GNUNET_MESH_PathBroken)},
5324 {&handle_mesh_tunnel_destroy, GNUNET_MESSAGE_TYPE_MESH_TUNNEL_DESTROY,
5325 sizeof (struct GNUNET_MESH_TunnelDestroy)},
5326 {&handle_mesh_data_unicast, GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
5327 {&handle_mesh_keepalive, GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE,
5328 sizeof (struct GNUNET_MESH_TunnelKeepAlive)},
5329 {&handle_mesh_data_to_orig, GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
5330 {&handle_mesh_ack, GNUNET_MESSAGE_TYPE_MESH_ACK,
5331 sizeof (struct GNUNET_MESH_ACK)},
5332 {&handle_mesh_poll, GNUNET_MESSAGE_TYPE_MESH_POLL,
5333 sizeof (struct GNUNET_MESH_Poll)},
5334 {&handle_mesh_path_ack, GNUNET_MESSAGE_TYPE_MESH_PATH_ACK,
5335 sizeof (struct GNUNET_MESH_PathACK)},
5341 /******************************************************************************/
5342 /**************** MESH LOCAL HANDLER HELPERS ***********************/
5343 /******************************************************************************/
5348 * notify_client_connection_failure: notify a client that the connection to the
5349 * requested remote peer is not possible (for instance, no route found)
5350 * Function called when the socket is ready to queue more data. "buf" will be
5351 * NULL and "size" zero if the socket was closed for writing in the meantime.
5353 * @param cls closure
5354 * @param size number of bytes available in buf
5355 * @param buf where the callee should write the message
5356 * @return number of bytes written to buf
5359 notify_client_connection_failure (void *cls, size_t size, void *buf)
5362 struct MeshPeerInfo *peer_info;
5363 struct GNUNET_MESH_PeerControl *msg;
5364 struct GNUNET_PeerIdentity id;
5366 if (0 == size && NULL == buf)
5368 // TODO retry? cancel?
5372 size_needed = sizeof (struct GNUNET_MESH_PeerControl);
5373 peer_info = (struct MeshPeerInfo *) cls;
5374 msg = (struct GNUNET_MESH_PeerControl *) buf;
5375 msg->header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
5376 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED);
5377 // msg->tunnel_id = htonl(peer_info->t->tid);
5378 GNUNET_PEER_resolve (peer_info->id, &id);
5379 memcpy (&msg->peer, &id, sizeof (struct GNUNET_PeerIdentity));
5387 * Send keepalive packets for a peer
5389 * @param cls Closure (tunnel for which to send the keepalive).
5390 * @param tc Notification context.
5393 path_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
5395 struct MeshTunnel *t = cls;
5396 struct GNUNET_MESH_TunnelKeepAlive *msg;
5397 size_t size = sizeof (struct GNUNET_MESH_TunnelKeepAlive);
5400 t->path_refresh_task = GNUNET_SCHEDULER_NO_TASK;
5401 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
5406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5407 "sending keepalive for tunnel %d\n", t->id.tid);
5409 msg = (struct GNUNET_MESH_TunnelKeepAlive *) cbuf;
5410 msg->header.size = htons (size);
5411 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_PATH_KEEPALIVE);
5412 msg->oid = my_full_id;
5413 msg->tid = htonl (t->id.tid);
5414 /* FIXME tunnel_send_multicast (t, &msg->header); */
5416 t->path_refresh_task =
5417 GNUNET_SCHEDULER_add_delayed (refresh_path_time, &path_refresh, t);
5418 tunnel_reset_timeout(t);
5423 * Function to process paths received for a new peer addition. The recorded
5424 * paths form the initial tunnel, which can be optimized later.
5425 * Called on each result obtained for the DHT search.
5427 * @param cls closure
5428 * @param exp when will this value expire
5429 * @param key key of the result
5430 * @param get_path path of the get request
5431 * @param get_path_length lenght of get_path
5432 * @param put_path path of the put request
5433 * @param put_path_length length of the put_path
5434 * @param type type of the result
5435 * @param size number of bytes in data
5436 * @param data pointer to the result data
5438 * TODO: re-issue the request after certain time? cancel after X results?
5441 dht_get_id_handler (void *cls, struct GNUNET_TIME_Absolute exp,
5442 const struct GNUNET_HashCode * key,
5443 const struct GNUNET_PeerIdentity *get_path,
5444 unsigned int get_path_length,
5445 const struct GNUNET_PeerIdentity *put_path,
5446 unsigned int put_path_length, enum GNUNET_BLOCK_Type type,
5447 size_t size, const void *data)
5449 struct MeshPathInfo *path_info = cls;
5450 struct MeshPeerPath *p;
5451 struct GNUNET_PeerIdentity pi;
5454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got results from DHT!\n");
5455 GNUNET_PEER_resolve (path_info->peer->id, &pi);
5456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " for %s\n", GNUNET_i2s (&pi));
5458 p = path_build_from_dht (get_path, get_path_length,
5459 put_path, put_path_length);
5460 path_add_to_peers (p, GNUNET_NO);
5462 for (i = 0; i < path_info->peer->ntunnels; i++)
5464 tunnel_add_peer (path_info->peer->tunnels[i], path_info->peer);
5465 peer_info_connect (path_info->peer, path_info->t);
5472 /******************************************************************************/
5473 /********************* MESH LOCAL HANDLES **************************/
5474 /******************************************************************************/
5478 * Handler for client disconnection
5480 * @param cls closure
5481 * @param client identification of the client; NULL
5482 * for the last call when the server is destroyed
5485 handle_local_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
5487 struct MeshClient *c;
5488 struct MeshClient *next;
5491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "client disconnected\n");
5494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " (SERVER DOWN)\n");
5501 if (c->handle != client)
5503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ... searching\n");
5507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "matching client found (%u)\n",
5509 GNUNET_SERVER_client_drop (c->handle);
5510 c->shutting_down = GNUNET_YES;
5511 GNUNET_assert (NULL != c->own_tunnels);
5512 GNUNET_assert (NULL != c->incoming_tunnels);
5513 GNUNET_CONTAINER_multihashmap_iterate (c->own_tunnels,
5514 &tunnel_destroy_iterator, c);
5515 GNUNET_CONTAINER_multihashmap_iterate (c->incoming_tunnels,
5516 &tunnel_destroy_iterator, c);
5517 GNUNET_CONTAINER_multihashmap_iterate (c->ignore_tunnels,
5518 &tunnel_destroy_iterator, c);
5519 GNUNET_CONTAINER_multihashmap_destroy (c->own_tunnels);
5520 GNUNET_CONTAINER_multihashmap_destroy (c->incoming_tunnels);
5521 GNUNET_CONTAINER_multihashmap_destroy (c->ignore_tunnels);
5523 if (NULL != c->types)
5524 GNUNET_CONTAINER_multihashmap_destroy (c->types);
5525 for (i = 0; i < c->n_regex; i++)
5527 GNUNET_free (c->regexes[i].regex);
5528 if (NULL != c->regexes[i].h)
5529 GNUNET_REGEX_announce_cancel (c->regexes[i].h);
5531 GNUNET_free_non_null (c->regexes);
5532 if (GNUNET_SCHEDULER_NO_TASK != c->regex_announce_task)
5533 GNUNET_SCHEDULER_cancel (c->regex_announce_task);
5535 GNUNET_CONTAINER_DLL_remove (clients, clients_tail, c);
5536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT FREE at %p\n", c);
5538 GNUNET_STATISTICS_update (stats, "# clients", -1, GNUNET_NO);
5541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " done!\n");
5547 * Handler for new clients
5549 * @param cls closure
5550 * @param client identification of the client
5551 * @param message the actual message, which includes messages the client wants
5554 handle_local_new_client (void *cls, struct GNUNET_SERVER_Client *client,
5555 const struct GNUNET_MessageHeader *message)
5557 struct GNUNET_MESH_ClientConnect *cc_msg;
5558 struct MeshClient *c;
5564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client connected\n");
5565 /* Check data sanity */
5566 size = ntohs (message->size) - sizeof (struct GNUNET_MESH_ClientConnect);
5567 cc_msg = (struct GNUNET_MESH_ClientConnect *) message;
5568 ntypes = ntohs (cc_msg->types);
5569 if (size != ntypes * sizeof (uint16_t))
5572 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5576 /* Create new client structure */
5577 c = GNUNET_malloc (sizeof (struct MeshClient));
5578 c->id = next_client_id++;
5579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " CLIENT NEW %u\n", c->id);
5581 GNUNET_SERVER_client_keep (client);
5585 struct GNUNET_HashCode hc;
5587 t = (uint16_t *) &cc_msg[1];
5588 c->types = GNUNET_CONTAINER_multihashmap_create (ntypes, GNUNET_NO);
5589 for (i = 0; i < ntypes; i++)
5592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " msg type: %u\n", u16);
5593 GNUNET_CRYPTO_hash (&u16, sizeof (u16), &hc);
5595 /* store in clients hashmap */
5596 GNUNET_CONTAINER_multihashmap_put (c->types, &hc, c,
5597 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5598 /* store in global hashmap */
5599 GNUNET_CONTAINER_multihashmap_put (types, &hc, c,
5600 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
5603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " client has %u types\n", ntypes);
5605 GNUNET_CONTAINER_DLL_insert (clients, clients_tail, c);
5606 c->own_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5607 c->incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5608 c->ignore_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
5609 GNUNET_SERVER_notification_context_add (nc, client);
5610 GNUNET_STATISTICS_update (stats, "# clients", 1, GNUNET_NO);
5612 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new client processed\n");
5618 * Handler for requests of new tunnels
5620 * @param cls closure
5621 * @param client identification of the client
5622 * @param message the actual message
5625 handle_local_tunnel_create (void *cls, struct GNUNET_SERVER_Client *client,
5626 const struct GNUNET_MessageHeader *message)
5628 struct GNUNET_MESH_TunnelMessage *t_msg;
5629 struct MeshTunnel *t;
5630 struct MeshClient *c;
5631 MESH_TunnelNumber tid;
5633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel requested\n");
5635 /* Sanity check for client registration */
5636 if (NULL == (c = client_get (client)))
5639 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5644 /* Message sanity check */
5645 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5648 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5652 t_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5653 /* Sanity check for tunnel numbering */
5654 tid = ntohl (t_msg->tunnel_id);
5655 if (0 == (tid & GNUNET_MESH_LOCAL_TUNNEL_ID_CLI))
5658 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5661 /* Sanity check for duplicate tunnel IDs */
5662 if (NULL != tunnel_get_by_local_id (c, tid))
5665 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5669 while (NULL != tunnel_get_by_pi (myid, next_tid))
5670 next_tid = (next_tid + 1) & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5671 t = tunnel_new (myid, next_tid++, c, tid);
5674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tunnel creation failed.\n");
5676 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5679 next_tid = next_tid & ~GNUNET_MESH_LOCAL_TUNNEL_ID_CLI;
5680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CREATED TUNNEL %s [%x] (%x)\n",
5681 GNUNET_i2s (&my_full_id), t->id.tid, t->local_tid);
5683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "new tunnel created\n");
5684 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5690 * Handler for requests of deleting tunnels
5692 * @param cls closure
5693 * @param client identification of the client
5694 * @param message the actual message
5697 handle_local_tunnel_destroy (void *cls, struct GNUNET_SERVER_Client *client,
5698 const struct GNUNET_MessageHeader *message)
5700 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5701 struct MeshClient *c;
5702 struct MeshTunnel *t;
5703 MESH_TunnelNumber tid;
5705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5706 "Got a DESTROY TUNNEL from client!\n");
5708 /* Sanity check for client registration */
5709 if (NULL == (c = client_get (client)))
5712 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5717 /* Message sanity check */
5718 if (sizeof (struct GNUNET_MESH_TunnelMessage) != ntohs (message->size))
5721 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5725 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5727 /* Retrieve tunnel */
5728 tid = ntohl (tunnel_msg->tunnel_id);
5729 t = tunnel_get_by_local_id(c, tid);
5732 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5734 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5737 if (c != t->owner || tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5739 client_ignore_tunnel (c, t);
5740 tunnel_destroy_empty (t);
5741 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5744 send_client_tunnel_disconnect (t, c);
5745 client_delete_tunnel (c, t);
5747 /* Don't try to ACK the client about the tunnel_destroy multicast packet */
5749 tunnel_send_destroy (t, 0);
5750 t->destroy = GNUNET_YES;
5751 /* The tunnel will be destroyed when the last message is transmitted. */
5752 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5758 * Handler for requests of seeting tunnel's buffering policy.
5760 * @param cls Closure (unused).
5761 * @param client Identification of the client.
5762 * @param message The actual message.
5765 handle_local_tunnel_buffer (void *cls, struct GNUNET_SERVER_Client *client,
5766 const struct GNUNET_MessageHeader *message)
5768 struct GNUNET_MESH_TunnelMessage *tunnel_msg;
5769 struct MeshClient *c;
5770 struct MeshTunnel *t;
5771 MESH_TunnelNumber tid;
5773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5774 "Got a BUFFER request from client!\n");
5776 /* Sanity check for client registration */
5777 if (NULL == (c = client_get (client)))
5780 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5785 tunnel_msg = (struct GNUNET_MESH_TunnelMessage *) message;
5787 /* Retrieve tunnel */
5788 tid = ntohl (tunnel_msg->tunnel_id);
5789 t = tunnel_get_by_local_id(c, tid);
5792 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, " tunnel %X not found\n", tid);
5794 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5798 switch (ntohs(message->type))
5800 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER:
5801 t->nobuffer = GNUNET_NO;
5803 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER:
5804 t->nobuffer = GNUNET_YES;
5810 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5815 * Handler for client traffic directed to one peer
5817 * @param cls closure
5818 * @param client identification of the client
5819 * @param message the actual message
5822 handle_local_unicast (void *cls, struct GNUNET_SERVER_Client *client,
5823 const struct GNUNET_MessageHeader *message)
5825 struct MeshClient *c;
5826 struct MeshTunnel *t;
5827 struct GNUNET_MESH_Unicast *data_msg;
5828 MESH_TunnelNumber tid;
5831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5832 "Got a unicast request from a client!\n");
5834 /* Sanity check for client registration */
5835 if (NULL == (c = client_get (client)))
5838 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5843 data_msg = (struct GNUNET_MESH_Unicast *) message;
5845 /* Sanity check for message size */
5846 size = ntohs (message->size);
5847 if (sizeof (struct GNUNET_MESH_Unicast) +
5848 sizeof (struct GNUNET_MessageHeader) > size)
5851 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5855 /* Tunnel exists? */
5856 tid = ntohl (data_msg->tid);
5857 t = tunnel_get_by_local_id (c, tid);
5861 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5865 /* Is it a local tunnel? Then, does client own the tunnel? */
5866 if (t->owner->handle != client)
5869 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5873 /* PID should be as expected */
5874 if (ntohl (data_msg->pid) != t->fwd_pid + 1)
5877 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5878 "Unicast PID, expected %u, got %u\n",
5879 t->fwd_pid + 1, ntohl (data_msg->pid));
5880 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5884 /* Ok, everything is correct, send the message
5885 * (pretend we got it from a mesh peer)
5888 /* Work around const limitation */
5889 char buf[ntohs (message->size)] GNUNET_ALIGN;
5890 struct GNUNET_MESH_Unicast *copy;
5892 copy = (struct GNUNET_MESH_Unicast *) buf;
5893 memcpy (buf, data_msg, size);
5894 copy->oid = my_full_id;
5895 copy->tid = htonl (t->id.tid);
5896 copy->ttl = htonl (default_ttl);
5897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5898 " calling generic handler...\n");
5899 handle_mesh_data_unicast (NULL, &my_full_id, ©->header);
5901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "receive done OK\n");
5902 GNUNET_SERVER_receive_done (client, GNUNET_OK);
5909 * Handler for client traffic directed to the origin
5911 * @param cls closure
5912 * @param client identification of the client
5913 * @param message the actual message
5916 handle_local_to_origin (void *cls, struct GNUNET_SERVER_Client *client,
5917 const struct GNUNET_MessageHeader *message)
5919 struct GNUNET_MESH_ToOrigin *data_msg;
5920 struct MeshTunnelClientInfo *clinfo;
5921 struct MeshClient *c;
5922 struct MeshTunnel *t;
5923 MESH_TunnelNumber tid;
5926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
5927 "Got a ToOrigin request from a client!\n");
5928 /* Sanity check for client registration */
5929 if (NULL == (c = client_get (client)))
5932 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
5937 data_msg = (struct GNUNET_MESH_ToOrigin *) message;
5939 /* Sanity check for message size */
5940 size = ntohs (message->size);
5941 if (sizeof (struct GNUNET_MESH_ToOrigin) +
5942 sizeof (struct GNUNET_MessageHeader) > size)
5945 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5949 /* Tunnel exists? */
5950 tid = ntohl (data_msg->tid);
5951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
5952 if (tid < GNUNET_MESH_LOCAL_TUNNEL_ID_SERV)
5955 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5958 t = tunnel_get_by_local_id (c, tid);
5961 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
5962 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
5964 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5968 /* It should be sent by someone who has this as incoming tunnel. */
5969 if (GNUNET_NO == client_knows_tunnel (c, t))
5972 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5976 /* PID should be as expected */
5977 clinfo = tunnel_get_client_fc (t, c);
5978 if (ntohl (data_msg->pid) != clinfo->bck_pid + 1)
5981 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
5982 "To Origin PID, expected %u, got %u\n",
5983 clinfo->bck_pid + 1,
5984 ntohl (data_msg->pid));
5985 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
5990 /* Ok, everything is correct, send the message
5991 * (pretend we got it from a mesh peer)
5994 char buf[ntohs (message->size)] GNUNET_ALIGN;
5995 struct GNUNET_MESH_ToOrigin *copy;
5997 /* Work around const limitation */
5998 copy = (struct GNUNET_MESH_ToOrigin *) buf;
5999 memcpy (buf, data_msg, size);
6000 GNUNET_PEER_resolve (t->id.oid, ©->oid);
6001 copy->tid = htonl (t->id.tid);
6002 copy->ttl = htonl (default_ttl);
6003 copy->pid = htonl (t->bck_pid + 1);
6005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6006 " calling generic handler...\n");
6007 handle_mesh_data_to_orig (NULL, &my_full_id, ©->header);
6009 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6016 * Handler for client's ACKs for payload traffic.
6018 * @param cls Closure (unused).
6019 * @param client Identification of the client.
6020 * @param message The actual message.
6023 handle_local_ack (void *cls, struct GNUNET_SERVER_Client *client,
6024 const struct GNUNET_MessageHeader *message)
6026 struct GNUNET_MESH_LocalAck *msg;
6027 struct MeshTunnel *t;
6028 struct MeshClient *c;
6029 MESH_TunnelNumber tid;
6032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got a local ACK\n");
6033 /* Sanity check for client registration */
6034 if (NULL == (c = client_get (client)))
6037 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id);
6042 msg = (struct GNUNET_MESH_LocalAck *) message;
6044 /* Tunnel exists? */
6045 tid = ntohl (msg->tunnel_id);
6046 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " on tunnel %X\n", tid);
6047 t = tunnel_get_by_local_id (c, tid);
6051 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tunnel %X unknown.\n", tid);
6052 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, " for client %u.\n", c->id);
6053 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6057 ack = ntohl (msg->max_pid);
6058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, " ack %u\n", ack);
6060 /* Does client own tunnel? I.E: Is this an ACK for BCK traffic? */
6061 if (NULL != t->owner && t->owner->handle == client)
6063 /* The client owns the tunnel, ACK is for data to_origin, send BCK ACK. */
6065 tunnel_send_bck_ack(t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
6069 /* The client doesn't own the tunnel, this ACK is for FWD traffic. */
6070 tunnel_set_client_fwd_ack (t, c, ack);
6071 tunnel_send_fwd_ack (t, GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK);
6074 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6082 * Iterator over all tunnels to send a monitoring client info about each tunnel.
6084 * @param cls Closure (client handle).
6085 * @param key Key (hashed tunnel ID, unused).
6086 * @param value Tunnel info.
6088 * @return GNUNET_YES, to keep iterating.
6091 monitor_all_tunnels_iterator (void *cls,
6092 const struct GNUNET_HashCode * key,
6095 struct GNUNET_SERVER_Client *client = cls;
6096 struct MeshTunnel *t = value;
6097 struct GNUNET_MESH_LocalMonitor *msg;
6099 msg = GNUNET_malloc (sizeof(struct GNUNET_MESH_LocalMonitor));
6100 GNUNET_PEER_resolve(t->id.oid, &msg->owner);
6101 msg->tunnel_id = htonl (t->id.tid);
6102 msg->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6103 msg->header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS);
6104 GNUNET_PEER_resolve (t->peer, &msg->destination);
6106 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6107 "* sending info about tunnel %s [%u]\n",
6108 GNUNET_i2s (&msg->owner), t->id.tid);
6110 GNUNET_SERVER_notification_context_unicast (nc, client,
6111 &msg->header, GNUNET_NO);
6117 * Handler for client's MONITOR request.
6119 * @param cls Closure (unused).
6120 * @param client Identification of the client.
6121 * @param message The actual message.
6124 handle_local_get_tunnels (void *cls, struct GNUNET_SERVER_Client *client,
6125 const struct GNUNET_MessageHeader *message)
6127 struct MeshClient *c;
6129 /* Sanity check for client registration */
6130 if (NULL == (c = client_get (client)))
6133 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6138 "Received get tunnels request from client %u\n",
6140 GNUNET_CONTAINER_multihashmap_iterate (tunnels,
6141 monitor_all_tunnels_iterator,
6143 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6144 "Get tunnels request from client %u completed\n",
6146 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6151 * Handler for client's MONITOR_TUNNEL request.
6153 * @param cls Closure (unused).
6154 * @param client Identification of the client.
6155 * @param message The actual message.
6158 handle_local_show_tunnel (void *cls, struct GNUNET_SERVER_Client *client,
6159 const struct GNUNET_MessageHeader *message)
6161 const struct GNUNET_MESH_LocalMonitor *msg;
6162 struct GNUNET_MESH_LocalMonitor *resp;
6163 struct MeshClient *c;
6164 struct MeshTunnel *t;
6166 /* Sanity check for client registration */
6167 if (NULL == (c = client_get (client)))
6170 GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
6174 msg = (struct GNUNET_MESH_LocalMonitor *) message;
6175 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6176 "Received tunnel info request from client %u for tunnel %s[%X]\n",
6179 ntohl (msg->tunnel_id));
6180 t = tunnel_get (&msg->owner, ntohl (msg->tunnel_id));
6183 /* We don't know the tunnel FIXME */
6184 struct GNUNET_MESH_LocalMonitor warn;
6187 GNUNET_SERVER_notification_context_unicast (nc, client,
6190 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6194 /* Initialize context */
6195 resp = GNUNET_malloc (sizeof (struct GNUNET_MESH_LocalMonitor));
6197 GNUNET_PEER_resolve (t->peer, &resp->destination);
6198 resp->header.size = htons (sizeof (struct GNUNET_MESH_LocalMonitor));
6199 GNUNET_SERVER_notification_context_unicast (nc, c->handle,
6200 &resp->header, GNUNET_NO);
6203 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6204 "Monitor tunnel request from client %u completed\n",
6206 GNUNET_SERVER_receive_done (client, GNUNET_OK);
6211 * Functions to handle messages from clients
6213 static struct GNUNET_SERVER_MessageHandler client_handlers[] = {
6214 {&handle_local_new_client, NULL,
6215 GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT, 0},
6216 {&handle_local_tunnel_create, NULL,
6217 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE,
6218 sizeof (struct GNUNET_MESH_TunnelMessage)},
6219 {&handle_local_tunnel_destroy, NULL,
6220 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY,
6221 sizeof (struct GNUNET_MESH_TunnelMessage)},
6222 {&handle_local_tunnel_buffer, NULL,
6223 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_BUFFER,
6224 sizeof (struct GNUNET_MESH_TunnelMessage)},
6225 {&handle_local_tunnel_buffer, NULL,
6226 GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_NOBUFFER,
6227 sizeof (struct GNUNET_MESH_TunnelMessage)},
6228 {&handle_local_unicast, NULL,
6229 GNUNET_MESSAGE_TYPE_MESH_UNICAST, 0},
6230 {&handle_local_to_origin, NULL,
6231 GNUNET_MESSAGE_TYPE_MESH_TO_ORIGIN, 0},
6232 {&handle_local_ack, NULL,
6233 GNUNET_MESSAGE_TYPE_MESH_LOCAL_ACK,
6234 sizeof (struct GNUNET_MESH_LocalAck)},
6235 {&handle_local_get_tunnels, NULL,
6236 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNELS,
6237 sizeof (struct GNUNET_MessageHeader)},
6238 {&handle_local_show_tunnel, NULL,
6239 GNUNET_MESSAGE_TYPE_MESH_LOCAL_INFO_TUNNEL,
6240 sizeof (struct GNUNET_MESH_LocalMonitor)},
6246 * To be called on core init/fail.
6248 * @param cls service closure
6249 * @param server handle to the server for this service
6250 * @param identity the public identity of this peer
6253 core_init (void *cls, struct GNUNET_CORE_Handle *server,
6254 const struct GNUNET_PeerIdentity *identity)
6257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core init\n");
6258 core_handle = server;
6259 if (0 != memcmp (identity, &my_full_id, sizeof (my_full_id)) ||
6262 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Wrong CORE service\n"));
6263 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6265 GNUNET_i2s (identity));
6266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6268 GNUNET_i2s (&my_full_id));
6269 GNUNET_SCHEDULER_shutdown (); // Try gracefully
6271 GNUNET_abort(); // Try harder
6278 * Method called whenever a given peer connects.
6280 * @param cls closure
6281 * @param peer peer identity this notification is about
6284 core_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
6286 struct MeshPeerInfo *peer_info;
6287 struct MeshPeerPath *path;
6289 DEBUG_CONN ("Peer connected\n");
6290 DEBUG_CONN (" %s\n", GNUNET_i2s (&my_full_id));
6291 peer_info = peer_info_get (peer);
6292 if (myid == peer_info->id)
6294 DEBUG_CONN (" (self)\n");
6299 DEBUG_CONN (" %s\n", GNUNET_i2s (peer));
6301 path = path_new (2);
6302 path->peers[0] = myid;
6303 path->peers[1] = peer_info->id;
6304 GNUNET_PEER_change_rc (myid, 1);
6305 GNUNET_PEER_change_rc (peer_info->id, 1);
6306 peer_info_add_path (peer_info, path, GNUNET_YES);
6307 GNUNET_STATISTICS_update (stats, "# peers", 1, GNUNET_NO);
6313 * Method called whenever a peer disconnects.
6315 * @param cls closure
6316 * @param peer peer identity this notification is about
6319 core_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
6321 struct MeshPeerInfo *pi;
6322 struct MeshPeerQueue *q;
6323 struct MeshPeerQueue *n;
6325 DEBUG_CONN ("Peer disconnected\n");
6326 pi = GNUNET_CONTAINER_multihashmap_get (peers, &peer->hashPubKey);
6336 /* TODO try to reroute this traffic instead */
6337 queue_destroy(q, GNUNET_YES);
6340 if (NULL != pi->core_transmit)
6342 GNUNET_CORE_notify_transmit_ready_cancel(pi->core_transmit);
6343 pi->core_transmit = NULL;
6345 peer_info_remove_path (pi, pi->id, myid);
6348 DEBUG_CONN (" (self)\n");
6350 GNUNET_STATISTICS_update (stats, "# peers", -1, GNUNET_NO);
6355 /******************************************************************************/
6356 /************************ MAIN FUNCTIONS ****************************/
6357 /******************************************************************************/
6360 * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
6362 * @param cls closure
6363 * @param key current key code
6364 * @param value value in the hash map
6365 * @return GNUNET_YES if we should continue to iterate,
6369 shutdown_tunnel (void *cls, const struct GNUNET_HashCode * key, void *value)
6371 struct MeshTunnel *t = value;
6378 * Iterator over peer hash map entries to destroy the tunnel during shutdown.
6380 * @param cls closure
6381 * @param key current key code
6382 * @param value value in the hash map
6383 * @return GNUNET_YES if we should continue to iterate,
6387 shutdown_peer (void *cls, const struct GNUNET_HashCode * key, void *value)
6389 struct MeshPeerInfo *p = value;
6390 struct MeshPeerQueue *q;
6391 struct MeshPeerQueue *n;
6399 queue_destroy(q, GNUNET_YES);
6403 peer_info_destroy (p);
6409 * Task run during shutdown.
6415 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
6417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
6419 if (core_handle != NULL)
6421 GNUNET_CORE_disconnect (core_handle);
6426 GNUNET_CRYPTO_ecc_key_create_stop (keygen);
6429 GNUNET_CONTAINER_multihashmap_iterate (tunnels, &shutdown_tunnel, NULL);
6430 GNUNET_CONTAINER_multihashmap_iterate (peers, &shutdown_peer, NULL);
6431 if (dht_handle != NULL)
6433 GNUNET_DHT_disconnect (dht_handle);
6438 GNUNET_SERVER_notification_context_destroy (nc);
6441 if (GNUNET_SCHEDULER_NO_TASK != announce_id_task)
6443 GNUNET_SCHEDULER_cancel (announce_id_task);
6444 announce_id_task = GNUNET_SCHEDULER_NO_TASK;
6446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shut down\n");
6451 * Callback for hostkey read/generation.
6453 * @param cls Closure (Configuration handle).
6454 * @param pk The ECC private key.
6455 * @param emsg Error message, if any.
6458 key_generation_cb (void *cls,
6459 struct GNUNET_CRYPTO_EccPrivateKey *pk,
6462 const struct GNUNET_CONFIGURATION_Handle *c = cls;
6463 struct MeshPeerInfo *peer;
6464 struct MeshPeerPath *p;
6469 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6470 _("Mesh service could not access hostkey: %s. Exiting.\n"),
6472 GNUNET_SCHEDULER_shutdown ();
6475 my_private_key = pk;
6476 GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
6477 GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key),
6478 &my_full_id.hashPubKey);
6479 myid = GNUNET_PEER_intern (&my_full_id);
6480 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6481 "Mesh for peer [%s] starting\n",
6482 GNUNET_i2s(&my_full_id));
6484 core_handle = GNUNET_CORE_connect (c, /* Main configuration */
6485 NULL, /* Closure passed to MESH functions */
6486 &core_init, /* Call core_init once connected */
6487 &core_connect, /* Handle connects */
6488 &core_disconnect, /* remove peers on disconnects */
6489 NULL, /* Don't notify about all incoming messages */
6490 GNUNET_NO, /* For header only in notification */
6491 NULL, /* Don't notify about all outbound messages */
6492 GNUNET_NO, /* For header-only out notification */
6493 core_handlers); /* Register these handlers */
6495 if (core_handle == NULL)
6498 GNUNET_SCHEDULER_shutdown ();
6503 next_local_tid = GNUNET_MESH_LOCAL_TUNNEL_ID_SERV;
6506 GNUNET_SERVER_add_handlers (server_handle, client_handlers);
6507 nc = GNUNET_SERVER_notification_context_create (server_handle, 1);
6508 GNUNET_SERVER_disconnect_notify (server_handle,
6509 &handle_local_client_disconnect, NULL);
6513 clients_tail = NULL;
6516 announce_id_task = GNUNET_SCHEDULER_add_now (&announce_id, cls);
6518 /* Create a peer_info for the local peer */
6519 peer = peer_info_get (&my_full_id);
6522 GNUNET_PEER_change_rc (myid, 1);
6523 peer_info_add_path (peer, p, GNUNET_YES);
6524 GNUNET_SERVER_resume (server_handle);
6525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Mesh service running\n");
6530 * Process mesh requests.
6532 * @param cls closure
6533 * @param server the initialized server
6534 * @param c configuration to use
6537 run (void *cls, struct GNUNET_SERVER_Handle *server,
6538 const struct GNUNET_CONFIGURATION_Handle *c)
6542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "starting to run\n");
6543 server_handle = server;
6546 GNUNET_CONFIGURATION_get_value_filename (c, "PEER", "PRIVATE_KEY",
6549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6551 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6552 "mesh", "peer/privatekey");
6553 GNUNET_SCHEDULER_shutdown ();
6558 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "REFRESH_PATH_TIME",
6559 &refresh_path_time))
6561 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6563 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6564 "mesh", "refresh path time");
6565 GNUNET_SCHEDULER_shutdown ();
6570 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "APP_ANNOUNCE_TIME",
6571 &app_announce_time))
6573 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6575 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6576 "mesh", "app announce time");
6577 GNUNET_SCHEDULER_shutdown ();
6580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
6581 "APP_ANNOUNCE_TIME %llu ms\n",
6582 app_announce_time.rel_value);
6584 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "ID_ANNOUNCE_TIME",
6587 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6589 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6590 "mesh", "id announce time");
6591 GNUNET_SCHEDULER_shutdown ();
6596 GNUNET_CONFIGURATION_get_value_time (c, "MESH", "CONNECT_TIMEOUT",
6599 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6601 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6602 "mesh", "connect timeout");
6603 GNUNET_SCHEDULER_shutdown ();
6608 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_MSGS_QUEUE",
6611 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6613 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6614 "mesh", "max msgs queue");
6615 GNUNET_SCHEDULER_shutdown ();
6620 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_TUNNELS",
6623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
6625 ("%s service is lacking key configuration settings (%s). Exiting.\n"),
6626 "mesh", "max tunnels");
6627 GNUNET_SCHEDULER_shutdown ();
6632 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DEFAULT_TTL",
6635 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6637 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6638 "mesh", "default ttl", 64);
6643 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
6646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6647 _("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6648 "mesh", "max peers", 1000);
6653 GNUNET_CONFIGURATION_get_value_number (c, "MESH", "DHT_REPLICATION_LEVEL",
6654 &dht_replication_level))
6656 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
6658 ("%s service is lacking key configuration settings (%s). Using default (%u).\n"),
6659 "mesh", "dht replication level", 3);
6660 dht_replication_level = 3;
6663 tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6664 incoming_tunnels = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6665 peers = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6666 types = GNUNET_CONTAINER_multihashmap_create (32, GNUNET_NO);
6668 dht_handle = GNUNET_DHT_connect (c, 64);
6669 if (NULL == dht_handle)
6673 stats = GNUNET_STATISTICS_create ("mesh", c);
6675 GNUNET_SERVER_suspend (server_handle);
6676 /* Scheduled the task to clean up when shutdown is called */
6677 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
6679 keygen = GNUNET_CRYPTO_ecc_key_create_start (keyfile,
6682 GNUNET_free (keyfile);
6687 * The main function for the mesh service.
6689 * @param argc number of arguments from the command line
6690 * @param argv command line arguments
6691 * @return 0 ok, 1 on error
6694 main (int argc, char *const *argv)
6699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main()\n");
6700 r = GNUNET_SERVICE_run (argc, argv, "mesh", GNUNET_SERVICE_OPTION_NONE, &run,
6702 ret = (GNUNET_OK == r) ? 0 : 1;
6703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "main() END\n");
6707 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
6708 "Mesh for peer [%s] FWD ACKs %u, BCK ACKs %u\n",
6709 GNUNET_i2s(&my_full_id), debug_fwd_ack, debug_bck_ack);